2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 \section[ListSetOps]{Set-like operations on lists}
9 unionLists, minusList, insertList,
12 Assoc, assoc, assocMaybe, assocUsing, assocDefault, assocDefaultUsing,
13 emptyAssoc, unitAssoc, mapAssoc, plusAssoc_C, extendAssoc_C,
14 mkLookupFun, findInList, assocElts,
17 hasNoDups, runs, removeDups, findDupsEq,
18 equivClasses, equivClassesByUniq
22 #include "HsVersions.h"
33 %************************************************************************
35 Treating lists as sets
36 Assumes the lists contain no duplicates, but are unordered
38 %************************************************************************
41 insertList :: Eq a => a -> [a] -> [a]
42 -- Assumes the arg list contains no dups; guarantees the result has no dups
43 insertList x xs | isIn "insert" x xs = xs
46 unionLists :: (Outputable a, Eq a) => [a] -> [a] -> [a]
47 -- Assumes that the arguments contain no duplicates
49 = WARN(length xs > 100 || length ys > 100, ppr xs $$ ppr ys)
50 [x | x <- xs, isn'tIn "unionLists" x ys] ++ ys
52 minusList :: (Eq a) => [a] -> [a] -> [a]
53 -- Everything in the first list that is not in the second list:
54 minusList xs ys = [ x | x <- xs, isn'tIn "minusList" x ys]
58 %************************************************************************
60 \subsection[Utils-assoc]{Association lists}
62 %************************************************************************
64 Inefficient finite maps based on association lists and equality.
67 -- A finite mapping based on equality and association lists
68 type Assoc a b = [(a,b)]
70 emptyAssoc :: Assoc a b
71 unitAssoc :: a -> b -> Assoc a b
72 assocElts :: Assoc a b -> [(a,b)]
73 assoc :: (Eq a) => String -> Assoc a b -> a -> b
74 assocDefault :: (Eq a) => b -> Assoc a b -> a -> b
75 assocUsing :: (a -> a -> Bool) -> String -> Assoc a b -> a -> b
76 assocMaybe :: (Eq a) => Assoc a b -> a -> Maybe b
77 assocDefaultUsing :: (a -> a -> Bool) -> b -> Assoc a b -> a -> b
78 mapAssoc :: (b -> c) -> Assoc a b -> Assoc a c
79 extendAssoc_C :: (Eq a) => (b -> b -> b) -> Assoc a b -> (a,b) -> Assoc a b
80 plusAssoc_C :: (Eq a) => (b -> b -> b) -> Assoc a b -> Assoc a b -> Assoc a b
81 -- combining fn takes (old->new->result)
84 unitAssoc a b = [(a,b)]
87 assocDefaultUsing _ deflt [] _ = deflt
88 assocDefaultUsing eq deflt ((k,v) : rest) key
90 | otherwise = assocDefaultUsing eq deflt rest key
92 assoc crash_msg list key = assocDefaultUsing (==) (panic ("Failed in assoc: " ++ crash_msg)) list key
93 assocDefault deflt list key = assocDefaultUsing (==) deflt list key
94 assocUsing eq crash_msg list key = assocDefaultUsing eq (panic ("Failed in assoc: " ++ crash_msg)) list key
100 lookup ((tv,ty):rest) = if key == tv then Just ty else lookup rest
102 mapAssoc f alist = [(key, f val) | (key,val) <- alist]
104 plusAssoc_C _ [] new = new -- Shortcut for common case
105 plusAssoc_C combine old new = foldl (extendAssoc_C combine) old new
107 extendAssoc_C combine old_list (new_key, new_val)
110 go [] = [(new_key, new_val)]
111 go ((old_key, old_val) : old_list)
112 | new_key == old_key = ((old_key, old_val `combine` new_val) : old_list)
113 | otherwise = (old_key, old_val) : go old_list
117 @mkLookupFun eq alist@ is a function which looks up
118 its argument in the association list @alist@, returning a Maybe type.
119 @mkLookupFunDef@ is similar except that it is given a value to return
123 mkLookupFun :: (key -> key -> Bool) -- Equality predicate
124 -> [(key,val)] -- The assoc list
126 -> Maybe val -- The corresponding value
128 mkLookupFun eq alist s
129 = case [a | (s',a) <- alist, s' `eq` s] of
133 findInList :: (a -> Bool) -> [a] -> Maybe a
134 findInList _ [] = Nothing
135 findInList p (x:xs) | p x = Just x
136 | otherwise = findInList p xs
140 %************************************************************************
142 \subsection[Utils-dups]{Duplicate-handling}
144 %************************************************************************
147 hasNoDups :: (Eq a) => [a] -> Bool
149 hasNoDups xs = f [] xs
152 f seen_so_far (x:xs) = if x `is_elem` seen_so_far
154 else f (x:seen_so_far) xs
156 is_elem = isIn "hasNoDups"
160 equivClasses :: (a -> a -> Ordering) -- Comparison
164 equivClasses _ [] = []
165 equivClasses _ stuff@[_] = [stuff]
166 equivClasses cmp items = runs eq (sortLe le items)
168 eq a b = case cmp a b of { EQ -> True; _ -> False }
169 le a b = case cmp a b of { LT -> True; EQ -> True; GT -> False }
172 The first cases in @equivClasses@ above are just to cut to the point
175 @runs@ groups a list into a list of lists, each sublist being a run of
176 identical elements of the input list. It is passed a predicate @p@ which
177 tells when two elements are equal.
180 runs :: (a -> a -> Bool) -- Equality
185 runs p (x:xs) = case (span (p x) xs) of
186 (first, rest) -> (x:first) : (runs p rest)
190 removeDups :: (a -> a -> Ordering) -- Comparison function
192 -> ([a], -- List with no duplicates
193 [[a]]) -- List of duplicate groups. One representative from
194 -- each group appears in the first result
196 removeDups _ [] = ([], [])
197 removeDups _ [x] = ([x],[])
199 = case (mapAccumR collect_dups [] (equivClasses cmp xs)) of { (dups, xs') ->
202 collect_dups _ [] = panic "ListSetOps: removeDups"
203 collect_dups dups_so_far [x] = (dups_so_far, x)
204 collect_dups dups_so_far dups@(x:_) = (dups:dups_so_far, x)
206 findDupsEq :: (a->a->Bool) -> [a] -> [[a]]
208 findDupsEq eq (x:xs) | null eq_xs = findDupsEq eq xs
209 | otherwise = (x:eq_xs) : findDupsEq eq neq_xs
210 where (eq_xs, neq_xs) = partition (eq x) xs
215 equivClassesByUniq :: (a -> Unique) -> [a] -> [[a]]
216 -- NB: it's *very* important that if we have the input list [a,b,c],
217 -- where a,b,c all have the same unique, then we get back the list
221 -- Hence the use of foldr, plus the reversed-args tack_on below
222 equivClassesByUniq get_uniq xs
223 = eltsUFM (foldr add emptyUFM xs)
225 add a ufm = addToUFM_C tack_on ufm (get_uniq a) [a]
226 tack_on old new = new++old