Newbie qustion about monads

Juanma Barranquero jmbarranquero at laley.wke.es
Thu Oct 2 13:59:25 EDT 2003


On Thu, 02 Oct 2003 11:22:13 +0200
"Marcin 'Qrczak' Kowalczyk" <qrczak at knm.org.pl> wrote:

> As you discovered, there is no meaningful count of operations. If an
> operation doesn't do anything, do you count it?

It's not about counting the operations (that's just an example), but
accumulating any kind of state. For example:

  data Accum a = Ac [a] a

  instance Monad Accum where
      return x     = Ac [x] x
      Ac _ x >>= f = let Ac l x' = f x in Ac (l ++ [x']) x'

  dupAcc x = Ac [x,x] x

  m1 = (return 'a') >>= dupAcc   -- Ac "aaa" 'a'
  m2 = dupAcc 'a'                -- Ac "aa"  'a'

> but monad laws say that "return" *really* doesn't do anything

I don't see it. As I understand, the problem (if any) would be in my
implementations of either >>= or f (dupAcc, in this case). I get the
same values for m1 and m2 even if I define return x = Ac [] x.

I'm not trying to create useful monads (I'm pretty sure they aren't :),
but understanding the concepts. So, the question remains: when the monad
laws say that

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

what is intended in that "=="? Eq."==" for

  instance Eq MyCustomMonad where x == y = whatever...

or a more profound kind of equality?


                                                                Juanma




More information about the Haskell-Cafe mailing list