Copyright | © 2022–2024 Jonathan Knowles |
---|---|
License | Apache-2.0 |
Safe Haskell | None |
Language | Haskell2010 |
Synopsis
- data MonoidMap k v
- empty :: MonoidMap k v
- fromList :: (Ord k, MonoidNull v) => [(k, v)] -> MonoidMap k v
- fromListWith :: (Ord k, MonoidNull v) => (v -> v -> v) -> [(k, v)] -> MonoidMap k v
- fromMap :: MonoidNull v => Map k v -> MonoidMap k v
- singleton :: (Ord k, MonoidNull v) => k -> v -> MonoidMap k v
- toList :: MonoidMap k v -> [(k, v)]
- toMap :: MonoidMap k v -> Map k v
- get :: (Ord k, Monoid v) => k -> MonoidMap k v -> v
- set :: (Ord k, MonoidNull v) => k -> v -> MonoidMap k v -> MonoidMap k v
- adjust :: (Ord k, MonoidNull v) => (v -> v) -> k -> MonoidMap k v -> MonoidMap k v
- nullify :: Ord k => k -> MonoidMap k v -> MonoidMap k v
- null :: MonoidMap k v -> Bool
- nullKey :: Ord k => k -> MonoidMap k v -> Bool
- nonNull :: MonoidMap k v -> Bool
- nonNullCount :: MonoidMap k v -> Int
- nonNullKey :: Ord k => k -> MonoidMap k v -> Bool
- nonNullKeys :: MonoidMap k v -> Set k
- take :: Int -> MonoidMap k v -> MonoidMap k v
- drop :: Int -> MonoidMap k v -> MonoidMap k v
- splitAt :: Int -> MonoidMap k a -> (MonoidMap k a, MonoidMap k a)
- filter :: (v -> Bool) -> MonoidMap k v -> MonoidMap k v
- filterKeys :: (k -> Bool) -> MonoidMap k v -> MonoidMap k v
- filterWithKey :: (k -> v -> Bool) -> MonoidMap k v -> MonoidMap k v
- partition :: (v -> Bool) -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v)
- partitionKeys :: (k -> Bool) -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v)
- partitionWithKey :: (k -> v -> Bool) -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v)
- map :: MonoidNull v2 => (v1 -> v2) -> MonoidMap k v1 -> MonoidMap k v2
- mapKeys :: (Ord k2, MonoidNull v) => (k1 -> k2) -> MonoidMap k1 v -> MonoidMap k2 v
- mapKeysWith :: (Ord k2, MonoidNull v) => (v -> v -> v) -> (k1 -> k2) -> MonoidMap k1 v -> MonoidMap k2 v
- append :: (Ord k, MonoidNull v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- minus :: (Ord k, MonoidNull v, Group v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- minusMaybe :: (Ord k, MonoidNull v, Reductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v)
- monus :: (Ord k, MonoidNull v, Monus v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- invert :: (MonoidNull v, Group v) => MonoidMap k v -> MonoidMap k v
- power :: (Integral i, MonoidNull v, Group v) => MonoidMap k v -> i -> MonoidMap k v
- isSubmapOf :: (Ord k, Monoid v, Reductive v) => MonoidMap k v -> MonoidMap k v -> Bool
- isSubmapOfBy :: (Ord k, Monoid v1, Monoid v2) => (v1 -> v2 -> Bool) -> MonoidMap k v1 -> MonoidMap k v2 -> Bool
- disjoint :: (Ord k, GCDMonoid v, MonoidNull v) => MonoidMap k v -> MonoidMap k v -> Bool
- disjointBy :: (Ord k, Monoid v1, Monoid v2) => (v1 -> v2 -> Bool) -> MonoidMap k v1 -> MonoidMap k v2 -> Bool
- intersection :: (Ord k, MonoidNull v, GCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- intersectionWith :: (Ord k, MonoidNull v3) => (v1 -> v2 -> v3) -> MonoidMap k v1 -> MonoidMap k v2 -> MonoidMap k v3
- intersectionWithA :: (Applicative f, Ord k, MonoidNull v3) => (v1 -> v2 -> f v3) -> MonoidMap k v1 -> MonoidMap k v2 -> f (MonoidMap k v3)
- union :: (Ord k, MonoidNull v, LCMMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- unionWith :: (Ord k, Monoid v1, Monoid v2, MonoidNull v3) => (v1 -> v2 -> v3) -> MonoidMap k v1 -> MonoidMap k v2 -> MonoidMap k v3
- unionWithA :: (Applicative f, Ord k, Monoid v1, Monoid v2, MonoidNull v3) => (v1 -> v2 -> f v3) -> MonoidMap k v1 -> MonoidMap k v2 -> f (MonoidMap k v3)
- isPrefixOf :: (Ord k, Monoid v, LeftReductive v) => MonoidMap k v -> MonoidMap k v -> Bool
- stripPrefix :: (Ord k, MonoidNull v, LeftReductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v)
- commonPrefix :: (Ord k, MonoidNull v, LeftGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripCommonPrefix :: (Ord k, MonoidNull v, LeftGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v)
- isSuffixOf :: (Ord k, Monoid v, RightReductive v) => MonoidMap k v -> MonoidMap k v -> Bool
- stripSuffix :: (Ord k, MonoidNull v, RightReductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v)
- commonSuffix :: (Ord k, MonoidNull v, RightGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripCommonSuffix :: (Ord k, MonoidNull v, RightGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v)
- overlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripPrefixOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripSuffixOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v)
Introduction
This module provides the MonoidMap
type, which:
- models a total function with
+
Data.MonoidMap Copyright © 2022–2024 Jonathan Knowles License Apache-2.0 Safe Haskell None Language Haskell2010 Synopsis
- data MonoidMap k v
- empty :: MonoidMap k v
- fromList :: (Ord k, MonoidNull v) => [(k, v)] -> MonoidMap k v
- fromListWith :: (Ord k, MonoidNull v) => (v -> v -> v) -> [(k, v)] -> MonoidMap k v
- fromMap :: MonoidNull v => Map k v -> MonoidMap k v
- singleton :: (Ord k, MonoidNull v) => k -> v -> MonoidMap k v
- toList :: MonoidMap k v -> [(k, v)]
- toMap :: MonoidMap k v -> Map k v
- get :: (Ord k, Monoid v) => k -> MonoidMap k v -> v
- set :: (Ord k, MonoidNull v) => k -> v -> MonoidMap k v -> MonoidMap k v
- adjust :: (Ord k, MonoidNull v) => (v -> v) -> k -> MonoidMap k v -> MonoidMap k v
- nullify :: Ord k => k -> MonoidMap k v -> MonoidMap k v
- null :: MonoidMap k v -> Bool
- nullKey :: Ord k => k -> MonoidMap k v -> Bool
- nonNull :: MonoidMap k v -> Bool
- nonNullCount :: MonoidMap k v -> Int
- nonNullKey :: Ord k => k -> MonoidMap k v -> Bool
- nonNullKeys :: MonoidMap k v -> Set k
- take :: Int -> MonoidMap k v -> MonoidMap k v
- drop :: Int -> MonoidMap k v -> MonoidMap k v
- splitAt :: Int -> MonoidMap k a -> (MonoidMap k a, MonoidMap k a)
- filter :: (v -> Bool) -> MonoidMap k v -> MonoidMap k v
- filterKeys :: (k -> Bool) -> MonoidMap k v -> MonoidMap k v
- filterWithKey :: (k -> v -> Bool) -> MonoidMap k v -> MonoidMap k v
- partition :: (v -> Bool) -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v)
- partitionKeys :: (k -> Bool) -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v)
- partitionWithKey :: (k -> v -> Bool) -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v)
- map :: MonoidNull v2 => (v1 -> v2) -> MonoidMap k v1 -> MonoidMap k v2
- mapKeys :: (Ord k2, MonoidNull v) => (k1 -> k2) -> MonoidMap k1 v -> MonoidMap k2 v
- mapKeysWith :: (Ord k2, MonoidNull v) => (v -> v -> v) -> (k1 -> k2) -> MonoidMap k1 v -> MonoidMap k2 v
- append :: (Ord k, MonoidNull v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- minus :: (Ord k, MonoidNull v, Group v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- minusMaybe :: (Ord k, MonoidNull v, Reductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v)
- monus :: (Ord k, MonoidNull v, Monus v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- invert :: (MonoidNull v, Group v) => MonoidMap k v -> MonoidMap k v
- power :: (Integral i, MonoidNull v, Group v) => MonoidMap k v -> i -> MonoidMap k v
- isSubmapOf :: (Ord k, Monoid v, Reductive v) => MonoidMap k v -> MonoidMap k v -> Bool
- isSubmapOfBy :: (Ord k, Monoid v1, Monoid v2) => (v1 -> v2 -> Bool) -> MonoidMap k v1 -> MonoidMap k v2 -> Bool
- disjoint :: (Ord k, GCDMonoid v, MonoidNull v) => MonoidMap k v -> MonoidMap k v -> Bool
- disjointBy :: (Ord k, Monoid v1, Monoid v2) => (v1 -> v2 -> Bool) -> MonoidMap k v1 -> MonoidMap k v2 -> Bool
- intersection :: (Ord k, MonoidNull v, GCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- intersectionWith :: (Ord k, MonoidNull v3) => (v1 -> v2 -> v3) -> MonoidMap k v1 -> MonoidMap k v2 -> MonoidMap k v3
- intersectionWithA :: (Applicative f, Ord k, MonoidNull v3) => (v1 -> v2 -> f v3) -> MonoidMap k v1 -> MonoidMap k v2 -> f (MonoidMap k v3)
- union :: (Ord k, MonoidNull v, LCMMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- unionWith :: (Ord k, Monoid v1, Monoid v2, MonoidNull v3) => (v1 -> v2 -> v3) -> MonoidMap k v1 -> MonoidMap k v2 -> MonoidMap k v3
- unionWithA :: (Applicative f, Ord k, Monoid v1, Monoid v2, MonoidNull v3) => (v1 -> v2 -> f v3) -> MonoidMap k v1 -> MonoidMap k v2 -> f (MonoidMap k v3)
- isPrefixOf :: (Ord k, Monoid v, LeftReductive v) => MonoidMap k v -> MonoidMap k v -> Bool
- stripPrefix :: (Ord k, MonoidNull v, LeftReductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v)
- commonPrefix :: (Ord k, MonoidNull v, LeftGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripCommonPrefix :: (Ord k, MonoidNull v, LeftGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v)
- isSuffixOf :: (Ord k, Monoid v, RightReductive v) => MonoidMap k v -> MonoidMap k v -> Bool
- stripSuffix :: (Ord k, MonoidNull v, RightReductive v) => MonoidMap k v -> MonoidMap k v -> Maybe (MonoidMap k v)
- commonSuffix :: (Ord k, MonoidNull v, RightGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripCommonSuffix :: (Ord k, MonoidNull v, RightGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v)
- overlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripPrefixOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripSuffixOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v
- stripOverlap :: (Ord k, MonoidNull v, OverlappingGCDMonoid v) => MonoidMap k v -> MonoidMap k v -> (MonoidMap k v, MonoidMap k v, MonoidMap k v)
Introduction
This module provides the
MonoidMap
type, which:- models a total function with
finite support
from keys to monoidal values, with a default value
of
mempty
. - encodes key-value mappings with a minimal encoding that
@@ -12,38 +12,38 @@
possible key of type
k
with a value of typev
:get
:: (Ord
k,Monoid
v) => k ->MonoidMap
k v -> vThe
empty
map associates every keyk
with a default value ofmempty
:∀ k.
get
kempty
==
mempty
-Comparison with standard
Map
typeThe
MonoidMap
type differs from the standardMap
type in how it relates - keys to values:Type Models a total function with finite support Map
k v
+Comparison with standard
Map
typeThe
MonoidMap
type differs from the standardMap
type in how it relates + keys to values:Type Models a total function with finite support Map
k v
from keys of type k
to values of type
.Maybe
vMonoidMap
k v
from keys of type k
to values of typev
.This difference can be illustrated by comparing the type signatures of - operations to query a key for its value, for both types:
Map
.lookup
:: k ->Map
k v ->Maybe
v + operations to query a key for its value, for both types:Map
.lookup
:: k ->Map
k v ->Maybe
vMonoidMap
.get
::Monoid
v => k ->MonoidMap
k v -> v -Whereas a standard
Map
has a default value ofNothing
, aMonoidMap
has - a default value ofmempty
:∀ k.
Map
.lookup
kMap
.empty
==
Nothing
+Whereas a standard
Map
has a default value ofNothing
, aMonoidMap
has + a default value ofmempty
:∀ k.
Map
.lookup
kMap
.empty
==
Nothing
∀ k.MonoidMap
.get
kMonoidMap
.empty
==
mempty
-In practice, the standard
Map
type usesMaybe
to indicate the presence +In practice, the standard
Map
type usesMaybe
to indicate the presence or absence of a value for a particular key. This representation is - necessary because theMap
type imposes no constraints on value types.However, monoidal types already have a natural way to represent null or + necessary because the
Map
type imposes no constraints on value types.However, monoidal types already have a natural way to represent null or empty values: the
mempty
constant, which represents the neutral or - identity element of aMonoid
.Consequently, using a standard
Map
with a monoidal value type gives rise + identity element of aMonoid
.Consequently, using a standard
Map
with a monoidal value type gives rise to two distinct representations for null or empty values:-
Map
.lookup
k m +Map
.lookup
k mInterpretation Nothing
-Map
m
has no entry +Map
m
has no entry for keyk
.Just
mempty
-Map
m
has an entry +Map
m
has an entry for keyk
, but the value is empty.In constrast, the
MonoidMap
type provides a single, canonical representation for null or empty values, according to the following conceptual mapping:-
Map
.lookup
k m +Map
.lookup
k m@@ -107,14 +107,14 @@ function.
Satisfies the following property for all possible keys
k
:get
k (fromListWith
f kvs)==
maybe
mempty
(foldl1
f) (nonEmpty
(snd
<$>
filter
((==
k) . fst) kvs)) -fromMap :: MonoidNull v => Map k v -> MonoidMap k v #
fromMap :: MonoidNull v => Map k v -> MonoidMap k v #
singleton :: (Ord k, MonoidNull v) => k -> v -> MonoidMap k v #
Deconstruction
toList :: MonoidMap k v -> [(k, v)] #
\(O(n)\). Converts a
MonoidMap
to a list of key-value pairs, where the keys are in ascending order.The result only includes entries with values that are not
null
.Satisfies the following round-trip property:
fromList
(toList
m)==
mThe resulting list is sorted in ascending key order:
sortOn
fst
(toList
m)==
toList
m -Lookup
Modification
nonNullCount :: MonoidMap k v -> Int #
\(O(1)\). Returns a count of all values in the map that are not
null
.Satisfies the following property:
nonNullCount
m==
size
(nonNullKeys
m)nonNullKey :: Ord k => k -> MonoidMap k v -> Bool #
nonNullKeys :: MonoidMap k v -> Set k #
\(O(n)\). Returns the set of keys associated with values that are not +
nonNullKeys :: MonoidMap k v -> Set k #
\(O(n)\). Returns the set of keys associated with values that are not
null
.Satisfies the following property:
k
`member`
(nonNullKeys
m)==
nonNullKey
k mSlicing
take :: Int -> MonoidMap k v -> MonoidMap k v #
\(O(\log n)\). Takes a slice from a map.
This function takes a given number of non-
null
entries from a map, producing a new map from the entries that were taken.Entries are taken in key order, beginning with the smallest keys.
Satifies the following property:
take
n==
fromList
.take
n .toList
@@ -247,7 +247,7 @@ (\r ->Just
(get
k r)==
get
k m1</>
get
k m2) (m1`minusMaybe`
m2)This function provides the definition of
(</>)
for theMonoidMap
- instance ofReductive
.Examples
monus :: (Ord k, MonoidNull v, Monus v) => MonoidMap k v -> MonoidMap k v -> MonoidMap k v #
Performs monus subtraction of the second map from the first.
Uses the
Monus
subtraction operator(<\>)
to subtract each value in the second map from its matching value in the first map.Satisfies the following property for all possible keys
k
:get
k (m1`monus`
m2)==
get
k m1<\>
get
k m2This function provides the definition of
(<\>)
for theMonoidMap
- instance ofMonus
.Examples
With
Set
Natural
values, this function performs set + instance ofMonus
.Examples
With
Set
Natural
values, this function performs set subtraction of matching values:f xs =
fromList
(fromList
<$>
xs)>>> m1 = f [("a", [0,1,2]), ("b", [0,1,2])] >>> m2 = f [("a", [ ]), ("b", [0,1,2])] @@ -394,7 +394,7 @@ >>> m3 =
fromList
[("a", 0), ("b", 1), ("c", 1), ("d", 0)]>>>
intersection
m1 m2==
m3True
-With
Set
Natural
values, this function computes the +With
Set
Natural
values, this function computes the set intersection of each pair of matching values:f xs =
fromList
(fromList
<$>
xs)>>> m1 = f [("a", [0,1,2]), ("b", [0,1,2 ]), ("c", [0,1,2 ])] >>> m2 = f [("a", [0,1,2]), ("b", [ 1,2,3]), ("c", [ 2,3,4])] @@ -445,7 +445,7 @@ >>> m3 =
fromList
[("a", 3), ("b", 2), ("c", 2), ("d", 3)]>>>
union
m1 m2==
m3True
-With
Set
Natural
values, this function computes the +With
Set
Natural
values, this function computes the set union of each pair of matching values:f xs =
fromList
(fromList
<$>
xs)>>> m1 = f [("a", [0,1,2]), ("b", [0,1,2 ]), ("c", [0,1,2 ])] >>> m2 = f [("a", [0,1,2]), ("b", [ 1,2,3]), ("c", [ 2,3,4])]