Parametric Polymorphism COS 441 Princeton University Fall 2004.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Type Inference David Walker COS 320. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
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.
Current Techniques in Language-based Security David Walker COS 597B With slides stolen from: Steve Zdancewic University of Pennsylvania.
1 PROPERTIES OF A TYPE ABSTRACT INTERPRETATER. 2 MOTIVATION OF THE EXPERIMENT § a well understood case l type inference in functional programming à la.
Pedigree Types Yu David Liu, Johns Hopkins University / SUNY Binghamton Scott F. Smith, Johns Hopkins University July 7, IWACO’08.
1 1 Regression Verification for Multi-Threaded Programs Sagar Chaki, SEI-Pittsburgh Arie Gurfinkel, SEI-Pittsburgh Ofer Strichman, Technion-Haifa Originally.
Principles of programming languages 1: Introduction (with a simple language) Isao Sasano Department of Information Science and Engineering.
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.
Dynamic Typing COS 441 Princeton University Fall 2004.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 19: Minding Ps & Qs: Axiomatic.
Type Checking.
CSE-321 Programming Languages Polymorphism POSTECH May 15, 2006 박성우.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
CSE341: Programming Languages Lecture 13 Equivalence; Parametric Polymorphism Dan Grossman Fall 2011.
Introduction to Computability Theory
Quick Review Get paper and pencil out. Not graded just for review.
Typed Assembly Languages COS 441, Fall 2004 Frances Spalding Based on slides from Dave Walker and Greg Morrisett.
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
1 Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications.
Subtyping COS 441 Princeton University Fall 2004.
Data Abstraction COS 441 Princeton University Fall 2004.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Type Checking- Contd Compiler Design Lecture (03/02/98) Computer Science Rensselaer Polytechnic.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Syntax With Binders COS 441 Princeton University Fall 2004.
Catriel Beeri Pls/Winter 2004/05 types 65  A type-checking algorithm The task: (since we start with empty H, why is the goal not just E?) The rule set.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
Aggregate Data Structures COS 441 Princeton University Fall 2004.
Semantics for MinML COS 441 Princeton University Fall 2004.
Type Inference II David Walker CS 510. ML-style Polymorphism 2 characteristics: prenex (let) polymorphism let f : all [a,b,c].t =... in e polymorphic.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
Imperative Programming
Reading and Writing Mathematical Proofs
Polymorphism Lecture-10. Print A Cheque A Report A Photograph PrintCheque() PrintReport() PrintPhoto() Printing.
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Type Safety Kangwon National University 임현승 Programming Languages.
Type Inference II David Walker COS 441. Type Inference Goal: Given unannotated program, find its type or report it does not type check Overview: generate.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
Universal Types Report by Matthias Horbach. Contents Types of Polymorphism System F Basic Properties Erasure Impredicativity Parametricity.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
Operational Semantics Mooly Sagiv Tel Aviv University Textbook: Semantics with Applications Chapter.
Soundness of Types Ensuring that a type system is not broken.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
1 Proving Properties of Recursive List Functions CS 270 Math Foundations of CS Jeremy Johnson.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Programming Languages and Compilers (CS 421)
Corky Cartwright January 18, 2017
Proving Properties of Recursive List Functions
Drew Wyborski Programming Languages
Abstract Types Defined as Classes of Variables
Presentation transcript:

Parametric Polymorphism COS 441 Princeton University Fall 2004

Polymorphic Polymorphism Polymorphism has different meanings in different contexts. Cardelli and Wegner (1985) decompose it as follows

Varieties of Polymorphism Universal polymorphism is obtained when a function works uniformly on a range of types Ad-hoc polymorphism is obtained when a function works, or appears to work, on several different types and may behave in unrelated ways for each type. From: “On Understanding Types, Data Abstraction, and Polymorphism,” Luca Cardelli and Peter Wegner Computing Surveys, (December, 1985)

Forms of Universal Polymorphism parametric polymorphism (generics) ML-style, based on type parameters Focus of this lecture inclusion polymorphism (subtyping) OO-style based on subtype inclusion Will study this Friday

