You are on page 1of 10

Lambda Calculus Concrete Syntax for Lambda Calculus

<expression> ::=

Very general mathematical language. <variable>


| <constant>
Simple syntax, powerful semantics. | ( <expression1> <expression2> )
| ( <variable> . <expression> )
Encapsulates function abstraction (definition)
and application. Variables - lower case identifiers

Constants - any predefined object


Functions easily used as values.
- impure/applied lambda calculus

Used in denotational semantics. Rule 3 - application of expr1 to expr2


- operator (rator)
- operand (rand)
Functional programming languages can be
viewed as syntactic variants. Rule 4 - lambda abstraction
- bound variable
- body

Chapter 5 1 Chapter 5 2

Application (Combinations) Abstractions


(E1 E2) means: anonymous functions:
apply the result of evaluating E1 to E2
(x . x)
E1 should be a function, either predefined
is similar to
(a constant) or an abstraction.
f(x) = x
In the pure lambda calculus,
every (closed) expression is a function. but has no name.

Examples Examples

(succ 1) where succ is the successor (m . (add m 1)) f(m) = m + 1


function

(zerop 0) where zerop is the zero predicate (m . (n . (sub n m))) g(m, n) = n - m


(test if zero)

(add 3 5) where add is the addition where means is similar to.


operation

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)

3. The scope of <variable> in an abstraction


extends as far to the right as possible. 2. (x . x (y . y x))
x . E1 E2 E3 means (x . (E1 E2 E3))
Application has a higher precedence than 3. (x . (x (y . (y x))))
abstraction.
Parentheses are needed for (x . E1 E2) E3
where E3 is an argument to the function To be completely parenthesized, the expression
should have parentheses around all applications,
x . E1 E2, not part of the body of the function.
but we will normally stop at step 2.
4. An abstraction allows a list of variables that
abbreviates a series of lambda abstractions.
x y z . E means (x . (y . (z . E)))

5. Name functions using define.


Chapter 5 5 Chapter 5 6

Parenthesizing Examples Curried functions


Note differences between
(x y z . x z (y z)) (x y . x) x y . x
(m . (n . (sub n m)))
g(m, n) = n - m

abstraction has no name


abstraction is a function of 1 parameter

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

Using signatures (domain and codomain):

g : N x N N
(m . (n . (sub n m))) : N (N N)

where N is the set of natural numbers.

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

Note that Curry and Uncurry have no effect on


what is being computed.

Chapter 5 9 Chapter 5 10

The set of free variables in an expression E, Substitution


denoted by FV(E), is defined by:
a) FV(c) = for any constant c E [vE1 ] denotes the lambda expression
b) FV(x) = {x} for any variable x obtained by replacing each free occurrence of
the variable v in E by the lambda expression E1.
c) FV(E1 E2) = FV(E1) FV(E2)
Such a substitution is call valid or safe
d) FV(x . E) = FV(E) {x} if no free variable in E1 becomes bound as
a result of the substitution E[vE1].
FV(E) is the set of variable that have free
occurrences in E. An invalid substitution involves
a variable captureor a name clash.
FV((x . x) y) = { y }
An Invalid Substitution
FV((x . x) x) = { x }
Note the definition deals with occurrences (x . (sub x y))[yx] = (x . (sub x x))
of variables.
Here one occurrence of x is bound First function: Subtract y from the argument.
and one occurrence is free.
Second function: Return zero.

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

b) x[vE1]= x for any variable xv

c) c[vE1]= c for any constant c

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

Reduction Rules -reduction:


If v is a variable, E is a lambda expression
-reduction: (denoting a function), and v has no free
occurrence in E,
If v and w are variables and E is a lambda
expression, v . (E v) E.
v . E w . (E[vw]) In the pure lambda calculus every expression is
a function.
provided that w does not occur at all in E, which
The rule fails when E represents some
makes the substitution E[vw] safe.
predefined constants:
x . f x y . f y if 5 is a predefined numeral, x . (5 x)
f . x . f (f x) g . x . g (g x) and 5 are not equivalent or even related.
If E stands for a predefined function, the rule
remains valid as shown by these examples:
-reduction:
m . (sqr m) sqr
If v is a variable and E and E1 are lambda
expressions, n . (mul 2 n) (mul 2).
(v . E) E1 E[vE1]
provided the substitution E[vE1] is carried out Take E F to mean E F or F E.
according to the rules for a safe substitution.

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)

Normal order reduction lamb comb

Reduce the leftmost outermost -redex first.


x lamb lamb con(6)

Applicative order reduction y comb m comb


Reduce the leftmost innermost -redex first.
comb var(x) comb con(2)

var(y) con(5) con(add) var(m)

Chapter 5 21 Chapter 5 22

Answer 3: Yes Answer 4: Yes

Church-Rosser TheoremI: Church-Rosser TheoremII:


