1

正如标题所说,由于某种原因,传递给trace(嗯,它的一个变体)函数的消息在调试函数时没有正确显示。简单地刷新 stdout/stderr 似乎也无济于事。

-- Makes it more like Haskell's trace
debug :: String -> α -> α
debug msg f = const f $ trace msg

-- dummy function
polyA :: (Num α) => α
polyA = debug "polyA\n" 0

-- another dummy function
polyB :: (Num α) => α
polyB = debug "polyB\n" polyA

main :: IO ()
main = do println (polyB :: Int    )
          println (polyB :: Int    )
          println (polyB :: Integer)

输出只是

0
0

在 stderr 中看不到任何东西(通常由 Eclipse 控制台中的红色文本表示)。

4

2 回答 2

2

我改为debug

debug :: String -> α -> α
debug msg f = if trace msg then f else f

并将输出发送到标准错误。

于 2016-08-14T00:43:43.823 回答
2

Asconst不使用第二个参数,trace不会被调用。您可以使用seq或模式匹配。

如果您将debug功能更改为:

debug msg f = trace msg `seq` f

或对此:

debug msg f | trace msg = undefined
            | otherwise = f

由于冲洗,它仍然不会打印任何内容,因此如果您将其更改main为 flush stderr

main :: IO ()
main = do println (polyB :: Int    )
          println (polyB :: Int    )
          println (polyB :: Integer)
          stderr.flush

它会工作,并在最后打印所有调试消息:

frege> main
0
0
0
polyA
polyB
polyA
polyB
polyA
polyB
()

正如 Ingo 提到的,我们也可以traceLn在调用函数时自动刷新它。

于 2016-08-14T00:44:03.133 回答