6.001 SICP 1/30 6.001 SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams.

Slides:



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

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
1 Lecture 18 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure.
Object System I. OO System in Scheme class private variables methods NAMED-OBJECT self: name: sicp instance x root self: TYPE IS-A NAMED-OBJECT name:
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
SICP Data Mutation Primitive and Compound Data Mutators Stack Example non-mutating mutating Queue Example non-mutating mutating.
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
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,
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 The metacircular evaluator (Cont.) Defining new procedures (define (lambda? e) (tag-check e 'lambda)) (define (eval exp env) (cond ((number? exp)
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
1/21 4/6/2004 OOPS – One more example Goal: See an example that distinguishes between –“is-a” or inheritance relationships –“has-a” or local variable relationships.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Fall 2008Programming Development Techniques 1 Topic 14 Multiple Representations of Abstract Data – Data Directed Programming and Additivity Section
1 Read-Eval-Print Loop (define (driver-loop) (prompt-for-input input-prompt) (let ((input (read))) (let ((output (eval input the-global-env))) (announce-output.
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 Lecture 19 Dynamic Scoping Lazy Evaluation (4.2.1, 4.2.2)
1/ SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Making the environment model concrete Defining eval defines the language –Provides.
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.
Scheme in Scheme 2. What’s next  Adding set!  Dynamic vs. lexical variable scope  Extending mcscheme v1 with libraries  Can mcscheme execute mcscheme?
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Object Oriented Programming Systems
Operational Semantics of Scheme
Edited by Original material by Eric Grimson
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
6.001 SICP Compilation Context: special purpose vs. universal machines
The interpreter.
The role of abstractions
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
Original material by Eric Grimson
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
The Metacircular Evaluator
Lecture 15: Tables and OOP
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Lecture 16: Tables and OOP
6.001 SICP Streams – the lazy way
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
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
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
6.001 SICP Interpretation Parts of an interpreter
Lecture 13: Assignment and the Environment Model (EM)
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Principles of Programming Languages
*Lecture based on notes from SICP
Lecture 25: The Metacircular Evaluator Eval Apply
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Presentation transcript:

6.001 SICP 1/ SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams Instance diagrams Example: space wars simulation

6.001 SICP 2/30 The role of abstractions Procedural abstractions Data abstractions Questions: How easy is it to break system into abstraction modules? How easy is it to extend the system? Adding new data types? Adding new methods? Goal: treat complex things as primitives, and hide details

6.001 SICP 3/30 One View of Data 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 (scale x factor) (cond ((number? x) (* x factor)) ((line? x) (line-scale x factor)) ((shape? x) (shape-scale x factor)) (else (error "unknown type"))))

6.001 SICP 4/30 Dispatch on Type Adding new data types: Must change every generic operation Must keep names distinct Adding new methods: Just create generic operations Data type 1Data type 2Data type 3Data type 4 Operation 1Some proc Operation 2Some proc Operation 3Some proc Operation 4Some proc Generic operation Generic data object?

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

6.001 SICP 6/30 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?)))

6.001 SICP 7/30 Example: What is our "pair" object? (define foo (cons 1 2)) GE p: x y body: ( (msg) (cond..)) cons: 1 p: msg body: (cond...) E1 foo: x: 1 y: 2 1 (car foo) | GE => (foo 'CAR) | E2 => 2 (cond...) | E3 => x | E3 => 1 msg: CAR E (car foo) becomes (foo 'CAR) (define (cons x y) (lambda (msg) (cond ((eq? msg ‘CAR) x) ((eq? msg ‘CDR) y) ((eq? msg ‘PAIR?) #t) (else (error …))))) (define (car p) (p ‘CAR))

6.001 SICP 8/30 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))

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

6.001 SICP 10/30 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 (first args))) ((eq? msg ‘SET-CDR!) (change-cdr (first args))) (else (error "pair cannot" msg))))) (define (car p) (p 'CAR)) (define (set-car! p val) (p 'SET-CAR! val))

