Stitch: The Sound Type-Indexed Type Checker Richard A. Eisenberg - - PowerPoint PPT Presentation

stitch the sound type indexed type checker
SMART_READER_LITE
LIVE PREVIEW

Stitch: The Sound Type-Indexed Type Checker Richard A. Eisenberg - - PowerPoint PPT Presentation

Stitch: The Sound Type-Indexed Type Checker Richard A. Eisenberg Bryn Mawr College rae@cs.brynmawr.edu Wednesday, April 25, 2018 New York City Haskell Users' Group New York, NY, USA A brief history of Haskell types type classes (Wadler


slide-1
SLIDE 1

Stitch: The Sound Type-Indexed Type Checker

Richard A. Eisenberg Bryn Mawr College rae@cs.brynmawr.edu

Wednesday, April 25, 2018 New York City Haskell Users' Group New York, NY, USA

slide-2
SLIDE 2

A brief history of Haskell types

  • type classes (Wadler & Blott, POPL '89)
  • functional dependencies (Jones, ESOP '00)
  • data families (Chakravarty et al., POPL '05)
  • type families (Chakravarty et al., ICFP '05)
  • GADTs (Peyton Jones et al., ICFP '06)
  • datatype promotion (Yorgey et al., TLDI '12)
  • singletons (Eisenberg & Weirich, HS '12)
  • Type :: Type (Weirich et al., ICFP '13)
  • closed type families (Eisenberg et al., POPL '14)
  • GADT pattern checking (Karachalias et al., ICFP '15)
  • injective type families (Stolarek et al., HS '15)
  • type application (Eisenberg et al., ESOP '16)
  • new new Typeable (Peyton Jones et al., Wadlerfest '16)
  • pattern synonyms (Pickering et al., HS '16)
  • quantified class constraints (Bottu et al., HS '17)
slide-3
SLIDE 3

How can we use all this technology?

slide-4
SLIDE 4

Stitch!

Download from: https://cs.brynmawr.edu/~rae/pubs.html

K#CCE.-),KDGC

slide-5
SLIDE 5

Demo time!

slide-6
SLIDE 6

De Bruijn indices A de Bruijn index counts the number of intervening binders between a variable binding and its occurrence.

slide-7
SLIDE 7

De Bruijn indices Why?

  • No shadowing
  • Names are meaningless anyway
  • Easier to formalize

Why not?

  • Hard for humans
slide-8
SLIDE 8

A type-indexed abstract
 syntax tree

data Exp :: forall n. Ctx n

  • > Type -> Type where

Var :: Elem ctx ty -> Exp ctx ty Lam :: TypeRep arg

  • > Exp (arg :> ctx) res
  • > Exp ctx (arg -> res)

App :: Exp ctx (arg -> res)

  • > Exp ctx arg -> Exp ctx res

...

Language.Stitch.Exp

slide-9
SLIDE 9

But first, we must parse!

slide-10
SLIDE 10

A length-indexed abstract
 syntax tree

data UExp (n :: Nat) = UVar (Fin n)
 | ULam Ty (UExp (Succ n)) | UApp (UExp n) (UExp n) | ULet (UExp n) (UExp (Succ n)) | ...

IEG IAEE IKG EKE CK'EC

  • Language.Stitch.Unchecked
slide-11
SLIDE 11

What's that Fin? Fin stands for finite set. The type Fin n contains exactly n values.

slide-12
SLIDE 12

What's that Fin?

data Nat = Zero | Succ Nat data Fin :: Nat -> Type where FZ :: Fin (Succ n) FS :: Fin n -> Fin (Succ n) FS (FS FZ) :: Fin 5 FS (FS FZ) :: Fin 3 FS (FS FZ) :: Fin 2 @2 @0 @???

Language.Stitch.Data.Fin

slide-13
SLIDE 13

A length-indexed abstract
 syntax tree

data UExp (n :: Nat) = UVar (Fin n)
 | ULam Ty (UExp (Succ n)) | UApp (UExp n) (UExp n) | ULet (UExp n) (UExp (Succ n)) | ...

Language.Stitch.Unchecked

)CCICDK CCG

slide-14
SLIDE 14

Well scoped parsing

How to parse an identifier? var :: Parser (UExp n) but we don't know what n should be

slide-15
SLIDE 15

To the code!

slide-16
SLIDE 16

Types Key idea: use GHC’s TypeRep The value of type TypeRep a represents the type a.

slide-17
SLIDE 17

Types

data TypeRep (a :: k) class Typeable (a :: k) typeRep :: Typeable a => TypeRep a eqTypeRep :: TypeRep a


  • > TypeRep b

  • > Maybe (a :~~: b)

GIGG DGIGG

slide-18
SLIDE 18

Types

eqTypeRep :: TypeRep a


  • > TypeRep b

  • > Maybe (a :~~: b)

data (a :: k1) :~~: (b :: k2) where
 HRefl :: a :~~: a

KIE GIGKEC HCK

KGIEKBE EKKDKCCBEE.

slide-19
SLIDE 19

Types

eqTypeRep :: TypeRep a


  • > TypeRep b

  • > Maybe (a :~~: b)

data (a :: k1) :~~: (b :: k2) where
 HRefl :: a :~~: a

KIE GIGKEC HCK

KGIEKBE EKKDKCCBEE. KKEKKIKD

slide-20
SLIDE 20

Types

eqTypeRep :: TypeRep a


  • > TypeRep b

  • > Maybe (a :~~: b)

data (a :: k1) :~~: (b :: k2) where
 HRefl :: a :~~: a cast :: a :~~: b -> a -> b
 cast HRefl x = x

GKKIE'DKEC KCC.KK(

slide-21
SLIDE 21

But first, we must parse!

slide-22
SLIDE 22

Parsing a TypeRep

How to parse a TypeRep? ty :: Parser n (TypeRep t) but we don't know what t should be

slide-23
SLIDE 23

Existentials

data Ex :: (k -> Type) -> Type where Ex :: a i -> Ex a

KEKCCE
 KEKDEKEEICKKG Thus, Ex TypeRep is a representation of any type.

type Ty = Ex (TypeRep :: Type -> Type)

)IGIEKKGBEG(

slide-24
SLIDE 24

Parsing a TypeRep

How to parse a TypeRep? ty :: Parser n Ty

data UExp (n :: Nat) = UVar (Fin n)
 | ULam Ty (UExp (Succ n)) | UApp (UExp n) (UExp n) | ULet (UExp n) (UExp (Succ n)) | ...

slide-25
SLIDE 25

Milepost

  • Parsed into a well scoped AST
  • AST uses Fin for de Bruijn indices
  • Parser indexed by # of vars in scope
  • Parser env't is a length-indexed vec
  • Parsing types requires existentials
slide-26
SLIDE 26

A type-indexed abstract
 syntax tree

data Exp :: forall n. Ctx n

  • > Type -> Type where

Var :: Elem ctx ty -> Exp ctx ty Lam :: TypeRep arg

  • > Exp (arg :> ctx) res
  • > Exp ctx (arg -> res)

App :: Exp ctx (arg -> res)

  • > Exp ctx arg -> Exp ctx res

...

Language.Stitch.Exp

slide-27
SLIDE 27

A type-indexed abstract
 syntax tree

data Exp :: forall n. Ctx n

  • > Type -> Type

Language.Stitch.Exp

If exp :: Exp ctx ty then ctx ⊢ exp : ty

slide-28
SLIDE 28

Contexts

Language.Stitch.Exp

type Ctx n = Vec Type n

  • A context is a vector of types.
  • A de Bruijn index is just an

index into this vector. KKG

slide-29
SLIDE 29

Contexts

Language.Stitch.Exp

  • A context is a vector of types.
  • A de Bruijn index is just an

index into this vector. KKG type Ctx n = Vec Type n

slide-30
SLIDE 30

A type-indexed abstract
 syntax tree

data Exp :: forall n. Ctx n

  • > Type -> Type where

Var :: Elem ctx ty -> Exp ctx ty Lam :: TypeRep arg

  • > Exp (arg :> ctx) res
  • > Exp ctx (arg -> res)

App :: Exp ctx (arg -> res)

  • > Exp ctx arg -> Exp ctx res

...

Language.Stitch.Exp

GCDIG IIE

slide-31
SLIDE 31

A type-indexed abstract
 syntax tree

data Exp :: forall n. Ctx n

  • > Type -> Type where

Var :: Elem ctx ty -> Exp ctx ty Lam :: TypeRep arg

  • > Exp (arg :> ctx) res
  • > Exp ctx (arg -> res)

App :: Exp ctx (arg -> res)

  • > Exp ctx arg -> Exp ctx res

...

Language.Stitch.Exp

IAE E

slide-32
SLIDE 32

Informative de Bruijn index

Language.Stitch.Data.Vec

data Elem :: forall a n. Vec a n

  • > a -> Type where

EZ :: Elem (x :> xs) x ES :: Elem xs x -> Elem (y :> xs) x

KII((( (((IKI

slide-33
SLIDE 33

Type checking

check :: UExp n -> M (Exp ctx ty)

slide-34
SLIDE 34

Type checking

check :: UExp n -> M (Exp ctx ty) check :: ∀ (ctx :: Ctx n).
 UExp n


  • > M (∃ ty. Exp ctx ty)
slide-35
SLIDE 35

Type checking

check :: UExp n -> M (Exp ctx ty) check :: ∀ (ctx :: Ctx n).
 UExp n


  • > M (∃ ty. Exp ctx ty)

check :: ∀ (ctx :: Ctx n). UExp n

  • > (∀ ty. Exp ctx ty -> M r)
  • > M r
slide-36
SLIDE 36

Type checking

check :: ∀ (ctx :: Ctx n). UExp n

  • > (∀ ty. Exp ctx ty -> M r)
  • > M r
slide-37
SLIDE 37

Type checking

check :: ∀ (ctx :: Ctx n). UExp n

  • > (∀ ty. Exp ctx ty -> M r)
  • > M r

check :: Sing (ctx :: Ctx n)

  • > UExp n
  • > (∀ ty. TypeRep ty
  • > Exp ctx ty -> M r)
  • > M r
slide-38
SLIDE 38

Type checking

check :: ∀ (ctx :: Ctx n). UExp n

  • > (∀ ty. Exp ctx ty -> M r)
  • > M r

check :: Sing (ctx :: Ctx n)

  • > UExp n
  • > (∀ ty. TypeRep ty
  • > Exp ctx ty -> M r)
  • > M r
slide-39
SLIDE 39

Type checking

check :: Sing (ctx :: Ctx n)

  • > UExp n
  • > (∀ ty. TypeRep ty
  • > Exp ctx ty -> M r)
  • > M r

ECKEKI.), 'GEG!

Language.Stitch.Check

slide-40
SLIDE 40

To the code!

slide-41
SLIDE 41

Evaluation It's easy! If it type-checks, it works!

slide-42
SLIDE 42

Common Subexpression Elimination It's easy! If it type-checks, it works!

slide-43
SLIDE 43

Common Subexpression Elimination

Generalized to data HashMap k v = ... data IHashMap (k :: i -> Type) (v :: i -> Type) = ...

It took ~1hr for ~2k lines.

slide-44
SLIDE 44

Common Subexpression Elimination

data IHashMap (k :: i -> Type) (v :: i -> Type) = ...

Writing instances requires quantified class constraints.

slide-45
SLIDE 45

Conclusion It's good to be fancy!

slide-46
SLIDE 46

Dependent Types

  • Stephanie Weirich and I have a grant
  • Lots of GHC proposals
  • Summer research students:


Nadine, Dorothy, Eileen, My, Emma, Pablo, Ningning, and Matt

  • Goals: merge type/term parsers,

implement dependent Core, enable interactive error messages

slide-47
SLIDE 47

Dependent Types

  • Upcoming research leave: 2019-20
  • Goal: Merge on π-day, 2021
  • Help wanted!
slide-48
SLIDE 48

Stitch: The Sound Type-Indexed Type Checker

Richard A. Eisenberg Bryn Mawr College rae@cs.brynmawr.edu

Wednesday, April 25, 2018 New York City Haskell Users' Group New York, NY, USA

https://cs.brynmawr.edu/~rae/pubs.html