简体   繁体   English

看似合法的Eta减少导致问题

[英]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: 在第一种情况下,您将获得以下评估顺序:

  • apply the function a to b (call a with b as an argument) 将函数a应用于b (使用b作为参数调用a
  • apply the function f to the result 将函数f应用于结果

In the second case: 在第二种情况:

  • apply the function 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM