【问题标题】:Why does GHC infer type of `IO b` for an application of `hSetBuffering`?为什么 GHC 会为 `hSetBuffering` 的应用程序推断 `IO b` 的类型?
【发布时间】:2011-01-21 22:30:10
【问题描述】:

我正在尝试在使用 GHC 6.10 的 Haskell 程序中使用 hSetBuffering。 当我尝试这个非常简单的程序时:

module Ctlc
where

import IO

main :: ()
main = do hSetBuffering stdout NoBuffering
          return ()

我收到一条令人费解的错误消息:

ctlc.hs:8:10:
    Couldn't match expected type `()' against inferred type `IO b'
    In a stmt of a 'do' expression: hSetBuffering stdout NoBuffering
    In the expression:
        do hSetBuffering stdout NoBuffering
           return ()
    In the definition of `main':
        main = do hSetBuffering stdout NoBuffering
                  return ()

我不明白为什么 GHC 推断出 IO b 的类型,因为 ghci 声称

Prelude Data.List IO> :t hSetBuffering
hSetBuffering :: Handle -> BufferMode -> IO ()

回答:我愚蠢地在main 上输入了错误的类型。感谢ja 的敏锐眼睛。

【问题讨论】:

  • 我确实想知道为什么 GHC 不会更早地失败:() 不是 monad,所以 do { ... } :: () 是不可能的,即使 hSetBuffering stdout NoBuffering :: ()。如果错误是“can't match () against IO () in main = do ...”,您可能已经注意到缺少的IO

标签: haskell typeerror


【解决方案1】:

您已将 main 声明为 () 类型,而不是 IO ()。

【讨论】:

  • 非常感谢如此。绝对是键盘和椅子之间的飞行员错误。 +1,我显然需要考虑睡觉...
【解决方案2】:

该函数必须具有某种 IO 类型,因为该实现将进行系统调用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 2019-08-19
    • 2014-02-11
    • 1970-01-01
    相关资源
    最近更新 更多