scrapping your dependently typed boilerplate is hard
play

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


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

  2. DisposiCon ¡ • IntroducCon ¡ • Uniplate ¡in ¡10 ¡minutes ¡ • The ¡Hard ¡Part ¡ • Related ¡Work ¡ 13/07/14 ¡ 2 ¡

  3. IntroducCon ¡ • Outline ¡for ¡a ¡problem ¡ ¡ • Example-­‑oriented ¡approach ¡ ¡ • Based ¡on ¡my ¡work ¡on ¡described ¡types ¡ 13/07/14 ¡ 3 ¡

  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 ¡

  5. A ¡Framework ¡to ¡Scrap ¡Your ¡Boilerplate ¡ UNIPLATE ¡IN ¡10 ¡MINUTES ¡ 13/07/14 ¡ 5 ¡

  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 ¡

  7. Background ¡ ¡ • Type-­‑directed ¡querying ¡and ¡transformaCon ¡ • AutomaCcally ¡derivable ¡using ¡generic ¡ programming ¡techniques ¡ 13/07/14 ¡ 7 ¡

  8. Interface ¡-­‑ ¡Uniplate ¡ Haskell ¡ children :: :: Uniplate on => => on -> -> [on] universe :: :: Uniplate on => => on -> -> [on] descend :: :: Uniplate on => => (on -> -> on) -> -> on -> -> on transform :: :: Uniplate on => => (on -> -> on) -> -> on -> -> on 13/07/14 ¡ 8 ¡

  9. Interface ¡-­‑ ¡Biplate ¡ Haskell ¡ 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 13/07/14 ¡ 9 ¡

  10. Example: ¡Blog ¡Post ¡ Haskell ¡ type Title = String type type type Timestamp = Int data data Post = Single Title Timestamp | Aggregate [Post] timestamps :: :: Post -> -> [Timestamp] timestamps = universeBi capitaliseTitles :: :: Post -> -> Post capitaliseTitles = transformBi capitalise 13/07/14 ¡ 10 ¡

  11. Adapted ¡RealisCc ¡Case ¡ Haskell ¡ data TT = Ref Name TT | Var Int | Bind Name TT TT | App TT TT | ConstantStr String | ConstantI Int | Proj TT Int | Type | Erased 13/07/14 ¡ 11 ¡

  12. Adapted ¡RealisCc ¡Case ¡ Haskell ¡ 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 _ = [] 13/07/14 ¡ 12 ¡

  13. Adapted ¡RealisCc ¡Case ¡ Haskell ¡ 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] 13/07/14 ¡ 13 ¡

  14. Uniplate ¡type ¡class ¡ Haskell ¡ class class Uniplate on where where uniplate :: :: on -> -> ([on], [on] -> -> on) 13/07/14 ¡ 14 ¡

  15. Instance ¡of ¡Uniplate ¡ Haskell ¡ instance instance Uniplate Post where where uniplate (Single ttl ts) = ([], \_ -> -> Single ttl ts) uniplate (Aggregate psts) = (psts, \psts' -> -> Aggregate psts’) 13/07/14 ¡ 15 ¡

  16. Biplate ¡type ¡class ¡ Haskell ¡ class class Uniplate to => Biplate from to where where biplate :: :: from -> -> ([to], [to] -> -> from) 13/07/14 ¡ 16 ¡

  17. Instance ¡of ¡Biplate ¡ Haskell ¡ instance instance Biplate Post Timestamp where where biplate (Single ttl ts) = ([ts], \ts' -> -> Single ttl (head ts’)) biplate (Aggregate psts) = ([], \_ -> -> Aggregate psts) 13/07/14 ¡ 17 ¡

  18. How ¡I ¡tried ¡and ¡failed ¡to ¡specify ¡a ¡correct ¡interface ¡ for ¡a ¡dependent ¡version ¡of ¡Uniplate ¡ THE ¡HARD ¡PART ¡ 13/07/14 ¡ 18 ¡

  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 ¡

  20. AutomaCc ¡Deriving ¡ Agda ¡ data data List_Nat : Set where where nil : List_Nat cons : (head : Nat) (tail : List_Nat) -> -> List_Nat 13/07/14 ¡ 20 ¡

  21. AutomaCc ¡Deriving ¡ Agda ¡ 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) Index ¡indisCnguishable ¡from ¡ordinary ¡data ¡ 13/07/14 ¡ 21 ¡

  22. AutomaCc ¡Deriving ¡ Agda ¡ data OList : Nat -> Set where � nil : {n : Nat} -> OList n � cons : {n : Nat} � (head : Nat) (ok : n <= head) � (tail : OList head) � -> OList n � Index ¡is ¡based ¡on ¡ordinary ¡data ¡ 13/07/14 ¡ 22 ¡

  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 ¡

  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 ¡

  25. Generic ¡Traversal ¡ Agda ¡ descendBi : {from to : Set} {{bip : Biplate from to}} -> -> (to -> -> to) -> -> from -> -> from transformBi : {from to : Set} {{bip : Biplate from to}} -> -> (to -> -> to) -> -> from -> -> from 13/07/14 ¡ 25 ¡

  26. Generic ¡Traversal ¡ descendBi : {from ix : Set} Agda ¡ {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 Is ¡this ¡OK? ¡ 13/07/14 ¡ 26 ¡

  27. Generic ¡Traversal ¡ data data TwoVec : Nat -> -> Set where where Agda ¡ 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 What ¡if ¡we ¡do ¡“ descendBi double xs ”? ¡ 13/07/14 ¡ 27 ¡

  28. Generic ¡Traversal ¡ • TransformaCons ¡can ¡break ¡dependent ¡ datatype ¡invariants ¡ • TransformaCons ¡must ¡be ¡constrained ¡to ¡only ¡ allow ¡creaCon ¡of ¡valid ¡structures ¡ 13/07/14 ¡ 28 ¡

  29. Generic ¡Traversal ¡ Agda ¡ data OList : Nat -> Set where � nil : {n : Nat} -> OList n � cons : {n : Nat} � (head : Nat) (ok : n <= head) � (tail : OList head) � -> OList n � OList ¡is ¡the ¡best ¡ ¡to ¡describe ¡the ¡constraints ¡of ¡ OList ¡ 13/07/14 ¡ 29 ¡

  30. Generic ¡Traversal ¡ Agda ¡ 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 Index-­‑like ¡restricCons ¡can ¡be ¡added ¡to ¡any ¡ datatype ¡ 13/07/14 ¡ 30 ¡

  31. Fret ¡not! ¡There ¡is ¡sCll ¡hope. ¡ RELATED ¡WORK ¡ 13/07/14 ¡ 31 ¡

  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 ¡

  33. TransporCng ¡funcCons ¡across ¡ ornaments ¡ • Work ¡by ¡Conor ¡McBride ¡and ¡Piérre-­‑Evariste ¡ Dagand ¡ 13/07/14 ¡ 33 ¡

  34. TransporCng ¡funcCon ¡across ¡ ornaments ¡ 13/07/14 ¡ 34 ¡

  35. Just ¡3 ¡more ¡minutes… ¡ DISCUSSION ¡ 13/07/14 ¡ 35 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend