/compiler/Eta/Utils/ListSetOps.hs

https://github.com/typelead/eta · Haskell · 186 lines · 89 code · 32 blank · 65 comment · 10 complexity · cd253250b8137ce365d120fd56a77d87 MD5 · raw file

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