lecture 16 the environment model n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Lecture 16 The Environment Model PowerPoint Presentation
Download Presentation
Lecture 16 The Environment Model

Loading in 2 Seconds...

play fullscreen
1 / 64

Lecture 16 The Environment Model - PowerPoint PPT Presentation


  • 114 Views
  • Uploaded on

Lecture 16 The Environment Model. Substitution Model. Represents computation as doing substitutions for bound variables at reduction of let, application: let val x = v in e  e { v / x } (fn( x : t )=> e )( v )  e { v / x } let val x = fn z:’a=>z in x(x(x))

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Lecture 16 The Environment Model' - easter


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
substitution model
Substitution Model
  • Represents computation as doing substitutions for bound variables at reduction of let, application:

let val x = v in e e{v/x}

(fn(x:t)=>e)(v) e{v/x}

let val x = fn z:’a=>z in

x(x(x))

end (fn z=>z)((fn z=>z)(fn z=>z))

problems
Problems
  • Not a realistic implementation: substitution is too slow

(fn(x:t)=>e)(v) e{v/x}

… could be many x’s to substitute for…

  • Doesn’t handle refs: no aliasing

ref2?

let val a = ref 2 in(fn(x,y)=>…x:=1…!y…) (a,a)

end

 (fn(x,y)=>…x:=1…!y…)(ref 2,ref 2) ? …(ref 2):=1…!(ref 2)… ?

environment model
Environment Model

Don’t substitute for variables; look them up lazily in an environment!

  • No substitution, realistic cost model
  • The environment is a finite map from variables to values
  • Example:let val x = 2 val y = “hello” val f = fn z:int=>xin f(x + size(y)) end

Evaluate:

f(x + size(y))

in environment:

x: 2y: “hello”f: fn z:int=>x

?

variables
Variables
  • To evaluate a variable, look it up in the environment. To look it up, we start with the last binding added to the environment and then work towards the nil.
  • Evaluating “x” in this environment yields 3:

nil

x : 2

y : 4

x : 3

Blue boxes:

bindings

env

z : 3

let expressions
Let expressions

To evaluate let val x = e1 in e2:

  • Evaluate e1 in the current environment
  • Extend the current environment with a binding that maps x to the value of e1
  • Evaluate e2 in the extended environment
  • Restore the old environment (i.e., remove the binding for x)
  • Return the value of e2
slide7

Let Example

let val x = (1,2) in #1 x end

nil

current env

let example
Let Example

let val x = (1,2) in #1 x end

1. Evaluating (1,2) yields a pointer to a tuple in memory.

1 2

nil

current env

slide9

Let Example

let val x = (1,2) in #1 x end

1. Evaluating (1,2) yields a pointer to a tuple in memory.

1 2

2. Extend the environment with a binding for x.

x:

current env

nil

current env

slide10

Let Example

let val x = (1,2) in #1 x end

1. Evaluating (1,2) yields a pointer to a tuple in memory.

1 2

2. Extend the environment with a binding for x.

3. Evaluate the body of the let in the new environment.

x evaluates to a pointer to the tuple, so #1 x evaluates to the first component, namely 1.

x:

current env

nil

slide11

Let Example

let val x = (1,2) in #1 x end

1. Evaluating (1,2) yields a pointer to a tuple in memory.

1 2

2. Extend the environment with a binding for x.

3. Evaluate the body of the let in the new environment.

x evaluates to a pointer to the tuple, so #1 x evaluates to the first component, namely 1.

x:

current env

current env

4. Restore the old environment.

nil

slide12

Let Example

let val x = (1,2) in #1 x end

1. Evaluating (1,2) yields a pointer to a tuple in memory.

1 2

2. Extend the environment with a binding for x.

3. Evaluate the body of the let in the new environment.

x evaluates to a pointer to the tuple, so #1 x evaluates to the first component, namely 1.

x:

current env

4. Restore the old environment.

nil

5. Return the value we got: 1

pictorial overview
Pictorial Overview:
  • Primitive values like integers, reals, unit, or nil evaluate to themselves.
  • A tuple value, such as (1,2,3) evaluates to a pointer to a box in memory containing the values of the sub-expressions:

1

2

3

multiple declarations
Multiple Declarations

To evaluate:let val x = e1 val y = e2 val z = e3 in e4 end

