6.001 SICP Data Mutation Primitive and Compound Data Mutators

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

1 Lecture 16: Tables and OOP. 2 Tables -- get and put.
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Fall 2008Programming Development Techniques 1 Topic 19 Mutable Data Objects Section 3.3.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
SICP Symbolic data Symbol: a primitive type Generalization Symbolic differentiation.
Data Abstraction… The truth comes out…. What we’re doing today… Abstraction ADT: Dotted Pair ADT: List Box and Pointer List Recursion Deep List Recursion.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
SICP Data Mutation Primitive and Compound Data Mutators Stack Example non-mutating mutating Queue Example non-mutating mutating.
Mutation So far, our data abstractions consists of the following: –Constructors –Selectors/Accessors –Operations –Contract Once a data object is created,
( (lambda (z) (define x (lambda (x) (lambda (y z) (y x)))) ( ( (x (lambda () z)) (lambda (z) z) 3 ) ) ) 2)
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
1 Lecture 15: More about assignment and the Environment Model (EM)
1 Lecture OO, the notion of inheritance 3 Stacks in OO style (define (make-stack) (let ((top-ptr '())) (define (empty?) (null? top-ptr)) (define.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
6.001 SICP 1/ SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams.
1 Lecture 14: Assignment and the Environment Model (EM)
SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams Instance.
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.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
Abstraction A way of managing complexity for large programs A means of separating details of computation from use of computation Types of Abstraction Data.
Abstract Syntax cs7100 (Prasad) L7AST.
Edited by Original material by Eric Grimson
6.001 Jeopardy.
6.001 SICP Variations on a Scheme
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
6.001 SICP Object Oriented Programming
CS 1114: Implementing Search
The role of abstractions
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Lists in Lisp and Scheme
COP4020 Programming Languages
Env. Model Implementation
6.001 SICP Data abstractions
The Metacircular Evaluator
Lecture 15: Tables and OOP
FP Foundations, Scheme In Text: Chapter 14.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Lecture 16: Tables and OOP
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Explicit Application of Procedures, and Explicit Evaluation
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture #9 מבוא מורחב.
Abstract Syntax cs7100 (Prasad) L7AST.
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
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
topics mutable data structures
6.001 SICP Variations on a Scheme
Mutators for compound data Stack Queue
Lecture 14: The environment model (cont
6.001 SICP Data abstractions
Lecture #7 מבוא מורחב.
Defining Functions with DEFUN
Today’s topics Abstractions Procedural Data
6.001 SICP Interpretation Parts of an interpreter
Lecture 13: Assignment and the Environment Model (EM)
Common Lisp II.
More Scheme CS 331.
Presentation transcript:

6.001 SICP Data Mutation Primitive and Compound Data Mutators Stack Example non-mutating mutating Queue Example

Elements of a Data Abstraction A data abstraction consists of: constructors selectors mutators operations contract -- makes a new structure -- changes an existing structure

Primitive Data (define x 10) creates a new binding for name; special form x returns value bound to name To Mutate: (set! x "foo") changes the binding for name; special form

Assignment -- set! Substitution model -- functional programming: (define x 10) (+ x 5) ==> 15 - expression has same value ... each time it evaluated (in (+ x 5) ==> 15 same scope as binding) With assignment: (define x 10) (+ x 5) ==> 15 - expression "value" depends ... on when it is evaluated (set! x 94) ... (+ x 5) ==> 99

Compound Data constructor: (cons x y) creates a new pair p selectors: (car p) returns car part of pair (cdr p) returns cdr part of pair mutators: (set-car! p new-x) changes car pointer in pair (set-cdr! p new-y) changes cdr pointer in pair ; Pair,anytype -> undef -- side-effect only!

Example 1: Pair/List Mutation (define a (list 1 2)) (define b a) a  (1 2) b  (1 2) 1 2 b a 10 X (set-car! a 10) b ==> (10 2) 1 2 a Compare with: (define a (list 1 2)) (define b (list 1 2)) 10 X (set-car! a 10) 1 2 b b  (1 2)

Example 2: Pair/List Mutation (define x (list 'a 'b)) a b x X 2 1 How mutate to achieve the result at right? (set-car! (cdr x) (list 1 2)) Eval (cdr x) to get a pair object Change car pointer of that pair object

Sharing, Equivalence and Identity How can we tell if two things are equivalent? -- Well, what do you mean by "equivalent"? The same object: test with eq? (eq? a b) ==> #t Objects that "look" the same: test with equal? (equal? (list 1 2) (list 1 2)) ==> #t (eq? (list 1 2) (list 1 2)) ==> #f (1 2) (1 2) 1 2 1 2

Sharing, Equivalence and Identity How can we tell if two things are equivalent? -- Well, what do you mean by "equivalent"? The same object: test with eq? (eq? a b) ==> #t Objects that "look" the same: test with equal? (equal? (list 1 2) (list 1 2)) ==> #t (eq? (list 1 2) (list 1 2)) ==> #f If we change an object, is it the same object? -- Yes, if we retain the same pointer to the object How tell if parts of an object is shared with another? -- If we mutate one, see if the other also changes

Your Turn 3 4 x x ==> (3 4) y ==> (1 2) (set-car! x y) x ==> followed by (set-cdr! y (cdr x)) 1 2 y

Your Turn 3 4 x x ==> (3 4) y ==> (1 2) (set-car! x y) x ==> followed by (set-cdr! y (cdr x)) X ((1 4) 4) X 1 2 y ((1 2) 4)

End of part 1 Scheme provides built-in mutators set! to change a binding set-car! and set-cdr! to change a pair Mutation introduces substantial complexity Unexpected side effects Substitution model is no longer sufficient to explain behavior

Stack Data Abstraction constructor: (make-stack) returns an empty stack selectors: (top stack) returns current top element from a stack operations: (insert stack elt) returns a new stack with the element added to the top of the stack (delete stack) returns a new stack with the top element removed from the stack (empty-stack? stack) returns #t if no elements, #f otherwise

Stack Contract If s is a stack, created by (make-stack)and subsequent stack procedures, where i is the number of insertions and j is the number of deletions, then If j>i then it is an error If j=i then (empty-stack? s) is true, and (top s) and (delete s) are errors. If j<i then (empty-stack? s) is false and (top (delete (insert s val))) = (top s) If j<=i then (top (insert s val)) = val for any val

Stack Implementation Strategy implement a stack as a list a b d we will insert and delete items off the front of the stack

Stack Implementation (define (make-stack) nil) (define (empty-stack? stack) (null? stack)) (define (insert stack elt) (cons elt stack)) (define (delete stack) (if (empty-stack? stack) (error "stack underflow – delete") (cdr stack))) (define (top stack) (error "stack underflow – top") (car stack)))

Limitations in our Stack Stack does not have identity (define s (make-stack)) s ==> () (insert s 'a) ==> (a) (set! s (insert s 'b)) s ==> (b)

Alternative Stack Implementation – pg. 1 Attach a type tag – defensive programming Additional benefit: Provides an object whose identity remains even as the object mutates X (delete! s) a c d stack s Note: This is a change to the abstraction! User should know if the object mutates or not in order to use the abstraction correctly.

Alternative Stack Implementation – pg. 2 (define (make-stack) (cons 'stack nil)) (define (stack? stack) (and (pair? stack) (eq? 'stack (car stack)))) (define (empty-stack? stack) (if (not (stack? stack)) (error "object not a stack:" stack) (null? (cdr stack))))

Alternative Stack Implementation – pg. 3 (define (insert! stack elt) (cond ((not (stack? stack)) (error "object not a stack:" stack)) (else (set-cdr! stack (cons elt (cdr stack))) stack))) (define (delete! stack) (if (empty-stack? stack) (error "stack underflow – delete") (set-cdr! stack (cddr stack))) stack) (define (top stack) (error "stack underflow – top") (cadr stack)))

Queue Data Abstraction (Non-Mutating) constructor: (make-queue) returns an empty queue accessors: (front-queue q) returns the object at the front of the queue. If queue is empty signals error mutators: (insert-queue q elt) returns a new queue with elt at the rear of the queue (delete-queue q) returns a new queue with the item at the front of the queue removed operations: (empty-queue? q) tests if the queue is empty

Queue Contract If q is a queue, created by (make-queue) and subsequent queue procedures, where i is the number of insertions, j is the number of deletions, and xi is the ith item inserted into q , then If j>i then it is an error If j=i then (empty-queue? q) is true, and (front-queue q) and (delete-queue q) are errors. If j<i then (front-queue q) = xj+1

Simple Queue Implementation – pg. 1 Let the queue simply be a list of queue elements: c d b The front of the queue is the first element in the list To insert an element at the tail of the queue, we need to “copy” the existing queue onto the front of the new element: b c d new

Simple Queue Implementation – pg. 2 (define (make-queue) nil) (define (empty-queue? q) (null? q)) (define (front-queue q) (if (empty-queue? q) (error "front of empty queue:" q) (car q))) (define (delete-queue q) (error "delete of empty queue:" q) (cdr q))) (define (insert-queue q elt) (cons elt nil) (cons (car q) (insert-queue (cdr q) elt))))

