<p>The theory behind recursion schemes is that algebraic data types are initial algebras.</p>
<h2>1. Recursive types and folds</h2>
<p>The programmer's point of view is that, given a recursive type,</p>
<pre><code>data Tree = Node Tree Tree | Leaf
</code></pre>
<p>... there is a standard way to consume it, which is a <em>fold</em>:</p>
<pre><code>fold :: (a -&gt; a -&gt; a) -&gt; a -&gt; Tree -&gt; a
fold node leaf (Node l r) = node (fold node leaf l) (fold node leaf r)
fold node leaf Leaf = leaf
</code></pre>
<p><code>fold</code> pattern-matches on a tree, makes recursive calls on the children, and combines the result somehow. The point is that:</p>
<ol>
<li>many recursive functions fit this mold (including factorial; it's not the best example, but for that exact reason it's a good exercise for the reader to (1) write the fold for Peano <code>Nat</code>; (2) define <code>factorial</code> using <code>fold</code>)</li>
<li>if you disallow explicit recursion, and program only using folds, then your functions are guaranteed to terminate.</li>
</ol>
<p>See also <a href="https://ris.utwente.nl/ws/portalfiles/portal/6142049/meijer91functional.pdf" rel="nofollow noreferrer">Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire</a>. It is the inspiration for the <a href="https://hackage.haskell.org/package/recursion-schemes" rel="nofollow noreferrer">recursion-schemes</a> library in Haskell, <a href="https://hackage.haskell.org/package/recursion-schemes-5.0.1" rel="nofollow noreferrer">whose description used to read</a> &quot;Generalized bananas, lenses and barbed wire&quot;.</p>
<h2>2. Initial algebras</h2>
<p>The mathematician's point of view is that a data type such as <code>Tree</code> is a language, i.e., a set/type of expressions:</p>
<pre><code>Tree :: Type
</code></pre>
<p>Constructors are the words of that language:</p>
<pre><code>Node :: Tree -&gt; Tree -&gt; Tree
Leaf :: Tree
</code></pre>
<p>Given a semantic domain <code>a</code>, and an interpretation of those words in that domain:</p>
<pre><code>node :: a -&gt; a -&gt; a
leaf :: a
</code></pre>
<p>... we obtain an interpretation of whole expressions, which is precisely a <code>fold</code>:</p>
<pre><code>fold node leaf :: Tree -&gt; a
</code></pre>
<p>An &quot;interpretation&quot; is also called an algebra, or algebraic structure:</p>
<blockquote>
<p>In mathematics, an algebraic structure consists of a nonempty set A (called the underlying set, carrier set or domain), [and] a collection of operations on A (typically binary operations such as addition and multiplication) (...).</p>
<p>--- <a href="https://en.wikipedia.org/wiki/Algebraic_structure" rel="nofollow noreferrer">Algebraic structure</a>, Wikipedia</p>
</blockquote>
<p>In the case of binary trees, the structures they describe are called <a href="https://en.wikipedia.org/wiki/Magma_(algebra)" rel="nofollow noreferrer">magmas</a>. Most other data types don't correspond to a notable class of algebraic structures, if they have a name at all. So the individual structures are not what's interesting about this point of view. Instead, meditate on the idea that words don't have intrinsic value, and their worth is in their meaning.</p>
<p>A language, can be viewed as a trivial interpretation---an algebra---of itself.</p>
<pre><code>fold Node Leaf :: Tree -&gt; Tree
= id
</code></pre>
<p>And in the category of algebras, it is an initial object, also called &quot;free algebra&quot;. It is the most general algebra (for a given signature, here of <code>node</code> and <code>leaf</code>): it can be mapped (in a respectful manner, i.e., via a homomorphism) into any other algebra, and there is only one way to do so, using <code>fold</code>.</p>
<blockquote>
<p>The basic method of introducing a new concrete data type, as in a number of
other languages, is to declare a free algebra.</p>
<p>--- <a href="https://www.cs.kent.ac.uk/people/staff/dat/miranda/nancypaper.pdf" rel="nofollow noreferrer">Miranda: A non-strict functional
language with polymorphic types</a>, D. A. Turner, 1985</p>
</blockquote>
<h2>3. Initial F-algebras</h2>
<p>The notion of <a href="https://ncatlab.org/nlab/show/algebra+for+an+endofunctor" rel="nofollow noreferrer">F-algebra</a> in category theory generalizes the idea of algebras further.
As mentioned above, an algebra is a set <code>a</code> equipped with some operations.</p>
<pre><code>node :: a -&gt; a -&gt; a
leaf :: a
</code></pre>
<p>This can be rearranged into an F-algebra: a set <code>a</code> equipped with one morphism of this form:</p>
<pre><code>alg :: F a -&gt; a
</code></pre>
<p>for some endofunctor <code>F</code> which encodes the shape of the operations. Here, for <code>node</code> and <code>leaf</code>, the relevant functor is <code>F a = (a × a) + 1</code> (in Haskell, <code>×</code> is the tuple type <code>(,)</code>, <code>+</code> is <code>Either</code>, <code>1</code> is the unit type <code>()</code>).</p>
<pre><code>alg :: (a × a) + 1 -&gt; a

