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

Alpha Beta Pruning with Recursion Schemes

I'm trying to get more proficient with recursion schemes as they have so far been really helpful for turning gnarly explicit recursion code into something less spike-y. One of the other tools I tend to reach for when implementing algorithms that can…
8
votes
0 answers

Significant overhead implementing Catalan numbers with histomorphism

I'm exploring recursion-schemes recently and want to find some use cases for histomorphism - for which I think Catalan numbers could be a fun one (I'm aware there are better ways to implement Catalan numbers, which are not the focus of this…
Javran
  • 3,394
  • 2
  • 23
  • 40
8
votes
1 answer

How to use recursion-schemes to `cata` two mutually-recursive types?

I started with this type for leaf-valued trees with labeled nodes: type Label = String data Tree a = Leaf Label a | Branch Label [Tree a] I have some folds I'd like to write over this tree, and they all take the form of catamorphisms,…
amalloy
  • 89,153
  • 8
  • 140
  • 205
7
votes
2 answers

Can I write `foldr` (or `foldMap`) in terms of 'recursion schemes' `cata`?

I recently read about recursion schemes where catamorphisms are described as analogous to generalized foldr. Is is possible to write an instance of Foldable (via either foldr or foldMap) in terms of cata in all cases?
Michael Thomas
  • 1,354
  • 7
  • 18
7
votes
3 answers

How does compiler figure out fixed point of a functor and how cata work at leaf level?

I feel like understanding the abstract concept of fixed point of a functor, however, I am still struggling to figure out the exact implementation of it and its catamorphism in Haskell. For example, if I define, as according to the book of "Category…
7
votes
1 answer

Deforestation in a Hylomorphism

Wikipedia writes about Hylomorphism: In [...] functional programming a hylomorphism is a recursive function, corresponding to the composition of an anamorphism (which first builds a set of results; also known as 'unfolding') followed by a…
Jogger
  • 1,617
  • 2
  • 12
  • 22
7
votes
1 answer

No instance for (Data.Functor.Classes.Show1 ExprF)

I've written a small program to find the prime factorization of a number. Everything seems to compile except for the main function, which complains about not being able to find a Show1 instance. {-# LANGUAGE DeriveFunctor #-} module…
Ian Macalinao
  • 1,608
  • 3
  • 20
  • 30
7
votes
1 answer

How to work with AST with Cofree annotation?

I have this simple Expr AST and I can easily convert it to String. import Prelude hiding (Foldable) import qualified Prelude import Data.Foldable as F import Data.Functor.Foldable import Data.Monoid import Control.Comonad.Cofree data ExprF r =…
ais
  • 2,514
  • 2
  • 17
  • 24
7
votes
1 answer

In what way is Scala's Option fold a catamorphism?

The answer to this question suggests that the fold method on Option in Scala is a catamoprhism. From the wikipedia a catamophism is "the unique homomorphism from an initial algebra into some other algebra. The concept has been applied to functional…
6
votes
2 answers

`refold :: Functor s => (a -> s a, a) -> (s b -> b) -> b` as a morphism between universal types

Various recursion scheme boil down to specific instantiation of refold refold :: Functor s => (s b -> b) -> (a -> s a) -> a -> b refold f g = go where go a = f (fmap go (g a)) What is the meaningful interpretation of refold ? The data type data Nu…
6
votes
1 answer

How to update a structure with recursion schemes?

In recursion schemes, how can I construct something with type definition like (Recursive t, CoRecursive t) -> t -> ? -> t I try to use recursion-schemes to update nodes. Taking list as an example, I can come up with two methods like: update :: [a]…
Zhu Jinxuan
  • 406
  • 2
  • 8
6
votes
2 answers

How can I walk this type with a recursion scheme instead of explicit recursion?

Consider this code: import Data.Maybe (fromMaybe) data MyStructure = Foo Int | Bar String MyStructure | Baz MyStructure MyStructure | Qux Bool Bool MyStructure MyStructure deriving(Eq,Show) makeReplacements :: [(MyStructure, MyStructure)] ->…
6
votes
1 answer

Infinite recursive types in Coq (for Bananas and Lenses)

I'd like to see a Coq version of the Bananas, Lenses, etc. They are built up in the excellent series of blog posts at sumtypeofway Introduction to Recursion schemes However, the blog post is in Haskell, which permits infinite non-terminating…
larsr
  • 5,447
  • 19
  • 38
6
votes
1 answer

How can I use a recursion scheme to express this probability distribution in Haskell

This question is part theory / part implementation. Background assumption: I'm using the monad-bayes library to represent probability distributions as monads. A distribution p(a|b) can be represented as a function MonadDist m => b -> m a. Suppose I…
6
votes
2 answers

Algorithm W using recursion schemes

I wanted to be able to formulate the hindley-milner type inference algorithm using fix-point data types and recursion schemes. Disregarding all detail apart from the actual recursion parts: w env term = case term of Lam n e -> lam (w (modify1…