I have been trying to get a good understanding of mtl by building a project using it in combination with persistent.
One module of that project has a function which uses insertMany_
service
:: (MonadReader ApplicationConfig m, MonadIO m) =>
ReaderT SqlBackend (ExceptT ApplicationError m) ()
service = insertMany_ =<< lift talkToAPI
Here talkToAPI
can fail so the response is wrapped in ExceptT
, its type is
ExceptT ApplicationError m [Example]
In short the service
's job is to talk to an API, parse the response and use insertMany_
to store that response into a database.
The actual storage action is handled by withPostgresqlConn
withPostgresqlConn
:: (MonadUnliftIO m, MonadLogger m) =>
ConnectionString -> (SqlBackend -> m a) -> m a
Using runReaderT
on my service
function yields
ghci> :t runReaderT service
ghci> (MonadReader ApplicationConfig m, MonadIO m) =>
SqlBackend -> ExceptT ApplicationError m ()
so to process this I believe I need to use runExceptT
like this
runService :: ConnectionString -> IO ()
runService connStr = either print return
=<< runStdLoggingT (runExceptT $ withPostgresqlConn connStr $ runReaderT service)
But I get these two errors
• 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’
What could be the problem here? There has likely been an error on my part but I'm not sure where to look for.