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

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Object-Oriented Programming Python. OO Paradigm - Review Three Characteristics of OO Languages –Inheritance It isn’t necessary to build every class from.
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Paradigms and languages
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Type checking © Marcelo d’Amorim 2010.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Catriel Beeri Pls/Winter 2004/5 last 55 Two comments on let polymorphism I. What is the (time, space) complexity of type reconstruction? In practice –
Object-Oriented PHP (1)
Inheritance and Polymorphism CS351 – Programming Paradigms.
Object-oriented design CS 345 September 20,2002. Unavoidable Complexity Many software systems are very complex: –Many developers –Ongoing lifespan –Large.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
Language Evaluation Criteria
Programming Languages and Paradigms Object-Oriented Programming.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
CS 403 – Programming Languages Class 25 November 28, 2000.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Programming Languages and Paradigms Object-Oriented Programming.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
Introduction to Object Oriented Programming CMSC 331.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Object-Oriented Programming. An object is anything that can be represented by data in a computer’s memory and manipulated by a computer program.
Chapter 12 Support for Object oriented Programming.
Types in programming languages1 What are types, and why do we need them?
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Salman Marvasti Sharif University of Technology Winter 2015.
Object-Oriented Programming Chapter Chapter
C++ The reason why it is still in use. Table of Contents Disadvantages Disadvantages Advantages Advantages Compare with object-oriented programming language.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 16: Smalltalking about Objects.
Software Engineering and Object-Oriented Design Topics: Solutions Modules Key Programming Issues Development Methods Object-Oriented Principles.
ISBN Object-Oriented Programming Chapter Chapter
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
OO in Context Lecture 13: Dolores Zage. Confused about OO Not alone, there is much confusion about OO many programs are claimed to be OO but are not really.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Chapter 18 Object Database Management Systems. Outline Motivation for object database management Object-oriented principles Architectures for object database.
Lecture 2: Review of Object Orientation. © Lethbridge/La ganière 2005 Chapter 2: Review of Object Orientation What is Object Orientation? Procedural.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
COP 4331 – OOD&P Lecture 7 Object Concepts. What is an Object Programming language definition: An instance of a class Design perspective is different.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
ISBN Chapter 12 Support for Object-Oriented Programming.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Polymorphism in Methods
Types for Programs and Proofs
Object-oriented Programming in Java
Object Oriented Programming in Java
Type Systems Terms to learn about types: Related concepts: Type
Interfaces and Inheritance
Names, Binding, and Scope
Chapter 1: Introduction to Object Oriented Design
Type Systems Terms to learn about types: Related concepts: Type
Lecture 10 Concepts of Programming Languages
Subtype Substitution Principle
Chapter 11 Class Inheritance
Presentation transcript:

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

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 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 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 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 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 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 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 Research Goal Scheme Dynamically TypedStatically Typed ML Smalltalk???? Procedural OO OO typing is more difficult because of OO = procedural + data encapsulation + inheritance

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

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 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 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 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 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 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 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