/
Practice session #9: Practice session #9:

Practice session #9: - PowerPoint Presentation

briana-ranney
briana-ranney . @briana-ranney
Follow
386 views
Uploaded On 2016-04-05

Practice session #9: - PPT Presentation

The environment model Frame A substitution from variables to values ie every variable in a frame has a single value Environment A finite sequence of frames where the last frame is ID: 274403

sel environment lambda model environment sel model lambda fact pair frame procedure application implementation square closure lazy sum scoping

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Practice session #9:" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Practice session #9:

The environment modelSlide2

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.

GE

I

x:3y:5

IIz:6x:7

IIIn:1y:2

E1

E2

E

1

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

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

Environments:

Environment Model:

IntroductionSlide3

GE

I

x:3

y:5

II

z:6x:7

IIIn:1y:2

E1

E2

E

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

Enclosing-env

(E1) = GEEnclosing-env

(E2) = GEThe 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:

IntroductionSlide4

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

1:(* x x(

p:(y(

b

2:y

> (lambda (y) y)

b1b2

Environment Model:

IntroductionSlide5

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:

GE

square:

p:(x(

b1:(* x x(

GE

E1

B

1

x:5

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

> (square 5)b1

Environment Model:

IntroductionSlide6

GE

sq

:

sum-of-squares:

f:

p: (x)

b

1: (* x x)

p:

(

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

p: (a)

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

Environment Model:

Definition and ApplicationSlide7

B

3

E

1

a:5

GE136

B2

E2x:6y:10

B

1

E

3

x:6

B1

x:10

E

2

100E1136GE

sq

:

sum-of-squares:

f:

p: (x)

b

1

: (* x x)

p:

(

x y(

b

2

: (+ (sq x)

(sq y))

p: (a)

b

3

: (sum-of-squares

(+ a 1)

(* a 2))

Environment Model:

Definition and Application

E

2

36

E

4Slide8

Environment Model:

Definition and Let

GE

a:8

b:5

c:8

f:

b

1

E

1

x:8

y:8

GE

16

p: (x y)

b

1: (+ x y)Slide9

GE

53

b

2

E

1a:8b:5

c:3

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

53

b

3

E2

d:13e:40

E253

b1

E

3

x:40y:13f:

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

p:

p: (d e)

b

3

: (f e d)

Environment Model:

Definition and Let

GE

a:8

b:5

c:8Slide10

GE

p:(n)

b:(if…)

fact:

b

E

1

n:3GE

6

b

E

2

n:2

E12

b

E

3

n:1E21

b

E

4

n:0

E

3

1

Environment Model:

RecursionSlide11

GE

p:(x y)

b

1

:(lambda(

sel)…)

make-pair:P1:

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

1

E

1

x:5

y:10GE

Environment Model:

Pair

ADT, Lazy implementationSlide12

GE

b

2

E

2

sel:

b3

E3a:5

b:10

E

2

5

GE

p:(x y)b1:(lambda(

sel)…)

make-pair:

P1:

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

x:5

y:10

GE

p:(a b)

b

3

:a

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

5

Environment Model:

Pair

ADT, Lazy implementationSlide13

GE

b

2

E

2

sel:

b3

E3a:5

b:10

E

2

5

GE

p:(x y)b1:(lambda(

sel)…)

make-pair:

P1:

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

x:5

y:10

GE

p:(a b)

b

3

:a

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

5

Environment Model:

Pair

ADT, Lazy implementationSlide14

p:(x y)

b

3:(p1(lambda…)

b

3

E2x:1

y:2GE

p:(first second)b4:first

b

1

E

3

E

2sel:

b

4

E

4first:5second:10GE

p:(x y)

b

1

:(lambda(

sel

)…)

make-pair:

P1:

p:(

sel

)

b

2

:(

sel

x y)

b

1

E

1

x:5

y:10

GE

> (let ((x 1)

(y 2))

(p1 (lambda (first second)

first)))

b4

b3

Environment Model:

Pair

ADT, Lazy implementationSlide15

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

GE

p:(x y)

b

1

:(lambda(

sel)…)make-pair:p1:

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

b1E1

x:5

y:10

p:(x y)

b

3:(p1(lambda…)

b3E2x:1y:2

p:(first second)

b

4

:firstb2E3sel:

b

4

E

4

first:1

second:2

1

1

1

Environment Model:

Dynamic Scoping - Example

> (let ((x 1)

(y 2))

(p1 (lambda (first second)

first)))

b4

b3Slide17

p:(n c)

b

1:(if…)GE

fact$:

b

1

E1

n:3

GE6

p(fact-3)

b3:(fact-3)

c:

p:(fact-n-1)

b2:(c…)b1

E

2

n:2

E16c:p:(fact-n-1)b

2:(c…)

b

1

E

3

n:1

E

2

6

c:

b

2

E

4

fact-n-1:1

E

3

6

b

2

E

5

fact-n-1:2

E

4

6

b

3

E

6

fact-n-1:6

E

5

6

Environment Model:

CPS