Simple Queue - Orders of Growth How efficient is the simple queue implementation? For a queue of length n Time required -- number of cons, car, cdr calls? Space required -- number of new cons cells? front-queue, delete-queue: Time: T(n) = O(1) that is, constant in time Space: S(n) = O(1) that is, constant in space insert-queue: Time: T(n) = O(n) that is, linear in time Space: S(n) = O(n) that is, linear in space

Queue Data Abstraction (Mutating) constructor: (make-queue) returns an empty queue accessors: (front-queue q) returns the object at the front of the queue. If queue is empty signals error mutators: (insert-queue! q elt) inserts the elt at the rear of the queue and returns the modified queue (delete-queue! q) removes the elt at the front of the queue and returns the modified queue operations: (queue? q) tests if the object is a queue (empty-queue? q) tests if the queue is empty

Better Queue Implementation – pg. 1 We’ll attach a type tag as a defensive measure Maintain queue identity Build a structure to hold: a list of items in the queue a pointer to the front of the queue a pointer to the rear of the queue queue c d b a front-ptr rear-ptr

Queue Helper Procedures Hidden inside the abstraction (define (front-ptr q) (cadr q)) (define (rear-ptr q) (cddr q)) (define (set-front-ptr! q item) (set-car! (cdr q) item)) (define (set-rear-ptr! q item) (set-cdr! (cdr q) item)) queue c d b a front-ptr rear-ptr

