[Haskell-beginners] Using Debug.Trace

Patrick LeBoutillier patrick.leboutillier at gmail.com
Sun Aug 8 13:14:41 EDT 2010


Hi all,

I'm writing a parser for a binary format, and I'm trying to debug it
since I have a bug and the code is getting lost in the bits and bytes.
Basically my main is like this:

  import Data.Binary.Get
  import Debug.Trace
  import qualified Data.ByteString.Lazy as L

  main = do
    bytes <- L.readFile "song.gp4"
    let (version, bytes') = getVersion bytes
    putStrLn version

    let stuff = runGet (getDocument version) bytes'
    putStrLn $ show stuff

    return ()

The getVersion and getDocument functions use the Data.Binary.Get monad
to decode the byte string into various objects.
I tried sprinkling "trace" calls a bit everywhere and I realize they
don't always get called at the expected time. Most of them are only
called when I reach the "putStrLn $ show stuff" statement.
Unfortunately that doesn't help me because I get a

  *** Exception: too few bytes. Failed reading at byte position 35939

before that.

Is this happening because I'm using lazy IO to read the file?
Is there a way to force the evaluation of these "trace" calls?
Are there any other ways to debug this kind of stuff in Haskell?


Thanks a lot,

Patrick




-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada


More information about the Beginners mailing list