问题标签 [free-monad]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
6 回答
6037 浏览

haskell - 暂停单子

Monads 可以做许多令人惊奇、疯狂的事情。他们可以创建包含值叠加的变量。它们可以让您在计算之前访问未来的数据。它们可以让你编写破坏性的更新,但不是真的。然后延续单子让你打破人们的想法!通常是你自己的。;-)

但这里有一个挑战:你能制作一个可以暂停的 monad吗?

Pausemonad 是一种状态 monad(因此,mutate具有明显的语义)。通常,像这样的 monad 具有某种“运行”功能,它运行计算并将最终状态返回给您。但Pause不同的是:它提供了一个step函数,该函数运行计算,直到它调用神奇的yield函数。此处计算暂停,向调用者返回足够的信息以便稍后恢复计算。

对于额外的 awesomne​​ss:允许调用者修改step调用之间的状态。(例如,上面的类型签名应该允许这样做。)


用例:编写执行复杂操作的代码通常很容易,但需要一个完整的 PITA 来将其转换为还输出其操作中的中间状态。如果您希望用户能够在执行过程中更改某些内容,那么事情会变得非常复杂。

实施思路:

  • 显然,它可以通过线程、锁和IO. 但我们能做得更好吗?;-)

  • 延续单子有些疯狂吗?

  • 也许是某种 writer monad,yield只记录当前状态,然后我们可以step通过迭代日志中的状态来“假装”它。(显然这排除了改变步骤之间的状态,因为我们现在并没有真正“暂停”任何东西。)

0 投票
7 回答
61846 浏览

haskell - 什么是自由单子?

我已经看到Free Monad这个词时不时出现了一段时间每个 人似乎只是在使用/讨论它们而没有解释它们是什么。那么:什么是自由单子?(我想说我熟悉 monad 和 Haskell 基础知识,但对范畴论只有非常粗略的了解。)

0 投票
3 回答
1223 浏览

haskell - 是否可以为“免费”实现 MonadFix?

http://hackage.haskell.org/package/free inControl.Monad.Free.Free允许访问任何给定的“免费单子” Functor。但是,它没有MonadFix实例。这是因为这样的实例无法编写,还是只是被遗漏了?

如果不能编写这样的实例,为什么不呢?

0 投票
4 回答
1987 浏览

haskell - 哪些单子可以通过某个函子表示为 Free?

文档Free说:

许多常见的单子以自由单子的形式出现,

  • Given data Empty a,与monadFree Empty同构。Identity
  • FreeMaybe可用于对偏性 monad 建模,其中每一层代表运行计算一段时间。

还有哪些其他 monad 可以用 来表达Free

我只能再想到一个:我相信Free (Const e)Either e.

编辑:哪些单子是不可表达的Free,为什么?

0 投票
2 回答
706 浏览

haskell - 单子的自由单子

x >>= f相当于retract (liftF x >>= liftF . f)?_

也就是说,从同样是 Monad 的 Functor 构建的自由 monad 的 monad 实例是否将具有与原始 Monad 等效的 monad 实例?

0 投票
1 回答
649 浏览

haskell - 这些类似 Free 的结构有一个概括吗?

我正在玩一些类似免费的想法,发现了这个:

我想找到可以编写函数的条件:

但我一直无法找到一个有意义的结构f(除了一个简单的结构,其中mkFree是 的一种方法???),它允许编写这个函数。特别是,如果这个结构没有提到Free类型,我的审美会更喜欢。

有没有人见过这样的东西?这种概括可能吗?在我还没有想到的方向上是否有一个已知的概括?

0 投票
2 回答
664 浏览

haskell - 如何使用免费的 monad 实现 Reader?

好的,所以我已经弄清楚了如何使用该包来实现Reader(并且ReaderT未显示) :operational

但是我一生无法弄清楚如何使用免费的单子来做到这一点(我正在使用 Edward Kmett 的free包)。我得到的最接近的是这个,我知道这是作弊(关于如何((->) r)已经是一个单子的东西):

即使这不像我怀疑的那样愚蠢,这也不是我想要的,因为我想要的基本上是能够检查 a Readeras 数据......

0 投票
2 回答
935 浏览

haskell - Applying Semantics to Free Monads

I am trying to abstract the pattern of applying a certain semantics to a free monad over some functor. The running example I am using to motivate this is applying updates to an entity in a game. So I import a few libraries and define a few example types and an entity class for the purposes of this example (I am using the free monad implementation in control-monad-free):

I now lift some basic updates into the monad:

Now we have the free monad, we need to provide the possibility of different implementations, or semantic interpretations, of monad instance such as test above. The best pattern I can come up with for this is given by the following function:

Then with some basic semantic functions we can give the two following possible interpretations, one as a basic evaluation and one as a writer monad preforming logging:

Testing this in GHCI:

This all works fine, but it gives me a slightly uneasy feeling that it could be more general, or could be better organised. Having to provide a function to provide the continuation wasn't obvious at first and I'm not sure it is the best approach. I have made several efforts to redefine interpret in terms of functions in the Control.Monad.Free module, such as foldFree and induce. But they all seem to not quite work.

Am I on the right lines with this, or am a making a misjudgement? Most of the articles on free monads I have found concentrate on their efficiency or different ways to implement them, rather than on patterns for actually using them like this.

It also seems desirable to encapsulate this in some kind of Semantic class, so I could simply make different monad instances from my free monad by wrapping the functor in a newtype and making it an instance of this class. I couldn't quite work out how to do this however.

UPDATE --

I wish I could have accepted both answers as they are both extremely informative and thoughtfully written. In the end though, the edit to the accepted answer contains the function I was after:

(retract and hoistFree are in Edward Kemmet's free package in Control.Monad.Free).

All three of pipes, operational and sacundim's free-operational package are very relevant and look like they will be very useful for me in the future. Thank you all.

0 投票
1 回答
384 浏览

haskell - 有免费的代理变压器吗?

你认为免费的代理变压器是可能的吗?就像是

这不仅是好奇心,我实际上会发现这很有用。

0 投票
2 回答
3400 浏览

haskell - 自由单子和函子的固定点之间的区别?

我正在阅读http://www.haskellforall.com/2013/06/from-zero-to-cooperative-threads-in-33.html,其中抽象语法树派生为代表一组的函子的自由单子指示。我注意到自由单子Free与函子Fix上的定点运算符没有太大区别。

本文使用 monad 操作和do语法以简洁的方式构建这些 AST(固定点)。我想知道这是否是免费 monad 实例的唯一好处?它还支持其他有趣的应用程序吗?