module Agda.Utils.BiMap where
import Prelude hiding (null, lookup)
import Control.Monad.Identity
import Control.Monad.State
import Data.Function
import qualified Data.List as List
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe
import Data.Ord
import Data.Tuple
import GHC.Generics (Generic)
import Agda.Utils.List
import Agda.Utils.Null
class HasTag a where
type Tag a
tag :: a -> Maybe (Tag a)
tagInjectiveFor ::
(Eq v, Eq (Tag v), HasTag v) =>
[v] -> Bool
tagInjectiveFor :: forall v. (Eq v, Eq (Tag v), HasTag v) => [v] -> Bool
tagInjectiveFor [v]
vs = forall (t :: * -> *). Foldable t => t Bool -> Bool
and
[ v
v1 forall a. Eq a => a -> a -> Bool
== v
v2
| v
v1 <- [v]
vs
, v
v2 <- [v]
vs
, forall a. Maybe a -> Bool
isJust (forall a. HasTag a => a -> Maybe (Tag a)
tag v
v1)
, forall a. HasTag a => a -> Maybe (Tag a)
tag v
v1 forall a. Eq a => a -> a -> Bool
== forall a. HasTag a => a -> Maybe (Tag a)
tag v
v2
]
data BiMap k v = BiMap
{ forall k v. BiMap k v -> Map k v
biMapThere :: !(Map k v)
, forall k v. BiMap k v -> Map (Tag v) k
biMapBack :: !(Map (Tag v) k)
}
deriving forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k v x. Rep (BiMap k v) x -> BiMap k v
forall k v x. BiMap k v -> Rep (BiMap k v) x
$cto :: forall k v x. Rep (BiMap k v) x -> BiMap k v
$cfrom :: forall k v x. BiMap k v -> Rep (BiMap k v) x
Generic
biMapInvariant ::
(Eq k, Eq v, Ord (Tag v), HasTag v) =>
BiMap k v -> Bool
biMapInvariant :: forall k v.
(Eq k, Eq v, Ord (Tag v), HasTag v) =>
BiMap k v -> Bool
biMapInvariant m :: BiMap k v
m@(BiMap Map k v
t Map (Tag v) k
u) =
Map (Tag v) k
u forall a. Eq a => a -> a -> Bool
==
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList
[ (Tag v
k', k
k)
| (k
k, v
v) <- forall k a. Map k a -> [(k, a)]
Map.toList Map k v
t
, Just Tag v
k' <- [forall a. HasTag a => a -> Maybe (Tag a)
tag v
v]
]
Bool -> Bool -> Bool
&&
forall v. (Eq v, Eq (Tag v), HasTag v) => [v] -> Bool
tagInjectiveFor (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall k v. BiMap k v -> [(k, v)]
toList BiMap k v
m)
instance Null (BiMap k v) where
empty :: BiMap k v
empty = forall k v. Map k v -> Map (Tag v) k -> BiMap k v
BiMap forall k a. Map k a
Map.empty forall k a. Map k a
Map.empty
null :: BiMap k v -> Bool
null = forall a. Null a => a -> Bool
null forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v. BiMap k v -> Map k v
biMapThere
source :: Ord k => k -> BiMap k v -> Bool
source :: forall k v. Ord k => k -> BiMap k v -> Bool
source k
k = forall k a. Ord k => k -> Map k a -> Bool
Map.member k
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v. BiMap k v -> Map k v
biMapThere
target :: Ord (Tag v) => Tag v -> BiMap k v -> Bool
target :: forall v k. Ord (Tag v) => Tag v -> BiMap k v -> Bool
target Tag v
k = forall k a. Ord k => k -> Map k a -> Bool
Map.member Tag v
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v. BiMap k v -> Map (Tag v) k
biMapBack
lookup :: Ord k => k -> BiMap k v -> Maybe v
lookup :: forall k v. Ord k => k -> BiMap k v -> Maybe v
lookup k
a = forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup k
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v. BiMap k v -> Map k v
biMapThere
invLookup :: Ord (Tag v) => Tag v -> BiMap k v -> Maybe k
invLookup :: forall v k. Ord (Tag v) => Tag v -> BiMap k v -> Maybe k
invLookup Tag v
k = forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Tag v
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v. BiMap k v -> Map (Tag v) k
biMapBack
singleton :: HasTag v => k -> v -> BiMap k v
singleton :: forall v k. HasTag v => k -> v -> BiMap k v
singleton k
k v
v =
forall k v. Map k v -> Map (Tag v) k -> BiMap k v
BiMap
(forall k a. k -> a -> Map k a
Map.singleton k
k v
v)
(case forall a. HasTag a => a -> Maybe (Tag a)
tag v
v of
Maybe (Tag v)
Nothing -> forall k a. Map k a
Map.empty
Just Tag v
k' -> forall k a. k -> a -> Map k a
Map.singleton Tag v
k' k
k)
insert ::
(Ord k, HasTag v, Ord (Tag v)) =>
k -> v -> BiMap k v -> BiMap k v
insert :: forall k v.
(Ord k, HasTag v, Ord (Tag v)) =>
k -> v -> BiMap k v -> BiMap k v
insert k
k v
v (BiMap Map k v
t Map (Tag v) k
b) =
forall k v. Map k v -> Map (Tag v) k -> BiMap k v
BiMap
(forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert k
k v
v Map k v
t)
(case forall a. HasTag a => a -> Maybe (Tag a)
tag v
v of
Maybe (Tag v)
Nothing -> Map (Tag v) k
b'
Just Tag v
k' -> forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert Tag v
k' k
k Map (Tag v) k
b')
where
b' :: Map (Tag v) k
b' = case forall a. HasTag a => a -> Maybe (Tag a)
tag forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup k
k Map k v
t of
Maybe (Tag v)
Nothing -> Map (Tag v) k
b
Just Tag v
k' -> forall k a. Ord k => k -> Map k a -> Map k a
Map.delete Tag v
k' Map (Tag v) k
b
insertPrecondition ::
(Eq k, Eq v, Eq (Tag v), HasTag v) =>
k -> v -> BiMap k v -> Bool
insertPrecondition :: forall k v.
(Eq k, Eq v, Eq (Tag v), HasTag v) =>
k -> v -> BiMap k v -> Bool
insertPrecondition k
k v
v BiMap k v
m =
case forall a. HasTag a => a -> Maybe (Tag a)
tag v
v of
Maybe (Tag v)
Nothing -> Bool
True
Just Tag v
_ ->
Bool -> Bool
not forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\(k
k', v
v') -> k
k' forall a. Eq a => a -> a -> Bool
/= k
k Bool -> Bool -> Bool
&& forall a. HasTag a => a -> Maybe (Tag a)
tag v
v forall a. Eq a => a -> a -> Bool
== forall a. HasTag a => a -> Maybe (Tag a)
tag v
v') forall a b. (a -> b) -> a -> b
$ forall k v. BiMap k v -> [(k, v)]
toList BiMap k v
m
alterM ::
forall k v m. (Ord k, Ord (Tag v), HasTag v, Monad m) =>
(Maybe v -> m (Maybe v)) -> k -> BiMap k v -> m (BiMap k v)
alterM :: forall k v (m :: * -> *).
(Ord k, Ord (Tag v), HasTag v, Monad m) =>
(Maybe v -> m (Maybe v)) -> k -> BiMap k v -> m (BiMap k v)
alterM Maybe v -> m (Maybe v)
f k
k m :: BiMap k v
m@(BiMap Map k v
t Map (Tag v) k
b) = do
(Map k v
t', Maybe (Maybe (Tag v), Maybe (Tag v))
r) <- forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT (forall (f :: * -> *) k a.
(Functor f, Ord k) =>
(Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)
Map.alterF Maybe v
-> StateT (Maybe (Maybe (Tag v), Maybe (Tag v))) m (Maybe v)
f' k
k Map k v
t) forall a. Maybe a
Nothing
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Maybe (Maybe (Tag v), Maybe (Tag v))
r of
Maybe (Maybe (Tag v), Maybe (Tag v))
Nothing -> BiMap k v
m
Just (Maybe (Tag v), Maybe (Tag v))
r -> forall k v. Map k v -> Map (Tag v) k -> BiMap k v
BiMap Map k v
t' ((Maybe (Tag v), Maybe (Tag v)) -> Map (Tag v) k -> Map (Tag v) k
updateBack (Maybe (Tag v), Maybe (Tag v))
r Map (Tag v) k
b)
where
f' ::
Maybe v ->
StateT (Maybe (Maybe (Tag v), Maybe (Tag v))) m (Maybe v)
f' :: Maybe v
-> StateT (Maybe (Maybe (Tag v), Maybe (Tag v))) m (Maybe v)
f' Maybe v
v = do
Maybe v
r <- forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Maybe v -> m (Maybe v)
f Maybe v
v)
forall s (m :: * -> *). MonadState s m => s -> m ()
put forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (forall a. HasTag a => a -> Maybe (Tag a)
tag forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Maybe v
v, forall a. HasTag a => a -> Maybe (Tag a)
tag forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Maybe v
r)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe v
r
updateBack :: (Maybe (Tag v), Maybe (Tag v)) -> Map (Tag v) k -> Map (Tag v) k
updateBack (Maybe (Tag v)
k'1, Maybe (Tag v)
k'2) =
if Maybe (Tag v)
k'1 forall a. Eq a => a -> a -> Bool
== Maybe (Tag v)
k'2
then forall a. a -> a
id
else forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id (forall a b c. (a -> b -> c) -> b -> a -> c
flip forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert k
k) Maybe (Tag v)
k'2 forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id forall k a. Ord k => k -> Map k a -> Map k a
Map.delete Maybe (Tag v)
k'1
alter ::
forall k v. (Ord k, Ord (Tag v), HasTag v) =>
(Maybe v -> Maybe v) -> k -> BiMap k v -> BiMap k v
alter :: forall k v.
(Ord k, Ord (Tag v), HasTag v) =>
(Maybe v -> Maybe v) -> k -> BiMap k v -> BiMap k v
alter Maybe v -> Maybe v
f k
k BiMap k v
m = forall a. Identity a -> a
runIdentity forall a b. (a -> b) -> a -> b
$ forall k v (m :: * -> *).
(Ord k, Ord (Tag v), HasTag v, Monad m) =>
(Maybe v -> m (Maybe v)) -> k -> BiMap k v -> m (BiMap k v)
alterM (forall a. a -> Identity a
Identity forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe v -> Maybe v
f) k
k BiMap k v
m
alterPrecondition ::
(Ord k, Eq v, Eq (Tag v), HasTag v) =>
(Maybe v -> Maybe v) -> k -> BiMap k v -> Bool
alterPrecondition :: forall k v.
(Ord k, Eq v, Eq (Tag v), HasTag v) =>
(Maybe v -> Maybe v) -> k -> BiMap k v -> Bool
alterPrecondition Maybe v -> Maybe v
f k
k BiMap k v
m =
case forall a. HasTag a => a -> Maybe (Tag a)
tag forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Maybe v -> Maybe v
f (forall k v. Ord k => k -> BiMap k v -> Maybe v
lookup k
k BiMap k v
m) of
Maybe (Tag v)
Nothing -> Bool
True
Just Tag v
k' -> forall (t :: * -> *). Foldable t => t Bool -> Bool
and
[ forall a. a -> Maybe a
Just Tag v
k' forall a. Eq a => a -> a -> Bool
/= forall a. HasTag a => a -> Maybe (Tag a)
tag v
v
| (k
k'', v
v) <- forall k v. BiMap k v -> [(k, v)]
toList BiMap k v
m
, k
k'' forall a. Eq a => a -> a -> Bool
/= k
k
]
update ::
(Ord k, Ord (Tag v), HasTag v) =>
(v -> Maybe v) -> k -> BiMap k v -> BiMap k v
update :: forall k v.
(Ord k, Ord (Tag v), HasTag v) =>
(v -> Maybe v) -> k -> BiMap k v -> BiMap k v
update v -> Maybe v
f = forall k v.
(Ord k, Ord (Tag v), HasTag v) =>
(Maybe v -> Maybe v) -> k -> BiMap k v -> BiMap k v
alter (v -> Maybe v
f forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<)
updatePrecondition ::
(Ord k, Eq v, Eq (Tag v), HasTag v) =>
(v -> Maybe v) -> k -> BiMap k v -> Bool
updatePrecondition :: forall k v.
(Ord k, Eq v, Eq (Tag v), HasTag v) =>
(v -> Maybe v) -> k -> BiMap k v -> Bool
updatePrecondition v -> Maybe v
f = forall k v.
(Ord k, Eq v, Eq (Tag v), HasTag v) =>
(Maybe v -> Maybe v) -> k -> BiMap k v -> Bool
alterPrecondition (v -> Maybe v
f forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<)
adjust ::
(Ord k, Ord (Tag v), HasTag v) =>
(v -> v) -> k -> BiMap k v -> BiMap k v
adjust :: forall k v.
(Ord k, Ord (Tag v), HasTag v) =>
(v -> v) -> k -> BiMap k v -> BiMap k v
adjust v -> v
f = forall k v.
(Ord k, Ord (Tag v), HasTag v) =>
(v -> Maybe v) -> k -> BiMap k v -> BiMap k v
update (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. v -> v
f)
adjustPrecondition ::
(Ord k, Eq v, Eq (Tag v), HasTag v) =>
(v -> v) -> k -> BiMap k v -> Bool
adjustPrecondition :: forall k v.
(Ord k, Eq v, Eq (Tag v), HasTag v) =>
(v -> v) -> k -> BiMap k v -> Bool
adjustPrecondition v -> v
f = forall k v.
(Ord k, Eq v, Eq (Tag v), HasTag v) =>
(v -> Maybe v) -> k -> BiMap k v -> Bool
updatePrecondition (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. v -> v
f)
insertLookupWithKey ::
forall k v. (Ord k, Ord (Tag v), HasTag v) =>
(k -> v -> v -> v) -> k -> v -> BiMap k v -> (Maybe v, BiMap k v)
insertLookupWithKey :: forall k v.
(Ord k, Ord (Tag v), HasTag v) =>
(k -> v -> v -> v) -> k -> v -> BiMap k v -> (Maybe v, BiMap k v)
insertLookupWithKey k -> v -> v -> v
f k
k v
v BiMap k v
m = forall a b. (a, b) -> (b, a)
swap forall a b. (a -> b) -> a -> b
$ forall s a. State s a -> s -> (a, s)
runState (forall k v (m :: * -> *).
(Ord k, Ord (Tag v), HasTag v, Monad m) =>
(Maybe v -> m (Maybe v)) -> k -> BiMap k v -> m (BiMap k v)
alterM Maybe v -> State (Maybe v) (Maybe v)
f' k
k BiMap k v
m) forall a. Maybe a
Nothing
where
f' :: Maybe v -> State (Maybe v) (Maybe v)
f' :: Maybe v -> State (Maybe v) (Maybe v)
f' Maybe v
Nothing = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just v
v
f' r :: Maybe v
r@(Just v
v') = do
forall s (m :: * -> *). MonadState s m => s -> m ()
put Maybe v
r
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (k -> v -> v -> v
f k
k v
v v
v')
insertLookupWithKeyPrecondition ::
(Ord k, Eq v, Eq (Tag v), HasTag v) =>
(k -> v -> v -> v) -> k -> v -> BiMap k v -> Bool
insertLookupWithKeyPrecondition :: forall k v.
(Ord k, Eq v, Eq (Tag v), HasTag v) =>
(k -> v -> v -> v) -> k -> v -> BiMap k v -> Bool
insertLookupWithKeyPrecondition k -> v -> v -> v
f k
k v
v =
forall k v.
(Ord k, Eq v, Eq (Tag v), HasTag v) =>
(Maybe v -> Maybe v) -> k -> BiMap k v -> Bool
alterPrecondition (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b a. b -> (a -> b) -> Maybe a -> b
maybe v
v (k -> v -> v -> v
f k
k v
v)) k
k
mapWithKey ::
(Ord k, Ord (Tag v), HasTag v) =>
(k -> v -> v) -> BiMap k v -> BiMap k v
mapWithKey :: forall k v.
(Ord k, Ord (Tag v), HasTag v) =>
(k -> v -> v) -> BiMap k v -> BiMap k v
mapWithKey k -> v -> v
f = forall k v. (Ord k, Ord (Tag v), HasTag v) => [(k, v)] -> BiMap k v
fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (\(k
k, v
v) -> (k
k, k -> v -> v
f k
k v
v)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v. BiMap k v -> [(k, v)]
toList
mapWithKeyPrecondition ::
(Eq k, Eq v, Eq (Tag v), HasTag v) =>
(k -> v -> v) -> BiMap k v -> Bool
mapWithKeyPrecondition :: forall k v.
(Eq k, Eq v, Eq (Tag v), HasTag v) =>
(k -> v -> v) -> BiMap k v -> Bool
mapWithKeyPrecondition k -> v -> v
f =
forall k v. (Eq k, Eq v, Eq (Tag v), HasTag v) => [(k, v)] -> Bool
fromListPrecondition forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (\(k
k, v
v) -> (k
k, k -> v -> v
f k
k v
v)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v. BiMap k v -> [(k, v)]
toList
mapWithKeyFixedTags :: (k -> v -> v) -> BiMap k v -> BiMap k v
mapWithKeyFixedTags :: forall k v. (k -> v -> v) -> BiMap k v -> BiMap k v
mapWithKeyFixedTags k -> v -> v
f (BiMap Map k v
t Map (Tag v) k
b) = forall k v. Map k v -> Map (Tag v) k -> BiMap k v
BiMap (forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey k -> v -> v
f Map k v
t) Map (Tag v) k
b
mapWithKeyFixedTagsPrecondition ::
(Eq v, Eq (Tag v), HasTag v) =>
(k -> v -> v) -> BiMap k v -> Bool
mapWithKeyFixedTagsPrecondition :: forall v k.
(Eq v, Eq (Tag v), HasTag v) =>
(k -> v -> v) -> BiMap k v -> Bool
mapWithKeyFixedTagsPrecondition k -> v -> v
f BiMap k v
m = forall (t :: * -> *). Foldable t => t Bool -> Bool
and
[ forall a. HasTag a => a -> Maybe (Tag a)
tag (k -> v -> v
f k
k v
v) forall a. Eq a => a -> a -> Bool
== forall a. HasTag a => a -> Maybe (Tag a)
tag v
v
| (k
k, v
v) <- forall k v. BiMap k v -> [(k, v)]
toList BiMap k v
m
]
union :: (Ord k, Ord (Tag v)) => BiMap k v -> BiMap k v -> BiMap k v
union :: forall k v.
(Ord k, Ord (Tag v)) =>
BiMap k v -> BiMap k v -> BiMap k v
union (BiMap Map k v
t1 Map (Tag v) k
b1) (BiMap Map k v
t2 Map (Tag v) k
b2) =
forall k v. Map k v -> Map (Tag v) k -> BiMap k v
BiMap (forall k a. Ord k => Map k a -> Map k a -> Map k a
Map.union Map k v
t1 Map k v
t2) (forall k a. Ord k => Map k a -> Map k a -> Map k a
Map.union Map (Tag v) k
b1 Map (Tag v) k
b2)
unionPrecondition ::
(Ord k, Eq v, Eq (Tag v), HasTag v) =>
BiMap k v -> BiMap k v -> Bool
unionPrecondition :: forall k v.
(Ord k, Eq v, Eq (Tag v), HasTag v) =>
BiMap k v -> BiMap k v -> Bool
unionPrecondition m1 :: BiMap k v
m1@(BiMap Map k v
t1 Map (Tag v) k
_) m2 :: BiMap k v
m2@(BiMap Map k v
t2 Map (Tag v) k
_) =
forall (t :: * -> *). Foldable t => t Bool -> Bool
and
[ forall a. HasTag a => a -> Maybe (Tag a)
tag v
v2 forall a. Eq a => a -> a -> Bool
== forall a. Maybe a
Nothing Bool -> Bool -> Bool
|| forall a. HasTag a => a -> Maybe (Tag a)
tag v
v1 forall a. Eq a => a -> a -> Bool
== forall a. HasTag a => a -> Maybe (Tag a)
tag v
v2
| (v
v1, v
v2) <- forall k a. Map k a -> [a]
Map.elems forall a b. (a -> b) -> a -> b
$ forall k a b c.
Ord k =>
(a -> b -> c) -> Map k a -> Map k b -> Map k c
Map.intersectionWith (,) Map k v
t1 Map k v
t2
] Bool -> Bool -> Bool
&&
forall (t :: * -> *). Foldable t => t Bool -> Bool
and
[ k
k1 forall a. Eq a => a -> a -> Bool
== k
k2
| (k
k1, v
v1) <- forall k v. BiMap k v -> [(k, v)]
toList BiMap k v
m1
, (k
k2, v
v2) <- forall k v. BiMap k v -> [(k, v)]
toList BiMap k v
m2
, forall a. HasTag a => a -> Maybe (Tag a)
tag v
v1 forall a. Eq a => a -> a -> Bool
== forall a. HasTag a => a -> Maybe (Tag a)
tag v
v2
, forall a. Maybe a -> Bool
isJust (forall a. HasTag a => a -> Maybe (Tag a)
tag v
v1)
]
Bool -> Bool -> Bool
&&
forall v. (Eq v, Eq (Tag v), HasTag v) => [v] -> Bool
tagInjectiveFor
([ v
v1
| (k
_, v
v1) <- forall k v. BiMap k v -> [(k, v)]
toList BiMap k v
m1
] forall a. [a] -> [a] -> [a]
++
[ v
v2
| (k
k2, v
v2) <- forall k v. BiMap k v -> [(k, v)]
toList BiMap k v
m2
, Bool -> Bool
not (k
k2 forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [k]
ks1)
])
where
ks1 :: [k]
ks1 = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall k v. BiMap k v -> [(k, v)]
toList BiMap k v
m1)
fromList ::
(Ord k, Ord (Tag v), HasTag v) =>
[(k, v)] -> BiMap k v
fromList :: forall k v. (Ord k, Ord (Tag v), HasTag v) => [(k, v)] -> BiMap k v
fromList = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
List.foldr (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall k v.
(Ord k, HasTag v, Ord (Tag v)) =>
k -> v -> BiMap k v -> BiMap k v
insert) forall a. Null a => a
empty
fromListPrecondition ::
(Eq k, Eq v, Eq (Tag v), HasTag v) =>
[(k, v)] -> Bool
fromListPrecondition :: forall k v. (Eq k, Eq v, Eq (Tag v), HasTag v) => [(k, v)] -> Bool
fromListPrecondition [(k, v)]
kvs =
forall (t :: * -> *). Foldable t => t Bool -> Bool
and
[ k
k1 forall a. Eq a => a -> a -> Bool
== k
k2
| (k
k1, v
v1) <- [(k, v)]
kvs
, (k
k2, v
v2) <- [(k, v)]
kvs
, forall a. Maybe a -> Bool
isJust (forall a. HasTag a => a -> Maybe (Tag a)
tag v
v1)
, forall a. Maybe a -> Bool
isJust (forall a. HasTag a => a -> Maybe (Tag a)
tag v
v2)
, v
v1 forall a. Eq a => a -> a -> Bool
== v
v2
]
Bool -> Bool -> Bool
&&
forall v. (Eq v, Eq (Tag v), HasTag v) => [v] -> Bool
tagInjectiveFor (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> b
snd [(k, v)]
kvs)
toList :: BiMap k v -> [(k, v)]
toList :: forall k v. BiMap k v -> [(k, v)]
toList = forall k a. Map k a -> [(k, a)]
Map.toAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v. BiMap k v -> Map k v
biMapThere
keys :: BiMap k v -> [k]
keys :: forall k v. BiMap k v -> [k]
keys = forall k a. Map k a -> [k]
Map.keys forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v. BiMap k v -> Map k v
biMapThere
elems :: BiMap k v -> [v]
elems :: forall k v. BiMap k v -> [v]
elems = forall k a. Map k a -> [a]
Map.elems forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k v. BiMap k v -> Map k v
biMapThere
fromDistinctAscendingLists ::
([(k, v)], [(Tag v, k)]) -> BiMap k v
fromDistinctAscendingLists :: forall k v. ([(k, v)], [(Tag v, k)]) -> BiMap k v
fromDistinctAscendingLists ([(k, v)]
t, [(Tag v, k)]
b) =
forall k v. Map k v -> Map (Tag v) k -> BiMap k v
BiMap (forall k a. [(k, a)] -> Map k a
Map.fromDistinctAscList [(k, v)]
t) (forall k a. [(k, a)] -> Map k a
Map.fromDistinctAscList [(Tag v, k)]
b)
fromDistinctAscendingListsPrecondition ::
(Ord k, Eq v, Ord (Tag v), HasTag v) =>
([(k, v)], [(Tag v, k)]) -> Bool
fromDistinctAscendingListsPrecondition :: forall k v.
(Ord k, Eq v, Ord (Tag v), HasTag v) =>
([(k, v)], [(Tag v, k)]) -> Bool
fromDistinctAscendingListsPrecondition ([(k, v)]
kvs, [(Tag v, k)]
kks) =
forall a. Ord a => [a] -> Bool
fastDistinct (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(k, v)]
kvs) Bool -> Bool -> Bool
&& forall a. Ord a => [a] -> Bool
sorted (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(k, v)]
kvs)
Bool -> Bool -> Bool
&&
forall a. Ord a => [a] -> Bool
fastDistinct (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(Tag v, k)]
kks) Bool -> Bool -> Bool
&& forall a. Ord a => [a] -> Bool
sorted (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(Tag v, k)]
kks)
Bool -> Bool -> Bool
&&
[(Tag v, k)]
kks forall a. Eq a => a -> a -> Bool
==
forall a. (a -> a -> Ordering) -> [a] -> [a]
List.sortBy (forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing forall a b. (a, b) -> a
fst)
[ (Tag v
k', k
k)
| (k
k, v
v) <- [(k, v)]
kvs
, Just Tag v
k' <- [forall a. HasTag a => a -> Maybe (Tag a)
tag v
v]
]
Bool -> Bool -> Bool
&&
forall v. (Eq v, Eq (Tag v), HasTag v) => [v] -> Bool
tagInjectiveFor [ v
v | (k
_, v
v) <- [(k, v)]
kvs ]
toDistinctAscendingLists :: BiMap k v -> ([(k, v)], [(Tag v, k)])
toDistinctAscendingLists :: forall k v. BiMap k v -> ([(k, v)], [(Tag v, k)])
toDistinctAscendingLists (BiMap Map k v
t Map (Tag v) k
b) =
(forall k a. Map k a -> [(k, a)]
Map.toAscList Map k v
t, forall k a. Map k a -> [(k, a)]
Map.toAscList Map (Tag v) k
b)
instance (Eq k, Eq v) => Eq (BiMap k v) where
== :: BiMap k v -> BiMap k v -> Bool
(==) = forall a. Eq a => a -> a -> Bool
(==) forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall k v. BiMap k v -> Map k v
biMapThere
instance (Ord k, Ord v) => Ord (BiMap k v) where
compare :: BiMap k v -> BiMap k v -> Ordering
compare = forall a. Ord a => a -> a -> Ordering
compare forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall k v. BiMap k v -> Map k v
biMapThere
instance (Show k, Show v) => Show (BiMap k v) where
show :: BiMap k v -> String
show BiMap k v
bimap = String
"Agda.Utils.BiMap.fromList " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall k v. BiMap k v -> [(k, v)]
toList BiMap k v
bimap)