****** 1

Practice session #7:
The environment model
Environment Model: Introduction
Environments:
• Frame:
GE
Maps variables to values (i.e., every variable in a
frame has a single value).
I
x:3
y:5
• Environment:
A finite linked list of frames, where the last frame
is the global environment.
• The global environment:
A single-frame environment, the only environment
that statically exists.
• Enclosing environment of E:
E, excluding its first frame.
II
z:6
x:7
III
n:1
y:2
E1
E2
E1=<II,I>, E2=<III,I>, GE=<I>
Enclosing-env (E1) = GE
Enclosing-env (E2) = GE
Environment Model: Introduction
Environments:
• The value of variable x in E:
GE
The value of x in the first frame where it is defined.
I
x:3
y:5
II
z:6
x:7
III
n:1
y:2
E1
E2
E1=<II,I>, E2=<III,I>, GE=<I>
Enclosing-env (E1) = GE
Enclosing-env (E2) = GE
Environment Model: Introduction
Closure:
A complex data structure with:
(1) Procedure parameters.
(2) Procedure body.
(3) The environment at the time it was created.
b1
> (define square (lambda (x) (* x x)))
> (lambda (y) y)
b2
GE
square:
p:(x(
b1: (* x x)
p:(y(
b2:y
Environment Model: Introduction
Procedure application:
(1) Create new frame, mapping the procedure params to application values.
(2) The new frame extends the environment associated with the procedure.
(3) Evaluate the body of the procedure in the new environment.
b1
> (define square (lambda (x) (* x x)))
> (square 5)
GE
square:
p:(x(
b1: (* x x)
E1
b1
x:5
GE
Environment Model: Recursion
GE
fact:
p:(n)
b:(if…)
E1
b
n:3
GE
6
E2
b
n:2
E1
2
E3
b
n:1
E2
1
E4
b
n:0
E3
1
Environment Model: Definition and Application
GE
sq:
p: (x)
b1: (* x x)
sum-of-squares:
f:
p :(x y(
b2: (+ (sq x)
(sq y))
p: (a)
b3: (sum-of-squares
(+ a 1)
(* a 2))
Environment Model: Definition and Application
GE
sq:
p: (x)
b1: (* x x)
sum-of-squares:
f:
p :(x y(
b2: (+ (sq x)
(sq y))
E1
B3
GE
136
a:5
E2
x:6
y:10
B2
E1
136
p: (a)
b3: (sum-of-squares
(+ a 1)
(* a 2))
E3
x:6
B1
E2
36
E4
x:10
B1
E2
100
Environment Model: Definition and Let
GE
a:8
b:5
c:8
f:
p: (x y)
b1: (+ x y)
E1
x:8
y:8
b1
GE
16
Environment Model: Definition and Let
GE
a:8
b:5
c:8
f:
p: (x y)
b1: (+ x y)
p:
E1
b2
a:8
b:5
c:3
p: (a b c)
b2: (let…)
GE
53
p: (d e)
b3: (f e d)
E2
b3
E1
53
d:13
e:40
E3
b1
x:40
y:13
E2
53
Environment Model: Pair ADT, Lazy implementation
GE
make-pair:
P1:
E1
b1
x:5
y:10
p:(x y)
b1:(lambda(sel)…)
GE
p:(sel)
b2:(sel x y)
Environment Model: Pair ADT, Lazy implementation
GE
make-pair:
P1:
E1
x:5
y:10
p:(a b)
b3:a
E2
sel:
b1
b2
p:(x y)
b1:(lambda(sel)…)
GE
p:(sel)
b2:(sel x y)
GE
5
b3
E3
a:5
b:10
E2
5
> (p1 (lambda (a b) a))
Environment Model: Pair ADT, Lazy implementation
GE
make-pair:
P1:
E1
p:(x y)
b3:(p1(lambda…)
E2
b3
x:1
y:2
b1
x:5
y:10
GE
p:(first second)
b4:first
p:(x y)
b1:(lambda(sel)…)
GE
p:(sel)
b2:(sel x y)
E3
b2
E2
sel:
E4
b4
first:5
second:10
> (let ((x 1)
(y 2))
b3 (p1 (lambda (first second)
first)))
b4
Environment Model: Lexical (static) vs Dynamic Scoping
Lexical Scoping:
• A closure “carries” the environment in which it has been created.
• In application of a closure, its carried environment is extended.
Dynamic Scoping:
• A closure does not correspond to any environment.
• In application of a closure, the calling environment is extended.
• Simpler implementation (no need to “store” environments).
Environment Model: Dynamic Scoping - Example
GE
make-pair:
p1:
E1
p:(x y)
b1:(lambda(sel)…)
b1
p:(sel)
b2:(sel x y)
x:5
y:10
1
1
E2
E3
b3
x:1
y:2
p:(x y)
b3:(p1(lambda…)
b2
sel:
p:(first second)
b4:first
1
E4
b4
first:1
second:2
> (let ((x 1)
(y 2))
b3 (p1 (lambda (first second)
first)))
b4
Environment Model: box, set-box!, set!
Box:
• Required to wrap a variable so it can be accessed and changed, much like pointers.
• In the box environment model, it is used to enable mutation (seen in class)
• Box is defined by the interface:
(box v)
- value constructor
(box? x)
- type predicate
(unbox b) - accessor
(set-box!) - mutator
set!
•
A way to change the value of an existing variable. For example:
> (define x 5)
> x
5
> (set! x 80)
> x
80
> (set! y 5)
Error: cannot set variable before its
definition
Environment Model: set! vs set-box!
Example 1: counter (set! vs set-box!)
(define counter
(let ((count 0))
(lambda ()
(set! count (+ count 1))
count)))
> (counter)
1
> (define count 5)
> (counter)
2
E1
count:0 1
GE
counter
(lambda () (set!...
P = count
B = (lambda () (set!...
o count is the local state of the counter object.
17
E2
(set! count...
P=
B = (set! count...
Environment Model: set! vs set-box!
Example 1: counter (set! vs set-box!)
> (counter (lambda(x)
(set! x (+ x 1))))
0
> (counter (lambda(x)
(set! x (+ x 1))))
0
(define counter
(let ((count 0))
(lambda (modifer)
(modifer count)
count)))
GE
counter
E1
count:0
(lambda (modifier)...
P=x
B = (set! x...
E3
x: 0 1
(set! x...
18
P = count
B = (lambda (modifier)...
E2
modifier
(modifier count)…
P = modifier
B = (modifier count)…
Environment Model: set! vs set-box!
Example 1: counter (set! vs set-box!)
> (counter (lambda(x)
(set-box! x (+ x 1))))
1
> (counter (lambda(x)
(set-box! x (+ x 1))))
2
(define counter
(let ((count (box 0)))
(lambda (modifer)
(modifer count)
(unbox count))))
01
GE
counter
E1
count:
(lambda (modifier)...
P=x
B = (set-box! x...
E3
x:
(set-box! x...
19
P = count
B = (lambda (modifier)...
E2
modifier
(modifier count)…
P = modifier
B = (modifier count)…