@@ -1754,8 +1754,9 @@ lookupOf l k = foldrOf l (\(k',v) next -> if k == k' then Just v else next) Noth
1754
1754
-- 'foldr1Of' :: 'Traversal'' s a -> (a -> a -> a) -> s -> a
1755
1755
-- @
1756
1756
foldr1Of :: HasCallStack => Getting (Endo (Maybe a )) s a -> (a -> a -> a ) -> s -> a
1757
- foldr1Of l f xs = fromMaybe (error " foldr1Of: empty structure" )
1758
- (foldrOf l mf Nothing xs) where
1757
+ -- See: NOTE: [Inlining and arity]
1758
+ foldr1Of l f = fromMaybe (error " foldr1Of: empty structure" )
1759
+ . foldrOf l mf Nothing where
1759
1760
mf x my = Just $ case my of
1760
1761
Nothing -> x
1761
1762
Just y -> f x y
@@ -1780,7 +1781,8 @@ foldr1Of l f xs = fromMaybe (error "foldr1Of: empty structure")
1780
1781
-- 'foldl1Of' :: 'Traversal'' s a -> (a -> a -> a) -> s -> a
1781
1782
-- @
1782
1783
foldl1Of :: HasCallStack => Getting (Dual (Endo (Maybe a ))) s a -> (a -> a -> a ) -> s -> a
1783
- foldl1Of l f xs = fromMaybe (error " foldl1Of: empty structure" ) (foldlOf l mf Nothing xs) where
1784
+ -- See: NOTE: [Inlining and arity]
1785
+ foldl1Of l f = fromMaybe (error " foldl1Of: empty structure" ) . foldlOf l mf Nothing where
1784
1786
mf mx y = Just $ case mx of
1785
1787
Nothing -> y
1786
1788
Just x -> f x y
@@ -1800,7 +1802,8 @@ foldl1Of l f xs = fromMaybe (error "foldl1Of: empty structure") (foldlOf l mf No
1800
1802
-- 'foldrOf'' :: 'Traversal'' s a -> (a -> r -> r) -> r -> s -> r
1801
1803
-- @
1802
1804
foldrOf' :: Getting (Dual (Endo (Endo r ))) s a -> (a -> r -> r ) -> r -> s -> r
1803
- foldrOf' l f z0 xs = foldlOf l f' (Endo id ) xs `appEndo` z0
1805
+ -- See: NOTE: [Inlining and arity]
1806
+ foldrOf' l f z0 = \ xs -> foldlOf l f' (Endo id ) xs `appEndo` z0
1804
1807
where f' (Endo k) x = Endo $ \ z -> k $! f x z
1805
1808
{-# INLINE foldrOf' #-}
1806
1809
@@ -1818,7 +1821,8 @@ foldrOf' l f z0 xs = foldlOf l f' (Endo id) xs `appEndo` z0
1818
1821
-- 'foldlOf'' :: 'Traversal'' s a -> (r -> a -> r) -> r -> s -> r
1819
1822
-- @
1820
1823
foldlOf' :: Getting (Endo (Endo r )) s a -> (r -> a -> r ) -> r -> s -> r
1821
- foldlOf' l f z0 xs = foldrOf l f' (Endo id ) xs `appEndo` z0
1824
+ -- See: NOTE: [Inlining and arity]
1825
+ foldlOf' l f z0 = \ xs -> foldrOf l f' (Endo id ) xs `appEndo` z0
1822
1826
where f' x (Endo k) = Endo $ \ z -> k $! f z x
1823
1827
{-# INLINE foldlOf' #-}
1824
1828
@@ -1838,7 +1842,8 @@ foldlOf' l f z0 xs = foldrOf l f' (Endo id) xs `appEndo` z0
1838
1842
-- 'foldr1Of'' :: 'Traversal'' s a -> (a -> a -> a) -> s -> a
1839
1843
-- @
1840
1844
foldr1Of' :: HasCallStack => Getting (Dual (Endo (Endo (Maybe a )))) s a -> (a -> a -> a ) -> s -> a
1841
- foldr1Of' l f xs = fromMaybe (error " foldr1Of': empty structure" ) (foldrOf' l mf Nothing xs) where
1845
+ -- See: NOTE: [Inlining and arity]
1846
+ foldr1Of' l f = fromMaybe (error " foldr1Of': empty structure" ) . foldrOf' l mf Nothing where
1842
1847
mf x Nothing = Just $! x
1843
1848
mf x (Just y) = Just $! f x y
1844
1849
{-# INLINE foldr1Of' #-}
@@ -1859,7 +1864,8 @@ foldr1Of' l f xs = fromMaybe (error "foldr1Of': empty structure") (foldrOf' l mf
1859
1864
-- 'foldl1Of'' :: 'Traversal'' s a -> (a -> a -> a) -> s -> a
1860
1865
-- @
1861
1866
foldl1Of' :: HasCallStack => Getting (Endo (Endo (Maybe a ))) s a -> (a -> a -> a ) -> s -> a
1862
- foldl1Of' l f xs = fromMaybe (error " foldl1Of': empty structure" ) (foldlOf' l mf Nothing xs) where
1867
+ -- See: NOTE: [Inlining and arity]
1868
+ foldl1Of' l f = fromMaybe (error " foldl1Of': empty structure" ) . foldlOf' l mf Nothing where
1863
1869
mf Nothing y = Just $! y
1864
1870
mf (Just x) y = Just $! f x y
1865
1871
{-# INLINE foldl1Of' #-}
@@ -1881,7 +1887,8 @@ foldl1Of' l f xs = fromMaybe (error "foldl1Of': empty structure") (foldlOf' l mf
1881
1887
foldrMOf :: Monad m
1882
1888
=> Getting (Dual (Endo (r -> m r ))) s a
1883
1889
-> (a -> r -> m r ) -> r -> s -> m r
1884
- foldrMOf l f z0 xs = foldlOf l f' return xs z0
1890
+ -- See: NOTE: [Inlining and arity]
1891
+ foldrMOf l f z0 = \ xs -> foldlOf l f' return xs z0
1885
1892
where f' k x z = f x z >>= k
1886
1893
{-# INLINE foldrMOf #-}
1887
1894
@@ -1902,10 +1909,26 @@ foldrMOf l f z0 xs = foldlOf l f' return xs z0
1902
1909
foldlMOf :: Monad m
1903
1910
=> Getting (Endo (r -> m r )) s a
1904
1911
-> (r -> a -> m r ) -> r -> s -> m r
1905
- foldlMOf l f z0 xs = foldrOf l f' return xs z0
1912
+ -- See: NOTE: [Inlining and arity]
1913
+ foldlMOf l f z0 = \ xs -> foldrOf l f' return xs z0
1906
1914
where f' x k z = f z x >>= k
1907
1915
{-# INLINE foldlMOf #-}
1908
1916
1917
+ -- NOTE: [Inlining and arity]
1918
+ -- ~~~~~~~~~~~~~~~~~~~~~~~~~~
1919
+ --
1920
+ -- GHC uses the following inlining heuristic: a function body is inlined if
1921
+ -- all its arguments on the LHS are applied. So the following two definitions
1922
+ -- are not equivalent from the inliner's PoV:
1923
+ --
1924
+ -- > foldlOf' l f z0 xs = ...
1925
+ -- > foldlOf' l f z0 = \xs -> ...
1926
+ --
1927
+ -- GHC will be less eager to inline the first one and this results in
1928
+ -- worse code. For example, a simple list summation using `sumOf` will be 8x slower
1929
+ -- with the first version.
1930
+
1931
+
1909
1932
-- | Check to see if this 'Fold' or 'Traversal' matches 1 or more entries.
1910
1933
--
1911
1934
-- >>> has (element 0) []
0 commit comments