Are the Writer m and E e monads categorically dual?

I noticed that there is a double relationship between Writer mand monads Either e. 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)

Double () is Void (empty type), dual to the product is coproduct. Each type e can be assigned a "comonoid" structure:

unit :: Void -> e
join :: Either e e -> e

in an 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 a monad Either e. The arrows match exactly the same pattern.

Question: Is it possible to write one general code that can execute both Either e, and Writer mdepending on the given monoid?

+5
source share
3 answers

, , , : ( C, & ot;, 1) A C, , X & otimes; X. C Hask, & otimes; & times;, , C Hask, & otimes; ∐ (), - ( - w.r.t. ∐ - , "", - , . ). , , ⊥ , & times; . , , ( , , - ) Writer Either ( modulo newtypes, ).

Writer m &mdash, , , , -, comonad (,) m - m:

instance Comonad ((,) m) where
    coreturn (m, a) = a
    cojoin (m, a) = (m, (m, a))

(, , m , .. m →(), m → m & times; m).

+5

:

{-# LANGUAGE FlexibleInstances, EmptyDataDecls, MultiParamTypeClasses,
FunctionalDependencies, GeneralizedNewtypeDeriving, UndecidableInstances #-}

import Control.Arrow (first, second, left, right)
import Data.Monoid

data Void
data Iso a b = Iso { from :: a -> b, to :: b -> a}

-- monoidal category (Hask, m, unit)
class MonoidalCategory m unit | m -> unit where
  iso1 :: Iso (m (m x y) z) (m x (m y z))
  iso2 :: Iso x (m x unit)
  iso3 :: Iso x (m unit x)

  map1 :: (a -> b) -> (m a c -> m b c)
  map2 :: (a -> b) -> (m c a -> m c b)

instance MonoidalCategory (,) () where
  iso1 = Iso (\((x,y),z) -> (x,(y,z))) (\(x,(y,z)) -> ((x,y),z))
  iso2 = Iso (\x -> (x,())) (\(x,()) -> x)
  iso3 = Iso (\x -> ((),x)) (\((),x) -> x)
  map1 = first
  map2 = second

instance MonoidalCategory Either Void where
  iso1 = Iso f g
         where f (Left (Left x)) = Left x
               f (Left (Right x)) = Right (Left x)
               f (Right x) = Right (Right x)

               g (Left x) = Left (Left x)
               g (Right (Left x)) = Left (Right x)
               g (Right (Right x)) = Right x
  iso2 = Iso Left (\(Left x) -> x)
  iso3 = Iso Right (\(Right x) -> x)
  map1 = left
  map2 = right

-- monoid in monoidal category (Hask, c, u)
class MonoidM m c u | m -> c u where
  mult :: c m m -> m
  unit :: u -> m

-- object of monoidal category (Hask, Either, Void)
newtype Eith a = Eith { getEith :: a } deriving (Show)

-- object of monoidal category (Hask, (,), ())
newtype Monoid m => Mult m = Mult { getMult :: m } deriving (Monoid, Show)

instance MonoidM (Eith a) Either Void where
  mult (Left x) = x
  mult (Right x) = x
  unit _ = undefined

instance Monoid m => MonoidM (Mult m) (,) () where
  mult = uncurry mappend
  unit = const mempty

instance (MonoidalCategory c u, MonoidM m c u) => Monad (c m) where
  return = map1 unit . from iso3
  x >>= f = (map1 mult . to iso1) (map2 f x)

:

a = (Mult "hello", 5) >>= (\x -> (Mult " world", x+1))
                                 -- (Mult {getMult = "hello world"}, 6)
inv 0 = Left (Eith "error")
inv x = Right (1/x)
b = Right 5 >>= inv              -- Right 0.2
c = Right 0 >>= inv              -- Left (Eith {getEith="error"})
d = Left (Eith "a") >>= inv      -- Left (Eith {getEith="a"})
+3

, () Void - ⊥ , , ⊥ Void, , . () , . , () , Void , .

, , - - , :

class Comonoid a
    coempty :: a -> ()
    coappend :: a -> (a, a)

, , , , .

Interestingly, what do you get is more closely related to standard sum / product monoids over naturals, applicable to algebraic data types? Voidand Eitherequal to 0 / +, ()and (,)equal to 1 / *. But I'm not sure how to justify everything else.

+1
source

All Articles