# Fixed-point combinator

In combinatory logic for computer science, a fixed-point combinator (or fixpoint combinator)$^{:p.26}$ is a higher-order function (i.e., a function which takes a function as argument) that returns some fixed point (a value that is mapped to itself) of its argument function, if one exists.
Formally, if ${\textrm {fix}}$ is a fixed-point combinator and the function $f$ has one or more fixed points, then ${\textrm {fix}}\ f$ is one of these fixed points, i.e.,
    $f\ ({\textrm {fix}}\ f)={\textrm {fix}}\ f\ .$
Fixed-point combinators can be defined in the lambda calculus and in functional programming languages, and provide a means to allow for recursive definitions.

## Y combinator in lambda calculus

In the classical untyped lambda calculus, every function has a fixed point. A particular implementation of ${\textrm {fix}}$ is Haskell Curry's paradoxical combinator Y, given by$^{:131}$
    $Y=\lambda f.\ (\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x))$
(Here using the standard notations and conventions of lambda calculus: Y is a function that takes one argument f and returns the entire expression following the first period; the expression $\lambda x.f\ (x\ x)$ denotes a function that takes one argument x, thought of as a function, and returns the expression $f\ (x\ x)$, where $(x\ x)$ denotes x applied to itself. Juxtaposition of expressions denotes function application, is left-associative, and has higher precedence than the period.)

### Verification

The following calculation verifies that $Yg$ is indeed a fixed point of the function $g$:
    $Y\ g\$ $=(\lambda f.(\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x)))\ g\ \ \$ by the definition of $Y$ $=(\lambda x.g\ (x\ x))\ (\lambda x.g\ (x\ x))\$ by β-reduction: replacing the formal argument f of Y with the actual argument g $=g\ ((\lambda x.g\ (x\ x))\ (\lambda x.g\ (x\ x)))\$ by β-reduction: replacing the formal argument x of the first function with the actual argument $(\lambda x.g\ (x\ x))$ $=g\ (Y\ g)$ by second equality, above
The lambda term $g\ (Y\ g)$ may not, in general, β-reduce to the term $Y\ g$. However, both terms β-reduce to the same term, as shown.

### Uses

Applied to a function with one variable, the Y combinator usually does not terminate. More interesting results are obtained by applying the Y combinator to functions of two or more variables. The added variables may be used as a counter, or index. The resulting function behaves like a while or a for loop in an imperative language.
Used in this way, the Y combinator implements simple recursion. The lambda calculus does not allow a function to appear as a term in its own definition as is possible in many programming languages, but a function can be passed as an argument to a higher-order function that applies it in a recursive manner.
The Y combinator may also be used in implementing Curry's paradox. The heart of Curry's paradox is that untyped lambda calculus is unsound as a deductive system, and the Y combinator demonstrates this by allowing an anonymous expression to represent zero, or even many values. This is inconsistent in mathematical logic.

### Example implementations

An example implementation of Y in the language R is presented below:
This can then be used to implement factorial as follows:
Y is only needed when function names are absent. Substituting all the definitions into one line so that function names are not required gives:
This works because R uses lazy evaluation.
Languages that use strict evaluation, such as Python, C++, and other strict programming languages, can often express Y; however, any implementation is useless in practice since it loops indefinitely until terminating via a stack overflow.

## Fixed-point combinator

The Y combinator is an implementation of a fixed-point combinator in lambda calculus. Fixed-point combinators may also be easily defined in other functional and imperative languages. The implementation in lambda calculus is more difficult due to limitations in lambda calculus. The fixed-point combinator may be used in a number of different areas:
* General mathematics
* Untyped lambda calculus
* Typed lambda calculus
* Functional programming
* Imperative programming
Fixed-point combinators may be applied to a range of different functions, but normally will not terminate unless there is an extra parameter. When the function to be fixed refers to its parameter, another call to the function is invoked, so the calculation never gets started. Instead, the extra parameter is used to trigger the start of the calculation.
The type of the fixed point is the return type of the function being fixed. This may be a real or a function or any other type.
In the untyped lambda calculus, the function to apply the fixed-point combinator to may be expressed using an encoding, like Church encoding. In this case particular lambda terms (which define functions) are considered as values. "Running" (beta reducing) the fixed-point combinator on the encoding gives a lambda term for the result which may then be interpreted as fixed-point value.
Alternately, a function may be considered as a lambda term defined purely in lambda calculus.
These different approaches affect how a mathematician and a programmer may regard a fixed-point combinator. A mathematician may see the Y combinator applied to a function as being an expression satisfying the fixed-point equation, and therefore a solution.
In contrast, a person only wanting to apply a fixed-point combinator to some general programming task may see it only as a means of implementing recursion.

