Untyped Lambda Calculus

Lambda Terms

An intuitive thought: for xx, x2+1x^2+1 can be a function or merely a number.

Thus we distinguish them, by denoting the function by λx.x2+1\lambda x.x^2+1.

Definition 1. Let VV be an infinite variable set {x,y,z,}\{x,y,z,\dots\}, Λ\Lambda be a set of Lambda terms.

(1) If uVu\in V, then uΛu\in \Lambda.

(2)(juxtaposition for application) If M,NΛM,N\in \Lambda, then (M  N)Λ(M\;N)\in\Lambda.

(3) If xVx\in V, NΛN\in \Lambda, then (λx.N)Λ(\lambda x.N)\in\Lambda.

Or in short, Λ=V(ΛΛ)(λV.Λ)\Lambda=V|(\Lambda\Lambda)|(\lambda V.\Lambda).

Definition 2. Subterms.

(1) Sub(x)={x}\text{Sub}(x)=\{x\}, for xVx\in V

(2) Sub(M  N)=Sub(M)Sub(N){(M  N)}\text{Sub}(M\;N)=\text{Sub}(M)\cup\text{Sub}(N)\cup\{(M\;N)\}

(3) Sub(λx.M)=Sub(M){(λx.M)}\text{Sub}(\lambda x.M)=\text{Sub}(M)\cup\{(\lambda x.M)\}

proper: sub but not \equiv.

Reflexivity and Transitivity

\equiv says that two λ\lambda-terms are exactly the same.

Tree of (y(λx.(xz)))(y(\lambda x.(xz)))

(a: application, l: lambda)

  y
 /
a     x
 \   /
  l-a
     \
      z
   

Notation 3.

Definition 4. FV()FV(·)

(1) FV(x)={x}FV(x)=\{x\}

(2) FV(MN)=FV(M)FV(N)FV(MN)=FV(M)\cup FV(N)

(3) FV(λx.M)=FV(M){x}FV(\lambda x.M)=FV(M)\setminus\{x\}

α\alpha-Substitution

Definition 5. =α=_\alpha, or in modulo α\alpha, \equiv

x[x:=N]Nx[x:=N]\equiv N

To be noticed especially, [λy.M](x:=N)λz.(M[y:=z][x:=N])[\lambda y.M](x:=N)\equiv\lambda z.(M[y:=z][x:=N]), where zFV(N)z\notin FV(N).

Besides, L[x:=M][y:=N]L[y:=N][x:=M[y:=N]]L[x:=M][y:=N]\equiv L[y:=N][x:=M[y:=N]] when xFV(N)x\notin FV(N)

=α=_\alpha implies a renaming of bound variables and an isoporphism between expression trees.

β\beta-Reduction

Definition 6. β\to_\beta, one-step reduction (1) (λx.M)NβM[x:=N](\lambda x.M)N\to_\beta M[x:=N] (2) (Compatibility) MβNM\to_\beta N implies that MLβNLML\to_\beta NL, LMβLNLM\to_\beta LN, λx.Mβλx.N\lambda x.M\to_\beta\lambda x.N

(λx.M)N(\lambda x.M)N is called a reducible expression, or a redex; M[x:=N]M[x:=N] is called a contractum of the redex.

Definition 7. β\twoheadrightarrow_{\beta}, zero- or multi-step reduction

MM0βM1ββMnNM\equiv M_0\to_\beta M_1\to_\beta\dots\to_\beta M_n\equiv N

Reflexive and Transitive

Definition 8. β\beta-equality, =β=_\beta

MM0βM1ββMnNM\equiv M_0-_\beta M_1-_\beta\dots-_\beta M_n\equiv N

with the arrows either leftwards or rightwards.

Example 9. (λx.xy)zβzyβ(λx.zx)y(\lambda x.xy)z\to_\beta zy\leftarrow_\beta (\lambda x.zx)y

=β=_\beta is an equivalence relation.

Definition 10. β\beta-normal form

(1) MM is in β\beta-normal form if MM has no redex.

