{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
module Control.Class.Impl.Map (
Key, Value,
LookupMap(..),
SingletonMap(..),
InsertMap(..),
UpdateMap(..), adjustF, unsafeAdjustF, safeAdjustF,
DeleteMap(..), optDeleteF, unsafeOptDeleteF, safeOptDeleteF,
UpsertMap(..), adsertF,
UpleteMap(..), adleteF, unsafeAdleteF, safeAdleteF,
AlterMap(..), alterF,
Strict(..), Lazy(..),
(!),
fromCoyonedaTransform, fromCoyonedaTransformF,
toCoyonedaTransform, toCoyonedaTransformF,
) where
import qualified Data.Map.Strict
import qualified Data.Map.Lazy
import qualified Data.IntMap.Strict
import qualified Data.IntMap.Lazy
import qualified Data.Set
import Data.Set (Set)
import qualified Data.IntSet
import Data.IntSet (IntSet)
import qualified Data.Sequence
import Data.Sequence (Seq)
import Data.Ix (Ix)
import qualified Data.Array.IArray
import Prelude hiding (lookup)
import qualified Control.Class.Impl.Map.CPP
import Data.Maybe (fromMaybe, isJust)
import Data.Functor.Identity (Identity(Identity, runIdentity))
import Data.Functor.Compose (Compose(Compose, getCompose))
import Data.Maybe.HT (toMaybe)
import Data.Coerce (Coercible, coerce)
import Data.Functor.Coyoneda (Coyoneda, liftCoyoneda, lowerCoyoneda)
import Data.Array (Array)
import qualified Data.ByteString
import qualified Data.ByteString.Unsafe
import qualified Data.ByteString.Lazy
import qualified Data.ByteString.Short
import Data.Word (Word8)
import Data.Int (Int64)
{-# ANN module "HLint: ignore Use if" #-}
type family Key t
type family Value t
{-# INLINE[1] fromCoyonedaTransform #-}
fromCoyonedaTransform :: Functor f1 =>
((a1 -> Coyoneda f2 a2) -> t1 -> t2 -> Coyoneda f1 a3)
-> (a1 -> f2 a2) -> t1 -> t2 -> f1 a3
fromCoyonedaTransform g f k x = lowerCoyoneda $ g (liftCoyoneda . f) k x
{-# INLINE[1] fromCoyonedaTransformF #-}
fromCoyonedaTransformF :: (Functor f1, Functor f3) =>
((a1 -> Coyoneda f2 a2) -> t1 -> t2 -> f3 (Coyoneda f1 a3))
-> (a1 -> f2 a2) -> t1 -> t2 -> f3 (f1 a3)
fromCoyonedaTransformF g f k x = lowerCoyoneda <$> g (liftCoyoneda . f) k x
{-# INLINE[1] toCoyonedaTransform #-}
toCoyonedaTransform :: Functor f =>
(forall f'. Functor f' => (a1 -> f' a2) -> t1 -> t2 -> f' a3)
-> ((a1 -> Coyoneda f a2) -> t1 -> t2 -> Coyoneda f a3)
toCoyonedaTransform = id
{-# INLINE[1] toCoyonedaTransformF #-}
toCoyonedaTransformF :: Functor f =>
(forall f'. Functor f' => (a1 -> f' a2) -> t1 -> t2 -> f3 (f' a3))
-> ((a1 -> Coyoneda f a2) -> t1 -> t2 -> f3 (Coyoneda f a3))
toCoyonedaTransformF = id
{-# RULES
-- An attempt to remove going to and from Coyonedas.
"fromToCoyonedaTransform" forall (x :: forall f2' f1'. (a1 -> f2' a2) -> t1 -> t2 -> f1' a3). fromCoyonedaTransform (toCoyonedaTransform x) = x
"fromToCoyonedaTransformF" forall (x :: forall f2' f1'. (a1 -> f2' a2) -> t1 -> t2 -> f3 (f1' a3)). fromCoyonedaTransformF (toCoyonedaTransformF x) = x
-- How do I write these rules? Should I even write these rules?
-- "fromToCoyonedaTransform" fromCoyonedaTransform . toCoyonedaTransform = id
-- "fromToCoyonedaTransformF" fromCoyonedaTransformF . toCoyonedaTransformF = id
#-}
class LookupMap t where
{-# MINIMAL lookup | ((unsafeIndex | index), member) #-}
lookup :: Key t -> t -> Maybe (Value t)
lookup k x = case member k x of
True -> Just (unsafeIndex k x)
False -> Nothing
index :: Key t -> t -> Value t
index k x = fromMaybe (error "index: Key does not exist.") (lookup k x)
unsafeIndex :: Key t -> t -> Value t
unsafeIndex = index
member :: Key t -> t -> Bool
member k x = isJust (lookup k x)
notMember :: Key t -> t -> Bool
notMember k x = not (member k x)
class LookupMap t => SingletonMap t where
singleton :: Key t -> Value t -> t
class LookupMap t => UpdateMap t where
{-# MINIMAL unsafeUpdate | update | safeUpdate | safeUpdateLookup | safeAdjustLookup | safeAdjustLookup | safeAdjustF_ #-}
update :: Key t -> Value t -> t -> t
update k v x = fromMaybe (error "update: Key not found.") (safeUpdate k v x)
updateLookup :: Key t -> Value t -> t -> (Value t, t)
updateLookup k v x = fromMaybe (error "updateLookup: Key not found.") (safeUpdateLookup k v x)
unsafeUpdate :: Key t -> Value t -> t -> t
unsafeUpdate = update
unsafeUpdateLookup :: Key t -> Value t -> t -> (Value t, t)
unsafeUpdateLookup = updateLookup
maybeUpdate :: Key t -> Value t -> t -> t
maybeUpdate k v x = fromMaybe x (safeUpdate k v x)
safeUpdate :: Key t -> Value t -> t -> Maybe t
safeUpdate k v x = snd <$> safeUpdateLookup k v x
safeUpdateLookup :: Key t -> Value t -> t -> Maybe (Value t, t)
safeUpdateLookup k v = safeAdjustLookup g k where
g old_v = (old_v, v)
adjust :: (Value t -> Value t) -> Key t -> t -> t
adjust f k x = fromMaybe (error "Adjust: Key not found.") (safeAdjust f k x)
adjustLookup :: (Value t -> (r, Value t)) -> Key t -> t -> (r, t)
adjustLookup f k x = fromMaybe (error "AdjustLookup: Key not found.") (safeAdjustLookup f k x)
adjustF_ :: Functor f => (Value t -> Coyoneda f (Value t)) -> Key t -> t -> Coyoneda f t
adjustF_ f k x = fromMaybe (error "AdjustF: Key not found.") (safeAdjustF_ f k x)
unsafeAdjust :: (Value t -> Value t) -> Key t -> t -> t
unsafeAdjust f k x = runIdentity $ unsafeAdjustF (Identity . f) k x
unsafeAdjustLookup :: (Value t -> (r, Value t)) -> Key t -> t -> (r, t)
unsafeAdjustLookup = unsafeAdjustF
unsafeAdjustF_ :: Functor f => (Value t -> Coyoneda f (Value t)) -> Key t -> t -> Coyoneda f t
unsafeAdjustF_ = adjustF_
maybeAdjust :: (Value t -> Value t) -> Key t -> t -> t
maybeAdjust f k x = fromMaybe x (safeAdjust f k x)
safeAdjust :: (Value t -> Value t) -> Key t -> t -> Maybe t
safeAdjust f k x = runIdentity <$> safeAdjustF (Identity . f) k x
safeAdjustLookup :: (Value t -> (r, Value t)) -> Key t -> t -> Maybe (r, t)
safeAdjustLookup = safeAdjustF
safeAdjustF_ :: Functor f => (Value t -> Coyoneda f (Value t)) -> Key t -> t -> Maybe (Coyoneda f t)
default safeAdjustF_ :: (UpsertMap t, Functor f) => (Value t -> Coyoneda f (Value t)) -> Key t -> t -> Maybe (Coyoneda f t)
safeAdjustF_ = defaultSafeAdjustFBasedOnAdsertF
unsafeAdjustF :: (UpdateMap t, Functor f) => (Value t -> f (Value t)) -> Key t -> t -> f t
unsafeAdjustF = fromCoyonedaTransform unsafeAdjustF_
adjustF :: (UpdateMap t, Functor f) => (Value t -> f (Value t)) -> Key t -> t -> f t
adjustF = fromCoyonedaTransform adjustF_
safeAdjustF :: (UpdateMap t, Functor f) => (Value t -> f (Value t)) -> Key t -> t -> Maybe (f t)
safeAdjustF = fromCoyonedaTransformF safeAdjustF_
defaultSafeAdjustFBasedOnAdsertF :: (UpsertMap t, Functor f) => (Value t -> f (Value t)) -> Key t -> t -> Maybe (f t)
defaultSafeAdjustFBasedOnAdsertF f k x = getCompose $ adsertF (Compose . fmap f) k x
defaultSafeAdjustFBasedOnUnsafeUpdate :: (UpdateMap t, Functor f) => (Value t -> f (Value t)) -> Key t -> t -> Maybe (f t)
defaultSafeAdjustFBasedOnUnsafeUpdate f k x = g <$> lookup k x where
g old_val =
let
new_x_func new_val = unsafeUpdate k new_val x
in
new_x_func <$> f old_val
class LookupMap t => InsertMap t where
{-# MINIMAL unsafeInsert | insert | safeInsert #-}
insert :: Key t -> Value t -> t -> t
insert k v x = fromMaybe (error "Insert: Key already exists.") (safeInsert k v x)
unsafeInsert :: Key t -> Value t -> t -> t
unsafeInsert = insert
maybeInsert :: Key t -> Value t -> t -> t
maybeInsert k v x = fromMaybe x (safeInsert k v x)
safeInsert :: Key t -> Value t -> t -> Maybe t
default safeInsert :: UpsertMap t => Key t -> Value t -> t -> Maybe t
safeInsert = defaultSafeInsertBasedOnAdsertF
defaultSafeInsertBasedOnAdsertF :: UpsertMap t => Key t -> Value t -> t -> Maybe t
defaultSafeInsertBasedOnAdsertF k v = adsertF (fmap (const v)) k
class LookupMap t => DeleteMap t where
{-# MINIMAL unsafeDelete | delete | safeDelete | safeDeleteLookup #-}
delete :: Key t -> t -> t
delete k x = fromMaybe (error "delete: key not found.") (safeDelete k x)
deleteLookup :: Key t -> t -> (Value t, t)
deleteLookup k x = fromMaybe (error "deleteLookup: key not found.") (safeDeleteLookup k x)
unsafeDelete :: Key t -> t -> t
unsafeDelete = delete
unsafeDeleteLookup :: Key t -> t -> (Value t, t)
unsafeDeleteLookup = deleteLookup
maybeDelete :: Key t -> t -> t
maybeDelete k x = fromMaybe x (safeDelete k x)
safeDelete :: Key t -> t -> Maybe t
safeDelete k x = snd <$> safeDeleteLookup k x
safeDeleteLookup :: Key t -> t -> Maybe (Value t, t)
safeDeleteLookup = safeOptDeleteLookup g where
g val = (val, True)
optDelete :: (Value t -> Bool) -> Key t -> t -> t
optDelete f k x = fromMaybe (error "optDelete: key not found.") (safeOptDelete f k x)
optDeleteLookup :: (Value t -> (r, Bool)) -> Key t -> t -> (r, t)
optDeleteLookup f k x = fromMaybe (error "optDeleteLookup: key not found.") (safeOptDeleteLookup f k x)
optDeleteF_ :: Functor f => (Value t -> Coyoneda f Bool) -> Key t -> t -> Coyoneda f t
optDeleteF_ f k x = fromMaybe (error "optDeleteF: key not found.") (safeOptDeleteF f k x)
unsafeOptDelete :: (Value t -> Bool) -> Key t -> t -> t
unsafeOptDelete f k x = runIdentity $ unsafeOptDeleteF (Identity . f) k x
unsafeOptDeleteLookup :: (Value t -> (r, Bool)) -> Key t -> t -> (r, t)
unsafeOptDeleteLookup = unsafeOptDeleteF
unsafeOptDeleteF_ :: Functor f => (Value t -> Coyoneda f Bool) -> Key t -> t -> Coyoneda f t
unsafeOptDeleteF_ = optDeleteF
maybeOptDelete :: (Value t -> Bool) -> Key t -> t -> t
maybeOptDelete f k x = fromMaybe x (safeOptDelete f k x)
safeOptDelete :: (Value t -> Bool) -> Key t -> t -> Maybe t
safeOptDelete f k x = runIdentity <$> safeOptDeleteF (Identity . f) k x
safeOptDeleteLookup :: (Value t -> (r, Bool)) -> Key t -> t -> Maybe (r, t)
safeOptDeleteLookup = safeOptDeleteF
safeOptDeleteF_ :: Functor f => (Value t -> Coyoneda f Bool) -> Key t -> t -> Maybe (Coyoneda f t)
default safeOptDeleteF_ :: (UpleteMap t, Functor f) => (Value t -> Coyoneda f Bool) -> Key t -> t -> Maybe (Coyoneda f t)
safeOptDeleteF_ = defaultOptDeleteFBasedOnSafeAdleteF
unsafeOptDeleteF :: (DeleteMap t, Functor f) => (Value t -> f Bool) -> Key t -> t -> f t
unsafeOptDeleteF = fromCoyonedaTransform unsafeOptDeleteF_
optDeleteF :: (DeleteMap t, Functor f) => (Value t -> f Bool) -> Key t -> t -> f t
optDeleteF = fromCoyonedaTransform optDeleteF_
safeOptDeleteF :: (DeleteMap t, Functor f) => (Value t -> f Bool) -> Key t -> t -> Maybe (f t)
safeOptDeleteF = fromCoyonedaTransformF safeOptDeleteF_
defaultOptDeleteFBasedOnSafeAdleteF :: (UpleteMap t, Functor f) => (Value t -> f Bool) -> Key t -> t -> Maybe (f t)
defaultOptDeleteFBasedOnSafeAdleteF f = safeAdleteF g where
g val = (`toMaybe` val) <$> f val
class (InsertMap t, UpdateMap t) => UpsertMap t where
upsert :: Key t -> Value t -> t -> t
upsert k v x = snd (upsertLookup k v x)
upsertLookup :: Key t -> Value t -> t -> (Maybe (Value t), t)
upsertLookup k v = adsertLookup g k where
g old_v = (old_v, v)
adsert :: (Maybe (Value t) -> Value t) -> Key t -> t -> t
adsert f k x = snd $ adsertLookup g k x where
g maybe_old_v = ((), f maybe_old_v)
adsertLookup :: (Maybe (Value t) -> (r, Value t)) -> Key t -> t -> (r, t)
adsertLookup = adsertF
adsertF_ :: Functor f => (Maybe (Value t) -> Coyoneda f (Value t)) -> Key t -> t -> Coyoneda f t
default adsertF_ :: (AlterMap t, Functor f) => (Maybe (Value t) -> Coyoneda f (Value t)) -> Key t -> t -> Coyoneda f t
adsertF_ = defaultAdsertFBasedOnAlterF
adsertF :: (UpsertMap t, Functor f) => (Maybe (Value t) -> f (Value t)) -> Key t -> t -> f t
adsertF = fromCoyonedaTransform adsertF_
defaultAdsertFBasedOnAlterF :: (AlterMap t, Functor f) => (Maybe (Value t) -> f (Value t)) -> Key t -> t -> f t
defaultAdsertFBasedOnAlterF f = alterF (fmap Just . f)
class (DeleteMap t, UpdateMap t) => UpleteMap t where
adlete :: (Value t -> Maybe (Value t)) -> Key t -> t -> t
adlete f k x = fromMaybe (error "Adlete: Key not found.") (safeAdlete f k x)
adleteLookup :: (Value t -> (r, Maybe (Value t))) -> Key t -> t -> (r, t)
adleteLookup f k x = fromMaybe (error "AdleteLookup: Key not found.") (safeAdleteLookup f k x)
adleteF_ :: Functor f => (Value t -> Coyoneda f (Maybe (Value t))) -> Key t -> t -> Coyoneda f t
adleteF_ f k x = fromMaybe (error "AdleteF: Key not found.") (safeAdleteF_ f k x)
unsafeAdlete :: (Value t -> Maybe (Value t)) -> Key t -> t -> t
unsafeAdlete f k x = runIdentity $ unsafeAdleteF (Identity . f) k x
unsafeAdleteLookup :: (Value t -> (r, Maybe (Value t))) -> Key t -> t -> (r, t)
unsafeAdleteLookup = unsafeAdleteF
unsafeAdleteF_ :: Functor f => (Value t -> Coyoneda f (Maybe (Value t))) -> Key t -> t -> Coyoneda f t
unsafeAdleteF_ = adleteF
maybeAdlete :: (Value t -> Maybe (Value t)) -> Key t -> t -> t
maybeAdlete f k x = fromMaybe x (safeAdlete f k x)
safeAdlete :: (Value t -> Maybe (Value t)) -> Key t -> t -> Maybe t
safeAdlete f k x = runIdentity <$> safeAdleteF (Identity . f) k x
safeAdleteLookup :: (Value t -> (r, Maybe (Value t))) -> Key t -> t -> Maybe (r, t)
safeAdleteLookup = safeAdleteF
safeAdleteF_ :: Functor f => (Value t -> Coyoneda f (Maybe (Value t))) -> Key t -> t -> Maybe (Coyoneda f t)
default safeAdleteF_ :: (AlterMap t, Functor f) => (Value t -> Coyoneda f (Maybe (Value t))) -> Key t -> t -> Maybe (Coyoneda f t)
safeAdleteF_ = defaultSafeAdleteFBasedOnAlterF
safeAdleteF :: (UpleteMap t, Functor f) => (Value t -> f (Maybe (Value t))) -> Key t -> t -> Maybe (f t)
safeAdleteF = fromCoyonedaTransformF safeAdleteF_
unsafeAdleteF :: (UpleteMap t, Functor f) => (Value t -> f (Maybe (Value t))) -> Key t -> t -> f t
unsafeAdleteF = fromCoyonedaTransform unsafeAdleteF_
adleteF :: (UpleteMap t, Functor f) => (Value t -> f (Maybe (Value t))) -> Key t -> t -> f t
adleteF = fromCoyonedaTransform adleteF_
defaultSafeAdleteFBasedOnAlterF :: (AlterMap t, Functor f) => (Value t -> f (Maybe (Value t))) -> Key t -> t -> Maybe (f t)
defaultSafeAdleteFBasedOnAlterF f k x = getCompose $ alterF (Compose . fmap f) k x
class (UpsertMap t, UpleteMap t) => AlterMap t where
alter :: (Maybe (Value t) -> Maybe (Value t)) -> Key t -> t -> t
alter f k x = let g v = ((), f v) in snd (alterLookup g k x)
alterLookup :: (Maybe (Value t) -> (r, Maybe (Value t))) -> Key t -> t -> (r, t)
alterLookup = alterF
alterF_ :: Functor f => (Maybe (Value t) -> Coyoneda f (Maybe (Value t))) -> Key t -> t -> Coyoneda f t
alterF_ = defaultAlterFBasedOnUnsafeInsertUpdateDelete
alterF :: (AlterMap t, Functor f) => (Maybe (Value t) -> f (Maybe (Value t))) -> Key t -> t -> f t
alterF = fromCoyonedaTransform alterF_
defaultAlterFBasedOnUnsafeInsertUpdateDelete :: (InsertMap t, UpdateMap t, DeleteMap t, Functor f) => (Maybe (Value t) -> f (Maybe (Value t))) -> Key t -> t -> f t
defaultAlterFBasedOnUnsafeInsertUpdateDelete f k x =
let
maybe_old_val = lookup k x
new_x_func = case maybe_old_val of
Nothing -> \maybe_new_val -> case maybe_new_val of
Nothing -> x
Just new_val -> unsafeInsert k new_val x
Just _ -> \maybe_new_val -> case maybe_new_val of
Nothing -> unsafeDelete k x
Just new_val -> unsafeUpdate k new_val x
in
new_x_func <$> f maybe_old_val
class LookupMap t => AppendMap t where
appendGetKey :: Value t -> t -> (Key t, t)
append :: Value t -> t -> t
append v x = snd (appendGetKey v x)
newtype Strict t = Strict { getStrict :: t }
newtype Lazy t = Lazy { getLazy :: t }
class IsStrictMap t
class IsLazyMap t
type instance Key (Strict t) = Key t
type instance Value (Strict t) = Value t
type instance Key (Lazy t) = Key t
type instance Value (Lazy t) = Value t
instance IsStrictMap t => IsStrictMap (Strict t)
deriving instance {-# OVERLAPPABLE #-} (IsStrictMap t, LookupMap t) => LookupMap (Strict t)
deriving instance {-# OVERLAPPABLE #-} (IsStrictMap t, InsertMap t) => InsertMap (Strict t)
deriving instance {-# OVERLAPPABLE #-} (IsStrictMap t, UpdateMap t) => UpdateMap (Strict t)
deriving instance {-# OVERLAPPABLE #-} (IsStrictMap t, DeleteMap t) => DeleteMap (Strict t)
deriving instance {-# OVERLAPPABLE #-} (IsStrictMap t, UpsertMap t) => UpsertMap (Strict t)
deriving instance {-# OVERLAPPABLE #-} (IsStrictMap t, UpleteMap t) => UpleteMap (Strict t)
deriving instance {-# OVERLAPPABLE #-} (IsStrictMap t, AlterMap t) => AlterMap (Strict t)
type instance Key (Lazy t) = Key t
type instance Value (Lazy t) = Value t
instance IsLazyMap t => IsLazyMap (Lazy t)
deriving instance {-# OVERLAPPABLE #-} (IsLazyMap t, LookupMap t) => LookupMap (Lazy t)
deriving instance {-# OVERLAPPABLE #-} (IsLazyMap t, InsertMap t) => InsertMap (Lazy t)
deriving instance {-# OVERLAPPABLE #-} (IsLazyMap t, UpdateMap t) => UpdateMap (Lazy t)
deriving instance {-# OVERLAPPABLE #-} (IsLazyMap t, DeleteMap t) => DeleteMap (Lazy t)
deriving instance {-# OVERLAPPABLE #-} (IsLazyMap t, UpsertMap t) => UpsertMap (Lazy t)
deriving instance {-# OVERLAPPABLE #-} (IsLazyMap t, UpleteMap t) => UpleteMap (Lazy t)
deriving instance {-# OVERLAPPABLE #-} (IsLazyMap t, AlterMap t) => AlterMap (Lazy t)
unwrapCoerce1 :: (Coercible (f t2) t2) => (t1 -> t2 -> t3) -> t1 -> f t2 -> t3
unwrapCoerce1 f = g where
g x1 x2 = f x1 (coerce x2)
rewrapCoerce1 :: (Coercible (f t2) t2, Coercible t3 (f t3)) => (t1 -> t2 -> t3) -> t1 -> f t2 -> f t3
rewrapCoerce1 f = g where
g x1 x2 = coerce (f x1 (coerce x2))
rewrapCoerce2 :: (Coercible (f t3) t3, Coercible t4 (f t4)) => (t1 -> t2 -> t3 -> t4) -> t1 -> t2 -> f t3 -> f t4
rewrapCoerce2 f = g where
g x1 x2 x3 = coerce (f x1 x2 (coerce x3))
rewrapCoerce2F :: (Coercible (f t3) t3, Coercible t4 (f t4), Functor g) => (t1 -> t2 -> t3 -> g t4) -> t1 -> t2 -> f t3 -> g (f t4)
rewrapCoerce2F f = g where
g x1 x2 x3 = coerce <$> f x1 x2 (coerce x3)
type instance Key (Data.Map.Strict.Map k _) = k
type instance Value (Data.Map.Strict.Map _ v) = v
instance IsStrictMap (Data.Map.Strict.Map k v)
instance Ord k => SingletonMap (Data.Map.Strict.Map k v) where
singleton = Data.Map.Strict.singleton
instance Ord k => LookupMap (Data.Map.Strict.Map k v) where
lookup = Data.Map.Strict.lookup
index = flip (Data.Map.Strict.!)
member = Data.Map.Strict.member
notMember = Data.Map.Strict.notMember
instance Ord k => InsertMap (Data.Map.Strict.Map k v) where
unsafeInsert = Data.Map.Strict.insert
instance Ord k => UpdateMap (Data.Map.Strict.Map k v) where
unsafeUpdate = Data.Map.Strict.insert
unsafeAdjust = Data.Map.Strict.adjust
maybeAdjust = Data.Map.Strict.adjust
instance Ord k => DeleteMap (Data.Map.Strict.Map k v) where
unsafeDelete = Data.Map.Strict.delete
maybeDelete = Data.Map.Strict.delete
instance Ord k => UpsertMap (Data.Map.Strict.Map k v) where
upsert = Data.Map.Strict.insert
instance Ord k => UpleteMap (Data.Map.Strict.Map k v) where
adlete = Data.Map.Strict.update
instance Ord k => AlterMap (Data.Map.Strict.Map k v) where
alter = Data.Map.Strict.alter
alterF_ = toCoyonedaTransform Data.Map.Strict.alterF
instance Ord k => LookupMap (Lazy (Data.Map.Lazy.Map k v)) where
lookup = unwrapCoerce1 Data.Map.Lazy.lookup
index = unwrapCoerce1 $ flip (Data.Map.Lazy.!)
member = unwrapCoerce1 Data.Map.Lazy.member
notMember = unwrapCoerce1 Data.Map.Lazy.notMember
instance Ord k => SingletonMap (Lazy (Data.Map.Lazy.Map k v)) where
singleton k v = Lazy (Data.Map.Lazy.singleton k v)
instance Ord k => InsertMap (Lazy (Data.Map.Lazy.Map k v)) where
unsafeInsert = rewrapCoerce2 Data.Map.Lazy.insert
instance Ord k => UpdateMap (Lazy (Data.Map.Lazy.Map k v)) where
unsafeUpdate = rewrapCoerce2 Data.Map.Lazy.insert
unsafeAdjust = rewrapCoerce2 Data.Map.Lazy.adjust
maybeAdjust = rewrapCoerce2 Data.Map.Lazy.adjust
instance Ord k => DeleteMap (Lazy (Data.Map.Lazy.Map k v)) where
unsafeDelete = rewrapCoerce1 Data.Map.Lazy.delete
maybeDelete = rewrapCoerce1 Data.Map.Lazy.delete
instance Ord k => UpsertMap (Lazy (Data.Map.Lazy.Map k v)) where
upsert = rewrapCoerce2 Data.Map.Lazy.insert
instance Ord k => UpleteMap (Lazy (Data.Map.Lazy.Map k v)) where
adlete = rewrapCoerce2 Data.Map.Lazy.update
instance Ord k => AlterMap (Lazy (Data.Map.Lazy.Map k v)) where
alter = rewrapCoerce2 Data.Map.Lazy.alter
alterF_ = toCoyonedaTransform (rewrapCoerce2F Data.Map.Lazy.alterF)
type instance Key (Data.IntMap.Strict.IntMap v) = Int
type instance Value (Data.IntMap.Strict.IntMap v) = v
instance IsStrictMap (Data.IntMap.Strict.IntMap v)
instance LookupMap (Data.IntMap.Strict.IntMap v) where
lookup = Data.IntMap.Strict.lookup
index = flip (Data.IntMap.Strict.!)
member = Data.IntMap.Strict.member
notMember = Data.IntMap.Strict.notMember
instance SingletonMap (Data.IntMap.Strict.IntMap v) where
singleton = Data.IntMap.Strict.singleton
instance InsertMap (Data.IntMap.Strict.IntMap v) where
unsafeInsert = Data.IntMap.Strict.insert
instance UpdateMap (Data.IntMap.Strict.IntMap v) where
unsafeUpdate = Data.IntMap.Strict.insert
unsafeAdjust = Data.IntMap.Strict.adjust
maybeAdjust = Data.IntMap.Strict.adjust
instance DeleteMap (Data.IntMap.Strict.IntMap v) where
unsafeDelete = Data.IntMap.Strict.delete
maybeDelete = Data.IntMap.Strict.delete
instance UpsertMap (Data.IntMap.Strict.IntMap v) where
upsert = Data.IntMap.Strict.insert
instance UpleteMap (Data.IntMap.Strict.IntMap v) where
adlete = Data.IntMap.Strict.update
instance AlterMap (Data.IntMap.Strict.IntMap v) where
alter = Data.IntMap.Strict.alter
alterF_ = toCoyonedaTransform Data.IntMap.Strict.alterF
instance LookupMap (Lazy (Data.IntMap.Lazy.IntMap v)) where
lookup = unwrapCoerce1 Data.IntMap.Lazy.lookup
index = unwrapCoerce1 $ flip (Data.IntMap.Lazy.!)
member = unwrapCoerce1 Data.IntMap.Lazy.member
notMember = unwrapCoerce1 Data.IntMap.Lazy.notMember
instance SingletonMap (Lazy (Data.IntMap.Lazy.IntMap v)) where
singleton k v = Lazy $ Data.IntMap.Lazy.singleton k v
instance InsertMap (Lazy (Data.IntMap.Lazy.IntMap v)) where
unsafeInsert = rewrapCoerce2 Data.IntMap.Lazy.insert
instance UpdateMap (Lazy (Data.IntMap.Lazy.IntMap v)) where
unsafeUpdate = rewrapCoerce2 Data.IntMap.Lazy.insert
unsafeAdjust = rewrapCoerce2 Data.IntMap.Lazy.adjust
maybeAdjust = rewrapCoerce2 Data.IntMap.Lazy.adjust
instance DeleteMap (Lazy (Data.IntMap.Lazy.IntMap v)) where
unsafeDelete = rewrapCoerce1 Data.IntMap.Lazy.delete
maybeDelete = rewrapCoerce1 Data.IntMap.Lazy.delete
instance UpsertMap (Lazy (Data.IntMap.Lazy.IntMap v)) where
upsert = rewrapCoerce2 Data.IntMap.Lazy.insert
instance UpleteMap (Lazy (Data.IntMap.Lazy.IntMap v)) where
adlete = rewrapCoerce2 Data.IntMap.Lazy.update
instance AlterMap (Lazy (Data.IntMap.Lazy.IntMap v)) where
alter = rewrapCoerce2 Data.IntMap.Lazy.alter
alterF_ = toCoyonedaTransform (rewrapCoerce2F Data.IntMap.Lazy.alterF)
type instance Key (Set a) = a
type instance Value (Set a) = ()
instance IsStrictMap (Set a)
instance IsLazyMap (Set a)
instance Ord a => SingletonMap (Set a) where
singleton k _ = Data.Set.singleton k
instance Ord a => LookupMap (Set a) where
lookup k x = toMaybe (member k x) ()
member = Data.Set.member
index k x = if member k x then () else error "Class 'LookupMap', instance 'Set', function 'index': Index not found."
unsafeIndex _ _ = ()
instance Ord a => InsertMap (Set a) where
unsafeInsert k _ = Data.Set.insert k
maybeInsert k _ = Data.Set.insert k
instance Ord a => DeleteMap (Set a) where
unsafeDelete = Data.Set.delete
maybeDelete = Data.Set.delete
instance Ord a => UpdateMap (Set a) where
unsafeUpdate _ _ = id
unsafeAdjust _ _ = id
instance Ord a => UpsertMap (Set a) where
upsert k _ = Data.Set.insert k
instance Ord a => UpleteMap (Set a)
instance Ord a => AlterMap (Set a)
type instance Key IntSet = Int
type instance Value IntSet = ()
instance SingletonMap IntSet where
singleton k _ = Data.IntSet.singleton k
instance LookupMap IntSet where
lookup k x = toMaybe (member k x) ()
member = Data.IntSet.member
index k x = case member k x of
True -> ()
False -> error "Class 'LookupMap', instance 'IntSet', function 'index': Index not found."
unsafeIndex _ _ = ()
instance InsertMap IntSet where
unsafeInsert k _ = Data.IntSet.insert k
maybeInsert k _ = Data.IntSet.insert k
instance DeleteMap IntSet where
unsafeDelete = Data.IntSet.delete
maybeDelete = Data.IntSet.delete
instance UpdateMap IntSet where
unsafeUpdate _ _ x = x
unsafeAdjust _ _ x = x
instance UpsertMap IntSet where
upsert k _ = Data.IntSet.insert k
instance UpleteMap IntSet
instance AlterMap IntSet
type instance Key (Seq a) = Int
type instance Value (Seq a) = a
instance LookupMap (Seq a) where
lookup = Control.Class.Impl.Map.CPP.seqLookup
index = flip Data.Sequence.index
member k x = 0 <= k && k < length x
instance UpdateMap (Seq a) where
unsafeAdjust = Data.Sequence.adjust'
maybeAdjust = Data.Sequence.adjust'
unsafeUpdate = Data.Sequence.update
maybeUpdate = Data.Sequence.update
safeAdjustF_ = defaultSafeAdjustFBasedOnUnsafeUpdate
instance AppendMap (Seq a) where
append v x = x Data.Sequence.|> v
appendGetKey v x = (Data.Sequence.length x, append v x)
type instance Key (Array i e) = i
type instance Value (Array i e) = e
instance IsLazyMap (Array i e)
instance Ix i => LookupMap (Array i e) where
index = flip (Data.Array.IArray.!)
member k x = let (lbound, ubound) = Data.Array.IArray.bounds x in (lbound <= k && k <= ubound)
instance Ix i => SingletonMap (Array i e) where
singleton k v = Data.Array.IArray.array (k,k) [(k,v)]
type instance Key Data.ByteString.ByteString = Int
type instance Value Data.ByteString.ByteString = Word8
instance LookupMap Data.ByteString.ByteString where
index = flip Data.ByteString.index
member k x = 0 <= k && k < Data.ByteString.length x
unsafeIndex = flip Data.ByteString.Unsafe.unsafeIndex
type instance Key Data.ByteString.Lazy.ByteString = Int64
type instance Value Data.ByteString.Lazy.ByteString = Word8
instance LookupMap Data.ByteString.Lazy.ByteString where
index = flip Data.ByteString.Lazy.index
member k x = 0 <= k && k < Data.ByteString.Lazy.length x
type instance Key Data.ByteString.Short.ShortByteString = Int
type instance Value Data.ByteString.Short.ShortByteString = Word8
instance LookupMap Data.ByteString.Short.ShortByteString where
index = flip Data.ByteString.Short.index
member k x = 0 <= k && k < Data.ByteString.Short.length x
(!) :: LookupMap t => t -> Key t -> Value t
(!) = flip index