Data Abstraction and State Abstract Data Types (3.7) State

Report
Chapter 2: Lambda Calculus
Programming Distributed Computing Systems: A
Foundational Approach
Carlos Varela
Rensselaer Polytechnic Institute
C. Varela
1
Mathematical Functions
Take the mathematical function:
f(x) = x2
f is a function that maps integers to integers:
Function
f: Z  Z
Domain
Range
We apply the function f to numbers in its domain to obtain a number
in its range, e.g.:
f(-2) = 4
C. Varela
2
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 (higher-order) function with the
following type:
 : (Z  Z) x (Z  Z)  (Z  Z)
C. Varela
3
Lambda Calculus (Church and Kleene
1930’s)
A unified language to manipulate and reason about functions.
Given
f(x) = x2
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
4
Lambda Calculus Syntax and Semantics
The syntax of a -calculus expression is as follows:
e
::=
|
|
v
v.e
(e e)
variable
functional abstraction
function application
The semantics of a -calculus expression is as follows:
(x.E M)  E{M/x}
where we alpha-rename the lambda abstraction E if necessary to
avoid capturing free variables in M.
C. Varela
5
Currying
The lambda calculus can only represent functions of one variable.
It turns out that one-variable functions are sufficient to represent
multiple-variable functions, using a strategy called currying.
E.g., given the mathematical function:
of type
h(x,y) = x+y
h: Z x Z Z
We can represent h as h’ of type:
h’: Z Z Z
Such that
h(x,y) = h’(x)(y) = x+y
For example,
h’(2) = g, where g(y) = 2+y
We say that h’ is the curried version of h.
C. Varela
6
Function Composition in Lambda Calculus
S:
I:
x.x2
x.x+1
(Square)
(Increment)
C:
f.g.x.(f (g x))
(Function Composition)
Recall semantics rule:
((C S) I)
(x.E M)  E{M/x}
((f.g.x.(f (g x)) x.x2) x.x+1)
 (g.x.(x.x2 (g x)) x.x+1)
 x.(x.x2 (x.x+1 x))
 x.(x.x2 x+1)
 x.x+12
C. Varela
7
Free and Bound Variables
The lambda functional abstraction is the only syntactic construct
that binds variables. That is, in an expression of the form:
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 Variables
C. Varela
Free 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
9
Order of Evaluation in the Lambda Calculus
Does the order of evaluation change the final result?
Consider:
Recall semantics rule:
2
x.(x.x (x.x+1 x))
(x.E M)  E{M/x}
There are two possible evaluation orders:
and:
x.(x.x2 (x.x+1 x))
 x.(x.x2 x+1)
 x.x+12
Applicative
Order
x.(x.x2 (x.x+1 x))
 x.(x.x+1 x)2
 x.x+12
Normal Order
Is the final result always the same?
C. Varela
10
Church-Rosser 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’
Also called the diamond or confluence property.
Furthermore, if there is a way for an expression evaluation to
terminate, using normal order will cause termination.
C. Varela
11
Order of Evaluation and Termination
Consider:
(x.y (x.(x x) x.(x x)))
Recall semantics rule:
There are two possible evaluation orders:
and:
(x.E M)  E{M/x}
(x.y (x.(x x) x.(x x)))
 (x.y (x.(x x) x.(x x)))
Applicative
Order
(x.y (x.(x x) x.(x x)))
 y
Normal Order
In this example, normal order terminates whereas applicative order
does not.
C. Varela
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
f.x.(f x)
f.g.x.(f (g x))
(x.(x x) x.(x x))
f.x.y.((f x) y)
x.y.(z.y x)
x.y.(y x)
(Identity)
(Application)
(Composition)
(Loop)
(Currying)
(Sequencing--normal order)
(Sequencing--applicative order)
where y denotes a thunk, i.e., a lambda abstraction
wrapping the second expression to evaluate.
The meaning of a combinator is always the same independently of
its context.
C. Varela
13
Combinators in Functional Programming
Languages
Most functional programming languages have a syntactic form for
lambda abstractions. For example the identity combinator:
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
Normal vs Applicative Order of Evaluation
and Termination
Consider:
(x.y (x.(x x) x.(x x)))
Recall semantics rule:
There are two possible evaluation orders:
and:
(x.E M)  E{M/x}
(x.y (x.(x x) x.(x x)))
 (x.y (x.(x x) x.(x x)))
