[英]Seemingly legal Eta reduction causing issues
I am attempting to η-reduce the function 我试图η-减少功能
foldr :: (a -> b -> b) -> b -> BinaryTree a -> b
foldr combiner base tree = foldMap combiner tree base where
foldMap = ...
with 同
foldMap :: (a -> b -> b) -> BinaryTree a -> b -> b
working as intended. 按预期工作。
I have η-reduced 我减少了η
foldr combiner base tree = foldMap combiner tree base
to 至
foldr combiner = flip $ foldMap combiner where
...
This works as intended. 这按预期工作。 It seems like I should be able to η-reduce completely to get the pointfree function
看起来我应该能够完全η-reduce以获得无点功能
foldr = flip $ foldMap where
...
However, this causes a compilation error 但是,这会导致编译错误
Couldn't match type ‘a -> b -> b’ with ‘BinaryTree t0’
Expected type: (a -> b -> b) -> b -> BinaryTree a -> b
Actual type: BinaryTree t0 -> (t0 -> b -> b) -> b -> b
Is it possible to η-reduce farther, and if so, how? 有可能η-减少更远,如果是这样,怎么样?
The error is raised, because gb = f $ ab
is not equivalent to g = f $ a
. 引发错误,因为
gb = f $ ab
不等于g = f $ a
。
In the first case you get the following sequence of evaluation: 在第一种情况下,您将获得以下评估顺序:
a
to b
(call a
with b
as an argument) a
应用于b
(使用b
作为参数调用a
) f
to the result f
应用于结果 In the second case: 在第二种情况:
f
to a
f
应用于a
Thus, you just flip
the foldMap
function, but actually want to flip
the foldMap
function after passing combiner
to it. 因此,您只需要
flip
foldMap
函数,但实际上想要在将combiner
传递给它之后 flip
foldMap
函数。 This leads us to the conclusion, that you actually want the composition, ie .
这导致我们得出结论,你实际上想要的是组合,即
.
function: 功能:
foldr = flip . foldMap where
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.