Professional Documents
Culture Documents
State Monads and Their Algebras
State Monads and Their Algebras
François Métayer∗
Abstract
State monads in cartesian closed categories are those defined by the
familiar adjunction between product and exponential. We investigate
the structure of their algebras, and show that the exponential functor
is monadic provided the base category is sufficiently regular, and the ex-
ponent is a non-empty object.
1 Introduction
Let C be a cartesian closed category. Any object S in C gives rise to a monad
T = U F , where
U : X 7→ X S
and
F : X 7→ S × X
T , among other monads, has been extensively studied by researchers in func-
tional programming, in order to model computational effects (see for example
[3, 4, 6]), and called a state monad since then. A main source of the present
work is [5], which investigates the algebras of state monads, in a significantly
different setting however, and provides an explicit equational presentation of
these algebras.
This raises the question as whether the original functor U itself is monadic:
it turns out that the answer is positive, provided C is sufficiently regular, and
S is non-empty (Theorem 2).
1.1 Notations
Throughout this article, S will denote the fixed object of C, on which T is built.
The natural isomorphism
will be denoted by
f 7→ f ∗
∗ Équipe PPS, Université Paris 7-CNRS, 2 pl. Jussieu, Case 7014 F75251 Paris Cedex 05
Email: metayer@logique.jussieu.fr
1
and its inverse by
g 7→ g∗
In particular, for each X, the identity
idX S : X S → X S
gives rise to
(idX S )∗ : S × X S → X
which is the evaluation morphism hs, f i 7→ f [s], and will be denoted by ǫX . It
is of course the counit of the adjunction. Finally, for each object X in C, the
projections S × X → S and S × X → X will be denoted by pX , qX respectively.
Note that p, q are natural transformations F → 1.
in T T X, and outputs
λs hc′ [s, c[s]], f [s, c[s]]i
which belongs to T X. In other words, µX is ǫSS×X . Recall that T -algebras are
pairs hX, hi where X and h : T X → X are such that the following diagrams
commute:
Th / ηX
T 2X TX XC / TX (1)
CC
µX
CCC
h h
idX CC
!
TX h / X X
h(λs hc[s], h(λs′ hc′ [s, s′ ], x[s, s′ ]i)i) = h(λs hc′ [s, c[s]], x[s, c[s]]i) (2)
h(λs hs, xi) = x (3)
Tf
TX / T X′
h h′
X / X′
f
2
Algebras and morphisms build a category AlgT , with an obvious forgetful func-
tor U T : hX, hi 7→ X.
As with any adjunction, we may define a comparison functor
K : C → AlgT
Y 7→ Y S , ǫSY
L : AlgT → Sets
such that KL and LK are naturally isomorphic to the identity functor on AlgT
and Sets respectively. Let hX, hi be a T -algebra, we define the set
Y = L hX, hi
qS×X : S × (S × X) → S × X
we get
∗
qS×X : S × X → (S × X)S = T X
hence
∗
φ = h ◦ qS×X :S×X →X
Then Y = L hX, hi will be the image of φ. Concretely, Y is the subset of X
given by
Y = {h(λs′ hs, xi)|s ∈ S, x ∈ X}
and we get a surjective map eX : S × X → Y making the following diagram
commutative:
∗
qS×X
S ×X / TX
eX h
Y /X
mX
3
where mX is the inclusion monomorphism (in fact eX and mX really depend on
the algebra hX, hi). Now L has to be defined on morphisms as well: let then u be
a morphism of algebras u : hX, hi → hX ′ , h′ i, Y = L hX, hi and Y ′ = L hX ′ , h′ i.
There is a unique map Lu : Y → Y ′ such that the following diagram commutes:
S×u
S×X / S × X′
eX eX ′
Y / Y′
Lu
Display eX as a cokernel
a // S × X eX
Z /Y
b
The inner square commutes because u is a morphism of algebras, the top trape-
zoid commutes by naturality of q ∗ , and the left and right hand trapezoids com-
mute by definition of e, m . Hence
mX ′ ◦ eX ′ ◦ (S × u) ◦ a = u ◦ mX ◦ e X ◦ a
= u ◦ mX ◦ e X ◦ b
= mX ′ ◦ eX ′ ◦ (S × u) ◦ b
eX ′ ◦ (S × u) ◦ a = eX ′ ◦ (S × u) ◦ b
e : FUT → L
4
As a consequence, the adjunction gives a natural transformation from U T to
U L, or better, as U = U T K
e∗ : U T → UT KL
Thus we reduced the existence of a natural isomorphism:
KL ≃ 1
to the following statements:
• for all hX, hi, e∗X is a bijection;
• for all hX, hi, e∗X is a morphism of algebras.
which will be proved separately in Lemmas 1 and 2 below.
Finally, there is a natural isomorphism:
LK ≃ 1
If Y is a set, LKY is nothing but the subset of Y S consisting of constant maps
from S to Y , which is naturally isomorphic to Y because S 6= ∅. 2
The two following lemmas will complete our argument. Keeping the previous
notations,
Lemma 1 For each T -algebra hX, hi, the map e∗X is a bijection.
Proof. Let us give an explicit construction of the inverse map f . For each
y ∈ Y S , we define f (y) ∈ X by
f (y) = h(λs hs, y[s]i)
Now, for each x ∈ X,
f (e∗X (x)) = h(λs hs, h(λs′ hs, xi)i)
= h(λs hs, xi) by (2)
= x by (3)
Thus f is a retraction for e∗X .
On the other hand, let y ∈ Y S ,
e∗X (f (y)) = λsh(λs′ hs, f (y)i)
= λsh(λs′ hs, h(λs′′ hs′′ , y[s′′ ]i)i
= λsh(λs′ hs, y[s]i) by (2)
but y[s] = h(λs′′ hc[s], x[s]i) for suitable maps c and x, by definition of Y , so
that the last expression reduces to
λsh(λs′ hc[s], x[s]i) = λsy[s] = y
by (2) again. Thus e∗X (f (y)) = y, and f is also a section of e∗X . Hence f =
(e∗X )−1 , as required. 2
5
Lemma 2 For each T -algebra hX, hi, e∗X is a morphism of algebras.
T e∗
X
TX / T (Y S )
h ǫS
Y
X / YS
e∗
X
ǫSY ◦ T e∗X (u) = ǫSY (λs hc[s], λs′ h(λs′′ hs′ , x[s]i)i)
= λsh(λs′′ hc[s], x[s]i)
so that
e∗X ◦ h = ǫSY ◦ T e∗X
2
3 A general theorem
By carefully examining the proofs in section 2, we see that the equations (2) and
(3) are only used in proving Lemmas 1 and 2. Thus large parts of our argument
still hold beyond Sets. As we shall see, a key hypothesis for generalizing the
previous results is that C has regular epi-mono factorization, that is, each arrow
in C factorizes as a composition of a regular epimorphism and a monomorphism.
The regularity hypothesis implies that such a factorization is unique, up to
isomorphism. Thus from now on, we assume that C has regular epi-mono
factorization.
3.1 Construction of L
Let us first generalize the construction of
L : AlgT → C
6
so that all we need to extend the previous definition of L is the existence of
an image object for fX , which immediately follows from the regular epi-mono
factorization property on C.
Thus we get a regular epimorphism eX and a monomorphism mX such that
f X = mX ◦ e X (4)
and L hX, hi can be defined as the object Y in the commutative diagram
∗
qS×X
S ×X / TX
eX h
Y /X
mX
3.2 A retraction
Proposition 1 For each T -algebra hX, hi, e∗X : X → Y S has a retraction.
Proof. Consider the following commutative diagram:
∗
qS×X
S × XH / TX (5)
HH f
eX
HH X
HH h
HH
$
Y /X
mX
The left lower triangle gives rise by adjunction to the following commutative
triangle:
XD
DD f ∗
∗ DD X
eX DD
D!
Y S / XS
S
mX
If ∗
fX has a retraction r, then r ◦ mSX
is immediately a retraction for e∗X . Thus
∗
we turn to the construction of such a retraction for fX . The upper right triangle
of (5) gives rise again by adjunction to the following commutative triangle:
∗
(qS×X )∗
XE / (T X)S (6)
EE
EE
EE S
∗
fX EE h
"
XS
7
Let us introduce a natural transformation θ between U and T . For each object
Z, we first have
hpZ S , ǫZ i : S × Z S → S × Z
whence
θZ = hpZ S , ǫZ i∗ : Z S → T Z
as required. The triangle (6) then fits into a larger diagram:
∗
(qS×X )∗ θT X
XE / (T X)S / TTX (7)
EE
EE
∗
EE hS Th
fX EE
"
X S J θX / T X
JJ
JJ
J
r JJJ h
J$
X
where r is defined by
r = h ◦ θX
Now (7) commutes: in fact (6) commutes already, the lower right triangle com-
mutes by definition of r, and the square by naturality of θ. By defining
∗
ψ = θT X ◦ (qS×X )∗
we get
∗
r ◦ fX = h ◦ Th ◦ ψ
From the definition of a T -algebra (1), we know that h ◦ T h = h ◦ µX . We claim
that
µX ◦ ψ = ηX (8)
which implies
∗
r ◦ fX = h ◦ Th ◦ ψ
= h ◦ µX ◦ ψ
= h ◦ ηX
= idX
by (1) again, and the result reduces to (8), proved in Lemma 3. 2
∗
Lemma 3 For each object X, µX ◦ θT X ◦ (qS×X )∗ = ηX .
Proof. The lemma states the commutativity of the following diagram:
θT X
(T X)S / TTX
: (9)
O uu
∗ ∗ uu ψ
(qS×X ) uu µX
uu
uu
X / TX
ηX
8
where the upper left triangle commutes by definition of ψ. The first step is
∗
to note that, as θT X = hpT X S , ǫT X i , ψ is of the form φ∗ , where φ makes the
following triangle commute:
hpT X S ,ǫT X i
S × (T X)S / S × TX (10)
O p p8
∗
S×(qS×X )∗ p p pp
pp φ
ppp
S×X
S × XI (12)
pX ww II q∗
w IIS×X
www φ II
II
w $
{oww
S pT X S × T X qT X / T X
∗
S×(qS×X )∗ hpT X S ,ǫT X i
S × XM / S × (T X)S / S × TX (13)
MMM pp p
MMM pT X S p pp
pX MMMM p
pp pT X
M& xpppp
S
∗
S×(qS×X )∗ hpT X S ,ǫT X i
S × XM / S × (T X)S / S × TX (14)
MMM p p
MMM ǫ p p pp
∗
qS×X
MMM T X ppp qT X
M& xpp
TX
9
This achieves the proof of (11).
Back to (9), we now prove that the right lower triangle commutes; we just
proved that this triangle is in fact:
T< T X (15)
hpX ,qS×X
∗
i∗ xxx
x µX
xx
xx
X ηX / T X
∗
Let ∆ = hidS , idS i : S → S × S, we may express pX , qS×X as a compostion:
∗
∗
pX , qS×X = (S × qS×X ) ◦ (∆ × X) (17)
such that the commutativity of (16) reduces to the commutativity of the fol-
lowing diagram:
∗
S×qS×X
S × SO × X / S × TX (18)
S×∆ ǫS×X
S×X / S×X
idS×X
Finally
qS×X ◦ (∆ × X) = idS×X
and the lemma is proved. 2
10
Lemma 4 Suppose hX, hi is a T -algebra, and the epimorphism eX has a sec-
tion. Then e∗X also has a section.
Proof. Let σ be a section of eX , that is
eX ◦ σ = idY (19)
and define
Σ = h ◦ σS (20)
we claim that Σ is a section of e∗X . Consider the following diagram:
∗ ∗
σS (qS×T X)
YSB / TX / (T T X)S (21)
BB
BB
B h (T h)S µS
X
Σ BBB
X / (T X)S
∗ ∗
(qS×X )
e∗
X hS
YS / XS
mS
X
= hS ◦ µSX ◦ (qS×T
∗ ∗
X) ◦ σ
S
µSX ◦ (qS×T
∗ ∗ ∗ ∗
X) = (µX ◦ qS×T X)
∗
= (ǫSS×X ◦ qS×T
∗
X)
= (qS×X ◦ ǫS×X )∗
∗
= ∗
(qS×X )S ◦ ǫ∗S×X
∗ S ∗
= (qS×X ) ◦ ((idT X )∗ )
∗ S
= (qS×X )
so that
S
mSX ◦ e∗X ◦ Σ = hS ◦ (qS×X
∗
) ◦ σS
= mSX ◦ eSX ◦ σ S
= mSX
and finally
e∗X ◦ Σ = idY S (22)
11
because mSX is a monomorphism (right-adjoints preserve monomorphisms). 2
Now eX was defined as a regular epimorphism, which does not imply the ex-
istence of a section: think for example at the presheaf category of graphs, where
all epimorphisms are regular, many of them without a section. Nevertheless, a
very simple condition on S will be sufficient: recall that a (global) element of
an object Z in C is an arrow z : 1 → Z, where 1 is the terminal object, then
Lemma 5 Suppose S has at least one element and hX, hi is a T -algebra. Then
eX has a section.
γZ : Z S → Z
Let us now consider an algebra hX, hi, and build the following diagram:
γS×T X
S × T X o∗ / T TO X
q S×T X
S×h Th ηT X
γS×X
S ×X o ∗ / TX O
q S×X
eX h ηX
Y /X
mX
Define
σ = γS×X ◦ ηX ◦ mX (24)
we claim that σ is a section of e. To see this, we compute
mX ◦ e X ◦ σ ◦ e X (25)
mX ◦ eX ◦ σ ◦ eX = h ◦ ǫST X ◦ qS×T
∗ ∗
X ◦ γS×T X ◦ ηT X ◦ qS×X
12
and by using naturality of q∗ and γ again, (25) reduces to:
∗
h ◦ qS×X ◦ γS×X ◦ ǫST X ◦ ηT X ◦ qS×X
∗
(26)
Now
ǫST X ◦ ηX = µX ◦ ηT X = idT X
and
∗
γS×X ◦ qS×X = idS×X
so that finally
∗
mX ◦ e X ◦ σ ◦ e X = h ◦ qS×X
= mX ◦ e X
As mX is a monomorphism, and eX an epimorphism, this implies
eX ◦ σ = idY
which ends the proof. 2
h ǫS
Y ǫS
X
X / YS / XS
e∗
X mS
X
We must show that the left hand side square commutes. First, by using the
adjunction, together with (1), (4) and the fact that q ∗ is natural,
mSX ◦ e∗X ◦ h = (mX ◦ eX )∗ ◦ h
= (mX ◦ eX ◦ (S × h))∗
∗
= (h ◦ qS×X ◦ (S × h))∗
∗ ∗
= (h ◦ T h ◦ qS×T X)
∗ ∗
= (h ◦ µX ◦ qS×T X)
= (h ◦ ǫSS×X ◦ qS×T
∗
X)
∗
∗
= (h ◦ qS×X ◦ ǫS×X )∗
13
then the same ingredients plus the facts that T is a functor, and ǫS is natural
imply:
whence
mSX ◦ e∗X ◦ h = mSX ◦ ǫSY ◦ T (e∗X )
and because mSX is a monomorphism,
ǫY ǫS
Y
Y / YS
∗
qY
s0 : 1 → S
14
which easily provides a retraction for qY∗ , and a section for ǫY . In particular,
qY∗ is a monomorphism and ǫY a split — thus regular — epimorphism. Hence
there is a unique isomorphism ξY making the following diagram commutative:
eY S
S×YS / LKY
tt t:
ǫY
t m
tξY YS
tttt
Y / YS
q∗
Y
4 Equations
As we pointed out in the introduction, the present work is strongly related to the
analysis of global states given in [5], with a significant difference: in [5], C is any
category with countable products and coproducts, and S is a countable set of
states, so that S × X (resp. X S ) now denotes the coproduct (resp. the product)
of S copies of X, and not as in cartesian closed categories the internal product
(resp. exponential) by an object S. Suppose however that C satisfies both sets
of conditions, those in [5] and those of the present paper, as for example will be
the case of any presheaf category: then a countable set S can be embedded as
an object of C, coproduct of S copies of the terminal element, and the results
of [5] still make sense in our setting, and the two possible interpretations of the
notations S × − and (−)S coincide.
In particular, under these additional hypotheses, we may revisit the equa-
tional presentation of global state algebras. Let Σ be the signature consisting
of a symbol l of arity S, and for each s ∈ S, a symbol us , of arity 1; let E be
the following set of equations among terms generated by Σ:
A hΣ, Ei-algebra in C is now a pair hA, |.|A i where A is an object of C, and |.|A
assigns to each symbol in Σ an arrow of C of appropriate arity:
|l|A : AS → A
|us |A : A→A
15
in such a way that the equations in E are satisfied.
Examples of hΣ, Ei-algebras are objects of the form B S , with the following
interpretation of Σ: |l|A is defined by
≃ Bδ
(B S )S / B S×S / BS
Bs /B B! / BS
BS
Remark
Because we deal with global states only, we take here the set of locations L as
a singleton, which reduces the seven equations of [5], section 3 to the first four.
Acknowledgement
Many thanks to Albert Burroni for numerous illuminating conversations on
monadicity.
References
[1] J. Beck. Triples, algebras and cohomology. Reprints
in Theory and Applications of Categories, 2:1–59, 2003.
http://www.tac.mta.ca/tac/reprints/articles/2/tr2abs.html.
[2] S. Mac Lane. Categories for the Working Mathematician. Springer, 1971.
16