문제

Consider the following contrived example :

module Main where
myadd3 first second third = 
  let result1 = first               -- line 3
      result2 = second              -- line 4
      result3 = third               -- line 5
  in result1 + result2 + result3    -- line 6

I would like to inspect the values of result1, result2, and result3 by the time I reach line 6. However when I step through the code, at no point do any of these "computations" appear to be available for inspection :

*Main> :break 3
Breakpoint 0 activated at Main4.hs:3:17-21
*Main> :break 4
Breakpoint 1 activated at Main4.hs:4:17-22
*Main> :break 5
Breakpoint 2 activated at Main4.hs:5:17-21
*Main> :break 6
Breakpoint 3 activated at Main4.hs:6:6-32
*Main> myadd3 2 3 4
Stopped at Main4.hs:6:6-32
_result :: a = _
result1 :: a = _
result2 :: a = _
result3 :: a = _
[Main4.hs:6:6-32] *Main> :step
Stopped at Main4.hs:6:6-22
_result :: a = _
result1 :: a = _
result2 :: a = _
[Main4.hs:6:6-22] *Main> :step
Stopped at Main4.hs:3:17-21
_result :: Integer = _
first :: Integer = 2
[Main4.hs:3:17-21] *Main> :step
Stopped at Main4.hs:4:17-22
_result :: Integer = _
second :: Integer = 3
[Main4.hs:4:17-22] *Main> :step
Stopped at Main4.hs:5:17-21
_result :: Integer = _
third :: Integer = 4
[Main4.hs:5:17-21] *Main> :step
9

That is we have lines like result1 :: a = _ but not something like result1 :: a = 2. Is this because of the lazy nature of haskell?, and if so is there a way to inspect result1, result2, and result3 at the moment values are bound to them? Thanks

도움이 되었습니까?

해결책

You're correct that the behavior is due to laziness. To force evaluation you can do:

[debug.hs:6:6-32] *Main> :print result1
result1 = (_t1::a)
[debug.hs:6:6-32] *Main> :force result1
*** Ignoring breakpoint
result1 = 2
[debug.hs:6:6-32] *Main> :step
Stopped at debug.hs:6:6-22
_result :: Integer = _
result1 :: Integer = 2
result2 :: Integer = _
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top