In combinatory logic for computer science, a fixed-point combinator (or fixpoint combinator) is a higher-order function (i.e., a function that 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
f
i
x
{\displaystyle \mathrm {fix} }
is a fixed-point combinator and the function
f
{\displaystyle f}
has one or more fixed points, then
f
i
x
f
{\displaystyle \mathrm {fix} \ f}
is one of these fixed points, i.e.,
f
i
x
f
=
f
(
f
i
x
f
)
.
{\displaystyle \mathrm {fix} \ f\ =f\ (\mathrm {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.
Contents
Introduction
Applied to a non-constant function of one variable that treats its argument as a piece of data (such as e.g. the sine function), the Y combinator usually does not terminate. Y really is meant to be used with codata, e.g. a colist constructor that places the first element there and expects "the rest of elements" as an argument, that will be fleshed out later; or a higher-order function that expects to be supplied as its first argument with a function "to compute the rest," which it might or might not call, as needed.
Applied to such "one step functional," Y arranges for the creation of that "rest of computation" function, consisting of that same step it was given and the "rest of computation" again, which really means just the original step repeated as many times as needed, calling "the rest" function as and if needed, but not always. The resulting function behaves like a while or a for loop. Used in this way, the Y combinator implements general recursion.
The lambda calculus does not have global names, for a function to refer to itself by name inside its own definition, as is possible in many programming languages. But it has local names, i.e. parameters in lambda abstractions. An abstraction term can receive another term as an argument, and refer to that argument by the parameter name inside the function's body.
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.
Y combinator in lambda calculus
In the classical untyped lambda calculus, every function has a fixed point. A particular implementation of
f
i
x
{\displaystyle \mathrm {fix} }
is Haskell Curry's paradoxical combinator Y, given by
Y
=
λ
f
.
(
λ
x
.
f
(
x
x
)
)
(
λ
x
.
f
(
x
x
)
)
{\displaystyle \mathrm {Y} =\lambda f.\ (\lambda x.f\ (x\ x))\ (\lambda x.f\ (x\ x))}
Verification
The following calculation verifies that
Y
g
{\displaystyle \mathrm {Y} g}
is indeed a fixed point of the function
g
{\displaystyle g}
:
The lambda term
g
(
Y
g
)
{\displaystyle g\ (\mathrm {Y} \ g)}
may not, in general, β-reduce to the term
Y
g
{\displaystyle \mathrm {Y} \ g}
. However, both terms β-reduce to the same term, as shown.
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.
Values and domains
Many functions do not have any fixed points, for instance
f
:
N
→
N
{\displaystyle f:\mathbb {N} \to \mathbb {N} }
with
f
(
n
)
=
n
+
1
{\displaystyle 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.
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
fact
n
=
{
1
if
n
=
0
n
×
fact
(
n
−
1
)
otherwise.
{\displaystyle \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.
This poses a problem, though, as
fact
{\displaystyle \operatorname {fact} }
inside the definition of
fact
{\displaystyle \operatorname {fact} }
Fixed-point combinators in lambda calculus
The Y combinator, discovered by Haskell Curry, is defined as
Y
=
λ
f
.
(
λ
x
.
f
(
x
x
)
)
(
λ
x
.
f
(
x
x
)
)
{\displaystyle 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
Y
=
S
(
K
(
S
I
I
)
)
(
S
(
S
(
K
S
)
K
)
(
K
(
S
I
I
)
)
)
=
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
Z
g
{\displaystyle Zg}
in the right-hand side of the definition:
Z
g
v
=
g
(
Z
g
)
v
.
{\displaystyle Zgv=g(Zg)v\ .}
and in lambda calculus it is an eta-expansion of the Y combinator:
Z
=
λ
f
.
(
λ
x
.
f
(
λ
Non-standard fixed-point combinators
If F is a fixed-point combinator in untyped lambda calculus, then there is:
F
=
λ
x
.
F
x
=
λ
x
.
x
(
F
x
)
=
λ
x
.
x
(
x
(
F
x
)
)
=
⋯
{\displaystyle {\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
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
(
f
i
x
f
)
)
.
.
.
)
x
{\displaystyle 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:
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 FixedPointCombinator, using CRTP. The function to be fixed is contained in a class that inherits from fixer. The fix() function accesses the function to be fixed using a concept to call apply(). As for the strict functional definition, fix() is explicitly given an extra parameter x, which means that lazy evaluation is not needed.
Using only lambdas, one can create a fixed-point combinator like so:
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
{\displaystyle a}
is a type variable. That is, if the type of
f
i
x
f
{\displaystyle \mathrm {fix} \ f}
fulfilling the equation
f
i
x
f
=
f
(
f
i
x
f
)
{\displaystyle \mathrm {fix} \ f\ =\ f\ (\mathrm {fix} \ f)}
is
a
{\displaystyle a}
—the most general type—then the type of
f
{\displaystyle f}
Type for the Y combinator
In programming languages that support named recursive data types, the unbounded recursion in
t
:=
t
→
a
{\displaystyle t:=t\to a}
, which creates the would-be infinite type
t
{\displaystyle t}
, is broken by naming the type
t
{\displaystyle t}
explicitly, as e.g. type
R
a
{\displaystyle R\ a}
which is defined so as to be isomorphic to (or just to be a synonym of) the type
R
a
→
a
{\displaystyle R\ a\to a}
. Thus
R
a
:=
R
a
→
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:
∀
x
(
f
x
=
x
)
{\displaystyle \forall x(f\ x=x)}
In contrast to universal quantification over all
x
{\displaystyle x}
, a fixed-point combinator constructs one value that is a fixed point of
f
{\displaystyle f}
. The remarkable property of a fixed-point combinator is that it constructs a fixed point for an arbitrary given function
f
{\displaystyle f}
.
Other functions have the special property that, after being applied once, further applications don't have any effect. More formally: