Proposal: Add Text.Read.maybeRead :: Read a => String -> Maybe a

Ross Paterson ross at
Fri Feb 15 19:38:21 EST 2008

On Fri, Feb 08, 2008 at 09:35:34PM +0100, Twan van Laarhoven wrote:
> David Menendez wrote:
>>> Isn't the MonadPlus approach also by-default-safe?
>> Safe, yes, but is it more useful?
> Yes. In this case, take a parsing monad for example. You could write:
>   parseInt :: CharParser () Int
>   parseInt = do ds <- many digit
>                 readM ds
> And it would work automatically. A reading error would be propagated to 
> the parser monad, and it would backtrack/report the error/whatever.

In this example a read error indicates a bug, which you'd want to treat
differently from a syntax error in the input.

If one were using read as part of the parsing process, one could give
better messages with something like

    parseInt :: CharParser () Int
    parseInt = do ds <- many alphaNum
                  fromMaybe (fail "integer expected") $
                      maybeRead ds

More information about the Libraries mailing list