[英]Seemingly legal Eta reduction causing issues
我试图η-减少功能
foldr :: (a -> b -> b) -> b -> BinaryTree a -> b
foldr combiner base tree = foldMap combiner tree base where
foldMap = ...
同
foldMap :: (a -> b -> b) -> BinaryTree a -> b -> b
按预期工作。
我减少了η
foldr combiner base tree = foldMap combiner tree base
至
foldr combiner = flip $ foldMap combiner where
...
这按预期工作。 看起来我应该能够完全η-reduce以获得无点功能
foldr = flip $ foldMap where
...
但是,这会导致编译错误
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
有可能η-减少更远,如果是这样,怎么样?
引发错误,因为gb = f $ ab
不等于g = f $ a
。
在第一种情况下,您将获得以下评估顺序:
a
应用于b
(使用b
作为参数调用a
) f
应用于结果 在第二种情况:
f
应用于a
因此,您只需要flip
foldMap
函数,但实际上想要在将combiner
传递给它之后 flip
foldMap
函数。 这导致我们得出结论,你实际上想要的是组合,即.
功能:
foldr = flip . foldMap where
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.