gpt4 book ai didi

haskell - 让每个 monad 转换器成为 MonadTrans 的实例真的是默认做法吗?

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

所以真实世界的 Haskell 说:

Every monad transformer is an instance of MonadTrans



但我正在和 Scotty 一起玩,发现它的基础单子(monad)转换器 ScottyT不是 MonadTrans 的实例.

查看发行说明,似乎这是一个深思熟虑的设计决定: here .引用:

The monad parameters to ScottyT have been decoupled, causing the type of the ScottyT constructor to change. As a result, ScottyT is no longer a MonadTrans instance ...



我希望你能理解我的困惑。尽管如此,我将尝试提出严格的问题:
  • 为什么不希望 monad 转换器成为 MonadTrans 的实例? ?
  • 您如何解释上述 ScottyT 设计的变化?

  • P.S.:我明白我可以定义 MonadTrans ScottyT 的实例我自己,但我应该吗? (链接回问题)

    最佳答案

    ScottyT不是单子(monad)变压器。让我们内联(简化的)定义:

    newtype ScottyT' m a = ScottyT' {
    runS :: State [ (Request->m Response) -> Request->m Response ] a
    }

    定义 lift为此,您需要从一般 m a action 和这样的中间件列表,必须得到一个实际的 a值,因为 State s产生实际的单值。没有办法做到这一点。

    现在,如果你认为 MonadTrans实际上并不需要某个东西成为 monad 转换器:从数学上讲,monad 转换器对应于仿函数的组合,但是 ScottyT实际上并没有实现这样的组合。

    关于haskell - 让每个 monad 转换器成为 MonadTrans 的实例真的是默认做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32854747/

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