https youtube com watch v wtp2rud cl0 theorems for free
play

https://www.youtube.com/watch?v=wTP2RUD_cL0 Theorems for Free Blame - PowerPoint PPT Presentation

https://www.youtube.com/watch?v=wTP2RUD_cL0 Theorems for Free Blame for All Philip Wadler University of Edinburgh QCon S ao Paulo 25 April 2017 Part I Theorems for Free A magic trick r :: a . [ a ] [ a ] :: a b . ( a b


  1. https://www.youtube.com/watch?v=wTP2RUD_cL0

  2. Theorems for Free Blame for All Philip Wadler University of Edinburgh QCon S˜ ao Paulo 25 April 2017

  3. Part I Theorems for Free

  4. A magic trick

  5. r :: ∀ a . [ a ] → [ a ]

  6. :: ∀ a b . ( a → b ) → ([ a ] → [ b ]) map :: A → B f :: [ A ] → [ B ] map f

  7. r [ A ] [ A ] A f map f map f [ B ] [ B ] B r

  8. reverse :: ∀ a . [ a ] → [ a ]

  9. :: ∀ a b . ( a → b ) → ([ a ] → [ b ]) map :: Int → Char chr :: [ Int ] → [ Char ] map f

  10. reverse [ Int ] [ Int ] Int chr map chr map chr [ Char ] [ Char ] Char reverse

  11. [ 97 , 98 , 99 ] [ 99 , 98 , 97 ] reverse [ Int ] [ Int ] Int chr map chr map chr [ Char ] [ Char ] Char reverse [ ‘ a ’ , ‘ b ’ , ‘ c ’ ] [ ‘ c ’ , ‘ b ’ , ‘ a ’ ]

  12. :: ∀ a . Int → ([ a ] → [ a ]) drop :: ∀ a . [ a ] → [ a ] drop 1

  13. drop 1 Int [ Int ] [ Int ] inc map inc map inc Int [ Int ] [ Int ] drop 1

  14. [ 1 , 2 , 3 ] [ 2 , 3 ] drop 1 Int [ Int ] [ Int ] inc map inc map inc Int [ Int ] [ Int ] drop 1 [ 2 , 3 , 4 ] [ 3 , 4 ]

  15. :: ∀ a . ( a → Bool ) → ([ a ] → [ a ]) filter :: Int → Bool odd :: [ Int ] → [ Int ] filter odd

  16. filter odd Int [ Int ] [ Int ] inc map inc map inc Int [ Int ] [ Int ] filter odd

  17. [ 1 , 2 , 3 ] [ 1 , 3 ] filter odd Int [ Int ] [ Int ] inc map inc map inc Int [ Int ] [ Int ] filter odd [ 2 , 3 , 4 ] [ 3 ] � = [ 2 , 4 ]

  18. filter :: ∀ a . ( a → Bool ) → ([ a ] → [ a ])

  19. p filter p A Bool [ A ] [ A ] f id map f map f B Bool [ B ] [ B ] q filter q

  20. filter odd odd [ Int ] [ Int ] Int Bool inc id map inc map inc [ Int ] [ Int ] Int Bool even filter even

  21. [ 1 , 2 , 3 ] [ 1 , 3 ] 1 True filter odd odd [ Int ] [ Int ] Int Bool inc id map inc map inc [ Int ] [ Int ] Int Bool even filter even 2 True [ 2 , 3 , 4 ] [ 2 , 4 ]

  22. Part II And now a word from our sponsor

  23. Lambda Calculus L , M , N ::= x | λ x . N | L M Polymorphic Lambda Calculus A , B , C ::= X | A → B | ∀ X . B L , M , N ::= x | λ x : A . N | L M | Λ X . N | L A

  24. Part III Blame for All

  25. A simple typed program inc :: Int → Int inc x = x + 1 twice :: ∀ a . ( a → a ) → ( a → a ) twice f x = f ( f x ) main :: Int main = twice inc 40 �− → 42 : Int

  26. A simple untyped program inc ⋆ :: ⋆ inc ⋆ x = x + 1 twice ⋆ :: ⋆ twice ⋆ f x = f ( f x ) main ⋆ :: ⋆ main ⋆ = twice ⋆ inc ⋆ 40 �− → 42 : ⋆

  27. A simple untyped program inc ⋆ :: ⋆ inc ⋆ x = x + 1 twice ⋆ :: ⋆ twice ⋆ f x = f ( f x ) main ⋆ :: ⋆ main ⋆ = twice ⋆ inc ⋆ 40 �− → 42 : ⋆ Untyped is unityped

  28. Widening: casting from typed to untyped inc ⋆ :: ⋆ inc ⋆ x = x + 1 twice :: ∀ a . ( a → a ) → ( a → a ) twice f x = f ( f x ) twice ⋆ :: ⋆ twice ⋆ = � twice � p main :: ⋆ main = twice ⋆ inc ⋆ 40 �− → 42 : ⋆

  29. Widening: casting from typed to untyped inc ⋆ :: ⋆ inc ⋆ x = x + 1 twice :: ∀ a . ( a → a ) → ( a → a ) twice f x = f ( f x ) twice ⋆ :: ⋆ twice = � twice � p main :: ⋆ main = twice ⋆ 40 inc ⋆ �− → blame p A cast from more-precise to less-precise type may blame the context containing the cast, but never the term contained in the cast.

  30. Narrowing: casting from untyped to typed inc :: Int → Int inc x = x + 1 twice ⋆ :: ⋆ twice ⋆ f x = f ( f x ) twice :: ∀ a . ( a → a ) → ( a → a ) twice = � twice ⋆ � p main :: Int main = twice inc 40 �− → 42 : Int

  31. Narrowing: casting from untyped to typed inc :: Int → Int inc x = x + 1 twice ⋆ :: ⋆ twice ⋆ f x = 42 twice :: ∀ a . ( a → a ) → ( a → a ) twice = � twice ⋆ � p main :: Int main = twice inc 40 �− → blame p A cast from less-precise to more-precise type may blame the term contained in the cast, but never the context containing the cast.

  32. Polymorphic Blame Calculus

  33. Part IV Further Reading

  34. Theorems for Free Wadler; ICFP, 1989 Blame for All Ahmed, Findler, Siek, Wadler; POPL, 2011 Theorems for Free for Free Ahmed, Jamner, Siek, Wadler; ICFP, 2017 Mixed Messages Williams, Morris, Wadler, Zalewski; ECOOP, 2017 Propositions as Types Wadler; CACM, December 2015

Recommend


More recommend