C. Varela
Mathematical Functions
Take the mathematical function:
f(x) = x2
f is a function that maps integers to integers:
Function
Function
f: Z Z
Domain
Domain
Range
Range
Function Composition
Given the mathematical functions:
f(x) = x2 , g(x) = x+1
f g is the composition of f and g:
f g (x) = f(g(x))
f g (x) = f(g(x)) = f(x+1) = (x+1) 2 = x2 + 2x + 1
g f (x) = g(f(x)) = g(x2) = x2 + 1
Function composition is therefore not commutative. Function
composition can be regarded as a (higherorder) function with the
following type:
: (Z Z) x (Z Z) (Z Z)
C. Varela
x. x2
represents the same f function, except it is anonymous.
To represent the function evaluation f(2) = 4,
we use the following calculus syntax:
( x. x2 2) 22 4
C. Varela
::=


v
v.e
(e e)
variable
functional abstraction
function application
C. Varela
Currying
The lambda calculus can only represent functions of one variable.
It turns out that onevariable functions are sufficient to represent
multiplevariable functions, using a strategy called currying.
E.g., given the mathematical function:
of type
h(x,y) = x+y
h: Z x Z Z
x.x2
x.x+1
(Square)
(Increment)
C:
f. g. x.(f (g x))
Composition)
(Function
Recall
Recallsemantics
semanticsrule:
rule:
((C S) I)
((x.E
x.EM)
M)
E{M/x}
E{M/x}
v.e
we say that free occurrences of variable v in expression e are bound.
All other variable occurrences are said to be free.
E.g.,
( x. y.(x y) (y w))
Bound
BoundVariables
Variables
C. Varela
Free
Free
Variables
Variables
8
renaming
Alpha renaming is used to prevent capturing free occurrences of
variables when reducing a lambda calculus expression, e.g.,
( x. y.(x y) (y w))
y.((y w) y)
This reduction erroneously captures the free occurrence of y.
A correct reduction first renames y to z, (or any other fresh variable)
e.g.,
( x. y.(x y) (y w))
( x. z.(x z) (y w))
z.((y w) z)
where y remains free.
C. Varela
and:
Applicative
Applicative
Order
Order
Normal
Normal
Order
Order
10
ChurchRosser Theorem
If a lambda calculus expression can be evaluated in two different
ways and both ways terminate, both ways will yield the same result.
e
e1
e2
e
11
((x.E
x.EM)
M)
E{M/x}
E{M/x}
Applicative
Applicative
Order
Order
Normal
Normal
Order
Order
12
Combinators
A lambda calculus expression with no free variables is called a
combinator. For example:
I:
App:
C:
L:
Cur:
Seq:
ASeq:
x.x
(Identity)
f. x.(f x)
(Application)
f. g. x.(f (g x))
(Composition)
( x.(x x) x.(x x))
(Loop)
f. x. y.((f x) y)
(Currying)
x. y.( z.y x)
(Sequencingnormal order)
x. y.(y x)
(Sequencingapplicative order)
where y denotes a thunk, i.e., a lambda abstraction
wrapping the second expression to evaluate.
13
x.x
can be written in Oz as follows:
fun {$ X} X end
and it can be written in Scheme as follows:
(lambda(x) x)
C. Varela
14
Currying Combinator in Oz
The currying combinator can be written in Oz as follows:
fun {$ F}
fun {$ X}
fun {$ Y}
{F X Y}
end
end
end
It takes a function of two arguments, F, and returns its curried
version, e.g.,
{{{Curry Plus} 2} 3} 5
C. Varela
15
((x.E
x.EM)
M)
E{M/x}
E{M/x}
Applicative
Applicative
Order
Order
Normal
Normal
Order
Order
16
renaming
Alpha renaming is used to prevent capturing free occurrences of
variables when betareducing a lambda calculus expression.
In the following, we rename x to z, (or any other fresh variable):
( x.(y x) x)
( z.(y z) x)
Only bound variables can be renamed. No free variables can be
captured (become bound) in the process. For example, we cannot
alpharename x to y.
C. Varela
17
reduction
( x.E M)
E{M/x}
z.((y w) z)
Where the free y remains free.
C. Varela
18
conversion
x.(E x)
if x is not free in E.
For example:
( x. y.(x y) (y w))
( x. z.(x z) (y w))
z.((y w) z)
(y w)
C. Varela
19
n=0
n*(n1)!
n>0
f(n) = n! =
We may try to write it as:
f:
n.(if (= n 0)
1
(* n (f ( n 1))))
C. Varela
20
g. n.(if (= n 0)
1
(* n (g ( n 1))))
C. Varela
21
n=0
n*(n1)
n>0
f(n) =
We need to solve the fixpoint equation:
(f X) = X
C. Varela
22
X:
C. Varela
23
Applicative
Applicative
Order
Order
Y:
f.( x.(f (x x))
x.(f (x x)))
Normal
Normal
Order
Order
You get from the normal order to the applicative order recursion
combinator by expansion (conversion from right to left).
C. Varela
24
n+1:
x.x
x. x.x
(One)
x.n
(N+1)
s:
n. x.n
(Successor)
(Zero)
(s 0)
( n. x.n x.x)
Recall
Recallsemantics
semanticsrule:
rule:
((x.E
x.EM)
M)
E{M/x}
E{M/x}
x. x.x
C. Varela
25
x. y.x
x. y.y
(False)
if:
b. t. e.((b t) e)
(If)
(True)
Recall
Recallsemantics
semanticsrule:
rule:
(((if true) a) b)
((x.E
x.EM)
M)
E{M/x}
E{M/x}
26
Exercises
C. Varela
27
Exercises
24.PDCS Exercise 2.11.7 (page 31).
25.PDCS Exercise 2.11.9 (page 31).
26.PDCS Exercise 2.11.10 (page 31).
27.Prove that your addition operation is correct using
induction.
28.PDCS Exercise 2.11.11 (page 31).
29.PDCS Exercise 2.11.12 (page 31).
C. Varela
28
Much more than documents.
Discover everything Scribd has to offer, including books and audiobooks from major publishers.
Cancel anytime.