Incombinatory logic forcomputer science, afixed-point combinator (orfixpoint combinator)[1]: p.26 is ahigher-order function (i.e., afunction that takes a function asargument) that returns somefixed point (a value that is mapped to itself) of its argument function, if one exists.
Formally, if is a fixed-point combinator and the function has one or more fixed points, then is one of these fixed points, i.e.,
Fixed-point combinators can be defined in thelambda calculus and infunctional programming languages, and provide a means to allow forrecursive definitions.
In the classical untypedlambda calculus, every function has a fixed point. A particular implementation of isHaskell Curry's paradoxical combinator Y, given by[2]: 131 [note 1][note 2]
(Here using the standard notations and conventions of lambda calculus: Y is a function that takes one argumentf and returns the entire expression following the first period; the expression denotes a function that takes one argumentx, thought of as a function, and returns the expression, where denotesx applied to itself. Juxtaposition of expressions denotesfunction application, is left-associative, and has higher precedence than the period.)
The following calculation verifies that is indeed a fixed point of the function:
| by the definition of | ||
| byβ-reduction: replacing the formal argumentf of Y with the actual argumentg | ||
| by β-reduction: replacing the formal argumentx of the first function with the actual argument | ||
| by second equality, above |
The lambda term may not, in general,β-reduce to the term. However, both terms β-reduce to the same term, as shown.
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 awhile or afor loop in an imperative language.
Used in this way, the Y combinator implements simplerecursion. The lambda calculus does not allow a function to appear as a term in its own definition as is possible in manyprogramming 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 implementingCurry'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 inmathematical logic.
An example implementation of Y in the languageR is presented below:
Y<-\(f){g<-\(x)f(x(x))g(g)}
This can then be used to implement factorial as follows:
fact<-\(f)\(n)if(n==0)1elsen*f(n-1)Y(fact)(5)# yields 5! = 120
Y is only needed when function names are absent. Substituting all the definitions into one line so that function names are not required gives:
(\(f)(\(x)f(x(x)))(\(x)f(x(x))))(\(f)\(n)if(n==0)1elsen*f(n-1))(5)
This works because R useslazy evaluation.
Languages that usestrict evaluation, such asPython,C++, and otherstrict programming languages, can often express Y; however, any implementation is useless in practice since itloops indefinitely until terminating via astack overflow.
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:
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, likeChurch 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.
Many functions do not have any fixed points, for instance with. UsingChurch encoding, natural numbers can be represented in lambda calculus, and this functionf can be defined in lambda calculus. However, itsdomain will now containall lambda expressions, not just those representing natural numbers. The Y combinator, applied tof, will yield a fixed-point forf, but this fixed-point won't represent anatural number. If trying to compute Yf in an actual programming language, an infinite loop will occur.
The fixed-point combinator may be defined in mathematics and then implemented in other languages. General mathematics defines a function based on itsextensional properties.[3] That is, two functions are equal if they perform the same mapping. Lambda calculus and programming languages regard function identity as anintensional 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.
Combinatory logic is ahigher-order functions theory. Acombinator is aclosed lambda expression, meaning that it has nofree variables. The combinators may be combined to direct values to their correct places in the expression without ever naming them as variables.
Fixed-point combinators can be used to implementrecursive definition of functions. However, they are rarely used in practical programming.[4]Strongly normalizingtype systems such as thesimply 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.[1]: page 232
Thefactorial 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
wheren is a non-negativeinteger.Implementing this in lambda calculus, where integers are represented usingChurch 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 as follows.
Define a functionF of two argumentsf andn:
(Here is a function that takes two arguments and returns its first argument ifn=0, and its second argument otherwise; evaluates ton–1.)
Now define. Then is a fixed-point ofF, which gives
as desired.
The Y combinator, discovered byHaskell Curry, is defined as
In untyped lambda calculus fixed-point combinators are not especially rare. In fact there are infinitely many of them.[5] In 2005 Mayer Goldberg showed that the set of fixed-point combinators of untyped lambda calculus isrecursively enumerable.[6]
The Y combinator can be expressed in theSKI-calculus as
Additional combinators (B, C, K, W system) allow for much shorter encodings. With the self-application combinator, since and, the above becomes
The shortest fixed-point combinator in the SK-calculus using S and K combinators only, found byJohn Tromp, is
although note that it is not in normal form, which is longer. This combinator corresponds to the lambda expression
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:
Another common fixed-point combinator is the Turing fixed-point combinator (named after its discoverer,Alan Turing):[7][2]: 132
Its advantage over is that beta-reduces to,[note 3]whereas and only beta-reduce to a common term.
also has a simplecall-by-value form:
The analog formutual recursion is apolyvariadic fix-point combinator,[8][9][10] which may be denoted Y*.
In astrict programming language the Y combinator will expand untilstack overflow, or never halt in case oftail call optimization.[11] The Z combinator will work instrict languages (also called eager languages, where applicative evaluation order is applied). The Z combinator has the next argument defined explicitly, preventing the expansion of in the right-hand side of the definition:[12]
and in lambda calculus it is aneta-expansion of the Y combinator:
If F is a fixed-point combinator in untyped lambda calculus, then there is:
Terms that have the sameBöhm tree as a fixed-point combinator, i.e., have the same infinite extension, are callednon-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 calledstrictly non-standard fixed-point combinators; an example is the following combinator:
where
since
where are modifications of created on the fly which add instances of at once into the chain while being replaced with.
The set of non-standard fixed-point combinators is notrecursively enumerable.[6]
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 someprogramming paradigms are given below.
In a language that supportslazy evaluation, as inHaskell, it is possible to define a fixed-point combinator using the defining equation of the fixed-point combinator which is conventionally namedfix. Since Haskell has lazydata 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:[13]
fix,fix'::(a->a)->afixf=letx=fxinx-- Lambda dropped. Sharing.-- Original definition in Data.Function.-- alternative:fix'f=f(fix'f)-- Lambda lifted. Non-sharing.fix(\x->9)-- this evaluates to 9fix(\x->3:x)-- evaluates to the lazy infinite list [3,3,3,...]fact=fixfac-- evaluates to the factorial functionwherefacf0=1facfx=x*f(x-1)fact5-- evaluates to 120
In a strict functional language, as illustrated below withOCaml, the argument tof is expanded beforehand, yielding an infinite call sequence,
This may be resolved by defining fix with an extra parameter.
letrecfixfx=f(fixf)x(* note the extra x; hence fix f = \x-> f (fix f) x *)letfactabsfact=function(* factabs has extra level of lambda abstraction *)0->1|x->x*fact(x-1)let_=(fixfactabs)5(* evaluates to "120" *)
In a multi-paradigm functional language (one decorated with imperative features), such asLisp,Peter Landin suggested the use of a variable assignment to create a fixed-point combinator,[14] as in the below example usingScheme:
(defineY!(lambda(f)((lambda(g)(set!g(f(lambda(x)(gx))));; (set! g expr) assigns g the value of expr,g);; replacing g's initial value of #f, creating#f)));; thus the truly self-referential value in g
Using a lambda calculus with axioms for assignment statements, it can be shown thatY! satisfies the same fixed-point law as the call-by-value Y combinator:[15][16]
In more idiomatic modern Scheme usage, this would typically be handled via aletrec expression, aslexical scope was introduced to Lisp in the 1970s:
(defineY*(lambda(f)(letrec;; (letrec ((g expr)) ...) locally defines g((g;; as expr recursively: g in expr refers to(f(lambda(x)(gx)))));; that same g being defined, g = f (λx. g x)g)));; ((Y* f) ...) = (g ...) = ((f (λx. g x)) ...)
Or without the internal label:
(defineY*(lambda(f)((lambda(i)(ii))(lambda(i)(f(lambdax(apply(ii)x)))))))
This example is a slightly interpretive implementation of a fixed-point combinator. A class is used to contain thefix() function, calledFixedPointCombinator. The function to be fixed is contained in a class that inherits from fixer. Thefix() function accesses the function to be fixed using aconcept to callapply(). As for the strict functional definition,fix() is explicitly given an extra parameterx, which means that lazy evaluation is not needed.
usingstd::same_as;template<typenameRet,typenameArg,typenameT>conceptFixedPointApplicable=requires(Argx){{T::apply(x)}->same_as<Ret>;};template<typenameRet,typenameArg,FixedPointApplicable<Ret,Arg>Derived>classFixedPointCombinator{public:staticRetfix(Argx)noexcept{returnDerived::apply(x);}};classFactorial:publicFixedPointCombinator<long,long,Factorial>{staticlongapply(longx)noexcept{if(x==0){return1;}returnx*fix(x-1);}};longresult=Factorial::fix(5);
Using only lambdas, one can create a fixed-point combinator like so:
autofix=[](autof){return[f](auto&&...args)->decltype(auto){returnf(f,std::forward<decltype(args)>(args)...);};};autofactorial=fix([](autoself,longn)->long{returnn==0?1:n*self(self,n-1);});std::println("5! = {}",factorial(5));// prints 120
Another example can be shown to demonstrateSKI combinator calculus (with given bird name fromcombinatory logic) being used to build up Z combinator to achievetail call-like behavior through trampolining:
// CombinatorsconstK=<A,B>(a:A)=>(_b:B)=>a;// KestrelconstS=<A,B,C>(a:(x:C)=>(y:B)=>A)=>(b:(x:C)=>B)=>(c:C)=>a(c)(b(c));// Starling// Derived combinatorsconstI=S(K)(K);// IdentityconstB=S(K(S))(K);// BluebirdconstC=S(B(B)(S))(K(K));// CardinalconstW=C(S)(I);// WarblerconstT=C(I);// ThrushconstV=B(C)(T);// VireoconstI1=C(C(I));// Identity Bird Once Removed; same as C(B(B)(I))(I)constC1=B(C);// Cardinal Once RemovedconstR1=C1(C1);// Robin Once RemovedconstV1=B(R1)(C1);// Vireo Once RemovedconstI2=R1(V);// Identity Bird Twice Removed// Z combinatorsconstZ=B(W(I1))(V1(B)(W(I2)));constZ2=S(K(S(S(K(S(S(K)(K))(S(K)(K))))(S(K(S(K(S))(K)))(S(K(S(S(K)(K))))(K))))(K(S(S(K))))))(S(S(K(S(S(K(S(K(S))(K)))(S))(K(K))))(S(K(S(S(K(S(K(S))(K)))(S))(K(K))))(S(K(S))(K))))(K(S(S(K(S(S(K)(K))(S(K)(K))))(S(K(S(K(S))(K)))(S(K(S(S(K)(K))))(K))))(K(S(K(S(S(K(S(S(K(S(K(S))(K)))(S))(K(K))))(S(K(S(S(K(S(K(S))(K)))(S))(K(K))))(S(K(S(S(K)(K))))(K))))))(K))))));// Alternative fully expanded form.constZ3=S(S(K(S(S)(K(S(S(K)(K))(S(K)(K))))))(K))(S(S(K(S))(K))(K(S(S(K(S))(S(K(S(K(S(K(S(K(S(S)(K(K))))(K)))(S)))(S(S(K)(K)))))(K)))(K(K(S(S(K)(K))(S(K)(K))))))));// Another shorter version.consttrampoline=<T>(fn:T|(()=>T)):T=>{letctx=fn;while(typeofctx==="function"){ctx=(ctxas()=>T)();}returnctx;};constcountFn=(self:(n:number)=>any)=>(n:number):any=>n===0?(console.log(n),n):()=>self(n-1);// Return thunk "() => self(n - 1)" instead.// Examplestrampoline(Z(countFn)(10));trampoline(Z2(countFn)(10));trampoline(Z3(countFn)(10));
InSystem F (polymorphic lambda calculus) a polymorphic fixed-point combinator has type[17]
where is atype variable. That is, if the type of fulfilling the equation is—the most general type—then the type of is. So then, takes a function that maps to and uses it to return a value of type.
In the simply typed lambda calculus extended withrecursive 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 thesimply typed lambda calculus, the fixed-point combinator Y cannot be assigned a type[18] 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.
In programming languages that support namedrecursive data types, the unbounded recursion in, which creates the would-be infinite type, is broken by naming the type explicitly, as e.g. type which is defined so as to be isomorphic to (or just to be a synonym of) the type. Thus is recognized as a recursive type. A datum (value) of the type is created by simply tagging a value that is a function that has the type, by the data constructor tag for the type.
For example, in the following Haskell code, letRec be that tag name, and thusRec andapp be the names of the two directions of the isomorphism, with types:[19][20]
Rec::(Ra->a)->Raapp::Ra->(Ra->a)
(where means "has type"). This lets us write:
newtypeRa=Rec{app::Ra->a}-- app (Rec g) = g -- g :: R a -> a-- Rec g :: R a-- app :: R a -> (R a -> a)y::(a->a)->ayf=(\x->f(appxx))(Rec(\x->f(appxx)))-- x :: R a-- app x :: R a -> a-- app x x :: a
The usual lambda-calculus definition (where, and thus the term witnesses the equivalence) contains at its core the self-application, which is untypeable in the statically typed programming languages. But the expressionapp x x aboveis typeable. Equivalently, we can redefine the self-application combinator and use it as
U'g=g(Recg)yf=U'(\Recg->f(U'g))-- g (Rec g) = f (g (Rec g))U''x=appxxyf=U''(Rec(\x->f(U''x)))-- app x x = f (app x x)
Or in OCaml:
type'arecc=Inof('arecc->'a)letout(Inx)=xletyf=(funxa->f(outxx)a)(In(funxa->f(outxx)a))
Alternatively:
letyf=(funx->f(funz->outxxz))(In(funx->f(funz->outxxz)))
Some languages allow for explicitly marking general types as being recursive even without naming them. Doing that also allows for defining the combinator in such languages.
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 infixed-point iteration,iterative methods,recursive join inrelational databases,data-flow analysis, FIRST and FOLLOW sets of non-terminals in acontext-free grammar,transitive closure, and other types ofclosure operations.
A function for whichevery input is a fixed point is called anidentity function. Formally:
In contrast touniversal quantification over all, a fixed-point combinator constructsone value that is a fixed point of. The remarkable property of a fixed-point combinator is that it constructs a fixed point for anarbitrary given function.
Other functions have the special property that, after being applied once, further applications don't have any effect. More formally:
Such functions are calledidempotent (see alsoProjection (mathematics)). An example of such a function is the function that returns 0 for all even integers, and 1 for all odd integers.
Inlambda 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
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 insimply typed lambda calculus.
The Y combinator allowsrecursion to be defined as a set ofrewrite rules,[21] without requiring native recursion support in the language.[22]
In programming languages that supportanonymous functions, fixed-point combinators allow the definition and use of anonymousrecursive functions, i.e., without having tobind such functions toidentifiers. In this setting, the use of fixed-point combinators is sometimes calledanonymous recursion.[note 4][23]
In the chapter we have derived a Y-combinator which allows us to write recursive functions of one argument without using define.
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.