6.001 SICP 11/30 Variable number of arguments A scheme mechanism to be aware of: Desire: (add 1 2) (add ) How do this? (define (add x y. rest)...) (add 1 2) => x bound to 1 y bound to 2 rest bound to '() (add 1) => error; requires 2 or more args (add 1 2 3) => rest bound to (3) (add ) => rest bound to (3 4 5)

6.001 SICP 12/30 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 (first args))) ((eq? msg ‘SET-CDR!) (change-cdr (first args))) (else (error "pair cannot" msg))))) (define (car p) (p 'CAR)) (define (set-car! p val) (p 'SET-CAR! val))

6.001 SICP 13/30 Programming Styles – Procedural vs. Object-Oriented Procedural programming: Organize system around procedures that operate on data (do-something...) (do-another-thing ) Object-based programming: Organize system around objects that receive messages ( 'do-something ) ( 'do-another-thing) An object encapsulates data and operations

6.001 SICP 14/30 Object-Oriented Programming Terminology Class: specifies the common behavior of entities in scheme, a "maker" procedure Instance: A particular object or entity of a given class in scheme, an instance is a message-handling procedure made by the maker procedure

6.001 SICP 15/30 Class Diagram Instance Diagram PAIR x: y: CAR CDR PAIR? SET-CAR! SET-CDR! class private state public messages (define (cons x y) ( (msg)...)) (define foo (cons 3 (cons 1 2))) PAIR x: y: PAIR x: y: instance of pair foo

6.001 SICP 16/30 Using classes and instances to design a system Suppose we want to build a “star wars” simulator I can start by thinking about what kinds of objects do I want (what classes, their state information, and their interfaces) ships planets other objects I can then extend to thinking about what particular instances of objects are useful Millenium Falcon Enterprise Earth

