是否可以使用类型同义词作为 monad 转换器类型构造函数的参数?特别是,如果应用的 monad 转换器有一个一元类型的同义词,它可以用作另一个 monad 转换器中的底层 monad 的类型吗?
从我看到的类型同义词不被接受为一流的类型构造函数,请参阅下面的示例和错误消息:
-- Using type synonym of a monad transformer in another monad transformer.
import Control.Monad.Reader
-- inner transformer
type A a = ReaderT Int IO a
-- type B a = ReaderT String A a
{- Error:
readert2.hs:8:0:
Type synonym `A' should have 1 argument, but has been given 0
In the type synonym declaration for `B'
-}
-- type B a = ReaderT String (A a) a
{- Error:
readert2.hs:15:27:
Kind mis-match
The second argument of `ReaderT' should have kind `* -> *',
but `A a' has kind `*'
In the type `ReaderT String (A a) a'
In the type synonym declaration for `B'
-}
type B a = ReaderT String (ReaderT Int IO) a
{- OK -}
main = do
r <- flip runReaderT 39 $ do
n <- ask :: A Int
s <- flip runReaderT "foo" $ (ask :: B String)
return $ n + length s
print r
有没有办法避免扩展A
定义中的类型同义词B a
?