【发布时间】:2020-10-03 05:09:35
【问题描述】:
我一直在尝试通过将mtl 与persistent 结合使用来构建一个项目来更好地理解它。
该项目的一个模块有一个使用insertMany_的函数
service
:: (MonadReader ApplicationConfig m, MonadIO m) =>
ReaderT SqlBackend (ExceptT ApplicationError m) ()
service = insertMany_ =<< lift talkToAPI
这里talkToAPI 可能会失败,所以响应包含在ExceptT 中,它的类型是
ExceptT ApplicationError m [Example]
简而言之,service 的工作是与 API 通信、解析响应并使用 insertMany_ 将该响应存储到数据库中。
实际存储动作由withPostgresqlConn处理
withPostgresqlConn
:: (MonadUnliftIO m, MonadLogger m) =>
ConnectionString -> (SqlBackend -> m a) -> m a
在我的 service 函数上使用 runReaderT 会产生
ghci> :t runReaderT service
ghci> (MonadReader ApplicationConfig m, MonadIO m) =>
SqlBackend -> ExceptT ApplicationError m ()
所以要处理这个我相信我需要像这样使用runExceptT
runService :: ConnectionString -> IO ()
runService connStr = either print return
=<< runStdLoggingT (runExceptT $ withPostgresqlConn connStr $ runReaderT service)
但是我得到了这两个错误
• No instance for (MonadUnliftIO (ExceptT ApplicationError IO))
arising from a use of ‘withPostgresqlConn’
• No instance for (MonadReader ApplicationConfig IO)
arising from a use of ‘service’
这可能是什么问题?我可能有一个错误,但我不确定在哪里寻找。
【问题讨论】:
标签: haskell monad-transformers haskell-persistent