<p>By the way, this:</p>

<blockquote>
  <p>Let $(\times)$ be a product bifunctor in $C$. As $C$ is CCC, we can curry $(\times)$</p>
</blockquote>

<p>is slightly incorrect. For one, the usual terminology would be (if I'm not mistaken) that $\times$ is a bifunctor over or on $C$. "In" typically means constructions using the arrows and objects of a category, whereas functors "on" categories refer to constructions relating multiple categories. And the product bifunctor isn't a construction within a Cartesian category.</p>

<p>And this relates to the larger inaccuracy: the ability to curry the product bifunctor has nothing to do with $C$ being Cartesian closed. Rather, it is possible because $Cat$, the category of categories (insert caveats) is Cartesian closed. So the currying in question is given by:</p>

<p>$$Hom_{Cat}(C \times D, E) \cong Hom_{Cat}(C, E^D)$$</p>

<p>where $C \times D$ is a product of categories, and $E^D$ is the category of functors $F : D \to E$. This works regardless of whether $C$, $D$ and $E$ are Cartesian closed, though. When we let $C = D = E$, we get:</p>

<p>$$\times : C \times C \to C$$
$$curry_\times : C \to C^C$$</p>

<p>But this is merely a special case of:</p>

<p>$$F : C \times D \to E$$
$$curry_F : C \to E^D$$</p>

CC BY-SA 2.5
2010-10-20T00:56:43.047Z
2 Dan Doel: Yes, yes, yes, thanks. I did the mistake while translating from the original post http://beroal.livejournal.com/23223.html .
beroal
CC BY-SA 2.5
2010-10-18T19:30:05.353Z
2010-10-18T19:30:05.353Z
Dan Doel
<p>Consider the adjunction $\langle F,G,\epsilon,\eta \rangle$. For every such adjunction we have a monad $\langle GF, \eta, G\epsilon F\rangle$ and also a comonad $\langle FG, \epsilon, F\eta G\rangle$. Notably, $F$ and $G$ need not be endofunctors, and in general they aren't (e.g., the list monad is an adjuction between the free and forgetful functors between $\mathbf{Set}$ and $\mathbf{Mon}$).</p>

<p>So, what you want to do is take Reader (or Writer) and decompose it into the adjoint functors which give rise to the monad and the corresponding comonad. Which is to say that the connection between Reader and Coreader (or Writer and Cowriter) isn't the one you're looking for.</p>

<p>And it's probably better to think of currying as $-^\ast : \text{hom}({-}\times A, {=}) \cong \text{hom}({-}, {=}^A)$, i.e. $\forall X,Y.~ \lbrace f : X\times A \to Y \rbrace \cong \lbrace f^\ast : X \to Y^A \rbrace$. Or if it helps, $-^\ast : \text{hom}({-}\times A, {=}\times 1) \cong \text{hom}({-}^1, {=}^A)$</p>

CC BY-SA 2.5
2010-10-20T01:16:13.990Z
2 wren ng thornton: I am not aware of any defining adjunction for Reader and Writer similar to adjunctions between Set and a category of algebraic structures. Or do you mean that every monad is defined by an adjunction as in "MacLane. Categories for the Working Mathematician. VI. Monads and Algebras. 2. Algebras for a Monad. Theorem 1 (Every monad is defined by its T-algebras)."? Can you be more specific? Actually my question is the conclusion of an attempt to define those (co)monads in elegant words as the list monad is.
beroal
CC BY-SA 2.5
2010-10-20T08:04:12.617Z
@beroal: I'm pretty sure Reader and Writer aren't adjoint, or at least I've yet to find a way to get the categories to work out for it. No, my point was that monads and comonads arise in "the same way", namely via an adjunction, as described above. I don't have a copy of MacLane, but yes $T$-algebras are the standard name for the trick above (but then again, all sorts of unrelated things are called "X-algebras", "Y-algebras",...).
wren romano
CC BY-SA 2.5
2010-10-20T08:13:05.010Z
Which description of the list monad are you trying to match the eloquence of? Given the free monoid functor $F:\mathbf{Set}\to\mathbf{Mon}$, the forgetful functor $U:\mathbf{Mon}\to\mathbf{Set}$, the unit transformation $\eta:\text{id}_{\mathbf{Set}}\to UF$, and the counit transformation $\epsilon:FU\to\text{id}_{\mathbf{Mon}}$ you have an adjunction $\langle F,U,\eta,\epsilon \rangle$. Which means you have a monad $\langle UF,\eta, U\epsilon F\rangle$, namely the list monad in $\mathbf{Set}$. And you get the list comonad in $\mathbf{Mon}$: $\langle FU,\epsilon, F\eta U\rangle$. Eloquent?
wren romano
CC BY-SA 2.5
2010-10-20T14:47:08.083Z
Functors (Reader a) and (Writer a) are adjoint, and that adjunction gives rise to the (State a) monad.
beroal
CC BY-SA 2.5
2010-10-25T19:01:01.400Z
"No, my point was that monads and comonads arise in "the same way", namely via an adjunction, as described above". If you get the monad and the comonad from the adjunction between categories Set and Mon, you get the monad on Set and the comonad on Mon — different categories. But Reader and Writer are on the same CCC category.
beroal
CC BY-SA 2.5
2010-10-17T23:29:38.320Z
2010-10-17T23:39:40.320Z
2010-10-17T23:39:40.320Z
wren romano
<p>Yes, if a monad $M : C \to C$ has a right adjoint $N$, then $N$ automatically inherits a comonad structure.</p>

<p>The general category-theoretic setting to understand this is as follows.  Let $C$ and $D$ be two categories.  Write $\mathrm{Fun}(C, D)$ for the <em>categeory</em> of functors from $C$ to $D$; Its objects are functors and its morphisms natural transformations.  Write $\mathrm{Fun}^L(C, D)$ for the full subcategory of $\mathrm{Fun}(C, D)$ on the functors which have right adjoints (in other words, we consider functors $C \to D$ with right adjoints and arbitrary natural transformations between them).  Write $F^R : D \to C$ for the right adjoint of a functor $F : C \to D$.  Then $-^R : \mathrm{Fun}^L(C, D) \to \mathrm{Fun}(D, C)$ is a contravariant functor: if $\alpha : F \to G$ is a natural transformation then there is an induced natural transformation $\alpha^R : G^R \to F^R$.</p>

<p>If $C = D$, then $\mathrm{Fun}(C, C)$ has a monoidal structure given by composition and so does $\mathrm{Fun}^L(C, D)$, because the composition of left adjoints is a left adjoint.  Specifically, $(FG)^R = G^R F^R$, so $-^R$ is an antimonoidal contravariant functor.  If you apply $-^R$ to the structural natural transformations which equip a functor $M$ with the structure of a monad, what you get out is a comonad.</p>

CC BY-SA 2.5
2010-10-19T18:26:40.473Z
And one should mention that some of these functors, for example ${-}^R$ is not really a functor but rather something like a pseudo-functor because it typically satisfies functoriality only up to canonical isomorphisms. Nevertheless, the main point is valid.
Andrej Bauer
CC BY-SA 2.5
2010-10-17T23:45:04.507Z
2010-10-17T23:45:04.507Z
Reid Barton
<p>Let $C$ be a <a href="http://en.wikipedia.org/wiki/Cartesian_closed_category" rel="nofollow noreferrer">CCC</a>. Let $(\times)$ be a product bifunctor on $C$. As <a href="http://en.wikipedia.org/wiki/Category_of_small_categories" rel="nofollow noreferrer">Cat</a> is CCC, we can curry $(\times)$:</p>

<p>$curry (\times) : C \rightarrow(C \Rightarrow C)$</p>

<p>$curry (\times) A = \lambda B. A \times B$</p>

<p><a href="http://en.wikipedia.org/wiki/Monoidal_category#Examples" rel="nofollow noreferrer">Functor category $C \Rightarrow C$ has usual monoidal structure.</a> <a href="http://en.wikipedia.org/wiki/Monad_(category_theory)#Formal_definition" rel="nofollow noreferrer">A monoid in $C \Rightarrow C$ is a monad in $C$.</a> <a href="http://ncatlab.org/nlab/show/cartesian+monoidal+category" rel="nofollow noreferrer">We consider finite products as monoidal structure on $C$.</a></p>

<p>$curry (\times) 1 \cong id$</p>

<p>$\forall A\ B. curry (\times) (A\times B) \cong (curry (\times) A) \circ (curry (\times) B)$</p>

<p>Therefore $(curry (\times))$ preserves monoidal structure, so it transports a monoid to a monad and a comonoid to a comonad. Namely, it transports an arbitrary monoid $w$ to <a href="http://www.haskell.org/haskellwiki/All_About_Monads#The_Writer_monad" rel="nofollow noreferrer">$(Writer\ w)$ monad</a> (look at the definition — $w$ must be a monoid). Similarly it transports the <a href="http://ncatlab.org/nlab/show/cartesian+monoidal+category" rel="nofollow noreferrer">diagonal comonoid</a> to the <a href="http://hackage.haskell.org/packages/archive/category-extras/latest/doc/html/Control-Comonad-Reader.html" rel="nofollow noreferrer">Coreader</a> comonad.</p>

<p>Now, for concreteness, I unfold the construction of Writer.</p>

<p><strong>Begin.</strong> Actually $Writer = Coreader = curry(\times)$, they simply have distinct names in Haskell. We have a <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-Monoid.html" rel="nofollow noreferrer">Haskell monoid</a> $\langle w, mappend, mempty\rangle$:</p>

<p>$mappend : w\times w \to w$</p>

<p>$mempty : 1 \to w$</p>

<p>Writer is a functor, so it must map also morphisms, such as $mappend$ and $mempty$. I write this as below, though it is invalid in Haskell:</p>

<p>$Writer\ mappend : Writer(w\times w) \to Writer\ w$</p>

<p>$Writer\ mappend$ is a natural transformation, a morphism in $C\Rightarrow C$. By properties of $curry(\times)$ it is a function, which takes $a \in Ob(C)$ and gives a morphism in $C$:</p>

<p>$Writer\ mappend\ a = mappend\times(id(a)) : Writer (w\times w) a \to Writer\ w\ a$</p>

<p>Informally, $Writer\ mappend\ a$ sums components of type $w$ and pumps $a$ intact. This is exactly the definition of Writer in Haskell. One obstacle is that for the monad $\langle Writer\ w,\mu,\eta\rangle$ we need</p>

<p>$\mu : Writer\ w \circ Writer\ w \to Writer\ w$</p>

<p>i.e. incompatibility of types. But these functors are isomorphic: $Writer (w\times w) = \lambda a. (w\times w)\times a$ by the usual associator for finite products which is a natural isomorphism $\cong \lambda a. w\times(w\times a) = Writer\ w \circ Writer\ w$. Then we define $\mu$ via $Writer\ mappend$. I omit a construction of $\eta$ via $mempty$.</p>

<p>Writer, being a functor, preserves commutative diagrams, i.e. preserves monoid equalities, so we have for granted proved equalities for $\langle Writer\ w,\mu,\eta\rangle$ = a monoid in $(C\Rightarrow C)$ = a monad in $C$. <strong>End.</strong></p>

<p>What about <a href="http://hackage.haskell.org/packages/archive/mtl/latest/doc/html/Control-Monad-Reader.html" rel="nofollow noreferrer">Reader</a> and Cowriter? Reader is adjoint to Coreader, as explained in the definition of Coreader, see link above. Similarly, Cowriter is adjoint to Writer. I did not find a definition of Cowriter, so I made it up by analogy shown in the table:</p>

<p><img src="https://i.sstatic.net/fWdZU.png" alt="alt text"></p>

<pre><code>{- base, Hackage.category-extras -}
import Control.Comonad
import Data.Monoid
data Cowriter w a = Cowriter (w -&gt; a)
instance Functor (Cowriter w) where
    fmap f (Cowriter g) = Cowriter (f . g)
instance Monoid w =&gt; Copointed (Cowriter w) where
    extract (Cowriter g) = g mempty
instance Monoid w =&gt; Comonad (Cowriter w) where
    duplicate (Cowriter g) = Cowriter
        (\w' -&gt; Cowriter (\w -&gt; g (w `mappend` w')))
