{-# LANGUAGE DeriveDataTypeable, DeriveGeneric #-}
{- |
Module      :  ./Common/Lib/MapSet.hs
Description :  Maps of sets
Copyright   :  (c) DFKI GmbH 2011
License     :  GPLv2 or higher, see LICENSE.txt

Maintainer  :  Christian.Maeder@dfki.de
Stability   :  provisional
Portability :  portable

supply total mappings from keys to sets of values based on Data.Map.
Undefined keys are mapped to the empty set. An abstract data type is needed
to avoid differences due to empty set values versus undefined map keys.

-}

module Common.Lib.MapSet
  ( rmNullSets
  , setLookup
  , setElems
  , setMember
  , setInsert
  , setAll
  , setDifference
  , setToMap
  , restrict
  , imageList
  , imageSet
  , MapSet
  , toMap
  , fromDistinctMap
  , fromMap
  , empty
  , null
  , fromList
  , toList
  , toPairList
  , keysSet
  , elems
  , insert
  , update
  , lookup
  , member
  , delete
  , union
  , difference
  , intersection
  , map
  , mapMonotonic
  , mapSet
  , foldWithKey
  , filter
  , partition
  , filterWithKey
  , all
  , isSubmapOf
  , preImage
  , transpose
  ) where

import Prelude hiding (all, filter, map, null, lookup)

import Data.Data
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.List as List

import GHC.Generics (Generic)

-- * functions directly working over unwrapped maps of sets

-- | remove empty elements from the map
rmNullSets :: Ord a => Map.Map a (Set.Set b) -> Map.Map a (Set.Set b)
rmNullSets :: Map a (Set b) -> Map a (Set b)
rmNullSets = (Set b -> Bool) -> Map a (Set b) -> Map a (Set b)
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (Bool -> Bool
not (Bool -> Bool) -> (Set b -> Bool) -> Set b -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set b -> Bool
forall a. Set a -> Bool
Set.null)

-- | get elements for a key
setLookup :: Ord a => a -> Map.Map a (Set.Set b) -> Set.Set b
setLookup :: a -> Map a (Set b) -> Set b
setLookup = Set b -> a -> Map a (Set b) -> Set b
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault Set b
forall a. Set a
Set.empty

-- | all elementes united
setElems :: Ord b => Map.Map a (Set.Set b) -> Set.Set b
setElems :: Map a (Set b) -> Set b
setElems = [Set b] -> Set b
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions ([Set b] -> Set b)
-> (Map a (Set b) -> [Set b]) -> Map a (Set b) -> Set b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map a (Set b) -> [Set b]
forall k a. Map k a -> [a]
Map.elems

-- | test for an element under a key
setMember :: (Ord a, Ord b) => a -> b -> Map.Map a (Set.Set b) -> Bool
setMember :: a -> b -> Map a (Set b) -> Bool
setMember k :: a
k v :: b
v = b -> Set b -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member b
v (Set b -> Bool)
-> (Map a (Set b) -> Set b) -> Map a (Set b) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Map a (Set b) -> Set b
forall a b. Ord a => a -> Map a (Set b) -> Set b
setLookup a
k

-- | insert into a set of values
setInsert :: (Ord k, Ord a) => k -> a -> Map.Map k (Set.Set a)
  -> Map.Map k (Set.Set a)
setInsert :: k -> a -> Map k (Set a) -> Map k (Set a)
setInsert k :: k
k v :: a
v m :: Map k (Set a)
m = k -> Set a -> Map k (Set a) -> Map k (Set a)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert k
k (a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
Set.insert a
v (Set a -> Set a) -> Set a -> Set a
forall a b. (a -> b) -> a -> b
$ k -> Map k (Set a) -> Set a
forall a b. Ord a => a -> Map a (Set b) -> Set b
setLookup k
k Map k (Set a)
m) Map k (Set a)
m

-- | update an element set under the given key
setUpdate :: (Ord k, Ord a) => (Set.Set a -> Set.Set a) -> k
  -> Map.Map k (Set.Set a) -> Map.Map k (Set.Set a)
setUpdate :: (Set a -> Set a) -> k -> Map k (Set a) -> Map k (Set a)
setUpdate f :: Set a -> Set a
f k :: k
k m :: Map k (Set a)
m = let s :: Set a
s = Set a -> Set a
f (Set a -> Set a) -> Set a -> Set a
forall a b. (a -> b) -> a -> b
$ k -> Map k (Set a) -> Set a
forall a b. Ord a => a -> Map a (Set b) -> Set b
setLookup k
k Map k (Set a)
m in
  if Set a -> Bool
forall a. Set a -> Bool
Set.null Set a
s then k -> Map k (Set a) -> Map k (Set a)
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete k
k Map k (Set a)
m else k -> Set a -> Map k (Set a) -> Map k (Set a)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert k
k Set a
s Map k (Set a)
m

-- | test all elements of a set
setAll :: (a -> Bool) -> Set.Set a -> Bool
setAll :: (a -> Bool) -> Set a -> Bool
setAll p :: a -> Bool
p = (a -> Bool) -> [a] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
List.all a -> Bool
p ([a] -> Bool) -> (Set a -> [a]) -> Set a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set a -> [a]
forall a. Set a -> [a]
Set.toList

-- | difference function for differenceWith, returns Nothing for empty sets
setDifference :: Ord a => Set.Set a -> Set.Set a -> Maybe (Set.Set a)
setDifference :: Set a -> Set a -> Maybe (Set a)
setDifference s :: Set a
s t :: Set a
t = let d :: Set a
d = Set a -> Set a -> Set a
forall a. Ord a => Set a -> Set a -> Set a
Set.difference Set a
s Set a
t in
    if Set a -> Bool
forall a. Set a -> Bool
Set.null Set a
d then Maybe (Set a)
forall a. Maybe a
Nothing else Set a -> Maybe (Set a)
forall a. a -> Maybe a
Just Set a
d

-- | convert a set into an identity map
setToMap :: Ord a => Set.Set a -> Map.Map a a
setToMap :: Set a -> Map a a
setToMap = [(a, a)] -> Map a a
forall k a. [(k, a)] -> Map k a
Map.fromDistinctAscList ([(a, a)] -> Map a a) -> (Set a -> [(a, a)]) -> Set a -> Map a a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> (a, a)) -> [a] -> [(a, a)]
forall a b. (a -> b) -> [a] -> [b]
List.map (\ a :: a
a -> (a
a, a
a)) ([a] -> [(a, a)]) -> (Set a -> [a]) -> Set a -> [(a, a)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set a -> [a]
forall a. Set a -> [a]
Set.toList

-- | restrict a map by a keys set
restrict :: Ord k => Map.Map k a -> Set.Set k -> Map.Map k a
restrict :: Map k a -> Set k -> Map k a
restrict m :: Map k a
m = Map k a -> Map k k -> Map k a
forall k a b. Ord k => Map k a -> Map k b -> Map k a
Map.intersection Map k a
m (Map k k -> Map k a) -> (Set k -> Map k k) -> Set k -> Map k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set k -> Map k k
forall a. Ord a => Set a -> Map a a
setToMap

-- | the image of a map
imageList :: Ord k => Map.Map k a -> Set.Set k -> [a]
imageList :: Map k a -> Set k -> [a]
imageList m :: Map k a
m = Map k a -> [a]
forall k a. Map k a -> [a]
Map.elems (Map k a -> [a]) -> (Set k -> Map k a) -> Set k -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map k a -> Set k -> Map k a
forall k a. Ord k => Map k a -> Set k -> Map k a
restrict Map k a
m

-- | the image of a map
imageSet :: (Ord k, Ord a) => Map.Map k a -> Set.Set k -> Set.Set a
imageSet :: Map k a -> Set k -> Set a
imageSet m :: Map k a
m = [a] -> Set a
forall a. Ord a => [a] -> Set a
Set.fromList ([a] -> Set a) -> (Set k -> [a]) -> Set k -> Set a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map k a -> Set k -> [a]
forall k a. Ord k => Map k a -> Set k -> [a]
imageList Map k a
m

-- * protected maps of set as a newtype

-- | a map to non-empty sets
newtype MapSet a b = MapSet { MapSet a b -> Map a (Set b)
toMap :: Map.Map a (Set.Set b) }
  deriving (MapSet a b -> MapSet a b -> Bool
(MapSet a b -> MapSet a b -> Bool)
-> (MapSet a b -> MapSet a b -> Bool) -> Eq (MapSet a b)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall a b. (Eq a, Eq b) => MapSet a b -> MapSet a b -> Bool
/= :: MapSet a b -> MapSet a b -> Bool
$c/= :: forall a b. (Eq a, Eq b) => MapSet a b -> MapSet a b -> Bool
== :: MapSet a b -> MapSet a b -> Bool
$c== :: forall a b. (Eq a, Eq b) => MapSet a b -> MapSet a b -> Bool
Eq, Eq (MapSet a b)
Eq (MapSet a b) =>
(MapSet a b -> MapSet a b -> Ordering)
-> (MapSet a b -> MapSet a b -> Bool)
-> (MapSet a b -> MapSet a b -> Bool)
-> (MapSet a b -> MapSet a b -> Bool)
-> (MapSet a b -> MapSet a b -> Bool)
-> (MapSet a b -> MapSet a b -> MapSet a b)
-> (MapSet a b -> MapSet a b -> MapSet a b)
-> Ord (MapSet a b)
MapSet a b -> MapSet a b -> Bool
MapSet a b -> MapSet a b -> Ordering
MapSet a b -> MapSet a b -> MapSet a b
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a b. (Ord a, Ord b) => Eq (MapSet a b)
forall a b. (Ord a, Ord b) => MapSet a b -> MapSet a b -> Bool
forall a b. (Ord a, Ord b) => MapSet a b -> MapSet a b -> Ordering
forall a b.
(Ord a, Ord b) =>
MapSet a b -> MapSet a b -> MapSet a b
min :: MapSet a b -> MapSet a b -> MapSet a b
$cmin :: forall a b.
(Ord a, Ord b) =>
MapSet a b -> MapSet a b -> MapSet a b
max :: MapSet a b -> MapSet a b -> MapSet a b
$cmax :: forall a b.
(Ord a, Ord b) =>
MapSet a b -> MapSet a b -> MapSet a b
>= :: MapSet a b -> MapSet a b -> Bool
$c>= :: forall a b. (Ord a, Ord b) => MapSet a b -> MapSet a b -> Bool
> :: MapSet a b -> MapSet a b -> Bool
$c> :: forall a b. (Ord a, Ord b) => MapSet a b -> MapSet a b -> Bool
<= :: MapSet a b -> MapSet a b -> Bool
$c<= :: forall a b. (Ord a, Ord b) => MapSet a b -> MapSet a b -> Bool
< :: MapSet a b -> MapSet a b -> Bool
$c< :: forall a b. (Ord a, Ord b) => MapSet a b -> MapSet a b -> Bool
compare :: MapSet a b -> MapSet a b -> Ordering
$ccompare :: forall a b. (Ord a, Ord b) => MapSet a b -> MapSet a b -> Ordering
$cp1Ord :: forall a b. (Ord a, Ord b) => Eq (MapSet a b)
Ord, Typeable, Typeable (MapSet a b)
Constr
DataType
Typeable (MapSet a b) =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> MapSet a b -> c (MapSet a b))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (MapSet a b))
-> (MapSet a b -> Constr)
-> (MapSet a b -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (MapSet a b)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (MapSet a b)))
-> ((forall b. Data b => b -> b) -> MapSet a b -> MapSet a b)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> MapSet a b -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> MapSet a b -> r)
-> (forall u. (forall d. Data d => d -> u) -> MapSet a b -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> MapSet a b -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> MapSet a b -> m (MapSet a b))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> MapSet a b -> m (MapSet a b))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> MapSet a b -> m (MapSet a b))
-> Data (MapSet a b)
MapSet a b -> Constr
MapSet a b -> DataType
(forall b. Data b => b -> b) -> MapSet a b -> MapSet a b
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MapSet a b -> c (MapSet a b)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (MapSet a b)
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (MapSet a b))
forall a.
Typeable a =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> MapSet a b -> u
forall u. (forall d. Data d => d -> u) -> MapSet a b -> [u]
forall a b. (Data a, Data b, Ord b, Ord a) => Typeable (MapSet a b)
forall a b. (Data a, Data b, Ord b, Ord a) => MapSet a b -> Constr
forall a b.
(Data a, Data b, Ord b, Ord a) =>
MapSet a b -> DataType
forall a b.
(Data a, Data b, Ord b, Ord a) =>
(forall b. Data b => b -> b) -> MapSet a b -> MapSet a b
forall a b u.
(Data a, Data b, Ord b, Ord a) =>
Int -> (forall d. Data d => d -> u) -> MapSet a b -> u
forall a b u.
(Data a, Data b, Ord b, Ord a) =>
(forall d. Data d => d -> u) -> MapSet a b -> [u]
forall a b r r'.
(Data a, Data b, Ord b, Ord a) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MapSet a b -> r
forall a b r r'.
(Data a, Data b, Ord b, Ord a) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MapSet a b -> r
forall a b (m :: * -> *).
(Data a, Data b, Ord b, Ord a, Monad m) =>
(forall d. Data d => d -> m d) -> MapSet a b -> m (MapSet a b)
forall a b (m :: * -> *).
(Data a, Data b, Ord b, Ord a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> MapSet a b -> m (MapSet a b)
forall a b (c :: * -> *).
(Data a, Data b, Ord b, Ord a) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (MapSet a b)
forall a b (c :: * -> *).
(Data a, Data b, Ord b, Ord a) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MapSet a b -> c (MapSet a b)
forall a b (t :: * -> *) (c :: * -> *).
(Data a, Data b, Ord b, Ord a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (MapSet a b))
forall a b (t :: * -> * -> *) (c :: * -> *).
(Data a, Data b, Ord b, Ord a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (MapSet a b))
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MapSet a b -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MapSet a b -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> MapSet a b -> m (MapSet a b)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> MapSet a b -> m (MapSet a b)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (MapSet a b)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MapSet a b -> c (MapSet a b)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (MapSet a b))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (MapSet a b))
$cMapSet :: Constr
$tMapSet :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> MapSet a b -> m (MapSet a b)
$cgmapMo :: forall a b (m :: * -> *).
(Data a, Data b, Ord b, Ord a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> MapSet a b -> m (MapSet a b)
gmapMp :: (forall d. Data d => d -> m d) -> MapSet a b -> m (MapSet a b)
$cgmapMp :: forall a b (m :: * -> *).
(Data a, Data b, Ord b, Ord a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> MapSet a b -> m (MapSet a b)
gmapM :: (forall d. Data d => d -> m d) -> MapSet a b -> m (MapSet a b)
$cgmapM :: forall a b (m :: * -> *).
(Data a, Data b, Ord b, Ord a, Monad m) =>
(forall d. Data d => d -> m d) -> MapSet a b -> m (MapSet a b)
gmapQi :: Int -> (forall d. Data d => d -> u) -> MapSet a b -> u
$cgmapQi :: forall a b u.
(Data a, Data b, Ord b, Ord a) =>
Int -> (forall d. Data d => d -> u) -> MapSet a b -> u
gmapQ :: (forall d. Data d => d -> u) -> MapSet a b -> [u]
$cgmapQ :: forall a b u.
(Data a, Data b, Ord b, Ord a) =>
(forall d. Data d => d -> u) -> MapSet a b -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MapSet a b -> r
$cgmapQr :: forall a b r r'.
(Data a, Data b, Ord b, Ord a) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> MapSet a b -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MapSet a b -> r
$cgmapQl :: forall a b r r'.
(Data a, Data b, Ord b, Ord a) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> MapSet a b -> r
gmapT :: (forall b. Data b => b -> b) -> MapSet a b -> MapSet a b
$cgmapT :: forall a b.
(Data a, Data b, Ord b, Ord a) =>
(forall b. Data b => b -> b) -> MapSet a b -> MapSet a b
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (MapSet a b))
$cdataCast2 :: forall a b (t :: * -> * -> *) (c :: * -> *).
(Data a, Data b, Ord b, Ord a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (MapSet a b))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (MapSet a b))
$cdataCast1 :: forall a b (t :: * -> *) (c :: * -> *).
(Data a, Data b, Ord b, Ord a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (MapSet a b))
dataTypeOf :: MapSet a b -> DataType
$cdataTypeOf :: forall a b.
(Data a, Data b, Ord b, Ord a) =>
MapSet a b -> DataType
toConstr :: MapSet a b -> Constr
$ctoConstr :: forall a b. (Data a, Data b, Ord b, Ord a) => MapSet a b -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (MapSet a b)
$cgunfold :: forall a b (c :: * -> *).
(Data a, Data b, Ord b, Ord a) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (MapSet a b)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MapSet a b -> c (MapSet a b)
$cgfoldl :: forall a b (c :: * -> *).
(Data a, Data b, Ord b, Ord a) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> MapSet a b -> c (MapSet a b)
$cp1Data :: forall a b. (Data a, Data b, Ord b, Ord a) => Typeable (MapSet a b)
Data, (forall x. MapSet a b -> Rep (MapSet a b) x)
-> (forall x. Rep (MapSet a b) x -> MapSet a b)
-> Generic (MapSet a b)
forall x. Rep (MapSet a b) x -> MapSet a b
forall x. MapSet a b -> Rep (MapSet a b) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a b x. Rep (MapSet a b) x -> MapSet a b
forall a b x. MapSet a b -> Rep (MapSet a b) x
$cto :: forall a b x. Rep (MapSet a b) x -> MapSet a b
$cfrom :: forall a b x. MapSet a b -> Rep (MapSet a b) x
Generic)

