Note, this question is not about "monoids in the category of endofunctors". Nor is it directly about
Functor
s (aMonad
is always aFunctor
, but this question is concerned mainly about monad transformers)
docs在 Haskell 的
SelectT
上monad 转换器指出
SelectT
is not a functor on the category of monads, and many operations cannot be lifted through it.
MaybeT
, RWST
等),而有些不是( ContT
, SelectT
)? 最佳答案
- What's the category of monads? What are the arrows in that category?
对象为 monad 的类别,即类型
T
实物Type -> Type
与 Monad
实例和箭头 A -> B
是它们底层仿函数之间的自然转换,在 Haskell 中通常由 forall x. A x -> B x
类型的函数表示(尽管严格来说参数化是比自然性更强的条件)。mmorph 中有一个实现。包裹。
此类别中的初始对象是
Identity
, 因为对于任何单子(monad) T
恰好有一个自然变换forall x. Identity x -> T x
.双重的,我认为最终的对象是 Const ()
.
- Why are some monad transformers functors on the category of monads (
MaybeT
,RWST
, etc), but some not (ContT
,SelectT
)?
此类别中的仿函数需要提升
fmap
:fmap'
:: forall m n. (Monad m, Monad n)
=> (forall x. m x -> n x) -> forall x. T m x -> T n x
而且您通常无法为 ContT
实现此功能和 SelectT
.我不确定为什么,但它似乎取决于方差:我们正在尝试实现一个协变仿函数,但是 ContT
和 SelectT
在它们的底层单子(monad)中是不变的,例如,m
在 (a -> m r) -> m r
中出现正面和负面的情况在 ContT r m a
内.
- What good does it do, from a programming perspective, to be a functor on the category of monads? Why should I care as a consumer of the library?
如果你有一个通用的方法来“运行”一个 monad
m
在单子(monad)中n
, 你不一定能把它提升到 ContT
或 SelectT
;您会遇到以下更受限制的映射操作:mapSelectT :: (m a -> m a) -> SelectT r m a -> SelectT r m a
mapContT :: (m r -> m r) -> ContT r m a -> ContT r m a
底层 monad 和结果类型是固定的。所以你不能总是在使用这些转换器的堆栈中自由地提升 Action 。
关于haskell - 什么是单子(monad)类别的仿函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63882053/