Missing laws #5
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.90.2560 provides an identical set of laws to the ones I'd expect.
I'm currently mulling over my
In particular, the last example shows how to lift a control operation in a unique way, something which I didn't think was possible.
These laws are not an a-priori characterization, they only hold for monad transformers. You need to walk down the transformer stack to see how they relate to each other. For instance, to see that
one has to unpeel all the intermediate
(Actually, I think that a-priori laws are not a complete story, you also need to know how the instances are lifted through a monad transformer.)
The class for the writer monad is weird because it has so many control operations. I think the laws for a monad transformer should be:
The laws for
This was referenced
Sep 7, 2015
@ekmett That paper states
modify f >> modify g = modify (g . f) -- modify-modify get >>= \s1 -> get -> \s2 -> return (s1, s2) = get >>= \s -> return (s, s) -- get-get get >>= \s1 -> modify f >> get >>= \s2 -> return (s1, s2) = get >>= \s -> modify f >> return (s, f s) -- get-modify-get
get >>= \s1 -> get -> \s2 -> return (s1, s2) = get >>= \s -> return (s, s) -- get-get (the same) get >>= \_ -> put s = put s -- get-put put s1 >> put s2 = put s2 -- put-put put s >> get = put s >> return s -- put-get
get >>= \s -> put s = return () -- get-put-2
In a similar vein, one might think of
void get = return () -- get-void
Are the equations above general enough? Is there a litmus test for sufficiently general
This may just be my wishful thinking, but one nice-sounding property is that any sequence of
EDIT: I have a lot to read; I could find these laws mentioned in various places. http://gallium.inria.fr/blog/lawvere-theories-and-monads/
(,) <$> ask <*> ask = (\r -> (r, r)) <$> ask -- ask-ask local f ask = fmap f ask -- local-ask local f . local g = local (g . f) -- local-local local f (return x) = return x -- local-M-return local f (m >>= \x -> k x) = local f m >>= \x -> local f (k x) -- local-M-bind
@ocharles Right, that looks equivalent and simpler. Formally speaking, I'm missing a step to prove that
Interesting, these homomorphisms imply a lot of the laws.
For example, obviously in