topic 17
play

Topic 17 You have just been introduced to an assignment operator in - PDF document

Substitution Model and Why it Doesnt work Topic 17 You have just been introduced to an assignment operator in Assignment, Local State, and the scheme this is really the first time where symbols are viewed as variables whose values


  1. Substitution Model and Why it Doesn’t work Topic 17 • You have just been introduced to an assignment operator in Assignment, Local State, and the scheme – this is really the first time where symbols are viewed as variables whose values can be set (rather than as values Environment Model of themselves) • Introducing assignment breaks us away from the functional Evaluation model of programming Today 1. We will explicitly see WHY the substitution model of evaluation no longer works and Section 3.1 & 3.2 2. We will be introduced to the environment model of evaluation that can better explain the behavior of these new programs Fall 2008 Programming Development 1 Fall 2008 Programming Development 2 Techniques Techniques Functional Programming Functional • (Almost) everything we’ve seen to now has been • a function, always returns the same value for the “functional” same inputs: � f(x) = 2x+3 � f(3) = 9 … always • Functional in the sense that it is based on a � f(3) = 9 mathematical model of functions • I hope this seems obvious… • Each of our procedures take input and return a value Fall 2008 Programming Development 3 Fall 2008 Programming Development 4 Techniques Techniques Functional values never change (fib 6) • once we assign a value � 8 – it is always the same – it never changes (fact 6) • x = 6 � 720 – then x always equals 6 in this context (fib 6) � 8 (fact 6) � 720 Fall 2008 Programming Development 5 Fall 2008 Programming Development 6 Techniques Techniques 1

  2. but we do have different contexts no change • f(x) = x * f(x-1) ;; x > 1 • and a call to a function never changes anything else • i.e. different calls may have different bindings for x • (f 6) (g 7) (f 8) • but within a call (a single function) • (f 6) (f 8) ;; return the same thing – the value of any variable never changes ;; regardless of call to g • (+ (f 6) (g 7) (f 8)) • (+ (f 6) (f 8) (g 7)) ;; same value Fall 2008 Programming Development 7 Fall 2008 Programming Development 8 Techniques Techniques Functional Model change • is a beautiful model of computation • introduce the ability to change values • completely capable – a variable’s value may change over time – can solve any computable problem with it • easy to reason about • once we start using this – the substitution view won’t be correct • …but it does make programming some things awkward. Fall 2008 Programming Development 9 Fall 2008 Programming Development 10 Techniques Techniques in other languages... set! • changing values of variables happens all the time • By introducing set! we just produced the ability to • e.g. in C: change values in scheme int y = 10; y = 20; y = y + 30; • set! is another special form – evaluate its 2 nd argument (value) • in those languages, change is second nature – reassign the 1 st argument (variable) to the second • change the binding • also known as mutation • variable "mutates" to new value Fall 2008 Programming Development 11 Fall 2008 Programming Development 12 Techniques Techniques 2

  3. consider: value changes over time • ( define astate 0) • (define (accum0! x) • (accum0! 1) • ( define (accum0! x) (set! astate • astate ( set! astate (+ astate x))) • � 1 ( + astate x))) • astate does not have a unique value here • (accum0! 1) – initially has one value • astate – has a different value after assignment • � 2 Fall 2008 Programming Development 13 Fall 2008 Programming Development 14 Techniques Techniques accumulator (revised) accumulator (revised) • ( define astate 0) • ( define astate 0) • ( define (accum! x) • ( define (accum! x) ( begin ( begin ( set! astate (+ astate x)) astate)) ( set! astate (+ astate x)) – that is: astate)) • (begin (set! astate (+ astate x)) astate) • Now, the set! expression changes the value of the • is not the same as merely: astate final expression Fall 2008 Programming Development 15 Fall 2008 Programming Development 16 Techniques Techniques using accum! history starts to matter • (define astate 0) • (accum! 1) • (begin not same as: � 1 (accum! 1) • (define astate 0) • (accum! 1) (accum! 1) • (accum! 1) (accum! 1) � 1 � 2 • ) • (accum! 1) � 3 • • intervening accum!’s change � 3 the value of astate • changes the value of the final (accum! 1) Fall 2008 Programming Development 17 Fall 2008 Programming Development 18 Techniques Techniques 3

  4. side-effects notational conventions • operations with embedded set! • ( foo …) is a functional function – may have effects other than to compute their value ;; no side effects – may change state • ( foo? …) is a predicate • that affects the way other things behave ;; returns a boolean value – we say they have “side effects” • ( foo! …) has side effects • have an effect beyond their local computation ;; has an internal set! or equivalent Fall 2008 Programming Development 19 Fall 2008 Programming Development 20 Techniques Techniques so far… evaluating with set! • before introducing set! • (define (sadd x y z) – variable values did not change (begin – intervening functions never changed the value of (set! x (+ x y)) succeeding operations (set! x (+ x z)) x)) • introduce set! – variable values may change • intuitively: what does this do? – results of operations may depend on previous operations Fall 2008 Programming Development 21 Fall 2008 Programming Development 22 Techniques Techniques Substitution Model Huh? evaluating with set! • (begin • evaluate: • (define (sadd x y z) (set! 1 (+ 1 2)) (sadd 1 2 3) (begin (set! 1 (+ 1 3)) • apply sadd to 1 2 3 (set! x (+ x y)) 1) (set! x (+ x z)) • substitute x)) • does this make any sense? • (begin (set! 1 (+ 1 2)) (set! 1 (+ 1 3)) � set!: not an identifier in: 1 1) Fall 2008 Programming Development 23 Fall 2008 Programming Development 24 Techniques Techniques 4

  5. problem problem • substitute • substitute • (define (sadd x y z) • (define (sadd x y z) (begin (begin • (begin • (begin (set! x (+ x y)) (set! x (+ x y)) (set! 1 (+ 1 2)) (set! 1 (+ 1 2)) (set! x (+ x z)) (set! x (+ x z)) (set! 1 (+ 1 3)) (set! 1 (+ 1 3)) x)) x)) 1) 1) our substitution model does not admit the our substitution model does not distinguish possibility that a variable’s value might change between a value and a variable Fall 2008 Programming Development 25 Fall 2008 Programming Development 26 Techniques Techniques the bottom line the environment model • new model • the substitution model – need to reason about variables as locations – breaks down in the presence of side-effects – cannot handle change of variable's value • we need a better model... • recall that we said define created an "association" – a mapping between a variable and a value • now need to bring that to the forefront of our model Fall 2008 Programming Development 27 Fall 2008 Programming Development 28 Techniques Techniques frames environment • we call the association table a frame • An environment – is a collection of linked • a frame contains bindings frames – mapping from a variable name – to a value • e.g. – x is currently 1 – y is currently 2 Fall 2008 Programming Development 29 Fall 2008 Programming Development 30 Techniques Techniques 5

  6. enclosing environment variables and their values • frames • the value of a variable – include a pointer to their – is the value enclosing environment associated with the – except for a special frame variable in the called the global environment lowest enclosing frame – relative to the current frame x ? x ? 10 100 x ? Fall 2008 Programming Development 31 Fall 2008 Programming Development 32 1 Techniques Techniques variables and their values environments • the value of a variable • are trees made out of connected frames – if variable binding not found in current • from the POV of any 3 frame given frame, you "see" the environment as a – search the parent list of frames frame 2 z ? q ? 12 3 q ? 1 Fall 2008 Programming Development 33 Fall 2008 Programming Development 34 12 Techniques Techniques Day 1 Substitution Model environment model evaluation to evaluate a Scheme expression: • changes the way we model apply 1. evaluate its operands • to apply a procedure 2. evaluate the operator 3. apply the operator to the evaluated operands 1. construct a new frame 2. bind the formal parameters to the arguments of the call in that new frame (fun op1 op2 op3 …) 3. the new frame’s parent is the environment associated with the Substitution Model called procedure • not the calling procedure 4. evaluate the body in the new environment Fall 2008 Programming Development 35 Fall 2008 Programming Development 36 Techniques Techniques 6

Recommend


More recommend