Better Queue Implementation – pg. 2 (define (make-queue) (cons 'queue (cons nil nil))) (define (queue? q) (and (pair? q) (eq? 'queue (car q)))) (define (empty-queue? q) (if (not (queue? q)) ;defensive (error "object not a queue:" q) ;programming (null? (front-ptr q)))) (define (front-queue q) (if (empty-queue? q) (error "front of empty queue:" q) (car (front-ptr q))))

Queue Implementation – pg. 3 (define (insert-queue! q elt) (let ((new-pair (cons elt nil))) (cond ((empty-queue? q) (set-front-ptr! q new-pair) (set-rear-ptr! q new-pair) q) (else (set-cdr! (rear-ptr q) new-pair) q)))) queue c d b a front-ptr rear-ptr e

Queue Implementation – pg. 4 (define (delete-queue! q) (cond ((empty-queue? q) (error "delete of empty queue:" q)) (else (set-front-ptr! q (cdr (front-ptr q))) q))) queue c d b a front-ptr rear-ptr

Summary Built-in mutators which operate by side-effect set! (special form) set-car! ; Pair, anytype -> undef set-cdr! ; Pair, anytype -> undef Extend our notion of data abstraction to include mutators Mutation is a powerful idea enables new and efficient data structures can have surprising side effects breaks our "functional" programming (substitution) model