module Data.Type.Witness.Specific.WitnessMap.Of where

import Data.Type.Witness.Specific.Some
import Data.Type.Witness.Specific.WitnessMap.For
import Import

-- | A dictionary that is heterogenous up to its simple witness type @w@.
-- Witnesses are the keys of the dictionary, and the values they witness are the values of the dictionary.
type WitnessMapOf :: (Type -> Type) -> Type
type WitnessMapOf = WitnessMapFor Identity

-- | An empty dictionary.
emptyWitnessMapOf :: WitnessMapOf w
emptyWitnessMapOf :: forall (w :: Type -> Type). WitnessMapOf w
emptyWitnessMapOf = forall {k} (f :: k -> Type) (w :: k -> Type). WitnessMapFor f w
emptyWitnessMapFor

-- | Look up the first value in the dictionary that matches the given witness.
witnessMapOfLookup :: TestEquality w => w a -> WitnessMapOf w -> Maybe a
witnessMapOfLookup :: forall (w :: Type -> Type) a.
TestEquality w =>
w a -> WitnessMapOf w -> Maybe a
witnessMapOfLookup w a
wit WitnessMapOf w
d = forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Identity a -> a
runIdentity forall a b. (a -> b) -> a -> b
$ forall {k} (w :: k -> Type) (a :: k) (f :: k -> Type).
TestEquality w =>
w a -> WitnessMapFor f w -> Maybe (f a)
witnessMapForLookup w a
wit WitnessMapOf w
d

-- | Modify the first value in the dictionary that matches a particular witness.
witnessMapOfModify :: TestEquality w => w a -> (a -> a) -> WitnessMapOf w -> WitnessMapOf w
witnessMapOfModify :: forall (w :: Type -> Type) a.
TestEquality w =>
w a -> (a -> a) -> WitnessMapOf w -> WitnessMapOf w
witnessMapOfModify w a
wit a -> a
amap = forall {k} (w :: k -> Type) (a :: k) (f :: k -> Type).
TestEquality w =>
w a -> (f a -> f a) -> WitnessMapFor f w -> WitnessMapFor f w
witnessMapForModify w a
wit (forall a. a -> Identity a
Identity forall {k} (cat :: k -> k -> Type) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> a
amap forall {k} (cat :: k -> k -> Type) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. Identity a -> a
runIdentity)

-- | Replace the first value in the dictionary that matches the witness
witnessMapOfReplace :: TestEquality w => w a -> a -> WitnessMapOf w -> WitnessMapOf w
witnessMapOfReplace :: forall (w :: Type -> Type) a.
TestEquality w =>
w a -> a -> WitnessMapOf w -> WitnessMapOf w
witnessMapOfReplace w a
wit a
newa = forall (w :: Type -> Type) a.
TestEquality w =>
w a -> (a -> a) -> WitnessMapOf w -> WitnessMapOf w
witnessMapOfModify w a
wit forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const a
newa

witnessMapOfSingle :: w a -> a -> WitnessMapOf w
witnessMapOfSingle :: forall (w :: Type -> Type) a. w a -> a -> WitnessMapOf w
witnessMapOfSingle w a
wit a
a = forall {k} (w :: k -> Type) (a :: k) (f :: k -> Type).
w a -> f a -> WitnessMapFor f w
witnessMapForSingle w a
wit forall a b. (a -> b) -> a -> b
$ forall a. a -> Identity a
Identity a
a

witnessMapOfFold :: Monoid m => WitnessMapOf w -> (forall a. w a -> a -> m) -> m
witnessMapOfFold :: forall m (w :: Type -> Type).
Monoid m =>
WitnessMapOf w -> (forall a. w a -> a -> m) -> m
witnessMapOfFold WitnessMapOf w
d forall a. w a -> a -> m
f = forall {k} m (f :: k -> Type) (w :: k -> Type).
Monoid m =>
WitnessMapFor f w -> (forall (a :: k). w a -> f a -> m) -> m
witnessMapForFold WitnessMapOf w
d forall a b. (a -> b) -> a -> b
$ \w a
wa (Identity a
a) -> forall a. w a -> a -> m
f w a
wa a
a

-- | Add a witness and value as the first entry in the dictionary.
witnessMapOfAdd :: w a -> a -> WitnessMapOf w -> WitnessMapOf w
witnessMapOfAdd :: forall (w :: Type -> Type) a.
w a -> a -> WitnessMapOf w -> WitnessMapOf w
witnessMapOfAdd w a
wit a
a = forall {k} (w :: k -> Type) (a :: k) (f :: k -> Type).
w a -> f a -> WitnessMapFor f w -> WitnessMapFor f w
witnessMapForAdd w a
wit forall a b. (a -> b) -> a -> b
$ forall a. a -> Identity a
Identity a
a

-- | Remove the first entry in the dictionary that matches the given witness.
witnessMapOfRemove :: TestEquality w => w a -> WitnessMapOf w -> WitnessMapOf w
witnessMapOfRemove :: forall (w :: Type -> Type) a.
TestEquality w =>
w a -> WitnessMapOf w -> WitnessMapOf w
witnessMapOfRemove = forall {k} (w :: k -> Type) (a :: k) (f :: k -> Type).
TestEquality w =>
w a -> WitnessMapFor f w -> WitnessMapFor f w
witnessMapForRemove

-- | Create a dictionary from a list of witness\/value pairs
witnessMapOfFromList :: [SomeOf w] -> WitnessMapOf w
witnessMapOfFromList :: forall (w :: Type -> Type). [SomeOf w] -> WitnessMapOf w
witnessMapOfFromList = forall k (f :: k -> Type) (w :: k -> Type).
[SomeFor f w] -> WitnessMapFor f w
MkWitnessMapFor

witnessMapOfToList :: WitnessMapOf w -> [SomeOf w]
witnessMapOfToList :: forall (w :: Type -> Type). WitnessMapOf w -> [SomeOf w]
witnessMapOfToList = forall k (f :: k -> Type) (w :: k -> Type).
WitnessMapFor f w -> [SomeFor f w]
witnessMapForToList