Practice session #9: The environment model. Frame: A substitution from variables to values (i.e.,...

16
Practice session #9: The environment model

Transcript of Practice session #9: The environment model. Frame: A substitution from variables to values (i.e.,...

Page 1: Practice session #9: The environment model. Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment:

Practice session #9:

The environment model

Page 2: Practice session #9: The environment model. Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment:

• Frame: A substitution from variables to values (i.e., every variable in a frame has a single value).

• Environment: A finite sequence 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: For an environment E, the enclosing environment is E, excluding its first frame.

GEI

x:3y:5

IIz:6x:7

IIIn:1y:2

E1 E2

E1=<II,I>, E2=<III,I>, GE=<I>

Enclosing-env (E1) = GEEnclosing-env (E2) = GE

Environments:

Environment Model: Introduction

Page 3: Practice session #9: The environment model. Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment:

GEI

x:3y:5

IIz:6x:7

IIIn:1y:2

E1 E2

E1=<II,I>, E2=<III,I>, GE=<I>

Enclosing-env (E1) = GEEnclosing-env (E2) = GE

• The value of variable in an environment:The value of x in the environment E is its values in the first frame in which it is define.

Environments:

Environment Model: Introduction

Page 4: Practice session #9: The environment model. Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment:

• A procedure value (closure) is a data structure with:

(1) Procedure parameters.

(2) Procedure body.

(3) The environment in which it has been created.

Procedures:

> (define square (lambda (x) (* x x)))

GEsquare:

p:(x(b1:(* x x(

p:(y(b2:y

> (lambda (y) y)

b1

b2

Environment Model: Introduction

Page 5: Practice session #9: The environment model. Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment:

• 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.

Procedures:

GEsquare:

p:(x(b1:(* x x(

GEE1 B1

x:5

> (define square (lambda (x) (* x x)))

> (square 5)

b1

Environment Model: Introduction

Page 6: Practice session #9: The environment model. Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment:

GE

sq:

sum-of-squares:

f:

p: (x)b1: (* x x)

p: (x y(b2: (+ (sq x) (sq y))

p: (a)b3: (sum-of-squares (+ a 1) (* a 2))

Environment Model: Definition and Application

Page 7: Practice session #9: The environment model. Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment:

B3E1

a:5

GE136

B2E2

x:6y:10

B1E3

x:6B1

x:10

E2

100

E1

136

GE

sq:

sum-of-squares:

f:

p: (x)b1: (* x x)

p: (x y(b2: (+ (sq x) (sq y))

p: (a)b3: (sum-of-squares (+ a 1) (* a 2))

Environment Model: Definition and Application

E2

36

E4

Page 8: Practice session #9: The environment model. Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment:

Environment Model: Definition and Let

GE

a:8b:5c:8f:

b1E1

x:8y:8

GE16

p: (x y)b1: (+ x y)

Page 9: Practice session #9: The environment model. Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment:

GE53

b2E1

a:8b:5c:3

p: (a b c)b2: (let…)

E1

53b3E2

d:13e:40

E2

53b1E3

x:40y:13

f:

p: (x y)b1: (+ x y)

p:

p: (d e)b3: (f e d)

Environment Model: Definition and Let

GE

a:8b:5c:8

Page 10: Practice session #9: The environment model. Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment:

GE

p:(n)b:(if…)

fact:

bE1

n:3

GE6

bE2

n:2

E1

2bE3

n:1

E2

1bE4

n:0

E3

1

Environment Model: Recursion

Page 11: Practice session #9: The environment model. Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment:

GE

p:(x y)b1:(lambda(sel)…)

make-pair:P1:

p:(sel)b2:(sel x y)

b1E1

x:5y:10

GE

Environment Model: Pair ADT, Lazy implementation

Page 12: Practice session #9: The environment model. Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment:

GEb2E2

sel:

b3E3

a:5b:10

E2

5

GE

p:(x y)b1:(lambda(sel)…)

make-pair:P1:

p:(sel)b2:(sel x y)

b1E1

x:5y:10

GE

p:(a b)b3:a

( >p1 (lambda (a b( a((

5

Environment Model: Pair ADT, Lazy implementation

Page 13: Practice session #9: The environment model. Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment:

p:(x y)b3:(p1(lambda…)

b3E2

x:1y:2

GE

p:(first second)b4:first

b1E3 E2

sel:

b4E4

first:5second:10

GE

p:(x y)b1:(lambda(sel)…)

make-pair:P1:

p:(sel)b2:(sel x y)

b1E1

x:5y:10

GE

> (let ((x 1( (y 2(( (p1 (lambda (first second( first(((

b4

b3

Environment Model: Pair ADT, Lazy implementation

Page 14: Practice session #9: The environment model. Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment:

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).

Page 15: Practice session #9: The environment model. Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment:

GE

p:(x y)b1:(lambda(sel)…)

make-pair:p1:

p:(sel)b2:(sel x y)

b1E1

x:5y:10

p:(x y)b3:(p1(lambda…)b3E2

x:1y:2

p:(first second)b4:first

b2E3

sel:

b4E4

first:1second:2

1

11

Environment Model: Dynamic Scoping - Example

> (let ((x 1( (y 2(( (p1 (lambda (first second( first(((

b4

b3

Page 16: Practice session #9: The environment model. Frame: A substitution from variables to values (i.e., every variable in a frame has a single value). Environment:

p:(n c)b1:(if…)

GE

fact$:

b1E1

n:3

GE6

p(fact-3)b3:(fact-3)

c:

p:(fact-n-1)b2:(c…)

b1E2

n:2

E1

6

c:

p:(fact-n-1)b2:(c…)

b1E3

n:1

E2

6

c:

b2E4

fact-n-1:1

E3

6

b2E5

fact-n-1:2

E4

6

b3E6

fact-n-1:6

E5

6

Environment Model: CPS