Lecture #23: Conversion and Type Inference

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

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

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Type Checking and Type Inference

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

Types-2. Polymorphism

CSCI-GA Scripting Languages

CS558 Programming Languages

Overloading, Type Classes, and Algebraic Datatypes

CS558 Programming Languages

CS558 Programming Languages

l e t print_name r = p r i n t _ e n d l i n e ( Name : ^ r. name)

CSE 431S Type Checking. Washington University Spring 2013

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

CS-XXX: Graduate Programming Languages. Lecture 17 Recursive Types. Dan Grossman 2012

Lecture 19: Functions, Types and Data Structures in Haskell

CS 11 Haskell track: lecture 1

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

Static Checking and Type Systems

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

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Introducing Scala-like function types into Java-TX

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

CSE-505: Programming Languages. Lecture 20.5 Recursive Types. Zach Tatlock 2016

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

Semantic Processing (Part 2)

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

Type Systems, Type Inference, and Polymorphism

Types and Type Inference

Programming Languages and Compilers (CS 421)

User-Defined Algebraic Data Types

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

Types, Type Inference and Unification

CSCE 314 Programming Languages. Type System

Types and Programming Languages. Lecture 5. Extensions of simple types

Types. What is a type?

Currying fun f x y = expression;

CMSC 330: Organization of Programming Languages

VARIABLES AND TYPES CITS1001

Polymorphism and Type Inference

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Intermediate Code Generation Part II

Lecture 12: Data Types (and Some Leftover ML)

Programming Languages

Programming Languages Fall 2013

Lecture Set 4: More About Methods and More About Operators

Programming Languages Lecture 14: Sum, Product, Recursive Types

Lecture Set 4: More About Methods and More About Operators

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

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

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

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

Type Systems. Seman&cs. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

An introduction introduction to functional functional programming programming using usin Haskell

Conversions and Overloading : Overloading

Programming Language Concepts, CS2104 Lecture 7

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

Programming Languages

Harvard School of Engineering and Applied Sciences Computer Science 152

Types and Type Inference

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Overview. Elements of Programming Languages. Another example. Consider the humble identity function

Programming Languages

Mini-ML. CS 502 Lecture 2 8/28/08

IA010: Principles of Programming Languages

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

To figure this out we need a more precise understanding of how ML works

ML 4 Unification Algorithm

CS 320: Concepts of Programming Languages

Functional Logic Programming Language Curry

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

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

Semantics of programming languages

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus

Lambda Calculus and Type Inference

F28PL1 Programming Languages. Lecture 12: Standard ML 2

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

Semantic Processing (Part 2)

Topic 9: Type Checking

Topic 9: Type Checking

CSE 3302 Programming Languages Lecture 8: Functional Programming

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

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

Type Checking. Error Checking

Unifying Nominal and Structural Ad-Hoc Polymorphism

Programming Language Concepts: Lecture 14

CS 457/557: Functional Languages

Lecture 1. Types, Expressions, & Variables

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

Introduction to Lambda Calculus. Lecture 7 CS /08/09

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

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

TYPE INFERENCE. François Pottier. The Programming Languages Mentoring ICFP August 30, 2015

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Subtyping. Lecture 13 CS 565 3/27/06

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

Transcription:

Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 1

In Java, this is legal: Object x = "Hello"; Conversion vs. Subtyping Can explain by saying that static type of string literal is a subtype of Object. That is, any String is an Object. However, Java calls the assignment to x a widening reference conversion. Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 2

Integer Conversions One can also write: int x = c ; float y = x; The relationship between char and int, or int and float not generally called subtyping. Instead, these are conversions (or coercions), implying there might be some change in value or representation. In fact, in case of int to float, can lose information (example?) Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 3

Conversions: Implicit vs. Explicit With exception of int to float and long to double, Java uses general rule: Widening conversions do not require explicit casts. Narrowing conversions do. A widening conversion converts a smaller type to a larger (i.e., one whose values are a superset). A narrowing conversion goes in the opposite direction. Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 4

Conversion Examples Thus, Object x =... String y =... int a = 42; short b = 17; x = y; a = b; y = x; b = a; x = (Object) y; a = (int) b; y = (String) x; b = (short) a; // { OK} // { ERRORS} // { OK} // { OK} // { OK, but may cause exception} // { OK, but may lose information} Possibility of implicit coercion can complicate type-matching rules (see C++). Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 5

