Are monads Writer m and Either e categorically dual?
Posted
by sdcvvc
on Stack Overflow
See other posts from Stack Overflow
or by sdcvvc
Published on 2010-04-22T10:44:57Z
Indexed on
2010/04/23
7:23 UTC
Read the original article
Hit count: 288
I noticed there is a dual relation between Writer m
and Either e
monads. If m is a monoid, then
unit :: () -> m
join :: (m,m) -> m
can be used to form a monad:
return is composition: a -> ((),a) -> (m,a)
join is composition: (m,(m,a)) -> ((m,m),a) -> (m,a)
The dual of () is Void (empty type), the dual of product is coproduct. Every type e can be given "comonoid" structure:
unit :: Void -> e
join :: Either e e -> e
in the obvious way. Now,
return is composition: a -> Either Void a -> Either e a
join is composition: Either e (Either e a) -> Either (Either e e) a -> Either e a
and this is the Either e
monad. The arrows follow exactly the same pattern.
Question: Is it possible to write a single generic code that will be able to perform both as Either e
and as Writer m
depending on the monoid given?
© Stack Overflow or respective owner