Professional Documents
Culture Documents
Lecture 5
Lecture 5
<expression> ::=
Chapter 5 1 Chapter 5 2
Examples Examples
Chapter 5 3 Chapter 5 4
Notational Conventions Example
1. Uppercase: metavariables for expressions.
Given x . x y . y x, use the conventions above
to parenthesize it.
2. Function application associates to the left.
E1 E2 E3 means ((E1 E2) E3) 1. (x . x y . y x)
So
((m . (n . (sub n m))) 1) = (n . (sub n 1))
x . (y . y y) z (w . w) x returns a function
whereas g is a function of 2 parameters
g : N x N N
(m . (n . (sub n m))) : N (N N)
Chapter 5 7 Chapter 5 8
The signature of g can be read: Semantics (operational)
g takes a tuple (pair) of natural numbers and Defined in terms of syntactic manipulations on
returns a natural number. lambda expressions
The other signature can be read as Intuitively, applications are like function calls:
we need to
take a natural number, and return a
function from natural numbers to natural - bind values to the formal parameters
numbers. - evaluate the body of the function
Computationally though, these functions are the
same. If < , > is the tuple constructor, define Bound and Free Variables
Curry = (f . x . y . f <x, y>) and
An occurrence of a variable v in a lambda
Uncurry = (f . <x, y> . f x y) expression is called bound if it is within the scope
of a v; otherwise it is called free.
Then
Curry g = (x . (y . (sub y x)))
((x . (y x)) x)
Uncurry (x . (y . (sub y x))) = g
Chapter 5 9 Chapter 5 10
Chapter 5 11 Chapter 5 12
E [vE1 ] is defined by: Substitution Examples
(x . (y . y z) (w . w) z x)[z y]
a) v[vE1]= E1 for any variable v
d) (Erator Erand)[vE1] =
(Erator[vE1]) (Erand[vE1])
(x . (y . y y) z (w . w) x)[z f x]
e) (v . E)[vE1] = (v . E)
f) (x . E)[vE1] = x . (E[vE1])
when xv and xFV(E1)
g) (x . E)[vE1] = z . (E[xz][vE1])
when xv and xFV(E1),
where zv and zFV(E E1)
Chapter 5 13 Chapter 5 14
Chapter 5 15 Chapter 5 16
Extensionality of functions: Reduction Example
If f(x) = g(x) for all x, then f = g. Thrice (m . (mul 2 m)) 7
where Thrice = f . x . f (f (f x))
Extensionality Theorem : If F1 x E and F2 Thrice (m . (mul 2 m)) 7
x E where xFV(F1 F2), then (f . x . f (f (f x))) (m . (mul 2 m)) 7
F1 F2 where includes -reductions. (x . (m . (mul 2 m))
((m . (mul 2 m)) ((m . (mul 2 m)) x))) 7
Proof: F1 x . (F1 x) x . E
(m . (mul 2 m))
x . (F2 x) F2. ((m . (mul 2 m)) ((m . (mul 2 m)) 7))
mul 2 ((m . (mul 2 m)) ((m . (mul 2 m)) 7))
-reduction: mul 2 (mul 2 ((m . (mul 2 m)) 7))
In an applied lambda calculus (not pure), rules mul 2 (mul 2 (mul 2 7)) mul 2 (mul 2 14)
associated with predefined values and
functions are called rules. mul 2 28 56
(succ 13) 14
Normal Form
(mul 4 7) 28
A lambda expression that contains no
(not false) true. -redexes and no -redexes.
It cannot be further reduced using the -rule or
the -rule (no function applications to evaluate).
Chapter 5 17 Chapter 5 18
Answer 2: Yes
Questions
1. Can every lambda expression be reduced Example:
to a normal form?
(x . y . y 5 x) ((m . add m 2) 6) mul
2. Is there more than one way to reduce a
particular lambda expression? Path One:
(x . y . y 5 x) ((m . add m 2) 6)mul
3. If there is more than one reduction
strategy, does every one (that terminates) (y . y 5 ((m . add m 2) 6)) mul
lead to the same normal form expression?
mul 5 ((m . add m 2) 6)
4. Is there a reduction strategy that will
guarantee that a normal form expression mul 5 (add 6 2)
will be produced?
mul 5 8 40
Answer 1: No Path Two:
(x . y . y 5 x) ((m . add m 2) 6) mul
(x . x x) (x . x x )
(x . x x) (x . x x ) (x . y . y 5 x) (add 6 2) mul
(x . x x) (x . x x ) (x . y . y 5 x) 8 mul
(y . y 5 8) mul
mul 5 8 40
Chapter 5 19 Chapter 5 20
Example: (y . 5) ((x . x x) (x . x x))
Outermost and Innermost Redexes
Path One:
(y . 5) ((x . x x) (x . x x)) 5 Example:
Path Two: (x . y . y 5 x) ((m . add m 2) 6) mul
(y . 5) ((x . x x) (x . x x ))
(y . 5) ((x . x x) (x . x x )) comb
(y . 5) ((x . x x) (x . x x ))
comb con(mul)
Chapter 5 21 Chapter 5 22
Chapter 5 23 Chapter 5 24
Call by valueis applicative order reduction
except that no -redex or -redex within an ab-
Constants in Pure Lambda Calculus
straction is reduced. The argument to a function
is evaluated before the function is applied. Constructor:
Example:
define Pair = a . b . f . f a b
(x . (g . g x)
((f . y . z . ((n . mul n (f x y)) z)) add)) Selectors:
((m . sqr m) 3) define Head = g . g (a . b . a)
(x . (g . g x) define Tail = g . g (a . b . b)
((f . y . z . ((n . mul n (f x y)) z)) add)) Correctness of the definitions:
(sqr 3)
Tail (Pair p q)
(x . (g . g x)
(g . g (a . b . b))
((f . y . z . ((n . mul n (f x y)) z)) add)) 9
( (a . b . f . f a b) p q)
(g . g 9) ((a . b . f . f a b) p q) (a . b . b)
((f . y . z . ((n . mul n (f 9 y)) z)) add)
((b . f . f p b) q) (a . b . b)
(g . g 9)
(f . f p q) (a . b . b)
(y . z . (( n . mul n (add 9 y)) z))
(y . z . ((n . mul n (add 9 y)) z)) 9 (a . b . b) p q
Chapter 5 27 Chapter 5 28
Functional Programming Languages A Lambda Calculus Evaluator
1. let-expression
Two abbreviations
let x=5 in (add x 3)
(L x y E) means (L x (L y E)), which stands
means (x . (add x 3)) 5
for x . y . E
lamb con(2)
expr(con(true)) --> [true].
expr(con(false)) --> [false].
n comb expr(con(succ)) --> [succ].
expr(con(sqr)) --> [sqr].
comb var(n) expr(con(add)) --> [add].
expr(con(sub)) --> [sub].
con(add) var(m)
expr(con(mul)) --> [mul].
Chapter 5 31 Chapter 5 32
Lambda Calculus Evaluator Substitution
evaluate(E,NewE) :- reduce(E,TempE),
evaluate(TempE,NewE). subst(var(V),V,E1,E1). % a)
evaluate(E,E).
subst(var(X),V,E1,var(X)). % b)
Utilities
subst(con(C),V,E1,con(C)). % c)
freevars(Exp,FV).
subst(comb(Rator,Rand),V,E1, % d)
FV is a list containing the free variables that comb(NewRator,NewRand)) :-
occur in Exp. subst(Rator,V,E1,NewRator),
subst(Rand,V,E1,NewRand).
subst(Exp,Var,Exp1,NewExp).
NewExp is the expression Exp with each free subst(lamb(V,E),V,E1,lamb(V,E)). % e)
freevars(var(X),[X]). reduce(comb(lamb(X,Body),Arg),R) :- %1
freevars(con(C),[ ]). subst(Body,X,Arg,R).
freevars(comb(Rator,Rand),FV) :-
freevars(Rator,RatorFV), reduce(comb(con(C),con(Arg)),R) :- %2
freevars(Rand,RandFV), compute(C,Arg,R).
union(RatorFV,RandFV,FV).
freevars(lamb(X,E),FV) :- reduce(comb(Rator,Rand), %3
freevars(E,F),delete(X,F,FV). comb(NRator,Rand)) :-
reduce(Rator,NRator).
Variants reduce(comb(Rator,Rand), %4
comb(Rator,NRand)) :-
prime(X,PrimeX) :- name(X,L), reduce(Rand,NRand).
concat(L,[39],NewL),
name(PrimeX,NewL).
reduce(lamb(X,Body),lamb(X,NBody)) :- %5
reduce(Body,NBody).
variant(X,L,NewX) :- member(X,L),
prime(X,PrimeX),
variant(PrimeX,L,NewX).
variant(X,L,X).
Chapter 5 35 Chapter 5 36
Evaluating Constants
compute(succ,N,con(R)) :- R is N+1.
compute(sqr,N,con(R)) :- R is N*N.
compute(add,N,con(add(N))).
compute(add(M),N,con(R)) :- R is M+N.
compute(sub,N,con(sub(N))).
compute(sub(M),N,con(R)) :- R is M-N.
compute(mul,N,con(mul(N))).
compute(mul(M),N,con(R)) :- R is M*N.
Top Level
evaluate(Exp,Result), nl,
write('Result = '), pp(Result), nl.
Try It cp ~slonnegr/public/plf/normal .
cp ~slonnegr/public/plf/twice .
Chapter 5 37