one slide summary programming programming
play

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

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


  1. One-Slide Summary Programming Programming • The substitution model for evaluating Python does not allow us to reason about mutation. In the with with environment model : State State • A name is a place for storing a value. Definitions, lists, and function application create places. = 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 has no parent. Golden Golden • To evaluate a name, walk up the frames until you Ages Ages find a definition. • A golden age is a period when knowledge or quality increases rapidly. #1 #2 Outline Reading Quiz • Names and Places • Write your UVA ID on a piece of paper. • Assignment and friends • In the Neil deGrass Tyson essay Science's • Environment Model Endless Golden Age (assigned reading before • Golden Ages today's class), the author focuses primarily on one law. Name it. (Note that multiple laws are mentioned, but one is at the heart of the matter.) #3 #4 From Lecture 3: Evaluation Rule 2: Names Names and Places If the expression is a name , it • A name is not just a value, it is a evaluates to the value associated with place for storing a value. that name. • define creates a new place, associates a name with that place, >>> myvar = 2 and stores a value in that place >>> myvar 2 >>> x = 3 x: 3 This is called the substitution model . You can reason about Python expressions by substituting the definition in whenever it is used. #5 #6

  2. Assignment = should make you nervous = (“assignment”) changes the value >>> x = 2 associated with a place >>> def nextx(): ... >>> nextx() >>> x = 3 Before = all procedures 3 >>> x were pure functions (except >>> nextx() 3 for some with side-effects). 4 >>> x = 7 The value of f() was the >>> x >>> x same every time you 4 7 evaluated it. Now it might be different! x: 7 3 #7 #8 Defining nextx Evaluation Rules def nextx(): >>> x = 3 global x >>> nextx() + x x = x + 1 7 return x # you can also assign over or 8 # the elements of lists! >>> y = [1,2,3] >>> x + nextx() >>> y[1] = “hello” 9 >>> y [1, 'hello', 3] or 10 #9 #10 Evaluation Rules Assigning to Lists >>> x = 3 >>> lst[0] = v >>> nextx() + x Replaces the zero-th element of the list lst with v . 7 Python evaluates or 8 subexpressions left to right, but evaluation >>> lst[1:] = v >>> x + nextx() rules can allow any order. Replaces the rest of the list lst with v . 9 Do not write a These should scare you even more than before! or 10 program that depends on this ordering. #11 #12

  3. >>> pair = [1,2] >>> pair = [1,2] >>> pair >>> pair [1, 2] [1, 2] pair: pair: >>> pair[0] = 'a' >>> pair[0] 'a' 1 'a' 2 2 >>> pair[1:] [2] #13 #14 >>> pair = [1,2] Functional vs. Imperative >>> pair [1, 2] Functional Solution: A procedure that pair: >>> pair[0] = 'a' takes a procedure of one argument and a >>> pair[0] list, and returns a list of the results 'a' 'a' 8 6 'b' produced by applying the procedure to >>> pair[1:] each element in the list. [2] def map (proc, lst): >>> pair[1:] = [8,6,'b'] >>> pair if not lst: return [] ['a', 8, 6, 'b'] return [proc(lst[0])] + map(proc,lst[1:]) #15 #16 Programming with Mutation Imperative def map (proc, lst): if not lst: return [] return [proc(lst[0])] + \ >>> mapi(square, range(4)) Solution map(proc,lst[1:]) I m >>> i4 = range(4) p Imperative Solution: A procedure that takes a >>> mapi(square, i4) e r procedure and list as arguments, and replaces a >>> i4 t i each element in the list with the value of the v (0 1 4 9) e procedure applied to that element. >>> i4 = range(4) def mapi (proc, lst): F >>> map(square, i4) for i in range(len(lst)): u n (0 1 4 9) lst[i] = proc( lst[i] ) c t >>> i4 i o n (0 1 2 3) a l #17 #18

  4. Mutation Changes Everything! Why Substitution Fails >>> x = 0 • We can no longer talk about the “value of >>> def nextx (): an expression” global x x = x + 1 – The value of a give expression can change! return x – We need to talk about “the value of an >>> (lambda x,y : x+y) ( nextx() ) expression in an execution environment ” 2 • “execution environment” = “context so far” Substitution model for evaluation would predict: • The order in which expressions are (nextx()) + (nextx()) evaluated now matters (x=x+1 ; x) + (x=x+1; x) (x=0+1 ; x) + (x=x+1; x) (x=1; 1) + (x=1+1; x) 1 + 2 # wrong! #19 #20 Liberal Arts Trivia: Astrophysics Liberal Arts Trivia: Rhetoric • According to this 1915 theory (be specific), • This type of “values” debate traditionally the observed gravitational attraction between places a heavy emphasis on logic, ethical masses results from the warping of space and values and philosophy. It is a one-on-one time by those masses. This theory helps to debate practiced in National Forensic explain observed phenomena, such as League competitions. The format was anomalies in the orbit of Mercury, that are named for the series of seven debates in not predicted by Newton's Laws, and can deal 1858 for the Illinois seat in the United with accelerated reference frames. It is part State Senate. of the framework of the standard Big Bang model of Cosmology. #21 #22 Names and Places Very Scary! • A name is a place for storing a value. • The old • The first = creates a new place substitution • [1,2] creates two new places, the [0] and model does not the [1:] explain Python • name = expr changes the value in the programs that place name to the value of expr contain mutation. • list[0] = expr changes the value in the 0th place of list to the value of expr • We need a new environment • list[1:] = expr changes the value of the model . rest of the list to the value of expr #23 #24

  5. Lambda and Places Location, Location, Location • Places live in frames • (lambda x: …) also creates a new place • An environment is a frame and named x a pointer to a parent • The passed argument is put in that place environment • All environments except the >>> x = 3 x : 3 >>> (lambda x : x) (4) global environment have x : 4 4 exactly one parent >>> x How are these environment , global 3 places different? environment has no parent • Application creates a new environment #25 #26 Environments Environments len : <built-in function len> len : <built-in function len> global global environment environment + : <primitive:+> + : <primitive:+> x : 3 The global environment points to the outermost The global environment points to the outermost frame. It starts with all Python primitives. frame. It starts with all Python primitives. >>> x =3 #27 #28 Procedures Evaluation Rule 2: Names A name expression evaluates to the value associated with that name. + : <primitive:+> global To find the value associated with a name, look for the len : <built-in function len> environment name in the frame associated with the evaluation x : 3 environment. If it contains a place with that name, the value of the name expression is the value in that double: ??? 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 >>> x = 3 parent. Otherwise, the name expression evaluates to an error (the name is not defined). >>> double = lambda x: x+x >>> #29 #30

  6. How to Draw a Procedure How to Draw a Procedure (for artists only) • A procedure needs both code and an Environment environment pointer – We’ll see why soon • We draw procedures like this: Environment x (x + x) pointer Input parameters (in mouth) Procedure Body environment: parameters: x body: x + x #31 #32 Procedures Application • Old rule: (Substitution model) + : <primitive:+> global len : <built-in function len> environment Apply Rule 2: Constructed Procedures. x : 3 To apply a constructed procedure, double: evaluate the body of the procedure with each formal parameter replaced by the corresponding actual argument >>> double = \ environment: parameters: x expression value. lambda x: x+x body: x+x #33 #34 global 1. Construct a new environment New Application Rule 2: environment, parent is procedure’s environment + : <primitive:+> 1. Construct a new environment , whose pointer 2. Make places in that parent is the environment to which the x : 3 double: frame with the names of environment pointer of the applied each parameter, and procedure points. operand values environment: 3. Evaluate the body in the 2. Create places in that frame for each parameters: x new environment parameter containing the value of the body: (x + x) corresponding operand expression. 4 x : 3. Evaluate the body in the new >>> double(4) environment . Result is the value of the (+ x x) (x + x) 8 application. #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