Ticket #3999: map-folding.patch

File map-folding.patch, 4.0 KB (added by LouisWasserman, 3 years ago)

A patch improving the Foldable instance for Data.Map and adding useful deforesting rewrite rules.

Line 
1Wed Apr 21 17:57:22 CDT 2010  wasserman.louis@gmail.com
2  * Improved Foldable instance and fold-related rewrite rules for Data.Map
3
4New patches:
5
6[Improved Foldable instance and fold-related rewrite rules for Data.Map
7wasserman.louis@gmail.com**20100421225722
8 Ignore-this: 84a727887cd85fdd45469e5f1e46f276
9] {
10hunk ./Data/Map.hs 177
11             , valid
12             ) where
13 
14-import Prelude hiding (lookup,map,filter,null)
15+import Prelude hiding (lookup,map,filter,null,foldr,foldl)
16 import qualified Data.Set as Set
17 import qualified Data.List as List
18 import Data.Monoid (Monoid(..))
19hunk ./Data/Map.hs 183
20 import Control.Applicative (Applicative(..), (<$>))
21 import Data.Traversable (Traversable(traverse))
22-import Data.Foldable (Foldable(foldMap))
23+import Data.Foldable (Foldable(foldr, foldl, foldMap))
24 #ifndef __GLASGOW_HASKELL__
25 import Data.Typeable ( Typeable, typeOf, typeOfDefault
26                      , Typeable1, typeOf1, typeOf1Default)
27hunk ./Data/Map.hs 1545
28 toList :: Map k a -> [(k,a)]
29 toList t      = toAscList t
30 
31+{-# NOINLINE [0] toAscList #-}
32 -- | /O(n)/. Convert to an ascending list.
33 --
34 -- > toAscList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
35hunk ./Data/Map.hs 1974
36   foldMap _f Tip = mempty
37   foldMap f (Bin _s _k v l r)
38     = foldMap f l `mappend` f v `mappend` foldMap f r
39+  foldr f = foldrWithKey (const f)
40+  foldl f = foldlWithKey (const . f)
41 
42 {--------------------------------------------------------------------
43   Read
44hunk ./Data/Map.hs 2351
45   = (sort (nub xs) == [x | (x,()) <- toList (fromList [(x,()) | x <- xs])])
46 -}
47 
48+
49+{-# RULES
50+       "foldr/Data.Map.elems" forall f z m . List.foldr f z (elems m) = foldr f z m;
51+       "foldl/Data.Map.elems" forall f z m . List.foldl f z (elems m) = foldl f z m;
52+       "foldr/Data.Map.keys" forall f z m . List.foldr f z (keys m) = foldrWithKey (\ k _ -> f k) z m;
53+       "foldl/Data.Map.keys" forall f z m . List.foldl f z (keys m) = foldlWithKey (\ z k _ -> f z k) z m;
54+       "foldr/Data.Map.toAscList" forall f z m . List.foldr f z (toAscList m) = foldrWithKey (curry f) z m;
55+       "foldl/Data.Map.toAscList" forall f z m . List.foldl f z (toAscList m) = foldlWithKey (curry . f) z m;
56+       #-}
57}
58
59Context:
60
61[Tweak layout to work with the alternative layout rule
62Ian Lynagh <igloo@earth.li>**20091129154519]
63[Disable building Data.Sequence (and dependents) for nhc98.
64Malcolm.Wallace@cs.york.ac.uk**20091124025653
65 There is some subtlety of polymorphically recursive datatypes and
66 type-class defaulting that nhc98's type system barfs over.
67]
68[Fix another instance of non-ghc breakage.
69Malcolm.Wallace@cs.york.ac.uk**20091123092637]
70[Add #ifdef around ghc-only (<$) as member of Functor class.
71Malcolm.Wallace@cs.york.ac.uk**20091123085155]
72[Fix broken code in non-GHC branch of an ifdef.
73Malcolm.Wallace@cs.york.ac.uk**20091123084824]
74[doc bugfix: correct description of index argument
75Ross Paterson <ross@soi.city.ac.uk>**20091028105532
76 Ignore-this: 9790e7bf422c4cb528722c03cfa4fed9
77 
78 As noted by iaefai on the libraries list.
79 
80 Please merge to STABLE.
81]
82[Bump version to 0.3.0.0
83Ian Lynagh <igloo@earth.li>**20090920141847]
84[update base dependency
85Ross Paterson <ross@soi.city.ac.uk>**20090916073125
86 Ignore-this: ad382ffc6c6a18c15364e6c072f19edb
87 
88 The package uses mkNoRepType and Data.Functor, which were not in the
89 stable branch of base-4.
90]
91[add fast version of <$ for Seq
92Ross Paterson <ross@soi.city.ac.uk>**20090916072812
93 Ignore-this: 5a39a7d31d39760ed589790b1118d240
94]
95[new methods for Data.Sequence (proposal #3271)
96Ross Paterson <ross@soi.city.ac.uk>**20090915173324
97 Ignore-this: cf17bedd709a6ab3448fd718dcdf62e7
98 
99 Adds a lot of new methods to Data.Sequence, mostly paralleling those
100 in Data.List.  Several of these are significantly faster than versions
101 implemented with the previous public interface.  In particular, replicate
102 takes O(log n) time and space instead of O(n).
103 (by Louis Wasserman)
104]
105[Fix "Cabal check" warnings
106Ian Lynagh <igloo@earth.li>**20090811215900]
107[TAG 2009-06-25
108Ian Lynagh <igloo@earth.li>**20090625160202]
109Patch bundle hash:
110cc5c8751bda2ece10c3ef250b773de1761003a8d