Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 798
Jimmy Choi Avatar asked Oct 19 '25 23:10

Jimmy Choi


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.

like image 183
Daniel Wagner Avatar answered Oct 21 '25 22:10

Daniel Wagner