如何使用Servant库中的Enter解决类型错误?

时间:2017-01-21 18:41:53

标签: haskell servant

我正在尝试使用enter函数来允许我运行我的API处理程序,其中包含一组异常,我会将其转换为较高级别的Servant,但我遇到了类型问题匹配。

鉴于这一组最小的定义:

-- server :: Config -> Server Routes
server :: Config -> ServerT Routes (ExceptT ServantErr IO)
server c = enter runApp (handlers c)

-- runApp :: AppM :~> Handler
runApp :: ExceptT AppErr IO :~> ExceptT ServantErr IO
runApp = Nat undefined

handlers :: Config -> ServerT Routes (ExceptT AppErr IO)
handlers = undefined

我最终遇到了这种类型错误:

Couldn't match type `IO' with `ExceptT ServantErr IO'
arising from a functional dependency between:
  constraint `servant-0.7.1:Servant.Utils.Enter.Enter
                (IO ResponseReceived)
                (ExceptT AppErr IO :~> ExceptT ServantErr IO)
                (IO ResponseReceived)'
    arising from a use of `enter'
  instance `servant-0.7.1:Servant.Utils.Enter.Enter
              (m a) (m :~> n) (n a)'
    at <no location info>
In the expression: enter runApp (handlers c)
In an equation for `server': server c = enter runApp (handlers c)

例外来自调用enter的行。供参考,enter

的声明
class Enter typ arg ret | typ arg -> ret, typ ret -> arg where
  enter :: arg -> typ -> ret

instance Enter (m a) (m :~> n) (n a) where
  -- enter :: (m :~> n) -> m a -> n a

所以,当我打电话给enter runApp时,我希望这些类型如下:

enter :: (m :~> n) -> m a -> n a
enter (runApp :: m ~ ExceptT AppErr IO :~> n ~ ExceptT ServantErr IO) :: ExceptT AppErr IO a -> ExceptT ServantErr IO a

(上面我使用n ~ ExceptT ServantErr IO来说明我的类型替换)

实际上,我从其他代码(我试图模仿,我不明白我哪里出错)知道,enter runApp应该/必须有这种类型:

enter runApp :: ServerT Routes (ExceptT AppErr IO a) -> ServerT Routes (ExceptT ServantErr IO a)

所以,问题很多:

  • enter runApp的实际类型是什么(不是ghci会给我什么,而是更具描述性的解释)?
  • (IO ResponseReceived)约束来自哪里?
  • 如何调整上面的代码以使整个处理程序通过自然转换?

1 个答案:

答案 0 :(得分:4)

您不能在Api类型中拥有Raw个端点。这样做的方法是将Raw端点从handlers中取出并调用它,例如。

server c = enter runApp (handlers c) :<|> handleRaw