Principles of Programming Languages

Similar documents
301AA - Advanced Programming [AP-2017]

Types and Type Inference

Types and Type Inference

Types, Type Inference and Unification

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

Principles of Programming Languages

Polymorphism and Type Inference

Polymorphism and Type Inference

Type Processing by Constraint Reasoning

Lecture 19: Functions, Types and Data Structures in Haskell

Overloading, Type Classes, and Algebraic Datatypes

CSc 372 Comparative Programming Languages

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

Exercise 1 ( = 24 points)

How does ML deal with +?

Programming Language Concepts: Lecture 14

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

CS 360: Programming Languages Lecture 12: More Haskell

Exercises on ML. Programming Languages. Chanseok Oh

Standard ML. Curried Functions. ML Curried Functions.1

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen

Language Sequence. The Algol Family and ML. Algol 60 Sample. Algol 60. Some trouble spots in Algol 60. Algol Joke. John Mitchell

Principles of Programming Languages

Haskell Overview II (2A) Young Won Lim 8/9/16

Functional Paradigm II

Higher Order Functions in Haskell

Programming Languages Fall 2013

Haskell Types, Classes, and Functions, Currying, and Polymorphism

Advanced features of Functional Programming (Haskell)

Type Systems, Type Inference, and Polymorphism

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Lambda Calculus and Type Inference

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Introduction to Haskell

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

An introduction introduction to functional functional programming programming using usin Haskell

CS 320: Concepts of Programming Languages

Haskell Overview III (3A) Young Won Lim 10/4/16

301AA - Advanced Programming [AP-2017]

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Introduction to Programming, Aug-Dec 2006

Type system. Type theory. Haskell type system. EDAN40: Functional Programming Types and Type Classes (revisited)

CSE 3302 Programming Languages Lecture 8: Functional Programming

CSCE 314 Programming Languages

Haskell Overview II (2A) Young Won Lim 8/23/16

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

301AA - Advanced Programming [AP-2017]

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Type Checking and Type Inference

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Exercise 1 ( = 18 points)

Imperative languages

Introduction to OCaml

The Algol family and ML

301AA - Advanced Programming

Functional Paradigm II

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

SMURF Language Reference Manual Serial MUsic Represented as Functions

Faith, Evolution, and Programming Languages. Philip Wadler University of Edinburgh

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Haskell An Introduction

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

CSE341 Autumn 2017, Midterm Examination October 30, 2017

Datatype declarations

CSCI-GA Final Exam

Lists. Michael P. Fourman. February 2, 2010

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions

Type Inference. COS 326 David Walker Princeton University

CSE 130, Fall 2006: Final Examination

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

CS 345. Functions. Vitaly Shmatikov. slide 1

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

An introduction to functional programming. July 23, 2010

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh

A general introduction to Functional Programming using Haskell

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

CS 440: Programming Languages and Translators, Spring 2019 Mon

CSC324 Principles of Programming Languages

Haskell Types COMP360

Exercise 1 ( = 22 points)

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Principles of Programming Languages

CS 457/557: Functional Languages

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

Programming Languages (CS 550) Simple Query Language*

Programming with Math and Logic

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures

Lecture #23: Conversion and Type Inference

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

SML A F unctional Functional Language Language Lecture 19

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Chapter 3 Linear Structures: Lists

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Transcription:

Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp-16/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 24! Type inference in ML / Haskell 1

Type Checking vs Type Inference Standard type checking: int f(int x) { return x+1; }; int g(int y) { return f(y+1)*2; }; Examine body of each funcfon Use declared types to check agreement Type inference: int f(int x) { return x+1; }; int g(int y) { return f(y+1)*2; }; Examine code without type informafon. Infer the most general types that could have been declared. ML and Haskell are designed to make type inference feasible. 2

Why study type inference? Types and type checking Improved steadily since Algol 60 Eliminated sources of unsoundness. Become substanfally more expressive. Important for modularity, reliability and compilafon Type inference Reduces syntacfc overhead of expressive types. Guaranteed to produce most general type. Widely regarded as important language innovafon. IllustraFve example of a flow-insensifve stafc analysis algorithm. 3

History Original type inference algorithm Invented by Haskell Curry and Robert Feys for the simply typed lambda calculus in 1958 In 1969, Hindley extended the algorithm to a richer language and proved it always produced the most general type In 1978, Milner independently developed equivalent algorithm, called algorithm W, during his work designing ML. In 1982, Damas proved the algorithm was complete. Currently used in many languages: ML, Ada, Haskell, C# 3.0, F#, Visual Basic.Net 9.0. Have been plans for Fortress, Perl 6, C+ +0x, 4