</code></pre>

<p>Below are the simplified definitions of those (co)monads. fr_ob F denotes a mapping of a functor F on objects, fr_mor F denotes a mapping of a functor F on morphisms. There is a monoid object $\langle w,\hat{+},\hat{0}\rangle$ in $C$.</p>

<ul>
<li>Writer
<ul>
<li>$fr\_ob (Writer\ w) a = a\times w$</li>
<li>$fr\_mor (Writer\ w) f = \lambda \langle a_0,w_2\rangle. \langle a_0,f\ w_2\rangle$</li>
<li>$\eta a = \lambda a_0. \langle a_0, \hat{0} \rangle$</li>
<li>$\mu a = \lambda \langle \langle a_0,w_1\rangle,w_0\rangle. \langle a_0, w_0\hat{+}w_1\rangle$</li>
</ul>
</li>
<li>Reader
<ul>
<li>$fr\_ob (Reader\ r) a = r\to a$</li>
<li>$fr\_mor (Reader\ r) f = \lambda g\ r_0. f (g\ r_0)$</li>
<li>$\eta a = \lambda a_0\ r_0. a_0$</li>
<li>$\mu a = \lambda f\ r_0. f\ r_0\ r_0$</li>
</ul>
</li>
<li>Coreader
<ul>
<li>$fr\_ob (Coreader\ r) a = r\times a$</li>
<li>$fr\_mor (Coreader\ r) f = \lambda \langle r_0,a_0\rangle. \langle f\ r_0,a_0\rangle$</li>
<li>$\eta a = \lambda \langle r_0,a_0\rangle. a_0$</li>
<li>$\mu a = \lambda \langle r_0,a_0\rangle. \langle r_0,\langle r_0,a_0\rangle\rangle$</li>
</ul>
</li>
<li>Cowriter
<ul>
<li>$fr\_ob (Cowriter\ w) a = w\to a$</li>
<li>$fr\_mor (Cowriter\ w) f = \lambda g\ r_0. f (g\ r_0)$</li>
<li>$\eta a = \lambda f. f\ \hat{0}$</li>
<li>$\mu a = \lambda f\ w_1 w_0. f (w_0\hat{+}w_1)$</li>
</ul>
</li>
</ul>

