所提出的案例是否可以优化为一个循环?

发布于 2025-01-03 22:31:22 字数 448 浏览 1 评论 0原文

假设我有两个函数 f :: [a] -> bg :: [a] -> c.我有以下两个问题:

  1. 如果我执行 (f &&& g) xs where xs :: [a],并且如果都 < code>f和g涉及循环,编译器是否可以将这两个循环优化为一个? (请注意,我并不是在问某个特定的 Haskell 编译器是否实现了这一点。我想知道这样的事情是否可能。)

  2. traverse 函数可以从Traverse 类型类帮助我进行了这样的优化,如下所示:

    遍历(someCombinator fg)xs
    

Suppose I have two functions f :: [a] -> b and g :: [a] -> c. I have the following two questions:

  1. If I perform (f &&& g) xs where xs :: [a], and if both f and g 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.)

  2. Can the traverse function from Traverse type class help me have such an optimization with something along the following lines:

    traverse (someCombinator f g) xs
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新一帅帅 2025-01-10 22:31:22

理论上可以优化此代码,但非常非常困难,因为 fg 可能会消耗不同数量的输入列表。只有当它们消耗相同的量,或者 g 总是比 f 消耗更多的列表(反之亦然)时,才有可能执行优化。停止问题会阻止编译器检测复杂代码中的此类情况。

仅在简单的情况下,例如,fg 都在内部使用 foldr,才有可能执行简单的优化而无需进一步内省。

traverse 函数在这里对您没有帮助,因为没有合理的方法来实现 someCombinator (您想如何转换 a 的多个调用)进入一个[a]而没有严重的黑客攻击?然后你又回到了开始的地方)。

您唯一真正的选择是将 fg 放入文件夹中,以便它们具有签名 f :: a -> b-> bg :: a -> c-> c,意味着bc的值可以增量计算。然后您可以使用 \ a -> fa *** g a 获取可以在常规(在本例中为右侧)折叠中使用的文件夹。

It is theoretically possible to optimize this code, but very very difficult, because f and g might consume different amounts of the input list. Only when they consume the same amount, or g always consumes more of the list than f (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 and g use foldr 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 implementing someCombinator (How do you want to transform multiple calls of a's into one [a] without serious hacks? And then you are back where you started anyways).

Your only real option is to make f and g into folders, so that they have the signatures f :: a -> b -> b and g :: a -> c -> c, meaning that the value of b and c 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文