INF 3110 - 2008 Polymorphism and Type Inference Fatemeh Kazemeyni Department of Informatics – University of Oslo Based on John C. Mitchell’s.

Slides:



Advertisements
Similar presentations
Sml2java a source to source translator Justin Koser, Haakon Larsen, Jeffrey Vaughan PLI 2003 DP-COOL.
Advertisements

Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Modern Programming Languages, 2nd ed.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
Type Inference David Walker COS 320. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Chapter EightModern Programming Languages1 Polymorphism.
CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Type Checking- Contd Compiler Design Lecture (03/02/98) Computer Science Rensselaer Polytechnic.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
Approaches to Typing Programming Languages Robert Dewar.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Templates. Example… A useful routine to have is void swap(int &a, int &b){ int tmp = a; a = b; b = tmp; }
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Types and Type Inference Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Reading: “Concepts in Programming Languages”, Revised Chapter 6 - handout.
FALL 2001ICOM Lecture 21 ICOM 4015 Advanced Programming Lecture 2 Procedural Abstraction Reading: LNN Chapter 4, 14 Prof. Bienvenido Velez.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
Polymorphism Lecture-10. Print A Cheque A Report A Photograph PrintCheque() PrintReport() PrintPhoto() Printing.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Types John Mitchell CS 242 Reading: Chapter 6. Announcements uHomework 1 due today Turn in at the end of class, OR Turn in by 5PM to the homework drop.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
Slide 1 Vitaly Shmatikov CS 345 Types and Parametric Polymorphism.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Slide 1 Dr. Mohammad El-Ramly Fall 2010 Set 7- II - Functions Slides by Vitaly Shmatikov Cairo University Faculty of Computers and Information CS317 Concepts.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Object Lifetime and Pointers
Functional Programming
Type Checking and Type Inference
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
Chapter 20 Generic Classes and Methods
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Case Study: ML John Mitchell Reading: Chapter 5.
ML’s Type Inference and Polymorphism
ML’s Type Inference and Polymorphism
CSE-321 Programming Languages Introduction to Functional Programming
CS 242 Types John Mitchell.
CS 242 Types John Mitchell Reading: Chapter 6.
ML’s Type Inference and Polymorphism
ML’s Type Inference and Polymorphism
Modern Programming Languages
Presentation transcript:

INF Polymorphism and Type Inference Fatemeh Kazemeyni Department of Informatics – University of Oslo Based on John C. Mitchell’s slides (Stanford U.),

INF Inf ML III 2 ML lectures : The Algol Family and ML (Mitchell’s chap. 5 + more) : More on ML & types (chap. 5, 6, more) : More on Types: Type Inference and Polymorphism (chap. 6) : Control in sequential languages, Exceptions and Continuations (chap. 8)

INF Inf ML III 3 Outline uPolymorphism uType inference uType declaration

INF Inf ML III 4 Revision -Types u A type is a collection of computational entities sharing some common property uUses for types Program organization and documentation Identify and prevent errors Support optimization uType safety A Prog. Lang. is type safe if no program can violate its type distinction Unsafe elements: –Type casts (a value of one type used as another type) –Pointer arithmetic –Explicit deallocation and dangling pointers u Static/compile-time vs. dynamic/run-time checking

INF Inf ML III 5 Outline uPolymorphisms parametric polymorphism ad hoc polymorphism subtype polymorphism uType inference uType declaration

INF Inf ML III 6 Polymorphism: three forms uParametric polymorphism Single function may be given (infinitely) many types The type expression involves type variables Example: in ML the identity function is polymorphic - fn x => x; > val it = fn : 'a -> 'a An instance of the type scheme may give: int  int, bool  bool, char  char, int*string*int  int*string*int, (int  real)  (int  real),... Type variable may be replaced by any type This pattern is called type scheme

INF Inf ML III 7 Polymorphism: three forms uParametric polymorphism Single function may be given (infinitely) many types The type expression involves type variables Example: polymorphic sort - sort : ('a * 'a -> bool) * 'a list -> 'a list - sort((op<),[1,7,3]); > val it = [1,3,7] : int list

