# Haskell/Debugging

* Debugging
* General Practices
* Debugging
* Testing
* Packaging your software (Cabal)
* Using the Foreign Function Interface (FFI)
* Generic programming: Scrap your boilerplate

## Debug prints with Debug.Trace

Debug prints are a common way to debug programs. In imperative languages, we can just sprinkle the code with print statements to standard output or to some as log file in order to track debug information (e.g. value of a particular variable, or some human-readable message). In Haskell, however, we cannot output any information other than through the IO monad; and we don't want to introduce that just for debugging.
To deal with this problem, the standard library provides the Debug.Trace. That module exports a function called trace which provides a convenient way to attach debug print statements anywhere in a program. For instance, this program prints every argument passed to fib when not equal to 0 or 1:
Below is the resulting output:
Also, trace makes it possible to trace execution steps of program; that is, which function is called first, second, etc. To do so, we annotate parts of functions we are interested in, like this:
When a program annotated in such way is run, it will print the debug strings in the same order the annotated statements were executed. That output might help to locate errors in case of missing statements or similar things.

### Some extra advice

As demonstrated above, trace can be used outside of the IO monad; and indeed its type signature...
...indicates that it is a pure function. Yet surely trace is doing IO while printing useful messages. What's going on? In fact, trace uses a dirty trick of sorts to circumvent the separation between IO and pure Haskell. That is reflected in the following disclaimer, found in the documentation for trace:
   The trace function should only be used for debugging, or for monitoring execution. The function is not referentially transparent: its type indicates that it is a pure function but it has the side effect of outputting the trace message.
A common mistake in using trace: while trying to fit the debug traces into an existing function, one accidentally includes the value being evaluated in the message to be printed by trace; e.g. don't do anything like this:
That leads to infinite recursion because trace message will be evaluated before bar expression which will lead to evaluation of foo in terms of trace message and bar again and trace message will be evaluated before bar and so forth to infinity. Instead of show foo, the correct trace message should have show bar:

### Useful idioms

A helper function that incorporates show can be convenient:
In a similar vein, Debug.Trace defines a traceShow function, that "prints" its first argument and evaluates to the second one:
Finally, a function debug like this one may prove handy as well:
This will allow you to write code like...
... making it easier to comment/uncomment debugging statements.

## General tips

* Debugging
* Solutions to exercises
* General Practices
* Debugging
* >> Testing
* >> Packaging your software (Cabal)
* >> Using the Foreign Function Interface (FFI)
* >> Generic programming: Scrap your boilerplate
* Haskell
* Haskell Basics
* >> Elementary Haskell
* >> Intermediate Haskell
* >> Monads
* Advanced Haskell
* >> Fun with Types
* >> Wider Theory
* >> Haskell Performance
* Libraries Reference
* >> General Practices
* >> Specialised Tasks