Я пытаюсь преобразовать обработку ошибок моей программы в исключения.Я построил REPL для управления моим моделированием, где я также обрабатываю ошибки:
> repl :: Sim -> IO Sim
> repl old_sim = do
> new_sim <- E.catch (do line <- getLine
> commands <- parseCommand line
> runCommands commands old_sim)
> (\err -> do putStrLn . show $ err
> return old_sim)
> if alive new_sim
> then repl new_sim
> else return new_sim
Либо parseCommands или runCommands могут fail String
, оба из которых возвращают IO Sim
.
Когда я загружаюфайл, я получаю эту гигантскую ошибку:
Prelude> :l simple_sim
[1 of 1] Compiling Main ( simple_sim.lhs, interpreted )
simple_sim.lhs:95:18:
Ambiguous type variable `a0' in the constraints:
(E.Exception a0) arising from a use of `E.catch'
at simple_sim.lhs:95:18-24
(Show a0) arising from a use of `show' at simple_sim.lhs:100:49-52
Probable fix: add a type signature that fixes these type variable(s)
In a stmt of a 'do' expression:
new_sim <- E.catch
(do { line <- getLine;
commands <- parseCommand line;
runCommands commands old_sim })
(\ err
-> do { putStrLn . show $ err;
return old_sim })
In the expression:
do { new_sim <- E.catch
(do { line <- getLine;
commands <- parseCommand line;
runCommands commands old_sim })
(\ err
-> do { putStrLn . show $ err;
.... });
if alive new_sim then repl new_sim else return new_sim }
In an equation for `repl':
repl old_sim
= do { new_sim <- E.catch
(do { line <- getLine;
commands <- parseCommand line;
.... })
(\ err -> do { ... });
if alive new_sim then repl new_sim else return new_sim }
Failed, modules loaded: none.
В частности:
Probable fix: add a type signature that fixes these type variable(s)
Куда я вставлю подпись этого типа?