SLIDE 1
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 - - 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
SLIDE 2
SLIDE 3
Part I
Theorems for Free
SLIDE 4
A magic trick
SLIDE 5
r :: ∀a. [a] → [a]
SLIDE 6
map :: ∀a b. (a → b) → ([a] → [b]) f :: A → B map f :: [A] → [B]
SLIDE 7
A [A] [A] B [B] [B] f r map f map f r
SLIDE 8
reverse :: ∀a. [a] → [a]
SLIDE 9
map :: ∀a b. (a → b) → ([a] → [b]) chr :: Int → Char map f :: [Int] → [Char]
SLIDE 10
Int [Int] [Int] Char [Char] [Char] chr reverse map chr map chr reverse
SLIDE 11
[97, 98, 99] [99, 98, 97] Int [Int] [Int] Char [Char] [Char] [‘a’, ‘b’, ‘c’] [‘c’, ‘b’, ‘a’] chr reverse map chr map chr reverse
SLIDE 12
drop :: ∀a. Int → ([a] → [a]) drop 1 :: ∀a. [a] → [a]
SLIDE 13
Int [Int] [Int] Int [Int] [Int] inc drop 1 map inc map inc drop 1
SLIDE 14
[1, 2, 3] [2, 3] Int [Int] [Int] Int [Int] [Int] [2, 3, 4] [3, 4] inc drop 1 map inc map inc drop 1
SLIDE 15
filter :: ∀a. (a → Bool) → ([a] → [a])
- dd
:: Int → Bool filter odd :: [Int] → [Int]
SLIDE 16
Int [Int] [Int] Int [Int] [Int] inc filter odd map inc map inc filter odd
SLIDE 17
[1, 2, 3] [1, 3] Int [Int] [Int] Int [Int] [Int] [2, 3, 4] [3] = [2, 4] inc filter odd map inc map inc filter odd
SLIDE 18
filter :: ∀a. (a → Bool) → ([a] → [a])
SLIDE 19
A Bool [A] [A] B Bool [B] [B] p f id filter p map f map f q filter q
SLIDE 20
Int Bool [Int] [Int] Int Bool [Int] [Int]
- dd
inc id filter odd map inc map inc even filter even
SLIDE 21
1 True [1, 2, 3] [1, 3] Int Bool [Int] [Int] Int Bool [Int] [Int] 2 True [2, 3, 4] [2, 4]
- dd
inc id filter odd map inc map inc even filter even
SLIDE 22
SLIDE 23
SLIDE 24
Part II
And now a word from our sponsor
SLIDE 25
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
SLIDE 26
Part III
Blame for All
SLIDE 27
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
SLIDE 28
A simple untyped program
inc⋆ :: ⋆ inc⋆ x = x + 1 twice⋆ :: ⋆ twice⋆ f x = f (f x) main⋆ :: ⋆ main⋆ = twice⋆ inc⋆ 40 − → 42 : ⋆
SLIDE 29
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
SLIDE 30
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⋆ = twicep main :: ⋆ main = twice⋆ inc⋆ 40 − → 42 : ⋆
SLIDE 31
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 = twicep 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.
SLIDE 32
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
SLIDE 33
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.
SLIDE 34
Polymorphic Blame Calculus
SLIDE 35
SLIDE 36
SLIDE 37
SLIDE 38
SLIDE 39
Part IV
Further Reading
SLIDE 40