2015-6-28PADL041 A Typeful Approach to Object- Oriented Programming with Multiple Inheritance Chiyan Chen, Rui Shi, Hongwei Xi Computer Science Dept. Boston.

Slides:



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

Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Type Systems and Object- Oriented Programming (III) John C. Mitchell Stanford University.
Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
PADL 041 Implementing Cut Elimination: A case study of simulating dependent types in Haskell Chiyan Chen Dengping Zhu Hongwei Xi Boston University.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1 Spring 2012.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1.
Distributed Meta- Programming Rui Shi, Chiyan Chen and Hongwei Xi Boston University.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
CSE341: Programming Languages Lecture 22 Multiple Inheritance, Interfaces, Mixins Dan Grossman Fall 2011.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Distributed Meta- Programming (To appear GPCE’06) Rui Shi, Chiyan Chen and Hongwei Xi Boston University.
1 Unifying Object-Oriented Programming with Typed Functional Programming Hongwei Xi Boston University.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Comparison of OO Programming Languages © Jason Voegele, 2003.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
1 Structure and Interpretation of Computer Programs Modularity, Objects, and State (part 1) Abelson & Sussman 2 UC San Diego CSE 294 May 11, 2011 Barry.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
1 Object Oriented Programming OOP: Many things to many people Some common elements –Objects = state + behavior –Interfaces –Classes –Subtyping, polymorphism.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Late binding and dynamic dispatch (Based on CSE 341 slides by Dan Grossman) 1.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Session 24 Chapter 12: Polymorphism. Polymorphism polymorphism comes from the Greek root for “many forms” polymorphism is about how we can use different.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 14: Object Oriented Programming 한 태숙.
Session 18 Lab 9 Re-cap, Chapter 12: Polymorphism & Using Sound in Java Applications.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2013.
TeachJava! 2003 Corky Cartwright Dung Nguyen Stephen Wong Charlie Reis, James Hsia, Peter Centgraf.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Design issues for Object-Oriented Languages
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Winter 2013.
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
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2018.
Lecture 15: Tables and OOP
More Object-Oriented Programming
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Zach Tatlock Winter 2018.
CSE341: Programming Languages Section 9 Dynamic Dispatch Manually in Racket Zach Tatlock Winter 2018.
Lecture 16: Tables and OOP
Inheritance and Polymorphism
C++ Programming CLASS This pointer Static Class Friend Class
Lecture 10 Concepts of Programming Languages
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
Presentation transcript:

PADL041 A Typeful Approach to Object- Oriented Programming with Multiple Inheritance Chiyan Chen, Rui Shi, Hongwei Xi Computer Science Dept. Boston University

PADL042 Motivation  Through translating high-level OOP syntax to a typed -calculus, the soundness of the typing rules for the OOP features follows from the soundness of the target system.  Within the Applied Type System (ATS) framework, we want to support OOP as an additional package without complicating the existing system.

PADL043 Objects as Functions  Examples: Smalltalk  Interpretation: An object is represented as a message interpreter, that is, a function which takes messages dispatched to it as arguments and then selects the appropriate behavior according to the messages.  Message passing: function application.

PADL044 Constructing an Integer Pair Object  fun newIntPair x y = let val xref = ref x and yref = ref y fun dispatch msg = case msg of | MSGgetfst => !xref | MSGgetsnd => !yref | MSGsetfst x' => (xref := x') | MSGsetsnd y' => (yref := y') | _ => raise UnknownMessage in dispatch end withtype int -> int -> OBJ(ip)  Given a class C, OBJ(C) is a shorthand for the following type:  : type.MSG(C,  ) → 

PADL045 Typed OOP Model  An improved typed OOP model based on the previous work (POPL ’ 03, Xi, Chen and Chen) Takes the “ objects as functions ” view Models the basic features: message passing, single inheritance. Provides a natural solution to the notion of “ self type ”. Subclassing is modeled with explicit proof terms. Supports multiple inheritance

PADL046 Single Inheritance vs. Multiple Inheritance  Single Inheritance: Examples: Smalltalk, Java Easy to reason and implement.  Multiple Inheritance: Examples: Eiffel, C++ Complicated model Interaction with other OO features

PADL047 Subtlety with MI in C++ A B1B2 C A :: foo() = 0 B2 :: foo() = 2 B2 :: bar2() = foo() B1 :: foo() = 1 B1 :: bar1() = foo() C :: foo() = 3 C* pc = new C(); pc->bar1() returns 3

PADL048 Subtlety with MI in C++ A B1B2 C A :: foo() = 0 B2 :: foo() = 2 B2 :: bar2() = foo() B1 :: foo() = 1 B1 :: bar1() = foo() foo() is not overridden. C* pc = new C(); pc->bar1() returns 1

PADL049 Subtlety with MI in C++ A B1B2 C A :: foo() = 0 B2 :: foo() = 2 B2 :: bar2() = foo() B1 :: foo() = 1 B1 :: bar1() = foo() C :: bar3 () = B1 :: foo() D* pd = new D(); pd->bar3() returns 1 D D :: foo() = 4

