Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom
Contents Our exercise What are modules Modules in ML First-class modules as records Our syntax Well kinded and well-typed records Conclusion
Our goal Design a module system for the UHC Based on existing work No implementation; just design
What are modules? Packaging code Orderly organization of types and values Modules can be combined List module in Haskell Java package
What are modules? Core language and module language Good example: ML Haskell module language is weaker
Modules in ML Separate types and values that belong together Structures and signatures Structure contains values and types Signature classifies them
Modules in ML - Signatures Signature example signature INTMAP = sig type ‘a intmap val empty: ‘a intmap val find: ‘a intmap -> int -> ‘a val insert: int*’a -> ‘a intmap -> ‘a intmap end;
Modules in ML - Structures Structure example 1: Structure Intfn : INTMAP = struct type ‘a intmap = int -> ‘a fun empty i = “error” fun find f x = f x fun insert (a,b) f i = if i==a then b else f i end;
Modules in ML - Structures Structure example 2: Structure IntList : INTMAP = struct type ‘a intmap = [(Int, a)] fun empty i = [] fun find f x = … fun insert (a,b) f i = … end;
Modules in ML - Matching Implementation independent Signature can be seen as Java interface Structure should match signature Notation: s tructure : signature Or: structure :> signature
Modules in ML - Matching Transparent matching (‘:’) vs. opaque matching (‘:>’) Expression: IntFn.empty 5 Legal with transparent matching Illegal with opaque matching Correct: IntFn.insert IntFn.empty 5
Modules in ML - Functors Parameterized modules Function maps a structure to structure Functors
Modules in ML - Functors A signature: signature NewSig = sig newVal : `a intmap end; Create new structure using parameter: Functor NewStruct(IM : INTMAP) : NewSig struct val newVal = IM.extend IM.empty 5 end;
Modules in ML - Conclusion Structures can use each others types and values! This is exactly what modules are all about Idea: modules are implemented by using signatures, structures Functors are ordinary functions
How to do this in UHC? Use the ideas from ML UHC is written in Haskell Problem: Haskell has no signatures etc. Solution: implement modules as first- class objects
First-class modules as records We introduce record types (signatures) and records (structures) These record types are first-class citizens and can thus be used in functions We implement modules as records
Record types We would like to define a record type: record Set a f = { empty :: f a add :: a -> f a -> f a asList:: f a -> [a] }
Records And implement this record type with a record: intSet :: Set Int [] intSet = Set { empty = [] add = \(x::Int) xs -> … asList= id }
Our Syntax We have created a syntax that realizes modules in Haskell First, we introduce some additional syntax.
Record types and constructors Record type body declarations S ::= record A ∆ = {S’};S | l :: σ; S | ε Record constructor body declaration s ::= l = t | ε Addition to terms t ::= record A ∆ = {S} in t | P{s} | t.l|
Type classes Not in our syntax You can make more restriction Eg. Elements of a List module should be of the class EG You can do it with records. It works like functors in ML..
Types and type schemes Types: τ, ν ::= ν -> τ | A τ | a τ | τ ν Type schemes: σ ::=forall Δ. σ | exists Δ. σ | σ -> σ | τ Δ ::= a : κ, Δ | ε
More general types Types: quantifiers only on outer level: forall c. [c] -> Int Schemes: quantifiers also inside: f (g : forall c. [c] -> Int) = g[“hello”] + g[1, 2]
Well-kinded records k ::= * | k ->k Ω ::= a: k, Ω | A: k, Ω | record A, Ω | ε Ω τ : k Three different check rules kind, term and record type rules
Kind rules
Record type body rules
Term kind-check rules
What still to be done Nested records Abstract modules and existentials Kind and type inferencing Implement it for Atze in UHC
Conclusion Modules can be implemented as first- class citizens Using records Kind and type checking rules for records Still a lot of design work to do for a well working implementation for UHC