the practical guide to levitation
play

The Practical Guide to Levitation By Ahmad Salim Al-Sibahi - PowerPoint PPT Presentation

The Practical Guide to Levitation By Ahmad Salim Al-Sibahi Supervisors: Dr. Peter Sestoft David R. Christiansen A Practical Guide to Levitation http://wordswithoutborders.org/article/a- practical-guide-to-levitation Excellent, but


  1. The Practical Guide to Levitation By Ahmad Salim Al-Sibahi Supervisors: Dr. Peter Sestoft David R. Christiansen

  2. A Practical Guide to Levitation http://wordswithoutborders.org/article/a- practical-guide-to-levitation • Excellent, but unrelated short story by José Agualusa 2

  3. Disposition • Introduction • Idris overview • Tutorial on described types • Generic algorithm examples • Overhead and specialisation • Discussion 3

  4. INTRODUCTION 4

  5. Background • Generic programming allows writing algorithms that work on the structure of datatypes • This saves time and helps avoiding programmer errors • It is possible to have first-class descriptions of datatypes in dependently typed programming languages • Work is mostly theoretically focused 5

  6. Motivation To investigate how described types can be used in a practical language, what challenges arise, and how to achieve acceptable performance 6

  7. Key Contributions • Example-based tutorial for understanding described types • Procedure generating descriptions from ordinary datatype declarations • Generic implementation of commonly needed functionality, notably decidable equality • Discussion of the challenges of implementing a generic traversal library in a dependently-typed programming language • Design of a specialisation algorithm for datatypes based on partial evaluation techniques, suited for optimising described types 7

  8. IDRIS OVERVIEW 8

  9. Description • Haskell and ML inspired programming language with full dependent types • Support for some automation using a small set of tactics • Practically oriented, with features such as partial functions, codata, type classes, and aggressive erasure 9

  10. Core Features • Indexed datatypes data data Vec : (a : Type) -> Nat -> Type where where Nil : Vec a Z Cons : {n : Nat} -> a -> Vec a n -> Vec a (S n) • Type-level functions So : Bool -> Type So True = () So False = _|_ • Built-in notion of propositional equality stillnot1984 : 2 + 2 = 4 Stillnot1984 = Refl 10

  11. TUTORIAL ON DESCRIBED TYPES

  12. Anatomy of a datatype r o t c u r t s n o c e p y t data Vec : (a : Type) -> Nat -> Type where data where Nil : Vec a Z Cons : {n : Nat} -> a -> Vec a n -> Vec a (S n) 12

  13. Describing datatypes data data Desc : Type where where Ret : Desc Arg : (a : Type) -> (a -> Desc) -> Desc Rec : Desc -> Desc 13

  14. Describing natural numbers NatDesc : Desc NatDesc = Arg Bool data Nat : Type where data where (\isZero => Zero : Nat if if isZero Succ : Nat -> Nat then then Ret else else Rec Ret) 14

  15. Describing list ListDesc : (a : Type) -> Desc data List : data (a : Type) -> Type where where ListDesc a = Nil : List a Arg Bool (\isNil => Cons : a -> List a -> if if isNil List a then then Ret else else Arg a (\x => Rec Ret)) 15

  16. Supporting indexing data data Desc : (ix : Type) -> Type where where Ret : ix -> Desc ix Arg : (a : Type) -> (a -> Desc ix) -> Desc ix Rec : ix -> Desc ix -> Desc ix 16

  17. Informative encoding of tags CLabel : Type CLabel = String CEnum : Type CEnum = List CLabel data data Tag : CLabel -> CEnum -> Type where where TZ : Tag l (l :: e) TS : Tag l e -> Tag l (l' :: e) 17

  18. Describing vectors VecDesc : (a : Type) -> Desc Nat VecDesc a = data Vec : data Arg CLabel (\l=> (a : Type) -> Nat -> Type Arg (Tag l where where [ “Nil” , “Cons” ]) Nil : Vec a Z ((switchDesc switchDesc Cons : {n : Nat} -> a -> (Ret Z Vec a n -> , (Arg Nat (\n=> Vec a (S n) Arg a (\x=> Rec n (Ret (S n)))) , () ) )) l)) 18

  19. Synthesising datatypes Synthesise Synthesise : : Desc Desc ix ix -> ( -> (ix ix -> -> Type Type) -> ) -> ( (ix ix -> -> Type Type) ) Synthesise Synthesise (Ret Ret j) ) x x i = ( = (j = i) ) Synthesise Synthesise (Rec Rec j d j d) ) x x i = = (rec rec : : x j x j ** ** Synthesise Synthesise d x d x i) ) Synthesise Synthesise (Arg Arg a d a d) ) x x i = = (arg arg : : a ** ** Synthesise Synthesise (d arg arg) ) x x i) 19

  20. Tying-up recursion data data Data : {ix : Type} -> Desc ix -> ix -> Type where where Con : {d : Desc ix} -> {i : ix} -> Synthesise d (Data d) i -> Data d i 20

  21. Example vector exampleVec : Data (VecDesc Nat) 3 exampleVec = Con (“Cons” ** (TS TZ ** (2 ** (1 ** (Con (“Cons” ** (TS TZ ** (1 ** (2 ** (Con (“Cons” ** (TS TZ ** (0 ** (3 ** (Con (“Nil” ** (TZ ** Refl)) ** Refl) )))) ** Refl) )))) ** Refl) )))) 21

  22. EXAMPLES OF GENERIC ALGORITHM 22

  23. Pretty printing 1. Print the name of the constructor 2. Pretty print the arguments a. If argument is of the same type, repeat procedure recursively b. Otherwise, find the corresponding procedure for the relevant datatype and use that for pretty printing 23

  24. Decidable equality 1. Check if constructor tags are equal, otherwise disprove using difference of constructors lemma 2. Iterate through arguments a. Check if current arguments are equal, otherwise disprove using injectivity of current argument lemma b. Check if the rest of the arguments are equal, otherwise disprove using injectivity of rest of arguments lemma 3. The resulting types are equal if not disproved 24

  25. OVERHEAD AND SPECIALISATION 25

  26. Overhead comparison • Generic version of [42] Con ("Cons" ** (TS TZ ** (0 ** (42 ** (Con ("Nil” ** (TZ ** Refl)) ** Refl))))) 26

  27. Overhead issues • Large amount of data is purely encoding, and does not add significantly more information • Index arguments cannot be automatically erased because they are used in the data • Has implicit arguments which further increases elaboration time 27

  28. Specialisation algorithm • Specialise ¡sta+c ¡parameters ¡ • Unbox ¡nested ¡types ¡ • Applying ¡the ¡trick ¡ • Subs+tute ¡indices ¡ • Do ¡corresponding ¡specialisa+on ¡for ¡ dependent ¡func+ons ¡ 28

  29. Specialising static parameters data data Data__Vec_Int : Nat -> Type where where Con : Synthesise (VecD Int) Data__Vec_Int i -> Data d i 29

  30. Inlining and unboxing data data Data__Vec_Int : Nat -> Type where where Con : (l : CLabel) -> (t : Tag l ["Nil", "Cons”]) -> (arg : Synthesise (switchDesc ( Ret Z , Arg Erasable Nat (\n => Arg None Int (\arg => Rec n (Ret (S n)))) ,() ) l t) i) -> Data d i 30

  31. The Trick and index subsitution data data Data__Vec_Int : Nat -> Type where where Con_Nil : Data_Vec_Int Z «l = "Nil", t = TZ, i = Z» Con_Cons : .(n : Nat) -> (arg : Int) -> (rec : Data_Vec_Int n) -> Data_Vec_Int (S n) «l = "Cons", t = TS TZ, i = S n» 31

  32. Advantages • Exploits the added type information in Idris and other dependently-typed languages • Is not specialised to a particular representation • Does not require a mapping to existing datatypes • Works independently of Constructor Specialisation 32

  33. DISCUSSION 33

  34. Limitations • Constraints for generic algorithms – New type of boilerplate – Not obvious if correct for nested datatypes • Implementation and constraints – Interaction with advanced datatypes – Heuristics for applying the trick – Possible issues with unfolding • Performance – Lack of quantitative analysis (benchmarking) 34

  35. Conclusion • Provided extensions for Idris to work with dependent types • Showed how commonly used, and other interesting generic algorithms could be implemented in a practical language • Designed a suitable specialisation algorithm 35

  36. QUESTIONS 36

  37. OTHER SLIDES 37

  38. GENERIC TRAVERSAL AND LIMITATIONS 38

  39. Background • Type-directed querying and transformation • Automatically derivable using generic programming • Example: capitalise all titles in a CMS system (with tree-like structure) 39

  40. Limitations • Requires type casting, loses parametericity • A deriving algorithm cannot differentiate between data used for indexing and ordinary data • Hard to model a flexible type signature without break datatype invariants 40

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