(2) MM is β\beta-normalizable if there exists NN such that MβNM\twoheadrightarrow_\beta N and NN is in β\beta-normal form.

Lemma 11. If MM is in β\beta-normal form, then MβNM\twoheadrightarrow_\beta N implies MNM\equiv N.

Astounding Example "Reduction"

Let N=λx.xxxN=\lambda x.xxx. NNβNNNβNN\to_\beta NNN\to_\beta\dots, an infinite reduction path.

Let M=(λu.v)((λx.xx)(λx.xx))M=(\lambda u.v)((\lambda x.xx)(\lambda x.xx)). From one hand, reducing λu.v\lambda u.v first yields vv. From the other hand, reducing (λx.xx)(λx.xx)(\lambda x.xx)(\lambda x.xx) first yields (λx.xx)(λx.xx)(\lambda x.xx)(\lambda x.xx).

Thus MM has both a β\beta-normal form and an infinite reduction path.

Definition 12. A λ\lambda-term MM is

(1) weakly normalising, if there is an NN such that MβNM\twoheadrightarrow_\beta N and NN is in β\beta-normal form.

(2) strongly normalising, if every reduction path starting from MM is finite.

MM is weakly normalising. And since the reductiong cannot go on indefinitely, strong->weak.

Theorem 13.(Church-Rosser) If MβN1M\twoheadrightarrow_\beta N_1 and MβN2M\twoheadrightarrow_\beta N_2, then there is a λ\lambda-term L s.t. N1βLN_1\twoheadrightarrow_\beta L and N2βLN_2\twoheadrightarrow_\beta L.

(For a given group GG, if N1G,N2GN_1\lhd G,N_2\lhd G then N1N1N2N_1\lhd N_1N_2 and N1N2N2GN_1\cap N_2\lhd N_2\lhd G.)

Corollary 14. If M=βNM=_\beta N then there is LL s.t. MβLM\twoheadrightarrow_\beta L and NβLN\twoheadrightarrow_\beta L.

(induction on the length of the reduction path, using CR Thm)

Fixed Point Thms

Theorem 15. (Fixed Point Thm) For any λ\lambda-term FF, there is a λ\lambda-term XX s.t. X=βFXX=_\beta FX.

Proof. Take X(λx.F(xx))(λx.F(xx))βF(λx.F(xx))(λx.F(xx))FXX\equiv(\lambda x.F(xx))(\lambda x.F(xx))\to_\beta F(\lambda x.F(xx))(\lambda x.F(xx))\equiv FX.

X(λx.F(xx))(λx.F(xx))βλy.(λx.y(xx))(λx.y(xx))F:LFX\equiv(\lambda x.F(xx))(\lambda x.F(xx))\leftarrow_\beta \lambda y.(\lambda x.y(xx))(\lambda x.y(xx))F:\equiv LF

Thus LFβXβFXβFLFLF\to_\beta X\to_\beta FX\leftarrow_\beta FLF, and LF=βFLFLF=_\beta FLF.

Key: Make XX into sth. like X=βXX=_\beta\dots X\dots, and let L=λu.uL=\lambda u.\dots u\dots

We have YY-combiantor

Lλf.(λx.f(xx))(λx.f(xx))λf.(λx.xx)(λx.f(xx))L\equiv\lambda f.(\lambda x.f(xx))(\lambda x.f(xx))\equiv\lambda f.(\lambda x.xx)(\lambda x.f(xx))

, s.t. LF=βF(LF)LF=_\beta F(LF) is the fixed point of FF.

A demo for factorial:

void f(int n, const std::function<int(int)> &g) {
    cout << g(n) << endl;
}

int main() {
    f(10, [](const auto &f) {
        return [&](const auto &x) {
            return x(x);
        }([&](const auto &x) -> std::function<int(int)> {
            return f([&](const int &n) {
                return x(x)(n);
            });
        });
    }([](const auto &f) {
        return [=](const int &n) {
            return n == 0 ? 1 : 2 * f(n - 1) + 3 ;
        };
    }));
    return 0;
}