Questions tagged [catamorphism]

Use catamorphism in questions to refer to functions which recursively iterate through a data structure to return a new structure. The new structure is derived from the process of applying one or more functions to the data at each iteration along with the result of previous iterations.

References

55 questions
35
votes
2 answers

Are Ana-/Catamorphisms just slower?

After writing this article I decided to put my money where my mouth is and started to convert a previous project of mine to use recursion-schemes. The data structure in question is a lazy kdtree. Please have a look at the implementations with…
fho
  • 6,787
  • 26
  • 71
27
votes
5 answers

What is a catamorphism and can it be implemented in C# 3.0?

I'm trying to learn about catamorphisms and I've read the Wikipedia article and the first couple posts in the series of the topic for F# on the Inside F# blog. I understand that it's a generalization of folds (i.e., mapping a structure of many…
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
23
votes
1 answer

Is it possible to make GHC optimize (deforest) generic functions such as catamorphisms?

I really like the idea of working with catamorphisms/anamorphisms in a generic way, but it seems to me it has a significant performance drawback: Suppose we want to work with a tree structure in the categorical way - to describe different folding…
Petr
  • 62,528
  • 13
  • 153
  • 317
18
votes
1 answer

What's the type of a catamorphism (fold) for non-regular recursive types?

Many catamorphisms seem to be simple enough, mostly replacing each data constructor with a custom function, e.g. data Bool = False | True foldBool :: r -- False constructor -> r -- True constructor -> Bool…
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
17
votes
4 answers

When is a composition of catamorphisms a catamorphism?

From page 3 of http://research.microsoft.com/en-us/um/people/emeijer/Papers/meijer94more.pdf: it is not true in general that catamorphisms are closed under composition Under what conditions do catamorphisms compose to a catamorphism? More…
16
votes
2 answers

Catamorphism and tree-traversing in Haskell

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…
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
15
votes
1 answer

Catamorphism in F#

I'm reading the wikipedia article about catamorphisms and for the moment I was able to reproduce the Haskell examples in F# except for this part : type Algebra f a = f a -> a -- the generic f-algebras newtype Fix f = Iso { invIso :: f (Fix f) } --…
Mario
  • 313
  • 2
  • 11
14
votes
1 answer

Recursion Schemes in Agda

Needless to say, the standard construction in Haskell newtype Fix f = Fix { getFix :: f (Fix f) } cata :: (Functor f) => (f a -> a) -> Fix f -> a cata f = f . fmap (cata f) . getFix is awesome and extremely useful. Trying to define a similar…
luqui
  • 59,485
  • 12
  • 145
  • 204
12
votes
1 answer

How to implement fixed points of functors in Java

I recently discovered how to simulate higher order types in Java in a somewhat roundabout way like so interface H { } Here H encodes a higher order type that takes a type parameter F which itself takes parameter T. Now this leaves me to…
michid
  • 10,536
  • 3
  • 32
  • 59
12
votes
2 answers

Does each type have a unique catamorphism?

Recently I've finally started to feel like I understand catamorphisms. I wrote some about them in a recent answer, but briefly I would say a catamorphism for a type abstracts over the process of recursively traversing a value of that type, with the…
amalloy
  • 89,153
  • 8
  • 140
  • 205
12
votes
2 answers

How to make catamorphisms work with parameterized/indexed types?

I recently learned a bit about F-algebras: https://www.fpcomplete.com/user/bartosz/understanding-algebras. I wanted to lift this functionality to more advanced types (indexed and higher-kinded). Furthermore, I checked "Giving Haskell a Promotion"…
Mathijs Kwik
  • 1,227
  • 9
  • 12
11
votes
2 answers

What's the relation of fold on Option, Either etc and fold on Traversable?

Scalaz provides a method named fold for various ADTs such as Boolean, Option[_], Validation[_, _], Either[_, _] etc. This method basically takes functions corresponding to all possible cases for that given ADT. In other words, a pattern match shown…
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
11
votes
1 answer

What is the connection between primitive recursion and catamorphisms?

Using the following catamorphism for natural numbers I can implement various arithmetic algorithms whithout having to deal with recursion: cataNat :: b -> (b -> b) -> Natural -> b cataNat zero succ = go where go n = if (n <= 0) then zero else…
user5536315
11
votes
2 answers

Is it possible to compare two trees with recursion schemes?

I have this AST data ExprF r = Const Int | Add r r type Expr = Fix ExprF and I want to compare x = Fix $ Add (Fix (Const 1)) (Fix (Const 1)) y = Fix $ Add (Fix (Const 1)) (Fix (Const 2)) But all recursion schemes functions seems to work only…
ais
  • 2,514
  • 2
  • 17
  • 24
11
votes
1 answer

A library implementation of a recursion scheme

I 'invented' a recursion scheme which is a generalization of catamorphism. When you fold a data structure with catamorphism you don't have access to subterms, only to subresults of folding: {-# LANGUAGE DeriveFunctor #-} import qualified Data.Map as…
nponeccop
  • 13,527
  • 1
  • 44
  • 106
1
2 3 4