Typing In the Language ML Examples from the language ML: fun map f [] = [] map f (a :: y) = (f a) :: (map f y) fun reduce f init [] = init reduce f init (a :: y) = reduce (f init a) y fun count [] = 0 count ( :: y) = 1 + count y fun addt [] = 0 addt ((a,,c) :: y) = (a+c) :: addt y Despite lack of explicit types here, this language is statically typed! Compiler will reject the calls map 3 [1, 2] and reduce (op +) [] [3, 4, 5]. Does this by deducing types from their uses. Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 6

Type Inference In simple case: fun add [] = 0 add (a :: L) = a + add L compiler deduces that add has type int list int. Uses facts that (a) 0 is an int, (b) [] and a::l are lists (:: is cons), (c) + yields int. More interesting case: fun count [] = 0 count ( :: y) = 1 + count y ( means don t care or wildcard ). In this case, compiler deduces that count has type α list int. Here, α is a type parameter (we say that count is polymorphic). Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 7

Doing Type Inference Given a definition such as fun add [] = 0 add (a :: L) = a + add L First give each named entity here an unbound type parameter as its type: add : α, a : β, L : γ. Now use the type rules of the language to give types to everything and to relate the types: 0: int, []: δ list. Since add is function and applies to int, must be that α = ι κ, and ι = δ list etc. Gives us a large set of type equations, which can be solved to give types. Solving involves pattern matching, known formally as type unification. Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 8

Type Expressions For this lecture, a type expression can be A primitive type (int, bool); A type variable (today we ll use ML notation: a, b, c 1, etc.); The type constructor T list, where T is a type expression; A function type D C, where D and C are type expressions. Will formulate our problems as systems of type equations between pairs of type expressions. Need to find the substitution Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 9

Solving Simple Type Equations Simple example: solve a list = int list Easy: a = int. How about this: a list = b list list; b list = int list Also easy: a = int list; b = int. On the other hand: a list = b b is unsolvable: lists are not functions. Also, if we require finite solutions, then a = b list; b = a list is unsolvable. Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 10

Most General Solutions Rather trickier: a list= b list list Clearly, there are lots of solutions to this: e.g, a = int list; b = int a = (int int) list; b = int int etc. But prefer a most general solution that will be compatible with any possible solution. Any substitution for a must be some kind of list, and b must be the type of element in a, but otherwise, no constraints Leads to solution a = b list where b remains a free type variable. In general, our solutions look like a bunch of equations a i = T i, where the T i are type expressions and none of the a i appear in any of the T s. Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 11

Finding Most-General Solution by Unification To unify two type expressions is to find substitutions for all type variables that make the expressions identical. The set of substitutions is called a unifier. Represent substitutions by giving each type variable, τ, a binding to some type expression. Initially, each variable is unbound. Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 12

For any type expression, define binding(t) = Now proceed recursively: Unification Algorithm binding(t ), if T is a type variable bound to T T, otherwise unify (T1,T2): T1 = binding(t1); T2 = binding(t2); if T1 = T2: return true; if T1 is a type variable and does not appear in T2: bind T1 to T2; return true if T2 is a type variable and does not appear in T1: bind T2 to T1; return true if T1 and T2 are S1 list and S2 list: return unify (S1,S2) if T1 and T2 are D1 C1 and D2 C2: return unify(d1,d2) and unify(c1,c2) else: return false Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 13

Try to solve Example of Unification b list= a list; a b = c; c bool= (bool bool) bool We unify both sides of each equation (in any order), keeping the bindings from one unification to the next. a: b: c: Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 14

Try to solve Example of Unification b list= a list; a b = c; c bool= (bool bool) bool We unify both sides of each equation (in any order), keeping the bindings from one unification to the next. a: Unify b list, a list: b: c: Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 14

Try to solve Example of Unification b list= a list; a b = c; c bool= (bool bool) bool We unify both sides of each equation (in any order), keeping the bindings from one unification to the next. a: b: a Unify b list, a list: Unify b, a c: Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 14

Try to solve Example of Unification b list= a list; a b = c; c bool= (bool bool) bool We unify both sides of each equation (in any order), keeping the bindings from one unification to the next. a: b: a Unify b list, a list: Unify b, a Unify a b, c c: a b Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 14

Try to solve Example of Unification b list= a list; a b = c; c bool= (bool bool) bool We unify both sides of each equation (in any order), keeping the bindings from one unification to the next. a: b: a Unify b list, a list: Unify b, a Unify a b, c Unify c bool, (bool bool) bool c: a b Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 14

Try to solve Example of Unification b list= a list; a b = c; c bool= (bool bool) bool We unify both sides of each equation (in any order), keeping the bindings from one unification to the next. a: b: a c: a b Unify b list, a list: Unify b, a Unify a b, c Unify c bool, (bool bool) bool Unify c, bool bool: Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 14

