Evolving the ML Module System Derek Dreyer Toyota Technological Institute at Chicago April 15, 2004.

Slides:



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

Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Type Systems and Object- Oriented Programming (III) John C. Mitchell Stanford University.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
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.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
1 Chapter 1 Object-Oriented Programming. 2 OO programming and design Object-oriented programming and design can be contrasted with alternative programming.
Data Abstraction COS 441 Princeton University Fall 2004.
Elaboration or: Semantic Analysis Compiler Baojian Hua
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Inheritance and Polymorphism CS351 – Programming Paradigms.
C++ fundamentals.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
CSSE501 Object-Oriented Development
A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich : Objects and Aspects Carnegie Mellon University.
Programming Languages and Paradigms Object-Oriented Programming.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
An Object-Oriented Approach to Programming Logic and Design
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Programming Languages and Paradigms Object-Oriented Programming.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
Inheritance in the Java programming language J. W. Rider.
OOP Class Lawrence D’Antonio Lecture 3 An Overview of C++
CSC 508 – Theory of Programming Languages, Spring, 2009 Week 3: Data Abstraction and Object Orientation.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
OO as a language for acm l OO phrase l Mental model of key concepts.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
A Type System for Higher-Order Modules Derek Dreyer, Karl Crary, and Robert Harper Carnegie Mellon University POPL 2003.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Cs776(Prasad)L112Modules1 Modules value : type : function :: structure : signature : functor.
Variations on Inheritance Object-Oriented Programming Spring
CMSC 330: Organization of Programming Languages Operational Semantics.
1 Objects for Amadeus Gert Smolka Programming Systems Lab, UdS, Saarbrücken August 1999.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Data Abstraction: The Walls
CSE341: Programming Languages Lecture 11 Type Inference
Programming Languages Dan Grossman 2013
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
11.1 The Concept of Abstraction
Stateful Manifest Contracts
FP Foundations, Scheme In Text: Chapter 14.
Objects and Aspects: What we’ve seen so far
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
11.1 The Concept of Abstraction
Presentation transcript:

Evolving the ML Module System Derek Dreyer Toyota Technological Institute at Chicago April 15, 2004

2 Data Abstraction Should be able to restrict how much clients of a program module know about its implementation Enforcement of program invariants Protect clients from implementation changes

3 Object-Oriented Approach (Java) Classes/objects encapsulate code with data Private fields/methods are inaccessible to clients Semantics of data abstraction tied up with other features of OOP –Inheritance, subtyping, dynamic dispatch

4 Module-Oriented Approach (ML) Modules are units of “core-language” code Interface of a module describes what other modules get to know about it Implementor-side data abstraction via sealing Client-side data abstraction via functors

5 Evolving the ML Module System Want to make the ML module system even better: –e.g. Add support for recursive modules

6 Evolving the ML Module System Want to make the ML module system even better: –e.g. Add support for recursive modules But where do we start? –Several variants of the ML module system: Standard ML, Objective Caml, Moscow ML, etc. –Relationships/tradeoffs between them are unclear

7 The Goal of This Work Develop a unifying account of existing variations on the ML module system Build upon this foundation with support for new features (e.g. recursive modules)

8 Overview Concrete examples of data abstraction in ML High-level analysis of data abstraction in ML Extending ML with recursive modules Future work

9 IntSet Module Module implementing sets of integers: module IntSet = mod type set = int list val emptyset : set = [] fun insert (x:int,S:set) : set = x::S fun member (x:int,S:set) : bool = end

10 Using the IntSet Module Clients use “dot notation” to refer to components of the IntSet module: module IntSet = mod... end val S : IntSet.set = IntSet.insert(3, IntSet.emptyset)

11 Using the IntSet Module Clients use “dot notation” to refer to components of the IntSet module: module IntSet = mod... end val S : int list = IntSet.insert(3, IntSet.emptyset)

12 Abstract IntSet Interface Hide definition of set type in interface of IntSet : interface INT_SET = iface type set val emptyset : set fun insert : int * set -> set fun member : int * set -> bool... end

13 Data Abstraction via Sealing Seal the implementation with the interface: module IntSet = (mod... end) :> INT_SET Clients of IntSet can’t see definition of IntSet.set IntSet.set is an abstract type

