[Haskell-cafe] The danger of Monad ((->) r)

Tomasz Zielonka tomasz.zielonka at gmail.com
Tue May 15 06:05:17 EDT 2007


Hello!

In a CGI application I was gathering SQL statements I wanted to run in
the final transaction. Because I use haskelldb, it was most convenient
to use (Database -> IO ()) as the type of the statement or a group of
statements. In this representation concatenating two statement groups so
they are executed in sequence can be done with:

    concatStmts s1 s2 = \db -> s1 db >> s2 db

My mistake was that I forgot about db and wrote:

    concatStmts s1 s2 = s1 >> s2

And it was accepted because I had the Monad instance for ((->) r) in
scope (from Control.Monad.Trans I guess)!

Of course it doesn't do the right thing - s1 is simply ignored.
Fortunately I noticed it quite quickly, but I think a similar bug can
cause someone a big headache.

Perhaps we could move this instance to some separate module, so people
won't import it if they don't want it or even don't know about its
existence?

Best regards
Tomek


More information about the Haskell-Cafe mailing list