Programming Language Concepts: Lecture 16 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 16, 23 March 2009
λ -calculus: syntax ◮ Assume a set Var of variables ◮ Set Λ of lambda expressions is given by Λ = x | λ x . M | MM ′ where x ∈ Var , M , M ′ ∈ Λ.
λ -calculus: syntax ◮ Assume a set Var of variables ◮ Set Λ of lambda expressions is given by Λ = x | λ x . M | MM ′ where x ∈ Var , M , M ′ ∈ Λ. ◮ Basic rule for computing (rewriting) is called β ( λ x . M ) M ′ → β M { x ← M ′ } ◮ M { x ← M ′ } : substitute free occurrences of x in M by M ′
λ -calculus: syntax ◮ Assume a set Var of variables ◮ Set Λ of lambda expressions is given by Λ = x | λ x . M | MM ′ where x ∈ Var , M , M ′ ∈ Λ. ◮ Basic rule for computing (rewriting) is called β ( λ x . M ) M ′ → β M { x ← M ′ } ◮ M { x ← M ′ } : substitute free occurrences of x in M by M ′ ◮ When we apply β to MM ′ , assume that we always rename the bound variables in M to avoid “capturing” free variables from M ′ .
Encoding arithmetic Church numerals � 0 � = λ fx . x � n + 1 � = λ fx . f ( � n � fx )
Encoding arithmetic Church numerals � 0 � = λ fx . x � n + 1 � = λ fx . f ( � n � fx ) ◮ Let g k y denote g ( g ( . . . ( gy ))) with k applications of g to y ◮ Show by induction that � n � = λ fx . f ( � n − 1 � fx ) → β . . . → β λ fx . ( f n x )
Encoding arithmetic functions . . . Successor ◮ succ ( n ) = n + 1 ◮ Define as λ pfx . f ( pfx )
Encoding arithmetic functions . . . Successor ◮ succ ( n ) = n + 1 ◮ Define as λ pfx . f ( pfx ) ( λ pfx . f ( pfx )) � n �
Encoding arithmetic functions . . . Successor ◮ succ ( n ) = n + 1 ◮ Define as λ pfx . f ( pfx ) ( λ pfx . f ( pfx )) � n � → β λ fx . f ( � n � fx )
Encoding arithmetic functions . . . Successor ◮ succ ( n ) = n + 1 ◮ Define as λ pfx . f ( pfx ) ( λ pfx . f ( pfx )) � n � → β λ fx . f ( � n � fx ) → β λ fx . f ( f n x )
Encoding arithmetic functions . . . Successor ◮ succ ( n ) = n + 1 ◮ Define as λ pfx . f ( pfx ) ( λ pfx . f ( pfx )) � n � → β λ fx . f ( � n � fx ) → β λ fx . f ( f n x ) = λ fx . f n +1 x
Encoding arithmetic functions . . . Successor ◮ succ ( n ) = n + 1 ◮ Define as λ pfx . f ( pfx ) ( λ pfx . f ( pfx )) � n � → β λ fx . f ( � n � fx ) → β λ fx . f ( f n x ) = λ fx . f n +1 x = � n +1 �
Encoding arithmetic functions . . . Successor ◮ succ ( n ) = n + 1 ◮ Define as λ pfx . f ( pfx ) ( λ pfx . f ( pfx )) � n � → β λ fx . f ( � n � fx ) → β λ fx . f ( f n x ) = λ fx . f n +1 x = � n +1 � plus: λ pqfx . pf ( qfx ).
Encoding arithmetic functions . . . Successor ◮ succ ( n ) = n + 1 ◮ Define as λ pfx . f ( pfx ) ( λ pfx . f ( pfx )) � n � → β λ fx . f ( � n � fx ) → β λ fx . f ( f n x ) = λ fx . f n +1 x = � n +1 � plus: λ pqfx . pf ( qfx ). ( λ pqfx . pf ( qfx )) � m �� n �
Encoding arithmetic functions . . . Successor ◮ succ ( n ) = n + 1 ◮ Define as λ pfx . f ( pfx ) ( λ pfx . f ( pfx )) � n � → β λ fx . f ( � n � fx ) → β λ fx . f ( f n x ) = λ fx . f n +1 x = � n +1 � plus: λ pqfx . pf ( qfx ). ( λ pqfx . pf ( qfx )) � m �� n � → β ( λ qfx . � m � f ( qfx )) � n �
Encoding arithmetic functions . . . Successor ◮ succ ( n ) = n + 1 ◮ Define as λ pfx . f ( pfx ) ( λ pfx . f ( pfx )) � n � → β λ fx . f ( � n � fx ) → β λ fx . f ( f n x ) = λ fx . f n +1 x = � n +1 � plus: λ pqfx . pf ( qfx ). ( λ pqfx . pf ( qfx )) � m �� n � → β ( λ qfx . � m � f ( qfx )) � n � → β ( λ fx . � m � f ( � n � fx ))
Encoding arithmetic functions . . . Successor ◮ succ ( n ) = n + 1 ◮ Define as λ pfx . f ( pfx ) ( λ pfx . f ( pfx )) � n � → β λ fx . f ( � n � fx ) → β λ fx . f ( f n x ) = λ fx . f n +1 x = � n +1 � plus: λ pqfx . pf ( qfx ). ( λ pqfx . pf ( qfx )) � m �� n � → β ( λ qfx . � m � f ( qfx )) � n � → β ( λ fx . � m � f ( � n � fx )) ( λ fx . � m � f ( f n x )) → β
Encoding arithmetic functions . . . Successor ◮ succ ( n ) = n + 1 ◮ Define as λ pfx . f ( pfx ) ( λ pfx . f ( pfx )) � n � → β λ fx . f ( � n � fx ) → β λ fx . f ( f n x ) = λ fx . f n +1 x = � n +1 � plus: λ pqfx . pf ( qfx ). ( λ pqfx . pf ( qfx )) � m �� n � → β ( λ qfx . � m � f ( qfx )) � n � → β ( λ fx . � m � f ( � n � fx )) ( λ fx . � m � f ( f n x )) → β ( λ fx . f m ( f n x )) → β
Encoding arithmetic functions . . . Successor ◮ succ ( n ) = n + 1 ◮ Define as λ pfx . f ( pfx ) ( λ pfx . f ( pfx )) � n � → β λ fx . f ( � n � fx ) → β λ fx . f ( f n x ) = λ fx . f n +1 x = � n +1 � plus: λ pqfx . pf ( qfx ). ( λ pqfx . pf ( qfx )) � m �� n � → β ( λ qfx . � m � f ( qfx )) � n � → β ( λ fx . � m � f ( � n � fx )) ( λ fx . � m � f ( f n x )) → β ( λ fx . f m ( f n x )) → β ( λ fx . f m + n x ) =
Encoding arithmetic functions . . . Successor ◮ succ ( n ) = n + 1 ◮ Define as λ pfx . f ( pfx ) ( λ pfx . f ( pfx )) � n � → β λ fx . f ( � n � fx ) → β λ fx . f ( f n x ) = λ fx . f n +1 x = � n +1 � plus: λ pqfx . pf ( qfx ). ( λ pqfx . pf ( qfx )) � m �� n � → β ( λ qfx . � m � f ( qfx )) � n � → β ( λ fx . � m � f ( � n � fx )) ( λ fx . � m � f ( f n x )) → β ( λ fx . f m ( f n x )) → β ( λ fx . f m + n x ) = � m + n � =
Encoding arithmetic functions . . . ◮ If functions are applied to meaningful terms we get meaningful answers!
Encoding arithmetic functions . . . ◮ If functions are applied to meaningful terms we get meaningful answers! Other functions: multiplication : λ pqfx . q ( pf ) x exponentiation : λ pq . ( pq )
One step reduction ◮ Can have other reduction rules like β
One step reduction ◮ Can have other reduction rules like β ◮ Observe that λ x . ( Mx ) and M are equivalent with respect to β -reduction
One step reduction ◮ Can have other reduction rules like β ◮ Observe that λ x . ( Mx ) and M are equivalent with respect to β -reduction ◮ New reduction rule η λ x . ( Mx ) → η M
One step reduction ◮ Can have other reduction rules like β ◮ Observe that λ x . ( Mx ) and M are equivalent with respect to β -reduction ◮ New reduction rule η λ x . ( Mx ) → η M ◮ Given basic rules β , η , . . . , we are allowed to use them “in any context”
One step reduction ◮ Can have other reduction rules like β ◮ Observe that λ x . ( Mx ) and M are equivalent with respect to β -reduction ◮ New reduction rule η λ x . ( Mx ) → η M ◮ Given basic rules β , η , . . . , we are allowed to use them “in any context” ◮ Define a one step reduction relation → inductively ′ M → x M ′ M → M x ∈ { β, η, . . . }
One step reduction ◮ Can have other reduction rules like β ◮ Observe that λ x . ( Mx ) and M are equivalent with respect to β -reduction ◮ New reduction rule η λ x . ( Mx ) → η M ◮ Given basic rules β , η , . . . , we are allowed to use them “in any context” ◮ Define a one step reduction relation → inductively ′ M → x M ′ M → M ′ M → M λ x . M → λ x . M ′ x ∈ { β, η, . . . }
One step reduction ◮ Can have other reduction rules like β ◮ Observe that λ x . ( Mx ) and M are equivalent with respect to β -reduction ◮ New reduction rule η λ x . ( Mx ) → η M ◮ Given basic rules β , η , . . . , we are allowed to use them “in any context” ◮ Define a one step reduction relation → inductively ′ M → x M ′ M → M ′ M → M ′ M → M λ x . M → λ x . M ′ MN → M ′ N x ∈ { β, η, . . . }
One step reduction ◮ Can have other reduction rules like β ◮ Observe that λ x . ( Mx ) and M are equivalent with respect to β -reduction ◮ New reduction rule η λ x . ( Mx ) → η M ◮ Given basic rules β , η , . . . , we are allowed to use them “in any context” ◮ Define a one step reduction relation → inductively ′ M → x M ′ M → M ′ M → M ′ N → N ′ M → M λ x . M → λ x . M ′ MN → M ′ N MN → MN ′ x ∈ { β, η, . . . }
Computability ◮ Church numerals encode n ∈ N
Computability ◮ Church numerals encode n ∈ N ◮ Can we encode computable functions f : N k → N ? ◮ Let � f � be the encoding of computable function f
Computability ◮ Church numerals encode n ∈ N ◮ Can we encode computable functions f : N k → N ? ◮ Let � f � be the encoding of computable function f ◮ Want � f �� n 1 �� n 2 � . . . � n k � → ∗ � f ( n 1 , n 2 , . . . , n k ) �
Computability ◮ Church numerals encode n ∈ N ◮ Can we encode computable functions f : N k → N ? ◮ Let � f � be the encoding of computable function f ◮ Want � f �� n 1 �� n 2 � . . . � n k � → ∗ � f ( n 1 , n 2 , . . . , n k ) � ◮ Note! currying . . .
Recommend
More recommend