如何将" IO(E e)转换为ExceptT e m a

时间:2017-07-10 08:20:06

标签: haskell monads monad-transformers

我正在使用Control.Monad.Except而且我被困在一个我必须强制执行ExceptT AppError m a动作才能获得IO动作的地方,然后将其包裹起来再次进入ExceptT AppErr m a。如果您想知道为什么需要这样做,那是因为我需要运行的底层库函数只接受IO操作。在这种情况下,它将是Database.PostgreSQL.Simple.withTransaction :: Connection -> IO a -> IO a

如何编写以下概念等效词:

type AppM = ExceptT AppError (ReaderT Env (LoggingT IO))

runAppM :: Env -> AppM a -> a

withTransaction :: AppM a -> AppM a
withTransaction appm = do
  conn <- getDbConnection
  env <- getEnv
  liftIO $ PGS.withTransaction conn $ runAppM appm

这是我得到的错误:

Excepted type: ExceptT AppError (ReadertT Env (LoggingT IO)) a
Actual type: ExceptT AppError (ReadertT Env (LoggingT IO)) (Either AppError a)

1 个答案:

答案 0 :(得分:-1)

使用构造函数ExceptT : m (Either e a) -> ExceptT e m a

withTransaction :: AppM a -> AppM a
withTransaction appm = do
  conn <- getDbConnection
  env <- getEnv
  ExceptT $ PGS.withTransaction conn $ runAppM appm