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

the practical guide to levitation
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

The Practical Guide to Levitation

By Ahmad Salim Al-Sibahi Supervisors:

  • Dr. Peter Sestoft

David R. Christiansen

slide-2
SLIDE 2

A Practical Guide to Levitation

http://wordswithoutborders.org/article/a- practical-guide-to-levitation

  • Excellent, but unrelated short story by José

Agualusa

2

slide-3
SLIDE 3

Disposition

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

3

slide-4
SLIDE 4

INTRODUCTION

4

slide-5
SLIDE 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

slide-6
SLIDE 6

Motivation

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

6

slide-7
SLIDE 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
  • n partial evaluation techniques, suited for optimising

described types

7

slide-8
SLIDE 8

IDRIS OVERVIEW

8

slide-9
SLIDE 9

Description

  • Haskell and ML inspired programming language

with full dependent types

  • Support for some automation using a small set
  • f tactics
  • Practically oriented, with features such as partial

functions, codata, type classes, and aggressive erasure

9

slide-10
SLIDE 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

slide-11
SLIDE 11

TUTORIAL ON DESCRIBED TYPES

slide-12
SLIDE 12

Anatomy of a datatype

12

data data Vec : (a : Type) -> Nat -> Type where where Nil : Vec a Z Cons : {n : Nat} -> a -> Vec a n -> Vec a (S n) t y p e c

  • n

s t r u c t

  • r
slide-13
SLIDE 13

Describing datatypes

data data Desc : Type where where Ret : Desc Arg : (a : Type) -> (a -> Desc) -> Desc Rec : Desc -> Desc

13

slide-14
SLIDE 14

Describing natural numbers

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

14

slide-15
SLIDE 15

Describing list

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

15

slide-16
SLIDE 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

slide-17
SLIDE 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

slide-18
SLIDE 18

Describing vectors

data data Vec : (a : Type) -> Nat -> Type where 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 switchDesc (Ret Z , (Arg Nat (\n=> Arg a (\x=> Rec n (Ret (S n)))) , () ) )) l))

18

slide-19
SLIDE 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

slide-20
SLIDE 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

slide-21
SLIDE 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

slide-22
SLIDE 22

EXAMPLES OF GENERIC ALGORITHM

22

slide-23
SLIDE 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

slide-24
SLIDE 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

slide-25
SLIDE 25

OVERHEAD AND SPECIALISATION

25

slide-26
SLIDE 26

Overhead comparison

  • Generic version of [42]

Con ("Cons" ** (TS TZ ** (0 ** (42 ** (Con ("Nil” ** (TZ ** Refl)) ** Refl)))))

26

slide-27
SLIDE 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

slide-28
SLIDE 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

slide-29
SLIDE 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

slide-30
SLIDE 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

slide-31
SLIDE 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

slide-32
SLIDE 32

Advantages

  • Exploits the added type information in Idris and
  • ther dependently-typed languages
  • Is not specialised to a particular representation
  • Does not require a mapping to existing

datatypes

  • Works independently of Constructor

Specialisation

32

slide-33
SLIDE 33

DISCUSSION

33

slide-34
SLIDE 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

slide-35
SLIDE 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

slide-36
SLIDE 36

QUESTIONS

36

slide-37
SLIDE 37

OTHER SLIDES

37

slide-38
SLIDE 38

GENERIC TRAVERSAL AND LIMITATIONS

38

slide-39
SLIDE 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

slide-40
SLIDE 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