AFAIK, есть два способа получить стек вызовов для отладки в Haskell:
- добавить
HasCallStack
ограничение в коде
- код компиляции с
ghc -prof -fprof-auto-top
Мой тестовый код:
import GHC.Stack
-- | a wrapper function to make "last" from base traceable
last' :: HasCallStack => [a] -> a
last' xs = case xs of [] -> error "abuse last"; _ -> last xs
-- | a untraceable partial function
foo :: [Int] -> Int
foo xs = last' xs + 1
-- | a untraceable partial function
-- , which looks like traceable, but it's call stack is cut off by "foo"
bar :: HasCallStack => [Int] -> Int
bar xs = foo xs
-- | an empty list
-- , which is also an improper input of "last'"
xs :: [Int]
xs = []
-- | the program is expected to print a call stack telling how "bar" is called
-- , but we can only know that "foo" called "last'" improperly from the printed call stack
main :: IO ()
main = print $ bar xs
Ниже приведен стек вызовов, который я получаю обоими вышеупомянутыми двумя способами, используя этот тестовый код:
$ ghc -prof -fprof-auto call-stack-cut-off.hs
$ ./call-stack-cut-off
call-stack-cut-off: abuse last
CallStack (from HasCallStack):
error, called at call-stack-cut-off.hs:5:29 in main:Main
last', called at call-stack-cut-off.hs:9:10 in main:Main
CallStack (from -prof):
Main.last' (call-stack-cut-off.hs:5:1-60)
Main.foo (call-stack-cut-off.hs:9:1-21)
Main.bar (call-stack-cut-off.hs:14:1-15)
Main.main (call-stack-cut-off.hs:24:1-21)
Main.CAF (<entire-module>)
IMO, стек вызовов из -prof
уже достаточно хорош и проще в использовании. Поэтому мне интересно, почему механизм HasCallStack
добавлен еще. Есть ли какая-то разница между этими двумя способами, которая существенно повлияет на процесс отладки?