Types-2. Polymorphism

Similar documents
Type Checking and Type Inference

Lecture #23: Conversion and Type Inference

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

Programming Languages and Compilers (CS 421)

Static Checking and Type Systems

Introduction to Programming Using Java (98-388)

Programming Languages Lecture 15: Recursive Types & Subtyping

Semantic Processing (Part 2)

Harvard School of Engineering and Applied Sciences Computer Science 152

CS558 Programming Languages

CS558 Programming Languages

CSCI-GA Scripting Languages

Programming Languages Lecture 14: Sum, Product, Recursive Types

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

Type Checking. Error Checking

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

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

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

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Agenda. CS301 Session 11. Common type constructors. Things we could add to Impcore. Discussion: midterm exam - take-home or inclass?

Overloading, Type Classes, and Algebraic Datatypes

Intermediate Code Generation Part II

Type-indexed functions in Generic Haskell

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

Types, Type Inference and Unification

CS 320: Concepts of Programming Languages

Types and Type Inference

Types and Type Inference

Lambda Calculus and Type Inference

CS558 Programming Languages

The Design of Core C++ (Notes)

Data Types The ML Type System

User-Defined Algebraic Data Types

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

Types. What is a type?

FUNCTIONAL AND LOGIC PROGRAMS

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

Software System Design and Implementation

6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science

Some instance messages and methods

A Brief Introduction to Standard ML

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

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

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

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

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Begin at the beginning

Functional Paradigm II

Motivation for typed languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

MPRI course 2-4 Functional programming languages Exercises

GADTs meet Subtyping

Where is ML type inference headed?

CS 320: Concepts of Programming Languages

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27

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

Integration of SMT Solvers with ITPs There and Back Again

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

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

Specification and Verification in Higher Order Logic

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

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

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

How does ML deal with +?

ML Type Inference and Unification. Arlen Cox

If a program is well-typed, then a type can be inferred. For example, consider the program

First-Class Type Classes

Flang typechecker Due: February 27, 2015

Compiler Construction I

Functional Programming and Modeling

Principles of Programming Languages

Polymorphism. Lecture 19 CS 565 4/17/08

Subtyping. Lecture 13 CS 565 3/27/06

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Handout 2 August 25, 2008

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

CSE 341 Section 5. Winter 2018

If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

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

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

Type systems. Static typing

Semantic Processing (Part 2)

CSE 3302 Programming Languages Lecture 8: Functional Programming

Functional programming Primer I

Compilers and computer architecture: Semantic analysis

Lecture: Functional Programming

CSE-321: Assignment 8 (100 points)

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)

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

Compiler Construction 2009/2010 Polymorphic Types and Generics

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Functional Programming - 2. Higher Order Functions

SML A F unctional Functional Language Language Lecture 19

Exercise 1 ( = 22 points)

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

CSc 520. Principles of Programming Languages 11: Haskell Basics

λ calculus is inconsistent

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

CMSC 330: Organization of Programming Languages

Transcription:

Types-2 Polymorphism Type reconstruction (type inference) for a simple PL Typing functions Coercion, conversion, reconstruction Rich area of programming language research as people try to provide safety assertions about code as part of type systems Types- 2, CS5314 BGRyder 1 Polymorphism Motivation: to allow flexibility in implementation with automatic adaptation to correctly typed operations for parameter types used Ease of design (gets rid of special cases) Ease of maintenance (1 copy of code) A cool idea Types- 2, CS5314 BGRyder 2 1

Polymorphism - Realization Ad hoc polymorphism Use coercion to make types work Overloading same name used for different functions compiler chooses by context information Parametric polymorphism generics Code is same for range of types, parameterized by the type and instantiated to particular types when code is generated True polymorphism Only one copy of the code! (e.g., ML, Ocaml) Question: Do types have to be declared or can they be deduced in a PL allowing polymorphism? Types- 2, CS5314 BGRyder 3 How type reconstruction (type inference) works? - <expression> : <type> 1. can always type a constant - 5.8 : ft/sec 2. can build rules for combining types in expressions e.g., Distance = Velocity * Time Conversions - e1 : ft/sec, - e2: sec - e1*e2 : ft Velocity = Distance / Time - e1: ft, - e2: sec - e1/e2: ft/sec - e1:ft/sec, - e2: sec/min - e1*e2 : ft/min Types- 2, CS5314 BGRyder 4 2

