dependent-map-0.3.1.0: Dependent finite maps (partial dependent products)
Safe HaskellTrustworthy
LanguageHaskell98

Data.Dependent.Map

Synopsis

Documentation

data DMap k f Source #

Dependent maps: k is a GADT-like thing with a facility for rediscovering its type parameter, elements of which function as identifiers tagged with the type of the thing they identify. Real GADTs are one useful instantiation of k, as are Tags from Data.Unique.Tag in the 'prim-uniq' package.

Semantically, DMap k f is equivalent to a set of DSum k f where no two elements have the same tag.

More informally, DMap is to dependent products as Map is to (->). Thus it could also be thought of as a partial (in the sense of "partial function") dependent product.

Instances

Instances details
(GEq k2, Has' Eq k2 f) => Eq (DMap k2 f) Source # 
Instance details

Defined in Data.Dependent.Map

Methods

(==) :: DMap k2 f -> DMap k2 f -> Bool #

(/=) :: DMap k2 f -> DMap k2 f -> Bool #

(GCompare k2, Has' Eq k2 f, Has' Ord k2 f) => Ord (DMap k2 f) Source # 
Instance details

Defined in Data.Dependent.Map

Methods

compare :: DMap k2 f -> DMap k2 f -> Ordering #

(<) :: DMap k2 f -> DMap k2 f -> Bool #

(<=) :: DMap k2 f -> DMap k2 f -> Bool #

(>) :: DMap k2 f -> DMap k2 f -> Bool #

(>=) :: DMap k2 f -> DMap k2 f -> Bool #

max :: DMap k2 f -> DMap k2 f -> DMap k2 f #

min :: DMap k2 f -> DMap k2 f -> DMap k2 f #

(GCompare k2, GRead k2, Has' Read k2 f) => Read (DMap k2 f) Source # 
Instance details

Defined in Data.Dependent.Map

Methods

readsPrec :: Int -> ReadS (DMap k2 f) #

readList :: ReadS [DMap k2 f] #

readPrec :: ReadPrec (DMap k2 f) #

readListPrec :: ReadPrec [DMap k2 f] #

(GShow k2, Has' Show k2 f) => Show (DMap k2 f) Source # 
Instance details

Defined in Data.Dependent.Map

Methods

showsPrec :: Int -> DMap k2 f -> ShowS #

show :: DMap k2 f -> String #

showList :: [DMap k2 f] -> ShowS #

GCompare k2 => Semigroup (DMap k2 f) Source # 
Instance details

Defined in Data.Dependent.Map

Methods

(<>) :: DMap k2 f -> DMap k2 f -> DMap k2 f #

sconcat :: NonEmpty (DMap k2 f) -> DMap k2 f #

stimes :: Integral b => b -> DMap k2 f -> DMap k2 f #

GCompare k2 => Monoid (DMap k2 f) Source # 
Instance details

Defined in Data.Dependent.Map

Methods

mempty :: DMap k2 f #

mappend :: DMap k2 f -> DMap k2 f -> DMap k2 f #

mconcat :: [DMap k2 f] -> DMap k2 f #

data DSum (tag :: k -> Type) (f :: k -> Type) #

A basic dependent sum type where the first component is a tag that specifies the type of the second. For example, think of a GADT such as:

data Tag a where
   AString :: Tag String
   AnInt   :: Tag Int
   Rec     :: Tag (DSum Tag Identity)

Then we can write expressions where the RHS of (:=>) has different types depending on the Tag constructor used. Here are some expressions of type DSum Tag Identity:

AString :=> Identity "hello!"
AnInt   :=> Identity 42

Often, the f we choose has an Applicative instance, and we can use the helper function (==>). The following expressions all have the type Applicative f => DSum Tag f:

AString ==> "hello!"
AnInt   ==> 42

We can write functions that consume DSum Tag f values by matching, such as:

toString :: DSum Tag Identity -> String
toString (AString :=> Identity str) = str
toString (AnInt   :=> Identity int) = show int
toString (Rec     :=> Identity sum) = toString sum

The (:=>) constructor and (==>) helper are chosen to resemble the (key => value) construction for dictionary entries in many dynamic languages. The :=> and ==> operators have very low precedence and bind to the right, making repeated use of these operators behave as you'd expect:

-- Parses as: Rec ==> (AnInt ==> (3 + 4))
-- Has type: Applicative f => DSum Tag f
Rec ==> AnInt ==> 3 + 4

The precedence of these operators is just above that of $, so foo bar $ AString ==> "eep" is equivalent to foo bar (AString ==> "eep").

To use the Eq, Ord, Read, and Show instances for DSum tag f, you will need an ArgDict instance for your tag type. Use deriveArgDict from the constraints-extras package to generate this instance.

Constructors

!(tag a) :=> (f a) infixr 1 

Instances

Instances details
(GEq tag, Has' Eq tag f) => Eq (DSum tag f) 
Instance details

Defined in Data.Dependent.Sum

Methods

(==) :: DSum tag f -> DSum tag f -> Bool #

(/=) :: DSum tag f -> DSum tag f -> Bool #

(GCompare tag, Has' Eq tag f, Has' Ord tag f) => Ord (DSum tag f) 
Instance details

Defined in Data.Dependent.Sum

Methods

compare :: DSum tag f -> DSum tag f -> Ordering #

(<) :: DSum tag f -> DSum tag f -> Bool #

(<=) :: DSum tag f -> DSum tag f -> Bool #

(>) :: DSum tag f -> DSum tag f -> Bool #

(>=) :: DSum tag f -> DSum tag f -> Bool #

max :: DSum tag f -> DSum tag f -> DSum tag f #

min :: DSum tag f -> DSum tag f -> DSum tag f #

(GRead tag, Has' Read tag f) => Read (DSum tag f) 
Instance details

Defined in Data.Dependent.Sum

Methods

readsPrec :: Int -> ReadS (DSum tag f) #

readList :: ReadS [DSum tag f] #

readPrec :: ReadPrec (DSum tag f) #

readListPrec :: ReadPrec [DSum tag f] #

(GShow tag, Has' Show tag f) => Show (DSum tag f) 
Instance details

Defined in Data.Dependent.Sum

Methods

showsPrec :: Int -> DSum tag f -> ShowS #

show :: DSum tag f -> String #

showList :: [DSum tag f] -> ShowS #

data Some (tag :: k -> Type) where #

Existential. This is type is useful to hide GADTs' parameters.

>>> data Tag :: * -> * where TagInt :: Tag Int; TagBool :: Tag Bool
>>> instance GShow Tag where gshowsPrec _ TagInt = showString "TagInt"; gshowsPrec _ TagBool = showString "TagBool"
>>> classify s = case s of "TagInt" -> [mkGReadResult TagInt]; "TagBool" -> [mkGReadResult TagBool]; _ -> []
>>> instance GRead Tag where greadsPrec _ s = [ (r, rest) | (con, rest) <-  lex s, r <- classify con ]

You can either use PatternSynonyms (available with GHC >= 8.0)

>>> let x = Some TagInt
>>> x
Some TagInt
>>> case x of { Some TagInt -> "I"; Some TagBool -> "B" } :: String
"I"

or you can use functions

>>> let y = mkSome TagBool
>>> y
Some TagBool
>>> withSome y $ \y' -> case y' of { TagInt -> "I"; TagBool -> "B" } :: String
"B"

The implementation of mapSome is safe.

>>> let f :: Tag a -> Tag a; f TagInt = TagInt; f TagBool = TagBool
>>> mapSome f y
Some TagBool

but you can also use:

>>> withSome y (mkSome . f)
Some TagBool
>>> read "Some TagBool" :: Some Tag
Some TagBool
>>> read "mkSome TagInt" :: Some Tag
Some TagInt

Bundled Patterns

pattern Some :: forall k tag (a :: k). () => tag a -> Some tag 

Instances

Instances details
GEq tag => Eq (Some tag) 
Instance details

Defined in Data.Some.Newtype

Methods

(==) :: Some tag -> Some tag -> Bool #

(/=) :: Some tag -> Some tag -> Bool #

GCompare tag => Ord (Some tag) 
Instance details

Defined in Data.Some.Newtype

Methods

compare :: Some tag -> Some tag -> Ordering #

(<) :: Some tag -> Some tag -> Bool #

(<=) :: Some tag -> Some tag -> Bool #

(>) :: Some tag -> Some tag -> Bool #

(>=) :: Some tag -> Some tag -> Bool #

max :: Some tag -> Some tag -> Some tag #

min :: Some tag -> Some tag -> Some tag #

GRead f => Read (Some f) 
Instance details

Defined in Data.Some.Newtype

GShow tag => Show (Some tag) 
Instance details

Defined in Data.Some.Newtype

Methods

showsPrec :: Int -> Some tag -> ShowS #

show :: Some tag -> String #

showList :: [Some tag] -> ShowS #

Applicative m => Semigroup (Some m) 
Instance details

Defined in Data.Some.Newtype

Methods

(<>) :: Some m -> Some m -> Some m #

sconcat :: NonEmpty (Some m) -> Some m #

stimes :: Integral b => b -> Some m -> Some m #

Applicative m => Monoid (Some m) 
Instance details

Defined in Data.Some.Newtype

Methods

mempty :: Some m #

mappend :: Some m -> Some m -> Some m #

mconcat :: [Some m] -> Some m #

GNFData tag => NFData (Some tag) 
Instance details

Defined in Data.Some.Newtype

Methods

rnf :: Some tag -> () #

class GEq f => GCompare (f :: k -> Type) where #

Type class for comparable GADT-like structures. When 2 things are equal, must return a witness that their parameter types are equal as well (GEQ).

Methods

gcompare :: forall (a :: k) (b :: k). f a -> f b -> GOrdering a b #

Instances

Instances details
GCompare (TypeRep :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

gcompare :: forall (a :: k0) (b :: k0). TypeRep a -> TypeRep b -> GOrdering a b #

GCompare ((:~:) a :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

gcompare :: forall (a0 :: k0) (b :: k0). (a :~: a0) -> (a :~: b) -> GOrdering a0 b #

(GCompare a, GCompare b) => GCompare (Sum a b :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

gcompare :: forall (a0 :: k0) (b0 :: k0). Sum a b a0 -> Sum a b b0 -> GOrdering a0 b0 #

(GCompare a, GCompare b) => GCompare (Product a b :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

gcompare :: forall (a0 :: k0) (b0 :: k0). Product a b a0 -> Product a b b0 -> GOrdering a0 b0 #

data GOrdering (a :: k) (b :: k) where #

A type for the result of comparing GADT constructors; the type parameters of the GADT values being compared are included so that in the case where they are equal their parameter types can be unified.

Constructors

GLT :: forall k (a :: k) (b :: k). GOrdering a b 
GEQ :: forall k (a :: k). GOrdering a a 
GGT :: forall k (a :: k) (b :: k). GOrdering a b 

Instances

Instances details
GShow (GOrdering a :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

gshowsPrec :: forall (a0 :: k0). Int -> GOrdering a a0 -> ShowS #

GRead (GOrdering a :: k -> Type) 
Instance details

Defined in Data.GADT.Internal

Methods

greadsPrec :: Int -> GReadS (GOrdering a) #

Eq (GOrdering a b) 
Instance details

Defined in Data.GADT.Internal

Methods

(==) :: GOrdering a b -> GOrdering a b -> Bool #

(/=) :: GOrdering a b -> GOrdering a b -> Bool #

Ord (GOrdering a b) 
Instance details

Defined in Data.GADT.Internal

Methods

compare :: GOrdering a b -> GOrdering a b -> Ordering #

(<) :: GOrdering a b -> GOrdering a b -> Bool #

(<=) :: GOrdering a b -> GOrdering a b -> Bool #

(>) :: GOrdering a b -> GOrdering a b -> Bool #

(>=) :: GOrdering a b -> GOrdering a b -> Bool #

max :: GOrdering a b -> GOrdering a b -> GOrdering a b #

min :: GOrdering a b -> GOrdering a b -> GOrdering a b #

Show (GOrdering a b) 
Instance details

Defined in Data.GADT.Internal

Methods

showsPrec :: Int -> GOrdering a b -> ShowS #

show :: GOrdering a b -> String #

showList :: [GOrdering a b] -> ShowS #

Operators

(!) :: GCompare k => DMap k f -> k v -> f v infixl 9 Source #

O(log n). Find the value at a key. Calls error when the element can not be found.

fromList [(5,'a'), (3,'b')] ! 1    Error: element not in the map
fromList [(5,'a'), (3,'b')] ! 5 == 'a'

(\\) :: GCompare k => DMap k f -> DMap k f -> DMap k f infixl 9 Source #

Same as difference.

Query

null :: DMap k f -> Bool Source #

O(1). Is the map empty?

size :: DMap k f -> Int Source #

O(1). The number of elements in the map.

member :: GCompare k => k a -> DMap k f -> Bool Source #

O(log n). Is the key a member of the map? See also notMember.

notMember :: GCompare k => k v -> DMap k f -> Bool Source #

O(log n). Is the key not a member of the map? See also member.

lookup :: forall k f v. GCompare k => k v -> DMap k f -> Maybe (f v) Source #

O(log n). Lookup the value at a key in the map.

The function will return the corresponding value as (Just value), or Nothing if the key isn't in the map.

findWithDefault :: GCompare k => f v -> k v -> DMap k f -> f v Source #

O(log n). The expression (findWithDefault def k map) returns the value at key k or returns default value def when the key is not in the map.

Construction

empty :: DMap k f Source #

O(1). The empty map.

empty      == fromList []
size empty == 0

singleton :: k v -> f v -> DMap k f Source #

O(1). A map with a single element.

singleton 1 'a'        == fromList [(1, 'a')]
size (singleton 1 'a') == 1

Insertion

insert :: forall k f v. GCompare k => k v -> f v -> DMap k f -> DMap k f Source #

O(log n). Insert a new key and value in the map. If the key is already present in the map, the associated value is replaced with the supplied value. insert is equivalent to insertWith const.

insertWith :: GCompare k => (f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f Source #

O(log n). Insert with a function, combining new value and old value. insertWith f key value mp will insert the entry key :=> value into mp if key does not exist in the map. If the key does exist, the function will insert the entry key :=> f new_value old_value.

insertWith' :: GCompare k => (f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f Source #

Same as insertWith, but the combining function is applied strictly. This is often the most desirable behavior.

insertWithKey :: forall k f v. GCompare k => (k v -> f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f Source #

O(log n). Insert with a function, combining key, new value and old value. insertWithKey f key value mp will insert the entry key :=> value into mp if key does not exist in the map. If the key does exist, the function will insert the entry key :=> f key new_value old_value. Note that the key passed to f is the same key passed to insertWithKey.

insertWithKey' :: forall k f v. GCompare k => (k v -> f v -> f v -> f v) -> k v -> f v -> DMap k f -> DMap k f Source #

Same as insertWithKey, but the combining function is applied strictly.

insertLookupWithKey :: forall k f v. GCompare k => (k v -> f v -> f v -> f v) -> k v -> f v -> DMap k f -> (Maybe (f v), DMap k f) Source #

O(log n). Combines insert operation with old value retrieval. The expression (insertLookupWithKey f k x map) is a pair where the first element is equal to (lookup k map) and the second element equal to (insertWithKey f k x map).

insertLookupWithKey' :: forall k f v. GCompare k => (k v -> f v -> f v -> f v) -> k v -> f v -> DMap k f -> (Maybe (f v), DMap k f) Source #

O(log n). A strict version of insertLookupWithKey.

Delete/Update

delete :: forall k f v. GCompare k => k v -> DMap k f -> DMap k f Source #

O(log n). Delete a key and its value from the map. When the key is not a member of the map, the original map is returned.

adjust :: GCompare k => (f v -> f v) -> k v -> DMap k f -> DMap k f Source #

O(log n). Update a value at a specific key with the result of the provided function. When the key is not a member of the map, the original map is returned.

adjustWithKey :: GCompare k => (k v -> f v -> f v) -> k v -> DMap k f -> DMap k f Source #

O(log n). Adjust a value at a specific key. When the key is not a member of the map, the original map is returned.

adjustWithKey' :: GCompare k => (k v -> f v -> f v) -> k v -> DMap k f -> DMap k f Source #

O(log n). A strict version of adjustWithKey.

update :: GCompare k => (f v -> Maybe (f v)) -> k v -> DMap k f -> DMap k f Source #

O(log n). The expression (update f k map) updates the value x at k (if it is in the map). If (f x) is Nothing, the element is deleted. If it is (Just y), the key k is bound to the new value y.

updateWithKey :: forall k f v. GCompare k => (k v -> f v -> Maybe (f v)) -> k v -> DMap k f -> DMap k f Source #

O(log n). The expression (updateWithKey f k map) updates the value x at k (if it is in the map). If (f k x) is Nothing, the element is deleted. If it is (Just y), the key k is bound to the new value y.

updateLookupWithKey :: forall k f v. GCompare k => (k v -> f v -> Maybe (f v)) -> k v -> DMap k f -> (Maybe (f v), DMap k f) Source #

O(log n). Lookup and update. See also updateWithKey. The function returns changed value, if it is updated. Returns the original key value if the map entry is deleted.

alter :: forall k f v. GCompare k => (Maybe (f v) -> Maybe (f v)) -> k v -> DMap k f -> DMap k f Source #

O(log n). The expression (alter f k map) alters the value x at k, or absence thereof. alter can be used to insert, delete, or update a value in a Map. In short : lookup k (alter f k m) = f (lookup k m).

alterF :: forall k f v g. (GCompare k, Functor f) => k v -> (Maybe (g v) -> f (Maybe (g v))) -> DMap k g -> f (DMap k g) Source #

Works the same as alter except the new value is returned in some Functor f. In short : (v' -> alter (const v') k dm) $ f (lookup k dm)

Combine

Union

union :: GCompare k => DMap k f -> DMap k f -> DMap k f Source #

O(m*log(n/m + 1)), m <= n. The expression (union t1 t2) takes the left-biased union of t1 and t2. It prefers t1 when duplicate keys are encountered, i.e. (union == unionWith const).

unionWithKey :: GCompare k => (forall v. k v -> f v -> f v -> f v) -> DMap k f -> DMap k f -> DMap k f Source #

O(n+m). Union with a combining function.

unions :: GCompare k => [DMap k f] -> DMap k f Source #

The union of a list of maps: (unions == foldl union empty).

unionsWithKey :: GCompare k => (forall v. k v -> f v -> f v -> f v) -> [DMap k f] -> DMap k f Source #

The union of a list of maps, with a combining operation: (unionsWithKey f == foldl (unionWithKey f) empty).

Difference

difference :: GCompare k => DMap k f -> DMap k g -> DMap k f Source #

O(m * log (n/m + 1)), m <= n. Difference of two maps. Return elements of the first map not existing in the second map.

differenceWithKey :: GCompare k => (forall v. k v -> f v -> g v -> Maybe (f v)) -> DMap k f -> DMap k g -> DMap k f Source #

O(n+m). Difference with a combining function. When two equal keys are encountered, the combining function is applied to the key and both values. If it returns Nothing, the element is discarded (proper set difference). If it returns (Just y), the element is updated with a new value y.

Intersection

intersection :: GCompare k => DMap k f -> DMap k f -> DMap k f Source #

O(m * log (n/m + 1), m <= n. Intersection of two maps. Return data in the first map for the keys existing in both maps. (intersection m1 m2 == intersectionWith const m1 m2).

intersectionWithKey :: GCompare k => (forall v. k v -> f v -> g v -> h v) -> DMap k f -> DMap k g -> DMap k h Source #

O(m * log (n/m + 1), m <= n. Intersection with a combining function.

Traversal

Map

map :: (forall v. f v -> g v) -> DMap k f -> DMap k g Source #

O(n). Map a function over all values in the map.

ffor :: DMap k f -> (forall v. f v -> g v) -> DMap k g Source #

O(n). ffor == flip map except we cannot actually use flip because of the lack of impredicative types.

mapWithKey :: (forall v. k v -> f v -> g v) -> DMap k f -> DMap k g Source #

O(n). Map a function over all values in the map.

fforWithKey :: DMap k f -> (forall v. k v -> f v -> g v) -> DMap k g Source #

O(n). fforWithKey == flip mapWithKey except we cannot actually use flip because of the lack of impredicative types.

traverseWithKey_ :: Applicative t => (forall v. k v -> f v -> t ()) -> DMap k f -> t () Source #

O(n). traverseWithKey f m == fromList $ traverse ((k, v) -> (,) k $ f k v) (toList m) That is, behaves exactly like a regular traverse except that the traversing function also has access to the key associated with a value.

forWithKey_ :: Applicative t => DMap k f -> (forall v. k v -> f v -> t ()) -> t () Source #

O(n). forWithKey == flip traverseWithKey except we cannot actually use flip because of the lack of impredicative types.

traverseWithKey :: Applicative t => (forall v. k v -> f v -> t (g v)) -> DMap k f -> t (DMap k g) Source #

O(n). traverseWithKey f m == fromList $ traverse ((k, v) -> (,) k $ f k v) (toList m) That is, behaves exactly like a regular traverse except that the traversing function also has access to the key associated with a value.

forWithKey :: Applicative t => DMap k f -> (forall v. k v -> f v -> t (g v)) -> t (DMap k g) Source #

O(n). forWithKey == flip traverseWithKey except we cannot actually use flip because of the lack of impredicative types.

mapAccumLWithKey :: (forall v. a -> k v -> f v -> (a, g v)) -> a -> DMap k f -> (a, DMap k g) Source #

O(n). The function mapAccumLWithKey threads an accumulating argument through the map in ascending order of keys.

mapAccumRWithKey :: (forall v. a -> k v -> f v -> (a, g v)) -> a -> DMap k f -> (a, DMap k g) Source #

O(n). The function mapAccumRWithKey threads an accumulating argument through the map in descending order of keys.

mapKeysWith :: GCompare k2 => (forall v. k2 v -> f v -> f v -> f v) -> (forall v. k1 v -> k2 v) -> DMap k1 f -> DMap k2 f Source #

O(n*log n). mapKeysWith c f s is the map obtained by applying f to each key of s.

The size of the result may be smaller if f maps two or more distinct keys to the same new key. In this case the associated values will be combined using c.

mapKeysMonotonic :: (forall v. k1 v -> k2 v) -> DMap k1 f -> DMap k2 f Source #

O(n). mapKeysMonotonic f s == mapKeys f s, but works only when f is strictly monotonic. That is, for any values x and y, if x < y then f x < f y. The precondition is not checked. Semi-formally, we have:

and [x < y ==> f x < f y | x <- ls, y <- ls]
                    ==> mapKeysMonotonic f s == mapKeys f s
    where ls = keys s

This means that f maps distinct original keys to distinct resulting keys. This function has better performance than mapKeys.

Fold

foldWithKey :: (forall v. k v -> f v -> b -> b) -> b -> DMap k f -> b Source #

Deprecated: Use foldrWithKey instead

O(n). Fold the keys and values in the map, such that foldWithKey f z == foldr (uncurry f) z . toAscList.

This is identical to foldrWithKey, and you should use that one instead of this one. This name is kept for backward compatibility.

foldrWithKey :: (forall v. k v -> f v -> b -> b) -> b -> DMap k f -> b Source #

O(n). Post-order fold. The function will be applied from the lowest value to the highest.

foldlWithKey :: (forall v. b -> k v -> f v -> b) -> b -> DMap k f -> b Source #

O(n). Pre-order fold. The function will be applied from the highest value to the lowest.

Conversion

keys :: DMap k f -> [Some k] Source #

O(n). Return all keys of the map in ascending order.

keys (fromList [(5,"a"), (3,"b")]) == [3,5]
keys empty == []

assocs :: DMap k f -> [DSum k f] Source #

O(n). Return all key/value pairs in the map in ascending key order.

Lists

toList :: DMap k f -> [DSum k f] Source #

O(n). Convert to a list of key/value pairs.

fromList :: GCompare k => [DSum k f] -> DMap k f Source #

O(n*log n). Build a map from a list of key/value pairs. See also fromAscList. If the list contains more than one value for the same key, the last value for the key is retained.

fromListWithKey :: GCompare k => (forall v. k v -> f v -> f v -> f v) -> [DSum k f] -> DMap k f Source #

O(n*log n). Build a map from a list of key/value pairs with a combining function. See also fromAscListWithKey.

Ordered lists

toAscList :: DMap k f -> [DSum k f] Source #

O(n). Convert to an ascending list.

toDescList :: DMap k f -> [DSum k f] Source #

O(n). Convert to a descending list.

fromAscList :: GEq k => [DSum k f] -> DMap k f Source #

O(n). Build a map from an ascending list in linear time. The precondition (input list is ascending) is not checked.

fromAscListWithKey :: GEq k => (forall v. k v -> f v -> f v -> f v) -> [DSum k f] -> DMap k f Source #

O(n). Build a map from an ascending list in linear time with a combining function for equal keys. The precondition (input list is ascending) is not checked.

fromDistinctAscList :: [DSum k f] -> DMap k f Source #

O(n). Build a map from an ascending list of distinct elements in linear time. The precondition is not checked.

Filter

filter :: (a -> Bool) -> [a] -> [a] #

O(n). filter, applied to a predicate and a list, returns the list of those elements that satisfy the predicate; i.e.,

filter p xs = [ x | x <- xs, p x]
>>> filter odd [1, 2, 3]
[1,3]

filterWithKey :: GCompare k => (forall v. k v -> f v -> Bool) -> DMap k f -> DMap k f Source #

O(n). Filter all keys/values that satisfy the predicate.

partitionWithKey :: GCompare k => (forall v. k v -> f v -> Bool) -> DMap k f -> (DMap k f, DMap k f) Source #

O(n). Partition the map according to a predicate. The first map contains all elements that satisfy the predicate, the second all elements that fail the predicate. See also split.

mapMaybe :: GCompare k => (forall v. f v -> Maybe (g v)) -> DMap k f -> DMap k g Source #

O(n). Map values and collect the Just results.

mapMaybeWithKey :: GCompare k => (forall v. k v -> f v -> Maybe (g v)) -> DMap k f -> DMap k g Source #

O(n). Map keys/values and collect the Just results.

mapEitherWithKey :: GCompare k => (forall v. k v -> f v -> Either (g v) (h v)) -> DMap k f -> (DMap k g, DMap k h) Source #

O(n). Map keys/values and separate the Left and Right results.

split :: forall k f v. GCompare k => k v -> DMap k f -> (DMap k f, DMap k f) Source #

O(log n). The expression (split k map) is a pair (map1,map2) where the keys in map1 are smaller than k and the keys in map2 larger than k. Any key equal to k is found in neither map1 nor map2.

splitLookup :: forall k f v. GCompare k => k v -> DMap k f -> (DMap k f, Maybe (f v), DMap k f) Source #

O(log n). The expression (splitLookup k map) splits a map just like split but also returns lookup k map.

Submap

isSubmapOf :: forall k f. (GCompare k, Has' Eq k f) => DMap k f -> DMap k f -> Bool Source #

O(n+m). This function is defined as (isSubmapOf = isSubmapOfBy eqTagged)).

isSubmapOfBy :: GCompare k => (forall v. k v -> k v -> f v -> g v -> Bool) -> DMap k f -> DMap k g -> Bool Source #

O(n+m). The expression (isSubmapOfBy f t1 t2) returns True if all keys in t1 are in tree t2, and when f returns True when applied to their respective keys and values.

isProperSubmapOf :: forall k f. (GCompare k, Has' Eq k f) => DMap k f -> DMap k f -> Bool Source #

O(n+m). Is this a proper submap? (ie. a submap but not equal). Defined as (isProperSubmapOf = isProperSubmapOfBy eqTagged).

isProperSubmapOfBy :: GCompare k => (forall v. k v -> k v -> f v -> g v -> Bool) -> DMap k f -> DMap k g -> Bool Source #

O(n+m). Is this a proper submap? (ie. a submap but not equal). The expression (isProperSubmapOfBy f m1 m2) returns True when m1 and m2 are not equal, all keys in m1 are in m2, and when f returns True when applied to their respective keys and values.

Indexed

lookupIndex :: forall k f v. GCompare k => k v -> DMap k f -> Maybe Int Source #

O(log n). Lookup the index of a key. The index is a number from 0 up to, but not including, the size of the map.

findIndex :: GCompare k => k v -> DMap k f -> Int Source #

O(log n). Return the index of a key. The index is a number from 0 up to, but not including, the size of the map. Calls error when the key is not a member of the map.

elemAt :: Int -> DMap k f -> DSum k f Source #

O(log n). Retrieve an element by index. Calls error when an invalid index is used.

updateAt :: (forall v. k v -> f v -> Maybe (f v)) -> Int -> DMap k f -> DMap k f Source #

O(log n). Update the element at index. Does nothing when an invalid index is used.

deleteAt :: Int -> DMap k f -> DMap k f Source #

O(log n). Delete the element at index. Defined as (deleteAt i map = updateAt (k x -> Nothing) i map).

Min/Max

findMin :: DMap k f -> DSum k f Source #

O(log n). The minimal key of the map. Calls error is the map is empty.

findMax :: DMap k f -> DSum k f Source #

O(log n). The maximal key of the map. Calls error is the map is empty.

lookupMin :: DMap k f -> Maybe (DSum k f) Source #

lookupMax :: DMap k f -> Maybe (DSum k f) Source #

deleteMin :: DMap k f -> DMap k f Source #

O(log n). Delete the minimal key. Returns an empty map if the map is empty.

deleteMax :: DMap k f -> DMap k f Source #

O(log n). Delete the maximal key. Returns an empty map if the map is empty.

deleteFindMin :: DMap k f -> (DSum k f, DMap k f) Source #

O(log n). Delete and find the minimal element.

deleteFindMin (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((3,"b"), fromList[(5,"a"), (10,"c")])
deleteFindMin                                            Error: can not return the minimal element of an empty map

deleteFindMax :: DMap k f -> (DSum k f, DMap k f) Source #

O(log n). Delete and find the maximal element.

deleteFindMax (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((10,"c"), fromList [(3,"b"), (5,"a")])
deleteFindMax empty                                      Error: can not return the maximal element of an empty map

updateMinWithKey :: (forall v. k v -> f v -> Maybe (f v)) -> DMap k f -> DMap k f Source #

O(log n). Update the value at the minimal key.

updateMaxWithKey :: (forall v. k v -> f v -> Maybe (f v)) -> DMap k f -> DMap k f Source #

O(log n). Update the value at the maximal key.

minViewWithKey :: forall k f. DMap k f -> Maybe (DSum k f, DMap k f) Source #

O(log n). Retrieves the minimal (key :=> value) entry of the map, and the map stripped of that element, or Nothing if passed an empty map.

maxViewWithKey :: forall k f. DMap k f -> Maybe (DSum k f, DMap k f) Source #

O(log n). Retrieves the maximal (key :=> value) entry of the map, and the map stripped of that element, or Nothing if passed an empty map.

Debugging

showTree :: (GShow k, Has' Show k f) => DMap k f -> String Source #

O(n). Show the tree that implements the map. The tree is shown in a compressed, hanging format. See showTreeWith.

showTreeWith :: (forall v. k v -> f v -> String) -> Bool -> Bool -> DMap k f -> String Source #

O(n). The expression (showTreeWith showelem hang wide map) shows the tree that implements the map. Elements are shown using the showElem function. If hang is True, a hanging tree is shown otherwise a rotated tree is shown. If wide is True, an extra wide version is shown.

valid :: GCompare k => DMap k f -> Bool Source #

O(n). Test if the internal map structure is valid.

Orphan instances

(GEq k2, Has' Eq k2 f) => Eq (DMap k2 f) Source # 
Instance details

Methods

(==) :: DMap k2 f -> DMap k2 f -> Bool #

(/=) :: DMap k2 f -> DMap k2 f -> Bool #

(GCompare k2, Has' Eq k2 f, Has' Ord k2 f) => Ord (DMap k2 f) Source # 
Instance details

Methods

compare :: DMap k2 f -> DMap k2 f -> Ordering #

(<) :: DMap k2 f -> DMap k2 f -> Bool #

(<=) :: DMap k2 f -> DMap k2 f -> Bool #

(>) :: DMap k2 f -> DMap k2 f -> Bool #

(>=) :: DMap k2 f -> DMap k2 f -> Bool #

max :: DMap k2 f -> DMap k2 f -> DMap k2 f #

min :: DMap k2 f -> DMap k2 f -> DMap k2 f #

(GCompare k2, GRead k2, Has' Read k2 f) => Read (DMap k2 f) Source # 
Instance details

Methods

readsPrec :: Int -> ReadS (DMap k2 f) #

readList :: ReadS [DMap k2 f] #

readPrec :: ReadPrec (DMap k2 f) #

readListPrec :: ReadPrec [DMap k2 f] #

(GShow k2, Has' Show k2 f) => Show (DMap k2 f) Source # 
Instance details

Methods

showsPrec :: Int -> DMap k2 f -> ShowS #

show :: DMap k2 f -> String #

showList :: [DMap k2 f] -> ShowS #

GCompare k2 => Semigroup (DMap k2 f) Source # 
Instance details

Methods

(<>) :: DMap k2 f -> DMap k2 f -> DMap k2 f #

sconcat :: NonEmpty (DMap k2 f) -> DMap k2 f #

stimes :: Integral b => b -> DMap k2 f -> DMap k2 f #

GCompare k2 => Monoid (DMap k2 f) Source # 
Instance details

Methods

mempty :: DMap k2 f #

mappend :: DMap k2 f -> DMap k2 f -> DMap k2 f #

mconcat :: [DMap k2 f] -> DMap k2 f #