Scrapping Your Dependently- Typed Boilerplate is Hard Ahmad - - PowerPoint PPT Presentation

scrapping your dependently typed boilerplate is hard
SMART_READER_LITE
LIVE PREVIEW

Scrapping Your Dependently- Typed Boilerplate is Hard Ahmad - - PowerPoint PPT Presentation

Scrapping Your Dependently- Typed Boilerplate is Hard Ahmad Salim Al-Sibahi Supervised by: Dr. Peter Sesto@ David R. ChrisCansen IT University of


slide-1
SLIDE 1

Scrapping ¡Your ¡Dependently-­‑ Typed ¡Boilerplate ¡is ¡Hard ¡

Ahmad ¡Salim ¡Al-­‑Sibahi ¡ Supervised ¡by: ¡

  • Dr. ¡Peter ¡Sesto@ ¡

David ¡R. ¡ChrisCansen ¡ IT ¡University ¡of ¡Copenhagen ¡

slide-2
SLIDE 2

DisposiCon ¡

  • IntroducCon ¡
  • Uniplate ¡in ¡10 ¡minutes ¡
  • The ¡Hard ¡Part ¡
  • Related ¡Work ¡

13/07/14 ¡ 2 ¡

slide-3
SLIDE 3

IntroducCon ¡

  • Outline ¡for ¡a ¡problem ¡ ¡
  • Example-­‑oriented ¡approach ¡ ¡
  • Based ¡on ¡my ¡work ¡on ¡described ¡types ¡

13/07/14 ¡ 3 ¡

slide-4
SLIDE 4

MoCvaCon ¡

  • Generic ¡traversal ¡frameworks ¡aims ¡to ¡reduce ¡

repeCCve ¡programs ¡using ¡pracCcal ¡interfaces ¡

  • Dependently-­‑typed ¡programming ¡is ¡geVng ¡

increasingly ¡popular ¡as ¡paradigm ¡

13/07/14 ¡ 4 ¡

slide-5
SLIDE 5

UNIPLATE ¡IN ¡10 ¡MINUTES ¡

A ¡Framework ¡to ¡Scrap ¡Your ¡Boilerplate ¡

13/07/14 ¡ 5 ¡

slide-6
SLIDE 6

Background ¡

  • Popular ¡framework ¡created ¡by ¡ ¡Neil ¡Mitchell ¡
  • Based ¡on ¡Scrap ¡Your ¡Boilerplate ¡(SYB) ¡by ¡Ralf ¡

Lämmel ¡and ¡Simon ¡Peyton-­‑Jones ¡

13/07/14 ¡ 6 ¡

slide-7
SLIDE 7

Background ¡

¡

  • Type-­‑directed ¡querying ¡and ¡transformaCon ¡
  • AutomaCcally ¡derivable ¡using ¡generic ¡

programming ¡techniques ¡

13/07/14 ¡ 7 ¡

slide-8
SLIDE 8

Interface ¡-­‑ ¡Uniplate ¡

children :: :: Uniplate on => =>

  • n ->
  • > [on]

universe :: :: Uniplate on => =>

  • n ->
  • > [on]

descend :: :: Uniplate on => => (on ->

  • > on) ->
  • > on ->
  • > on

transform :: :: Uniplate on => => (on ->

  • > on) ->
  • > on ->
  • > on

Haskell ¡

13/07/14 ¡ 8 ¡

slide-9
SLIDE 9

Interface ¡-­‑ ¡Biplate ¡

childrenBi :: :: Biplate from to => => from ->

  • > [to]

universeBi :: :: Biplate from to => => from ->

  • > [to]

descendBi :: :: Biplate from to => => (to ->

  • > to) ->
  • > from ->
  • > from

transformBi :: :: Biplate from to => => (to ->

  • > to) ->
  • > from ->
  • > from

Haskell ¡

13/07/14 ¡ 9 ¡

slide-10
SLIDE 10

Example: ¡Blog ¡Post ¡

type type Title = String type type Timestamp = Int data data Post = Single Title Timestamp | Aggregate [Post] timestamps :: :: Post ->

  • > [Timestamp]

timestamps = universeBi capitaliseTitles :: :: Post ->

  • > Post

capitaliseTitles = transformBi capitalise

Haskell ¡

13/07/14 ¡ 10 ¡

slide-11
SLIDE 11

Adapted ¡RealisCc ¡Case ¡

data TT = Ref Name TT | Var Int | Bind Name TT TT | App TT TT | ConstantStr String | ConstantI Int | Proj TT Int | Type | Erased

Haskell ¡

13/07/14 ¡ 11 ¡

slide-12
SLIDE 12

Adapted ¡RealisCc ¡Case ¡

freeNames :: :: TT ->

  • > [Name]

freeNames (Ref n _) =[n] freeNames (Bind n ty sc) = freeNames ty ++ (freeNames sc \\ [n]) freeNames (App tf ta) = freeNames tf ++ freeNames ta freeNames (Proj tm _) = freeNames tm freeNames _ = []

