![Page 1: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/1.jpg)
Parametric Polymorphism
COS 441
Princeton University
Fall 2004
![Page 2: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/2.jpg)
Polymorphic Polymorphism
Polymorphism has different meanings in different contexts.Cardelli and Wegner (1985) decompose it as follows
![Page 3: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/3.jpg)
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)
![Page 4: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/4.jpg)
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 inclusionWill study this Friday
![Page 5: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/5.jpg)
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
![Page 6: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/6.jpg)
First vs Second Class
First-class
::= int | 1 ! 2 | t | 8t(
Example: 8t(t ! t) ! 8t’(t’ ! t’) Second-class
::= | 8t( ::= int | 1 ! 2 | t
Example: 8t(8t’((t ! t) ! (t’ ! t’)))
![Page 7: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/7.jpg)
Second Class Polymorphism
Second-class or “prenex” polymorphism the 8 are all at the front of the type
::= | 8t((polytypes)
::= int | 1 ! 2 | t (monotypes)
Polytypes: ,8t(,8 t(8 t’(, intMonotypes: t, int, t ! int,…
![Page 8: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/8.jpg)
PolyMinML
• Supports second-class parametric polymorphism
• Only instantiate polytypes with monotypes
![Page 9: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/9.jpg)
Dynamic Semantics
![Page 10: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/10.jpg)
Static Semantics
Must modify judgment forms to account for free type variables occurring in types during type checking
![Page 11: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/11.jpg)
Static Semantics
Must modify judgment forms to account for free type variables occurring in types during type checking
Typo should be
![Page 12: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/12.jpg)
Well-formed Type
![Page 13: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/13.jpg)
Well-Formed Expressions
Remaining rules maintain/assume the following invariant
![Page 14: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/14.jpg)
Example: Polymorphic Compose
![Page 15: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/15.jpg)
Type Soundness
Similar to Substitution Lemma
Proofs are all similar to previous proofs just with more tedium!
![Page 16: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/16.jpg)
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
![Page 17: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/17.jpg)
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
![Page 18: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/18.jpg)
Value Restriction
Consider v : v must be of the form
Fun t1 in Fun t2 in … Fun tn 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
![Page 19: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/19.jpg)
Example: Value Restriction
Consider program below
the program is sound because there is a unique reference created with each instantiation of r
![Page 20: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/20.jpg)
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
![Page 21: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/21.jpg)
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
![Page 22: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/22.jpg)
Example: 8t(t ! t)
Consider function f the type 8t(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
![Page 23: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/23.jpg)
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
![Page 24: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/24.jpg)
Other Theorems
8t(t) No interesting values of this type
8t(t list ! t list) Interesting functions only examine the “spine”
of the list
![Page 25: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/25.jpg)
A Simple Abstraction Theorem
Consider N: 8t(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))
![Page 26: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/26.jpg)
Formal Definition of Parametricity
Define a notion of equivalence of expressions indexed by type
![Page 27: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/27.jpg)
Definition of Parametricity (cont.)
Define a notion of equivalence of closed values indexed by type
![Page 28: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/28.jpg)
Definition of Parametricity (cont.)
Interesting case is definition of “all”
Almost arbitrary relation
![Page 29: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/29.jpg)
Parametricity Theorems
![Page 30: Parametric Polymorphism COS 441 Princeton University Fall 2004](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d435503460f94a1f27a/html5/thumbnails/30.jpg)
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