A Type System for Higher-Order Modules Derek Dreyer, Karl Crary, and Robert Harper Carnegie Mellon University POPL 2003.

Slides:



Advertisements
Similar presentations
Transposing F to C Transposing F to C Andrew Kennedy & Don Syme Microsoft Research Cambridge, U.K.
Advertisements

More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Certified Typechecking in Foundational Certified Code Systems Susmit Sarkar Carnegie Mellon University.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Elaboration or: Semantic Analysis Compiler Baojian Hua
Standard ML- Part I Compiler Baojian Hua
ECE 103 Engineering Programming Chapter 11 One Minute Synopsis Herbert G. Mayer, PSU CS Status 7/1/2014.
Module Language. Module language The Standard ML module language comprises the mechanisms for structuring programs into separate units. –Program units.
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.
ISBN Chapter 3 Describing Syntax and Semantics.
What is a recursive module? Crary, Harper, Puri Module Systems, Fall 2002 Aleksey Kliger.
Modular Type Classes Derek Dreyer Robert Harper Manuel M.T. Chakravarty POPL 2007 Nice, France.
Principal Type Schemes for Modular Programs Derek Dreyer and Matthias Blume Toyota Technological Institute at Chicago ESOP 2007 Braga, Portugal.
CSE341: Programming Languages Lecture 12 Modules Dan Grossman Fall 2011.
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
1 CS 312 – Lecture 28 Continuations –Probably the most confusing thing you’ve seen all semester… Course summary –Life after CS 312.
Katz Formal Specifications Larch 1 Algebraic Specification and Larch Formal Specifications of Complex Systems Shmuel Katz The Technion.
Data Abstraction COS 441 Princeton University Fall 2004.
Parametric Polymorphism COS 441 Princeton University Fall 2004.
Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
Elaboration or: Semantic Analysis Compiler Baojian Hua
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Read Robert Harper’s notes on “an introduction to.
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.
Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom.
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Crash course on SML Wojciech Moczydłowski SML – functional programming language Complete formal semantics Extremely convenient for writing compilers, interpreters,
Describing Syntax and Semantics
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich : Objects and Aspects Carnegie Mellon University.
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
CSE 341 Programming Languages Racket Datatype Style Programming Zach Tatlock Spring 2014.
Chapter Twenty-ThreeModern Programming Languages1 Formal Semantics.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
CS 363 Comparative Programming Languages 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.
CSE S. Tanimoto Introduction to ML 1 Introduction to ML History Special features Interacting with ML ML’s basic types ML’s composite types Math.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
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.
CS536 Types 1. Roadmap Back from our LR Parsing Detour Name analysis – Static v dynamic – Scope Today – Type checking 2 Scanner Parser Tokens Semantic.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
Cs776(Prasad)L112Modules1 Modules value : type : function :: structure : signature : functor.
CSE 341 : Programming Languages Lecture 12 ML Modules Zach Tatlock Spring 2014.
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.
Faithful mapping of model classes to mathematical structures Ádám Darvas ETH Zürich Switzerland Peter Müller Microsoft Research Redmond, WA, USA SAVCBS.
Cs776 (Prasad)L2HOF1 Higher-Order Functions. cs776 (Prasad)L2HOF2 Higher-Order Functions A function that takes a function as argument and/or returns a.
Programming Languages and Compilers (CS 421)
CSE341: Programming Languages Lecture 11 Type Inference
Stateful Manifest Contracts
FP Foundations, Scheme In Text: Chapter 14.
CSE341: Programming Languages Lecture 11 Type Inference
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 11 Type Inference
CSE-321 Programming Languages Introduction to Functional Programming
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Presentation transcript:

A Type System for Higher-Order Modules Derek Dreyer, Karl Crary, and Robert Harper Carnegie Mellon University POPL 2003

Type Theory for Module Systems Lots of work on module system design Theory has had impact on real language design: –Harper-Lillibridge 94, Leroy 94 ) SML ’97 –Leroy 95 ) Objective Caml –Russo 00 ) Moscow ML No general semantic framework for understanding relationships between designs

A Unifying Type Theory High-level semantic analysis ) Unifying type theory Previous designs can be seen as subsystems Key idea: Explain semantics of abstract data types in terms of purity and effects

Projectibility When is a module expression M projectible? –When can we project out M’s type components? “Non-projectible” module expression: if buttonIsPressed() then struct type t = int... end else struct type t = bool... end “Projectible” module expression: struct type t = int; val x = 3 end

Projectibility When is a module expression M projectible? –When can we project out M’s type components? “Non-projectible” module expression: if buttonIsPressed() then struct type t = int... end else struct type t = bool... end “Projectible” module expression: struct type t = int; val x = 3 end

Projectibility When is a module expression M projectible? –When can we project out M’s type components? “Non-projectible” module expression: if buttonIsPressed() then struct type t = int... end else struct type t = bool... end “Projectible” module expression: struct type t = int; val x = 3 end

Projectibility When is a module expression M projectible? –When can we project out M’s type components? “Impure” module expression: if buttonIsPressed() then struct type t = int... end else struct type t = bool... end “Pure” module expression: struct type t = int; val x = 3 end