Haskell ¡

13/07/14 ¡ 12 ¡

slide-13
SLIDE 13

Adapted ¡RealisCc ¡Case ¡

freeNames :: :: TT ->

  • > [Name]

freeNames (Ref n _) = [n] freeNames (Bind n ty sc) = freeNames ty ++ (freeNames sc \\ [n]) freeNames tm = concat [freeNames t | t <- <- children tm]

Haskell ¡

13/07/14 ¡ 13 ¡

slide-14
SLIDE 14

Uniplate ¡type ¡class ¡

class class Uniplate on where where uniplate :: :: on ->

  • >

([on], [on] ->

  • > on)

13/07/14 ¡ 14 ¡

Haskell ¡

slide-15
SLIDE 15

Instance ¡of ¡Uniplate ¡

instance instance Uniplate Post where where uniplate (Single ttl ts) = ([], \_ ->

  • > Single ttl ts)

uniplate (Aggregate psts) = (psts, \psts' ->

  • > Aggregate psts’)

Haskell ¡

13/07/14 ¡ 15 ¡

slide-16
SLIDE 16

Biplate ¡type ¡class ¡

class class Uniplate to => Biplate from to where where biplate :: :: from ->

  • > ([to], [to] ->
  • > from)

13/07/14 ¡ 16 ¡

Haskell ¡

slide-17
SLIDE 17

Instance ¡of ¡Biplate ¡

instance instance Biplate Post Timestamp where where biplate (Single ttl ts) = ([ts], \ts' ->

  • > Single ttl (head ts’))

biplate (Aggregate psts) = ([], \_ ->

  • > Aggregate psts)

Haskell ¡

13/07/14 ¡ 17 ¡

slide-18
SLIDE 18

THE ¡HARD ¡PART ¡

How ¡I ¡tried ¡and ¡failed ¡to ¡specify ¡a ¡correct ¡interface ¡ for ¡a ¡dependent ¡version ¡of ¡Uniplate ¡

13/07/14 ¡ 18 ¡

slide-19
SLIDE 19

AutomaCc ¡Deriving ¡

¡

  • Significantly ¡simplifies ¡usage ¡of ¡the ¡library ¡
  • Depends ¡on ¡structure ¡of ¡datatypes ¡(like ¡Eq ¡or ¡

Show) ¡

  • Works ¡only ¡on ¡monomorphic ¡types ¡

13/07/14 ¡ 19 ¡

slide-20
SLIDE 20

AutomaCc ¡Deriving ¡

data data List_Nat : Set where where nil : List_Nat cons : (head : Nat) (tail : List_Nat)

  • >
  • > List_Nat

Agda ¡

13/07/14 ¡ 20 ¡

slide-21
SLIDE 21

AutomaCc ¡Deriving ¡

data data Vec_Nat : Nat ->

  • > Set where

where nil : Vec_Nat zero cons : {n : Nat} (head : Nat) (tail : Vec_Nat n)

  • >
  • > Vec_Nat (suc n)

Agda ¡ Index ¡indisCnguishable ¡from ¡ordinary ¡data ¡

13/07/14 ¡ 21 ¡

slide-22
SLIDE 22

AutomaCc ¡Deriving ¡

data OList : Nat -> Set where nil : {n : Nat} -> OList n cons : {n : Nat} (head : Nat) (ok : n <= head) (tail : OList head)

  • > OList n

Agda ¡ Index ¡is ¡based ¡on ¡ordinary ¡data ¡

13/07/14 ¡ 22 ¡

slide-23
SLIDE 23

Generic ¡Querying ¡

Agda ¡

childrenBi : {from to : Set} {{bip : Biplate from to}}

  • >
  • > from ->
  • > List to

universeBi : {from to : Set} {{bip : Biplate from to}}

  • >
  • > from ->
  • > List to

List ¡Nat ¡may ¡be ¡interesCng ¡ ¡ Vec ¡0 ¡Nat ¡is ¡definitely ¡not ¡

13/07/14 ¡ 23 ¡

slide-24
SLIDE 24

Generic ¡Querying ¡

Agda ¡

childrenBi : {from ix : Set} {to : ix ->

  • > Set}

{{bip : Biplate from to}}

  • >
  • > from ->
  • > List (Sigma ix to)

universeBi : {from ix : Set} {to : ix ->

  • > Set}

{{bip : Biplate from to}}

  • >
  • > from ->
  • > List (Sigma ix to)

Is ¡all ¡data ¡we ¡are ¡geVng ¡useful? ¡

13/07/14 ¡ 24 ¡

slide-25
SLIDE 25

Generic ¡Traversal ¡

descendBi : {from to : Set} {{bip : Biplate from to}}

  • >
  • > (to ->
  • > to) ->
  • > from ->
  • > from

transformBi : {from to : Set} {{bip : Biplate from to}}

  • >
  • > (to ->
  • > to) ->
  • > from ->
  • > from

