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 ) → ([ a ] → [ b ]) map :: A → B f :: [ A ] → [ B ] map f
r [ A ] [ A ] A f map f map f [ B ] [ B ] B r
reverse :: ∀ a . [ a ] → [ a ]
:: ∀ a b . ( a → b ) → ([ a ] → [ b ]) map :: Int → Char chr :: [ Int ] → [ Char ] map f
reverse [ Int ] [ Int ] Int chr map chr map chr [ Char ] [ Char ] Char reverse
[ 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 ’ ]
:: ∀ a . Int → ([ a ] → [ a ]) drop :: ∀ a . [ a ] → [ a ] drop 1
drop 1 Int [ Int ] [ Int ] inc map inc map inc Int [ Int ] [ Int ] drop 1
[ 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 ]
:: ∀ a . ( a → Bool ) → ([ a ] → [ a ]) filter :: Int → Bool odd :: [ Int ] → [ Int ] filter odd
filter odd Int [ Int ] [ Int ] inc map inc map inc Int [ Int ] [ Int ] filter odd
[ 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 ]
filter :: ∀ a . ( a → Bool ) → ([ a ] → [ a ])
p filter p A Bool [ A ] [ A ] f id map f map f B Bool [ B ] [ B ] q filter q
filter odd odd [ Int ] [ Int ] Int Bool inc id map inc map inc [ Int ] [ Int ] Int Bool even filter even
[ 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 ]
Part II And now a word from our sponsor
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
Part III Blame for All
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
A simple untyped program inc ⋆ :: ⋆ inc ⋆ x = x + 1 twice ⋆ :: ⋆ twice ⋆ f x = f ( f x ) main ⋆ :: ⋆ main ⋆ = twice ⋆ inc ⋆ 40 �− → 42 : ⋆
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
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 : ⋆
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.
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
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.
Polymorphic Blame Calculus
Part IV Further Reading
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