Do the same the same thing as you would for:let val x = e1 in let val y = e2 in let val z = e3 in e4 end end

end

example
Example

let val x = (3,4)in let val y = (x,x) in #1(#2 y) end

end

let val x = (3,4) val y = (x,x)in #1(#2 y)end

current env

nil

evaluation of example
Evaluation of Example

let val x = (3,4)in let val y = (x,x) in #1(#2 y) end

end

let val x = (3,4) val y = (x,x)in #1(#2 y)end

3 4

current env

nil

slide17

Evaluation of Example

let val x = (3,4)in let val y = (x,x) in #1(#2 y) end

end

let val x = (3,4) val y = (x,x)in #1(#2 y)end

3 4

current env

x :

current env

nil

slide18

Evaluation of Example

let val x = (3,4)in let val y = (x,x) in #1(#2 y) end

end

let val x = (3,4) val y = (x,x)in #1(#2 y)end

3 4

current env

x :

nil

slide19

Evaluation of Example

let val x = (3,4)in let val y = (x,x) in #1(#2 y) end

end

let val x = (3,4) val y = (x,x)in #1(#2 y)end

current env

y :

3 4

current env

x :

nil

slide20

Evaluation of Example

let val x = (3,4)in let val y = (x,x) in #1(#2 y) end

end

let val x = (3,4) val y = (x,x)in #1(#2 y)end

current env

y :

3 4

x :

nil

slide21

Evaluation of Example

let val x = (3,4)in let val y = (x,x) in #1(#2 y) end

end

let val x = (3,4) val y = (x,x)in #1(#2 y)end

current env

y :

3 4

x :

Result : 3

nil

slide22

Evaluation of Example

let val x = (3,4)in let val y = (x,x) in #1(#2 y)end

end

let val x = (3,4) val y = (x,x)in#1(#2 y)end

y :

Restore last env

3 4

current env

x :

Result is 3

nil

slide23

Evaluation of Example

let val x = (3,4)in let val y = (x,x) in #1(#2 y) end

end

let val x = (3,4) val y = (x,x)in#1(#2 y)end

y :

3 4

x :

Restore original env

Result is 3

current env

nil

slide24
Refs
  • To evaluate ref e, evaluate e to a value first, and then allocate a new ref cell, place the value in the ref cell, and return a pointer to the ref cell. For instance, ref (1,2,3)evaluates to:

1

2

3

ref cells = red boxes.

ref example
Ref Example

let val x = ref 2 in

val y = x

in

x:=1; !y

end

2

current env

nil

ref example1
Ref Example

let val x = ref 2 in

val y = x

in

x:=1; !y

end

x :

current env

2

nil

ref example2
Ref Example

let val x = ref 2 in

val y = x

in

x:=1; !y

end

x :

current env

2

nil

ref example3
Ref Example

let val x = ref 2 in

val y = x

in

x:=1; !y

end

y :

current env

x :

2

nil

ref example4
Ref Example

let val x = ref 2 in

val y = x

in

x:=1; !y

end

y :

current env

x :

1

nil

ref example5
Ref Example

let val x = ref 2 in

val y = x

in

x:=1; !y

end

Result: 1

y :

current env

x :

1

nil

functions
Functions

Static scope:

ML, Java, Scheme, …

Dynamic scope:Perl, Python, BASIC

let val x = 2 val f = fn z:int => xinlet val x = “bye” in f(size(x)) end

  • How do we make sure the environment has the (correct) binding for x?
    • We must keep track of the environment at the point where the function was evaluated.
    • Function evaluation: fn z:int => x, not f(size(x))
  • We create a closure
    • A pair of a function and its environment
functions1
Functions
  • To evaluate a function(fn x => e)create a closure out of the function and the current environment and return a pointer to the closure.

nil

x : 2

y : 4

current env

slide33

Creating closures

  • To evaluate a function (fn x => e)create a closure out of the function and the current environment and return a pointer to the closure.

fn x => e

nil

x : 2

y : 4

current env

I’ll draw closures using yellow.

Result

function example
Function Example

fn z:int => x

x : 2

current env

current env

let val x = 2 val f = fn z:int => xinlet val x = “bye” in f(size(x)) end

nil

function example1
Function Example

fn z:int => x

x : 2

current env