INF Inf ML III 8 Polymorphism: three forms (cont.) uAd-hoc polymorphism (or Overloading) A single symbol has two (or more) meanings (it refers to more than one algorithm) Each algorithm may have different type Overloading is resolved at compile time Choice of algorithm determined by type context Example: In ML, + has 2 different associated implementations: it can have types int*int  int and real*real  real, no others

INF Inf ML III 9 Polymorphism: three forms (cont.) uSubtype polymorphism The subtype relation allows an expression to have many possible types Polymorphism not through type parameters, but through subtyping: –If method m accept any argument of type t then m may also be applied to any argument from any subtype of t REMARK 1: In OO, the term “polymorphism” is usually used to denote subtype polymorphism (ex. Java, OCAML, etc) REMARK 2: ML does not support subtype polymorphism!

INF Inf ML III 10 Parametric polymorphism uExplicit: The program contains type variables Often involves explicit instantiation to indicate how type variables are replaced with specific types Example: C++ templates uImplicit: Programs do not need to contain types The type inference algorithm determines when a function is polymorphic and instantiate the type variables as needed Example: ML polymorphism

INF Inf ML III 11 Parametric Polymorphism: ML vs. C++ uC++ function template Declaration gives type of funct. arguments and result Place declaration inside a template to define type variables Function application: type checker does instantiation automatically uML polymorphic function Declaration has no type information Type inference algorithm –Produce type expression with variables –Substitute for variables as needed ML also has module system with explicit type parameters

INF Inf ML III 12 Example: swap two values uC++ uInstantiations: int i,j; … swap(i,j); //use swap with T replaced with int float a,b;… swap(a,b); //use swap with T replaced with float string s,t;… swap(s,t); //use swap with T replaced with string void swap (int& x, int& y){ int tmp=x; x=y; y=tmp; } template void swap(T& x, T& y){ T tmp=x; x=y; y=tmp; }

INF Inf ML III 13 Example: swap two values uML - fun swap(x,y) = let val z = !x in x := !y; y := z end; > val swap = fn : 'a ref * 'a ref -> unit - val a = ref 3 ; val b = ref 7 ; > val a = ref 3 : int ref > val b = ref 7 : int ref - swap(a,b) ; > val it = () : unit - !a ; > val it = 7 : int Remark: Declarations look similar in ML and C++, but compile code is very different!

INF Inf ML III 14 Parametric Polymorphism: Implementation uC++ Templates are instantiated at program link time Swap template may be stored in one file and the program(s) calling swap in another Linker duplicates code for each type of use uML Swap is compiled into one function (no need for different copies!) Typechecker determines how function can be used

INF Inf ML III 15 uWhy the difference? C++ arguments passed by reference (pointer), but local variables (e.g. tmp, of type T) are on stack –Compiled code for swap depends on the size of type T => Need to know the size for proper addressing ML uses pointers in parameter passing (uniform data representation) –It can access all necessary data in the same way, regardless of its type; Pointers are the same size anyway uComparison C++: more effort at link time and bigger code ML: run more slowly, but give smaller code and avoids linking problems Global link time errors can be more difficult to find out than local compile errors Parametric Polymorphism: Implementation

INF Inf ML III 16 ML overloading uSome predefined operators are overloaded + has types int*int  int and real*real  real uUser-defined functions must have unique type - fun plus(x,y) = x+y; (compiled to int or real function, not both) In SML/NJ: - fun plus(x,y) = x+y; > val plus = fn : int * int -> int If you want to have plus = fn : real * real -> real you must provide the type: - fun plus(x:real,y:real) = x+y;

INF Inf ML III 17 ML overloading (cont.) uWhy is a unique type needed? Need to compile code implies need to know which + (different algorithm for distinct types) Overloading is resolved at compile time –The compiler must choose one algorithm among all the possible ones –Automatic conversion is possible (not in ML!) –But in e.g. Java : consider the expression (1 + “foo”) ; Efficiency of type inference – overloading complicates type checking Overloading of user-defined functions is not allowed in ML!

