繁体   English   中英

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

[英]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.

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