Applicative
Order
(x.y (x.(x x) x.(x x)))
 y
Normal Order
In this example, normal order terminates whereas applicative order
does not.
C. Varela
16
-renaming
Alpha renaming is used to prevent capturing free occurrences of
variables when beta-reducing 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
alpha-rename x to y.
C. Varela
17
b-reduction
→
(x.E M)
b
E{M/x}
Beta-reduction may require alpha renaming to prevent capturing
free variable occurrences. For example:
(x.y.(x y) (y w))
α
→
→
b
(x.z.(x z) (y w))
z.((y w) z)
Where the free y remains free.
C. Varela
18
h-conversion
→
x.(E x)
h
E
if x is not free in E.
For example:
(x.y.(x y) (y w))
α
→
→
b
→
h
(x.z.(x z) (y w))
z.((y w) z)
(y w)
C. Varela
19
Recursion Combinator (Y or rec)
Suppose we want to express a factorial function in the  calculus.
1
n=0
n*(n-1)!
n>0
f(n) = n! =
We may try to write it as:
f:
n.(if (= n 0)
1
(* n (f (- n 1))))
But f is a free variable that should represent our factorial function.
C. Varela
20
Recursion Combinator (Y or rec)
We may try to pass f as an argument (g) as follows:
f:
g.n.(if (= n 0)
1
(* n (g (- n 1))))
The type of f is:
f: (Z  Z)  (Z  Z)
So, what argument g can we pass to f to get the factorial function?
C. Varela
21
Recursion Combinator (Y or rec)
f: (Z  Z)  (Z  Z)
(f f) is not well-typed.
(f I) corresponds to:
1
n=0
n*(n-1)
n>0
f(n) =
We need to solve the fixpoint equation:
(f X) = X
C. Varela
22
Recursion Combinator (Y or rec)
(f X) = X
The X that solves this equation is the following:
X:
(x.(g.n.(if (= n 0)
1
(* n (g (- n 1))))
y.((x x) y))
x.(g.n.(if (= n 0)
1
(* n (g (- n 1))))
y.((x x) y)))
C. Varela
23
Recursion Combinator (Y or rec)
X can be defined as (Y f), where Y is the recursion combinator.
Y:
f.(x.(f y.((x x) y))
x.(f y.((x x) y)))
Y:
f.(x.(f (x x))
x.(f (x x)))
Applicative
Order
Normal Order
You get from the normal order to the applicative order recursion
combinator by h-expansion (h-conversion from right to left).
C. Varela
24
Natural Numbers in Lambda Calculus
|0|:
|1|:
…
|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 semantics rule:
(x.E M)  E{M/x}
 x.x.x
C. Varela
25
Booleans and Branching (if) in  Calculus
|true|:
|false|:
x.y.x
x.y.y
(False)
|if|:
b.t.e.((b t) e)
(If)
(True)
Recall semantics rule:
(((if true) a) b)
(x.E M)  E{M/x}
(((b.t.e.((b t) e) x.y.x) a) b)
 ((t.e.((x.y.x t) e) a) b)
 (e.((x.y.x a) e) b)
 ((x.y.x a) b)
(y.a b)
a
C. Varela
26
Exercises
20. PDCS Exercise 2.11.1 (page 31).
21. PDCS Exercise 2.11.2 (page 31).
22. PDCS Exercise 2.11.5 (page 31).
23. PDCS Exercise 2.11.6 (page 31).
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

similar documents