INF Inf ML III 18 Parametric polymorphism vs. overloading uParametric polymorphism One algorithm for arguments of many different types uOverloading Different algorithms for each type of argument

INF Inf ML III 19 Outline uPolymorphisms uType inference uType declaration

INF Inf ML III 20 Type checking and type inference uType checking: The process of checking whether the types declared by the programmer “agrees” with the language constraints/ requirement uType inference: The process of determining the type of an expression based on information given by (some of) its symbols/sub-expressions Provides a flexible form of compile-time/static type checking uType inference naturally leads to polymorphism, since the inference uses type variables and some of these might not be resolved in the end ML is designed to make type inference tractable (one of the reason for not having subtypes in ML!)

INF Inf ML III 21 Type checking and type inference uStandard type checking int f(int x) { return x+1; }; int g(int y) { return f(y+1)*2;}; Look at body of each function and use declared types of identifies to check agreement uType inference int f(int x) { return x+1; }; int g(int y) { return f(y+1)*2;}; Look at code without type information and figure out what types could have been declared

INF Inf ML III 22 Type inference algorithm: s ome history uUsually known as Milner-Hindley algorithm u1958: Type inference algorithm given by H.B. Curry and Robert Feys for the typed lambda calculus u1969: Roger Hindley extended the algorithm and proved that it gives the most general type u1978: Robin Milner -independently of Hindley- provided an equivalent algorithm (for ML) u1985: Luis Damas proved its completeness and extended it with polymorphism

INF Inf ML III 23 ML Type Inference uExample - fun f(x) = 2+x; > val f = fn : int  int uHow does this work? + has two types: int*int  int, real*real  real 2 : int, has only one type This implies + : int*int  int From context, need x: int Therefore f(x:int) = 2+x has type int  int Overloaded + is unusual - Most ML symbols have unique type In many cases, unique type may be polymorphic

INF Inf ML III 24 ML Type Inference uExample -fun f(g,h) = g(h(0)); uHow does this work? h must have the type: int  ´a, since 0 is of type int this implies that g must have the type: ´a  ´b Then f must have the type: (´a  ´b) * (int  ´a)  ´b

INF Inf ML III 25 The type inference algorithm uExample - fun f(x) = 2+x; (val f = fn x => 2+x ;) > val f = fn : int  int f(x) = 2+x equiv f = x. (2+x) equiv f = x. ((plus 2) x)

INF Inf ML III 26 Detour: the -calculus u Two ways to construct terms: Application: F A (or F(A) ) Abstraction: x.e If e is an expression on x, then x.e is a function Ex: e = 3x+4. x.e = x.(3x+4) ( fn x => (3x+4) ) compare with “school book” notation: if f(x) = 3x+4 then f = x.(3x+4) uRules for computation x.(3x+4)  y.(3y+4) (  – conversion) ( x.(3x+4)) 2( x.(3x+4)) 2  (3*2) + 4 ( x.(3x+4)) 2  (3*2) + 4  10 (  – reduction)

INF Inf ML III 27 Application and Abstraction uApplication f x f must have function type domain  range domain of f must be type of argument x (b) the range of f is the result type (c) thus we know that a = b  c uAbstraction x.e (fn x => e) The type of x.e is a function type domain  range the domain is the type of the variable x (a) the range is the type of the function body e (b) x e : b: a : b : c (a = b  c ): a  fx f x x.e : c

The type inference algorithm uExample -fun f(x) = 2+x; -(val f = fn x => 2+x ;) > val f = fn : int  int uHow does this Generate constraints: int  int = u  s r = u  s int  int = u  s int  int =u  s 3. Solve by unification/substitution = int  int Graph for x. ((+ 2) x) f(x) = 2+x equiv f = x. (2+x) equiv f = x. ((plus 2) x) 1. Assign types to expressions :u int  int  int real  real  real :int :s :r

INF Inf ML III 29 Types with type variables uExample - fun f(g) = g(2); > val f = fn : (int  ’a)  ’a uHow does this work? g 1. Assign types to leaves : int: s 2. Propagate to internal nodes and generate constraints t (s= int  t) stst 3. Solve by substitution = (int  t)  t Graph for g. (g 2) ’a is syntax for “type variable” (t in the graph)

