1

在我观看了 George Wilson 的精彩演讲后(Next Level MTL https://github.com/gwils/next-level-mtl-with-classy-optics/blob/master/Slides.pdf)我尝试创建使用 MTL 的应用程序风格设计并决定使用仆人,看起来这个库不适合这种设计。
下面的代码无法编译,因为我无法将m转换为 Handler。

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

mkApp :: Application
mkApp = serve itemApi getItems

你可以在这里找到完整的例子:https ://github.com/paweln1986/ServantMTLStackOverflowExample

是否可以将任何单子与仆人一起使用?如何做到这一点?我尝试使用提升服务器但没有成功。你知道我在这里想念什么吗?

编译错误:

   • 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
   |                       ^^^^^^^^

更短的例子:

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

这给了我下面的编译错误

    • 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
   |                                       ^^^^^^^^^^^^
4

1 回答 1

5

最后我设法解决了我的问题。

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

我错过的一件事是正确使用提升服务器。在我创建了从我的 monad (AppT) 到 Handler 的正确自然转换之后,一切都编译并按预期工作。

于 2018-09-15T19:14:22.287 回答