### Values and domains

Many functions do not have any fixed points, for instance $f:\mathbb {N} \to \mathbb {N}$ with $f(n)=n+1$. Using Church encoding, natural numbers can be represented in lambda calculus, and this function f can be defined in lambda calculus. However, its domain will now contain all lambda expressions, not just those representing natural numbers. The Y combinator, applied to f, will yield a fixed-point for f, but this fixed-point won't represent a natural number. If trying to compute Y f in an actual programming language, an infinite loop will occur.

### Function versus implementation

The fixed-point combinator may be defined in mathematics and then implemented in other languages. General mathematics defines a function based on its extensional properties. That is, two functions are equal if they perform the same mapping. Lambda calculus and programming languages regard function identity as an intensional property. A function's identity is based on its implementation.
A lambda calculus function (or term) is an implementation of a mathematical function. In the lambda calculus there are a number of combinators (implementations) that satisfy the mathematical definition of a fixed-point combinator.

### Definition of the term "combinator"

Combinatory logic is a higher-order functions theory. A combinator is a closed lambda expression, meaning that it has no free variables. The combinators may be combined to direct values to their correct places in the expression without ever naming them as variables.

## Recursive definitions and fixed-point combinators

Fixed-point combinators can be used to implement recursive definition of functions. However, they are rarely used in practical programming. Strongly normalizing type systems such as the simply typed lambda calculus disallow non-termination and hence fixed-point combinators often cannot be assigned a type or require complex type system features. Furthermore fixed-point combinators are often inefficient compared to other strategies for implementing recursion, as they require more function reductions and construct and take apart a tuple for each group of mutually recursive definitions.$^{:page232}$

### The factorial function

The factorial function provides a good example of how a fixed-point combinator may be used to define recursive functions. The standard recursive definition of the factorial function in mathematics can be written as
    $\operatorname {fact} \ n={\begin{cases}1&{\text{if}}~n=0\\n\times \operatorname {fact} (n-1)&{\text{otherwise.}}\end{cases}}$
where n is a non-negative integer. Implementing this in lambda calculus, where integers are represented using Church encoding, encounters the problem that the lambda calculus disallows the name of a function ('fact') to be used in the function's definition. This can be circumvented using a fixed-point combinator ${\textsf {fix}}$ as follows.
Define a function F of two arguments f and n:
    $F\ f\ n=(\operatorname {IsZero} \ n)\ 1\ (\operatorname {multiply} \ n\ (f\ (\operatorname {pred} \ n)))$
(Here $(\operatorname {IsZero} \ n)$ is a function that takes two arguments and returns its first argument if n=0, and its second argument otherwise; $\operatorname {pred} \ n$ evaluates to n-1.)
Now define $\operatorname {fact} ={\textsf {fix}}\ F$. Then $\operatorname {fact}$ is a fixed-point of F, which gives
    ${\begin{aligned}\operatorname {fact} n&=F\ \operatorname {fact} \ n\\&=(\operatorname {IsZero} \ n)\ 1\ (\operatorname {multiply} \ n\ (\operatorname {fact} \ (\operatorname {pred} \ n)))\ \end{aligned}}$
as desired.

## Fixed-point combinators in lambda calculus

The Y combinator, discovered by Haskell Curry, is defined as
    $Y=\lambda f.(\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x))$

### Other fixed-point combinators

In untyped lambda calculus fixed-point combinators are not especially rare. In fact there are infinitely many of them. In 2005 Mayer Goldberg showed that the set of fixed-point combinators of untyped lambda calculus is recursively enumerable.
The Y combinator can be expressed in the SKI-calculus as
    ${\mathsf {Y=S(K(SII))(S(S(KS)K)(K(SII)))=SSI(S(S(KS)K)(K(SII)))}}$
Additional combinators (B, C, K, W system) allow for much shorter encodings. With ${\mathsf {U=SII}}$ the self-application combinator, since ${\mathsf {S}}({\mathsf {K}}x)yz=x(yz)={\mathsf {B}}xyz$ and ${\mathsf {S}}x({\mathsf {K}}y)z=xzy={\mathsf {C}}xyz$, the above becomes
    ${\mathsf {Y=S(KU)(SB(KU))=BU(CBU)}}\ \ \ ;\ \ {\mathsf {Y=SSI(BWB)}}$