6.001 SICP 17/30 A Space-Ship Object (define (make-ship position velocity num-torps) (define (move) (set! position (add-vect position...))) (define (fire-torp) (cond ((> num-torps 0)...) (else 'FAIL))) (lambda (msg) (cond ((eq? msg 'POSITION) position) ((eq? msg 'VELOCITY) velocity) ((eq? msg 'MOVE) (move)) ((eq? msg 'ATTACK) (fire-torp)) (else (error "ship can't" msg)))))

6.001 SICP 18/30 Space-Ship Class SHIP position: velocity: num-torps: POSITION VELOCITY MOVE ATTACK

6.001 SICP 19/30 Example – Instance Diagram (define enterprise (make-ship (make-vect 10 10) (make-vect 5 0) 3)) (define war-bird (make-ship (make-vect ) (make-vect 10 0) 10)) SHIP pos: (vec 10 10) vel: (vec 5 0) num-torps: 3 SHIP pos: (vec 10 10) vel: (vec 5 0) num-torps: 3 enterprise war-bird

6.001 SICP 20/30 position: (vec 10 10) velocity: (vec 5 0) num-torps: Example – Environment Diagram (define enterprise (make-ship (make-vect 10 10) (make-vect 5 0) 3)) (enterprise ‘MOVE) ==> DONE (enterprise ‘POSITION) ==> (vect 15 10) GE par: msg body: (cond …) enterprise: move: fire-torp: par: body: (set! position …) (vec 15 10) 2 From internal definitions 3 3

6.001 SICP 21/30 Some Extensions to our World Add a PLANET class to our world Add predicate messages so we can check type of objects Add display handler to our system Draws objects on a screen Can be implemented as a procedure (e.g. draw ) -- not everything has to be an object! Add 'DISPLAY message to classes so objects will display themselves upon request (by calling draw procedure)

6.001 SICP 22/30 Space-Ship Class SHIP position: velocity: num-torps: POSITION VELOCITY MOVE ATTACK SHIP? DISPLAY PLANET position: POSITION PLANET? DISPLAY

6.001 SICP 23/30 Planet Implementation (define (make-planet position) (lambda (msg) (cond ((eq? msg ‘PLANET?) #T) ((eq? msg ‘POSITION) position) ((eq? msg ‘DISPLAY) (draw...)) (else (error "planet can't" msg)))))

6.001 SICP 24/30 Further Extensions to our World Animate our World! Add a clock that moves time forward in the universe Keep track of things that can move (the *universe* ) Clock sends ‘ACTIVATE message to objects to have them update their state Add TORPEDO class to system

6.001 SICP 25/30 Class Diagram SHIP position: velocity: num-torps: POSITION VELOCITY MOVE ATTACK SHIP? DISPLAY EXPLODE PLANET position: POSITION PLANET? DISPLAY TORPEDO position: velocity: TORPEDO? POSITION VELOCITY MOVE DISPLAY EXPLODE

6.001 SICP 26/30 Coordinating with a clock CLOCK The-time: callbacks: CLOCK? NAME THE-TIME TICK ADD-CALLBACK CALLBACK Object: message: Data: … ACTIVATE object State info … methods Sends object message and data

6.001 SICP 27/30 The Universe and Time (define (make-clock. args) (let ((the-time 0) (callbacks '())) (lambda (message) (case message ((CLOCK?) (lambda (self) #t)) ((NAME) (lambda (self) name)) ((THE-TIME) (lambda (self) the-time)) ((TICK) (lambda (self) (map (lambda (x) (ask x 'activate)) callbacks) (set! the-time (+ the-time 1)))) ((ADD-CALLBACK) (lambda (self cb) (set! callbacks (cons cb callbacks)) 'added))

6.001 SICP 28/30 Controlling the clock ;; Clock callbacks ;; ;; A callback is an object that stores a target object, ;; message, and arguments. When activated, it sends the target ;; object the message. It can be thought of as a button that ;; executes an action at every tick of the clock. (define (make-clock-callback name object msg. data) (lambda (message) (case message ((CLOCK-CALLBACK?) (lambda (self) #t)) ((NAME) (lambda (self) name)) ((OBJECT) (lambda (self) object)) ((MESSAGE) (lambda (self) msg)) ((ACTIVATE) (lambda (self) (apply-method object object msg data)))

6.001 SICP 29/30 Implementations for our Extended World (define (make-ship position velocity num-torps) (define (move) (set! position (add-vect position...))) (define (fire-torp) (cond ((> num-torps 0) (set! num-torps (- num-torps 1)) (let ((torp (make-torpedo...)) (add-to-universe torp)))) (define (explode ship) (display "Ouch. That hurt.")) (ask clock 'ADD-CALLBACK (make-clock-callback ‘moveit me ‘MOVE)) (define (me msg. args) (cond ((eq? msg ‘SHIP?) #T)... ((eq? msg ‘ATTACK) (fire-torp)) ((eq? msg ‘EXPLODE) (explode (car args))) (else (error "ship can't" msg)))) ME)

6.001 SICP 30/30 Torpedo Implementation (define (make-torpedo position velocity) (define (explode torp) (display “torpedo goes off!”) (remove-from-universe torp)) (define (move) (set! position...)) (ask clock 'ADD-CALLBACK (make-clock-callback ‘moveit me ‘MOVE)) (define (me msg. args) (cond ((eq? msg ‘TORPEDO?) #T) ((eq? msg ‘POSITION) position) ((eq? msg ‘VELOCITY) velocity) ((eq? msg ‘MOVE) (move)) ((eq? msg ‘EXPLODE) (explode (car args))) ((eq? msg ‘DISPLAY) (draw...)) (else (error “No method” msg)))) ME)

6.001 SICP 31/30 Running the Simulation ;; Build some things (define earth (make-planet (make-vect 0 0))) (define enterprise (make-ship (make-vect 10 10) (make-vect 5 0) 3)) (define war-bird (make-ship (make-vect ) (make-vect 10 0) 10)) ;; Start simulation (run-clock 100)

6.001 SICP 32/30 Summary Introduced a new programming style: Object-oriented vs. Procedural Uses – simulations, complex systems,... Object-Oriented Modeling Language independent! Class – template for state and behavior Instances – specific objects with their own identities Next time: powerful ideas of inheritance and delegation