3

我稍微改变了servant教程中显示的应用程序来制作Readermonad a 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.

任何想法出了什么问题?

4

1 回答 1

2

问题是Raw端点的存在,它与enter. 这是Servant中众所周知的烦恼

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

  • Enter (m a) ((:~>) m n) (n a)最简单的情况。如果您有一个单子动作和一个将其带到不同单子的自然转换,您可以应用该转换。

  • Enter b arg ret => Enter (a -> b) arg (a -> ret). 如果您有一个带有参数的处理程序,并且您知道如何转换处理程序的最终一元操作,则可以使用相同的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实例。


有一种解决方法:调用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
于 2017-02-18T22:13:33.263 回答