The shortest fixed-point combinator in the SK-calculus using S and K combinators only, found by John Tromp, is
    ${\mathsf {Y'=SSK(S(K(SS(S(SSK))))K)=WC(SB(C(WC)))}}$
although note that it is not in normal form, which is longer. This combinator corresponds to the lambda expression
    ${\mathsf {Y}}'=(\lambda xy.xyx)(\lambda yx.y(xyx))$
The following fixed-point combinator is simpler than the Y combinator, and β-reduces into the Y combinator; it is sometimes cited as the Y combinator itself:
    ${\mathsf {X}}=\lambda f.(\lambda x.xx)(\lambda x.f(xx))\ \ \ ;\ \ {\mathsf {Xf=U(BfU)}}$
Another common fixed-point combinator is the Turing fixed-point combinator (named after its discoverer, Alan Turing):$^{:132}$
    $\Theta =(\lambda xy.y(xxy))\ (\lambda xy.y(xxy))={\mathsf {SII(S(K(SI))(SII))=U(B(SI)U)}}$
Its advantage over ${\mathsf {Y}}$ is that $\Theta \ f$ beta-reduces to $f\ (\Theta f)$, whereas ${\mathsf {Y}}\ f$ and $f\ ({\mathsf {Y}}f)$ only beta-reduce to a common term.
$\Theta$ also has a simple call-by-value form:
    $\Theta _{v}=(\lambda xy.y(\lambda z.xxyz))\ (\lambda xy.y(\lambda z.xxyz))$
The analog for mutual recursion is a polyvariadic fix-point combinator, which may be denoted Y*.

### Strict fixed-point combinator

In a strict programming language the Y combinator will expand until stack overflow, or never halt in case of tail call optimization. The Z combinator will work in strict languages (also called eager languages, where applicative evaluation order is applied). The Z combinator has the next argument defined explicitly, preventing the expansion of $Zg$ in the right-hand side of the definition:
    $Zgv=g(Zg)v\ .$
and in lambda calculus it is an eta-expansion of the Y combinator:
    $Z=\lambda f.(\lambda x.f(\lambda v.xxv))\ (\lambda x.f(\lambda v.xxv))\ .$

### Non-standard fixed-point combinators

If F is a fixed-point combinator in untyped lambda calculus, then there is:
    ${\mathsf {F}}=\lambda x.Fx=\lambda x.x(Fx)=\lambda x.x(x(Fx))=\cdots$
Terms that have the same Böhm tree as a fixed-point combinator, i.e., have the same infinite extension $\lambda x.x(x(x\cdots ))$, are called non-standard fixed-point combinators. Any fixed-point combinator is also a non-standard one, but not all non-standard fixed-point combinators are fixed-point combinators because some of them fail to satisfy the fixed-point equation that defines the "standard" ones. These combinators are called strictly non-standard fixed-point combinators; an example is the following combinator:
    ${\mathsf {N=BU(B(BU)B)}}$
where
    ${\mathsf {B}}=\lambda xyz.x(yz)$
    ${\mathsf {U}}=\lambda x.xx\$
since
    ${\mathsf {N}}=\lambda x.Nx=\lambda x.x(N_{2}x)=\lambda x.x(x(x(N_{3}x)))=\lambda x.x(x(x(x(x(x(N_{4}x))))))=\cdots$
where ${\mathsf {N}}_{i}$ are modifications of ${\mathsf {N}}$ created on the fly which add $i$ instances of $x$ at once into the chain while being replaced with ${\mathsf {N}}_{i+1}$.
The set of non-standard fixed-point combinators is not recursively enumerable.

## Implementation in other languages

The Y combinator is a particular implementation of a fixed-point combinator in lambda calculus. Its structure is determined by the limitations of lambda calculus. It is not necessary or helpful to use this structure in implementing the fixed-point combinator in other languages.
Simple examples of fixed-point combinators implemented in some programming paradigms are given below.

### Lazy functional implementation

In a language that supports lazy evaluation, as in Haskell, it is possible to define a fixed-point combinator using the defining equation of the fixed-point combinator which is conventionally named fix. Since Haskell has lazy data types, this combinator can also be used to define fixed points of data constructors (and not only to implement recursive functions). The definition is given here, followed by some usage examples. In Hackage, the original sample is:

### Strict functional implementation

In a strict functional language, as illustrated below with OCaml, the argument to f is expanded beforehand, yielding an infinite call sequence,
    $f\ (f...(f\ ({\mathsf {fix}}\ f))...)\ x$.
This may be resolved by defining fix with an extra parameter.
In a multi-paradigm functional language (one decorated with imperative features), such as Lisp, Peter Landin suggested the use of a variable assignment to create a fixed-point combinator, as in the below example using Scheme:
Using a lambda calculus with axioms for assignment statements, it can be shown that Y! satisfies the same fixed-point law as the call-by-value Y combinator:
    $(Y_{!}\ \lambda x.e)e'=(\lambda x.e)\ (Y_{!}\ \lambda x.e)e'$
In more idiomatic modern Scheme usage, this would typically be handled via a letrec expression, as lexical scope was introduced to Lisp in the 1970s:
Or without the internal label:

### Imperative language implementation

This example is a slightly interpretive implementation of a fixed-point combinator. A class is used to contain the fix function, called fixer. The function to be fixed is contained in a class that inherits from fixer. The fix function accesses the function to be fixed as a virtual function. As for the strict functional definition, fix is explicitly given an extra parameter x, which means that lazy evaluation is not needed.
Another example can be shown to demonstrate SKI combinator calculus (with given bird name from combinatory logic) being used to build up Z combinator to achieve tail call-like behavior through trampolining:

## Typing

In System F (polymorphic lambda calculus) a polymorphic fixed-point combinator has type
    ∀a.(a → a) → a
where $a$ is a type variable. That is, if the type of $fix\ f$ fulfilling the equation $fix\ f\ =\ f\ (fix\ f)$ is $a$, – the most general type, – then the type of $f$ is $a\to a$. So then, $fix$ takes a function which maps $a$ to $a$ and uses it to return a value of type $a$.
In the simply typed lambda calculus extended with recursive data types, fixed-point operators can be written, but the type of a "useful" fixed-point operator (one whose application always returns) may be restricted.
In the simply typed lambda calculus, the fixed-point combinator Y cannot be assigned a type because at some point it would deal with the self-application sub-term $x~x$ by the application rule:
    ${\Gamma \vdash x\!:\!t_{1}\to t_{2}\quad \Gamma \vdash x\!:\!t_{1}} \over {\Gamma \vdash x~x\!:\!t_{2}}$
where $x$ has the infinite type $t_{1}=t_{1}\to t_{2}$. No fixed-point combinator can in fact be typed; in those systems, any support for recursion must be explicitly added to the language.

### Type for the Y combinator

In programming languages that support recursive data types, the unbounded recursion in $t=t\to a$ which creates the infinite type $t$ is broken by marking the $t$ type explicitly as a recursive type $Rec\ a$, which is defined so as to be isomorphic to (or just to be a synonym of) $Rec\ a\to a$. The $Rec\ a$ type value is created by simply tagging the function value of type $Rec\ a\to a$ with the data constructor tag $Rec$ (or any other of our choosing).
For example, in the following Haskell code, has Rec and app being the names of the two directions of the isomorphism, with types:
which lets us write:
Or equivalently in OCaml:
Alternatively:

## General information

Because fixed-point combinators can be used to implement recursion, it is possible to use them to describe specific types of recursive computations, such as those in fixed-point iteration, iterative methods, recursive join in relational databases, data-flow analysis, FIRST and FOLLOW sets of non-terminals in a context-free grammar, transitive closure, and other types of closure operations.
A function for which every input is a fixed point is called an identity function. Formally:
    $\forall x(f\ x=x)$
In contrast to universal quantification over all $x$, a fixed-point combinator constructs one value that is a fixed point of $f$. The remarkable property of a fixed-point combinator is that it constructs a fixed point for an arbitrary given function $f$.
Other functions have the special property that, after being applied once, further applications don't have any effect. More formally:
    $\forall x(f\ (f\ x)=f\ x)$
Such functions are called idempotent (see also Projection (mathematics)). An example of such a function is the function that returns 0 for all even integers, and 1 for all odd integers.
In lambda calculus,from a computational point of view, applying a fixed-point combinator to an identity function or an idempotent function typically results in non-terminating computation. For example, obtaining
    $(Y\ \lambda x.x)=(\lambda x.(\lambda x.x)(x\ x))\ (\lambda x.(\lambda x.x)(x\ x))$
where the resulting term can only reduce to itself and represents an infinite loop.
Fixed-point combinators do not necessarily exist in more restrictive models of computation. For instance, they do not exist in simply typed lambda calculus.
The Y combinator allows recursion to be defined as a set of rewrite rules, without requiring native recursion support in the language.
In programming languages that support anonymous functions, fixed-point combinators allow the definition and use of anonymous recursive functions, i.e., without having to bind such functions to identifiers. In this setting, the use of fixed-point combinators is sometimes called anonymous recursion.