Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.

Similar presentations


Presentation on theme: "Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the."— Presentation transcript:

1 Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the previous one) were taken from an old CalTech Course Page

2 Fall 2008Programming Development Techniques 2 bindings (review) a binding is an association between a name and a Scheme value names: –variable names, procedure names –formal parameters of procedures –in let statements: (let ((name value)...)...) values: any Scheme value

3 Fall 2008Programming Development Techniques 3 bindings (review) a binding is an association between a name and a Scheme value examples: –name: x value: 10 –name: y value: #f –name: square value: (lambda (x) (* x x))

4 Fall 2008Programming Development Techniques 4 frames (review) a frame is a collection of bindings: x: 10 y: #f square: (lambda (x) (* x x))

5 Fall 2008Programming Development Techniques 5 frames (review) frames are used to look up the value corresponding to a name –x = ? –y = ? –square = ? x: 10 y: #f square: (lambda (x) (* x x))

6 Fall 2008Programming Development Techniques 6 frames (review) frames have an enclosing environment –which will be another frame (parent frame) –if lookup fails, go to parent frame and try again –then to its parent frame etc. x: 10 y: #f square: (lambda (x) (* x x)) to parent frame

7 Fall 2008Programming Development Techniques 7 environments (review) an environment is a linked chain of frames ending in the global environment every frame defines an environment starting from it x: 10 y: 20 z: 30 (global environment) E1 E2 E3

8 Fall 2008Programming Development Techniques 8 environments (review) the global environment is there when Scheme interpreter starts up evaluating code can create new frames –and thus new environments all code being evaluated does so in the context of an environment –because names must be looked up –called the current environment

9 Fall 2008Programming Development Techniques 9 rule 1: define define creates a new binding in the current environment (current frame) example: (define x 10) (global environment)

10 Fall 2008Programming Development Techniques 10 rule 1: define define creates a new binding in the current environment (current frame) example: (define x 10) (global environment) x: 10

