[Haskell-cafe] Re: how do you debug programs?

Jón Fairbairn jon.fairbairn at cl.cam.ac.uk
Wed Sep 6 05:58:17 EDT 2006

Tamas K Papp <tpapp at Princeton.EDU> writes:

> I would like to learn a reasonable way (ie how others do it) to debug
> programs in Haskell.  Is it possible to "see" what's going on when a
> function is evaluated?  Eg in
> f a b = let c = a+b
>       	    d = a*b
> 	in c+d
> evaluating 
> f 1 2
> would output something like
> f called with values 1 2
> c is now (+) a b => (+) 1 2 => 3
> d is now (*) a b => (*) 1 2 => 2

But why should c and d exist at runtime? They're only used
once each, so the compiler is free to replace f with 

\a b -> (a+b)+ a*b

and indeed, it's quite likely that f should disappear
too. So if your debugger outputs what you want, it's not
debugging the programme that you would otherwise have run.

> Or maybe I am thinking the wrong way, and functional programming has
> its own debugging style...

I've said this before, but I think it's worth repeating: in
most cases, if you need to debug your programme it's because
it's too complicated for you to understand, so the correct
thing to do is to simplify it and try again. That's not to
say that I don't think you should test programmes... in fact
I'm all for testing little bits. You can define f above and
try it on a few arguments in ghci or hugs, and you can use
QuickCheck and/or HUnit to automate this. But there's no
reason for you to care what happens while f is evaluating.

Remember there's very little (often no) overhead for
defining subsidiary functions, so break complicated stuff
into parts you can understand.

Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk
http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html  (updated 2006-07-14)

More information about the Haskell-Cafe mailing list