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

Recursion scheme for symbolic differentiation

Following terminology from this excellent series, let's represent an expression such as (1 + x^2 - 3x)^3 by a Term Expr, where the data types are the following: data Expr a = Var | Const Int | Plus a a | Mul a a | Pow a Int deriving…
nnnmmm
  • 7,964
  • 4
  • 22
  • 41
3
votes
4 answers

Specifying a function type signature in a where clause

The following function implements the good old filter function from lists by using the recursion-schemes library. import Data.Functor.Foldable catafilter :: (a -> Bool) -> [a] -> [a] catafilter p = cata alg where -- alg :: ListF a [a] ->…
Jogger
  • 1,617
  • 2
  • 12
  • 22
3
votes
2 answers

Expression expansion using recursion schemes

I have a data type representing arithmetic expressions: data E = Add E E | Mul E E | Var String I want to write an expansion function which will convert an expression into sum of products of variables (sort of braces expansion). Using recursion…
nponeccop
  • 13,527
  • 1
  • 44
  • 106
3
votes
2 answers

Corecursive fibonacci using recursion schemes

There is an elegant derinition of list of fibonacci numbers: fibs :: [Integer] fibs = fib 1 1 where fib a b = a : fib b (a + b) Can it be translated to use recursion-schemes library? The closest I could get is the following code that uses…
nponeccop
  • 13,527
  • 1
  • 44
  • 106
3
votes
0 answers

Applying a local match/rewrite globally using recursion schemes

{-# LANGUAGE FlexibleContexts, DeriveFoldable, TemplateHaskell, TypeFamilies, DeriveFunctor, DeriveTraversable #-} import Control.Applicative import Data.Functor.Foldable import Data.Functor.Foldable.TH import Data.Maybe I want to rewrite a…
nponeccop
  • 13,527
  • 1
  • 44
  • 106
3
votes
2 answers

Efficient implementation of Catamorphisms in Scala

For a datatype representing the natural numbers: sealed trait Nat case object Z extends Nat case class S(pred: Nat) extends Nat In Scala, here is an elementary way of implementing the corresponding catamorphism: def cata[A](z: A)(l: Nat)(f: A =>…
3
votes
2 answers

Haskell Higher Order Functions

I have a homework on higher order functions in Haskell and I'm having a little trouble getting started. If I could get some help and explanation on the first question, I'm confident I can finish the rest. Using higher order functions (map, fold, or…
3
votes
0 answers

How to mix CoFree into a F-algebra catamorphism?

Firstly, this builds on https://www.schoolofhaskell.com/user/bartosz/understanding-algebras so please read for context if unfamiliar with algebras and recursion schemes. Say I have a simple expression parser: data Expr a = ... -- whatever parse ::…
Golly
  • 1,319
  • 8
  • 18
3
votes
1 answer

Why is unused value produced by catamorphism evaluated?

I expected that the following code would run and exit immediately because p is never actually used, but instead, it runs for over 7 minutes and then is seemingly killed by the os. {-# LANGUAGE DeriveFunctor #-} import Control.Monad (liftM2) main =…
bwroga
  • 5,379
  • 2
  • 23
  • 25
3
votes
1 answer

Optimizing a Free Monad

If I have a value a: Free[Op, A], is it possible to "flatten" the structure of a so that two Ops that are bound together by the free monad may be collapsed into one? Context: I'd like to perform this as an optimization step before interpretation…
3
votes
1 answer

Haskell cata types

After reading (and implementing) part of http://blog.sumtypeofway.com/recursion-schemes-part-2/ I still wonder how the types in the cata function work. The cata function is defined as: mystery :: Functor f => (f a -> a) -> Term f -> a mystery f = f…
ondra
  • 9,122
  • 1
  • 25
  • 34
3
votes
1 answer

Having an issue extending Data.Functor.Foldable

This question uses the concepts/imports from http://hackage.haskell.org/package/recursion-schemes-4.0/docs/Data-Functor-Foldable.html I'm trying to extend this to thread a given monad through a catamorphism. Here is the code that I'm trying to…
Will
  • 1,711
  • 1
  • 12
  • 17
2
votes
1 answer

What do the generic type constraints ":<:" and ":+:" mean in this Scala example?

From this talk about nanopass compilers in 2017 (https://github.com/sellout/recursion-scheme-talk/blob/master/nanopass-compiler-talk.org) I found the code snippet below. In this code snipped, I see two generic constraints that I have searched high…
2
votes
1 answer

Selectively recurse into left or right subtree of a binary tree using a catamorphism (or any recursion scheme)

I'm trying to implement a binary search tree (or set) using fixed points of functors. I've defined my fixed point as follows: newtype Fix f = In (f (Fix f)) out :: Fix f -> f (Fix f) out (In f) = f -- Catamorphism type Algebra f a…
2
votes
2 answers

Recursion scheme allowing dependencies between recursive calls (an ordered catamorphism?)

I'm interested in a higher-order way (recursion scheme) to write recursive code in which there might be dependencies between recursive calls. As a simplified example, consider a function that traverses a tree of integers, checking if the sum is less…