views:

168

answers:

3

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?

+1  A: 

Strictly speaking, () and Void aren't dual--the presence of ⊥ means that all types are inhabited, thus ⊥ is the sole inhabitant of Void, making it a terminal object as you'd expect. () is inhabited by two values, so isn't relevant. If you handwave ⊥ away, then () is terminal and Void is initial as hoped.

I don't think your example is a comonoid structure, either--the signature for a comonoid should be something like this, I think:

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

Which, if you consider what the equivalent comonoid laws must be, ends up being fairly useless, I think.

I wonder instead if what you're getting at is more closely related to the standard sum/product monoids over the naturals, as applied to algebraic data types? Void and Either are 0/+, while () and (,) are 1/*. But I'm not sure how to justify the rest of it.

camccann
+4  A: 
Reid Barton
Thanks! It is this construction, I posted the code.
sdcvvc
+2  A: 

Here's the code:

{-# 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)

Usage:

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"})
sdcvvc