INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Object Oriented Programming with Java
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
The Substitution Principle SWE 332 – Fall Liskov Substitution Principle In any client code, if subtype object is substituted for supertype object,
CS 2430 Day 28. Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Generic programming in Java
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
CSE 425: Semantics II Implementing Scopes A symbol table is in essence a dictionary –I.e., every name appears in it, with the info known about it –Usually.
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
Creating Generic Classes. Introduction Java Generics were added to allow for type- safe collections and eliminate the need for burdensome, code-cluttering.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
ADSA: Subtypes/ Advanced Data Structures and Algorithms Objective –explain how subtyping/subclassing and generics are combined in Java –introduce.
Java Generics.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Class Design III: Advanced Inheritance Additional References “Object-Oriented Software Development Using Java”, Xiaoping Jia, Addison Wesley, 2002 “Core.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Outline §Review of the last class l class variables and methods l method overloading and overriding §Inheritance and polymorphism l polymorphism l abstract.
OO as a language for acm l OO phrase l Mental model of key concepts.
Chapter FifteenModern Programming Languages1 A Second Look At Java.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 17: Inheritance & Behavioral.
Type Abstraction Liskov, Chapter 7. 2 Liskov Substitution Principle In any client code, if the supertype object is substituted by a subtype object, the.
Type Abstraction SWE Spring October 05Kaushik, Ammann Substitution Principle “In any client code, if supertype object is substituted.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Session 24 Chapter 12: Polymorphism. Polymorphism polymorphism comes from the Greek root for “many forms” polymorphism is about how we can use different.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Subtype Polymorphism, Subtyping vs
1 CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Polymorphism CMPS Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional.
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
Subtype Polymorphism, cont. Parametric Polymorphism and Java Generics.
Session 18 Lab 9 Re-cap, Chapter 12: Polymorphism & Using Sound in Java Applications.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Component Based Software Engineering
CSE 331 Subtyping slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Type Abstraction SWE Spring 2009.
Extending Classes.
Generics (Parametric Polymorphism)
Type Abstraction Liskov, Chapter 7.
Subtype Polymorphism, Subtyping vs
Generic programming in Java
COMPUTER 2430 Object Oriented Programming and Data Structures I
CIS 199 Final Review.
Type Abstraction SWE Spring 2013.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.

Polymorphism Constructs that can take different forms poly = many morph = shape

Types of Polymorphism Ad-hoc polymorphism similar function implementations for different types (method overloading, but not only) Subtype (inclusion) polymorphism instances of different classes related by common super class Parametric polymorphism functions that work for different types of data def plus(x, y): return x + y class A {...} class B extends A {...}; class C extends A {...}

Ad-hoc Polymorphism int plus(int x, int y) { return x + y; } string plus(string x, string y) { return x + y; } float plusfloat(float x, float y) { return x + y; }

Subtype Polymorphism First introduced in the 60s with Simula Usually associated with OOP (in some circles, polymorphism = subtyping) Principle of safe substitution (Liskov substitution principle) Note that this is behavioral subtyping, stronger than simple functional subtyping. “if S is a subtype of T, then objects of type T may be replaced with objects of type S without altering any of the desirable properties of the program.”

Behavioral Subtyping Requirements Contravariance of method arguments in subtype (from narrower to wider, e.g. Triangle to Shape) Covariance of return types in subtype (from wider to narrower, e.g. Shape to Triangle) Preconditions cannot be strengthened in subtype Postcondition cannot be weakened in subtype History constraint: state changes in subtype not possible in supertype are not allowed (Liskov’s constraint)

LSP Violations? class Thing {...} class Shape extends Thing { Shape m1(Shape a) {...} } class Triangle extends Shape Triangle m1(Shape a) {...} } class Square extends Shape Thing m1(Shape a) {...} } Java does not support contravariance of method arguments

LSP Violations? class Thing {...} class Shape extends Thing { Shape m1(Shape a) { assert(Shape.color == Color.Red);... } class Triangle extends Shape Triangle m1(Shape a) { assert(Shape.color == Color.Red); assert(Shape.nsizes == 3);... }

Parametric Polymorphism Parametric polymorphism functions that work for different types of data def plus(x, y): return x + y How to do this in statically-typed languages? int plus(int x, int y): return x + y ???

Parametric Polymorphism Parametric polymorphism for statically-typed languages introduced in ML in the 70s aka “generic functions” C++: templates Java: generics C#, Haskell: parametric types

Parametric Polymorphism Explicit Parametric Polymorphism C++ implements explicit polymorphism, in which explicit instantiation or type application to indicate how type variables are replaced with specific types in the use of a polymorphic value. Example: template T lessthan(T& x, T& y){ if( x < y) return x; else return y; } We define a template function with T as a parameter which can take any type as a parameter to the function.

Parametric Polymorphism Explicit Parametric Polymorphism Java example: /** * Generic version of the Box class. the type of value being boxed */ public class Box { // T stands for "Type" private T t; public void add(T t) { this.t = t; } public T get() { return t; } Box integerBox;... void m(Box fbox) {...}

Parametric Polymorphism Implicit parametric polymorphism Haskell uses polymorphic functions that do not need to contain types The inference algorithm computes when a function is polymorphic and computes the instantiation of type variables as needed Example: template insert(less :: (t, t) -> Bool, x :: t, [] :: [t]) = [x] insert(less, x, y:ys) = if less(x,y) then x:y:ys else y:insert(less,x,ys) Type Inference Algorithm can be simply considered as preprocessor that converts expression without type information into expression in intermediate language with templates

Video The structure of a compiler