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