から結果を取得していgetInputline
ます。そのタイプは次のとおりです。
(MonadException m) => IO String -> InputT m (Maybe String)
その部分だけを手に入れたいMaybe String
。この回答(および同じ質問の他の回答)で説明されているように、一般にモナドを削除する方法がないことをよく知っています。ただし、 内で実行しているので、ここでInputT
提案されているように可能だと思います。ただし、は の中にあるため、答えが示すように、 をそのまま使用することはできません。liftIO
IO
StateT
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
完全なコードはここで見つけることができ、私がやろうとしていることについての説明はここで見つけることができます。