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
2
votes
1 answer

Is it possible to create efficient recombining trees using (co)recursion?

By co-recursion I mean unfolding a tree, such as with anamorphism from Ed Kmett's recursion-schemes By re-combining trees I mean graphs that share structure. For example, a binomial option pricing tree, or Pascal's triangle. Both of these have some…
Luciano
  • 2,388
  • 1
  • 22
  • 33
2
votes
0 answers

How to prove that the Church encoding, forall r. (F r -> r) -> r, gives an initial algebra of the functor F?

The well-known Church encoding of natural numbers can be generalized to use an arbitrary functor F. The result is the type, call it C, defined by data C = Cfix { run :: forall r. (F r -> r) -> r } Here and below, for simplicity, we will assume…
2
votes
0 answers

Using recursive types vs parameterized types with recursion schemes in practice in Haskell

I've recently looked up some guides on recursion schemes in Haskell, however most articles don't go much beyond implementing the basic infrastructure required for these concepts. Given a recursive data type like a binary tree data Tree a = Leaf…
2
votes
1 answer

Top-down recursion schemes

Can we define a recursion scheme (without losing any of their generality) that constructs the value top-down, instead of bottom-up? This would be quite helpful as I've seen plenty of times where the function, defined internally with a recursion…
2
votes
2 answers

What is the type of apomorphism specific to list and how is it implemented?

I am learning recursion schemes and it has proven very helpful to me to implement them specific to the list type. However, I got stuck on apomorphisms. Here is an implementation of tails in terms of apo I recently found: import…
user5536315
2
votes
1 answer

Haskell type instance failed to resolve

I'm trying to create a Cofree structure by anamorphism, according to this post. But the compiler complains about type mismatch: Expected type: Base (Cofree Term E) (E, Fix Term) Actual type: CofreeF Term E (E, Fix Term) But at the source code of…
Jiaming Lu
  • 875
  • 6
  • 20
2
votes
1 answer

Haskell: Labeling an AST with type information using Algorithm W

We have an AST definition: data Term a = Lam String a | App a a | Var String deriving(Read,Show,Eq,Functor,Foldable,Traversable) And an F-Algebra for the type inference: type Wrapped m a = Enviroment -> m a type Result m = Wrapped…
2
votes
2 answers

Haskell Recursion Schemes: Label the tree with intermediate results

Using cata I can fold an AST to a result. With Cofree I can store additional annotations on the AST. How can I take an AST and return an annotated AST with the results at each step of the way? alg :: Term Result -> Result alg = undefined run :: Fix…
2
votes
1 answer

Haskell Recursion Schemes: Traverse two structures simultaneously

I'm trying to write Robinson's unification algorithm using recursion schemes. The unification algorithm takes two types and spits a result. A type is a: data TypeF a = TypeApplication a a | TypeVariable Name deriving…
user47376
  • 2,263
  • 3
  • 21
  • 26
2
votes
1 answer

How to transform a tree using a futumorphism in PureScript?

I have the following data type and example equation that I want to transform with a futumorphism... import Matryoshka as M import Data.Functor.Nu (Nu(..), observe, unfold) data ArithmeticF a = Mult a a | Div a a | Add a a | Num Number type…
Albtzrly
  • 924
  • 1
  • 6
  • 15
2
votes
1 answer

Recursion issue when writing a "Pretext" aware version of Lens.para

I've been trying to build a replacement for Lens.para that provides lensed contexts to the para function as it does its work. However, I seem to have made an error in the recursion somewhere. According to my understanding of it, Lens.para is a…
Julian Leviston
  • 1,646
  • 10
  • 21
2
votes
1 answer

Factoring out recursion in a complex AST

For a side project I am working on I currently have to deal with an abstract syntax tree and transform it according to rules (the specifics are unimportant). The AST itself is nontrivial, meaning it has subexpressions which are restricted to some…
ThreeFx
  • 7,250
  • 1
  • 27
  • 51
2
votes
1 answer

How does gpostpro "escape from the monad"?

I am trying to understand how this very abstract recursive function from the Haskell recursion-schemes package works (or, indeed, what it does!) - from this file: class Functor (Base t) => Corecursive t where [...] -- | A generalized…
Robin Green
  • 32,079
  • 16
  • 104
  • 187
1
vote
1 answer

Modeling a dependent computation task?

I need to model a computation task and some sub-tasks depend on it: First I run a task, if it fails then it's over. If it succeeds then run a bunch of sub-tasks(zero or many), any of them can fail or succeed, and can run zero or many sub-sub-tasks…
1
vote
1 answer

Using a paramorphism inside of an apomorphism

I'm trying to use paramorphisms and apomorhisms (in haskell): -- Fixed point of a Functor newtype Fix f = In (f (Fix f)) …
cocorudeboy
  • 119
  • 5