<p>The question is that the adjunction in $C$ relates functors, not monads. I do not see how the adjunction implies "Coreader is a comonad" $\to$ "Reader is a monad" and "Writer is a monad" $\to$ "Cowriter is a comonad".</p>

<p>Remark. I am struggling to provide more context. It requires some work. Especially, if you require categorical purity and those (co)monads were introduced for programmers. Keep nagging! ;)</p>

CC BY-SA 2.5
2010-10-11T01:48:42.233Z
Offer: You can take a screenshot of the table, and put the image here.
Sadeq Dousti
CC BY-SA 2.5
2010-10-11T06:00:13.450Z
You should copy the question here.
Dave Clarke
CC BY-SA 2.5
2010-10-12T03:30:05.187Z
Images are not allowed for new users.
beroal
CC BY-SA 2.5
2010-10-12T04:02:42.477Z
people downvoting should post a comment explaining why.
Suresh Venkat
CC BY-SA 2.5
2010-10-12T08:46:58.760Z
I've added some links to try to make your question more self-contained. By I couldn't find anything for Cowriter. Can you fill in this gap and more clearly state what you want?
Dave Clarke
CC BY-SA 2.5
2010-10-12T13:32:01.867Z
I'm not sure I understand what you say by "This is the construction for Haskell's Writer and Coreader (co)monads, respectively.". Did you prove that if you apply $curry (\times)$ to a monoid you get the Writer monad? Can you elaborate on that proof (starting with "what is the Reader monad in Category Theoretical terms?").
Ohad Kammar
CC BY-SA 2.5
2010-10-12T14:53:24.520Z
@Ohad. I confess that I introduced that change to try to provide the question with more context (as was originally found in the blog  post originally referenced). I think beroal should spend more effort making his question self contained, for example, by defining what Reader and Writer and Coreader and Cowriter are in categorical terms or in Haskell or both, rather than assuming that we all know what is being referred to.
Dave Clarke
CC BY-SA 2.5
2010-10-12T16:38:19.840Z
@Dave: I read the original post, and it also wasn't clear there. I also looked up their definition in the Haskell packages, but my Haskell is rusty and I didn't put too much effort into bridging the gap. But if someone requires less effort to define those monads in categorical terms, I think it would be easier to get a good answer.
Ohad Kammar
CC BY-SA 2.5
2010-10-12T21:00:03.167Z
@Ohad: Certainly a precise definition of all the necessary ingredients might generate some answers.
Dave Clarke
CC BY-SA 2.5
2010-10-12T23:16:03.217Z
2 Dave Clarke: "definition in categorical terms or in Haskell or both" You can interpret Haskell definitions in CCC, so there is practically no difference. Haskell is needed. And currently no motivation because AFAIK those things are not used outside functional programming texts.
beroal
CC BY-SA 2.5
2010-10-13T02:11:32.757Z
@beroal: What I meant was that, as I don't use Haskell on a day to day basis, parsing the Haskell code and making the transition into CT is non-trivial for me, and perhaps others.  By rephrasing the question in purely categorical terms, you are more likely to receive an answer quicker...
Ohad Kammar
CC BY-SA 2.5
2010-10-20T08:20:52.093Z
N.B., for comonads, we usually spell the natural transformations as counit = $\epsilon$, duplicate = $\delta$. Whereas monads are spelled unit = $\eta$, join = $\mu$. Your Coreader and Cowriter definitions are named oddly...
wren romano
CC BY-SA 2.5
2010-10-25T18:58:23.990Z
@wren ng thornton: No abmiguity will arise.
beroal
<functional-programming><pl.programming-languages><ct.category-theory>
Reader, Writer monads
beroal
# Reader, Writer monads

