CS558 Programming Languages

Similar documents
CS558 Programming Languages

CS558 Programming Languages

CS558 Programming Languages

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Overloading, Type Classes, and Algebraic Datatypes

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

Type Systems, Type Inference, and Polymorphism

Types, Type Inference and Unification

CS 320: Concepts of Programming Languages

Types and Type Inference

Types and Type Inference

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

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

Programming Languages and Compilers (CS 421)

Lecture #23: Conversion and Type Inference

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

Values (a.k.a. data) representation. Advanced Compiler Construction Michel Schinz

Values (a.k.a. data) representation. The problem. Values representation. The problem. Advanced Compiler Construction Michel Schinz

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

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

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

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

Type Checking and Type Inference

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

Programming Languages

CS558 Programming Languages

CS 320: Concepts of Programming Languages

CS558 Programming Languages

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

Data Types The ML Type System

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA

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

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

CSCI-GA Scripting Languages

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

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

Polymorphism and Type Inference

CS558 Programming Languages

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

How does ML deal with +?

CSc 520 Principles of Programming Languages

Introduction to Programming, Aug-Dec 2006

Programming Languages Fall 2013

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

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

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Lists. Michael P. Fourman. February 2, 2010

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

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

Some instance messages and methods

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

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

Type-indexed functions in Generic Haskell

Overview. Elements of Programming Languages. Records. Named variants. Last time: Simple data structures: pairing (product types), choice (sum types)

CS 360: Programming Languages Lecture 12: More Haskell

CSci 4223 Lecture 12 March 4, 2013 Topics: Lexical scope, dynamic scope, closures

