我怎么知道我没有完全应用此ghc错误消息中的函数?

时间:2011-11-07 11:21:41

标签: haskell monads currying

我收到了来自ghc的错误消息,表示我没有发现这些消息,并将我的代码缩减为:

import System.Process

main = do
  (_, out, _) <- readProcessWithExitCode "echo" ["foo"]
  putStr out

(我应该给readProcessWithExitCode一个额外的参数)。使用runghc编译破坏的程序给出:

Test.hs:4:2:
    Couldn't match expected type `IO
                                    (GHC.IO.Exception.ExitCode, String, String)'
           against inferred type `(a, b, c)'
    In the pattern: (_, out, _)
    In a stmt of a 'do' expression:
        (_, out, _) <- readProcessWithExitCode "echo" ["foo"]
    In the expression:
        do { (_, out, _) <- readProcessWithExitCode "echo" ["foo"];
             putStr out }

我怎么知道我未能完全应用此ghc错误消息中的函数?

1 个答案:

答案 0 :(得分:10)

如果您指定了类型签名main :: IO (),则错误将变为:

test.hs:5:18:
    Couldn't match expected type `IO t0'
                with actual type `String
                                  -> IO (GHC.IO.Exception.ExitCode, String, String)'
    In the return type of a call of `readProcessWithExitCode'
    In a stmt of a 'do' expression:
        (_, out, _) <- readProcessWithExitCode "echo" ["foo"]
    In the expression:
      do { (_, out, _) <- readProcessWithExitCode "echo" ["foo"];
           putStr out }

在这种情况下,错误更加明显。

我的猜测是错误并不明确,因为在推断类型时,GHC认为你在函数(即(a->))monad而不是IO,在这种情况下你将应用之后的争论。

所以不要懒惰,并包括那些类型签名! ; - )