1 Programming Languages (CS 550) Lecture 5 Summary Object Oriented Programming and Implementation* Jeremy R. Johnson *Lecture based on notes from SICP.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
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.
Scheme in Scheme?!? …the metacircular evaluator….
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 Programming Languages (CS 550) Operational Semantics of Scheme using Substitution and the Lambda Calculus Jeremy R. Johnson TexPoint fonts used in EMF.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
1 Programming Languages (CS 550) Lecture 7 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts used in EMF. Read.
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.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Metacircular Evaluator 4.1, pages definitions file on web 2.
(define applicative-eval (lambda (exp) (cond ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list.
1 Lecture 18 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
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.
Scheme More MCE examples. Q1 new special form which defines global variables (static ) search the global environment – Variable exists: does nothing,
OOP! POO! Spelled backwards. Intro to OOP What is OOP?  Stands for Object Oriented Programming  Create different types of objects which can do multiple.
SICP Variations on a Scheme (2) Beyond Scheme – designing language variants: Lazy evaluation Complete conversion – normal order evaluator Upward.
The environment model evaluator and compiler 1 The env model evaluator Motivation In one word: Efficiency Saves repeated renaming and substitution: Using.
1 Saves repeated renaming and substitution: explicit substitution is replaced by variable bindings using new data structures (frame, environment). Can.
1 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure + symbol.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
1 The metacircular evaluator (Cont.) Defining new procedures (define (lambda? e) (tag-check e 'lambda)) (define (eval exp env) (cond ((number? exp)
PrasadL145OOL1 Managing Environments An Exercise in the Design, Analysis, Specification, and Implementation of the Core of an OOP Language. Object-Oriented.
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.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 15: Meta-Circular Evaluator 한 태숙.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
1 Subt. model interpreter: Structure of the interpreter ASP Derived expressions Core Test Data structures Utils A racket lib Proc / Primitive-Proc. Global.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
1 Lecture 20 Lazy Evaluation Continued (4.2.1, 4.2.2) MC-eval examples from exams (Time permitting)
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.
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 한 태숙.
1 Subt. model interpreter: Introduction Abstract Syntax Parser Abstract Syntax Parser Derived Expressions Core Test Data Structures Utils (a racket lib)
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
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.
CS 598 Scripting Languages Design and Implementation 10. Interpreters Part I: Lisp.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
(Thunking about Thunks)
Operational Semantics of Scheme
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
6.001 SICP Compilation Context: special purpose vs. universal machines
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.
CSC 533: Organization of Programming Languages Spring 2008
Env. Model Implementation
Types of Programming Languages
Original material by Eric Grimson
Nondeterministic Evaluation
Mini Language Interpreter Programming Languages (CS 550)
The Metacircular Evaluator
Lecture 15: Tables and OOP
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Lecture 16: Tables and OOP
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
6.001 SICP Variations on a Scheme
6.001 SICP Interpretation Parts of an interpreter
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
*Lecture based on notes from SICP
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

1 Programming Languages (CS 550) Lecture 5 Summary Object Oriented Programming and Implementation* Jeremy R. Johnson *Lecture based on notes from SICP

2 Theme  Object oriented programming provides a convenient mechanism for modeling state. State is local to the object itself. Each object includes a set of functions (methods) through which its state can be accessed and changed.  Inheritance is the major mechanism of object- oriented languages that allows sharing of data and operations among classes, as well as the ability to redefine these operations without modifying existing code (type polymorphism).  Objects can be easily implemented in a language with first class functions (environments). Using an interpreter we can incorporate these features in a language.

3 Outline  Review concepts of OOP  Implementing objects in scheme  Store local state in environment  Use message passing for method invocation  Inheritance stores internally superclass  Creating an object oriented language  TOOL (Tiny Object Oriented Language)  Extend metacircular interpreter to support classes, class instantiation, generic functions, and methods

4 Object Oriented Programming  Classes  Object instantiation (constructors)  Instance variables  Methods and method invocation  Inheritance  Single and multiple inheritance  Abstract classes and dynamic binding  Subtype polymorphism

5 Modeling State in Scheme (define (make-account balance) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (define (dispatch m) (cond ((eq? m 'withdraw) withdraw) ((eq? m 'deposit) deposit) (else (error "Unknown request -- MAKE- ACCOUNT" m)))) dispatch) (define acc (make-account 100)) ((acc 'withdraw) 50) 50 ((acc 'withdraw) 60) "Insufficient funds" ((acc 'deposit) 40) 90 ((acc 'withdraw) 60) 30

6 Objects in Scheme  An object is a procedure that, given a message as argument, returns another procedure called a method (define (get-method object message) (object message)) (define (make-speaker) (define (self message) (cond ((eq? message 'say) (lambda (stuff) (display stuff))) (else (no-method "SPEAKER")))) self)

7 Invoking a Method  To ask an object to apply a method to some arguments we send the object a message asking for the appropriate method, and apply the method to the arguments (define (ask object message. args) (let ((method (get-method object message))) (if (method? method) (apply method args) (error "No method" message (cadr method)))))

8 Example (define george (make-speaker)) ;Value: george (ask george 'say '(the sky is blue)) (the sky is blue)

9 Inheritance  One thing we may want to do is define an object type to be a kind of some other object type (define (make-lecturer) (let ((speaker (make-speaker))) (define (self message) (cond ((eq? message 'lecture) (lambda (stuff) (ask self 'say stuff) (ask self 'say '(you should be taking notes)))) (else (get-method speaker message)))) self))

10 Example (define gerry (make-lecturer)) ;Value: gerry (ask gerry 'say '(the sky is blue)) (the sky is blue) ;Unspecified return value (ask gerry 'lecture '(the sky is blue)) (the sky is blue)(you should be taking notes) ;Unspecified return value

11 More Inheritance  The following example shows a bug in the implementation (self is lost) (define (make-arrogant-lecturer) (let ((lecturer (make-lecturer))) (define (self message) (cond ((eq? message 'say) (lambda (stuff) (ask lecturer 'say (append '(it is obvious that) stuff)))) (else (get-method lecturer message)))) self))

12 A Bug: Losing Self  The problem is that when Albert calls internal lecturer, Albert’s self is lost. Albert, his internal lecturer, and his internal lecturer’s internal speaker, each have a self (define albert (make-arrogant-lecturer)) (ask albert 'say '(the sky is blue)) (it is obvious that the sky is blue) (ask albert 'lecture '(the sky is blue)) (the sky is blue)(you should be taking notes)

13 Fixing the Bug  We can fix this by changing the implementation so that all the methods keep track of self by taking self as an extra input (define (make-speaker) (define (self message) (cond ((eq? message 'say) (lambda (self stuff) (display stuff))) (else (no-method message)))) self) (define (ask object message. args) (let ((method (get-method object message))) (if (method? method) (apply method (cons object args)) (error "No method" message (cadr method)))))

14 Multiple Inheritance  We can have object types that inherit methods from more than one type (define (make-singer) (lambda (message) (cond ((eq? message 'say) (lambda (self stuff) (display (append '(tra-la-la --) stuff)))) ((eq? message 'sing) (lambda (self) (display '(tra-la-la)))) (else (no-method "SINGER")))))

15 Multiple Inheritance (singer/lecturer) (define ben (let ((singer (make-singer)) (lecturer (make-lecturer))) (lambda (message) (let ((sing (get-method singer message)) (lect (get-method lecturer message))) (if (method? sing) sing lect))))) (ask ben 'sing) (tra-la-la) (ask ben 'say '(the sky is blue)) (tra-la-la -- the sky is blue) (ask ben 'lecture '(the sky is blue)) (tra-la-la -- the sky is blue) (tra-la-la -- you should be taking notes)

16 Multiple Inheritance (lecturer/singer) (define alyssa (let ((singer (make-singer)) (lecturer (make-lecturer))) (lambda (message) (let ((sing (get-method singer message)) (lect (get-method lecturer message))) (if (method? lect) lect sing))))) (ask alyssa 'sing) (tra-la-la) (ask alyssa 'lecture '(the sky is blue)) (the sky is blue)(you should be taking notes)

17 TOOL Overview  Define classes  (define-class name superclass. slots)  Instantiate classes  (make class slot-names-and-values)  Generic functions (dispatch on signature)  (define-generic-function name)  Method definition  (define-method generic-function (params-and- classes. body)

18 TOOL Examples (load "teval") ;Loading "teval.scm"... done ;Value: set-binding-value! (initialize-tool) TOOL==> (define-class size breed) (defined class: ) TOOL==> (define garfield (make (size 6) (breed 'wierd))) *undefined* TOOL==> (get-slot garfield 'breed) wierd TOOL==> (get-slot garfield 'size) 6

19 TOOL Examples TOOL==> (define-generic-function 4-legged?) (defined generic function: 4-legged?) TOOL==> (define-method 4-legged? ((x )) true) (added method to generic function: 4-legged?) TOOL==> (4-legged? garfield) #t TOOL==> (define-method 4-legged? ((x )) 'who-knows) (added method to generic function: 4-legged?) TOOL==> (4-legged? 3) who-knows

20 TOOL Examples TOOL==> (define-generic-function say) (defined generic function: say) TOOL==> (define-method say ((x ) (stuff )) (print 'meow:) (print stuff)) (added method to generic function: say) TOOL==> (define-class awards) (defined class: ) TOOL==> (define-method say ((cat ) (stuff )) (print stuff) (print '(I am beautiful))) (added method to generic function: say)

21 TOOL Examples TOOL==> (define Cornelius-Silverspoon-the-Third (make (size 'large) (breed '(Cornish Rex)) (awards '((prettiest skin))))) *undefined* TOOL==> (say Cornelius-Silverspoon-the-Third '(feed me)) (feed me) (i am beautiful) #!unspecific TOOL==> (define-method say ((cat ) (stuff )) (print '(cats never discuss numbers))) (added method to generic function: say) TOOL==> (say fluffy 37) (cats never discuss numbers) #!unspecific

22 TOOL Implementation  Extend metacircular interpreter to handle class, generic function, and method definitions, and instance creations  Provide code to apply a generic function

23 Modified Scheme Interpreter eval (define (tool-eval exp env) (cond ((self-evaluating? exp) exp) ((quoted? exp) (text-of-quotation exp)) ((variable? exp) (lookup-variable-value exp env)) ((definition? exp) (eval-definition exp env)) ((assignment? exp) (eval-assignment exp env)) ;;((lambda? exp) (make-procedure exp env)) ((conditional? exp) (eval-cond (clauses exp) env)) ((generic-function-definition? exp) (eval-generic-function-definition exp env)) ((method-definition? exp) (eval-define-method exp env)) ((class-definition? exp) (eval-define-class exp env)) ((instance-creation? exp) (eval-make exp env)) ((application? exp) (tool-apply (tool-eval (operator exp) env) (map (lambda (operand) (tool-eval operand env)) (operands exp)))) (else (error "Unknown expression type -- EVAL >> " exp))))

24 Modified Scheme Interpreter apply (define (tool-apply procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure arguments)) ((compound-procedure? procedure) (eval-sequence (procedure-body procedure) (extend-environment (parameters procedure) arguments (procedure-environment procedure)))) ((generic-function? procedure) (apply-generic-function procedure arguments)) (else (error "Unknown procedure type -- APPLY"))))

25 TOOL Data Structures  Class  Class name, list of slots, list of ancestors  Generic function  Name, list of methods defined for function  Method  Specializers, procedure  Instance  Class, list of values for slots

26 Defining Generic Functions (define-generic-function name) (define (eval-generic-function-definition exp env) (let ((name (generic-function-definition-name exp))) (let ((val (make-generic-function name))) (define-variable! name val env) (list 'defined 'generic 'function: name))))

27 Defining Methods (define-method generic-function (params-and-classes). Body) (define (eval-define-method exp env) (let ((gf (tool-eval (method-definition-generic-function exp) env))) (if (not (generic-function? gf)) (error "Unrecognized generic function -- DEFINE-METHOD >> " (method-definition-generic-function exp)) (let ((params (method-definition-parameters exp))) (install-method-in-generic-function gf (map (lambda (p) (paramlist-element-class p env)) params) (make-procedure (make-lambda-expression (map paramlist-element-name params) (method-definition-body exp)) env)) (list 'added 'method 'to 'generic 'function: (generic-function-name gf))))))

28 Defining and Instantiating Classes (define-class name superclass. Slots) (make class slot-names-and-values) (define (eval-define-class exp env) (let ((superclass (tool-eval (class-definition-superclass exp) env))) (if (not (class? superclass)) (error "Unrecognized superclass -- MAKE-CLASS >> " (class-definition-superclass exp)) (let ((name (class-definition-name exp)) (all-slots (collect-slots (class-definition-slot-names exp) superclass))) (let ((new-class (make-class name superclass all-slots))) (define-variable! name new-class env) (list 'defined 'class: name))))))

29 Applying Generic Functions  Find all methods that are applicable  Use first one (most specialized)  Method1 < Method2 (class method1 is a subclass of class method2)  Extract procedure for that method and apply (define (apply-generic-function generic-function arguments) (let ((methods (compute-applicable-methods-using-classes generic-function (map class-of arguments)))) (if (null? methods) (error "No method found -- APPLY-GENERIC-FUNCTION") (tool-apply (method-procedure (car methods)) arguments))))

30 Applying Generic Functions (define (compute-applicable-methods-using-classes generic-function classes) (sort (filter (lambda (method) (method-applies-to-classes? method classes)) (generic-function-methods generic-function)) method-more-specific?)) (define (method-applies-to-classes? method classes) (define (check-classes supplied required) (cond ((and (null? supplied) (null? required)) true) ;;something left over, so number of arugments does not match ((or (null? supplied) (null? required)) false) ((subclass? (car supplied) (car required)) ;; class1 is subclass of class2 if (check-classes (cdr supplied) (cdr required))) ;; class2 in ancestor list of class1 (else false) )) (check-classes classes (method-specializers method)))

31 Assignment 5  Extend assignment 4 (mini language with first class functions) to support classes, objects, and inheritance  Class name (p1,…,pn) End  The statement list defines the methods (method 1,…,method n ) and attributes in the class  The class definition provides a constructor with parameters p1,…,pn, called by name(p1,…,pn). The constructor returns an object which can access the methods and attributes of the class initialized by the constructor using the familiar dot notation.

32 Assignment 5  Example Class list(init) L := init; Cons := proc(x) return cons(x,L) end; Car := proc() return car(L) end; Cdr := proc() return cdr(L) end; end; L := list([]); L.Cons(3); L.Cons(2); L.Cons(1); x := L.Car(); // = 1 M := L.Cdr(); // = [2,3]

33 Assignment 5  Inheritance  Classes can be derived from other classes using the notation Class name(p1,…,pn) : supername End  Objects in the derived class should be able to access methods and attributes from the derived class, however, they can be redefined in the subclass.