gpt4 book ai didi

haskell - 由于使用 `Control.Exception.catch' 而产生

转载 作者:行者123 更新时间:2023-12-02 14:35:22 26 4
gpt4 key购买 nike

当我加载 haskell 文件时,出现错误

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

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!")

最佳答案

通过在文件顶部包含此行来使用作用域类型变量:

{-# LANGUAGE ScopedTypeVariables #-}

或者在 GHCi session 中只需执行以下操作:

> :set -XScopedTypeVariables

并在您的 catch 函数中提供签名。在 GHCi 中:

> import Control.Exception as E
> E.catch undefined (\(e :: E.SomeException) -> putStrLn "Handled exception!")
Handled exception!

庆祝美好时光,加油!

关于haskell - 由于使用 `Control.Exception.catch' 而产生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5631116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com