Principles of Programming Languages, 2

Similar documents
Principles of Programming Languages (S)

Objects as Session-Typed Processes

Smalltalk: developed at Xerox Palo Alto Research Center by the Learning Research Group in the 1970 s (Smalltalk-72, Smalltalk-76, Smalltalk-80)

CPS 506 Comparative Programming Languages. Programming Language

Object Oriented Programming

Programmazione. Prof. Marco Bertini

Object Oriented Programming (OOP)

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Konzepte von Programmiersprachen

Data Abstraction. Hwansoo Han

CS558 Programming Languages Winter 2013 Lecture 8

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

Lecture 16: Object Programming Languages

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

Overview. Elements of Programming Languages. Objects. Self-Reference

CSE 341: Programming Languages

CSE 413 Spring Introduction to Ruby. Credit: Dan Grossman, CSE341

Object-Oriented Programming Languages: Application and Interpretation

CS558 Programming Languages

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Object Oriented Classes, Objects, Inheritance, and Typing

OOPLs - Inheritance. Inheritance

Overview. Elements of Programming Languages. Objects. Self-Reference

Comp-304 : Object-Oriented Design What does it mean to be Object Oriented?

Functions & First Class Function Values

Dynamically-typed Languages. David Miller

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Java Object Oriented Design. CSC207 Fall 2014

CS558 Programming Languages

Ruby. Mooly Sagiv. Most slides taken from Dan Grossman

Object-oriented perspective

Concepts of Programming Languages

Back to OCaml. Summary of polymorphism. Example 1. Type inference. Example 2. Example 2. Subtype. Polymorphic types allow us to reuse code.

The role of semantic analysis in a compiler

features of Python 1.5, including the features earlier described in [2]. Section 2.6 summarizes what is new in Python The class and the class

Dynamic and Interactive Graphics in Lisp-Stat

DOWNLOAD PDF BEGINNING C 3.0 AN INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

