haskell - 什么是单子(monad)类别的仿函数?

标签 haskell monads monad-transformers category-theory

Note, this question is not about "monoids in the category of endofunctors". Nor is it directly about Functors (a Monad is always a Functor, 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.


  • monad 的类别是什么?该类别中的箭头是什么?
  • 为什么有些单子(monad)转换器仿函数属于单子(monad)类别( MaybeTRWST 等),而有些不是( ContTSelectT )?
  • 从编程的角度来看,成为 monad 类别的仿函数有什么好处?作为图书馆的消费者,我为什么要关心?
  • 最佳答案

    1. What's the category of monads? What are the arrows in that category?

    对象为 monad 的类别,即类型 T实物Type -> TypeMonad实例和箭头 A -> B是它们底层仿函数之间的自然转换,在 Haskell 中通常由 forall x. A x -> B x 类型的函数表示(尽管严格来说参数化是比自然性更强的条件)。
    mmorph 中有一个实现。包裹。
    此类别中的初始对象是 Identity , 因为对于任何单子(monad) T恰好有一个自然变换forall x. Identity x -> T x .双重的,我认为最终的对象是 Const () .
    1. 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 .我不确定为什么,但它似乎取决于方差:我们正在尝试实现一个协变仿函数,但是 ContTSelectT在它们的底层单子(monad)中是不变的,例如,m(a -> m r) -> m r 中出现正面和负面的情况在 ContT r m a 内.
    1. 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 , 你不一定能把它提升到 ContTSelectT ;您会遇到以下更受限制的映射操作:
    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/

    相关文章:

    haskell - 在没有 monad 转换器的情况下避免使用 case 表达式阶梯

    haskell - 列出单子(monad)转换器

    haskell - 在 MonadState 中获取 put 和状态

    haskell - 简化一些 Haskell 代码

    haskell - 为什么 Haskell 不泛化一些函数

    Haskell:在 Monads 中做符号并返回

    python - 结合 maybe 和 seq monads : confused at the output

    haskell - 理解 Monad 变形金刚的困难

    Haskell 插件和 cabal 沙箱

    Haskell 日期解析和格式化