14 Modules Can Have Effects IntSet module was purely functional –Body just defined values and functions But modules can have side effects –E.g. creation of mutable state

15 Symbol Table Module When evaluated, generates a new symbol table: module SymbolTable = mod val table = HashTable.create(...) type symbol = int fun string_to_symbol (x:string) =... fun symbol_to_string (n:symbol) =... end

16 Sealing the Symbol Table module SymbolTable = (mod... end) :> iface type symbol fun string_to_symbol : string -> symbol fun symbol_to_string : symbol -> string... end

17 Making Sets More Generic IntSet module only supports integer sets Implementation of sets basically the same regardless of what the type of items in the set is Functors allow you to implement generic sets –Can be instantiated with different item types –Similar to templates in C++, but more powerful

18 Functors A functor is a function from modules to modules: Some Comparable Item Type Set Functor Sets of That Item inputoutput

19 Interface of Comparable Items interface COMPARABLE = iface type item fun compare : item * item -> bool end

20 The Set Functor module Set = functor (Item : COMPARABLE) -> mod type set = Item.item list fun member (x,S) =...Item.compare(x,y) end

21 Applying the Set Functor module IntItem = mod type item = int fun compare(x,y) = Int.compare(x,y) end module IntSet = Set(IntItem)

22 Applying the Set Functor module StringItem = mod type item = string fun compare(x,y) = String.compare(x,y) end module StringSet = Set(StringItem)

23 Two Forms of Data Abstraction Can seal a module with an abstract interface –Implementor-side abstraction Can use functors to make a module more generic –Client-side abstraction

24 Overview Concrete examples of data abstraction in ML High-level analysis of data abstraction in ML Extending ML with recursive modules Future work

25 Type Components of Modules Modules in ML have type components Can “project out” type components of modules: –e.g. IntSet.set, SymbolTable.symbol. In all the examples so far, we’ve only projected types out of module variables (or names)

26 Module Expressions Examples of module expressions: mod... end (mod... end) :> (iface... end) Set(IntItem)

27 Question Why not be able to project types from arbitrary module expressions? mod... end (mod... end) :> (iface... end) Set(IntItem)

28 Example interface I = iface type t... end module A = (mod type t = int... end) :> I module B = (mod type t = float... end) :> I

29 Non-Projectible Module Suppose M = if button_is_selected() then A else B

30 Non-Projectible Module Suppose M = if button_is_selected() then A else B module C = M module D = M

31 Non-Projectible Module Suppose M = if button_is_selected() then A else B module C = M module D = M If M.t is a valid type, then C.t = M.t = D.t But C.t might be int and D.t might be float !! Unsound for M to be projectible

32 Projectible Module Suppose M = mod type t = int; val x = 3 end module C = M module D = M Fine if M is projectible, since C.t = M.t = D.t = int

33 Purity “Impure” module expression: if button_is_selected() then A else B “Pure” module expression: mod type t = int; val x = 3 end

34 Purity “Impure” module expression: if button_is_selected() then A else B “Pure” module expression: mod type t = int; val x = ref 3 end

35 Purity “Impure” module expression: if button_is_selected() then A else B “Pure” module expression: mod type t = int; val x = ref 3 end Sound for M to be projectible, M is pure (w.r.t. type components)

36 Plan Consider how purity and projectibility relate to: –Sealing –Functors –The way that sealing and functors interact

37 Sealing Suppose M = (mod... end) :> (iface type t... end) module C = M module D = M Sealing has no run-time effect, so M is pure But if M is projectible, then C.t = M.t = D.t This violates abstraction!

38 Big Picture if button_is_selected() then A else B Impure Modules (all non-projectible) Pure Modules ProjectibleNon-projectible A mod type t = int val x = ref 3 end B M :> I

39 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 : I 1 ! I 2, body of F is pure –F : I 1 ! I 2, body of F is impure tot par

40 Total, Applicative F : I 1 ! I 2, M :  1, F and M are pure module C = F(M) module D = F(M) F(M) is pure ) projectible C.t = F(M). t = D.t tot