Type Reconstruction -1 See handout for small expression language definition Types: τ Int Char Bool primitive PL types τ Pointer(τ) Tuple (τ,τ) List(τ) constructed PL Record(label τ, label τ,...) types Expressions syntax: e <intliteral> <listliteral> e varid (e) e e mod e e + e e and e e or e not e Boolean/numerical operations e e eq e comparison operator Types- 2, CS5314 BGRyder 5 Type Reconstruction -2 e deref e pointer operation e fst e snd e pair(e,e) tuple operations e hd e tail e cons (e,e) list operations tuple constructor list constructor where <intliteral> 0 1 2 3 4 5 6 7 8 9 <listliteral> nil, etc. To perform type reconstruction, we need assumptions for types of constants and then define type deduction rules to type other constructs Types- 2, CS5314 BGRyder 6 3

Type Reconstruction - 3 Type rules define the types of results of legal operations Constants: c :τ - c : τ given in type environment Variables: y: τ - y: τ e.g., in declarations Arithmetic: - e1: Int, - e2: Int means mod op - (e1 mod e2) : Int only applicable to integers Equality: - e1 : τ, - e2 : τ can only compare - (e1 eq e2) : Bool exprs of same type result is Boolean Types- 2, CS5314 BGRyder 7 Type Reconstruction - 4 Deref: - e: Pointer(τ) can only apply deref operator - deref(e) : τ to pointer type Examples of use of rules fst(1, 2.0) + snd(3.5, 5) τ1 = Tuple (Int, Real), τ2 = Tuple (Real, Int) fst(τ1) : Int, snd(τ2) : Int, therefore + operation is well-typed fst(1, 2.0) + hd(cons(5, nil)) τ1 = Tuple(Int, Real), and we want: τ2 = List(Int) but how to get this? Types- 2, CS5314 BGRyder 8 4

Type Reconstruction - 5 Need two more rules to type lists: [Cons] - e1: τ, - e2: List(τ) (1) - cons(e1, e2): List(τ) - nil: List[ _ ] (2) read this as List of any type or instead use rules (1) and (3): - e:τ (3) - cons(e, nil) : List(τ) means lists are made up of homogeneously typed elements, but not necessarily of primitive type e.g., List (Tuple( Int, Bool )) is legal Types- 2, CS5314 BGRyder 9 Typing Statements Problem: what to do about typing statements? can use special type called void for correctly typed statements - y: τ, - e: τ - s1: void, s2:void -b:bool, - s:void - y:=e : void - s1; s2 :void - if b then s:void Assignment Stmt sequence If stmt 10 5

Typing Functions -1 Want to write a truly polymorphic function and be able to use it on arguments of different types length L = if L=nil then 0 else 1 + length (tl(l)); has type signature: length: List( _ ) Int Examples from our small expression language cons : τ List[ τ ] List [τ ] pair: σ * τ Tuple( σ,τ ) fst: Tuple( σ,τ ) σ if_then_else: bool * τ * τ τ Types- 2, CS5314 BGRyder 11 Typing Functions -2 Need for type variables to represent unknown types during reconstruction α. List(α ) int is type of SML length function Type of deref: β. Pointer(β) β Note: α does not include type error, which is used in type checking more later on this Need new inference rule for function application: - e1: σ τ, - e2: σ - e1(e2) : τ Types- 2, CS5314 BGRyder 12 6

Typing Functions -3 Functions are usually typed in their curried form incr(k,x) = x + k; plus(k), curried incr incr: Tuple(int, int) int plus: int (int int) In curried form can use previous slide s inference rule What is a curried form of a function? A process of transforming a function that takes multiple arguments into a function that takes one argument and returns another function if any arguments are still needed. from https://wiki.haskell.org/currying Currying, an idea from functional programming in which functions are first-class Functions can be values assigned to a variable, passed as parameters, applied to arguments (of the right type) Types- 2, CS5314 BGRyder 13 Curried Form of a Function Continuing with our example of incr Incr(K,X) is function of type int x int à int Incr(5,X) returns a value of X+5 (function application) Plus(K) is of type int à (int à int) Plus(5) returns a function that adds 5 to its argument; we write this as lambda(x) = 5 + X (more later on this too) Currying is a notion from functional programming in which functions are first-class Functions can be values assigned to a variable, passed as parameters, applied to arguments (of the right type) Types- 2, CS5314 BGRyder 14 7

