是否有可能为`Free'实现MonadFix? [英] Is it possible to implement MonadFix for `Free`?
问题描述
中的 http://hackage.haskell.org/package/free Control.Monad .Free.Free
可以让任何给定的 Functor
访问free monad。但是,它不包含 MonadFix
实例。这是因为这样一个实例不能被写入,或者它被遗漏了吗? 如果这样的实例不能被写入,为什么不能?
考虑描述 mfix
的作用:
单点计算的固定点。
mfix f
只执行一次动作f
,并将最终输出作为输入反馈。
$ $ b在
Free
的上下文中,执行一词意味着创建函子
。因此,只有一次意味着在评估mfix f
的结果中,Pure
构造函数中保存的值必须完全确定有多少个函数层被创建。
现在,假设我们有一个特定的函数
一次
,我们知道将始终只创建一个Free
构造函数,但是需要很多Pure
构造函数来保存叶值。那么,'once'的输出将只是类型Free fa
的值,它们同构于类型fa $ c $的某个值C>。有了这些知识,我们可以安全地取消< - code> Free
一次
的输出,以获得<$ c
$ b现在请注意,只执行一次动作,
因此,一个符合实例mfix一次
的结果对于符合的实例应该不包含比一次的其他一元结构一次
在单个应用程序中创建。因此,我们可以推断,从mfix获得的值必须同构于<给定任何类型为
a - >的函数。对于一些
Functor
f
,我们可以用一个Free
并获得一个类型为a - >的函数。免费fa
满足上面一次
的描述,并且我们已经确定可以解开mfix的结果一次
来获取类型fa
返回值。(Functor f)=> MonadFix(Free f)
意味着能够通过上述的打包和解包来写入函数ffix ::(Functor f)=> (a - > f a) - > fa
,它可以用于Functor
的所有实例。
这显然不是证明你不能写这样一个实例......但是如果可能的话,
MonadFix
将是完全多余的,因为你可以轻松地直接写ffix
。 (我想用> liftM $>将它重新实现为
mfix
并带有Monad
约束。 c $ c>。呃。)http://hackage.haskell.org/package/free in
Control.Monad.Free.Free
allows one to get access to the "free monad" for any givenFunctor
. It does not, however, have aMonadFix
instance. Is this because such an instance cannot be written, or was it just left out?If such an instance cannot be written, why not?
解决方案Consider the description of what
mfix
does:The fixed point of a monadic computation.
mfix f
executes the actionf
only once, with the eventual output fed back as the input.The word "executes", in the context of
Free
, means creating layers of theFunctor
. Thus, "only once" means that in the result of evaluatingmfix f
, the values held inPure
constructors must fully determine how many layers of the functor are created.Now, say we have a specific function
once
that we know will always only create a singleFree
constructor, plus however manyPure
constructors are needed to hold the leaf values. The output of 'once', then, will be only values of typeFree f a
that are isomorphic to some value of typef a
. With this knowledge, we can un-Free
the output ofonce
safely, to get a value of typef a
.Now, note that because
mfix
is required to "execute the action only once", the result ofmfix once
should, for a conforming instance, contain no additional monadic structure thanonce
creates in a single application. Thus we can deduce that the value obtained frommfix once
must also be isomorphic to a value of typef a
.Given any function with type
a -> f a
for someFunctor
f
, we can wrap the result with a singleFree
and get a function of typea -> Free f a
which satisfies the description ofonce
above, and we've already established that we can unwrap the result ofmfix once
to get a value of typef a
back.Therefore, a conforming instance
(Functor f) => MonadFix (Free f)
would imply being able to write, via the wrapping and unwrapping described above, a functionffix :: (Functor f) => (a -> f a) -> f a
that would work for all instances ofFunctor
.That's obviously not a proof that you can't write such an instance... but if it were possible,
MonadFix
would be completely superfluous, because you could just as easily writeffix
directly. (And I suppose reimplement it asmfix
with aMonad
constraint, usingliftM
. Ugh.)这篇关于是否有可能为`Free'实现MonadFix?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!