Type Systems and ObjectOriented Programming IV PowerPoint PPT Presentation

presentation player overlay
About This Presentation
Transcript and Presenter's Notes

Title: Type Systems and ObjectOriented Programming IV


1
Type Systems and Object-Oriented Programming (IV)
  • John C. Mitchell
  • Stanford University

2
Outline
  • Foundations type-theoretic framework
  • Principles of object-oriented programming
  • Decomposition of OOP into parts
  • Formal models of objects

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

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

5
Analysis
  • Study properties of object-oriented languages
    using simplified models

Lambda calculus
Turing machines

Programming languages
Real hardware
Analogy is extremely unfair to l-calculus - D.
Scott
6
Object Calculus
  • extend untyped lambda calculus
  • á ñ empty object
  • e
  • áe
  • áe
  • symbolic evaluation rules
  • áe eáe
  • static type system
  • prevent message not understood

7
Examples
  • Syntactic Sugar
  • write áx lself.3, y lself.5ñ
  • for áááñ
  • Selection
  • áx lself.3, y lself.5ñ
  • (lself.3) áx lself.3, y lself.5ñ
  • 3

8
Examples
  • Use of Self
  • o á x lself.3, inc
  • lself.self xls.((self
  • o
  • (lself.self
  • o
  • o
  • á x lself.4, inc lself. self ... ñ

9
Examples
  • Use of Self
  • o á x lself.3, inc
  • lself.self xls.((self
  • o
  • (lself.self
  • o
  • o
  • á x lself.4, inc lself. self ... ñ

10
Examples
  • Non-termination
  • bee ábuzz lself.(self
  • bee
  • -- (lself. self
  • -- bee

11
Numerals as Pure Objects
  • Represent number n by object
  • n á rep lself. lx. x
  • succ lself. áself
  • rep ls. lx.(self
  • pred lself. áself
  • Fixed-point operator also definable using object
    operations

12
Choice of Object Primitives
  • Create, Select, Extend, Override
  • With extension, simplify creation
  • build ám_1 e_1, ..., m_k e_kñ
  • by áááñ
  • Typing complications with extension
  • Need intermediate objects to make sense
  • may want three methods, need sensible obj with 2
  • Permutation rules in op semantics

13
Alternatives in the literature
  • Mitchell 1990, FHM 93, ...
  • empty object, selection, extension, override
  • Abadi, Cardelli
  • direct construction, selection, overide
  • Trade-offs
  • extensible objects are more complicated,
    especially with method specialization
  • inheritance modelled directly with extension

14
Methods vs Functions
  • Calculus presented so far
  • á ... m lself. e ... ñ
  • functions used for data fields, methods
  • Alternatives Abadi/Cardelli, Obliq
  • method m meth(self, args) .... end
  • data field v exp
  • function fun (arg) ... end
  • Restrictions can be useful in practice

15
Imperative Objects
  • Various studies Bruce, Abadi/Cardelli,
    Harper/Mitchell, ...
  • Similar to extending lambda calc with asg
    Felleisen, Harper, ...
  • Can be viewed as special case of concurrency
    Walker, Pierce, ...
  • Cloning becomes interesting operation

16
Object types
  • Type determines set of operations
  • can choose any combination of
  • select, extend, override
  • Subtyping depends on set of operations
  • more operations, weaker subtyping
  • Include type recursion or keep separate
  • obj t. áá ... m t ... ññ restricted recursive
    type
  • some simplification, some idiosyncracies

17
Basic Forms of Object Types
  • áá m_1 t_1, ... , m_k t_k ññ_ops
  • where
  • m_1, ..., m_k are method (or field) names
  • t_1, ... , t_k are types
  • ops is a subset of sel,ext,ov , for
  • select, extend, override

18
Basic Forms (contd)
  • Example
  • áá x int, y int ññ_sel, ov
  • points, with selection and override
  • Confusing to have so many options
  • will focus on subset of possibilities
  • Also need more (in a few slides ...)
  • recursive types for common object interfaces
  • first-class rows for certain forms of poly...

19
Object operations and subtyping
  • Selection compatible with width, depth
  • For other operations, a small lattice

width and depth
ext depth only
width only ov
ext, ov no subtyping
20
Review forms of subtyping
  • Width subtyping
  • áám_1 t_1, ..., m_k t_k , n s ññ
  • Depth subtyping
  • s_1
  • áám_1 s_1, ..., m_k s_k ññ ..., m_k t_kññ

21
Examples
  • Width subtyping
  • áá x int, y int, c color ññ
  • Depth subtyping
  • manager
  • áá name string, sponsor managerññ

22
Conflict Width and Extension
  • Suppose
  • ob áá fint-int, gint-int ññ_sel,ext
  • If we asume width subtyping
  • áá fint-int, gint-int ññ_sel,ext
  • int ññ_sel,ext
  • Then we can form extended object
  • áob
  • But if f refers to g, this causes an error.
  • ob á f lself.( ... self ...ñ

23
Conflict Depth and Override
  • Suppose
  • ob áá x posint, logX real ññ_sel,ov
  • áx lself.3, logX lself. log(selfñ
  • If we asume depth subtyping
  • ááx posint, logX real ññ_sel,ov
  • Then we can override x by
  • áob
  • Violates assumption made in typing logX
  • Example taken from
    Abadi and Cardelli 94

24
Conclusion from this?
  • Want to support subtyping, inheritance
  • Subtyping
  • must drop extension and/or override
  • Inheritance
  • must have some extensible data structure
  • extensible objects, records, or modules
  • Typed OOLs must have two distinct ways of
    aggregating methods and fields

25
Models of Inheritance
  • Extensible objects M, FHM, FM,
    ...
  • separate extensible prototypes from
    non-extensible proper objects (without ext, ov)
  • Inheritance based on records or modules
  • Recursively-defined records as objects, combine
    generators for inheritance Cook,...
  • Record of traits Abadi,
    Cardelli
  • Other related approaches

26
Roadmap
  • Few more typing concepts
  • Recursive types
  • Rows
  • Typing rules for extensible objects
  • Use class construct to compare object calculi
  • inheritance by extensible objects
  • inheritance by record of constructor, traits

27
Recursive Types
  • Occur frequently
  • Point áá x int, y int, move int int -
    Point ññ
  • Standard approach
  • Point m t. áá xint, yint, move int x int
    - t ññ
  • Two variants
  • m t. A(t) A ( m t. A(t) ) with other
    equations
  • m t. A(t) _at_ A ( m t. A(t) ) by fold and
    unfold

28
Subtyping Recursive Types
  • Basic Rule
  • s
  • m s. A(s)
  • Useful in many contexts
  • Introduces complications
  • algorithmics of type checking
  • type equality

29
Specialized Alternative
  • Use isomorphism m t. A(t) _at_ A ( m t. A(t) )
  • Special syntax obj t. A(t)
  • combine fold with object formation
  • combine unfold with method invocation
  • Simplified subtyping (A, B covariant)
  • t Type - A(t)
  • obj t. A(t)
  • Allows special treatment of meth spec

30
Technical Device Rows
  • A row is a set of nametype pairs
  • Operator from Rows to Types
  • obj (Type Row) Type
  • write obj t.R for obj (lt.R)
  • Use only subtyping on Rows
  • width
  • depth

31
Specific Type System
  • Extensible objects
  • Row expressions
  • R r áá ññ áá R m t ññ lt.R
    Rt
  • relations
  • Types contain rows
  • t t t t pro R obj R
  • write pro t.R for pro (lt.R)

32
Prototypes vs Objects
  • Prototypes pro v.áá m... ññ
  • Extension and overriding
  • No subtyping between pro types
  • Objects obj v.áá m... ññ
  • Selection only (no extension, override)
  • Subtyping between obj types
  • Method body may override a method
  • Conversion pro R

33
Typing Rule for Messages
  • e obj t.R R
  • e
  • Combination of selection and application
  • Includes unfolding recursive type

34
Example
  • p á x lself. 3,
  • inc lself. self xls.((self
  • obj t. áá x int, inc t ññ
  • p
  • obj t. áá x int, inc t ññ /t
    t
  • obj t. áá x int, inc t ññ
  • Similar to Eiffel like current (but sound)

35
Typing Rule for Extension
  • e pro u.R
  • u T - R n
  • r
  • (pro u.ru)/u(u-t)
  • áe
  • Combines extension, folding of rec type
  • Method specialization useful in incremental
    construction of objects

w
36
Example
  • á ñ pro t. áá ññ
  • t Type - áá ññ x
  • r rt) int
  • á x lself.3 ñ obj t. áá x
    int ññ
  • t Type - áá xint ññ
    inc
  • r
  • (pro t.
    rt) (pro t. rt)
  • á x lself.3, inc lself. ... ñ obj t.
    áá x int, inc t ññ
  • inc returns object like current when this object
    extended

37
Typing Rule for Override
e pro u.R uType - R ññ r (pro u.ru)/u(u-t) áe pro u.R Combines redefinition, folding of
recursive type.
w
w
38
Example
s (pro t. rt) self pro
t. áá x int, inc t ññ (self int (self
  • r
  • self (pro t. rt)
  • self (pro t. rt)
  • tType - r t
  • r (pro t. rt) int
  • - self (pro t .rt)
  • - lself.self t. rt) (pro t. rt)

39
Review
  • o áx lself.3, inc lself.selfls.((self
  • obj t. áá x int, inc t ññ
  • o
  • ---- ( lself.self
  • ---- o
  • ---- áx lself.4, inc lself.self ls.((self

40
Technical Results Fisher,M
  • Subject Reduction Theorem
  • If G - e t is derivable and e ---
    e
  • then G - e t is derivable.
  • Type Soundness Theorem
  • If Æ - e t is derivable
  • then eval(e)error by specific strategy.
  • Type system prevents message not understood.

41
Final Topic Classes from Objects
  • Class provides
  • type definition
  • implementation of objects
  • control over initialization
  • basis for inheritance
  • distinguish public, private, protected
  • control implementation in derived classes
  • Goal Represent classes using extensible objects
    and abstract data types

42
Interfaces vs Class types
  • Interface type obj v. áá m ... ññ
  • all objects with these methods, regardless of
    implementation
  • useful for library functions, etc.
  • cannot determine offset statically
  • cannot implement binary operations
  • set áá mem elem bool, union set set
    ññ
  • C types fix part of implementation

43
Sample Class Construct
  • class class_name superclass
  • constructor
  • name type class_name

  • implementation
  • private name type implementation
  • public name type implementation
  • end

44
Example (implementation omitted)
  • class Point
  • constructor newP int Point
  • private x int
  • public setX int Point
  • getX int
  • end
  • class ColorPoint Point
  • constructor newCP color int
    ColorPoint
  • private c color
  • public setC color
    ColorPoint
  • getC color
  • end

45
Interfaces for Point Classes
  • Point Interfaces
  • within implementation pro P_priv
  • to derived class pro P_pub
  • to client program obj P_pub
  • ColorPoint Interfaces
  • within implementation pro CP_priv
  • to derived class pro CP_pub
  • to client program obj CP_pub
  • For protected, use P_priv

46
Interface Types
  • P_pub lt. áá setX int t, getX int ññ
  • P_priv lt. áá x int, setX int t, getX
    int ññ
  • CP_pub lt. áá p t setC color t, getC
    color ññ
  • CP_priv lt. áá p t ccolor, setCcolor t,
    getCcolorññ
  • The CP interfaces are written using a row
    variable p that
  • will be bound to existentially bound sub-rows
    of P_pub
  • or P_priv in the program.

47
Point Implementation
  • l iX. á x l self. iX,
  • setX l self. l newX.
  • á self
  • getX l self. self
  • ñ
  • Other operations in same style, e.g.,
  • mv l self. l dX.
  • áself

48
ColorPoint Implementation
  • l iX. l iC. á newPoint(iX)
  • á self
  • ñ

49
Program Structure
  • Abstype p (c,setC, getC,ø)
  • with newPoint int - pro v. p v
  • is p
  • in
  • Abstype cp (ø,ø)
  • with newCPoint int - col -prov v.
    cp v
  • is cp
  • in
  • let newPoint int - obj v. p
    v newPoint
  • newCPoint int - col -
    obj v. cp v newCPoint
  • in ... program ...

50
Properties
  • Restricted access to objects
  • derived class
  • see public (or protected) methods
  • extend objects, override methods
  • program
  • see public methods
  • send messages but cannot extend or override
  • subtyping
  • Standard properties of data abstraction

51
Program Structure with protected
  • Abstype p (c,setC,getC,ø)
  • with newPoint int - pro v. p(v)
  • is p
  • in
  • ... declaration of cp ...
  • Abstype p (ø,ø)
  • with newPoint int - obj
    v. p(v)
  • is p newPoint
  • in
  • ... program
    ...

52
Technical results Fisher,M
  • Object calculus with
  • Extensible objects, row variable polymorphism,
    variance annotations, negative information about
    methods, abstract types
  • Prove type soundness using
  • operational semantics
  • analysis of typing rules
  • Appeal to prior work on data abstraction

53
Insight
  • Link between inheritance and subtyping
  • subtypes of abstract types can only be produced
    by extension
  • C private virtual problem
  • Tentative design for ML 2000
  • modules object primitives OOP
  • but need extensible objects, subtyping, ...
  • type system seems very complicated

54
Assorted References
  • Abadi/Cardelli
  • object calculi without extension imperative
  • Castagna, et al.
  • multimethods
  • Pierce/Turner
  • exis types model, friend functions, etc.
  • Fisher, Di Blasio
  • concurrent extension of calculus here

55
Web pages
  • My home page for slides, other papers
  • http//theory.stanford.edu/people/jcm
  • Cardelli always has good stuff
  • http//www.research.digital.com/SRC/personal/Luca_
    Cardelli/home.html
  • Page of object/types people
  • http//cuiwww.unige.ch/OSG/Hop/types.html
  • Dont write any of this down. Use a search engine.

56
Summary
  • Foundations type-theoretic framework
  • Concepts in object-oriented programming
  • Decomposition of OOP into parts
  • Formal models of objects
  • Questions ???

57
Remember in 5 years
  • programming languages are not just a matter of
    taste, but subject to scientific study
  • types determine the building blocks of languages
  • mathematical semantics is essential for the
    design of sound proof methods
  • proof methods are essential if you really want to
    be sure about some aspect of a program
Write a Comment
User Comments (0)
About PowerShow.com