Chris Stryczynski

Software Developer / Consultant

Tips for debugging Haskell code

Posted on: 16/03/2018

Console logging

I find traceM one of the most useful debugging functions from Debug.Trace.

import Debug.Trace
import Data.List

...
    traceM $ do
      mconcat $ intersperse "\n" $ 
        [ ""
        , k1
        , k2
        , show $ zz1
        , show $ zz1
        , show i
        ]

If you’re tracing / printing out debug lines, due to the lazy nature everything seems to occur out of order… You can enable these two pragmas to force it to be more strict:

{-# LANGUAGE Strict #-}
{-# LANGUAGE StrictData #-}

Purity hassles

import System.IO.Unsafe
  () <- return $ unsafePerformIO $! do
    print "?????????????"
    print x
    print x'

Improved formatting

pretty-simple library is really helpfull to have things more human readable

import Text.Pretty.Simple (pPrint)

Conditional tracing

mydbg :: Show a => String -> a -> a
-- mydbg'' s = traceShow
--   <$> ((++) s . show)
--   <*> id
mydbg _ = id
Comments

No comments, yet!

Submit a comment