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
31 %************************************************************************
33 Treating lists as sets
34 Assumes the lists contain no duplicates, but are unordered
36 %************************************************************************
39 insertList :: Eq a => a -> [a] -> [a]
40 -- Assumes the arg list contains no dups; guarantees the result has no dups
41 insertList x xs | isIn "insert" x xs = xs
44 unionLists :: (Eq a) => [a] -> [a] -> [a]
45 -- Assumes that the arguments contain no duplicates
46 unionLists xs ys = [x | x <- xs, isn'tIn "unionLists" x ys] ++ ys
48 minusList :: (Eq a) => [a] -> [a] -> [a]
49 -- Everything in the first list that is not in the second list:
50 minusList xs ys = [ x | x <- xs, isn'tIn "minusList" x ys]
54 %************************************************************************
56 \subsection[Utils-assoc]{Association lists}
58 %************************************************************************
60 Inefficient finite maps based on association lists and equality.
63 -- A finite mapping based on equality and association lists
64 type Assoc a b = [(a,b)]
66 emptyAssoc :: Assoc a b
67 unitAssoc :: a -> b -> Assoc a b
68 assocElts :: Assoc a b -> [(a,b)]
69 assoc :: (Eq a) => String -> Assoc a b -> a -> b
70 assocDefault :: (Eq a) => b -> Assoc a b -> a -> b
71 assocUsing :: (a -> a -> Bool) -> String -> Assoc a b -> a -> b
72 assocMaybe :: (Eq a) => Assoc a b -> a -> Maybe b
73 assocDefaultUsing :: (a -> a -> Bool) -> b -> Assoc a b -> a -> b
74 mapAssoc :: (b -> c) -> Assoc a b -> Assoc a c
75 extendAssoc_C :: (Eq a) => (b -> b -> b) -> Assoc a b -> (a,b) -> Assoc a b
76 plusAssoc_C :: (Eq a) => (b -> b -> b) -> Assoc a b -> Assoc a b -> Assoc a b
77 -- combining fn takes (old->new->result)
80 unitAssoc a b = [(a,b)]
83 assocDefaultUsing _ deflt [] _ = deflt
84 assocDefaultUsing eq deflt ((k,v) : rest) key
86 | otherwise = assocDefaultUsing eq deflt rest key
88 assoc crash_msg list key = assocDefaultUsing (==) (panic ("Failed in assoc: " ++ crash_msg)) list key
89 assocDefault deflt list key = assocDefaultUsing (==) deflt list key
90 assocUsing eq crash_msg list key = assocDefaultUsing eq (panic ("Failed in assoc: " ++ crash_msg)) list key
96 lookup ((tv,ty):rest) = if key == tv then Just ty else lookup rest
98 mapAssoc f alist = [(key, f val) | (key,val) <- alist]
100 plusAssoc_C _ [] new = new -- Shortcut for common case
101 plusAssoc_C combine old new = foldl (extendAssoc_C combine) old new
103 extendAssoc_C combine old_list (new_key, new_val)
106 go [] = [(new_key, new_val)]
107 go ((old_key, old_val) : old_list)
108 | new_key == old_key = ((old_key, old_val `combine` new_val) : old_list)
109 | otherwise = (old_key, old_val) : go old_list
113 @mkLookupFun eq alist@ is a function which looks up
114 its argument in the association list @alist@, returning a Maybe type.
115 @mkLookupFunDef@ is similar except that it is given a value to return
119 mkLookupFun :: (key -> key -> Bool) -- Equality predicate
120 -> [(key,val)] -- The assoc list
122 -> Maybe val -- The corresponding value
124 mkLookupFun eq alist s
125 = case [a | (s',a) <- alist, s' `eq` s] of
129 findInList :: (a -> Bool) -> [a] -> Maybe a
130 findInList _ [] = Nothing
131 findInList p (x:xs) | p x = Just x
132 | otherwise = findInList p xs
136 %************************************************************************
138 \subsection[Utils-dups]{Duplicate-handling}
140 %************************************************************************
143 hasNoDups :: (Eq a) => [a] -> Bool
145 hasNoDups xs = f [] xs
148 f seen_so_far (x:xs) = if x `is_elem` seen_so_far
150 else f (x:seen_so_far) xs
152 is_elem = isIn "hasNoDups"
156 equivClasses :: (a -> a -> Ordering) -- Comparison
160 equivClasses _ [] = []
161 equivClasses _ stuff@[_] = [stuff]
162 equivClasses cmp items = runs eq (sortLe le items)
164 eq a b = case cmp a b of { EQ -> True; _ -> False }
165 le a b = case cmp a b of { LT -> True; EQ -> True; GT -> False }
168 The first cases in @equivClasses@ above are just to cut to the point
171 @runs@ groups a list into a list of lists, each sublist being a run of
172 identical elements of the input list. It is passed a predicate @p@ which
173 tells when two elements are equal.
176 runs :: (a -> a -> Bool) -- Equality
181 runs p (x:xs) = case (span (p x) xs) of
182 (first, rest) -> (x:first) : (runs p rest)
186 removeDups :: (a -> a -> Ordering) -- Comparison function
188 -> ([a], -- List with no duplicates
189 [[a]]) -- List of duplicate groups. One representative from
190 -- each group appears in the first result
192 removeDups _ [] = ([], [])
193 removeDups _ [x] = ([x],[])
195 = case (mapAccumR collect_dups [] (equivClasses cmp xs)) of { (dups, xs') ->
198 collect_dups _ [] = panic "ListSetOps: removeDups"
199 collect_dups dups_so_far [x] = (dups_so_far, x)
200 collect_dups dups_so_far dups@(x:_) = (dups:dups_so_far, x)
202 findDupsEq :: (a->a->Bool) -> [a] -> [[a]]
204 findDupsEq eq (x:xs) | null eq_xs = findDupsEq eq xs
205 | otherwise = (x:eq_xs) : findDupsEq eq neq_xs
206 where (eq_xs, neq_xs) = partition (eq x) xs
211 equivClassesByUniq :: (a -> Unique) -> [a] -> [[a]]
212 -- NB: it's *very* important that if we have the input list [a,b,c],
213 -- where a,b,c all have the same unique, then we get back the list
217 -- Hence the use of foldr, plus the reversed-args tack_on below
218 equivClassesByUniq get_uniq xs
219 = eltsUFM (foldr add emptyUFM xs)
221 add a ufm = addToUFM_C tack_on ufm (get_uniq a) [a]
222 tack_on old new = new++old