(Asked by: username_0 on 2010-10-11)

Let $C$ be a CCC. Let $(\times)$ be a product bifunctor on $C$. As Cat is CCC, we can curry $(\times)$:
$curry (\times) : C \rightarrow(C \Rightarrow C)$
$curry (\times) A = \lambda B. A \times B$
Functor category $C \Rightarrow C$ has usual monoidal structure. A monoid in $C \Rightarrow C$ is a monad in $C$. We consider finite products as monoidal structure on $C$.
$curry (\times) 1 \cong id$
$\forall A\ B. curry (\times) (A\times B) \cong (curry (\times) A) \circ (curry (\times) B)$
Therefore $(curry (\times))$ preserves monoidal structure, so it transports a monoid to a monad and a comonoid to a comonad. Namely, it transports an arbitrary monoid $w$ to $(Writer\ w)$ monad (look at the definition — $w$ must be a monoid). Similarly it transports the diagonal comonoid to the Coreader comonad.
Now, for concreteness, I unfold the construction of Writer.
Begin. Actually $Writer = Coreader = curry(\times)$, they simply have distinct names in Haskell. We have a Haskell monoid $\langle w, mappend, mempty\rangle$:
$mappend : w\times w \to w$
$mempty : 1 \to w$
Writer is a functor, so it must map also morphisms, such as $mappend$ and $mempty$. I write this as below, though it is invalid in Haskell:
$Writer\ mappend : Writer(w\times w) \to Writer\ w$
$Writer\ mappend$ is a natural transformation, a morphism in $C\Rightarrow C$. By properties of $curry(\times)$ it is a function, which takes $a \in Ob(C)$ and gives a morphism in $C$:
$Writer\ mappend\ a = mappend\times(id(a)) : Writer (w\times w) a \to Writer\ w\ a$
Informally, $Writer\ mappend\ a$ sums components of type $w$ and pumps $a$ intact. This is exactly the definition of Writer in Haskell. One obstacle is that for the monad $\langle Writer\ w,\mu,\eta\rangle$ we need
$\mu : Writer\ w \circ Writer\ w \to Writer\ w$
i.e. incompatibility of types. But these functors are isomorphic: $Writer (w\times w) = \lambda a. (w\times w)\times a$ by the usual associator for finite products which is a natural isomorphism $\cong \lambda a. w\times(w\times a) = Writer\ w \circ Writer\ w$. Then we define $\mu$ via $Writer\ mappend$. I omit a construction of $\eta$ via $mempty$.
Writer, being a functor, preserves commutative diagrams, i.e. preserves monoid equalities, so we have for granted proved equalities for $\langle Writer\ w,\mu,\eta\rangle$ = a monoid in $(C\Rightarrow C)$ = a monad in $C$. End.
What about Reader and Cowriter? Reader is adjoint to Coreader, as explained in the definition of Coreader, see link above. Similarly, Cowriter is adjoint to Writer. I did not find a definition of Cowriter, so I made it up by analogy shown in the table:

