Wearing the hair shirt Wearing the hair shirt - Microsoft Research
Wearing the hair shirt Wearing the hair shirt - Microsoft Research Wearing the hair shirt Wearing the hair shirt - Microsoft Research
Type classes class Eq a where (==) :: a -> a -> Bool instance Eq Int where i1 == i2 = eqInt i1 i2 Initially, just a neat way to get systematic overloading of (==), read, show. instance (Eq a) => Eq [a] where [] == [] = True (x:xs) == (y:ys) = (x == y) && (xs == ys) member :: Eq a => a -> [a] -> Bool member x [] = False member x (y:ys) | x==y = True | otherwise = member x ys
Implementing type classes data Eq a = MkEq (a->a->Bool) eq (MkEq e) = e dEqInt :: Eq Int dEqInt = MkEq eqInt Instance declarations create dictionaries Class witnessed by a “dictionary” of methods dEqList :: Eq a -> Eq [a] dEqList (MkEq e) = MkEq el where el [] [] = True el (x:xs) (y:ys) = x `e` y && xs `el` ys Overloaded functions take extra dictionary parameter(s) member :: Eq a -> a -> [a] -> Bool member d x [] = False member d x (y:ys) | eq d x y = True | otherwise = member deq x ys
- Page 1 and 2: Wearing the hair shirt A retrospect
- Page 3 and 4: Haskell is 15 years old (born FPCA
- Page 5 and 6: Timeline Sept 87: kick off Apr 90:
- Page 7 and 8: Reflections on the process The i
- Page 9: Reflections on process Self-appoin
- Page 12 and 13: Syntactic redundancy Seductive ide
- Page 14 and 15: “Expression style” Define a fun
- Page 16 and 17: Example (ICFP02 prog comp) Pattern
- Page 18 and 19: What really matters?
- Page 20 and 21: But... Laziness makes it much, muc
- Page 22: Combinator libraries Recursive valu
- Page 25 and 26: Monadic I/O A value of type (IO t)
- Page 27 and 28: Connecting I/O operations (>>=) ::
- Page 29 and 30: Control structures Values of type (
- Page 31 and 32: Monads Exceptions type Exn a = E
- Page 33 and 34: The IO monad The IO monad allows
- Page 35 and 36: What have we achieved? ...without
- Page 37 and 38: Open challenge 1 Open problem: the
- Page 39 and 40: Monad summary Monads are a beautif
- Page 41 and 42: What really matters? Laziness Purit
- Page 46 and 47: Type classes over time Type classe
- Page 48 and 49: Quickcheck propRev :: [Int] -> Bool
- Page 50 and 51: Extensiblity Like OOP, one can add
- Page 52 and 53: Type-based dispatch class Num a whe
- Page 54 and 55: Cool generalisations Multi-paramet
- Page 56: Type classes summary A much more f
- Page 59 and 60: Is sexy good? Yes! Well typed prog
- Page 61 and 62: Destination = F w
- Page 63 and 64: Modules Porsche High power, but poo
- Page 65 and 66: Encapsulating it all data ST s a --
- Page 67 and 68: Shirts off to Wadler Type classes
Type classes<br />
class Eq a where<br />
(==) :: a -> a -> Bool<br />
instance Eq Int where<br />
i1 == i2 = eqInt i1 i2<br />
Initially, just a neat<br />
way to get systematic<br />
overloading of (==),<br />
read, show.<br />
instance (Eq a) => Eq [a] where<br />
[] == [] = True<br />
(x:xs) == (y:ys) = (x == y) && (xs == ys)<br />
member :: Eq a => a -> [a] -> Bool<br />
member x []<br />
= False<br />
member x (y:ys) | x==y = True<br />
| o<strong>the</strong>rwise = member x ys