簡體   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