Projectibility When is a module expression M projectible? –When can we project out M’s type components? “Impure” module expression: if buttonIsPressed() then struct type t = int... end else struct type t = bool... end “Pure” module expression: struct type t = int; val x = ref 3 end

Purity M is soundly projectible, M is pure (w.r.t. type components), Type components of M are the same every time M is evaluated M is impure ) Meaning of M.t not statically well-determined

Second-Class Modules Second-class modules admit “phase separation” –Type components can’t depend on run-time conditions SML and O’Caml modules are second-class because of syntactic restrictions All second-class modules are pure –But should they all be projectible?

Sealing Principal means of creating abstract data types –M :> , aka “opaque signature ascription” Treating sealed modules as projectible violates abstraction: –A = (M :>  ) and B = (M :>  ) –If (M :>  ) is projectible, then A.t = (M :>  ).t = B.t –But “A.t = B.t” not observable in 

You Can’t Handle the Truth! In truth, sealing doesn’t affect a module’s purity But sealing obstructs our knowledge about a module’s purity Projectibility is a judgment of knowledge, not truth: –Sealed modules treated as impure/non-projectible

Total and Partial Functors To track purity in the presence of functors: –Need to know whether applying a functor will unleash an effect or not Distinguish types of total and partial functors: –F :  tot s:  1.  2, body of F is known to be pure –F :  par s:  1.  2, body of F could be impure

Total, Applicative F :  tot s:  1.  2, M :  1, F and M are pure structure Res 1 = F(M) structure Res 2 = F(M) F(M) known to be pure ) projectible Res 1.t = F(M).t = Res 2.t

Partial, Generative F :  par s:  1.  2, M :  1, F and M are pure structure Res 1 = F(M) structure Res 2 = F(M) F(M) possibly impure ) non-projectible Res 1.t  Res 2.t

Functors with Sealing If body of a functor contains sealing, then: –Body is impure –Functor is generative Can be both a good and bad thing: –Gives correct semantics of abstraction for functors that use imperative features –Overly restrictive for purely functional functors

Importance of Generativity functor MakeSymbolTable() = (struct... (* creates new mutable hash table *) end :> sig type symbol val string_to_symbol : string -> symbol val symbol_to_string : symbol -> string end) ) Generativity ties the symbol type to the run-time state of the module defining it

Purely Functional Abstraction functor MakeSet (Elem : COMPARABLE) = (struct... end :> sig type elem = Elem.elem type set val insert : elem * set -> set end) What if a sealed module is purely functional? –Abstract types not tied to any run-time state –Only care about hiding type identity

Hoisting the Sealing Instead of sealing the body of the functor: s:  1. (M :>  2 ) Seal the functor itself (with a total signature): ( s:  1. M) :>  tot s:  1.  2 Problem: Only works if M is pure –Not true if M contains sealed substructures, such as datatype definitions

Module Classifications Impure, non-projectible Pure, projectible Sealing M :> 

Static and Dynamic Effects Split effects into two kinds: static and dynamic Module with any kind of effects is impure Dynamic effects occur “during execution” Static effects occur “during typechecking”

Weak and Strong Sealing Pure, projectible Statically impure, dynamically pure Statically and dynamically impure Weak Sealing M ::  Strong Sealing M :>  Non-projectible Impure,

Set Functor Revisited functor MakeSet (Elem : COMPARABLE) = (struct... end :: sig type elem = Elem.elem type set val insert : elem * set -> set end) We expand totality to allow body of a total functor to contain static (but not dynamic) effects

A Unifying Framework Standard ML –Only has strong sealing, all functors are partial/generative Objective Caml / Leroy (1995) –Only has weak sealing, all functors are total/applicative Shao (1999) –Distinguishes two kinds of functor signatures –Only tracks dynamic effects and strong sealing Russo (2000) –Two languages, one like SML and one like O’Caml –Moscow ML combines them, but language is unsound

Modules as First-Class Values Packaging modules as first-class values: –Add a new “package type” Coercions between modules and terms: –If M : , then pack M as : –If e :, then unpack e as  : 

Modules as First-Class Values structure X = struct type t = int... end structure Y = struct type t = bool... end M = (if buttonIsPressed() then X else Y) Type components of M actually depend on run-time conditions Unpacking induces a truly dynamic effect

Modules as First-Class Values signature S = sig type t... end structure X = struct type t = int... end structure Y = struct type t = bool... end M = unpack (if buttonIsPressed() then pack X as else pack Y as ) as S Type components of M actually depend on run-time conditions Unpacking induces a truly dynamic effect

The Rest of the Paper Formalism: –Synthesis of previous work on dependent types and singleton kinds Fully expressive higher-order functors –Via “static” module equivalence Decidable typechecking algorithm Avoidance problem –Restrict type theory (as little as possible) to avoid it –Unrestricted language definable by elaboration

Conclusion Future Work: –Recursive modules –Using monads instead of total/partial We’ve provided a framework for understanding: –Alternative module system designs –Semantics of abstraction, via a framework of module-level effects