Re:Monoids, Functors, Applicatives, and Monads: Main Ideas
by dllthomas on Monday January 05, 2015 @08:29PM
(#8842430)
I think "fmap f m = (m >>= f >>> return)" also needs to obey the functor laws. I can quickly show that "fmap id = id" follows from the monad laws, but I don't know whether "fmap (p . q) = (fmap p) . (fmap q)" does.
Re:Monoids, Functors, Applicatives, and Monads: Main Ideas
by dllthomas on Monday January 05, 2015 @10:53PM
(#8842842)
Ah, it does seem to follow:
M1: return a >>= f = f a
M2: m >>= return = m
M3: (m >>= f) >>= g = m >>= (\ x -> f x >>= g)
(fmap f . fmap g) m = (fmap f . fmap g) m
by defn of (.) = fmap f (fmap g m)
by defn of fmap = fmap f (m >>= return . g)
by defn of fmap = (m >>= return . g) >>= return . f
by M3 = m >>= (\ x -> (return . g) x >>= return . f)
by defn of (.) = m >>= (\ x -> return (g x) >>= return . f)
by M1 = m >>= (\ x -> (return . f) (g x))
by defn of (.) = m >>= (\ x -> return (f (g x)))
by defn of (.) = m >>= (\ x -> (return . (f . g)) x)
eta reduction = m >>= return . (f . g)
by defn of fmap = fmap (f . g) m
(fmap f . fmap g) m = fmap (f . g) m
[Edit: And if the implementing pieces are named correctly.]