41 Partial, Generative F : I 1 ! I 2, M :  1, F and M are pure module C = F(M) module D = F(M) F(M) is impure ) non-projectible C.t   D.t par

42 Set Functor Example module Set = functor (Item : COMPARABLE) -> (mod... end :> iface type set fun member : Item.item * set -> set... end) Body is pure, so Set functor is total

43 Set Functor Example module Set = functor (Item : COMPARABLE) -> (mod... end :> iface type set fun member : Item.item * set -> set... end) module IntSet1 = Set(IntItem) module IntSet2 = Set(IntItem) IntSet1.set and IntSet2.set are compatible

44 SymbolTable Module module SymbolTable = mod... end :> iface type symbol fun string_to_symbol : string -> symbol fun symbol_to_string : symbol -> string end

45 SymbolTable Functor module SymbolTable = functor () -> (mod... end :> iface type symbol fun string_to_symbol : string -> symbol fun symbol_to_string : symbol -> string end) Body is pure, so SymbolTable functor is total

46 SymbolTable Functor module SymbolTable = functor () -> (mod... end :> iface type symbol fun string_to_symbol : string -> symbol fun symbol_to_string : symbol -> string end) module ST1 = SymbolTable() module ST2 = SymbolTable() But ST1.symbol and ST2.symbol are not compatible!

47 Pure vs. Impure Sealing if button_is_selected() then A else B Impure Modules (all non-projectible) Pure Modules ProjectibleNon-projectible A mod type t = int val x = ref 3 end B M :> I M :>> I Two forms of sealing

48 SymbolTable Functor Revisited module SymbolTable = functor () -> (mod... end :>> iface type symbol fun string_to_symbol : string -> symbol fun symbol_to_string : symbol -> string end) Body is impure, so SymbolTable functor is partial

49 Summary Analysis in terms of purity and projectibility Previous systems make “applicative” or “generative” a design choice, but we support both Previous systems just employ one or the other form of sealing and call it “sealing”

50 Unifying Previous Systems Standard ML ‘97 –Only has impure sealing, all functors are partial/generative Objective Caml / Leroy (1995) –Only has pure sealing, all functors are total/applicative Shao (1999) –Supports both total and partial functors –Only has impure sealing, can’t write applicative Set functor Russo (2000) –Two languages, one like SML and one like O’Caml –Moscow ML combines them, but language is unsound

51 Overview Concrete examples of data abstraction in ML High-level analysis of data abstraction in ML Extending ML with recursive modules Future work

52 Recursive Modules Existing proposals fall into two categories: –“Units” or “Mixin modules” meant to replace ML modules (Flatt-Felleisen 98, Ancona-Zucca 96, Duggan-Sourelis 98) –Extend ML module system with recursive module construct (Crary et al. 99, Russo 01) Issues provoked by recursive module construct: –Recursion over general expressions with effects (see [Dreyer 04] for details) –Interaction of recursion and data abstraction

53 Recursive Module Example rec (X : iface module A : I A module B : I B end. mod module A = M A :> I A module B = M B :> I B end )

54 Abstract Interface iface module A : iface type t... end module B : iface type u fun g : A.t -> u end X :

55 Recursive Module Body mod module A = mod type t = int fun f(x) =... X.B.g(3)... end :> I A module B =... end

56 The Two A.t ’s mod module A = mod type t = int fun f(x) =... X.B.g(3)... end :> I A module B =... end But X.B.g ’s argument type is X.A.t, which is an abstract type!

57 Moscow ML Solution iface module A : iface type t = int... end module B : iface type u fun g : A.t -> u end X :

58 Our Solution mod module A = mod type t = int fun f(x) =... X.B.g(3)... end :> I A module B =... end At this point, we know that A.t = int, so we know that X.A.t = int as well.

59 Our Solution mod module A = mod type t = int fun f(x) =... X.B.g(3)... end :> I A module B = mod... end :> I B end At this point, A.t is abstract, so all we know is that X.A.t = A.t.

60 Current and Future Work Designed an ML dialect based on our analysis, following [Harper-Stone 97] –Currently implementing it in TILT compiler for SML Future Work: –Further extensions to ML module system E.g. type classes –Generalizations of ML-style data abstraction

Thank you!