2 ms·
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
by dllthomas 12y ago
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.
- dllthomas 12y agoAh, 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