instance (Show a, Show b) => Show (MapSet a b) where
    show :: MapSet a b -> String
show = Map a (Set b) -> String
forall a. Show a => a -> String
show (Map a (Set b) -> String)
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

instance (Ord a, Read a, Ord b, Read b) => Read (MapSet a b) where
    readsPrec :: Int -> ReadS (MapSet a b)
readsPrec d :: Int
d = ((Map a (Set b), String) -> (MapSet a b, String))
-> [(Map a (Set b), String)] -> [(MapSet a b, String)]
forall a b. (a -> b) -> [a] -> [b]
List.map (\ (m :: Map a (Set b)
m, r :: String
r) -> (Map a (Set b) -> MapSet a b
forall a b. Ord a => Map a (Set b) -> MapSet a b
fromMap Map a (Set b)
m , String
r)) ([(Map a (Set b), String)] -> [(MapSet a b, String)])
-> (String -> [(Map a (Set b), String)]) -> ReadS (MapSet a b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String -> [(Map a (Set b), String)]
forall a. Read a => Int -> ReadS a
readsPrec Int
d

-- | unsafe variant of fromMap (without removal of empty sets)
fromDistinctMap :: Map.Map a (Set.Set b) -> MapSet a b
fromDistinctMap :: Map a (Set b) -> MapSet a b
fromDistinctMap = Map a (Set b) -> MapSet a b
forall a b. Map a (Set b) -> MapSet a b
MapSet

-- | remove empty values from the map before applying wrapping constructor
fromMap :: Ord a => Map.Map a (Set.Set b) -> MapSet a b
fromMap :: Map a (Set b) -> MapSet a b
fromMap = Map a (Set b) -> MapSet a b
forall a b. Map a (Set b) -> MapSet a b
MapSet (Map a (Set b) -> MapSet a b)
-> (Map a (Set b) -> Map a (Set b)) -> Map a (Set b) -> MapSet a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map a (Set b) -> Map a (Set b)
forall a b. Ord a => Map a (Set b) -> Map a (Set b)
rmNullSets

-- | the empty relation
empty :: MapSet a b
empty :: MapSet a b
empty = Map a (Set b) -> MapSet a b
forall a b. Map a (Set b) -> MapSet a b
MapSet Map a (Set b)
forall k a. Map k a
Map.empty

-- | test for the empty mapping
null :: MapSet a b -> Bool
null :: MapSet a b -> Bool
null (MapSet m :: Map a (Set b)
m) = Map a (Set b) -> Bool
forall k a. Map k a -> Bool
Map.null Map a (Set b)
m

-- | convert from a list
fromList :: (Ord a, Ord b) => [(a, [b])] -> MapSet a b
fromList :: [(a, [b])] -> MapSet a b
fromList = Map a (Set b) -> MapSet a b
forall a b. Ord a => Map a (Set b) -> MapSet a b
fromMap
  (Map a (Set b) -> MapSet a b)
-> ([(a, [b])] -> Map a (Set b)) -> [(a, [b])] -> MapSet a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Set b -> Set b -> Set b) -> [(a, Set b)] -> Map a (Set b)
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith Set b -> Set b -> Set b
forall a. Ord a => Set a -> Set a -> Set a
Set.union
  ([(a, Set b)] -> Map a (Set b))