-- gives the same information as ---

node :: a -&gt; a -&gt; a
leaf :: a
</code></pre>
<p>The point is that the definition of an algebra is seemingly tied to the notion of sets: &quot;a SET equipped with operations&quot;). In contrast, we can talk about F-algebras in any category, for example in a category of <a href="https://en.wikipedia.org/wiki/Domain_theory" rel="nofollow noreferrer">domains</a>. Whereas the category of sets has total functions, domains give semantics to programming languages with nontermination. We don't need to reinvent universal algebra---where algebras are traditionally carried by sets---from sets to domain theory or whatever, because instead we can prove general theorems about F-algebras in any category and then apply them to the relevant category.</p>
<p>To sum up, folds are a principled way of implementing recursive functions, in many senses:</p>
<ol>
<li><p>if you only use folds, avoiding unstructured recursion, programs are guaranteed to terminate. This eliminates the class of mistakes &quot;accidental infinite loop&quot;.</p>
</li>
<li><p>Folds play a natural role in programming language theory: a fold gives a compositional intepretation of a language in terms of its individual constructors. It can also be quite illuminating to to go the other way, viewing arbitrary data types as languages. I would argue that it can be a good perspective from which to appreciate general ideas such as Church encodings or <a href="https://blog.sigplan.org/2019/12/30/defunctionalization-everybody-does-it-nobody-talks-about-it/" rel="nofollow noreferrer">defunctionalization</a>.</p>
</li>
<li><p>Data types and their folds can be represented as initial F-algebras and catamorphisms, which make sense in categories which may model your programming language more accurately than sets and total functions, with the prime example of domains for modelling nontermination.</p>
</li>
<li><p>What about final F-coalgebras, aka., unfolds? This is getting too long so I'll briefly make the following points.</p>
<p>a. F-coalgebras <code>s -&gt; F s</code> can be thought of as an extremely general class of state machines, using the functor <code>F</code> to encode various forms of inputs and outputs. Unfolding an F-coalgebra turns it into an infinite tree, which may be more intuitive to manipulate.</p>
<p>b. <code>fold</code> and <code>unfold</code> recover Turing-completess. And now you have algebraically inspired primitives instead of unstructured loops. A possible application is that optimizations like deforestation (removing intermediate data structures) become &quot;just&quot; an exercise in equational reasoning.</p>
</li>
</ol>