let val x = 2 val f = fn z:int => xinlet val x = “bye” in f(size(x)) end

f:

nil

function example2
Function Example

fn z:int => x

x : 2

x :

current env

let val x = 2 val f = fn z:int => xinlet val x = “bye” in f(size(x)) end

“bye”

f:

nil

function example3
Function Example

fn z:int => x

x : 2

x :

current env

let val x = 2 val f = fn z:int => xinlet val x = “bye” inf(size(x)) end

“bye”

f:

nil

function calls
Function Calls

To evaluate e1(e2):

  • evaluate e1 -- you should get a pointer to a closure.
  • evaluate e2 to a value.
  • save the current environment -- we’ll come back to it after the function call.
  • extend the environment of the closure, mapping the formal argument to the actual argument.
  • evaluate the body of the function within the extended environment -- this gives us our result value.
  • restore the old environment (saved in step 3)
  • return the result.
function call example
Function Call Example

fn z:int => x

x : 2

x :

current env

let val x = 2 val f = fn z:int => xinlet val x = “bye” in f(size(x)) end

1. Evaluate e1, e2

“bye”

f:

nil

function call example1
Function Call Example

fn z:int => x

x : 2

x :

saved env

current env

let val x = 2 val f = fn z:int => xinlet val x = “bye” in f(size(x)) end

1. Evaluate e1, e22. Save environ.

“bye”

f:

nil

function call example2
Function Call Example

x : 2

x :

saved env

let val x = 2 val f = fn z:int => xinlet val x = “bye” in f(size(x)) end

1. Evaluate e1, e22. Save environ.3. Extend env with actual

“bye”

f:

fn z:int => x

current env

z : 3

nil

function call example3
Function Call Example

x : 2

x :

saved env

let val x = 2 val f = fn z:int => xinlet val x = “bye” in f(size(x)) end

1. Evaluate e1, e22. Save environ.3. Extend env with actual

4. Evaluate body (result: 2)

“bye”

f:

fn z:int => x

current env

z : 3

nil

function call example4
Function Call Example

x : 2

x :

current env

let val x = 2 val f = fn z:int => xinlet val x = “bye” in f(size(x)) end

1. Evaluate e1, e22. Save environ.3. Extend env with actual

4. Evaluate body (result: 2)

5. Restore env. (result: 2)

“bye”

f:

fn z:int => x

nil

creating a cycle
Creating a cycle

let val x = ref (fn x:int => x)

val f = fn n:int => if n <= 1 then 1 else n * (!x)(n-1)

in

x := f;

f(3)

end

nil

current env

slide45

Creating a cycle

let val x = ref (fn x => x)

val f = fn n => if n <= 1 then 1 else n * (!x)(n-1)

in

x := f;

f(3)

end

fn x => x

nil

current env

slide46

Creating a cycle

let val x = ref (fn x => x)

val f = fn n => if n <= 1 then 1 else n * (!x)(n-1)

in

x := f;

f(3)

end

fn x => x

nil

current env

slide47

Creating a cycle

let val x = ref (fn x => x)

val f = fn n => if n <= 1 then 1 else n * (!x)(n-1)

in

x := f;

f(3)

end

current env

x :

fn x => x

nil

slide48

Creating a cycle

let val x = ref (fn x => x)

val f = fn n => if n <= 1 then 1 else n * (!x)(n-1)

in

x := f;

f(3)

end

fn n => if n <= 1 then 1

else n * (!x)(n-1)

current env

x :

fn x => x

nil

slide49

Creating a cycle

fn x => x

let val x = ref (fn x => x)

val f = fn n => if n <= 1 then 1 else n * (!x)(n-1)

in

x := f;

f(3)

end

fn n => if n <= 1 then 1

else n * (!x)(n-1)

f :

current env

x :

nil

slide50

Creating a cycle

fn x => x

let val x = ref (fn x => x)

val f = fn n => if n <= 1 then 1 else n * (!x)(n-1)

in

x := f;

f(3)

end

fn n => if n <= 1 then 1

else n * (!x)(n-1)

f :

current env

x :

nil

slide51

Creating a cycle

let val x = ref (fn x => x)

val f = fn n => if n <= 1 then 1 else n * (!x)(n-1)

in

x := f;

f(3)

end

fn n => if n <= 1 then 1