uhaskell Subset of Haskell to explain type inference. Haskell and ML both have overloading Will do not consider overloading now <decl> ::= <name> <pat> = <exp> <pat> ::= Id (<pat>, <pat>) <pat> : <pat> [] <exp> ::= Int Bool [] Id (<exp>) <exp> <op> <exp> <exp> <exp> (<exp>, <exp>) if <exp> then <exp> else <exp> 5

Type Inference: Basic Idea Example f x = 2 + x > f :: Int -> Int What is the type of f? + has type: Int Int Int (with overloading would be Num a => a a a) 2 has type: Int Since we are applying + to x we need x :: Int Therefore f x = 2 + x has type Int Int 6

Step 1: Parse Program Parse program text to construct parse tree. f x = 2 + x Binary @-nodes to represent application Ternary Fun-node for function definitions Infix operators are converted to Curried function application during parsing: 2 + x è (+) 2 x 7

Step 2: Assign type variables to nodes f x = 2 + x Variables are given same type as binding occurrence. 8

Constraints from ApplicaFon Nodes f x t_0 = t_1 -> t_2 FuncFon applicafon (apply f to x) Type of f (t_0 in figure) must be domain range. Domain of f must be type of argument x (t_1 in fig) Range of f must be result of applicafon (t_2 in fig) Constraint: t_0 = t_1 -> t_2 9

Constraints from AbstracFons f x = e t_0 = t_1 -> t_2 FuncFon declarafon: Type of f (t_0 in figure) must domain range Domain is type of abstracted variable x (t_1 in fig) Range is type of funcfon body e (t_2 in fig) Constraint: t_0 = t_1 -> t_2 10

Step 3: Add Constraints f x = 2 + x t_0 = t_1 -> t_6 t_4 = t_1 -> t_6 t_2 = t_3 -> t_4 t_2 = Int -> Int -> Int t_3 = Int 11

Step 4: Solve Constraints t_0 = t_1 -> t_6 t_4 = t_1 -> t_6 t_2 = t_3 -> t_4 t_2 = Int -> Int -> Int t_3 = Int t_0 = t_1 -> t_6 t_4 = t_1 -> t_6 t_4 = Int -> Int t_2 = Int -> Int -> Int t_3 = Int t_3 -> t_4 = Int -> (Int -> Int) t_3 = Int t_4 = Int -> Int t_1 -> t_6 = Int -> Int t_0 = Int -> Int t_1 = Int t_6 = Int t_4 = Int -> Int t_2 = Int -> Int -> Int t_3 = Int t_1 = Int t_6 = Int 12

Step 5: Determine type of declarafon t_0 = Int -> Int t_1 = Int t_6 = Int t_4 = Int -> Int t_2 = Int -> Int -> Int t_3 = Int f x = 2 + x > f :: Int -> Int 13

Type Inference Algorithm Parse program to build parse tree Assign type variables to nodes in tree Generate constraints: From environment: constants (2), built-in operators (+), known funcfons (tail). From form of parse tree: e.g., applicafon and abstracfon nodes. Solve constraints using unifica,on Determine types of top-level declarafons 14

Inferring Polymorphic Types Example: Step 1: Build Parse Tree f g = g 2 > f :: (Int -> t_4) -> t_4 15

Inferring Polymorphic Types Example: Step 2: Assign type variables f g = g 2 > f :: (Int -> t_4) -> t_4 16

Inferring Polymorphic Types Example: Step 3: Generate constraints t_0 = t_1 -> t_4 t_1 = t_3 -> t_4 t_3 = Int f g = g 2 > f :: (Int -> t_4) -> t_4 17

Inferring Polymorphic Types Example: Step 4: Solve constraints t_0 = t_1 -> t_4 t_1 = t_3 -> t_4 t_3 = Int f g = g 2 > f :: (Int -> t_4) -> t_4 t_0 = (Int -> t_4) -> t_4 t_1 = Int -> t_4 t_3 = Int 18

Inferring Polymorphic Types Example: Step 5: Determine type of top-level declarafon Unconstrained type variables become polymorphic types. f g = g 2 > f :: (Int -> t_4) -> t_4 t_0 = (Int -> t_4) -> t_4 t_1 = Int -> t_4 t_3 = Int 19

Using Polymorphic FuncFons FuncFon: f g = g 2 > f :: (Int -> t_4) -> t_4 Possible applicafons: add x = 2 + x > add :: Int -> Int f add > 4 :: Int iseven x = mod (x, 2) == 0 > iseven:: Int -> Bool f iseven > True :: Int 20

Recognizing Type Errors FuncFon: f g = g 2 > f :: (Int -> t_4) -> t_4 Incorrect use not x = if x then True else False > not :: Bool -> Bool f not > Error: operator and operand don t agree operator domain: Int -> a operand: Bool -> Bool Type error: cannot unify Bool Bool and Int t 21

Another Example Example: Step 1: Build Parse Tree f (g,x) = g (g x) > f :: (t_8 -> t_8, t_8) -> t_8 22

