Lecture 15: Tables and OOP

Slides:



Advertisements
Similar presentations
1 Lecture 16: Tables and OOP. 2 Tables -- get and put.
Advertisements

Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
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.
מבוא מורחב 1 Lecture #13. מבוא מורחב 2 Multiple representations of data.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
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.
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.
Cs784(Prasad)L123Assg1 Assignments. cs784(Prasad)L123Assg2 l-value vs. r-value Pascal/Ada: x := x + 1 C/Java: x = x + 1 l-value = location, address, reference,
Trees! =) Write up-add tree which behaves in this manner: 1 31 / \ / \ 3 5  21 9 / | \ \ / | \ \
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Fall 2008Programming Development Techniques 1 Topic 14 Multiple Representations of Abstract Data – Data Directed Programming and Additivity Section
6.001 SICP 1/ SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 14: Object Oriented Programming 한 태숙.
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 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CS61A Lecture Colleen Lewis. Clicker poll How do you think the midterm compared to previous midterms online? A)Harder B)A little harder.
Object Oriented Programming Systems
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
Abstract Syntax cs7100 (Prasad) L7AST.
Intelligent Agents Lecture 3-2 October 14th, 1999 CS250 Lecture 2-1
Edited by Original material by Eric Grimson
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Winter 2013.
Tagged Data Tag: a symbol in a data structure that identifies its type
6.001 SICP Variations on a Scheme
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
6.001 SICP Object Oriented Programming
The role of abstractions
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2017.
CSC 533: Organization of Programming Languages Spring 2008
COP4020 Programming Languages
Env. Model Implementation
Original material by Eric Grimson
6.001 SICP Data abstractions
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Data abstraction, revisited
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
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
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
Lecture 11: Multiple representations of abstract data Message passing Overloading Section 2.4, pages ,2.5.2 pages מבוא.
Lecture #7 מבוא מורחב.
Today’s topics Abstractions Procedural Data
6.001 SICP Interpretation Parts of an interpreter
Lecture 13: Assignment and the Environment Model (EM)
CSC 533: Organization of Programming Languages Spring 2007
More Scheme CS 331.
Principles of Programming Languages
*Lecture based on notes from SICP
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
Presentation transcript:

Lecture 15: Tables and OOP

Last Lecture: Stack Data Abstraction Constructor: (make-stack) returns an empty stack Selector: (top stack) returns current top element from a stack Mutators and operations: (insert stack elem) 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 Data Abstraction Insert Insert First in, Last out. Insert Delete Insert

