last time phantom types
play

Last time: phantom types type a t = int 1/ 49 This time: GADTs a - PowerPoint PPT Presentation

Last time: phantom types type a t = int 1/ 49 This time: GADTs a b 2/ 49 What we gain : : (Addtionally, some programs become faster!) 3/ 49 What we gain : : (Addtionally, some programs


  1. Last time: phantom types type ’a t = int 1/ 49

  2. This time: GADTs a ≡ b 2/ 49

  3. What we gain Γ ⊢ : ⇓ Γ ⊢ : ⇓ (Addtionally, some programs become faster!) 3/ 49

  4. What we gain Γ ⊢ : ⇓ Γ ⊢ : ⇓ (Addtionally, some programs become faster!) 3/ 49

  5. What it costs We’ll need to: describe our data more precisely strengthen the relationship between data and types look at programs through a propositions-as-types lens 4/ 49

  6. What we’ll write Non-regularity in constructor return types type t = T : t 1 → t 2 t Locally abstract types : l e t f : type a b . a t → b t = f u n c t i o n . . . l e t g ( type a ) ( type b ) ( x : a t ) : b t = . . . 5/ 49

  7. Nested types review 6/ 49

  8. Unconstrained trees T T c E T b E E a E type ’ a t r e e = Empty : ’ a t r e e | Tree : ’ a t r e e ∗ ’ a ∗ ’ a t r e e → ’ a t r e e 7/ 49

  9. Functions on unconstrained trees v a l ? : ’ a t r e e → i n t v a l ? : ’ a t r e e → ’ a option v a l ? : ’ a t r e e → ’ a t r e e 8/ 49

  10. Unconstrained trees: depth T 1 + max (1 + max T c E (1 + max 0 T b E 0) 0) E a E 0 l e t rec depth : ’ a . ’ a t r e e → i n t = f u n c t i o n Empty → 0 | Tree ( l , , r ) → 1 + max ( depth l ) ( depth r ) 9/ 49

  11. Unconstrained trees: top T Some c T c E T b E E a E l e t top : ’ a . ’ a t r e e → ’ a option = f u n c t i o n Empty → None | Tree ( , v , ) → Some v 10/ 49

  12. Unconstrained trees: swivel T T T c E E c T T b E E b T E a E E a E l e t rec s w i v e l : ’ a . ’ a t r e e → ’ a t r e e = f u n c t i o n Empty → Empty | Tree ( l , v , r ) → Tree ( s w i v e l r , v , s w i v e l l ) 11/ 49

  13. Perfect leaf trees via nesting S S S Z e f g h a b c d type ’ a p e r f e c t = ZeroP : ’ a → ’ a p e r f e c t | SuccP : ( ’ a ∗ ’ a ) p e r f e c t → ’ a p e r f e c t 12/ 49

  14. Perfect (branch) trees via nesting T a T (b, c) T ((d, e), (f, g)) E type ntree = EmptyN : ’ a ntree | TreeN : ’ a ∗ ( ’ a ∗ ’ a ) ntree → ’ a ntree 13/ 49

  15. Functions on perfect nested trees v a l ? : ’ a ntree → i n t v a l ? : ’ a ntree → ’ a option v a l ? : ’ a ntree → ’ a ntree 14/ 49

  16. Perfect trees: depth T a 1 + 1 + T (b, c) 1 + 0 T ((d, e), (f, g)) E l e t rec depthN : ’ a . ’ a ntree → i n t = f u n c t i o n EmptyN → 0 | TreeN ( , t ) → 1 + depthN t 15/ 49

  17. Perfect trees: top T a Some a T (b, c) T ((d, e), (f, g)) E l e t rec topN : ’ a . ’ a ntree → ’ a option = f u n c t i o n EmptyN → None | TreeN ( v , ) → Some v 16/ 49

  18. Perfect trees: swivel T T a a T T (b, c) (c, b) T T ((d, e), (f, g)) ((g, f), (e, d)) E E l e t rec swiv : ’ a . ( ’ a → ’ a ) → ’ a ntree → ’ a ntree = fun f t → match t with EmptyN → EmptyN | TreeN ( v , t ) → TreeN ( f v , swiv ( fun ( x , y ) → ( f y , f x )) t ) l e t swivelN p = swiv id p 17/ 49

  19. GADTs 18/ 49

  20. Perfect trees, take two T[3] T[2] a T[2] T[1] b T[1] T[1] c T[1] E d E E e E E f E E g E type ( ’ a , ) g t r e e = EmptyG : ( ’ a , z ) g t r e e | TreeG : ( ’ a , ’ n) g t r e e ∗ ’ a ∗ ( ’ a , ’ n) g t r e e → ( ’ a , ’ n s ) g t r e e 19/ 49

  21. Natural numbers type z = Z type s = S : ’n → ’n s # l e t zero = Z ; ; v a l zero : z = Z # l e t three = S (S (S Z ) ) ; ; v a l three : z s s s = S (S (S Z)) 20/ 49

  22. Functions on perfect trees (GADTs) v a l ? : ( ’ a , ’ n) g t r e e → ’n v a l ? : ( ’ a , ’ n s ) g t r e e → ’ a v a l ? : ( ’ a , ’ n) g t r e e → ( ’ a , ’ n) g t r e e 21/ 49

  23. Perfect trees (GADTs): depth T[2] S ( depth T[1] b T[1] S ( depth Z)) E d E E e E l e t rec depthG : type a n . ( a , n) g t r e e → n = f u n c t i o n EmptyG → Z | TreeG ( l , , ) → S ( depthG l ) 22/ 49

  24. Perfect trees (GADTs): depth type ( ’ a , ) g t r e e = EmptyG : ( ’ a , z ) g t r e e | TreeG : ( ’ a , ’ n) g t r e e ∗ ’ a ∗ ( ’ a , ’ n) g t r e e → ( ’ a , ’ n s ) g t r e e l e t rec depthG : type a n . ( a , n) g t r e e → n = f u n c t i o n EmptyG → Z | TreeG ( l , , ) → S ( depthG l ) Type refinement In the EmptyG branch: n ≡ z In the TreeG branch: n ≡ m s (for some m) l : (a, m) gtree depthG l : m Polymorphic recursion The argument to the recursive call has size m (s.t. s m ≡ n) 23/ 49

  25. Perfect trees (GADTs): top T[2] a T[1] a T[1] E b E E c E l e t topG : type a n . ( a , n s ) g t r e e → a = f u n c t i o n TreeG ( , v , ) → v 24/ 49

  26. Perfect trees (GADTs): depth type ( ’ a , ) g t r e e = EmptyG : ( ’ a , z ) g t r e e | TreeG : ( ’ a , ’ n) g t r e e ∗ ’ a ∗ ( ’ a , ’ n) g t r e e → ( ’ a , ’ n s ) g t r e e l e t topG : type a n . ( a , n s ) g t r e e → a = f u n c t i o n TreeG ( , v , ) → v Type refinement In an EmptyG branch we would have: n s ≡ z — impossible! 25/ 49

  27. Perfect trees (GADTs): swivel T[2] T[2] T[1] a T[1] T[1] a T[1] E c E E c E E b E E b E l e t rec swivelG : type a n . ( a , n) g t r e e → (a , n) g t r e e = f u n c t i o n EmptyG → EmptyG | TreeG ( l , v , r ) → TreeG ( swivelG r , v , swivelG l ) 26/ 49

  28. Perfect trees (GADTs): swivel type ( ’ a , ) g t r e e = EmptyG : ( ’ a , z ) g t r e e | TreeG : ( ’ a , ’ n) g t r e e ∗ ’ a ∗ ( ’ a , ’ n) g t r e e → ( ’ a , ’ n s ) g t r e e l e t rec swivelG : type a n . ( a , n) g t r e e → (a , n) g t r e e = f u n c t i o n EmptyG → EmptyG | TreeG ( l , v , r ) → TreeG ( swivelG r , v , swivelG l ) Type refinement In the EmptyG branch: n ≡ z In the TreeG branch: n ≡ m s (for some m) l , r : (a, m) gtree swivelG l : (a, m) gtree Polymorphic recursion The argument to the recursive call has size m (s.t. s m ≡ n) 27/ 49

  29. Zipping perfect trees T[2] T[2] T[1] a T[1] T[1] (a,d) T[1] E b E E c E T[2] E (b,e) E E (c,f) E T[1] d T[1] E f E E e E l e t rec zipTree : type a n . ( a , n) g t r e e → (a , n) g t r e e → ( a ∗ a , n) g t r e e = fun x y → match x , y with EmptyG , EmptyG → EmptyG | TreeG ( l , v , r ) , TreeG (m,w, s ) → TreeG ( zipTree l m, ( v ,w) , zipTree r s ) 28/ 49

  30. Zipping perfect trees type ( ’ a , ) g t r e e = EmptyG : ( ’ a , z ) g t r e e | TreeG : ( ’ a , ’ n) g t r e e ∗ ’ a ∗ ( ’ a , ’ n) g t r e e → ( ’ a , ’ n s ) g t r e e l e t rec zipTree : type a n . ( a , n) g t r e e → (a , n) g t r e e → ( a ∗ a , n) g t r e e = fun x y → match x , y with EmptyG , EmptyG → EmptyG | TreeG ( l , v , r ) , TreeG (m,w, s ) → TreeG ( zipTree l m, ( v ,w) , zipTree r s ) Type refinement In the EmptyG branch: n ≡ z In the TreeG branch: n ≡ m s (for some m) EmptyG, TreeG produces n ≡ z and n ≡ m s — impossible! 29/ 49

  31. Conversions between perfect tree representations T a T[2] T T[1] a T[1] (b, c) E b E E c E E l e t rec n e s t i f y : type a n . ( a , n) g t r e e → a ntree = f u n c t i o n EmptyG → EmptyN | TreeG ( l , v , r ) → TreeN ( v , n e s t i f y ( zipTree l r )) 30/ 49

  32. Depth-annotated trees T[2] max(1,0) ≡ 1 T[1] b E max(0,0) ≡ 0 E a E type ( ’ a , ) d t r e e = EmptyD : ( ’ a , z ) d t r e e | TreeD : ( ’ a , ’m) d t r e e ∗ ’ a ∗ ( ’ a , ’ n ) d t r e e ∗ ( ’m, ’ n , ’ o ) max → ( ’ a , ’ o s ) d t r e e 31/ 49

  33. The untyped maximum function val max : ’a → ’a → ’a Parametricity: max is one of fun x → x fun y → y 32/ 49

  34. A typed maximum function val max : (’a ,’ b,’c) max → ’a → ’b → ’c (max (a,b) ≡ c) → a → b → c 33/ 49

  35. A typed maximum function: equality type ( , ) e q l = R e f l : ( ’ a , ’ a ) e q l a ≡ a 34/ 49

  36. A typed maximum function: a max predicate type ( , ) e q l = R e f l : ( ’ a , ’ a ) e q l type ( , , ) max = MaxEq : ( ’ a , ’ b) e q l → ( ’ a , ’ b , ’ a ) max | MaxFlip : ( ’ a , ’ b , ’ c ) max → ( ’ b , ’ a , ’ c ) max | MaxSuc : ( ’ a , ’ b , ’ a ) max → ( ’ a s , ’ b , ’ a s ) max a ≡ b → max(a,b) ≡ a max(a,b) ≡ c → max(b,a) ≡ c max(a,b) ≡ a → max(a+1,b) ≡ a+1 35/ 49

  37. A typed maximum function type ( , ) e q l = R e f l : ( ’ a , ’ a ) e q l type ( , , ) max = MaxEq : ( ’ a , ’ b) e q l → ( ’ a , ’ b , ’ a ) max | MaxFlip : ( ’ a , ’ b , ’ c ) max → ( ’ b , ’ a , ’ c ) max | MaxSuc : ( ’ a , ’ b , ’ a ) max → ( ’ a s , ’ b , ’ a s ) max l e t rec max : type a b c . ( a , b , c ) max → a → b → c = fun mx m n → match mx,m with MaxEq R e f l , → m | MaxFlip mx’ , → max mx’ n m | MaxSuc mx’ , S m’ → S (max mx’ m’ n) 36/ 49

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