{- base, Hackage.category-extras -}
import Control.Comonad
import Data.Monoid
data Cowriter w a = Cowriter (w -> a)
instance Functor (Cowriter w) where
    fmap f (Cowriter g) = Cowriter (f . g)
instance Monoid w => Copointed (Cowriter w) where
    extract (Cowriter g) = g mempty
instance Monoid w => Comonad (Cowriter w) where
    duplicate (Cowriter g) = Cowriter
        (\w' -> Cowriter (\w -> g (w `mappend` w')))

Below are the simplified definitions of those (co)monads. fr_ob F denotes a mapping of a functor F on objects, fr_mor F denotes a mapping of a functor F on morphisms. There is a monoid object $\langle w,\hat{+},\hat{0}\rangle$ in $C$.

Writer

$fr\_ob (Writer\ w) a = a\times w$
$fr\_mor (Writer\ w) f = \lambda \langle a_0,w_2\rangle. \langle a_0,f\ w_2\rangle$
$\eta a = \lambda a_0. \langle a_0, \hat{0} \rangle$
$\mu a = \lambda \langle \langle a_0,w_1\rangle,w_0\rangle. \langle a_0, w_0\hat{+}w_1\rangle$


Reader

$fr\_ob (Reader\ r) a = r\to a$
$fr\_mor (Reader\ r) f = \lambda g\ r_0. f (g\ r_0)$
$\eta a = \lambda a_0\ r_0. a_0$
$\mu a = \lambda f\ r_0. f\ r_0\ r_0$