Queue Data Abstraction Constructor: (make-queue) returns an empty queue Selector: (front-queue q) returns the object at the front of the queue. If queue is empty signals error Mutators and operations: (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 (empty-queue? q) tests if the queue is empty

Queue illustration Insert Insert First in, First out Insert x1 x2 x3 front

Queue illustration Insert Insert First in, First out Insert Delete x2 front

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

Today: Tables -- get and put

One dimentional tables c d b a 1 2 3 4 (define (lookup key table) (let ((record (assoc key (cdr table)))) (if record (cdr record) false)))

One dimentional tables (define (lookup key table) (let ((record (assoc key (cdr table)))) (if record (cdr record) false))) (define (assoc key records) (cond ((null? records) false) ((equal? key (caar records)) (car records)) (else (assoc key (cdr records)))))

One dimentional tables (define (insert! key value table) (let ((record (assoc key (cdr table)))) (if record (set-cdr! record value) (set-cdr! table (cons (cons key value) (cdr table))))) 'ok) Example: (insert! ‘e 5 table)

One dimentional tables (define (insert! key value table) (let ((record (assoc key (cdr table)))) (if record (set-cdr! record value) (set-cdr! table (cons (cons key value) (cdr table))))) 'ok) *table* c d b a 1 2 3 4 e 5

One dimentional tables (define (make-table)(list '*table*)) *table*

Two dimentional tables presidents Bush 88 Clinton 92 elections Florida Bush Bush NY Gore California

Two dimentional tables (define (lookup key-1 key-2 table) (let ((subtable (assoc key-1 (cdr table)))) (if subtable (let ((record (assoc key-2 (cdr subtable)))) (if record (cdr record) false)) false))) Example: (lookup ‘elections ‘Florida table) ==> Bush

Two dimentional tables (define (insert! key-1 key-2 value table) (let ((subtable (assoc key-1 (cdr table)))) (if subtable (let ((record (assoc key-2 (cdr subtable)))) (if record (set-cdr! record value) (set-cdr! subtable (cons (cons key-2 value) (cdr subtable))))) (set-cdr! table (cons (list key-1 (cons key-2 value)) (cdr table))))) 'ok) Example: (insert! ‘elections ‘California ‘Gore table) ==> okbb

Two dimentional tables presidents Bush 88 Clinton 92 elections Florida Bush Bush Gore NY Gore California

Two dimentional tables Example: (insert! ‘singers ‘Madona ‘M table) ==> ok

Two dimentional tables presidents *table* Bush 88 Clinton 92 singers elections NY Florida Gore Bush California Madona M

Implement get and put (define oper-table (make-table)) (define (put x y v) (insert! x y v oper-table)) (define (get x y) (lookup x y oper-table))

Introduction to Object Oriented Programming

One View of Data "Generic" operations by looking at types: Tagged data: Some complex structure constructed from cons cells Explicit tags to keep track of data types Implement a data abstraction as set of procedures that operate on the data "Generic" operations by looking at types: (define (real-part z) (cond ((rectangular? z) (real-part-rectangular (contents z))) ((polar? z) (real-part-polar (contents z))) (else (error "Unknown type -- REAL-PART" z))))

An Alternative View of Data: Procedures with State A procedure has parameters and body as specified by l expression environment (which can hold name-value bindings!) Can use procedure to encapsulate (and hide) data, and provide controlled access to that data constructor, accessors, mutators, predicates, operations mutation: changes in the private state of the procedure

Example: Pair as a Procedure with State (define (cons x y) (lambda (msg) (cond ((eq? msg ‘CAR) x) ((eq? msg ‘CDR) y) ((eq? msg ‘PAIR?) #t) (else (error "pair cannot" msg))))) (define (car p) (p ‘CAR)) (define (cdr p) (p ‘CDR)) (define (pair? p) (and (procedure? p) (p ‘PAIR?)))

Reminder: What is our "pair" object? 1 p: msg body: (cond ...) E1 foo: x: 1 y: 2 (define foo (cons 1 2)) 2 3 (car foo) becomes (foo 'CAR) GE p: x y body: (l (msg) (cond ..)) cons: (car foo) | GE => (foo 'CAR) | E2 => 2 (cond ...) | E3 => x | E3 => 1 msg: CAR E3 3

Pair Mutation as Change in State (define (cons x y) (lambda (msg) (cond ((eq? msg ‘CAR) x) ((eq? msg ‘CDR) y) ((eq? msg ‘PAIR?) #t) ((eq? msg ‘SET-CAR!) (lambda (new-car) (set! x new-car))) ((eq? msg ‘SET-CDR!) (lambda (new-cdr) (set! y new-cdr))) (else (error "pair cannot" msg))))) (define (set-car! p new-car) ((p ‘SET-CAR!) new-car)) (define (set-cdr! p new-cdr) ((p ‘SET-CDR!) new-cdr))

Example: Mutating a pair object 1 p: msg body: (cond ...) E4 bar: x: 3 y: 4 (define bar (cons 3 4)) (set-car! bar 0) | GE => ((bar 'SET-CAR!) 0) | E5 2 (set-car! bar 0) GE 6 changes x value to 0 in E4 (cond ...) | E6 => (l (new-car) (set! x new-car)) | E6 msg: SET-CAR! E6 3 (set! x new-car) | E7 new-car: 0 E7 5 p: new-car body: (set! x new-car) 4

Message Passing Style - Refinements lexical scoping for private state and private procedures (define (cons x y) (define (change-car new-car) (set! x new-car)) (define (change-cdr new-cdr) (set! y new-cdr)) (lambda (msg . args) (cond ((eq? msg ‘CAR) x) ((eq? msg ‘CDR) y) ((eq? msg ‘PAIR?) #t) ((eq? msg ‘SET-CAR!) (change-car (car args))) ((eq? msg ‘SET-CDR!) (change-cdr (car args))) (else (error "pair cannot" msg))))) (define (car p) (p 'CAR)) (define (set-car! p val) (p 'SET-CAR! val))

Message Passing Style - Refinements lexical scoping for private state and private procedures (define (cons x y) (define (change-car new-car) (set! x new-car)) (define (change-cdr new-cdr) (set! y new-cdr)) (lambda (msg . args) (cond ((eq? msg ‘CAR) x) ((eq? msg ‘CDR) y) ((eq? msg ‘PAIR?) #t) ((eq? msg ‘SET-CAR!) (change-car (car args))) ((eq? msg ‘SET-CDR!) (change-cdr (car args))) (else (error "pair cannot" msg))))) (define (car p) (p 'CAR)) (define (set-car! p val) (p 'SET-CAR! val))

Programming Styles – Procedural vs. Object-Oriented Procedural programming: Organize system around procedures that operate on data (do-something <data> <arg> ...) (do-another-thing <data>) Object-based programming: Organize system around objects that receive messages (<object> 'do-something <arg>) (<object> 'do-another-thing) An object encapsulates data and operations Message passing and procedure are the means to write Object Oriented code in scheme

Tables in OO style (define (make-table) (let ((local-table (list '*table*))) (define (lookup key-1 key-2) . . . ) (define (insert! key-1 key-2 value) 'ok) (define (dispatch m) (cond ((eq? m 'lookup-proc) lookup) ((eq? m 'insert-proc!) insert!) (else (error "Unknown operation -- TABLE" m)))) dispatch))

Table in OO style (define operation-table (make-table)) (define get (operation-table 'lookup-proc)) (define put (operation-table 'insert-proc!))

(define oper-table (make-table)) | GE local-table *table* p: b: (let ((local-table (list '*table*))) . . . ) lookup: p: key-1 key-2 b: . . . insert!: dispatch:

Object-Oriented Programming Terminology Class: specifies the common behavior of entities in scheme, a "maker" procedure E.g. cons or make-table in our previous examples Instance: A particular object or entity of a given class in scheme, an instance is a message-handling procedure made by the maker procedure E.g. foo or bar or oper-table in our previous examples

Stacks in OO style (define (make-stack) (let ((top-ptr '())) (define (empty?) (null? top-ptr)) (define (delete!) (if (null? top-ptr) (error . . .) (set! top-ptr (cdr top-ptr))) top-ptr ) (define (insert! elmt) (set! top-ptr (cons elmt top-ptr)) top-ptr) (define (top) (if (null? top-ptr) (error . . .) (car top-ptr))) (define (dispatch op) (cond ((eq? op 'empty?) empty?) ((eq? op 'top) top) ((eq? op 'insert!) insert!) ((eq? op 'delete!) delete!))) dispatch))

Stacks in OO style (define s (make-stack)) ==> ((s 'insert!) 'a) ==> ((s 'insert!) 'b) ==> ((s 'top)) ==> ((s 'delete!)) ==> undef (a) (b a) b (a) a ()

Queues in OO style A lazy approach: We know how to do stacks so lets do queues with stacks :) We need two stacks: stack2 stack1 delete insert

Queues in OO style a a b a b b c b c ((q ‘insert) ‘a) ((q ‘insert) ‘b) ((q ‘delete)) b b c ((q ‘insert) ‘c) ((q ‘delete)) c

Queues in OO style (define (make-queue) (let ((stack1 (make-stack)) (stack2 (make-stack))) (define (reverse-stack s1 s2) _______________) (define (empty?) (and ((stack1 'empty?)) ((stack2 'empty?)))) (define (delete!) (if ((stack2 'empty?)) (reverse-stack stack1 stack2)) (if ((stack2 'empty?)) (error . . .) ((stack2 'delete!)))) (define (first) ((stack2 'top)))) (define (dispatch op) (cond ((eq? op 'empty?) empty?) ((eq? op 'first) first) ((eq? op 'delete!) delete!) (else (stack1 op)))) dispatch))

Queues in OO style Inheritance: One class is a refinement of another The queue class is a subclass of the stack class