servant与`enter`的函数依赖错误

时间:2017-02-18 14:35:33

标签: haskell servant

我略微更改了仆人教程中显示的应用,使Reader monad成为ReaderT,就像这样

{-# LANGUAGE DataKinds                  #-}
{-# LANGUAGE DeriveGeneric              #-}
{-# LANGUAGE FlexibleInstances          #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE PolyKinds                  #-}
{-# LANGUAGE ScopedTypeVariables        #-}
{-# LANGUAGE TupleSections              #-}
{-# LANGUAGE TypeOperators              #-}

module Lib
    ( runServer
    ) where

import           Control.Monad.Except
import           Control.Monad.Reader
import qualified Data.Text                as T
import           Network.Wai
import           Servant

type WebApi
  =    "static" :> Raw
  :<|> "foo" :> Get '[PlainText] T.Text

type Foo = String

server :: ServerT WebApi (ReaderT Foo (ExceptT ServantErr IO))
server = static :<|> foo
  where
    static :: Application
    static = undefined

    -- Handler T.Text
    foo :: ReaderT Foo (ExceptT ServantErr IO) T.Text
    foo = undefined

webAPI :: Proxy WebApi
webAPI = Proxy

readerToHandler :: Foo -> ReaderT Foo (ExceptT ServantErr IO) :~> ExceptT ServantErr IO
readerToHandler t = Nat (\x -> runReaderT x t)

-- readerServer :: ServerT WebApi (ExceptT ServantErr IO)
-- readerServer = enter (readerToHandler "foobarbaz") server

-- serve' :: Application
-- serve' = serve webAPI server

runServer :: IO ()
runServer = return ()

问题是我无法启用readerServer功能,类型检查失败并出现这个难以理解的错误

src/Lib.hs:45:16: error:
    • Couldn't match type ‘IO’ with ‘ExceptT ServantErr IO’
        arising from a functional dependency between:
          constraint ‘Servant.Utils.Enter.Enter
                        (IO ResponseReceived)
                        (ReaderT Foo (ExceptT ServantErr IO) :~> ExceptT ServantErr IO)
                        (IO ResponseReceived)’
            arising from a use of ‘enter’
          instance ‘Servant.Utils.Enter.Enter (m a) (m :~> n) (n a)’
            at <no location info>
    • In the expression: enter (readerToHandler "foobarbaz") server
      In an equation for ‘readerServer’:
          readerServer = enter (readerToHandler "foobarbaz") server
Failed, modules loaded: none.

任何想法出了什么问题?

1 个答案:

答案 0 :(得分:2)

问题是Raw端点的存在,它与enter无法很好地交互。这是Servant中的known annoyance

Enter类型类确定可以转换哪些处理程序集以及转换。它有三个实例:

  • Enter (m a) ((:~>) m n) (n a)最简单的情况。如果你有一个monadic动作和一个将它带到另一个monad的自然转换,你可以应用转换。

  • Enter b arg ret => Enter (a -> b) arg (a -> ret)。如果你有一个带有参数的处理程序,并且你知道如何转换处理程序的最终monadic动作,你可以使用相同的arg转换来转换处理程序。

  • (Enter typ1 arg1 ret1, Enter typ2 arg2 ret2, (~) * arg1 arg2) => Enter ((:<|>) typ1 typ2) arg1 ((:<|>) ret1 ret2)如果你有一个处理程序组合:<|>,并且每个处理程序都可以使用相同的自然转换arg1单独转换,那么你也可以使用arg1转换合成。

最后一个条件在您的示例中失败,因为Raw的处理程序具有Application类型Request -> (Response -> IO ResponseReceived) -> IO ResponseReceived,而另一个处理程序是ReaderT Foo (ExceptT ServantErr IO)操作。类型不匹配,因此组合没有Enter实例。

您的自定义处理程序上有一个workaround:调用enter,之后才会使用Application处理程序进行组合。

type WebApi
  =    "static" :> Raw
  :<|> FooEndpoint

type FooEndpoint = "foo" :> Get '[PlainText] T.Text

readerServer :: ServerT WebApi (ExceptT ServantErr IO)
readerServer = static :<|> enter (readerToHandler "foobarbaz") foo
  where
    static :: Application
    static = undefined
    foo :: ReaderT Foo (ExceptT ServantErr IO) T.Text
    foo = undefined