How to debug infinite recursion in Haskell?

How can I debug this (obviously) buggy program using the GHC profiling tools? The program introduces infinite recursion in the second sentence frobnicate

.

-- Foo.hs
frobnicate :: Show a => Maybe a -> String
frobnicate Nothing = ""
frobnicate x       = case frobnicate x of
  "" -> "X"
  _  -> show x

main :: IO ()
main = print (frobnicate (Just "" :: Maybe String))

      

The example may look contrived, but in fact it has lost the version of the real error that I encountered today.

In an imperative language, the error will be obvious since the stack trace will say something like frobnicate -> frobnicate -> frobnicate -> ...

. But how do you find out about this in Haskell? How can you narrow down the blame for this particular feature?

I tried something similar to the following:

ghc -fforce-recomp -rtsopts -prof -fprof-auto Foo.hs
./Foo +RTS -M250M -i0.001 -h
hp2ps -c Foo.hp

      

where a flag is added -M250M

to ensure it doesn't kill the machine -i0.001

increases the frequency of profiling in an attempt to catch the overflow in action (which happens very quickly).

This creates a rather useless plot:

enter image description here

There is no obvious overflow in this plot. The y-axis does not go through a single megabyte! What am I doing wrong here?

+3


source to share





All Articles