{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE UndecidableInstances #-}

-- | Provides a MergeMap, which is basically a map with
module Jordan.Types.Internal.MergeMap where

import qualified Data.Map as Map
import GHC.Exts (IsList (..))

-- | A map where '<>' merges values with '<>'.
newtype MergeMap key val = MergeMap {MergeMap key val -> Map key val
getMergeMap :: Map.Map key val}
  deriving (MergeMap key val -> MergeMap key val -> Bool
(MergeMap key val -> MergeMap key val -> Bool)
-> (MergeMap key val -> MergeMap key val -> Bool)
-> Eq (MergeMap key val)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall key val.
(Eq key, Eq val) =>
MergeMap key val -> MergeMap key val -> Bool
/= :: MergeMap key val -> MergeMap key val -> Bool
$c/= :: forall key val.
(Eq key, Eq val) =>
MergeMap key val -> MergeMap key val -> Bool
== :: MergeMap key val -> MergeMap key val -> Bool
$c== :: forall key val.
(Eq key, Eq val) =>
MergeMap key val -> MergeMap key val -> Bool
Eq)
  deriving (Int -> [Item (MergeMap key val)] -> MergeMap key val
[Item (MergeMap key val)] -> MergeMap key val
MergeMap key val -> [Item (MergeMap key val)]
([Item (MergeMap key val)] -> MergeMap key val)
-> (Int -> [Item (MergeMap key val)] -> MergeMap key val)
-> (MergeMap key val -> [Item (MergeMap key val)])
-> IsList (MergeMap key val)
forall l.
([Item l] -> l)
-> (Int -> [Item l] -> l) -> (l -> [Item l]) -> IsList l
forall key val.
Ord key =>
Int -> [Item (MergeMap key val)] -> MergeMap key val
forall key val.
Ord key =>
[Item (MergeMap key val)] -> MergeMap key val
forall key val.
Ord key =>
MergeMap key val -> [Item (MergeMap key val)]
toList :: MergeMap key val -> [Item (MergeMap key val)]
$ctoList :: forall key val.
Ord key =>
MergeMap key val -> [Item (MergeMap key val)]
fromListN :: Int -> [Item (MergeMap key val)] -> MergeMap key val
$cfromListN :: forall key val.
Ord key =>
Int -> [Item (MergeMap key val)] -> MergeMap key val
fromList :: [Item (MergeMap key val)] -> MergeMap key val
$cfromList :: forall key val.
Ord key =>
[Item (MergeMap key val)] -> MergeMap key val
IsList) via (Map.Map key val)
  deriving (a -> MergeMap key b -> MergeMap key a
(a -> b) -> MergeMap key a -> MergeMap key b
(forall a b. (a -> b) -> MergeMap key a -> MergeMap key b)
-> (forall a b. a -> MergeMap key b -> MergeMap key a)
-> Functor (MergeMap key)
forall a b. a -> MergeMap key b -> MergeMap key a
forall a b. (a -> b) -> MergeMap key a -> MergeMap key b
forall key a b. a -> MergeMap key b -> MergeMap key a
forall key a b. (a -> b) -> MergeMap key a -> MergeMap key b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> MergeMap key b -> MergeMap key a
$c<$ :: forall key a b. a -> MergeMap key b -> MergeMap key a
fmap :: (a -> b) -> MergeMap key a -> MergeMap key b
$cfmap :: forall key a b. (a -> b) -> MergeMap key a -> MergeMap key b
Functor, a -> MergeMap key a -> Bool
MergeMap key m -> m
MergeMap key a -> [a]
MergeMap key a -> Bool
MergeMap key a -> Int
MergeMap key a -> a
MergeMap key a -> a
MergeMap key a -> a
MergeMap key a -> a
(a -> m) -> MergeMap key a -> m
(a -> m) -> MergeMap key a -> m
(a -> b -> b) -> b -> MergeMap key a -> b
(a -> b -> b) -> b -> MergeMap key a -> b
(b -> a -> b) -> b -> MergeMap key a -> b
(b -> a -> b) -> b -> MergeMap key a -> b
(a -> a -> a) -> MergeMap key a -> a
(a -> a -> a) -> MergeMap key a -> a
(forall m. Monoid m => MergeMap key m -> m)
-> (forall m a. Monoid m => (a -> m) -> MergeMap key a -> m)
-> (forall m a. Monoid m => (a -> m) -> MergeMap key a -> m)
-> (forall a b. (a -> b -> b) -> b -> MergeMap key a -> b)
-> (forall a b. (a -> b -> b) -> b -> MergeMap key a -> b)
-> (forall b a. (b -> a -> b) -> b -> MergeMap key a -> b)
-> (forall b a. (b -> a -> b) -> b -> MergeMap key a -> b)
-> (forall a. (a -> a -> a) -> MergeMap key a -> a)
-> (forall a. (a -> a -> a) -> MergeMap key a -> a)
-> (forall a. MergeMap key a -> [a])
-> (forall a. MergeMap key a -> Bool)
-> (forall a. MergeMap key a -> Int)
-> (forall a. Eq a => a -> MergeMap key a -> Bool)
-> (forall a. Ord a => MergeMap key a -> a)
-> (forall a. Ord a => MergeMap key a -> a)
-> (forall a. Num a => MergeMap key a -> a)
-> (forall a. Num a => MergeMap key a -> a)
-> Foldable (MergeMap key)
forall a. Eq a => a -> MergeMap key a -> Bool
forall a. Num a => MergeMap key a -> a
forall a. Ord a => MergeMap key a -> a
forall m. Monoid m => MergeMap key m -> m
forall a. MergeMap key a -> Bool
forall a. MergeMap key a -> Int
forall a. MergeMap key a -> [a]
forall a. (a -> a -> a) -> MergeMap key a -> a
forall key a. Eq a => a -> MergeMap key a -> Bool
forall key a. Num a => MergeMap key a -> a
forall key a. Ord a => MergeMap key a -> a
forall m a. Monoid m => (a -> m) -> MergeMap key a -> m
forall key m. Monoid m => MergeMap key m -> m
forall key a. MergeMap key a -> Bool
forall key a. MergeMap key a -> Int
forall key a. MergeMap key a -> [a]
forall b a. (b -> a -> b) -> b -> MergeMap key a -> b
forall a b. (a -> b -> b) -> b -> MergeMap key a -> b
forall key a. (a -> a -> a) -> MergeMap key a -> a
forall key m a. Monoid m => (a -> m) -> MergeMap key a -> m
forall key b a. (b -> a -> b) -> b -> MergeMap key a -> b
forall key a b. (a -> b -> b) -> b -> MergeMap key a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: MergeMap key a -> a
$cproduct :: forall key a. Num a => MergeMap key a -> a
sum :: MergeMap key a -> a
$csum :: forall key a. Num a => MergeMap key a -> a
minimum :: MergeMap key a -> a
$cminimum :: forall key a. Ord a => MergeMap key a -> a
maximum :: MergeMap key a -> a
$cmaximum :: forall key a. Ord a => MergeMap key a -> a
elem :: a -> MergeMap key a -> Bool
$celem :: forall key a. Eq a => a -> MergeMap key a -> Bool
length :: MergeMap key a -> Int
$clength :: forall key a. MergeMap key a -> Int
null :: MergeMap key a -> Bool
$cnull :: forall key a. MergeMap key a -> Bool
toList :: MergeMap key a -> [a]
$ctoList :: forall key a. MergeMap key a -> [a]
foldl1 :: (a -> a -> a) -> MergeMap key a -> a
$cfoldl1 :: forall key a. (a -> a -> a) -> MergeMap key a -> a
foldr1 :: (a -> a -> a) -> MergeMap key a -> a
$cfoldr1 :: forall key a. (a -> a -> a) -> MergeMap key a -> a
foldl' :: (b -> a -> b) -> b -> MergeMap key a -> b
$cfoldl' :: forall key b a. (b -> a -> b) -> b -> MergeMap key a -> b
foldl :: (b -> a -> b) -> b -> MergeMap key a -> b
$cfoldl :: forall key b a. (b -> a -> b) -> b -> MergeMap key a -> b
foldr' :: (a -> b -> b) -> b -> MergeMap key a -> b
$cfoldr' :: forall key a b. (a -> b -> b) -> b -> MergeMap key a -> b
foldr :: (a -> b -> b) -> b -> MergeMap key a -> b
$cfoldr :: forall key a b. (a -> b -> b) -> b -> MergeMap key a -> b
foldMap' :: (a -> m) -> MergeMap key a -> m
$cfoldMap' :: forall key m a. Monoid m => (a -> m) -> MergeMap key a -> m
foldMap :: (a -> m) -> MergeMap key a -> m
$cfoldMap :: forall key m a. Monoid m => (a -> m) -> MergeMap key a -> m
fold :: MergeMap key m -> m
$cfold :: forall key m. Monoid m => MergeMap key m -> m
Foldable) via (Map.Map key)

instance Traversable (MergeMap key) where
  traverse :: (a -> f b) -> MergeMap key a -> f (MergeMap key b)
traverse a -> f b
f (MergeMap Map key a
m) = Map key b -> MergeMap key b
forall key val. Map key val -> MergeMap key val
MergeMap (Map key b -> MergeMap key b)
-> f (Map key b) -> f (MergeMap key b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> Map key a -> f (Map key b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f Map key a
m

instance (Semigroup val, Ord key) => Semigroup (MergeMap key val) where
  (MergeMap Map key val
lhs) <> :: MergeMap key val -> MergeMap key val -> MergeMap key val
<> (MergeMap Map key val
rhs) = Map key val -> MergeMap key val
forall key val. Map key val -> MergeMap key val
MergeMap (Map key val -> MergeMap key val)
-> Map key val -> MergeMap key val
forall a b. (a -> b) -> a -> b
$ (val -> val -> val) -> Map key val -> Map key val -> Map key val
forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith val -> val -> val
forall a. Semigroup a => a -> a -> a
(<>) Map key val
lhs Map key val
rhs
  {-# INLINE (<>) #-}

instance (Semigroup val, Ord key) => Monoid (MergeMap key val) where
  mempty :: MergeMap key val
mempty = Map key val -> MergeMap key val
forall key val. Map key val -> MergeMap key val
MergeMap Map key val
forall a. Monoid a => a
mempty
  {-# INLINE mempty #-}

mergeSingleton :: k -> v -> MergeMap k v
mergeSingleton :: k -> v -> MergeMap k v
mergeSingleton k
k v
v = Map k v -> MergeMap k v
forall key val. Map key val -> MergeMap key val
MergeMap (k -> v -> Map k v
forall k a. k -> a -> Map k a
Map.singleton k
k v
v)