int f(char a, char b) {

Polymorphism and Type Inference

Lecture 16: Static Semantics Overview 1

CS153: Compilers Lecture 14: Type Checking

What is Needle? The Needle Programming Language Neel Krishnaswami

Project Compiler. CS031 TA Help Session November 28, 2011

Harvard School of Engineering and Applied Sciences Computer Science 152

Software System Design and Implementation

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

Programming in Omega Part 1. Tim Sheard Portland State University

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

Static Checking and Type Systems

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Types. What is a type?

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

Programming Languages

CS205: Scalable Software Systems

Intro. Scheme Basics. scm> 5 5. scm>

Programming Languages

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

A Fourth Look At ML. Chapter Eleven Modern Programming Languages, 2nd ed. 1

Programming with Math and Logic

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

Lecture 23: Priority Queues 10:00 AM, Mar 19, 2018

List Functions, and Higher-Order Functions

Subtyping and Objects

TYPES, VALUES AND DECLARATIONS

CS 251 Intermediate Programming Methods and Classes

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

CSC324 Principles of Programming Languages

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

Programming Languages: Lecture 11

ML Type Inference and Unification. Arlen Cox

Lecture 02, Fall 2018 Friday September 7

Lecture 12: Data Types (and Some Leftover ML)

G Programming Languages - Fall 2012

Chapter 5 Names, Binding, Type Checking and Scopes

A Type is a Set of Values. Here we declare n to be a variable of type int; what we mean, n can take on any value from the set of all integer values.

All the operations used in the expression are over integers. a takes a pair as argument. (a pair is also a tuple, or more specifically, a 2-tuple)

BM214E Object Oriented Programming Lecture 8

Introduction to System F. Lecture 18 CS 565 4/20/09

CS457/557 Functional Languages

Exercises on ML. Programming Languages. Chanseok Oh

Transcription:

CS558 Programming Languages Fall 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017

Type Inference Some statically typed languages, like ML (and to a lesser extent Scala), offer alternative ways to regain the flexibility of dynamic typing, via type inference and polymorphism. Type inference works like this: The types of identifiers are automatically inferred from the way they are used The programmer is no longer required to declare the types of identifiers (although this is still permitted) Method requires that the types of operators and literals is known

Inference Examples (let f (fun (x) (+ x 2)) (@ f y)) The type of x must be int because it is used as an arg to +. So the type of f must be int int (i.e. the type of functions that expect an int argument and return an int result), and y must be an int. (let f (fun (x) (cons x nil)) (@ f true)) Suppose x has some type t. Then the type of f must be t (list t). Since f is applied to a bool, we must have t = bool. For the moment, we assume that f must be given a unique monomorphic type; we will relax this later

Systematic Inference Here s a harder example: (let f (fun (x) (if x p q)) (+ 1 (@ f r))) Can only infer types by looking at both the function s body and its application In general, we can solve the inference task by extracting a collection of typing constraints from the program s AST, and then finding a simultaneous solution for the constraints using unification Extracted constraints tell us how types must be related if we are to be able to find a typing derivation. Each node generates one or more constraints

Rules for First-class Functions To handle this example, we ll need some extra typing rules: TE + {x t 1 } e : t 2 TE (fun (x) e) : t 1 t 2 (Fn) TE e 1 : t 1 t 2 TE e 2 : t 1 TE (@ e 1 e 2 ) : t 2 (Appl)

Inference Example Solution : t 1 = t 7 = t 8 = t 9 = t 3 = t 5 = t p = t 6 = t q = int t 4 = t x = t 11 = t r = bool t 2 = t f = t 10 = bool int Node Rule Constraints 1 Let t f = t 2 t 1 = t 7 2 Fun t 2 = t x t 3 3 If t 4 = bool t 3 = t 5 = t 6 4 Var t 4 = t x 5 Var t 5 = t p Let(f) 1 6 Var t 5 = t q 7 Add t 7 = t 8 = t 9 = int 8 Int t 8 = int 9 Appl t 10 = t 11 t 9 Fun(x) + 2 7 10 Var t 10 = t f 11 Var t 11 = t r If 3 1 Appl 8 9 x p q 4 5 6 f r 10 11

Drawbacks of Inference Consider this variant example: (let f (fun (x) (if x p false) (+ 1 (@ f r))) Now the body of f returns type bool, but it is used in a context expecting an int. The corresponding extracted constraints will be inconsistent; no solution can be found. Can report a type error to the programmer. But which is wrong, the definition of f or the use? No good way to associate the error message with a single program point.

Polymorphism Consider (let snd (fun (l) (head (tail l))) (@ snd (cons 1 (cons 2 (cons 3 nil))))) By extracting constraints and solving, we will get snd : (list int) int We could also write Same definition! (let snd (fun (l) (head (tail l))) (@ snd (cons true (cons false (cons true nil))))) And get snd : (list bool) bool

Polymorphism (2) So why can t we write something like this? (let snd (fun (l) (head (tail l))) (block (@ snd (cons 1 (cons 2 (cons 3 nil)))) (@ snd (cons true (cons false (cons true nil)))))) We can, by treating the type of snd as polymorphic snd : (list t) t Here t is an unconstrained type variable

Inferring Polymorphism In fact, if we extract constraints and solve just for the definition (fun l (head (tail l))) without considering its uses, we will end up with exactly the type (list t) t We can assign this polymorphic type to snd allowing it to be used multiple times, each with a different instance of t (e.g. with t = bool or t = int). By default, languages like ML infer the most polymorphic possible type for every function This is the natural result of the inference process we ve described

Parametric Polymorphism We can think of these polymorphic types as being universally quantified over their type variables and instantiated at use sites let snd : t. list t -> t = in snd {bool} (true::false::nil); snd {int} (1::2::nil) This is called parametric polymorphism because the function definition is (implicitly) parameterized by the instantiating type In ML-like languages the quantification and instantiation don t actually appear

Explicit Parametric Polymorphism Java generics and Scala type parameterization are also a form a parametric polymorphism, in which type abstraction and instantiation are (mostly) explicit def snd[a](l: List[A]) : A = l.tail.head val a = snd[boolean] (List(true,false)) val b = snd(list(1,2)) In parametric polymorphism, the behavior of the polymorphic function is the same no matter what the instantiating type is Scala In fact, an ML compiler typically generates just one piece of machine code for each polymorphic function, shared by all instances

Overloading and Ad-hoc Polymorphism Most languages provide some form of overloading, where the same function name or operator symbol means different things depending on the types to which it is applied e.g. overloading of arithmetic operators to work on either integers or floats is very common Some languages (e.g. Ada, C++) support user-defined overloading, especially useful for user-defined types (e.g. complex numbers) OO languages (e.g. C++,Java) often support method overloading based on argument types Overloading is sometimes called ad-hoc polymorphism, because the implementation of the overloaded operator changes based on the argument types

Static vs. Dynamic Overloading In most statically-typed languages, overloading is resolved statically; i.e. the compiler selects the right version of the overloaded definition once and for all at compile time. Dynamically-typed languages also often overload operators (e.g. + on different kinds of numbers, strings, etc.) Here the right version of the overloaded operator is picked at runtime after checking the (runtime) types of the arguments Of course, the operator might fail altogether if there is no version suitable for the types discovered Haskell type classes provide an unusual form of dynamic overloading with a static guarantee that a suitable version exists