【问题标题】:Frege trace not printing弗雷格跟踪不打印
【发布时间】:2016-08-13 23:46:22
【问题描述】:

正如标题所说,由于某种原因,传递给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 的控制台中用红色文本表示)。

【问题讨论】:

    标签: haskell trace stderr frege


    【解决方案1】:

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

    如果您将debug 函数更改为:

    debug msg f = trace msg `seq` f
    

    或者到这个:

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

    由于刷新,它仍然不会打印任何内容,因此如果您将main 更改为刷新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 在函数被调用时自动刷新它。

    【讨论】:

    • 也可以使用traceLn 使其“实时”打印
    【解决方案2】:

    我把debug改成了:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 2013-10-05
      • 2013-09-03
      • 1970-01-01
      • 2015-07-27
      • 2021-11-27
      • 1970-01-01
      相关资源
      最近更新 更多