Skip to content
Tech News
← Back to articles

Haskell: Debugging

read original get Haskell Programming Debugging Book → more articles
Why This Matters

This article highlights the importance of effective debugging tools in Haskell, such as stack traces and tracing functions, which help developers identify and resolve errors more efficiently. These tools are crucial for improving code reliability and developer productivity in the functional programming landscape, ultimately benefiting both industry and consumers by enabling more robust software development.

Key Takeaways

Stack trace

General usage

Recent versions of GHC allow a dump of a stack trace (of all cost centres) when an exception is raised. In order to enable this, compile with -prof , and run with +RTS -xc . (Since only the cost centre stack will be printed, you may want to add -fprof-auto -fprof-cafs [1] to the compilation step to include all definitions in the trace.) Since GHC version 7.8, the function errorWithStackTrace can be used to programmatically dump the stack trace, see How to get (approx) stack traces with profiled builds.

A more detailed list of options can be found in the RTS section of the GHC user's guide.

Example

-- test.hs crash = sum [1,2,3,undefined,5,6,7,8] main = print crash

> ghc-7.6.3 test.hs -prof -fprof-auto -fprof-cafs && ./test +RTS -xc *** Exception (reporting due to +RTS -xc): (THUNK_2_0), stack trace: GHC.Err.CAF --> evaluated by: Main.crash, called from Main.CAF:crash_reH test: Prelude.undefined

... continue reading