Documentos de Académico
Documentos de Profesional
Documentos de Cultura
. . ..
. .
1 / 38
May 9, 2011
References
Church, A.: The Calculi of Lambda Conversion. Princeton University Press, Princeton, N.J., 1941. Barendregt, H.P.: The lambda calculus: its syntax and semantics, Studies in logic and the foundations of mathematics, v.103, North-Holland, 1984. (ISBN 044487 5085) Turner, D.A. : A New Implementation Technique for Applicative Languages, Software-Practice and Experience, Vol.9, 31-49 (1979)
2 / 38
Lambda Calculus
Functional languages have been inuenced from Churchs work on the lambda calculus.
No computers when the theory was developed in 1932.
Lambda Calculus
. Denition (lambda expression) . .. x Ide Identiers () e Exp Lambda expressions () Syntax () e ::= x Variable | e1 e2 Application () | x.e Abstraction () . .. . . Lambda expression in Haskell . .. type Ide = String data Exp = Var Ide | App Exp Exp | Lam Ide Exp deriving Show -- derives default instance . -- of class Show .. .
This lambda calculus is sometimes called Pure Untyped Lambda Calculus to distinguish it from other versions developed later. Application assumed to be left associative; (e1 e2 e3 ) is the same as ((e1 e2 ) e3 )
4 / 38
Exercises .. Write a Haskell expression for (y.x)(x.x)x. 1 . Write a lambda expression for Haskell expression 2 .
. . .
App(App(Var"f")(Lam"x"(App(Var"x")(Var"y"))))(Var"z") . By removing deriving Show clause in Exp and dening our own show 3 . function, we have a GHCi session as shown below. . Instance declaration Show Exp . .. instance Show Exp where . show e = ... .. . . GHCi Session . .. *LC> App (Var "x") (App (Var "y") (Var "z")) x (y z) *LC> Lam "x" (Lam "y" (Var "x")) \ . x -> \ y -> x . . . and returns a Write a Haskell function show above which takes an Exp string representing corresponding lambda expression. Abstraction expression x.e should be shown as \x -> e.
5 / 38
Free Variables
Rewrite rules depend on substitution [e/x]e : substitute an expression e for all free occurrences of x in an expression e .
. Denition (free variables) . .. fv(x) = {x} fv(e1 e2 ) = fv(e1 ) fv(e2 ) . fv(x.e) = fv(e) {x} .. . . Haskell function fv . .. fv :: Exp -> [Ide] fv (Var x) = [x] fv (App e1 e2) = union (fv e1) (fv e2) fv (Lam x e) = delete x (fv e) . .. .
. Note on Haskell module Data.List . .. Import declaration import Data.List required for union and delete . . .. .
.
6 / 38
Substitution
Substitution must be careful to avoid name conicts.
. Haskell function subst for [e/x]e . .. -- substitute x with e in e' subst :: (Ide, Exp) -> Exp -> Exp subst s@(x,e) e'@(Var y) | x==y = e | otherwise = e' subst s@(x,e) e'@(App e1 e2) = App (subst s e1) (subst s e2) subst s@(x,e) e'@(Lam y e'') | x==y = e' | y `notElem` (fv e) = Lam y (subst s e'') | otherwise = Lam z (subst s (subst (y, Var z) e'') ) where z = newIde (y: (fv e)) -- generate a new Ide which does not appear in xs newIde :: [Ide] -> Ide newIde xs = concat xs . .. .
.
7 / 38
Exercises . Find the free variables of (y.x)(x.x)x and f (x.xy)z, respectively. 1 . . Apply a substitution [y/x] to a lambda expression (y.x)(x.x)x. 2 .
. .
Conrm that our Haskell function subst ("x", Var "y") transforms the expression into the result you obtain by hand. The Haskell function newIde above is somewhat tricky; if we have a counter to generate new identiers, the function would be dened as follows. Complete the following denition of auxiliary function subst'. . Alternative subst denition . .. subst :: (Ide, Exp) -> Exp -> Exp subst s e' = e'' where (e'', _) = subst' s e' 0 subst' :: (Ide, Exp) -> Exp -> Int -> (Exp,Int) subst' s@(x,e) e'@(Var y) c | x==y = (e, c) | otherwise = (e', c) subst' ... newIde :: Int -> (Ide, Int) newIde c = ("x"++show c, c+1) . .. . .
8 / 38
...
3
. . ..
9 / 38
Reduction
The notion of reduction is the same as convertibility, but restricted that - and -conversions only happen in one direction.
. Denition ( -reduction) .. ( . x.e1 ) e2 [e2 /x]e1 ..
. betaReduce for -reduction . .. -- returns [e] if reducible, [] otherwise betaReduce :: Exp -> [Exp] betaReduce (App (Lam x e1) e2) = [subst (x,e2) e1] betaReduce _ = [] . .. . . etaReduce for -reduction . .. -- returns [e] if reducible, [] otherwise etaReduce :: Exp -> [Exp] etaReduce (Lam x (App e (Var y))) | (x==y) && (x `notElem` fv(e)) = [e] | otherwise = [] etaReduce _ = [] . .. .
. . .
10 / 38
Exercises . Perform a single -reduction step on the outermost expression of each 1 . expression below.
(y.x y) (x.x) (x.x x) (y.y y) (y.x y) ((x.x x) (y.y y)) (y.x y) (x.x x) (y.y y) (y.x) (x.x) x
...
2
Perform a single -reduction step on the outermost expression of each expression below.
11 / 38
Redex
Reducible expression is called redex. There may be redexes inside an expression.
Example: (y.x) (x.x) x is of the form of an application (e x) which is not -reducible, while e = (y.x) (x.x) is -reducible.
. Leftmost-innermost Reduction . . .. Leftmost-outermost Reduction . liReduce :: Exp -> [Exp] .. loReduce :: Exp -> [Exp] liReduce (Var _) = [] loReduce (Var _) = [] liReduce (App e@(Lam x e1) e2) = loReduce (App (Lam x e1) e2) = case liReduce e2 of [subst (x,e2) e1] [e2'] ->[App e e2'] loReduce (App e1 e2) = [] -> [subst (x,e2) e1] case loReduce e1 of liReduce (App e1 e2) = [e1'] -> [App e1' e2] case liReduce e1 of [] -> [e1'] -> [App e1' e2] case loReduce e2 of [] -> [e2'] -> [App e1 e2']; [] -> [] case liReduce e2 of loReduce (Lam x e'@(App e (Var y))) [e2'] -> [App e1 e2']; [] -> [] | (x==y)&&(x `notElem` fv(e))=[e] liReduce (Lam x e'@(App e (Var y))) | otherwise = | (x==y)&&(x `notElem` fv(e))=[e] case loReduce e' of | otherwise = [e''] -> [Lam x e'']; [] -> [] case liReduce e of loReduce (Lam x e) = [e'] -> [Lam x e']; [] -> [] case loReduce e of liReduce (Lam x e) = case liReduce e of . [e'] -> [Lam x e']; [] -> [] .. . . [e'] -> [Lam x e']; [] -> [] 13 / 38 .
...
2
Perform a single Leftmost-innermost reduction step on each expression below. of each expression below.
14 / 38
. ..
.
15 / 38
. .
Normal Form
. Denition (Normal form) .. A lambda expression is in normal form if it cannot be further reduced using - or -reduction. . .. Some lambda expressions have no normal form
(x.(x x))(x.(x x)) has no normal form; the only possible reduction leads to an identical expression, and thus the reduction process does not terminate.
The normal form is a nal result of computation which we intuitively think of the value of an expression. We expect the value, i.e., the nal result, to be unique, and to be able to nd it whenever it exists.
16 / 38
Church-Rosser Theorem
. Theorem (Church-Rosser Theorem I) . .. If . e1 e2 then there exists an e such that e1 e and e2 e. .. . If e1 and e2 are convertible, then there exists a third expression e (possibly the same as e1 or e2 ) which they can both be reduced. . Corollary .. No lambda expression can be converted to two distinct normal forms ignoring dierences due to -conversion. . .. The order of reductions is irrelevant. Is it always possible to nd the normal form (assuming it exists)?
17 / 38
. .
.
18 / 38
. Normal-order and Applicative-order reductions . .. -- iterates reduction steps iterateReduce :: (Exp -> [Exp]) -> Exp -> [Exp] iterateReduce stepReduce e = e: case stepReduce e of [e'] -> iterateReduce stepReduce e' [] -> [] -- normal-order reduction noReduce = iterateReduce loReduce -- applicative order reduction aoReduce = iterateReduce liReduce . .. . . Use take function to avoid nonterminating processes. . .. *LC> e3 (\x -> y) ((\x -> x x) (\y -> y y)) *LC> take 4 (noReduce e3) [(\x -> y) ((\x -> x x) (\y -> y y)),y] *LC> take 4 (aoReduce e3) [(\x -> y) ((\x -> x x) (\y -> y y)),(\x -> y) ((\y -> y y) (\y -> y y)), (\x -> y) ((\y -> y y) (\y -> y y)),(\x -> y) ((\y -> y y) (\y -> y y))] . .. . 19 / 38 . .
Computational Lambda Calculus Lambda Calculus Recursion, Lambda-denability and Churchs Thesis
Fixpoint Theorem
. Theorem (Fixpoint Theorem) . .. Every lambda expression e has a xpoint e such that (e e ) e . . .. .
. Proof. . .. Take e to be (Y e), where Y f. (x. f (x x)) (x. f (x x)). Then we have Ye = (x. e (x x)) (x. e (x x)) = e ((x. e (x x)) (x. e (x x))) = e ( Y e) . .. . .
. xpoint and xed-point .. The word xpoint is rather new and means xed-point. . ..
.
20 / 38
. .
Computational Lambda Calculus Lambda Calculus Recursion, Lambda-denability and Churchs Thesis
Y = f. (x. f (x x)) (x. f (x x)) is called Y combinator, or paradoxical combinator. The xpoint theorem means that any recursive function may be written nonrecursively.
A recursive function f dened by f f could be written as f (f. f ) f. This equation says that f is a xpoint of (f. f ), which is exactly Y computes. And thus we arrive at the nonrecursive denition f Y (f. f ).
21 / 38
Computational Lambda Calculus Lambda Calculus Recursion, Lambda-denability and Churchs Thesis
Churchs Thesis
. Churchs Thesis . .. Eectively computable functions from positive integers to positive integers are just those denable in the lambda calculus. . .. . Although the notion of functions from positive integers to positive integers can be formalized precisely, the notion of eectively computable cannot.
No proof can be given for the thesis.
Kleene [1936]: -denability is equivalent to the notion of recursiveness by G odel and Herbrand. Turing [1937]: Turing computability is equivalent to -denability.
22 / 38
Computational Lambda Calculus Lambda Calculus Recursion, Lambda-denability and Churchs Thesis
Exercises (Expressions are beyond lambda. Take their meaning as usual.) . Rewrite the denition of the factorial function 1 .
. .
fac n. if n = 0 then 1 else n fac(n 1) into a nonrecursive form with the Y combinator. .. Consider a function denition 2 f x.if x = 0 then 1 else if x = 1 then f(3) else f(x 2). Conrm that each of the following function is a xpoint of the denition above. Which one do you think of as the xpoint?
f(x) = 1, if even(x) x 0; f(x) = undefined, otherwise f(x) = 1 for all x f(x) = 1, if even(x) x 0; f(x) = a, if odd(x) x > 0; f(x) = b, otherwise. Here we assume that a and b are arbitrary.
...
3
Haskell provides us with the function abstraction notation as \x -> e, which is similar to the lambda abstraction x.e. Can we dene the xpoint combinator Y in Haskell? If not, why?
23 / 38
Encoding Booleans
How can we compute numbers and Booleans?
We have seen arithmetic, comparison, and conditional expression in the last exercises.
Booleans lam true and lam false, and logical operators lam and (), lam or (), lam not (), and cond (if then else) can be encoded with lambda expressions.
. Church Booleans: Encoding Booleans and logical operators .. lam true x.y.x lam false x.y.y lam and x.y.(x y x) lam or x.y.(x x y) lam not x.y.z.(x z y) cond x.y.z.(x y z) .
. ..
.
24 / 38
Exercises .. Write Haskell code for Church Booleans and conrm how they 1 work by reductions.
...
2
. Sample session of Church Booleans . .. *LC> noReduce (App lam_not lam_true) [(\x -> \y -> \z -> x z y) (\x -> \y -> x),\y -> \z -> (\x -> \y -> x) z y,\y -> \z -> (\y . -> z) y,\y -> \z -> z] .. .
Consider whether the reduction strategies normal-order and applicative-order produce the same or dierent reductions for Church Boolean operations.
25 / 38
Natural numbers and arithmetic operators lam succ (+1), lam plus (+), lam mult (), and lam power () can be encoded with lambda expressions.
. Church Numerals: Encoding natural numbers and arithmetic operators .. lam zero f.x.x lam succ n.f.x.f (n f x) lam plus m.n.f.x.m f (n f x) lam mult m.n.f.m (n f) . .. .
26 / 38
Exercises .. In Church numerals, natural number n is represented by a 1 lambda expression of the form
f.x.f(f (f x)), where f appears n times in nested applications. Calculate Church numerals for 1 and 2 with pencil and paper.
...
2
Write Haskell code for Church numerals and compute ones for 1 and 2. Have you got the same result with the previous excercise for 1? Prove that lam plus = m.n.m lam succ n, and lam mult = m.n.m (lam plus n) lam zero
27 / 38
...
3
. Note on conventional representation of Constants .. Var construct with identiers of capitalized initial letter or numerals stands for constants. . ..
. . .
28 / 38
29 / 38
Combinators
. Denition (Combinator) .. A combinator is a lambda expression of the form x.e, that is lambda abstraction, with no free variables in e. . .. Any closed expressions, i.e., with no free variables are combinators. . Example Combinators .. S x.y.z. x z (y z) K x.y. x . .. I x. x . . . . Combinators are constants . .. We can take combinators as constants. In lambda expressions with constants, combinators may appear as x.S(x K), for example. . .. . .
30 / 38
Exercises
... ...
1 2
and reduce the expression (App(App s k) k). . Also compare reduction sequences by normal-order and applicative-order reductions. Reduce expressions (S (K e) (K e )) and (S (K e) I), respectively, where e and e are any expressions. When you conrm the results by Haskell code, e and e should be taken as constants. For example,
. .
...
3
31 / 38
.
32 / 38
SKI Combinators
An algorithm proves the theorem by translating any lambda expression into a constant expression solely composed of S and K. Note that I is extensionally equal to (S K K) and hence may be used.
. SKI translation .. T[x] T[c] T[(e1 e2 )] T[x.e] X [ x, x] X [ x, y] X[x, (e1 e2 )] . X[x, y.e] .. . Haskell code for SKI-translation . .. -- Translation to SKI-combinators trans e@(Var _) = e trans (App e1 e2) = App (trans e1) (trans e2) trans (Lam x e) = extract x e extract x e@(Var y) | x==y = Var "I" | otherwise = App (Var "K") e extract x (App e1 e2) = App (App (Var "S") (extract x e1)) (extract x e2) extract x (Lam y e) = . extract x (extract y e) .. . 33 / 38 .
. x, x Ide c, c Con (T[e1 ] T[e2 ]) X [ x, e ] I (K y), x = y (S X[x, e1 ] X[x, e2 ]) X[x, X[y, e]] . .
Exercises
...
1
Apply the translation rules to following lambda expressions. These are combinators. M = x. x x T = x.y. y x B = x.y.z. x (y z) C = x.y.z. x z y
...
2
Prove that the following simplication rules hold. That is, prove that both the expressions of the should be extensionally equal.
. K (e e ) e . .
...
3
. ..
S (K e) I
Apply the translation rules to an expression x.y. x y. And simplify the result further using the simplication rules. Improve the translation algorithm with the simplication rules.
34 / 38
...
4
SKIBC Combinators
SKI combinator expressions for lambda expression tend to become longer even if simplication rules are applied. Introduction of a few new combinators may reduce the size of combinator expressions.
. SKIBC Combinators .. S x.y.z. x z (y z) K x.y. x I x. x B x.y.z. x (y z) . .. C . x.y.z. x z y . . . SKIBC simplication rules .. S (K e) (K e ) K (e e ) S (K e) I e S (K e) e B e e . .. S e (K e ) C e e . .
35 / 38
Exercises
...
1
...
2
S e (K e )
Apply the transformation rules with the simplication rules to an expression x.y. x y to get an SKIBC expression. .. Improve the translation algorithm to produce SKIBC 3 combinators. We may write programs in either of the following ways.
First produce SKI expressions, and then apply the SKIBC simplication rules to the result. Rewrite translation rules to take the SKIBC rules into account to produce SKIBC expressions.
The rst strategy is simpler than the second, but has some diculties in reducing the size of the resultant expression. Try both if you like to know the dierences.
36 / 38
Combinator Reduction
Combinator expressions are reduced to simpler ones by the reduction rules that is induced from corresponding lambda reductions. Note that the combinator reduction may take place if sucient number of arguments are supplied for application.
. SKIBC Reduction rules .. S x y z x z (y z) Kxy x Ix x . .. Bxyz Cxyz x (y z) xzy . Other combinators ( -reduction) .. (+ e e ) V[e] + V[e ] ( e e ) V[e] V[e ] (Cond b e e ) e , if V[b] e , otherwise .
.37 / 38 .
Exercises
...
1
Find a combinator expression for succ = x.(+ 1 x), where + is the add function with two arguments, and 1 is a numeral constant. Also evaluate (succ 5) by combinator reduction. Find a combinator expression for fac = n.Cond (= n 0) 1 ( n (fac ( n 1)))
...
2
and evaluate (fac 2) by reduction. . Recursion and the xpoint combinator Y . .. To deal with recursion, the xpoint combinator Y = f. (x. f (x x)) (x. f (x x)) may be used with the SKIBC combinators. Alternatively, we may use the mechanism of denition of constants for recursion. Take fac of the RHS of the above denition as constant that is dened by the equation, for example. . .. . . Write Haskell code for the SKIBC combinator reduction. 3 .
38 / 38