More Lambda Calculus: Announcements
More Lambda Calculus: Announcements
Plan
Recall
Operational Semantics
Evaluations strategies
Equality
Encodings
Fixed Points
Lambda Syntax
Combinators
Informal Semantics
Informal Semantics
Informal Semantics
Operational Semantics
Binds x to e
Evaluates e with the new binding
Yields the result of this evaluation
Operational Semantics
10
Examples of Evaluation
The identity function:
(
x. x) E [E / x] x = E
Another example with the identity:
(
f. f (
x. x)) (
x. x)
betabeta
-reduction
(x.e1) e2 [e2/x]e1
11
12
Examples of Evaluation
Examples of Evaluation
A non-terminating evaluation:
(
x. xx) (
y. yy)
[
y. yy / x] xx = (
y. yy) (
y. yy)
Try T T, where T = x. x x x
14
13
The application
x. e
e
xxx
Becomes:
16
Normal Forms
e1 e1
e1 e2 e1 e2
e2 e2
e1 e2 e1 e2
e e
x. e x. e
This is a non-deterministic semantics
Note that we evaluate under (where?)
K = x. y. x is in normal form
K I is not in normal form
17
18
20
21
22
R
e
inner
outer
(
y. [y/x] x) E = (
y. y) E [E/y] (
x. x) y = (
x. x) E
e2
e1
(
y. (
x. x) y) E
23
24
Beta Equality
Dataflow systems
Makefiles
26
Corollaries
e1
e2
27
28
Evaluation Strategies
Normal Order
( x. z) (( y. y y) ( y. y y))
( x. z) (( y. y y) ( y. y y))
Call-By-Name (lazy)
( x. z) (( y. y y) ( y. y y)) z
29
Normal-Order Reduction
A redex is outermost if it is not contained
inside another redex
Example:
S (K x y) (K u v)
K x, K u and S (K x y) are all redexes
Both K u and S (K x y) are outermost
Normal order always reduces the leftmost
outermost redex first
Bonus: Evaluation
Strategies
Call-by-Name
e1 n* x. e1 [e2/x]e1 n* e
x. en* x. e
e1 e2 n* e
33
34
Call-by-Value Evaluation
Example:
32
e1 v* x. e1 e2 v* e2 [e2/x]e1 v* e
x. ev* x. e
(u. u) (v. v) n
e1 e2 v* e
v. v
35
36
Example:
Call-by-value:
easy to implement
well-behaved (predictable) with respect to side-effects
Call-by-name:
More difficult to implement (must pass unevaluated
expressions)
The order of evaluation is harder to predict (e.g., difficulty
with side-effects)
Has a simpler theory than call-by-value
Allows the natural expression of infinite data structures (e.g.
streams)
Terminates more often than call-by-value
37
38
Which is Better?
Caveats
by reference
value-result
40
39
Functional Programming
no side effects
several evaluation strategies
lots of functions
nothing but functions (pure -calculus does
not have any other data type)
42
Referential Transparency
43
44
Expressiveness of -Calculus
45
46
Encodings
47
48
Recall:
true
false
if E1 then E2 else E3
=def x. y. x
=def x. y. y
=def E1 E2 E3
Intuition:
true
=def x. y. x
false
=def x. y. y
if E1 then E2 else E3 =def E1 E2 E3
or
or
(x. y. x) u v (y. u) v u
=def a. b. a true b
=def a. b. x. y. a x (b x y)
49
50
51
52
and a b
and
and
not a
not
not
=def a. b. a b false
=def a. b. x. y. a (b x y) y
true x y
53
b. b x y
p true
p false
(mkpair x y) true
x
54
(= for
=def f. s. f (n f s)
=def f. s. n f (f s)
=def n1 succ n2
=def n1 (add n2) 0
=def n (b. false) true
56
Computation Example
Toward Recursion
57
58
Encoding Recursion
Define
or
find p n = F find p n
59
60
10
11