Coreader

$fr\_ob (Coreader\ r) a = r\times a$
$fr\_mor (Coreader\ r) f = \lambda \langle r_0,a_0\rangle. \langle f\ r_0,a_0\rangle$
$\eta a = \lambda \langle r_0,a_0\rangle. a_0$
$\mu a = \lambda \langle r_0,a_0\rangle. \langle r_0,\langle r_0,a_0\rangle\rangle$


Cowriter

$fr\_ob (Cowriter\ w) a = w\to a$
$fr\_mor (Cowriter\ w) f = \lambda g\ r_0. f (g\ r_0)$
$\eta a = \lambda f. f\ \hat{0}$
$\mu a = \lambda f\ w_1 w_0. f (w_0\hat{+}w_1)$



The question is that the adjunction in $C$ relates functors, not monads. I do not see how the adjunction implies "Coreader is a comonad" $\to$ "Reader is a monad" and "Writer is a monad" $\to$ "Cowriter is a comonad".
Remark. I am struggling to provide more context. It requires some work. Especially, if you require categorical purity and those (co)monads were introduced for programmers. Keep nagging! ;)

**Tags:** functional-programming, pl.programming-languages, ct.category-theory
> **Comments:**
> **username_1**: Offer: You can take a screenshot of the table, and put the image here.  
> **username_2**: You should copy the question here.  
> **username_0**: Images are not allowed for new users.  
> **username_3**: people downvoting should post a comment explaining why.  
> **username_2**: I've added some links to try to make your question more self-contained. By I couldn't find anything for Cowriter. Can you fill in this gap and more clearly state what you want?  
> **username_4**: I'm not sure I understand what you say by "This is the construction for Haskell's Writer and Coreader (co)monads, respectively.". Did you prove that if you apply $curry (\times)$ to a monoid you get the Writer monad? Can you elaborate on that proof (starting with "what is the Reader monad in Category Theoretical terms?").  
> **username_2**: @Ohad. I confess that I introduced that change to try to provide the question with more context (as was originally found in the blog  post originally referenced). I think beroal should spend more effort making his question self contained, for example, by defining what Reader and Writer and Coreader and Cowriter are in categorical terms or in Haskell or both, rather than assuming that we all know what is being referred to.  
> **username_4**: @Dave: I read the original post, and it also wasn't clear there. I also looked up their definition in the Haskell packages, but my Haskell is rusty and I didn't put too much effort into bridging the gap. But if someone requires less effort to define those monads in categorical terms, I think it would be easier to get a good answer.  
> **username_2**: @Ohad: Certainly a precise definition of all the necessary ingredients might generate some answers.  
> **username_0**: 2 Dave Clarke: "definition in categorical terms or in Haskell or both" You can interpret Haskell definitions in CCC, so there is practically no difference. Haskell is needed. And currently no motivation because AFAIK those things are not used outside functional programming texts.  
> **username_4**: @beroal: What I meant was that, as I don't use Haskell on a day to day basis, parsing the Haskell code and making the transition into CT is non-trivial for me, and perhaps others.  By rephrasing the question in purely categorical terms, you are more likely to receive an answer quicker...  
> **username_5**: N.B., for comonads, we usually spell the natural transformations as counit = $\epsilon$, duplicate = $\delta$. Whereas monads are spelled unit = $\eta$, join = $\mu$. Your Coreader and Cowriter definitions are named oddly...  
> **username_0**: @wren ng thornton: No abmiguity will arise.  

