所提出的案例是否可以优化为一个循环?
假设我有两个函数 f :: [a] -> b
和 g :: [a] -> c
.我有以下两个问题:
如果我执行
(f &&& g) xs
wherexs :: [a]
,并且如果都 < code>f和g
涉及循环,编译器是否可以将这两个循环优化为一个? (请注意,我并不是在问某个特定的 Haskell 编译器是否实现了这一点。我想知道这样的事情是否可能。)traverse
函数可以从Traverse
类型类帮助我进行了这样的优化,如下所示:遍历(someCombinator fg)xs
Suppose I have two functions f :: [a] -> b
and g :: [a] -> c
. I have the following two questions:
If I perform
(f &&& g) xs
wherexs :: [a]
, and if bothf
andg
involve loops, is it possible for the compiler to optimize these two loops into one? (Please note that I am not asking whether some specific Haskell compiler implements this. I want to know whether such a thing is possible.)Can the
traverse
function fromTraverse
type class help me have such an optimization with something along the following lines:traverse (someCombinator f g) xs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
理论上可以优化此代码,但非常非常困难,因为
f
和g
可能会消耗不同数量的输入列表。只有当它们消耗相同的量,或者g
总是比f
消耗更多的列表(反之亦然)时,才有可能执行优化。停止问题会阻止编译器检测复杂代码中的此类情况。仅在简单的情况下,例如,
f
和g
都在内部使用foldr
,才有可能执行简单的优化而无需进一步内省。traverse
函数在这里对您没有帮助,因为没有合理的方法来实现someCombinator
(您想如何转换a
的多个调用)进入一个[a]
而没有严重的黑客攻击?然后你又回到了开始的地方)。您唯一真正的选择是将
f
和g
放入文件夹中,以便它们具有签名f :: a -> b-> b
和g :: a -> c-> c
,意味着b
和c
的值可以增量计算。然后您可以使用\ a -> fa *** g a
获取可以在常规(在本例中为右侧)折叠中使用的文件夹。It is theoretically possible to optimize this code, but very very difficult, because
f
andg
might consume different amounts of the input list. Only when they consume the same amount, org
always consumes more of the list thanf
(or vice versa), would it be possible to perform the optimization. The Halting Problem prevents a compiler from detecting such conditions in complex code.Only in simple cases, where both
f
andg
usefoldr
internally, for example, would it be possible to perform trivial optimizations without further introspection.The
traverse
function will not help you here, because there is no reasonable way of implementingsomeCombinator
(How do you want to transform multiple calls ofa
's into one[a]
without serious hacks? And then you are back where you started anyways).Your only real option is to make
f
andg
into folders, so that they have the signaturesf :: a -> b -> b
andg :: a -> c -> c
, meaning that the value ofb
andc
can be computed incrementally. You can then use\ a -> f a *** g a
to get a folder that you can use in a conventional (right- in this case) fold.