“读取器”单子 [英] The "reader" monad

查看:103
本文介绍了“读取器”单子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以作者monad允许你写一些东西给[通常]某种容器,最后把容器拿回来。在大多数实现中,容器实际上可以是任何monoid。



现在,还有一个读取器monad。这样,你可能会认为它会提供双重操作 - 逐步从某种容器中读取一个项目。实际上,这不是通常读者monad提供的功能。 (相反,它仅提供了对半全局常量的简单访问。)



为了实际编写一个monad,其中与通常的作者是双重的monad,我们需要某种与monoid相对应的结构。


  1. 有人知道这种双重结构可能是什么吗?

  2. 有没有人写过这个monad?有没有一个知名的名字?
  3. 我不完全确定幺半群的双重性应该是什么,但是认为双重(可能是不正确的)与某些事物相反(简单地说,基于一个共同体是一个单子的双重性,并且具有所有相同的操作,但是相反的方式)。而不是基于它 mappend mempty 我会根据它:

      fold ::(Foldable f,Monoid m)=> f m  - > m 

    如果我们将f专门化到列表中,我们可以得到:

      fold :: Monoid m => [m]  - > m 

    这在我看来尤其包含了所有monoid类。

      mempty == fold [] 
    mappend xy == fold [x,y]



    那么,我猜这个不同的monoid类的双重性会是:

    pre > unfold ::(Comonoid m)=> m - > [m]

    这很像我在hackage here



    所以在这个基础上,我认为你描述的'读者'monad将是一个 supply monad 。供应monad实际上是一个值列表的状态转换器,因此在任何时候我们都可以选择从列表中提供一个项目。在这种情况下,列表将是unfold.supply monad的结果。我应该强调,我不是Haskell专家,也不是专家理论家。但这是你的描述让我想到的。


    OK, so the writer monad allows you to write stuff to [usually] some kind of container, and get that container back at the end. In most implementations, the "container" can actually be any monoid.

    Now, there is also a "reader" monad. This, you might think, would offer the dual operation - incrementally reading from some kind of container, one item at a time. In fact, this is not the functionality that the usual reader monad provides. (Instead, it merely offers easy access to a semi-global constant.)

    To actually write a monad which is dual to the usual writer monad, we would need some kind of structure which is dual to a monoid.

    1. Does anybody have any idea what this dual structure might be?
    2. Has anybody written this monad? Is there a well-known name for it?

    解决方案

    I'm not entirely sure of what the dual of a monoid should be, but thinking of dual (probably incorrectly) as the opposite of something (simply on the basis that a Comonad is the dual of a Monad, and has all the same operations but the opposite way round). Rather than basing it on mappend and mempty I would base it on:

    fold :: (Foldable f, Monoid m) => f m -> m
    

    If we specialise f to a list here, we get:

    fold :: Monoid m => [m] -> m
    

    This seems to me to contain all of the monoid class, in particular.

    mempty == fold []
    mappend x y == fold [x, y]
    

    So, then I guess the dual of this different monoid class would be:

    unfold :: (Comonoid m) => m -> [m]
    

    This is a lot like the monoid factorial class that I have seen on hackage here.

    So on this basis, I think the 'reader' monad you describe would be a supply monad. The supply monad is effectively a state transformer of a list of values, so that at any point we can choose to be supplied with an item from the list. In this case, the list would be the result of unfold.supply monad

    I should stress, I am no Haskell expert, nor an expert theoretician. But this is what your description made me think of.

    这篇关于“读取器”单子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