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

Slides:



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

Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
Type Inference David Walker COS 320. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Static and User-Extensible Proof Checking Antonis StampoulisZhong Shao Yale University POPL 2012.
Comparing Semantic and Syntactic Methods in Mechanized Proof Frameworks C.J. Bell, Robert Dockins, Aquinas Hobor, Andrew W. Appel, David Walker 1.
Certified Typechecking in Foundational Certified Code Systems Susmit Sarkar Carnegie Mellon University.
1 PROPERTIES OF A TYPE ABSTRACT INTERPRETATER. 2 MOTIVATION OF THE EXPERIMENT § a well understood case l type inference in functional programming à la.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Elaboration or: Semantic Analysis Compiler Baojian Hua
Standard ML- Part I Compiler Baojian Hua
B. Ross Cosc 4f79 1 Forward chaining backward chaining systems: take high-level goal, and prove it by reducing it to a null goal - to reduce it to null,
A Type System for Well-Founded Recursion Derek Dreyer Carnegie Mellon University POPL 2004 Venice, Italy.
Typed Compilation of Recursive Datatypes Joseph C. Vanderwaart, Derek Dreyer, Leaf Petersen, Karl Crary, Robert Harper, and Perry Cheng Carnegie Mellon.
VeriML: Revisiting the Foundations of Proof Assistants Zhong Shao Yale University MacQueen Fest May 13, 2012 (Joint work with Antonis Stampoulis)
Code-Carrying Proofs Aytekin Vargun Rensselaer Polytechnic Institute.
Type Checking.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
Modular Type Classes Derek Dreyer Robert Harper Manuel M.T. Chakravarty POPL 2007 Nice, France.
Lecture 8 Recursively enumerable (r.e.) languages
Parametric Polymorphism COS 441 Princeton University Fall 2004.
Strict Bidirectional Type Checking Adam Chlipala, Leaf Petersen, and Robert Harper.
Standard ML- Part II Compiler Baojian Hua
Generative type abstraction and type-level computation (Wrestling with System FC) Stephanie Weirich, Steve Zdancewic University of Pennsylvania Dimitrios.
A Type-Checked Restrict Qualifier Jeff Foster OSQ Retreat May 9-10, 2001.
Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom.
Catriel Beeri Pls/Winter 2004/05 types 65  A type-checking algorithm The task: (since we start with empty H, why is the goal not just E?) The rule set.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
Semantics for MinML COS 441 Princeton University Fall 2004.
CS21 Decidability and Tractability
1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder.
J. Michael Moore Recursion CSCE 110 From James Tam’s material.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Evolving the ML Module System Derek Dreyer Toyota Technological Institute at Chicago April 15, 2004.
Type Safety Kangwon National University 임현승 Programming Languages.
PZ03EX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03EX - ML Programming Language Design and Implementation.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
Universal Types Report by Matthias Horbach. Contents Types of Polymorphism System F Basic Properties Erasure Impredicativity Parametricity.
Singleton Kinds and Singleton Types Christopher A. Stone August 2, 1999 Thesis Committee Bob Harper, chair Peter Lee John Reynolds Jon Riecke (Bell Laboratories)
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
A Type System for Higher-Order Modules Derek Dreyer, Karl Crary, and Robert Harper Carnegie Mellon University POPL 2003.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CS162 Week 8 Kyle Dewey. Overview Example online going over fail03.not (from the test suite) in depth A type system for secure information flow Implementing.
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
Cs776 (Prasad)L2HOF1 Higher-Order Functions. cs776 (Prasad)L2HOF2 Higher-Order Functions A function that takes a function as argument and/or returns a.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Lesson 10 Type Reconstruction
Functional Programming
Programming Languages and Compilers (CS 421)
CSE341: Programming Languages Lecture 11 Type Inference
Programming Languages Dan Grossman 2013
Types for Programs and Proofs
Stateful Manifest Contracts
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Drew Wyborski Programming Languages
Presentation transcript:

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” Declarative semantics:  ` e :  –Clean, but non-deterministic: e may have many types  Algorithm W:  ` e ) (  ;  ) –Computes the principal, “most general” type of e

Principal Type Schemes for Modular 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?

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

Example (a)

Value restriction: f ’s type cannot be polymorphically generalized because id id is not a syntactic value.

Example (a)

Example (a) is Well-Typed According to the Definition

What Went Wrong?

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 value and is not uniquely typed, e.g. val f = id id.

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 value and 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. 1 st -class modules, modular type classes)

Our Solution

Need a way of generalizing  at the functor binding

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

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

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

Our Solution

Example (a) Typechecks! …

Still Typechecks! …

Problem Solved! #1

Example (b)

Example (b) is Well-Typed According to the Definition

Example (b) Using a GFS

Example (b) Rejected! Not in scope!

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

Example (b) Accepted! No problem!

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?

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

Isn’t It Complicated?

No

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

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

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

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

Example (c)

Example (c) is Not Well-Typed According to the Definition Not in scope!

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)

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

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)

“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

“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

Obrigado!