INF Inf ML III 30 Use of Polymorphic Function uFunction - fun f(g) = g(2); > val f = fn : (int  ’a)  ’a uPossible applications g may be the function: - fun add(x) = 2+x; > val add = fn : int  int Then: - f(add); > val it = 4 : int g may be the function: - fun isEven(x) =...; > val it = fn : int  bool Then: - f(isEven); > val it = true : bool

INF Inf ML III 31 Recognizing type errors uFunction - fun f(g) = g(2); > val f = fn : (int  ’a)  ’a uIncorrect use - fun not(x) = if x then false else true; > val not = fn : bool  bool - f(not); Why? Type error: cannot make bool  bool = int  ’a

INF Inf ML III 32 Another type inference example uFunction Definition - fun f(g,x) = g(g(x)); Solve by substitution = (v  v)*v  g g Assign types to leaves : t : s Propagate to internal nodes and generate constraints: s = t  u, s = u  v t=u,u=v t=v v (s = u  v) s*t  v u (s = t  u) Graph for  g,x . g(g x)

INF Inf ML III 33 Multiple clause function uDatatype with type variable - datatype ’a list = nil | cons of ’a*(’a list); > nil : ’a list > cons : ’a*(’a list)  ’a list u Polymorphic function - fun append(nil,l) = l | append (x::xs,l) = x:: append(xs,l); > val append= fn: ‘a list * ‘a list  ’a list uType inference Infer separate type for each clause append: ‘a list * ‘b -> ‘b append: ‘a list * ‘b -> ‘a list Combine by making the two types equal (if necessary) ‘b = ‘a list

INF Inf ML III 34 Main points about type inference uCompute type of expression Does not require type declarations for variables Find most general type by solving constraints Leads to polymorphism uStatic type checking without type specifications uMay lead to better error detection than ordinary type checking Type may indicate a programming error even if there is no type error (example following slide).

INF Inf ML III 35 Information from type inference uAn interesting function on lists - fun reverse (nil) = nil | reverse (x::lst) = reverse(lst); uMost general type > reverse : ’a list  ’b list uWhat does this mean? Since reversing a list does not change its type, there must be an error in the definition x is not used in “reverse(lst)”!

INF Inf ML III 36 Outline uPolymorphisms uType inference uType declaration

INF Inf ML III 37 Type declaration uTransparent: alternative name to a type that can be expressed without this name uOpaque: new type introduced into the program, different to any other ML has both forms of type declaration

INF Inf ML III 38 Type declaration: Examples uTransparent (”type” declaration) Since Fahrenheit and Celsius are synonyms for real, the function may be applied to a real: - type Celsius = real; - type Fahrenheit = real; - toCelsius(60.4); > val it = : Celsius More information: - fun toCelsius(x: Fahrenheit) = ((x-32.0)*0.5556): Celsius; > val toCelsius = fn : Fahrenheit  Celsius - fun toCelsius(x) = ((x-32.0)*0.5556); > val toCelsius = fn : real  real

INF Inf ML III 39 Type declaration: Examples uOpaque (”datatype” declaration) A and B are different types Since B declaration follows A decl.: C has type int  B Hence: - fun f(x:A) = x: B; > Error: expression doesn't match constraint [tycon mismatch] expression: A constraint: B in expression: x: B - datatype A = C of int; - datatype B = C of int;

INF Inf ML III 40 Equality on Types Two forms of type equality: uName type equality: Two type names are equal in type checking only if they are the same name uStructural type equality: Two type names are equal if the types they name are the same Example: Celsius and Fahrenheit are structurally equal although their names are different

INF Inf ML III 41 Remarks – Further reading uMore on subtype polymorphism (Java): Mitchell’s Section

INF Inf ML III 42 ML lectures : The Algol Family and ML (Mitchell’s chap. 5 + more) : More on ML & types (chap. 5, 6, more) : More on Types: Type Inference and Polymorphism (chap. 6) : Control in sequential languages, Exceptions and Continuations (chap. 8)