gpt4 book ai didi

haskell - 在 monad 转换器中使用类型同义词

转载 作者:行者123 更新时间:2023-12-03 22:18:04 25 4
gpt4 key购买 nike

是否可以使用类型同义词作为 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

有没有办法避免扩展类型同义词 AB a 的定义中?

最佳答案

不能部分应用类型同义词。在这种特定情况下,您可以编写

type A = ReaderT Int IO
type B a = ReaderT String A a

[甚至更好 type B = ReaderT String A使用 B在另一个单子(monad)变压器]

一般来说,如果不使用 newtype/data,这种转换是不可能的,例如:
type A a = Reader a Int

不能等价地写成 type A = ... .在某种意义上,这个特性相当于类型级 lambda \a -> Reader a Int .

关于haskell - 在 monad 转换器中使用类型同义词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3372343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com