How can I figure out the line number where exception occured in Haskell?

Viewed 193

I'm beginner of haskell.

Now I'm struggling to resolve negative index exception.

But unlike other common language, it seems to me that haskell doesn't show line number where exception occured.

Is it possible to know the line number where exception occured?

1 Answers

These days there are basically two good ways.

  1. Liberally sprinkle HasCallStack constraints through your code, as in

     foo :: HasCallStack => Int -> [a] -> a
     foo n xs = xs !! n
    
  2. Compile with -prof and pass the -xc RTS flag, as in

     ghc -prof foo.hs && ./foo +RTS -xc
    

The latter is easier, the former gives you more control over exactly what things are considered "interesting enough" to put in a stack trace.

Related