1 / 50

Principal Type Schemes for Modular Programs

Principal Type Schemes for Modular Programs. Derek Dreyer and Matthias Blume Toyota Technological Institute at Chicago ESOP 2007 Braga, Portugal. Principal Type Schemes for Functional Programs. Damas and Milner’s classic POPL’82 paper about implicit ML-style “let-polymorphism”

nelia
Download Presentation

Principal Type Schemes for Modular Programs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Principal Type Schemesfor Modular Programs Derek Dreyer and Matthias Blume Toyota Technological Institute at Chicago ESOP 2007 Braga, Portugal

  2. Principal Type Schemes for Functional 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

  3. Principal Type Schemes forModular 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?

  4. Contributions 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

  5. Example (a)

  6. Example (a) • Value restriction: f’s type cannot be polymorphically generalized because id id is not a syntactic value.

  7. Example (a)

  8. Example (a)

  9. Example (a)

  10. Example (a)

  11. Example (a)

  12. Example (a) is Well-TypedAccording to the Definition

  13. What Went Wrong?

  14. MacQueen’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.

  15. MacQueen’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)

  16. Our Solution

  17. Our Solution • Need a way of generalizing  at the functor binding

  18. Our Solution • Idea: Generalized Functor Signatures (GFS) • Allow functors to take implicit type arguments in addition to their explicit module arguments

  19. Our Solution • Idea: Generalized Functor Signatures (GFS) • Allow functors to take implicit type arguments in addition to their explicit module arguments

  20. Our 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

  21. Our Solution

  22. Our Solution

  23. Our Solution

  24. Our Solution

  25. Example (a) Typechecks!

  26. Still Typechecks!

  27. #1 Problem Solved!

  28. Example (b)

  29. Example (b) is Well-TypedAccording to the Definition

  30. Example (b) Using a GFS

  31. Example (b) Using a GFS

  32. Example (b) Rejected! Not in scope!

  33. Our Solution • Idea: • Expand the definition of “in scope” • Allow inferred types to mention abstract types that are not defined until later in the program

  34. Example (b) Accepted! No problem!

  35. Our 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?

  36. Our 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

  37. Isn’t It Complicated?

  38. Isn’t It Complicated? • No

  39. Isn’t It Complicated? • No • Typing judgment for terms essentially same as Definition’s:

  40. Isn’t It Complicated? • No • Traditional Definition-style typing judgment (a la Russo):

  41. Isn’t It Complicated? • No • Our new declarative typing judgment:

  42. Isn’t It Complicated? • No • Our new declarative typing judgment: • Moreover, type inference becomes much simpler

  43. Example (c)

  44. Example (c) is Not Well-TypedAccording to the Definition Not in scope!

  45. 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)

  46. In Our System,Example (c) is Well-Typed No problem!

  47. What 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)

  48. “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

  49. “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

  50. Obrigado!

More Related