16
p: (d e) b 3 : (f e d) b 2 E 1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b 3 E 2 d:13 e:40 E 1 53 b 1 E 3 x:40 y:13 E 2 53 Practice Session 10 The Environment Model

P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1

Embed Size (px)

Citation preview

Page 1: P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1

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

b2E1

GE

a:8b:5c:8

f:

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

a:8b:5c:3

GE53

p:

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

b3E2

d:13e:40

E1

53 b1E3

x:40y:13

E2

53Practice Session 10

The Environment Model

Page 2: P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1

• Frame: A substitution, a mapping between variables and values. 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: P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1

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: P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1

• 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(b:(* x x(

p:(y(b:y

> (lambda (y) y)

b1

b2

Environment Model: Introduction

Page 5: P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1

• 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(b:(* x x(

GEE1

Bx:5

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

> (square 5)

b1

Environment Model: Introduction

Page 6: P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1

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: P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1

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: P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1

Environment Model: Definition and Let

GE

a:8b:5c:8f:

b1E1

x:8y:8

GE16

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

Page 9: P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1

GE53b2E1

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: P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1

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: P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1

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

Page 12: P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1

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

Environment Model: Pair ADT

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

5

Page 13: P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1

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

Environment Model: Pair ADT

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

b4

b3

Page 14: P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1

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: P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1

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: P: (d e) b 3 : (f e d) b2b2 E1E1 GE a:8 b:5 c:8 f: p: (x y) b 1 : (+ x y) a:8 b:5 c:3 GE 53 p: p: (a b c) b 2 : (let…) b3b3 E2E2 d:13 e:40 E 1 53 b1b1

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