Download presentation
Presentation is loading. Please wait.
1
Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom
2
Contents Our exercise What are modules Modules in ML First-class modules as records Our syntax Well kinded and well-typed records Conclusion
3
Our goal Design a module system for the UHC Based on existing work No implementation; just design
4
What are modules? Packaging code Orderly organization of types and values Modules can be combined List module in Haskell Java package
5
What are modules? Core language and module language Good example: ML Haskell module language is weaker
6
Modules in ML Separate types and values that belong together Structures and signatures Structure contains values and types Signature classifies them
7
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;
8
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;
9
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;
10
Modules in ML - Matching Implementation independent Signature can be seen as Java interface Structure should match signature Notation: s tructure : signature Or: structure :> signature
11
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
12
Modules in ML - Functors Parameterized modules Function maps a structure to structure Functors
13
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;
14
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
15
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
16
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
17
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] }
18
Records And implement this record type with a record: intSet :: Set Int [] intSet = Set { empty = [] add = \(x::Int) xs -> … asList= id }
19
Our Syntax We have created a syntax that realizes modules in Haskell First, we introduce some additional syntax.
20
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|
21
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..
22
Types and type schemes Types: τ, ν ::= ν -> τ | A τ | a τ | τ ν Type schemes: σ ::=forall Δ. σ | exists Δ. σ | σ -> σ | τ Δ ::= a : κ, Δ | ε
23
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]
24
Well-kinded records k ::= * | k ->k Ω ::= a: k, Ω | A: k, Ω | record A, Ω | ε Ω τ : k Three different check rules kind, term and record type rules
25
Kind rules
26
Record type body rules
27
Term kind-check rules
28
What still to be done Nested records Abstract modules and existentials Kind and type inferencing Implement it for Atze in UHC
29
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.