Forms of Parametric Polymorphism Harper distinguishes between “first-class” and “second-class” forms of parametric polymorphism ML supports only “second-class” parametric polymorphism because of limitations in type-inference Supporting first-class versions is easy if you don’t care about type-inference

First vs Second Class First-class  ::= int |  1 !  2 | t | 8 t(  Example: 8 t(t ! t) ! 8 t’(t’ ! t’) Second-class  ::=  | 8 t(   ::= int |  1 !  2 | t Example: 8 t( 8 t’((t ! t) ! (t’ ! t’)))

Second Class Polymorphism Second-class or “prenex” polymorphism the 8 are all at the front of the type  ::=  | 8 t(  (polytypes)  ::= int |  1 !  2 | t (monotypes) Polytypes: ,  8 t( ,  8 t( 8 t’( , int Monotypes: t, int, t ! int,…

PolyMinML Supports second-class parametric polymorphism Only instantiate polytypes with monotypes

Dynamic Semantics

Static Semantics Must modify judgment forms to account for free type variables occurring in types during type checking

Static Semantics Must modify judgment forms to account for free type variables occurring in types during type checking Typo should be 

Well-formed Type

Well-Formed Expressions Remaining rules maintain/assume the following invariant

Example: Polymorphic Compose

Type Soundness Similar to Substitution Lemma Proofs are all similar to previous proofs just with more tedium!

First-Class Polymorphism Easy to obtain just relax distinction between polytypes and monotypes Zero changes to the proof or operational semantics Not supported for many reasons –Interacts badly with type-inference –Does not allow type-erasure in opsem

Effects/Erasure and Polymorphism The expressions Fun and inst similar to roll and unroll –No real need on a executing machine –Just to make type checking and type- soundness easier In second-order system with value restriction can be erased from program ML compilers use type-erasure during compilation

Value Restriction Consider v :  v must be of the form Fun t 1 in Fun t 2 in … Fun t n in e Value restriction only allows type-abstraction over values so with the value restriction we know that “e” is also a value If it is a value than it is safe to remove all the Fun

Example: Value Restriction Consider program below the program is sound because there is a unique reference created with each instantiation of r

Example: Value Restriction Consider erased version below the program is unsound because there is just one reference created with each instantiation of r. SML rejects the above

Polymorphism and Abstraction Parametric polymorphism can be used to enforce data abstraction Parametricity theorem lets us infer information about a value just by inspecting it’s type –Similar to a canonical forms lemmas –Gives us a semantic description of a value based on it’s type

Example: 8 t(t ! t) Consider function f the type 8 t(t ! t) assuming we only care about “interesting” functions (those that terminate) how can functions of that type behave? All functions of that type must simply return their argument. i.e. they all behave as the identity function

Semantics vs. Syntax Note parametricity theorem states any function must “act like” the identity function it may “look” different (fn x => x) (fn x => (((fn f => f) (fn y => y)) x)) (fn x => (1+2;x)) Compare to canonical forms lemmas which states values must be a specific piece of syntax

Other Theorems 8 t(t) No interesting values of this type 8 t(t list ! t list) Interesting functions only examine the “spine” of the list

A Simple Abstraction Theorem Consider N: 8 t(t ! (t ! t) ! t) Interesting functions of this form can only apply their first and second arguments. Fun t in fn z : t is fn s : t ! t is z Fun t in fn z : t is fn s : t ! t is s(z) Fun t in fn z : t is fn s : t ! t is s(s(z))

Formal Definition of Parametricity Define a notion of equivalence of expressions indexed by type

Definition of Parametricity (cont.) Define a notion of equivalence of closed values indexed by type

Definition of Parametricity (cont.) Interesting case is definition of “ all ” Almost arbitrary relation

Parametricity Theorems

Summary Many different forms of “polymorphism” Parametric polymorphism comes in both first-class and second-class forms Parametricity theorems basis for formal claims of data-abstraction