English 中文(简体)
跟踪Haskell的错误
原标题:Tracking down errors in Haskell
  • 时间:2010-01-26 13:02:53
  •  标签:
  • haskell

我怎么能获得更多关于发生了黑星错误的信息? 例如,昨天,我正在实施一项“Haskell”方案,该方案将投入文件编成册,改变数据,然后印刷报告信息。

在某个时候,我跑了“主人”回来。

*** Prelude.read: parse error

没有其他信息。 幸运的是,我知道我只读过一个地方,并且能够固定下来,但将来:

  • Is it possible to get a backtrace or a line number for errors like these?
  • Is it possible to get the actual data that triggered the error, i.e. the string that caused the parse error?

感谢!

<><>Edit>/strong> 利用GHC。

最佳回答

you can get the string that caused the parse error by importing Debug.Trace and changing your call

import Debug.Trace (trace)

--change
myRead s = read s
--to 
myRead s = trace s (read s)
--or 
myRead s = trace (take 100 s) (read s)
问题回答

如果你能够以格西实施该守则,那么 de子可以做你想要的一切。 这里的方案提出例外情况

foo s i
  | i == 57 = read s
  | otherwise = i
main = mapM_ (print . foo "") [1..100]

Now load it into ghci and use the debugger, as documented here: http://www.haskell.org/ghc/docs/latest/html/users_guide/ghci-debugger.html#ghci-debugger-exceptions

> ghci test.hs
*Main> :set -fbreak-on-error
*Main> :trace main
1
2
... snipped 3 through 55 ...
56
Stopped at <exception thrown>
_exception :: e = _
[<exception thrown>] *Main> :back
Logged breakpoint at test.hs:2:15-20
_result :: a
s :: String
[-1: test.hs:2:15-20] *Main> :list
1  foo s i
2    | i == 57 = **read s**
3    | otherwise = i
[-1: test.hs:2:15-20] *Main> s
""
[-1: test.hs:2:15-20] *Main> 

它让你在评价史上走过一步,强调提出例外(bold,而不是在终点站挨饿)的实际表述,并允许你检查当地变量。

Another option is to recompile with profiling and some flags to tag appropriate cost centers, and run with the -xc profiling option which prints the cost center stack on uncaught exceptions http://www.haskell.org/ghc/docs/latest/html/users_guide/prof-time-options.html

> ghc -prof -auto-all test.hs
> ./test +RTS -cs
1
2
... snipped 3 through 55 ...
56
*** Exception (reporting due to +RTS -xc): (THUNK_2_0), stack trace: 
  Main.foo,
  called from Main.main,
  called from Main.CAF
  --> evaluated by: Main.main,
  called from Main.CAF
test: Prelude.read: no parse

The reason this is a bit difficult is described a bit earlier on the debugger page http://www.haskell.org/ghc/docs/latest/html/users_guide/ghci-debugger.html#tracing Basically, efficient Haskell execution doesn t use anything resembling a normal call stack, so to get that kind of information on an exception you have to be running in some special mode (debugging or profiling) which does keep that sort of information.

总的来说,应由你处理错误,这样你就有足够的环境来回避这一原因。

哈斯凯尔的zy性使得难以执行,因为在发生错误时可能不再存在呼吁。

一种简单的错误处理方式是,在出现错误时,使用允许你回馈价值的任何类型,或者在某种情况下(电传、显示投入......)。

最后,在你的具体案例中,read就是一个例外,因此,你必须赶上这一条,然后处理打字码中的错误(见<编码>Control.Exception。





相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签