How to do when load error Couldn't match expected type `IO (Maybe String)'
module REPL(REPL(..), repl) where
import qualified Control.Exception as E
import System.Console.Readline(readline, addHistory)
data REPL s = REPL {
repl_init :: IO (String, s), -- prompt and initial state
repl_eval :: s -> String -> IO (Bool, s), -- quit flag and new state
repl_exit :: s -> IO ()
}
repl :: REPL s -> IO ()
repl p = do
(prompt, state) <- repl_init p
let loop s = (do
mline <- readline prompt
case mline of
Nothing -> loop s
Just line -> do
(quit, s') <- repl_eval p s line
if quit then
repl_exit p s'
else do
addHistory line
loop s'
) E.catch undefined (\(e :: E.SomeException) -> putStrLn "Handled exception!"
)
loop state
Output:
REPL.hs:21:5:
Couldn't match expected type `IO (Maybe String)'
against inferred type `t -> Maybe String'
In a stmt of a 'do' expression: mline <- readline prompt
In the 开发者_开发百科expression:
(do { mline <- readline prompt;
case mline of {
Nothing -> loop s
Just line
-> do { (quit, s') <- repl_eval p s line;
.... } } })
E.catch
undefined
(\ (e :: E.SomeException) -> putStrLn "Handled exception!")
In the definition of `loop':
loop s = (do { mline <- readline prompt;
case mline of {
Nothing -> loop s
Just line -> do { ... } } })
E.catch
undefined
(\ (e :: E.SomeException) -> putStrLn "Handled exception!")
Since your previous question you have removed the backticks, `, around your E.catch
statement. My answer to that question didn't use backticks because I wasn't calling catch
infix. You can either use catch post-fix without backticks:
let loop s = E.catch (do ...) (\(e :: E.SomeException) -> ...)
or you can use catch infix with the backticks:
let loop s = (do ...) `E.catch` (\(e :: E.SomeException) -> ...)
Also, the undefined
was me just forcing an exception for sake of an example on the GHCi REPL - it took the place of the (do ..)
statement in your code.
精彩评论