gpt4 book ai didi

haskell - 在haskell程序中使用返回的EitherT

转载 作者:行者123 更新时间:2023-12-04 23:00:38 25 4
gpt4 key购买 nike

我正在尝试在我正在处理的 Haskell 项目中使用“引用解析”包,但我无法在实际代码中使用 IsraelT。我知道它们是 monad 转换器,我想我明白这意味着什么,但是我似乎无法真正弄清楚如何使用它们。代表我正在尝试做的玩具示例如下:

module Main where
import Text.EditDistance
import Text.CSL.Input.Identifier
import Text.CSL.Reference
import Control.Monad.Trans.Class
import Control.Monad.Trans.Either

main = do
putStrLn "Resolving definition"
let resRef = runEitherT $ resolveEither "doi:10.1145/2500365.2500595"
case resRef of
Left e -> do
putStrLn ("Got error: "++ e)
Right ref -> do
putStrLn ("Added reference to database: "++ (show ref))

在这里, resolveEither有类型:
resolveEither :: (HasDatabase s,
Control.Monad.IO.Class.MonadIO m,
mtl-2.1.3.1:Control.Monad.State.Class.MonadState s m)
=> String -> EitherT String m Reference

runEitherT $ resolveEither "ref"有类型:
runEitherT $ resolveEither "ref"
:: (HasDatabase s,
Control.Monad.IO.Class.MonadIO m,
mtl-2.1.3.1:Control.Monad.State.Class.MonadState s m)
=> m (Either String Reference)

但是,这会产生以下错误:
Main.hs:10:34:
No instance for (Control.Monad.IO.Class.MonadIO (Either [Char]))
arising from a use of ‘resolveEither’
In the first argument of ‘runEitherT’, namely
‘(resolveEither "doi:10.1145/2500365.2500595")’
In the expression:
runEitherT (resolveEither "doi:10.1145/2500365.2500595")
In an equation for ‘resRef’:
resRef = runEitherT (resolveEither "doi:10.1145/2500365.2500595")

我不知道如何解决或解决。

任何帮助将不胜感激,尤其是从使用角度而不是实现角度处理 monad 转换器的教程的指针。

编辑:

为了反射(reflect) dfeuer 和 Christian 对答案的评论,如果我将 main 更改为以下内容,我仍然会遇到错误:
main = do
putStrLn "Resolving definition"
resRef <- runEitherT (resolveEither "doi:10.1145/2500365.2500595")
case resRef of
Left e -> do
putStrLn ("Got error: "++ e)
Right ref -> do
putStrLn ("Added reference to database: "++ (show ref))

我现在得到的错误是:
No instance for (MonadState s0 IO)
arising from a use of ‘resolveEither’
In the first argument of ‘runEitherT’, namely
‘(resolveEither "doi:10.1145/2500365.2500595")’
In a stmt of a 'do' block:
resRef <- runEitherT (resolveEither "doi:10.1145/2500365.2500595")
In the expression:
do { putStrLn "Resolving definition";
resRef <- runEitherT (resolveEither "doi:10.1145/2500365.2500595");
case resRef of {
Left e -> do { ... }
Right ref -> do { ... } } }

我正在编辑我的问题并进行评论,因为这里的代码格式比在评论中容易得多。

最佳答案

我相信问题在于您试图在 resRef 上进行模式匹配当您可能想要做的是执行它并对结果进行模式匹配时。

所以你应该试试这个:

main = do
putStrLn "Resolving definition"
resRef <- runEitherT $ resolveEither "doi:10.1145/2500365.2500595"
case resRef of
Left e -> do

关于haskell - 在haskell程序中使用返回的EitherT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26110628/

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