Alex Rivera | Logout

Tracking down errors in Haskell

Asked 2010-01-26T13:02:53.287
16

How can I get more information about where a Haskell error has occurred? For example, yesterday I was working on a Haskell program that parses an input file, transforms the data and then prints out reporting information.

At one point, I ran "main" and got back

*** Prelude.read: parse error

with no other information. Fortunately, I knew I was calling read in only one place and was able to fix it, but for the future:

  • Is it possible to get a backtrace or a line number for errors like these?
  • Is it possible to get the actual data that triggered the error, i.e. the string that caused the parse error?

Thanks!

Edit Using GHC.

Edit
Report

2 Answers

8

you can get the string that caused the parse error by importing Debug.Trace and changing your call

import Debug.Trace (trace)

--change
myRead s = read s
--to 
myRead s = trace s (read s)
--or 
myRead s = trace (take 100 s) (read s)
answered 2010-01-26T19:47:07.480
2

You didn't tell us which compiler you are using. If you use GHC, then you should take a look at the GHCi Debugger.

Stack tracing in Haskell is not trivial, because of its laziness. Nevertheless, the aforementioned debugger provides some tools (see section 2.5.5. Tracing and history in the above URL).

answered 2010-01-26T13:17:39.873

Your Answer