Georg Sauthoff | 19 Dec 17:51

How to make Prelude.read: no parse more verbose ...

Hi,

I try to debug some existing Haskell-Code. Out of the blue I get a
'progname: Prelude.read: no parse'
error message from GHC.

Great.

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 ...

Best regards
Georg

--

-- 
Fortune : 'Real programmers don't comment their code.
           It was hard to write, it should be hard to understand.' ;)
Favicon

Re: How to make Prelude.read: no parse more verbose ...


On Dec 19, 2007, at 11:53 , Georg Sauthoff wrote:

> I try to debug some existing Haskell-Code. Out of the blue I get a
> 'progname: Prelude.read: no parse'
> error message from GHC.

If you can install GHC 6.8.x, you can use ghci's interactive  
debugger.  See http://cgi.cse.unsw.edu.au/~dons/blog/2007/11/14 for  
an example.

--

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery <at> kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery <at> ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH
Ketil Malde | 19 Dec 20:50

Re: How to make Prelude.read: no parse more verbose ...

Georg Sauthoff <g_sauthoff <at> web.de> writes:

> 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.

It's dirty, it's mean, but you can use CPP.  (On one line, and with
ghc -cpp):

#define read (\s -> case [ x | (x,t) <- reads s, ("","") <- lex t] of
 { [x] -> x ; 
   [] -> error("read: no parse at "++__FILE__++":"++show __LINE__); 
   _ -> error("read: no parse at "++__FILE__++":"++show __LINE__)})

-k
--

-- 
If I haven't seen further, it is by standing in the footprints of giants
Neil Mitchell | 19 Dec 21:17

Re: How to make Prelude.read: no parse more verbose ...

Hi

> > 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.

I use the Safe library to do this sort of stuff:

http://www-users.cs.york.ac.uk/~ndm/safe/

You can call readMay to get a maybe result, or readNote which gives an
augmented error message on a crash. You can of course combine this
with the CPP trick:

#define read readNote (__FILE__++":"++show __LINE__)

Thanks

Neil
Jeff Polakow | 19 Dec 21:28
Favicon

Re: How to make Prelude.read: no parse more verbose ...


Hello,

  You can also just use reads which returns a list of (partial) parses.

-Jeff

haskell-cafe-bounces <at> haskell.org wrote on 12/19/2007 03:17:39 PM:

> Hi
>
> > > 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.
>
> I use the Safe library to do this sort of stuff:
>
> http://www-users.cs.york.ac.uk/~ndm/safe/
>
> You can call readMay to get a maybe result, or readNote which gives an
> augmented error message on a crash. You can of course combine this
> with the CPP trick:
>
> #define read readNote (__FILE__++":"++show __LINE__)
>
> Thanks
>
> Neil
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe <at> haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

---

This e-mail may contain confidential and/or privileged information. If you
are not the intended recipient (or have received this e-mail in error)
please notify the sender immediately and destroy this e-mail. Any
unauthorized copying, disclosure or distribution of the material in this
e-mail is strictly forbidden.
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Georg Sauthoff | 21 Dec 15:18

Re: How to make Prelude.read: no parse more verbose ...

Ketil Malde <ketil+haskell <at> ii.uib.no> wrote:
> Georg Sauthoff <g_sauthoff <at> web.de> writes:

>> 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.

> It's dirty, it's mean, but you can use CPP.  (On one line, and with
> ghc -cpp):
[..]

Thanks! Indeed, it looks mean, but it helps, because I am
currently using ghc < 6.8 ...

Best regards
Georg Sauthoff
--

-- 
Fortune : 'Real programmers don't comment their code.
           It was hard to write, it should be hard to understand.' ;)
Don Stewart | 20 Dec 00:43
Gravatar

Re: How to make Prelude.read: no parse more verbose ...

g_sauthoff:
> Hi,
> 
> I try to debug some existing Haskell-Code. Out of the blue I get a
> 'progname: Prelude.read: no parse'
> error message from GHC.
> 
> Great.
> 
> 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.

    http://cgi.cse.unsw.edu.au/~dons/blog/2007/11/14#no-exceptions

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.

-- Don

Gmane