modules in uhc a proposal by: tom hofte & eric eijkelenboom
Post on 21-Dec-2015
216 views
TRANSCRIPT
![Page 1: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/1.jpg)
Modules in UHC
A proposal by: Tom Hofte & Eric Eijkelenboom
![Page 2: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/2.jpg)
Contents
Our exercise What are modules Modules in ML First-class modules as records Our syntax Well kinded and well-typed records Conclusion
![Page 3: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/3.jpg)
Our goal
Design a module system for the UHC Based on existing work No implementation; just design
![Page 4: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/4.jpg)
What are modules?
Packaging code Orderly organization of types and values Modules can be combined List module in Haskell Java package
![Page 5: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/5.jpg)
What are modules?
Core language and module language Good example: ML Haskell module language is weaker
![Page 6: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/6.jpg)
Modules in ML
Separate types and values that belong together Structures and signatures Structure contains values and types Signature classifies them
![Page 7: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/7.jpg)
Modules in ML - Signatures
Signature examplesignature INTMAP = sig
type ‘a intmapval empty: ‘a intmapval find: ‘a intmap -> int -> ‘aval insert: int*’a -> ‘a intmap
-> ‘a intmap
end;
![Page 8: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/8.jpg)
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;
![Page 9: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/9.jpg)
Modules in ML - Structures
Structure example 2: Structure IntList : INTMAP = structtype ‘a intmap = [(Int, a)]fun empty i = []fun find f x = …
fun insert (a,b) f i = …end;
![Page 10: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/10.jpg)
Modules in ML - Matching
Implementation independent Signature can be seen as Java interface Structure should match signature Notation: structure : signature Or: structure :> signature
![Page 11: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/11.jpg)
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
![Page 12: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/12.jpg)
Modules in ML - Functors
Parameterized modules Function maps a structure to structure Functors
![Page 13: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/13.jpg)
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;
![Page 14: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/14.jpg)
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
![Page 15: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/15.jpg)
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
![Page 16: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/16.jpg)
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
![Page 17: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/17.jpg)
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]
![Page 18: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/18.jpg)
Records
And implement this record type with a record:
intSet :: Set Int []
intSet = Set
empty = []
add = \(x::Int) xs -> …
asList= id
![Page 19: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/19.jpg)
Our Syntax
We have created a syntax that realizes modules in Haskell First, we introduce some additional syntax.
![Page 20: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/20.jpg)
Record types and constructors
Record type body declarationsS ::= record A ∆ = S’;S |
l :: σ; S | ε
Record constructor body declarations ::= l = t | ε
Addition to terms t ::= record A ∆ = S in t |
Ps |
t.l|
![Page 21: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/21.jpg)
Type classes
Not in our syntax You can make more restrictionEg. Elements of a List module should be of the class EG You can do it with records. It works like functors in ML..
![Page 22: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/22.jpg)
Types and type schemes
Types: τ, ν ::= ν -> τ | A τ | a τ | τ ν Type schemes:
σ ::=forall Δ. σ | exists Δ. σ
| σ -> σ| τ
Δ ::= a : κ, Δ | ε
![Page 23: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/23.jpg)
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]
![Page 24: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/24.jpg)
Well-kinded records
k ::= * | k ->k Ω ::= a: k, Ω | A: k, Ω | record A, Ω |
ε Ω τ : k Three different check rules kind, term and record type rules
![Page 25: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/25.jpg)
Kind rules
![Page 26: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/26.jpg)
Record type body rules
![Page 27: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/27.jpg)
Term kind-check rules
![Page 28: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/28.jpg)
What still to be done
Nested records Abstract modules and existentials Kind and type inferencing Implement it for Atze in UHC
![Page 29: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom](https://reader030.vdocuments.site/reader030/viewer/2022032521/56649d595503460f94a38f94/html5/thumbnails/29.jpg)
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