[Haskell-cafe] How to make Prelude.read: no parse more verbose ...
dons at galois.com
Wed Dec 19 18:43:51 EST 2007
> I try to debug some existing Haskell-Code. Out of the blue I get a
> 'progname: Prelude.read: no parse'
> error message from GHC.
> Well, the code includes
> # grep '\<read\>' *| wc -l
> 23 (sic!)
> calls to the read fn.
> Well, how do I compile a Haskell program in such a way, that I
> get a useful error message from read? I mean, like the
> filename/linenumber of the calling expression for starters.
> Really crazy would be the possibility to print out a backtrace,
> if some (library) function calls error ...
ghci now has a debugger that can backtrace like this, see this
blog post for more details.
using reads or even read lifted to a monad, like this, can be
very useful, if it is code you control:
readM :: (Monad m, Read a) => String -> m a
readM s = case [x | (x,t) <- reads s, ("","") <- lex t] of
[x] -> return x
 -> fail "readM: no parse"
_ -> fail "readM: ambiguous parse"
since you can handle failure without throwing an async exception.
More information about the Haskell-Cafe