11 Fall 2008Programming Development Techniques 11 rule 2: set! set! changes an old binding in the current environment NEVER creates a new binding example: (set! x 'foo) (global environment) x: 10

12 Fall 2008Programming Development Techniques 12 rule 2: set! set! changes an old binding in the current environment NEVER creates a new binding example: (set! x 'foo) (global environment) x: foo

13 Fall 2008Programming Development Techniques 13 rule 2: set! set! can change a binding in a different frame (if no binding in current frame) example: (set! x 24) –no x in current env –so change in global env (global environment) x: foo y: 13 (current environment)

14 Fall 2008Programming Development Techniques 14 rule 2: set! set! can change a binding in a different frame (if no binding in current frame) example: (set! x 24) –no x in current env –so change in global env (global environment) x: 24 y: 13 (current environment)

15 Fall 2008Programming Development Techniques 15 rule 3: lambda a lambda expression (procedure) is a pair: –the text of the lambda expression –a pointer to the environment in which the lambda expression was evaluated (created)

16 Fall 2008Programming Development Techniques 16 rule 3: lambda evaluate: (lambda (x) (* x x)) global environment params: x code: (* x x) pointer to environment where lambda evaluated text of lambda

17 Fall 2008Programming Development Techniques 17 with define (define square (lambda (x) (* x x))) global environment params: x code: (* x x) square:

18 Fall 2008Programming Development Techniques 18 rule 4: evaluation changes the way we model apply To apply a procedure: 1.construct a new frame 2.bind the formal parameters of the procedure to the arguments of the procedure call 3.the new frame’s parent is the environment associated with the called procedure not the calling procedure 4.evaluate the body in the new environment

19 Fall 2008Programming Development Techniques 19 evaluating (square 10) global environment params: x code: (* x x) square:

20 Fall 2008Programming Development Techniques 20 evaluating (square 10) global environment params: x code: (* x x) square: new frame current environment

21 Fall 2008Programming Development Techniques 21 evaluating (square 10) global environment params: x code: (* x x) square: new frame current environment x: 10

22 Fall 2008Programming Development Techniques 22 evaluating (square 10) global environment params: x code: (* x x) square: current env evaluate: (* x x) x: 10

23 Fall 2008Programming Development Techniques 23 evaluating (square 10) global environment params: x code: (* x x) square: current env (* x x) => (* 10 10) => 100 x: 10

24 Fall 2008Programming Development Techniques 24 evaluating (square 10) global environment params: x code: (* x x) square:current env result: 100 new frame "goes away"

25 Fall 2008Programming Development Techniques 25 That was WAY too easy! Let's try something harder...

26 Fall 2008Programming Development Techniques 26 accumulator (revisited) (define (make-accum value) (lambda (x) (set! value (+ value x)) value)) N.B. body of lambda, define, cond clause is implicitly a begin block.

27 Fall 2008Programming Development Techniques 27 accumulator (define (make-accum value) (lambda (x) (set! value (+ value x)) value)) current env

28 Fall 2008Programming Development Techniques 28 accumulator (another look) (define make-accum (lambda (value) (lambda (x) (set! value (+ value x)) value)) current env

29 Fall 2008Programming Development Techniques 29 accumulator (define a! (make-accum 0)) –evaluate (make-accum 0) create frame bind arguments current env

30 Fall 2008Programming Development Techniques 30 accumulator evaluate (make-accum 0) –create, bind –evaluate body (lambda (x) …) current env

31 Fall 2008Programming Development Techniques 31 rule 3: when we create a procedure (evaluate a lambda expression) –its environment is the environment in which the lambda expression is evaluated

32 Fall 2008Programming Development Techniques 32 accumulator evaluate (make-accum 0) –create, bind –evaluate body (lambda (x) …) –result is lambda with env binding current env

33 Fall 2008Programming Development Techniques 33 accumulator (define a! (make-accum 0)) –evaluate (make-accum 0) result is (lambda (x) …) with env ptr –make binding for a! in its environment current env

34 Fall 2008Programming Development Techniques 34 after (define a!…) current env

35 Fall 2008Programming Development Techniques 35 after (define a!…) "trapped" frame persists current env

36 Fall 2008Programming Development Techniques 36 using a! (a! 1) –create frame –bind values current env

37 Fall 2008Programming Development Techniques 37 using a! (a! 1) –create frame –bind values –evaluate body (begin (set! value…) …) –(set! value (+ x value)) –(set! value (+ 1 0)) –(set! value 1) current env

38 Fall 2008Programming Development Techniques 38 using a! (a! 1) –Create frame –Bind values –Evaluate body (begin (set! value…),,,,) (set! value (+ x value)) (set! value (+ 1 0)) (set! value 1) current env

39 Fall 2008Programming Development Techniques 39 environment after (a! 1) trapped frame current env

40 Fall 2008Programming Development Techniques 40 more usage (a! 1)  2 (a! 1)  3

41 Fall 2008Programming Development Techniques 41 accumulator 2 (define b! (make-accum 0)) –evaluate (make-accum 0) create bind… current env

42 Fall 2008Programming Development Techniques 42 accumulator 2 (define b! (make-accum 0)) –evaluate (make-accum 0) result is (lambda (x) …) w/ env ptr –make binding for b! current env

43 Fall 2008Programming Development Techniques 43 resulting environment a! and b! have their own value current env

44 Fall 2008Programming Development Techniques 44 using b! (b! 1) –…frame current env

45 Fall 2008Programming Development Techniques 45 using b! (b! 1) –…frame –…set! current env

46 Fall 2008Programming Development Techniques 46 using a! again (a! 1) –…frame current env

47 Fall 2008Programming Development Techniques 47 using a! again (a! 1) –…frame –…set! current env

48 Fall 2008Programming Development Techniques 48 using b! (b! 1)  2 (a! 1)  3 (a! 1)  4 (b! 1)  3

49 Fall 2008Programming Development Techniques 49 procedure environment make-accum creates a unique frame for each call the lambda traps the frame and keeps a pointer to it only that lambda has access to that frame

50 Fall 2008Programming Development Techniques 50 procedure environment value is a local variable that is owned (and accessible) exclusively by the resulting lambda

51 Fall 2008Programming Development Techniques 51 a! and b! a! and b! have unique procedure frames have unique local variables: value don’t affect each other remember own state important form of encapsulation –limits unwanted interactions

52 Fall 2008Programming Development Techniques 52 contrast (define astate 0) (define (accum! x) (set! astate (+ x astate)) astate)) (define (toggle!) (if (= astate 0) (set! astate 1) (set! astate 0)))

53 Fall 2008Programming Development Techniques 53 interaction (accum! 1)  1 (toggle!) ;; astate: 0 (accum! 1)  1 (accum! 1)  2 (toggle!) ;; astate: 0 (accum! 1)  1 toggle! and accum! share a variable not independent

54 Fall 2008Programming Development Techniques 54 encapsulation generally a bad idea to put state in the global environment –pollutes namespace –gives other routines access even ones that don't need it –can get bad interactions even accidental ones

55 Fall 2008Programming Development Techniques 55 disciplined use of side-effects moral of the story: there are many things we can do with side-effects …that we really should not do. keep your state private! –nobody else has to know

56 Fall 2008Programming Development Techniques 56 controlling access What if I want multiple procedures to access the same state? want specificity –give multiple procedures access –but not all procedures

57 Fall 2008Programming Development Techniques 57 code and data encapsulate data –with associated (limited) code to manipulate –code mediates kinds of access familiar… –message-passing associated data and code

58 Fall 2008Programming Development Techniques 58 message-passing ; returns a procedure that will act as an accumulator ; or will return its value or reset the value (define (mp-make-accum value) (lambda (op) (cond ((eq? op 'accum) (lambda (x) (set! value (+ value x)))) ((eq? op 'value) value) ((eq? op 'reset) (set! value 0)) (else (error “unknown op: ” op)))))

59 Fall 2008Programming Development Techniques 59 message-passing use (define a2 (mp-make-accum 0)) (a2 'value)  0 ((a2 'accum) 1) (a2 'value)  1

60 Fall 2008Programming Development Techniques 60 environment (define a2 (mp-make-accum 0)) current env

61 Fall 2008Programming Development Techniques 61 environment (define a2 (mp-make-accum 0)) (a2 ‘accum) current env

62 Fall 2008Programming Development Techniques 62 environment (define a2 (mp-make-accum 0)) (a2 ‘value) current env

63 Fall 2008Programming Development Techniques 63 usage (define a2 (mp-make-accum 0)) during: ((a2 ‘accum) 1) current env

64 Fall 2008Programming Development Techniques 64 usage (define a2 (mp-make-accum 0)) during: ((a2 ‘accum) 1) current env

65 Fall 2008Programming Development Techniques 65 usage (define a2 (mp-make-accum 0)) during: ((a2 ‘accum) 1) current env (set! value (+ value x)) (set! value (+ 0 1)) (set! value 1)

66 Fall 2008Programming Development Techniques 66 usage after: ((a2 ‘accum) 1) 1 current env

67 Fall 2008Programming Development Techniques 67 Big Ideas procedures point to environment at the place where they're evaluated procedures can have local state allows us to create procedure-specific (object-specific) state allows us to encapsulate data –avoiding conflict –control usage


Download ppt "Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the."

Similar presentations


Ads by Google