Home

comonads

Comonads are a concept in category theory that are dual to monads. They consist of an endofunctor W on a category C, together with two natural transformations: a counit ε: W ⇒ Id_C and a comultiplication δ: W ⇒ W ∘ W. These must satisfy the coassociativity and counit laws: δ followed by δ, when composed appropriately, equals δ followed by Wδ (coassociativity), and applying ε after or before δ yields the identity (counit laws). In components, for each object X in C, δ_X: W X → W(W X) and ε_X: W X → X, with the usual naturality conditions.

Intuitively, a comonad models computations that can observe and duplicate context. The comonadic structure provides a

Common examples arise in concrete categories. The store comonad on Set is a canonical instance: for a

In functional programming, comonads provide a dual perspective to monads for modeling context, coeffects, and dataflow.

way
to
extract
a
value
from
a
context
and
to
expand
a
context
to
contexts
of
contexts,
enabling
context-dependent
computations
through
operations
like
extend
(a
higher-level
map
that
threads
the
comonad’s
context
through
functions).
set
of
states
S,
define
W
X
=
Store
S
X,
with
extract
(Store
f
s)
=
f
s
and
duplicate
(Store
f
s)
=
Store
(\s'
->
Store
f
s')
s.
This
captures
read-only,
indexable
stores
where
the
current
position
is
observable.
Another
familiar
example
is
the
stream
comonad,
where
W
X
is
an
infinite
stream
of
X
values;
extract
yields
the
current
element,
and
duplicate
yields
streams
of
streams
representing
all
possible
continuations.
They
appear
in
design
patterns
for
context-dependent
computations,
data
streaming,
and
reversible
or
zoomable
data
structures.
The
theory
of
comonads
parallels
that
of
monads,
including
notions
such
as
cofree
comonads
and
dual
constructions.