Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal:...
Transcript of Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal:...
![Page 1: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/1.jpg)
CSE 505: Programming Languages
Lecture 18 — Parametric Polymorphism
Zach TatlockAutumn 2017
![Page 2: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/2.jpg)
Earlier
Saw structural subtyping
I constraints over record fields
I propagate constraints to “bigger” types
I covariance, contravariance
Provided polymorphism over records with “enough” fields ...but at fixed types.
What if code imposes no constraints on some types?
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 2
![Page 3: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/3.jpg)
Earlier
Saw structural subtyping
I constraints over record fields
I propagate constraints to “bigger” types
I covariance, contravariance
Provided polymorphism over records with “enough” fields ...but at fixed types.
What if code imposes no constraints on some types?
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 2
![Page 4: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/4.jpg)
This Time: Parametric Polymorphism
Some code just doesn’t care what types it’s operating over.
You might even say it works universally...
???
Before we figure out what that means, a word from a luminary:
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 3
![Page 5: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/5.jpg)
This Time: Parametric Polymorphism
Some code just doesn’t care what types it’s operating over.
You might even say it works universally... ???
Before we figure out what that means, a word from a luminary:
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 3
![Page 6: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/6.jpg)
MOVIE TIME!
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 4
![Page 7: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/7.jpg)
Goal: Everybody Wins!
Understand what this interface means and why it matters:
type ’a mylist;
val empty : ’a mylist
val cons : ’a -> ’a mylist -> ’a mylist
val decons : ’a mylist -> ((’a * ’a mylist) option)
val length : ’a mylist -> int
val map : (’a -> ’b) -> ’a mylist -> ’b mylist
From two perspectives:
1. Client: Code against this specification
2. Library: Implement this specification
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5
![Page 8: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/8.jpg)
Goal: Client Wins!
1. Reusability (at different types!)I Different lists with elements of different typesI New reusable functions outside of library, e.g.:
val twocons: ’a -> ’a -> ’a mylist -> ’a mylist
2. Easier, faster, more reliable than subtypingI No downcast to write, run, maybe-fail (cf. Java 1.4 List)
3. Library must “behave the same” for all “type instantiations”!I ’a and ’b held abstract from libraryI e.g., suppose foo: ’a list -> int, then
foo [1;2;3] totally equivalent to foo [(5,4);(7,2);(9,2)]
I Why? Still true if we have downcasts?I Proof left as exercise to the readerI In theory, means less (re-)integration testing
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 6
![Page 9: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/9.jpg)
Goal: Client Wins!
1. Reusability (at different types!)I Different lists with elements of different typesI New reusable functions outside of library, e.g.:
val twocons: ’a -> ’a -> ’a mylist -> ’a mylist
2. Easier, faster, more reliable than subtypingI No downcast to write, run, maybe-fail (cf. Java 1.4 List)
3. Library must “behave the same” for all “type instantiations”!I ’a and ’b held abstract from libraryI e.g., suppose foo: ’a list -> int, then
foo [1;2;3] totally equivalent to foo [(5,4);(7,2);(9,2)]
I Why? Still true if we have downcasts?I Proof left as exercise to the readerI In theory, means less (re-)integration testing
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 6
![Page 10: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/10.jpg)
Goal: Client Wins!
1. Reusability (at different types!)I Different lists with elements of different typesI New reusable functions outside of library, e.g.:
val twocons: ’a -> ’a -> ’a mylist -> ’a mylist
2. Easier, faster, more reliable than subtypingI No downcast to write, run, maybe-fail (cf. Java 1.4 List)
3. Library must “behave the same” for all “type instantiations”!I ’a and ’b held abstract from libraryI e.g., suppose foo: ’a list -> int, then
foo [1;2;3] totally equivalent to foo [(5,4);(7,2);(9,2)]
I Why? Still true if we have downcasts?I Proof left as exercise to the readerI In theory, means less (re-)integration testing
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 6
![Page 11: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/11.jpg)
Goal: Client Wins!
1. Reusability (at different types!)I Different lists with elements of different typesI New reusable functions outside of library, e.g.:
val twocons: ’a -> ’a -> ’a mylist -> ’a mylist
2. Easier, faster, more reliable than subtypingI No downcast to write, run, maybe-fail (cf. Java 1.4 List)
3. Library must “behave the same” for all “type instantiations”!I ’a and ’b held abstract from libraryI e.g., suppose foo: ’a list -> int, then
foo [1;2;3] totally equivalent to foo [(5,4);(7,2);(9,2)]
I Why? Still true if we have downcasts?
I Proof left as exercise to the readerI In theory, means less (re-)integration testing
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 6
![Page 12: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/12.jpg)
Goal: Client Wins!
1. Reusability (at different types!)I Different lists with elements of different typesI New reusable functions outside of library, e.g.:
val twocons: ’a -> ’a -> ’a mylist -> ’a mylist
2. Easier, faster, more reliable than subtypingI No downcast to write, run, maybe-fail (cf. Java 1.4 List)
3. Library must “behave the same” for all “type instantiations”!I ’a and ’b held abstract from libraryI e.g., suppose foo: ’a list -> int, then
foo [1;2;3] totally equivalent to foo [(5,4);(7,2);(9,2)]
I Why? Still true if we have downcasts?I Proof left as exercise to the readerI In theory, means less (re-)integration testing
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 6
![Page 13: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/13.jpg)
Goal: Library Wins!
1. Reusability — all the same reasons client likes it
2. Abstraction of mylist from clients
I Clients can only assume interface, no implementation details
I Free to change/optimize hidden details of ’a mylist
I Clients typechecked knowing only:there exists some type constructor mylist
I Unlike Java/C++ cannot downcast a t mylist to, e.g., a pair
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 7
![Page 14: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/14.jpg)
Start Simple
The mylist interface has a lot going on:
1. Element types held abstract from library
2. List type (constructor) held abstract from client
3. Reuse of type variables constrains expressions over abstract types
4. Lists need some form of recursive type
We’ll focus on (1) and (3):
I First using a formal language with explicit type abstraction
I Then compare and contrast with ML
Note: Much more interesting than “not getting stuck”
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 8
![Page 15: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/15.jpg)
Start Simple
The mylist interface has a lot going on:
1. Element types held abstract from library
2. List type (constructor) held abstract from client
3. Reuse of type variables constrains expressions over abstract types
4. Lists need some form of recursive type
We’ll focus on (1) and (3):
I First using a formal language with explicit type abstraction
I Then compare and contrast with ML
Note: Much more interesting than “not getting stuck”
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 8
![Page 16: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/16.jpg)
Start Simple
The mylist interface has a lot going on:
1. Element types held abstract from library
2. List type (constructor) held abstract from client
3. Reuse of type variables constrains expressions over abstract types
4. Lists need some form of recursive type
We’ll focus on (1) and (3):
I First using a formal language with explicit type abstraction
I Then compare and contrast with ML
Note: Much more interesting than “not getting stuck”
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 8
![Page 17: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/17.jpg)
Recipe for Extension
1. Add syntax
2. Add semantics
3. Add typing rules
4. Patch up type safety proof
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 9
![Page 18: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/18.jpg)
1. Add Syntax
e ::= c | x | λx:τ . e | e e
| Λα. e | e[τ ]τ ::= int | τ → τ | α | ∀α.τv ::= c | λx:τ . e | Λα. eΓ ::= · | Γ, x:τ∆ ::= · | ∆, α
Summary of new things:
I Terms: Type abstraction and type application
I Types: Type variables and universal types
I Type contexts: what type variables are in scope
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 10
![Page 19: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/19.jpg)
1. Add Syntax
e ::= c | x | λx:τ . e | e e | Λα. e
| e[τ ]τ ::= int | τ → τ | α | ∀α.τv ::= c | λx:τ . e | Λα. eΓ ::= · | Γ, x:τ∆ ::= · | ∆, α
Summary of new things:
I Terms: Type abstraction and type application
I Types: Type variables and universal types
I Type contexts: what type variables are in scope
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 10
![Page 20: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/20.jpg)
1. Add Syntax
e ::= c | x | λx:τ . e | e e | Λα. e | e[τ ]
τ ::= int | τ → τ | α | ∀α.τv ::= c | λx:τ . e | Λα. eΓ ::= · | Γ, x:τ∆ ::= · | ∆, α
Summary of new things:
I Terms: Type abstraction and type application
I Types: Type variables and universal types
I Type contexts: what type variables are in scope
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 10
![Page 21: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/21.jpg)
1. Add Syntax
e ::= c | x | λx:τ . e | e e | Λα. e | e[τ ]τ ::= int | τ → τ
| α | ∀α.τv ::= c | λx:τ . e | Λα. eΓ ::= · | Γ, x:τ∆ ::= · | ∆, α
Summary of new things:
I Terms: Type abstraction and type application
I Types: Type variables and universal types
I Type contexts: what type variables are in scope
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 10
![Page 22: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/22.jpg)
1. Add Syntax
e ::= c | x | λx:τ . e | e e | Λα. e | e[τ ]τ ::= int | τ → τ | α
| ∀α.τv ::= c | λx:τ . e | Λα. eΓ ::= · | Γ, x:τ∆ ::= · | ∆, α
Summary of new things:
I Terms: Type abstraction and type application
I Types: Type variables and universal types
I Type contexts: what type variables are in scope
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 10
![Page 23: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/23.jpg)
1. Add Syntax
e ::= c | x | λx:τ . e | e e | Λα. e | e[τ ]τ ::= int | τ → τ | α | ∀α.τ
v ::= c | λx:τ . e | Λα. eΓ ::= · | Γ, x:τ∆ ::= · | ∆, α
Summary of new things:
I Terms: Type abstraction and type application
I Types: Type variables and universal types
I Type contexts: what type variables are in scope
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 10
![Page 24: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/24.jpg)
1. Add Syntax
e ::= c | x | λx:τ . e | e e | Λα. e | e[τ ]τ ::= int | τ → τ | α | ∀α.τv ::= c | λx:τ . e
| Λα. eΓ ::= · | Γ, x:τ∆ ::= · | ∆, α
Summary of new things:
I Terms: Type abstraction and type application
I Types: Type variables and universal types
I Type contexts: what type variables are in scope
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 10
![Page 25: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/25.jpg)
1. Add Syntax
e ::= c | x | λx:τ . e | e e | Λα. e | e[τ ]τ ::= int | τ → τ | α | ∀α.τv ::= c | λx:τ . e | Λα. e
Γ ::= · | Γ, x:τ∆ ::= · | ∆, α
Summary of new things:
I Terms: Type abstraction and type application
I Types: Type variables and universal types
I Type contexts: what type variables are in scope
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 10
![Page 26: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/26.jpg)
1. Add Syntax
e ::= c | x | λx:τ . e | e e | Λα. e | e[τ ]τ ::= int | τ → τ | α | ∀α.τv ::= c | λx:τ . e | Λα. eΓ ::= · | Γ, x:τ
∆ ::= · | ∆, α
Summary of new things:
I Terms: Type abstraction and type application
I Types: Type variables and universal types
I Type contexts: what type variables are in scope
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 10
![Page 27: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/27.jpg)
1. Add Syntax
e ::= c | x | λx:τ . e | e e | Λα. e | e[τ ]τ ::= int | τ → τ | α | ∀α.τv ::= c | λx:τ . e | Λα. eΓ ::= · | Γ, x:τ∆ ::= · | ∆, α
Summary of new things:
I Terms: Type abstraction and type application
I Types: Type variables and universal types
I Type contexts: what type variables are in scope
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 10
![Page 28: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/28.jpg)
1. Add Syntax
e ::= c | x | λx:τ . e | e e | Λα. e | e[τ ]τ ::= int | τ → τ | α | ∀α.τv ::= c | λx:τ . e | Λα. eΓ ::= · | Γ, x:τ∆ ::= · | ∆, α
Summary of new things:
I Terms: Type abstraction and type application
I Types: Type variables and universal types
I Type contexts: what type variables are in scope
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 10
![Page 29: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/29.jpg)
2. Add Semantics
What is this Λ (big lambda) thing? Informally:
1. Λα. e: a value that takes some τ , plugs it in for α, then runs e
I type-check e knowing α is some type, but not which type
2. e[τ ]: crunch e down to some Λα. e′, plug in τ for α, run e′
I choice of τ is irrelevant at run-timeI τ used for type-checking and proof of Preservation
What is this ∀ (upside down “A”) thing? Informally:
Types can use type variables α, β, etc., but only if they’re in scope (justlike term variables)
I Type-checking ∆; Γ ` e : τ uses ∆ to scope type vars in e
I universal type ∀α.τ , brings α into scope for τ
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 11
![Page 30: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/30.jpg)
2. Add Semantics
What is this Λ (big lambda) thing? Informally:
1. Λα. e: a value that takes some τ , plugs it in for α, then runs e
I type-check e knowing α is some type, but not which type
2. e[τ ]: crunch e down to some Λα. e′, plug in τ for α, run e′
I choice of τ is irrelevant at run-timeI τ used for type-checking and proof of Preservation
What is this ∀ (upside down “A”) thing? Informally:
Types can use type variables α, β, etc., but only if they’re in scope (justlike term variables)
I Type-checking ∆; Γ ` e : τ uses ∆ to scope type vars in e
I universal type ∀α.τ , brings α into scope for τ
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 11
![Page 31: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/31.jpg)
2. Add Semantics
What is this Λ (big lambda) thing? Informally:
1. Λα. e: a value that takes some τ , plugs it in for α, then runs e
I type-check e knowing α is some type, but not which type
2. e[τ ]: crunch e down to some Λα. e′, plug in τ for α, run e′
I choice of τ is irrelevant at run-timeI τ used for type-checking and proof of Preservation
What is this ∀ (upside down “A”) thing? Informally:
Types can use type variables α, β, etc., but only if they’re in scope (justlike term variables)
I Type-checking ∆; Γ ` e : τ uses ∆ to scope type vars in e
I universal type ∀α.τ , brings α into scope for τ
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 11
![Page 32: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/32.jpg)
2. Add Semantics
Formal, small-step, CBV, left-to-right operational semantics:
I Recall: Λα. e is a value
e→ e′
Old:e1 → e′1
e1 e2 → e′1 e2
e2 → e′2v e2 → v e′2 (λx:τ . e) v → e[v/x]
New:e→ e′
e[τ ]→ e′[τ ] (Λα. e)[τ ]→ e[τ/α]
Plus now have 3 different kinds of substitution, all defined instraightforward capture-avoiding way:
I e1[e2/x] (old)
I e[τ ′/α] (new)
I τ [τ ′/α] (new)
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 12
![Page 33: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/33.jpg)
2. Add Semantics
Formal, small-step, CBV, left-to-right operational semantics:
I Recall: Λα. e is a value
e→ e′
Old:e1 → e′1
e1 e2 → e′1 e2
e2 → e′2v e2 → v e′2 (λx:τ . e) v → e[v/x]
New:e→ e′
e[τ ]→ e′[τ ] (Λα. e)[τ ]→ e[τ/α]
Plus now have 3 different kinds of substitution, all defined instraightforward capture-avoiding way:
I e1[e2/x] (old)
I e[τ ′/α] (new)
I τ [τ ′/α] (new)
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 12
![Page 34: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/34.jpg)
2. Add Semantics
Formal, small-step, CBV, left-to-right operational semantics:
I Recall: Λα. e is a value
e→ e′
Old:e1 → e′1
e1 e2 → e′1 e2
e2 → e′2v e2 → v e′2 (λx:τ . e) v → e[v/x]
New:e→ e′
e[τ ]→ e′[τ ]
(Λα. e)[τ ]→ e[τ/α]
Plus now have 3 different kinds of substitution, all defined instraightforward capture-avoiding way:
I e1[e2/x] (old)
I e[τ ′/α] (new)
I τ [τ ′/α] (new)
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 12
![Page 35: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/35.jpg)
2. Add Semantics
Formal, small-step, CBV, left-to-right operational semantics:
I Recall: Λα. e is a value
e→ e′
Old:e1 → e′1
e1 e2 → e′1 e2
e2 → e′2v e2 → v e′2 (λx:τ . e) v → e[v/x]
New:e→ e′
e[τ ]→ e′[τ ] (Λα. e)[τ ]→ e[τ/α]
Plus now have 3 different kinds of substitution, all defined instraightforward capture-avoiding way:
I e1[e2/x] (old)
I e[τ ′/α] (new)
I τ [τ ′/α] (new)
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 12
![Page 36: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/36.jpg)
2. Add Semantics
Formal, small-step, CBV, left-to-right operational semantics:
I Recall: Λα. e is a value
e→ e′
Old:e1 → e′1
e1 e2 → e′1 e2
e2 → e′2v e2 → v e′2 (λx:τ . e) v → e[v/x]
New:e→ e′
e[τ ]→ e′[τ ] (Λα. e)[τ ]→ e[τ/α]
Plus now have 3 different kinds of substitution, all defined instraightforward capture-avoiding way:
I e1[e2/x] (old)
I e[τ ′/α] (new)
I τ [τ ′/α] (new)
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 12
![Page 37: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/37.jpg)
2. Add Semantics
Formal, small-step, CBV, left-to-right operational semantics:
I Recall: Λα. e is a value
e→ e′
Old:e1 → e′1
e1 e2 → e′1 e2
e2 → e′2v e2 → v e′2 (λx:τ . e) v → e[v/x]
New:e→ e′
e[τ ]→ e′[τ ] (Λα. e)[τ ]→ e[τ/α]
Plus now have 3 different kinds of substitution, all defined instraightforward capture-avoiding way:
I e1[e2/x] (old)
I e[τ ′/α] (new)
I τ [τ ′/α] (new)
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 12
![Page 38: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/38.jpg)
2. Add Semantics
Formal, small-step, CBV, left-to-right operational semantics:
I Recall: Λα. e is a value
e→ e′
Old:e1 → e′1
e1 e2 → e′1 e2
e2 → e′2v e2 → v e′2 (λx:τ . e) v → e[v/x]
New:e→ e′
e[τ ]→ e′[τ ] (Λα. e)[τ ]→ e[τ/α]
Plus now have 3 different kinds of substitution, all defined instraightforward capture-avoiding way:
I e1[e2/x] (old)
I e[τ ′/α] (new)
I τ [τ ′/α] (new)
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 12
![Page 39: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/39.jpg)
Example
Example (using addition):
(Λα.Λβ. λx : α. λf :α→ β. f x) [int] [int] 3 (λy : int. y + y)
→ (Λβ. λx : int. λf :int→ β. f x) [int] 3 (λy : int. y + y)
→ (λx : int. λf :int→ int. f x) 3 (λy : int. y + y)
→ (λf :int→ int. f 3) (λy : int. y + y)
→ (λy : int. y + y) 3
→ 3 + 3
→ 6
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 13
![Page 40: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/40.jpg)
Example
Example (using addition):
(Λα.Λβ. λx : α. λf :α→ β. f x) [int] [int] 3 (λy : int. y + y)
→ (Λβ. λx : int. λf :int→ β. f x) [int] 3 (λy : int. y + y)
→ (λx : int. λf :int→ int. f x) 3 (λy : int. y + y)
→ (λf :int→ int. f 3) (λy : int. y + y)
→ (λy : int. y + y) 3
→ 3 + 3
→ 6
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 13
![Page 41: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/41.jpg)
Example
Example (using addition):
(Λα.Λβ. λx : α. λf :α→ β. f x) [int] [int] 3 (λy : int. y + y)
→ (Λβ. λx : int. λf :int→ β. f x) [int] 3 (λy : int. y + y)
→ (λx : int. λf :int→ int. f x) 3 (λy : int. y + y)
→ (λf :int→ int. f 3) (λy : int. y + y)
→ (λy : int. y + y) 3
→ 3 + 3
→ 6
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 13
![Page 42: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/42.jpg)
Example
Example (using addition):
(Λα.Λβ. λx : α. λf :α→ β. f x) [int] [int] 3 (λy : int. y + y)
→ (Λβ. λx : int. λf :int→ β. f x) [int] 3 (λy : int. y + y)
→ (λx : int. λf :int→ int. f x) 3 (λy : int. y + y)
→ (λf :int→ int. f 3) (λy : int. y + y)
→ (λy : int. y + y) 3
→ 3 + 3
→ 6
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 13
![Page 43: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/43.jpg)
Example
Example (using addition):
(Λα.Λβ. λx : α. λf :α→ β. f x) [int] [int] 3 (λy : int. y + y)
→ (Λβ. λx : int. λf :int→ β. f x) [int] 3 (λy : int. y + y)
→ (λx : int. λf :int→ int. f x) 3 (λy : int. y + y)
→ (λf :int→ int. f 3) (λy : int. y + y)
→ (λy : int. y + y) 3
→ 3 + 3
→ 6
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 13
![Page 44: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/44.jpg)
Example
Example (using addition):
(Λα.Λβ. λx : α. λf :α→ β. f x) [int] [int] 3 (λy : int. y + y)
→ (Λβ. λx : int. λf :int→ β. f x) [int] 3 (λy : int. y + y)
→ (λx : int. λf :int→ int. f x) 3 (λy : int. y + y)
→ (λf :int→ int. f 3) (λy : int. y + y)
→ (λy : int. y + y) 3
→ 3 + 3
→ 6
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 13
![Page 45: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/45.jpg)
Example
Example (using addition):
(Λα.Λβ. λx : α. λf :α→ β. f x) [int] [int] 3 (λy : int. y + y)
→ (Λβ. λx : int. λf :int→ β. f x) [int] 3 (λy : int. y + y)
→ (λx : int. λf :int→ int. f x) 3 (λy : int. y + y)
→ (λf :int→ int. f 3) (λy : int. y + y)
→ (λy : int. y + y) 3
→ 3 + 3
→ 6
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 13
![Page 46: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/46.jpg)
3. Add Typing Rules
Need to be picky about “no free type variables”
I Typing judgment has the form ∆; Γ ` e : τ(whole program ·; · ` e : τ )
I Uses helper judgment ∆ ` τI “all free type variables in τ are in ∆”
∆ ` τ
α ∈ ∆
∆ ` α ∆ ` int
∆ ` τ1 ∆ ` τ2∆ ` τ1 → τ2
∆, α ` τ∆ ` ∀α.τ
Rules are boring, but smart people found out the hard way thatallowing free type variables is a pernicious source oflanguage/compiler bugs.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 14
![Page 47: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/47.jpg)
3. Add Typing Rules
Need to be picky about “no free type variables”
I Typing judgment has the form ∆; Γ ` e : τ(whole program ·; · ` e : τ )
I Uses helper judgment ∆ ` τI “all free type variables in τ are in ∆”
∆ ` τ
α ∈ ∆
∆ ` α
∆ ` int
∆ ` τ1 ∆ ` τ2∆ ` τ1 → τ2
∆, α ` τ∆ ` ∀α.τ
Rules are boring, but smart people found out the hard way thatallowing free type variables is a pernicious source oflanguage/compiler bugs.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 14
![Page 48: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/48.jpg)
3. Add Typing Rules
Need to be picky about “no free type variables”
I Typing judgment has the form ∆; Γ ` e : τ(whole program ·; · ` e : τ )
I Uses helper judgment ∆ ` τI “all free type variables in τ are in ∆”
∆ ` τ
α ∈ ∆
∆ ` α ∆ ` int
∆ ` τ1 ∆ ` τ2∆ ` τ1 → τ2
∆, α ` τ∆ ` ∀α.τ
Rules are boring, but smart people found out the hard way thatallowing free type variables is a pernicious source oflanguage/compiler bugs.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 14
![Page 49: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/49.jpg)
3. Add Typing Rules
Need to be picky about “no free type variables”
I Typing judgment has the form ∆; Γ ` e : τ(whole program ·; · ` e : τ )
I Uses helper judgment ∆ ` τI “all free type variables in τ are in ∆”
∆ ` τ
α ∈ ∆
∆ ` α ∆ ` int
∆ ` τ1 ∆ ` τ2∆ ` τ1 → τ2
∆, α ` τ∆ ` ∀α.τ
Rules are boring, but smart people found out the hard way thatallowing free type variables is a pernicious source oflanguage/compiler bugs.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 14
![Page 50: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/50.jpg)
3. Add Typing Rules
Need to be picky about “no free type variables”
I Typing judgment has the form ∆; Γ ` e : τ(whole program ·; · ` e : τ )
I Uses helper judgment ∆ ` τI “all free type variables in τ are in ∆”
∆ ` τ
α ∈ ∆
∆ ` α ∆ ` int
∆ ` τ1 ∆ ` τ2∆ ` τ1 → τ2
∆, α ` τ∆ ` ∀α.τ
Rules are boring, but smart people found out the hard way thatallowing free type variables is a pernicious source oflanguage/compiler bugs.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 14
![Page 51: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/51.jpg)
3. Add Typing Rules
Need to be picky about “no free type variables”
I Typing judgment has the form ∆; Γ ` e : τ(whole program ·; · ` e : τ )
I Uses helper judgment ∆ ` τI “all free type variables in τ are in ∆”
∆ ` τ
α ∈ ∆
∆ ` α ∆ ` int
∆ ` τ1 ∆ ` τ2∆ ` τ1 → τ2
∆, α ` τ∆ ` ∀α.τ
Rules are boring, but smart people found out the hard way thatallowing free type variables is a pernicious source oflanguage/compiler bugs.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 14
![Page 52: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/52.jpg)
3. Add Typing Rules
Old (with one technical change to prevent free type variables):
∆; Γ ` x : Γ(x) ∆; Γ ` c : int
∆; Γ, x:τ1 ` e : τ2 ∆ ` τ1∆; Γ ` λx:τ1. e : τ1 → τ2
∆; Γ `e1 : τ2→τ1 ∆; Γ ` e2 : τ2
∆; Γ ` e1 e2 : τ1
New:
∆, α; Γ ` e : τ1
∆; Γ ` Λα. e : ∀α.τ1∆; Γ ` e : ∀α.τ1 ∆ ` τ2
∆; Γ ` e[τ2] : τ1[τ2/α]
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 15
![Page 53: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/53.jpg)
3. Add Typing Rules
Old (with one technical change to prevent free type variables):
∆; Γ ` x : Γ(x) ∆; Γ ` c : int
∆; Γ, x:τ1 ` e : τ2 ∆ ` τ1∆; Γ ` λx:τ1. e : τ1 → τ2
∆; Γ `e1 : τ2→τ1 ∆; Γ ` e2 : τ2
∆; Γ ` e1 e2 : τ1
New:
∆, α; Γ ` e : τ1
∆; Γ ` Λα. e : ∀α.τ1
∆; Γ ` e : ∀α.τ1 ∆ ` τ2∆; Γ ` e[τ2] : τ1[τ2/α]
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 15
![Page 54: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/54.jpg)
3. Add Typing Rules
Old (with one technical change to prevent free type variables):
∆; Γ ` x : Γ(x) ∆; Γ ` c : int
∆; Γ, x:τ1 ` e : τ2 ∆ ` τ1∆; Γ ` λx:τ1. e : τ1 → τ2
∆; Γ `e1 : τ2→τ1 ∆; Γ ` e2 : τ2
∆; Γ ` e1 e2 : τ1
New:
∆, α; Γ ` e : τ1
∆; Γ ` Λα. e : ∀α.τ1∆; Γ ` e : ∀α.τ1 ∆ ` τ2
∆; Γ ` e[τ2] : τ1[τ2/α]
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 15
![Page 55: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/55.jpg)
Example
Example (using addition):
(Λα.Λβ. λx : α. λf :α→ β. f x) [int] [int] 3 (λy : int. y + y)
Ouch.
Just a syntax-directed derivation by instantiating the typing rules.Still, machines are better suited to this stuff.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 16
![Page 56: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/56.jpg)
Example
Example (using addition):
(Λα.Λβ. λx : α. λf :α→ β. f x) [int] [int] 3 (λy : int. y + y)
Ouch.
Just a syntax-directed derivation by instantiating the typing rules.Still, machines are better suited to this stuff.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 16
![Page 57: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/57.jpg)
Example
Example (using addition):
(Λα.Λβ. λx : α. λf :α→ β. f x) [int] [int] 3 (λy : int. y + y)
Ouch.
Just a syntax-directed derivation by instantiating the typing rules.Still, machines are better suited to this stuff.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 16
![Page 58: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/58.jpg)
System F (Tah Dah!)
e ::= c | x | λx:τ . e | e e | Λα. e | e[τ ]τ ::= int | τ → τ | α | ∀α.τv ::= c | λx:τ . e | Λα. eΓ ::= · | Γ, x:τ∆ ::= · | ∆, α
e→ e′
e e2 → e′ e2
e→ e′
v e→ v e′e→ e′
e[τ ]→ e′[τ ]
(λx:τ . e) v → e[v/x] (Λα. e)[τ ]→ e[τ/α]
∆; Γ ` x : Γ(x) ∆; Γ ` c : int
∆; Γ, x:τ1 ` e : τ2 ∆ ` τ1∆; Γ ` λx:τ1. e : τ1 → τ2
∆, α; Γ ` e : τ1
∆; Γ ` Λα. e : ∀α.τ1
∆; Γ `e1 : τ2→τ1 ∆; Γ ` e2 : τ2
∆; Γ ` e1 e2 : τ1
∆; Γ `e : ∀α.τ1 ∆`τ2∆; Γ ` e[τ2] : τ1[τ2/α]
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 17
![Page 59: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/59.jpg)
Examples
Perhaps the simplest polymorphic function...
Let id = Λα. λx : α. x
I id has type
∀α.α→ α
I id [int] has type int→ int
I id [int ∗ int] has type (int ∗ int)→ (int ∗ int)
I (id [∀α.α→ α]) id has type ∀α.α→ α
In ML you can’t do the last one! What?!
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 18
![Page 60: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/60.jpg)
Examples
Perhaps the simplest polymorphic function...
Let id = Λα. λx : α. x
I id has type ∀α.α→ α
I id [int] has type int→ int
I id [int ∗ int] has type (int ∗ int)→ (int ∗ int)
I (id [∀α.α→ α]) id has type ∀α.α→ α
In ML you can’t do the last one! What?!
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 18
![Page 61: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/61.jpg)
Examples
Perhaps the simplest polymorphic function...
Let id = Λα. λx : α. x
I id has type ∀α.α→ α
I id [int] has type
int→ int
I id [int ∗ int] has type (int ∗ int)→ (int ∗ int)
I (id [∀α.α→ α]) id has type ∀α.α→ α
In ML you can’t do the last one! What?!
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 18
![Page 62: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/62.jpg)
Examples
Perhaps the simplest polymorphic function...
Let id = Λα. λx : α. x
I id has type ∀α.α→ α
I id [int] has type int→ int
I id [int ∗ int] has type (int ∗ int)→ (int ∗ int)
I (id [∀α.α→ α]) id has type ∀α.α→ α
In ML you can’t do the last one! What?!
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 18
![Page 63: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/63.jpg)
Examples
Perhaps the simplest polymorphic function...
Let id = Λα. λx : α. x
I id has type ∀α.α→ α
I id [int] has type int→ int
I id [int ∗ int] has type
(int ∗ int)→ (int ∗ int)
I (id [∀α.α→ α]) id has type ∀α.α→ α
In ML you can’t do the last one! What?!
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 18
![Page 64: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/64.jpg)
Examples
Perhaps the simplest polymorphic function...
Let id = Λα. λx : α. x
I id has type ∀α.α→ α
I id [int] has type int→ int
I id [int ∗ int] has type (int ∗ int)→ (int ∗ int)
I (id [∀α.α→ α]) id has type ∀α.α→ α
In ML you can’t do the last one! What?!
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 18
![Page 65: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/65.jpg)
Examples
Perhaps the simplest polymorphic function...
Let id = Λα. λx : α. x
I id has type ∀α.α→ α
I id [int] has type int→ int
I id [int ∗ int] has type (int ∗ int)→ (int ∗ int)
I (id [∀α.α→ α]) id has type
∀α.α→ α
In ML you can’t do the last one! What?!
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 18
![Page 66: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/66.jpg)
Examples
Perhaps the simplest polymorphic function...
Let id = Λα. λx : α. x
I id has type ∀α.α→ α
I id [int] has type int→ int
I id [int ∗ int] has type (int ∗ int)→ (int ∗ int)
I (id [∀α.α→ α]) id has type ∀α.α→ α
In ML you can’t do the last one! What?!
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 18
![Page 67: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/67.jpg)
Examples
Perhaps the simplest polymorphic function...
Let id = Λα. λx : α. x
I id has type ∀α.α→ α
I id [int] has type int→ int
I id [int ∗ int] has type (int ∗ int)→ (int ∗ int)
I (id [∀α.α→ α]) id has type ∀α.α→ α
In ML you can’t do the last one! What?!
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 18
![Page 68: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/68.jpg)
More Examples
Let apply1 = Λα. Λβ. λx : α. λf : α→ β. f x
I apply1 has type
∀α.∀β.α→ (α→ β)→ β
I ·; g:int→ int ` (apply1 [int][int] 3 g) : int
Let apply2 = Λα. λx : α. Λβ. λf : α→ β. f x
I apply2 has type ∀α.α→ (∀β.(α→ β)→ β)(also impossible in ML!)
I ·; g:int→ string, h:int→ int `(let z = apply2 [int] in z (z 3 [int] h) [string] g) :string
Let twice = Λα. λx : α. λf : α→ α. f (f x).
I twice has type ∀α.α→ (α→ α)→ α
I Could this be any more polymorphic?
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 19
![Page 69: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/69.jpg)
More Examples
Let apply1 = Λα. Λβ. λx : α. λf : α→ β. f x
I apply1 has type ∀α.∀β.α→ (α→ β)→ β
I ·; g:int→ int ` (apply1 [int][int] 3 g) : int
Let apply2 = Λα. λx : α. Λβ. λf : α→ β. f x
I apply2 has type ∀α.α→ (∀β.(α→ β)→ β)(also impossible in ML!)
I ·; g:int→ string, h:int→ int `(let z = apply2 [int] in z (z 3 [int] h) [string] g) :string
Let twice = Λα. λx : α. λf : α→ α. f (f x).
I twice has type ∀α.α→ (α→ α)→ α
I Could this be any more polymorphic?
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 19
![Page 70: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/70.jpg)
More Examples
Let apply1 = Λα. Λβ. λx : α. λf : α→ β. f x
I apply1 has type ∀α.∀β.α→ (α→ β)→ β
I ·; g:int→ int ` (apply1 [int][int] 3 g) : int
Let apply2 = Λα. λx : α. Λβ. λf : α→ β. f x
I apply2 has type ∀α.α→ (∀β.(α→ β)→ β)(also impossible in ML!)
I ·; g:int→ string, h:int→ int `(let z = apply2 [int] in z (z 3 [int] h) [string] g) :string
Let twice = Λα. λx : α. λf : α→ α. f (f x).
I twice has type ∀α.α→ (α→ α)→ α
I Could this be any more polymorphic?
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 19
![Page 71: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/71.jpg)
More Examples
Let apply1 = Λα. Λβ. λx : α. λf : α→ β. f x
I apply1 has type ∀α.∀β.α→ (α→ β)→ β
I ·; g:int→ int ` (apply1 [int][int] 3 g) : int
Let apply2 = Λα. λx : α. Λβ. λf : α→ β. f x
I apply2 has type
∀α.α→ (∀β.(α→ β)→ β)(also impossible in ML!)
I ·; g:int→ string, h:int→ int `(let z = apply2 [int] in z (z 3 [int] h) [string] g) :string
Let twice = Λα. λx : α. λf : α→ α. f (f x).
I twice has type ∀α.α→ (α→ α)→ α
I Could this be any more polymorphic?
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 19
![Page 72: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/72.jpg)
More Examples
Let apply1 = Λα. Λβ. λx : α. λf : α→ β. f x
I apply1 has type ∀α.∀β.α→ (α→ β)→ β
I ·; g:int→ int ` (apply1 [int][int] 3 g) : int
Let apply2 = Λα. λx : α. Λβ. λf : α→ β. f x
I apply2 has type ∀α.α→ (∀β.(α→ β)→ β)
(also impossible in ML!)
I ·; g:int→ string, h:int→ int `(let z = apply2 [int] in z (z 3 [int] h) [string] g) :string
Let twice = Λα. λx : α. λf : α→ α. f (f x).
I twice has type ∀α.α→ (α→ α)→ α
I Could this be any more polymorphic?
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 19
![Page 73: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/73.jpg)
More Examples
Let apply1 = Λα. Λβ. λx : α. λf : α→ β. f x
I apply1 has type ∀α.∀β.α→ (α→ β)→ β
I ·; g:int→ int ` (apply1 [int][int] 3 g) : int
Let apply2 = Λα. λx : α. Λβ. λf : α→ β. f x
I apply2 has type ∀α.α→ (∀β.(α→ β)→ β)(also impossible in ML!)
I ·; g:int→ string, h:int→ int `(let z = apply2 [int] in z (z 3 [int] h) [string] g) :string
Let twice = Λα. λx : α. λf : α→ α. f (f x).
I twice has type ∀α.α→ (α→ α)→ α
I Could this be any more polymorphic?
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 19
![Page 74: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/74.jpg)
More Examples
Let apply1 = Λα. Λβ. λx : α. λf : α→ β. f x
I apply1 has type ∀α.∀β.α→ (α→ β)→ β
I ·; g:int→ int ` (apply1 [int][int] 3 g) : int
Let apply2 = Λα. λx : α. Λβ. λf : α→ β. f x
I apply2 has type ∀α.α→ (∀β.(α→ β)→ β)(also impossible in ML!)
I ·; g:int→ string, h:int→ int `(let z = apply2 [int] in z (z 3 [int] h) [string] g) :string
Let twice = Λα. λx : α. λf : α→ α. f (f x).
I twice has type ∀α.α→ (α→ α)→ α
I Could this be any more polymorphic?
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 19
![Page 75: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/75.jpg)
More Examples
Let apply1 = Λα. Λβ. λx : α. λf : α→ β. f x
I apply1 has type ∀α.∀β.α→ (α→ β)→ β
I ·; g:int→ int ` (apply1 [int][int] 3 g) : int
Let apply2 = Λα. λx : α. Λβ. λf : α→ β. f x
I apply2 has type ∀α.α→ (∀β.(α→ β)→ β)(also impossible in ML!)
I ·; g:int→ string, h:int→ int `(let z = apply2 [int] in z (z 3 [int] h) [string] g) :string
Let twice = Λα. λx : α. λf : α→ α. f (f x).
I twice has type
∀α.α→ (α→ α)→ α
I Could this be any more polymorphic?
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 19
![Page 76: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/76.jpg)
More Examples
Let apply1 = Λα. Λβ. λx : α. λf : α→ β. f x
I apply1 has type ∀α.∀β.α→ (α→ β)→ β
I ·; g:int→ int ` (apply1 [int][int] 3 g) : int
Let apply2 = Λα. λx : α. Λβ. λf : α→ β. f x
I apply2 has type ∀α.α→ (∀β.(α→ β)→ β)(also impossible in ML!)
I ·; g:int→ string, h:int→ int `(let z = apply2 [int] in z (z 3 [int] h) [string] g) :string
Let twice = Λα. λx : α. λf : α→ α. f (f x).
I twice has type ∀α.α→ (α→ α)→ α
I Could this be any more polymorphic?
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 19
![Page 77: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/77.jpg)
More Examples
Let apply1 = Λα. Λβ. λx : α. λf : α→ β. f x
I apply1 has type ∀α.∀β.α→ (α→ β)→ β
I ·; g:int→ int ` (apply1 [int][int] 3 g) : int
Let apply2 = Λα. λx : α. Λβ. λf : α→ β. f x
I apply2 has type ∀α.α→ (∀β.(α→ β)→ β)(also impossible in ML!)
I ·; g:int→ string, h:int→ int `(let z = apply2 [int] in z (z 3 [int] h) [string] g) :string
Let twice = Λα. λx : α. λf : α→ α. f (f x).
I twice has type ∀α.α→ (α→ α)→ α
I Could this be any more polymorphic?
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 19
![Page 78: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/78.jpg)
4. Type Safety and Metatheory
I Safety: System F is type-safeI Need a Type Substitution Lemma
I Termination: All programs terminateI Even with self application — we saw id [τ ] id
I Parametricity, a.k.a. “theorems for free”I Example: If ·; · ` e : ∀α.∀β.(α ∗ β)→ (β ∗ α),
then e is equivalent to Λα. Λβ. λx:α ∗ β. (x.2, x.1).Every term with this type is the swap function!!
I Intuition: e has no way to make an α or a β and it cannot tellwhat α or β are or raise an exception or diverge...
I How many terms have type ∀α.(α→ α)→ (α→ α)?
Note: Mutation breaks everything :(
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 20
![Page 79: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/79.jpg)
4. Type Safety and Metatheory
I Safety: System F is type-safeI Need a Type Substitution Lemma
I Termination: All programs terminateI Even with self application — we saw id [τ ] id
I Parametricity, a.k.a. “theorems for free”I Example: If ·; · ` e : ∀α.∀β.(α ∗ β)→ (β ∗ α),
then e is equivalent to Λα. Λβ. λx:α ∗ β. (x.2, x.1).Every term with this type is the swap function!!
I Intuition: e has no way to make an α or a β and it cannot tellwhat α or β are or raise an exception or diverge...
I How many terms have type ∀α.(α→ α)→ (α→ α)?
Note: Mutation breaks everything :(
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 20
![Page 80: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/80.jpg)
4. Type Safety and Metatheory
I Safety: System F is type-safeI Need a Type Substitution Lemma
I Termination: All programs terminateI Even with self application — we saw id [τ ] id
I Parametricity, a.k.a. “theorems for free”I Example: If ·; · ` e : ∀α.∀β.(α ∗ β)→ (β ∗ α),
then e is equivalent to Λα. Λβ. λx:α ∗ β. (x.2, x.1).
Every term with this type is the swap function!!
I Intuition: e has no way to make an α or a β and it cannot tellwhat α or β are or raise an exception or diverge...
I How many terms have type ∀α.(α→ α)→ (α→ α)?
Note: Mutation breaks everything :(
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 20
![Page 81: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/81.jpg)
4. Type Safety and Metatheory
I Safety: System F is type-safeI Need a Type Substitution Lemma
I Termination: All programs terminateI Even with self application — we saw id [τ ] id
I Parametricity, a.k.a. “theorems for free”I Example: If ·; · ` e : ∀α.∀β.(α ∗ β)→ (β ∗ α),
then e is equivalent to Λα. Λβ. λx:α ∗ β. (x.2, x.1).Every term with this type is the swap function!!
I Intuition: e has no way to make an α or a β and it cannot tellwhat α or β are or raise an exception or diverge...
I How many terms have type ∀α.(α→ α)→ (α→ α)?
Note: Mutation breaks everything :(
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 20
![Page 82: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/82.jpg)
4. Type Safety and Metatheory
I Safety: System F is type-safeI Need a Type Substitution Lemma
I Termination: All programs terminateI Even with self application — we saw id [τ ] id
I Parametricity, a.k.a. “theorems for free”I Example: If ·; · ` e : ∀α.∀β.(α ∗ β)→ (β ∗ α),
then e is equivalent to Λα. Λβ. λx:α ∗ β. (x.2, x.1).Every term with this type is the swap function!!
I Intuition: e has no way to make an α or a β and it cannot tellwhat α or β are or raise an exception or diverge...
I How many terms have type ∀α.(α→ α)→ (α→ α)?
Note: Mutation breaks everything :(
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 20
![Page 83: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/83.jpg)
4. Type Safety and Metatheory
I Safety: System F is type-safeI Need a Type Substitution Lemma
I Termination: All programs terminateI Even with self application — we saw id [τ ] id
I Parametricity, a.k.a. “theorems for free”I Example: If ·; · ` e : ∀α.∀β.(α ∗ β)→ (β ∗ α),
then e is equivalent to Λα. Λβ. λx:α ∗ β. (x.2, x.1).Every term with this type is the swap function!!
I Intuition: e has no way to make an α or a β and it cannot tellwhat α or β are or raise an exception or diverge...
I How many terms have type ∀α.(α→ α)→ (α→ α)?
Note: Mutation breaks everything :(
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 20
![Page 84: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/84.jpg)
4. Type Safety and Metatheory
I Safety: System F is type-safeI Need a Type Substitution Lemma
I Termination: All programs terminateI Even with self application — we saw id [τ ] id
I Parametricity, a.k.a. “theorems for free”I Example: If ·; · ` e : ∀α.∀β.(α ∗ β)→ (β ∗ α),
then e is equivalent to Λα. Λβ. λx:α ∗ β. (x.2, x.1).Every term with this type is the swap function!!
I Intuition: e has no way to make an α or a β and it cannot tellwhat α or β are or raise an exception or diverge...
I How many terms have type ∀α.(α→ α)→ (α→ α)?
Note: Mutation breaks everything :(
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 20
![Page 85: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/85.jpg)
What next?
Now that we have System F...
I What hath we wrought? Example of our mighty new powers.
I How/why ML is more restrictive and implicit.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 21
![Page 86: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/86.jpg)
Security from safety?
Example: A process e should not access files it did not open(fopen checks permissions)
Require an untrusted process e to type-check as follows:·; · ` e : ∀α.{fopen : string→ α, fread : α→ int} → unit
This type ensures that a process won’t “forge a file handle” andpass it to fread
So fread doesn’t need to check (faster), file handles don’t need tobe encrypted (safer), etc.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 22
![Page 87: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/87.jpg)
Security from safety?
Example: A process e should not access files it did not open(fopen checks permissions)
Require an untrusted process e to type-check as follows:·; · ` e : ∀α.{fopen : string→ α, fread : α→ int} → unit
This type ensures that a process won’t “forge a file handle” andpass it to fread
So fread doesn’t need to check (faster), file handles don’t need tobe encrypted (safer), etc.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 22
![Page 88: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/88.jpg)
Security from safety?
Example: A process e should not access files it did not open(fopen checks permissions)
Require an untrusted process e to type-check as follows:·; · ` e : ∀α.{fopen : string→ α, fread : α→ int} → unit
This type ensures that a process won’t “forge a file handle” andpass it to fread
So fread doesn’t need to check (faster), file handles don’t need tobe encrypted (safer), etc.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 22
![Page 89: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/89.jpg)
Moral of Example
In STLC, type safety just meant not getting stuck
Type abstraction gives us new powers, e.g. secure interfaces!
Suppose we (the system library) implement file-handles as ints.Then we instantiate α with int, but untrusted code cannot tell
Memory safety is a necessary but insufficient condition forlanguage-based enforcement of strong abstractions
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 23
![Page 90: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/90.jpg)
Moral of Example
In STLC, type safety just meant not getting stuck
Type abstraction gives us new powers, e.g. secure interfaces!
Suppose we (the system library) implement file-handles as ints.Then we instantiate α with int, but untrusted code cannot tell
Memory safety is a necessary but insufficient condition forlanguage-based enforcement of strong abstractions
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 23
![Page 91: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/91.jpg)
Are types used at run-time?
We said polymorphism was about “many types for same term”,but for clarity and easy checking, we changed:
I The syntax via Λα. e and e [τ ]
I The operational semantics via type substitution
I The type system via ∆
Claim: The operational semantics did not “really” change; typesneed not exist at run-time
More formally: Erasing all types from System F produces anequivalent program in the untyped lambda calculus
Strengthened induction hypothesis: If e→ e1 in System F anderase(e)→ e2 in untyped lambda-calculus, thene2 = erase(e1)
“Erasure and evaluation commute”
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 24
![Page 92: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/92.jpg)
Are types used at run-time?
We said polymorphism was about “many types for same term”,but for clarity and easy checking, we changed:
I The syntax via Λα. e and e [τ ]
I The operational semantics via type substitution
I The type system via ∆
Claim: The operational semantics did not “really” change; typesneed not exist at run-time
More formally: Erasing all types from System F produces anequivalent program in the untyped lambda calculus
Strengthened induction hypothesis: If e→ e1 in System F anderase(e)→ e2 in untyped lambda-calculus, thene2 = erase(e1)
“Erasure and evaluation commute”
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 24
![Page 93: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/93.jpg)
Are types used at run-time?
We said polymorphism was about “many types for same term”,but for clarity and easy checking, we changed:
I The syntax via Λα. e and e [τ ]
I The operational semantics via type substitution
I The type system via ∆
Claim: The operational semantics did not “really” change; typesneed not exist at run-time
More formally: Erasing all types from System F produces anequivalent program in the untyped lambda calculus
Strengthened induction hypothesis: If e→ e1 in System F anderase(e)→ e2 in untyped lambda-calculus, thene2 = erase(e1)
“Erasure and evaluation commute”Zach Tatlock CSE 505 Autumn 2017, Lecture 18 24
![Page 94: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/94.jpg)
Erasure
Erasure is easy to define:
erase(c) =
cerase(x) = x
erase(e1 e2) = erase(e1) erase(e2)erase(λx:τ . e) = λx. erase(e)erase(Λα. e) = λ . erase(e)erase(e [τ ]) = erase(e) 0
In pure System F, preserving evaluation order isn’t crucial, but it iswith fix, exceptions, mutation, etc.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 25
![Page 95: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/95.jpg)
Erasure
Erasure is easy to define:
erase(c) = c
erase(x) = xerase(e1 e2) = erase(e1) erase(e2)
erase(λx:τ . e) = λx. erase(e)erase(Λα. e) = λ . erase(e)erase(e [τ ]) = erase(e) 0
In pure System F, preserving evaluation order isn’t crucial, but it iswith fix, exceptions, mutation, etc.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 25
![Page 96: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/96.jpg)
Erasure
Erasure is easy to define:
erase(c) = cerase(x) =
xerase(e1 e2) = erase(e1) erase(e2)
erase(λx:τ . e) = λx. erase(e)erase(Λα. e) = λ . erase(e)erase(e [τ ]) = erase(e) 0
In pure System F, preserving evaluation order isn’t crucial, but it iswith fix, exceptions, mutation, etc.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 25
![Page 97: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/97.jpg)
Erasure
Erasure is easy to define:
erase(c) = cerase(x) = x
erase(e1 e2) = erase(e1) erase(e2)erase(λx:τ . e) = λx. erase(e)erase(Λα. e) = λ . erase(e)erase(e [τ ]) = erase(e) 0
In pure System F, preserving evaluation order isn’t crucial, but it iswith fix, exceptions, mutation, etc.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 25
![Page 98: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/98.jpg)
Erasure
Erasure is easy to define:
erase(c) = cerase(x) = x
erase(e1 e2) =
erase(e1) erase(e2)erase(λx:τ . e) = λx. erase(e)erase(Λα. e) = λ . erase(e)erase(e [τ ]) = erase(e) 0
In pure System F, preserving evaluation order isn’t crucial, but it iswith fix, exceptions, mutation, etc.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 25
![Page 99: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/99.jpg)
Erasure
Erasure is easy to define:
erase(c) = cerase(x) = x
erase(e1 e2) = erase(e1) erase(e2)
erase(λx:τ . e) = λx. erase(e)erase(Λα. e) = λ . erase(e)erase(e [τ ]) = erase(e) 0
In pure System F, preserving evaluation order isn’t crucial, but it iswith fix, exceptions, mutation, etc.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 25
![Page 100: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/100.jpg)
Erasure
Erasure is easy to define:
erase(c) = cerase(x) = x
erase(e1 e2) = erase(e1) erase(e2)erase(λx:τ . e) =
λx. erase(e)erase(Λα. e) = λ . erase(e)erase(e [τ ]) = erase(e) 0
In pure System F, preserving evaluation order isn’t crucial, but it iswith fix, exceptions, mutation, etc.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 25
![Page 101: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/101.jpg)
Erasure
Erasure is easy to define:
erase(c) = cerase(x) = x
erase(e1 e2) = erase(e1) erase(e2)erase(λx:τ . e) = λx. erase(e)
erase(Λα. e) = λ . erase(e)erase(e [τ ]) = erase(e) 0
In pure System F, preserving evaluation order isn’t crucial, but it iswith fix, exceptions, mutation, etc.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 25
![Page 102: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/102.jpg)
Erasure
Erasure is easy to define:
erase(c) = cerase(x) = x
erase(e1 e2) = erase(e1) erase(e2)erase(λx:τ . e) = λx. erase(e)erase(Λα. e) =
λ . erase(e)erase(e [τ ]) = erase(e) 0
In pure System F, preserving evaluation order isn’t crucial, but it iswith fix, exceptions, mutation, etc.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 25
![Page 103: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/103.jpg)
Erasure
Erasure is easy to define:
erase(c) = cerase(x) = x
erase(e1 e2) = erase(e1) erase(e2)erase(λx:τ . e) = λx. erase(e)erase(Λα. e) = λ . erase(e)
erase(e [τ ]) = erase(e) 0
In pure System F, preserving evaluation order isn’t crucial, but it iswith fix, exceptions, mutation, etc.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 25
![Page 104: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/104.jpg)
Erasure
Erasure is easy to define:
erase(c) = cerase(x) = x
erase(e1 e2) = erase(e1) erase(e2)erase(λx:τ . e) = λx. erase(e)erase(Λα. e) = λ . erase(e)erase(e [τ ]) =
erase(e) 0
In pure System F, preserving evaluation order isn’t crucial, but it iswith fix, exceptions, mutation, etc.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 25
![Page 105: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/105.jpg)
Erasure
Erasure is easy to define:
erase(c) = cerase(x) = x
erase(e1 e2) = erase(e1) erase(e2)erase(λx:τ . e) = λx. erase(e)erase(Λα. e) = λ . erase(e)erase(e [τ ]) = erase(e) 0
In pure System F, preserving evaluation order isn’t crucial, but it iswith fix, exceptions, mutation, etc.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 25
![Page 106: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/106.jpg)
Erasure
Erasure is easy to define:
erase(c) = cerase(x) = x
erase(e1 e2) = erase(e1) erase(e2)erase(λx:τ . e) = λx. erase(e)erase(Λα. e) = λ . erase(e)erase(e [τ ]) = erase(e) 0
In pure System F, preserving evaluation order isn’t crucial, but it iswith fix, exceptions, mutation, etc.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 25
![Page 107: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/107.jpg)
Connection to reality... or at least ML
System F has been one of the most important theoretical PLmodels since the 1970s and inspires languages like ML.
But you have seen ML polymorphism and it looks different. Infact, it is an implicitly typed restriction of System F.
These two qualifications ((1) implicit, (2) restriction) are deeplyrelated.
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 26
![Page 108: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/108.jpg)
ML Restrictions
I All types have the form ∀α1, . . . , αn.τ where n ≥ 0 and τhas no ∀. (Prenex-quantification; no first-classpolymorphism.)
I Only let (rec) variables (e.g., x in let x = e1 in e2) canhave polymorphic types. So n = 0 for function arguments,pattern variables, etc. (Let-bound polymorphism)
I So cannot (always) desugar let to λ in ML
I In let rec f x = e1 in e2, the variable f can have type∀α1, . . . , αn.τ1 → τ2 only if every use of f in e1
instantiates each αi with αi. (No polymorphic recursion)
I Let variables can be polymorphic only if e1 is a “syntacticvalue”
I A variable, constant, function definition, ...I Called the “value restriction” (relaxed partially in OCaml)
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 27
![Page 109: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/109.jpg)
ML Restrictions
I All types have the form ∀α1, . . . , αn.τ where n ≥ 0 and τhas no ∀. (Prenex-quantification; no first-classpolymorphism.)
I Only let (rec) variables (e.g., x in let x = e1 in e2) canhave polymorphic types. So n = 0 for function arguments,pattern variables, etc. (Let-bound polymorphism)
I So cannot (always) desugar let to λ in ML
I In let rec f x = e1 in e2, the variable f can have type∀α1, . . . , αn.τ1 → τ2 only if every use of f in e1
instantiates each αi with αi. (No polymorphic recursion)
I Let variables can be polymorphic only if e1 is a “syntacticvalue”
I A variable, constant, function definition, ...I Called the “value restriction” (relaxed partially in OCaml)
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 27
![Page 110: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/110.jpg)
ML Restrictions
I All types have the form ∀α1, . . . , αn.τ where n ≥ 0 and τhas no ∀. (Prenex-quantification; no first-classpolymorphism.)
I Only let (rec) variables (e.g., x in let x = e1 in e2) canhave polymorphic types. So n = 0 for function arguments,pattern variables, etc. (Let-bound polymorphism)
I So cannot (always) desugar let to λ in ML
I In let rec f x = e1 in e2, the variable f can have type∀α1, . . . , αn.τ1 → τ2 only if every use of f in e1
instantiates each αi with αi. (No polymorphic recursion)
I Let variables can be polymorphic only if e1 is a “syntacticvalue”
I A variable, constant, function definition, ...I Called the “value restriction” (relaxed partially in OCaml)
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 27
![Page 111: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/111.jpg)
ML Restrictions
I All types have the form ∀α1, . . . , αn.τ where n ≥ 0 and τhas no ∀. (Prenex-quantification; no first-classpolymorphism.)
I Only let (rec) variables (e.g., x in let x = e1 in e2) canhave polymorphic types. So n = 0 for function arguments,pattern variables, etc. (Let-bound polymorphism)
I So cannot (always) desugar let to λ in ML
I In let rec f x = e1 in e2, the variable f can have type∀α1, . . . , αn.τ1 → τ2 only if every use of f in e1
instantiates each αi with αi. (No polymorphic recursion)
I Let variables can be polymorphic only if e1 is a “syntacticvalue”
I A variable, constant, function definition, ...I Called the “value restriction” (relaxed partially in OCaml)
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 27
![Page 112: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/112.jpg)
ML Restrictions: Why?
ML-style polymorphism can seem weird after you have seen SystemF. And the restrictions do come up in practice, though tolerable.
I Type inference for System F (given untyped e, is there aSystem F term e′ such that erase(e′) = e) is undecidable(1995)
I Type inference for ML with polymorphic recursion isundecidable (1992)
I Type inference for ML is decidable and efficient in practice,though pathological programs of size O(n) and run-timeO(n) can have types of size O(22n
)
I The type inference algorithm is unsound in the presence ofML-style mutation, but value-restriction restores soundness
I Based on unification
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 28
![Page 113: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/113.jpg)
ML Restrictions: Why?
ML-style polymorphism can seem weird after you have seen SystemF. And the restrictions do come up in practice, though tolerable.
I Type inference for System F (given untyped e, is there aSystem F term e′ such that erase(e′) = e) is undecidable(1995)
I Type inference for ML with polymorphic recursion isundecidable (1992)
I Type inference for ML is decidable and efficient in practice,though pathological programs of size O(n) and run-timeO(n) can have types of size O(22n
)
I The type inference algorithm is unsound in the presence ofML-style mutation, but value-restriction restores soundness
I Based on unification
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 28
![Page 114: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/114.jpg)
ML Restrictions: Why?
ML-style polymorphism can seem weird after you have seen SystemF. And the restrictions do come up in practice, though tolerable.
I Type inference for System F (given untyped e, is there aSystem F term e′ such that erase(e′) = e) is undecidable(1995)
I Type inference for ML with polymorphic recursion isundecidable (1992)
I Type inference for ML is decidable and efficient in practice,though pathological programs of size O(n) and run-timeO(n) can have types of size O(22n
)
I The type inference algorithm is unsound in the presence ofML-style mutation, but value-restriction restores soundness
I Based on unification
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 28
![Page 115: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/115.jpg)
ML Restrictions: Why?
ML-style polymorphism can seem weird after you have seen SystemF. And the restrictions do come up in practice, though tolerable.
I Type inference for System F (given untyped e, is there aSystem F term e′ such that erase(e′) = e) is undecidable(1995)
I Type inference for ML with polymorphic recursion isundecidable (1992)
I Type inference for ML is decidable and efficient in practice,though pathological programs of size O(n) and run-timeO(n) can have types of size O(22n
)
I The type inference algorithm is unsound in the presence ofML-style mutation, but value-restriction restores soundness
I Based on unification
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 28
![Page 116: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/116.jpg)
ML Restrictions: Why?
ML-style polymorphism can seem weird after you have seen SystemF. And the restrictions do come up in practice, though tolerable.
I Type inference for System F (given untyped e, is there aSystem F term e′ such that erase(e′) = e) is undecidable(1995)
I Type inference for ML with polymorphic recursion isundecidable (1992)
I Type inference for ML is decidable and efficient in practice,though pathological programs of size O(n) and run-timeO(n) can have types of size O(22n
)
I The type inference algorithm is unsound in the presence ofML-style mutation, but value-restriction restores soundness
I Based on unification
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 28
![Page 117: Lecture 18 Parametric Polymorphism€¦ · Zach Tatlock CSE 505 Autumn 2017, Lecture 18 5. Goal: Client Wins! 1.Reusability (at di erent types!) I Di erent lists with elements of](https://reader035.fdocuments.us/reader035/viewer/2022081615/5fdc8e6954f7d84ab417d73c/html5/thumbnails/117.jpg)
Recover Lost Ground
Extensions to the ML type system to be closer to System F:
I Usually require some type annotations
I Are judged by:
I Soundness: Do programs still not get stuck?
I Conservatism: Do all (or most) old ML programs stilltype-check?
I Power: Does it accept many more useful programs?
I Convenience: Are many new types still inferred?
Zach Tatlock CSE 505 Autumn 2017, Lecture 18 29