one slide summary programming
play

One-Slide Summary Programming The substitution model for evaluating - PDF document

One-Slide Summary Programming The substitution model for evaluating Scheme does with not allow us to reason about mutation. In the environment model : State A name is a place for storing a value. define , cons and function application


  1. One-Slide Summary Programming • The substitution model for evaluating Scheme does with not allow us to reason about mutation. In the environment model : State • A name is a place for storing a value. define , cons and function application create places. set! changes & the value in a place. • Places live in frames . An environment is a frame and a pointer to a parent frame . The global environment Golden has no parent. • To evaluate a name, walk up the frames until you Ages find a definition. • A golden age is a period when knowledge or quality increases rapidly. #1 #2 From Lecture 3: Outline Evaluation Rule 2: Names • Names and Places If the expression is a name , it • set! and friends evaluates to the value associated with • Environment Model that name. • Golden Ages > (define two 2) • Interested in random weekly emails about > two available CS 150 tutoring ? Send email to the course staff (or me) to get on that list. 2 • There will not be normally scheduled lab hours or This is called the substitution model . You can office hours over spring break. reason about Scheme expressions by substituting • Your Exam 1 grade will be visible on the the definition in whenever it is used. Automatic Adjudication website. #3 #4 Names and Places Bang! set! (“set bang”) changes the value • A name is not just a value, it is a associated with a place place for storing a value. > (define x 3) x: • define creates a new place, 7 3 > x associates a name with that place, 3 and stores a value in that place > (set! x 7) > x (define x 3) x: 3 7 #5 #6

  2. set! should make you nervous Defining nextx (define (nextx) > (define x 2) (set! x (+ x 1)) > (nextx) Before set! all procedures 3 x) were pure functions (except (define nextx > (nextx) for some with side-effects). 4 (lambda () The value of (f) was the > x (begin syntactic sugar for same every time you 4 (set! x (+ x 1)) evaluated it. Now it might x)))) be different! #7 #8 Evaluation Rules Evaluation Rules > (define x 3) > (define x 3) > (+ (nextx) x) > (+ (nextx) x) 7 7 or 8 or 8 DrScheme evaluates > (+ x (nextx)) > (+ x (nextx)) application subexpressions 9 9 left to right, but Scheme evaluation rules allow any or 10 or 10 order. #9 #10 > (define pair (cons 1 2)) set-car! and set-cdr! > pair (1 . 2) pair: ( set-car! p v ) Replaces the car of the cons p with 1 2 v . ( set-cdr! p v ) Replaces the cdr of the cons p with v . These should scare you even more then set! ! #11 #12

  3. > (define pair (cons 1 2)) > (define pair (cons 1 2)) > pair > pair (1 . 2) (1 . 2) pair: pair: > (set-car! pair 0) > (set-car! pair 0) > (car pair) > (car pair) 0 0 1 1 0 0 2 2 2 1 > (cdr pair) > (cdr pair) 2 2 > (set-cdr! pair 1) > pair (0 . 1) #13 #14 Imperative (define ( map proc lst) Functional vs. Imperative (if (null? lst) null (cons (proc (car lst)) Solution (map proc (cdr lst))))) Functional Solution: A procedure that A procedure that takes a procedure and list as takes a procedure of one argument and a arguments, and replaces each element in the list list, and returns a list of the results with the value of the procedure applied to that produced by applying the procedure to element. each element in the list. (define ( map! f lst) (define ( map proc lst) (if (null? lst) (void) (if (null? lst) null (begin (cons (proc (car lst)) (set-car ! lst (f (car lst))) (map proc (cdr lst))))) (map ! f (cdr lst))))) #15 #16 Programming with Mutation Mutation Changes Everything! > (map ! square (intsto 4)) Imperative > (define i4 (intsto 4)) • We can no longer talk about the “value of > (map ! square i4) an expression” > i4 (1 4 9 16) – The value of a give expression can change! – We need to talk about “the value of an > (define i4 (intsto 4)) expression in an execution environment ” Functional > (map square i4) • “execution environment” = “context so far” (1 4 9 16) • The order in which expressions are > i4 evaluated now matters (1 2 3 4) #17 #18

  4. Why Substitution Fails Liberal Arts Trivia: Astrophysics > (define ( nextx ) (set! x (+ x 1)) x) • According to this 1915 theory (be specific), the > (define x 0) observed gravitational attraction between > ((lambda (x) (+ x x)) (nextx)) masses results from the warping of space and 2 time by those masses. This theory helps to explain observed phenomena, such as Substitution model for evaluation would predict: (+ (nextx) (nextx)) anomalies in the orbit of Mercury, that are not (+ (begin (set! x (+ x 1)) x) (begin (set! x (+ x 1)) x)) predicted by Newton's Laws, and can deal with (+ (begin (set! 0 (+ 0 1)) 0) (begin (set! 0 (+ 0 1)) 0)) accelerated reference frames. It is part of the (+ 0 0) framework of the standard Big Bang model of 0 Cosmology. #19 #20 Liberal Arts Trivia: Rhetoric Very Scary! • This type of “values” debate traditionally • The old substitution places a heavy emphasis on logic, ethical model does not values and philosophy. It is a one-on-one explain Scheme debate practiced in National Forensic programs that League competitions. The format was contain named for the series of seven debates in mutation. 1858 for the Illinois seat in the United • We need a new State Senate. environment model . #21 #22 Names and Places Lambda and Places • A name is a place for storing a value. • (lambda (x) …) also creates a new place • define creates a new place named x • cons creates two new places, the car and • The passed argument is put in that place the cdr • (set! name expr ) changes the value in the > (define x 3) x : 3 > ((lambda (x) x) 4) place name to the value of expr x : 4 4 • (set-car! pair expr ) changes the value in the > x How are these car place of pair to the value of expr 3 places different? #23 #24

  5. Location, Location, Location Environments • Places live in frames • An environment is a frame and a + : #<primitive:+> global pointer to a parent environment environment null? : #<primitive:null?> • All environments except the global environment have exactly one parent environment , global environment has The global environment points to the outermost no parent frame. It starts with all Scheme primitives. • Application creates a new environment #25 #26 Environments Evaluation Rule 2: Names A name expression evaluates to the value associated with that name. To find the value associated with a name, look for the + : #<primitive:+> global name in the frame associated with the evaluation environment null? : #<primitive:null?> environment. If it contains a place with that name, the x : 3 value of the name expression is the value in that place. If it doesn’t, the value of the name expression is the value of the name expression evaluated in the parent environment if the current environment has a parent. The global environment points to the outermost Otherwise, the name expression evaluates to an error frame. It starts with all Scheme primitives. (the name is not defined). > (define x 3) > #27 #28 Procedures How to Draw a Procedure • A procedure needs both code and an environment + : #<primitive:+> global null? : #<primitive:null?> environment – We’ll see why soon x : 3 • We draw procedures like this: double: ??? Environment pointer > (define x 3) environment: parameters: x > (define double (lambda (x) (+ x x))) body: (+ x x) > #29 #30

  6. How to Draw a Procedure Procedures (for artists only) Environment + : #<primitive:+> pointer global null? : #<primitive:null?> environment x : 3 double: x (+ x x) Input parameters (in mouth) Procedure Body > (define double environment: parameters: x (lambda (x) (+ x x))) body: (+ x x) #31 #32 Application New Application Rule 2: 1. Construct a new environment , whose • Old rule: (Substitution model) parent is the environment to which the environment pointer of the applied Apply Rule 2: Constructed Procedures. procedure points. To apply a constructed procedure, 2. Create places in that frame for each evaluate the body of the procedure with parameter containing the value of the each formal parameter replaced by the corresponding operand expression. corresponding actual argument expression 3. Evaluate the body in the new value. environment . Result is the value of the application. #33 #34 global global 1. Construct a new 1. Construct a new environment environment environment, parent is environment, parent is procedure’s environment procedure’s environment x : 999 + : #<primitive:+> + : #<primitive:+> pointer pointer 2. Make places in that 2. Make places in that x : 3 double: frame with the names of frame with the names of each parameter, and each parameter, and operand values operand values environment: 3. Evaluate the body in the 3. Evaluate the body in the parameters: x new environment new environment body: (+ x x) > (define x 999) 4 x : > (double 4) (+ x x) (+ x x) 8 #35 #36

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend