Initial algebra
In mathematics, an initial algebra is an initial object in the category of F-algebras for a given endofunctor F. This initiality provides a general framework for induction and recursion.
Examples
Functor 1 + (−)
Consider the endofunctor F : Set → Set sending X to 1 + X, where 1 is the one-point (singleton) set, the terminal object in the category. An algebra for this endofunctor is a set X (called the carrier of the algebra) together with a function f : (1 + X) → X. Defining such a function amounts to defining a point [math]\displaystyle{ x\in X }[/math] and a function X → X. Define
- [math]\displaystyle{ \begin{align} \operatorname{zero} \colon 1 &\longrightarrow\mathbf{N} \\ * &\longmapsto 0 \end{align} }[/math]
and
- [math]\displaystyle{ \begin{align} \operatorname{succ}\colon \mathbf{N}&\longrightarrow\mathbf{N} \\ n &\longmapsto n + 1. \end{align} }[/math]
Then the set N of natural numbers together with the function [zero,succ]: 1 + N → N is an initial F-algebra. The initiality (the universal property for this case) is not hard to establish; the unique homomorphism to an arbitrary F-algebra (A, [e, f]), for e: 1 → A an element of A and f: A → A a function on A, is the function sending the natural number n to fn(e), that is, f(f(…(f(e))…)), the n-fold application of f to e.
The set of natural numbers is the carrier of an initial algebra for this functor: the point is zero and the function is the successor function.
Functor 1 + N × (−)
For a second example, consider the endofunctor 1 + N × (−) on the category of sets, where N is the set of natural numbers. An algebra for this endofunctor is a set X together with a function 1 + N × X → X. To define such a function, we need a point [math]\displaystyle{ x\in X }[/math] and a function N × X → X. The set of finite lists of natural numbers is an initial algebra for this functor. The point is the empty list, and the function is cons, taking a number and a finite list, and returning a new finite list with the number at the head.
In categories with binary coproducts, the definitions just given are equivalent to the usual definitions of a natural number object and a list object, respectively.
Final coalgebra
Dually, a final coalgebra is a terminal object in the category of F-coalgebras. The finality provides a general framework for coinduction and corecursion.
For example, using the same functor 1 + (−) as before, a coalgebra is defined as a set X together with a function f : X → (1 + X). Defining such a function amounts to defining a partial function f': X ⇸ Y whose domain is formed by those [math]\displaystyle{ x \in X }[/math] for which f(x) belongs to 1. Such a structure can be viewed as a chain of sets, X0 on which f′ is not defined, X1 which elements map into X0 by f′, X2 which elements map into X1 by f′, etc., and Xω containing the remaining elements of X. With this in view, the set [math]\displaystyle{ \mathbf{N} \cup \{\omega\} }[/math] consisting of the set of natural numbers extended with a new element ω is the carrier of the final coalgebra in the category, where [math]\displaystyle{ f' }[/math] is the predecessor function (the inverse of the successor function) on the positive naturals, but acts like the identity on the new element ω: f(n + 1) = n, f(ω) = ω. This set [math]\displaystyle{ \mathbf{N} \cup \{\omega\} }[/math] that is the carrier of the final coalgebra of 1 + (−) is known as the set of conatural numbers.
For a second example, consider the same functor 1 + N × (−) as before. In this case the carrier of the final coalgebra consists of all lists of natural numbers, finite as well as infinite. The operations are a test function testing whether a list is empty, and a deconstruction function defined on non-empty lists returning a pair consisting of the head and the tail of the input list.
Theorems
- Initial algebras are minimal (i.e., have no proper subalgebra).
- Final coalgebras are simple (i.e., have no proper quotients).
Use in computer science
Various finite data structures used in programming, such as lists and trees, can be obtained as initial algebras of specific endofunctors. While there may be several initial algebras for a given endofunctor, they are unique up to isomorphism, which informally means that the "observable" properties of a data structure can be adequately captured by defining it as an initial algebra.
To obtain the type List(A) of lists whose elements are members of set A, consider that the list-forming operations are:
- [math]\displaystyle{ \mathrm{nil}\colon 1 \to \mathrm{List}(A) }[/math]
- [math]\displaystyle{ \mathrm{cons}\colon A \times \mathrm{List}(A) \to \mathrm{List}(A) }[/math]
Combined into one function, they give:
- [math]\displaystyle{ [\mathrm{nil},\mathrm{cons}]\colon (1 + A \times \mathrm{List}(A))\to \mathrm{List}(A), }[/math]
which makes this an F-algebra for the endofunctor F sending X to 1 + (A × X). It is, in fact, the initial F-algebra. Initiality is established by the function known as foldr in functional programming languages such as Haskell and ML.
Likewise, binary trees with elements at the leaves can be obtained as the initial algebra
- [math]\displaystyle{ [\mathrm{tip},\mathrm{join}]\colon A + (\mathrm{Tree}(A) \times \mathrm{Tree}(A)) \to \mathrm{Tree}(A). }[/math]
Types obtained this way are known as algebraic data types.
Types defined by using least fixed point construct with functor F can be regarded as an initial F-algebra, provided that parametricity holds for the type.[1]
In a dual way, similar relationship exists between notions of greatest fixed point and terminal F-coalgebra, with applications to coinductive types. These can be used for allowing potentially infinite objects while maintaining strong normalization property.[1] In the strongly normalizing (each program terminates) Charity programming language, coinductive data types can be used for achieving surprising results, e.g. defining lookup constructs to implement such “strong” functions like the Ackermann function.[2]
See also
Notes
- ↑ 1.0 1.1 Philip Wadler: Recursive types for free! University of Glasgow, July 1998. Draft.
- ↑ Robin Cockett: Charitable Thoughts (ps.gz)
External links
- Categorical programming with inductive and coinductive types by Varmo Vene
- Recursive types for free! by Philip Wadler, University of Glasgow, 1990-2014.
- Initial Algebra and Final Coalgebra Semantics for Concurrency by J.J.M.M. Rutten and D. Turi
- Initiality and finality from CLiki
- Typed Tagless Final Interpreters by Oleg Kiselyov
Original source: https://en.wikipedia.org/wiki/Initial algebra.
Read more |