date post

26-Aug-2014Category

Self Improvementview

326download

12

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.

- types, types, types
- 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.
- goals
- dissatisfied excited common language
- 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
- just ask the type inferencer
- just ask the type inferencer ghci> :type 3 > 0 Hey type inferencer, I was just wondering: 4 What's the type of 3 > 0?
- just ask the type inferencer ghci> :type 3 > 0 3 > 0 :: Bool The term 3 > 0 has the type Bool.
- just ask the type inferencer ghci> :type not
- just ask the type inferencer ghci> :type not not :: Bool -> Bool "not" is a function that takes a Bool to a Bool.
- just ask the type inferencer ghci> let f (x, y) = True ghci> :type f
- 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
- just ask the type inferencer ghci> let f x y = x == y ghci> :type f
- 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.
- 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.)
- just ask the type inferencer ghci> let f x = x * 2 ghci> :type f
- 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".
- 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 ...
- just ask the type inferencer ghci> let f x = x * 2 ghci> f True :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
- ok
- summary 4 if you don't know the type, ask the type inferencer 4 functions have types 4 operations constrain types
- main A
- type checking is lie detection
- type checking is lie detection intIdentity :: Int -> Int intIdentity x = x
- type checking is lie detection intIdentity :: Int -> Int intIdentity x = 42 But that's not an identity function! :(
- how can we prevent ourselves from lying?
- the function that
- type checking is lie detection -- intIdentity :: Int -> Int identity :: a -> a identity x = ???
- type checking is lie detection -- intIdentity :: Int -> Int identity :: a -> a identity x = 42
- 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.
- 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.
- 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
- parametricity
- c++ templates and parametricity template t identity(t a) { return ???; }
- c++ templates and parametricity template t identity(t a) { return 42; }
- end of intermission
- parametricity f :: [t] -> [t] What can this function not do?
- parametricity f :: [t] -> [t] What can this function not do? 4 insert items 4 change values
- parametricity f :: [t] -> [t] So what can it do at all?
- parametricity f :: [t] -> [t] So what can it do at all? 4 rearrange list items 4 drop list items
- 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 "" -- ""
- parametricity / lie detection f :: [t] -> t
- parametricity / lie detection head :: [t] -> t head (x:_) = x
- parametricity / lie detection head :: [t] -> t head (x:_) = x Warning: Pattern match(es) are non-exhaustive In an equation for `head': Patterns not matched: []
- 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
- 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
- main B
- what is type theory anyway?
- "type theory is the grand, unified theory of computation" robert harper
- type theory is a formal language that describes how terms and types relate to each other
- 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
- 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"
- 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) : AB
- type theory: elimination rules A is a type B is a type (F) A B is a type x:A y:B z : AB (I) (E1) (x,y) : AB first z : A
- type theory: elimination rules A is a type B is a type (F) A B is a type x:A y:B z : AB z : AB (I) (E1) (E2) (x,y) : AB first z : A second z : B
- type theory: computation rules first(x,y) > x second(x,y) > y (The left side reduces to the right side.)
- 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 t