5

也许我在这里做一些愚蠢的事情,但我得到:

No instance for (MonadException Ti)
  arising from a use of `getInputLine'

在代码示例中:

module Foo where
import System.Console.Haskeline
import System.Console.Haskeline.MonadException
import Control.Monad.State
import Control.Monad.IO.Class

type Ti = StateT Int IO

action :: String -> Ti ()
action s = do
    n <- get
    lift $ putStrLn $ show n ++ ": " ++ s

repl :: InputT Ti ()
repl = do
    minput <- getInputLine "?"
    case minput of
        Nothing    -> return ()
        Just input -> lift (action input) >> repl

现在,System.Console.Haskeline.MonadException定义

MonadException IO
(MonadIO (StateT s m), MonadException m) => MonadException (StateT s m)

Control.Monad.IO.Class

MonadIO IO
MonadIO m => MonadIO (StateT s m)

那么,它不应该Ti自动推断实例吗?

4

1 回答 1

9

没有惰性状态转换器的实例,只有严格的。使用import Control.Monad.State.Strict.

于 2013-06-05T16:26:29.537 回答