module Agda.Utils.AssocList
( module Agda.Utils.AssocList
, lookup
) where
import Prelude hiding (lookup)
import Data.Function
import Data.List (lookup)
import qualified Data.List as List
import qualified Data.Map as Map
import Agda.Utils.Tuple
import Agda.Utils.Impossible
type AssocList k v = [(k,v)]
apply :: Ord k => AssocList k v -> k -> Maybe v
apply :: forall k v. Ord k => AssocList k v -> k -> Maybe v
apply AssocList k v
m = (forall k a. Ord k => k -> Map k a -> Maybe a
`Map.lookup` forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith (\ v
_new v
old -> v
old) AssocList k v
m)
keys :: AssocList k v -> [k]
keys :: forall k v. AssocList k v -> [k]
keys = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst
insert :: k -> v -> AssocList k v -> AssocList k v
insert :: forall k v. k -> v -> AssocList k v -> AssocList k v
insert k
k v
v = ((k
k,v
v) forall a. a -> [a] -> [a]
:)
update :: Eq k => k -> v -> AssocList k v -> AssocList k v
update :: forall k v. Eq k => k -> v -> AssocList k v -> AssocList k v
update k
k v
v = forall k v. Eq k => k -> (v -> v) -> AssocList k v -> AssocList k v
updateAt k
k forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const v
v
delete :: Eq k => k -> AssocList k v -> AssocList k v
delete :: forall k v. Eq k => k -> AssocList k v -> AssocList k v
delete k
k = forall a. (a -> a -> Bool) -> a -> [a] -> [a]
List.deleteBy (forall a. Eq a => a -> a -> Bool
(==) forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall a b. (a, b) -> a
fst) (k
k, forall a. HasCallStack => a
__IMPOSSIBLE__)
updateAt :: Eq k => k -> (v -> v) -> AssocList k v -> AssocList k v
updateAt :: forall k v. Eq k => k -> (v -> v) -> AssocList k v -> AssocList k v
updateAt k
k v -> v
f = [(k, v)] -> [(k, v)]
loop where
loop :: [(k, v)] -> [(k, v)]
loop [] = forall a. HasCallStack => a
__IMPOSSIBLE__
loop (p :: (k, v)
p@(k
k',v
v) : [(k, v)]
ps)
| k
k forall a. Eq a => a -> a -> Bool
== k
k' = (k
k, v -> v
f v
v) forall a. a -> [a] -> [a]
: [(k, v)]
ps
| Bool
otherwise = (k, v)
p forall a. a -> [a] -> [a]
: [(k, v)] -> [(k, v)]
loop [(k, v)]
ps
mapWithKey :: (k -> v -> v) -> AssocList k v -> AssocList k v
mapWithKey :: forall k v. (k -> v -> v) -> AssocList k v -> AssocList k v
mapWithKey k -> v -> v
f = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a -> b) -> a -> b
$ \ (k
k,v
v) -> (k
k, k -> v -> v
f k
k v
v)
mapWithKeyM :: Applicative m => (k -> v -> m v) -> AssocList k v -> m (AssocList k v)
mapWithKeyM :: forall (m :: * -> *) k v.
Applicative m =>
(k -> v -> m v) -> AssocList k v -> m (AssocList k v)
mapWithKeyM k -> v -> m v
f = forall {f :: * -> *} {t} {a}.
Applicative f =>
(t -> f a) -> [t] -> f [a]
mapM forall a b. (a -> b) -> a -> b
$ \ (k
k,v
v) -> (k
k,) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> k -> v -> m v
f k
k v
v
where
mapM :: (t -> f a) -> [t] -> f [a]
mapM t -> f a
g [] = forall (f :: * -> *) a. Applicative f => a -> f a
pure []
mapM t -> f a
g (t
x : [t]
xs) = (:) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t -> f a
g t
x forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (t -> f a) -> [t] -> f [a]
mapM t -> f a
g [t]
xs
mapKeysMonotonic :: (k -> k') -> AssocList k v -> AssocList k' v
mapKeysMonotonic :: forall k k' v. (k -> k') -> AssocList k v -> AssocList k' v
mapKeysMonotonic k -> k'
f = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a -> b) -> a -> b
$ forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst k -> k'
f