Untyped Lambda Calculus
Lambda Terms
An intuitive thought: for x, x2+1 can be a function or merely a number.
Thus we distinguish them, by denoting the function by λx.x2+1.
Definition 1. Let V be an infinite variable set {x,y,z,…}, Λ be a set of Lambda terms.
(1) If u∈V, then u∈Λ.
(2)(juxtaposition for application) If M,N∈Λ, then (MN)∈Λ.
(3) If x∈V, N∈Λ, then (λx.N)∈Λ.
Or in short, Λ=V∣(ΛΛ)∣(λV.Λ).
Definition 2. Subterms.
(1) Sub(x)={x}, for x∈V
(2) Sub(MN)=Sub(M)∪Sub(N)∪{(MN)}
(3) Sub(λx.M)=Sub(M)∪{(λx.M)}
proper: sub but not ≡.
Reflexivity and Transitivity
≡ says that two λ-terms are exactly the same.
Tree of (y(λx.(xz)))
(a: application, l: lambda)
y
/
a x
\ /
l-a
\
z
Notation 3.
- It's recommended that you should not omit the parentheses.
- Application is left-associative, with MNL denoting ((MN)L)
- Abstruction is right-associative, with λxy.M denoting (λx.(λy.M))
- Application takes precedence over abstraction, with λx.MN denoting λx.(MN)
Definition 4. FV(⋅)
(1) FV(x)={x}
(2) FV(MN)=FV(M)∪FV(N)
(3) FV(λx.M)=FV(M)∖{x}
α-Substitution
Definition 5. =α, or in modulo α, ≡
x[x:=N]≡N
To be noticed especially, [λy.M](x:=N)≡λz.(M[y:=z][x:=N]), where z∈/FV(N).
Besides, L[x:=M][y:=N]≡L[y:=N][x:=M[y:=N]] when x∈/FV(N)
=α implies a renaming of bound variables and an isoporphism between expression trees.
β-Reduction
Definition 6. →β, one-step reduction
(1) (λx.M)N→βM[x:=N]
(2) (Compatibility) M→βN implies that ML→βNL, LM→βLN, λx.M→βλx.N
(λx.M)N is called a reducible expression, or a redex; M[x:=N] is called a contractum of the redex.
Definition 7. ↠β, zero- or multi-step reduction
M≡M0→βM1→β⋯→βMn≡N
Reflexive and Transitive
Definition 8. β-equality, =β
M≡M0−βM1−β⋯−βMn≡N
with the arrows either leftwards or rightwards.
Example 9. (λx.xy)z→βzy←β(λx.zx)y
=β is an equivalence relation.
Definition 10. β-normal form
(1) M is in β-normal form if M has no redex.
(2) M is β-normalizable if there exists N such that M↠βN and N is in β-normal form.
Lemma 11. If M is in β-normal form, then M↠βN implies M≡N.
Astounding Example "Reduction"
Let N=λx.xxx. NN→βNNN→β…, an infinite reduction path.
Let M=(λu.v)((λx.xx)(λx.xx)).
From one hand, reducing λu.v first yields v.
From the other hand, reducing (λx.xx)(λx.xx) first yields (λx.xx)(λx.xx).
Thus M has both a β-normal form and an infinite reduction path.
Definition 12. A λ-term M is
(1) weakly normalising, if there is an N such that M↠βN and N is in β-normal form.
(2) strongly normalising, if every reduction path starting from M is finite.
M is weakly normalising. And since the reductiong cannot go on indefinitely, strong->weak.
Theorem 13.(Church-Rosser) If M↠βN1 and M↠βN2, then
there is a λ-term L s.t. N1↠βL and N2↠βL.
(For a given group G, if N1⊲G,N2⊲G then N1⊲N1N2 and N1∩N2⊲N2⊲G.)
Corollary 14. If M=βN then there is L s.t. M↠βL and N↠βL.
(induction on the length of the reduction path, using CR Thm)
Fixed Point Thms
Theorem 15. (Fixed Point Thm) For any λ-term F, there is a λ-term X s.t. X=βFX.
Proof. Take X≡(λx.F(xx))(λx.F(xx))→βF(λx.F(xx))(λx.F(xx))≡FX.
X≡(λx.F(xx))(λx.F(xx))←βλy.(λx.y(xx))(λx.y(xx))F:≡LF
Thus LF→βX→βFX←βFLF, and LF=βFLF.
Key: Make X into sth. like X=β…X…, and let L=λu.…u…
We have Y-combiantor
L≡λf.(λx.f(xx))(λx.f(xx))≡λf.(λx.xx)(λx.f(xx))
, s.t. LF=βF(LF) is the fixed point of F.
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;
}