9

当函数的结果已知时,是否可以停止 GHCi 调试器?

例如,考虑以下代码片段:

blabla    :: [Int] -> Int
bla       :: Int -> Int
papperlap :: Int -> Int -> Int

bla x         = x+x
papperlap y x = ((y *) . bla) x
blabla xs     = foldl papperlap 0 x

现在,我想看看“papperlap”和“bla”的结果。但请记住,我想在评估结果时停止。因此使用 ':force' 是不可能的,因为它改变了评估的顺序。

当我使用 ':break' 时,调试器停止但 _result 尚未评估。请在下面找到我的 GHCi 会话,它不会产生所需的中间结果:

GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main             ( bla1.hs, interpreted )
Ok, modules loaded: Main.
*Main> :break blabla
Breakpoint 0 activated at bla1.hs:7:1-36
*Main> :break papperlap
Breakpoint 1 activated at bla1.hs:6:1-31
*Main> :break bla
Breakpoint 2 activated at bla1.hs:5:1-19
*Main> blabla [1,2,3]
Stopped at bla1.hs:7:1-36
_result :: Int = _
[bla1.hs:7:1-36] *Main> :step
Stopped at bla1.hs:7:17-36
_result :: Int = _
xs :: [Int] = [1,2,3]
[bla1.hs:7:17-36] *Main> :step
Stopped at bla1.hs:6:1-31
_result :: Int = _
[bla1.hs:6:1-31] *Main> :step
Stopped at bla1.hs:6:17-31
_result :: Int = _
x :: Int = 3
y :: Int = _
[bla1.hs:6:17-31] *Main> :step
Stopped at bla1.hs:6:1-31
_result :: Int = _
[bla1.hs:6:1-31] *Main> :step
Stopped at bla1.hs:6:17-31
_result :: Int = _
x :: Int = 2
y :: Int = _
[bla1.hs:6:17-31] *Main> :step
Stopped at bla1.hs:6:1-31
_result :: Int = _
[bla1.hs:6:1-31] *Main> :step
Stopped at bla1.hs:6:17-31
_result :: Int = _
x :: Int = 1
y :: Int = 0
[bla1.hs:6:17-31] *Main> :step
Stopped at bla1.hs:5:1-19
_result :: Int = _
[bla1.hs:5:1-19] *Main> :step
Stopped at bla1.hs:5:17-19
_result :: Int = _
x :: Int = 1
[bla1.hs:5:17-19] *Main> :step
Stopped at bla1.hs:5:1-19
_result :: Int = _
[bla1.hs:5:1-19] *Main> :step
Stopped at bla1.hs:5:17-19
_result :: Int = _
x :: Int = 2
[bla1.hs:5:17-19] *Main> :step
Stopped at bla1.hs:5:1-19
_result :: Int = _
[bla1.hs:5:1-19] *Main> :step
Stopped at bla1.hs:5:17-19
_result :: Int = _
x :: Int = 3
[bla1.hs:5:17-19] *Main> :step
0
*Main>
4

2 回答 2

3

对于您的直接调试问题可能有点晚了,但这是我要做的:

blabla    :: [Int] -> Int
bla       :: Int -> Int
papperlap :: Int -> Int -> Int

bla x         = (undefined :: Int)
papperlap y x = ((y *) . bla) x
blabla xs = foldl papperlap 0 xs

现在我们知道我们会在评估bla. 所以让我们进入 ghci:

[1 of 1] Compiling Main             ( temp.hs, interpreted )
Ok, modules loaded: Main.
λ: :set -fbreak-on-exception
λ: :trace blabla [1,5,17]
Stopped at <exception thrown>
_exception :: e = _
λ: :hist
-1  : bla (temp.hs:6:17-35)
-2  : bla (temp.hs:6:1-35)
-3  : papperlap (temp.hs:7:17-31)
-4  : papperlap (temp.hs:7:1-31)
-5  : papperlap (temp.hs:7:17-31)
-6  : papperlap (temp.hs:7:1-31)
-7  : papperlap (temp.hs:7:17-31)
-8  : papperlap (temp.hs:7:1-31)
-9  : blabla (temp.hs:8:13-32)
-10 : blabla (temp.hs:8:1-32)
<end of history>
λ: :back
Logged breakpoint at temp.hs:6:17-35
_result :: a
λ: :list
5
6  bla x         = (undefined :: Int )
                   ^^^^^^^^^^^^^^^^^^^
7  papperlap y x = ((y *) . bla) x
λ: :back
Logged breakpoint at temp.hs:6:1-35
_result :: a
λ: :list
5
6  bla x         = (undefined :: Int )
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7  papperlap y x = ((y *) . bla) x
λ: :back
Logged breakpoint at temp.hs:7:17-31
_result :: Int
x :: Int
y :: Int
λ: :list
6  bla x         = (undefined :: Int )
7  papperlap y x = ((y *) . bla) x
                   ^^^^^^^^^^^^^^^
8  blabla xs = foldl papperlap 0 xs

因此我们可以看到导致 的右侧求值的堆栈bla

这并不完美,因为在undefined任何地方都添加 s 看起来很俗气和 hacky,但:hist已经给了你很多工作,并且:list在你后退时使用会让事情变得更加清晰。

于 2013-09-19T22:29:52.150 回答
1

Haskell 不会为您逐一介绍文字表达式。就像是

e = 2*(2+2)

将立即计算为 8,因为编译器将优化由文字组成的任何表达式,它可以在编译时找到“放置”(这种类型的表达式称为常量应用形式)。

现在你必须意识到这foldl是懒惰的。如果您调用foldl f列表,它不会执行单个评估,f直到它绝对被迫这样做。

>foldl (+) 0 [1,2,3] 
>foldl (+) a1 [2,3]
     where a1 = 0+1
>foldl (+) a2 [3]
     where a2 = a1+2
           where a1 = 0+1

最终我们有了((0+1)+2)+3),编译器说“好的,我们已经用尽了每个列表并将每个评估扩展为我们可以的最原始形式。让我们评估”。而且我们已经知道 Haskell不会逐步评估 CAF。

如果您想查看评估的中间值,您必须首先实际生成它们。你这样做的方式是以下严格的变体foldl

foldl' f z []     = z
foldl' f z (x:xs) = let z' = z `f` x 
                    in seq z' $ foldl' f z' xs

我会让你弄清楚它是如何工作的,但seq a ba在继续懒惰评估之前进行全面评估b

像以前一样做其他所有事情,除了更改foldlfoldl'. 当您逐步执行评估时,当您在函数内暂停时,您将看到中间foldl'值。

于 2013-05-29T20:34:37.380 回答