CS1101S Lecture 14: Environment Model & Mutable Data 10 October 2012.

Slides:



Advertisements
Similar presentations
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Fundamentals of Python: From First Programs Through Data Structures
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages
Fall 2008Programming Development Techniques 1 Topic 19 Mutable Data Objects Section 3.3.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
SICP Data Mutation Primitive and Compound Data Mutators Stack Example non-mutating mutating Queue Example non-mutating mutating.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
1 Lecture 15: More about assignment and the Environment Model (EM)
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
Built-in Data Structures in Python An Introduction.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Variables, Environments and Closures. Overview Touch on the notions of variable extent and scope Introduce the notions of lexical scope and dynamic.
CS1101S Lecture 15: Memoization 12 Oct Using Tables Problem: determine word frequency in a text message. e.g. given ’(mary had a little lamb, little.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Lecture 14: Assignment and the Environment Model (EM)
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
Fall 2008Programming Development Techniques 1 Topic 17 Assignment, Local State, and the Environment Model of Evaluation Section 3.1 & 3.2.
1/32 Data Mutation Primitive and compound data mutators set! for names set-car!, set-cdr! for pairs Stack example non-mutating mutating Queue example non-mutating.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
String and Lists Dr. José M. Reyes Álamo.
Operational Semantics of Scheme
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Edited by Original material by Eric Grimson
6.001 SICP Variations on a Scheme
The interpreter.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Env. Model Implementation
Class 19: Think Globally, Mutate Locally CS150: Computer Science
The Metacircular Evaluator
Lecture 15: Tables and OOP
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
6.001 SICP Environment model
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
Data Mutation Primitive and compound data mutators set! for names
6.001 SICP Environment model
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
topics mutable data structures
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
6.001 SICP Variations on a Scheme
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
6.001 SICP Interpretation Parts of an interpreter
6.001 SICP Environment model
Lecture 13: Assignment and the Environment Model (EM)
COMPILERS Semantic Analysis
topics interpreters meta-linguistic abstraction eval and apply
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

CS1101S Lecture 14: Environment Model & Mutable Data 10 October 2012

Midterm survey closes today

Winner announcement of ‘Force Crystals’ and ‘Alien Pancakes’: Lecture 16, 17/10

JavaScript Buddy Scheme

Re-Midterm 16 Oct 2012 (Tues) 7:30 pm, venue: TBC

Recap: Last Lecture Introduced assignment: v = E; By introducing state and assignment, the Revised Substitution Model (RSM) is broken Today, we need to learn how to cope with this new environment

Assignment statement = ; is a symbol, not evaluated. is evaluated, then its value is assigned to the variable in the environment. That is, from now on, will evaluate to Compare with expression statement === ;

Today’s Agenda Environment Model Mutable Data –Mutating lists –Queues –Tables

Environment Model RSM doesn’t work anymore! An Environment –determines the context in which an expression should be evaluated –Every expression HAS an environment in which is evaluated. If none is created by the user, then the expression is evaluated in the Global Environment.

Environment Model The Global Environment consists of a single frame with bindings of function variables to their implementation. alert: prompt: pair: list: impl global environment

Environment Manipulation Every function stores the bindings of its global variables in its environment Every function call extends this environment by a binding –of the parameter variables to the actual arguments, and –of the local variables to undefined

Environment Extension So far, we have not spent much time worrying about how environments are extended In the presence of assignment, we need to be clear about this

Environment Model An environment is a sequence of frames. Each frame contains bindings of symbols and values. A frame points to its enclosing environment, the next one in the sequence. Extending an environment means adding a new frame inside the old one.

Accessing Environments To evaluate a symbol, look up its value in the current frame. If not found in current frame, then look in enclosing environment, and so on.

Example A, B, C, D are environments (C same as D) Value of x in environment A is: Value of x in environment B is: x in Frame II shadows x in Frame I Value of w in environment A? Frame I x:3 y:5 Frame II z:6 x:7 Frame III m:1 y:2 A B C D 7 3 unbound

Evaluation Rules To evaluate an application: fun(arg1,arg2,…,argn) –Evaluate the subexpressions of the application in the current environment. –Apply the value of the function subexpression to the values of the argument subexpressions.

Evaluation Rules Var declarations: var = ; –Applying enclosing function def extends function environment by binding of Variable occurrence searches environment from current frame Assignment statement: = ; –searches environment for from current frame, changes its binding to value of

Example Access x in A leads to 3 Evaluate x = 13; in B Frame I x:3 y:5 Frame II z:6 w:7 v:8 Frame III m:1 y:2 A B C D 13

More rules Function expression: function(,,..){ } –Creates a function object, represented as two circles. –One points to the code (text). –The other points to the environment in which the function expression was evaluated; called the closing environment. –A function therefore remembers the environment in which it was created.

Function application Create a new frame that points to the closing environment of the function. In this new frame, bind the formal parameters to the actual arguments. Bind variables declared with var to undefined Then evaluate the body of the function in this new frame.

JavaScript toplevel Variable declaration outside any function: var = ; –Evaluate in global environment –If is not defined in global environment, define it, and bind it to undefined –Change binding of to eval result Function definition outside any function: function (args) { } similar to var = function(args){ };

Example After evaluating function square(x) { return x * x; } in the global environment. other variables… square: global env parameters: x body: return x * x;

Example Now, evaluate square(5) in the global env. Create new frame that points to square’s closing environment. In this frame, bind x to 5, evaluate x*x; other bindings… square: global env parameters: x body: return x * x; x: 5 E1 x * x

Example 2 (from last lecture) function make_withdraw(balance) { return function(amount) { if (balance >= amount) { balance = balance – amount; return balance; } else { return “error”; } } } var W1 = make_withdraw(100); W1(50)  50 W1(60)  “error” W1(40)  10

Example 2 (from last lecture) After defining procedure make_withdraw other bindings… make_withdraw: global env parameters: balance... body: return function...

Example 2 Let’s evaluate W1 = make_withdraw(100); make_withdraw: global env parameters: balance body: return function … balance: 100 Evaluate this: return function(amount){ if (balance >= amount) … }

Example 2 Let’s evaluate W1 = make_withdraw(100); make_withdraw: global env parameters: balance body: return fucnction… parameters: amount if (balance >= amount) … body: if (balance >= amount) … balance: 100 W1:

Example 2 Evaluating W1(50) make_withdraw:... W1: global env parameters: amount if …) body: if …) balance: 100 amount: 50 if (balance >= amount) { balance = balance – amount; balance = balance – amount; return balance; return balance; } else { return “error”; } return “error”; } assignment is about to change balance here. 50

