Type Systems and Object- Oriented Programming (III) John C. Mitchell Stanford University.

Slides:



Advertisements
Similar presentations
Transposing F to C Transposing F to C Andrew Kennedy & Don Syme Microsoft Research Cambridge, U.K.
Advertisements

Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
MT311 (Oct 2007) Java Application Development Object-Oriented Programming Languages Tutorial 8.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1 Spring 2012.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1.
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Principles of Object-Oriented Software Development The language Eiffel.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Concepts in Object-Oriented Programming Languages Slides Today Taken From John Mitchell.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
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,
Chapter 24 Introduction to Object DBMSs Prepared by Kai Huang CS157B Prof Sin-Min Lee.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 Object Oriented Programming OOP: Many things to many people Some common elements –Objects = state + behavior –Interfaces –Classes –Subtyping, polymorphism.
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton October 18, 2015October 18, 2015October 18, 2015.
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.
Introduction to Object Oriented Programming CMSC 331.
OBJECT-ORIENTED PROGRAMMING (OOP) WITH C++ Instructor: Dr. Hany H. Ammar Dept. of Electrical and Computer Engineering, WVU.
Chapter 12 Support for Object oriented Programming.
OO as a language for acm l OO phrase l Mental model of key concepts.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 17: Inheritance & Behavioral.
Data Abstaraction Chapter 10.
Abstraction ADTs, Information Hiding and Encapsulation.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Basic Concepts of Object Orientation Object-Oriented Analysis CIM2566 Bavy LI.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Abstract Data Types and Modules Lecture 11 – ADT and Modules, Spring CSE3302 Programming.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
Eight languages Eight weeks How I designed and implemented a teaching OO language in 4 days.
Object-Oriented Programming “The Rest of the Story”, CS 4450 – Chapter 16.
1 Objects for Amadeus Gert Smolka Programming Systems Lab, UdS, Saarbrücken August 1999.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Principles of programming languages 10: Object oriented languages
Multi-Methods in Cecil
Object Oriented Programming in Java
Lecture 2 of Computer Science II
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Object Oriented Analysis and Design
Lecture 9 Concepts of Programming Languages
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2018.
Objects and Aspects: What we’ve seen so far
Introduction to Computer Science for Majors II
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Zach Tatlock Winter 2018.
Introduction to Data Structure
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2017.
Lecture 10 Concepts of Programming Languages
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2013.
Languages and Compilers (SProg og Oversættere)
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2019.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Type Systems and Object- Oriented Programming (III) John C. Mitchell Stanford University

Outline 3 Foundations; type-theoretic framework 3 Principles of object-oriented programming l Decomposition of OOP into parts l Formal models of objects

Goals l Understand constituents of object- oriented programming l Possible research opportunities »language design »formal methods »system development, reliability, security

Object-oriented programming 3 Programming methodology »organize concepts into objects and classes »build extensible systems l Language concepts »encapsulate data and functions into objects »subtyping allows extensions of data types »inheritance allows reuse of implementation

Varieties of OO languages l class-based languages »behavior of object determined by its class »objects created by instantiating a classes l object-based »objects defined directly –in total, cloning, extension, override l multi-methods »code-centric instead of object-centric »run-time overloaded functions

Method invocation l single dispatch: »receiver.message(object,..., object) »code depends on receiver only l multiple dispatch (“multi-methods”) »operation(object,..., object) »code may depend on types of all objects

Comparison l single dispatch »data hidden in objects »cannot access private data of parameters l multiple dispatch »better for symmetric binary operations »loss of encapsulation »but see work by Chambers and Leavens »curried multiple dispatch =? single dispatch

These lectures l Class-based, object-based languages l Single-dispatch method invocation l References for other languages »Cecil, CommonLisp are multimethod-based »Foundations by Castagna, et al., others

Intuitive picture of objects l An object consists of »hidden data »public operations l Program sends messages to objects Hidden data msg 1 msg n method 1 method n...

Class-based Languages l Simula 1960’s »Object concept used in simulation »Activation record; no encapsulation l Smalltalk 1970’s »Improved metaphor; wholly object-oriented l C ’s »Adapted Simula ideas to C l Java 1990’s

