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

107 questions
6
votes
1 answer

Unfolding non-empty structures to lists

I want to write Foldable.toList for a non-empty rose tree using an anamorphism, but it seems impossible to extract the last element: import Data.Functor.Foldable data RoseTree a = RoseNode a [RoseTree a] ana5 :: RoseTree a -> [a] ana5 = ana…
nponeccop
  • 13,527
  • 1
  • 44
  • 106
5
votes
1 answer

Memoizing a recursion scheme

Is it possible to memoize a recursion scheme? If so, how would you? For example, the following uses anamophism and catamorphism newtype Fix f = In (f (Fix f)) deriving instance (Eq (f (Fix f))) => Eq (Fix f) deriving instance (Ord (f (Fix f))) =>…
5
votes
1 answer

Haskell monadic parser with anamorphisms

My problem is how to combine the recursive, F-algebra-style recursive type definitions, with monadic/applicative-style parsers, in way that would scale to a realistic programming language. I have just started with the Expr definition below: data…
Matei
  • 152
  • 1
  • 5
5
votes
0 answers

Recursion scheme for tree-like structure

I'm trying to build the following recursion scheme: {-# LANGUAGE DeriveFunctor #-} import Data.Functor.Foldable import Control.Comonad import Control.Comonad.Cofree data Term a = Str String | Array [a] deriving (Show, Functor) type Tree = Fix…
5
votes
3 answers

Catamorphism that allows looking at part of the final result

Is there a name for a recursion scheme that's like a catamorphism, but that allows peeking at the final result while it's still running? Here's a slighly contrived example: toPercents :: Floating a => [a] -> [a] toPercents xs = result where …
5
votes
1 answer

What are the rules to compose f-algebras in a catamorphism

Here are some simple F-algebras for lists. They work with the cata function from the recursion-schemes library. import Data.Functor.Foldable algFilterSmall :: ListF Int [Int] -> [Int] algFilterSmall Nil = [] algFilterSmall (Cons x xs) = if x >=…
Jogger
  • 1,617
  • 2
  • 12
  • 22
5
votes
2 answers

How to write less boilerplate in a expression evaluator written with recursion-schemes

With the recursion-scheme library it's easy to write abstract syntax trees and the corresponding expression evaluators: {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE DeriveFunctor #-} {-# LANGUAGE DeriveFoldable #-} {-# LANGUAGE DeriveTraversable…
Jogger
  • 1,617
  • 2
  • 12
  • 22
5
votes
1 answer

What is Fokkinga's prepromorphism meant to do?

I've been looking at the recursion-schemes library, and I'm very confused about what prepro is supposed to be used for, or even what it does. The description of it as 'Fokkinga's prepromorphism' isn't very informative, and the signature (prepro ::…
Koz Ross
  • 3,040
  • 2
  • 24
  • 44
5
votes
2 answers

Proving the fusion law for unfold

I was reading Jeremy Gibbons' article on origami programming and I got stuck on exercise 3.7, which asks the reader to prove the fusion law for list unfolds: unfoldL p f g . h = unfoldL p' f' g' if p . h = p' f . h = f' g . h = h . g' The…
Dan Oneață
  • 968
  • 7
  • 14
5
votes
1 answer

Monoidal folds on fixed points

Given an arbitrary datastructure with a fixed point, can we construct an monoidal algebra without manually specifying all cases? Assume we are given the datatype Expr as below. Using the recursion-schemes library, we can derive a base functor ExprF,…
ThreeFx
  • 7,250
  • 1
  • 27
  • 51
5
votes
2 answers

Recursion schemes using `Fix` on a data-type that's already a Functor?

Still working on my text editor Rasa. At the moment I'm building out the system for tracking viewports/splits (similar to vim splits). It seemed natural to me to represent this structure as a tree: data Dir = Hor | Vert deriving…
5
votes
1 answer

Catamorphisms for Church-encoded lists

I want to be able to use cata from recursion-schemes package for lists in Church encoding. type ListC a = forall b. (a -> b -> b) -> b -> b I used a second rank type for convenience, but I don't care. Feel free to add a newtype, use GADTs, etc. if…
nponeccop
  • 13,527
  • 1
  • 44
  • 106
5
votes
1 answer

Avoid non-exhaustive pattern match when using para recursion-scheme

Below is some code which I wrote as an exercise for using para from recursion-schemes (I know that this reduced example could also be solved using just cata, but let's ignore this for this question). While doing this, I noticed that I have to do a…
bennofs
  • 11,873
  • 1
  • 38
  • 62
5
votes
2 answers

Chaining values with catamorphisms

Suppose I have definitions as follows (where cata is the catamorphism): type Algebra f a = f a -> a newtype Fix f = Fx (f (Fix f)) unFix :: Fix f -> f (Fix f) unFix (Fx x) = x cata :: Functor f => (f a -> a) -> Fix f -> a cata alg = alg . fmap…
4
votes
3 answers

Is this some kind of morphism in the recursion-schemes?

I implemented Euclid's algorithm in the following way at first. euclid x 0 = x euclid x y = euclid y (x `mod` y) The algorithm is tail-recursion, and I expect that it can be intuitively written with recursion-schemes. Then, the following euc is an…