PADL0410 Subtlety with MI in C++ A B1B2 C A :: foo() = 0 B2 :: foo() = 2 B2 :: bar2() = foo() B1 :: foo() = 1 B1 :: bar1() = foo() C :: foo () = B1 :: foo() D* pd = new D(); pd->bar2() returns 1 D foo() is not overridden.

PADL0411 Our Solution  Using explicit inheritance paths to direct method lookup. A B1B2 C A :: foo() = 0 B2 :: foo() = 2 B2 :: bar2() = A :: B2 :: foo() B1 :: foo() = 1 B1 :: bar1() = A :: B1 :: foo() C :: bar3 () = A :: B1 :: C :: foo() D* pd = new D(); pd->C :: D :: bar3() returns 4 D D :: foo() = 4

PADL0412 Run-Time Class Tags  datasort cls = obj | eq | ip | cip | …  datatype ClS (cls) = | CLSobj (obj) | CLSeq (eq) | CLSip (ip) | CLScip (cip) |... obj eq Ip cip

PADL0413 Inheritance Paths  Paths from (super)classes to (sub)classes are treated as run-time proof terms (of subclass relation)  Declare PTH as a guarded datatype, which takes two static class tags C 1 and C 2 to form a type PTH(C 1, C 2 ) for paths from C 1 to C 2  E.g. Abbreviate a path from class obj to cip as [CLSobj, CLSip, CLScip]

PADL0414 Regular/Temporary Objects  Given a static class tag C, OBJ(C) is the type for regular objects in class C, which is the shorthand for the following type:  c 0 :cls.  :type.PTH(c 0,C) → MSG(c 0,C,  ) →   There is another form of objects that are only constructed during run-time called temporary objects. Given a static class tag C, OBJ 0 (C) stands for the type:  c 0 :cls.  :type.MSG(c 0,C,  ) →   Note that a temporary object is not constructed by applying a regular object to a given path.

PADL0415 Wrapper Functions  A wrapper function for a class C is assigned the type WRP(C), where WRP(C) stands for the following type: OBJ(C) → OBJ 0 (C)  Intuitively, a wrapper function turns a regular object in class C into a temporary object in class C.  A wrapper function is mainly used to “hard-wire” a method lookup strategy into a regular object.

PADL0416 Super Functions  For each class C, there is a super function associated with it, which plays a key role in implementing (multiple) inheritance.  Given a static class tag C 0, the super function associated with C 0 is assigned the type SUPER(C 0 ), which is the shorthand for the following type:  c:cls.PTH(C 0,c) → WRP (c) → WRP (c)

PADL0417 An Example of Super Function  fun SUPEReq pth wrp obj = let fun dispatch msg = case msg of | MSGeq (obj') => not (obj pth (MSGneq (obj'))) | MSGneq (obj') => not (obj pth (MSGeq (obj'))) | _ => wrp obj msg in dispatch end withtype {c:cls} PTH (eq,c) → WRP(c) → WRP (c)

PADL0418 Chaining Super Functions Together  Given an object o, nullwrapper(o) constructs a temporary object, which raises the UnknownMessage exception for any message received.  Given a run-time inheritance path p, path2wrapper(p) turns the path to a wrapper. e.g. path2wrapper [CLSobj, CLSip, CLScip] = SUPERcip [CLScip] (SUPERip [CLSip, CLScip] (SUPERobj [CLSobj, CLSip, CLScip] nullWrapper))

PADL0419 Constructing Objects  newintPair is an object constructor function, which takes two integers to create an integer pair object.  fun newIntPair x y = let val xref = ref x and yref = ref y fun dispatch pth msg = case msg of | MSGcopy => newIntPair (!xref) (!yref) | MSGgetfst => !xref | MSGsetfst x' => (xref := x') | … | _ => path2wrapper pth dispatch msg in dispatch end withtype int -> int -> OBJ (ip)

PADL0420 Example for Method Lookup  Given two integer pair objects o 1 and o 2, let us consider O1O1 (MSGneq(o 2 )) [CLSeq, CLSip] No message handler for MSGneq(o 2 ) path2wrapper [CLSeq, CLSip] o 1 (MSGneq(o 2 )) SUPERip [CLSip] (SUPEReq [CLSeq, CLSip] nullWrapper) o 1 (MSGneq(o 2 )) No message handler for MSGneq(o 2 ) (SUPEReq [CLSeq, CLSip] nullWrapper) o 1 (MSGneq(o 2 )) Has message handler for MSGneq(o 2 ) O1O1 (MSGeq(o 2 )) [CLSeq, CLSip] not MSGeq(o 2 ) finally be handled by SUPERip

PADL0421 Conclusion  We have developed a general approach to multiple inheritance in a typed setting Representing inheritance paths as (typed) run- time proof-terms. Different strategies to resolve dispatching ambiguity Interaction between multiple inheritance and parametric polymorphism A clear model which can be implemented efficiently (e.g. by following C++ implementation)