-> ([(a, [b])] -> [(a, Set b)]) -> [(a, [b])] -> Map a (Set b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, [b]) -> (a, Set b)) -> [(a, [b])] -> [(a, Set b)]
forall a b. (a -> b) -> [a] -> [b]
List.map (\ (a :: a
a, bs :: [b]
bs) -> (a
a, [b] -> Set b
forall a. Ord a => [a] -> Set a
Set.fromList [b]
bs))

-- | convert to a list
toList :: MapSet a b -> [(a, [b])]
toList :: MapSet a b -> [(a, [b])]
toList = ((a, Set b) -> (a, [b])) -> [(a, Set b)] -> [(a, [b])]
forall a b. (a -> b) -> [a] -> [b]
List.map (\ (a :: a
a, bs :: Set b
bs) -> (a
a, Set b -> [b]
forall a. Set a -> [a]
Set.toList Set b
bs)) ([(a, Set b)] -> [(a, [b])])
-> (MapSet a b -> [(a, Set b)]) -> MapSet a b -> [(a, [b])]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map a (Set b) -> [(a, Set b)]
forall k a. Map k a -> [(k, a)]
Map.toList (Map a (Set b) -> [(a, Set b)])
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> [(a, Set b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

toPairList :: MapSet a b -> [(a, b)]
toPairList :: MapSet a b -> [(a, b)]
toPairList = ((a, [b]) -> [(a, b)]) -> [(a, [b])] -> [(a, b)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\ (c :: a
c, ts :: [b]
ts) -> (b -> (a, b)) -> [b] -> [(a, b)]
forall a b. (a -> b) -> [a] -> [b]
List.map (\ t :: b
t -> (a
c, b
t)) [b]
ts) ([(a, [b])] -> [(a, b)])
-> (MapSet a b -> [(a, [b])]) -> MapSet a b -> [(a, b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> [(a, [b])]
forall a b. MapSet a b -> [(a, [b])]
toList

-- | keys for non-empty elements
keysSet :: MapSet a b -> Set.Set a
keysSet :: MapSet a b -> Set a
keysSet = Map a (Set b) -> Set a
forall k a. Map k a -> Set k
Map.keysSet (Map a (Set b) -> Set a)
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> Set a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

-- | all elementes united
elems :: Ord b => MapSet a b -> Set.Set b
elems :: MapSet a b -> Set b
elems = Map a (Set b) -> Set b
forall b a. Ord b => Map a (Set b) -> Set b
setElems (Map a (Set b) -> Set b)
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> Set b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

-- | get elements for a key
lookup :: Ord a => a -> MapSet a b -> Set.Set b
lookup :: a -> MapSet a b -> Set b
lookup k :: a
k = a -> Map a (Set b) -> Set b
forall a b. Ord a => a -> Map a (Set b) -> Set b
setLookup a
k (Map a (Set b) -> Set b)
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> Set b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

-- | insert an element under the given key
insert :: (Ord a, Ord b) => a -> b -> MapSet a b -> MapSet a b
insert :: a -> b -> MapSet a b -> MapSet a b
insert k :: a
k v :: b
v = Map a (Set b) -> MapSet a b
forall a b. Map a (Set b) -> MapSet a b
MapSet (Map a (Set b) -> MapSet a b)
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> MapSet a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b -> Map a (Set b) -> Map a (Set b)
forall k a.
(Ord k, Ord a) =>
k -> a -> Map k (Set a) -> Map k (Set a)
setInsert a
k b
v (Map a (Set b) -> Map a (Set b))
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> Map a (Set b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

-- | update an element set under the given key
update :: (Ord a, Ord b) => (Set.Set b -> Set.Set b) -> a -> MapSet a b
  -> MapSet a b
update :: (Set b -> Set b) -> a -> MapSet a b -> MapSet a b
update f :: Set b -> Set b
f k :: a
k = Map a (Set b) -> MapSet a b
forall a b. Map a (Set b) -> MapSet a b
MapSet (Map a (Set b) -> MapSet a b)
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> MapSet a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Set b -> Set b) -> a -> Map a (Set b) -> Map a (Set b)
forall k a.
(Ord k, Ord a) =>
(Set a -> Set a) -> k -> Map k (Set a) -> Map k (Set a)
setUpdate Set b -> Set b
f a
k (Map a (Set b) -> Map a (Set b))
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> Map a (Set b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

-- | test for an element under a key
member :: (Ord a, Ord b) => a -> b -> MapSet a b -> Bool
member :: a -> b -> MapSet a b -> Bool
member k :: a
k v :: b
v = a -> b -> Map a (Set b) -> Bool
forall a b. (Ord a, Ord b) => a -> b -> Map a (Set b) -> Bool
setMember a
k b
v (Map a (Set b) -> Bool)
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

-- | delete an element under the given key
delete :: (Ord a, Ord b) => a -> b -> MapSet a b -> MapSet a b
delete :: a -> b -> MapSet a b -> MapSet a b
delete k :: a
k v :: b
v m :: MapSet a b
m@(MapSet r :: Map a (Set b)
r) = Map a (Set b) -> MapSet a b
forall a b. Map a (Set b) -> MapSet a b
MapSet
  (Map a (Set b) -> MapSet a b) -> Map a (Set b) -> MapSet a b
forall a b. (a -> b) -> a -> b
$ let s :: Set b
s = b -> Set b -> Set b
forall a. Ord a => a -> Set a -> Set a
Set.delete b
v (Set b -> Set b) -> Set b -> Set b
forall a b. (a -> b) -> a -> b
$ a -> MapSet a b -> Set b
forall a b. Ord a => a -> MapSet a b -> Set b
lookup a
k MapSet a b
m in
    if Set b -> Bool
forall a. Set a -> Bool
Set.null Set b
s then a -> Map a (Set b) -> Map a (Set b)
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete a
k Map a (Set b)
r else a -> Set b -> Map a (Set b) -> Map a (Set b)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert a
k Set b
s Map a (Set b)
r

-- | union of two maps
union :: (Ord a, Ord b) => MapSet a b -> MapSet a b -> MapSet a b
union :: MapSet a b -> MapSet a b -> MapSet a b
union (MapSet m :: Map a (Set b)
m) = Map a (Set b) -> MapSet a b
forall a b. Map a (Set b) -> MapSet a b
MapSet (Map a (Set b) -> MapSet a b)
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> MapSet a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Set b -> Set b -> Set b)
-> Map a (Set b) -> Map a (Set b) -> Map a (Set b)
forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith Set b -> Set b -> Set b
forall a. Ord a => Set a -> Set a -> Set a
Set.union Map a (Set b)
m (Map a (Set b) -> Map a (Set b))
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> Map a (Set b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

-- | difference of two maps
difference :: (Ord a, Ord b) => MapSet a b -> MapSet a b -> MapSet a b
difference :: MapSet a b -> MapSet a b -> MapSet a b
difference (MapSet m :: Map a (Set b)
m) = Map a (Set b) -> MapSet a b
forall a b. Map a (Set b) -> MapSet a b
MapSet (Map a (Set b) -> MapSet a b)
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> MapSet a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Set b -> Set b -> Maybe (Set b))
-> Map a (Set b) -> Map a (Set b) -> Map a (Set b)
forall k a b.
Ord k =>
(a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a
Map.differenceWith Set b -> Set b -> Maybe (Set b)
forall a. Ord a => Set a -> Set a -> Maybe (Set a)
setDifference Map a (Set b)
m (Map a (Set b) -> Map a (Set b))
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> Map a (Set b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

-- | intersection of two maps
intersection :: (Ord a, Ord b) => MapSet a b -> MapSet a b -> MapSet a b
intersection :: MapSet a b -> MapSet a b -> MapSet a b
intersection (MapSet m :: Map a (Set b)
m) = Map a (Set b) -> MapSet a b
forall a b. Ord a => Map a (Set b) -> MapSet a b
fromMap
  (Map a (Set b) -> MapSet a b)
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> MapSet a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Set b -> Set b -> Set b)
-> Map a (Set b) -> Map a (Set b) -> Map a (Set b)
forall k a b c.
Ord k =>
(a -> b -> c) -> Map k a -> Map k b -> Map k c
Map.intersectionWith Set b -> Set b -> Set b
forall a. Ord a => Set a -> Set a -> Set a
Set.intersection Map a (Set b)
m (Map a (Set b) -> Map a (Set b))
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> Map a (Set b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

-- | map a function to all elements
map :: (Ord b, Ord c) => (b -> c) -> MapSet a b -> MapSet a c
map :: (b -> c) -> MapSet a b -> MapSet a c
map f :: b -> c
f = Map a (Set c) -> MapSet a c
forall a b. Map a (Set b) -> MapSet a b
MapSet (Map a (Set c) -> MapSet a c)
-> (MapSet a b -> Map a (Set c)) -> MapSet a b -> MapSet a c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Set b -> Set c) -> Map a (Set b) -> Map a (Set c)
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map ((b -> c) -> Set b -> Set c
forall b a. Ord b => (a -> b) -> Set a -> Set b
Set.map b -> c
f) (Map a (Set b) -> Map a (Set c))
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> Map a (Set c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

-- | unsafe map a function to all elements
mapMonotonic :: (b -> c) -> MapSet a b -> MapSet a c
mapMonotonic :: (b -> c) -> MapSet a b -> MapSet a c
mapMonotonic f :: b -> c
f = Map a (Set c) -> MapSet a c
forall a b. Map a (Set b) -> MapSet a b
MapSet (Map a (Set c) -> MapSet a c)
-> (MapSet a b -> Map a (Set c)) -> MapSet a b -> MapSet a c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Set b -> Set c) -> Map a (Set b) -> Map a (Set c)
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map ((b -> c) -> Set b -> Set c
forall a b. (a -> b) -> Set a -> Set b
Set.mapMonotonic b -> c
f) (Map a (Set b) -> Map a (Set c))
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> Map a (Set c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

-- | apply a function to all element sets
mapSet :: Ord a => (Set.Set b -> Set.Set c) -> MapSet a b -> MapSet a c
mapSet :: (Set b -> Set c) -> MapSet a b -> MapSet a c
mapSet f :: Set b -> Set c
f = Map a (Set c) -> MapSet a c
forall a b. Ord a => Map a (Set b) -> MapSet a b
fromMap (Map a (Set c) -> MapSet a c)
-> (MapSet a b -> Map a (Set c)) -> MapSet a b -> MapSet a c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Set b -> Set c) -> Map a (Set b) -> Map a (Set c)
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map Set b -> Set c
f (Map a (Set b) -> Map a (Set c))
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> Map a (Set c)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

-- | fold over all elements
foldWithKey :: (a -> b -> c -> c) -> c -> MapSet a b -> c
foldWithKey :: (a -> b -> c -> c) -> c -> MapSet a b -> c
foldWithKey f :: a -> b -> c -> c
f e :: c
e = (a -> Set b -> c -> c) -> c -> Map a (Set b) -> c
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey (\ a :: a
a bs :: Set b
bs c :: c
c -> (b -> c -> c) -> c -> Set b -> c
forall a b. (a -> b -> b) -> b -> Set a -> b
Set.fold (a -> b -> c -> c
f a
a) c
c Set b
bs) c
e (Map a (Set b) -> c)
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

-- | filter elements
filter :: (Ord a, Ord b) => (b -> Bool) -> MapSet a b -> MapSet a b
filter :: (b -> Bool) -> MapSet a b -> MapSet a b
filter p :: b -> Bool
p = Map a (Set b) -> MapSet a b
forall a b. Ord a => Map a (Set b) -> MapSet a b
fromMap (Map a (Set b) -> MapSet a b)
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> MapSet a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Set b -> Set b) -> Map a (Set b) -> Map a (Set b)
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map ((b -> Bool) -> Set b -> Set b
forall a. (a -> Bool) -> Set a -> Set a
Set.filter b -> Bool
p) (Map a (Set b) -> Map a (Set b))
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> Map a (Set b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

-- | partition elements
partition :: (Ord a, Ord b) => (b -> Bool) -> MapSet a b
  -> (MapSet a b, MapSet a b)
partition :: (b -> Bool) -> MapSet a b -> (MapSet a b, MapSet a b)
partition p :: b -> Bool
p m :: MapSet a b
m = ((b -> Bool) -> MapSet a b -> MapSet a b
forall a b.
(Ord a, Ord b) =>
(b -> Bool) -> MapSet a b -> MapSet a b
filter b -> Bool
p MapSet a b
m, (b -> Bool) -> MapSet a b -> MapSet a b
forall a b.
(Ord a, Ord b) =>
(b -> Bool) -> MapSet a b -> MapSet a b
filter (Bool -> Bool
not (Bool -> Bool) -> (b -> Bool) -> b -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Bool
p) MapSet a b
m)

-- | filter complete entries
filterWithKey :: Ord a => (a -> Set.Set b -> Bool) -> MapSet a b -> MapSet a b
filterWithKey :: (a -> Set b -> Bool) -> MapSet a b -> MapSet a b
filterWithKey p :: a -> Set b -> Bool
p = Map a (Set b) -> MapSet a b
forall a b. Map a (Set b) -> MapSet a b
MapSet (Map a (Set b) -> MapSet a b)
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> MapSet a b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Set b -> Bool) -> Map a (Set b) -> Map a (Set b)
forall k a. (k -> a -> Bool) -> Map k a -> Map k a
Map.filterWithKey a -> Set b -> Bool
p (Map a (Set b) -> Map a (Set b))
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> Map a (Set b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

-- | test all elements
all :: Ord b => (b -> Bool) -> MapSet a b -> Bool
all :: (b -> Bool) -> MapSet a b -> Bool
all p :: b -> Bool
p = (b -> Bool) -> Set b -> Bool
forall a. (a -> Bool) -> Set a -> Bool
setAll b -> Bool
p (Set b -> Bool) -> (MapSet a b -> Set b) -> MapSet a b -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Set b
forall b a. Ord b => MapSet a b -> Set b
elems

-- | submap test
isSubmapOf :: (Ord a, Ord b) => MapSet a b -> MapSet a b -> Bool
isSubmapOf :: MapSet a b -> MapSet a b -> Bool
isSubmapOf (MapSet m :: Map a (Set b)
m) = (Set b -> Set b -> Bool) -> Map a (Set b) -> Map a (Set b) -> Bool
forall k a b.
Ord k =>
(a -> b -> Bool) -> Map k a -> Map k b -> Bool
Map.isSubmapOfBy Set b -> Set b -> Bool
forall a. Ord a => Set a -> Set a -> Bool
Set.isSubsetOf Map a (Set b)
m (Map a (Set b) -> Bool)
-> (MapSet a b -> Map a (Set b)) -> MapSet a b -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MapSet a b -> Map a (Set b)
forall a b. MapSet a b -> Map a (Set b)
toMap

-- | pre-image of a map
preImage :: (Ord a, Ord b) => Map.Map a b -> MapSet b a
preImage :: Map a b -> MapSet b a
preImage = (a -> b -> MapSet b a -> MapSet b a)
-> MapSet b a -> Map a b -> MapSet b a
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
Map.foldrWithKey ((b -> a -> MapSet b a -> MapSet b a)
-> a -> b -> MapSet b a -> MapSet b a
forall a b c. (a -> b -> c) -> b -> a -> c
flip b -> a -> MapSet b a -> MapSet b a
forall a b. (Ord a, Ord b) => a -> b -> MapSet a b -> MapSet a b
insert) MapSet b a
forall a b. MapSet a b
empty

-- | transpose a map set
transpose :: (Ord a, Ord b) => MapSet a b -> MapSet b a
transpose :: MapSet a b -> MapSet b a
transpose = (a -> b -> MapSet b a -> MapSet b a)
-> MapSet b a -> MapSet a b -> MapSet b a
forall a b c. (a -> b -> c -> c) -> c -> MapSet a b -> c
foldWithKey ((b -> a -> MapSet b a -> MapSet b a)
-> a -> b -> MapSet b a -> MapSet b a
forall a b c. (a -> b -> c) -> b -> a -> c
flip b -> a -> MapSet b a -> MapSet b a
forall a b. (Ord a, Ord b) => a -> b -> MapSet a b -> MapSet a b
insert) MapSet b a
forall a b. MapSet a b
empty