【问题标题】:Matching types and monads. Error匹配类型和单子。错误
【发布时间】:2016-10-29 23:13:43
【问题描述】:
type C = StateT Int (ErrorT String IO)


main :: C ()
main = do
  args <- liftIO $ getArgs
  case args of
    [] -> liftIO $ putStrLn "E"
    (p:ps) -> s "d"

s :: String -> C ()

我遇到了错误:

     Couldn't match type ‘Control.Monad.Trans.State.Lazy.StateT
                           Int
                           (Control.Monad.Trans.Error.ErrorT String IO)
                           ()’
                   with ‘IO t0’
    Expected type: IO t0
      Actual type: C ()
    In the expression: main
    When checking the type of the IO action ‘main’

我不明白为什么会出现错误以及如何修复它。请帮忙。

【问题讨论】:

标签: haskell


【解决方案1】:

对于某些a(通常但不一定是()),main 必须是IO a 类型。它不能是StateT Int (ErrorT String IO) 类型。您可以使用 StateTErrorT 提供的函数将它们转换为底层 monad 的操作来解决此问题。

【讨论】:

  • “您可以通过使用 StateT 和 ErrorT 提供的函数将它们转换为底层 monad 的操作来解决此问题。”。什么意思?
  • @Gilgamesz 例如,runStateTrunErrorT
猜你喜欢
  • 2023-03-06
  • 2013-01-16
  • 2016-07-07
  • 2014-02-05
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
  • 2012-02-16
相关资源
最近更新 更多