CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.

Slides:



Advertisements
Similar presentations
Sml2java a source to source translator Justin Koser, Haakon Larsen, Jeffrey Vaughan PLI 2003 DP-COOL.
Advertisements

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Type Checking.
CSE341: Programming Languages Lecture 6 Tail Recursion, Accumulators, Exceptions Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 7 Functions Taking/Returning Functions Dan Grossman Fall 2011.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Good Advice for Type-directed Programming Aspect-oriented Programming and Extensible Generic Functions Geoffrey Washburn [ ] Joint.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Read Robert Harper’s notes on “an introduction to.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture: Datatypes and Recursion.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions, Polymorphism.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
CS235 Languages and Automata Department of Computer Science Wellesley College Introduction to Standard ML Wednesday, September 23, 2009 Reading: Beginning.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Chapter 9: Functional Programming in a Typed Language.
CMSC 330: Organization of Programming Languages Maps and Folds Anonymous Functions.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
CSE 341 : Programming Languages Lecture 8 First Class Functions Zach Tatlock Spring 2014.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
CSE 341 : Programming Languages Lecture 9 Lexical Scope, Closures Zach Tatlock Spring 2014.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 7 First-Class Functions
Types for Programs and Proofs
ML: a quasi-functional language with strong typing
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 11 Type Inference
ML’s Type Inference and Polymorphism
ML’s Type Inference and Polymorphism
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
CSE-321 Programming Languages Introduction to Functional Programming
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 11 Type Inference
ML’s Type Inference and Polymorphism
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
ML’s Type Inference and Polymorphism
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 11 Type Inference
Presentation transcript:

CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism

Functions are “first-class” values Arguments, return values, bindings … What are the benefits ? Creating, (Returning) Functions Using, (Taking) Functions Parameterized, similar functions (e.g. Testers) Iterator, Accumul, Reuse computation pattern w/o exposing local info Compose Functions: Flexible way to build Complex functions from primitives.

What is the deal with ’a ? These meta-functions have strange types: map: filter: Why ? (’a ! ’b) ! ’a list ! ’b list (’a ! bool) ! ’a list ! ’a list

Polymorphism Poly = many, morph = kind ’a * ’b ! ’b * ’a ’a and ’b are type variables! For-all types: ’a,’b can be “instantiated” 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) fun swap (x,y) = (y,x) For all ’a, ’b: ’a * ’b ! ’b * ’a

Instantiation at use map: fun f x = x^“ like”; val fm = map f1 [“cat”, “dog”, “burrito”]; (’a ! ’b) ! ’a list ! ’b list fun f x = x + 10; val fm = map f;

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 Implict for-all at the “left” of all types –Never printed out, or specified

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

Genericity is everywhere… What’s the type of this function ? fun sort (lt,l) = case l of [] => [] | (h::t) = let val (l,r) = partition ((lt h),t) in (sort (lt,r)) end;

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 ! 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 is instantiated for each use: datatype ’a list = Nil | Cons of (’a * ’a list)

Datatypes with many type variables Multiple type variables Type is instantiated for each use: datatype (’a,’b) tree = Leaf of (’a * ’b) | Node of (’a,’b) tree * (’a,’b) tree

Polymorphic Data Structures “Container” data structures independent of type ! Appropriate type is instantiated at each use: Appropriate type instantiated at use –No “casting” as in C++/Java –Static type checking catches errors early –Cannot add “int” key to “string” hashtable Generics: feature of next versions of Java,C#,VB… ’a list (’a, ’b) Tree (’a, ’b) Hashtbl …

Polymorphic Types Polymorphic types are tricky Not always obvious from staring at code How to ensure correctness ? Types (almost) never entered w/ program!

Remember: ML is statically typed 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression meaningful type for the expr 3.ML evaluates expression to compute value Of the same “type” found in step 2 Expressions (Syntax)Values (Semantics) Types Compile-time “Static” Exec-time “Dynamic”

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

Polymorphic Type Inference Can have no idea what a function does, but still know its exact type A function may never (or sometimes terminate), but will still have a valid type Every expression accepted by ML must have a valid inferred type

Example 1 val x = val y = Int.toString x

Example 2 val x = val y = Int.toString x fun inc y = x + y

Example 3 fun foo x = let val (y,z) = x in (~y) + z end;

Example 4 fun cat [] = “” |cat (h::t) = h^(cat t)

Example 4 fun cat [] = “” |cat (h::t) = h^(cat t) fun cat [] = cat [] |cat (h::t) = h^(cat t)

Example 5 fun map f [] = [] |map f (h::t)=(map h)::(map f t)

Example 6 fun compose (f,g) x = f (g x)

Example 7 fun fold f b [] = b |fold f (h::t)=fold f (f (h,b)) t

ML Review: many key PL ideas 1.Expressions, Types, Values 2.Environments: No mutation/assignment 3.Static Scoping 4.Recursive Functions 5.Functions taking/returning functions 6.Polymorphism 7.Modules and Signatures 1.Constructing large systems from subsystems 2.Managing complexity 3.Safe reuse

A little Prolog Radically different view of computation Program = facts + rules Facts : mexican(“burrito”) Rules : delicious(x) :- mexican(x) Computation = does fact follow from rules ? delicious(“burrito”) ?