HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduledependent-map-0.4.0.0Haskell98

Data.Dependent.Map

  • 1 type
  • 91 values
datadata DMap (k1 :: k -> Type) (f :: k -> Type) where
#

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 M.Map is to (->). Thus it could also be thought of as a partial (in the sense of "partial function") dependent product.

Instances6Eq, Ord, Read, Show, Semigroup, Monoid

Operators

2 declarations
value(!) :: GCompare k2 => DMap k2 f -> k2 v -> f v
#

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'

Query

6 declarations
valuesize :: DMap k2 f -> Int
#

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

valuelookup :: GCompare k2 => k2 v -> DMap k2 f -> Maybe (f v)
#

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.

valuefindWithDefault :: GCompare k2 => f v -> k2 v -> DMap k2 f -> f v
#

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

2 declarations
valueempty :: DMap k2 f
#

O(1). The empty map.

empty      == fromList []
size empty == 0
valuesingleton :: k2 v -> f v -> DMap k2 f
#

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

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

Insertion

valueinsert :: GCompare k2 => k2 v -> f v -> DMap k2 f -> DMap k2 f
#

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.

valueinsertWith
  1. :: GCompare k2
  2. => f v -> f v -> f v
  3. -> k2 v
  4. -> f v
  5. -> DMap k2 f
  6. -> DMap k2 f
#

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.

valueinsertWith'
  1. :: GCompare k2
  2. => f v -> f v -> f v
  3. -> k2 v
  4. -> f v
  5. -> DMap k2 f
  6. -> DMap k2 f
#

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

valueinsertWithKey
  1. :: GCompare k2
  2. => k2 v -> f v -> f v -> f v
  3. -> k2 v
  4. -> f v
  5. -> DMap k2 f
  6. -> DMap k2 f
#

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.

Delete/Update

valuedelete :: GCompare k2 => k2 v -> DMap k2 f -> DMap k2 f
#

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.

valueadjust :: GCompare k2 => (f v -> f v) -> k2 v -> DMap k2 f -> DMap k2 f
#

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.

valueadjustWithKey
  1. :: GCompare k2
  2. => k2 v -> f v -> f v
  3. -> k2 v
  4. -> DMap k2 f
  5. -> DMap k2 f
#

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.

valueupdate
  1. :: GCompare k2
  2. => f v -> Maybe (f v)
  3. -> k2 v
  4. -> DMap k2 f
  5. -> DMap k2 f
#

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.

valueupdateWithKey
  1. :: GCompare k2
  2. => k2 v -> f v -> Maybe (f v)
  3. -> k2 v
  4. -> DMap k2 f
  5. -> DMap k2 f
#

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.

Combine

0 declarations

Union

valueunion :: GCompare k2 => DMap k2 f -> DMap k2 f -> DMap k2 f
#

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).

Difference

valuedifference :: GCompare k2 => DMap k2 f -> DMap k2 g -> DMap k2 f
#

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

valuedifferenceWithKey
  1. :: GCompare k2
  2. => forall (v :: k1). k2 v -> f v -> g v -> Maybe (f v)
  3. -> DMap k2 f
  4. -> DMap k2 g
  5. -> DMap k2 f
#

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

valueintersection :: GCompare k2 => DMap k2 f -> DMap k2 f -> DMap k2 f
#

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).

Traversal

0 declarations

Map

valuemap :: (forall (v :: k1). f v -> g v) -> DMap k2 f -> DMap k2 g
#

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

valueffor :: DMap k2 f -> (forall (v :: k1). f v -> g v) -> DMap k2 g
#

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

valuemapWithKey
  1. :: forall (v :: k1). k2 v -> f v -> g v
  2. -> DMap k2 f
  3. -> DMap k2 g
#

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

valuemapAccumLWithKey
  1. :: forall (v :: k1). a -> k2 v -> f v -> (a, g v)
  2. -> a
  3. -> DMap k2 f
  4. -> (a, DMap k2 g)
#

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

valuemapAccumRWithKey
  1. :: forall (v :: k1). a -> k2 v -> f v -> (a, g v)
  2. -> a
  3. -> DMap k2 f
  4. -> (a, DMap k2 g)
#

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

valuemapKeysWith
  1. :: GCompare k2
  2. => forall (v :: k). k2 v -> f v -> f v -> f v
  3. -> forall (v :: k). k1 v -> k2 v
  4. -> DMap k1 f
  5. -> DMap k2 f
#

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.

