301AA - Advanced Programming [AP-2017]

Similar documents
Principles of Programming Languages

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

301AA - Advanced Programming [AP-2017]

Polymorphism and Type Inference

Polymorphism and Type Inference

301AA - Advanced Programming [AP-2017]

Type Systems, Type Inference, and Polymorphism

Pierce Ch. 3, 8, 11, 15. Type Systems

Type Checking and Type Inference

CS558 Programming Languages

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

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

Principles of Programming Languages

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Lecture #23: Conversion and Type Inference

CS 360: Programming Languages Lecture 12: More Haskell

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

CSCI-GA Scripting Languages

Types. What is a type?

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

CS558 Programming Languages

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

CS558 Programming Languages

301AA - Advanced Programming [AP-2017]

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Lists. Michael P. Fourman. February 2, 2010

22c:111 Programming Language Concepts. Fall Types I

CS558 Programming Languages

CMSC 330: Organization of Programming Languages

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

Overloading, Type Classes, and Algebraic Datatypes

Programming Languages, Summary CSC419; Odelia Schwartz

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

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Simply-Typed Lambda Calculus

Example: Haskell algebraic data types (1)

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Introduction to Programming, Aug-Dec 2006

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

Programming Language Concepts: Lecture 14

Type Processing by Constraint Reasoning

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

301AA - Advanced Programming [AP-2017]

CSCI312 Principles of Programming Languages!

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

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

CS 320: Concepts of Programming Languages

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

COS 320. Compiling Techniques

G Programming Languages - Fall 2012

Functional Paradigm II

CONTROL FLOW ANALYSES!

Lecture 19: Functions, Types and Data Structures in Haskell

How does ML deal with +?

Programming Languages Fall 2013

G Programming Languages - Fall 2012

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

CS 345. Functions. Vitaly Shmatikov. slide 1

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

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

Back to OCaml. Summary of polymorphism. Example 1. Type inference. Example 2. Example 2. Subtype. Polymorphic types allow us to reuse code.

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Functional Programming. Pure Functional Programming

Recursive Types and Subtyping

Lecture 12: Data Types (and Some Leftover ML)

Harvard School of Engineering and Applied Sciences Computer Science 152

Data Types (cont.) Administrative Issues. Academic Dishonesty. How do we detect plagiarism? Strongly Typed Languages. Type System

More Lambda Calculus and Intro to Type Systems

The role of semantic analysis in a compiler

Subtyping. Lecture 13 CS 565 3/27/06

Programming Languages and Compilers (CS 421)

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

Polymorphism. Chapter Eight Modern Programming Languages, 2nd ed. 1

Data Types The ML Type System

CSE 431S Type Checking. Washington University Spring 2013

Advanced oo concepts Specialization of behaviour? Multiple inheritance - alternatives to. Inner classes Classes

