last time generic programming val show a data a string
play

Last time: generic programming val show : a data a string 1/ 65 - PowerPoint PPT Presentation

Last time: generic programming val show : a data a string 1/ 65 This time: staging . < e > . 2/ 65 Review: abstraction Row polymorphism Lambda abstraction x : A . M [ > `A | `B] A :: K . M A :: K . B


  1. Last time: generic programming val show : ’a data → ’a → string 1/ 65

  2. This time: staging . < e > . 2/ 65

  3. Review: abstraction Row polymorphism Lambda abstraction λ x : A . M [ > `A | `B] Λ A :: K . M λ A :: K . B Abstraction of type equalities Modular abstraction a ≡ b module F(X : T) = . . . Interfaces to computation First-class ∀ and ∃ m > > = k f ⊗ p type t = { f : ’ a . . . . } type t = E : ’ a s → t Abstraction over data shape v a l show : ’ a data → ’ a → s t r i n g 3/ 65

  4. The cost of ignorance Fewer opportunities for optimization l e t both eq1 : i n t ∗ i n t → i n t ∗ i n t → bool = fun ( x1 , y1 ) ( x2 , y2 ) → x1 = x2 && y1 = y2 l e t both eq2 : ( i n t → i n t → bool ) → i n t ∗ i n t → i n t ∗ i n t → bool = fun eq ( x1 , y1 ) ( x2 , y2 ) → eq x1 x2 && eq y1 y2 both eq2 ( fun x y → x = y ) type eq = { eq : ’ a . ’ a → ’ a → bool } l e t both eq { eq } ( x1 , y1 ) ( x2 , y2 ) = eq x1 x2 && eq y1 y2 4/ 65

  5. The cost of ignorance Interpretative overhead l e t p r i n t i n t p a i r ( x , y ) = p r i n t c h a r ’ ( ’ ; p r i n t i n t x ; p r i n t c h a r ’ , ’ ; p r i n t i n t y ; p r i n t c h a r ’) ’ l e t p r i n t i n t p a i r 2 ( x , y ) = P r i n t f . s p r i n t f ”(%d,%d)” x y l e t p r i n t i n t p a i r 3 ( x , y ) = p r i n t s t r i n g ( gshow ( p a i r i n t i n t ) ( x , y )) 5/ 65

  6. Abstraction wants to be free ( ∗ x 2 ∗ ) l e t pow2 x = x ∗ x ( ∗ x 3 ∗ ) l e t pow3 x = x ∗ x ∗ x pow5 x = x ∗ x ∗ x ∗ x ∗ x ( ∗ x 5 ∗ ) l e t ( ∗ x n ∗ ) l e t rec pow x n = i f n = 0 then 1 e l s e x ∗ pow x (n − 1) 6/ 65

  7. Power, staged l e t rec pow x n = i f n = 0 then . < 1 > . e l s e . < .˜x ∗ .˜(pow x (n − 1)) > . 7/ 65

  8. Power, staged l e t rec pow x n = i f n = 0 then . < 1 > . e l s e . < .˜x ∗ .˜(pow x (n − 1)) > . v a l pow : i n t code → i n t → i n t code 8/ 65

  9. Power, staged l e t rec pow x n = i f n = 0 then . < 1 > . e l s e . < .˜x ∗ .˜(pow x (n − 1)) > . v a l pow : i n t code → i n t → i n t code l e t pow code n = . < fun x → .˜(pow . < x > . n) > . 9/ 65

  10. Power, staged l e t rec pow x n = i f n = 0 then . < 1 > . e l s e . < .˜x ∗ .˜(pow x (n − 1)) > . v a l pow : i n t code → i n t → i n t code l e t pow code n = . < fun x → .˜(pow . < x > . n) > . v a l pow code : i n t → ( i n t → i n t ) code 10/ 65

  11. Power, staged l e t rec pow x n = i f n = 0 then . < 1 > . e l s e . < .˜x ∗ .˜(pow x (n − 1)) > . v a l pow : i n t code → i n t → i n t code l e t pow code n = . < fun x → .˜(pow . < x > . n) > . v a l pow code : i n t → ( i n t → i n t ) code # pow code 3 ; ; . < fun x → x ∗ x ∗ x ∗ 1 > . 11/ 65

  12. Power, staged l e t rec pow x n = i f n = 0 then . < 1 > . e l s e . < .˜x ∗ .˜(pow x (n − 1)) > . v a l pow : i n t code → i n t → i n t code l e t pow code n = . < fun x → .˜(pow . < x > . n) > . v a l pow code : i n t → ( i n t → i n t ) code # pow code 3 ; ; . < fun x → x ∗ x ∗ x ∗ 1 > . # l e t pow3 ’ = ! . ( pow code 3 ) ; ; v a l pow3 ’ : i n t → i n t = < fun > 12/ 65

  13. Power, staged l e t rec pow x n = i f n = 0 then . < 1 > . e l s e . < .˜x ∗ .˜(pow x (n − 1)) > . v a l pow : i n t code → i n t → i n t code l e t pow code n = . < fun x → .˜(pow . < x > . n) > . v a l pow code : i n t → ( i n t → i n t ) code # pow code 3 ; ; . < fun x → x ∗ x ∗ x ∗ 1 > . # l e t pow3 ’ = ! . ( pow code 3 ) ; ; v a l pow3 ’ : i n t → i n t = < fun > # pow3 ’ 4 ; ; − : i n t = 64 13/ 65

  14. MetaOCaml basics 14/ 65

  15. Quoting l e t x = ”w” in l e t x = ”w” in l e t y = ”x” in l e t y = x in p r i n t s t r i n g ( x ˆ y ) p r i n t s t r i n g (” x ˆ y ”) l e t x = ”w” in l e t x = ”w” in l e t y = x in l e t y = x in p r i n t s t r i n g ( x ˆ y ) p r i n t s t r i n g (” x” ˆ y ) Quoting prevents evaluation . 15/ 65

  16. Quoting code MetaOCaml : multi-stage programming with code quoting. Stages : current (available now) and delayed (available later). (Also double-delayed, triple-delayed, etc.) Brackets Running code . < e > . ! . e Escaping (within brackets) Cross-stage persistence .˜e . < x > . 16/ 65

  17. Quoting and escaping: some examples . < 3 > . . < 1 + 2 > . . < [1; 2; 3] > . . < x + y > . . < fun x → x > . . < (.˜f) 3 > . . < .˜(f 3) > . . < fun x → .˜(f . < x > .) > . 17/ 65

  18. Quoting: typing Γ ⊢ n e : τ Γ + ⊢ n e : τ code T-run Γ ⊢ n + e : τ T-bracket Γ ⊢ n !. e : τ Γ ⊢ n . < e > . : τ code Γ ⊢ n e : τ code T-escape Γ( x ) = τ ( n − m ) Γ ⊢ n + .˜e : τ T-var Γ ⊢ n : τ 18/ 65

  19. Quoting: open code Open code l e t pow code n = . < fun x → .˜(pow . < x > . n) > . Cross-stage persistence l e t p r i n t i n t p a i r ( x , y ) = P r i n t f . p r i n t f ”(%d,%d)” x y l e t p a i r s = . < [ ( 3 , 4 ) ; (5 , 6 ) ] > . . < L i s t . i t e r p r i n t i n t p a i r .˜p a i r s > . 19/ 65

  20. Quoting: scoping Scoping is lexical , just as in OCaml. . < fun x → .˜( l e t x = 3 in . < x > . ) > . l e t x = 3 in . < fun x → .˜( . < x > .) > . MetaOCaml renames variables to avoid clashes: . < l e t x = 3 in .˜( l e t y = . < x > . in . < fun x → .˜y + x > . ) > . 20/ 65

  21. Quoting: scoping Scoping is lexical , just as in OCaml. . < fun x → .˜( l e t x = 3 in . < x > . ) > . l e t x = 3 in . < fun x → .˜( . < x > .) > . MetaOCaml renames variables to avoid clashes: # . < l e t x = 3 in .˜( l e t y = . < x > . in . < fun x → .˜y + x > . ) > .; ; − : ( i n t → i n t ) code = . < l e t x 1 = 3 in fun x 2 → x 1 + x 2 > . 21/ 65

  22. Learning from mistakes 22/ 65

  23. Error: quoting nonsense . < 1 + ”two” > . 23/ 65

  24. Error: quoting nonsense # . < 1 + ”two” > .;; Characters 7 − 12: . < 1 + ”two” > .;; ˆˆˆˆˆ Error : This e x p r e s s i o n has type s t r i n g but an e x p r e s s i o n was expected of type i n t 24/ 65

  25. Error: looking into the future . < fun x → .˜( x ) > . 25/ 65

  26. Error: looking into the future # . < fun x → .˜( x ) > .; ; Characters 14 − 19: . < fun x → .˜( x ) > .; ; ˆˆˆˆˆ Wrong l e v e l : v a r i a b l e bound at l e v e l 1 and used at l e v e l 0 26/ 65

  27. Error: escape from nowhere l e t x = . < 3 > . in .˜x 27/ 65

  28. Error: escape from nowhere # l e t x = . < 3 > . in .˜ x ; ; Characters 22 − 23: l e t x = . < 3 > . in .˜ x ; ; ˆ Wrong l e v e l : escape at l e v e l 0 28/ 65

  29. Error: running open code . < fun x → .˜( ! . . < x > . ) > . 29/ 65

  30. Error: running open code # . < fun x → . ˜ ( ! . . < x > . ) > .;; Exception : F a i l u r e ”The code b u i l t at Characters 7 − 8: \ n . < fun x → . ˜ ( ! . . < x > . ) > .;; \ n ˆ \ n i s not c l o s e d : i d e n t i f i e r x 2 bound at Characters 7 − 8: \ n . < fun x → . ˜ ( ! . . < x > . ) > .;; \ n ˆ \ n i s f r e e ”. 30/ 65

  31. Learning by doing 31/ 65

  32. Power again Reducing the number of multiplications: x 0 = 1 x 2 n +2 = ( x n +1 ) 2 x 2 n +1 = x ( x 2 n ) l e t even x = x mod 2 = 0 l e t sqr x = x ∗ x l e t rec pow x n = i f n = 0 then 1 e l s e i f even n then sqr (pow x (n / 2)) e l s e x ∗ pow (n − 1) x 32/ 65

  33. Power again, staged Reducing the number of multiplications: x 0 = 1 x 2 n +2 = ( x n +1 ) 2 x 2 n +1 = x ( x 2 n ) l e t even x = x mod 2 = 0 l e t sqr x = . < l e t y = .˜x in y ∗ y > . l e t rec pow ’ x n = i f n = 0 then . < 1 > . e l s e i f even n then sqr (pow ’ x (n / 2)) e l s e . < .˜x ∗ .˜(pow ’ x (n − 1)) > . 33/ 65

  34. Power again, staged l e t rec pow ’ x n = i f n = 0 then . < 1 > . e l s e i f even n then sqr (pow ’ x (n / 2)) e l s e . < .˜x ∗ .˜(pow ’ x (n − 1)) > . 34/ 65

  35. Power again, staged l e t rec pow ’ x n = i f n = 0 then . < 1 > . e l s e i f even n then sqr (pow ’ x (n / 2)) e l s e . < .˜x ∗ .˜(pow ’ x (n − 1)) > . v a l pow ’ : i n t code → i n t → i n t code 35/ 65

  36. Power again, staged l e t rec pow ’ x n = i f n = 0 then . < 1 > . e l s e i f even n then sqr (pow ’ x (n / 2)) e l s e . < .˜x ∗ .˜(pow ’ x (n − 1)) > . v a l pow ’ : i n t code → i n t → i n t code l e t pow code ’ n = . < fun x → .˜(pow ’ . < x > . n) > . 36/ 65

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