Fixed-point combinator
In computer science, a fixed-point combinator (or fixpoint combinator^{[1]}) is a higher-order function y that satisfies the equation
or in words: y, when applied to an arbitrary function f, yields the same result as f applied to the result of applying y to f. It is so named because, by setting , it represents a solution to the fixed point equation,
A fixed point of a function f is a value that doesn't change under the application of the function f.
Functions that satisfy the equation for y expand as,
A particular implementation of y is Curry's paradoxical combinator Y, represented in lambda calculus by
This combinator may 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 that by allowing an anonymous expression to represent zero, or even many values. This is inconsistent in mathematical logic.
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 second variable 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. In the lambda calculus it is not possible to refer to the definition of a function in a function body. Recursion may only be achieved by passing in a function as a parameter. The Y combinator demonstrates this style of programming.
Introduction
The Y combinator is an implementation of the 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 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. Even with lazy evaluation when the function to be fixed refers to its parameter, another call to the function is invoked. The calculation never gets started. The extra parameter is needed 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 fix 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 lambda calculus 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
Every expression has one value. This is true in general mathematics and it must be true in lambda calculus. This means that in lambda calculus, applying a fixed point combinator to a function gives you an expression whose value is the fixed point of the function.
However this is a value in the lambda calculus domain, it may not correspond to any value in the domain of the function, so in a practical sense it is not necessarily a fixed point of the function, and only in the lambda calculus domain is it a fixed point of the equation.
For example, consider,
Division of Signed numbers may be implemented in the Church encoding, so f may be represented by a lambda term. This equation has no solution in the real numbers. But in the domain of the complex numbers i and -i are solutions. This demonstrates that there may be solutions to an equation in another domain. However the lambda term for the solution for the above equation is weirder than that. The lambda term represents the state where x could be either i or -i, as one value. The information distinguishing these two values has been lost, in the change of domain.
For the lambda calculus mathematician, this is a consequence of the definition of lambda calculus. For the programmer, it means that the beta reduction of the lambda term will loop forever, never reaching a normal form.
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.^{[2]} 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 functions 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 combinator (implementations) that satisfy the mathematical definition of a fixed-point combinator.
What is a "combinator"?
A combinator is a particular type of higher-order function that may be used in defining functions without using variables. The combinators may be combined to direct values to their correct places in the expression without ever naming them as variables.
Usage
Usually when applied to functions of one parameter, implementations of the fixed point combinator fail to terminate. Functions with extra parameters are more interesting.
The Y combinator is an example of what makes the Lambda calculus inconsistent. So it should be regarded with suspicion. However it is safe to consider the Y combinator when defined in mathematic logic only. The definition is,
It is easy to see how f may be applied to one variable. Applying it to two or more variables requires adding them to the equation,
This version of the equation must be shown consistent with the previous by the definition for equality of functions,
This definition allows the two equations for y to be regarded as equivalent, provided that the domain of x is well defined. So if f has multiple parameters the y f may still be regarded as a fixed point, with some restrictions.
The factorial function
The factorial function provides a good example of how the fixed point combinator may be applied to functions of two variables. The result demonstrates simple recursion, as would be implemented in a single loop, in an imperative language. The definition of numbers used is explained in Church encoding. The fixed point function is,
so y F is,
or
Setting gives,
this definition is equivalent to the mathematical definition of factorial,
This definition puts F in the role of the body of a loop to be iterated.
Fixed point combinators in lambda calculus
The Y combinator, discovered by Haskell B. Curry, is defined as:
Beta reduction of this gives,
(by definition of Y) | ||
(by β-reduction of λf: applied Y to g) | ||
(by β-reduction of λx: applied left function to right function) | ||
(by second equality) |
By repeatedly applying this equality we get,
Equivalent definition of a fixed-point combinator
This fixed-point combinator may be defined as y in,
An expression for y may be derived using rules from the definition of a let expression. Firstly using the rule,
gives,
Also using,
gives
Then using the eta reduction rule,
gives,
Derivation of the Y combinator
Curry's Y combinator may be readily obtained from the definition of y.^{[3]} Starting with,
A lambda abstraction does not support reference to the variable name, in the applied expression, so x must be passed in as a parameter to x. We can think of this as replacing x by x x, but formally this is not correct. Instead defining y by gives,
The let expression may be regarded as the definition of the function y, where z is the parameter. Instantiation z as y in the call gives,
And because the parameter z always passes the function y.
Using the eta reduction rule,
gives,
A let expression may be expressed as a lambda abstraction using,
gives,
This is possibly the simplest implementation of a fixed point combinator in lambda calculus. However one beta reduction gives the more symmetrical form of Curry's Y combinator.
See also translating between let and lambda expressions.
Other fixed-point combinators
In untyped lambda calculus fixed-point combinators are not especially rare. In fact there are infinitely many of them.^{[4]} In 2005 Mayer Goldberg showed that the set of fixed-point combinators of untyped lambda calculus is recursively enumerable.^{[5]}
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)))
The simplest fixed point combinator in the SK-calculus, found by John Tromp, is
- Y' = S S K (S (K (S S (S (S S K)))) K)
although note that it is not in normal form, which is longer. This combinator corresponds to the lambda expression
- Y' = (λx. λy. x y x) (λy. λx. y (x y x))
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:
- X = λf.(λx.x x) (λx.f (x x))
Another common fixed point combinator is the Turing fixed-point combinator (named after its discoverer, Alan Turing):
- Θ = (λx. λy. (y (x x y))) (λx. λy. (y (x x y)))
It also has a simple call-by-value form:
- Θ_{v} = (λx. λy. (y (λz. x x y z))) (λx. λy. (y (λz. x x y z)))
The analog for mutual recursion is a polyvariadic fix-point combinator, ^{[6]}^{[7]}^{[8]} which may be denoted Y*.
Strict fixed point combinator
The Z combinator will work in strict languages (or where normal order is applied). The Z combinator has the next argument defined explicitly, preventing the expansion of Z g in the right hand side of the definition:
- Z g v = g (Z g) v
and in lambda calculus is an eta-expansion:
- Z = λf.(λx.f (λv.((x x) v))) (λx.f (λv.((x x) v)))
Non-standard fixed-point combinators
In untyped lambda calculus there are terms that have the same Böhm tree as a fixed-point combinator, that is they have the same infinite extension λx.x (x (x ... )). These 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 equation that defines the "standard" ones. These strange combinators are called strictly non-standard fixed-point combinators; an example is the following combinator;
- N = B M (B (B M) B)
where,
- B = λx,y,z.x (y z)
- M = λx.x x
The set of non-standard fixed-point combinators is not recursively enumerable.^{[5]}
Implementation in other languages
Note that 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.
For examples of implementations of the fixed point combinators in various languages see,
Lazy functional implementation
In a language that supports lazy evaluation, like 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
. The definition is given here, followed by some usage examples.
fix :: (a -> a) -> a
fix f = f (fix f) -- Lambda lifted
-- alternative:
-- fix f = let x = f x in x -- Lambda dropped
fix (\x -> 9) -- this evaluates to 9
factabs fact 0 = 1 -- factabs is F from the lambda calculus example
factabs fact x = x * fact (x-1)
(fix factabs) 5 -- evaluates to 120
Strict functional implementation
In a strict functional language the argument to f is expanded beforehand, yielding an infinite call sequence,
- .
This may be resolved by defining fix with an extra parameter.
let rec fix f x = f (fix f) x (* note the extra x; here fix f = \x-> f (fix f) x *)
let factabs fact = function (* factabs has extra level of lambda abstraction *)
0 -> 1
| x -> x * fact (x-1)
let _ = (fix factabs) 5 (* evaluates to "120" *)
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.
template <typename R, typename D>
class fixer
{
public:
R fix(D x)
{
return f(x);
}
private:
virtual R f(D) = 0;
};
class fact : public fixer<long, long>
{
virtual long f(long x)
{
if (x == 0)
{
return 1;
}
return x * fix(x-1);
}
};
long result = fact().fix(5);
Typing
In polymorphic lambda calculus (System F) a polymorphic fixed-point combinator has type;
- ∀a.(a → a) → a
where a is a type variable. That is, fix takes a function, which maps a → a and uses it to return a value of type a.
In the simply typed lambda calculus extended with recursive 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^{[9]} because at some point it would deal with the self-application sub-term by the application rule:
where has the infinite type . 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 types, it is possible to type the Y combinator by appropriately accounting for the recursion at the type level. The need to self-apply the variable x can be managed using a type (Rec a), which is defined so as to be isomorphic to (Rec a -> a).
For example, in the following Haskell code, we have In
and out
being the names of the two directions of the isomorphism, with types:^{[10]}
In :: (Rec a -> a) -> Rec a
out :: Rec a -> (Rec a -> a)
which lets us write:
newtype Rec a = In { out :: Rec a -> a }
y :: (a -> a) -> a
y = \f -> (\x -> f (out x x)) (In (\x -> f (out x x)))
Or equivalently in OCaml:
type 'a recc = In of ('a recc -> 'a)
let out (In x) = x
let y f = (fun x a -> f (out x x) a) (In (fun x a -> f (out x x) a))
General information
The function for which any input is a fixed point is called the identity function. Formally:
Other functions have the special property that after being applied once, further applications don't have any effect. More formally:
Such functions are called idempotent (see also projection). An example of such a function is the function that returns 0 for all even integers, and 1 for all odd integers. This is akin to projection
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,^{[11]} without requiring native recursion support in the language.^{[12]}
The recursive join in relational databases implements a fixed point, by recursively adding records to a set until no more may be added.
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.^{[13]}^{[14]}
See also
Notes
- ↑ Peyton Jones, Simon L. (1987). The Implementation of Functional Programming. Prentice Hall International.
- ↑ Selinger, Peter. "Lecture Notes on Lambda Calculus (PDF)" (PDF). p. 6.
- ↑ http://math.stackexchange.com/questions/51246/can-someone-explain-the-y-combinator
- ↑ Bimbó, Katalin. Combinatory Logic: Pure, Applied and Typed. p. 48.
- 1 2 Goldberg, 2005
- ↑ Poly-variadic fix-point combinators
- ↑ Polyvariadic Y in pure Haskell98, lang.haskell.cafe, October 28, 2003
- ↑ Fixed point combinator for mutually recursive functions?
- ↑ An Introduction to the Lambda Calculus
- ↑ Haskell mailing list thread on How to define Y combinator in Haskell, 15 Sep 2006
- ↑ Daniel P. Friedman, Matthias Felleisen (1986). "Chapter 9 - Lambda The Ultimate". The Little Lisper. Science Research Associates. p. 179. "In the chapter we have derived a Y-combinator which allows us to write recursive functions of one argument withour using define."
- ↑ Mike Vanier. "The Y Combinator (Slight Return) or: How to Succeed at Recursion Without Really Recursing". "More generally, Y gives us a way to get recursion in a programming language that supports first-class functions but that doesn't have recursion built in to it."
- ↑ This terminology appear to be largely folklore, but it does appear in the following:
- Trey Nash, Accelerated C# 2008, Apress, 2007, ISBN 1-59059-873-3, p. 462—463. Derived substantially from Wes Dyer's blog (see next item).
- Wes Dyer Anonymous Recursion in C#, February 02, 2007, contains a substantially similar example found in the book above, but accompanied by more discussion.
- ↑ The If Works Deriving the Y combinator, January 10th, 2008
References
- Werner Kluge, Abstract computing machines: a lambda calculus perspective, Springer, 2005, ISBN 3-540-21146-2, pp. 73–77
- Mayer Goldberg, (2005) On the Recursive Enumerability of Fixed-Point Combinators, BRICS Report RS-05-1, University of Aarhus
- Matthias Felleisen. A Lecture on the Why of Y.
External links
Wikibooks has a book on the topic of: Haskell/Fix and recursion |
- http://www.latrobe.edu.au/philosophy/phimvt/joy/j05cmp.html
- http://okmij.org/ftp/Computation/fixed-point-combinators.html
- "Fixed-point combinators in Javascript"
- http://www.cs.brown.edu/courses/cs173/2002/Lectures/2002-10-28-lc.pdf
- http://www.mactech.com/articles/mactech/Vol.07/07.05/LambdaCalculus/
- http://www.csse.monash.edu.au/~lloyd/tildeFP/Lambda/Examples/Y/ (executable)
- http://www.ece.uc.edu/~franco/C511/html/Scheme/ycomb.html
- an example and discussion of a perl implementation
- "A Lecture on the Why of Y"
- "A Use of the Y Combinator in Ruby"
- "Functional programming in Ada"
- "Y Combinator in Erlang"
- "The Y Combinator explained with JavaScript"
- "The Y Combinator (Slight Return)" (detailed derivation)
- "The Y Combinator in C#"
- Rosetta code - Y combinator