A monad is a monoid in the category of endofunctors,
what's the problem?
Slide 2
Slide 2 text
No content
Slide 3
Slide 3 text
The magic of functional composition
Slide 4
Slide 4 text
add2(1)
add2(x) = x + 2
3
Slide 5
Slide 5 text
sub1(add2(1))
sub1(x) = x - 1
2
Slide 6
Slide 6 text
crunch(5)
crunch(x) = sub1(add2(x))
6
Slide 7
Slide 7 text
crunch(5)
crunch = sub1 . add2
6
Slide 8
Slide 8 text
crunch([1,2,3])
WTF?????
Slide 9
Slide 9 text
FUNCTORS
Slide 10
Slide 10 text
fcrunch([1,2,3])
fcrunch = fmap crunch
[2,3,4]
Slide 11
Slide 11 text
class Functor f where
fmap :: (a -> b) -> f a -> f b
Slide 12
Slide 12 text
fmap transforms a “normal” function (g :: a -> b) into
one which operates over containers/contexts (fmap
g :: f a -> f b). This transformation is often referred to
as a lift; fmap “lifts” a function from the “normal world”
into the “f world”.
Slide 13
Slide 13 text
*> :t add2
add2 :: Num a => a -> a
*> :t crunch
crunch :: Num a => a -> a
*> :t fcrunch
fcrunch :: (Functor f, Num b) => f b -> f b
Slide 14
Slide 14 text
instance Functor [] where
fmap _ [] = []
fmap g (x:xs) = g x : fmap g xs
-- or we could just say fmap = map
Slide 15
Slide 15 text
instance Functor Maybe where
fmap _ Nothing = Nothing
fmap g (Just a) = Just (g a)
Slide 16
Slide 16 text
•[]
•Tree
•Map
•Sequence
•Maybe
•IO
Slide 17
Slide 17 text
MATH TIME
Slide 18
Slide 18 text
–Wikipedia
“In mathematics, a functor is a type of mapping
between categories, which is applied in category
theory. Functors can be thought of as
homomorphisms between categories”
Slide 19
Slide 19 text
Categories are bags of stuff that hold:
• Objects
• Morphisms
Math
Slide 20
Slide 20 text
Categories are bags of stuff that hold:
• Types
• Functions
Code
Slide 21
Slide 21 text
add2
sub1
Number
f add2
f sub1
f Number
Functor
Slide 22
Slide 22 text
add2
sub1
10
fmap add2
fmap sub1
[10]
[]
LIFT
Slide 23
Slide 23 text
FUNCTOR RULES
Math
Slide 24
Slide 24 text
FUNCTOR RULES
•fmap id x = x
•fmap(g.h) = (fmap g).(fmap h)
Code
Slide 25
Slide 25 text
BROKEN FUNCTOR
instance Functor [] where
fmap _ [] = []
fmap g (x:xs) = g x : g x : fmap g xs