else n * (!x)(n-1)

f :

current env

x :

nil

slide52

Creating a cycle

Note: !x is the same as f

let val x = ref (fn x => x)

val f = fn n => if n <= 1 then 1 else n * (!x)(n-1)

in

x := f;

f(3)

end

fn n => if n <= 1 then 1

else n * (!x)(n-1)

f :

saved env

current env

n : 3

x :

nil

slide53

Creating a cycle

let val x = ref (fn x => x)

val f = fn n => if n <= 1 then 1 else n * (!x)(n-1)

in

x := f;

f(3)

end

fn n => if n <= 1 then 1

else n * (!x)(n-1)

f :

saved env 1

saved env 2

n : 3

curr env

x :

n : 2

nil

slide54

Creating a cycle

let val x = ref (fn x => x)

val f = fn n => if n <= 1 then 1 else n * (!x)(n-1)

in

x := f;

f(3)

end

fn n => if n <= 1 then 1

else n * (!x)(n-1)

f :

saved env 1

saved env 2

n : 3

saved 3

x :

n : 2

curr env

Result = 1

nil

n : 1

slide55

Creating a cycle

let val x = ref (fn x => x)

val f = fn n => if n <= 1 then 1 else n * (!x)(n-1)

in

x := f;

f(3)

end

fn n => if n <= 1 then 1

else n * (!x)(n-1)

f :

saved env 1

saved env 2

n : 3

curr env

x :

n : 2

Result = 2*1

nil

n : 1

slide56

Creating a cycle

let val x = ref (fn x => x)

val f = fn n => if n <= 1 then 1 else n * (!x)(n-1)

in

x := f;

f(3)

end

fn n => if n <= 1 then 1

else n * (!x)(n-1)

f :

saved env 1

curr env

n : 3

x :

n : 2

Result = 3*2*1

nil

n : 1

slide57

Creating a cycle

let val x = ref (fn x => x)

val f = fn n => if n <= 1 then 1 else n * (!x)(n-1)

in

x := f;

f(3)

end

fn n => if n <= 1 then 1

else n * (!x)(n-1)

f :

curr env

n : 3

x :

n : 2

f(3) = 6

nil

n : 1

recursion
Recursion

let fun f(n) =

if n <= 1 then 1 else n * f(n-1)

in

f(3)

end

  • create a new binding for f before creating the closure and extend the current environment with it (but don’t put in the value yet.)
  • now create a closure for f that uses the extended environment.
  • fix the binding to use the closure!
recursion1
Recursion

let fun f(n) => if n <= 1 then 1 else n * f(n-1)

in

f(3)

end

No value for f yet!

  • create a new binding for f before creating the closure and extend the current environment with it (but don’t put in the value yet.)
  • now create a closure for f that uses the extended environment.
  • fix the binding to use the closure!

current env

f :

nil

slide60

Recursion

let fun f(n) => if n <= 1 then 1 else n * f(n-1)

in

f(3)

end

fn n => if n <= 1 then 1 else n * f(n-1)

  • create a new binding for f before creating the closure and extend the current environment with it (but don’t put in the value yet.)
  • now create a closure for f that uses the extended environment.
  • fix the binding to use the closure!

current env

f :

nil

slide61

Recursion

let fun f(n) => if n <= 1 then 1 else n * f(n-1)

in

f(3)

end

fn n => if n <= 1 then 1 else n * f(n-1)

  • create a new binding for f before creating the closure and extend the current environment with it (but don’t put in the value yet.)
  • now create a closure for f that uses the extended environment.
  • fix the binding to use the closure!

current env

f :

nil

cycle
Cycle

let fun f(n) => if n <= 1 then 1 else n * f(n-1)

in

f(3)

end

  • Closure points to environment
  • Environment points to closure

fn n => if n <= 1 then 1 else n * f(n-1)

current env

f :

nil

cycle1
Cycle

let fun f(n) => if n <= 1 then 1 else n * f(n-1)

in

f(3)

end

fn n => if n <= 1 then 1 else n * f(n-1)

current env

f :

n : 3

nil

slide64

Comparison

current env

f :

fn n => if n <= 1 then 1 else n * (!x)(n-1)

x :

nil

fn n => if n <= 1 then 1 else n * f(n-1)

current env

f :

nil