the practical guide to levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · •...
TRANSCRIPT
![Page 1: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/1.jpg)
The Practical Guide to Levitation
By Ahmad Salim Al-Sibahi Supervisors:
Dr. Peter Sestoft David R. Christiansen
![Page 2: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/2.jpg)
A Practical Guide to Levitation
http://wordswithoutborders.org/article/a-practical-guide-to-levitation
• Excellent, but unrelated short story by José Agualusa
2
![Page 3: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/3.jpg)
Disposition
• Introduction • Idris overview • Tutorial on described types • Generic algorithm examples • Overhead and specialisation • Discussion
3
![Page 4: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/4.jpg)
INTRODUCTION
4
![Page 5: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/5.jpg)
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
![Page 6: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/6.jpg)
Motivation
To investigate how described types can be used in a practical language, what challenges arise, and how to achieve acceptable performance
6
![Page 7: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/7.jpg)
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
![Page 8: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/8.jpg)
IDRIS OVERVIEW
8
![Page 9: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/9.jpg)
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
![Page 10: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/10.jpg)
Core Features • Indexed datatypes data Vec : (a : Type) -> Nat -> Type 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
![Page 11: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/11.jpg)
TUTORIAL ON DESCRIBED TYPES
![Page 12: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/12.jpg)
Anatomy of a datatype
12
data Vec : (a : Type) -> Nat -> Type where Nil : Vec a Z Cons : {n : Nat} -> a -> Vec a n -> Vec a (S n)
type constructor
![Page 13: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/13.jpg)
Describing datatypes
data Desc : Type where
Ret : Desc
Arg : (a : Type) -> (a -> Desc) -> Desc
Rec : Desc -> Desc
13
![Page 14: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/14.jpg)
Describing natural numbers
data Nat : Type where
Zero : Nat
Succ : Nat -> Nat
NatDesc : Desc
NatDesc = Arg Bool
(\isZero =>
if isZero
then Ret
else Rec Ret)
14
![Page 15: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/15.jpg)
Describing list
data List :
(a : Type) -> Type where
Nil : List a
Cons : a -> List a ->
List a
ListDesc :
(a : Type) -> Desc
ListDesc a =
Arg Bool (\isNil =>
if isNil
then Ret
else Arg a (\x => Rec Ret))
15
![Page 16: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/16.jpg)
Supporting indexing
data Desc : (ix : Type) -> Type where
Ret : ix -> Desc ix
Arg : (a : Type) -> (a -> Desc ix) ->
Desc ix
Rec : ix -> Desc ix -> Desc ix
16
![Page 17: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/17.jpg)
Informative encoding of tags
CLabel : Type
CLabel = String
CEnum : Type
CEnum = List CLabel
data Tag : CLabel -> CEnum -> Type where TZ : Tag l (l :: e)
TS : Tag l e -> Tag l (l' :: e)
17
![Page 18: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/18.jpg)
Describing vectors
data Vec :
(a : Type) -> Nat -> Type where
Nil : Vec a Z
Cons : {n : Nat} -> a ->
Vec a n ->
Vec a (S n)
VecDesc :
(a : Type) -> Desc Nat
VecDesc a =
Arg CLabel (\l=>
Arg (Tag l
[ “Nil” , “Cons” ])
((switchDesc
(Ret Z
, (Arg Nat (\n=>
Arg a (\x=>
Rec n
(Ret (S n))))
, () ) )) l))
18
![Page 19: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/19.jpg)
Synthesising datatypes
Synthesise : Desc ix -> (ix -> Type) ->
(ix -> Type)
Synthesise (Ret j) x i = (j = i)
Synthesise (Rec j d) x i =
(rec : x j ** Synthesise d x i)
Synthesise (Arg a d) x i =
(arg : a ** Synthesise (d arg) x i)
19
![Page 20: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/20.jpg)
Tying-up recursion
data Data : {ix : Type} -> Desc ix
-> ix -> Type where
Con : {d : Desc ix} -> {i : ix} ->
Synthesise d (Data d) i ->
Data d i
20
![Page 21: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/21.jpg)
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
![Page 22: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/22.jpg)
EXAMPLES OF GENERIC ALGORITHM
22
![Page 23: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/23.jpg)
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
![Page 24: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/24.jpg)
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
![Page 25: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/25.jpg)
OVERHEAD AND SPECIALISATION
25
![Page 26: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/26.jpg)
Overhead comparison
• Generic version of [42]
Con ("Cons" ** (TS TZ **
(0 ** (42 **
(Con ("Nil” ** (TZ ** Refl))
** Refl)))))
26
![Page 27: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/27.jpg)
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
![Page 28: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/28.jpg)
Specialisation algorithm
• Specialise sta+c parameters • Unbox nested types • Applying the trick • Subs+tute indices • Do corresponding specialisa+on for dependent func+ons
28
![Page 29: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/29.jpg)
Specialising static parameters
data Data__Vec_Int : Nat -> Type where
Con : Synthesise (VecD Int) Data__Vec_Int i ->
Data d i
29
![Page 30: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/30.jpg)
Inlining and unboxing
data Data__Vec_Int : Nat -> Type 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
![Page 31: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/31.jpg)
The Trick and index subsitution
data Data__Vec_Int : Nat -> Type 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
![Page 32: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/32.jpg)
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
![Page 33: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/33.jpg)
DISCUSSION
33
![Page 34: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/34.jpg)
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
![Page 35: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/35.jpg)
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
![Page 36: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/36.jpg)
QUESTIONS
36
![Page 37: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/37.jpg)
OTHER SLIDES
37
![Page 38: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/38.jpg)
GENERIC TRAVERSAL AND LIMITATIONS
38
![Page 39: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/39.jpg)
Background
• Type-directed querying and transformation • Automatically derivable using generic
programming • Example: capitalise all titles in a CMS system
(with tree-like structure)
39
![Page 40: The Practical Guide to Levitationitu.dk/people/asal/pubs/msc-thesis-presentation.pdf · • Introduction • Idris overview ... datatypes in dependently typed programming languages](https://reader033.vdocument.in/reader033/viewer/2022042807/5f7708110247e23a547cb826/html5/thumbnails/40.jpg)
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