Try to solve Example of Unification b list= a list; a b = c; c bool= (bool bool) bool We unify both sides of each equation (in any order), keeping the bindings from one unification to the next. a: b: a c: a b Unify b list, a list: Unify b, a Unify a b, c Unify c bool, (bool bool) bool Unify c, bool bool: Unify a b, bool bool: Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 14

Try to solve Example of Unification b list= a list; a b = c; c bool= (bool bool) bool We unify both sides of each equation (in any order), keeping the bindings from one unification to the next. a: bool b: a c: a b Unify b list, a list: Unify b, a Unify a b, c Unify c bool, (bool bool) bool Unify c, bool bool: Unify a b, bool bool: Unify a, bool Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 14

Try to solve Example of Unification b list= a list; a b = c; c bool= (bool bool) bool We unify both sides of each equation (in any order), keeping the bindings from one unification to the next. a: bool b: a c: a b Unify b list, a list: Unify b, a Unify a b, c Unify c bool, (bool bool) bool Unify c, bool bool: Unify a b, bool bool: Unify a, bool Unify b, bool: Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 14

Try to solve Example of Unification b list= a list; a b = c; c bool= (bool bool) bool We unify both sides of each equation (in any order), keeping the bindings from one unification to the next. a: bool b: a c: a b Unify b list, a list: Unify b, a Unify a b, c Unify c bool, (bool bool) bool Unify c, bool bool: Unify a b, bool bool: Unify a, bool Unify b, bool: Unify bool, bool Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 14

Try to solve Example of Unification b list= a list; a b = c; c bool= (bool bool) bool We unify both sides of each equation (in any order), keeping the bindings from one unification to the next. a: bool b: a bool c: a b bool bool Unify b list, a list: Unify b, a Unify a b, c Unify c bool, (bool bool) bool Unify c, bool bool: Unify a b, bool bool: Unify a, bool Unify b, bool: Unify bool, bool Unify bool, bool Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 14

Type Rules for a Small Language Each of the a, a i mentioned is a fresh type variable, introduced for each application of the rule. (i an integer literal) i : int [] : a list L : a list hd(l) : a tl(l) : a list E 1 : int E 2 : int E 1 + E 2 : int E 1 : a, E 2 : a list E 1 ::E 2 : a list E 1 : a, E 2 : a E 1 = E 2 : bool E 1!= E 2 : bool E 1 : bool, E 2 : a, E 3 : a if E 1 then E 2 else E 3 : a E 1 : a b, E 2 : a E 1 E 2 : b x1: a 1,...,xn : a n, f: a 1... a n a 0 E: a 0 def f x1...xn = E : void f: a 1... a n a 0 Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 15

Alternative Definition Construct Type Conditions Integer literal int [] a list hd (L) a L: a list tl (L) a list L: a list E 1 +E 2 int E 1 : int, E 2 : int E 1 ::E 2 a list E 1 : a, E 2 : a list E 1 = E 2 bool E 1 : a, E 2 : a E 1!=E 2 bool E 1 : a, E 2 : a if E 1 then E 2 else E 3 a E 1 : bool, E 2 : a, E 3 : a E 1 E 2 b E 1 : a b, E 2 : a def f x1...xn = E x1: a 1,..., xn: a n E: a 0, f: a 1... a n a 0. Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 16

Using the Type Rules Apply these rules to a program to get a bunch of Conditions. Whenever two Conditions ascribe a type to the same expression, equate those types. Solve the resulting equations. Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 17

Aside: Currying Writing def sqr x = x*x; means essentially that sqr is defined to have the value λ x. x*x. To get more than one argument, write def f x y = x + y; and f will have the value λ x. λ y. x+y It s type will be int int int (Note: is right associative). So, f 2 3 = (f 2) 3 = (λ y. 2 + y) (3) = 5 Zounds! It s the CS61A substitution model! This trick of turning multi-argument functions into one-argument functions is called currying (after Haskell Curry). Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 18

Example def f x L = if L = [] then [] else if x!= hd(l) then f x (tl L) else x :: f x (tl L) fi fi Let s initially use f, x, L, etc. as the fresh type variables. Using the rules then generates equations like this: f = a0 a1 a2 # def rule L = a3 list # = rule, [] rule L = a4 list # hd rule, x = a4 #!= rule x = a0 # call rule L = a5 list # tl rule a1 = a5 list # tl rule, call rule... Last modified: Fri Oct 20 10:46:40 2006 CS164: Lecture #23 19