[Haskell-cafe] Debugging partial functions by rewriting ...

Donald Bruce Stewart dons at cse.unsw.edu.au
Wed Nov 15 01:14:28 EST 2006

Ok, so I took the rule rewriting idea and added a preprocessor instead, that
inserts 'assert's for you, currently just for head,tail and fromJust.

This program, for example:

    module Main where

    import qualified Data.Map as M
    import Data.Maybe

    main = do print f

    f = let m = M.fromList
            s = M.lookup 4 m :: Maybe String
        in head ([] :: [()])

Fails with:

    $ ghc A.hs --make
    [1 of 1] Compiling Main             ( A.hs, A.o )
    Linking A ...
    $ ./A
    A: Maybe.fromJust: Nothing

Add one import statement:
    import Debug.Trace.Location

And recompile with the preprocessor:

    $ ghc A.hs --make -pgmF loch -F -no-recomp
    [1 of 1] Compiling Main             ( A.hs, A.o )
    Linking A ...

And our fromJust is rewritten to a located fromJust:

    $ ./A
    A: A.hs:14:14-19: Maybe.fromJust: Nothing

Currently this only works for fromJust, head and tail. Adding user-specified
located functions should be trivial.

The 'loch' preprocessor is in the darcs repo, now.

Comments, suggestions?

-- Don

More information about the Haskell-Cafe mailing list