types, types, types
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

types, types, types

  • 394 views
Uploaded on

In a weird parallel world where programming is based on type theory, every term has a type, if you declare it or not, every function has a type, and polymorphic functions can't know the type of......

In a weird parallel world where programming is based on type theory, every term has a type, if you declare it or not, every function has a type, and polymorphic functions can't know the type of their argument or see what's inside.

Even if you don't use languages from that world, they can still be an inspiration.

More in: Self Improvement
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
394
On Slideshare
385
From Embeds
9
Number of Embeds
2

Actions

Shares
Downloads
3
Comments
0
Likes
1

Embeds 9

https://twitter.com 5
https://www.linkedin.com 4

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. types, types, types
  • 2. types, types, types In a weird parallel world where programming is based on type theory, every term has a type, if you declare it or not, every function has a type, and polymorphic functions can't know the type of their argument or see what's inside. Even if you don't use languages from that world, they can still be an inspiration.
  • 3. goals
  • 4. dissatisfied excited common language
  • 5. outline warm-up. just ask the type inferencer main A. type checking is lie detection main B. what is type theory anyway? examples. bonus. kinds
  • 6. just ask the type inferencer
  • 7. just ask the type inferencer ghci> :type 3 > 0 Hey type inferencer, I was just wondering: 4 What's the type of 3 > 0?
  • 8. just ask the type inferencer ghci> :type 3 > 0 3 > 0 :: Bool The term 3 > 0 has the type Bool.
  • 9. just ask the type inferencer ghci> :type not
  • 10. just ask the type inferencer ghci> :type not not :: Bool -> Bool "not" is a function that takes a Bool to a Bool.
  • 11. just ask the type inferencer ghci> let f (x, y) = True ghci> :type f
  • 12. just ask the type inferencer ghci> let f (x, y) = True ghci> :type f f :: (t, t1) -> Bool f is a function that takes a pair of any two types to a Bool. someVar = f (True, "abc") -- True
  • 13. just ask the type inferencer ghci> let f x y = x == y ghci> :type f
  • 14. just ask the type inferencer ghci> let f x y = x == y ghci> :type f f :: Eq a => a -> a -> Bool f is a function that takes two of the same type "a" to a Bool. The type "a" must support equality.
  • 15. just ask the type inferencer ghci> :info Eq class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool ("a" is a placeholder for a concrete type here.)
  • 16. just ask the type inferencer ghci> let f x = x * 2 ghci> :type f
  • 17. just ask the type inferencer ghci> let f x = x * 2 ghci> :type f f :: Num a => a -> a f is a function that takes a value of any type "a" to a value of the same type "a". There must be an instance of Num for the type "a".
  • 18. just ask the type inferencer ghci> :info Num class Num a where (+) :: a -> a -> a (*) :: a -> a -> a (-) :: a -> a -> a negate :: a -> a abs :: a -> a ...
  • 19. just ask the type inferencer ghci> let f x = x * 2 ghci> f True <interactive>:24:1: No instance for (Num Bool) arising from a use of `f' Possible fix: add an instance declaration for (Num Bool) In the expression: f True In an equation for `it': it = f True
  • 20. ok
  • 21. summary 4 if you don't know the type, ask the type inferencer 4 functions have types 4 operations constrain types
  • 22. main A
  • 23. type checking is lie detection
  • 24. type checking is lie detection intIdentity :: Int -> Int intIdentity x = x
  • 25. type checking is lie detection intIdentity :: Int -> Int intIdentity x = 42 But that's not an identity function! :(
  • 26. how can we prevent ourselves from lying?
  • 27. the function that…
  • 28. type checking is lie detection -- intIdentity :: Int -> Int identity :: a -> a identity x = ???
  • 29. type checking is lie detection -- intIdentity :: Int -> Int identity :: a -> a identity x = 42
  • 30. type checking is lie detection -- intIdentity :: Int -> Int identity :: a -> a identity x = 42 That's a lie! The function must accept an argument of any type.
  • 31. type checking is lie detection -- intIdentity :: Int -> Int identity :: a -> a identity x = 42 That's a lie! The function must accept an argument of any type. And it must return an argument of the same type.
  • 32. type checking is lie detection -- intIdentity :: Int -> Int identity :: a -> a identity x = x There is only one valid implementation * ! *that terminates and returns a value
  • 33. parametricity
  • 34. c++ templates and parametricity template <typename t> t identity(t a) { return ???; }
  • 35. c++ templates and parametricity template <typename t> t identity(t a) { return 42; }
  • 36. end of intermission
  • 37. parametricity f :: [t] -> [t] What can this function not do?
  • 38. parametricity f :: [t] -> [t] What can this function not do? 4 insert items 4 change values
  • 39. parametricity f :: [t] -> [t] So what can it do at all?
  • 40. parametricity f :: [t] -> [t] So what can it do at all? 4 rearrange list items 4 drop list items
  • 41. parametricity Here is a valid implementation: tail :: [t] -> [t] tail (x:xs) = xs -- non-empty tail _ = [] -- empty -- example usage: -- tail [1,2,3] -- [2,3] -- tail "abc" -- "bc" -- tail [] -- [] -- tail "" -- ""
  • 42. parametricity / lie detection f :: [t] -> t
  • 43. parametricity / lie detection head :: [t] -> t head (x:_) = x
  • 44. parametricity / lie detection head :: [t] -> t head (x:_) = x Warning: Pattern match(es) are non-exhaustive In an equation for `head': Patterns not matched: []
  • 45. parametricity / lie detection head :: [t] -> t head (x:_) = x head [] = ??? 4 the type [t] -> t is not inhabited 4 any function that claims to have that type is a lie
  • 46. summary 4 the less a function knows, the less it can do 4 if you can use a more abstract type, do it 4 not every type is inhabited
  • 47. main B
  • 48. what is type theory anyway?
  • 49. "type theory is the grand, unified theory of computation" — robert harper
  • 50. type theory is a formal language that describes how terms and types relate to each other
  • 51. example: pairs 4 how to combine two types into a pair type 4 how to construct values of that type 4 how to deconstruct pair values and types 4 how to simplify terms involving pairs
  • 52. type theory: formation rules A is a type <—— "assuming that…" B is a type ——————————————— (∧F) A ∧ B is a type <—— "…this is a valid thing to write"
  • 53. type theory: introduction rules A is a type B is a type ——————————————— (∧F) A ∧ B is a type x:A y:B ——————————— (∧-Introduction) (x,y) : A∧B
  • 54. type theory: elimination rules A is a type B is a type ——————————————— (∧F) A ∧ B is a type x:A y:B z : A∧B ——————————— (∧I) ——————————— (∧E1) (x,y) : A∧B first z : A
  • 55. type theory: elimination rules A is a type B is a type ——————————————— (∧F) A ∧ B is a type x:A y:B z : A∧B z : A∧B ——————————— (∧I) ——————————— (∧E1) ———————————— (∧E2) (x,y) : A∧B first z : A second z : B
  • 56. type theory: computation rules first(x,y) —> x second(x,y) —> y (The left side reduces to the right side.)
  • 57. 4 formation rules specify syntax for types 4 introduction rules specify syntax for types and terms 4 elimination rules are inverses of introduction rules (sort of) 4 computation rules describe how terms can be simplified by evaluating them
  • 58. next example: functions
  • 59. type theory: implication propositional logic ------------------- A => B assuming that A implies B A and assuming there is a proof of A —————— B there is a proof of B
  • 60. type theory: implication propositional logic type theory ------------------- ----------- A => B f : (A => B) A x : A —————— ———————————— (=> Elimination) B (f x) : B
  • 61. type theory: implication How do we construct a function?
  • 62. type theory: implication How do we construct a function? x:A . . given an object of type A, . an object of type B can be deduced . ——— y:B
  • 63. type theory: implication How do we construct a function? [x:A] [discharged assumption] . . given an object of type A, . an object of type B can be deduced . y:B ———————————————— (=> Introduction) ??? : A => B
  • 64. type theory: implication How do we construct a function? [x:A] [discharged assumption] . . given an object of type A, . an object of type B can be deduced . y:B ———————————————— (=>I) (λx. y) : A => B any function that takes an A and returns a B is a proof of A => B
  • 65. type theory: implication introduction: elimination: [x:A] f: A => B x:A . ——————————————— . (f x) : B . y:B ———————————————— computation: (λx. y) : A => B beta reduction
  • 66. a program is well-typed if it can be derived using the rules
  • 67. types as propositions programs as proofs
  • 68. "Curry–Howard correspondence"
  • 69. what does that mean for some actual programs?
  • 70. programs as proofs x :: Bool
  • 71. programs as proofs x :: Bool x = False
  • 72. programs as proofs x :: Bool x = False (empty) (empty) ——————————— ———————————— True : bool False : bool False is exactly one of the ways of how you construct a bool.
  • 73. not bad
  • 74. programs as proofs data Color = Red | Green | Blue c :: Color
  • 75. programs as proofs data Color = Red | Green | Blue c :: Color c = Blue Blue is exactly one of the ways of how you construct a Color.
  • 76. programs as proofs f :: (a -> b) -> a -> b
  • 77. programs as proofs f :: (a -> b) -> a -> b f f1 x = ???
  • 78. programs as proofs f :: (a -> b) -> a -> b f f1 x = f1 x
  • 79. programs as proofs f :: (a -> b) -> a -> b f f1 x = f1 x Why does a function of that type exist?
  • 80. programs as proofs f :: (a -> b) -> a -> b f f1 x = f1 x Why does a function of that type exist? A => B A ———————————— B
  • 81. programs as proofs f :: (a -> b) -> a -> b f f1 x = f1 x Why does a function of that type exist? A => B [A] ———————————— A => B
  • 82. programs as proofs f :: (a -> b) -> (a -> b) f f1 x = f1 x Why does a function of that type exist? [A => B] [A] ———————————————————— (sort of a silly proof) (A => B) => (A => B)
  • 83. if it's provable, you can write a program a well-typed program constitutes a proof
  • 84. programs as proofs f :: (b -> c) -> (a -> b) -> a -> c f f1 f2 x = ???
  • 85. programs as proofs f :: (b -> c) -> (a -> b) -> a -> c f f1 f2 x = ??? compare: A => B B => C ——————————————— transitivity A => C
  • 86. programs as proofs f :: (b -> c) -> (a -> b) -> a -> c f f1 f2 x = f1 (f2 x) compare: A => B B => C ——————————————— transitivity A => C
  • 87. programs as proofs f :: (a -> b -> c) -> b -> a -> c
  • 88. programs as proofs flip :: (a -> b -> c) -> b -> a -> c flip f y x = f x y
  • 89. programs as proofs flip :: (a -> b -> c) -> b -> a -> c flip f y x = f x y So what is flip a proof of?
  • 90. programs as proofs flip :: (a -> b -> c) -> b -> a -> c flip f y x = f x y So what is flip a proof of? 4 commutativity of implication (A => (B => C)) <=> (B => (A => C))
  • 91. but not every type has a proof
  • 92. programs as proofs f :: a -> b f x = ???
  • 93. programs as proofs f :: a -> b f x = ??? If there is such a function, A => B must be provable. ? —————— A => B
  • 94. programs as proofs f :: a -> b f x = ??? If there is such a function, A => B must be provable. Nope. —————— A => B
  • 95. programs as proofs f :: a -> b f x = ??? -- hypothetical usage examples: foo :: Int bar :: Bool foo = f "hello" bar = f "hello"
  • 96. programs as proofs f :: a -> b f x = ??? -- hypothetical usage examples: foo :: Int bar :: Bool foo = f "hello" bar = f "hello" You can't construct a value of an unknown type.
  • 97. c++ templates and proofs template <typename t, typename t2> t2 f(t a, t2 b) { return ???; }
  • 98. c++ templates and proofs template <typename t, typename t2> t2 f(t a, t2 b) { return "whatever"; } okay, so… `typename t` does not mean `∀t`
  • 99. end of intermission
  • 100. summary 4 type theory is a formal language that describes how terms and types relate to each other 4 a program is a proof of its type 4 not every type has a proof (or is inhabited)
  • 101. end of theory
  • 102. so like, how would i write a head function if i needed to?
  • 103. 3 options
  • 104. option 1: more explicit input type type NonEmptyList a = (a, [a])
  • 105. option 1: more explicit input type type NonEmptyList a = (a, [a]) head :: NonEmptyList a -> a head (x, _) = x
  • 106. option 2: more explicit output type -- head :: [a] -> ?? head (x:_) = ThereIsTotallyAn x
  • 107. option 2: more explicit output type -- head :: [a] -> ?? head (x:_) = ThereIsTotallyAn x head [] = NopeNothingNada
  • 108. option 2: more explicit output type head :: [a] -> MaybeAn a head (x:_) = ThereIsTotallyAn x head [] = NopeNothingNada
  • 109. option 2: more explicit output type head :: [a] -> MaybeAn a head (x:_) = ThereIsTotallyAn x head [] = NopeNothingNada data MaybeAn a = ThereIsTotallyAn a | NopeNothingNada MaybeAn a may contain an a, but it is not an a itself.
  • 110. option 2: more explicit output type head :: [a] -> Maybe a head (x:_) = Just x head [] = Nothing data Maybe a = Just a | Nothing Maybe a may contain an a, but it is not an a itself.
  • 111. option 3: length-aware list type data TaggedList length a -- -- add here: constructors for -- empty and non-empty lists -- data Zero -- 0 data Succ n -- 1, 2, 3, 4, 5, 6, … head :: TaggedList (Succ n) a -> a head (x : _) = x
  • 112. option 3: length-aware list type data TaggedList length a where Empty :: TaggedList Zero a (:.) :: a -- an item -> TaggedList length a -- a list -> TaggedList (Succ length) a -- a longer list data Zero -- 0 data Succ n -- 1, 2, 3, 4, 5, 6, … head :: TaggedList (Succ n) a -> a head (x :. _) = x
  • 113. wow
  • 114. let's do some static duck-typing!
  • 115. duck —> type # THIS IS PYTHON def foo(x): return x * 2 print foo(3) # 6 print foo("hi") # "hihi"
  • 116. duck —> type foo x = x * 2 main = do print (foo 3) -- 6     ghci> :type foo foo :: Num a => a -> a
  • 117. duck —> type foo x = x * 2 main = do print (foo 3) print (foo "hi") No instance for (Num [Char]) arising from a use of `foo' Possible fix: add an instance declaration for (Num [Char]) In the first argument of `print', namely `(foo "hi")' In a stmt of a 'do' block: print (foo "hi")
  • 118. duck —> type class Mul a where (*) :: a -> Int -> a (a can be any concrete type)
  • 119. duck —> type class Mul a where (*) :: a -> Int -> a instance Mul Int where x * y = x Builtin.* y instance Mul String where x * 1 = x x * n | n <= 0 = "" | n > 0 = x ++ x * (n - 1)
  • 120. duck —> type main = do print (foo 3::Int) -- 6 print (foo "hi") -- "hihi" Boom. Done. Works.
  • 121. bonus: kinds
  • 122. kinds The comma in (Bool, Int) is a type-forming operator! ghci> :kind (,) (,) :: * -> * -> *
  • 123. kinds The comma in (Bool, Int) is a type-forming operator! ghci> :kind (,) (,) :: * -> * -> * ghci> :kind (,) Bool (,) Bool :: * -> *
  • 124. kinds The comma in (Bool, Int) is a type-forming operator! ghci> :kind (,) (,) :: * -> * -> * ghci> :kind (,) Bool (,) Bool :: * -> * ghci> :kind (,) Bool Int (,) Bool Int :: *
  • 125. kinds ghci> :kind [Int] [] :: * ghci> :kind [] [] :: * -> *
  • 126. alright
  • 127. Reading recommendation 4 TTFP — Type Theory and Functional Programming
  • 128. fin
  • 129. questions
  • 130. questions
  • 131. appendix
  • 132. nested lists var nestedList = [ 1 , 2 , [ 3 , [ 4, 5 ] ] ];
  • 133. nested lists data NestedList a = Atom a | Nest [NestedList a] nestedList :: NestedList Int nestedList = Nest [ Atom 1 , Atom 2 , Nest [ Atom 3 , Nest [ Atom 4 , Atom 5 ] ] ]