Example 2 Environment after evaluating W1(50) make_withdraw:... W1: global env parameters: amount if … body: if … balance: 50

Example 2 Environment after evaluating var W2 = make_withdraw(100) W2 has its own state variable. make-withdraw:... W1: W2: global env parameters: amount if … body: if … balance: 50 balance: 100 parameters: amount if … body: if …

Mutable Data Assignment gives us the ability to create mutable data, i.e. data that can be modified. –e.g. bank account In SICP Chap. 2, all our data were immutable. We had –Constructors, selectors, predicates, printers –But no mutators.

Mutable Data After creating a pair with pair, the head and tail can be changed using set_head and set_tail

Mutable Data set_head(p, x) changes head pointer of p to point to x. set_tail(p, y) changes tail pointer of p to point to y.

Initially, x, y are as shown. Effect of: set_head(x, y) Example d c b a x f e y d c b a x f e y

Effect of: set_tail(x, y) Initially, x, y are as shown. d c b a x f e y d c b a x f e y

Warning! Be careful when using mutators! var a = list(1, 2, 3); var b = list(6, 5); var c = append(a, b); c  [1, [2, [3, [6, [5, []]]]]] set_head(b, 100); b  [100, [5, []] c  [1, [2, [3, [100, [5, []]]]]] Mutating b changes c as well!

Mutation and Sharing 132 a 65 b X 100 Initially, 132 c Structure of c Mutating b

Another Example var a = list(1, 2, 3); set_head(tail(tail(a)), a); 132 a Cyclical structure! Question What is length(a)?

Data Structure: Queue A Queue is a sequence of items with the first-in-first-out (FIFO) property. –Items are removed in the order they were inserted. Items are inserted at the rear of the queue, and removed from the front (or head) of the queue.

Queue: Specs Constructor – make_queue : function Arguments: none Returns: a new, empty queue Predicate – is_empty_queue : function Arguments: –Q: a queue Returns: true if Q is empty, false otherwise

Queue: Specs Selector – Front_queue : function Arguments: –Q: a queue Returns: the item at the front of Q. Q is unchanged. Signals an error if Q is empty.

Queue: Specs Mutator – Insert_queue : function Arguments: –Q: a queue –x: an item to be inserted. Effects: Q is mutated so that x is added to the rear of Q. Returns: the modified Q

Queue: Specs Mutator – delete_queue : function Arguments: –Q: a queue Effects: Q is mutated so that the item at the front is removed. An error is signaled if Q is empty before the deletion. Returns: the modified Q.

Queue: implementation The items are stored in a list. But a pair is used to keep track of the front and rear of the queue. –This is for efficiency reasons. b a c front-ptr rear-ptr

Queue: implementation Adding an item. b a c front-ptr rear-ptr d

Queue: implementation Removing an item. bc a front-ptr d rear-ptr

Queue: implementation First, define helper functions: front_ptr = head; rear_ptr = tail; set_front_ptr = set_head; set_rear_ptr = set_tail;

Queue: implementation function is_empty_queue(q) { return is_empty_list(front_ptr(q));} function make_queue() { return pair([], []); } function front_queue(q) { if (is_empty_queue(q)) { “error” } else { return head(front_ptr(q)); } }

Queue: implementation function delete_queue(q) { if (is_empty_queue(q)) { “error” } else { set_front_ptr(q, tail(front_ptr(q))); return q; }

Queue: implementation function insert_queue(q, item) { var new_pair = pair(item,[]); if (is_empty_queue(q)) { set_front_ptr(q, new_pair); set_rear_ptr(q, new_pair); } else { set_tail(rear_ptr(q), new_pair); set_rear_ptr(q, new_pair); } return q; }

Useful mutator Remove front item, and return it. function dequeue(q) { var result = front_queue(q); delete_queue(q); return result; }

Using a Queue Printing documents Lee’s document Mary’s document Lee’s Tom’s paper Joe’s picture Print Queue

Data Structure: Table A table is a sequence of key-value pairs. The key is used to index the table, and the value is associated with the key. A key-value pair is also called an entry.

Table: Specs Constructor – make_table : function Arguments: none Returns: a new, empty table Predicate – is_empty_table : function Arguments: –T, a table Returns: true if T is empty, false otherwise

Table: Specs Selector – lookup (get) : function Arguments: –K: a key to search for an entry –T: the table to be searched Returns: the key-value pair in T whose key matches K. If no entry found, error

Table: Specs Mutator – insert (put) : function Arguments: –K: a key –V: a value to be associated with K –T: the table to be inserted Effects: if an existing entry in T matches K, then the entry’s value is changed to V. Otherwise, a new entry K-V is added to T. T is modified. Returns: undefined

Table: implementation Key-value entries are stored as pairs. Entries are stored in a list. List has a head, to allow first entry to be changed. bac *table* 213

Table: implementation function make_table() { return list(‘table’); } function empty_table(table) { return equal(table, list(‘table’)); } function lookup(key, table) { return tail(assoc(key, tail(table)); } function assoc(key, records) { if (is_empty_list(records)) { “error”; } else if (equal(key, head(head(records)))) { return head(records); } }

Table: implementation function check_assoc(key, records) { if (is_empty_list(records)) { return false; } else if (equal(key, head(head(records)))) { return true; } } else { return check_assoc(key, tail(records)); } } function insert(key, value, table) { if (check_assoc(key, tail(table))) { var record = assoc(key, tail(table)); set_tail(record, value); } else { set_tail(table, pair(pair(key,value), tail(table))); } }

Table: implementation function delete(key,table){ Homework! }

2D tables Two-dimensional tables are possible: –Two keys needed –Entry: key1-key2-value Implementation: –Nested 1D tables (subtables) –key1 used to locate a 1D subtable –key2 used to index within this subtable put, and get are simply lookup and insert on a 2D table

2D tables ba letters * math *table*

Using Tables Problem: determine word frequency in a text message. e.g. given list(‘mary’, ‘had’, ‘a’, ‘little’, ‘lamb’, ‘little’, ‘lamb’, ‘little’, ‘lamb’, ‘mary’, ‘had’, ‘a’, ‘little’, ‘lamb’, ‘whose’, ‘fleece’, ‘was’, ‘white’, ‘as’, ‘snow’) Determine how many times each word occurs. mary: 2, little: 4, lamb: 4,...

Solution Maintain a table –Word is the key –Count is the value cdr down the message list –For each word w If w exists in table, then increment its value Otherwise, add entry (w,1) to the table –Ignore punctuations

Queues using tables empty-Q = empty-table To insert item M, insert entry (N, M) into table. –N is a running number that we automatically generate. This serves as a unique key in the table. when table is first created, N=0 –After inserting, we increment N. –N also indicates the order in which the items were inserted.

Queues using tables To delete an item from the front of the queue, –Lookup key S in the table –S is the smallest key –Delete that entry –Increment S To think about: –How to implement front_queue? –What is the time complexity of these functions?

Questions of the Day Can you implement tables using queues? –Hint: consider using 2 queues. –Think of how to represent an empty table. –How to implement table operations (e.g. lookup, insert!) using queue operations? Can you implement table more efficiently?

Summary Environment Model replaces RSM. –Frames are local repositories of state To evaluate a function in the Environment Model: 1.Create a NEW frame in the closing environment (second pointer of function value) 2.Bind variables in new frame 3.Evaluate body of the function (first pointer of function value) in the new frame

Summary Assignment allows us to create mutable data structures. set_head, set_tail mutate (mutable) lists. Be careful when mutating things! Queues and tables are useful data structures Data structures can be used to implement other data structures!