Juanma Barranquero jmbarranquero at laley.wke.es
Thu Oct 2 12:13:08 EDT 2003

```I have an extremely-newbie question about monads and how to interpret
the monadic laws; I asked that same question yesterday on IRC and the
answers were interesting but non-conclusive (to me anyway).

I though of defining a monad like that:

data Counted a = Ct Int a

return x     = Ct 1 x
Ct n x >>= f = let (Ct n' x') = f x in Ct (n+n') x'
-- fail as default

The intent is that Counted objects "count" the number of times an
operation is applied to them. (For the purpose of the question, that's
irrelevant anyway: the problem would be the same if I assigned a random
number to the Int on each calling of >>=.)

(return x) >>= f == f x

In my case, let's suppose I define:

reverseCounted :: [a] -> Counted [a]
reverseCounted x = return (reverse x)

so I do:

c1 = return "xxx" >>= reverseCounted   -- c1 == Ct 2 "xxx"
c2 = reverseCounted "xxx"              -- c2 == Ct 1 "xxx"

Now comes the question: In what sense should I interpret the "==" in the

Obviously, c1 and c2 are not structurally equal. If I can accept that
two Counted things are "equal" even if they are not identical, is enough
for me to define:

instance Eq a => Eq (Counted a) where
Ct _ x == Ct _ y = x == y

to satisfy the first law?

Yesterday I was said that it'd work if c1 and c2 are mutually
substitutable. They are, for the purposes of equality, though evidently
not for every purpose. A simple:

count (Ct n _) = n

allows me to distinguish between them.

Juanma

```