如何为MonadReader和MonadIO上受约束的函数修复IO丢失的实例?

问题描述 投票:2回答:1

我一直在尝试通过与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

runReaderT函数上使用service产生

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
1个回答
0
投票
一个问题是ExceptT ApplicationError IO没有-实际上没有-具有MonadUnliftIO实例。很少有Monad具有该实例:MonadUnliftIO(小写),IO和类似Identity的Monad。
© www.soinside.com 2019 - 2024. All rights reserved.