Questions tagged [recursion-schemes]

Recursion schemes are reusable patterns for making recursive calls. They include catamorphisms and anamorphisms. This tag covers both the general concept, and the Haskell library of the same name.

Recursion schemes are reusable patterns for making recursive calls. They include catamorphisms and anamorphisms. This tag covers both the general concept, and the Haskell library of the same name which implements them using higher-order functions and type families.

cf. the recursion-schemes package.

See also

87 questions
345
votes
4 answers

What does "coalgebra" mean in the context of programming?

I have heard the term "coalgebras" several times in functional programming and PLT circles, especially when the discussion is about objects, comonads, lenses, and such. Googling this term gives pages that give mathematical description of these…
85
votes
3 answers

Recursion schemes for dummies?

I'm looking for some really simple, easy-to-grasp explanations of recursion schemes and corecursion schemes (catamorphisms, anamorphisms, hylomorphisms etc.) which do not require following lots of links, or opening a category theory textbook. I'm…
Robin Green
  • 29,408
  • 13
  • 94
  • 178
38
votes
3 answers

Histomorphisms, Zygomorphisms and Futumorphisms specialised to lists

I ended up figuring it out. See the video and slides of a talk I gave: slides/pdf video Original question: In my effort to understand generic recursion schemes (i.e., that use Fix) I have found it useful to write list-only versions of the various…
haroldcarr
  • 1,431
  • 13
  • 17
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,578
  • 19
  • 63
27
votes
1 answer

What is the difference between Fix, Mu and Nu in Ed Kmett's recursion scheme package

In Ed Kmett's recursion-scheme package, there are three declarations: newtype Fix f = Fix (f (Fix f)) newtype Mu f = Mu (forall a. (f a -> a) -> a) data Nu f where Nu :: (a -> f a) -> a -> Nu f What is the difference between those three data…
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
  • 94,042
  • 31
  • 216
  • 230
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
  • 81,733
  • 18
  • 105
  • 196
12
votes
2 answers

Once I have an F-Algebra, can I define Foldable and Traversable in terms of it?

I have defined an F-Algebra, as per Bartosz Milewski's articles (one, two): (This is not to say my code is an exact embodiment of Bartosz's ideas, it's merely my limited understanding of them, and any faults are mine alone.) module Algebra…
Ignat Insarov
  • 4,444
  • 13
  • 34
12
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
  • 57,324
  • 7
  • 134
  • 191
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,236
  • 1
  • 13
  • 19
11
votes
2 answers

List filter using an anamorphism

I implemented a broken filter function using an anamorphism from recursion-schemes Hackage library: import Data.Functor.Foldable xfilter :: (a -> Bool) -> [a] -> [a] xfilter f = ana $ project . phi f phi :: (a -> Bool) -> [a] -> [a] phi f (h : t)…
nponeccop
  • 13,141
  • 1
  • 39
  • 101
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,141
  • 1
  • 39
  • 101
10
votes
4 answers

How do I give a Functor instance to a datatype built for general recursion schemes?

I have a recursive datatype which has a Functor instance: data Expr1 a = Val1 a | Add1 (Expr1 a) (Expr1 a) deriving (Eq, Show, Functor) Now, I'm interested in modifying this datatype to support general recursion schemes, as they are described…
hugomg
  • 63,082
  • 19
  • 144
  • 230
9
votes
1 answer

What is an anamorphism, and how does one look like in C#?

I am trying to wrap my head around the concept of anamorphism. In functional programming, an anamorphism is a generalization of the concept of unfolds on lists. Formally, anamorphisms are generic functions that can corecursively construct a result…
8
votes
1 answer

Fix and Mu isomorphic

In the recursion-schemes package the following types are defined: newtype Fix f = Fix (f (Fix f)) newtype Mu f = Mu (forall a. (f a -> a) -> a) Are they isomorphic? If so, how do you prove it?
marcosh
  • 8,079
  • 2
  • 41
  • 64
1
2 3 4 5 6