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

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

Date post:31-Mar-2015
Category:
View:216 times
Download:2 times
Share this document with a friend
Transcript:
  • Slide 1

Type Systems and Object- Oriented Programming (III) John C. Mitchell Stanford University Slide 2 Outline 3 Foundations; type-theoretic framework 3 Principles of object-oriented programming l Decomposition of OOP into parts l Formal models of objects Slide 3 Goals l Understand constituents of object- oriented programming l Possible research opportunities language design formal methods system development, reliability, security Slide 4 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 Slide 5 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 Slide 6 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 Slide 7 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 Slide 8 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 Slide 9 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... Slide 10 Class-based Languages l Simula 1960s Object concept used in simulation Activation record; no encapsulation l Smalltalk 1970s Improved metaphor; wholly object-oriented l C++ 1980s Adapted Simula ideas to C l Java 1990s Slide 11 Language concepts l encapsulation l dynamic lookup different code for different object integer + different from real + l subtyping l inheritance Slide 12 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 Slide 13 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 qs treated as lists of elems qs are abstract Slide 14 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 Slide 15 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 Slide 16 Dynamic Lookup l receiver 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; Slide 19stk.pop;stk.pop } end; But cannot similarly define nstack from dstack with pop redefined, and have dpop refer to new pop."> 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. Slide 21 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. Slide 22 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. Slide 23 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 Slide 24 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 Slide 25 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 Slide 26 Object Interfaces l Type Counter = val : int, inc : int -> Counter l Subtyping RCounter = val : int, inc : int -> RCounter, reset : RCounter 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 qPoint 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 Slide 44 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 Slide 45 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

Popular Tags:
of 45/45
Type Systems and Object-Oriented Programming (III) John C. Mitchell Stanford University
Embed Size (px)
Recommended