1

我从 得到一个结果getInputline,其类型是:

(MonadException m) => IO String -> InputT m (Maybe String)

我只想得到那Maybe String部分。我很清楚,一般来说,没有办法剥离单子,正如这个答案(以及同一问题中的其他答案)中所解释的那样。但是,由于我是在 内部进行的InputT,所以我想这是可能的,正如这里所建议的那样。但是,正如答案所暗示的那样,我不能只使用liftIO,因为IOStateT.

loop :: Counter -> InputT (StateT [String] IO) ()    
loop c = do
        minput <- getLineIO $ in_ps1 $ c
        case minput of
          Nothing -> outputStrLn "Goodbye."
          Just input -> (process' c input) >> loop c      

getLineIO :: (MonadException m) => IO String -> InputT m (Maybe String)
getLineIO ios = do
    s <- liftIO ios
    getInputLine s

process' :: Counter -> String -> InputT (StateT [String] IO) ()
[...]

我得到的错误:

Main.hs:81:15:
    No instance for (MonadException (StateT [String] IO))
      arising from a use of ‘getLineIO’
    In the expression: getLineIO
    In a stmt of a 'do' block: minput <- getLineIO $ in_ps1 $ c
    In the expression:
      do { minput <- getLineIO $ in_ps1 $ c;
           case minput of {
             Nothing -> outputStrLn "Goodbye."
             Just input -> (process' c input) >> loop c } }

如果我按照@chepner 的建议直接删除getLineIO并使用:getInputLine

loop :: Counter -> InputT (StateT [String] IO) ()
loop c = do
    minput <- (in_ps1 c) >>= getInputLine
    case minput of
      Nothing -> outputStrLn "Goodbye."
      Just input -> (process' c input) >> loop c

我收到一个错误:

Main.hs:81:16:
    Couldn't match type ‘IO’ with ‘InputT (StateT [String] IO)’
    Expected type: InputT (StateT [String] IO) String
      Actual type: IO String
    In the first argument of ‘(>>=)’, namely ‘(in_ps1 c)’
    In a stmt of a 'do' block: minput <- (in_ps1 c) >>= getInputLine

完整的代码可以在这里找到,关于我想要做什么的解释可以在这里找到。

4

0 回答 0