1
votes

After I watched good talk of George Wilson (Next Level MTL https://github.com/gwils/next-level-mtl-with-classy-optics/blob/master/Slides.pdf) I try to create application which uses MTL style design and decided to use servant, it looks like this library doesn't fit well to such design.
Below code doesn't compile because I am not able to transform m into Handler.

getItems :: (MonadIO m, MonadReader r m, HasNetworkConfig r) => m [Item]
getItems =
   return [Item "foo" "bar"]

mkApp :: Application
mkApp = serve itemApi getItems

You can find complete example here: https://github.com/paweln1986/ServantMTLStackOverflowExample

Is it possible to use any monad with servant? How to achieve this? I tried to use hoistServer without success. Do you have any idea what I miss here?

Compilation error:

   • No instance for (MonadReader r0 Handler)
    arising from a use of ‘getItems’
   • In the second argument of ‘serve’, namely ‘getItems’
     In the expression: serve itemApi getItems
     In an equation for ‘mkApp’: mkApp = serve itemApi getItems
   |
40 | mkApp = serve itemApi getItems
   |                       ^^^^^^^^

Shorter example:

type ReaderAPI = "ep1" :> Get '[JSON] Int :<|> "ep2" :> Get '[JSON] String    :<|> Raw :<|> EmptyAPI

readerApi = Proxy :: Proxy ReaderAPI

readerServer :: (MonadIO m, HasNetworkConfig r, MonadReader r m) => ServerT ReaderAPI (AppT m)
readerServer = return 1797 :<|> view (networkConfig . host) :<|> Tagged (error "raw server") :<|> emptyServer

nt x = return undefined

mainServer = hoistServer readerApi nt readerServer :: Server ReaderAPI

This gives me below compilation error

    • Ambiguous type variable ‘m0’ arising from a use of ‘readerServer’
  prevents the constraint ‘(MonadIO m0)’ from being solved.
  Probable fix: use a type annotation to specify what ‘m0’ should be.
  These potential instances exist:
    instance [safe] MonadIO IO -- Defined in ‘Control.Monad.IO.Class’
    instance MonadIO m => MonadIO (AppT m) -- Defined in ‘Types’
    instance MonadIO Handler
      -- Defined in ‘Servant.Server.Internal.Handler’
    ...plus 18 instances involving out-of-scope types
    (use -fprint-potential-instances to see them all)
• In the third argument of ‘hoistServer’, namely ‘readerServer’
  In the expression:
      hoistServer readerApi nt readerServer :: Server ReaderAPI
  In an equation for ‘mainServer’:
      mainServer
        = hoistServer readerApi nt readerServer :: Server ReaderAPI
   |
64 | mainServer = hoistServer readerApi nt readerServer :: Server ReaderAPI
   |                                       ^^^^^^^^^^^^
1
What's the full compiler error?Gurkenglas
I forgot to add compilation error. I updated my question.PawelN
Wouldn't it be enough to just fix your choice of m in mainServer's definition; for example, to ReaderT R IO (with some suitable R)?Cactus

1 Answers

5
votes

Finally I managed to solve my problem.

run :: (MonadIO m, MonadReader r m, HasNetworkConfig r) => AppConfig -> m ()
run config = do
  serverPort <- view (networkConfig . port)
  let settings =
        setPort serverPort $
        setBeforeMainLoop (liftIO $ hPutStrLn stderr ("listening on port " ++ show serverPort)) defaultSettings
  liftIO $ runSettings settings (mainServer config)

printM :: (MonadIO m, Show a) => a -> m ()
printM a = liftIO $ print a

type ReaderAPI = "ep1" :> Get '[ JSON] String :<|> "ep2" :> Get '[ JSON] String :<|> Raw :<|> EmptyAPI

readerApi :: Proxy ReaderAPI
readerApi = Proxy :: Proxy ReaderAPI

fromConfig :: (Functor m, MonadReader r m, HasNetworkConfig r) => m String
fromConfig = view (networkConfig . host)

rawValue :: (Applicative m) => m String
rawValue = pure "1797"

readerServer :: (Monad m) => ServerT ReaderAPI (AppT m)
readerServer = rawValue :<|> fromConfig :<|> Tagged (error "raw server") :<|> emptyServer

nt :: AppConfig -> AppT IO x -> Handler x
nt config x = do
  res <- liftIO $ runExceptT $ runReaderT (runApp x) config
  case res of
    Left e       -> throwError e
    Right result -> return result

mainServer :: AppConfig -> Application
mainServer config = serve readerApi api
  where
    api = hoistServer readerApi (nt config) readerServer

One thing I missed was correct use of hoistServer. After I created correct natural transformation from my monad (AppT) to Handler, everything compiled and work as expected.