Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom.

29
Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    1

Transcript of Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom.

Page 1: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom.

Modules in UHC

A proposal by: Tom Hofte & Eric Eijkelenboom

Page 2: 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

Page 3: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

Types and type schemes

Types: τ, ν ::= ν -> τ | A τ | a τ | τ ν Type schemes:

σ ::=forall Δ. σ | exists Δ. σ

| σ -> σ| τ

Δ ::= a : κ, Δ | ε

Page 23: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom.

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.

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.

Kind rules

Page 26: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom.

Record type body rules

Page 27: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom.

Term kind-check rules

Page 28: Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom.

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.

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