fixed-point combinator in nLab (original) (raw)

Fixed-point combinators

Context

Type theory

natural deduction metalanguage, practical foundations

  1. type formation rule
  2. term introduction rule
  3. term elimination rule
  4. computation rule

type theory (dependent, intensional, observational type theory, homotopy type theory)

syntax object language

homotopy levels

semantics

Fixed-point combinators

Idea

In combinatory logic, in the λ \lambda -calculus, or more generally in type theory, a fixed-point combinator is a term YY which, when applied to a term nn, yields a term YnY n that is a fixed-point of nn:

n(Yn)=Yn. n (Y n) = Y n .

This equality is usually a directed beta-reduction as follows:

Yn→ βn(Yn). Y n \to_\beta n (Y n) .

Implementing general recursion

When “programming” in any of these systems, a fixed-point combinator serves as a mechanism for implementing general recursion. When writing a recursive function in a standard programming language, such as the factorial

def fact(n:nat) : nat = {
  if (n == 0) {
    return 1
  } else {
    return n * fact(n-1)
  }
}

one generally calls the function being defined inside of its own body. This is not possible for a combinator or a lambda-term to do directly, but it can be implemented using a fixed-point combinator. One first defines a “generator” which takes “the function to call recursively” as an additional argument:

def genfact(f : nat -> nat)(n:nat) : nat = {
  if (n == 0) {
    return 1
  } else {
    return n * f(n-1)
  }
}

and then “closes the loop” by applying the fixed-point combinator. That is, we curry genfact to view it as an endofunction of nat -> nat (an operator) and then construct its fixed point,

fact=Y(genfact). fact = Y(genfact).

The directed β\beta-reduction version of the fixed-point property of YY then implements the process of calling a function recursively:

fact(3) =Y(genfact)(3) → βgenfact(Y(genfact))(3) → β3*Y(genfact)(2) → β3*genfact(Y(genfact))(2) → β3*2*Y(genfact)(1) → β3*2*genfact(Y(genfact))(1) → β3*2*1*Y(genfact)(0) → β3*2*1*genfact(Y(genfact))(0) → β3*2*1*1 =6\begin{aligned} fact(3) &= Y(genfact)(3)\\ &\to_\beta genfact(Y(genfact))(3)\\ &\to_\beta 3 * Y(genfact)(2)\\ &\to_\beta 3 * genfact(Y(genfact))(2)\\ &\to_\beta 3 * 2 * Y(genfact)(1)\\ &\to_\beta 3 * 2 * genfact(Y(genfact))(1)\\ &\to_\beta 3 * 2 * 1 * Y(genfact)(0)\\ &\to_\beta 3 * 2 * 1 * genfact(Y(genfact))(0)\\ &\to_\beta 3 * 2 * 1 * 1\\ &= 6 \end{aligned}

In particular, observe that because general recursion allows the definition of nonterminating functions, so does a fixed-point combinator. An obvious example is the fixed point of the identity function II, which reduces as follows:

YI→ βI(YI)→ βYI→ βI(YI)→ βYI→ β⋯ Y I \to_\beta I(Y I) \to_\beta Y I \to_\beta I(Y I) \to_\beta Y I \to_\beta \cdots

Existence

There are many ways of constructing or otherwise obtaining a fixed-point combinator, varying with the formal system in which one works.

Unityped λ\lambda-calculus

In the unityped λ \lambda -calculus, a traditional construction (due to Curry) is

Y=λn.(λs.n(ss))(λs.n(ss)) Y = \lambda n. (\lambda s. n (s s)) (\lambda s. n (s s))

For a given term nn, put t=λs.n(ss)t = \lambda s. n (s s). We then have Yn=ttY n = t t, and we also have

Yn = (λs.n(ss))(λs.n(ss)) = (λs.n(ss))(t) = n(tt) = n(Yn) \array { Y n & = & (\lambda s. n (s s)) (\lambda s. n (s s)) \\ & = & (\lambda s. n (s s)) (t) \\ & = & n (t t) \\ & = & n (Y n) }

so that YnY n is a fixed point of nn. Compare Lawvere's proof of Cantor's theorem.

Another construction is due to (Klop 07):

Y K=LLLLLLLLLLLLLLLLLLLLLLLLLLY_K = L L L L L L L L L L L L L L L L L L L L L L L L L L

where

L=λabcdefghijklmnopqstuvwxyzr.r(thisisafixedpointcombinator)L = \lambda a b c d e f g h i j k l m n o p q s t u v w x y z r. r (t h i s i s a f i x e d p o i n t c o m b i n a t o r)

Note that Y KY_K is LL repeated 26 times, and the string thisisafixedpointcombinatorthisisafixedpointcombinator contains 27 characters. Thus

Y Kn = (λr.r(LLLLLLLLLLLLLLLLLLLLLLLLLLr))n = (λr.r(Y Kr))n = n(Y Kn)\array { Y_K n & = & (\lambda r. r(L L L L L L L L L L L L L L L L L L L L L L L L L L r)) n \\ & = & (\lambda r. r(Y_K r)) n \\ & = & n (Y_K n) }

Combinatory logic

In combinatory logic (based on the combinators SS, KK, and II), one construction is

S(K(SII))(S(S(KS)K)(K(SII))) S(K(S I I)) \big( S(S(K S)K)(K(S I I)) \big)

following the standard formulas Sxyz=(xz)(yz)S x y z = (x z)(y z), Kxy=xK x y = x and Ix=xI x = x, and where bracketings left unspecified are by convention to the left. For a derivation of this, see the article on combinatory algebra.

Typed λ\lambda-calculus

In many forms of (multi-) typed λ\lambda-calculus (and more general type theory), a fixed-point combinator cannot be constructed, because there is no type whose terms can be applied to themselves. This is usually intentional, because it avoids the nontermination inherent in the existence of a fixed-point combinator.

However, it is possible to add a fixed-point combinator to typed λ\lambda-calculus by fiat, obtaining a typed system which includes general recursion and hence nontermination. This is appropriate for some forms of domain semantics, and for modeling some real-world programming languages (Haskell is a notable example).

References

In type theory:

Last revised on January 28, 2023 at 15:45:00. See the history of this page for a list of all contributions to it.