2016-01-09 21 views
7

我跑了通過GHCI一些代碼,並得到這個錯誤:更具描述性的錯誤消息從GHC

*** Exception: Prelude.!!: index too large 

一會兒我去解決這一切造成的錯誤(如你所想象的,由後指數太大),但我希望GHC會告訴我這個大指數正在被評估的線上。

有沒有辦法要麼

  • A)使GHCI更冗長,或
  • B)使用,避免 這個錯誤不知何故(害羞使用較小的索引的一種常見的做法,當然)
+0

從屬打字將是一個快速修復... –

回答

5

You can use GHC's profiling facilities to get a kind of stack trace on errors,例如,假設這是你的源文件:

xs :: [Int] 
xs = [1..10] 

foo :: Int -> IO() 
foo i = print $ xs !! i 

main :: IO() 
main = mapM_ foo [1..10] 

如果編譯這個與

ghc --make -prof -fprof-auto StackTrace.hs 

然後運行它作爲

./StackTrace +RTS -xc 

那麼你得到

*** Exception (reporting due to +RTS -xc): (THUNK_1_0), stack trace: 
    GHC.List.CAF 
    --> evaluated by: Main.foo, 
    called from Main.main, 
    called from Main.CAF 
StackTrace: Prelude.!!: index too large 

至少告訴你mainfoo鏈。