Revert "Add new mbmi and mbmi2 compiler flags"
[ghc.git] / compiler / cmm / Hoopl / Collections.hs
1 {-# LANGUAGE TypeFamilies #-}
2 module Hoopl.Collections
3 ( IsSet(..)
4 , setInsertList, setDeleteList, setUnions
5 , IsMap(..)
6 , mapInsertList, mapDeleteList, mapUnions
7 ) where
8
9 import GhcPrelude
10
11 import Data.List (foldl', foldl1')
12
13 class IsSet set where
14 type ElemOf set
15
16 setNull :: set -> Bool
17 setSize :: set -> Int
18 setMember :: ElemOf set -> set -> Bool
19
20 setEmpty :: set
21 setSingleton :: ElemOf set -> set
22 setInsert :: ElemOf set -> set -> set
23 setDelete :: ElemOf set -> set -> set
24
25 setUnion :: set -> set -> set
26 setDifference :: set -> set -> set
27 setIntersection :: set -> set -> set
28 setIsSubsetOf :: set -> set -> Bool
29
30 setFold :: (ElemOf set -> b -> b) -> b -> set -> b
31
32 setElems :: set -> [ElemOf set]
33 setFromList :: [ElemOf set] -> set
34
35 -- Helper functions for IsSet class
36 setInsertList :: IsSet set => [ElemOf set] -> set -> set
37 setInsertList keys set = foldl' (flip setInsert) set keys
38
39 setDeleteList :: IsSet set => [ElemOf set] -> set -> set
40 setDeleteList keys set = foldl' (flip setDelete) set keys
41
42 setUnions :: IsSet set => [set] -> set
43 setUnions [] = setEmpty
44 setUnions sets = foldl1' setUnion sets
45
46
47 class IsMap map where
48 type KeyOf map
49
50 mapNull :: map a -> Bool
51 mapSize :: map a -> Int
52 mapMember :: KeyOf map -> map a -> Bool
53 mapLookup :: KeyOf map -> map a -> Maybe a
54 mapFindWithDefault :: a -> KeyOf map -> map a -> a
55
56 mapEmpty :: map a
57 mapSingleton :: KeyOf map -> a -> map a
58 mapInsert :: KeyOf map -> a -> map a -> map a
59 mapInsertWith :: (a -> a -> a) -> KeyOf map -> a -> map a -> map a
60 mapDelete :: KeyOf map -> map a -> map a
61
62 mapUnion :: map a -> map a -> map a
63 mapUnionWithKey :: (KeyOf map -> a -> a -> a) -> map a -> map a -> map a
64 mapDifference :: map a -> map a -> map a
65 mapIntersection :: map a -> map a -> map a
66 mapIsSubmapOf :: Eq a => map a -> map a -> Bool
67
68 mapMap :: (a -> b) -> map a -> map b
69 mapMapWithKey :: (KeyOf map -> a -> b) -> map a -> map b
70 mapFold :: (a -> b -> b) -> b -> map a -> b
71 mapFoldWithKey :: (KeyOf map -> a -> b -> b) -> b -> map a -> b
72 mapFilter :: (a -> Bool) -> map a -> map a
73
74 mapElems :: map a -> [a]
75 mapKeys :: map a -> [KeyOf map]
76 mapToList :: map a -> [(KeyOf map, a)]
77 mapFromList :: [(KeyOf map, a)] -> map a
78 mapFromListWith :: (a -> a -> a) -> [(KeyOf map,a)] -> map a
79
80 -- Helper functions for IsMap class
81 mapInsertList :: IsMap map => [(KeyOf map, a)] -> map a -> map a
82 mapInsertList assocs map = foldl' (flip (uncurry mapInsert)) map assocs
83
84 mapDeleteList :: IsMap map => [KeyOf map] -> map a -> map a
85 mapDeleteList keys map = foldl' (flip mapDelete) map keys
86
87 mapUnions :: IsMap map => [map a] -> map a
88 mapUnions [] = mapEmpty
89 mapUnions maps = foldl1' mapUnion maps