Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 读卡器monad-读卡器与asks功能的区别?_Haskell_Monads_Reader Monad - Fatal编程技术网

Haskell 读卡器monad-读卡器与asks功能的区别?

Haskell 读卡器monad-读卡器与asks功能的区别?,haskell,monads,reader-monad,Haskell,Monads,Reader Monad,读卡器monad有一个asks函数,它的定义正好是读卡器函数,为什么它作为一个单独的函数存在,其定义与读卡器相同?为什么不总是使用阅读器 class Monad m => MonadReader r m | m -> r where -- | Retrieves the monad environment. ask :: m r ask = reader id -- | Executes a computation in a modified

读卡器monad有一个asks函数,它的定义正好是读卡器函数,为什么它作为一个单独的函数存在,其定义与读卡器相同?为什么不总是使用阅读器

class Monad m => MonadReader r m | m -> r where

    -- | Retrieves the monad environment.
    ask   :: m r
    ask = reader id

    -- | Executes a computation in a modified environment.
    local :: (r -> r) -- ^ The function to modify the environment.
          -> m a      -- ^ @Reader@ to run in the modified environment.
          -> m a

    -- | Retrieves a function of the current environment.
    reader :: (r -> a) -- ^ The selector function to apply to the environment.
           -> m a
    reader f = do
      r <- ask
      return (f r)

-- | Retrieves a function of the current environment.
asks :: MonadReader r m
    => (r -> a) -- ^ The selector function to apply to the environment.
    -> m a
asks = reader
class Monad m=>Monad r m | m->r其中
--|检索monad环境。
问:m r
ask=读卡器id
--|在修改后的环境中执行计算。
本地::(r->r)--^用于修改环境的函数。
->我将--^@Reader@运行在修改过的环境中。
->我是
--|检索当前环境的函数。
读卡器::(r->a)--^要应用于环境的选择器函数。
->我是
读卡器f=do
r(r->a)--^应用于环境的选择器函数。
->我是
asks=读取器

我找到了将这种冗余引入包和包的修补程序。修补程序/提交描述为。。。不是很有启发性。但是,在这两种情况下,
询问
早于
读取器
,并且在这两种情况下,相同的更改引入了
状态
编写器
原语

因此,一些猜测:

  • 据观察,将transformer/monad类所做的核心语义作为一个在库中表示的概念很方便
  • 为了可预测性,新的原语是以提供该原语的转换器命名的,而不是其他(
    StateT
    ->
    state
    writer
    ->
    writer
    ReaderT
    ->
    reader
    )。这使得用户更容易记住他们想要的东西叫什么
  • 由于
    asks
    已经存在,它被保留了下来,以获得少量的向后兼容性

  • 如果我们想要一个明确的答案,我们可能必须询问Ed Kmett或Twan van Laarhoven,他们显然是这些变化的始作俑者。

    与其他monad变形金刚的兼容性:以及自我记录的问题
    reader
    读作“我有一个函数,我想把它当作某个monad的实例来处理”<代码>询问读作“我想获得环境并对其应用一些东西”。它们有同样的效果,但对人类的阅读方式不同。@WillemVanOnsem我不确定这是否真的回答了这个问题。“奇怪的行为,所以它与X兼容”的明显后续是“为什么X有这种奇怪的行为?”。在这个例子中:
    monawarder
    有两个相同的函数,因为
    RWST
    有两个相同的函数;但是为什么
    RWST
    有两个相同的功能呢?看看这个
    transformers
    补丁,它看起来像是将现有的
    读卡器
    仅用于
    读卡器
    类型。这个
    reader
    是在一个应用程序中引入的,它可能是在尝试添加当
    reader
    和朋友从自己的
    newtype
    s切换到
    type reader=ReaderT Identity
    时丢失的“构造函数”,发生了什么?