module Bug (downsweep) where import GHC.Utils.Misc ( filterOut ) import qualified Data.Map.Strict as M ( Map, elems ) import qualified Data.Map as Map ( fromListWith ) type DownsweepCache = M.Map Int [Int] downsweep :: [Int] -> IO DownsweepCache downsweep rootSummariesOk = do let root_map = mkRootMap rootSummariesOk checkDuplicates root_map return root_map where checkDuplicates :: DownsweepCache -> IO () checkDuplicates root_map = multiRootsErr (head dup_roots) where dup_roots :: [[Int]] dup_roots = filterOut isSingleton (M.elems root_map) isSingleton :: [a] -> Bool isSingleton [_] = True isSingleton _ = False {- {-# NOINLINE filterOut #-} filterOut :: (a->Bool) -> [a] -> [a] -- ^ Like filter, only it reverses the sense of the test filterOut p = filter (not . p) -} mkRootMap :: [Int] -> DownsweepCache mkRootMap summaries = Map.fromListWith (flip (++)) [ (s, [s]) | s <- summaries ] multiRootsErr :: [a] -> IO () multiRootsErr [] = pure ()