Principal Type Schemes for Modular Programs
-
Upload
bruno-hyde -
Category
Documents
-
view
24 -
download
1
description
Transcript of Principal Type Schemes for Modular Programs
![Page 1: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/1.jpg)
Principal Type SchemesPrincipal Type Schemesfor Modular Programsfor Modular Programs
Derek Dreyer and Matthias Blume
Toyota Technological Institute at Chicago
ESOP 2007
Braga, Portugal
![Page 2: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/2.jpg)
Principal Type Schemes for Principal Type Schemes for FunctionalFunctional Programs Programs
• Damas and Milner’s classic POPL’82 paper about implicit ML-style “let-polymorphism”
• Declarative semantics: ` e : – Clean, but non-deterministic: e may have many types
• Algorithm W: ` e ) (; )
– Computes the principal, “most general” type of e
![Page 3: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/3.jpg)
Principal Type Schemes forPrincipal Type Schemes forModularModular Programs? Programs?
• Definition of Standard ML joins Damas-Milner’s declarative rules with the rules of the ML module system
• Implementations of SML employ various generalizations of Algorithm W to work in the presence of modules
• Is Damas-Milner being generalized properly?
– Does SML have principal types?
![Page 4: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/4.jpg)
Contributions of This WorkContributions of This Work
• A set of example programs on which no SML typechecker accurately matches the Definition
– Illustrate why the Definition is difficult to implement
• A novel declarative system for ML-style polymorphism in the presence of modules that is easy to implement
– Principal types theorem proved
– Backward-compatible with SML
– Elegant application of previous ideas/techniques
![Page 5: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/5.jpg)
Example (a)Example (a)
![Page 6: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/6.jpg)
Example (a)Example (a)
• Value restriction: f’s type cannot be polymorphically generalized because id id is not a syntactic value.
![Page 7: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/7.jpg)
Example (a)Example (a)
![Page 8: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/8.jpg)
Example (a)Example (a)
![Page 9: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/9.jpg)
Example (a)Example (a)
![Page 10: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/10.jpg)
Example (a)Example (a)
![Page 11: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/11.jpg)
Example (a)Example (a)
![Page 12: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/12.jpg)
Example (a) is Well-TypedExample (a) is Well-TypedAccording to the DefinitionAccording to the Definition
![Page 13: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/13.jpg)
What Went Wrong?What Went Wrong?
![Page 14: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/14.jpg)
MacQueen’s GambitMacQueen’s Gambit
• SML/NJ’s policy (according to Dave MacQueen):
– Core and module languages should not mix
– Reject module-level bindings where r.h.s. is not a valueand is not uniquely typed, e.g. val f = id id.
![Page 15: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/15.jpg)
MacQueen’s GambitMacQueen’s Gambit
• SML/NJ’s policy (according to Dave MacQueen):
– Core and module languages should not mix
– Reject module-level bindings where r.h.s. is not a valueand is not uniquely typed, e.g. val f = id id.
• Disadvantages:
– Rejects perfectly good, noncontrived examples, too.E.g. val L = ref nil.
– May not scale to languages where module and core are intertwined (e.g. 1st-class modules, modular type classes)
![Page 16: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/16.jpg)
Our SolutionOur Solution
![Page 17: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/17.jpg)
Our SolutionOur Solution
• Need a way of generalizing at the functor binding
![Page 18: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/18.jpg)
Our SolutionOur Solution
• Idea: Generalized Functor Signatures (GFS)
– Allow functors to take implicit type arguments in addition to their explicit module arguments
![Page 19: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/19.jpg)
Our SolutionOur Solution
• Idea: Generalized Functor Signatures (GFS)
– Allow functors to take implicit type arguments in addition to their explicit module arguments
![Page 20: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/20.jpg)
Our SolutionOur Solution
• Idea: Generalized Functor Signatures (GFS)
– Allow functors to take implicit type arguments in addition to their explicit module arguments
• Implicit functors were also useful for modular type classes
![Page 21: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/21.jpg)
Our SolutionOur Solution
![Page 22: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/22.jpg)
Our SolutionOur Solution
![Page 23: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/23.jpg)
Our SolutionOur Solution
…
![Page 24: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/24.jpg)
Our SolutionOur Solution
…
![Page 25: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/25.jpg)
Example (a) Typechecks!Example (a) Typechecks!
…
![Page 26: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/26.jpg)
Still Typechecks!Still Typechecks!
…
![Page 27: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/27.jpg)
ProblemProblem Solved!Solved!#1#1
![Page 28: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/28.jpg)
Example (b)Example (b)
![Page 29: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/29.jpg)
Example (b) is Well-TypedExample (b) is Well-TypedAccording to the DefinitionAccording to the Definition
![Page 30: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/30.jpg)
Example (b) Using a GFSExample (b) Using a GFS
![Page 31: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/31.jpg)
Example (b) Using a GFSExample (b) Using a GFS
![Page 32: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/32.jpg)
Example (b) Rejected!Example (b) Rejected!
Not in scope!
![Page 33: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/33.jpg)
Our SolutionOur Solution
• Idea:
– Expand the definition of “in scope”
– Allow inferred types to mention abstract types that are not defined until later in the program
![Page 34: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/34.jpg)
Example (b) Accepted!Example (b) Accepted!
No problem!
![Page 35: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/35.jpg)
Our SolutionOur Solution
• Idea:
– Expand the definition of “in scope”
– Allow inferred types to mention abstract types that are not defined until later in the program
• How does that work and is it sound?
![Page 36: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/36.jpg)
Our SolutionOur Solution
• Idea:
– Expand the definition of “in scope”
– Allow inferred types to mention abstract types that are not defined until later in the program
• How does that work and is it sound?
– Using Dreyer’s RTG type system (ICFP 05), which was designed as a foundation for recursive modules
– Soundness proved via progress/preservation
![Page 37: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/37.jpg)
Isn’t It Complicated?Isn’t It Complicated?
![Page 38: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/38.jpg)
Isn’t It Complicated?Isn’t It Complicated?
• No
![Page 39: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/39.jpg)
Isn’t It Complicated?Isn’t It Complicated?
• No
• Typing judgment for terms essentially same as Definition’s:
![Page 40: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/40.jpg)
Isn’t It Complicated?Isn’t It Complicated?
• No
• Traditional Definition-style typing judgment (a la Russo):
![Page 41: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/41.jpg)
Isn’t It Complicated?Isn’t It Complicated?
• No
• Our new declarative typing judgment:
![Page 42: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/42.jpg)
Isn’t It Complicated?Isn’t It Complicated?
• No
• Our new declarative typing judgment:
• Moreover, type inference becomes much simpler
![Page 43: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/43.jpg)
Example (c)Example (c)
![Page 44: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/44.jpg)
Example (c) is Not Well-TypedExample (c) is Not Well-TypedAccording to the DefinitionAccording to the Definition
Not in scope!
![Page 45: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/45.jpg)
Distinguishing (b) and (c)Distinguishing (b) and (c)
• Involves tracking dependencies between abstract types and unification variables
– Only 1.5 out of 9 SML implementations get it right
• Russo’s thesis (2000) gives an inference algorithm based on Miller’s technique of unification under a mixed prefix
– But does not prove that it works
– Algorithm doesn’t accept Example (a)
![Page 46: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/46.jpg)
In Our System,In Our System,Example (c) is Well-TypedExample (c) is Well-Typed
No problem!
![Page 47: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/47.jpg)
What Else Is In the PaperWhat Else Is In the Paper
• Full formalization of declarative semantics and inference algorithm
– Hybrid of Definition and Harper-Stone semantics
– Type soundness proven by reduction to RTG(reduction in tech report)
• Principal types theorem stated (proof in tech report)
![Page 48: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/48.jpg)
““Benchmarks”Benchmarks”
• Reject All: SML/NJ, ML-Kit, TILT, SML.NET, Hamlet
• Mixed Bag: Poly/ML, Alice, Moscow ML (interactive mode)
• MLton: Success relies on whole-program compilation, defunctorization coupled with typechecking
![Page 49: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/49.jpg)
““Benchmarks”Benchmarks”
• Reject All: SML/NJ, ML-Kit, TILT, SML.NET, Hamlet
• Mixed Bag: Poly/ML, Alice, Moscow ML (interactive mode)
• MLton: Success relies on whole-program compilation, defunctorization coupled with typechecking
![Page 50: Principal Type Schemes for Modular Programs](https://reader036.fdocuments.us/reader036/viewer/2022070401/56813653550346895d9dd936/html5/thumbnails/50.jpg)
Obrigado!Obrigado!