Parametric types. map: ( a (a b) a list b list. filter: ( a bool) a list a list. Why? a and b are type variables!

Similar documents
From last time. News. Example. Example. Typechecking let. Typechecking let. Pick up your first two quizzes after class. Assignment #2 due tomorrow

Parametric types. aka: what s up with those a???

CSE 130: Programming Languages. Polymorphism. Ranjit Jhala UC San Diego

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

Programming Languages. Example 5. Example 4. CSE 130 : Fall type, can reuse code for all types! let rec cat l = match l with

Monday Feb 16, 5pm. solve these equalities. Midterm Thursday Feb 12. covers only ocaml part of the course, assignments 1 through 3.

CMSC 330: Organization of Programming Languages

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

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

CSCI-GA Scripting Languages

CMSC 330: Organization of Programming Languages

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

Generic polymorphism on steroids

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

CMSC 330: Organization of Programming Languages. OCaml Data Types

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

ML Type Inference and Unification. Arlen Cox

Polymorphism and Type Inference

Polymorphism and Type Inference

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

Overloading, Type Classes, and Algebraic Datatypes

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

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

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

CS558 Programming Languages

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

Programming Languages

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

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

CSE 130, Fall 2006: Final Examination

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Some Advanced ML Features

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

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

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

CMSC 330, Fall 2013, Practice Problems 3

CS 11 Ocaml track: lecture 2

CMSC 430 Introduction to Compilers. Fall Everything (else) you always wanted to know about OCaml (but were afraid to ask)

Any questions. Say hello to OCaml. Say hello to OCaml. Why readability matters. History, Variants. Plan (next 4 weeks)

Type Checking and Type Inference

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Programming Languages

CSE 130, Fall 2005: Final Examination

Standard ML. Data types. ML Datatypes.1

CMSC330 Spring 2015 Final Exam 9:30am/11:00am/12:30pm

CMSC330 Spring 2014 Midterm 2 Solutions

Introduction to OCaml

CSE 130 Programming Languages. Lecture 3: Datatypes. Ranjit Jhala UC San Diego

CS558 Programming Languages

Types, Type Inference and Unification

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

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

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

Programming Languages

Lecture #23: Conversion and Type Inference

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

So what does studying PL buy me?

Programming Languages

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

CMSC 330: Organization of Programming Languages. Closures (Implementing Higher Order Functions)

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

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

Java Object Oriented Design. CSC207 Fall 2014

Sum-of-Product Data Types

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

CSC324 Principles of Programming Languages

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

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Anonymous Functions CMSC 330: Organization of Programming Languages

Sum-of-Product Data Types

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

CS Lectures 2-3. Introduction to OCaml. Polyvios Pratikakis

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

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm

CMSC330 Fall 2014 Midterm 2 Solutions

Recap: ML s Holy Trinity

CSE 130 [Winter 2014] Programming Languages

The Typed Racket Guide

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

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

Inheritance, Polymorphism and the Object Memory Model

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

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Programming Languages Lecture 14: Sum, Product, Recursive Types

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Introduction to Objective Caml

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Why OCaml? Introduction to Objective Caml. Features of OCaml. Applications written in OCaml. Stefan Wehr

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

CMSC 631. Functional Programming with OCaml

F28PL1 Programming Languages. Lecture 14: Standard ML 4

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

Functional programming with OCaml. Programming with OCaml. Fall Software Foundations CIS 500. OCaml and this course. Announcements. features.

Programming Languages. Tail Recursion. CSE 130: Winter Lecture 8: NOT TR. last thing function does is a recursive call

SNU Programming Language Theory

Motivation for typed languages

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

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

Transcription:

What is the deal with a? Parametric types aka: what s up with those a? These meta-functions have strange types: map: ( a b) a list b list filter: ( a bool) a list a list Why? Polymorphism Poly = many, morph = kind let swap (x,y) = (y,x) a and b are type variables! a * b b * a For-all types: For all a, b: a * b b * a a, b b can be instantiated ti t with any type: w/ int,string : int * string string * int w/ char, int list : char * int list int list * char w/ int int, bool : (int Int) * bool bool * (int int) Instantiation at Use map: ( a (a b) a list b list let f x = x + 10;; let fm = map f;; f : int -> int first arg. of map: a->b Instantiated : a with int, b with int let f x = x^ like ;; let fm = map f [ cat ; dog ; burrito ];; f : string > string first arg of map: a >b f : string -> string first arg. of map: a->b Instantiated : a with str, b with str

Instantiation at Use: be careful map: ( a (a b) a list b list let f x = x^ like ;; let fm = map f [1;2;3;4];; f : string -> string first arg. of map: a->b Instantiated : a with str, b with str So, list must be a list = string list! Polymorphic ML types Poly = many, morph = kind Possible ML types: tv = a b c T = int bool string char T 1 * T 2 * T n T 1 T 2 tv Implicit for-all at the left of all types Never printed out map: For all a, b : ( a b) a list b list Polymorphism enables Reuse Can reuse generic functions: map : a * b b * a filter: ( a bool) a list a list rev: a list a list length: a list int swap: a * b b * a sort: ( a (a a bool) a list a list fold: If function (algorithm) is independent of type, can reuse code for all types! Not just functions Data types are also polymorphic! type a list = Nil Cons of ( a * a list) Type is instantiated for each use: Cons(1,Cons(2,Nil)) Cons(2 : Cons( a,cons( b,nil)) : Cons((1,2),Cons((3,4),Nil)) : Nil : 27

Not just functions Data types are also polymorphic! type a list = Nil Cons of ( a * a list) Datatypes with many type variables Multiple type variables type ( a, b) tree = Leaf of ( a * b) Node of ( a, b) tree * ( a, b) tree Type is instantiated for each use: Cons(1,Cons(2,Nil)) Cons(2 : int list Type is instantiated for each use: Leaf( joe,1) : Cons( a,cons( b,nil)) : string list Leaf( william,2) : Cons((1,2),Cons((3,4),Nil)) : (int*int) list Node(, ) : Nil : a list 28 Node(Leaf( joe,1),leaf(3.14, pi )): 29 Datatypes with many type variables Multiple type variables type ( a, b) tree = Leaf of ( a * b) Node of ( a, b) tree * ( a, b) tree Type is instantiated for each use: Leaf( joe,1) : (string,int) i t) tree Leaf( william,2) : (string,int) tree Node(, ) : (string,int) tree Node(Leaf( joe,1),leaf(3.14, pi )) 30 Polymorphic Data Structures Container data structures independent of type! Appropriate type is instantiated at each use: key, data a list ( a, b) tree ( a, b) hashtbl Appropriate type instantiated ti t at use No unsafe casting as in C++/Java Static type checking catches errors early Cannot add int key to string hashtable Generics: in Java,C#,VB (borrowed from ML)

Other kinds of polymorphisms That was OCaml... But what about other kinds of polymorphisms.. Other kinds of polymorphisms Sub-type polymorphism void f(shape s) Can pass in any sub-type of Shape Parametric polymorphism void proc_elems(list[t]) can pass in ANY T this is the kind in OCaml! 32 33 Other kinds of polymorphisms Bounded polymorphism p Like parametric, except can provide a bound void proc_elems(list[t]) T extends Printable Hey... isn t this subtype polymorphism? No, for example: bool ShapeEq(T a, T b) T extends Shape Can call on (Rect, Rect) (Circle, Circle) But not (Rect, Circle) 34 Summary of polymorphism Subtype Parametric Bounded = Parametric + Subtype = In Java/C# 35

Back to OCaml Polymorphic types allow us to reuse code However, not always obvious from staring at code But... Types never entered w/ program! Type inference aka: how in the world does Ocaml figure out all the types??? 36 37 Polymorphic Types Polymorphic types are tricky Not always obvious from staring at code How to ensure correctness? Types (almost) never entered w/ program! Polymorphic Type Inference Computing the types of all expressions At compile time : Statically Typed Each binding is processed in order Types are computed for each binding For expression and variable bound to Types used for subsequent bindings How is this different from values? Values NOT computed statically (e.g. fun values)

Polymorphic Type Inference Every expression accepted by ML must have a valid inferred type Example 1 let x = 2 + 3;; Can have no idea what a function does, but still know its exact type Value let y = string_of_int x;; A function may never (or sometimes terminate), but will still have a valid type Value undefined d Example 2 Example 3 let x = 2 + 3;; let y = string_of_int x;; let inc y = x + y;; let foo x = let (y,z) = x in z-y ;;

Example 4 let rec cat l = [] -> h::t -> h^(cat t) ML doesn t know what the function does, or even that it finishes only its type! Example 4 let rec cat l = [] -> h::t -> h^(cat t) ML doesn t know what the function does, or even that it finishes only its type! string list -> string let rec cat l = [] -> cat [] h::t -> h^(cat t) Example 5 ( a (a-> b) -> a list -> b list Example 6 ( b (b-> c) * ( a -> b) -> ( a -> c) let rec map f l = [] -> [] h::t ->(map h)::(map f t) let compose (f,g) x = f (g x) Introduce unknown tyvar: Unify,solve, Remaining tyvar gets a forall

Example 7 ( a (a* b -> b) -> b -> a list -> b let rec fold f cur l = [] -> cur h::t -> fold f (f h cur) t let foo1 f g x = if f x then x else g x let foo2 f g x = if f x then x else foo2 f g (g x)