The role of abstractions

Slides:



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

Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
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.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
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:
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.
Trees! =) Write up-add tree which behaves in this manner: 1 31 / \ / \ 3 5  21 9 / | \ \ / | \ \
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
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
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
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.
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.
Object Oriented Programming Systems
Operational Semantics of Scheme
Edited by Original material by Eric Grimson
CS 326 Programming Languages, Concepts and Implementation
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
Class 22: Inheritance CS150: Computer Science University of Virginia
The interpreter.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Object Oriented Concepts -I
Env. Model Implementation
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
Original material by Eric Grimson
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
6.001 SICP Data abstractions
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
The Metacircular Evaluator
Lecture 15: Tables and OOP
FP Foundations, Scheme In Text: Chapter 14.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
Lecture 16: Tables and OOP
Defining Classes and Methods
6.001 SICP Streams – the lazy way
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Explicit Application of Procedures, and Explicit Evaluation
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
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 מבוא.
6.001 SICP Data abstractions
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
Today’s topics Abstractions Procedural Data
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
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Presentation transcript:

The role of abstractions Procedural abstractions Data abstractions Goal: treat complex things as primitives, and hide details 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? 3/30/2004 6.001 SICP

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")))) 3/30/2004 6.001 SICP

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

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!) 3/30/2004 6.001 SICP

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 Procedure application creates private environment Need access to that environment constructor, accessors, mutators, predicates, operations mutation: changes in the private state of the procedure 3/30/2004 6.001 SICP

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

Example: 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 3/30/2004 6.001 SICP

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

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 3/30/2004 6.001 SICP

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

Variable number of arguments A scheme mechanism to be aware of: Desire: (add 1 2) (add 1 2 3 4) 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 1 2 3 4 5) => rest bound to (3 4 5) 3/30/2004 6.001 SICP

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-oriented programming: Organize system around objects that receive messages (<object> 'do-something <arg>) (<object> 'do-another-thing) An object encapsulates data and operations 3/30/2004 6.001 SICP

Object-Oriented Programming Terminology Class: specifies the common behavior of entities in Scheme, a make-<type> procedure which makes a particular kind of message handler Instance: A particular object or entity of a given class in Scheme, an instance is a message-handling procedure made by the maker procedure 3/30/2004 6.001 SICP

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

Using classes & instances to design a system Suppose we want to build a “space wars” simulator We can start by thinking about what kinds of objects we want (what classes, their state information, and their interfaces) ships space-stations other objects We can then extend to thinking about what particular instances of objects are useful Millenium Falcon Enterprise Babylon3 3/30/2004 6.001 SICP

Space-Ship Object (define (make-ship position velocity num-torps) (define (move) (set! position (add-vect position velocity))) (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))))) 3/30/2004 6.001 SICP

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

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

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

Some Extensions to our World Add more classes to our world a SPACE-STATION class a TORPEDO class 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) 3/30/2004 6.001 SICP

Add Space-Station Class SHIP position: velocity: num-torps: POSITION VELOCITY MOVE ATTACK DISPLAY STATION position: POSITION DISPLAY 3/30/2004 6.001 SICP

Station Implementation (define (make-station position) (lambda (msg) (cond ((eq? msg ′POSITION) position) ((eq? msg ′DISPLAY) (draw ...)) (else (error "station can't" msg))))) 3/30/2004 6.001 SICP

Add Torpedo Class SHIP TORPEDO STATION position: velocity: num-torps: MOVE ATTACK DISPLAY EXPLODE TORPEDO position: velocity: POSITION VELOCITY MOVE DISPLAY EXPLODE STATION position: POSITION DISPLAY 3/30/2004 6.001 SICP

Torpedo Implementation (define (make-torpedo position velocity) (define (explode torp) (display “torpedo goes off!”) (remove-from-universe torp)) (define (move) (set! position ...)) (lambda (msg . args) (cond ((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))))) 3/30/2004 6.001 SICP

Application: Running a Simulation ;; Build some things (define babylon3 (make-station (make-vect 0 0))) (define enterprise (make-ship (make-vect 10 10) (make-vect 5 0) 3)) (define falcon (make-ship (make-vect -10 10) (make-vect 10 0) 8)) ;; Run a simulation (define *the-universe* (list babylon3 enterprise falcon)) (init-clock *the-universe*) (run-clock 100) … and magical things happen on a display near you … 3/30/2004 6.001 SICP

Elements of Object-Oriented Programming “Smart” data structure Set of state variables Set of methods for manipulating state variables Class: Specifies the common structure and behavior of entities Instance: A particular object or entity of a given class 3/30/2004 6.001 SICP

Space War Class Diagram SHIP position: velocity: num-torps: POSITION VELOCITY MOVE ATTACK DISPLAY EXPLODE STATION TORPEDO velocity: target: proximity-fuse: MOVE DISPLAY Ships and torpedoes have some behavior that is the same – is there are way to capture this commonality? 3/30/2004 6.001 SICP

Space War Class Diagram with Inheritance SHIP class is a specialization or sub-class of the MOBILE-THING class SHIP is-a MOBILE-THING SHIP inherits the state and behavior of MOBILE-THING MOBILE-THING class is a super-class of the SHIP and TORPEDO classes MOBILE-THING position: velocity: POSITION VELOCITY MOVE SHIP num-torps: ATTACK DISPLAY EXPLODE TORPEDO target: proximity-fuse: is-a STATION position: POSITION DISPLAY has-a target 3/30/2004 6.001 SICP

Elements of OOP Object “Smart” data structure Set of state variables Set of methods for manipulating state variables Class: Specifies the common structure and behavior of instances Inheritance to share structure and behaviors between classes Instance: A particular object or entity of a given class Next time: a full-bodied object-oriented system implementation In particular, how to incorporate inheritance 3/30/2004 6.001 SICP