2

I am trying to compile a regexp and get an error message that can be presented to the user. I tried this with Text.Regex.TDFA and Text.Regex.Posix and it seems to behave the same:

Prelude Text.Regex.TDFA Data.Maybe Data.Either.Utils> fromLeft $ (makeRegexM ".[" :: Either String Regex)
"*** Exception: parseRegex for Text.Regex.TDFA.String failed:".[" (line 1, column 3):
unexpected end of input
expecting "^", "]", "-" or Failed to parse bracketed string
Prelude Text.Regex.TDFA Data.Maybe Data.Either.Utils> isJust $ (makeRegexM ".[" :: Maybe Regex)
False
Prelude Text.Regex.TDFA Data.Maybe Data.Either.Utils> isJust $ (makeRegexM "." :: Maybe Regex)
True

The Maybe monad seems to work; the Either does not. However the documentation says, it should use 'fail' - which, as far as I know, is defined in Either monad. Am I doing something wrong?

ondra
  • 9,122
  • 1
  • 25
  • 34

1 Answers1

2

The reason probably is that the monad instance for Either e recently changed. In mtl-1.*, there used to be an

instance Error e => Monad (Either e) where
    ...
    fail msg = Left (strMsg msg)  -- I may misremember the exact names

so calling fail there didn't cause an exception. Now, there is a monad instance in base (Control.Monad.Instances)

instance Monad (Either e) where
    ...
    fail s = error s  -- implicitly from the default method for fail

so you get the above.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • I thought about it. Do you know some alternative "standard" monad that has this one defined 'correctly' (for this purpose), or do I have to build my own? – ondra Dec 14 '11 at 14:35
  • I Just found this: http://stackoverflow.com/questions/5112212/is-there-no-standard-either-a-monad-instance .. but you are right, "fail" is not defined in there. – ondra Dec 14 '11 at 14:39
  • 1
    Best bet is probably `ErrorT Identity String` as a replacement of `Either String`. – Daniel Fischer Dec 14 '11 at 14:44