Agda ¡

13/07/14 ¡ 25 ¡

slide-26
SLIDE 26

Generic ¡Traversal ¡

descendBi : {from ix : Set} {to : ix ->

  • > Set}

{{bip : Biplate from to}}

  • >
  • > (Sigma ix to ->
  • > Sigma ix to)
  • >
  • > from ->
  • > from

transformBi : {from ix : Set} {to : ix ->

  • > Set}

{{bip : Biplate from to}}

  • >
  • > (Sigma ix to ->
  • > Sigma ix to)
  • >
  • > from ->
  • > from

Agda ¡ Is ¡this ¡OK? ¡

13/07/14 ¡ 26 ¡

slide-27
SLIDE 27

Generic ¡Traversal ¡

data data TwoVec : Nat ->

  • > Set where

where two_vec : {n : Nat}

  • >
  • > Vec_Nat n ->
  • > Vec_Nat (S n)
  • >
  • > TwoVec n

double : Sigma Nat Vec_Nat

  • > Sigma Nat Vec_Nat

double (proj1 , proj2) = plus proj1 proj1 , append proj2 proj2

Agda ¡ What ¡if ¡we ¡do ¡“descendBi double xs”? ¡

13/07/14 ¡ 27 ¡

slide-28
SLIDE 28

Generic ¡Traversal ¡

  • TransformaCons ¡can ¡break ¡dependent ¡

datatype ¡invariants ¡

  • TransformaCons ¡must ¡be ¡constrained ¡to ¡only ¡

allow ¡creaCon ¡of ¡valid ¡structures ¡

13/07/14 ¡ 28 ¡

slide-29
SLIDE 29

Generic ¡Traversal ¡

data OList : Nat -> Set where nil : {n : Nat} -> OList n cons : {n : Nat} (head : Nat) (ok : n <= head) (tail : OList head)

  • > OList n

Agda ¡ OList ¡is ¡the ¡best ¡ ¡to ¡describe ¡the ¡constraints ¡of ¡ OList ¡

13/07/14 ¡ 29 ¡

slide-30
SLIDE 30

Generic ¡Traversal ¡

data data Vec (A : Set) (n : Nat) : Set where where nil : {{ix : n == == zero}} ->

  • > Vec A n

_::_ : {m : Nat} {{ix : n == == suc m}} (x : A) (xs : Vec A m) ->

  • > Vec A n

13/07/14 ¡ 30 ¡

Index-­‑like ¡restricCons ¡can ¡be ¡added ¡to ¡any ¡ datatype ¡ Agda ¡

slide-31
SLIDE 31

RELATED ¡WORK ¡

Fret ¡not! ¡There ¡is ¡sCll ¡hope. ¡

13/07/14 ¡ 31 ¡

slide-32
SLIDE 32

SYB ¡in ¡a ¡closed ¡universe ¡

  • Work ¡by ¡Larry ¡Diehl ¡
  • Typecasing ¡on ¡universes ¡using ¡described ¡types ¡
  • Traversal ¡change ¡the ¡type ¡of ¡input ¡

13/07/14 ¡ 32 ¡

slide-33
SLIDE 33

TransporCng ¡funcCons ¡across ¡

  • rnaments ¡
  • Work ¡by ¡Conor ¡McBride ¡and ¡Piérre-­‑Evariste ¡

Dagand ¡

13/07/14 ¡ 33 ¡

slide-34
SLIDE 34

TransporCng ¡funcCon ¡across ¡

  • rnaments ¡

13/07/14 ¡ 34 ¡

slide-35
SLIDE 35

DISCUSSION ¡

Just ¡3 ¡more ¡minutes… ¡

13/07/14 ¡ 35 ¡

slide-36
SLIDE 36

Conclusion ¡

  • Dependent ¡types ¡allow ¡us ¡to ¡constraint ¡our ¡

datatypes ¡

– However ¡we ¡lose ¡structural ¡genericity ¡

  • It ¡is ¡hard ¡to ¡define ¡a ¡correct ¡generalized ¡

interface ¡for ¡SYB-­‑style ¡programming ¡

– AutomaCc ¡Deriving: ¡Hard ¡ – Generic ¡Querying: ¡Doable ¡ – Generic ¡Traversal: ¡Hard ¡

  • Extrinsic ¡vs. ¡Intrinsic ¡proving ¡

13/07/14 ¡ 36 ¡

slide-37
SLIDE 37

ContribuCon ¡

  • An ¡example-­‑based ¡problem ¡definiCon ¡of ¡why ¡

it ¡can ¡be ¡hard ¡to ¡implement ¡a ¡SYB-­‑style ¡ generics ¡library ¡using ¡dependent ¡types ¡

13/07/14 ¡ 37 ¡

slide-38
SLIDE 38

QuesCons ¡

And ¡feedback ¡too! ¡

13/07/14 ¡ 38 ¡

‽ ¡