Language concepts l encapsulation l “dynamic lookup” »different code for different object »integer “+” different from real “+” l subtyping l inheritance

Abstract data types Abstype q with mk_Queue : unit -> q is_empty : q -> bool insert : q * elem -> q remove : q -> elem is {q = elem list,(  tuple of functions   in  program   end Block-structured simplification of modular organization

Abstract data types Abstype q with mk_Queue : unit -> q is_empty : q -> bool insert : q * elem -> q remove : q -> elem is {q = elem list,(  tuple of functions   in  program   end q’s treated as lists of elems q’s are abstract

Priority Q, similar to Queue Abstype pq with mk_Queue : unit -> pq is_empty : pq -> bool insert : pq * elem -> pq remove : pq -> elem is {pq = elem list,(  tuple of functions   in  program   end

Abstract Data Types l Guarantee invariants of data structure »only functions of the data type have access to the internal representation of data l Limited “reuse” »Cannot apply queue code to pqueue, except by explicit parameterization, even though signatures identical »Cannot form list of points, colored points

Dynamic Lookup l receiver <= operation (arguments) l code depends on receiver and operation l This is may be achieved in conventional languages using record with function components.

OOP in Conventional Lang. l Records provide “dynamic lookup” l Scoping provides another form of encapsulation Try object-oriented programming in ML

Stacks as closures fun create_stack(x) = let val store = ref [x] in {push = fn (y) => store := y::(!store), pop = fn () => case !store of nil => raise Empty | y::m => (store := m; y) } end;

Does this work ??? l Depends on what you mean by “work” l Provides »encapsulation of private data »dynamic lookup l But »cannot substitute extended stacks for stacks »only weak form of inheritance –can add new operations to stack –not mutually recursive with old operations

Weak Inheritance fun create_stack(x) = let val store =... in {push =..., pop=...} end; fun create_dstack(x) = let val stk = create_stack(x) in { push = stk.pusk, pop= stk.pop, dpop = fn () => stk.pop;stk.pop } end; But cannot similarly define nstack from dstack with pop redefined, and have dpop refer to new pop.

Weak Inheritance (II) fun create_dstack(x) = let val stk = create_stack(x) in { push = stk.push, pop= stk.pop, dpop = fn () => stk.pop;stk.pop } end; fun create_nstack(x) = let val stk = create_dstack(x) in { push = stk.push, pop= new_code, dpop = fn () => stk.dpop } end; Would like dpop to mean “pop twice”.

Weak Inheritance (II) fun create_dstack(x) = let val stk = create_stack(x) in { push = stk.push, pop= stk.pop, dpop = fn () => stk.pop;stk.pop } end; fun create_nstack(x) = let val stk = create_dstack(x) in { push = stk.push, pop= new_code, dpop = fn () => stk.dpop } end; New code does not alter meaning of dpop.

Inheritance with Self (almost) fun create_dstack(x) = let val stk = create_stack(x) in { push = stk.push, pop= stk.pop, dpop = fn () => self.pop; self.pop} end; fun create_nstack(x) = let val stk = create_dstack(x) in { push = stk.push, pop= new_code, dpop = fn () => stk.dpop } end; Self interpreted as “current object itself”

Summary l Have encapsulation, dynamic lookup in traditional languages (e.g., ML) l Can encode inheritance: »can extend objects with new fields »weak semantics of redefinition –NO “SELF” ; NO “OPEN RECURSION” l Need subtyping as language feature

Subtyping l A is a subtype of B if any expression of type A is allowed in every context requiring an expression of type B l Substitution principle subtype polymorphism provides extensibility l Property of types, not implementations

Object Interfaces l Type Counter =  val : int, inc : int -> Counter  l Subtyping RCounter =  val : int, inc : int -> RCounter, reset : RCounter  <: Counter

Facets of subtyping l Covariance, contravariance l Width and depth l For recursive types l F-bounded and higher-order

Covariance l Definition »A type form  (...) is covariant if s <: t implies  (s) <:  (t) l Examples »  (x) = int  x (cartesian product) »  (x) = int  x (function type)

Contravariance l Definition »A type form  (...) is contravariant if s <: t implies  (t) <:  (s) l Example »  (x) = x  bool Specifically, if int <: real, then real  bool <: int  bool and not conversely

Non-variance l Some type forms are neither covariant nor contravariant l Examples »  (x) = x  x »  (x) = Array[1..n] of x Arrays are covariant for read, contravariant for write, so non-variant if both are allowed.

Simula Bug l Statically-typed program with A <: B proc asg (x : Array[1..n] of B) begin; x[1] := new B; /* put new B value in B location */ end; y : Array[1..n] of A; asg( y ): l Places a B value in an A location l Also in Borning/Ingalls, Eiffel systems

Subtyping for records/objects l Width subtyping  m_1 :  _1,..., m_k :  _k, n:   <:  m_1 :  _1,..., m_k :  _k  l Depth subtyping   _1 <:  _1,...,  _k <:  _k  m_1 :  _1,..., m_k :  _k  <:  m_1 :  _1,..., m_k :  _k 

Examples l Width subtyping  x : int, y : int, c : color  <:  x : int, y : int  l Depth subtyping manager <: employee  name : string, sponsor : manager  <:  name : string, sponsor : employee 

Subtyping for recursive types l Basic rule If s <: t implies A(s) <: B(t) Then  t.A(t) <:  t.B(t) l Example »A(t) =  x : int,  y : int, m : int --> t  »B(t) =  x : int,  y : int, m : int --> t, c : color 

Subtyping recursive types l Example »Point =  x : int,  y : int, m : int --> Point  »Col_Point =  x : int,  y : int, m : int --> Col_Point, c : color  l Explanation »If p : Point and expression e(p) is OK, then if q : Col_Point then e(q) must be OK »Induction on the # of operations applied to q.

Contravariance Problem l Example »Point =  x : int,  y : int, equal : Point --> bool  »Col_Point =  x : int,  y : int, c : color, equal : Col_Point --> bool  l Neither is subtype of the other »Assume p: Point, q: Col_Point »Then q <= equal p may give type error.

Parametric Polymorphism l General “max” function »max(greater, a,b) = if greater(a, b) then a else b l How do we assign a type? »assume a:t, b:t for some type t »need greater :  t  t  bool l Polymorphic type »max :  t  t  t  bool)  t  t  t

Subtyping and Parametric Polymorphism l Object-oriented “max” function »max(a,b) = if a.greater(b) then a else b l How do we assign a type? »assume a:t, b:t for some type t »need t <:  greater : t  bool  l F-bounded polymorphism »max :  t <:  greater : t  bool  t  t  t

Why is type quantifier useful? l Recall conditions of problem »conditional requires a:t, b:t for some type t »need t <:  greater : t  bool  l “Simpler” solution »use type  t.  greater : t  bool  »max :  t. ...  t. ...  t. ...  l  However... »not supertype due to contravariance »return type has only greater method

Alternative l F-bounded polymorphism »max :  t <:  greater : t  bool  t  t  t l Higher-order bounded polymorphism »max :  F <: t.  greater : t  bool   F  F  F l Similar in spirit but technical differences »Transitive relation »“Standard” bounded quantificaion

Inheritance l Mechanism for reusing implementation »RCounter from Counter by extension » Counter from RCounter by hiding l In principle, not linked to subtyping l Puzzle: Why are subtyping and inheritance combined in C++, Eiffel, Trellis/Owl...?

Method Specialization l Change type of method to more specialized type l May not yield subtype due to contravariance problem l Illustrates difference between inheritance and subtyping l Also called “mytype specialization” [Bruce]; Eiffel “like current”

Covariant Method Specialization l Assume we have implemenation for Point =  x : int,  y : int, move : int  int --> Point  l Extension with color could give us an object of type Col_Point =  x : int,  y : int, c : color move : int  int --> Col_Point  l Inheritance results in a subtype

Contravariant Specialization l Assume we have implemenation for Point =  x : int,  y : int, eq : Point --> bool  l Extension with color and redefinition of equal could give us an object of type Col_Point =  x : int,  y : int, c : color eq : Col_Point --> bool  l Inheritance does not result in a subtype

Summary of Part III l Language support divided into four parts »encapsulation, dynamic lookup, subtyping, inheritancs l Subtyping and inheritance require extension to conventional languages l Subtyping and inheritance are distinct concepts that are sometimes correlated