Reconstructing Function Types -1 (ASU 86 ed, 6.6) High-level view 1. Introduce new type variables for the function and its parameters. 2. Setup equations that must hold for these variables based on statements within the function (infer compatible types from uses). 3. Solve these equations. a. If reach a type error, report it. b. If can get values for all type variables, then the equations are consistent. Types- 2, CS5314 BGRyder 15 Reconstructing Function Types - 2 c. Note: type value solution process involves using unification to see if two type variables, currently bound to specific types (represented by trees), can be unified to the same type; implementation uses the union-find algorithm 4. Add a new variable to the type environment to represent this function δ = Analyze(fcn_body, E) For an example, we will type the SML length function for lists Types- 2, CS5314 BGRyder 16 8

Analyze (e, E) e is expression, E is type environment if e is a type variable τ, return E[τ] if e is an identifier id, return E[id] with all variables renamed and dropped e.g., α, α x List(α) --> List(α) is type of cons e.g., α, bool x α x α --> α is type of if e.g., α, α-->β becomes γ-->β, an arbitrary function if e is function application, f(e1,,ek) let t1 - Analyze (e1, E) let s - Analyze (f, E) introduce fresh type variable, δ add equation (t1 x t2 x x tk --> δ) = s and return δ if e is a function definition, we need to follow the reasoning in this example. Types- 2, CS5314 BGRyder 17 Trace Algm Example -1 Analyze (lng (n) if (null n) then 0 else (1 + lng(tl n)), E); Rule 1. Extend E[n] = γ, E[lng] = {γ δ} Rule 2. Analyze function body. Analyze (if ((null n), 0, (1+lng(tl n))), E). t1 = Analyze (e1, E) for e1 = (null n) fcn application t11 = Analyze (n) E[n] = {γ} identifier s11 = Analyze (null) E[null]= {list α bool} identifier get new type variable β γ β = list α bool (1) return β as type of function application. Types- 2, CS5314 BGRyder 18 9

Trace Algm Example -2 Analyze (lng (n) if (null n) then 0 else (1 + lng(tl n)), E); t2 = Analyze(0,E) {int} constant t3 = Analyze (1+lng(tl n)) another fcn application t31=analyze(1,e) {int} t32 = Analyze(lng(tl n), E) t321 = Analyze((tl n),e) analyze the arg t3211 = Analyze(n,E) {γ} identifier s3211 = Analyze(tl,E) {list µ list µ} new type variable σ γ σ = list µ list µ (2) return σ as type of function application s321 = Analyze(lng,E) {γ δ } from fcn signature new type variable Γ σ Γ = γ δ (3) return Γ as type of function application Types- 2, CS5314 BGRyder 19 Trace Algm Example -3 s31 = Analyze(+,E) {int * int int} new type variable Δ int * Γ Δ = int * int int (4) return Δ s1 = Analyze(if,E) = {bool * ψ * ψ ψ} new type variable ρ β * int * Δ ρ = bool * ψ * ψ ψ (5) return ρ Types- 2, CS5314 BGRyder 20 10

Trace Algm Example -4 Rule 3: solve equations using unification using most general unifier (1) γ β = list α bool (2) γ σ = list µ list µ (3) σ Γ = γ δ (4) int * Γ Δ = int * int int (5) β * int * Δ ρ = bool * ψ * ψ ψ β = bool (from 1.) γ = σ = list µ (from 2.,3.) γ = list α (from 1.) (note: list α and list µ are same type) δ = Γ = Δ = int (from 3.,4.) Finally we obtain: lng: γ δ = list µ int Types- 2, CS5314 BGRyder 21 Trace Algm Example -5 In ASU 86 p375 Is trace of our algorithm as lines in a table in the same order as our slides Looks like a bottom up traversal of a type tree, typing the subtrees and then going upwards to type higher subtrees Types- 2, CS5314 BGRyder 22 11