2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[ListSetOps]{Set-like operations on lists}
11 Assoc, assoc, assocMaybe, assocUsing, assocDefault, assocDefaultUsing,
12 emptyAssoc, unitAssoc, mapAssoc, plusAssoc_C, extendAssoc_C,
13 mkLookupFun, assocElts,
16 hasNoDups, runs, removeDups, removeDupsEq,
17 equivClasses, equivClassesByUniq
21 #include "HsVersions.h"
24 import Unique ( Unique )
25 import UniqFM ( eltsUFM, emptyUFM, addToUFM_C )
26 import Util ( isn'tIn, isIn, mapAccumR, sortLt )
31 %************************************************************************
33 \subsection{Treating lists as sets}
35 %************************************************************************
38 unionLists :: (Eq a) => [a] -> [a] -> [a]
42 Everything in the first list that is not in the second list:
45 minusList :: (Eq a) => [a] -> [a] -> [a]
46 minusList xs ys = [ x | x <- xs, x `not_elem` ys]
48 not_elem = isn'tIn "minusList"
52 %************************************************************************
54 \subsection[Utils-assoc]{Association lists}
56 %************************************************************************
58 Inefficient finite maps based on association lists and equality.
61 type Assoc a b = [(a,b)] -- A finite mapping based on equality and association lists
63 emptyAssoc :: Assoc a b
64 unitAssoc :: a -> b -> Assoc a b
65 assocElts :: Assoc a b -> [(a,b)]
66 assoc :: (Eq a) => String -> Assoc a b -> a -> b
67 assocDefault :: (Eq a) => b -> Assoc a b -> a -> b
68 assocUsing :: (a -> a -> Bool) -> String -> Assoc a b -> a -> b
69 assocMaybe :: (Eq a) => Assoc a b -> a -> Maybe b
70 assocDefaultUsing :: (a -> a -> Bool) -> b -> Assoc a b -> a -> b
71 mapAssoc :: (b -> c) -> Assoc a b -> Assoc a c
72 extendAssoc_C :: (Eq a) => (b -> b -> b) -> Assoc a b -> (a,b) -> Assoc a b
73 plusAssoc_C :: (Eq a) => (b -> b -> b) -> Assoc a b -> Assoc a b -> Assoc a b
74 -- combining fn takes (old->new->result)
77 unitAssoc a b = [(a,b)]
80 assocDefaultUsing eq deflt ((k,v) : rest) key
82 | otherwise = assocDefaultUsing eq deflt rest key
84 assocDefaultUsing eq deflt [] key = deflt
86 assoc crash_msg list key = assocDefaultUsing (==) (panic ("Failed in assoc: " ++ crash_msg)) list key
87 assocDefault deflt list key = assocDefaultUsing (==) deflt list key
88 assocUsing eq crash_msg list key = assocDefaultUsing eq (panic ("Failed in assoc: " ++ crash_msg)) list key
94 lookup ((tv,ty):rest) = if key == tv then Just ty else lookup rest
96 mapAssoc f alist = [(key, f val) | (key,val) <- alist]
98 plusAssoc_C combine [] new = new -- Shortcut for common case
99 plusAssoc_C combine old new = foldl (extendAssoc_C combine) old new
101 extendAssoc_C combine old_list (new_key, new_val)
104 go [] = [(new_key, new_val)]
105 go ((old_key, old_val) : old_list)
106 | new_key == old_key = ((old_key, old_val `combine` new_val) : old_list)
107 | otherwise = (old_key, old_val) : go old_list
111 @mkLookupFun eq alist@ is a function which looks up
112 its argument in the association list @alist@, returning a Maybe type.
113 @mkLookupFunDef@ is similar except that it is given a value to return
117 mkLookupFun :: (key -> key -> Bool) -- Equality predicate
118 -> [(key,val)] -- The assoc list
120 -> Maybe val -- The corresponding value
122 mkLookupFun eq alist s
123 = case [a | (s',a) <- alist, s' `eq` s] of
129 %************************************************************************
131 \subsection[Utils-dups]{Duplicate-handling}
133 %************************************************************************
136 hasNoDups :: (Eq a) => [a] -> Bool
138 hasNoDups xs = f [] xs
140 f seen_so_far [] = True
141 f seen_so_far (x:xs) = if x `is_elem` seen_so_far then
146 is_elem = isIn "hasNoDups"
150 equivClasses :: (a -> a -> Ordering) -- Comparison
154 equivClasses cmp stuff@[] = []
155 equivClasses cmp stuff@[item] = [stuff]
156 equivClasses cmp items
157 = runs eq (sortLt lt items)
159 eq a b = case cmp a b of { EQ -> True; _ -> False }
160 lt a b = case cmp a b of { LT -> True; _ -> False }
163 The first cases in @equivClasses@ above are just to cut to the point
166 @runs@ groups a list into a list of lists, each sublist being a run of
167 identical elements of the input list. It is passed a predicate @p@ which
168 tells when two elements are equal.
171 runs :: (a -> a -> Bool) -- Equality
176 runs p (x:xs) = case (span (p x) xs) of
177 (first, rest) -> (x:first) : (runs p rest)
181 removeDups :: (a -> a -> Ordering) -- Comparison function
183 -> ([a], -- List with no duplicates
184 [[a]]) -- List of duplicate groups. One representative from
185 -- each group appears in the first result
187 removeDups cmp [] = ([], [])
188 removeDups cmp [x] = ([x],[])
190 = case (mapAccumR collect_dups [] (equivClasses cmp xs)) of { (dups, xs') ->
193 collect_dups dups_so_far [x] = (dups_so_far, x)
194 collect_dups dups_so_far dups@(x:xs) = (dups:dups_so_far, x)
196 removeDupsEq :: Eq a => [a] -> ([a], [[a]])
197 -- Same, but with only equality
198 -- It's worst case quadratic, but we only use it on short lists
199 removeDupsEq [] = ([], [])
200 removeDupsEq (x:xs) | x `elem` xs = (ys, (x : filter (== x) xs) : zs)
202 (ys,zs) = removeDupsEq (filter (/= x) xs)
203 removeDupsEq (x:xs) | otherwise = (x:ys, zs)
205 (ys,zs) = removeDupsEq xs
210 equivClassesByUniq :: (a -> Unique) -> [a] -> [[a]]
211 -- NB: it's *very* important that if we have the input list [a,b,c],
212 -- where a,b,c all have the same unique, then we get back the list
216 -- Hence the use of foldr, plus the reversed-args tack_on below
217 equivClassesByUniq get_uniq xs
218 = eltsUFM (foldr add emptyUFM xs)
220 add a ufm = addToUFM_C tack_on ufm (get_uniq a) [a]
221 tack_on old new = new++old