也许我在这里做了些蠢事,但我得到了:
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
的实例吗?
答案 0 :(得分:9)
Lazy状态变换器没有实例,只有严格的变压器。使用import Control.Monad.State.Strict
。