---

## 3 answers

### Answer by username_7 on 2010-10-17. Score: 14 | Accepted answer

Yes, if a monad $M : C \to C$ has a right adjoint $N$, then $N$ automatically inherits a comonad structure.

The general category-theoretic setting to understand this is as follows. Let $C$ and $D$ be two categories. Write $\mathrm{Fun}(C, D)$ for the *categeory* of functors from $C$ to $D$; Its objects are functors and its morphisms natural transformations. Write $\mathrm{Fun}^L(C, D)$ for the full subcategory of $\mathrm{Fun}(C, D)$ on the functors which have right adjoints (in other words, we consider functors $C \to D$ with right adjoints and arbitrary natural transformations between them). Write $F^R : D \to C$ for the right adjoint of a functor $F : C \to D$. Then $-^R : \mathrm{Fun}^L(C, D) \to \mathrm{Fun}(D, C)$ is a contravariant functor: if $\alpha : F \to G$ is a natural transformation then there is an induced natural transformation $\alpha^R : G^R \to F^R$.

If $C = D$, then $\mathrm{Fun}(C, C)$ has a monoidal structure given by composition and so does $\mathrm{Fun}^L(C, D)$, because the composition of left adjoints is a left adjoint. Specifically, $(FG)^R = G^R F^R$, so $-^R$ is an antimonoidal contravariant functor. If you apply $-^R$ to the structural natural transformations which equip a functor $M$ with the structure of a monad, what you get out is a comonad.

> **Comments:**
> **username_8**: And one should mention that some of these functors, for example ${-}^R$ is not really a functor but rather something like a pseudo-functor because it typically satisfies functoriality only up to canonical isomorphisms. Nevertheless, the main point is valid.  

---

### Answer by username_6 on 2010-10-18. Score: 7

By the way, this:

> Let $(\times)$ be a product bifunctor in $C$. As $C$ is CCC, we can curry $(\times)$

