Proposal: Improved error message of "Error in array index"

John Meacham john at
Tue Nov 8 04:18:47 EST 2005

jhc has a SRCLOC_ANNOTATE pragma to solve just this problem

here is the relevant part of the docs:

 * SRCLOC_ANNOTATE pragma. This is a generalization of GHCs assert
   magic. A function which is given an SRCLOC_ANNOTATE pragma will be
   replaced with an alternate version that takes the functions use site
   as an argument. This allows error messages to be in terms of where
   said function was used. The alternate function is named
   [function]_srcloc_ann__ and must be in the same module as the
   original function. Jhc does no checking to ensure both functions have
   the same effect, it is up to the user to ensure that. An example is

>   head :: [a] -> a
>   head (x:xs) = x
>   head [] = error "head - empty list"
>   {-# SRCLOC_ANNOTATE head #-}
>   head_srcloc_ann__ :: String -> [a] -> a
>   head_srcloc_ann__ pos (x:xs) = x
>   head_srcloc_ann__ pos [] = error $ pos ++ ": head - empty list"
>   -- Now, a call to head on an empty list will produce an error message like
>   -- "Main.hs:23: head - empty list"

much nicer than implicit parameters IMHO and it is up to the writer of
the library to decide how far up they care to propagate srclocs from so
the compiler need not make an arbitrary decision. (and it is pretty
dead-simple to implement)


John Meacham - ⑆⑆john⑈ 

More information about the Glasgow-haskell-users mailing list