CC BY-SA 4.0
2022-11-12T00:02:59.177Z
2022-11-12T10:26:39.707Z
2022-11-12T10:26:39.707Z
Li-yao Xia
<p>(I'm going to try to write an answer for functional programmers, with Haskell-like code.)</p>
<p>First, you should know that using higher-order functions, recursive definitions can be turned into <em>fixed-point equations</em>. Typically, defining</p>
<pre><code>fac :: Int -&gt; Int
fac n = if n &lt;= 0 then 1 else n * fact (n-1)
</code></pre>
<p>amounts to asking that <code>fac</code> satisfies the fixed-point equation <code>fac == f fac</code> (in other words that <code>fac</code> <em>is a fixed point of</em> <code>f</code>), for</p>
<pre><code>f :: (Int -&gt; Int) -&gt; Int -&gt; Int
f rec n = if n &lt;= 0 then 1 else n * rec (n-1)
</code></pre>
<p>This <code>f</code> has a completely non-circular definition, so to give meaning to recursive definitions it suffices to have a &quot;fixed-point operator&quot;</p>
<pre><code>fix :: (a -&gt; a) -&gt; a
</code></pre>
<p>that guarantees <code>fix g == g (fix g)</code> for every function <code>g</code>, because then we can define <code>fact = fix f</code> (instantiating <code>a</code> to <code>Int -&gt; Int</code>), cf. <a href="https://hackage.haskell.org/package/base/docs/Data-Function.html#v:fix" rel="noreferrer">https://hackage.haskell.org/package/base/docs/Data-Function.html#v:fix</a></p>
<p>So now, the category-theoretic counterpart of <code>fix</code> is going to be a family of morphisms <span class="math-container">$fix_A : (A \to A) \to A$</span> satisfying a bunch of conditions (such as the fixed-point equation and dinaturality); such categorical fixed-point operators have been studied in the literature, for example <a href="https://www.sciencedirect.com/science/article/pii/0304397593901127" rel="noreferrer">here</a> (this paper involves the domain theory that Andrej mentioned). And that gives you a categorical treatment of general recursion!</p>
<p>(I'll let someone else explain the catamorphism stuff, but for now let me note that there is no &quot;polynomial functor for factorial&quot;, but there is a functor whose initial algebra is the natural numbers.)</p>

CC BY-SA 4.0
2022-11-05T03:12:28.013Z
I don't have the reputation to upvote this xD but this is exactly the answer I was looking for!! Thank you so much
Ryan
CC BY-SA 4.0
2022-11-12T04:23:04.367Z
Upvoted! Thanks again
Ryan
CC BY-SA 4.0
2022-11-05T00:03:34.697Z
2022-11-05T00:03:34.697Z
Lê Thành Dũng 'Tito' Nguyễn
<p>I always understood functions in functional programming to be modeled by morphisms in the category of types, where any powerful function you write in your code is a morphism that is the composition of other morphisms within that category (in Haskell there are bottoms, I know. I'm speaking in general terms here).</p>
<p>But I can't grasp how that interacts with recursion. Putting F-algebras aside for now (I'll get there), the naive representation is that a composition of morphisms can be one of the morphisms in the composition; i.e. for some morphisms f, g, h you could have g = f ￮ g ￮ h, which is surely an impossible equality in general, as f and h need not be identity morphisms.</p>
<p>So then there are recursion schemes (catamorphism and friends). If you're modeling, say, using factorial in a function, do you compose with a functor into the category of F-algebras (or F-coalgebras), do all the magic there, and come back to the category of types for the rest of the composition? Do all the F-algebras (and F-coalgebras) exist already in the category of types, with the right initial and terminal morphisms between them? And in either case, how does one construct the polynomial functor for factorial?</p>
<p>I'm assuming that whatever the answer to this question is, it's the most common/standard method of modeling looping computation in category theory; correct me if this is incorrect.</p>

CC BY-SA 4.0
2022-10-25T19:48:05.793Z
Isn't "domain theory" the answer you're looking for?
Andrej Bauer
<functional-programming><ct.category-theory><recursion>
Composition with recursion in functions between types
Ryan
# Composition with recursion in functions between types

(Asked by: username_0 on 2022-10-21)

I always understood functions in functional programming to be modeled by morphisms in the category of types, where any powerful function you write in your code is a morphism that is the composition of other morphisms within that category (in Haskell there are bottoms, I know. I'm speaking in general terms here).

But I can't grasp how that interacts with recursion. Putting F-algebras aside for now (I'll get there), the naive representation is that a composition of morphisms can be one of the morphisms in the composition; i.e. for some morphisms f, g, h you could have g = f ￮ g ￮ h, which is surely an impossible equality in general, as f and h need not be identity morphisms.

So then there are recursion schemes (catamorphism and friends). If you're modeling, say, using factorial in a function, do you compose with a functor into the category of F-algebras (or F-coalgebras), do all the magic there, and come back to the category of types for the rest of the composition? Do all the F-algebras (and F-coalgebras) exist already in the category of types, with the right initial and terminal morphisms between them? And in either case, how does one construct the polynomial functor for factorial?

I'm assuming that whatever the answer to this question is, it's the most common/standard method of modeling looping computation in category theory; correct me if this is incorrect.

**Tags:** functional-programming, ct.category-theory, recursion

> **Comments:**
> **username_1**: Isn't "domain theory" the answer you're looking for?  

---

## 2 answers

### Answer by username_3 on 2022-11-05. Score: 7 | Accepted answer

(I'm going to try to write an answer for functional programmers, with Haskell-like code.)
First, you should know that using higher-order functions, recursive definitions can be turned into fixed-point equations. Typically, defining
fac :: Int -> Int
fac n = if n <= 0 then 1 else n * fact (n-1)

amounts to asking that fac satisfies the fixed-point equation fac == f fac (in other words that fac is a fixed point of f), for
f :: (Int -> Int) -> Int -> Int
f rec n = if n <= 0 then 1 else n * rec (n-1)

This f has a completely non-circular definition, so to give meaning to recursive definitions it suffices to have a "fixed-point operator"
fix :: (a -> a) -> a

that guarantees fix g == g (fix g) for every function g, because then we can define fact = fix f (instantiating a to Int -> Int), cf. https://hackage.haskell.org/package/base/docs/Data-Function.html#v:fix
So now, the category-theoretic counterpart of fix is going to be a family of morphisms $fix_A : (A \to A) \to A$ satisfying a bunch of conditions (such as the fixed-point equation and dinaturality); such categorical fixed-point operators have been studied in the literature, for example here (this paper involves the domain theory that Andrej mentioned). And that gives you a categorical treatment of general recursion!
(I'll let someone else explain the catamorphism stuff, but for now let me note that there is no "polynomial functor for factorial", but there is a functor whose initial algebra is the natural numbers.)

> **Comments:**
> **username_0**: I don't have the reputation to upvote this xD but this is exactly the answer I was looking for!! Thank you so much  
> **username_0**: Upvoted! Thanks again  

---

### Answer by username_2 on 2022-11-12. Score: 1

The theory behind recursion schemes is that algebraic data types are initial algebras.
1. Recursive types and folds
The programmer's point of view is that, given a recursive type,
data Tree = Node Tree Tree | Leaf

... there is a standard way to consume it, which is a fold:
fold :: (a -> a -> a) -> a -> Tree -> a
fold node leaf (Node l r) = node (fold node leaf l) (fold node leaf r)
fold node leaf Leaf = leaf

fold pattern-matches on a tree, makes recursive calls on the children, and combines the result somehow. The point is that:

many recursive functions fit this mold (including factorial; it's not the best example, but for that exact reason it's a good exercise for the reader to (1) write the fold for Peano Nat; (2) define factorial using fold)
if you disallow explicit recursion, and program only using folds, then your functions are guaranteed to terminate.

See also Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire. It is the inspiration for the recursion-schemes library in Haskell, whose description used to read "Generalized bananas, lenses and barbed wire".
2. Initial algebras
The mathematician's point of view is that a data type such as Tree is a language, i.e., a set/type of expressions:
Tree :: Type

Constructors are the words of that language:
Node :: Tree -> Tree -> Tree
Leaf :: Tree

Given a semantic domain a, and an interpretation of those words in that domain:
node :: a -> a -> a
leaf :: a

... we obtain an interpretation of whole expressions, which is precisely a fold:
fold node leaf :: Tree -> a

An "interpretation" is also called an algebra, or algebraic structure:

In mathematics, an algebraic structure consists of a nonempty set A (called the underlying set, carrier set or domain), [and] a collection of operations on A (typically binary operations such as addition and multiplication) (...).
--- Algebraic structure, Wikipedia

In the case of binary trees, the structures they describe are called magmas. Most other data types don't correspond to a notable class of algebraic structures, if they have a name at all. So the individual structures are not what's interesting about this point of view. Instead, meditate on the idea that words don't have intrinsic value, and their worth is in their meaning.
A language, can be viewed as a trivial interpretation---an algebra---of itself.
fold Node Leaf :: Tree -> Tree
= id

And in the category of algebras, it is an initial object, also called "free algebra". It is the most general algebra (for a given signature, here of node and leaf): it can be mapped (in a respectful manner, i.e., via a homomorphism) into any other algebra, and there is only one way to do so, using fold.

The basic method of introducing a new concrete data type, as in a number of
other languages, is to declare a free algebra.
--- Miranda: A non-strict functional
language with polymorphic types, D. A. Turner, 1985

3. Initial F-algebras
The notion of F-algebra in category theory generalizes the idea of algebras further.
As mentioned above, an algebra is a set a equipped with some operations.
node :: a -> a -> a
leaf :: a

This can be rearranged into an F-algebra: a set a equipped with one morphism of this form:
alg :: F a -> a

for some endofunctor F which encodes the shape of the operations. Here, for node and leaf, the relevant functor is F a = (a × a) + 1 (in Haskell, × is the tuple type (,), + is Either, 1 is the unit type ()).
alg :: (a × a) + 1 -> a

-- gives the same information as ---

node :: a -> a -> a
leaf :: a

The point is that the definition of an algebra is seemingly tied to the notion of sets: "a SET equipped with operations"). In contrast, we can talk about F-algebras in any category, for example in a category of domains. Whereas the category of sets has total functions, domains give semantics to programming languages with nontermination. We don't need to reinvent universal algebra---where algebras are traditionally carried by sets---from sets to domain theory or whatever, because instead we can prove general theorems about F-algebras in any category and then apply them to the relevant category.
To sum up, folds are a principled way of implementing recursive functions, in many senses:

if you only use folds, avoiding unstructured recursion, programs are guaranteed to terminate. This eliminates the class of mistakes "accidental infinite loop".

Folds play a natural role in programming language theory: a fold gives a compositional intepretation of a language in terms of its individual constructors. It can also be quite illuminating to to go the other way, viewing arbitrary data types as languages. I would argue that it can be a good perspective from which to appreciate general ideas such as Church encodings or defunctionalization.

Data types and their folds can be represented as initial F-algebras and catamorphisms, which make sense in categories which may model your programming language more accurately than sets and total functions, with the prime example of domains for modelling nontermination.

What about final F-coalgebras, aka., unfolds? This is getting too long so I'll briefly make the following points.
a. F-coalgebras s -> F s can be thought of as an extremely general class of state machines, using the functor F to encode various forms of inputs and outputs. Unfolding an F-coalgebra turns it into an infinite tree, which may be more intuitive to manipulate.
b. fold and unfold recover Turing-completess. And now you have algebraically inspired primitives instead of unstructured loops. A possible application is that optimizations like deforestation (removing intermediate data structures) become "just" an exercise in equational reasoning.


---

