Add kind equalities to GHC.
[ghc.git] / compiler / utils / ListSetOps.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
5 \section[ListSetOps]{Set-like operations on lists}
6 -}
7
8 {-# LANGUAGE CPP #-}
9
10 module ListSetOps (
11 unionLists, minusList, insertList,
12
13 -- Association lists
14 Assoc, assoc, assocMaybe, assocUsing, assocDefault, assocDefaultUsing,
15
16 -- Duplicate handling
17 hasNoDups, runs, removeDups, findDupsEq,
18 equivClasses, equivClassesByUniq,
19
20 -- Indexing
21 getNth
22 ) where
23
24 #include "HsVersions.h"
25
26 import Outputable
27 import Unique
28 import UniqFM
29 import Util
30
31 import Data.List
32
33 getNth :: Outputable a => [a] -> Int -> a
34 getNth xs n = ASSERT2( xs `lengthExceeds` n, ppr n $$ ppr xs )
35 xs !! n
36
37 {-
38 ************************************************************************
39 * *
40 Treating lists as sets
41 Assumes the lists contain no duplicates, but are unordered
42 * *
43 ************************************************************************
44 -}
45
46 insertList :: Eq a => a -> [a] -> [a]
47 -- Assumes the arg list contains no dups; guarantees the result has no dups
48 insertList x xs | isIn "insert" x xs = xs
49 | otherwise = x : xs
50
51 unionLists :: (Outputable a, Eq a) => [a] -> [a] -> [a]
52 -- Assumes that the arguments contain no duplicates
53 unionLists xs ys
54 = WARN(length xs > 100 || length ys > 100, ppr xs $$ ppr ys)
55 [x | x <- xs, isn'tIn "unionLists" x ys] ++ ys
56
57 minusList :: (Eq a) => [a] -> [a] -> [a]
58 -- Everything in the first list that is not in the second list:
59 minusList xs ys = [ x | x <- xs, isn'tIn "minusList" x ys]
60
61 {-
62 ************************************************************************
63 * *
64 \subsection[Utils-assoc]{Association lists}
65 * *
66 ************************************************************************
67
68 Inefficient finite maps based on association lists and equality.
69 -}
70
71 -- A finite mapping based on equality and association lists
72 type Assoc a b = [(a,b)]
73
74 assoc :: (Eq a) => String -> Assoc a b -> a -> b
75 assocDefault :: (Eq a) => b -> Assoc a b -> a -> b
76 assocUsing :: (a -> a -> Bool) -> String -> Assoc a b -> a -> b
77 assocMaybe :: (Eq a) => Assoc a b -> a -> Maybe b
78 assocDefaultUsing :: (a -> a -> Bool) -> b -> Assoc a b -> a -> b
79
80 assocDefaultUsing _ deflt [] _ = deflt
81 assocDefaultUsing eq deflt ((k,v) : rest) key
82 | k `eq` key = v
83 | otherwise = assocDefaultUsing eq deflt rest key
84
85 assoc crash_msg list key = assocDefaultUsing (==) (panic ("Failed in assoc: " ++ crash_msg)) list key
86 assocDefault deflt list key = assocDefaultUsing (==) deflt list key
87 assocUsing eq crash_msg list key = assocDefaultUsing eq (panic ("Failed in assoc: " ++ crash_msg)) list key
88
89 assocMaybe alist key
90 = lookup alist
91 where
92 lookup [] = Nothing
93 lookup ((tv,ty):rest) = if key == tv then Just ty else lookup rest
94
95 {-
96 ************************************************************************
97 * *
98 \subsection[Utils-dups]{Duplicate-handling}
99 * *
100 ************************************************************************
101 -}
102
103 hasNoDups :: (Eq a) => [a] -> Bool
104
105 hasNoDups xs = f [] xs
106 where
107 f _ [] = True
108 f seen_so_far (x:xs) = if x `is_elem` seen_so_far
109 then False
110 else f (x:seen_so_far) xs
111
112 is_elem = isIn "hasNoDups"
113
114 equivClasses :: (a -> a -> Ordering) -- Comparison
115 -> [a]
116 -> [[a]]
117
118 equivClasses _ [] = []
119 equivClasses _ stuff@[_] = [stuff]
120 equivClasses cmp items = runs eq (sortBy cmp items)
121 where
122 eq a b = case cmp a b of { EQ -> True; _ -> False }
123
124 {-
125 The first cases in @equivClasses@ above are just to cut to the point
126 more quickly...
127
128 @runs@ groups a list into a list of lists, each sublist being a run of
129 identical elements of the input list. It is passed a predicate @p@ which
130 tells when two elements are equal.
131 -}
132
133 runs :: (a -> a -> Bool) -- Equality
134 -> [a]
135 -> [[a]]
136
137 runs _ [] = []
138 runs p (x:xs) = case (span (p x) xs) of
139 (first, rest) -> (x:first) : (runs p rest)
140
141 removeDups :: (a -> a -> Ordering) -- Comparison function
142 -> [a]
143 -> ([a], -- List with no duplicates
144 [[a]]) -- List of duplicate groups. One representative from
145 -- each group appears in the first result
146
147 removeDups _ [] = ([], [])
148 removeDups _ [x] = ([x],[])
149 removeDups cmp xs
150 = case (mapAccumR collect_dups [] (equivClasses cmp xs)) of { (dups, xs') ->
151 (xs', dups) }
152 where
153 collect_dups _ [] = panic "ListSetOps: removeDups"
154 collect_dups dups_so_far [x] = (dups_so_far, x)
155 collect_dups dups_so_far dups@(x:_) = (dups:dups_so_far, x)
156
157 findDupsEq :: (a->a->Bool) -> [a] -> [[a]]
158 findDupsEq _ [] = []
159 findDupsEq eq (x:xs) | null eq_xs = findDupsEq eq xs
160 | otherwise = (x:eq_xs) : findDupsEq eq neq_xs
161 where (eq_xs, neq_xs) = partition (eq x) xs
162
163 equivClassesByUniq :: (a -> Unique) -> [a] -> [[a]]
164 -- NB: it's *very* important that if we have the input list [a,b,c],
165 -- where a,b,c all have the same unique, then we get back the list
166 -- [a,b,c]
167 -- not
168 -- [c,b,a]
169 -- Hence the use of foldr, plus the reversed-args tack_on below
170 equivClassesByUniq get_uniq xs
171 = eltsUFM (foldr add emptyUFM xs)
172 where
173 add a ufm = addToUFM_C tack_on ufm (get_uniq a) [a]
174 tack_on old new = new++old