int f(char a, char b) {

CSCC24 Functional Programming Typing, Scope, Exceptions ML

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Programming Languages Third Edition. Chapter 7 Basic Semantics

Announcements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML

Programming Languages

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

Motivation for typed languages

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

Transcription:

301AA - Advanced Programming [AP-2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP-2017-19: Type inference

Type Inference Type inference: the process of associanng a type with each symbol of a program, if possible, ensuring type safety. Recalling several relevant defininons: (Data) types Type system Type error Type safety Dynamic vs. stanc type checking 2

Data types A (data) type is a homogeneous collecnon of values, effecnvely presented, equipped with a set of operanons which manipulate these values Various perspecnves: collecnon of values from a domain (the denotanonal approach) internal structure of a bunch of data, described down to the level of a small set of fundamental types (the structural approach) collecnon of well-defined operanons that can be applied to objects of that type (the abstracnon approach) 3

Type system A type system consists of 1. The set of predefined types of the language. 2. The mechanisms which permit the defini2on of new types. 3. The mechanisms for the control (checking) of types, which include: 1. Equivalence rules which specify when two formally different types correspond to the same type. 2. Compa2bility rules specifying when a value of a one type can be used in given context. 3. Rules and techniques for type inference which specify how the language assigns a type to a complex expression based on informanon about its components (and somenmes on the context). 4. The specificanon as to whether (or which) constraints are sta2cally or dynamically checked. 4

Type errors A type error occurs when a value is used in a way that is inconsistent with its defininon ImplementaNons can react in various ways Hardware interrupt, e.g. apply fp addi1on to non-legal bit configura1on OS excepnon, e.g. segmenta1on fault when dereferencing 0 in C ConNnue execunon possibly with wrong values Examples Array out of bounds access C/C++: runnme errors Java: dynamic type error Null pointer dereferencing C/C++: run-nme errors Java: dynamic type error Haskell/ML: pointers are hidden inside datatypes Null pointer dereferences would be incorrect use of these datatypes, therefore stanc type errors 5

Type safety A language is type safe (strongly typed) when no program can violate the disnncnons between types defined in its type system That is, when no program, during its execunon, can generate an unsignalled type error Also: if code accesses data, it is handled with the type associated with the creanon and previous manipulanon of that data 6

Type checking To prevent type errors, before any operanon is performed, its operands must be typechecked to ensure that they comply with the companbility rules of the type system E.g., indexing opera1on: check that the leg operand is an array, and that the right operand is a value of the array s index type. Sta2cally typed languages: (most) type checking is done during compilanon Dynamically typed languages: type checking is done at runnme 7

StaNc vs dynamic typing In a sta2cally typed PL: all variables and expressions have fixed types (either stated by the programmer or inferred by the compiler) most operands are type-checked at compile-1me. Most PLs are called stancally typed, including Ada, C, C++, Java, Haskell, even if some type-checking is done at run-nme (e.g. access to arrays) In a dynamically typed PL: values have fixed types, but variables and expressions do not operands must be type-checked when they are computed at run-1me. Some PLs and many scripnng languages are dynamically typed, including Smalltalk, Lisp, Prolog, Perl, Python. 8

Polymorphism in Haskell Coercion Type Inference Implicit Parametric Universal Explicit Bounded Polymorphism Inclusion Covariant Invariant Contravariant Ad hoc Overriding Overloading Type classes

Universal vs Ad-hoc Polymorphism Universal polymorphism Single algorithm may be given many types Type variables (implicit or explicit) may be replaced by any type (almost -> bounded polymorphism) head::[a] a thus head::[int] Int, head:[bool] Bool,... Ad-hoc polymorphism (overloading) A single symbol may refer to more than one algorithm. Each algorithm may have different type. Choice of algorithm determined by type context. + has types Int Int Int and Float Float Float, but not t t t for arbitrary t. 10

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 funcnon 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 informanon. Infer the most general types that could have been declared. ML and Haskell are designed to make type inference feasible. 11

Why study type inference? Types and type checking Improved steadily since Algol 60 Eliminated sources of unsoundness. Become substannally more expressive. Important for modularity, reliability and compilanon Type inference Reduces syntacnc overhead of expressive types. Guaranteed to produce most general type. Increasingly used also in imperanve/oo languages. IllustraNve example of a flow-insensinve stanc analysis algorithm. 12

uhaskell Subset of Haskell to explain type inference. 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> 13

Type Inference: Basic Idea Example f x = 2 + x -- a simple declaration 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 implies that f has type Int Int f x = 2 + x -- a simple declaration > f :: Int -> Int 14

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 15

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

Constraints from ApplicaNon Nodes f x t_0 = t_1 -> t_2 FuncNon applicanon (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 applicanon (t_2 in fig) Constraint: t_0 = t_1 -> t_2 17

Constraints from AbstracNons f x = e t_0 = t_1 -> t_2 FuncNon declaranon: Type of f (t_0 in figure) must be domain range Domain is type of abstracted variable x (t_1 in fig) Range is type of funcnon body e (t_2 in fig) Constraint: t_0 = t_1 -> t_2 Idem for lambdas: f = \x -> e 18

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 19

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 20

Step 5: Determine type of declaranon 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 21

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 funcnons (tail). From form of parse tree: e.g., applicanon and abstracnon nodes. Solve constraints using unifica@on Determine types of top-level declaranons 22

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

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

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 25

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 26

Inferring Polymorphic Types Example: Step 5: Determine type of top-level declaranon 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 27

Using Polymorphic FuncNons FuncNon: f g = g 2 > f :: (Int -> t_4) -> t_4 Possible applicanons: add x = 2 + x > add :: Int -> Int f add > 4 :: Int iseven x = mod (x, 2) == 0 > iseven:: Int -> Bool f iseven > True :: Int 28

Recognizing Type Errors FuncNon: 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 29

Polymorphic Datatypes FuncNons may have mulnple clauses, and may be recursive length [] = 0 length (x:rest) = 1 + (length rest) Type inference Infer separate type for each clause Add constraint that all clauses must have the same type Recursive calls: funcnon has same type as its defininon 30

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

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

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 33

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 34

MulNple Clauses FuncNon with mulnple 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 equanng types of two clauses > append :: ([t_1], [t_1]) -> [t_1] 35

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] FuncNons 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 36

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, A bit also in Java. 37

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 exponennal- Nme complete Usually linear in pracnce though Running Nme is exponennal in the depth of polymorphic declaranons 38