{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-name-shadowing #-}
{-# OPTIONS_GHC -fno-specialise #-}
{-# OPTIONS_GHC -fno-omit-interface-pragmas #-}
module PlutusTx.AssocMap (
Map
, singleton
, empty
, null
, fromList
, toList
, keys
, elems
, lookup
, member
, insert
, delete
, union
, unionWith
, filter
, mapWithKey
, mapMaybe
, mapMaybeWithKey
, all
, mapThese
) where
import Control.DeepSeq (NFData)
import Data.Data
import GHC.Generics (Generic)
import PlutusTx.Builtins qualified as P
import PlutusTx.Builtins.Internal qualified as BI
import PlutusTx.IsData
import PlutusTx.Lift (makeLift)
import PlutusTx.Prelude hiding (filter, mapMaybe, null, toList)
import PlutusTx.Prelude qualified as P
import PlutusTx.These
import Prelude qualified as Haskell
import Prettyprinter (Pretty (..))
newtype Map k v = Map { Map k v -> [(k, v)]
unMap :: [(k, v)] }
deriving stock ((forall x. Map k v -> Rep (Map k v) x)
-> (forall x. Rep (Map k v) x -> Map k v) -> Generic (Map k v)
forall x. Rep (Map k v) x -> Map k v
forall x. Map k v -> Rep (Map k v) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k v x. Rep (Map k v) x -> Map k v
forall k v x. Map k v -> Rep (Map k v) x
$cto :: forall k v x. Rep (Map k v) x -> Map k v
$cfrom :: forall k v x. Map k v -> Rep (Map k v) x
Generic, Map k v -> Map k v -> Bool
(Map k v -> Map k v -> Bool)
-> (Map k v -> Map k v -> Bool) -> Eq (Map k v)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k v. (Eq k, Eq v) => Map k v -> Map k v -> Bool
/= :: Map k v -> Map k v -> Bool
$c/= :: forall k v. (Eq k, Eq v) => Map k v -> Map k v -> Bool
== :: Map k v -> Map k v -> Bool
$c== :: forall k v. (Eq k, Eq v) => Map k v -> Map k v -> Bool
Haskell.Eq, Int -> Map k v -> ShowS
[Map k v] -> ShowS
Map k v -> String
(Int -> Map k v -> ShowS)
-> (Map k v -> String) -> ([Map k v] -> ShowS) -> Show (Map k v)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k v. (Show k, Show v) => Int -> Map k v -> ShowS
forall k v. (Show k, Show v) => [Map k v] -> ShowS
forall k v. (Show k, Show v) => Map k v -> String
showList :: [Map k v] -> ShowS
$cshowList :: forall k v. (Show k, Show v) => [Map k v] -> ShowS
show :: Map k v -> String
$cshow :: forall k v. (Show k, Show v) => Map k v -> String
showsPrec :: Int -> Map k v -> ShowS
$cshowsPrec :: forall k v. (Show k, Show v) => Int -> Map k v -> ShowS
Haskell.Show, Typeable (Map k v)
DataType
Constr
Typeable (Map k v)
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Map k v -> c (Map k v))
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Map k v))
-> (Map k v -> Constr)
-> (Map k v -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Map k v)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Map k v)))
-> ((forall b. Data b => b -> b) -> Map k v -> Map k v)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Map k v -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Map k v -> r)
-> (forall u. (forall d. Data d => d -> u) -> Map k v -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Map k v -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Map k v -> m (Map k v))
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Map k v -> m (Map k v))
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Map k v -> m (Map k v))
-> Data (Map k v)
Map k v -> DataType
Map k v -> Constr
(forall b. Data b => b -> b) -> Map k v -> Map k v
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Map k v -> c (Map k v)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Map k v)
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Map k v))
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) -> Map k v -> u
forall u. (forall d. Data d => d -> u) -> Map k v -> [u]
forall k v. (Data k, Data v) => Typeable (Map k v)
forall k v. (Data k, Data v) => Map k v -> DataType
forall k v. (Data k, Data v) => Map k v -> Constr
forall k v.
(Data k, Data v) =>
(forall b. Data b => b -> b) -> Map k v -> Map k v
forall k v u.
(Data k, Data v) =>
Int -> (forall d. Data d => d -> u) -> Map k v -> u
forall k v u.
(Data k, Data v) =>
(forall d. Data d => d -> u) -> Map k v -> [u]
forall k v r r'.
(Data k, Data v) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Map k v -> r
forall k v r r'.
(Data k, Data v) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Map k v -> r
forall k v (m :: * -> *).
(Data k, Data v, Monad m) =>
(forall d. Data d => d -> m d) -> Map k v -> m (Map k v)
forall k v (m :: * -> *).
(Data k, Data v, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Map k v -> m (Map k v)
forall k v (c :: * -> *).
(Data k, Data v) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Map k v)
forall k v (c :: * -> *).
(Data k, Data v) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Map k v -> c (Map k v)
forall k v (t :: * -> *) (c :: * -> *).
(Data k, Data v, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Map k v))
forall k v (t :: * -> * -> *) (c :: * -> *).
(Data k, Data v, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Map k v))
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Map k v -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Map k v -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Map k v -> m (Map k v)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Map k v -> m (Map k v)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Map k v)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Map k v -> c (Map k v)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Map k v))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Map k v))
$cMap :: Constr
$tMap :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> Map k v -> m (Map k v)
$cgmapMo :: forall k v (m :: * -> *).
(Data k, Data v, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Map k v -> m (Map k v)
gmapMp :: (forall d. Data d => d -> m d) -> Map k v -> m (Map k v)
$cgmapMp :: forall k v (m :: * -> *).
(Data k, Data v, MonadPlus m) =>
(forall d. Data d => d -> m d) -> Map k v -> m (Map k v)
gmapM :: (forall d. Data d => d -> m d) -> Map k v -> m (Map k v)
$cgmapM :: forall k v (m :: * -> *).
(Data k, Data v, Monad m) =>
(forall d. Data d => d -> m d) -> Map k v -> m (Map k v)
gmapQi :: Int -> (forall d. Data d => d -> u) -> Map k v -> u
$cgmapQi :: forall k v u.
(Data k, Data v) =>
Int -> (forall d. Data d => d -> u) -> Map k v -> u
gmapQ :: (forall d. Data d => d -> u) -> Map k v -> [u]
$cgmapQ :: forall k v u.
(Data k, Data v) =>
(forall d. Data d => d -> u) -> Map k v -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Map k v -> r
$cgmapQr :: forall k v r r'.
(Data k, Data v) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Map k v -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Map k v -> r
$cgmapQl :: forall k v r r'.
(Data k, Data v) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Map k v -> r
gmapT :: (forall b. Data b => b -> b) -> Map k v -> Map k v
$cgmapT :: forall k v.
(Data k, Data v) =>
(forall b. Data b => b -> b) -> Map k v -> Map k v
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Map k v))
$cdataCast2 :: forall k v (t :: * -> * -> *) (c :: * -> *).
(Data k, Data v, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Map k v))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (Map k v))
$cdataCast1 :: forall k v (t :: * -> *) (c :: * -> *).
(Data k, Data v, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Map k v))
dataTypeOf :: Map k v -> DataType
$cdataTypeOf :: forall k v. (Data k, Data v) => Map k v -> DataType
toConstr :: Map k v -> Constr
$ctoConstr :: forall k v. (Data k, Data v) => Map k v -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Map k v)
$cgunfold :: forall k v (c :: * -> *).
(Data k, Data v) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Map k v)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Map k v -> c (Map k v)
$cgfoldl :: forall k v (c :: * -> *).
(Data k, Data v) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Map k v -> c (Map k v)
$cp1Data :: forall k v. (Data k, Data v) => Typeable (Map k v)
Data)
deriving newtype (Map k v -> Map k v -> Bool
(Map k v -> Map k v -> Bool) -> Eq (Map k v)
forall a. (a -> a -> Bool) -> Eq a
forall k v. (Eq k, Eq v) => Map k v -> Map k v -> Bool
== :: Map k v -> Map k v -> Bool
$c== :: forall k v. (Eq k, Eq v) => Map k v -> Map k v -> Bool
Eq, Eq (Map k v)
Eq (Map k v)
-> (Map k v -> Map k v -> Ordering)
-> (Map k v -> Map k v -> Bool)
-> (Map k v -> Map k v -> Bool)
-> (Map k v -> Map k v -> Bool)
-> (Map k v -> Map k v -> Bool)
-> (Map k v -> Map k v -> Map k v)
-> (Map k v -> Map k v -> Map k v)
-> Ord (Map k v)
Map k v -> Map k v -> Bool
Map k v -> Map k v -> Ordering
Map k v -> Map k v -> Map k v
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 k v. (Ord k, Ord v) => Eq (Map k v)
forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Bool
forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Ordering
forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Map k v
min :: Map k v -> Map k v -> Map k v
$cmin :: forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Map k v
max :: Map k v -> Map k v -> Map k v
$cmax :: forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Map k v
>= :: Map k v -> Map k v -> Bool
$c>= :: forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Bool
> :: Map k v -> Map k v -> Bool
$c> :: forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Bool
<= :: Map k v -> Map k v -> Bool
$c<= :: forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Bool
< :: Map k v -> Map k v -> Bool
$c< :: forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Bool
compare :: Map k v -> Map k v -> Ordering
$ccompare :: forall k v. (Ord k, Ord v) => Map k v -> Map k v -> Ordering
$cp1Ord :: forall k v. (Ord k, Ord v) => Eq (Map k v)
Ord, Map k v -> ()
(Map k v -> ()) -> NFData (Map k v)
forall a. (a -> ()) -> NFData a
forall k v. (NFData k, NFData v) => Map k v -> ()
rnf :: Map k v -> ()
$crnf :: forall k v. (NFData k, NFData v) => Map k v -> ()
NFData)
instance (ToData k, ToData v) => ToData (Map k v) where
toBuiltinData :: Map k v -> BuiltinData
toBuiltinData (Map [(k, v)]
es) = BuiltinList (BuiltinPair BuiltinData BuiltinData) -> BuiltinData
BI.mkMap ([(k, v)] -> BuiltinList (BuiltinPair BuiltinData BuiltinData)
mapToBuiltin [(k, v)]
es)
where
{-# INLINE mapToBuiltin #-}
mapToBuiltin :: [(k, v)] -> BI.BuiltinList (BI.BuiltinPair BI.BuiltinData BI.BuiltinData)
mapToBuiltin :: [(k, v)] -> BuiltinList (BuiltinPair BuiltinData BuiltinData)
mapToBuiltin = [(k, v)] -> BuiltinList (BuiltinPair BuiltinData BuiltinData)
go
where
go :: [(k, v)] -> BI.BuiltinList (BI.BuiltinPair BI.BuiltinData BI.BuiltinData)
go :: [(k, v)] -> BuiltinList (BuiltinPair BuiltinData BuiltinData)
go [] = BuiltinUnit -> BuiltinList (BuiltinPair BuiltinData BuiltinData)
BI.mkNilPairData BuiltinUnit
BI.unitval
go ((k
k,v
v):[(k, v)]
xs) = BuiltinPair BuiltinData BuiltinData
-> BuiltinList (BuiltinPair BuiltinData BuiltinData)
-> BuiltinList (BuiltinPair BuiltinData BuiltinData)
forall a. a -> BuiltinList a -> BuiltinList a
BI.mkCons (BuiltinData -> BuiltinData -> BuiltinPair BuiltinData BuiltinData
BI.mkPairData (k -> BuiltinData
forall a. ToData a => a -> BuiltinData
toBuiltinData k
k) (v -> BuiltinData
forall a. ToData a => a -> BuiltinData
toBuiltinData v
v)) ([(k, v)] -> BuiltinList (BuiltinPair BuiltinData BuiltinData)
go [(k, v)]
xs)
instance (FromData k, FromData v) => FromData (Map k v) where
fromBuiltinData :: BuiltinData -> Maybe (Map k v)
fromBuiltinData BuiltinData
d = BuiltinData
-> (Integer -> BuiltinList BuiltinData -> Maybe (Map k v))
-> (BuiltinList (BuiltinPair BuiltinData BuiltinData)
-> Maybe (Map k v))
-> (BuiltinList BuiltinData -> Maybe (Map k v))
-> (Integer -> Maybe (Map k v))
-> (BuiltinByteString -> Maybe (Map k v))
-> Maybe (Map k v)
forall r.
BuiltinData
-> (Integer -> BuiltinList BuiltinData -> r)
-> (BuiltinList (BuiltinPair BuiltinData BuiltinData) -> r)
-> (BuiltinList BuiltinData -> r)
-> (Integer -> r)
-> (BuiltinByteString -> r)
-> r
P.matchData' BuiltinData
d
(\Integer
_ BuiltinList BuiltinData
_ -> Maybe (Map k v)
forall a. Maybe a
Nothing)
(\BuiltinList (BuiltinPair BuiltinData BuiltinData)
es -> [(k, v)] -> Map k v
forall k v. [(k, v)] -> Map k v
Map ([(k, v)] -> Map k v) -> Maybe [(k, v)] -> Maybe (Map k v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BuiltinList (BuiltinPair BuiltinData BuiltinData) -> Maybe [(k, v)]
traverseFromBuiltin BuiltinList (BuiltinPair BuiltinData BuiltinData)
es)
(Maybe (Map k v) -> BuiltinList BuiltinData -> Maybe (Map k v)
forall a b. a -> b -> a
const Maybe (Map k v)
forall a. Maybe a
Nothing)
(Maybe (Map k v) -> Integer -> Maybe (Map k v)
forall a b. a -> b -> a
const Maybe (Map k v)
forall a. Maybe a
Nothing)
(Maybe (Map k v) -> BuiltinByteString -> Maybe (Map k v)
forall a b. a -> b -> a
const Maybe (Map k v)
forall a. Maybe a
Nothing)
where
{-# INLINE traverseFromBuiltin #-}
traverseFromBuiltin :: BI.BuiltinList (BI.BuiltinPair BI.BuiltinData BI.BuiltinData) -> Maybe [(k, v)]
traverseFromBuiltin :: BuiltinList (BuiltinPair BuiltinData BuiltinData) -> Maybe [(k, v)]
traverseFromBuiltin = BuiltinList (BuiltinPair BuiltinData BuiltinData) -> Maybe [(k, v)]
go
where
go :: BI.BuiltinList (BI.BuiltinPair BI.BuiltinData BI.BuiltinData) -> Maybe [(k, v)]
go :: BuiltinList (BuiltinPair BuiltinData BuiltinData) -> Maybe [(k, v)]
go BuiltinList (BuiltinPair BuiltinData BuiltinData)
l = BuiltinList (BuiltinPair BuiltinData BuiltinData)
-> (() -> Maybe [(k, v)])
-> (() -> Maybe [(k, v)])
-> ()
-> Maybe [(k, v)]
forall a b. BuiltinList a -> b -> b -> b
BI.chooseList BuiltinList (BuiltinPair BuiltinData BuiltinData)
l (Maybe [(k, v)] -> () -> Maybe [(k, v)]
forall a b. a -> b -> a
const ([(k, v)] -> Maybe [(k, v)]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [])) (\()
_ -> let tup :: BuiltinPair BuiltinData BuiltinData
tup = BuiltinList (BuiltinPair BuiltinData BuiltinData)
-> BuiltinPair BuiltinData BuiltinData
forall a. BuiltinList a -> a
BI.head BuiltinList (BuiltinPair BuiltinData BuiltinData)
l in ((k, v) -> [(k, v)] -> [(k, v)])
-> Maybe (k, v) -> Maybe [(k, v)] -> Maybe [(k, v)]
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:) ((k -> v -> (k, v)) -> Maybe k -> Maybe v -> Maybe (k, v)
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (,) (BuiltinData -> Maybe k
forall a. FromData a => BuiltinData -> Maybe a
fromBuiltinData (BuiltinData -> Maybe k) -> BuiltinData -> Maybe k
forall a b. (a -> b) -> a -> b
$ BuiltinPair BuiltinData BuiltinData -> BuiltinData
forall a b. BuiltinPair a b -> a
BI.fst BuiltinPair BuiltinData BuiltinData
tup) (BuiltinData -> Maybe v
forall a. FromData a => BuiltinData -> Maybe a
fromBuiltinData (BuiltinData -> Maybe v) -> BuiltinData -> Maybe v
forall a b. (a -> b) -> a -> b
$ BuiltinPair BuiltinData BuiltinData -> BuiltinData
forall a b. BuiltinPair a b -> b
BI.snd BuiltinPair BuiltinData BuiltinData
tup)) (BuiltinList (BuiltinPair BuiltinData BuiltinData) -> Maybe [(k, v)]
go (BuiltinList (BuiltinPair BuiltinData BuiltinData)
-> BuiltinList (BuiltinPair BuiltinData BuiltinData)
forall a. BuiltinList a -> BuiltinList a
BI.tail BuiltinList (BuiltinPair BuiltinData BuiltinData)
l))) ()
instance (UnsafeFromData k, UnsafeFromData v) => UnsafeFromData (Map k v) where
unsafeFromBuiltinData :: BuiltinData -> Map k v
unsafeFromBuiltinData BuiltinData
d = let es :: BuiltinList (BuiltinPair BuiltinData BuiltinData)
es = BuiltinData -> BuiltinList (BuiltinPair BuiltinData BuiltinData)
BI.unsafeDataAsMap BuiltinData
d in [(k, v)] -> Map k v
forall k v. [(k, v)] -> Map k v
Map ([(k, v)] -> Map k v) -> [(k, v)] -> Map k v
forall a b. (a -> b) -> a -> b
$ BuiltinList (BuiltinPair BuiltinData BuiltinData) -> [(k, v)]
mapFromBuiltin BuiltinList (BuiltinPair BuiltinData BuiltinData)
es
where
{-# INLINE mapFromBuiltin #-}
mapFromBuiltin :: BI.BuiltinList (BI.BuiltinPair BI.BuiltinData BI.BuiltinData) -> [(k, v)]
mapFromBuiltin :: BuiltinList (BuiltinPair BuiltinData BuiltinData) -> [(k, v)]
mapFromBuiltin = BuiltinList (BuiltinPair BuiltinData BuiltinData) -> [(k, v)]
go
where
go :: BI.BuiltinList (BI.BuiltinPair BI.BuiltinData BI.BuiltinData) -> [(k, v)]
go :: BuiltinList (BuiltinPair BuiltinData BuiltinData) -> [(k, v)]
go BuiltinList (BuiltinPair BuiltinData BuiltinData)
l = BuiltinList (BuiltinPair BuiltinData BuiltinData)
-> (() -> [(k, v)]) -> (() -> [(k, v)]) -> () -> [(k, v)]
forall a b. BuiltinList a -> b -> b -> b
BI.chooseList BuiltinList (BuiltinPair BuiltinData BuiltinData)
l ([(k, v)] -> () -> [(k, v)]
forall a b. a -> b -> a
const []) (\()
_ -> let tup :: BuiltinPair BuiltinData BuiltinData
tup = BuiltinList (BuiltinPair BuiltinData BuiltinData)
-> BuiltinPair BuiltinData BuiltinData
forall a. BuiltinList a -> a
BI.head BuiltinList (BuiltinPair BuiltinData BuiltinData)
l in (BuiltinData -> k
forall a. UnsafeFromData a => BuiltinData -> a
unsafeFromBuiltinData (BuiltinData -> k) -> BuiltinData -> k
forall a b. (a -> b) -> a -> b
$ BuiltinPair BuiltinData BuiltinData -> BuiltinData
forall a b. BuiltinPair a b -> a
BI.fst BuiltinPair BuiltinData BuiltinData
tup, BuiltinData -> v
forall a. UnsafeFromData a => BuiltinData -> a
unsafeFromBuiltinData (BuiltinData -> v) -> BuiltinData -> v
forall a b. (a -> b) -> a -> b
$ BuiltinPair BuiltinData BuiltinData -> BuiltinData
forall a b. BuiltinPair a b -> b
BI.snd BuiltinPair BuiltinData BuiltinData
tup) (k, v) -> [(k, v)] -> [(k, v)]
forall a. a -> [a] -> [a]
: BuiltinList (BuiltinPair BuiltinData BuiltinData) -> [(k, v)]
go (BuiltinList (BuiltinPair BuiltinData BuiltinData)
-> BuiltinList (BuiltinPair BuiltinData BuiltinData)
forall a. BuiltinList a -> BuiltinList a
BI.tail BuiltinList (BuiltinPair BuiltinData BuiltinData)
l)) ()
instance Functor (Map k) where
{-# INLINABLE fmap #-}
fmap :: (a -> b) -> Map k a -> Map k b
fmap a -> b
f (Map [(k, a)]
mp) = [(k, b)] -> Map k b
forall k v. [(k, v)] -> Map k v
Map (((k, a) -> (k, b)) -> [(k, a)] -> [(k, b)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> (k, a) -> (k, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) [(k, a)]
mp)
instance Foldable (Map k) where
{-# INLINABLE foldMap #-}
foldMap :: (a -> m) -> Map k a -> m
foldMap a -> m
f (Map [(k, a)]
mp) = ((k, a) -> m) -> [(k, a)] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((a -> m) -> (k, a) -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap a -> m
f) [(k, a)]
mp
instance Traversable (Map k) where
{-# INLINABLE traverse #-}
traverse :: (a -> f b) -> Map k a -> f (Map k b)
traverse a -> f b
f (Map [(k, a)]
mp) = [(k, b)] -> Map k b
forall k v. [(k, v)] -> Map k v
Map ([(k, b)] -> Map k b) -> f [(k, b)] -> f (Map k b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((k, a) -> f (k, b)) -> [(k, a)] -> f [(k, b)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((a -> f b) -> (k, a) -> f (k, b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f) [(k, a)]
mp
instance (Eq k, Semigroup v) => Semigroup (Map k v) where
<> :: Map k v -> Map k v -> Map k v
(<>) = (v -> v -> v) -> Map k v -> Map k v -> Map k v
forall k a. Eq k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWith v -> v -> v
forall a. Semigroup a => a -> a -> a
(<>)
instance (Eq k, Semigroup v) => Monoid (Map k v) where
mempty :: Map k v
mempty = Map k v
forall k v. Map k v
empty
instance (Pretty k, Pretty v) => Pretty (Map k v) where
pretty :: Map k v -> Doc ann
pretty (Map [(k, v)]
mp) = [(k, v)] -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty [(k, v)]
mp
{-# INLINABLE fromList #-}
fromList :: [(k, v)] -> Map k v
fromList :: [(k, v)] -> Map k v
fromList = [(k, v)] -> Map k v
forall k v. [(k, v)] -> Map k v
Map
{-# INLINABLE toList #-}
toList :: Map k v -> [(k, v)]
toList :: Map k v -> [(k, v)]
toList (Map [(k, v)]
l) = [(k, v)]
l
{-# INLINABLE lookup #-}
lookup :: forall k v . (Eq k) => k -> Map k v -> Maybe v
lookup :: k -> Map k v -> Maybe v
lookup k
c (Map [(k, v)]
xs) =
let
go :: [(k, v)] -> Maybe v
go :: [(k, v)] -> Maybe v
go [] = Maybe v
forall a. Maybe a
Nothing
go ((k
c', v
i):[(k, v)]
xs') = if k
c' k -> k -> Bool
forall a. Eq a => a -> a -> Bool
== k
c then v -> Maybe v
forall a. a -> Maybe a
Just v
i else [(k, v)] -> Maybe v
go [(k, v)]
xs'
in [(k, v)] -> Maybe v
go [(k, v)]
xs
{-# INLINABLE member #-}
member :: forall k v . (Eq k) => k -> Map k v -> Bool
member :: k -> Map k v -> Bool
member k
k Map k v
m = Maybe v -> Bool
forall a. Maybe a -> Bool
isJust (k -> Map k v -> Maybe v
forall k v. Eq k => k -> Map k v -> Maybe v
lookup k
k Map k v
m)
{-# INLINABLE insert #-}
insert :: forall k v . (Eq k) => k -> v -> Map k v -> Map k v
insert :: k -> v -> Map k v -> Map k v
insert k
k v
v Map k v
m = (v -> v -> v) -> Map k v -> Map k v -> Map k v
forall k a. Eq k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWith (\v
_ v
b -> v
b) Map k v
m ([(k, v)] -> Map k v
forall k v. [(k, v)] -> Map k v
fromList [(k
k, v
v)])
{-# INLINABLE delete #-}
delete :: forall k v . (Eq k) => k -> Map k v -> Map k v
delete :: k -> Map k v -> Map k v
delete k
key (Map [(k, v)]
ls) = [(k, v)] -> Map k v
forall k v. [(k, v)] -> Map k v
Map ([(k, v)] -> [(k, v)]
go [(k, v)]
ls)
where
go :: [(k, v)] -> [(k, v)]
go [] = []
go ((k
k, v
v) : [(k, v)]
rest) | k
k k -> k -> Bool
forall a. Eq a => a -> a -> Bool
== k
key = [(k, v)]
rest
| Bool
otherwise = (k
k, v
v) (k, v) -> [(k, v)] -> [(k, v)]
forall a. a -> [a] -> [a]
: [(k, v)] -> [(k, v)]
go [(k, v)]
rest
{-# INLINABLE keys #-}
keys :: Map k v -> [k]
keys :: Map k v -> [k]
keys (Map [(k, v)]
xs) = ((k, v) -> k) -> [(k, v)] -> [k]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
P.fmap (\(k
k, v
_ :: v) -> k
k) [(k, v)]
xs
{-# INLINABLE union #-}
union :: forall k v r . (Eq k) => Map k v -> Map k r -> Map k (These v r)
union :: Map k v -> Map k r -> Map k (These v r)
union (Map [(k, v)]
ls) (Map [(k, r)]
rs) =
let
f :: v -> Maybe r -> These v r
f :: v -> Maybe r -> These v r
f v
a Maybe r
b' = case Maybe r
b' of
Maybe r
Nothing -> v -> These v r
forall a b. a -> These a b
This v
a
Just r
b -> v -> r -> These v r
forall a b. a -> b -> These a b
These v
a r
b
ls' :: [(k, These v r)]
ls' :: [(k, These v r)]
ls' = ((k, v) -> (k, These v r)) -> [(k, v)] -> [(k, These v r)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
P.fmap (\(k
c, v
i) -> (k
c, v -> Maybe r -> These v r
f v
i (k -> Map k r -> Maybe r
forall k v. Eq k => k -> Map k v -> Maybe v
lookup k
c ([(k, r)] -> Map k r
forall k v. [(k, v)] -> Map k v
Map [(k, r)]
rs)))) [(k, v)]
ls
rs' :: [(k, r)]
rs' :: [(k, r)]
rs' = ((k, r) -> Bool) -> [(k, r)] -> [(k, r)]
forall a. (a -> Bool) -> [a] -> [a]
P.filter (\(k
c, r
_) -> Bool -> Bool
not (((k, v) -> Bool) -> [(k, v)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\(k
c', v
_) -> k
c' k -> k -> Bool
forall a. Eq a => a -> a -> Bool
== k
c) [(k, v)]
ls)) [(k, r)]
rs
rs'' :: [(k, These v r)]
rs'' :: [(k, These v r)]
rs'' = ((k, r) -> (k, These v r)) -> [(k, r)] -> [(k, These v r)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
P.fmap ((r -> These v r) -> (k, r) -> (k, These v r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
P.fmap r -> These v r
forall a b. b -> These a b
That) [(k, r)]
rs'
in [(k, These v r)] -> Map k (These v r)
forall k v. [(k, v)] -> Map k v
Map ([(k, These v r)]
ls' [(k, These v r)] -> [(k, These v r)] -> [(k, These v r)]
forall a. [a] -> [a] -> [a]
++ [(k, These v r)]
rs'')
{-# INLINABLE unionWith #-}
unionWith :: forall k a . (Eq k) => (a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWith :: (a -> a -> a) -> Map k a -> Map k a -> Map k a
unionWith a -> a -> a
merge Map k a
ls Map k a
rs = (a -> a) -> (a -> a) -> (a -> a -> a) -> These a a -> a
forall a c b.
(a -> c) -> (b -> c) -> (a -> b -> c) -> These a b -> c
these a -> a
forall a. a -> a
id a -> a
forall a. a -> a
id a -> a -> a
merge (These a a -> a) -> Map k (These a a) -> Map k a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map k a -> Map k a -> Map k (These a a)
forall k v r. Eq k => Map k v -> Map k r -> Map k (These v r)
union Map k a
ls Map k a
rs
{-# INLINABLE mapThese #-}
mapThese :: (v -> These a b) -> Map k v -> (Map k a, Map k b)
mapThese :: (v -> These a b) -> Map k v -> (Map k a, Map k b)
mapThese v -> These a b
f Map k v
mps = ([(k, a)] -> Map k a
forall k v. [(k, v)] -> Map k v
Map [(k, a)]
mpl, [(k, b)] -> Map k b
forall k v. [(k, v)] -> Map k v
Map [(k, b)]
mpr) where
([(k, a)]
mpl, [(k, b)]
mpr) = ((k, These a b) -> ([(k, a)], [(k, b)]) -> ([(k, a)], [(k, b)]))
-> ([(k, a)], [(k, b)]) -> [(k, These a b)] -> ([(k, a)], [(k, b)])
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
P.foldr (k, These a b) -> ([(k, a)], [(k, b)]) -> ([(k, a)], [(k, b)])
forall a b b.
(a, These b b) -> ([(a, b)], [(a, b)]) -> ([(a, b)], [(a, b)])
f' ([], []) [(k, These a b)]
mps'
Map [(k, These a b)]
mps' = (v -> These a b) -> Map k v -> Map k (These a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap v -> These a b
f Map k v
mps
f' :: (a, These b b) -> ([(a, b)], [(a, b)]) -> ([(a, b)], [(a, b)])
f' (a
k, These b b
v) ([(a, b)]
as, [(a, b)]
bs) = case These b b
v of
This b
a -> ((a
k, b
a)(a, b) -> [(a, b)] -> [(a, b)]
forall a. a -> [a] -> [a]
:[(a, b)]
as, [(a, b)]
bs)
That b
b -> ([(a, b)]
as, (a
k, b
b)(a, b) -> [(a, b)] -> [(a, b)]
forall a. a -> [a] -> [a]
:[(a, b)]
bs)
These b
a b
b -> ((a
k, b
a)(a, b) -> [(a, b)] -> [(a, b)]
forall a. a -> [a] -> [a]
:[(a, b)]
as, (a
k, b
b)(a, b) -> [(a, b)] -> [(a, b)]
forall a. a -> [a] -> [a]
:[(a, b)]
bs)
singleton :: k -> v -> Map k v
singleton :: k -> v -> Map k v
singleton k
c v
i = [(k, v)] -> Map k v
forall k v. [(k, v)] -> Map k v
Map [(k
c, v
i)]
{-# INLINABLE empty #-}
empty :: Map k v
empty :: Map k v
empty = [(k, v)] -> Map k v
forall k v. [(k, v)] -> Map k v
Map ([] :: [(k, v)])
{-# INLINABLE null #-}
null :: Map k v -> Bool
null :: Map k v -> Bool
null = [(k, v)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
P.null ([(k, v)] -> Bool) -> (Map k v -> [(k, v)]) -> Map k v -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map k v -> [(k, v)]
forall k v. Map k v -> [(k, v)]
unMap
{-# INLINABLE filter #-}
filter :: (v -> Bool) -> Map k v -> Map k v
filter :: (v -> Bool) -> Map k v -> Map k v
filter v -> Bool
f (Map [(k, v)]
m) = [(k, v)] -> Map k v
forall k v. [(k, v)] -> Map k v
Map ([(k, v)] -> Map k v) -> [(k, v)] -> Map k v
forall a b. (a -> b) -> a -> b
$ ((k, v) -> Bool) -> [(k, v)] -> [(k, v)]
forall a. (a -> Bool) -> [a] -> [a]
P.filter (v -> Bool
f (v -> Bool) -> ((k, v) -> v) -> (k, v) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k, v) -> v
forall a b. (a, b) -> b
snd) [(k, v)]
m
{-# INLINABLE elems #-}
elems :: Map k v -> [v]
elems :: Map k v -> [v]
elems (Map [(k, v)]
xs) = ((k, v) -> v) -> [(k, v)] -> [v]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
P.fmap (\(k
_ :: k, v
v) -> v
v) [(k, v)]
xs
{-# INLINABLE mapWithKey #-}
mapWithKey :: (k -> a -> b) -> Map k a -> Map k b
mapWithKey :: (k -> a -> b) -> Map k a -> Map k b
mapWithKey k -> a -> b
f (Map [(k, a)]
xs) = [(k, b)] -> Map k b
forall k v. [(k, v)] -> Map k v
Map ([(k, b)] -> Map k b) -> [(k, b)] -> Map k b
forall a b. (a -> b) -> a -> b
$ ((k, a) -> (k, b)) -> [(k, a)] -> [(k, b)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(k
k, a
v) -> (k
k, k -> a -> b
f k
k a
v)) [(k, a)]
xs
{-# INLINABLE mapMaybe #-}
mapMaybe :: (a -> Maybe b) -> Map k a -> Map k b
mapMaybe :: (a -> Maybe b) -> Map k a -> Map k b
mapMaybe a -> Maybe b
f (Map [(k, a)]
xs) = [(k, b)] -> Map k b
forall k v. [(k, v)] -> Map k v
Map ([(k, b)] -> Map k b) -> [(k, b)] -> Map k b
forall a b. (a -> b) -> a -> b
$ ((k, a) -> Maybe (k, b)) -> [(k, a)] -> [(k, b)]
forall a b. (a -> Maybe b) -> [a] -> [b]
P.mapMaybe (\(k
k, a
v) -> (k
k, ) (b -> (k, b)) -> Maybe b -> Maybe (k, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> Maybe b
f a
v) [(k, a)]
xs
{-# INLINABLE mapMaybeWithKey #-}
mapMaybeWithKey :: (k -> a -> Maybe b) -> Map k a -> Map k b
mapMaybeWithKey :: (k -> a -> Maybe b) -> Map k a -> Map k b
mapMaybeWithKey k -> a -> Maybe b
f (Map [(k, a)]
xs) = [(k, b)] -> Map k b
forall k v. [(k, v)] -> Map k v
Map ([(k, b)] -> Map k b) -> [(k, b)] -> Map k b
forall a b. (a -> b) -> a -> b
$ ((k, a) -> Maybe (k, b)) -> [(k, a)] -> [(k, b)]
forall a b. (a -> Maybe b) -> [a] -> [b]
P.mapMaybe (\(k
k, a
v) -> (k
k, ) (b -> (k, b)) -> Maybe b -> Maybe (k, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> k -> a -> Maybe b
f k
k a
v) [(k, a)]
xs
makeLift ''Map