p.1/65 Introduction to λ-calculus Ken-etsu FUJITA fujita@cs.gunma-u.ac.jp http://www.comp.cs.gunma-u.ac.jp/ fujita/ Department of Computer Science Gunma University
:Church 32, 36, 40; Curry 34 1. Universal Language Martin-Löf s Type Thoery 2. Entscheidungsproblem Turing Machine, recursive function, D 3. Abstract Prog. Lang. (Lisp, ML, etc.) 4. Logical Framework (PTS) Curry-Howard p.2/65
p.3/65 Church 41: The Calculi of Lambda-Conversion Underlying the formal calculi which we shall develop is the concept of a function,...... A function is a rule of correspondence by which when anything is given (as argument) another thing (the value of the function for that argument) may be obtained. That is, a function is an operation......
Church 41: The Calculi of Lambda-Conversion Underlying the formal calculi which we shall develop is the concept of a function,...... A function is a rule of correspondence by which when anything is given (as argument) another thing (the value of the function for that argument) may be obtained. That is, a function is an operation...... In particular it is not excluded that one of the elements of the range of arguments of a function f should be the function f itself. This possibility has frequently been denied,......... Here, however, we regard the operation or rule of correspondence, which constitutes the function, as being first given, and...... p.4/65
p.5/65 Barendregt 81: The Lambda Calculus The lambda calculus is a type free theory about functions as rule, rather than a graphs. Functions as rules is the old fashioned notion of function and refers to the process of going from argument to value, a process coded by a definition. the λ-calculus regards functions again as rules in order to stress their computational aspects. The functions as rules are considered in full generality.
Barendregt 81: The Lambda Calculus The lambda calculus is a type free theory about functions as rule, rather than a graphs. Functions as rules is the old fashioned notion of function and refers to the process of going from argument to value, a process coded by a definition. the λ-calculus regards functions again as rules in order to stress their computational aspects. The functions as rules are considered in full generality. This is the starting point of the type free λ-calcuus. In particular a function can be applied to itself. For the usual notion of function in mathematics (as in ZF set theory), this is impossible (because of the axiom of foundation). p.6/65
p.7/65 λ (λ-abstraction, Application) x + 1 x + 1 1 f : x x + 1
p.8/65 λ (λ-abstraction, Application) x + 1 x + 1 1 f : x x + 1 λ f = λx.(x + 1)
λ (λ-abstraction, Application) x + 1 x + 1 1 f : x x + 1 λ f = λx.(x + 1) λ λx.m N M x N M[x := N] (λx.(x + 1))(0) p.9/65 f(0) = 0 + 1 (λx.(x + 1))(0) = 0 + 1
p.10/65 Curry (Schönfinkel) (x,y) f(x,y)
p.11/65 Curry (Schönfinkel) (x,y) f(x,y) 1 ( ) f = λx.λy.f(x,y)
p.12/65 Curry (Schönfinkel) (x,y) f(x,y) 1 ( ) f = λx.λy.f(x,y) (f )(a) = (λx.λy.f(x,y))(a) = λy.f(a,y) (f (a))(b) = (λy.f(a,y))(b) = f(a,b)
p.13/65 λ (Var, Abst, App) Def. (λ-terms Λ) 1. x 0,x 1,... λ 2. M N λ (MN) λ 3. M λ x (λx.m) λ Λ M ::= x (MM) (λx.m)
λ (Var, Abst, App) Def. (λ-terms Λ) 1. x 0,x 1,... λ 2. M N λ (MN) λ 3. M λ x (λx.m) λ Λ M ::= x (MM) (λx.m) (λx.x) (λx.((x y) z)) (x(λx.(λy.(λz.z)))) application λx.x λy.y λx.xyz x(λx.λy.λz.z) M N λ abstraction λx.m p.14/65
p.15/65 Examples λy.y(y x) x 2 (λy.y(y x))f = f(f x) λ f λf.λx.f(f x), λf.λx.f(f(f x)) 2 3
p.16/65 Examples λy.y(y x) x 2 (λy.y(y x))f = f(f x) λ f λf.λx.f(f x), λf.λx.f(f(f x)) 2 3 (β-contraction, β-reduction) (λy.y(y x))f β (y(y x))[y := f] f(f x)
p.17/65 β β-reduction β Def. (β ( β ))1-1. (λx.m)n β M[x := N] 1-2. M β N λx.m β λx.n M β N MP β NP M β N PM β PN
p.18/65 β β-reduction β Def. (β ( β ))1-1. (λx.m)n β M[x := N] 1-2. M β N λx.m β λx.n M β N MP β NP M β N PM β PN 2. β def = β
β β-reduction β Def. (β ( β ))1-1. (λx.m)n β M[x := N] 1-2. M β N λx.m β λx.n M β N MP β NP M β N PM β PN 2. β def = β (λy.y(yx))n β (λy.x)n β (λx.(λy.yx)z)v β β (λx.xxy)(λx.xxy) β (y(yx))[y := N] N(Nx) x[y := N] x (λy.yv)z zv (λx.xxy)(λx.xxy)y... etc. p.19/65
p.20/65 β β-normal form Def. (β (β-nf)) (λx.m)n (β-redex)
p.21/65 β β-normal form Def. (β (β-nf)) (λx.m)n (β-redex) 1. zv is β-nf of (λx.(λy.yx)z)v 2. L (λx.xxy)(λx.xxy) has no β-nf L β Ly β Lyy β...
β β-normal form Def. (β (β-nf)) (λx.m)n (β-redex) 1. zv is β-nf of (λx.(λy.yx)z)v 2. L (λx.xxy)(λx.xxy) has no β-nf L β Ly β Lyy β... 3. β (λu.v)l β v (λu.v)l β (λu.v)(ly) β... 4. (λx.(λy.yx)z)v β (λy.yv)z β zv (λx.(λy.yx)z)v β (λx.zx)v β zv p.22/65
p.23/65 Church-Rosser Thm. (Church-Rosser Theorem for β ) P β M and P β N = T Λ. M β T and N β T
p.24/65 Church-Rosser Thm. (Church-Rosser Theorem for β ) P β M and P β N = T Λ. M β T and N β T Cor. (Uniqueness of β-nf if exists) P has β-nfs M and N = M N
Church-Rosser Thm. (Church-Rosser Theorem for β ) P β M and P β N = T Λ. M β T and N β T Cor. (Uniqueness of β-nf if exists) P has β-nfs M and N = M N Lem. (β-nf) 1. atom β-nf 2. M1,...,M n β-nf x Atom xm 1...M n β-nf M β-nf λx.m β-nf λx 1.λx 2...λx n.xm 1 M 2...M m β-nf for M i β-nf λx 1.λx 2...λx n.xm 1 M 2...M m HNF, Solvable p.25/65
p.26/65 β-equal β-convertible Def. (β-equal (= β )) P 0,...,P n (n 0). P = β Q def = 1. P 0 P 2. i (n 1). P i β P i+1 or P i+1 β P i 3. P n Q
p.27/65 β-equal Church-Rosser Def. (β-equal (= β )) P 0,...,P n (n 0). P = β Q def = 1. P 0 P 2. i (n 1). P i β P i+1 or P i+1 β P i 3. P n Q Thm. (Church-Rosser for = β ) P = β Q = T Λ. P β T and Q β T
β-equal Church-Rosser Def. (β-equal (= β )) P 0,...,P n (n 0). P = β Q def = 1. P 0 P 2. i (n 1). P i β P i+1 or P i+1 β P i 3. P n Q Thm. (Church-Rosser for = β ) P = β Q = T Λ. P β T and Q β T By induction on the number n IH: T n Λ. P β T n and P n β T n Show: P = β P n+1 = T n+1 Λ. P β T n+1 P n+1 β T n+1 Take T n+1 T n or T n+1 T from C-R for β p.28/65
p.29/65 Thm. (Fixed point theorem) M Λ. X Λ. MX = β X
p.30/65 Thm. (Fixed point theorem) M Λ. X Λ. MX = β X Let X Y M, where Y λf.(λx.f(xx))(λx.f(xx)). Then Y M = β (λx.m(xx))(λx.m(xx)) = β M((λx.M(xx))(λx.M(xx))) = β M(Y M)
p.31/65 Thm. (Fixed point theorem) M Λ. X Λ. MX = β X Let X Y M, where Y λf.(λx.f(xx))(λx.f(xx)). Then Y M = β (λx.m(xx))(λx.m(xx)) = β M((λx.M(xx))(λx.M(xx))) = β M(Y M) CF. Y Turing (λx.λf.f(xxf))(λx.λf.f(xxf)) Y Turing M β M(Y TuringM)
p.32/65 Functional Programming (1/4) t λx.λy.x, f λx.λy.y And λx.λy.xyf, Or λx.λy.xty, Not λx.xft And t B β B, Or f B β B, Not(And P Q) = β Or(Not P)(Not Q) if B then P else Q BPQ
Functional Programming (1/4) t λx.λy.x, f λx.λy.y And λx.λy.xyf, Or λx.λy.xty, Not λx.xft And t B β B, Or f B β B, Not(And P Q) = β Or(Not P)(Not Q) if B then P else Q BPQ M,N λx.xmn fst λx.xt, snd λx.xf fst M,N β M, M 1,M 2,...,M n snd M,N β N M 1, for n = 1 M 1, M 2,...,M n, for n > 1 p.33/65
p.34/65 (2/4) n f,f,...,f,i, where I λx.x }{{} n 0 I I, 1 f,i, 2 f,f,i, n +1 f,n
p.35/65 (2/4) n f,f,...,f,i, where I λx.x }{{} n 0 I I, 1 f,i, 2 f,f,i, n +1 f,n succ λx. f,x succ n β f,n n +1 pred λx.xf pred 0 β f, pred (n +1) β n IsZero λx.xt IsZero 0 β t, IsZero (n +1) β f n β-nf
p.36/65 λ-definable (3/4) Def. (λ (λ-definable)) λ G g λ = g : N k N λ G n, for g(n Gn 1... n k 1,...,n k ) = n β unsolvable, for g(n 1,...,n k ) undefined g λ def
p.37/65 λ-definable (3/4) Def. (λ (λ-definable)) λ G g λ = g : N k N λ G n, for g(n Gn 1... n k 1,...,n k ) = n β unsolvable, for g(n 1,...,n k ) undefined g λ Thm. (recursive λ-definable) g : N k N is a recursive function iff g is λ-definable def
p.38/65 (4/4) g(x, 0) = h(x) g(x,y + 1) = f(x,y,g(x,y)) m (λ ) m
p.38/65 (4/4) g(x, 0) = h(x) g(x,y + 1) = f(x,y,g(x,y)) m (λ ) m h,f λ H,F H m β h(m), F m n k β f(m,n,k)
p.39/65 (4/4) g(x, 0) = h(x) g(x,y + 1) = f(x,y,g(x,y)) m (λ ) m h,f λ H,F H m β h(m), F m n k β f(m,n,k) g λ G G x y β if (IsZero y)then (H x)else (F x (predy) (G x (predy)))
p.40/65 (4/4) g(x, 0) = h(x) g(x,y + 1) = f(x,y,g(x,y)) m (λ ) m h,f λ H,F H m β h(m), F m n k β f(m,n,k) g λ G G x y β if (IsZero y)then (H x)else (F x (predy) (G x (predy))) λ G M Y M
(4/4) g(x, 0) = h(x) g(x,y + 1) = f(x,y,g(x,y)) m (λ ) m h,f λ H,F H m β h(m), F m n k β f(m,n,k) g λ G G x y β if (IsZero y)then (H x)else (F x (predy) (G x (predy))) λ G M Y M M λg.λx.λy.if (IsZero y)then (H x)else (F x (predy) (g x (predy))) G Y Turing M β MG p.41/65
Types length : List(N) N [0,1] : List(N) length [0,1] : N ( ) member : N List(N) B Well-typed member 0 [1, 2, 3] plus : N N N, plus : R R R Curry-Howard Formulae-as-Types, Proofs-as-Programs A B B A ( E) f : A B fa : B a : A (app) p.42/65
p.43/65 λ Simply typed λ-calculus Def. ( (Types)) A ::= α (A A) (α α) α α (α (β α)) α β α ((α (β γ)) ((α β) (α γ))) (α β γ) (α β) α γ
p.44/65 λ Simply typed λ-calculus Def. ( (Types)) A ::= α (A A) (α α) α α (α (β α)) α β α ((α (β γ)) ((α β) (α γ))) (α β γ) (α β) α γ Def. ( (Terms)) à la Church M ::= x (λx:a.m) (MM) CF. à la Curry
p.45/65 λ Simply typed λ-calculus Def. ( (Types)) A ::= α (A A) (α α) α α (α (β α)) α β α ((α (β γ)) ((α β) (α γ))) (α β γ) (α β) α γ Def. ( (Terms)) à la Church Def. ( (Context)) M ::= x (λx:a.m) (MM) Γ ::= x 1 :A 1,x 2 :A 2,...,x n :A n
p.46/65 λ Derivation rules Def. ( Γ M : A) x:a Γ Γ x : A Γ,x:A M : B Γ λx:a.m : A B Γ M : A B Γ N : A Γ MN : B
p.47/65 λ Derivation rules Def. ( Γ M : A) x:a Γ Γ x : A Γ,x:A M : B Γ λx:a.m : A B Γ M : A B Γ N : A Γ MN : B x:a x : A λx:a.x : A A x:a,y:b x : A x:a λy:b.x : B A λx:a.λy:b.x : A B A
p.48/65 λ Derivation rules Def. ( Γ M : A) x:a Γ Γ x : A Γ,x:A M : B Γ λx:a.m : A B Γ M : A B Γ N : A Γ MN : B x:a x : A λx:a.x : A A x:a,y:b x : A x:a λy:b.x : B A λx:a.λy:b.x : A B A λx:a.x has type A A A B A is inhabited by λx:a.λy:b.x (A A) A is not inhabited
p.49/65 Static Properties Type checking problem Given Γ, M, and A, does one have Γ M : A? Typability problem Given Γ and M, does there exists Γ M :? Inhabitation problem Given A, does there eixists closed? : A
p.50/65 Static Properties, Formulae-as-Types (1/2) Type checking problem Given Γ, M, and A, does one have Γ M : A? Typability problem Given Γ and M, does there exists Γ M :? Inhabitation problem Given A, does there eixists closed? : A x 1 :A 1,...,x n :A n M : A 1. M is a program of output-type A from input-types A 1,...,A n 2. M is a proof of formula A from assumptions A 1,...,A n
p.51/65 Static Properties, Formulae-as-Types (2/2) Type checking problem = Is M a proof of A under Γ? Given Γ, M, and A, does one have Γ M : A? Typability problem = Find a formula with proof M Given Γ and M, does there exists Γ M :? Inhabitation problem = Find a proof of formula A Given A, does there eixists closed? : A x 1 :A 1,...,x n :A n M : A 1. M is a program of output-type A from input-types A 1,...,A n 2. M is a proof of formula A from assumptions A 1,...,A n
p.52/65 Examples: Formulae-as-Types λx:a B C.λy:A B.λz:A.xz(yz) : (A B C) (A B) A C
p.53/65 Examples: Formulae-as-Types λx:a B C.λy:A B.λz:A.xz(yz) : (A B C) (A B) A C [A B C] [A] [A B] [A] ( E) ( E) B C B ( E) C ( I) A C ( I) (A B) A C ( I) (A B C) (A B) A C
Examples: Formulae-as-Types λx:a B C.λy:A B.λz:A.xz(yz) : (A B C) (A B) A C [A B C] [A] [A B] [A] ( E) ( E) B C B ( E) C ( I) A C ( I) (A B) A C ( I) (A B C) (A B) A C [x : A B C] [z : A] [y : A B] [z : A] (app) (app) xz : B C yz : B (app) xz(yz) : C λx A.xz(yz) : A C (abst) λy A B.λx A.xz(yz) : (A B) A C (abst) λx A B C.λy A B.λx A.xz(yz) : (A B C) (A B) A C (abst) p.54/65
p.55/65 β-reduction Def. (β β ) (λx:a.m)n β M[x := N]
p.56/65 β-reduction Def. (β β ) [x : A] Π 1 M : B λx:a.m : A B (λx:a.m)n β M[x := N] (λx:a.m)n : B Π 2 N : A β Π 2 N : A Π 1 M[x := N] : B
p.57/65 β-reduction, Cut-elimination Def. (β β ) [x : A] Π 1 M : B λx:a.m : A B (λx:a.m)n β M[x := N] (λx:a.m)n : B Π 2 N : A β Π 2 N : A Π 1 M[x := N] : B [A] Π 1 B A B ( I) Π 2 A B ( E) Π 2 A Π 1 B
p.58/65 Fundamental Properties of λ : SN, CR Uniqueness of types Γ M : A and Γ M : B = A B
p.58/65 Fundamental Properties of λ : SN, CR Uniqueness of types Γ M : A and Γ M : B = A B Subject reduction Γ M : A and M β N = Γ N : A
p.58/65 Fundamental Properties of λ : SN, CR Uniqueness of types Γ M : A and Γ M : B = A B Subject reduction Γ M : A and M β N = Γ N : A Strong normalization Γ M : A = Every reduction sequence M β... is finite
Fundamental Properties of λ : SN, CR Uniqueness of types Γ M : A and Γ M : B = A B Subject reduction Γ M : A and M β N = Γ N : A Strong normalization Γ M : A = Every reduction sequence M β... is finite Terms may get larger, multiplied (λx.λy.x(xy))m β λy.m(my) New redexes may be created (λx.λy.x(xy))(λz.m) β λy.(λz.m)((λz.m)y) p.59/65
p.60/65 Strong Normalization Γ M : A = M SN
p.61/65 Strong Normalization Γ M : A = M SN By induction on the derivation? x:a Γ Γ x : A Γ,x:A M : B Γ λx:a.m : A B Γ M : A B Γ N : A Γ MN : B IH: M SN is too weak!
Strong Normalization Γ M : A = M SN By induction on the derivation? x:a Γ Γ x : A Γ,x:A M : B Γ λx:a.m : A B Γ M : A B Γ N : A Γ MN : B IH: M SN is too weak! A set of SN terms closed under head β-expansion SAT def = set of all saturated sets Interpret type A by saturated set [A] SAT [[A B]] = {M Λ N [[A]]. MN [[B]]} M : A = M [A] SN p.62/65
p.63/65 Tiny Demo: ML, Agda ML > fn x => fn y => x; val it = fn : a -> b -> a > fn x => fn y => fn z => x z (y z); val it = fn : ( a -> b -> c) -> ( a -> b) -> a -> c Martin-Löf s Type Theory Agda data (\/) (A,B::Set) = inl (a::a) inr (b::b) or_elim (A,B,C::Set) :: A \/ B -> (A -> C) -> (B -> C) -> C or_elim p q r = case p of (inl a)-> q a (inr b)-> r b data (/\) (A,B::Set) = pair (a::a) (b::b) and_elim (A,B,C::Set) :: A /\ B -> (A -> B -> C) -> C and_elim p f = case p of (pair a b)-> f a b
p.64/65 1., 2. J.R. Hindley and J.P. Seldin: Introduction to Combinators and λ-calculus, Cambridge Univ. 3. H.P. Barendregt: Lambda Calculi with Types, Handbook of Logic in Computer Science Vol. 2, Oxford Univ. 4. J.-Y. Girard, P. Taylor and Y. Lafont: Proofs and Types, Cambridge Univ. 5. H.P. Barendregt: The Lambda Calculus Its Syntax and Semantics, North-Holland
p.65/65 1. Poly/ML http://www.polyml.org/ 2. Agda http://www.cs.chalmers.se/cs /Research/Logic/TypesSS05/software.html 3. Coq http://coq.inria.fr/ 4. Isabelle http://www.cl.cam.ac.uk/research/hvg/isabelle/ 5. Mizar http://mizar.uwb.edu.pl/ 6. PVS http://pvs.csl.sri.com/ 7. The Seventeen Provers of the World http://www.cs.ru.nl/ freek/comparison/