For any lambda expressions E, F, and G, if For any lambda expression E, if E N
E F and E G, there is a lambda where N is in normal form, there is a normal
expression Z such that F Z and G Z. order reduction from E to N.

Corollary: For any lambda expressions E, M, Outcomes of a normal order reduction:


and N, if E M and E N where M and N are in
normal form, then M and N are variants of each 1. It reaches a unique (up to -conversion)
other (with respect to -reduction). normal form lambda expression,

Proof: The only reduction possible for an or


expression in normal form is an -reduction. 2. It never terminates.
Therefore the lambda expression Z in the
theorem must be a variant of M and N by
-reduction only. Applicative order reduction similar to call by value.

Normal order reduction similar to call by name.

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

z . ((n . mul n (add 9 9)) z) (b . b) q q


Chapter 5 25 Chapter 5 26

Lists in Lambda Calculus


A Computation
define Nil = x . a . b . a,
Add 2 2
define [1, 2, 3, 4]
= Pair 1 (Pair 2 (Pair 3 (Pair 4 Nil))). (m . n . f . x . m f (n f x))
(g . y . g (g y))(h . z . h (h z))
Head (Tail (Tail [1, 2, 3, 4])) 3.
(n . f . x . (g . y . g (g y)) f (n f x))
(h . z . h (h z))
Natural Numbers
define 0 = f . x . x f . x . (g . y . g (g y)) f
define 1 = f . x . f x ((h . z . h (h z)) f x)
define 2 = f . x . f (f x) f . x . (y . f (f y)) ((h . z . h (h z)) f x)
define 3 = f . x . f (f (f x))
: : f . x . (f (f ((h . z . h (h z)) f x)))

Successor function: Succ : N N f . x . (f (f ((z . f (f z)) x)))


define Succ = n . f . x . f (n f x) f . x . (f (f (f (f x)))) = 4

Addition operation: Add : N N N


define Add = m . n . f . x . m f (n f x)

Chapter 5 27 Chapter 5 28
Functional Programming Languages A Lambda Calculus Evaluator

Functional programming languages, such as Concrete Syntax


Lisp, Scheme, ML, Miranda, and Haskell, can <expression> ::= <identifier> | <constant>
be viewed as implementations of the lambda | ( L <identifier>+ <expression> )
calculus. | ( <expression>+ <expression> )
These languages usually provide one or two <constant> ::= <numeral> | true | false
syntactic expressions for readability: |succ | sqr | add | sub | mul

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

2. where-expression (E1 E2 E3) means ((E1 E2) E3).


(add x 3) where x=5 Outermost parentheses are never omitted.
means (x . (add x 3)) 5
Abstract Syntax
They also provide a mechanism for defining Expression ::= var(Identifier)
(naming) functions, recursive functions in | con(Constant)
particular. | lamb(Identifier,Expression)
We justify the recursive definitions in Chapter 10. | comb(Expression,Expression)
Chapter 5 29 Chapter 5 30

Lambda Expression: Logic Grammar for Lambda Calculus


(m . sqr ((n . (add m n)) 2)) 4
Concrete Syntax: program(expr(E)) --> expr(E).
((L m (sqr ((L n (add m n)) 2))) 4) expr(lamb(X,E)) -->
Abstract Syntax Tree: [lparen],['L'],[var(X)],expr(E1),restlamb(E1,E).
comb(lamb(m,comb(con(sqr),
comb(lamb(n,comb(comb(con(add), restlamb(E,E) --> [rparen].
var(m)), restlamb(var(Y),lamb(Y,E)) -->
var(n))), expr(E1), restlamb(E1,E).
con(2)))),
con(4)) expr(E) -->
comb [lparen],expr(E1),expr(E2),restcomb(E1,E2,E).
lamb con(4)
restcomb(E1,E2,comb(E1,E2)) --> [rparen].
restcomb(E1,E2,E) -->
expr(E3), restcomb(comb(E1,E2),E3,E).
m comb
expr(var(X)) --> [var(X)].
con(sqr) comb expr(con(X)) --> [num(X)].

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)

occurrence of variable Var replace by Exp1. subst(lamb(X,E),V,E1,lamb(Z,NewE)) :-


freevars(E1,F1),
variant(Var,VarList,NewVar).
(member(X,F1), freevars(E,F), % g)
NewVar is a variant of variable Var different
union(F,[V],F2),
from all the variables in VarList.
union(F1,F2,FV),
variant(X,FV,Z),
prime(Var,PrimeVar).
subst(E,X,var(Z),TempE),
PrimeVar is variable Var with an apostrophe. subst(TempE,V,E1,NewE)

pp(Exp). ; subst(E,V,E1,NewE), Z=X) . % f)

Pretty-print the expression Exp.


Chapter 5 33 Chapter 5 34

Free Variables Reduction

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

You might also like