is slightly incorrect. For one, the usual terminology would be (if I'm not mistaken) that $\times$ is a bifunctor over or on $C$. "In" typically means constructions using the arrows and objects of a category, whereas functors "on" categories refer to constructions relating multiple categories. And the product bifunctor isn't a construction within a Cartesian category.

And this relates to the larger inaccuracy: the ability to curry the product bifunctor has nothing to do with $C$ being Cartesian closed. Rather, it is possible because $Cat$, the category of categories (insert caveats) is Cartesian closed. So the currying in question is given by:

$$Hom\_{Cat}(C \times D, E) \cong Hom\_{Cat}(C, E^D)$$

where $C \times D$ is a product of categories, and $E^D$ is the category of functors $F : D \to E$. This works regardless of whether $C$, $D$ and $E$ are Cartesian closed, though. When we let $C = D = E$, we get:

$$\times : C \times C \to C$$
$$curry\_\times : C \to C^C$$

But this is merely a special case of:

$$F : C \times D \to E$$
$$curry\_F : C \to E^D$$

> **Comments:**
> **username_0**: 2 Dan Doel: Yes, yes, yes, thanks. I did the mistake while translating from the original post http://beroal.livejournal.com/23223.html .  

---

### Answer by username_5 on 2010-10-17. Score: 4

Consider the adjunction $\langle F,G,\epsilon,\eta \rangle$. For every such adjunction we have a monad $\langle GF, \eta, G\epsilon F\rangle$ and also a comonad $\langle FG, \epsilon, F\eta G\rangle$. Notably, $F$ and $G$ need not be endofunctors, and in general they aren't (e.g., the list monad is an adjuction between the free and forgetful functors between $\mathbf{Set}$ and $\mathbf{Mon}$).

So, what you want to do is take Reader (or Writer) and decompose it into the adjoint functors which give rise to the monad and the corresponding comonad. Which is to say that the connection between Reader and Coreader (or Writer and Cowriter) isn't the one you're looking for.

And it's probably better to think of currying as $-^\ast : \text{hom}({-}\times A, {=}) \cong \text{hom}({-}, {=}^A)$, i.e. $\forall X,Y.~ \lbrace f : X\times A \to Y \rbrace \cong \lbrace f^\ast : X \to Y^A \rbrace$. Or if it helps, $-^\ast : \text{hom}({-}\times A, {=}\times 1) \cong \text{hom}({-}^1, {=}^A)$

> **Comments:**
> **username_0**: 2 wren ng thornton: I am not aware of any defining adjunction for Reader and Writer similar to adjunctions between Set and a category of algebraic structures. Or do you mean that every monad is defined by an adjunction as in "MacLane. Categories for the Working Mathematician. VI. Monads and Algebras. 2. Algebras for a Monad. Theorem 1 (Every monad is defined by its T-algebras)."? Can you be more specific? Actually my question is the conclusion of an attempt to define those (co)monads in elegant words as the list monad is.  
> **username_5**: @beroal: I'm pretty sure Reader and Writer aren't adjoint, or at least I've yet to find a way to get the categories to work out for it. No, my point was that monads and comonads arise in "the same way", namely via an adjunction, as described above. I don't have a copy of MacLane, but yes $T$-algebras are the standard name for the trick above (but then again, all sorts of unrelated things are called "X-algebras", "Y-algebras",...).  
> **username_5**: Which description of the list monad are you trying to match the eloquence of? Given the free monoid functor $F:\mathbf{Set}\to\mathbf{Mon}$, the forgetful functor $U:\mathbf{Mon}\to\mathbf{Set}$, the unit transformation $\eta:\text{id}_{\mathbf{Set}}\to UF$, and the counit transformation $\epsilon:FU\to\text{id}_{\mathbf{Mon}}$ you have an adjunction $\langle F,U,\eta,\epsilon \rangle$. Which means you have a monad $\langle UF,\eta, U\epsilon F\rangle$, namely the list monad in $\mathbf{Set}$. And you get the list comonad in $\mathbf{Mon}$: $\langle FU,\epsilon, F\eta U\rangle$. Eloquent?  
> **username_0**: Functors (Reader a) and (Writer a) are adjoint, and that adjunction gives rise to the (State a) monad.  
> **username_0**: "No, my point was that monads and comonads arise in "the same way", namely via an adjunction, as described above". If you get the monad and the comonad from the adjunction between categories Set and Mon, you get the monad on Set and the comonad on Mon — different categories. But Reader and Writer are on the same CCC category.  

---

