Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder.

Similar presentations


Presentation on theme: "1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder."— Presentation transcript:

1 1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder

2 2 What is a Type System? (B.C. Pierce 2002): A type system is a tractable syntactic method for proving absence of certain program behaviors by classifying phrases according to the kinds of values they compute.

3 3 What is a Type System? What is a type? A type is a set of values sharing some properties (e.g. all object which support an interface). A value v has type t if v is an element of t. A type is denoted by a “type expression”. Examples Int = {0,-1,1,-2,2,...} String = { “abc”, “0xzA%”,... } String -> Int = { functions from String to Int } Int x String = { (i,s) | i is an Int and s is a String }

4 4 Two Kinds of Type Systems Type checking The programmer provides explicit type declarations. Type Inference The type system derives the types of expressions and variables from the context.

5 5 Type Checking function f(x : integer) : integer; begin return 2*x+1; end; Explicit type declarations provide types for key points: f : integer -> integer x : integer Types of expressions are inferred 2 * x : integer 2 * x + 1 : integer

6 6 Type Inference f x = 2*x + 1 No explicit type declarations are required Types of expressions and variables are “infered” 1 :: Int 2 :: Int 2*x :: Int x :: Int 2*x + 1 :: Int f :: Int -> Int

7 7 Why? Robustness: Elimination of type errors Readability: Types are excellent documentation Efficiency: Type information allows optimizations Abstraction: Support for interfaces, abstract data types, modules based on types. Static Types are useful for improving Static types are like proven assertions about a program

8 8 Important Terminology Type Safeness: Type correct programs do not cause “untrapped errors”. Type Soundness: Type correct programs do not cause “forbidden errors”. Forbidden Errors: In OO forbidden errors are: Untrapped errors “Message not understood” Subtype: A type b is a subtype of type p if values of type b can be used in any context where type p is expected without introducing errors.

9 9 Research Goal Scheme Dynamically TypedStatically Typed ML Smalltalk???? Procedural OO OO typing is more difficult because of OO = procedural + data encapsulation + inheritance

10 10 The Problems in OO Typing Typing of Encapsulation (Solved: “existential types” [Girard 71]) Inheritance (Partially solved) Polymorphism (Open issue)

11 11 Typing Inheritance Question: What relationship should the Type Checker enforce between a class and its superclass? Problem: Many OO languages link inheritance to subtyping (e.g. C++ and Java) => Seems intuitive but... Bad idea! [Snyder86,Cook92] => Chose between Covariant subtyping => Flexible but not type safe Contravriant typing => Type safe but not flexible Novariant typing => The worst of both worlds

12 12 Typing Inheritance Covariant Typing Animal { Shit eats(Food) } Cow extends Animal { CowShit eats(Grass) } Problem: Not type sound Animal a = new Cow(); Food f = new Hamburger(); a.eat(f); //Food poisoning Contravariant Typing Animal { Shit eats(Grass) } Cow extends Animal { CowShit eats(Food) } Problem: Sound but not flexible Novariant Typing Animal { Shit eats(Food) } Cow extends Animal { Shit eats(Food) } Problem: Worst of both worlds.

13 13 Typing Inheritance What is the problem realy? There is a difference between code that works with many different types of values all at the same time (e.g. a Zoo) => subtyping one specialized version of a type (e.g. BirdCage) If inheritance is used for both and linked to subtyping => Troubles Solution? 1) Decouple inheritance from subtyping (e.g. use something else such as matching [Bruce]) 2) Use different kinds of polymorphism instead of only subtype polymorphism.

14 14 Typing Polymorphism Very hard problem. Why? Late binding = Runtime Type Dependance Static typing = Compile Time Type Knowledge Many different forms of polymorphism[Cardelli+Wegner 85] Universal:one implementation for infinitely many types Parametric (e.g. Collections) Inclusion (Subype polymorphism) Ad-hoc: a few different implementations for a few types. e.g. message + for Integer, Float and String

15 15 Parametric Polymorphism Virtual Types class Animal typedef MyFood as Food; typedef MyShit as Shit; MyShit eat(MyFood); } Parametric Types class Animal { MyShit eat(MyFood); }

16 16 Conclusion Type Systems for OO are difficult mostly because of polymorphism and interactions of this with inheritance and subtyping. Either: Complex type systems difficult to understand and use Simple type systems which are unsound inflexible

17 17 Further Reading [ B.C. Pierce 2002] Types and Programming Languages, MIT Press. [Cardelli&Wegner85] On Understanding Types, Data Abstraction and Polymorphism. [Cardelli97] Type Systems. Cardelli papers available from http://www.luca.demon.co.uk/


Download ppt "1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder."

Similar presentations


Ads by Google