1
Programming Languages and Compilers (CS 421)
Based heavily on slides by Elsa Gunter, which were based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha
Objectives for Today
2
Objectives for Today
3
4
Questions from last time?
5
Intro to Recursion in OCaml
Recursive Functions
# (* rec needed for recursive function declarations *)
let rec factorial n =
if n = 0 then
1
else
n * factorial (n - 1);;
val factorial : int -> int = <fun>
# factorial 5;;
- : int = 120
*
6
Recursion in OCaml
Recursive Functions
# (* rec needed for recursive function declarations *)
let rec factorial n =
if n = 0 then
1
else
n * factorial (n - 1);;
val factorial : int -> int = <fun>
# factorial 5;;
- : int = 120
*
7
Recursion in OCaml
Recursive Functions
# (* rec needed for recursive function declarations *)
let rec factorial n =
if n = 0 then
1
else
n * factorial (n - 1);;
val factorial : int -> int = <fun>
# factorial 5;;
- : int = 120
*
8
Recursion in OCaml
Recursive Functions
# (* rec needed for recursive function declarations *)
let rec factorial n =
if n = 0 then
1
else
n * factorial (n - 1);;
val factorial : int -> int = <fun>
# factorial 5;;
- : int = 120
*
9
Recursion in OCaml
Recursive Functions
# (* rec needed for recursive function declarations *)
let rec factorial n =
if n = 0 then
1
else
n * factorial (n - 1);;
val factorial : int -> int = <fun>
# factorial 5;;
- : int = 120
*
10
Recursion in OCaml
Without the rec keyword, we’d get “Error: Unbound value factorial.”
Pattern Matching and Recursion
Compute n2 recursively using:
n2 = (2 * n - 1) + (n - 1)2
# let rec sq n = (* rec for recursion *)
match n with (* pattern matching for cases *)
| 0 -> 0 (* base case *)
| n -> (2 * n -1) + sq (n -1);; (* recursive case *)
val sq : int -> int = <fun>
# sq 3;;
11
Recursion in OCaml
Pattern Matching and Recursion
Compute n2 recursively using:
n2 = (2 * n - 1) + (n - 1)2
# let rec sq n = (* rec for recursion *)
match n with (* pattern matching for cases *)
| 0 -> 0 (* base case *)
| n -> (2 * n -1) + sq (n -1);; (* recursive case *)
val sq : int -> int = <fun>
# sq 3;;
12
Recursion in OCaml
Pattern Matching and Recursion
Compute n2 recursively using:
n2 = (2 * n - 1) + (n - 1)2
# let rec sq n = (* rec for recursion *)
match n with (* pattern matching for cases *)
| 0 -> 0 (* base case *)
| n -> (2 * n -1) + sq (n -1);; (* recursive case *)
val sq : int -> int = <fun>
# sq 3;;
13
Recursion in OCaml
Pattern Matching and Recursion
Compute n2 recursively using:
n2 = (2 * n - 1) + (n - 1)2
# let rec sq n = (* rec for recursion *)
match n with (* pattern matching for cases *)
| 0 -> 0 (* base case *)
| n -> (2 * n -1) + sq (n -1);; (* recursive case *)
val sq : int -> int = <fun>
# sq 3;;
14
Recursion in OCaml
Pattern Matching and Recursion
Compute n2 recursively using:
n2 = (2 * n - 1) + (n - 1)2
# let rec sq n = (* rec for recursion *)
match n with (* pattern matching for cases *)
| 0 -> 0 (* base case *)
| n -> (2 * n -1) + sq (n -1);; (* recursive case *)
val sq : int -> int = <fun>
# sq 3;;
15
Recursion in OCaml
Pattern Matching and Recursion
Compute n2 recursively using:
n2 = (2 * n - 1) + (n - 1)2
# let rec sq n = (* rec for recursion *)
match n with (* pattern matching for cases *)
| 0 -> 0 (* base case *)
| n -> (2 * n -1) + sq (n -1);; (* recursive case *)
val sq : int -> int = <fun>
# sq 3;;
16
Recursion in OCaml
Pattern Matching and Recursion
Compute n2 recursively using:
n2 = (2 * n - 1) + (n - 1)2
# let rec sq n = (* rec for recursion *)
match n with (* pattern matching for cases *)
| 0 -> 0 (* base case *)
| n -> (2 * n -1) + sq (n -1);; (* recursive case *)
val sq : int -> int = <fun>
# sq 3;;
17
Aside: Recursion and induction are deeply, beautifully related. (Discussed in thesis, if curious.)
Recursion in OCaml
Pattern Matching and Recursion
Compute n2 recursively using:
n2 = (2 * n - 1) + (n - 1)2
# let rec sq n = (* rec for recursion *)
match n with (* pattern matching for cases *)
| 0 -> 0 (* base case *)
| n -> (2 * n -1) + sq (n -1);; (* recursive case *)
val sq : int -> int = <fun>
# sq 3;;
18
Aside: Recursion and induction are deeply, beautifully related. (Discussed in thesis, if curious.)
Recursion in OCaml
Pattern Matching and Recursion
Compute n2 recursively using:
n2 = (2 * n - 1) + (n - 1)2
# let rec sq n = (* rec for recursion *)
match n with (* pattern matching for cases *)
| 0 -> 0 (* base case *)
| n -> (2 * n -1) + sq (n -1);; (* recursive case *)
val sq : int -> int = <fun>
# sq 3;;
19
Aside: Recursion and induction are deeply, beautifully related. (Discussed in thesis, if curious.)
Recursion in OCaml
Pattern Matching and Recursion
Compute n2 recursively using:
n2 = (2 * n - 1) + (n - 1)2
# let rec sq n = (* rec for recursion *)
match n with (* pattern matching for cases *)
| 0 -> 0 (* base case *)
| n -> (2 * n -1) + sq (n -1);; (* inductive case *)
val sq : int -> int = <fun>
# sq 3;;
20
Aside: Recursion and induction are deeply, beautifully related. (Discussed in thesis, if curious.)
Recursion in OCaml
Pattern Matching and Recursion
Compute n2 recursively using:
n2 = (2 * n - 1) + (n - 1)2
# let rec sq n = (* rec for recursion *)
match n with (* pattern matching for cases *)
| 0 -> 0 (* base case *)
| n -> (2 * n -1) + sq (n -1);; (* inductive case *)
val sq : int -> int = <fun>
# sq 3;;
21
Aside: Recursion and induction are deeply, beautifully related. (Discussed in thesis, if curious.)
Recursion in OCaml
Pattern Matching and Recursion
Compute n2 recursively using:
n2 = (2 * n - 1) + (n - 1)2
# let rec sq n = (* rec for recursion *)
match n with (* pattern matching for cases *)
| 0 -> 0 (* base case *)
| n -> (2 * n -1) + sq (n -1);; (* inductive case *)
val sq : int -> int = <fun>
# sq 3;;
22
Aside: Recursion and induction are deeply, beautifully related. (Discussed in thesis, if curious.)
Recursion in OCaml
23
Questions so far?
Recursion in OCaml
24
More Recursion Next Week
Recursion in OCaml
25
Evaluating OCaml Programs
Evaluating declarations
{x → 2, y → 3, a → “hi”} + {y → 100, b → 6} =
{x → 2, y → 3, a → “hi”, b → 6}
*
26
Evaluation
Evaluating declarations
{x → 2, y → 3, a → “hi”} + {y → 100, b → 6} =
{x → 2, y → 3, a → “hi”, b → 6}
*
27
Evaluation
Evaluating declarations
{x → 2, y → 3, a → “hi”} + {y → 100, b → 6} =
{x → 2, y → 3, a → “hi”, b → 6}
*
28
Evaluation
Evaluating declarations
{x → 2, y → 3, a → “hi”} + {y → 100, b → 6} =
{x → 2, y → 3, a → “hi”, b → 6}
*
29
Evaluation
Evaluating declarations
{x → 2, y → 3, a → “hi”} + {y → 100, b → 6} =
{x → 2, y → 3, a → “hi”, b → 6}
*
30
Evaluation
Evaluating expressions in OCaml
*
31
Evaluation
Evaluating expressions in OCaml
*
32
Evaluation
Evaluating expressions in OCaml
*
33
Evaluation
Evaluating expressions in OCaml
*
34
Evaluation
Evaluating expressions in OCaml
*
35
Evaluation
36
Questions so far?
Evaluation
Evaluating expressions in OCaml
for subexpression e’ and updated environment ρ’.
*
37
Evaluation
Evaluating expressions in OCaml
for subexpression e’ and updated environment ρ’
*
38
Evaluation
Evaluating expressions in OCaml
for subexpression e’ and updated environment ρ’
*
39
Evaluation
This gives us an algorithm to implement an interpreter!
Evaluating expressions in OCaml
Eval((e1 , … , Eval (en, ρ)), ρ)
Eval((e1 , … , Eval(ei, ρ), Val vi+1 , … , Val vn), ρ)
Val (v1 , … , vn)
*
40
Evaluation
Evaluating expressions in OCaml
Eval((e1 , … , Eval (en, ρ)), ρ)
Eval((e1 , … , Eval(ei, ρ), Val vi+1 , … , Val vn), ρ)
Val (v1 , … , vn)
*
41
Evaluation
Evaluating expressions in OCaml
Eval((e1 , … , Eval (en, ρ)), ρ)
Eval((e1 , … , Eval(ei, ρ), Val vi+1 , … , Val vn), ρ)
Val (v1 , … , vn)
*
42
Evaluation
Evaluating expressions in OCaml
Eval((e1 , … , Eval (en, ρ)), ρ)
Eval((e1 , … , Eval(ei, ρ), Val vi+1 , … , Val vn), ρ)
Val (v1 , … , vn)
*
43
Evaluation
Evaluating expressions in OCaml
Eval((e1 , … , Eval (en, ρ)), ρ)
Eval((e1 , … , Eval(ei, ρ), Val vi+1 , … , Val vn), ρ)
Val (v1 , … , vn)
*
44
Evaluation
Evaluating expressions in OCaml
Eval((e1 , … , Eval (en, ρ)), ρ)
Eval((e1 , … , Eval(ei, ρ), Val vi+1 , … , Val vn), ρ)
Val (v1 , … , vn)
*
45
Evaluation
46
Questions so far?
Evaluation
Evaluating expressions in OCaml
Eval(e1⦿ Eval(e2, ρ), ρ))
Eval(Eval(e1, ρ) ⦿ Val v2, ρ))
Val (v1⦿ v2)
*
47
Evaluation
Evaluating expressions in OCaml
Eval(e1⦿ Eval(e2, ρ), ρ))
Eval(Eval(e1, ρ) ⦿ Val v2, ρ))
Val (v1⦿ v2)
*
48
Evaluation
Evaluating expressions in OCaml
Eval(e1⦿ Eval(e2, ρ), ρ))
Eval(Eval(e1, ρ) ⦿ Val v2, ρ))
Val (v1⦿ v2)
*
49
Evaluation
Evaluating expressions in OCaml
Eval(e1⦿ Eval(e2, ρ), ρ))
Eval(Eval(e1, ρ) ⦿ Val v2, ρ))
Val (v1⦿ v2)
*
50
Evaluation
Evaluating expressions in OCaml
Eval(e1⦿ Eval(e2, ρ), ρ))
Eval(Eval(e1, ρ) ⦿ Val v2, ρ))
Val (v1⦿ v2)
*
51
Evaluation
Evaluating expressions in OCaml
let x = e1 in e2
*
52
Evaluation
Evaluating expressions in OCaml
let x = e1 in e2
*
53
Evaluation
Evaluating expressions in OCaml
let x = e1 in e2
*
54
Evaluation
Evaluating expressions in OCaml
if b then e1 else e2
*
55
Evaluation
Evaluating expressions in OCaml
if b then e1 else e2
*
56
Evaluation
Evaluating expressions in OCaml
if b then e1 else e2
*
57
Evaluation
Evaluating expressions in OCaml
if b then e1 else e2
*
58
Evaluation
Evaluating expressions in OCaml
if b then e1 else e2
*
59
Evaluation
Evaluating expressions in OCaml
if b then e1 else e2
*
60
Evaluation
61
Questions so far?
Evaluation
Evaluation of Application with Closures
*
62
Evaluation
Evaluation of Application with Closures
*
63
Evaluation
Evaluation of Application with Closures
*
64
Evaluation
Evaluation of Application with Closures
*
65
Evaluation
Evaluation of Application with Closures
*
66
Evaluation
Evaluation of Application with Closures
*
67
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (plus_x (Eval(z, ρ))) =>
Eval (plus_x (Val 3), ρ) =>
Eval ((Eval (plus_x, ρ)) (Val 3), ρ) =>
Eval ((Val <y → y + x, ρplus_x >)(Val 3), ρ) =>
Eval (y + x, {y → 3} + ρplus_x ) => …
*
68
Your turn!
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (plus_x (Eval(z, ρ))) =>
Eval (plus_x (Val 3), ρ) =>
Eval ((Eval (plus_x, ρ)) (Val 3), ρ) =>
Eval ((Val <y → y + x, ρplus_x >)(Val 3), ρ) =>
Eval (y + x, {y → 3} + ρplus_x ) => …
*
69
Keep going!
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (plus_x (Eval(z, ρ))) =>
Eval (plus_x (Val ?), ρ) =>
Eval ((Eval (plus_x, ρ)) (Val 3), ρ) =>
Eval ((Val <y → y + x, ρplus_x >)(Val 3), ρ) =>
Eval (y + x, {y → 3} + ρplus_x ) => …
*
70
Look it up!
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (plus_x (Eval(z, ρ))) =>
Eval (plus_x (Val ?), ρ) =>
Eval ((Eval (plus_x, ρ)) (Val 3), ρ) =>
Eval ((Val <y → y + x, ρplus_x >)(Val 3), ρ) =>
Eval (y + x, {y → 3} + ρplus_x ) => …
*
71
Look it up!
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (plus_x (Eval(z, ρ))) =>
Eval (plus_x (Val 3), ρ) =>
Eval ((Eval (plus_x, ρ)) (Val 3), ρ) =>
Eval ((Val <y → y + x, ρplus_x >)(Val 3), ρ) =>
Eval (y + x, {y → 3} + ρplus_x ) => …
*
72
Done with argument!
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (plus_x (Eval(z, ρ))) =>
Eval (plus_x (Val 3), ρ) =>
Eval ((Eval (plus_x, ρ)) (Val 3), ρ) =>
Eval ((Val <y → y + x, ρplus_x >)(Val 3), ρ) =>
Eval (y + x, {y → 3} + ρplus_x ) => …
*
73
Now what?
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (plus_x (Eval(z, ρ))) =>
Eval (plus_x (Val 3), ρ) =>
Eval ((Eval (plus_x, ρ)) (Val 3), ρ) =>
Eval ((Val <y → y + x, ρplus_x >)(Val 3), ρ) =>
Eval (y + x, {y → 3} + ρplus_x ) => …
*
74
Now what?
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (plus_x (Eval(z, ρ))) =>
Eval (plus_x (Val 3), ρ) =>
Eval ((Eval (plus_x, ρ)) (Val 3), ρ) =>
Eval ((Val ?)(Val 3), ρ) =>
Eval (y + x, {y → 3} + ρplus_x ) => …
*
75
Look it up!
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (plus_x (Eval(z, ρ))) =>
Eval (plus_x (Val 3), ρ) =>
Eval ((Eval (plus_x, ρ)) (Val 3), ρ) =>
Eval ((Val ?)(Val 3), ρ) =>
Eval (y + x, {y → 3} + ρplus_x ) => …
*
76
Look it up!
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (plus_x (Eval(z, ρ))) =>
Eval (plus_x (Val 3), ρ) =>
Eval ((Eval (plus_x, ρ)) (Val 3), ρ) =>
Eval ((Val <y → y + x, ρplus_x >)(Val 3), ρ) =>
Eval (y + x, {y → 3} + ρplus_x ) => …
*
77
Closure!
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (plus_x (Eval(z, ρ))) =>
Eval (plus_x (Val 3), ρ) =>
Eval ((Eval (plus_x, ρ)) (Val 3), ρ) =>
Eval ((Val <y → y + x, ρplus_x >)(Val 3), ρ) =>
Eval (y + x, {y → 3} + ρplus_x ) => …
*
78
What’s next?
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (y + x, {y → 3} + ρplus_x ) => …
*
79
How to evaluate closure?
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (y + x, … ) =>
*
80
Evaluate its body …
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (y + x, ? + ρplus_x ) =>
*
81
Evaluate its body in an updated environment!
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (y + x, {y → 3} + ρplus_x ) =>
*
82
Evaluate its body in an updated environment!
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (y + x, {y → 3} + ρplus_x ) =>
*
83
How to evaluate applied operator?
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (y + x, {y → 3} + ρplus_x ) =>
Eval (y + Eval(x, {y → 3} + ρplus_x ), {y → 3} + ρplus_x ) =>
*
84
RHS first!
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (y + x, {y → 3} + ρplus_x ) =>
Eval (y + Eval(x, {y → 3} + ρplus_x ), {y → 3} + ρplus_x ) =>
*
85
Look it up!
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (y + x, {y → 3} + ρplus_x ) =>
Eval (y + Eval(x, {y → 3} + ρplus_x ), {y → 3} + ρplus_x ) =>
Eval (y + Val 12), {y → 3} + ρplus_x ) =>
*
86
Now what?
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (y + x, {y → 3} + ρplus_x ) =>
Eval (y + Eval(x, {y → 3} + ρplus_x ), {y → 3} + ρplus_x ) =>
Eval (y + Val 12), {y → 3} + ρplus_x ) =>
*
87
LHS next!
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (y + x, {y → 3} + ρplus_x ) =>
Eval (y + Eval(x, {y → 3} + ρplus_x ), {y → 3} + ρplus_x ) =>
Eval (y + Val 12), {y → 3} + ρplus_x ) =>
Eval (Eval(y, {y → 3} + ρplus_x ) + Val 12, {y → 3} + ρplus_x ) =>
*
88
LHS next!
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (y + x, {y → 3} + ρplus_x ) =>
Eval (y + Eval(x, {y → 3} + ρplus_x ), {y → 3} + ρplus_x ) =>
Eval (y + Val 12), {y → 3} + ρplus_x ) =>
Eval (Eval(y, {y → 3} + ρplus_x ) + Val 12, {y → 3} + ρplus_x ) =>
*
89
Look it up in which environment?
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (y + x, {y → 3} + ρplus_x ) =>
Eval (y + Eval(x, {y → 3} + ρplus_x ), {y → 3} + ρplus_x ) =>
Eval (y + Val 12), {y → 3} + ρplus_x ) =>
Eval (Eval(y, {y → 3} + ρplus_x ) + Val 12, {y → 3} + ρplus_x ) =>
*
90
Look it up locally!
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (y + x, {y → 3} + ρplus_x ) =>
Eval (y + Eval(x, {y → 3} + ρplus_x ), {y → 3} + ρplus_x ) =>
Eval (y + Val 12), {y → 3} + ρplus_x ) =>
Eval (Eval(y, {y → 3} + ρplus_x ) + Val 12, {y → 3} + ρplus_x ) =>
Eval (Val 3 + Val 12 , {y → 3} + ρplus_x ) =>
*
91
Look it up locally!
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (y + x, {y → 3} + ρplus_x ) =>
Eval (y + Eval(x, {y → 3} + ρplus_x ), {y → 3} + ρplus_x ) =>
Eval (y + Val 12), {y → 3} + ρplus_x ) =>
Eval (Eval(y, {y → 3} + ρplus_x ) + Val 12, {y → 3} + ρplus_x ) =>
Eval (Val 3 + Val 12, {y → 3} + ρplus_x ) =>
*
92
Finally, the operator!
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (y + x, {y → 3} + ρplus_x ) =>
Eval (y + Eval(x, {y → 3} + ρplus_x ), {y → 3} + ρplus_x ) =>
Eval (y + Val 12), {y → 3} + ρplus_x ) =>
Eval (Eval(y, {y → 3} + ρplus_x ) + Val 12, {y → 3} + ρplus_x ) =>
Eval (Val 3 + Val 12, {y → 3} + ρplus_x ) =>
Val (3 + 12) = Val 15
*
93
Evaluation
Evaluation of Application of plus_x;;
ρ = { plus_x →<y → y + x, ρplus_x >, … ,
y → 19, x →17, z →3, … }
where: ρplus_x = {x → 12, … , y → 24, …}
Eval (y + x, {y → 3} + ρplus_x ) =>
Eval (y + Eval(x, {y → 3} + ρplus_x ), {y → 3} + ρplus_x ) =>
Eval (y + Val 12), {y → 3} + ρplus_x ) =>
Eval (Eval(y, {y → 3} + ρplus_x ) + Val 12, {y → 3} + ρplus_x ) =>
Eval (Val 3 + Val 12, {y → 3} + ρplus_x ) =>
Val (3 + 12) = Val 15
*
94
Evaluation
95
Questions so far?
Evaluation
Evaluation of Application of plus_pair
ρ = {x → 3 , … , plus_pair →<(n, m) →n + m, ρplus_pair>}
+ ρplus_pair
Eval (plus_pair (Eval ((4, x), ρ)), ρ) =>
Eval (plus_pair (Eval ((4, Eval (x , ρ)), ρ)), ρ) =>
Eval (plus_pair (Eval ((4, Val 3), ρ)), ρ) =>
Eval (plus_pair (Eval ((Eval (4, ρ), Val 3), ρ)), ρ) =>
Eval (plus_pair (Eval ((Val 4, Val 3), ρ)), ρ) =>
*
96
Evaluation
Evaluation of Application of plus_pair
ρ = {x → 3 , … , plus_pair →<(n, m) →n + m, ρplus_pair>}
+ ρplus_pair
Eval (plus_pair (Eval ((4, x), ρ)), ρ) =>
Eval (plus_pair (Eval ((4, Eval (x , ρ)), ρ)), ρ) =>
Eval (plus_pair (Eval ((4, Val 3), ρ)), ρ) =>
Eval (plus_pair (Eval ((Eval (4, ρ), Val 3), ρ)), ρ) =>
Eval (plus_pair (Eval ((Val 4, Val 3), ρ)), ρ) =>
*
97
Evaluation
Evaluation of Application of plus_pair
ρ = {x → 3 , … , plus_pair →<(n, m) →n + m, ρplus_pair>}
+ ρplus_pair
Eval (plus_pair (Eval ((4, x), ρ)), ρ) =>
Eval (plus_pair (Eval ((4, Eval (x , ρ)), ρ)), ρ) =>
Eval (plus_pair (Eval ((4, Val 3), ρ)), ρ) =>
Eval (plus_pair (Eval ((Eval (4, ρ), Val 3), ρ)), ρ) =>
Eval (plus_pair (Eval ((Val 4, Val 3), ρ)), ρ) =>
*
98
Evaluation
Evaluation of Application of plus_pair
ρ = {x → 3 , … , plus_pair →<(n, m) →n + m, ρplus_pair>}
+ ρplus_pair
Eval (plus_pair (Eval ((4, x), ρ)), ρ) =>
Eval (plus_pair (Eval ((4, Eval (x , ρ)), ρ)), ρ) =>
Eval (plus_pair (Eval ((4, Val 3), ρ)), ρ) =>
Eval (plus_pair (Eval ((Eval (4, ρ), Val 3), ρ)), ρ) =>
Eval (plus_pair (Eval ((Val 4, Val 3), ρ)), ρ) =>
*
99
Evaluation
Evaluation of Application of plus_pair
ρ = {x → 3 , … , plus_pair →<(n, m) →n + m, ρplus_pair>}
+ ρplus_pair
Eval (plus_pair (Eval ((4, x), ρ)), ρ) =>
Eval (plus_pair (Eval ((4, Eval (x , ρ)), ρ)), ρ) =>
Eval (plus_pair (Eval ((4, Val 3), ρ)), ρ) =>
Eval (plus_pair (Eval ((Eval (4, ρ), Val 3), ρ)), ρ) =>
Eval (plus_pair (Eval ((Val 4, Val 3), ρ)), ρ) =>
*
100
Evaluation
Evaluation of Application of plus_pair
ρ = {x → 3 , … , plus_pair →<(n, m) →n + m, ρplus_pair>}
+ ρplus_pair
Eval (plus_pair (Eval ((4, x), ρ)), ρ) =>
Eval (plus_pair (Eval ((4, Eval (x , ρ)), ρ)), ρ) =>
Eval (plus_pair (Eval ((4, Val 3), ρ)), ρ) =>
Eval (plus_pair (Eval ((Eval (4, ρ), Val 3), ρ)), ρ) =>
Eval (plus_pair (Eval ((Val 4, Val 3), ρ)), ρ) =>
*
101
Evaluation
Evaluation of Application of plus_pair
ρ = {x → 3 , … , plus_pair →<(n, m) →n + m, ρplus_pair>}
+ ρplus_pair
Eval (plus_pair (Eval ((4, x), ρ)), ρ) =>
Eval (plus_pair (Eval ((4, Eval (x , ρ)), ρ)), ρ) =>
Eval (plus_pair (Eval ((4, Val 3), ρ)), ρ) =>
Eval (plus_pair (Eval ((Eval (4, ρ), Val 3), ρ)), ρ) =>
Eval (plus_pair (Eval ((Val 4, Val 3), ρ)), ρ) =>
*
102
Evaluation
Evaluation of Application of plus_pair
ρ = {x → 3 , … , plus_pair →<(n, m) →n + m, ρplus_pair>}
+ ρplus_pair
Eval (plus_pair (Eval ((4, x), ρ)), ρ) =>
Eval (plus_pair (Eval ((4, Eval (x , ρ)), ρ)), ρ) =>
Eval (plus_pair (Eval ((4, Val 3), ρ)), ρ) =>
Eval (plus_pair (Eval ((Eval (4, ρ), Val 3), ρ)), ρ) =>
Eval (plus_pair (Eval ((Val 4, Val 3), ρ)), ρ) =>
*
103
Evaluation
Evaluation of Application of plus_pair
ρ = {x → 3 , … , plus_pair →<(n, m) →n + m, ρplus_pair>}
+ ρplus_pair
Eval (plus_pair (Val (4, 3)), ρ) =>
Eval (Eval (plus_pair, ρ), Val (4, 3)), ρ) => …
Eval ((Val<(n,m)→n+m, ρplus_pair>)(Val(4,3)) , ρ)=>
Eval (Val (n + m), {n -> 4, m -> 3} + ρplus_pair) =>
Eval (Val (4 + 3), {n -> 4, m -> 3} + ρplus_pair) =>
Val 7
*
104
Evaluation
Evaluation of Application of plus_pair
ρ = {x → 3 , … , plus_pair →<(n, m) →n + m, ρplus_pair>}
+ ρplus_pair
Eval (plus_pair (Val (4, 3)), ρ) =>
Eval (Eval (plus_pair, ρ), Val (4, 3)), ρ) => …
Eval ((Val<(n,m)→n+m, ρplus_pair>)(Val(4,3)) , ρ)=>
Eval (Val (n + m), {n -> 4, m -> 3} + ρplus_pair) =>
Eval (Val (4 + 3), {n -> 4, m -> 3} + ρplus_pair) =>
Val 7
*
105
Evaluation
Evaluation of Application of plus_pair
ρ = {x → 3 , … , plus_pair →<(n, m) →n + m, ρplus_pair>}
+ ρplus_pair
Eval (plus_pair (Val (4, 3)), ρ) =>
Eval (Eval (plus_pair, ρ), Val (4, 3)), ρ) => …
Eval ((Val<(n,m)→n+m, ρplus_pair>)(Val(4,3)) , ρ)=>
Eval (Val (n + m), {n -> 4, m -> 3} + ρplus_pair) =>
Eval (Val (4 + 3), {n -> 4, m -> 3} + ρplus_pair) =>
Val 7
*
106
Evaluation
Evaluation of Application of plus_pair
ρ = {x → 3 , … , plus_pair →<(n, m) →n + m, ρplus_pair>}
+ ρplus_pair
Eval (plus_pair (Val (4, 3)), ρ) =>
Eval (Eval (plus_pair, ρ), Val (4, 3)), ρ) =>
Eval ((Val<(n,m)→n+m, ρplus_pair>)(Val(4,3)) , ρ)=>
Eval (Val (n + m), {n -> 4, m -> 3} + ρplus_pair) =>
Eval (Val (4 + 3), {n -> 4, m -> 3} + ρplus_pair) =>
Val 7
*
107
Evaluation
Evaluation of Application of plus_pair
ρ = {x → 3 , … , plus_pair →<(n, m) →n + m, ρplus_pair>}
+ ρplus_pair
Eval (plus_pair (Val (4, 3)), ρ) =>
Eval (Eval (plus_pair, ρ), Val (4, 3)), ρ) =>
Eval ((Val<(n,m)→n+m, ρplus_pair>)(Val (4 , 3)) , ρ)=>
Eval (Val (n + m), {n -> 4, m -> 3} + ρplus_pair) =>
Eval (Val (4 + 3), {n -> 4, m -> 3} + ρplus_pair) =>
Val 7
*
108
Evaluation
Evaluation of Application of plus_pair
ρ = {x → 3 , … , plus_pair →<(n, m) →n + m, ρplus_pair>}
+ ρplus_pair
Eval (plus_pair (Val (4, 3)), ρ) =>
Eval (Eval (plus_pair, ρ), Val (4, 3)), ρ) =>
Eval ((Val<(n,m)→n+m, ρplus_pair>)(Val (4 , 3)) , ρ)=>
Eval (Val (n + m), {n -> 4, m -> 3} + ρplus_pair) =>
Eval (Val (4 + 3), {n -> 4, m -> 3} + ρplus_pair) =>
Val 7
*
109
Evaluation
Evaluation of Application of plus_pair
ρ = {x → 3 , … , plus_pair →<(n, m) →n + m, ρplus_pair>}
+ ρplus_pair
Eval (plus_pair (Val (4, 3)), ρ) =>
Eval (Eval (plus_pair, ρ), Val (4, 3)), ρ) =>
Eval ((Val<(n,m)→n+m, ρplus_pair>)(Val (4 , 3)) , ρ)=>
Eval (Val (n + m), {n -> 4, m -> 3} + ρplus_pair) =>
Eval (Val (4 + 3), {n -> 4, m -> 3} + ρplus_pair) =>
Val 7
*
110
Evaluation
Evaluation of Application of plus_pair
ρ = {x → 3 , … , plus_pair →<(n, m) →n + m, ρplus_pair>}
+ ρplus_pair
Eval (plus_pair (Val (4, 3)), ρ) =>
Eval (Eval (plus_pair, ρ), Val (4, 3)), ρ) =>
Eval ((Val<(n,m)→n+m, ρplus_pair>)(Val (4 , 3)) , ρ)=>
Eval (Val (n + m), {n -> 4, m -> 3} + ρplus_pair) =>
Eval (Val (4 + 3), {n -> 4, m -> 3} + ρplus_pair) =>
Val 7
*
111
Evaluation
Evaluation of Application of plus_pair
ρ = {x → 3 , … , plus_pair →<(n, m) →n + m, ρplus_pair>}
+ ρplus_pair
Eval (plus_pair (Val (4, 3)), ρ) =>
Eval (Eval (plus_pair, ρ), Val (4, 3)), ρ) =>
Eval ((Val<(n,m)→n+m, ρplus_pair>)(Val (4 , 3)) , ρ)=>
Eval (Val (n + m), {n -> 4, m -> 3} + ρplus_pair) =>
Eval (Val (4 + 3), {n -> 4, m -> 3} + ρplus_pair) =>
Val 7
*
112
Evaluation
113
Questions?
for subexpression e’ and updated environment ρ’
Takeaways
114
115
Next Class:
Lists, more recursion
Reminder: Also Next Class
116
Next Class