6.001, Fall Semester, 1998 Lecture Notes, October 27 { Object Oriented Programming 2 An environment diagram illustrating +define foo +cons 1 2,, +set-

CS152: Programming Languages. Lecture 24 Bounded Polymorphism; Classless OOP. Dan Grossman Spring 2011

Introduction to Object-Oriented Programming

CSCI 2041: Object Systems

Programming Languages & Paradigms PROP HT Abstraction & Modularity. Inheritance vs. delegation, method vs. message. Modularity, cont d.

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Combined Object-Lambda Architectures

Space War Class Diagram. Elements of OOP. How to design interactions between objects. Space War Class Diagram with Inheritance

CIS192 Python Programming

CS153: Compilers Lecture 11: Compiling Objects

MIDTERM EXAMINATION - CS130 - Spring 2005

Object Model. Object Oriented Programming Spring 2015

JavaScript: Sort of a Big Deal,

The results for a few specific cases below are indicated. allequal ([1,1,1,1]) should return true allequal ([1,1,2,1]) should return false

Lecture 09. Ada to Software Engineering. Mr. Mubashir Ali Lecturer (Dept. of Computer Science)

Object Oriented Paradigm

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

O-O-Overloading & D-Dispatch. Scott Kilpatrick 02 March 2011

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Static and Dynamic Behavior עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע "י ד"ר גרא וייס

COSC252: Programming Languages: Abstraction and OOP. Jeremy Bolton, PhD Asst Teaching Professor. Copyright 2015 Pearson. All rights reserved.

Dynamic Object-Oriented Programming with Smalltalk 1. Introduction

Key components of a lang. Deconstructing OCaml. In OCaml. Units of computation. In Java/Python. In OCaml. Units of computation.

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

low and not larger than high. 18 points

About Python. Python Duration. Training Objectives. Training Pre - Requisites & Who Should Learn Python

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Ruby: Introduction, Basics

CSE 341, Autumn 2015, Ruby Introduction Summary

C++ Programming: Introduction to C++ and OOP (Object Oriented Programming)

Simula 67. Simula and Smalltalk. Comparison to Algol 60. Brief history. Example: Circles and lines. Objects in Simula

Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

G Programming Languages - Fall 2012

CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Dan Grossman Winter 2013

Today. Continue our very basic intro to JavaScript. Lambda calculus

2. Classes & Inheritance. Classes. Classes. Éric Tanter Objects and Design in Smalltalk. How do you create objects?

Object Model Comparisons

Custom Specializers in Object-Oriented Lisp

Ruby logistics. CSE341: Programming Languages Lecture 19 Introduction to Ruby and OOP. Ruby: Not our focus. Ruby: Our focus. A note on the homework

Goals of the Lecture OO Programming Principles

Lecturer: William W.Y. Hsu. Programming Languages

Scala : an LLVM-targeted Scala compiler

CIS192 Python Programming

Perl 6 Hands-On Tutorial

What is a class? Responding to messages. Short answer 7/19/2017. Code Listing 11.1 First Class. chapter 11. Introduction to Classes

The Extensible Java Preprocessor Kit. and a Tiny Data-Parallel Java. Abstract

Object Model. Object Oriented Programming Winter

CS 360 Programming Languages Interpreters

MITOCW watch?v=flgjisf3l78

SOFTWARE ARCHITECTURE 6. LISP

Chapter 10 :: Data Abstraction and Object Orientation

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

Lecture content. Course goals. Course Introduction. TDDA69 Data and Program Structure Introduction Cyrille Berger

John McCarthy IBM 704

Lecture Notes on Programming Languages

An Explicit Continuation Evaluator for Scheme

Parsing Scheme (+ (* 2 3) 1) * 1

Chapter3: Introduction to Classes and Objects

Symbolic Computation and Common Lisp

Monday Feb 16, 5pm. solve these equalities. Midterm Thursday Feb 12. covers only ocaml part of the course, assignments 1 through 3.

Aspect-Oriented Programming On Lisp

Case Study: Meta Classes

Transcription:

Principles of Programming Languages, 2 Matteo Pradella February 2015 Matteo Pradella Principles of Programming Languages, 2 February 2015 1 / 23

1 Object Oriented Programming (OO) Matteo Pradella Principles of Programming Languages, 2 February 2015 2 / 23

What is Object Oriented programming? 1 First of all, I assume you already know the main concepts from Java 2 OO means different things to different people 3 According to Alan Kay, who introduced the term: 1 OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. It can be done in Smalltalk and in Lisp. There are possibly other systems in which this is possible, but I m not aware of them. 2 Actually I made up the term "object-oriented", and I can tell you I did not have C++ in mind. 4 On the other hand, Stroustrup based C++ s OO model on Simula, not Smalltalk. Interesting comparison with Objective-C. Matteo Pradella Principles of Programming Languages, 2 February 2015 2 / 23

Closures as objects (1) 1 basic idea: use "closed" values for encapsulation of values and methods + a simple dispatcher for messages: ( define ( make- simple- object ) ( let (( my-var 0)) ( define ( init ) ( set! my-var 0) my-var ) ( define ( my-add x) ( set! my-var (+ my-var x)) my-var ) ( define ( get-my-var ) my-var ) Matteo Pradella Principles of Programming Languages, 2 February 2015 3 / 23

Closures as objects (2) ( define ( my-display ) ( newline ) ( display "my Var is:") ( display my-var ) ( newline )) ( lambda ( message. args ) ; dispatcher ( apply ( case message (( init ) init ) (( my-add ) my-add ) (( my-display ) my-display ) (( get-my-var ) get-my-var ) ( else ( error " Unknown Method!" ))) args )))) Matteo Pradella Principles of Programming Languages, 2 February 2015 4 / 23

Closures as objects (3) 1 make-simple-object returns a closure which contains the dispatcher 2 Example usage: ( define a ( make- simple- object )) ( define b ( make- simple- object )) (a init ) ; => 0 (b init ) ; => 0 (a my-add 3) ; => 3 (a my-add 4) ; => 7 (a get-my-var ) ; => 7 (b get-my-var ) ; => 0 (a my-display ) ; => My Var is: 7 Matteo Pradella Principles of Programming Languages, 2 February 2015 5 / 23

Inheritance by delegation (1) ( define ( make-son ) ( let (( parent ( make- simple- object )) ( name "an object ")) ( define ( init ) ( parent init )) ( define ( hello ) "hi!") ( define ( my-display ) ( display "My name is ") ( display name ) ( display " and ") ( parent my-display )) Matteo Pradella Principles of Programming Languages, 2 February 2015 6 / 23

Inheritance by delegation (2) ( lambda ( message. args ) ( case message (( init ) ( apply init args )) ; overriding : (( my- display ) ( apply my- display args )) (( hello ) ( apply hello args )) ( else ( apply parent ( cons message args ))))))) Matteo Pradella Principles of Programming Languages, 2 February 2015 7 / 23

Inheritance by delegation (3) 1 Example usage: ( define c ( make-son )) (c init ) ( c my- display ) ; = > My name is an object and ; my Var is :0 ( display (c hello )) ; => hi! Matteo Pradella Principles of Programming Languages, 2 February 2015 8 / 23

A prototype-based object system 1 Self (1987), a variant of Smalltalk, is the programming language that introduced prototype-based object orientation 2 There are no classes: new objects are obtained by cloning and modifying existing objects 3 Its OO model inspired the one of JavaScript 4 We will see here how to implement it on top of Scheme, using hash tables as the main data structure Matteo Pradella Principles of Programming Languages, 2 February 2015 9 / 23

Proto-oo 1 An object is implemented with an hash table ( define new- object make- hash ) ( define ( clone object ) ( hash- copy object )) 2 keys are attribute/method names Matteo Pradella Principles of Programming Languages, 2 February 2015 10 / 23

Proto-oo: syntactic sugar 1 just for convenience: ( define-syntax!! ;; setter ( syntax-rules () ((_ object msg new- val ) ( hash-set! object msg new-val )))) ( define-syntax?? ;; reader ( syntax-rules () ((_ object msg ) ( hash-ref object msg )))) ( define- syntax - > ;; send message ( syntax-rules () ((_ object msg arg...) (( hash-ref object msg ) object arg...)))) Matteo Pradella Principles of Programming Languages, 2 February 2015 11 / 23

An example 1 First, we define an object and its methods ( define Pino ( new- object )) (!! Pino name " Pino ") ;; slot added (!! Pino hello ( lambda ( self x) ;; method added ( display (?? self name )) ( display ": hi, ") ( display (?? x name )) ( display "!") ( newline ))) Matteo Pradella Principles of Programming Languages, 2 February 2015 12 / 23

An example (cont.) 1 a couple of other methods: (!! Pino set-name ( lambda ( self x) (!! self name x ))) (!! Pino set-name- &- age ( lambda ( self n a) (!! self name n) (!! self age a ))) 2 and a clone: ( define Pina ( clone Pino )) (!! Pina name " Pina ") Matteo Pradella Principles of Programming Languages, 2 February 2015 13 / 23

Using the example (- > Pino hello Pina ) ; Pino : hi, Pina! (- > Pino set-name " Ugo ") (- > Pina set- name- &- age " Lucia " 25) (- > Pino hello Pina ) ; Ugo : hi, Lucia! Matteo Pradella Principles of Programming Languages, 2 February 2015 14 / 23

Proto-oo: inheritance 1 Inheritance is not typical of prototype object systems 2 Still, it is used in JavaScript to provide a "more standard" way of reusing code 3 Again, inheritance by delegation: ( define ( son- of parent ) ( let ((o ( new-object ))) (!! o << parent > > parent ) o)) Matteo Pradella Principles of Programming Languages, 2 February 2015 15 / 23

Proto-oo: dispatching 1 basic dispatching: ( define ( dispatch object msg ) (if (eq? object unknown ) ( error " Unknown message " msg ) ( let (( slot ( hash-ref object msg unknown ))) (if (eq? slot unknown ) ( dispatch ( hash- ref object <<parent >> unknown ) msg ) slot )))) Matteo Pradella Principles of Programming Languages, 2 February 2015 16 / 23

Proto-oo: dispatching (cont.) 1 we now have to modify?? and -> for dispatching ( define-syntax?? ;; reader ( syntax-rules () ((_ object msg ) ( dispatch object msg )))) ( define- syntax - > ;; send message ( syntax-rules () ((_ object msg arg...) (( dispatch object msg ) object arg...)))) Matteo Pradella Principles of Programming Languages, 2 February 2015 17 / 23

And the example: ( define Glenn ( son- of Pino )) (!! Glenn name " Glenn ") (!! Glenn age 50) (- > Glenn hello Pina ) ; Glenn : hi, Lucia! (- > Glenn ciao ) ; error : Unknown message Matteo Pradella Principles of Programming Languages, 2 February 2015 18 / 23

About inheritance: single or multiple? 1 Different languages have different approaches (Java vs C++ vs Python vs Ruby... ) 2 many recent languages prefer it single (e.g. Java, Ruby) 3 workarounds for its shortcomings: interfaces (like in Java), mixins Matteo Pradella Principles of Programming Languages, 2 February 2015 19 / 23

Proto-oo: mixins 1 mixins are containers of data/methods which can be combined to add behavior to objects 2 We may introduce mixins in proto-oo as follows: ( define ( extend target mixin ) ( hash- for- each mixin ; loop on table " mixin " ( lambda ( key val ) ( hash-set! target key val )))) Matteo Pradella Principles of Programming Languages, 2 February 2015 20 / 23

Example: ( define Pianist ( new- object )) ; a mixin (!! Pianist piano " Steinway CD318 ") (!! Pianist play ( lambda ( self ) ;; assumes a name : ( display (?? self name )) ( display " is playing his ") ( display (?? self piano )) ( newline ))) ;; using the Pianist mixin : ( extend Glenn Pianist ) (- > Glenn play ) ;; Glenn is playing his Steinway CD318 Matteo Pradella Principles of Programming Languages, 2 February 2015 21 / 23

Concluding remarks: Object Systems for Lisps 1 There is a long tradition of object systems for Lisp and Scheme 2 Common Lisp contains a standard, flexible and very sophisticated object system (called CLOS), that provides e.g. generic functions (for multiple dispatch), and method combination 3 There is an implementation for Scheme, written by Gregor Kiczales, and called Tiny CLOS (there are also many variants) 4 These concepts are forerunners of Aspect-Oriented Programming (AOP) 5 For the interested: Gregor Kiczales, Jim Des Rivieres, Daniel G. Bobrow: The Art of the Metaobject Protocol, MIT Press, 1991 Matteo Pradella Principles of Programming Languages, 2 February 2015 22 / 23

Legal stuff 1 2012-2015 by Matteo Pradella 2 Licensed under Creative Commons License, Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) Matteo Pradella Principles of Programming Languages, 2 February 2015 23 / 23