valuemapKeysMonotonic
  1. :: forall (v :: k). k1 v -> k2 v
  2. -> DMap k1 f
  3. -> DMap k2 f
#

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

valuefoldWithKey
  1. :: forall (v :: k1). k2 v -> f v -> b -> b
  2. -> b
  3. -> DMap k2 f
  4. -> b
#

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.

valuefoldrWithKey
  1. :: forall (v :: k1). k2 v -> f v -> b -> b
  2. -> b
  3. -> DMap k2 f
  4. -> b
#

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

valuefoldlWithKey
  1. :: forall (v :: k1). b -> k2 v -> f v -> b
  2. -> b
  3. -> DMap k2 f
  4. -> b
#

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

Conversion

2 declarations
valuekeys :: DMap k2 f -> [Some k2]
#

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

keys (fromList [(5,"a"), (3,"b")]) == [3,5]
keys empty == []
valueassocs :: DMap k2 f -> [DSum k2 f]
#

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

Lists

valuetoList :: DMap k2 f -> [DSum k2 f]
#

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

valuefromList :: GCompare k2 => [DSum k2 f] -> DMap k2 f
#

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.

Ordered lists

valuefromAscList :: GEq k2 => [DSum k2 f] -> DMap k2 f
#

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

valuefromAscListWithKey
  1. :: GEq k2
  2. => forall (v :: k1). k2 v -> f v -> f v -> f v
  3. -> [DSum k2 f]
  4. -> DMap k2 f
#

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.

valuefromDistinctAscList :: [DSum k2 f] -> DMap k2 f
#

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

Filter

8 declarations
valuefilter :: (a -> Bool) -> [a] -> [a]
#

\mathcal{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]
Examples
Example1 expression
filter odd [1, 2, 3][1,3]
Example1 expression
filter (\l -> length l > 3) ["Hello", ", ", "World", "!"]["Hello","World"]
Example1 expression
filter (/= 3) [1, 2, 3, 4, 3, 2, 1][1,2,4,2,1]
valuepartitionWithKey
  1. :: GCompare k2
  2. => forall (v :: k1). k2 v -> f v -> Bool
  3. -> DMap k2 f
  4. -> (DMap k2 f, DMap k2 f)
#

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.

valuesplit :: GCompare k2 => k2 v -> DMap k2 f -> (DMap k2 f, DMap k2 f)
#

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.

Submap

4 declarations
valueisSubmapOfBy
  1. :: GCompare k2
  2. => forall (v :: k1). k2 v -> k2 v -> f v -> g v -> Bool
  3. -> DMap k2 f
  4. -> DMap k2 g
  5. -> Bool
#

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.

valueisProperSubmapOfBy
  1. :: GCompare k2
  2. => forall (v :: k1). k2 v -> k2 v -> f v -> g v -> Bool
  3. -> DMap k2 f
  4. -> DMap k2 g
  5. -> Bool
#

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

5 declarations
valuelookupIndex :: GCompare k2 => k2 v -> DMap k2 f -> Maybe Int
#

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.

valuefindIndex :: GCompare k2 => k2 v -> DMap k2 f -> Int
#

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.

valueelemAt :: Int -> DMap k2 f -> DSum k2 f
#

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

valueupdateAt
  1. :: forall (v :: k1). k2 v -> f v -> Maybe (f v)
  2. -> Int
  3. -> DMap k2 f
  4. -> DMap k2 f
#

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

Min/Max

12 declarations
valuefindMin :: DMap k2 f -> DSum k2 f
#

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

valuefindMax :: DMap k2 f -> DSum k2 f
#

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

valuedeleteMin :: DMap k2 f -> DMap k2 f
#

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

valuedeleteMax :: DMap k2 f -> DMap k2 f
#

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

valuedeleteFindMin :: DMap k2 f -> (DSum k2 f, DMap k2 f)
#

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
valuedeleteFindMax :: DMap k2 f -> (DSum k2 f, DMap k2 f)
#

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
valueminViewWithKey :: DMap k2 f -> Maybe (DSum k2 f, DMap k2 f)
#

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.

valuemaxViewWithKey :: DMap k2 f -> Maybe (DSum k2 f, DMap k2 f)
#

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

3 declarations
valueshowTreeWith
  1. :: forall (v :: k1). k2 v -> f v -> String
  2. -> Bool
  3. -> Bool
  4. -> DMap k2 f
  5. -> String
#

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.

Orphan instances

6 instances