Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3612232
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:58:26+00:00 2026-05-18T21:58:26+00:00

I am impatient, looking forward to understanding catamorphism related to this SO question :)

  • 0

I am impatient, looking forward to understanding catamorphism related to this SO question 🙂

I have only practiced the beginning of Real World Haskell tutorial. So, Maybe I’m gonna ask for way too much right now, if it was the case, just tell me the concepts I should learn.

Below, I quote the wikipedia code sample for catamorphism.

I would like to know your opinion about foldTree below, a way of traversing a Tree, compared to this other SO question and answer, also dealing with traversing a Tree n-ary tree traversal. (independantly from being binary or not, I think the catamorphism below can be written so as to manage n-ary tree)

I put in comment what I understand, and be glad if you could correct me, and clarify some things.

{-this is a binary tree definition-}
data Tree a = Leaf a
            | Branch (Tree a) (Tree a)

{-I dont understand the structure between{} 
however it defines two morphisms, leaf and branch 
leaf take an a and returns an r, branch takes two r and returns an r-} 
data TreeAlgebra a r = TreeAlgebra { leaf   :: a      -> r
                                   , branch :: r -> r -> r }

{- foldTree is a morphism that takes: a TreeAlgebra for Tree a with result r, a Tree a
and returns an r -} 
foldTree :: TreeAlgebra a r -> Tree a -> r
foldTree a@(TreeAlgebra {leaf   = f}) (Leaf   x  ) = f x
foldTree a@(TreeAlgebra {branch = g}) (Branch l r) = g (foldTree a l) (foldTree a r)

at this point I am having many difficulties, I seem to guess that the morphism leaf
will be applied to any Leaf
But so as to use this code for real, foldTree needs to be fed a defined TreeAlgebra,
a TreeAlgebra that has a defined morphism leaf so as to do something ?
but in this case in the foldTree code I would expect {f = leaf} and not the contrary

Any clarification from you would be really welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:58:27+00:00Added an answer on May 18, 2026 at 9:58 pm

    Not exactly sure what you’re asking. But yeah, you feed a TreeAlgebra to foldTree corresponding to the computation you want to perform on the tree. For example, to sum all the elements in a tree of Ints you would use this algebra:

    sumAlgebra :: TreeAlgebra Int Int
    sumAlgebra = TreeAlgebra { leaf = id
                             , branch = (+) }
    

    Which means, to get the sum of a leaf, apply id (do nothing) to the value in the leaf. To get the sum of a branch, add together the sums of each of the children.

    The fact that we can say (+) for branch instead of, say, \x y -> sumTree x + sumTree y is the essential property of the catamorphism. It says that to compute some function f on some recursive data structure it suffices to have the values of f for its immediate children.

    Haskell is a pretty unique language in that we can formalize the idea of catamorphism abstractly. Let’s make a data type for a single node in your tree, parameterized over its children:

    data TreeNode a child
        = Leaf a
        | Branch child child
    

    See what we did there? We just replaced the recursive children with a type of our choosing. This is so that we can put the subtrees’ sums there when we are folding.

    Now for the really magical thing. I’m going to write this in pseudohaskell — writing it in real Haskell is possible, but we have to add some annotations to help the typechecker which can be kind of confusing. We take the “fixed point” of a parameterized data type — that is, constructing a data type T such that T = TreeNode a T. They call this operator Mu.

    type Mu f = f (Mu f)
    

    Look carefully here. The argument to Mu isn’t a type, like Int or Foo -> Bar. It’s a type constructor like Maybe or TreeNode Int — the argument to Mu itself takes an argument. (The possibility of abstracting over type constructors is one of the things that makes Haskell’s type system really stand out in its expressive power).

    So the type Mu f is defined as taking f and filling in its type parameter with Mu f itself. I’m going to define a synonym to reduce some of the noise:

    type IntNode = TreeNode Int
    

    Expanding Mu IntNode, we get:

    Mu IntNode = IntNode (Mu IntNode)
               = Leaf Int | Branch (Mu IntNode) (Mu IntNode)
    

    Do you see how Mu IntNode is equivalent to your Tree Int? We have just torn the recursive structure apart and then used Mu to put it back together again. This gives us the advantage that we can talk about all Mu types at once. This gives us what we need to define a catamorphism.

    Let’s define:

    type IntTree = Mu IntNode
    

    I said the essential property of the catamorphism is that to compute some function f, it suffices to have the values of f for its immediate children. Let’s call the type of the thing we are trying to compute r, and the data structure node (IntNode would be a possible instantiation of this). So to compute r on a particular node, we need the node with its children replaced with their rs. This computation has type node r -> r. So a catamorphism says that if we have one of these computations, then we can compute r for the entire recursive structure (remember recursion is denoted explicitly here with Mu):

    cata :: (node r -> r) -> Mu node -> r
    

    Making this concrete for our example, this looks like:

    cata :: (IntNode r -> r) -> IntTree -> r
    

    Restating, if we can take a node with rs for its children and compute an r, then we can compute an r for an entire tree.

    In order to actually compute this, we need node to be a Functor — that is we need to be able to map an arbitrary function over the children of a node.

    fmap :: (a -> b) -> node a -> node b
    

    This can be done straightforwardly for IntNode.

    fmap f (Leaf x) = Leaf x                  -- has no children, so stays the same
    fmap f (Branch l r) = Branch (f l) (f r)  -- apply function to each child
    

    Now, finally, we can give a definition for cata (the Functor node constraint just says that node has a suitable fmap):

    cata :: (Functor node) => (node r -> r) -> Mu node -> r
    cata f t = f (fmap (cata f) t)
    

    I used the parameter name t for the mnemonic value of “tree”. This is an abstract, dense definition, but it is really very simple. It says: recursively perform cata f — the computation we are doing over the tree — on each of t‘s children (which are themselves Mu nodes) to get a node r, and then pass that result to f compute the result for t itself.

    Tying this back to the beginning, the algebra you are defining is essentially a way of defining that node r -> r function. Indeed, given a TreeAlgebra, we can easily get the fold function:

    foldFunction :: TreeAlgebra a r -> (TreeNode a r -> r)
    foldFunction alg (Leaf a) = leaf alg a
    foldFunction alg (Branch l r) = branch alg l r
    

    Thus the tree catamorphism can be defined in terms of our generic one as follows:

    type Tree a = Mu (TreeNode a)
    
    treeCata :: TreeAlgebra a r -> (Tree a -> r)
    treeCata alg = cata (foldFunction alg)
    

    I’m out of time. I know that got really abstract really fast, but I hope it at least gave you a new viewpoint to help your learning. Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am following the hints to this question , but I'm impatient and I
Alright I know this isn't 100% related to programming (the Excel book in question
For the impatient: I want to have a navigationcontroller who's root viewcontroller is a
While going through the book Scala for the Impatient , I came across this
I asked this at the GWT forum but I'm impatient for the answer and
Consider this snippet of Jade: if(#{episode[question.name][field]}) #{episode[question.name][field]} I want to check if the value
I was pondering (and therefore am looking for a way to learn this, and
If the user gets impatient and touches this button before the next view can
I know this is a low-level question but, not being a database person, I
This is a ASP.NET MVC beginner question (I'm in phase of developing NerdDinner )...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.