Another Example Example: Step 2: Assign type variables f (g,x) = g (g x) > f :: (t_8 -> t_8, t_8) -> t_8 23

Another Example Example: Step 3: Generate constraints f (g,x) = g (g x) > f :: (t_8 -> t_8, t_8) -> t_8 t_0 = t_3 -> t_8 t_3 = (t_1, t_2) t_1 = t_7 -> t_8 t_1 = t_2 -> t_7 24

Another Example Example: Step 4: Solve constraints f (g,x) = g (g x) > f :: (t_8 -> t_8, t_8) -> t_8 t_0 = t_3 -> t_8 t_3 = (t_1, t_2) t_1 = t_7 -> t_8 t_1 = t_2 -> t_7 t_0 = (t_8 -> t_8, t_8) -> t_8 25

Another Example Example: Step 5: Determine type of f f (g,x) = g (g x) > f :: (t_8 -> t_8, t_8) -> t_8 t_0 = t_3 -> t_8 t_3 = (t_1, t_2) t_1 = t_7 -> t_8 t_1 = t_2 -> t_7 t_0 = (t_8 -> t_8, t_8) -> t_8 26

Polymorphic Datatypes FuncFons may have mulfple clauses length [] = 0 length (x:rest) = 1 + (length rest) Type inference Infer separate type for each clause Combine by adding constraint that all clauses must have the same type Recursive calls: funcfon has same type as its definifon 27

Type Inference with Datatypes Example: Step 1: Build Parse Tree length (x:rest) = 1 + (length rest) 28

Type Inference with Datatypes Example: length (x:rest) = 1 + (length rest) Step 2: Assign type variables 29

Type Inference with Datatypes Example: length (x:rest) = 1 + (length rest) Step 3: Generate constraints t_0 = t_3 -> t_10 t_3 = t_2 t_3 = [t_1] t_6 = t_9 -> t_10 t_4 = t_5 -> t_6 t_4 = Int -> Int -> Int t_5 = Int t_0 = t_2 -> t_9 30

Type Inference with Datatypes Example: Step 3: Solve Constraints length (x:rest) = 1 + (length rest) t_0 = t_3 -> t_10 t_3 = t_2 t_3 = [t_1] t_6 = t_9 -> t_10 t_4 = t_5 -> t_6 t_4 = Int -> Int -> Int t_5 = Int t_0 = t_2 -> t_9 t_0 = [t_1] -> Int 31

MulFple Clauses FuncFon with mulfple clauses Infer type of each clause First clause: append ([],r) = r append (x:xs, r) = x : append (xs, r) > append :: ([t_1], t_2) -> t_2 Second clause: > append :: ([t_3], t_4) -> [t_3] Combine by equafng types of two clauses > append :: ([t_1], [t_1]) -> [t_1] 32

Most General Type Type inference produces the most general type map (f, [] ) = [] map (f, x:xs) = f x : map (f, xs) > map :: (t_1 -> t_2, [t_1]) -> [t_2] FuncFons may have many less general types > map :: (t_1 -> Int, [t_1]) -> [Int] > map :: (Bool -> t_2, [Bool]) -> [t_2] > map :: (Char -> Int, [Char]) -> [Int] Less general types are all instances of most general type, also called the principal type 33

Type Inference with overloading In presence of overloading (Type Classes), type inference infers a qualified type Q => T T is a Hindley Milner type, inferred as usual Q is set of type class predicates, called a constraint Consider the example funcfon: example z xs = case xs of [] -> False (y:ys) -> y > z (y==z && ys == [z]) Type T is a -> [a] -> Bool Constraint Q is { Ord a, Eq a, Eq [a]} Ord a because y>z Eq a because y==z Eq [a] because ys == [z] 34

Simplifying Type Constraints Constraint sets Q can be simplified: Eliminate duplicates {Eq a, Eq a} simplifies to {Eq a} Use an instance declara<on If we have instance Eq a => Eq [a], then {Eq a, Eq [a]} simplifies to {Eq a} Use a class declara<on If we have class Eq a => Ord a where..., then {Ord a, Eq a} simplifies to {Ord a} Applying these rules, {Ord a, Eq a, Eq[a]} simplifies to {Ord a} 35

Type Inference with overloading Puong it all together: example z xs = case xs of [] -> False (y:ys) -> y > z (y==z && ys ==[z]) T = a -> [a] -> Bool Q = {Ord a, Eq a, Eq [a]} Q simplifies to {Ord a} example :: {Ord a} => a -> [a] -> Bool 36

Complexity of Type Inference Algorithm When Hindley/Milner type inference algorithm was developed, its complexity was unknown In 1989, Kanellakis, Mairson, and Mitchell proved that the problem was exponenfal- Fme complete Usually linear in pracfce though Running Fme is exponenfal in the depth of polymorphic declarafons 37