-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/


-- | Assorted concrete container types
gr
grThis package contains efficient general-purpose implementations of
grvarious immutable container types including sets, maps, sequences,
grtrees, and graphs.
gr
grFor a walkthrough of what this package provides with examples of
grcommon operations see the <a>containers introduction</a>.
gr
grThe declared cost of each operation is either worst-case or amortized,
grbut remains valid even if structures are shared.
@package containers
@version 0.5.11.0


module Utils.Containers.Internal.BitUtil
bitcount :: Int -> Word -> Int

-- | Return a word where only the highest bit is set.
highestBitMask :: Word -> Word
shiftLL :: Word -> Int -> Word
shiftRL :: Word -> Int -> Word
wordSize :: Int


-- | <h1>WARNING</h1>
gr
grThis module is considered <b>internal</b>.
gr
grThe Package Versioning Policy <b>does not apply</b>.
gr
grThis contents of this module may change <b>in any way whatsoever</b>
grand <b>without any warning</b> between minor versions of this package.
gr
grAuthors importing this module are expected to track development
grclosely.
gr
gr<h1>Description</h1>
gr
grAn extremely light-weight, fast, and limited representation of a
grstring of up to (2*WORDSIZE - 2) bits. In fact, there are two
grrepresentations, misleadingly named bit queue builder and bit queue.
grThe builder supports only <a>emptyQB</a>, creating an empty builder,
grand <a>snocQB</a>, enqueueing a bit. The bit queue builder is then
grturned into a bit queue using <a>buildQ</a>, after which bits can be
grremoved one by one using <a>unconsQ</a>. If the size limit is
grexceeded, further operations will silently produce nonsense.
module Utils.Containers.Internal.BitQueue
data BitQueue
data BitQueueB

-- | Create an empty bit queue builder. This is represented as a single
grguard bit in the most significant position.
emptyQB :: BitQueueB

-- | Enqueue a bit. This works by shifting the queue right one bit, then
grsetting the most significant bit as requested.
snocQB :: BitQueueB -> Bool -> BitQueueB

-- | Convert a bit queue builder to a bit queue. This shifts in a new guard
grbit on the left, and shifts right until the old guard bit falls off.
buildQ :: BitQueueB -> BitQueue

-- | Dequeue an element, or discover the queue is empty.
unconsQ :: BitQueue -> Maybe (Bool, BitQueue)

-- | Convert a bit queue to a list of bits by unconsing. This is used to
grtest that the queue functions properly.
toListQ :: BitQueue -> [Bool]
instance GHC.Show.Show Utils.Containers.Internal.BitQueue.BitQueue
instance GHC.Show.Show Utils.Containers.Internal.BitQueue.BitQueueB


-- | A strict pair
module Utils.Containers.Internal.StrictPair

-- | The same as a regular Haskell pair, but
gr
gr<pre>
gr(x :*: _|_) = (_|_ :*: y) = _|_
gr</pre>
data StrictPair a b
(:*:) :: !a -> !b -> StrictPair a b

-- | Convert a strict pair to a standard pair.
toPair :: StrictPair a b -> (a, b)


-- | <h1>WARNING</h1>
gr
grThis module is considered <b>internal</b>.
gr
grThe Package Versioning Policy <b>does not apply</b>.
gr
grThis contents of this module may change <b>in any way whatsoever</b>
grand <b>without any warning</b> between minor versions of this package.
gr
grAuthors importing this module are expected to track development
grclosely.
gr
gr<h1>Description</h1>
gr
grAn efficient implementation of sets.
gr
grThese modules are intended to be imported qualified, to avoid name
grclashes with Prelude functions, e.g.
gr
gr<pre>
grimport Data.Set (Set)
grimport qualified Data.Set as Set
gr</pre>
gr
grThe implementation of <a>Set</a> is based on <i>size balanced</i>
grbinary trees (or trees of <i>bounded balance</i>) as described by:
gr
gr<ul>
gr<li>Stephen Adams, "<i>Efficient sets: a balancing act</i>", Journal
grof Functional Programming 3(4):553-562, October 1993,
gr<a>http://www.swiss.ai.mit.edu/~adams/BB/</a>.</li>
gr<li>J. Nievergelt and E.M. Reingold, "<i>Binary search trees of
grbounded balance</i>", SIAM journal of computing 2(1), March 1973.</li>
gr</ul>
gr
grBounds for <a>union</a>, <a>intersection</a>, and <a>difference</a>
grare as given by
gr
gr<ul>
gr<li>Guy Blelloch, Daniel Ferizovic, and Yihan Sun, "<i>Just Join for
grParallel Ordered Sets</i>",
gr<a>https://arxiv.org/abs/1602.02120v3</a>.</li>
gr</ul>
gr
grNote that the implementation is <i>left-biased</i> -- the elements of
gra first argument are always preferred to the second, for example in
gr<a>union</a> or <a>insert</a>. Of course, left-biasing can only be
grobserved when equality is an equivalence relation instead of
grstructural equality.
gr
gr<i>Warning</i>: The size of the set must not exceed
gr<tt>maxBound::Int</tt>. Violation of this condition is not detected
grand if the size limit is exceeded, the behavior of the set is
grcompletely undefined.
module Data.Set.Internal

-- | A set of values <tt>a</tt>.
data Set a
Bin :: {-# UNPACK #-} !Size -> !a -> !(Set a) -> !(Set a) -> Set a
Tip :: Set a
type Size = Int

-- | <i>O(m*log(n/m+1)), m &lt;= n</i>. See <a>difference</a>.
(\\) :: Ord a => Set a -> Set a -> Set a
infixl 9 \\

-- | <i>O(1)</i>. Is this the empty set?
null :: Set a -> Bool

-- | <i>O(1)</i>. The number of elements in the set.
size :: Set a -> Int

-- | <i>O(log n)</i>. Is the element in the set?
member :: Ord a => a -> Set a -> Bool

-- | <i>O(log n)</i>. Is the element not in the set?
notMember :: Ord a => a -> Set a -> Bool

-- | <i>O(log n)</i>. Find largest element smaller than the given one.
gr
gr<pre>
grlookupLT 3 (fromList [3, 5]) == Nothing
grlookupLT 5 (fromList [3, 5]) == Just 3
gr</pre>
lookupLT :: Ord a => a -> Set a -> Maybe a

-- | <i>O(log n)</i>. Find smallest element greater than the given one.
gr
gr<pre>
grlookupGT 4 (fromList [3, 5]) == Just 5
grlookupGT 5 (fromList [3, 5]) == Nothing
gr</pre>
lookupGT :: Ord a => a -> Set a -> Maybe a

-- | <i>O(log n)</i>. Find largest element smaller or equal to the given
grone.
gr
gr<pre>
grlookupLE 2 (fromList [3, 5]) == Nothing
grlookupLE 4 (fromList [3, 5]) == Just 3
grlookupLE 5 (fromList [3, 5]) == Just 5
gr</pre>
lookupLE :: Ord a => a -> Set a -> Maybe a

-- | <i>O(log n)</i>. Find smallest element greater or equal to the given
grone.
gr
gr<pre>
grlookupGE 3 (fromList [3, 5]) == Just 3
grlookupGE 4 (fromList [3, 5]) == Just 5
grlookupGE 6 (fromList [3, 5]) == Nothing
gr</pre>
lookupGE :: Ord a => a -> Set a -> Maybe a

-- | <i>O(n+m)</i>. Is this a subset? <tt>(s1 <a>isSubsetOf</a> s2)</tt>
grtells whether <tt>s1</tt> is a subset of <tt>s2</tt>.
isSubsetOf :: Ord a => Set a -> Set a -> Bool

-- | <i>O(n+m)</i>. Is this a proper subset? (ie. a subset but not equal).
isProperSubsetOf :: Ord a => Set a -> Set a -> Bool

-- | <i>O(n+m)</i>. Check whether two sets are disjoint (i.e. their
grintersection is empty).
gr
gr<pre>
grdisjoint (fromList [2,4,6])   (fromList [1,3])     == True
grdisjoint (fromList [2,4,6,8]) (fromList [2,3,5,7]) == False
grdisjoint (fromList [1,2])     (fromList [1,2,3,4]) == False
grdisjoint (fromList [])        (fromList [])        == True
gr</pre>
disjoint :: Ord a => Set a -> Set a -> Bool

-- | <i>O(1)</i>. The empty set.
empty :: Set a

-- | <i>O(1)</i>. Create a singleton set.
singleton :: a -> Set a

-- | <i>O(log n)</i>. Insert an element in a set. If the set already
grcontains an element equal to the given value, it is replaced with the
grnew value.
insert :: Ord a => a -> Set a -> Set a

-- | <i>O(log n)</i>. Delete an element from a set.
delete :: Ord a => a -> Set a -> Set a

-- | Calculate the power set of a set: the set of all its subsets.
gr
gr<pre>
grt <a>member</a> powerSet s == t <a>isSubsetOf</a> s
gr</pre>
gr
grExample:
gr
gr<pre>
grpowerSet (fromList [1,2,3]) =
gr  fromList [[], [1], [2], [3], [1,2], [1,3], [2,3], [1,2,3]]
gr</pre>
powerSet :: Set a -> Set (Set a)

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. The union of two sets, preferring
grthe first set when equal elements are encountered.
union :: Ord a => Set a -> Set a -> Set a

-- | The union of a list of sets: (<tt><a>unions</a> == <a>foldl</a>
gr<a>union</a> <a>empty</a></tt>).
unions :: Ord a => [Set a] -> Set a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Difference of two sets.
difference :: Ord a => Set a -> Set a -> Set a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. The intersection of two sets.
grElements of the result come from the first set, so for example
gr
gr<pre>
grimport qualified Data.Set as S
grdata AB = A | B deriving Show
grinstance Ord AB where compare _ _ = EQ
grinstance Eq AB where _ == _ = True
grmain = print (S.singleton A `S.intersection` S.singleton B,
gr              S.singleton B `S.intersection` S.singleton A)
gr</pre>
gr
grprints <tt>(fromList [A],fromList [B])</tt>.
intersection :: Ord a => Set a -> Set a -> Set a

-- | Calculate the Cartesian product of two sets.
gr
gr<pre>
grcartesianProduct xs ys = fromList $ liftA2 (,) (toList xs) (toList ys)
gr</pre>
gr
grExample:
gr
gr<pre>
grcartesianProduct (fromList [1,2]) (fromList [<tt>a</tt>,<tt>b</tt>]) =
gr  fromList [(1,<tt>a</tt>), (1,<tt>b</tt>), (2,<tt>a</tt>), (2,<tt>b</tt>)]
gr</pre>
cartesianProduct :: Set a -> Set b -> Set (a, b)

-- | Calculate the disjoin union of two sets.
gr
gr<pre>
grdisjointUnion xs ys = map Left xs <a>union</a> map Right ys
gr</pre>
gr
grExample:
gr
gr<pre>
grdisjointUnion (fromList [1,2]) (fromList ["hi", "bye"]) =
gr  fromList [Left 1, Left 2, Right "hi", Right "bye"]
gr</pre>
disjointUnion :: Set a -> Set b -> Set (Either a b)

-- | <i>O(n)</i>. Filter all elements that satisfy the predicate.
filter :: (a -> Bool) -> Set a -> Set a

-- | <i>O(log n)</i>. Take while a predicate on the elements holds. The
gruser is responsible for ensuring that for all elements <tt>j</tt> and
gr<tt>k</tt> in the set, <tt>j &lt; k ==&gt; p j &gt;= p k</tt>. See
grnote at <a>spanAntitone</a>.
gr
gr<pre>
grtakeWhileAntitone p = <a>fromDistinctAscList</a> . <a>takeWhile</a> p . <a>toList</a>
grtakeWhileAntitone p = <a>filter</a> p
gr</pre>
takeWhileAntitone :: (a -> Bool) -> Set a -> Set a

-- | <i>O(log n)</i>. Drop while a predicate on the elements holds. The
gruser is responsible for ensuring that for all elements <tt>j</tt> and
gr<tt>k</tt> in the set, <tt>j &lt; k ==&gt; p j &gt;= p k</tt>. See
grnote at <a>spanAntitone</a>.
gr
gr<pre>
grdropWhileAntitone p = <a>fromDistinctAscList</a> . <a>dropWhile</a> p . <a>toList</a>
grdropWhileAntitone p = <a>filter</a> (not . p)
gr</pre>
dropWhileAntitone :: (a -> Bool) -> Set a -> Set a

-- | <i>O(log n)</i>. Divide a set at the point where a predicate on the
grelements stops holding. The user is responsible for ensuring that for
grall elements <tt>j</tt> and <tt>k</tt> in the set, <tt>j &lt; k ==&gt;
grp j &gt;= p k</tt>.
gr
gr<pre>
grspanAntitone p xs = (<a>takeWhileAntitone</a> p xs, <a>dropWhileAntitone</a> p xs)
grspanAntitone p xs = partition p xs
gr</pre>
gr
grNote: if <tt>p</tt> is not actually antitone, then
gr<tt>spanAntitone</tt> will split the set at some <i>unspecified</i>
grpoint where the predicate switches from holding to not holding (where
grthe predicate is seen to hold before the first element and to fail
grafter the last element).
spanAntitone :: (a -> Bool) -> Set a -> (Set a, Set a)

-- | <i>O(n)</i>. Partition the set into two sets, one with all elements
grthat satisfy the predicate and one with all elements that don't
grsatisfy the predicate. See also <a>split</a>.
partition :: (a -> Bool) -> Set a -> (Set a, Set a)

-- | <i>O(log n)</i>. The expression (<tt><a>split</a> x set</tt>) is a
grpair <tt>(set1,set2)</tt> where <tt>set1</tt> comprises the elements
grof <tt>set</tt> less than <tt>x</tt> and <tt>set2</tt> comprises the
grelements of <tt>set</tt> greater than <tt>x</tt>.
split :: Ord a => a -> Set a -> (Set a, Set a)

-- | <i>O(log n)</i>. Performs a <a>split</a> but also returns whether the
grpivot element was found in the original set.
splitMember :: Ord a => a -> Set a -> (Set a, Bool, Set a)

-- | <i>O(1)</i>. Decompose a set into pieces based on the structure of the
grunderlying tree. This function is useful for consuming a set in
grparallel.
gr
grNo guarantee is made as to the sizes of the pieces; an internal, but
grdeterministic process determines this. However, it is guaranteed that
grthe pieces returned will be in ascending order (all elements in the
grfirst subset less than all elements in the second, and so on).
gr
grExamples:
gr
gr<pre>
grsplitRoot (fromList [1..6]) ==
gr  [fromList [1,2,3],fromList [4],fromList [5,6]]
gr</pre>
gr
gr<pre>
grsplitRoot empty == []
gr</pre>
gr
grNote that the current implementation does not return more than three
grsubsets, but you should not depend on this behaviour because it can
grchange in the future without notice.
splitRoot :: Set a -> [Set a]

-- | <i>O(log n)</i>. Lookup the <i>index</i> of an element, which is its
grzero-based index in the sorted sequence of elements. The index is a
grnumber from <i>0</i> up to, but not including, the <a>size</a> of the
grset.
gr
gr<pre>
grisJust   (lookupIndex 2 (fromList [5,3])) == False
grfromJust (lookupIndex 3 (fromList [5,3])) == 0
grfromJust (lookupIndex 5 (fromList [5,3])) == 1
grisJust   (lookupIndex 6 (fromList [5,3])) == False
gr</pre>
lookupIndex :: Ord a => a -> Set a -> Maybe Int

-- | <i>O(log n)</i>. Return the <i>index</i> of an element, which is its
grzero-based index in the sorted sequence of elements. The index is a
grnumber from <i>0</i> up to, but not including, the <a>size</a> of the
grset. Calls <a>error</a> when the element is not a <a>member</a> of the
grset.
gr
gr<pre>
grfindIndex 2 (fromList [5,3])    Error: element is not in the set
grfindIndex 3 (fromList [5,3]) == 0
grfindIndex 5 (fromList [5,3]) == 1
grfindIndex 6 (fromList [5,3])    Error: element is not in the set
gr</pre>
findIndex :: Ord a => a -> Set a -> Int

-- | <i>O(log n)</i>. Retrieve an element by its <i>index</i>, i.e. by its
grzero-based index in the sorted sequence of elements. If the
gr<i>index</i> is out of range (less than zero, greater or equal to
gr<a>size</a> of the set), <a>error</a> is called.
gr
gr<pre>
grelemAt 0 (fromList [5,3]) == 3
grelemAt 1 (fromList [5,3]) == 5
grelemAt 2 (fromList [5,3])    Error: index out of range
gr</pre>
elemAt :: Int -> Set a -> a

-- | <i>O(log n)</i>. Delete the element at <i>index</i>, i.e. by its
grzero-based index in the sorted sequence of elements. If the
gr<i>index</i> is out of range (less than zero, greater or equal to
gr<a>size</a> of the set), <a>error</a> is called.
gr
gr<pre>
grdeleteAt 0    (fromList [5,3]) == singleton 5
grdeleteAt 1    (fromList [5,3]) == singleton 3
grdeleteAt 2    (fromList [5,3])    Error: index out of range
grdeleteAt (-1) (fromList [5,3])    Error: index out of range
gr</pre>
deleteAt :: Int -> Set a -> Set a

-- | Take a given number of elements in order, beginning with the smallest
grones.
gr
gr<pre>
grtake n = <a>fromDistinctAscList</a> . <a>take</a> n . <a>toAscList</a>
gr</pre>
take :: Int -> Set a -> Set a

-- | Drop a given number of elements in order, beginning with the smallest
grones.
gr
gr<pre>
grdrop n = <a>fromDistinctAscList</a> . <a>drop</a> n . <a>toAscList</a>
gr</pre>
drop :: Int -> Set a -> Set a

-- | <i>O(log n)</i>. Split a set at a particular index.
gr
gr<pre>
grsplitAt !n !xs = (<a>take</a> n xs, <a>drop</a> n xs)
gr</pre>
splitAt :: Int -> Set a -> (Set a, Set a)

-- | <i>O(n*log n)</i>. <tt><a>map</a> f s</tt> is the set obtained by
grapplying <tt>f</tt> to each element of <tt>s</tt>.
gr
grIt's worth noting that the size of the result may be smaller if, for
grsome <tt>(x,y)</tt>, <tt>x /= y &amp;&amp; f x == f y</tt>
map :: Ord b => (a -> b) -> Set a -> Set b

-- | <i>O(n)</i>. The
gr
gr<tt><a>mapMonotonic</a> f s == <a>map</a> f s</tt>, but works only
grwhen <tt>f</tt> is strictly increasing. <i>The precondition is not
grchecked.</i> Semi-formally, we have:
gr
gr<pre>
grand [x &lt; y ==&gt; f x &lt; f y | x &lt;- ls, y &lt;- ls]
gr                    ==&gt; mapMonotonic f s == map f s
gr    where ls = toList s
gr</pre>
mapMonotonic :: (a -> b) -> Set a -> Set b

-- | <i>O(n)</i>. Fold the elements in the set using the given
grright-associative binary operator, such that <tt><a>foldr</a> f z ==
gr<a>foldr</a> f z . <a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grtoAscList set = foldr (:) [] set
gr</pre>
foldr :: (a -> b -> b) -> b -> Set a -> b

-- | <i>O(n)</i>. Fold the elements in the set using the given
grleft-associative binary operator, such that <tt><a>foldl</a> f z ==
gr<a>foldl</a> f z . <a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grtoDescList set = foldl (flip (:)) [] set
gr</pre>
foldl :: (a -> b -> a) -> a -> Set b -> a

-- | <i>O(n)</i>. A strict version of <a>foldr</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldr' :: (a -> b -> b) -> b -> Set a -> b

-- | <i>O(n)</i>. A strict version of <a>foldl</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldl' :: (a -> b -> a) -> a -> Set b -> a

-- | <i>O(n)</i>. Fold the elements in the set using the given
grright-associative binary operator. This function is an equivalent of
gr<a>foldr</a> and is present for compatibility only.
gr
gr<i>Please note that fold will be deprecated in the future and
grremoved.</i>
fold :: (a -> b -> b) -> b -> Set a -> b

-- | <i>O(log n)</i>. The minimal element of a set.
lookupMin :: Set a -> Maybe a

-- | <i>O(log n)</i>. The maximal element of a set.
lookupMax :: Set a -> Maybe a

-- | <i>O(log n)</i>. The minimal element of a set.
findMin :: Set a -> a

-- | <i>O(log n)</i>. The maximal element of a set.
findMax :: Set a -> a

-- | <i>O(log n)</i>. Delete the minimal element. Returns an empty set if
grthe set is empty.
deleteMin :: Set a -> Set a

-- | <i>O(log n)</i>. Delete the maximal element. Returns an empty set if
grthe set is empty.
deleteMax :: Set a -> Set a

-- | <i>O(log n)</i>. Delete and find the minimal element.
gr
gr<pre>
grdeleteFindMin set = (findMin set, deleteMin set)
gr</pre>
deleteFindMin :: Set a -> (a, Set a)

-- | <i>O(log n)</i>. Delete and find the maximal element.
gr
gr<pre>
grdeleteFindMax set = (findMax set, deleteMax set)
gr</pre>
deleteFindMax :: Set a -> (a, Set a)

-- | <i>O(log n)</i>. Retrieves the maximal key of the set, and the set
grstripped of that element, or <a>Nothing</a> if passed an empty set.
maxView :: Set a -> Maybe (a, Set a)

-- | <i>O(log n)</i>. Retrieves the minimal key of the set, and the set
grstripped of that element, or <a>Nothing</a> if passed an empty set.
minView :: Set a -> Maybe (a, Set a)

-- | <i>O(n)</i>. An alias of <a>toAscList</a>. The elements of a set in
grascending order. Subject to list fusion.
elems :: Set a -> [a]

-- | <i>O(n)</i>. Convert the set to a list of elements. Subject to list
grfusion.
toList :: Set a -> [a]

-- | <i>O(n*log n)</i>. Create a set from a list of elements.
gr
grIf the elements are ordered, a linear-time implementation is used,
grwith the performance equal to <a>fromDistinctAscList</a>.
fromList :: Ord a => [a] -> Set a

-- | <i>O(n)</i>. Convert the set to an ascending list of elements. Subject
grto list fusion.
toAscList :: Set a -> [a]

-- | <i>O(n)</i>. Convert the set to a descending list of elements. Subject
grto list fusion.
toDescList :: Set a -> [a]

-- | <i>O(n)</i>. Build a set from an ascending list in linear time. <i>The
grprecondition (input list is ascending) is not checked.</i>
fromAscList :: Eq a => [a] -> Set a

-- | <i>O(n)</i>. Build a set from an ascending list of distinct elements
grin linear time. <i>The precondition (input list is strictly ascending)
gris not checked.</i>
fromDistinctAscList :: [a] -> Set a

-- | <i>O(n)</i>. Build a set from a descending list in linear time. <i>The
grprecondition (input list is descending) is not checked.</i>
fromDescList :: Eq a => [a] -> Set a

-- | <i>O(n)</i>. Build a set from a descending list of distinct elements
grin linear time. <i>The precondition (input list is strictly
grdescending) is not checked.</i>
fromDistinctDescList :: [a] -> Set a

-- | <i>O(n)</i>. Show the tree that implements the set. The tree is shown
grin a compressed, hanging format.
showTree :: Show a => Set a -> String

-- | <i>O(n)</i>. The expression (<tt>showTreeWith hang wide map</tt>)
grshows the tree that implements the set. If <tt>hang</tt> is
gr<tt>True</tt>, a <i>hanging</i> tree is shown otherwise a rotated tree
gris shown. If <tt>wide</tt> is <a>True</a>, an extra wide version is
grshown.
gr
gr<pre>
grSet&gt; putStrLn $ showTreeWith True False $ fromDistinctAscList [1..5]
gr4
gr+--2
gr|  +--1
gr|  +--3
gr+--5
gr
grSet&gt; putStrLn $ showTreeWith True True $ fromDistinctAscList [1..5]
gr4
gr|
gr+--2
gr|  |
gr|  +--1
gr|  |
gr|  +--3
gr|
gr+--5
gr
grSet&gt; putStrLn $ showTreeWith False True $ fromDistinctAscList [1..5]
gr+--5
gr|
gr4
gr|
gr|  +--3
gr|  |
gr+--2
gr   |
gr   +--1
gr</pre>
showTreeWith :: Show a => Bool -> Bool -> Set a -> String

-- | <i>O(n)</i>. Test if the internal set structure is valid.
valid :: Ord a => Set a -> Bool
bin :: a -> Set a -> Set a -> Set a
balanced :: Set a -> Bool
link :: a -> Set a -> Set a -> Set a
merge :: Set a -> Set a -> Set a
instance GHC.Base.Semigroup (Data.Set.Internal.MergeSet a)
instance GHC.Base.Monoid (Data.Set.Internal.MergeSet a)
instance GHC.Classes.Ord a => GHC.Base.Monoid (Data.Set.Internal.Set a)
instance GHC.Classes.Ord a => GHC.Base.Semigroup (Data.Set.Internal.Set a)
instance Data.Foldable.Foldable Data.Set.Internal.Set
instance (Data.Data.Data a, GHC.Classes.Ord a) => Data.Data.Data (Data.Set.Internal.Set a)
instance GHC.Classes.Ord a => GHC.Exts.IsList (Data.Set.Internal.Set a)
instance GHC.Classes.Eq a => GHC.Classes.Eq (Data.Set.Internal.Set a)
instance GHC.Classes.Ord a => GHC.Classes.Ord (Data.Set.Internal.Set a)
instance GHC.Show.Show a => GHC.Show.Show (Data.Set.Internal.Set a)
instance Data.Functor.Classes.Eq1 Data.Set.Internal.Set
instance Data.Functor.Classes.Ord1 Data.Set.Internal.Set
instance Data.Functor.Classes.Show1 Data.Set.Internal.Set
instance (GHC.Read.Read a, GHC.Classes.Ord a) => GHC.Read.Read (Data.Set.Internal.Set a)
instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Data.Set.Internal.Set a)


-- | <h1>Finite Sets</h1>
gr
grThe <tt><a>Set</a> e</tt> type represents a set of elements of type
gr<tt>e</tt>. Most operations require that <tt>e</tt> be an instance of
grthe <a>Ord</a> class. A <a>Set</a> is strict in its elements.
gr
grFor a walkthrough of the most commonly used functions see the <a>sets
grintroduction</a>.
gr
grNote that the implementation is generally <i>left-biased</i>.
grFunctions that take two sets as arguments and combine them, such as
gr<a>union</a> and <a>intersection</a>, prefer the entries in the first
grargument to those in the second. Of course, this bias can only be
grobserved when equality is an equivalence relation instead of
grstructural equality.
gr
grThese modules are intended to be imported qualified, to avoid name
grclashes with Prelude functions, e.g.
gr
gr<pre>
grimport Data.Set (Set)
grimport qualified Data.Set as Set
gr</pre>
gr
gr<h2>Warning</h2>
gr
grThe size of the set must not exceed <tt>maxBound::Int</tt>. Violation
grof this condition is not detected and if the size limit is exceeded,
grits behaviour is undefined.
gr
gr<h2>Implementation</h2>
gr
grThe implementation of <a>Set</a> is based on <i>size balanced</i>
grbinary trees (or trees of <i>bounded balance</i>) as described by:
gr
gr<ul>
gr<li>Stephen Adams, "<i>Efficient sets: a balancing act</i>", Journal
grof Functional Programming 3(4):553-562, October 1993,
gr<a>http://www.swiss.ai.mit.edu/~adams/BB/</a>.</li>
gr<li>J. Nievergelt and E.M. Reingold, "<i>Binary search trees of
grbounded balance</i>", SIAM journal of computing 2(1), March 1973.</li>
gr</ul>
gr
grBounds for <a>union</a>, <a>intersection</a>, and <a>difference</a>
grare as given by
gr
gr<ul>
gr<li>Guy Blelloch, Daniel Ferizovic, and Yihan Sun, "<i>Just Join for
grParallel Ordered Sets</i>",
gr<a>https://arxiv.org/abs/1602.02120v3</a>.</li>
gr</ul>
module Data.Set

-- | A set of values <tt>a</tt>.
data Set a

-- | <i>O(m*log(n/m+1)), m &lt;= n</i>. See <a>difference</a>.
(\\) :: Ord a => Set a -> Set a -> Set a
infixl 9 \\

-- | <i>O(1)</i>. Is this the empty set?
null :: Set a -> Bool

-- | <i>O(1)</i>. The number of elements in the set.
size :: Set a -> Int

-- | <i>O(log n)</i>. Is the element in the set?
member :: Ord a => a -> Set a -> Bool

-- | <i>O(log n)</i>. Is the element not in the set?
notMember :: Ord a => a -> Set a -> Bool

-- | <i>O(log n)</i>. Find largest element smaller than the given one.
gr
gr<pre>
grlookupLT 3 (fromList [3, 5]) == Nothing
grlookupLT 5 (fromList [3, 5]) == Just 3
gr</pre>
lookupLT :: Ord a => a -> Set a -> Maybe a

-- | <i>O(log n)</i>. Find smallest element greater than the given one.
gr
gr<pre>
grlookupGT 4 (fromList [3, 5]) == Just 5
grlookupGT 5 (fromList [3, 5]) == Nothing
gr</pre>
lookupGT :: Ord a => a -> Set a -> Maybe a

-- | <i>O(log n)</i>. Find largest element smaller or equal to the given
grone.
gr
gr<pre>
grlookupLE 2 (fromList [3, 5]) == Nothing
grlookupLE 4 (fromList [3, 5]) == Just 3
grlookupLE 5 (fromList [3, 5]) == Just 5
gr</pre>
lookupLE :: Ord a => a -> Set a -> Maybe a

-- | <i>O(log n)</i>. Find smallest element greater or equal to the given
grone.
gr
gr<pre>
grlookupGE 3 (fromList [3, 5]) == Just 3
grlookupGE 4 (fromList [3, 5]) == Just 5
grlookupGE 6 (fromList [3, 5]) == Nothing
gr</pre>
lookupGE :: Ord a => a -> Set a -> Maybe a

-- | <i>O(n+m)</i>. Is this a subset? <tt>(s1 <a>isSubsetOf</a> s2)</tt>
grtells whether <tt>s1</tt> is a subset of <tt>s2</tt>.
isSubsetOf :: Ord a => Set a -> Set a -> Bool

-- | <i>O(n+m)</i>. Is this a proper subset? (ie. a subset but not equal).
isProperSubsetOf :: Ord a => Set a -> Set a -> Bool

-- | <i>O(n+m)</i>. Check whether two sets are disjoint (i.e. their
grintersection is empty).
gr
gr<pre>
grdisjoint (fromList [2,4,6])   (fromList [1,3])     == True
grdisjoint (fromList [2,4,6,8]) (fromList [2,3,5,7]) == False
grdisjoint (fromList [1,2])     (fromList [1,2,3,4]) == False
grdisjoint (fromList [])        (fromList [])        == True
gr</pre>
disjoint :: Ord a => Set a -> Set a -> Bool

-- | <i>O(1)</i>. The empty set.
empty :: Set a

-- | <i>O(1)</i>. Create a singleton set.
singleton :: a -> Set a

-- | <i>O(log n)</i>. Insert an element in a set. If the set already
grcontains an element equal to the given value, it is replaced with the
grnew value.
insert :: Ord a => a -> Set a -> Set a

-- | <i>O(log n)</i>. Delete an element from a set.
delete :: Ord a => a -> Set a -> Set a

-- | Calculate the power set of a set: the set of all its subsets.
gr
gr<pre>
grt <a>member</a> powerSet s == t <a>isSubsetOf</a> s
gr</pre>
gr
grExample:
gr
gr<pre>
grpowerSet (fromList [1,2,3]) =
gr  fromList [[], [1], [2], [3], [1,2], [1,3], [2,3], [1,2,3]]
gr</pre>
powerSet :: Set a -> Set (Set a)

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. The union of two sets, preferring
grthe first set when equal elements are encountered.
union :: Ord a => Set a -> Set a -> Set a

-- | The union of a list of sets: (<tt><a>unions</a> == <a>foldl</a>
gr<a>union</a> <a>empty</a></tt>).
unions :: Ord a => [Set a] -> Set a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Difference of two sets.
difference :: Ord a => Set a -> Set a -> Set a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. The intersection of two sets.
grElements of the result come from the first set, so for example
gr
gr<pre>
grimport qualified Data.Set as S
grdata AB = A | B deriving Show
grinstance Ord AB where compare _ _ = EQ
grinstance Eq AB where _ == _ = True
grmain = print (S.singleton A `S.intersection` S.singleton B,
gr              S.singleton B `S.intersection` S.singleton A)
gr</pre>
gr
grprints <tt>(fromList [A],fromList [B])</tt>.
intersection :: Ord a => Set a -> Set a -> Set a

-- | Calculate the Cartesian product of two sets.
gr
gr<pre>
grcartesianProduct xs ys = fromList $ liftA2 (,) (toList xs) (toList ys)
gr</pre>
gr
grExample:
gr
gr<pre>
grcartesianProduct (fromList [1,2]) (fromList [<tt>a</tt>,<tt>b</tt>]) =
gr  fromList [(1,<tt>a</tt>), (1,<tt>b</tt>), (2,<tt>a</tt>), (2,<tt>b</tt>)]
gr</pre>
cartesianProduct :: Set a -> Set b -> Set (a, b)

-- | Calculate the disjoin union of two sets.
gr
gr<pre>
grdisjointUnion xs ys = map Left xs <a>union</a> map Right ys
gr</pre>
gr
grExample:
gr
gr<pre>
grdisjointUnion (fromList [1,2]) (fromList ["hi", "bye"]) =
gr  fromList [Left 1, Left 2, Right "hi", Right "bye"]
gr</pre>
disjointUnion :: Set a -> Set b -> Set (Either a b)

-- | <i>O(n)</i>. Filter all elements that satisfy the predicate.
filter :: (a -> Bool) -> Set a -> Set a

-- | <i>O(log n)</i>. Take while a predicate on the elements holds. The
gruser is responsible for ensuring that for all elements <tt>j</tt> and
gr<tt>k</tt> in the set, <tt>j &lt; k ==&gt; p j &gt;= p k</tt>. See
grnote at <a>spanAntitone</a>.
gr
gr<pre>
grtakeWhileAntitone p = <a>fromDistinctAscList</a> . <a>takeWhile</a> p . <a>toList</a>
grtakeWhileAntitone p = <a>filter</a> p
gr</pre>
takeWhileAntitone :: (a -> Bool) -> Set a -> Set a

-- | <i>O(log n)</i>. Drop while a predicate on the elements holds. The
gruser is responsible for ensuring that for all elements <tt>j</tt> and
gr<tt>k</tt> in the set, <tt>j &lt; k ==&gt; p j &gt;= p k</tt>. See
grnote at <a>spanAntitone</a>.
gr
gr<pre>
grdropWhileAntitone p = <a>fromDistinctAscList</a> . <a>dropWhile</a> p . <a>toList</a>
grdropWhileAntitone p = <a>filter</a> (not . p)
gr</pre>
dropWhileAntitone :: (a -> Bool) -> Set a -> Set a

-- | <i>O(log n)</i>. Divide a set at the point where a predicate on the
grelements stops holding. The user is responsible for ensuring that for
grall elements <tt>j</tt> and <tt>k</tt> in the set, <tt>j &lt; k ==&gt;
grp j &gt;= p k</tt>.
gr
gr<pre>
grspanAntitone p xs = (<a>takeWhileAntitone</a> p xs, <a>dropWhileAntitone</a> p xs)
grspanAntitone p xs = partition p xs
gr</pre>
gr
grNote: if <tt>p</tt> is not actually antitone, then
gr<tt>spanAntitone</tt> will split the set at some <i>unspecified</i>
grpoint where the predicate switches from holding to not holding (where
grthe predicate is seen to hold before the first element and to fail
grafter the last element).
spanAntitone :: (a -> Bool) -> Set a -> (Set a, Set a)

-- | <i>O(n)</i>. Partition the set into two sets, one with all elements
grthat satisfy the predicate and one with all elements that don't
grsatisfy the predicate. See also <a>split</a>.
partition :: (a -> Bool) -> Set a -> (Set a, Set a)

-- | <i>O(log n)</i>. The expression (<tt><a>split</a> x set</tt>) is a
grpair <tt>(set1,set2)</tt> where <tt>set1</tt> comprises the elements
grof <tt>set</tt> less than <tt>x</tt> and <tt>set2</tt> comprises the
grelements of <tt>set</tt> greater than <tt>x</tt>.
split :: Ord a => a -> Set a -> (Set a, Set a)

-- | <i>O(log n)</i>. Performs a <a>split</a> but also returns whether the
grpivot element was found in the original set.
splitMember :: Ord a => a -> Set a -> (Set a, Bool, Set a)

-- | <i>O(1)</i>. Decompose a set into pieces based on the structure of the
grunderlying tree. This function is useful for consuming a set in
grparallel.
gr
grNo guarantee is made as to the sizes of the pieces; an internal, but
grdeterministic process determines this. However, it is guaranteed that
grthe pieces returned will be in ascending order (all elements in the
grfirst subset less than all elements in the second, and so on).
gr
grExamples:
gr
gr<pre>
grsplitRoot (fromList [1..6]) ==
gr  [fromList [1,2,3],fromList [4],fromList [5,6]]
gr</pre>
gr
gr<pre>
grsplitRoot empty == []
gr</pre>
gr
grNote that the current implementation does not return more than three
grsubsets, but you should not depend on this behaviour because it can
grchange in the future without notice.
splitRoot :: Set a -> [Set a]

-- | <i>O(log n)</i>. Lookup the <i>index</i> of an element, which is its
grzero-based index in the sorted sequence of elements. The index is a
grnumber from <i>0</i> up to, but not including, the <a>size</a> of the
grset.
gr
gr<pre>
grisJust   (lookupIndex 2 (fromList [5,3])) == False
grfromJust (lookupIndex 3 (fromList [5,3])) == 0
grfromJust (lookupIndex 5 (fromList [5,3])) == 1
grisJust   (lookupIndex 6 (fromList [5,3])) == False
gr</pre>
lookupIndex :: Ord a => a -> Set a -> Maybe Int

-- | <i>O(log n)</i>. Return the <i>index</i> of an element, which is its
grzero-based index in the sorted sequence of elements. The index is a
grnumber from <i>0</i> up to, but not including, the <a>size</a> of the
grset. Calls <a>error</a> when the element is not a <a>member</a> of the
grset.
gr
gr<pre>
grfindIndex 2 (fromList [5,3])    Error: element is not in the set
grfindIndex 3 (fromList [5,3]) == 0
grfindIndex 5 (fromList [5,3]) == 1
grfindIndex 6 (fromList [5,3])    Error: element is not in the set
gr</pre>
findIndex :: Ord a => a -> Set a -> Int

-- | <i>O(log n)</i>. Retrieve an element by its <i>index</i>, i.e. by its
grzero-based index in the sorted sequence of elements. If the
gr<i>index</i> is out of range (less than zero, greater or equal to
gr<a>size</a> of the set), <a>error</a> is called.
gr
gr<pre>
grelemAt 0 (fromList [5,3]) == 3
grelemAt 1 (fromList [5,3]) == 5
grelemAt 2 (fromList [5,3])    Error: index out of range
gr</pre>
elemAt :: Int -> Set a -> a

-- | <i>O(log n)</i>. Delete the element at <i>index</i>, i.e. by its
grzero-based index in the sorted sequence of elements. If the
gr<i>index</i> is out of range (less than zero, greater or equal to
gr<a>size</a> of the set), <a>error</a> is called.
gr
gr<pre>
grdeleteAt 0    (fromList [5,3]) == singleton 5
grdeleteAt 1    (fromList [5,3]) == singleton 3
grdeleteAt 2    (fromList [5,3])    Error: index out of range
grdeleteAt (-1) (fromList [5,3])    Error: index out of range
gr</pre>
deleteAt :: Int -> Set a -> Set a

-- | Take a given number of elements in order, beginning with the smallest
grones.
gr
gr<pre>
grtake n = <a>fromDistinctAscList</a> . <a>take</a> n . <a>toAscList</a>
gr</pre>
take :: Int -> Set a -> Set a

-- | Drop a given number of elements in order, beginning with the smallest
grones.
gr
gr<pre>
grdrop n = <a>fromDistinctAscList</a> . <a>drop</a> n . <a>toAscList</a>
gr</pre>
drop :: Int -> Set a -> Set a

-- | <i>O(log n)</i>. Split a set at a particular index.
gr
gr<pre>
grsplitAt !n !xs = (<a>take</a> n xs, <a>drop</a> n xs)
gr</pre>
splitAt :: Int -> Set a -> (Set a, Set a)

-- | <i>O(n*log n)</i>. <tt><a>map</a> f s</tt> is the set obtained by
grapplying <tt>f</tt> to each element of <tt>s</tt>.
gr
grIt's worth noting that the size of the result may be smaller if, for
grsome <tt>(x,y)</tt>, <tt>x /= y &amp;&amp; f x == f y</tt>
map :: Ord b => (a -> b) -> Set a -> Set b

-- | <i>O(n)</i>. The
gr
gr<tt><a>mapMonotonic</a> f s == <a>map</a> f s</tt>, but works only
grwhen <tt>f</tt> is strictly increasing. <i>The precondition is not
grchecked.</i> Semi-formally, we have:
gr
gr<pre>
grand [x &lt; y ==&gt; f x &lt; f y | x &lt;- ls, y &lt;- ls]
gr                    ==&gt; mapMonotonic f s == map f s
gr    where ls = toList s
gr</pre>
mapMonotonic :: (a -> b) -> Set a -> Set b

-- | <i>O(n)</i>. Fold the elements in the set using the given
grright-associative binary operator, such that <tt><a>foldr</a> f z ==
gr<a>foldr</a> f z . <a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grtoAscList set = foldr (:) [] set
gr</pre>
foldr :: (a -> b -> b) -> b -> Set a -> b

-- | <i>O(n)</i>. Fold the elements in the set using the given
grleft-associative binary operator, such that <tt><a>foldl</a> f z ==
gr<a>foldl</a> f z . <a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grtoDescList set = foldl (flip (:)) [] set
gr</pre>
foldl :: (a -> b -> a) -> a -> Set b -> a

-- | <i>O(n)</i>. A strict version of <a>foldr</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldr' :: (a -> b -> b) -> b -> Set a -> b

-- | <i>O(n)</i>. A strict version of <a>foldl</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldl' :: (a -> b -> a) -> a -> Set b -> a

-- | <i>O(n)</i>. Fold the elements in the set using the given
grright-associative binary operator. This function is an equivalent of
gr<a>foldr</a> and is present for compatibility only.
gr
gr<i>Please note that fold will be deprecated in the future and
grremoved.</i>
fold :: (a -> b -> b) -> b -> Set a -> b

-- | <i>O(log n)</i>. The minimal element of a set.
lookupMin :: Set a -> Maybe a

-- | <i>O(log n)</i>. The maximal element of a set.
lookupMax :: Set a -> Maybe a

-- | <i>O(log n)</i>. The minimal element of a set.
findMin :: Set a -> a

-- | <i>O(log n)</i>. The maximal element of a set.
findMax :: Set a -> a

-- | <i>O(log n)</i>. Delete the minimal element. Returns an empty set if
grthe set is empty.
deleteMin :: Set a -> Set a

-- | <i>O(log n)</i>. Delete the maximal element. Returns an empty set if
grthe set is empty.
deleteMax :: Set a -> Set a

-- | <i>O(log n)</i>. Delete and find the minimal element.
gr
gr<pre>
grdeleteFindMin set = (findMin set, deleteMin set)
gr</pre>
deleteFindMin :: Set a -> (a, Set a)

-- | <i>O(log n)</i>. Delete and find the maximal element.
gr
gr<pre>
grdeleteFindMax set = (findMax set, deleteMax set)
gr</pre>
deleteFindMax :: Set a -> (a, Set a)

-- | <i>O(log n)</i>. Retrieves the maximal key of the set, and the set
grstripped of that element, or <a>Nothing</a> if passed an empty set.
maxView :: Set a -> Maybe (a, Set a)

-- | <i>O(log n)</i>. Retrieves the minimal key of the set, and the set
grstripped of that element, or <a>Nothing</a> if passed an empty set.
minView :: Set a -> Maybe (a, Set a)

-- | <i>O(n)</i>. An alias of <a>toAscList</a>. The elements of a set in
grascending order. Subject to list fusion.
elems :: Set a -> [a]

-- | <i>O(n)</i>. Convert the set to a list of elements. Subject to list
grfusion.
toList :: Set a -> [a]

-- | <i>O(n*log n)</i>. Create a set from a list of elements.
gr
grIf the elements are ordered, a linear-time implementation is used,
grwith the performance equal to <a>fromDistinctAscList</a>.
fromList :: Ord a => [a] -> Set a

-- | <i>O(n)</i>. Convert the set to an ascending list of elements. Subject
grto list fusion.
toAscList :: Set a -> [a]

-- | <i>O(n)</i>. Convert the set to a descending list of elements. Subject
grto list fusion.
toDescList :: Set a -> [a]

-- | <i>O(n)</i>. Build a set from an ascending list in linear time. <i>The
grprecondition (input list is ascending) is not checked.</i>
fromAscList :: Eq a => [a] -> Set a

-- | <i>O(n)</i>. Build a set from a descending list in linear time. <i>The
grprecondition (input list is descending) is not checked.</i>
fromDescList :: Eq a => [a] -> Set a

-- | <i>O(n)</i>. Build a set from an ascending list of distinct elements
grin linear time. <i>The precondition (input list is strictly ascending)
gris not checked.</i>
fromDistinctAscList :: [a] -> Set a

-- | <i>O(n)</i>. Build a set from a descending list of distinct elements
grin linear time. <i>The precondition (input list is strictly
grdescending) is not checked.</i>
fromDistinctDescList :: [a] -> Set a

-- | <i>O(n)</i>. Show the tree that implements the set. The tree is shown
grin a compressed, hanging format.
showTree :: Show a => Set a -> String

-- | <i>O(n)</i>. The expression (<tt>showTreeWith hang wide map</tt>)
grshows the tree that implements the set. If <tt>hang</tt> is
gr<tt>True</tt>, a <i>hanging</i> tree is shown otherwise a rotated tree
gris shown. If <tt>wide</tt> is <a>True</a>, an extra wide version is
grshown.
gr
gr<pre>
grSet&gt; putStrLn $ showTreeWith True False $ fromDistinctAscList [1..5]
gr4
gr+--2
gr|  +--1
gr|  +--3
gr+--5
gr
grSet&gt; putStrLn $ showTreeWith True True $ fromDistinctAscList [1..5]
gr4
gr|
gr+--2
gr|  |
gr|  +--1
gr|  |
gr|  +--3
gr|
gr+--5
gr
grSet&gt; putStrLn $ showTreeWith False True $ fromDistinctAscList [1..5]
gr+--5
gr|
gr4
gr|
gr|  +--3
gr|  |
gr+--2
gr   |
gr   +--1
gr</pre>
showTreeWith :: Show a => Bool -> Bool -> Set a -> String

-- | <i>O(n)</i>. Test if the internal set structure is valid.
valid :: Ord a => Set a -> Bool


-- | <h1>WARNING</h1>
gr
grThis module is considered <b>internal</b>.
gr
grThe Package Versioning Policy <b>does not apply</b>.
gr
grThis contents of this module may change <b>in any way whatsoever</b>
grand <b>without any warning</b> between minor versions of this package.
gr
grAuthors importing this module are expected to track development
grclosely.
gr
gr<h1>Description</h1>
gr
grGeneral purpose finite sequences. Apart from being finite and having
grstrict operations, sequences also differ from lists in supporting a
grwider variety of operations efficiently.
gr
grAn amortized running time is given for each operation, with
gr&lt;math&gt; referring to the length of the sequence and &lt;math&gt;
grbeing the integral index used by some operations. These bounds hold
greven in a persistent (shared) setting.
gr
grThe implementation uses 2-3 finger trees annotated with sizes, as
grdescribed in section 4.2 of
gr
gr<ul>
gr<li>Ralf Hinze and Ross Paterson, "Finger trees: a simple
grgeneral-purpose data structure", <i>Journal of Functional
grProgramming</i> 16:2 (2006) pp 197-217.
gr<a>http://staff.city.ac.uk/~ross/papers/FingerTree.html</a></li>
gr</ul>
gr
gr<i>Note</i>: Many of these operations have the same names as similar
groperations on lists in the <a>Prelude</a>. The ambiguity may be
grresolved using either qualification or the <tt>hiding</tt> clause.
gr
gr<i>Warning</i>: The size of a <a>Seq</a> must not exceed
gr<tt>maxBound::Int</tt>. Violation of this condition is not detected
grand if the size limit is exceeded, the behaviour of the sequence is
grundefined. This is unlikely to occur in most applications, but some
grcare may be required when using <a>&gt;&lt;</a>, <a>&lt;*&gt;</a>,
gr<a>*&gt;</a>, or <a>&gt;&gt;</a>, particularly repeatedly and
grparticularly in combination with <a>replicate</a> or
gr<a>fromFunction</a>.
module Data.Sequence.Internal
newtype Elem a
Elem :: a -> Elem a
[getElem] :: Elem a -> a
data FingerTree a
EmptyT :: FingerTree a
Single :: a -> FingerTree a
Deep :: {-# UNPACK #-} !Int -> !(Digit a) -> (FingerTree (Node a)) -> !(Digit a) -> FingerTree a
data Node a
Node2 :: {-# UNPACK #-} !Int -> a -> a -> Node a
Node3 :: {-# UNPACK #-} !Int -> a -> a -> a -> Node a
data Digit a
One :: a -> Digit a
Two :: a -> a -> Digit a
Three :: a -> a -> a -> Digit a
Four :: a -> a -> a -> a -> Digit a
class Sized a
size :: Sized a => a -> Int
class MaybeForce a

-- | General-purpose finite sequences.
newtype Seq a
Seq :: (FingerTree (Elem a)) -> Seq a
newtype State s a
State :: s -> (s, a) -> State s a
[runState] :: State s a -> s -> (s, a)
execState :: State s a -> s -> a
foldDigit :: (b -> b -> b) -> (a -> b) -> Digit a -> b
foldNode :: (b -> b -> b) -> (a -> b) -> Node a -> b
foldWithIndexDigit :: Sized a => (b -> b -> b) -> (Int -> a -> b) -> Int -> Digit a -> b
foldWithIndexNode :: Sized a => (m -> m -> m) -> (Int -> a -> m) -> Int -> Node a -> m

-- | &lt;math&gt;. The empty sequence.
empty :: Seq a

-- | &lt;math&gt;. A singleton sequence.
singleton :: a -> Seq a

-- | &lt;math&gt;. Add an element to the left end of a sequence. Mnemonic:
gra triangle with the single element at the pointy end.
(<|) :: a -> Seq a -> Seq a
infixr 5 <|

-- | &lt;math&gt;. Add an element to the right end of a sequence. Mnemonic:
gra triangle with the single element at the pointy end.
(|>) :: Seq a -> a -> Seq a
infixl 5 |>

-- | &lt;math&gt;. Concatenate two sequences.
(><) :: Seq a -> Seq a -> Seq a
infixr 5 ><

-- | &lt;math&gt;. Create a sequence from a finite list of elements. There
gris a function <a>toList</a> in the opposite direction for all
grinstances of the <a>Foldable</a> class, including <a>Seq</a>.
fromList :: [a] -> Seq a

-- | &lt;math&gt;. Convert a given sequence length and a function
grrepresenting that sequence into a sequence.
fromFunction :: Int -> (Int -> a) -> Seq a

-- | &lt;math&gt;. Create a sequence consisting of the elements of an
gr<a>Array</a>. Note that the resulting sequence elements may be
grevaluated lazily (as on GHC), so you must force the entire structure
grto be sure that the original array can be garbage-collected.
fromArray :: Ix i => Array i a -> Seq a

-- | &lt;math&gt;. <tt>replicate n x</tt> is a sequence consisting of
gr<tt>n</tt> copies of <tt>x</tt>.
replicate :: Int -> a -> Seq a

-- | <a>replicateA</a> is an <a>Applicative</a> version of
gr<a>replicate</a>, and makes &lt;math&gt; calls to <a>liftA2</a> and
gr<a>pure</a>.
gr
gr<pre>
grreplicateA n x = sequenceA (replicate n x)
gr</pre>
replicateA :: Applicative f => Int -> f a -> f (Seq a)

-- | <a>replicateM</a> is a sequence counterpart of <a>replicateM</a>.
gr
gr<pre>
grreplicateM n x = sequence (replicate n x)
gr</pre>
gr
grFor <tt>base &gt;= 4.8.0</tt> and <tt>containers &gt;= 0.5.11</tt>,
gr<a>replicateM</a> is a synonym for <a>replicateA</a>.
replicateM :: Applicative m => Int -> m a -> m (Seq a)

-- | <i>O(</i>log<i> k)</i>. <tt><a>cycleTaking</a> k xs</tt> forms a
grsequence of length <tt>k</tt> by repeatedly concatenating <tt>xs</tt>
grwith itself. <tt>xs</tt> may only be empty if <tt>k</tt> is 0.
gr
gr<pre>
grcycleTaking k = fromList . take k . cycle . toList
gr</pre>
cycleTaking :: Int -> Seq a -> Seq a

-- | &lt;math&gt;. Constructs a sequence by repeated application of a
grfunction to a seed value.
gr
gr<pre>
griterateN n f x = fromList (Prelude.take n (Prelude.iterate f x))
gr</pre>
iterateN :: Int -> (a -> a) -> a -> Seq a

-- | Builds a sequence from a seed value. Takes time linear in the number
grof generated elements. <i>WARNING:</i> If the number of generated
grelements is infinite, this method will not terminate.
unfoldr :: (b -> Maybe (a, b)) -> b -> Seq a

-- | <tt><a>unfoldl</a> f x</tt> is equivalent to <tt><a>reverse</a>
gr(<a>unfoldr</a> (<a>fmap</a> swap . f) x)</tt>.
unfoldl :: (b -> Maybe (b, a)) -> b -> Seq a

-- | &lt;math&gt;. Is this the empty sequence?
null :: Seq a -> Bool

-- | &lt;math&gt;. The number of elements in the sequence.
length :: Seq a -> Int

-- | View of the left end of a sequence.
data ViewL a

-- | empty sequence
EmptyL :: ViewL a

-- | leftmost element and the rest of the sequence
(:<) :: a -> Seq a -> ViewL a

-- | &lt;math&gt;. Analyse the left end of a sequence.
viewl :: Seq a -> ViewL a

-- | View of the right end of a sequence.
data ViewR a

-- | empty sequence
EmptyR :: ViewR a

-- | the sequence minus the rightmost element, and the rightmost element
(:>) :: Seq a -> a -> ViewR a

-- | &lt;math&gt;. Analyse the right end of a sequence.
viewr :: Seq a -> ViewR a

-- | <a>scanl</a> is similar to <a>foldl</a>, but returns a sequence of
grreduced values from the left:
gr
gr<pre>
grscanl f z (fromList [x1, x2, ...]) = fromList [z, z `f` x1, (z `f` x1) `f` x2, ...]
gr</pre>
scanl :: (a -> b -> a) -> a -> Seq b -> Seq a

-- | <a>scanl1</a> is a variant of <a>scanl</a> that has no starting value
grargument:
gr
gr<pre>
grscanl1 f (fromList [x1, x2, ...]) = fromList [x1, x1 `f` x2, ...]
gr</pre>
scanl1 :: (a -> a -> a) -> Seq a -> Seq a

-- | <a>scanr</a> is the right-to-left dual of <a>scanl</a>.
scanr :: (a -> b -> b) -> b -> Seq a -> Seq b

-- | <a>scanr1</a> is a variant of <a>scanr</a> that has no starting value
grargument.
scanr1 :: (a -> a -> a) -> Seq a -> Seq a

-- | &lt;math&gt;. Returns a sequence of all suffixes of this sequence,
grlongest first. For example,
gr
gr<pre>
grtails (fromList "abc") = fromList [fromList "abc", fromList "bc", fromList "c", fromList ""]
gr</pre>
gr
grEvaluating the &lt;math&gt;th suffix takes &lt;math&gt;, but
grevaluating every suffix in the sequence takes &lt;math&gt; due to
grsharing.
tails :: Seq a -> Seq (Seq a)

-- | &lt;math&gt;. Returns a sequence of all prefixes of this sequence,
grshortest first. For example,
gr
gr<pre>
grinits (fromList "abc") = fromList [fromList "", fromList "a", fromList "ab", fromList "abc"]
gr</pre>
gr
grEvaluating the &lt;math&gt;th prefix takes &lt;math&gt;, but
grevaluating every prefix in the sequence takes &lt;math&gt; due to
grsharing.
inits :: Seq a -> Seq (Seq a)

-- | &lt;math&gt;. <tt>chunksOf c xs</tt> splits <tt>xs</tt> into chunks of
grsize <tt>c&gt;0</tt>. If <tt>c</tt> does not divide the length of
gr<tt>xs</tt> evenly, then the last element of the result will be short.
gr
grSide note: the given performance bound is missing some messy terms
grthat only really affect edge cases. Performance degrades smoothly from
gr&lt;math&gt; (for &lt;math&gt;) to &lt;math&gt; (for &lt;math&gt;).
grThe true bound is more like &lt;math&gt;
chunksOf :: Int -> Seq a -> Seq (Seq a)

-- | &lt;math&gt; where &lt;math&gt; is the prefix length.
gr<a>takeWhileL</a>, applied to a predicate <tt>p</tt> and a sequence
gr<tt>xs</tt>, returns the longest prefix (possibly empty) of
gr<tt>xs</tt> of elements that satisfy <tt>p</tt>.
takeWhileL :: (a -> Bool) -> Seq a -> Seq a

-- | &lt;math&gt; where &lt;math&gt; is the suffix length.
gr<a>takeWhileR</a>, applied to a predicate <tt>p</tt> and a sequence
gr<tt>xs</tt>, returns the longest suffix (possibly empty) of
gr<tt>xs</tt> of elements that satisfy <tt>p</tt>.
gr
gr<tt><a>takeWhileR</a> p xs</tt> is equivalent to <tt><a>reverse</a>
gr(<a>takeWhileL</a> p (<a>reverse</a> xs))</tt>.
takeWhileR :: (a -> Bool) -> Seq a -> Seq a

-- | &lt;math&gt; where &lt;math&gt; is the prefix length.
gr<tt><a>dropWhileL</a> p xs</tt> returns the suffix remaining after
gr<tt><a>takeWhileL</a> p xs</tt>.
dropWhileL :: (a -> Bool) -> Seq a -> Seq a

-- | &lt;math&gt; where &lt;math&gt; is the suffix length.
gr<tt><a>dropWhileR</a> p xs</tt> returns the prefix remaining after
gr<tt><a>takeWhileR</a> p xs</tt>.
gr
gr<tt><a>dropWhileR</a> p xs</tt> is equivalent to <tt><a>reverse</a>
gr(<a>dropWhileL</a> p (<a>reverse</a> xs))</tt>.
dropWhileR :: (a -> Bool) -> Seq a -> Seq a

-- | &lt;math&gt; where &lt;math&gt; is the prefix length. <a>spanl</a>,
grapplied to a predicate <tt>p</tt> and a sequence <tt>xs</tt>, returns
gra pair whose first element is the longest prefix (possibly empty) of
gr<tt>xs</tt> of elements that satisfy <tt>p</tt> and the second element
gris the remainder of the sequence.
spanl :: (a -> Bool) -> Seq a -> (Seq a, Seq a)

-- | &lt;math&gt; where &lt;math&gt; is the suffix length. <a>spanr</a>,
grapplied to a predicate <tt>p</tt> and a sequence <tt>xs</tt>, returns
gra pair whose <i>first</i> element is the longest <i>suffix</i>
gr(possibly empty) of <tt>xs</tt> of elements that satisfy <tt>p</tt>
grand the second element is the remainder of the sequence.
spanr :: (a -> Bool) -> Seq a -> (Seq a, Seq a)

-- | &lt;math&gt; where &lt;math&gt; is the breakpoint index.
gr<a>breakl</a>, applied to a predicate <tt>p</tt> and a sequence
gr<tt>xs</tt>, returns a pair whose first element is the longest prefix
gr(possibly empty) of <tt>xs</tt> of elements that <i>do not satisfy</i>
gr<tt>p</tt> and the second element is the remainder of the sequence.
gr
gr<tt><a>breakl</a> p</tt> is equivalent to <tt><a>spanl</a> (not .
grp)</tt>.
breakl :: (a -> Bool) -> Seq a -> (Seq a, Seq a)

-- | <tt><a>breakr</a> p</tt> is equivalent to <tt><a>spanr</a> (not .
grp)</tt>.
breakr :: (a -> Bool) -> Seq a -> (Seq a, Seq a)

-- | &lt;math&gt;. The <a>partition</a> function takes a predicate
gr<tt>p</tt> and a sequence <tt>xs</tt> and returns sequences of those
grelements which do and do not satisfy the predicate.
partition :: (a -> Bool) -> Seq a -> (Seq a, Seq a)

-- | &lt;math&gt;. The <a>filter</a> function takes a predicate <tt>p</tt>
grand a sequence <tt>xs</tt> and returns a sequence of those elements
grwhich satisfy the predicate.
filter :: (a -> Bool) -> Seq a -> Seq a

-- | &lt;math&gt;. The element at the specified position, counting from 0.
grIf the specified position is negative or at least the length of the
grsequence, <a>lookup</a> returns <a>Nothing</a>.
gr
gr<pre>
gr0 &lt;= i &lt; length xs ==&gt; lookup i xs == Just (toList xs !! i)
gr</pre>
gr
gr<pre>
gri &lt; 0 || i &gt;= length xs ==&gt; lookup i xs = Nothing
gr</pre>
gr
grUnlike <a>index</a>, this can be used to retrieve an element without
grforcing it. For example, to insert the fifth element of a sequence
gr<tt>xs</tt> into a <a>Map</a> <tt>m</tt> at key <tt>k</tt>, you could
gruse
gr
gr<pre>
grcase lookup 5 xs of
gr  Nothing -&gt; m
gr  Just x -&gt; <a>insert</a> k x m
gr</pre>
lookup :: Int -> Seq a -> Maybe a

-- | &lt;math&gt;. A flipped, infix version of <a>lookup</a>.
(!?) :: Seq a -> Int -> Maybe a

-- | &lt;math&gt;. The element at the specified position, counting from 0.
grThe argument should thus be a non-negative integer less than the size
grof the sequence. If the position is out of range, <a>index</a> fails
grwith an error.
gr
gr<pre>
grxs `index` i = toList xs !! i
gr</pre>
gr
grCaution: <a>index</a> necessarily delays retrieving the requested
grelement until the result is forced. It can therefore lead to a space
grleak if the result is stored, unforced, in another structure. To
grretrieve an element immediately without forcing it, use <a>lookup</a>
gror '(!?)'.
index :: Seq a -> Int -> a

-- | &lt;math&gt;. Update the element at the specified position. If the
grposition is out of range, the original sequence is returned.
gr<a>adjust</a> can lead to poor performance and even memory leaks,
grbecause it does not force the new value before installing it in the
grsequence. <a>adjust'</a> should usually be preferred.
adjust :: (a -> a) -> Int -> Seq a -> Seq a

-- | &lt;math&gt;. Update the element at the specified position. If the
grposition is out of range, the original sequence is returned. The new
grvalue is forced before it is installed in the sequence.
gr
gr<pre>
gradjust' f i xs =
gr case xs !? i of
gr   Nothing -&gt; xs
gr   Just x -&gt; let !x' = f x
gr             in update i x' xs
gr</pre>
adjust' :: forall a. (a -> a) -> Int -> Seq a -> Seq a

-- | &lt;math&gt;. Replace the element at the specified position. If the
grposition is out of range, the original sequence is returned.
update :: Int -> a -> Seq a -> Seq a

-- | &lt;math&gt;. The first <tt>i</tt> elements of a sequence. If
gr<tt>i</tt> is negative, <tt><a>take</a> i s</tt> yields the empty
grsequence. If the sequence contains fewer than <tt>i</tt> elements, the
grwhole sequence is returned.
take :: Int -> Seq a -> Seq a

-- | &lt;math&gt;. Elements of a sequence after the first <tt>i</tt>. If
gr<tt>i</tt> is negative, <tt><a>drop</a> i s</tt> yields the whole
grsequence. If the sequence contains fewer than <tt>i</tt> elements, the
grempty sequence is returned.
drop :: Int -> Seq a -> Seq a

-- | &lt;math&gt;. <tt><a>insertAt</a> i x xs</tt> inserts <tt>x</tt> into
gr<tt>xs</tt> at the index <tt>i</tt>, shifting the rest of the sequence
grover.
gr
gr<pre>
grinsertAt 2 x (fromList [a,b,c,d]) = fromList [a,b,x,c,d]
grinsertAt 4 x (fromList [a,b,c,d]) = insertAt 10 x (fromList [a,b,c,d])
gr                                  = fromList [a,b,c,d,x]
gr</pre>
gr
gr<pre>
grinsertAt i x xs = take i xs &gt;&lt; singleton x &gt;&lt; drop i xs
gr</pre>
insertAt :: Int -> a -> Seq a -> Seq a

-- | &lt;math&gt;. Delete the element of a sequence at a given index.
grReturn the original sequence if the index is out of range.
gr
gr<pre>
grdeleteAt 2 [a,b,c,d] = [a,b,d]
grdeleteAt 4 [a,b,c,d] = deleteAt (-1) [a,b,c,d] = [a,b,c,d]
gr</pre>
deleteAt :: Int -> Seq a -> Seq a

-- | &lt;math&gt;. Split a sequence at a given position. <tt><a>splitAt</a>
gri s = (<a>take</a> i s, <a>drop</a> i s)</tt>.
splitAt :: Int -> Seq a -> (Seq a, Seq a)

-- | <a>elemIndexL</a> finds the leftmost index of the specified element,
grif it is present, and otherwise <a>Nothing</a>.
elemIndexL :: Eq a => a -> Seq a -> Maybe Int

-- | <a>elemIndicesL</a> finds the indices of the specified element, from
grleft to right (i.e. in ascending order).
elemIndicesL :: Eq a => a -> Seq a -> [Int]

-- | <a>elemIndexR</a> finds the rightmost index of the specified element,
grif it is present, and otherwise <a>Nothing</a>.
elemIndexR :: Eq a => a -> Seq a -> Maybe Int

-- | <a>elemIndicesR</a> finds the indices of the specified element, from
grright to left (i.e. in descending order).
elemIndicesR :: Eq a => a -> Seq a -> [Int]

-- | <tt><a>findIndexL</a> p xs</tt> finds the index of the leftmost
grelement that satisfies <tt>p</tt>, if any exist.
findIndexL :: (a -> Bool) -> Seq a -> Maybe Int

-- | <tt><a>findIndicesL</a> p</tt> finds all indices of elements that
grsatisfy <tt>p</tt>, in ascending order.
findIndicesL :: (a -> Bool) -> Seq a -> [Int]

-- | <tt><a>findIndexR</a> p xs</tt> finds the index of the rightmost
grelement that satisfies <tt>p</tt>, if any exist.
findIndexR :: (a -> Bool) -> Seq a -> Maybe Int

-- | <tt><a>findIndicesR</a> p</tt> finds all indices of elements that
grsatisfy <tt>p</tt>, in descending order.
findIndicesR :: (a -> Bool) -> Seq a -> [Int]
foldMapWithIndex :: Monoid m => (Int -> a -> m) -> Seq a -> m

-- | <a>foldlWithIndex</a> is a version of <a>foldl</a> that also provides
graccess to the index of each element.
foldlWithIndex :: (b -> Int -> a -> b) -> b -> Seq a -> b

-- | <a>foldrWithIndex</a> is a version of <a>foldr</a> that also provides
graccess to the index of each element.
foldrWithIndex :: (Int -> a -> b -> b) -> b -> Seq a -> b

-- | A generalization of <a>fmap</a>, <a>mapWithIndex</a> takes a mapping
grfunction that also depends on the element's index, and applies it to
grevery element in the sequence.
mapWithIndex :: (Int -> a -> b) -> Seq a -> Seq b

-- | <a>traverseWithIndex</a> is a version of <a>traverse</a> that also
groffers access to the index of each element.
traverseWithIndex :: Applicative f => (Int -> a -> f b) -> Seq a -> f (Seq b)

-- | &lt;math&gt;. The reverse of a sequence.
reverse :: Seq a -> Seq a

-- | &lt;math&gt;. Intersperse an element between the elements of a
grsequence.
gr
gr<pre>
grintersperse a empty = empty
grintersperse a (singleton x) = singleton x
grintersperse a (fromList [x,y]) = fromList [x,a,y]
grintersperse a (fromList [x,y,z]) = fromList [x,a,y,a,z]
gr</pre>
intersperse :: a -> Seq a -> Seq a
liftA2Seq :: (a -> b -> c) -> Seq a -> Seq b -> Seq c

-- | &lt;math&gt;. <a>zip</a> takes two sequences and returns a sequence of
grcorresponding pairs. If one input is short, excess elements are
grdiscarded from the right end of the longer sequence.
zip :: Seq a -> Seq b -> Seq (a, b)

-- | &lt;math&gt;. <a>zipWith</a> generalizes <a>zip</a> by zipping with
grthe function given as the first argument, instead of a tupling
grfunction. For example, <tt>zipWith (+)</tt> is applied to two
grsequences to take the sequence of corresponding sums.
zipWith :: (a -> b -> c) -> Seq a -> Seq b -> Seq c

-- | &lt;math&gt;. <a>zip3</a> takes three sequences and returns a sequence
grof triples, analogous to <a>zip</a>.
zip3 :: Seq a -> Seq b -> Seq c -> Seq (a, b, c)

-- | &lt;math&gt;. <a>zipWith3</a> takes a function which combines three
grelements, as well as three sequences and returns a sequence of their
grpoint-wise combinations, analogous to <a>zipWith</a>.
zipWith3 :: (a -> b -> c -> d) -> Seq a -> Seq b -> Seq c -> Seq d

-- | &lt;math&gt;. <a>zip4</a> takes four sequences and returns a sequence
grof quadruples, analogous to <a>zip</a>.
zip4 :: Seq a -> Seq b -> Seq c -> Seq d -> Seq (a, b, c, d)

-- | &lt;math&gt;. <a>zipWith4</a> takes a function which combines four
grelements, as well as four sequences and returns a sequence of their
grpoint-wise combinations, analogous to <a>zipWith</a>.
zipWith4 :: (a -> b -> c -> d -> e) -> Seq a -> Seq b -> Seq c -> Seq d -> Seq e

-- | Unzip a sequence of pairs.
gr
gr<pre>
grunzip ps = ps `<tt>seq'</tt> (<a>fmap</a> <a>fst</a> ps) (<a>fmap</a> <a>snd</a> ps)
gr</pre>
gr
grExample:
gr
gr<pre>
grunzip $ fromList [(1,"a"), (2,"b"), (3,"c")] =
gr  (fromList [1,2,3], fromList ["a", "b", "c"])
gr</pre>
gr
grSee the note about efficiency at <a>unzipWith</a>.
unzip :: Seq (a, b) -> (Seq a, Seq b)

-- | &lt;math&gt;. Unzip a sequence using a function to divide elements.
gr
gr<pre>
grunzipWith f xs == <a>unzip</a> (<a>fmap</a> f xs)
gr</pre>
gr
grEfficiency note:
gr
gr<tt>unzipWith</tt> produces its two results in lockstep. If you
grcalculate <tt> unzipWith f xs </tt> and fully force <i>either</i> of
grthe results, then the entire structure of the <i>other</i> one will be
grbuilt as well. This behavior allows the garbage collector to collect
greach calculated pair component as soon as it dies, without having to
grwait for its mate to die. If you do not need this behavior, you may be
grbetter off simply calculating the sequence of pairs and using
gr<a>fmap</a> to extract each component sequence.
unzipWith :: (a -> (b, c)) -> Seq a -> (Seq b, Seq c)
instance GHC.Read.Read a => GHC.Read.Read (Data.Sequence.Internal.ViewR a)
instance GHC.Show.Show a => GHC.Show.Show (Data.Sequence.Internal.ViewR a)
instance GHC.Classes.Ord a => GHC.Classes.Ord (Data.Sequence.Internal.ViewR a)
instance GHC.Classes.Eq a => GHC.Classes.Eq (Data.Sequence.Internal.ViewR a)
instance GHC.Read.Read a => GHC.Read.Read (Data.Sequence.Internal.ViewL a)
instance GHC.Show.Show a => GHC.Show.Show (Data.Sequence.Internal.ViewL a)
instance GHC.Classes.Ord a => GHC.Classes.Ord (Data.Sequence.Internal.ViewL a)
instance GHC.Classes.Eq a => GHC.Classes.Eq (Data.Sequence.Internal.ViewL a)
instance Data.Data.Data a => Data.Data.Data (Data.Sequence.Internal.ViewL a)
instance GHC.Generics.Generic1 Data.Sequence.Internal.ViewL
instance GHC.Generics.Generic (Data.Sequence.Internal.ViewL a)
instance Data.Data.Data a => Data.Data.Data (Data.Sequence.Internal.ViewR a)
instance GHC.Generics.Generic1 Data.Sequence.Internal.ViewR
instance GHC.Generics.Generic (Data.Sequence.Internal.ViewR a)
instance Data.Sequence.Internal.UnzipWith Data.Sequence.Internal.Elem
instance Data.Sequence.Internal.UnzipWith Data.Sequence.Internal.Node
instance Data.Sequence.Internal.UnzipWith Data.Sequence.Internal.Digit
instance Data.Sequence.Internal.UnzipWith Data.Sequence.Internal.FingerTree
instance Data.Sequence.Internal.UnzipWith Data.Sequence.Internal.Seq
instance GHC.Base.Functor Data.Sequence.Internal.ViewR
instance Data.Foldable.Foldable Data.Sequence.Internal.ViewR
instance Data.Traversable.Traversable Data.Sequence.Internal.ViewR
instance Data.Data.Data a => Data.Data.Data (Data.Sequence.Internal.Seq a)
instance GHC.Base.Functor Data.Sequence.Internal.ViewL
instance Data.Foldable.Foldable Data.Sequence.Internal.ViewL
instance Data.Traversable.Traversable Data.Sequence.Internal.ViewL
instance GHC.Base.Functor Data.Sequence.Internal.Seq
instance Data.Foldable.Foldable Data.Sequence.Internal.Seq
instance Data.Traversable.Traversable Data.Sequence.Internal.Seq
instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Data.Sequence.Internal.Seq a)
instance GHC.Base.Monad Data.Sequence.Internal.Seq
instance Control.Monad.Fix.MonadFix Data.Sequence.Internal.Seq
instance GHC.Base.Applicative Data.Sequence.Internal.Seq
instance GHC.Base.MonadPlus Data.Sequence.Internal.Seq
instance GHC.Base.Alternative Data.Sequence.Internal.Seq
instance GHC.Classes.Eq a => GHC.Classes.Eq (Data.Sequence.Internal.Seq a)
instance GHC.Classes.Ord a => GHC.Classes.Ord (Data.Sequence.Internal.Seq a)
instance GHC.Show.Show a => GHC.Show.Show (Data.Sequence.Internal.Seq a)
instance Data.Functor.Classes.Show1 Data.Sequence.Internal.Seq
instance Data.Functor.Classes.Eq1 Data.Sequence.Internal.Seq
instance Data.Functor.Classes.Ord1 Data.Sequence.Internal.Seq
instance GHC.Read.Read a => GHC.Read.Read (Data.Sequence.Internal.Seq a)
instance Data.Functor.Classes.Read1 Data.Sequence.Internal.Seq
instance GHC.Base.Monoid (Data.Sequence.Internal.Seq a)
instance GHC.Base.Semigroup (Data.Sequence.Internal.Seq a)
instance GHC.Exts.IsList (Data.Sequence.Internal.Seq a)
instance (a ~ GHC.Types.Char) => Data.String.IsString (Data.Sequence.Internal.Seq a)
instance Control.Monad.Zip.MonadZip Data.Sequence.Internal.Seq
instance Data.Sequence.Internal.MaybeForce (Data.Sequence.Internal.Elem a)
instance Data.Sequence.Internal.Sized a => Data.Sequence.Internal.Sized (Data.Sequence.Internal.FingerTree a)
instance Data.Sequence.Internal.Sized (Data.Sequence.Internal.Elem a)
instance GHC.Base.Functor Data.Sequence.Internal.Elem
instance Data.Foldable.Foldable Data.Sequence.Internal.Elem
instance Data.Traversable.Traversable Data.Sequence.Internal.Elem
instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Data.Sequence.Internal.Elem a)
instance Data.Foldable.Foldable Data.Sequence.Internal.FingerTree
instance GHC.Base.Functor Data.Sequence.Internal.FingerTree
instance Data.Traversable.Traversable Data.Sequence.Internal.FingerTree
instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Data.Sequence.Internal.FingerTree a)
instance Data.Sequence.Internal.MaybeForce (Data.Sequence.Internal.Node a)
instance Data.Foldable.Foldable Data.Sequence.Internal.Node
instance GHC.Base.Functor Data.Sequence.Internal.Node
instance Data.Traversable.Traversable Data.Sequence.Internal.Node
instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Data.Sequence.Internal.Node a)
instance Data.Sequence.Internal.Sized (Data.Sequence.Internal.Node a)
instance Data.Foldable.Foldable Data.Sequence.Internal.Digit
instance GHC.Base.Functor Data.Sequence.Internal.Digit
instance Data.Traversable.Traversable Data.Sequence.Internal.Digit
instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Data.Sequence.Internal.Digit a)
instance Data.Sequence.Internal.Sized a => Data.Sequence.Internal.Sized (Data.Sequence.Internal.Digit a)
instance Data.Sequence.Internal.MaybeForce (Data.Sequence.Internal.ForceBox a)
instance Data.Sequence.Internal.Sized (Data.Sequence.Internal.ForceBox a)


-- | <h1>WARNING</h1>
gr
grThis module is considered <b>internal</b>.
gr
grThe Package Versioning Policy <b>does not apply</b>.
gr
grThis contents of this module may change <b>in any way whatsoever</b>
grand <b>without any warning</b> between minor versions of this package.
gr
grAuthors importing this module are expected to track development
grclosely.
gr
gr<h1>Description</h1>
gr
grThis module provides the various sorting implementations for
gr<a>Data.Sequence</a>. Further notes are available in the file
grsorting.md (in this directory).
module Data.Sequence.Internal.Sorting

-- | &lt;math&gt;. <a>sort</a> sorts the specified <a>Seq</a> by the
grnatural ordering of its elements. The sort is stable. If stability is
grnot required, <a>unstableSort</a> can be slightly faster.
sort :: Ord a => Seq a -> Seq a

-- | &lt;math&gt;. <a>sortBy</a> sorts the specified <a>Seq</a> according
grto the specified comparator. The sort is stable. If stability is not
grrequired, <a>unstableSortBy</a> can be slightly faster.
sortBy :: (a -> a -> Ordering) -> Seq a -> Seq a

-- | &lt;math&gt;. <a>sortOn</a> sorts the specified <a>Seq</a> by
grcomparing the results of a key function applied to each element.
gr<tt><a>sortOn</a> f</tt> is equivalent to <tt><a>sortBy</a>
gr(<a>compare</a> `<a>on</a>` f)</tt>, but has the performance advantage
grof only evaluating <tt>f</tt> once for each element in the input list.
grThis is called the decorate-sort-undecorate paradigm, or Schwartzian
grtransform.
gr
grAn example of using <a>sortOn</a> might be to sort a <a>Seq</a> of
grstrings according to their length:
gr
gr<pre>
grsortOn length (fromList ["alligator", "monkey", "zebra"]) == fromList ["zebra", "monkey", "alligator"]
gr</pre>
gr
grIf, instead, <a>sortBy</a> had been used, <a>length</a> would be
grevaluated on every comparison, giving &lt;math&gt; evaluations, rather
grthan &lt;math&gt;.
gr
grIf <tt>f</tt> is very cheap (for example a record selector, or
gr<a>fst</a>), <tt><a>sortBy</a> (<a>compare</a> `<a>on</a>` f)</tt>
grwill be faster than <tt><a>sortOn</a> f</tt>.
sortOn :: Ord b => (a -> b) -> Seq a -> Seq a

-- | &lt;math&gt;. <a>unstableSort</a> sorts the specified <a>Seq</a> by
grthe natural ordering of its elements, but the sort is not stable. This
gralgorithm is frequently faster and uses less memory than <a>sort</a>.
unstableSort :: Ord a => Seq a -> Seq a

-- | &lt;math&gt;. A generalization of <a>unstableSort</a>,
gr<a>unstableSortBy</a> takes an arbitrary comparator and sorts the
grspecified sequence. The sort is not stable. This algorithm is
grfrequently faster and uses less memory than <a>sortBy</a>.
unstableSortBy :: (a -> a -> Ordering) -> Seq a -> Seq a

-- | &lt;math&gt;. <a>unstableSortOn</a> sorts the specified <a>Seq</a> by
grcomparing the results of a key function applied to each element.
gr<tt><a>unstableSortOn</a> f</tt> is equivalent to
gr<tt><a>unstableSortBy</a> (<a>compare</a> `<a>on</a>` f)</tt>, but has
grthe performance advantage of only evaluating <tt>f</tt> once for each
grelement in the input list. This is called the decorate-sort-undecorate
grparadigm, or Schwartzian transform.
gr
grAn example of using <a>unstableSortOn</a> might be to sort a
gr<a>Seq</a> of strings according to their length:
gr
gr<pre>
grunstableSortOn length (fromList ["alligator", "monkey", "zebra"]) == fromList ["zebra", "monkey", "alligator"]
gr</pre>
gr
grIf, instead, <a>unstableSortBy</a> had been used, <a>length</a> would
grbe evaluated on every comparison, giving &lt;math&gt; evaluations,
grrather than &lt;math&gt;.
gr
grIf <tt>f</tt> is very cheap (for example a record selector, or
gr<a>fst</a>), <tt><a>unstableSortBy</a> (<a>compare</a> `<a>on</a>`
grf)</tt> will be faster than <tt><a>unstableSortOn</a> f</tt>.
unstableSortOn :: Ord b => (a -> b) -> Seq a -> Seq a

-- | A simple pairing heap.
data Queue e
Q :: !e -> (QList e) -> Queue e
data QList e
Nil :: QList e
QCons :: {-# UNPACK #-} !(Queue e) -> (QList e) -> QList e

-- | A pairing heap tagged with the original position of elements, to allow
grfor stable sorting.
data IndexedQueue e
IQ :: {-# UNPACK #-} !Int -> !e -> (IQList e) -> IndexedQueue e
data IQList e
IQNil :: IQList e
IQCons :: {-# UNPACK #-} !(IndexedQueue e) -> (IQList e) -> IQList e

-- | A pairing heap tagged with some key for sorting elements, for use in
gr<a>unstableSortOn</a>.
data TaggedQueue a b
TQ :: !a -> b -> (TQList a b) -> TaggedQueue a b
data TQList a b
TQNil :: TQList a b
TQCons :: {-# UNPACK #-} !(TaggedQueue a b) -> (TQList a b) -> TQList a b

-- | A pairing heap tagged with both a key and the original position of its
grelements, for use in <a>sortOn</a>.
data IndexedTaggedQueue e a
ITQ :: {-# UNPACK #-} !Int -> !e -> a -> (ITQList e a) -> IndexedTaggedQueue e a
data ITQList e a
ITQNil :: ITQList e a
ITQCons :: {-# UNPACK #-} !(IndexedTaggedQueue e a) -> (ITQList e a) -> ITQList e a

-- | <a>mergeQ</a> merges two <a>Queue</a>s.
mergeQ :: (a -> a -> Ordering) -> Queue a -> Queue a -> Queue a

-- | <a>mergeIQ</a> merges two <a>IndexedQueue</a>s, taking into account
grthe original position of the elements.
mergeIQ :: (a -> a -> Ordering) -> IndexedQueue a -> IndexedQueue a -> IndexedQueue a

-- | <a>mergeTQ</a> merges two <a>TaggedQueue</a>s, based on the tag value.
mergeTQ :: (a -> a -> Ordering) -> TaggedQueue a b -> TaggedQueue a b -> TaggedQueue a b

-- | <a>mergeITQ</a> merges two <a>IndexedTaggedQueue</a>s, based on the
grtag value, taking into account the original position of the elements.
mergeITQ :: (a -> a -> Ordering) -> IndexedTaggedQueue a b -> IndexedTaggedQueue a b -> IndexedTaggedQueue a b

-- | Pop the smallest element from the queue, using the supplied
grcomparator.
popMinQ :: (e -> e -> Ordering) -> Queue e -> (Queue e, e)

-- | Pop the smallest element from the queue, using the supplied
grcomparator, deferring to the item's original position when the
grcomparator returns <a>EQ</a>.
popMinIQ :: (e -> e -> Ordering) -> IndexedQueue e -> (IndexedQueue e, e)

-- | Pop the smallest element from the queue, using the supplied comparator
gron the tag.
popMinTQ :: (a -> a -> Ordering) -> TaggedQueue a b -> (TaggedQueue a b, b)

-- | Pop the smallest element from the queue, using the supplied comparator
gron the tag, deferring to the item's original position when the
grcomparator returns <a>EQ</a>.
popMinITQ :: (e -> e -> Ordering) -> IndexedTaggedQueue e b -> (IndexedTaggedQueue e b, b)
buildQ :: (b -> b -> Ordering) -> (a -> Queue b) -> FingerTree a -> Maybe (Queue b)
buildIQ :: (b -> b -> Ordering) -> (Int -> Elem y -> IndexedQueue b) -> Int -> FingerTree (Elem y) -> Maybe (IndexedQueue b)
buildTQ :: (b -> b -> Ordering) -> (a -> TaggedQueue b c) -> FingerTree a -> Maybe (TaggedQueue b c)
buildITQ :: (b -> b -> Ordering) -> (Int -> Elem y -> IndexedTaggedQueue b c) -> Int -> FingerTree (Elem y) -> Maybe (IndexedTaggedQueue b c)

-- | A <a>foldMap</a>-like function, specialized to the <a>Option</a>
grmonoid, which takes advantage of the internal structure of <a>Seq</a>
grto avoid wrapping in <a>Maybe</a> at certain points.
foldToMaybeTree :: (b -> b -> b) -> (a -> b) -> FingerTree a -> Maybe b

-- | A <tt>foldMapWithIndex</tt>-like function, specialized to the
gr<a>Option</a> monoid, which takes advantage of the internal structure
grof <a>Seq</a> to avoid wrapping in <a>Maybe</a> at certain points.
foldToMaybeWithIndexTree :: (b -> b -> b) -> (Int -> Elem y -> b) -> Int -> FingerTree (Elem y) -> Maybe b


-- | <h1>Finite sequences</h1>
gr
grThe <tt><a>Seq</a> a</tt> type represents a finite sequence of values
grof type <tt>a</tt>.
gr
grSequences generally behave very much like lists.
gr
gr<ul>
gr<li>The class instances for sequences are all based very closely on
grthose for lists.</li>
gr<li>Many functions in this module have the same names as functions in
grthe <a>Prelude</a> or in <a>Data.List</a>. In almost all cases, these
grfunctions behave analogously. For example, <a>filter</a> filters a
grsequence in exactly the same way that
gr<tt><a>Prelude</a>.<a>filter</a></tt> filters a list. The only major
grexception is the <a>lookup</a> function, which is based on the
grfunction by that name in <a>Data.IntMap</a> rather than the one in
gr<a>Prelude</a>.</li>
gr</ul>
gr
grThere are two major differences between sequences and lists:
gr
gr<ul>
gr<li>Sequences support a wider variety of efficient operations than do
grlists. Notably, they offer<ul><li>Constant-time access to both the
grfront and the rear with <a>&lt;|</a>, <a>|&gt;</a>, <a>viewl</a>,
gr<a>viewr</a>. For recent GHC versions, this can be done more
grconveniently using the bidirectional patterns <a>Empty</a>,
gr<a>:&lt;|</a>, and <a>:|&gt;</a>. See the detailed explanation in the
gr"Pattern synonyms" section.</li><li>Logarithmic-time concatenation
grwith <a>&gt;&lt;</a></li><li>Logarithmic-time splitting with
gr<a>splitAt</a>, <a>take</a> and <a>drop</a></li><li>Logarithmic-time
graccess to any element with <a>lookup</a>, <a>!?</a>, <a>index</a>,
gr<a>insertAt</a>, <a>deleteAt</a>, <a>adjust'</a>, and
gr<a>update</a></li></ul></li>
gr</ul>
gr
grNote that sequences are typically <i>slower</i> than lists when using
gronly operations for which they have the same big-(O) complexity:
grsequences make rather mediocre stacks!
gr
gr<ul>
gr<li>Whereas lists can be either finite or infinite, sequences are
gralways finite. As a result, a sequence is strict in its length.
grIgnoring efficiency, you can imagine that <a>Seq</a> is defined<pre>
grdata Seq a = Empty | a :&lt;| !(Seq a)</pre>This means that many
groperations on sequences are stricter than those on lists. For
grexample,<pre> (1 : undefined) !! 0 = 1</pre>but<pre> (1 :&lt;|
grundefined) <a>index</a> 0 = undefined</pre></li>
gr</ul>
gr
grSequences may also be compared to immutable <a>arrays</a> or
gr<a>vectors</a>. Like these structures, sequences support fast
grindexing, although not as fast. But editing an immutable array or
grvector, or combining it with another, generally requires copying the
grentire structure; sequences generally avoid that, copying only the
grportion that has changed.
gr
gr<h2>Detailed performance information</h2>
gr
grAn amortized running time is given for each operation, with <i>n</i>
grreferring to the length of the sequence and <i>i</i> being the
grintegral index used by some operations. These bounds hold even in a
grpersistent (shared) setting.
gr
grDespite sequences being structurally strict from a semantic
grstandpoint, they are in fact implemented using laziness internally. As
gra result, many operations can be performed <i>incrementally</i>,
grproducing their results as they are demanded. This greatly improves
grperformance in some cases. These functions include
gr
gr<ul>
gr<li>The <a>Functor</a> methods <a>fmap</a> and <a>&lt;$</a>, along
grwith <a>mapWithIndex</a></li>
gr<li>The <a>Applicative</a> methods <a>&lt;*&gt;</a>, <a>*&gt;</a>, and
gr<a>&lt;*</a></li>
gr<li>The zips: <a>zipWith</a>, <a>zip</a>, etc.</li>
gr<li><tt>heads</tt> and <a>tails</a></li>
gr<li><a>fromFunction</a>, <a>replicate</a>, <a>intersperse</a>, and
gr<a>cycleTaking</a></li>
gr<li><a>reverse</a></li>
gr<li><a>chunksOf</a></li>
gr</ul>
gr
grNote that the <a>Monad</a> method, <a>&gt;&gt;=</a>, is not
grparticularly lazy. It will take time proportional to the sum of the
grlogarithms of the individual result sequences to produce anything
grwhatsoever.
gr
grSeveral functions take special advantage of sharing to produce results
grusing much less time and memory than one might expect. These are
grdocumented individually for functions, but also include the methods
gr<a>&lt;$</a> and <a>*&gt;</a>, each of which take time and space
grproportional to the logarithm of the size of the result.
gr
gr<h2>Warning</h2>
gr
grThe size of a <a>Seq</a> must not exceed <tt>maxBound::Int</tt>.
grViolation of this condition is not detected and if the size limit is
grexceeded, the behaviour of the sequence is undefined. This is unlikely
grto occur in most applications, but some care may be required when
grusing <a>&gt;&lt;</a>, <a>&lt;*&gt;</a>, <a>*&gt;</a>, or
gr<a>&gt;&gt;</a>, particularly repeatedly and particularly in
grcombination with <a>replicate</a> or <a>fromFunction</a>.
gr
gr<h2>Implementation</h2>
gr
grThe implementation uses 2-3 finger trees annotated with sizes, as
grdescribed in section 4.2 of
gr
gr<ul>
gr<li>Ralf Hinze and Ross Paterson, <a>"Finger trees: a simple
grgeneral-purpose data structure"</a>, <i>Journal of Functional
grProgramming</i> 16:2 (2006) pp 197-217.</li>
gr</ul>
module Data.Sequence

-- | General-purpose finite sequences.
data Seq a

-- | &lt;math&gt;. The empty sequence.
empty :: Seq a

-- | &lt;math&gt;. A singleton sequence.
singleton :: a -> Seq a

-- | &lt;math&gt;. Add an element to the left end of a sequence. Mnemonic:
gra triangle with the single element at the pointy end.
(<|) :: a -> Seq a -> Seq a
infixr 5 <|

-- | &lt;math&gt;. Add an element to the right end of a sequence. Mnemonic:
gra triangle with the single element at the pointy end.
(|>) :: Seq a -> a -> Seq a
infixl 5 |>

-- | &lt;math&gt;. Concatenate two sequences.
(><) :: Seq a -> Seq a -> Seq a
infixr 5 ><

-- | &lt;math&gt;. Create a sequence from a finite list of elements. There
gris a function <a>toList</a> in the opposite direction for all
grinstances of the <a>Foldable</a> class, including <a>Seq</a>.
fromList :: [a] -> Seq a

-- | &lt;math&gt;. Convert a given sequence length and a function
grrepresenting that sequence into a sequence.
fromFunction :: Int -> (Int -> a) -> Seq a

-- | &lt;math&gt;. Create a sequence consisting of the elements of an
gr<a>Array</a>. Note that the resulting sequence elements may be
grevaluated lazily (as on GHC), so you must force the entire structure
grto be sure that the original array can be garbage-collected.
fromArray :: Ix i => Array i a -> Seq a

-- | &lt;math&gt;. <tt>replicate n x</tt> is a sequence consisting of
gr<tt>n</tt> copies of <tt>x</tt>.
replicate :: Int -> a -> Seq a

-- | <a>replicateA</a> is an <a>Applicative</a> version of
gr<a>replicate</a>, and makes &lt;math&gt; calls to <a>liftA2</a> and
gr<a>pure</a>.
gr
gr<pre>
grreplicateA n x = sequenceA (replicate n x)
gr</pre>
replicateA :: Applicative f => Int -> f a -> f (Seq a)

-- | <a>replicateM</a> is a sequence counterpart of <a>replicateM</a>.
gr
gr<pre>
grreplicateM n x = sequence (replicate n x)
gr</pre>
gr
grFor <tt>base &gt;= 4.8.0</tt> and <tt>containers &gt;= 0.5.11</tt>,
gr<a>replicateM</a> is a synonym for <a>replicateA</a>.
replicateM :: Applicative m => Int -> m a -> m (Seq a)

-- | <i>O(</i>log<i> k)</i>. <tt><a>cycleTaking</a> k xs</tt> forms a
grsequence of length <tt>k</tt> by repeatedly concatenating <tt>xs</tt>
grwith itself. <tt>xs</tt> may only be empty if <tt>k</tt> is 0.
gr
gr<pre>
grcycleTaking k = fromList . take k . cycle . toList
gr</pre>
cycleTaking :: Int -> Seq a -> Seq a

-- | &lt;math&gt;. Constructs a sequence by repeated application of a
grfunction to a seed value.
gr
gr<pre>
griterateN n f x = fromList (Prelude.take n (Prelude.iterate f x))
gr</pre>
iterateN :: Int -> (a -> a) -> a -> Seq a

-- | Builds a sequence from a seed value. Takes time linear in the number
grof generated elements. <i>WARNING:</i> If the number of generated
grelements is infinite, this method will not terminate.
unfoldr :: (b -> Maybe (a, b)) -> b -> Seq a

-- | <tt><a>unfoldl</a> f x</tt> is equivalent to <tt><a>reverse</a>
gr(<a>unfoldr</a> (<a>fmap</a> swap . f) x)</tt>.
unfoldl :: (b -> Maybe (b, a)) -> b -> Seq a

-- | &lt;math&gt;. Is this the empty sequence?
null :: Seq a -> Bool

-- | &lt;math&gt;. The number of elements in the sequence.
length :: Seq a -> Int

-- | View of the left end of a sequence.
data ViewL a

-- | empty sequence
EmptyL :: ViewL a

-- | leftmost element and the rest of the sequence
(:<) :: a -> Seq a -> ViewL a

-- | &lt;math&gt;. Analyse the left end of a sequence.
viewl :: Seq a -> ViewL a

-- | View of the right end of a sequence.
data ViewR a

-- | empty sequence
EmptyR :: ViewR a

-- | the sequence minus the rightmost element, and the rightmost element
(:>) :: Seq a -> a -> ViewR a

-- | &lt;math&gt;. Analyse the right end of a sequence.
viewr :: Seq a -> ViewR a

-- | <a>scanl</a> is similar to <a>foldl</a>, but returns a sequence of
grreduced values from the left:
gr
gr<pre>
grscanl f z (fromList [x1, x2, ...]) = fromList [z, z `f` x1, (z `f` x1) `f` x2, ...]
gr</pre>
scanl :: (a -> b -> a) -> a -> Seq b -> Seq a

-- | <a>scanl1</a> is a variant of <a>scanl</a> that has no starting value
grargument:
gr
gr<pre>
grscanl1 f (fromList [x1, x2, ...]) = fromList [x1, x1 `f` x2, ...]
gr</pre>
scanl1 :: (a -> a -> a) -> Seq a -> Seq a

-- | <a>scanr</a> is the right-to-left dual of <a>scanl</a>.
scanr :: (a -> b -> b) -> b -> Seq a -> Seq b

-- | <a>scanr1</a> is a variant of <a>scanr</a> that has no starting value
grargument.
scanr1 :: (a -> a -> a) -> Seq a -> Seq a

-- | &lt;math&gt;. Returns a sequence of all suffixes of this sequence,
grlongest first. For example,
gr
gr<pre>
grtails (fromList "abc") = fromList [fromList "abc", fromList "bc", fromList "c", fromList ""]
gr</pre>
gr
grEvaluating the &lt;math&gt;th suffix takes &lt;math&gt;, but
grevaluating every suffix in the sequence takes &lt;math&gt; due to
grsharing.
tails :: Seq a -> Seq (Seq a)

-- | &lt;math&gt;. Returns a sequence of all prefixes of this sequence,
grshortest first. For example,
gr
gr<pre>
grinits (fromList "abc") = fromList [fromList "", fromList "a", fromList "ab", fromList "abc"]
gr</pre>
gr
grEvaluating the &lt;math&gt;th prefix takes &lt;math&gt;, but
grevaluating every prefix in the sequence takes &lt;math&gt; due to
grsharing.
inits :: Seq a -> Seq (Seq a)

-- | &lt;math&gt;. <tt>chunksOf c xs</tt> splits <tt>xs</tt> into chunks of
grsize <tt>c&gt;0</tt>. If <tt>c</tt> does not divide the length of
gr<tt>xs</tt> evenly, then the last element of the result will be short.
gr
grSide note: the given performance bound is missing some messy terms
grthat only really affect edge cases. Performance degrades smoothly from
gr&lt;math&gt; (for &lt;math&gt;) to &lt;math&gt; (for &lt;math&gt;).
grThe true bound is more like &lt;math&gt;
chunksOf :: Int -> Seq a -> Seq (Seq a)

-- | &lt;math&gt; where &lt;math&gt; is the prefix length.
gr<a>takeWhileL</a>, applied to a predicate <tt>p</tt> and a sequence
gr<tt>xs</tt>, returns the longest prefix (possibly empty) of
gr<tt>xs</tt> of elements that satisfy <tt>p</tt>.
takeWhileL :: (a -> Bool) -> Seq a -> Seq a

-- | &lt;math&gt; where &lt;math&gt; is the suffix length.
gr<a>takeWhileR</a>, applied to a predicate <tt>p</tt> and a sequence
gr<tt>xs</tt>, returns the longest suffix (possibly empty) of
gr<tt>xs</tt> of elements that satisfy <tt>p</tt>.
gr
gr<tt><a>takeWhileR</a> p xs</tt> is equivalent to <tt><a>reverse</a>
gr(<a>takeWhileL</a> p (<a>reverse</a> xs))</tt>.
takeWhileR :: (a -> Bool) -> Seq a -> Seq a

-- | &lt;math&gt; where &lt;math&gt; is the prefix length.
gr<tt><a>dropWhileL</a> p xs</tt> returns the suffix remaining after
gr<tt><a>takeWhileL</a> p xs</tt>.
dropWhileL :: (a -> Bool) -> Seq a -> Seq a

-- | &lt;math&gt; where &lt;math&gt; is the suffix length.
gr<tt><a>dropWhileR</a> p xs</tt> returns the prefix remaining after
gr<tt><a>takeWhileR</a> p xs</tt>.
gr
gr<tt><a>dropWhileR</a> p xs</tt> is equivalent to <tt><a>reverse</a>
gr(<a>dropWhileL</a> p (<a>reverse</a> xs))</tt>.
dropWhileR :: (a -> Bool) -> Seq a -> Seq a

-- | &lt;math&gt; where &lt;math&gt; is the prefix length. <a>spanl</a>,
grapplied to a predicate <tt>p</tt> and a sequence <tt>xs</tt>, returns
gra pair whose first element is the longest prefix (possibly empty) of
gr<tt>xs</tt> of elements that satisfy <tt>p</tt> and the second element
gris the remainder of the sequence.
spanl :: (a -> Bool) -> Seq a -> (Seq a, Seq a)

-- | &lt;math&gt; where &lt;math&gt; is the suffix length. <a>spanr</a>,
grapplied to a predicate <tt>p</tt> and a sequence <tt>xs</tt>, returns
gra pair whose <i>first</i> element is the longest <i>suffix</i>
gr(possibly empty) of <tt>xs</tt> of elements that satisfy <tt>p</tt>
grand the second element is the remainder of the sequence.
spanr :: (a -> Bool) -> Seq a -> (Seq a, Seq a)

-- | &lt;math&gt; where &lt;math&gt; is the breakpoint index.
gr<a>breakl</a>, applied to a predicate <tt>p</tt> and a sequence
gr<tt>xs</tt>, returns a pair whose first element is the longest prefix
gr(possibly empty) of <tt>xs</tt> of elements that <i>do not satisfy</i>
gr<tt>p</tt> and the second element is the remainder of the sequence.
gr
gr<tt><a>breakl</a> p</tt> is equivalent to <tt><a>spanl</a> (not .
grp)</tt>.
breakl :: (a -> Bool) -> Seq a -> (Seq a, Seq a)

-- | <tt><a>breakr</a> p</tt> is equivalent to <tt><a>spanr</a> (not .
grp)</tt>.
breakr :: (a -> Bool) -> Seq a -> (Seq a, Seq a)

-- | &lt;math&gt;. The <a>partition</a> function takes a predicate
gr<tt>p</tt> and a sequence <tt>xs</tt> and returns sequences of those
grelements which do and do not satisfy the predicate.
partition :: (a -> Bool) -> Seq a -> (Seq a, Seq a)

-- | &lt;math&gt;. The <a>filter</a> function takes a predicate <tt>p</tt>
grand a sequence <tt>xs</tt> and returns a sequence of those elements
grwhich satisfy the predicate.
filter :: (a -> Bool) -> Seq a -> Seq a

-- | &lt;math&gt;. <a>sort</a> sorts the specified <a>Seq</a> by the
grnatural ordering of its elements. The sort is stable. If stability is
grnot required, <a>unstableSort</a> can be slightly faster.
sort :: Ord a => Seq a -> Seq a

-- | &lt;math&gt;. <a>sortBy</a> sorts the specified <a>Seq</a> according
grto the specified comparator. The sort is stable. If stability is not
grrequired, <a>unstableSortBy</a> can be slightly faster.
sortBy :: (a -> a -> Ordering) -> Seq a -> Seq a

-- | &lt;math&gt;. <a>sortOn</a> sorts the specified <a>Seq</a> by
grcomparing the results of a key function applied to each element.
gr<tt><a>sortOn</a> f</tt> is equivalent to <tt><a>sortBy</a>
gr(<a>compare</a> `<a>on</a>` f)</tt>, but has the performance advantage
grof only evaluating <tt>f</tt> once for each element in the input list.
grThis is called the decorate-sort-undecorate paradigm, or Schwartzian
grtransform.
gr
grAn example of using <a>sortOn</a> might be to sort a <a>Seq</a> of
grstrings according to their length:
gr
gr<pre>
grsortOn length (fromList ["alligator", "monkey", "zebra"]) == fromList ["zebra", "monkey", "alligator"]
gr</pre>
gr
grIf, instead, <a>sortBy</a> had been used, <a>length</a> would be
grevaluated on every comparison, giving &lt;math&gt; evaluations, rather
grthan &lt;math&gt;.
gr
grIf <tt>f</tt> is very cheap (for example a record selector, or
gr<a>fst</a>), <tt><a>sortBy</a> (<a>compare</a> `<a>on</a>` f)</tt>
grwill be faster than <tt><a>sortOn</a> f</tt>.
sortOn :: Ord b => (a -> b) -> Seq a -> Seq a

-- | &lt;math&gt;. <a>unstableSort</a> sorts the specified <a>Seq</a> by
grthe natural ordering of its elements, but the sort is not stable. This
gralgorithm is frequently faster and uses less memory than <a>sort</a>.
unstableSort :: Ord a => Seq a -> Seq a

-- | &lt;math&gt;. A generalization of <a>unstableSort</a>,
gr<a>unstableSortBy</a> takes an arbitrary comparator and sorts the
grspecified sequence. The sort is not stable. This algorithm is
grfrequently faster and uses less memory than <a>sortBy</a>.
unstableSortBy :: (a -> a -> Ordering) -> Seq a -> Seq a

-- | &lt;math&gt;. <a>unstableSortOn</a> sorts the specified <a>Seq</a> by
grcomparing the results of a key function applied to each element.
gr<tt><a>unstableSortOn</a> f</tt> is equivalent to
gr<tt><a>unstableSortBy</a> (<a>compare</a> `<a>on</a>` f)</tt>, but has
grthe performance advantage of only evaluating <tt>f</tt> once for each
grelement in the input list. This is called the decorate-sort-undecorate
grparadigm, or Schwartzian transform.
gr
grAn example of using <a>unstableSortOn</a> might be to sort a
gr<a>Seq</a> of strings according to their length:
gr
gr<pre>
grunstableSortOn length (fromList ["alligator", "monkey", "zebra"]) == fromList ["zebra", "monkey", "alligator"]
gr</pre>
gr
grIf, instead, <a>unstableSortBy</a> had been used, <a>length</a> would
grbe evaluated on every comparison, giving &lt;math&gt; evaluations,
grrather than &lt;math&gt;.
gr
grIf <tt>f</tt> is very cheap (for example a record selector, or
gr<a>fst</a>), <tt><a>unstableSortBy</a> (<a>compare</a> `<a>on</a>`
grf)</tt> will be faster than <tt><a>unstableSortOn</a> f</tt>.
unstableSortOn :: Ord b => (a -> b) -> Seq a -> Seq a

-- | &lt;math&gt;. The element at the specified position, counting from 0.
grIf the specified position is negative or at least the length of the
grsequence, <a>lookup</a> returns <a>Nothing</a>.
gr
gr<pre>
gr0 &lt;= i &lt; length xs ==&gt; lookup i xs == Just (toList xs !! i)
gr</pre>
gr
gr<pre>
gri &lt; 0 || i &gt;= length xs ==&gt; lookup i xs = Nothing
gr</pre>
gr
grUnlike <a>index</a>, this can be used to retrieve an element without
grforcing it. For example, to insert the fifth element of a sequence
gr<tt>xs</tt> into a <a>Map</a> <tt>m</tt> at key <tt>k</tt>, you could
gruse
gr
gr<pre>
grcase lookup 5 xs of
gr  Nothing -&gt; m
gr  Just x -&gt; <a>insert</a> k x m
gr</pre>
lookup :: Int -> Seq a -> Maybe a

-- | &lt;math&gt;. A flipped, infix version of <a>lookup</a>.
(!?) :: Seq a -> Int -> Maybe a

-- | &lt;math&gt;. The element at the specified position, counting from 0.
grThe argument should thus be a non-negative integer less than the size
grof the sequence. If the position is out of range, <a>index</a> fails
grwith an error.
gr
gr<pre>
grxs `index` i = toList xs !! i
gr</pre>
gr
grCaution: <a>index</a> necessarily delays retrieving the requested
grelement until the result is forced. It can therefore lead to a space
grleak if the result is stored, unforced, in another structure. To
grretrieve an element immediately without forcing it, use <a>lookup</a>
gror '(!?)'.
index :: Seq a -> Int -> a

-- | &lt;math&gt;. Update the element at the specified position. If the
grposition is out of range, the original sequence is returned.
gr<a>adjust</a> can lead to poor performance and even memory leaks,
grbecause it does not force the new value before installing it in the
grsequence. <a>adjust'</a> should usually be preferred.
adjust :: (a -> a) -> Int -> Seq a -> Seq a

-- | &lt;math&gt;. Update the element at the specified position. If the
grposition is out of range, the original sequence is returned. The new
grvalue is forced before it is installed in the sequence.
gr
gr<pre>
gradjust' f i xs =
gr case xs !? i of
gr   Nothing -&gt; xs
gr   Just x -&gt; let !x' = f x
gr             in update i x' xs
gr</pre>
adjust' :: forall a. (a -> a) -> Int -> Seq a -> Seq a

-- | &lt;math&gt;. Replace the element at the specified position. If the
grposition is out of range, the original sequence is returned.
update :: Int -> a -> Seq a -> Seq a

-- | &lt;math&gt;. The first <tt>i</tt> elements of a sequence. If
gr<tt>i</tt> is negative, <tt><a>take</a> i s</tt> yields the empty
grsequence. If the sequence contains fewer than <tt>i</tt> elements, the
grwhole sequence is returned.
take :: Int -> Seq a -> Seq a

-- | &lt;math&gt;. Elements of a sequence after the first <tt>i</tt>. If
gr<tt>i</tt> is negative, <tt><a>drop</a> i s</tt> yields the whole
grsequence. If the sequence contains fewer than <tt>i</tt> elements, the
grempty sequence is returned.
drop :: Int -> Seq a -> Seq a

-- | &lt;math&gt;. <tt><a>insertAt</a> i x xs</tt> inserts <tt>x</tt> into
gr<tt>xs</tt> at the index <tt>i</tt>, shifting the rest of the sequence
grover.
gr
gr<pre>
grinsertAt 2 x (fromList [a,b,c,d]) = fromList [a,b,x,c,d]
grinsertAt 4 x (fromList [a,b,c,d]) = insertAt 10 x (fromList [a,b,c,d])
gr                                  = fromList [a,b,c,d,x]
gr</pre>
gr
gr<pre>
grinsertAt i x xs = take i xs &gt;&lt; singleton x &gt;&lt; drop i xs
gr</pre>
insertAt :: Int -> a -> Seq a -> Seq a

-- | &lt;math&gt;. Delete the element of a sequence at a given index.
grReturn the original sequence if the index is out of range.
gr
gr<pre>
grdeleteAt 2 [a,b,c,d] = [a,b,d]
grdeleteAt 4 [a,b,c,d] = deleteAt (-1) [a,b,c,d] = [a,b,c,d]
gr</pre>
deleteAt :: Int -> Seq a -> Seq a

-- | &lt;math&gt;. Split a sequence at a given position. <tt><a>splitAt</a>
gri s = (<a>take</a> i s, <a>drop</a> i s)</tt>.
splitAt :: Int -> Seq a -> (Seq a, Seq a)

-- | <a>elemIndexL</a> finds the leftmost index of the specified element,
grif it is present, and otherwise <a>Nothing</a>.
elemIndexL :: Eq a => a -> Seq a -> Maybe Int

-- | <a>elemIndicesL</a> finds the indices of the specified element, from
grleft to right (i.e. in ascending order).
elemIndicesL :: Eq a => a -> Seq a -> [Int]

-- | <a>elemIndexR</a> finds the rightmost index of the specified element,
grif it is present, and otherwise <a>Nothing</a>.
elemIndexR :: Eq a => a -> Seq a -> Maybe Int

-- | <a>elemIndicesR</a> finds the indices of the specified element, from
grright to left (i.e. in descending order).
elemIndicesR :: Eq a => a -> Seq a -> [Int]

-- | <tt><a>findIndexL</a> p xs</tt> finds the index of the leftmost
grelement that satisfies <tt>p</tt>, if any exist.
findIndexL :: (a -> Bool) -> Seq a -> Maybe Int

-- | <tt><a>findIndicesL</a> p</tt> finds all indices of elements that
grsatisfy <tt>p</tt>, in ascending order.
findIndicesL :: (a -> Bool) -> Seq a -> [Int]

-- | <tt><a>findIndexR</a> p xs</tt> finds the index of the rightmost
grelement that satisfies <tt>p</tt>, if any exist.
findIndexR :: (a -> Bool) -> Seq a -> Maybe Int

-- | <tt><a>findIndicesR</a> p</tt> finds all indices of elements that
grsatisfy <tt>p</tt>, in descending order.
findIndicesR :: (a -> Bool) -> Seq a -> [Int]
foldMapWithIndex :: Monoid m => (Int -> a -> m) -> Seq a -> m

-- | <a>foldlWithIndex</a> is a version of <a>foldl</a> that also provides
graccess to the index of each element.
foldlWithIndex :: (b -> Int -> a -> b) -> b -> Seq a -> b

-- | <a>foldrWithIndex</a> is a version of <a>foldr</a> that also provides
graccess to the index of each element.
foldrWithIndex :: (Int -> a -> b -> b) -> b -> Seq a -> b

-- | A generalization of <a>fmap</a>, <a>mapWithIndex</a> takes a mapping
grfunction that also depends on the element's index, and applies it to
grevery element in the sequence.
mapWithIndex :: (Int -> a -> b) -> Seq a -> Seq b

-- | <a>traverseWithIndex</a> is a version of <a>traverse</a> that also
groffers access to the index of each element.
traverseWithIndex :: Applicative f => (Int -> a -> f b) -> Seq a -> f (Seq b)

-- | &lt;math&gt;. The reverse of a sequence.
reverse :: Seq a -> Seq a

-- | &lt;math&gt;. Intersperse an element between the elements of a
grsequence.
gr
gr<pre>
grintersperse a empty = empty
grintersperse a (singleton x) = singleton x
grintersperse a (fromList [x,y]) = fromList [x,a,y]
grintersperse a (fromList [x,y,z]) = fromList [x,a,y,a,z]
gr</pre>
intersperse :: a -> Seq a -> Seq a

-- | &lt;math&gt;. <a>zip</a> takes two sequences and returns a sequence of
grcorresponding pairs. If one input is short, excess elements are
grdiscarded from the right end of the longer sequence.
zip :: Seq a -> Seq b -> Seq (a, b)

-- | &lt;math&gt;. <a>zipWith</a> generalizes <a>zip</a> by zipping with
grthe function given as the first argument, instead of a tupling
grfunction. For example, <tt>zipWith (+)</tt> is applied to two
grsequences to take the sequence of corresponding sums.
zipWith :: (a -> b -> c) -> Seq a -> Seq b -> Seq c

-- | &lt;math&gt;. <a>zip3</a> takes three sequences and returns a sequence
grof triples, analogous to <a>zip</a>.
zip3 :: Seq a -> Seq b -> Seq c -> Seq (a, b, c)

-- | &lt;math&gt;. <a>zipWith3</a> takes a function which combines three
grelements, as well as three sequences and returns a sequence of their
grpoint-wise combinations, analogous to <a>zipWith</a>.
zipWith3 :: (a -> b -> c -> d) -> Seq a -> Seq b -> Seq c -> Seq d

-- | &lt;math&gt;. <a>zip4</a> takes four sequences and returns a sequence
grof quadruples, analogous to <a>zip</a>.
zip4 :: Seq a -> Seq b -> Seq c -> Seq d -> Seq (a, b, c, d)

-- | &lt;math&gt;. <a>zipWith4</a> takes a function which combines four
grelements, as well as four sequences and returns a sequence of their
grpoint-wise combinations, analogous to <a>zipWith</a>.
zipWith4 :: (a -> b -> c -> d -> e) -> Seq a -> Seq b -> Seq c -> Seq d -> Seq e

-- | Unzip a sequence of pairs.
gr
gr<pre>
grunzip ps = ps `<tt>seq'</tt> (<a>fmap</a> <a>fst</a> ps) (<a>fmap</a> <a>snd</a> ps)
gr</pre>
gr
grExample:
gr
gr<pre>
grunzip $ fromList [(1,"a"), (2,"b"), (3,"c")] =
gr  (fromList [1,2,3], fromList ["a", "b", "c"])
gr</pre>
gr
grSee the note about efficiency at <a>unzipWith</a>.
unzip :: Seq (a, b) -> (Seq a, Seq b)

-- | &lt;math&gt;. Unzip a sequence using a function to divide elements.
gr
gr<pre>
grunzipWith f xs == <a>unzip</a> (<a>fmap</a> f xs)
gr</pre>
gr
grEfficiency note:
gr
gr<tt>unzipWith</tt> produces its two results in lockstep. If you
grcalculate <tt> unzipWith f xs </tt> and fully force <i>either</i> of
grthe results, then the entire structure of the <i>other</i> one will be
grbuilt as well. This behavior allows the garbage collector to collect
greach calculated pair component as soon as it dies, without having to
grwait for its mate to die. If you do not need this behavior, you may be
grbetter off simply calculating the sequence of pairs and using
gr<a>fmap</a> to extract each component sequence.
unzipWith :: (a -> (b, c)) -> Seq a -> (Seq b, Seq c)


-- | Multi-way trees (<i>aka</i> rose trees) and forests.
module Data.Tree

-- | Multi-way trees, also known as <i>rose trees</i>.
data Tree a
Node :: a -> Forest a -> Tree a

-- | label value
[rootLabel] :: Tree a -> a

-- | zero or more child trees
[subForest] :: Tree a -> Forest a
type Forest a = [Tree a]

-- | Neat 2-dimensional drawing of a tree.
drawTree :: Tree String -> String

-- | Neat 2-dimensional drawing of a forest.
drawForest :: Forest String -> String

-- | The elements of a tree in pre-order.
flatten :: Tree a -> [a]

-- | Lists of nodes at each level of the tree.
levels :: Tree a -> [[a]]

-- | Catamorphism on trees.
foldTree :: (a -> [b] -> b) -> Tree a -> b

-- | Build a tree from a seed value
unfoldTree :: (b -> (a, [b])) -> b -> Tree a

-- | Build a forest from a list of seed values
unfoldForest :: (b -> (a, [b])) -> [b] -> Forest a

-- | Monadic tree builder, in depth-first order
unfoldTreeM :: Monad m => (b -> m (a, [b])) -> b -> m (Tree a)

-- | Monadic forest builder, in depth-first order
unfoldForestM :: Monad m => (b -> m (a, [b])) -> [b] -> m (Forest a)

-- | Monadic tree builder, in breadth-first order, using an algorithm
gradapted from <i>Breadth-First Numbering: Lessons from a Small Exercise
grin Algorithm Design</i>, by Chris Okasaki, <i>ICFP'00</i>.
unfoldTreeM_BF :: Monad m => (b -> m (a, [b])) -> b -> m (Tree a)

-- | Monadic forest builder, in breadth-first order, using an algorithm
gradapted from <i>Breadth-First Numbering: Lessons from a Small Exercise
grin Algorithm Design</i>, by Chris Okasaki, <i>ICFP'00</i>.
unfoldForestM_BF :: Monad m => (b -> m (a, [b])) -> [b] -> m (Forest a)
instance GHC.Generics.Generic1 Data.Tree.Tree
instance GHC.Generics.Generic (Data.Tree.Tree a)
instance Data.Data.Data a => Data.Data.Data (Data.Tree.Tree a)
instance GHC.Show.Show a => GHC.Show.Show (Data.Tree.Tree a)
instance GHC.Read.Read a => GHC.Read.Read (Data.Tree.Tree a)
instance GHC.Classes.Eq a => GHC.Classes.Eq (Data.Tree.Tree a)
instance Data.Functor.Classes.Eq1 Data.Tree.Tree
instance Data.Functor.Classes.Ord1 Data.Tree.Tree
instance Data.Functor.Classes.Show1 Data.Tree.Tree
instance Data.Functor.Classes.Read1 Data.Tree.Tree
instance GHC.Base.Functor Data.Tree.Tree
instance GHC.Base.Applicative Data.Tree.Tree
instance GHC.Base.Monad Data.Tree.Tree
instance Control.Monad.Fix.MonadFix Data.Tree.Tree
instance Data.Traversable.Traversable Data.Tree.Tree
instance Data.Foldable.Foldable Data.Tree.Tree
instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Data.Tree.Tree a)
instance Control.Monad.Zip.MonadZip Data.Tree.Tree


-- | A version of the graph algorithms described in:
gr
gr<i>Structuring Depth-First Search Algorithms in Haskell</i>, by David
grKing and John Launchbury.
module Data.Graph

-- | The strongly connected components of a directed graph, topologically
grsorted.
stronglyConnComp :: Ord key => [(node, key, [key])] -> [SCC node]

-- | The strongly connected components of a directed graph, topologically
grsorted. The function is the same as <a>stronglyConnComp</a>, except
grthat all the information about each node retained. This interface is
grused when you expect to apply <a>SCC</a> to (some of) the result of
gr<a>SCC</a>, so you don't want to lose the dependency information.
stronglyConnCompR :: Ord key => [(node, key, [key])] -> [SCC (node, key, [key])]

-- | Strongly connected component.
data SCC vertex

-- | A single vertex that is not in any cycle.
AcyclicSCC :: vertex -> SCC vertex

-- | A maximal set of mutually reachable vertices.
CyclicSCC :: [vertex] -> SCC vertex

-- | The vertices of a strongly connected component.
flattenSCC :: SCC vertex -> [vertex]

-- | The vertices of a list of strongly connected components.
flattenSCCs :: [SCC a] -> [a]

-- | Adjacency list representation of a graph, mapping each vertex to its
grlist of successors.
type Graph = Table [Vertex]

-- | Table indexed by a contiguous set of vertices.
type Table a = Array Vertex a

-- | The bounds of a <a>Table</a>.
type Bounds = (Vertex, Vertex)

-- | An edge from the first vertex to the second.
type Edge = (Vertex, Vertex)

-- | Abstract representation of vertices.
type Vertex = Int

-- | Build a graph from a list of nodes uniquely identified by keys, with a
grlist of keys of nodes this node should have edges to. The out-list may
grcontain keys that don't correspond to nodes of the graph; they are
grignored.
graphFromEdges :: Ord key => [(node, key, [key])] -> (Graph, Vertex -> (node, key, [key]), key -> Maybe Vertex)

-- | Identical to <a>graphFromEdges</a>, except that the return value does
grnot include the function which maps keys to vertices. This version of
gr<a>graphFromEdges</a> is for backwards compatibility.
graphFromEdges' :: Ord key => [(node, key, [key])] -> (Graph, Vertex -> (node, key, [key]))

-- | Build a graph from a list of edges.
buildG :: Bounds -> [Edge] -> Graph

-- | The graph obtained by reversing all edges.
transposeG :: Graph -> Graph

-- | All vertices of a graph.
vertices :: Graph -> [Vertex]

-- | All edges of a graph.
edges :: Graph -> [Edge]

-- | A table of the count of edges from each node.
outdegree :: Graph -> Table Int

-- | A table of the count of edges into each node.
indegree :: Graph -> Table Int

-- | A spanning forest of the part of the graph reachable from the listed
grvertices, obtained from a depth-first search of the graph starting at
greach of the listed vertices in order.
dfs :: Graph -> [Vertex] -> Forest Vertex

-- | A spanning forest of the graph, obtained from a depth-first search of
grthe graph starting from each vertex in an unspecified order.
dff :: Graph -> Forest Vertex

-- | A topological sort of the graph. The order is partially specified by
grthe condition that a vertex <i>i</i> precedes <i>j</i> whenever
gr<i>j</i> is reachable from <i>i</i> but not vice versa.
topSort :: Graph -> [Vertex]

-- | The connected components of a graph. Two vertices are connected if
grthere is a path between them, traversing edges in either direction.
components :: Graph -> Forest Vertex

-- | The strongly connected components of a graph.
scc :: Graph -> Forest Vertex

-- | The biconnected components of a graph. An undirected graph is
grbiconnected if the deletion of any vertex leaves it connected.
bcc :: Graph -> Forest [Vertex]

-- | A list of vertices reachable from a given vertex.
reachable :: Graph -> Vertex -> [Vertex]

-- | Is the second vertex reachable from the first?
path :: Graph -> Vertex -> Vertex -> Bool
type Forest a = [Tree a]

-- | Multi-way trees, also known as <i>rose trees</i>.
data Tree a
Node :: a -> Forest a -> Tree a
instance GHC.Read.Read vertex => GHC.Read.Read (Data.Graph.SCC vertex)
instance GHC.Show.Show vertex => GHC.Show.Show (Data.Graph.SCC vertex)
instance GHC.Classes.Eq vertex => GHC.Classes.Eq (Data.Graph.SCC vertex)
instance Data.Data.Data vertex => Data.Data.Data (Data.Graph.SCC vertex)
instance GHC.Generics.Generic1 Data.Graph.SCC
instance GHC.Generics.Generic (Data.Graph.SCC vertex)
instance GHC.Base.Monad (Data.Graph.SetM s)
instance GHC.Base.Functor (Data.Graph.SetM s)
instance GHC.Base.Applicative (Data.Graph.SetM s)
instance Data.Functor.Classes.Eq1 Data.Graph.SCC
instance Data.Functor.Classes.Show1 Data.Graph.SCC
instance Data.Functor.Classes.Read1 Data.Graph.SCC
instance Data.Foldable.Foldable Data.Graph.SCC
instance Data.Traversable.Traversable Data.Graph.SCC
instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Data.Graph.SCC a)
instance GHC.Base.Functor Data.Graph.SCC


-- | <h1>WARNING</h1>
gr
grThis module is considered <b>internal</b>.
gr
grThe Package Versioning Policy <b>does not apply</b>.
gr
grThis contents of this module may change <b>in any way whatsoever</b>
grand <b>without any warning</b> between minor versions of this package.
gr
grAuthors importing this module are expected to track development
grclosely.
gr
gr<h1>Description</h1>
gr
grAn efficient implementation of maps from keys to values
gr(dictionaries).
gr
grSince many function names (but not the type name) clash with
gr<a>Prelude</a> names, this module is usually imported
gr<tt>qualified</tt>, e.g.
gr
gr<pre>
grimport Data.Map (Map)
grimport qualified Data.Map as Map
gr</pre>
gr
grThe implementation of <a>Map</a> is based on <i>size balanced</i>
grbinary trees (or trees of <i>bounded balance</i>) as described by:
gr
gr<ul>
gr<li>Stephen Adams, "<i>Efficient sets: a balancing act</i>", Journal
grof Functional Programming 3(4):553-562, October 1993,
gr<a>http://www.swiss.ai.mit.edu/~adams/BB/</a>.</li>
gr<li>J. Nievergelt and E.M. Reingold, "<i>Binary search trees of
grbounded balance</i>", SIAM journal of computing 2(1), March 1973.</li>
gr</ul>
gr
grBounds for <a>union</a>, <a>intersection</a>, and <a>difference</a>
grare as given by
gr
gr<ul>
gr<li>Guy Blelloch, Daniel Ferizovic, and Yihan Sun, "<i>Just Join for
grParallel Ordered Sets</i>",
gr<a>https://arxiv.org/abs/1602.02120v3</a>.</li>
gr</ul>
gr
grNote that the implementation is <i>left-biased</i> -- the elements of
gra first argument are always preferred to the second, for example in
gr<a>union</a> or <a>insert</a>.
gr
grOperation comments contain the operation time complexity in the Big-O
grnotation <a>http://en.wikipedia.org/wiki/Big_O_notation</a>.
module Data.Map.Internal

-- | A Map from keys <tt>k</tt> to values <tt>a</tt>.
data Map k a
Bin :: {-# UNPACK #-} !Size -> !k -> a -> !(Map k a) -> !(Map k a) -> Map k a
Tip :: Map k a
type Size = Int

-- | <i>O(log n)</i>. Find the value at a key. Calls <a>error</a> when the
grelement can not be found.
gr
gr<pre>
grfromList [(5,'a'), (3,'b')] ! 1    Error: element not in the map
grfromList [(5,'a'), (3,'b')] ! 5 == 'a'
gr</pre>
(!) :: Ord k => Map k a -> k -> a
infixl 9 !

-- | <i>O(log n)</i>. Find the value at a key. Returns <a>Nothing</a> when
grthe element can not be found.
gr
gr<pre>
grfromList [(5, 'a'), (3, 'b')] !? 1 == Nothing
gr</pre>
gr
gr<pre>
grfromList [(5, 'a'), (3, 'b')] !? 5 == Just 'a'
gr</pre>
(!?) :: Ord k => Map k a -> k -> Maybe a
infixl 9 !?

-- | Same as <a>difference</a>.
(\\) :: Ord k => Map k a -> Map k b -> Map k a
infixl 9 \\

-- | <i>O(1)</i>. Is the map empty?
gr
gr<pre>
grData.Map.null (empty)           == True
grData.Map.null (singleton 1 'a') == False
gr</pre>
null :: Map k a -> Bool

-- | <i>O(1)</i>. The number of elements in the map.
gr
gr<pre>
grsize empty                                   == 0
grsize (singleton 1 'a')                       == 1
grsize (fromList([(1,'a'), (2,'c'), (3,'b')])) == 3
gr</pre>
size :: Map k a -> Int

-- | <i>O(log n)</i>. Is the key a member of the map? See also
gr<a>notMember</a>.
gr
gr<pre>
grmember 5 (fromList [(5,'a'), (3,'b')]) == True
grmember 1 (fromList [(5,'a'), (3,'b')]) == False
gr</pre>
member :: Ord k => k -> Map k a -> Bool

-- | <i>O(log n)</i>. Is the key not a member of the map? See also
gr<a>member</a>.
gr
gr<pre>
grnotMember 5 (fromList [(5,'a'), (3,'b')]) == False
grnotMember 1 (fromList [(5,'a'), (3,'b')]) == True
gr</pre>
notMember :: Ord k => k -> Map k a -> Bool

-- | <i>O(log n)</i>. Lookup the value at a key in the map.
gr
grThe function will return the corresponding value as <tt>(<a>Just</a>
grvalue)</tt>, or <a>Nothing</a> if the key isn't in the map.
gr
grAn example of using <tt>lookup</tt>:
gr
gr<pre>
grimport Prelude hiding (lookup)
grimport Data.Map
gr
gremployeeDept = fromList([("John","Sales"), ("Bob","IT")])
grdeptCountry = fromList([("IT","USA"), ("Sales","France")])
grcountryCurrency = fromList([("USA", "Dollar"), ("France", "Euro")])
gr
gremployeeCurrency :: String -&gt; Maybe String
gremployeeCurrency name = do
gr    dept &lt;- lookup name employeeDept
gr    country &lt;- lookup dept deptCountry
gr    lookup country countryCurrency
gr
grmain = do
gr    putStrLn $ "John's currency: " ++ (show (employeeCurrency "John"))
gr    putStrLn $ "Pete's currency: " ++ (show (employeeCurrency "Pete"))
gr</pre>
gr
grThe output of this program:
gr
gr<pre>
grJohn's currency: Just "Euro"
grPete's currency: Nothing
gr</pre>
lookup :: Ord k => k -> Map k a -> Maybe a

-- | <i>O(log n)</i>. The expression <tt>(<a>findWithDefault</a> def k
grmap)</tt> returns the value at key <tt>k</tt> or returns default value
gr<tt>def</tt> when the key is not in the map.
gr
gr<pre>
grfindWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x'
grfindWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a'
gr</pre>
findWithDefault :: Ord k => a -> k -> Map k a -> a

-- | <i>O(log n)</i>. Find largest key smaller than the given one and
grreturn the corresponding (key, value) pair.
gr
gr<pre>
grlookupLT 3 (fromList [(3,'a'), (5,'b')]) == Nothing
grlookupLT 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
gr</pre>
lookupLT :: Ord k => k -> Map k v -> Maybe (k, v)

-- | <i>O(log n)</i>. Find smallest key greater than the given one and
grreturn the corresponding (key, value) pair.
gr
gr<pre>
grlookupGT 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
grlookupGT 5 (fromList [(3,'a'), (5,'b')]) == Nothing
gr</pre>
lookupGT :: Ord k => k -> Map k v -> Maybe (k, v)

-- | <i>O(log n)</i>. Find largest key smaller or equal to the given one
grand return the corresponding (key, value) pair.
gr
gr<pre>
grlookupLE 2 (fromList [(3,'a'), (5,'b')]) == Nothing
grlookupLE 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
grlookupLE 5 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
gr</pre>
lookupLE :: Ord k => k -> Map k v -> Maybe (k, v)

-- | <i>O(log n)</i>. Find smallest key greater or equal to the given one
grand return the corresponding (key, value) pair.
gr
gr<pre>
grlookupGE 3 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
grlookupGE 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
grlookupGE 6 (fromList [(3,'a'), (5,'b')]) == Nothing
gr</pre>
lookupGE :: Ord k => k -> Map k v -> Maybe (k, v)

-- | <i>O(1)</i>. The empty map.
gr
gr<pre>
grempty      == fromList []
grsize empty == 0
gr</pre>
empty :: Map k a

-- | <i>O(1)</i>. A map with a single element.
gr
gr<pre>
grsingleton 1 'a'        == fromList [(1, 'a')]
grsize (singleton 1 'a') == 1
gr</pre>
singleton :: k -> a -> Map k a

-- | <i>O(log n)</i>. Insert a new key and value in the map. If the key is
gralready present in the map, the associated value is replaced with the
grsupplied value. <a>insert</a> is equivalent to <tt><a>insertWith</a>
gr<a>const</a></tt>.
gr
gr<pre>
grinsert 5 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'x')]
grinsert 7 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'a'), (7, 'x')]
grinsert 5 'x' empty                         == singleton 5 'x'
gr</pre>
insert :: Ord k => k -> a -> Map k a -> Map k a

-- | <i>O(log n)</i>. Insert with a function, combining new value and old
grvalue. <tt><a>insertWith</a> f key value mp</tt> will insert the pair
gr(key, value) into <tt>mp</tt> if key does not exist in the map. If the
grkey does exist, the function will insert the pair <tt>(key, f
grnew_value old_value)</tt>.
gr
gr<pre>
grinsertWith (++) 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "xxxa")]
grinsertWith (++) 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
grinsertWith (++) 5 "xxx" empty                         == singleton 5 "xxx"
gr</pre>
insertWith :: Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a

-- | <i>O(log n)</i>. Insert with a function, combining key, new value and
grold value. <tt><a>insertWithKey</a> f key value mp</tt> will insert
grthe pair (key, value) into <tt>mp</tt> if key does not exist in the
grmap. If the key does exist, the function will insert the pair
gr<tt>(key,f key new_value old_value)</tt>. Note that the key passed to
grf is the same key passed to <a>insertWithKey</a>.
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grinsertWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:xxx|a")]
grinsertWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
grinsertWithKey f 5 "xxx" empty                         == singleton 5 "xxx"
gr</pre>
insertWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a

-- | <i>O(log n)</i>. Combines insert operation with old value retrieval.
grThe expression (<tt><a>insertLookupWithKey</a> f k x map</tt>) is a
grpair where the first element is equal to (<tt><a>lookup</a> k
grmap</tt>) and the second element equal to (<tt><a>insertWithKey</a> f
grk x map</tt>).
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grinsertLookupWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:xxx|a")])
grinsertLookupWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a"), (7, "xxx")])
grinsertLookupWithKey f 5 "xxx" empty                         == (Nothing,  singleton 5 "xxx")
gr</pre>
gr
grThis is how to define <tt>insertLookup</tt> using
gr<tt>insertLookupWithKey</tt>:
gr
gr<pre>
grlet insertLookup kx x t = insertLookupWithKey (\_ a _ -&gt; a) kx x t
grinsertLookup 5 "x" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "x")])
grinsertLookup 7 "x" (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a"), (7, "x")])
gr</pre>
insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a, Map k a)

-- | <i>O(log n)</i>. Delete a key and its value from the map. When the key
gris not a member of the map, the original map is returned.
gr
gr<pre>
grdelete 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grdelete 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grdelete 5 empty                         == empty
gr</pre>
delete :: Ord k => k -> Map k a -> Map k a

-- | <i>O(log n)</i>. Update a value at a specific key with the result of
grthe provided function. When the key is not a member of the map, the
groriginal map is returned.
gr
gr<pre>
gradjust ("new " ++) 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
gradjust ("new " ++) 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gradjust ("new " ++) 7 empty                         == empty
gr</pre>
adjust :: Ord k => (a -> a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. Adjust a value at a specific key. When the key is not
gra member of the map, the original map is returned.
gr
gr<pre>
grlet f key x = (show key) ++ ":new " ++ x
gradjustWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
gradjustWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gradjustWithKey f 7 empty                         == empty
gr</pre>
adjustWithKey :: Ord k => (k -> a -> a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. The expression (<tt><a>update</a> f k map</tt>)
grupdates the value <tt>x</tt> at <tt>k</tt> (if it is in the map). If
gr(<tt>f x</tt>) is <a>Nothing</a>, the element is deleted. If it is
gr(<tt><a>Just</a> y</tt>), the key <tt>k</tt> is bound to the new value
gr<tt>y</tt>.
gr
gr<pre>
grlet f x = if x == "a" then Just "new a" else Nothing
grupdate f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
grupdate f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grupdate f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
update :: Ord k => (a -> Maybe a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. The expression (<tt><a>updateWithKey</a> f k
grmap</tt>) updates the value <tt>x</tt> at <tt>k</tt> (if it is in the
grmap). If (<tt>f k x</tt>) is <a>Nothing</a>, the element is deleted.
grIf it is (<tt><a>Just</a> y</tt>), the key <tt>k</tt> is bound to the
grnew value <tt>y</tt>.
gr
gr<pre>
grlet f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
grupdateWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
grupdateWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grupdateWithKey f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. Lookup and update. See also <a>updateWithKey</a>. The
grfunction returns changed value, if it is updated. Returns the original
grkey value if the map entry is deleted.
gr
gr<pre>
grlet f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
grupdateLookupWithKey f 5 (fromList [(5,"a"), (3,"b")]) == (Just "5:new a", fromList [(3, "b"), (5, "5:new a")])
grupdateLookupWithKey f 7 (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a")])
grupdateLookupWithKey f 3 (fromList [(5,"a"), (3,"b")]) == (Just "b", singleton 5 "a")
gr</pre>
updateLookupWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> (Maybe a, Map k a)

-- | <i>O(log n)</i>. The expression (<tt><a>alter</a> f k map</tt>) alters
grthe value <tt>x</tt> at <tt>k</tt>, or absence thereof. <a>alter</a>
grcan be used to insert, delete, or update a value in a <a>Map</a>. In
grshort : <tt><a>lookup</a> k (<a>alter</a> f k m) = f (<a>lookup</a> k
grm)</tt>.
gr
gr<pre>
grlet f _ = Nothing
gralter f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gralter f 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr
grlet f _ = Just "c"
gralter f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "c")]
gralter f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "c")]
gr</pre>
alter :: Ord k => (Maybe a -> Maybe a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. The expression (<tt><a>alterF</a> f k map</tt>)
gralters the value <tt>x</tt> at <tt>k</tt>, or absence thereof.
gr<a>alterF</a> can be used to inspect, insert, delete, or update a
grvalue in a <a>Map</a>. In short: <tt><a>lookup</a> k &lt;$&gt;
gr<a>alterF</a> f k m = f (<a>lookup</a> k m)</tt>.
gr
grExample:
gr
gr<pre>
grinteractiveAlter :: Int -&gt; Map Int String -&gt; IO (Map Int String)
grinteractiveAlter k m = alterF f k m where
gr  f Nothing -&gt; do
gr     putStrLn $ show k ++
gr         " was not found in the map. Would you like to add it?"
gr     getUserResponse1 :: IO (Maybe String)
gr  f (Just old) -&gt; do
gr     putStrLn "The key is currently bound to " ++ show old ++
gr         ". Would you like to change or delete it?"
gr     getUserresponse2 :: IO (Maybe String)
gr</pre>
gr
gr<a>alterF</a> is the most general operation for working with an
grindividual key that may or may not be in a given map. When used with
grtrivial functors like <a>Identity</a> and <a>Const</a>, it is often
grslightly slower than more specialized combinators like <a>lookup</a>
grand <a>insert</a>. However, when the functor is non-trivial and key
grcomparison is not particularly cheap, it is the fastest way.
gr
grNote on rewrite rules:
gr
grThis module includes GHC rewrite rules to optimize <a>alterF</a> for
grthe <a>Const</a> and <a>Identity</a> functors. In general, these rules
grimprove performance. The sole exception is that when using
gr<a>Identity</a>, deleting a key that is already absent takes longer
grthan it would without the rules. If you expect this to occur a very
grlarge fraction of the time, you might consider using a private copy of
grthe <a>Identity</a> type.
gr
grNote: <a>alterF</a> is a flipped version of the <tt>at</tt> combinator
grfrom <a>At</a>.
alterF :: (Functor f, Ord k) => (Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. The expression (<tt><a>union</a>
grt1 t2</tt>) takes the left-biased union of <tt>t1</tt> and
gr<tt>t2</tt>. It prefers <tt>t1</tt> when duplicate keys are
grencountered, i.e. (<tt><a>union</a> == <a>unionWith</a>
gr<a>const</a></tt>).
gr
gr<pre>
grunion (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "a"), (7, "C")]
gr</pre>
union :: Ord k => Map k a -> Map k a -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Union with a combining function.
gr
gr<pre>
grunionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "aA"), (7, "C")]
gr</pre>
unionWith :: Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Union with a combining function.
gr
gr<pre>
grlet f key left_value right_value = (show key) ++ ":" ++ left_value ++ "|" ++ right_value
grunionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "5:a|A"), (7, "C")]
gr</pre>
unionWithKey :: Ord k => (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a

-- | The union of a list of maps: (<tt><a>unions</a> == <a>foldl</a>
gr<a>union</a> <a>empty</a></tt>).
gr
gr<pre>
grunions [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
gr    == fromList [(3, "b"), (5, "a"), (7, "C")]
grunions [(fromList [(5, "A3"), (3, "B3")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "a"), (3, "b")])]
gr    == fromList [(3, "B3"), (5, "A3"), (7, "C")]
gr</pre>
unions :: Ord k => [Map k a] -> Map k a

-- | The union of a list of maps, with a combining operation:
gr(<tt><a>unionsWith</a> f == <a>foldl</a> (<a>unionWith</a> f)
gr<a>empty</a></tt>).
gr
gr<pre>
grunionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
gr    == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")]
gr</pre>
unionsWith :: Ord k => (a -> a -> a) -> [Map k a] -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Difference of two maps. Return
grelements of the first map not existing in the second map.
gr
gr<pre>
grdifference (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 3 "b"
gr</pre>
difference :: Ord k => Map k a -> Map k b -> Map k a

-- | <i>O(n+m)</i>. Difference with a combining function. When two equal
grkeys are encountered, the combining function is applied to the values
grof these keys. If it returns <a>Nothing</a>, the element is discarded
gr(proper set difference). If it returns (<tt><a>Just</a> y</tt>), the
grelement is updated with a new value <tt>y</tt>.
gr
gr<pre>
grlet f al ar = if al == "b" then Just (al ++ ":" ++ ar) else Nothing
grdifferenceWith f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (7, "C")])
gr    == singleton 3 "b:B"
gr</pre>
differenceWith :: Ord k => (a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a

-- | <i>O(n+m)</i>. Difference with a combining function. When two equal
grkeys are encountered, the combining function is applied to the key and
grboth values. If it returns <a>Nothing</a>, the element is discarded
gr(proper set difference). If it returns (<tt><a>Just</a> y</tt>), the
grelement is updated with a new value <tt>y</tt>.
gr
gr<pre>
grlet f k al ar = if al == "b" then Just ((show k) ++ ":" ++ al ++ "|" ++ ar) else Nothing
grdifferenceWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (10, "C")])
gr    == singleton 3 "3:b|B"
gr</pre>
differenceWithKey :: Ord k => (k -> a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Intersection of two maps. Return
grdata in the first map for the keys existing in both maps.
gr(<tt><a>intersection</a> m1 m2 == <a>intersectionWith</a> <a>const</a>
grm1 m2</tt>).
gr
gr<pre>
grintersection (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "a"
gr</pre>
intersection :: Ord k => Map k a -> Map k b -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Intersection with a combining
grfunction.
gr
gr<pre>
grintersectionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "aA"
gr</pre>
intersectionWith :: Ord k => (a -> b -> c) -> Map k a -> Map k b -> Map k c

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Intersection with a combining
grfunction.
gr
gr<pre>
grlet f k al ar = (show k) ++ ":" ++ al ++ "|" ++ ar
grintersectionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "5:a|A"
gr</pre>
intersectionWithKey :: Ord k => (k -> a -> b -> c) -> Map k a -> Map k b -> Map k c

-- | A tactic for dealing with keys present in one map but not the other in
gr<a>merge</a>.
gr
grA tactic of type <tt> SimpleWhenMissing k x z </tt> is an abstract
grrepresentation of a function of type <tt> k -&gt; x -&gt; Maybe z
gr</tt>.
type SimpleWhenMissing = WhenMissing Identity

-- | A tactic for dealing with keys present in both maps in <a>merge</a>.
gr
grA tactic of type <tt> SimpleWhenMatched k x y z </tt> is an abstract
grrepresentation of a function of type <tt> k -&gt; x -&gt; y -&gt;
grMaybe z </tt>.
type SimpleWhenMatched = WhenMatched Identity

-- | Along with zipWithMaybeAMatched, witnesses the isomorphism between
gr<tt>WhenMatched f k x y z</tt> and <tt>k -&gt; x -&gt; y -&gt; f
gr(Maybe z)</tt>.
runWhenMatched :: WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)

-- | Along with traverseMaybeMissing, witnesses the isomorphism between
gr<tt>WhenMissing f k x y</tt> and <tt>k -&gt; x -&gt; f (Maybe y)</tt>.
runWhenMissing :: WhenMissing f k x y -> k -> x -> f (Maybe y)

-- | Merge two maps.
gr
gr<tt>merge</tt> takes two <a>WhenMissing</a> tactics, a
gr<a>WhenMatched</a> tactic and two maps. It uses the tactics to merge
grthe maps. Its behavior is best understood via its fundamental tactics,
gr<a>mapMaybeMissing</a> and <a>zipWithMaybeMatched</a>.
gr
grConsider
gr
gr<pre>
grmerge (mapMaybeMissing g1)
gr             (mapMaybeMissing g2)
gr             (zipWithMaybeMatched f)
gr             m1 m2
gr</pre>
gr
grTake, for example,
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>), (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 = [(1, "one"), (2, "two"), (4, "three")]
gr</pre>
gr
gr<tt>merge</tt> will first '<tt>align'</tt> these maps by key:
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>),               (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 =           [(1, "one"), (2, "two"),          (4, "three")]
gr</pre>
gr
grIt will then pass the individual entries and pairs of entries to
gr<tt>g1</tt>, <tt>g2</tt>, or <tt>f</tt> as appropriate:
gr
gr<pre>
grmaybes = [g1 0 <tt>a</tt>, f 1 <tt>b</tt> "one", g2 2 "two", g1 3 <tt>c</tt>, f 4 <tt>d</tt> "three"]
gr</pre>
gr
grThis produces a <a>Maybe</a> for each key:
gr
gr<pre>
grkeys =     0        1          2           3        4
grresults = [Nothing, Just True, Just False, Nothing, Just True]
gr</pre>
gr
grFinally, the <tt>Just</tt> results are collected into a map:
gr
gr<pre>
grreturn value = [(1, True), (2, False), (4, True)]
gr</pre>
gr
grThe other tactics below are optimizations or simplifications of
gr<a>mapMaybeMissing</a> for special cases. Most importantly,
gr
gr<ul>
gr<li><a>dropMissing</a> drops all the keys.</li>
gr<li><a>preserveMissing</a> leaves all the entries alone.</li>
gr</ul>
gr
grWhen <a>merge</a> is given three arguments, it is inlined at the call
grsite. To prevent excessive inlining, you should typically use
gr<a>merge</a> to define your custom combining functions.
gr
grExamples:
gr
gr<pre>
grunionWithKey f = merge preserveMissing preserveMissing (zipWithMatched f)
gr</pre>
gr
gr<pre>
grintersectionWithKey f = merge dropMissing dropMissing (zipWithMatched f)
gr</pre>
gr
gr<pre>
grdifferenceWith f = merge diffPreserve diffDrop f
gr</pre>
gr
gr<pre>
grsymmetricDifference = merge diffPreserve diffPreserve (\ _ _ _ -&gt; Nothing)
gr</pre>
gr
gr<pre>
grmapEachPiece f g h = merge (diffMapWithKey f) (diffMapWithKey g)
gr</pre>
merge :: Ord k => SimpleWhenMissing k a c -> SimpleWhenMissing k b c -> SimpleWhenMatched k a b c -> Map k a -> Map k b -> Map k c

-- | When a key is found in both maps, apply a function to the key and
grvalues and maybe use the result in the merged map.
gr
gr<pre>
grzipWithMaybeMatched :: (k -&gt; x -&gt; y -&gt; Maybe z)
gr                    -&gt; SimpleWhenMatched k x y z
gr</pre>
zipWithMaybeMatched :: Applicative f => (k -> x -> y -> Maybe z) -> WhenMatched f k x y z

-- | When a key is found in both maps, apply a function to the key and
grvalues and use the result in the merged map.
gr
gr<pre>
grzipWithMatched :: (k -&gt; x -&gt; y -&gt; z)
gr               -&gt; SimpleWhenMatched k x y z
gr</pre>
zipWithMatched :: Applicative f => (k -> x -> y -> z) -> WhenMatched f k x y z

-- | Map over the entries whose keys are missing from the other map,
groptionally removing some. This is the most powerful
gr<a>SimpleWhenMissing</a> tactic, but others are usually more
grefficient.
gr
gr<pre>
grmapMaybeMissing :: (k -&gt; x -&gt; Maybe y) -&gt; SimpleWhenMissing k x y
gr</pre>
gr
gr<pre>
grmapMaybeMissing f = traverseMaybeMissing (\k x -&gt; pure (f k x))
gr</pre>
gr
grbut <tt>mapMaybeMissing</tt> uses fewer unnecessary <a>Applicative</a>
groperations.
mapMaybeMissing :: Applicative f => (k -> x -> Maybe y) -> WhenMissing f k x y

-- | Drop all the entries whose keys are missing from the other map.
gr
gr<pre>
grdropMissing :: SimpleWhenMissing k x y
gr</pre>
gr
gr<pre>
grdropMissing = mapMaybeMissing (\_ _ -&gt; Nothing)
gr</pre>
gr
grbut <tt>dropMissing</tt> is much faster.
dropMissing :: Applicative f => WhenMissing f k x y

-- | Preserve, unchanged, the entries whose keys are missing from the other
grmap.
gr
gr<pre>
grpreserveMissing :: SimpleWhenMissing k x x
gr</pre>
gr
gr<pre>
grpreserveMissing = Merge.Lazy.mapMaybeMissing (\_ x -&gt; Just x)
gr</pre>
gr
grbut <tt>preserveMissing</tt> is much faster.
preserveMissing :: Applicative f => WhenMissing f k x x

-- | Map over the entries whose keys are missing from the other map.
gr
gr<pre>
grmapMissing :: (k -&gt; x -&gt; y) -&gt; SimpleWhenMissing k x y
gr</pre>
gr
gr<pre>
grmapMissing f = mapMaybeMissing (\k x -&gt; Just $ f k x)
gr</pre>
gr
grbut <tt>mapMissing</tt> is somewhat faster.
mapMissing :: Applicative f => (k -> x -> y) -> WhenMissing f k x y

-- | Filter the entries whose keys are missing from the other map.
gr
gr<pre>
grfilterMissing :: (k -&gt; x -&gt; Bool) -&gt; SimpleWhenMissing k x x
gr</pre>
gr
gr<pre>
grfilterMissing f = Merge.Lazy.mapMaybeMissing $ \k x -&gt; guard (f k x) *&gt; Just x
gr</pre>
gr
grbut this should be a little faster.
filterMissing :: Applicative f => (k -> x -> Bool) -> WhenMissing f k x x

-- | A tactic for dealing with keys present in one map but not the other in
gr<a>merge</a> or <a>mergeA</a>.
gr
grA tactic of type <tt> WhenMissing f k x z </tt> is an abstract
grrepresentation of a function of type <tt> k -&gt; x -&gt; f (Maybe z)
gr</tt>.
data WhenMissing f k x y
WhenMissing :: Map k x -> f (Map k y) -> k -> x -> f (Maybe y) -> WhenMissing f k x y
[missingSubtree] :: WhenMissing f k x y -> Map k x -> f (Map k y)
[missingKey] :: WhenMissing f k x y -> k -> x -> f (Maybe y)

-- | A tactic for dealing with keys present in both maps in <a>merge</a> or
gr<a>mergeA</a>.
gr
grA tactic of type <tt> WhenMatched f k x y z </tt> is an abstract
grrepresentation of a function of type <tt> k -&gt; x -&gt; y -&gt; f
gr(Maybe z) </tt>.
newtype WhenMatched f k x y z
WhenMatched :: k -> x -> y -> f (Maybe z) -> WhenMatched f k x y z
[matchedKey] :: WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)

-- | An applicative version of <a>merge</a>.
gr
gr<tt>mergeA</tt> takes two <a>WhenMissing</a> tactics, a
gr<a>WhenMatched</a> tactic and two maps. It uses the tactics to merge
grthe maps. Its behavior is best understood via its fundamental tactics,
gr<a>traverseMaybeMissing</a> and <a>zipWithMaybeAMatched</a>.
gr
grConsider
gr
gr<pre>
grmergeA (traverseMaybeMissing g1)
gr              (traverseMaybeMissing g2)
gr              (zipWithMaybeAMatched f)
gr              m1 m2
gr</pre>
gr
grTake, for example,
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>), (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 = [(1, "one"), (2, "two"), (4, "three")]
gr</pre>
gr
gr<tt>mergeA</tt> will first '<tt>align'</tt> these maps by key:
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>),               (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 =           [(1, "one"), (2, "two"),          (4, "three")]
gr</pre>
gr
grIt will then pass the individual entries and pairs of entries to
gr<tt>g1</tt>, <tt>g2</tt>, or <tt>f</tt> as appropriate:
gr
gr<pre>
gractions = [g1 0 <tt>a</tt>, f 1 <tt>b</tt> "one", g2 2 "two", g1 3 <tt>c</tt>, f 4 <tt>d</tt> "three"]
gr</pre>
gr
grNext, it will perform the actions in the <tt>actions</tt> list in
grorder from left to right.
gr
gr<pre>
grkeys =     0        1          2           3        4
grresults = [Nothing, Just True, Just False, Nothing, Just True]
gr</pre>
gr
grFinally, the <tt>Just</tt> results are collected into a map:
gr
gr<pre>
grreturn value = [(1, True), (2, False), (4, True)]
gr</pre>
gr
grThe other tactics below are optimizations or simplifications of
gr<a>traverseMaybeMissing</a> for special cases. Most importantly,
gr
gr<ul>
gr<li><a>dropMissing</a> drops all the keys.</li>
gr<li><a>preserveMissing</a> leaves all the entries alone.</li>
gr<li><a>mapMaybeMissing</a> does not use the <a>Applicative</a>
grcontext.</li>
gr</ul>
gr
grWhen <a>mergeA</a> is given three arguments, it is inlined at the call
grsite. To prevent excessive inlining, you should generally only use
gr<a>mergeA</a> to define custom combining functions.
mergeA :: (Applicative f, Ord k) => WhenMissing f k a c -> WhenMissing f k b c -> WhenMatched f k a b c -> Map k a -> Map k b -> f (Map k c)

-- | When a key is found in both maps, apply a function to the key and
grvalues, perform the resulting action, and maybe use the result in the
grmerged map.
gr
grThis is the fundamental <a>WhenMatched</a> tactic.
zipWithMaybeAMatched :: (k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z

-- | When a key is found in both maps, apply a function to the key and
grvalues to produce an action and use its result in the merged map.
zipWithAMatched :: Applicative f => (k -> x -> y -> f z) -> WhenMatched f k x y z

-- | Traverse over the entries whose keys are missing from the other map,
groptionally producing values to put in the result. This is the most
grpowerful <a>WhenMissing</a> tactic, but others are usually more
grefficient.
traverseMaybeMissing :: Applicative f => (k -> x -> f (Maybe y)) -> WhenMissing f k x y

-- | Traverse over the entries whose keys are missing from the other map.
traverseMissing :: Applicative f => (k -> x -> f y) -> WhenMissing f k x y

-- | Filter the entries whose keys are missing from the other map using
grsome <a>Applicative</a> action.
gr
gr<pre>
grfilterAMissing f = Merge.Lazy.traverseMaybeMissing $
gr  k x -&gt; (b -&gt; guard b *&gt; Just x) <a>$</a> f k x
gr</pre>
gr
grbut this should be a little faster.
filterAMissing :: Applicative f => (k -> x -> f Bool) -> WhenMissing f k x x

-- | <i>O(n+m)</i>. An unsafe general combining function.
gr
grWARNING: This function can produce corrupt maps and its results may
grdepend on the internal structures of its inputs. Users should prefer
gr<a>merge</a> or <a>mergeA</a>.
gr
grWhen <a>mergeWithKey</a> is given three arguments, it is inlined to
grthe call site. You should therefore use <a>mergeWithKey</a> only to
grdefine custom combining functions. For example, you could define
gr<a>unionWithKey</a>, <a>differenceWithKey</a> and
gr<a>intersectionWithKey</a> as
gr
gr<pre>
grmyUnionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -&gt; Just (f k x1 x2)) id id m1 m2
grmyDifferenceWithKey f m1 m2 = mergeWithKey f id (const empty) m1 m2
grmyIntersectionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -&gt; Just (f k x1 x2)) (const empty) (const empty) m1 m2
gr</pre>
gr
grWhen calling <tt><a>mergeWithKey</a> combine only1 only2</tt>, a
grfunction combining two <a>Map</a>s is created, such that
gr
gr<ul>
gr<li>if a key is present in both maps, it is passed with both
grcorresponding values to the <tt>combine</tt> function. Depending on
grthe result, the key is either present in the result with specified
grvalue, or is left out;</li>
gr<li>a nonempty subtree present only in the first map is passed to
gr<tt>only1</tt> and the output is added to the result;</li>
gr<li>a nonempty subtree present only in the second map is passed to
gr<tt>only2</tt> and the output is added to the result.</li>
gr</ul>
gr
grThe <tt>only1</tt> and <tt>only2</tt> methods <i>must return a map
grwith a subset (possibly empty) of the keys of the given map</i>. The
grvalues can be modified arbitrarily. Most common variants of
gr<tt>only1</tt> and <tt>only2</tt> are <a>id</a> and <tt><a>const</a>
gr<a>empty</a></tt>, but for example <tt><a>map</a> f</tt>,
gr<tt><a>filterWithKey</a> f</tt>, or <tt><a>mapMaybeWithKey</a> f</tt>
grcould be used for any <tt>f</tt>.
mergeWithKey :: Ord k => (k -> a -> b -> Maybe c) -> (Map k a -> Map k c) -> (Map k b -> Map k c) -> Map k a -> Map k b -> Map k c

-- | <i>O(n)</i>. Map a function over all values in the map.
gr
gr<pre>
grmap (++ "x") (fromList [(5,"a"), (3,"b")]) == fromList [(3, "bx"), (5, "ax")]
gr</pre>
map :: (a -> b) -> Map k a -> Map k b

-- | <i>O(n)</i>. Map a function over all values in the map.
gr
gr<pre>
grlet f key x = (show key) ++ ":" ++ x
grmapWithKey f (fromList [(5,"a"), (3,"b")]) == fromList [(3, "3:b"), (5, "5:a")]
gr</pre>
mapWithKey :: (k -> a -> b) -> Map k a -> Map k b

-- | <i>O(n)</i>. <tt><a>traverseWithKey</a> f m == <a>fromList</a>
gr<a>$</a> <a>traverse</a> ((k, v) -&gt; (,) k <a>$</a> f k v)
gr(<a>toList</a> m)</tt> That is, behaves exactly like a regular
gr<a>traverse</a> except that the traversing function also has access to
grthe key associated with a value.
gr
gr<pre>
grtraverseWithKey (\k v -&gt; if odd k then Just (succ v) else Nothing) (fromList [(1, 'a'), (5, 'e')]) == Just (fromList [(1, 'b'), (5, 'f')])
grtraverseWithKey (\k v -&gt; if odd k then Just (succ v) else Nothing) (fromList [(2, 'c')])           == Nothing
gr</pre>
traverseWithKey :: Applicative t => (k -> a -> t b) -> Map k a -> t (Map k b)

-- | <i>O(n)</i>. Traverse keys/values and collect the <a>Just</a> results.
traverseMaybeWithKey :: Applicative f => (k -> a -> f (Maybe b)) -> Map k a -> f (Map k b)

-- | <i>O(n)</i>. The function <a>mapAccum</a> threads an accumulating
grargument through the map in ascending order of keys.
gr
gr<pre>
grlet f a b = (a ++ b, b ++ "X")
grmapAccum f "Everything: " (fromList [(5,"a"), (3,"b")]) == ("Everything: ba", fromList [(3, "bX"), (5, "aX")])
gr</pre>
mapAccum :: (a -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)

-- | <i>O(n)</i>. The function <a>mapAccumWithKey</a> threads an
graccumulating argument through the map in ascending order of keys.
gr
gr<pre>
grlet f a k b = (a ++ " " ++ (show k) ++ "-" ++ b, b ++ "X")
grmapAccumWithKey f "Everything:" (fromList [(5,"a"), (3,"b")]) == ("Everything: 3-b 5-a", fromList [(3, "bX"), (5, "aX")])
gr</pre>
mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)

-- | <i>O(n)</i>. The function <tt>mapAccumR</tt> threads an accumulating
grargument through the map in descending order of keys.
mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)

-- | <i>O(n*log n)</i>. <tt><a>mapKeys</a> f s</tt> is the map obtained by
grapplying <tt>f</tt> to each key of <tt>s</tt>.
gr
grThe size of the result may be smaller if <tt>f</tt> maps two or more
grdistinct keys to the same new key. In this case the value at the
grgreatest of the original keys is retained.
gr
gr<pre>
grmapKeys (+ 1) (fromList [(5,"a"), (3,"b")])                        == fromList [(4, "b"), (6, "a")]
grmapKeys (\ _ -&gt; 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "c"
grmapKeys (\ _ -&gt; 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "c"
gr</pre>
mapKeys :: Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 a

-- | <i>O(n*log n)</i>. <tt><a>mapKeysWith</a> c f s</tt> is the map
grobtained by applying <tt>f</tt> to each key of <tt>s</tt>.
gr
grThe size of the result may be smaller if <tt>f</tt> maps two or more
grdistinct keys to the same new key. In this case the associated values
grwill be combined using <tt>c</tt>. The value at the greater of the two
groriginal keys is used as the first argument to <tt>c</tt>.
gr
gr<pre>
grmapKeysWith (++) (\ _ -&gt; 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "cdab"
grmapKeysWith (++) (\ _ -&gt; 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "cdab"
gr</pre>
mapKeysWith :: Ord k2 => (a -> a -> a) -> (k1 -> k2) -> Map k1 a -> Map k2 a

-- | <i>O(n)</i>. <tt><a>mapKeysMonotonic</a> f s == <a>mapKeys</a> f
grs</tt>, but works only when <tt>f</tt> is strictly monotonic. That is,
grfor any values <tt>x</tt> and <tt>y</tt>, if <tt>x</tt> &lt;
gr<tt>y</tt> then <tt>f x</tt> &lt; <tt>f y</tt>. <i>The precondition is
grnot checked.</i> Semi-formally, we have:
gr
gr<pre>
grand [x &lt; y ==&gt; f x &lt; f y | x &lt;- ls, y &lt;- ls]
gr                    ==&gt; mapKeysMonotonic f s == mapKeys f s
gr    where ls = keys s
gr</pre>
gr
grThis means that <tt>f</tt> maps distinct original keys to distinct
grresulting keys. This function has better performance than
gr<a>mapKeys</a>.
gr
gr<pre>
grmapKeysMonotonic (\ k -&gt; k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")]
grvalid (mapKeysMonotonic (\ k -&gt; k * 2) (fromList [(5,"a"), (3,"b")])) == True
grvalid (mapKeysMonotonic (\ _ -&gt; 1)     (fromList [(5,"a"), (3,"b")])) == False
gr</pre>
mapKeysMonotonic :: (k1 -> k2) -> Map k1 a -> Map k2 a

-- | <i>O(n)</i>. Fold the values in the map using the given
grright-associative binary operator, such that <tt><a>foldr</a> f z ==
gr<a>foldr</a> f z . <a>elems</a></tt>.
gr
grFor example,
gr
gr<pre>
grelems map = foldr (:) [] map
gr</pre>
gr
gr<pre>
grlet f a len = len + (length a)
grfoldr f 0 (fromList [(5,"a"), (3,"bbb")]) == 4
gr</pre>
foldr :: (a -> b -> b) -> b -> Map k a -> b

-- | <i>O(n)</i>. Fold the values in the map using the given
grleft-associative binary operator, such that <tt><a>foldl</a> f z ==
gr<a>foldl</a> f z . <a>elems</a></tt>.
gr
grFor example,
gr
gr<pre>
grelems = reverse . foldl (flip (:)) []
gr</pre>
gr
gr<pre>
grlet f len a = len + (length a)
grfoldl f 0 (fromList [(5,"a"), (3,"bbb")]) == 4
gr</pre>
foldl :: (a -> b -> a) -> a -> Map k b -> a

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grright-associative binary operator, such that <tt><a>foldrWithKey</a> f
grz == <a>foldr</a> (<a>uncurry</a> f) z . <a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grkeys map = foldrWithKey (\k x ks -&gt; k:ks) [] map
gr</pre>
gr
gr<pre>
grlet f k a result = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")"
grfoldrWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (5:a)(3:b)"
gr</pre>
foldrWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grleft-associative binary operator, such that <tt><a>foldlWithKey</a> f
grz == <a>foldl</a> (\z' (kx, x) -&gt; f z' kx x) z .
gr<a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grkeys = reverse . foldlWithKey (\ks k x -&gt; k:ks) []
gr</pre>
gr
gr<pre>
grlet f result k a = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")"
grfoldlWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (3:b)(5:a)"
gr</pre>
foldlWithKey :: (a -> k -> b -> a) -> a -> Map k b -> a

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grmonoid, such that
gr
gr<pre>
gr<a>foldMapWithKey</a> f = <a>fold</a> . <a>mapWithKey</a> f
gr</pre>
gr
grThis can be an asymptotically faster than <a>foldrWithKey</a> or
gr<a>foldlWithKey</a> for some monoids.
foldMapWithKey :: Monoid m => (k -> a -> m) -> Map k a -> m

-- | <i>O(n)</i>. A strict version of <a>foldr</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldr' :: (a -> b -> b) -> b -> Map k a -> b

-- | <i>O(n)</i>. A strict version of <a>foldl</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldl' :: (a -> b -> a) -> a -> Map k b -> a

-- | <i>O(n)</i>. A strict version of <a>foldrWithKey</a>. Each application
grof the operator is evaluated before using the result in the next
grapplication. This function is strict in the starting value.
foldrWithKey' :: (k -> a -> b -> b) -> b -> Map k a -> b

-- | <i>O(n)</i>. A strict version of <a>foldlWithKey</a>. Each application
grof the operator is evaluated before using the result in the next
grapplication. This function is strict in the starting value.
foldlWithKey' :: (a -> k -> b -> a) -> a -> Map k b -> a

-- | <i>O(n)</i>. Return all elements of the map in the ascending order of
grtheir keys. Subject to list fusion.
gr
gr<pre>
grelems (fromList [(5,"a"), (3,"b")]) == ["b","a"]
grelems empty == []
gr</pre>
elems :: Map k a -> [a]

-- | <i>O(n)</i>. Return all keys of the map in ascending order. Subject to
grlist fusion.
gr
gr<pre>
grkeys (fromList [(5,"a"), (3,"b")]) == [3,5]
grkeys empty == []
gr</pre>
keys :: Map k a -> [k]

-- | <i>O(n)</i>. An alias for <a>toAscList</a>. Return all key/value pairs
grin the map in ascending key order. Subject to list fusion.
gr
gr<pre>
grassocs (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
grassocs empty == []
gr</pre>
assocs :: Map k a -> [(k, a)]

-- | <i>O(n)</i>. The set of all keys of the map.
gr
gr<pre>
grkeysSet (fromList [(5,"a"), (3,"b")]) == Data.Set.fromList [3,5]
grkeysSet empty == Data.Set.empty
gr</pre>
keysSet :: Map k a -> Set k

-- | <i>O(n)</i>. Build a map from a set of keys and a function which for
greach key computes its value.
gr
gr<pre>
grfromSet (\k -&gt; replicate k 'a') (Data.Set.fromList [3, 5]) == fromList [(5,"aaaaa"), (3,"aaa")]
grfromSet undefined Data.Set.empty == empty
gr</pre>
fromSet :: (k -> a) -> Set k -> Map k a

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs. Subject to
grlist fusion.
gr
gr<pre>
grtoList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
grtoList empty == []
gr</pre>
toList :: Map k a -> [(k, a)]

-- | <i>O(n*log n)</i>. Build a map from a list of key/value pairs. See
gralso <a>fromAscList</a>. If the list contains more than one value for
grthe same key, the last value for the key is retained.
gr
grIf the keys of the list are ordered, linear-time implementation is
grused, with the performance equal to <a>fromDistinctAscList</a>.
gr
gr<pre>
grfromList [] == empty
grfromList [(5,"a"), (3,"b"), (5, "c")] == fromList [(5,"c"), (3,"b")]
grfromList [(5,"c"), (3,"b"), (5, "a")] == fromList [(5,"a"), (3,"b")]
gr</pre>
fromList :: Ord k => [(k, a)] -> Map k a

-- | <i>O(n*log n)</i>. Build a map from a list of key/value pairs with a
grcombining function. See also <a>fromAscListWith</a>.
gr
gr<pre>
grfromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")]
grfromListWith (++) [] == empty
gr</pre>
fromListWith :: Ord k => (a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n*log n)</i>. Build a map from a list of key/value pairs with a
grcombining function. See also <a>fromAscListWithKey</a>.
gr
gr<pre>
grlet f k a1 a2 = (show k) ++ a1 ++ a2
grfromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "3ab"), (5, "5a5ba")]
grfromListWithKey f [] == empty
gr</pre>
fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs where the
grkeys are in ascending order. Subject to list fusion.
gr
gr<pre>
grtoAscList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
gr</pre>
toAscList :: Map k a -> [(k, a)]

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs where the
grkeys are in descending order. Subject to list fusion.
gr
gr<pre>
grtoDescList (fromList [(5,"a"), (3,"b")]) == [(5,"a"), (3,"b")]
gr</pre>
toDescList :: Map k a -> [(k, a)]

-- | <i>O(n)</i>. Build a map from an ascending list in linear time. <i>The
grprecondition (input list is ascending) is not checked.</i>
gr
gr<pre>
grfromAscList [(3,"b"), (5,"a")]          == fromList [(3, "b"), (5, "a")]
grfromAscList [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "b")]
grvalid (fromAscList [(3,"b"), (5,"a"), (5,"b")]) == True
grvalid (fromAscList [(5,"a"), (3,"b"), (5,"b")]) == False
gr</pre>
fromAscList :: Eq k => [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from an ascending list in linear time with a
grcombining function for equal keys. <i>The precondition (input list is
grascending) is not checked.</i>
gr
gr<pre>
grfromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")]
grvalid (fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")]) == True
grvalid (fromAscListWith (++) [(5,"a"), (3,"b"), (5,"b")]) == False
gr</pre>
fromAscListWith :: Eq k => (a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from an ascending list in linear time with a
grcombining function for equal keys. <i>The precondition (input list is
grascending) is not checked.</i>
gr
gr<pre>
grlet f k a1 a2 = (show k) ++ ":" ++ a1 ++ a2
grfromAscListWithKey f [(3,"b"), (5,"a"), (5,"b"), (5,"b")] == fromList [(3, "b"), (5, "5:b5:ba")]
grvalid (fromAscListWithKey f [(3,"b"), (5,"a"), (5,"b"), (5,"b")]) == True
grvalid (fromAscListWithKey f [(5,"a"), (3,"b"), (5,"b"), (5,"b")]) == False
gr</pre>
fromAscListWithKey :: Eq k => (k -> a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from an ascending list of distinct elements
grin linear time. <i>The precondition is not checked.</i>
gr
gr<pre>
grfromDistinctAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")]
grvalid (fromDistinctAscList [(3,"b"), (5,"a")])          == True
grvalid (fromDistinctAscList [(3,"b"), (5,"a"), (5,"b")]) == False
gr</pre>
fromDistinctAscList :: [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from a descending list in linear time. <i>The
grprecondition (input list is descending) is not checked.</i>
gr
gr<pre>
grfromDescList [(5,"a"), (3,"b")]          == fromList [(3, "b"), (5, "a")]
grfromDescList [(5,"a"), (5,"b"), (3,"b")] == fromList [(3, "b"), (5, "b")]
grvalid (fromDescList [(5,"a"), (5,"b"), (3,"b")]) == True
grvalid (fromDescList [(5,"a"), (3,"b"), (5,"b")]) == False
gr</pre>
fromDescList :: Eq k => [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from a descending list in linear time with a
grcombining function for equal keys. <i>The precondition (input list is
grdescending) is not checked.</i>
gr
gr<pre>
grfromDescListWith (++) [(5,"a"), (5,"b"), (3,"b")] == fromList [(3, "b"), (5, "ba")]
grvalid (fromDescListWith (++) [(5,"a"), (5,"b"), (3,"b")]) == True
grvalid (fromDescListWith (++) [(5,"a"), (3,"b"), (5,"b")]) == False
gr</pre>
fromDescListWith :: Eq k => (a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from a descending list in linear time with a
grcombining function for equal keys. <i>The precondition (input list is
grdescending) is not checked.</i>
gr
gr<pre>
grlet f k a1 a2 = (show k) ++ ":" ++ a1 ++ a2
grfromDescListWithKey f [(5,"a"), (5,"b"), (5,"b"), (3,"b")] == fromList [(3, "b"), (5, "5:b5:ba")]
grvalid (fromDescListWithKey f [(5,"a"), (5,"b"), (5,"b"), (3,"b")]) == True
grvalid (fromDescListWithKey f [(5,"a"), (3,"b"), (5,"b"), (5,"b")]) == False
gr</pre>
fromDescListWithKey :: Eq k => (k -> a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from a descending list of distinct elements
grin linear time. <i>The precondition is not checked.</i>
gr
gr<pre>
grfromDistinctDescList [(5,"a"), (3,"b")] == fromList [(3, "b"), (5, "a")]
grvalid (fromDistinctDescList [(5,"a"), (3,"b")])          == True
grvalid (fromDistinctDescList [(5,"a"), (5,"b"), (3,"b")]) == False
gr</pre>
fromDistinctDescList :: [(k, a)] -> Map k a

-- | <i>O(n)</i>. Filter all values that satisfy the predicate.
gr
gr<pre>
grfilter (&gt; "a") (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grfilter (&gt; "x") (fromList [(5,"a"), (3,"b")]) == empty
grfilter (&lt; "a") (fromList [(5,"a"), (3,"b")]) == empty
gr</pre>
filter :: (a -> Bool) -> Map k a -> Map k a

-- | <i>O(n)</i>. Filter all keys/values that satisfy the predicate.
gr
gr<pre>
grfilterWithKey (\k _ -&gt; k &gt; 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
filterWithKey :: (k -> a -> Bool) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Take while a predicate on the keys holds. The user is
grresponsible for ensuring that for all keys <tt>j</tt> and <tt>k</tt>
grin the map, <tt>j &lt; k ==&gt; p j &gt;= p k</tt>. See note at
gr<a>spanAntitone</a>.
gr
gr<pre>
grtakeWhileAntitone p = <a>fromDistinctAscList</a> . <a>takeWhile</a> (p . fst) . <a>toList</a>
grtakeWhileAntitone p = <a>filterWithKey</a> (k _ -&gt; p k)
gr</pre>
takeWhileAntitone :: (k -> Bool) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Drop while a predicate on the keys holds. The user is
grresponsible for ensuring that for all keys <tt>j</tt> and <tt>k</tt>
grin the map, <tt>j &lt; k ==&gt; p j &gt;= p k</tt>. See note at
gr<a>spanAntitone</a>.
gr
gr<pre>
grdropWhileAntitone p = <a>fromDistinctAscList</a> . <a>dropWhile</a> (p . fst) . <a>toList</a>
grdropWhileAntitone p = <a>filterWithKey</a> (k -&gt; not (p k))
gr</pre>
dropWhileAntitone :: (k -> Bool) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Divide a map at the point where a predicate on the
grkeys stops holding. The user is responsible for ensuring that for all
grkeys <tt>j</tt> and <tt>k</tt> in the map, <tt>j &lt; k ==&gt; p j
gr&gt;= p k</tt>.
gr
gr<pre>
grspanAntitone p xs = (<a>takeWhileAntitone</a> p xs, <a>dropWhileAntitone</a> p xs)
grspanAntitone p xs = partition p xs
gr</pre>
gr
grNote: if <tt>p</tt> is not actually antitone, then
gr<tt>spanAntitone</tt> will split the map at some <i>unspecified</i>
grpoint where the predicate switches from holding to not holding (where
grthe predicate is seen to hold before the first key and to fail after
grthe last key).
spanAntitone :: (k -> Bool) -> Map k a -> (Map k a, Map k a)

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Restrict a <a>Map</a> to only
grthose keys found in a <a>Set</a>.
gr
gr<pre>
grm `<tt>restrictKeys'</tt> s = <a>filterWithKey</a> (k _ -&gt; k `<a>member'</a> s) m
grm `<tt>restrictKeys'</tt> s = m `<tt>intersect</tt> <a>fromSet</a> (const ()) s
gr</pre>
restrictKeys :: Ord k => Map k a -> Set k -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Remove all keys in a <a>Set</a>
grfrom a <a>Map</a>.
gr
gr<pre>
grm `<tt>withoutKeys'</tt> s = <a>filterWithKey</a> (k _ -&gt; k `<a>notMember'</a> s) m
grm `<tt>withoutKeys'</tt> s = m `<tt>difference'</tt> <a>fromSet</a> (const ()) s
gr</pre>
withoutKeys :: Ord k => Map k a -> Set k -> Map k a

-- | <i>O(n)</i>. Partition the map according to a predicate. The first map
grcontains all elements that satisfy the predicate, the second all
grelements that fail the predicate. See also <a>split</a>.
gr
gr<pre>
grpartition (&gt; "a") (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a")
grpartition (&lt; "x") (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
grpartition (&gt; "x") (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
gr</pre>
partition :: (a -> Bool) -> Map k a -> (Map k a, Map k a)

-- | <i>O(n)</i>. Partition the map according to a predicate. The first map
grcontains all elements that satisfy the predicate, the second all
grelements that fail the predicate. See also <a>split</a>.
gr
gr<pre>
grpartitionWithKey (\ k _ -&gt; k &gt; 3) (fromList [(5,"a"), (3,"b")]) == (singleton 5 "a", singleton 3 "b")
grpartitionWithKey (\ k _ -&gt; k &lt; 7) (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
grpartitionWithKey (\ k _ -&gt; k &gt; 7) (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
gr</pre>
partitionWithKey :: (k -> a -> Bool) -> Map k a -> (Map k a, Map k a)

-- | <i>O(n)</i>. Map values and collect the <a>Just</a> results.
gr
gr<pre>
grlet f x = if x == "a" then Just "new a" else Nothing
grmapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"
gr</pre>
mapMaybe :: (a -> Maybe b) -> Map k a -> Map k b

-- | <i>O(n)</i>. Map keys/values and collect the <a>Just</a> results.
gr
gr<pre>
grlet f k _ = if k &lt; 5 then Just ("key : " ++ (show k)) else Nothing
grmapMaybeWithKey f (fromList [(5,"a"), (3,"b")]) == singleton 3 "key : 3"
gr</pre>
mapMaybeWithKey :: (k -> a -> Maybe b) -> Map k a -> Map k b

-- | <i>O(n)</i>. Map values and separate the <a>Left</a> and <a>Right</a>
grresults.
gr
gr<pre>
grlet f a = if a &lt; "c" then Left a else Right a
grmapEither f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (fromList [(3,"b"), (5,"a")], fromList [(1,"x"), (7,"z")])
gr
grmapEither (\ a -&gt; Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (empty, fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr</pre>
mapEither :: (a -> Either b c) -> Map k a -> (Map k b, Map k c)

-- | <i>O(n)</i>. Map keys/values and separate the <a>Left</a> and
gr<a>Right</a> results.
gr
gr<pre>
grlet f k a = if k &lt; 5 then Left (k * 2) else Right (a ++ a)
grmapEitherWithKey f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (fromList [(1,2), (3,6)], fromList [(5,"aa"), (7,"zz")])
gr
grmapEitherWithKey (\_ a -&gt; Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (empty, fromList [(1,"x"), (3,"b"), (5,"a"), (7,"z")])
gr</pre>
mapEitherWithKey :: (k -> a -> Either b c) -> Map k a -> (Map k b, Map k c)

-- | <i>O(log n)</i>. The expression (<tt><a>split</a> k map</tt>) is a
grpair <tt>(map1,map2)</tt> where the keys in <tt>map1</tt> are smaller
grthan <tt>k</tt> and the keys in <tt>map2</tt> larger than <tt>k</tt>.
grAny key equal to <tt>k</tt> is found in neither <tt>map1</tt> nor
gr<tt>map2</tt>.
gr
gr<pre>
grsplit 2 (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3,"b"), (5,"a")])
grsplit 3 (fromList [(5,"a"), (3,"b")]) == (empty, singleton 5 "a")
grsplit 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a")
grsplit 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", empty)
grsplit 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], empty)
gr</pre>
split :: Ord k => k -> Map k a -> (Map k a, Map k a)

-- | <i>O(log n)</i>. The expression (<tt><a>splitLookup</a> k map</tt>)
grsplits a map just like <a>split</a> but also returns <tt><a>lookup</a>
grk map</tt>.
gr
gr<pre>
grsplitLookup 2 (fromList [(5,"a"), (3,"b")]) == (empty, Nothing, fromList [(3,"b"), (5,"a")])
grsplitLookup 3 (fromList [(5,"a"), (3,"b")]) == (empty, Just "b", singleton 5 "a")
grsplitLookup 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Nothing, singleton 5 "a")
grsplitLookup 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Just "a", empty)
grsplitLookup 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], Nothing, empty)
gr</pre>
splitLookup :: Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)

-- | <i>O(1)</i>. Decompose a map into pieces based on the structure of the
grunderlying tree. This function is useful for consuming a map in
grparallel.
gr
grNo guarantee is made as to the sizes of the pieces; an internal, but
grdeterministic process determines this. However, it is guaranteed that
grthe pieces returned will be in ascending order (all elements in the
grfirst submap less than all elements in the second, and so on).
gr
grExamples:
gr
gr<pre>
grsplitRoot (fromList (zip [1..6] ['a'..])) ==
gr  [fromList [(1,'a'),(2,'b'),(3,'c')],fromList [(4,'d')],fromList [(5,'e'),(6,'f')]]
gr</pre>
gr
gr<pre>
grsplitRoot empty == []
gr</pre>
gr
grNote that the current implementation does not return more than three
grsubmaps, but you should not depend on this behaviour because it can
grchange in the future without notice.
splitRoot :: Map k b -> [Map k b]

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. This function is defined as
gr(<tt><a>isSubmapOf</a> = <a>isSubmapOfBy</a> (==)</tt>).
isSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. The expression
gr(<tt><a>isSubmapOfBy</a> f t1 t2</tt>) returns <a>True</a> if all keys
grin <tt>t1</tt> are in tree <tt>t2</tt>, and when <tt>f</tt> returns
gr<a>True</a> when applied to their respective values. For example, the
grfollowing expressions are all <a>True</a>:
gr
gr<pre>
grisSubmapOfBy (==) (fromList [('a',1)]) (fromList [('a',1),('b',2)])
grisSubmapOfBy (&lt;=) (fromList [('a',1)]) (fromList [('a',1),('b',2)])
grisSubmapOfBy (==) (fromList [('a',1),('b',2)]) (fromList [('a',1),('b',2)])
gr</pre>
gr
grBut the following are all <a>False</a>:
gr
gr<pre>
grisSubmapOfBy (==) (fromList [('a',2)]) (fromList [('a',1),('b',2)])
grisSubmapOfBy (&lt;)  (fromList [('a',1)]) (fromList [('a',1),('b',2)])
grisSubmapOfBy (==) (fromList [('a',1),('b',2)]) (fromList [('a',1)])
gr</pre>
isSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Is this a proper submap? (ie. a
grsubmap but not equal). Defined as (<tt><a>isProperSubmapOf</a> =
gr<a>isProperSubmapOfBy</a> (==)</tt>).
isProperSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Is this a proper submap? (ie. a
grsubmap but not equal). The expression (<tt><a>isProperSubmapOfBy</a> f
grm1 m2</tt>) returns <a>True</a> when <tt>m1</tt> and <tt>m2</tt> are
grnot equal, all keys in <tt>m1</tt> are in <tt>m2</tt>, and when
gr<tt>f</tt> returns <a>True</a> when applied to their respective
grvalues. For example, the following expressions are all <a>True</a>:
gr
gr<pre>
grisProperSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
grisProperSubmapOfBy (&lt;=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
gr</pre>
gr
grBut the following are all <a>False</a>:
gr
gr<pre>
grisProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])
grisProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
grisProperSubmapOfBy (&lt;)  (fromList [(1,1)])       (fromList [(1,1),(2,2)])
gr</pre>
isProperSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool

-- | <i>O(log n)</i>. Lookup the <i>index</i> of a key, which is its
grzero-based index in the sequence sorted by keys. The index is a number
grfrom <i>0</i> up to, but not including, the <a>size</a> of the map.
gr
gr<pre>
grisJust (lookupIndex 2 (fromList [(5,"a"), (3,"b")]))   == False
grfromJust (lookupIndex 3 (fromList [(5,"a"), (3,"b")])) == 0
grfromJust (lookupIndex 5 (fromList [(5,"a"), (3,"b")])) == 1
grisJust (lookupIndex 6 (fromList [(5,"a"), (3,"b")]))   == False
gr</pre>
lookupIndex :: Ord k => k -> Map k a -> Maybe Int

-- | <i>O(log n)</i>. Return the <i>index</i> of a key, which is its
grzero-based index in the sequence sorted by keys. The index is a number
grfrom <i>0</i> up to, but not including, the <a>size</a> of the map.
grCalls <a>error</a> when the key is not a <a>member</a> of the map.
gr
gr<pre>
grfindIndex 2 (fromList [(5,"a"), (3,"b")])    Error: element is not in the map
grfindIndex 3 (fromList [(5,"a"), (3,"b")]) == 0
grfindIndex 5 (fromList [(5,"a"), (3,"b")]) == 1
grfindIndex 6 (fromList [(5,"a"), (3,"b")])    Error: element is not in the map
gr</pre>
findIndex :: Ord k => k -> Map k a -> Int

-- | <i>O(log n)</i>. Retrieve an element by its <i>index</i>, i.e. by its
grzero-based index in the sequence sorted by keys. If the <i>index</i>
gris out of range (less than zero, greater or equal to <a>size</a> of
grthe map), <a>error</a> is called.
gr
gr<pre>
grelemAt 0 (fromList [(5,"a"), (3,"b")]) == (3,"b")
grelemAt 1 (fromList [(5,"a"), (3,"b")]) == (5, "a")
grelemAt 2 (fromList [(5,"a"), (3,"b")])    Error: index out of range
gr</pre>
elemAt :: Int -> Map k a -> (k, a)

-- | <i>O(log n)</i>. Update the element at <i>index</i>, i.e. by its
grzero-based index in the sequence sorted by keys. If the <i>index</i>
gris out of range (less than zero, greater or equal to <a>size</a> of
grthe map), <a>error</a> is called.
gr
gr<pre>
grupdateAt (\ _ _ -&gt; Just "x") 0    (fromList [(5,"a"), (3,"b")]) == fromList [(3, "x"), (5, "a")]
grupdateAt (\ _ _ -&gt; Just "x") 1    (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "x")]
grupdateAt (\ _ _ -&gt; Just "x") 2    (fromList [(5,"a"), (3,"b")])    Error: index out of range
grupdateAt (\ _ _ -&gt; Just "x") (-1) (fromList [(5,"a"), (3,"b")])    Error: index out of range
grupdateAt (\_ _  -&gt; Nothing)  0    (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
grupdateAt (\_ _  -&gt; Nothing)  1    (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grupdateAt (\_ _  -&gt; Nothing)  2    (fromList [(5,"a"), (3,"b")])    Error: index out of range
grupdateAt (\_ _  -&gt; Nothing)  (-1) (fromList [(5,"a"), (3,"b")])    Error: index out of range
gr</pre>
updateAt :: (k -> a -> Maybe a) -> Int -> Map k a -> Map k a

-- | <i>O(log n)</i>. Delete the element at <i>index</i>, i.e. by its
grzero-based index in the sequence sorted by keys. If the <i>index</i>
gris out of range (less than zero, greater or equal to <a>size</a> of
grthe map), <a>error</a> is called.
gr
gr<pre>
grdeleteAt 0  (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
grdeleteAt 1  (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grdeleteAt 2 (fromList [(5,"a"), (3,"b")])     Error: index out of range
grdeleteAt (-1) (fromList [(5,"a"), (3,"b")])  Error: index out of range
gr</pre>
deleteAt :: Int -> Map k a -> Map k a

-- | Take a given number of entries in key order, beginning with the
grsmallest keys.
gr
gr<pre>
grtake n = <a>fromDistinctAscList</a> . <a>take</a> n . <a>toAscList</a>
gr</pre>
take :: Int -> Map k a -> Map k a

-- | Drop a given number of entries in key order, beginning with the
grsmallest keys.
gr
gr<pre>
grdrop n = <a>fromDistinctAscList</a> . <a>drop</a> n . <a>toAscList</a>
gr</pre>
drop :: Int -> Map k a -> Map k a

-- | <i>O(log n)</i>. Split a map at a particular index.
gr
gr<pre>
grsplitAt !n !xs = (<a>take</a> n xs, <a>drop</a> n xs)
gr</pre>
splitAt :: Int -> Map k a -> (Map k a, Map k a)

-- | <i>O(log n)</i>. The minimal key of the map. Returns <a>Nothing</a> if
grthe map is empty.
gr
gr<pre>
grlookupMin (fromList [(5,"a"), (3,"b")]) == Just (3,"b")
grfindMin empty = Nothing
gr</pre>
lookupMin :: Map k a -> Maybe (k, a)

-- | <i>O(log n)</i>. The maximal key of the map. Returns <a>Nothing</a> if
grthe map is empty.
gr
gr<pre>
grlookupMax (fromList [(5,"a"), (3,"b")]) == Just (5,"a")
grlookupMax empty = Nothing
gr</pre>
lookupMax :: Map k a -> Maybe (k, a)

-- | <i>O(log n)</i>. The minimal key of the map. Calls <a>error</a> if the
grmap is empty.
gr
gr<pre>
grfindMin (fromList [(5,"a"), (3,"b")]) == (3,"b")
grfindMin empty                            Error: empty map has no minimal element
gr</pre>
findMin :: Map k a -> (k, a)
findMax :: Map k a -> (k, a)

-- | <i>O(log n)</i>. Delete the minimal key. Returns an empty map if the
grmap is empty.
gr
gr<pre>
grdeleteMin (fromList [(5,"a"), (3,"b"), (7,"c")]) == fromList [(5,"a"), (7,"c")]
grdeleteMin empty == empty
gr</pre>
deleteMin :: Map k a -> Map k a

-- | <i>O(log n)</i>. Delete the maximal key. Returns an empty map if the
grmap is empty.
gr
gr<pre>
grdeleteMax (fromList [(5,"a"), (3,"b"), (7,"c")]) == fromList [(3,"b"), (5,"a")]
grdeleteMax empty == empty
gr</pre>
deleteMax :: Map k a -> Map k a

-- | <i>O(log n)</i>. Delete and find the minimal element.
gr
gr<pre>
grdeleteFindMin (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((3,"b"), fromList[(5,"a"), (10,"c")])
grdeleteFindMin                                            Error: can not return the minimal element of an empty map
gr</pre>
deleteFindMin :: Map k a -> ((k, a), Map k a)

-- | <i>O(log n)</i>. Delete and find the maximal element.
gr
gr<pre>
grdeleteFindMax (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((10,"c"), fromList [(3,"b"), (5,"a")])
grdeleteFindMax empty                                      Error: can not return the maximal element of an empty map
gr</pre>
deleteFindMax :: Map k a -> ((k, a), Map k a)

-- | <i>O(log n)</i>. Update the value at the minimal key.
gr
gr<pre>
grupdateMin (\ a -&gt; Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "Xb"), (5, "a")]
grupdateMin (\ _ -&gt; Nothing)         (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateMin :: (a -> Maybe a) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Update the value at the maximal key.
gr
gr<pre>
grupdateMax (\ a -&gt; Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "Xa")]
grupdateMax (\ _ -&gt; Nothing)         (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr</pre>
updateMax :: (a -> Maybe a) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Update the value at the minimal key.
gr
gr<pre>
grupdateMinWithKey (\ k a -&gt; Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"3:b"), (5,"a")]
grupdateMinWithKey (\ _ _ -&gt; Nothing)                     (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateMinWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Update the value at the maximal key.
gr
gr<pre>
grupdateMaxWithKey (\ k a -&gt; Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"b"), (5,"5:a")]
grupdateMaxWithKey (\ _ _ -&gt; Nothing)                     (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr</pre>
updateMaxWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Retrieves the value associated with minimal key of
grthe map, and the map stripped of that element, or <a>Nothing</a> if
grpassed an empty map.
gr
gr<pre>
grminView (fromList [(5,"a"), (3,"b")]) == Just ("b", singleton 5 "a")
grminView empty == Nothing
gr</pre>
minView :: Map k a -> Maybe (a, Map k a)

-- | <i>O(log n)</i>. Retrieves the value associated with maximal key of
grthe map, and the map stripped of that element, or <a>Nothing</a> if
grpassed an empty map.
gr
gr<pre>
grmaxView (fromList [(5,"a"), (3,"b")]) == Just ("a", singleton 3 "b")
grmaxView empty == Nothing
gr</pre>
maxView :: Map k a -> Maybe (a, Map k a)

-- | <i>O(log n)</i>. Retrieves the minimal (key,value) pair of the map,
grand the map stripped of that element, or <a>Nothing</a> if passed an
grempty map.
gr
gr<pre>
grminViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((3,"b"), singleton 5 "a")
grminViewWithKey empty == Nothing
gr</pre>
minViewWithKey :: Map k a -> Maybe ((k, a), Map k a)

-- | <i>O(log n)</i>. Retrieves the maximal (key,value) pair of the map,
grand the map stripped of that element, or <a>Nothing</a> if passed an
grempty map.
gr
gr<pre>
grmaxViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((5,"a"), singleton 3 "b")
grmaxViewWithKey empty == Nothing
gr</pre>
maxViewWithKey :: Map k a -> Maybe ((k, a), Map k a)
data AreWeStrict
Strict :: AreWeStrict
Lazy :: AreWeStrict
atKeyImpl :: (Functor f, Ord k) => AreWeStrict -> k -> (Maybe a -> f (Maybe a)) -> Map k a -> f (Map k a)
atKeyPlain :: Ord k => AreWeStrict -> k -> (Maybe a -> Maybe a) -> Map k a -> Map k a
bin :: k -> a -> Map k a -> Map k a -> Map k a
balance :: k -> a -> Map k a -> Map k a -> Map k a
balanceL :: k -> a -> Map k a -> Map k a -> Map k a
balanceR :: k -> a -> Map k a -> Map k a -> Map k a
delta :: Int
insertMax :: k -> a -> Map k a -> Map k a
link :: k -> a -> Map k a -> Map k a -> Map k a
link2 :: Map k a -> Map k a -> Map k a
glue :: Map k a -> Map k a -> Map k a
data MaybeS a
NothingS :: MaybeS a
JustS :: !a -> MaybeS a

-- | Identity functor and monad. (a non-strict monad)
newtype Identity a
Identity :: a -> Identity a
[runIdentity] :: Identity a -> a

-- | Map covariantly over a <tt><a>WhenMissing</a> f k x</tt>.
mapWhenMissing :: (Applicative f, Monad f) => (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b

-- | Map covariantly over a <tt><a>WhenMatched</a> f k x y</tt>.
mapWhenMatched :: Functor f => (a -> b) -> WhenMatched f k x y a -> WhenMatched f k x y b

-- | Map contravariantly over a <tt><a>WhenMissing</a> f k _ x</tt>.
lmapWhenMissing :: (b -> a) -> WhenMissing f k a x -> WhenMissing f k b x

-- | Map contravariantly over a <tt><a>WhenMatched</a> f k _ y z</tt>.
contramapFirstWhenMatched :: (b -> a) -> WhenMatched f k a y z -> WhenMatched f k b y z

-- | Map contravariantly over a <tt><a>WhenMatched</a> f k x _ z</tt>.
contramapSecondWhenMatched :: (b -> a) -> WhenMatched f k x a z -> WhenMatched f k x b z

-- | Map covariantly over a <tt><a>WhenMissing</a> f k x</tt>, using only a
gr'Functor f' constraint.
mapGentlyWhenMissing :: Functor f => (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b

-- | Map covariantly over a <tt><a>WhenMatched</a> f k x</tt>, using only a
gr'Functor f' constraint.
mapGentlyWhenMatched :: Functor f => (a -> b) -> WhenMatched f k x y a -> WhenMatched f k x y b
instance GHC.Base.Functor f => GHC.Base.Functor (Data.Map.Internal.WhenMatched f k x y)
instance (GHC.Base.Monad f, GHC.Base.Applicative f) => Control.Category.Category (Data.Map.Internal.WhenMatched f k x)
instance (GHC.Base.Monad f, GHC.Base.Applicative f) => GHC.Base.Applicative (Data.Map.Internal.WhenMatched f k x y)
instance (GHC.Base.Monad f, GHC.Base.Applicative f) => GHC.Base.Monad (Data.Map.Internal.WhenMatched f k x y)
instance (GHC.Base.Applicative f, GHC.Base.Monad f) => GHC.Base.Functor (Data.Map.Internal.WhenMissing f k x)
instance (GHC.Base.Applicative f, GHC.Base.Monad f) => Control.Category.Category (Data.Map.Internal.WhenMissing f k)
instance (GHC.Base.Applicative f, GHC.Base.Monad f) => GHC.Base.Applicative (Data.Map.Internal.WhenMissing f k x)
instance (GHC.Base.Applicative f, GHC.Base.Monad f) => GHC.Base.Monad (Data.Map.Internal.WhenMissing f k x)
instance GHC.Classes.Ord k => GHC.Base.Monoid (Data.Map.Internal.Map k v)
instance GHC.Classes.Ord k => GHC.Base.Semigroup (Data.Map.Internal.Map k v)
instance (Data.Data.Data k, Data.Data.Data a, GHC.Classes.Ord k) => Data.Data.Data (Data.Map.Internal.Map k a)
instance GHC.Classes.Ord k => GHC.Exts.IsList (Data.Map.Internal.Map k v)
instance (GHC.Classes.Eq k, GHC.Classes.Eq a) => GHC.Classes.Eq (Data.Map.Internal.Map k a)
instance (GHC.Classes.Ord k, GHC.Classes.Ord v) => GHC.Classes.Ord (Data.Map.Internal.Map k v)
instance Data.Functor.Classes.Eq2 Data.Map.Internal.Map
instance GHC.Classes.Eq k => Data.Functor.Classes.Eq1 (Data.Map.Internal.Map k)
instance Data.Functor.Classes.Ord2 Data.Map.Internal.Map
instance GHC.Classes.Ord k => Data.Functor.Classes.Ord1 (Data.Map.Internal.Map k)
instance Data.Functor.Classes.Show2 Data.Map.Internal.Map
instance GHC.Show.Show k => Data.Functor.Classes.Show1 (Data.Map.Internal.Map k)
instance (GHC.Classes.Ord k, GHC.Read.Read k) => Data.Functor.Classes.Read1 (Data.Map.Internal.Map k)
instance GHC.Base.Functor (Data.Map.Internal.Map k)
instance Data.Traversable.Traversable (Data.Map.Internal.Map k)
instance Data.Foldable.Foldable (Data.Map.Internal.Map k)
instance (Control.DeepSeq.NFData k, Control.DeepSeq.NFData a) => Control.DeepSeq.NFData (Data.Map.Internal.Map k a)
instance (GHC.Classes.Ord k, GHC.Read.Read k, GHC.Read.Read e) => GHC.Read.Read (Data.Map.Internal.Map k e)
instance (GHC.Show.Show k, GHC.Show.Show a) => GHC.Show.Show (Data.Map.Internal.Map k a)


-- | This module defines an API for writing functions that merge two maps.
grThe key functions are <a>merge</a> and <a>mergeA</a>. Each of these
grcan be used with several different "merge tactics".
gr
grThe <a>merge</a> and <a>mergeA</a> functions are shared by the lazy
grand strict modules. Only the choice of merge tactics determines
grstrictness. If you use <a>mapMissing</a> from
gr<a>Data.Map.Merge.Strict</a> then the results will be forced before
grthey are inserted. If you use <a>mapMissing</a> from this module then
grthey will not.
gr
gr<h2>Efficiency note</h2>
gr
grThe <tt>Category</tt>, <a>Applicative</a>, and <a>Monad</a> instances
grfor <a>WhenMissing</a> tactics are included because they are valid.
grHowever, they are inefficient in many cases and should usually be
gravoided. The instances for <a>WhenMatched</a> tactics should not pose
grany major efficiency problems.
module Data.Map.Merge.Lazy

-- | A tactic for dealing with keys present in one map but not the other in
gr<a>merge</a>.
gr
grA tactic of type <tt> SimpleWhenMissing k x z </tt> is an abstract
grrepresentation of a function of type <tt> k -&gt; x -&gt; Maybe z
gr</tt>.
type SimpleWhenMissing = WhenMissing Identity

-- | A tactic for dealing with keys present in both maps in <a>merge</a>.
gr
grA tactic of type <tt> SimpleWhenMatched k x y z </tt> is an abstract
grrepresentation of a function of type <tt> k -&gt; x -&gt; y -&gt;
grMaybe z </tt>.
type SimpleWhenMatched = WhenMatched Identity

-- | Merge two maps.
gr
gr<tt>merge</tt> takes two <a>WhenMissing</a> tactics, a
gr<a>WhenMatched</a> tactic and two maps. It uses the tactics to merge
grthe maps. Its behavior is best understood via its fundamental tactics,
gr<a>mapMaybeMissing</a> and <a>zipWithMaybeMatched</a>.
gr
grConsider
gr
gr<pre>
grmerge (mapMaybeMissing g1)
gr             (mapMaybeMissing g2)
gr             (zipWithMaybeMatched f)
gr             m1 m2
gr</pre>
gr
grTake, for example,
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>), (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 = [(1, "one"), (2, "two"), (4, "three")]
gr</pre>
gr
gr<tt>merge</tt> will first '<tt>align'</tt> these maps by key:
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>),               (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 =           [(1, "one"), (2, "two"),          (4, "three")]
gr</pre>
gr
grIt will then pass the individual entries and pairs of entries to
gr<tt>g1</tt>, <tt>g2</tt>, or <tt>f</tt> as appropriate:
gr
gr<pre>
grmaybes = [g1 0 <tt>a</tt>, f 1 <tt>b</tt> "one", g2 2 "two", g1 3 <tt>c</tt>, f 4 <tt>d</tt> "three"]
gr</pre>
gr
grThis produces a <a>Maybe</a> for each key:
gr
gr<pre>
grkeys =     0        1          2           3        4
grresults = [Nothing, Just True, Just False, Nothing, Just True]
gr</pre>
gr
grFinally, the <tt>Just</tt> results are collected into a map:
gr
gr<pre>
grreturn value = [(1, True), (2, False), (4, True)]
gr</pre>
gr
grThe other tactics below are optimizations or simplifications of
gr<a>mapMaybeMissing</a> for special cases. Most importantly,
gr
gr<ul>
gr<li><a>dropMissing</a> drops all the keys.</li>
gr<li><a>preserveMissing</a> leaves all the entries alone.</li>
gr</ul>
gr
grWhen <a>merge</a> is given three arguments, it is inlined at the call
grsite. To prevent excessive inlining, you should typically use
gr<a>merge</a> to define your custom combining functions.
gr
grExamples:
gr
gr<pre>
grunionWithKey f = merge preserveMissing preserveMissing (zipWithMatched f)
gr</pre>
gr
gr<pre>
grintersectionWithKey f = merge dropMissing dropMissing (zipWithMatched f)
gr</pre>
gr
gr<pre>
grdifferenceWith f = merge diffPreserve diffDrop f
gr</pre>
gr
gr<pre>
grsymmetricDifference = merge diffPreserve diffPreserve (\ _ _ _ -&gt; Nothing)
gr</pre>
gr
gr<pre>
grmapEachPiece f g h = merge (diffMapWithKey f) (diffMapWithKey g)
gr</pre>
merge :: Ord k => SimpleWhenMissing k a c -> SimpleWhenMissing k b c -> SimpleWhenMatched k a b c -> Map k a -> Map k b -> Map k c

-- | When a key is found in both maps, apply a function to the key and
grvalues and maybe use the result in the merged map.
gr
gr<pre>
grzipWithMaybeMatched :: (k -&gt; x -&gt; y -&gt; Maybe z)
gr                    -&gt; SimpleWhenMatched k x y z
gr</pre>
zipWithMaybeMatched :: Applicative f => (k -> x -> y -> Maybe z) -> WhenMatched f k x y z

-- | When a key is found in both maps, apply a function to the key and
grvalues and use the result in the merged map.
gr
gr<pre>
grzipWithMatched :: (k -&gt; x -&gt; y -&gt; z)
gr               -&gt; SimpleWhenMatched k x y z
gr</pre>
zipWithMatched :: Applicative f => (k -> x -> y -> z) -> WhenMatched f k x y z

-- | Map over the entries whose keys are missing from the other map,
groptionally removing some. This is the most powerful
gr<a>SimpleWhenMissing</a> tactic, but others are usually more
grefficient.
gr
gr<pre>
grmapMaybeMissing :: (k -&gt; x -&gt; Maybe y) -&gt; SimpleWhenMissing k x y
gr</pre>
gr
gr<pre>
grmapMaybeMissing f = traverseMaybeMissing (\k x -&gt; pure (f k x))
gr</pre>
gr
grbut <tt>mapMaybeMissing</tt> uses fewer unnecessary <a>Applicative</a>
groperations.
mapMaybeMissing :: Applicative f => (k -> x -> Maybe y) -> WhenMissing f k x y

-- | Drop all the entries whose keys are missing from the other map.
gr
gr<pre>
grdropMissing :: SimpleWhenMissing k x y
gr</pre>
gr
gr<pre>
grdropMissing = mapMaybeMissing (\_ _ -&gt; Nothing)
gr</pre>
gr
grbut <tt>dropMissing</tt> is much faster.
dropMissing :: Applicative f => WhenMissing f k x y

-- | Preserve, unchanged, the entries whose keys are missing from the other
grmap.
gr
gr<pre>
grpreserveMissing :: SimpleWhenMissing k x x
gr</pre>
gr
gr<pre>
grpreserveMissing = Merge.Lazy.mapMaybeMissing (\_ x -&gt; Just x)
gr</pre>
gr
grbut <tt>preserveMissing</tt> is much faster.
preserveMissing :: Applicative f => WhenMissing f k x x

-- | Map over the entries whose keys are missing from the other map.
gr
gr<pre>
grmapMissing :: (k -&gt; x -&gt; y) -&gt; SimpleWhenMissing k x y
gr</pre>
gr
gr<pre>
grmapMissing f = mapMaybeMissing (\k x -&gt; Just $ f k x)
gr</pre>
gr
grbut <tt>mapMissing</tt> is somewhat faster.
mapMissing :: Applicative f => (k -> x -> y) -> WhenMissing f k x y

-- | Filter the entries whose keys are missing from the other map.
gr
gr<pre>
grfilterMissing :: (k -&gt; x -&gt; Bool) -&gt; SimpleWhenMissing k x x
gr</pre>
gr
gr<pre>
grfilterMissing f = Merge.Lazy.mapMaybeMissing $ \k x -&gt; guard (f k x) *&gt; Just x
gr</pre>
gr
grbut this should be a little faster.
filterMissing :: Applicative f => (k -> x -> Bool) -> WhenMissing f k x x

-- | A tactic for dealing with keys present in one map but not the other in
gr<a>merge</a> or <a>mergeA</a>.
gr
grA tactic of type <tt> WhenMissing f k x z </tt> is an abstract
grrepresentation of a function of type <tt> k -&gt; x -&gt; f (Maybe z)
gr</tt>.
data WhenMissing f k x y

-- | A tactic for dealing with keys present in both maps in <a>merge</a> or
gr<a>mergeA</a>.
gr
grA tactic of type <tt> WhenMatched f k x y z </tt> is an abstract
grrepresentation of a function of type <tt> k -&gt; x -&gt; y -&gt; f
gr(Maybe z) </tt>.
data WhenMatched f k x y z

-- | An applicative version of <a>merge</a>.
gr
gr<tt>mergeA</tt> takes two <a>WhenMissing</a> tactics, a
gr<a>WhenMatched</a> tactic and two maps. It uses the tactics to merge
grthe maps. Its behavior is best understood via its fundamental tactics,
gr<a>traverseMaybeMissing</a> and <a>zipWithMaybeAMatched</a>.
gr
grConsider
gr
gr<pre>
grmergeA (traverseMaybeMissing g1)
gr              (traverseMaybeMissing g2)
gr              (zipWithMaybeAMatched f)
gr              m1 m2
gr</pre>
gr
grTake, for example,
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>), (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 = [(1, "one"), (2, "two"), (4, "three")]
gr</pre>
gr
gr<tt>mergeA</tt> will first '<tt>align'</tt> these maps by key:
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>),               (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 =           [(1, "one"), (2, "two"),          (4, "three")]
gr</pre>
gr
grIt will then pass the individual entries and pairs of entries to
gr<tt>g1</tt>, <tt>g2</tt>, or <tt>f</tt> as appropriate:
gr
gr<pre>
gractions = [g1 0 <tt>a</tt>, f 1 <tt>b</tt> "one", g2 2 "two", g1 3 <tt>c</tt>, f 4 <tt>d</tt> "three"]
gr</pre>
gr
grNext, it will perform the actions in the <tt>actions</tt> list in
grorder from left to right.
gr
gr<pre>
grkeys =     0        1          2           3        4
grresults = [Nothing, Just True, Just False, Nothing, Just True]
gr</pre>
gr
grFinally, the <tt>Just</tt> results are collected into a map:
gr
gr<pre>
grreturn value = [(1, True), (2, False), (4, True)]
gr</pre>
gr
grThe other tactics below are optimizations or simplifications of
gr<a>traverseMaybeMissing</a> for special cases. Most importantly,
gr
gr<ul>
gr<li><a>dropMissing</a> drops all the keys.</li>
gr<li><a>preserveMissing</a> leaves all the entries alone.</li>
gr<li><a>mapMaybeMissing</a> does not use the <a>Applicative</a>
grcontext.</li>
gr</ul>
gr
grWhen <a>mergeA</a> is given three arguments, it is inlined at the call
grsite. To prevent excessive inlining, you should generally only use
gr<a>mergeA</a> to define custom combining functions.
mergeA :: (Applicative f, Ord k) => WhenMissing f k a c -> WhenMissing f k b c -> WhenMatched f k a b c -> Map k a -> Map k b -> f (Map k c)

-- | When a key is found in both maps, apply a function to the key and
grvalues, perform the resulting action, and maybe use the result in the
grmerged map.
gr
grThis is the fundamental <a>WhenMatched</a> tactic.
zipWithMaybeAMatched :: (k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z

-- | When a key is found in both maps, apply a function to the key and
grvalues to produce an action and use its result in the merged map.
zipWithAMatched :: Applicative f => (k -> x -> y -> f z) -> WhenMatched f k x y z

-- | Traverse over the entries whose keys are missing from the other map,
groptionally producing values to put in the result. This is the most
grpowerful <a>WhenMissing</a> tactic, but others are usually more
grefficient.
traverseMaybeMissing :: Applicative f => (k -> x -> f (Maybe y)) -> WhenMissing f k x y

-- | Traverse over the entries whose keys are missing from the other map.
traverseMissing :: Applicative f => (k -> x -> f y) -> WhenMissing f k x y

-- | Filter the entries whose keys are missing from the other map using
grsome <a>Applicative</a> action.
gr
gr<pre>
grfilterAMissing f = Merge.Lazy.traverseMaybeMissing $
gr  k x -&gt; (b -&gt; guard b *&gt; Just x) <a>$</a> f k x
gr</pre>
gr
grbut this should be a little faster.
filterAMissing :: Applicative f => (k -> x -> f Bool) -> WhenMissing f k x x

-- | Map covariantly over a <tt><a>WhenMissing</a> f k x</tt>.
mapWhenMissing :: (Applicative f, Monad f) => (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b

-- | Map covariantly over a <tt><a>WhenMatched</a> f k x y</tt>.
mapWhenMatched :: Functor f => (a -> b) -> WhenMatched f k x y a -> WhenMatched f k x y b

-- | Map contravariantly over a <tt><a>WhenMissing</a> f k _ x</tt>.
lmapWhenMissing :: (b -> a) -> WhenMissing f k a x -> WhenMissing f k b x

-- | Map contravariantly over a <tt><a>WhenMatched</a> f k _ y z</tt>.
contramapFirstWhenMatched :: (b -> a) -> WhenMatched f k a y z -> WhenMatched f k b y z

-- | Map contravariantly over a <tt><a>WhenMatched</a> f k x _ z</tt>.
contramapSecondWhenMatched :: (b -> a) -> WhenMatched f k x a z -> WhenMatched f k x b z

-- | Along with zipWithMaybeAMatched, witnesses the isomorphism between
gr<tt>WhenMatched f k x y z</tt> and <tt>k -&gt; x -&gt; y -&gt; f
gr(Maybe z)</tt>.
runWhenMatched :: WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)

-- | Along with traverseMaybeMissing, witnesses the isomorphism between
gr<tt>WhenMissing f k x y</tt> and <tt>k -&gt; x -&gt; f (Maybe y)</tt>.
runWhenMissing :: WhenMissing f k x y -> k -> x -> f (Maybe y)

module Data.Map.Internal.Debug

-- | <i>O(n)</i>. Show the tree that implements the map. The tree is shown
grin a compressed, hanging format. See <a>showTreeWith</a>.
showTree :: (Show k, Show a) => Map k a -> String

-- | <i>O(n)</i>. The expression (<tt><a>showTreeWith</a> showelem hang
grwide map</tt>) shows the tree that implements the map. Elements are
grshown using the <tt>showElem</tt> function. If <tt>hang</tt> is
gr<a>True</a>, a <i>hanging</i> tree is shown otherwise a rotated tree
gris shown. If <tt>wide</tt> is <a>True</a>, an extra wide version is
grshown.
gr
gr<pre>
grMap&gt; let t = fromDistinctAscList [(x,()) | x &lt;- [1..5]]
grMap&gt; putStrLn $ showTreeWith (\k x -&gt; show (k,x)) True False t
gr(4,())
gr+--(2,())
gr|  +--(1,())
gr|  +--(3,())
gr+--(5,())
gr
grMap&gt; putStrLn $ showTreeWith (\k x -&gt; show (k,x)) True True t
gr(4,())
gr|
gr+--(2,())
gr|  |
gr|  +--(1,())
gr|  |
gr|  +--(3,())
gr|
gr+--(5,())
gr
grMap&gt; putStrLn $ showTreeWith (\k x -&gt; show (k,x)) False True t
gr+--(5,())
gr|
gr(4,())
gr|
gr|  +--(3,())
gr|  |
gr+--(2,())
gr   |
gr   +--(1,())
gr</pre>
showTreeWith :: (k -> a -> String) -> Bool -> Bool -> Map k a -> String
showsTree :: (k -> a -> String) -> Bool -> [String] -> [String] -> Map k a -> ShowS
showsTreeHang :: (k -> a -> String) -> Bool -> [String] -> Map k a -> ShowS
showWide :: Bool -> [String] -> String -> String
showsBars :: [String] -> ShowS
node :: String
withBar :: [String] -> [String]
withEmpty :: [String] -> [String]

-- | <i>O(n)</i>. Test if the internal map structure is valid.
gr
gr<pre>
grvalid (fromAscList [(3,"b"), (5,"a")]) == True
grvalid (fromAscList [(5,"a"), (3,"b")]) == False
gr</pre>
valid :: Ord k => Map k a -> Bool

-- | Test if the keys are ordered correctly.
ordered :: Ord a => Map a b -> Bool

-- | Test if a map obeys the balance invariants.
balanced :: Map k a -> Bool

-- | Test if each node of a map reports its size correctly.
validsize :: Map a b -> Bool


-- | <h1>WARNING</h1>
gr
grThis module is considered <b>internal</b>.
gr
grThe Package Versioning Policy <b>does not apply</b>.
gr
grThis contents of this module may change <b>in any way whatsoever</b>
grand <b>without any warning</b> between minor versions of this package.
gr
grAuthors importing this module are expected to track development
grclosely.
gr
gr<h1>Description</h1>
gr
grAn efficient implementation of ordered maps from keys to values
gr(dictionaries).
gr
grAPI of this module is strict in both the keys and the values. If you
grneed value-lazy maps, use <a>Data.Map.Lazy</a> instead. The <a>Map</a>
grtype is shared between the lazy and strict modules, meaning that the
grsame <a>Map</a> value can be passed to functions in both modules
gr(although that is rarely needed).
gr
grThese modules are intended to be imported qualified, to avoid name
grclashes with Prelude functions, e.g.
gr
gr<pre>
grimport qualified Data.Map.Strict as Map
gr</pre>
gr
grThe implementation of <a>Map</a> is based on <i>size balanced</i>
grbinary trees (or trees of <i>bounded balance</i>) as described by:
gr
gr<ul>
gr<li>Stephen Adams, "<i>Efficient sets: a balancing act</i>", Journal
grof Functional Programming 3(4):553-562, October 1993,
gr<a>http://www.swiss.ai.mit.edu/~adams/BB/</a>.</li>
gr<li>J. Nievergelt and E.M. Reingold, "<i>Binary search trees of
grbounded balance</i>", SIAM journal of computing 2(1), March 1973.</li>
gr</ul>
gr
grBounds for <a>union</a>, <a>intersection</a>, and <a>difference</a>
grare as given by
gr
gr<ul>
gr<li>Guy Blelloch, Daniel Ferizovic, and Yihan Sun, "<i>Just Join for
grParallel Ordered Sets</i>",
gr<a>https://arxiv.org/abs/1602.02120v3</a>.</li>
gr</ul>
gr
grNote that the implementation is <i>left-biased</i> -- the elements of
gra first argument are always preferred to the second, for example in
gr<a>union</a> or <a>insert</a>.
gr
gr<i>Warning</i>: The size of the map must not exceed
gr<tt>maxBound::Int</tt>. Violation of this condition is not detected
grand if the size limit is exceeded, its behaviour is undefined.
gr
grOperation comments contain the operation time complexity in the Big-O
grnotation (<a>http://en.wikipedia.org/wiki/Big_O_notation</a>).
gr
grBe aware that the <a>Functor</a>, <a>Traversable</a> and <tt>Data</tt>
grinstances are the same as for the <a>Data.Map.Lazy</a> module, so if
grthey are used on strict maps, the resulting maps will be lazy.
module Data.Map.Strict.Internal

-- | A Map from keys <tt>k</tt> to values <tt>a</tt>.
data Map k a
Bin :: {-# UNPACK #-} !Size -> !k -> a -> !(Map k a) -> !(Map k a) -> Map k a
Tip :: Map k a
type Size = Int

-- | <i>O(log n)</i>. Find the value at a key. Calls <a>error</a> when the
grelement can not be found.
gr
gr<pre>
grfromList [(5,'a'), (3,'b')] ! 1    Error: element not in the map
grfromList [(5,'a'), (3,'b')] ! 5 == 'a'
gr</pre>
(!) :: Ord k => Map k a -> k -> a
infixl 9 !

-- | <i>O(log n)</i>. Find the value at a key. Returns <a>Nothing</a> when
grthe element can not be found.
gr
gr<pre>
grfromList [(5, 'a'), (3, 'b')] !? 1 == Nothing
gr</pre>
gr
gr<pre>
grfromList [(5, 'a'), (3, 'b')] !? 5 == Just 'a'
gr</pre>
(!?) :: Ord k => Map k a -> k -> Maybe a
infixl 9 !?

-- | Same as <a>difference</a>.
(\\) :: Ord k => Map k a -> Map k b -> Map k a
infixl 9 \\

-- | <i>O(1)</i>. Is the map empty?
gr
gr<pre>
grData.Map.null (empty)           == True
grData.Map.null (singleton 1 'a') == False
gr</pre>
null :: Map k a -> Bool

-- | <i>O(1)</i>. The number of elements in the map.
gr
gr<pre>
grsize empty                                   == 0
grsize (singleton 1 'a')                       == 1
grsize (fromList([(1,'a'), (2,'c'), (3,'b')])) == 3
gr</pre>
size :: Map k a -> Int

-- | <i>O(log n)</i>. Is the key a member of the map? See also
gr<a>notMember</a>.
gr
gr<pre>
grmember 5 (fromList [(5,'a'), (3,'b')]) == True
grmember 1 (fromList [(5,'a'), (3,'b')]) == False
gr</pre>
member :: Ord k => k -> Map k a -> Bool

-- | <i>O(log n)</i>. Is the key not a member of the map? See also
gr<a>member</a>.
gr
gr<pre>
grnotMember 5 (fromList [(5,'a'), (3,'b')]) == False
grnotMember 1 (fromList [(5,'a'), (3,'b')]) == True
gr</pre>
notMember :: Ord k => k -> Map k a -> Bool

-- | <i>O(log n)</i>. Lookup the value at a key in the map.
gr
grThe function will return the corresponding value as <tt>(<a>Just</a>
grvalue)</tt>, or <a>Nothing</a> if the key isn't in the map.
gr
grAn example of using <tt>lookup</tt>:
gr
gr<pre>
grimport Prelude hiding (lookup)
grimport Data.Map
gr
gremployeeDept = fromList([("John","Sales"), ("Bob","IT")])
grdeptCountry = fromList([("IT","USA"), ("Sales","France")])
grcountryCurrency = fromList([("USA", "Dollar"), ("France", "Euro")])
gr
gremployeeCurrency :: String -&gt; Maybe String
gremployeeCurrency name = do
gr    dept &lt;- lookup name employeeDept
gr    country &lt;- lookup dept deptCountry
gr    lookup country countryCurrency
gr
grmain = do
gr    putStrLn $ "John's currency: " ++ (show (employeeCurrency "John"))
gr    putStrLn $ "Pete's currency: " ++ (show (employeeCurrency "Pete"))
gr</pre>
gr
grThe output of this program:
gr
gr<pre>
grJohn's currency: Just "Euro"
grPete's currency: Nothing
gr</pre>
lookup :: Ord k => k -> Map k a -> Maybe a

-- | <i>O(log n)</i>. The expression <tt>(<a>findWithDefault</a> def k
grmap)</tt> returns the value at key <tt>k</tt> or returns default value
gr<tt>def</tt> when the key is not in the map.
gr
gr<pre>
grfindWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x'
grfindWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a'
gr</pre>
findWithDefault :: Ord k => a -> k -> Map k a -> a

-- | <i>O(log n)</i>. Find largest key smaller than the given one and
grreturn the corresponding (key, value) pair.
gr
gr<pre>
grlookupLT 3 (fromList [(3,'a'), (5,'b')]) == Nothing
grlookupLT 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
gr</pre>
lookupLT :: Ord k => k -> Map k v -> Maybe (k, v)

-- | <i>O(log n)</i>. Find smallest key greater than the given one and
grreturn the corresponding (key, value) pair.
gr
gr<pre>
grlookupGT 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
grlookupGT 5 (fromList [(3,'a'), (5,'b')]) == Nothing
gr</pre>
lookupGT :: Ord k => k -> Map k v -> Maybe (k, v)

-- | <i>O(log n)</i>. Find largest key smaller or equal to the given one
grand return the corresponding (key, value) pair.
gr
gr<pre>
grlookupLE 2 (fromList [(3,'a'), (5,'b')]) == Nothing
grlookupLE 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
grlookupLE 5 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
gr</pre>
lookupLE :: Ord k => k -> Map k v -> Maybe (k, v)

-- | <i>O(log n)</i>. Find smallest key greater or equal to the given one
grand return the corresponding (key, value) pair.
gr
gr<pre>
grlookupGE 3 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
grlookupGE 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
grlookupGE 6 (fromList [(3,'a'), (5,'b')]) == Nothing
gr</pre>
lookupGE :: Ord k => k -> Map k v -> Maybe (k, v)

-- | <i>O(1)</i>. The empty map.
gr
gr<pre>
grempty      == fromList []
grsize empty == 0
gr</pre>
empty :: Map k a

-- | <i>O(1)</i>. A map with a single element.
gr
gr<pre>
grsingleton 1 'a'        == fromList [(1, 'a')]
grsize (singleton 1 'a') == 1
gr</pre>
singleton :: k -> a -> Map k a

-- | <i>O(log n)</i>. Insert a new key and value in the map. If the key is
gralready present in the map, the associated value is replaced with the
grsupplied value. <a>insert</a> is equivalent to <tt><a>insertWith</a>
gr<a>const</a></tt>.
gr
gr<pre>
grinsert 5 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'x')]
grinsert 7 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'a'), (7, 'x')]
grinsert 5 'x' empty                         == singleton 5 'x'
gr</pre>
insert :: Ord k => k -> a -> Map k a -> Map k a

-- | <i>O(log n)</i>. Insert with a function, combining new value and old
grvalue. <tt><a>insertWith</a> f key value mp</tt> will insert the pair
gr(key, value) into <tt>mp</tt> if key does not exist in the map. If the
grkey does exist, the function will insert the pair <tt>(key, f
grnew_value old_value)</tt>.
gr
gr<pre>
grinsertWith (++) 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "xxxa")]
grinsertWith (++) 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
grinsertWith (++) 5 "xxx" empty                         == singleton 5 "xxx"
gr</pre>
insertWith :: Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a

-- | <i>O(log n)</i>. Insert with a function, combining key, new value and
grold value. <tt><a>insertWithKey</a> f key value mp</tt> will insert
grthe pair (key, value) into <tt>mp</tt> if key does not exist in the
grmap. If the key does exist, the function will insert the pair
gr<tt>(key,f key new_value old_value)</tt>. Note that the key passed to
grf is the same key passed to <a>insertWithKey</a>.
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grinsertWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:xxx|a")]
grinsertWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
grinsertWithKey f 5 "xxx" empty                         == singleton 5 "xxx"
gr</pre>
insertWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a

-- | <i>O(log n)</i>. Combines insert operation with old value retrieval.
grThe expression (<tt><a>insertLookupWithKey</a> f k x map</tt>) is a
grpair where the first element is equal to (<tt><a>lookup</a> k
grmap</tt>) and the second element equal to (<tt><a>insertWithKey</a> f
grk x map</tt>).
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grinsertLookupWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:xxx|a")])
grinsertLookupWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a"), (7, "xxx")])
grinsertLookupWithKey f 5 "xxx" empty                         == (Nothing,  singleton 5 "xxx")
gr</pre>
gr
grThis is how to define <tt>insertLookup</tt> using
gr<tt>insertLookupWithKey</tt>:
gr
gr<pre>
grlet insertLookup kx x t = insertLookupWithKey (\_ a _ -&gt; a) kx x t
grinsertLookup 5 "x" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "x")])
grinsertLookup 7 "x" (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a"), (7, "x")])
gr</pre>
insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a, Map k a)

-- | <i>O(log n)</i>. Delete a key and its value from the map. When the key
gris not a member of the map, the original map is returned.
gr
gr<pre>
grdelete 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grdelete 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grdelete 5 empty                         == empty
gr</pre>
delete :: Ord k => k -> Map k a -> Map k a

-- | <i>O(log n)</i>. Update a value at a specific key with the result of
grthe provided function. When the key is not a member of the map, the
groriginal map is returned.
gr
gr<pre>
gradjust ("new " ++) 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
gradjust ("new " ++) 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gradjust ("new " ++) 7 empty                         == empty
gr</pre>
adjust :: Ord k => (a -> a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. Adjust a value at a specific key. When the key is not
gra member of the map, the original map is returned.
gr
gr<pre>
grlet f key x = (show key) ++ ":new " ++ x
gradjustWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
gradjustWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gradjustWithKey f 7 empty                         == empty
gr</pre>
adjustWithKey :: Ord k => (k -> a -> a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. The expression (<tt><a>update</a> f k map</tt>)
grupdates the value <tt>x</tt> at <tt>k</tt> (if it is in the map). If
gr(<tt>f x</tt>) is <a>Nothing</a>, the element is deleted. If it is
gr(<tt><a>Just</a> y</tt>), the key <tt>k</tt> is bound to the new value
gr<tt>y</tt>.
gr
gr<pre>
grlet f x = if x == "a" then Just "new a" else Nothing
grupdate f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
grupdate f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grupdate f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
update :: Ord k => (a -> Maybe a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. The expression (<tt><a>updateWithKey</a> f k
grmap</tt>) updates the value <tt>x</tt> at <tt>k</tt> (if it is in the
grmap). If (<tt>f k x</tt>) is <a>Nothing</a>, the element is deleted.
grIf it is (<tt><a>Just</a> y</tt>), the key <tt>k</tt> is bound to the
grnew value <tt>y</tt>.
gr
gr<pre>
grlet f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
grupdateWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
grupdateWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grupdateWithKey f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. Lookup and update. See also <a>updateWithKey</a>. The
grfunction returns changed value, if it is updated. Returns the original
grkey value if the map entry is deleted.
gr
gr<pre>
grlet f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
grupdateLookupWithKey f 5 (fromList [(5,"a"), (3,"b")]) == (Just "5:new a", fromList [(3, "b"), (5, "5:new a")])
grupdateLookupWithKey f 7 (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a")])
grupdateLookupWithKey f 3 (fromList [(5,"a"), (3,"b")]) == (Just "b", singleton 5 "a")
gr</pre>
updateLookupWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> (Maybe a, Map k a)

-- | <i>O(log n)</i>. The expression (<tt><a>alter</a> f k map</tt>) alters
grthe value <tt>x</tt> at <tt>k</tt>, or absence thereof. <a>alter</a>
grcan be used to insert, delete, or update a value in a <a>Map</a>. In
grshort : <tt><a>lookup</a> k (<a>alter</a> f k m) = f (<a>lookup</a> k
grm)</tt>.
gr
gr<pre>
grlet f _ = Nothing
gralter f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gralter f 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr
grlet f _ = Just "c"
gralter f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "c")]
gralter f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "c")]
gr</pre>
alter :: Ord k => (Maybe a -> Maybe a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. The expression (<tt><a>alterF</a> f k map</tt>)
gralters the value <tt>x</tt> at <tt>k</tt>, or absence thereof.
gr<a>alterF</a> can be used to inspect, insert, delete, or update a
grvalue in a <a>Map</a>. In short: <tt><a>lookup</a> k &lt;$&gt;
gr<a>alterF</a> f k m = f (<a>lookup</a> k m)</tt>.
gr
grExample:
gr
gr<pre>
grinteractiveAlter :: Int -&gt; Map Int String -&gt; IO (Map Int String)
grinteractiveAlter k m = alterF f k m where
gr  f Nothing -&gt; do
gr     putStrLn $ show k ++
gr         " was not found in the map. Would you like to add it?"
gr     getUserResponse1 :: IO (Maybe String)
gr  f (Just old) -&gt; do
gr     putStrLn "The key is currently bound to " ++ show old ++
gr         ". Would you like to change or delete it?"
gr     getUserresponse2 :: IO (Maybe String)
gr</pre>
gr
gr<a>alterF</a> is the most general operation for working with an
grindividual key that may or may not be in a given map. When used with
grtrivial functors like <a>Identity</a> and <a>Const</a>, it is often
grslightly slower than more specialized combinators like <a>lookup</a>
grand <a>insert</a>. However, when the functor is non-trivial and key
grcomparison is not particularly cheap, it is the fastest way.
gr
grNote on rewrite rules:
gr
grThis module includes GHC rewrite rules to optimize <a>alterF</a> for
grthe <a>Const</a> and <a>Identity</a> functors. In general, these rules
grimprove performance. The sole exception is that when using
gr<a>Identity</a>, deleting a key that is already absent takes longer
grthan it would without the rules. If you expect this to occur a very
grlarge fraction of the time, you might consider using a private copy of
grthe <a>Identity</a> type.
gr
grNote: <a>alterF</a> is a flipped version of the <tt>at</tt> combinator
grfrom <a>At</a>.
alterF :: (Functor f, Ord k) => (Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. The expression (<tt><a>union</a>
grt1 t2</tt>) takes the left-biased union of <tt>t1</tt> and
gr<tt>t2</tt>. It prefers <tt>t1</tt> when duplicate keys are
grencountered, i.e. (<tt><a>union</a> == <a>unionWith</a>
gr<a>const</a></tt>).
gr
gr<pre>
grunion (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "a"), (7, "C")]
gr</pre>
union :: Ord k => Map k a -> Map k a -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Union with a combining function.
gr
gr<pre>
grunionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "aA"), (7, "C")]
gr</pre>
unionWith :: Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Union with a combining function.
gr
gr<pre>
grlet f key left_value right_value = (show key) ++ ":" ++ left_value ++ "|" ++ right_value
grunionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "5:a|A"), (7, "C")]
gr</pre>
unionWithKey :: Ord k => (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a

-- | The union of a list of maps: (<tt><a>unions</a> == <a>foldl</a>
gr<a>union</a> <a>empty</a></tt>).
gr
gr<pre>
grunions [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
gr    == fromList [(3, "b"), (5, "a"), (7, "C")]
grunions [(fromList [(5, "A3"), (3, "B3")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "a"), (3, "b")])]
gr    == fromList [(3, "B3"), (5, "A3"), (7, "C")]
gr</pre>
unions :: Ord k => [Map k a] -> Map k a

-- | The union of a list of maps, with a combining operation:
gr(<tt><a>unionsWith</a> f == <a>foldl</a> (<a>unionWith</a> f)
gr<a>empty</a></tt>).
gr
gr<pre>
grunionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
gr    == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")]
gr</pre>
unionsWith :: Ord k => (a -> a -> a) -> [Map k a] -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Difference of two maps. Return
grelements of the first map not existing in the second map.
gr
gr<pre>
grdifference (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 3 "b"
gr</pre>
difference :: Ord k => Map k a -> Map k b -> Map k a

-- | <i>O(n+m)</i>. Difference with a combining function. When two equal
grkeys are encountered, the combining function is applied to the values
grof these keys. If it returns <a>Nothing</a>, the element is discarded
gr(proper set difference). If it returns (<tt><a>Just</a> y</tt>), the
grelement is updated with a new value <tt>y</tt>.
gr
gr<pre>
grlet f al ar = if al == "b" then Just (al ++ ":" ++ ar) else Nothing
grdifferenceWith f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (7, "C")])
gr    == singleton 3 "b:B"
gr</pre>
differenceWith :: Ord k => (a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a

-- | <i>O(n+m)</i>. Difference with a combining function. When two equal
grkeys are encountered, the combining function is applied to the key and
grboth values. If it returns <a>Nothing</a>, the element is discarded
gr(proper set difference). If it returns (<tt><a>Just</a> y</tt>), the
grelement is updated with a new value <tt>y</tt>.
gr
gr<pre>
grlet f k al ar = if al == "b" then Just ((show k) ++ ":" ++ al ++ "|" ++ ar) else Nothing
grdifferenceWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (10, "C")])
gr    == singleton 3 "3:b|B"
gr</pre>
differenceWithKey :: Ord k => (k -> a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Intersection of two maps. Return
grdata in the first map for the keys existing in both maps.
gr(<tt><a>intersection</a> m1 m2 == <a>intersectionWith</a> <a>const</a>
grm1 m2</tt>).
gr
gr<pre>
grintersection (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "a"
gr</pre>
intersection :: Ord k => Map k a -> Map k b -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Intersection with a combining
grfunction.
gr
gr<pre>
grintersectionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "aA"
gr</pre>
intersectionWith :: Ord k => (a -> b -> c) -> Map k a -> Map k b -> Map k c

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Intersection with a combining
grfunction.
gr
gr<pre>
grlet f k al ar = (show k) ++ ":" ++ al ++ "|" ++ ar
grintersectionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "5:a|A"
gr</pre>
intersectionWithKey :: Ord k => (k -> a -> b -> c) -> Map k a -> Map k b -> Map k c

-- | A tactic for dealing with keys present in one map but not the other in
gr<a>merge</a>.
gr
grA tactic of type <tt> SimpleWhenMissing k x z </tt> is an abstract
grrepresentation of a function of type <tt> k -&gt; x -&gt; Maybe z
gr</tt>.
type SimpleWhenMissing = WhenMissing Identity

-- | A tactic for dealing with keys present in both maps in <a>merge</a>.
gr
grA tactic of type <tt> SimpleWhenMatched k x y z </tt> is an abstract
grrepresentation of a function of type <tt> k -&gt; x -&gt; y -&gt;
grMaybe z </tt>.
type SimpleWhenMatched = WhenMatched Identity

-- | Merge two maps.
gr
gr<tt>merge</tt> takes two <a>WhenMissing</a> tactics, a
gr<a>WhenMatched</a> tactic and two maps. It uses the tactics to merge
grthe maps. Its behavior is best understood via its fundamental tactics,
gr<a>mapMaybeMissing</a> and <a>zipWithMaybeMatched</a>.
gr
grConsider
gr
gr<pre>
grmerge (mapMaybeMissing g1)
gr             (mapMaybeMissing g2)
gr             (zipWithMaybeMatched f)
gr             m1 m2
gr</pre>
gr
grTake, for example,
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>), (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 = [(1, "one"), (2, "two"), (4, "three")]
gr</pre>
gr
gr<tt>merge</tt> will first '<tt>align'</tt> these maps by key:
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>),               (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 =           [(1, "one"), (2, "two"),          (4, "three")]
gr</pre>
gr
grIt will then pass the individual entries and pairs of entries to
gr<tt>g1</tt>, <tt>g2</tt>, or <tt>f</tt> as appropriate:
gr
gr<pre>
grmaybes = [g1 0 <tt>a</tt>, f 1 <tt>b</tt> "one", g2 2 "two", g1 3 <tt>c</tt>, f 4 <tt>d</tt> "three"]
gr</pre>
gr
grThis produces a <a>Maybe</a> for each key:
gr
gr<pre>
grkeys =     0        1          2           3        4
grresults = [Nothing, Just True, Just False, Nothing, Just True]
gr</pre>
gr
grFinally, the <tt>Just</tt> results are collected into a map:
gr
gr<pre>
grreturn value = [(1, True), (2, False), (4, True)]
gr</pre>
gr
grThe other tactics below are optimizations or simplifications of
gr<a>mapMaybeMissing</a> for special cases. Most importantly,
gr
gr<ul>
gr<li><a>dropMissing</a> drops all the keys.</li>
gr<li><a>preserveMissing</a> leaves all the entries alone.</li>
gr</ul>
gr
grWhen <a>merge</a> is given three arguments, it is inlined at the call
grsite. To prevent excessive inlining, you should typically use
gr<a>merge</a> to define your custom combining functions.
gr
grExamples:
gr
gr<pre>
grunionWithKey f = merge preserveMissing preserveMissing (zipWithMatched f)
gr</pre>
gr
gr<pre>
grintersectionWithKey f = merge dropMissing dropMissing (zipWithMatched f)
gr</pre>
gr
gr<pre>
grdifferenceWith f = merge diffPreserve diffDrop f
gr</pre>
gr
gr<pre>
grsymmetricDifference = merge diffPreserve diffPreserve (\ _ _ _ -&gt; Nothing)
gr</pre>
gr
gr<pre>
grmapEachPiece f g h = merge (diffMapWithKey f) (diffMapWithKey g)
gr</pre>
merge :: Ord k => SimpleWhenMissing k a c -> SimpleWhenMissing k b c -> SimpleWhenMatched k a b c -> Map k a -> Map k b -> Map k c

-- | Along with zipWithMaybeAMatched, witnesses the isomorphism between
gr<tt>WhenMatched f k x y z</tt> and <tt>k -&gt; x -&gt; y -&gt; f
gr(Maybe z)</tt>.
runWhenMatched :: WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)

-- | Along with traverseMaybeMissing, witnesses the isomorphism between
gr<tt>WhenMissing f k x y</tt> and <tt>k -&gt; x -&gt; f (Maybe y)</tt>.
runWhenMissing :: WhenMissing f k x y -> k -> x -> f (Maybe y)

-- | When a key is found in both maps, apply a function to the key and
grvalues and maybe use the result in the merged map.
gr
gr<pre>
grzipWithMaybeMatched :: (k -&gt; x -&gt; y -&gt; Maybe z)
gr                    -&gt; SimpleWhenMatched k x y z
gr</pre>
zipWithMaybeMatched :: Applicative f => (k -> x -> y -> Maybe z) -> WhenMatched f k x y z

-- | When a key is found in both maps, apply a function to the key and
grvalues and use the result in the merged map.
gr
gr<pre>
grzipWithMatched :: (k -&gt; x -&gt; y -&gt; z)
gr               -&gt; SimpleWhenMatched k x y z
gr</pre>
zipWithMatched :: Applicative f => (k -> x -> y -> z) -> WhenMatched f k x y z

-- | Map over the entries whose keys are missing from the other map,
groptionally removing some. This is the most powerful
gr<a>SimpleWhenMissing</a> tactic, but others are usually more
grefficient.
gr
gr<pre>
grmapMaybeMissing :: (k -&gt; x -&gt; Maybe y) -&gt; SimpleWhenMissing k x y
gr</pre>
gr
gr<pre>
grmapMaybeMissing f = traverseMaybeMissing (\k x -&gt; pure (f k x))
gr</pre>
gr
grbut <tt>mapMaybeMissing</tt> uses fewer unnecessary <a>Applicative</a>
groperations.
mapMaybeMissing :: Applicative f => (k -> x -> Maybe y) -> WhenMissing f k x y

-- | Drop all the entries whose keys are missing from the other map.
gr
gr<pre>
grdropMissing :: SimpleWhenMissing k x y
gr</pre>
gr
gr<pre>
grdropMissing = mapMaybeMissing (\_ _ -&gt; Nothing)
gr</pre>
gr
grbut <tt>dropMissing</tt> is much faster.
dropMissing :: Applicative f => WhenMissing f k x y

-- | Preserve, unchanged, the entries whose keys are missing from the other
grmap.
gr
gr<pre>
grpreserveMissing :: SimpleWhenMissing k x x
gr</pre>
gr
gr<pre>
grpreserveMissing = Merge.Lazy.mapMaybeMissing (\_ x -&gt; Just x)
gr</pre>
gr
grbut <tt>preserveMissing</tt> is much faster.
preserveMissing :: Applicative f => WhenMissing f k x x

-- | Map over the entries whose keys are missing from the other map.
gr
gr<pre>
grmapMissing :: (k -&gt; x -&gt; y) -&gt; SimpleWhenMissing k x y
gr</pre>
gr
gr<pre>
grmapMissing f = mapMaybeMissing (\k x -&gt; Just $ f k x)
gr</pre>
gr
grbut <tt>mapMissing</tt> is somewhat faster.
mapMissing :: Applicative f => (k -> x -> y) -> WhenMissing f k x y

-- | Filter the entries whose keys are missing from the other map.
gr
gr<pre>
grfilterMissing :: (k -&gt; x -&gt; Bool) -&gt; SimpleWhenMissing k x x
gr</pre>
gr
gr<pre>
grfilterMissing f = Merge.Lazy.mapMaybeMissing $ \k x -&gt; guard (f k x) *&gt; Just x
gr</pre>
gr
grbut this should be a little faster.
filterMissing :: Applicative f => (k -> x -> Bool) -> WhenMissing f k x x

-- | A tactic for dealing with keys present in one map but not the other in
gr<a>merge</a> or <a>mergeA</a>.
gr
grA tactic of type <tt> WhenMissing f k x z </tt> is an abstract
grrepresentation of a function of type <tt> k -&gt; x -&gt; f (Maybe z)
gr</tt>.
data WhenMissing f k x y
WhenMissing :: Map k x -> f (Map k y) -> k -> x -> f (Maybe y) -> WhenMissing f k x y
[missingSubtree] :: WhenMissing f k x y -> Map k x -> f (Map k y)
[missingKey] :: WhenMissing f k x y -> k -> x -> f (Maybe y)

-- | A tactic for dealing with keys present in both maps in <a>merge</a> or
gr<a>mergeA</a>.
gr
grA tactic of type <tt> WhenMatched f k x y z </tt> is an abstract
grrepresentation of a function of type <tt> k -&gt; x -&gt; y -&gt; f
gr(Maybe z) </tt>.
newtype WhenMatched f k x y z
WhenMatched :: k -> x -> y -> f (Maybe z) -> WhenMatched f k x y z
[matchedKey] :: WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)

-- | An applicative version of <a>merge</a>.
gr
gr<tt>mergeA</tt> takes two <a>WhenMissing</a> tactics, a
gr<a>WhenMatched</a> tactic and two maps. It uses the tactics to merge
grthe maps. Its behavior is best understood via its fundamental tactics,
gr<a>traverseMaybeMissing</a> and <a>zipWithMaybeAMatched</a>.
gr
grConsider
gr
gr<pre>
grmergeA (traverseMaybeMissing g1)
gr              (traverseMaybeMissing g2)
gr              (zipWithMaybeAMatched f)
gr              m1 m2
gr</pre>
gr
grTake, for example,
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>), (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 = [(1, "one"), (2, "two"), (4, "three")]
gr</pre>
gr
gr<tt>mergeA</tt> will first '<tt>align'</tt> these maps by key:
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>),               (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 =           [(1, "one"), (2, "two"),          (4, "three")]
gr</pre>
gr
grIt will then pass the individual entries and pairs of entries to
gr<tt>g1</tt>, <tt>g2</tt>, or <tt>f</tt> as appropriate:
gr
gr<pre>
gractions = [g1 0 <tt>a</tt>, f 1 <tt>b</tt> "one", g2 2 "two", g1 3 <tt>c</tt>, f 4 <tt>d</tt> "three"]
gr</pre>
gr
grNext, it will perform the actions in the <tt>actions</tt> list in
grorder from left to right.
gr
gr<pre>
grkeys =     0        1          2           3        4
grresults = [Nothing, Just True, Just False, Nothing, Just True]
gr</pre>
gr
grFinally, the <tt>Just</tt> results are collected into a map:
gr
gr<pre>
grreturn value = [(1, True), (2, False), (4, True)]
gr</pre>
gr
grThe other tactics below are optimizations or simplifications of
gr<a>traverseMaybeMissing</a> for special cases. Most importantly,
gr
gr<ul>
gr<li><a>dropMissing</a> drops all the keys.</li>
gr<li><a>preserveMissing</a> leaves all the entries alone.</li>
gr<li><a>mapMaybeMissing</a> does not use the <a>Applicative</a>
grcontext.</li>
gr</ul>
gr
grWhen <a>mergeA</a> is given three arguments, it is inlined at the call
grsite. To prevent excessive inlining, you should generally only use
gr<a>mergeA</a> to define custom combining functions.
mergeA :: (Applicative f, Ord k) => WhenMissing f k a c -> WhenMissing f k b c -> WhenMatched f k a b c -> Map k a -> Map k b -> f (Map k c)

-- | When a key is found in both maps, apply a function to the key and
grvalues, perform the resulting action, and maybe use the result in the
grmerged map.
gr
grThis is the fundamental <a>WhenMatched</a> tactic.
zipWithMaybeAMatched :: Applicative f => (k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z

-- | When a key is found in both maps, apply a function to the key and
grvalues to produce an action and use its result in the merged map.
zipWithAMatched :: Applicative f => (k -> x -> y -> f z) -> WhenMatched f k x y z

-- | Traverse over the entries whose keys are missing from the other map,
groptionally producing values to put in the result. This is the most
grpowerful <a>WhenMissing</a> tactic, but others are usually more
grefficient.
traverseMaybeMissing :: Applicative f => (k -> x -> f (Maybe y)) -> WhenMissing f k x y

-- | Traverse over the entries whose keys are missing from the other map.
traverseMissing :: Applicative f => (k -> x -> f y) -> WhenMissing f k x y

-- | Filter the entries whose keys are missing from the other map using
grsome <a>Applicative</a> action.
gr
gr<pre>
grfilterAMissing f = Merge.Lazy.traverseMaybeMissing $
gr  k x -&gt; (b -&gt; guard b *&gt; Just x) <a>$</a> f k x
gr</pre>
gr
grbut this should be a little faster.
filterAMissing :: Applicative f => (k -> x -> f Bool) -> WhenMissing f k x x

-- | Map covariantly over a <tt><a>WhenMissing</a> f k x</tt>.
mapWhenMissing :: Functor f => (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b

-- | Map covariantly over a <tt><a>WhenMatched</a> f k x y</tt>.
mapWhenMatched :: Functor f => (a -> b) -> WhenMatched f k x y a -> WhenMatched f k x y b

-- | <i>O(n+m)</i>. An unsafe universal combining function.
gr
grWARNING: This function can produce corrupt maps and its results may
grdepend on the internal structures of its inputs. Users should prefer
gr<a>merge</a> or <a>mergeA</a>.
gr
grWhen <a>mergeWithKey</a> is given three arguments, it is inlined to
grthe call site. You should therefore use <a>mergeWithKey</a> only to
grdefine custom combining functions. For example, you could define
gr<a>unionWithKey</a>, <a>differenceWithKey</a> and
gr<a>intersectionWithKey</a> as
gr
gr<pre>
grmyUnionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -&gt; Just (f k x1 x2)) id id m1 m2
grmyDifferenceWithKey f m1 m2 = mergeWithKey f id (const empty) m1 m2
grmyIntersectionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -&gt; Just (f k x1 x2)) (const empty) (const empty) m1 m2
gr</pre>
gr
grWhen calling <tt><a>mergeWithKey</a> combine only1 only2</tt>, a
grfunction combining two <a>Map</a>s is created, such that
gr
gr<ul>
gr<li>if a key is present in both maps, it is passed with both
grcorresponding values to the <tt>combine</tt> function. Depending on
grthe result, the key is either present in the result with specified
grvalue, or is left out;</li>
gr<li>a nonempty subtree present only in the first map is passed to
gr<tt>only1</tt> and the output is added to the result;</li>
gr<li>a nonempty subtree present only in the second map is passed to
gr<tt>only2</tt> and the output is added to the result.</li>
gr</ul>
gr
grThe <tt>only1</tt> and <tt>only2</tt> methods <i>must return a map
grwith a subset (possibly empty) of the keys of the given map</i>. The
grvalues can be modified arbitrarily. Most common variants of
gr<tt>only1</tt> and <tt>only2</tt> are <a>id</a> and <tt><a>const</a>
gr<a>empty</a></tt>, but for example <tt><a>map</a> f</tt> or
gr<tt><a>filterWithKey</a> f</tt> could be used for any <tt>f</tt>.
mergeWithKey :: Ord k => (k -> a -> b -> Maybe c) -> (Map k a -> Map k c) -> (Map k b -> Map k c) -> Map k a -> Map k b -> Map k c

-- | <i>O(n)</i>. Map a function over all values in the map.
gr
gr<pre>
grmap (++ "x") (fromList [(5,"a"), (3,"b")]) == fromList [(3, "bx"), (5, "ax")]
gr</pre>
map :: (a -> b) -> Map k a -> Map k b

-- | <i>O(n)</i>. Map a function over all values in the map.
gr
gr<pre>
grlet f key x = (show key) ++ ":" ++ x
grmapWithKey f (fromList [(5,"a"), (3,"b")]) == fromList [(3, "3:b"), (5, "5:a")]
gr</pre>
mapWithKey :: (k -> a -> b) -> Map k a -> Map k b

-- | <i>O(n)</i>. <tt><a>traverseWithKey</a> f m == <a>fromList</a>
gr<a>$</a> <a>traverse</a> ((k, v) -&gt; (v' -&gt; v' <a>seq</a> (k,v'))
gr<a>$</a> f k v) (<a>toList</a> m)</tt> That is, it behaves much like a
grregular <a>traverse</a> except that the traversing function also has
graccess to the key associated with a value and the values are forced
grbefore they are installed in the result map.
gr
gr<pre>
grtraverseWithKey (\k v -&gt; if odd k then Just (succ v) else Nothing) (fromList [(1, 'a'), (5, 'e')]) == Just (fromList [(1, 'b'), (5, 'f')])
grtraverseWithKey (\k v -&gt; if odd k then Just (succ v) else Nothing) (fromList [(2, 'c')])           == Nothing
gr</pre>
traverseWithKey :: Applicative t => (k -> a -> t b) -> Map k a -> t (Map k b)

-- | <i>O(n)</i>. Traverse keys/values and collect the <a>Just</a> results.
traverseMaybeWithKey :: Applicative f => (k -> a -> f (Maybe b)) -> Map k a -> f (Map k b)

-- | <i>O(n)</i>. The function <a>mapAccum</a> threads an accumulating
grargument through the map in ascending order of keys.
gr
gr<pre>
grlet f a b = (a ++ b, b ++ "X")
grmapAccum f "Everything: " (fromList [(5,"a"), (3,"b")]) == ("Everything: ba", fromList [(3, "bX"), (5, "aX")])
gr</pre>
mapAccum :: (a -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)

-- | <i>O(n)</i>. The function <a>mapAccumWithKey</a> threads an
graccumulating argument through the map in ascending order of keys.
gr
gr<pre>
grlet f a k b = (a ++ " " ++ (show k) ++ "-" ++ b, b ++ "X")
grmapAccumWithKey f "Everything:" (fromList [(5,"a"), (3,"b")]) == ("Everything: 3-b 5-a", fromList [(3, "bX"), (5, "aX")])
gr</pre>
mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)

-- | <i>O(n)</i>. The function <tt>mapAccumR</tt> threads an accumulating
grargument through the map in descending order of keys.
mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)

-- | <i>O(n*log n)</i>. <tt><a>mapKeys</a> f s</tt> is the map obtained by
grapplying <tt>f</tt> to each key of <tt>s</tt>.
gr
grThe size of the result may be smaller if <tt>f</tt> maps two or more
grdistinct keys to the same new key. In this case the value at the
grgreatest of the original keys is retained.
gr
gr<pre>
grmapKeys (+ 1) (fromList [(5,"a"), (3,"b")])                        == fromList [(4, "b"), (6, "a")]
grmapKeys (\ _ -&gt; 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "c"
grmapKeys (\ _ -&gt; 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "c"
gr</pre>
mapKeys :: Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 a

-- | <i>O(n*log n)</i>. <tt><a>mapKeysWith</a> c f s</tt> is the map
grobtained by applying <tt>f</tt> to each key of <tt>s</tt>.
gr
grThe size of the result may be smaller if <tt>f</tt> maps two or more
grdistinct keys to the same new key. In this case the associated values
grwill be combined using <tt>c</tt>. The value at the greater of the two
groriginal keys is used as the first argument to <tt>c</tt>.
gr
gr<pre>
grmapKeysWith (++) (\ _ -&gt; 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "cdab"
grmapKeysWith (++) (\ _ -&gt; 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "cdab"
gr</pre>
mapKeysWith :: Ord k2 => (a -> a -> a) -> (k1 -> k2) -> Map k1 a -> Map k2 a

-- | <i>O(n)</i>. <tt><a>mapKeysMonotonic</a> f s == <a>mapKeys</a> f
grs</tt>, but works only when <tt>f</tt> is strictly monotonic. That is,
grfor any values <tt>x</tt> and <tt>y</tt>, if <tt>x</tt> &lt;
gr<tt>y</tt> then <tt>f x</tt> &lt; <tt>f y</tt>. <i>The precondition is
grnot checked.</i> Semi-formally, we have:
gr
gr<pre>
grand [x &lt; y ==&gt; f x &lt; f y | x &lt;- ls, y &lt;- ls]
gr                    ==&gt; mapKeysMonotonic f s == mapKeys f s
gr    where ls = keys s
gr</pre>
gr
grThis means that <tt>f</tt> maps distinct original keys to distinct
grresulting keys. This function has better performance than
gr<a>mapKeys</a>.
gr
gr<pre>
grmapKeysMonotonic (\ k -&gt; k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")]
grvalid (mapKeysMonotonic (\ k -&gt; k * 2) (fromList [(5,"a"), (3,"b")])) == True
grvalid (mapKeysMonotonic (\ _ -&gt; 1)     (fromList [(5,"a"), (3,"b")])) == False
gr</pre>
mapKeysMonotonic :: (k1 -> k2) -> Map k1 a -> Map k2 a

-- | <i>O(n)</i>. Fold the values in the map using the given
grright-associative binary operator, such that <tt><a>foldr</a> f z ==
gr<a>foldr</a> f z . <a>elems</a></tt>.
gr
grFor example,
gr
gr<pre>
grelems map = foldr (:) [] map
gr</pre>
gr
gr<pre>
grlet f a len = len + (length a)
grfoldr f 0 (fromList [(5,"a"), (3,"bbb")]) == 4
gr</pre>
foldr :: (a -> b -> b) -> b -> Map k a -> b

-- | <i>O(n)</i>. Fold the values in the map using the given
grleft-associative binary operator, such that <tt><a>foldl</a> f z ==
gr<a>foldl</a> f z . <a>elems</a></tt>.
gr
grFor example,
gr
gr<pre>
grelems = reverse . foldl (flip (:)) []
gr</pre>
gr
gr<pre>
grlet f len a = len + (length a)
grfoldl f 0 (fromList [(5,"a"), (3,"bbb")]) == 4
gr</pre>
foldl :: (a -> b -> a) -> a -> Map k b -> a

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grright-associative binary operator, such that <tt><a>foldrWithKey</a> f
grz == <a>foldr</a> (<a>uncurry</a> f) z . <a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grkeys map = foldrWithKey (\k x ks -&gt; k:ks) [] map
gr</pre>
gr
gr<pre>
grlet f k a result = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")"
grfoldrWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (5:a)(3:b)"
gr</pre>
foldrWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grleft-associative binary operator, such that <tt><a>foldlWithKey</a> f
grz == <a>foldl</a> (\z' (kx, x) -&gt; f z' kx x) z .
gr<a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grkeys = reverse . foldlWithKey (\ks k x -&gt; k:ks) []
gr</pre>
gr
gr<pre>
grlet f result k a = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")"
grfoldlWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (3:b)(5:a)"
gr</pre>
foldlWithKey :: (a -> k -> b -> a) -> a -> Map k b -> a

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grmonoid, such that
gr
gr<pre>
gr<a>foldMapWithKey</a> f = <a>fold</a> . <a>mapWithKey</a> f
gr</pre>
gr
grThis can be an asymptotically faster than <a>foldrWithKey</a> or
gr<a>foldlWithKey</a> for some monoids.
foldMapWithKey :: Monoid m => (k -> a -> m) -> Map k a -> m

-- | <i>O(n)</i>. A strict version of <a>foldr</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldr' :: (a -> b -> b) -> b -> Map k a -> b

-- | <i>O(n)</i>. A strict version of <a>foldl</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldl' :: (a -> b -> a) -> a -> Map k b -> a

-- | <i>O(n)</i>. A strict version of <a>foldrWithKey</a>. Each application
grof the operator is evaluated before using the result in the next
grapplication. This function is strict in the starting value.
foldrWithKey' :: (k -> a -> b -> b) -> b -> Map k a -> b

-- | <i>O(n)</i>. A strict version of <a>foldlWithKey</a>. Each application
grof the operator is evaluated before using the result in the next
grapplication. This function is strict in the starting value.
foldlWithKey' :: (a -> k -> b -> a) -> a -> Map k b -> a

-- | <i>O(n)</i>. Return all elements of the map in the ascending order of
grtheir keys. Subject to list fusion.
gr
gr<pre>
grelems (fromList [(5,"a"), (3,"b")]) == ["b","a"]
grelems empty == []
gr</pre>
elems :: Map k a -> [a]

-- | <i>O(n)</i>. Return all keys of the map in ascending order. Subject to
grlist fusion.
gr
gr<pre>
grkeys (fromList [(5,"a"), (3,"b")]) == [3,5]
grkeys empty == []
gr</pre>
keys :: Map k a -> [k]

-- | <i>O(n)</i>. An alias for <a>toAscList</a>. Return all key/value pairs
grin the map in ascending key order. Subject to list fusion.
gr
gr<pre>
grassocs (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
grassocs empty == []
gr</pre>
assocs :: Map k a -> [(k, a)]

-- | <i>O(n)</i>. The set of all keys of the map.
gr
gr<pre>
grkeysSet (fromList [(5,"a"), (3,"b")]) == Data.Set.fromList [3,5]
grkeysSet empty == Data.Set.empty
gr</pre>
keysSet :: Map k a -> Set k

-- | <i>O(n)</i>. Build a map from a set of keys and a function which for
greach key computes its value.
gr
gr<pre>
grfromSet (\k -&gt; replicate k 'a') (Data.Set.fromList [3, 5]) == fromList [(5,"aaaaa"), (3,"aaa")]
grfromSet undefined Data.Set.empty == empty
gr</pre>
fromSet :: (k -> a) -> Set k -> Map k a

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs. Subject to
grlist fusion.
gr
gr<pre>
grtoList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
grtoList empty == []
gr</pre>
toList :: Map k a -> [(k, a)]

-- | <i>O(n*log n)</i>. Build a map from a list of key/value pairs. See
gralso <a>fromAscList</a>. If the list contains more than one value for
grthe same key, the last value for the key is retained.
gr
grIf the keys of the list are ordered, linear-time implementation is
grused, with the performance equal to <a>fromDistinctAscList</a>.
gr
gr<pre>
grfromList [] == empty
grfromList [(5,"a"), (3,"b"), (5, "c")] == fromList [(5,"c"), (3,"b")]
grfromList [(5,"c"), (3,"b"), (5, "a")] == fromList [(5,"a"), (3,"b")]
gr</pre>
fromList :: Ord k => [(k, a)] -> Map k a

-- | <i>O(n*log n)</i>. Build a map from a list of key/value pairs with a
grcombining function. See also <a>fromAscListWith</a>.
gr
gr<pre>
grfromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")]
grfromListWith (++) [] == empty
gr</pre>
fromListWith :: Ord k => (a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n*log n)</i>. Build a map from a list of key/value pairs with a
grcombining function. See also <a>fromAscListWithKey</a>.
gr
gr<pre>
grlet f k a1 a2 = (show k) ++ a1 ++ a2
grfromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "3ab"), (5, "5a5ba")]
grfromListWithKey f [] == empty
gr</pre>
fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs where the
grkeys are in ascending order. Subject to list fusion.
gr
gr<pre>
grtoAscList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
gr</pre>
toAscList :: Map k a -> [(k, a)]

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs where the
grkeys are in descending order. Subject to list fusion.
gr
gr<pre>
grtoDescList (fromList [(5,"a"), (3,"b")]) == [(5,"a"), (3,"b")]
gr</pre>
toDescList :: Map k a -> [(k, a)]

-- | <i>O(n)</i>. Build a map from an ascending list in linear time. <i>The
grprecondition (input list is ascending) is not checked.</i>
gr
gr<pre>
grfromAscList [(3,"b"), (5,"a")]          == fromList [(3, "b"), (5, "a")]
grfromAscList [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "b")]
grvalid (fromAscList [(3,"b"), (5,"a"), (5,"b")]) == True
grvalid (fromAscList [(5,"a"), (3,"b"), (5,"b")]) == False
gr</pre>
fromAscList :: Eq k => [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from an ascending list in linear time with a
grcombining function for equal keys. <i>The precondition (input list is
grascending) is not checked.</i>
gr
gr<pre>
grfromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")]
grvalid (fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")]) == True
grvalid (fromAscListWith (++) [(5,"a"), (3,"b"), (5,"b")]) == False
gr</pre>
fromAscListWith :: Eq k => (a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from an ascending list in linear time with a
grcombining function for equal keys. <i>The precondition (input list is
grascending) is not checked.</i>
gr
gr<pre>
grlet f k a1 a2 = (show k) ++ ":" ++ a1 ++ a2
grfromAscListWithKey f [(3,"b"), (5,"a"), (5,"b"), (5,"b")] == fromList [(3, "b"), (5, "5:b5:ba")]
grvalid (fromAscListWithKey f [(3,"b"), (5,"a"), (5,"b"), (5,"b")]) == True
grvalid (fromAscListWithKey f [(5,"a"), (3,"b"), (5,"b"), (5,"b")]) == False
gr</pre>
fromAscListWithKey :: Eq k => (k -> a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from an ascending list of distinct elements
grin linear time. <i>The precondition is not checked.</i>
gr
gr<pre>
grfromDistinctAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")]
grvalid (fromDistinctAscList [(3,"b"), (5,"a")])          == True
grvalid (fromDistinctAscList [(3,"b"), (5,"a"), (5,"b")]) == False
gr</pre>
fromDistinctAscList :: [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from a descending list in linear time. <i>The
grprecondition (input list is descending) is not checked.</i>
gr
gr<pre>
grfromDescList [(5,"a"), (3,"b")]          == fromList [(3, "b"), (5, "a")]
grfromDescList [(5,"a"), (5,"b"), (3,"a")] == fromList [(3, "b"), (5, "b")]
grvalid (fromDescList [(5,"a"), (5,"b"), (3,"b")]) == True
grvalid (fromDescList [(5,"a"), (3,"b"), (5,"b")]) == False
gr</pre>
fromDescList :: Eq k => [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from a descending list in linear time with a
grcombining function for equal keys. <i>The precondition (input list is
grdescending) is not checked.</i>
gr
gr<pre>
grfromDescListWith (++) [(5,"a"), (5,"b"), (3,"b")] == fromList [(3, "b"), (5, "ba")]
grvalid (fromDescListWith (++) [(5,"a"), (5,"b"), (3,"b")]) == True
grvalid (fromDescListWith (++) [(5,"a"), (3,"b"), (5,"b")]) == False
gr</pre>
fromDescListWith :: Eq k => (a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from a descending list in linear time with a
grcombining function for equal keys. <i>The precondition (input list is
grdescending) is not checked.</i>
gr
gr<pre>
grlet f k a1 a2 = (show k) ++ ":" ++ a1 ++ a2
grfromDescListWithKey f [(5,"a"), (5,"b"), (5,"b"), (3,"b")] == fromList [(3, "b"), (5, "5:b5:ba")]
grvalid (fromDescListWithKey f [(5,"a"), (5,"b"), (5,"b"), (3,"b")]) == True
grvalid (fromDescListWithKey f [(5,"a"), (3,"b"), (5,"b"), (5,"b")]) == False
gr</pre>
fromDescListWithKey :: Eq k => (k -> a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from a descending list of distinct elements
grin linear time. <i>The precondition is not checked.</i>
gr
gr<pre>
grfromDistinctDescList [(5,"a"), (3,"b")] == fromList [(3, "b"), (5, "a")]
grvalid (fromDistinctDescList [(5,"a"), (3,"b")])          == True
grvalid (fromDistinctDescList [(5,"a"), (3,"b"), (3,"a")]) == False
gr</pre>
fromDistinctDescList :: [(k, a)] -> Map k a

-- | <i>O(n)</i>. Filter all values that satisfy the predicate.
gr
gr<pre>
grfilter (&gt; "a") (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grfilter (&gt; "x") (fromList [(5,"a"), (3,"b")]) == empty
grfilter (&lt; "a") (fromList [(5,"a"), (3,"b")]) == empty
gr</pre>
filter :: (a -> Bool) -> Map k a -> Map k a

-- | <i>O(n)</i>. Filter all keys/values that satisfy the predicate.
gr
gr<pre>
grfilterWithKey (\k _ -&gt; k &gt; 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
filterWithKey :: (k -> a -> Bool) -> Map k a -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Restrict a <a>Map</a> to only
grthose keys found in a <a>Set</a>.
gr
gr<pre>
grm `<tt>restrictKeys'</tt> s = <a>filterWithKey</a> (k _ -&gt; k `<a>member'</a> s) m
grm `<tt>restrictKeys'</tt> s = m `<tt>intersect</tt> <a>fromSet</a> (const ()) s
gr</pre>
restrictKeys :: Ord k => Map k a -> Set k -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Remove all keys in a <a>Set</a>
grfrom a <a>Map</a>.
gr
gr<pre>
grm `<tt>withoutKeys'</tt> s = <a>filterWithKey</a> (k _ -&gt; k `<a>notMember'</a> s) m
grm `<tt>withoutKeys'</tt> s = m `<tt>difference'</tt> <a>fromSet</a> (const ()) s
gr</pre>
withoutKeys :: Ord k => Map k a -> Set k -> Map k a

-- | <i>O(n)</i>. Partition the map according to a predicate. The first map
grcontains all elements that satisfy the predicate, the second all
grelements that fail the predicate. See also <a>split</a>.
gr
gr<pre>
grpartition (&gt; "a") (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a")
grpartition (&lt; "x") (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
grpartition (&gt; "x") (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
gr</pre>
partition :: (a -> Bool) -> Map k a -> (Map k a, Map k a)

-- | <i>O(n)</i>. Partition the map according to a predicate. The first map
grcontains all elements that satisfy the predicate, the second all
grelements that fail the predicate. See also <a>split</a>.
gr
gr<pre>
grpartitionWithKey (\ k _ -&gt; k &gt; 3) (fromList [(5,"a"), (3,"b")]) == (singleton 5 "a", singleton 3 "b")
grpartitionWithKey (\ k _ -&gt; k &lt; 7) (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
grpartitionWithKey (\ k _ -&gt; k &gt; 7) (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
gr</pre>
partitionWithKey :: (k -> a -> Bool) -> Map k a -> (Map k a, Map k a)

-- | <i>O(log n)</i>. Take while a predicate on the keys holds. The user is
grresponsible for ensuring that for all keys <tt>j</tt> and <tt>k</tt>
grin the map, <tt>j &lt; k ==&gt; p j &gt;= p k</tt>. See note at
gr<a>spanAntitone</a>.
gr
gr<pre>
grtakeWhileAntitone p = <a>fromDistinctAscList</a> . <a>takeWhile</a> (p . fst) . <a>toList</a>
grtakeWhileAntitone p = <a>filterWithKey</a> (k _ -&gt; p k)
gr</pre>
takeWhileAntitone :: (k -> Bool) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Drop while a predicate on the keys holds. The user is
grresponsible for ensuring that for all keys <tt>j</tt> and <tt>k</tt>
grin the map, <tt>j &lt; k ==&gt; p j &gt;= p k</tt>. See note at
gr<a>spanAntitone</a>.
gr
gr<pre>
grdropWhileAntitone p = <a>fromDistinctAscList</a> . <a>dropWhile</a> (p . fst) . <a>toList</a>
grdropWhileAntitone p = <a>filterWithKey</a> (k -&gt; not (p k))
gr</pre>
dropWhileAntitone :: (k -> Bool) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Divide a map at the point where a predicate on the
grkeys stops holding. The user is responsible for ensuring that for all
grkeys <tt>j</tt> and <tt>k</tt> in the map, <tt>j &lt; k ==&gt; p j
gr&gt;= p k</tt>.
gr
gr<pre>
grspanAntitone p xs = (<a>takeWhileAntitone</a> p xs, <a>dropWhileAntitone</a> p xs)
grspanAntitone p xs = partition p xs
gr</pre>
gr
grNote: if <tt>p</tt> is not actually antitone, then
gr<tt>spanAntitone</tt> will split the map at some <i>unspecified</i>
grpoint where the predicate switches from holding to not holding (where
grthe predicate is seen to hold before the first key and to fail after
grthe last key).
spanAntitone :: (k -> Bool) -> Map k a -> (Map k a, Map k a)

-- | <i>O(n)</i>. Map values and collect the <a>Just</a> results.
gr
gr<pre>
grlet f x = if x == "a" then Just "new a" else Nothing
grmapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"
gr</pre>
mapMaybe :: (a -> Maybe b) -> Map k a -> Map k b

-- | <i>O(n)</i>. Map keys/values and collect the <a>Just</a> results.
gr
gr<pre>
grlet f k _ = if k &lt; 5 then Just ("key : " ++ (show k)) else Nothing
grmapMaybeWithKey f (fromList [(5,"a"), (3,"b")]) == singleton 3 "key : 3"
gr</pre>
mapMaybeWithKey :: (k -> a -> Maybe b) -> Map k a -> Map k b

-- | <i>O(n)</i>. Map values and separate the <a>Left</a> and <a>Right</a>
grresults.
gr
gr<pre>
grlet f a = if a &lt; "c" then Left a else Right a
grmapEither f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (fromList [(3,"b"), (5,"a")], fromList [(1,"x"), (7,"z")])
gr
grmapEither (\ a -&gt; Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (empty, fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr</pre>
mapEither :: (a -> Either b c) -> Map k a -> (Map k b, Map k c)

-- | <i>O(n)</i>. Map keys/values and separate the <a>Left</a> and
gr<a>Right</a> results.
gr
gr<pre>
grlet f k a = if k &lt; 5 then Left (k * 2) else Right (a ++ a)
grmapEitherWithKey f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (fromList [(1,2), (3,6)], fromList [(5,"aa"), (7,"zz")])
gr
grmapEitherWithKey (\_ a -&gt; Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (empty, fromList [(1,"x"), (3,"b"), (5,"a"), (7,"z")])
gr</pre>
mapEitherWithKey :: (k -> a -> Either b c) -> Map k a -> (Map k b, Map k c)

-- | <i>O(log n)</i>. The expression (<tt><a>split</a> k map</tt>) is a
grpair <tt>(map1,map2)</tt> where the keys in <tt>map1</tt> are smaller
grthan <tt>k</tt> and the keys in <tt>map2</tt> larger than <tt>k</tt>.
grAny key equal to <tt>k</tt> is found in neither <tt>map1</tt> nor
gr<tt>map2</tt>.
gr
gr<pre>
grsplit 2 (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3,"b"), (5,"a")])
grsplit 3 (fromList [(5,"a"), (3,"b")]) == (empty, singleton 5 "a")
grsplit 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a")
grsplit 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", empty)
grsplit 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], empty)
gr</pre>
split :: Ord k => k -> Map k a -> (Map k a, Map k a)

-- | <i>O(log n)</i>. The expression (<tt><a>splitLookup</a> k map</tt>)
grsplits a map just like <a>split</a> but also returns <tt><a>lookup</a>
grk map</tt>.
gr
gr<pre>
grsplitLookup 2 (fromList [(5,"a"), (3,"b")]) == (empty, Nothing, fromList [(3,"b"), (5,"a")])
grsplitLookup 3 (fromList [(5,"a"), (3,"b")]) == (empty, Just "b", singleton 5 "a")
grsplitLookup 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Nothing, singleton 5 "a")
grsplitLookup 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Just "a", empty)
grsplitLookup 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], Nothing, empty)
gr</pre>
splitLookup :: Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)

-- | <i>O(1)</i>. Decompose a map into pieces based on the structure of the
grunderlying tree. This function is useful for consuming a map in
grparallel.
gr
grNo guarantee is made as to the sizes of the pieces; an internal, but
grdeterministic process determines this. However, it is guaranteed that
grthe pieces returned will be in ascending order (all elements in the
grfirst submap less than all elements in the second, and so on).
gr
grExamples:
gr
gr<pre>
grsplitRoot (fromList (zip [1..6] ['a'..])) ==
gr  [fromList [(1,'a'),(2,'b'),(3,'c')],fromList [(4,'d')],fromList [(5,'e'),(6,'f')]]
gr</pre>
gr
gr<pre>
grsplitRoot empty == []
gr</pre>
gr
grNote that the current implementation does not return more than three
grsubmaps, but you should not depend on this behaviour because it can
grchange in the future without notice.
splitRoot :: Map k b -> [Map k b]

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. This function is defined as
gr(<tt><a>isSubmapOf</a> = <a>isSubmapOfBy</a> (==)</tt>).
isSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. The expression
gr(<tt><a>isSubmapOfBy</a> f t1 t2</tt>) returns <a>True</a> if all keys
grin <tt>t1</tt> are in tree <tt>t2</tt>, and when <tt>f</tt> returns
gr<a>True</a> when applied to their respective values. For example, the
grfollowing expressions are all <a>True</a>:
gr
gr<pre>
grisSubmapOfBy (==) (fromList [('a',1)]) (fromList [('a',1),('b',2)])
grisSubmapOfBy (&lt;=) (fromList [('a',1)]) (fromList [('a',1),('b',2)])
grisSubmapOfBy (==) (fromList [('a',1),('b',2)]) (fromList [('a',1),('b',2)])
gr</pre>
gr
grBut the following are all <a>False</a>:
gr
gr<pre>
grisSubmapOfBy (==) (fromList [('a',2)]) (fromList [('a',1),('b',2)])
grisSubmapOfBy (&lt;)  (fromList [('a',1)]) (fromList [('a',1),('b',2)])
grisSubmapOfBy (==) (fromList [('a',1),('b',2)]) (fromList [('a',1)])
gr</pre>
isSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Is this a proper submap? (ie. a
grsubmap but not equal). Defined as (<tt><a>isProperSubmapOf</a> =
gr<a>isProperSubmapOfBy</a> (==)</tt>).
isProperSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Is this a proper submap? (ie. a
grsubmap but not equal). The expression (<tt><a>isProperSubmapOfBy</a> f
grm1 m2</tt>) returns <a>True</a> when <tt>m1</tt> and <tt>m2</tt> are
grnot equal, all keys in <tt>m1</tt> are in <tt>m2</tt>, and when
gr<tt>f</tt> returns <a>True</a> when applied to their respective
grvalues. For example, the following expressions are all <a>True</a>:
gr
gr<pre>
grisProperSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
grisProperSubmapOfBy (&lt;=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
gr</pre>
gr
grBut the following are all <a>False</a>:
gr
gr<pre>
grisProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])
grisProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
grisProperSubmapOfBy (&lt;)  (fromList [(1,1)])       (fromList [(1,1),(2,2)])
gr</pre>
isProperSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool

-- | <i>O(log n)</i>. Lookup the <i>index</i> of a key, which is its
grzero-based index in the sequence sorted by keys. The index is a number
grfrom <i>0</i> up to, but not including, the <a>size</a> of the map.
gr
gr<pre>
grisJust (lookupIndex 2 (fromList [(5,"a"), (3,"b")]))   == False
grfromJust (lookupIndex 3 (fromList [(5,"a"), (3,"b")])) == 0
grfromJust (lookupIndex 5 (fromList [(5,"a"), (3,"b")])) == 1
grisJust (lookupIndex 6 (fromList [(5,"a"), (3,"b")]))   == False
gr</pre>
lookupIndex :: Ord k => k -> Map k a -> Maybe Int

-- | <i>O(log n)</i>. Return the <i>index</i> of a key, which is its
grzero-based index in the sequence sorted by keys. The index is a number
grfrom <i>0</i> up to, but not including, the <a>size</a> of the map.
grCalls <a>error</a> when the key is not a <a>member</a> of the map.
gr
gr<pre>
grfindIndex 2 (fromList [(5,"a"), (3,"b")])    Error: element is not in the map
grfindIndex 3 (fromList [(5,"a"), (3,"b")]) == 0
grfindIndex 5 (fromList [(5,"a"), (3,"b")]) == 1
grfindIndex 6 (fromList [(5,"a"), (3,"b")])    Error: element is not in the map
gr</pre>
findIndex :: Ord k => k -> Map k a -> Int

-- | <i>O(log n)</i>. Retrieve an element by its <i>index</i>, i.e. by its
grzero-based index in the sequence sorted by keys. If the <i>index</i>
gris out of range (less than zero, greater or equal to <a>size</a> of
grthe map), <a>error</a> is called.
gr
gr<pre>
grelemAt 0 (fromList [(5,"a"), (3,"b")]) == (3,"b")
grelemAt 1 (fromList [(5,"a"), (3,"b")]) == (5, "a")
grelemAt 2 (fromList [(5,"a"), (3,"b")])    Error: index out of range
gr</pre>
elemAt :: Int -> Map k a -> (k, a)

-- | <i>O(log n)</i>. Update the element at <i>index</i>. Calls
gr<a>error</a> when an invalid index is used.
gr
gr<pre>
grupdateAt (\ _ _ -&gt; Just "x") 0    (fromList [(5,"a"), (3,"b")]) == fromList [(3, "x"), (5, "a")]
grupdateAt (\ _ _ -&gt; Just "x") 1    (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "x")]
grupdateAt (\ _ _ -&gt; Just "x") 2    (fromList [(5,"a"), (3,"b")])    Error: index out of range
grupdateAt (\ _ _ -&gt; Just "x") (-1) (fromList [(5,"a"), (3,"b")])    Error: index out of range
grupdateAt (\_ _  -&gt; Nothing)  0    (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
grupdateAt (\_ _  -&gt; Nothing)  1    (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grupdateAt (\_ _  -&gt; Nothing)  2    (fromList [(5,"a"), (3,"b")])    Error: index out of range
grupdateAt (\_ _  -&gt; Nothing)  (-1) (fromList [(5,"a"), (3,"b")])    Error: index out of range
gr</pre>
updateAt :: (k -> a -> Maybe a) -> Int -> Map k a -> Map k a

-- | <i>O(log n)</i>. Delete the element at <i>index</i>, i.e. by its
grzero-based index in the sequence sorted by keys. If the <i>index</i>
gris out of range (less than zero, greater or equal to <a>size</a> of
grthe map), <a>error</a> is called.
gr
gr<pre>
grdeleteAt 0  (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
grdeleteAt 1  (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grdeleteAt 2 (fromList [(5,"a"), (3,"b")])     Error: index out of range
grdeleteAt (-1) (fromList [(5,"a"), (3,"b")])  Error: index out of range
gr</pre>
deleteAt :: Int -> Map k a -> Map k a

-- | Take a given number of entries in key order, beginning with the
grsmallest keys.
gr
gr<pre>
grtake n = <a>fromDistinctAscList</a> . <a>take</a> n . <a>toAscList</a>
gr</pre>
take :: Int -> Map k a -> Map k a

-- | Drop a given number of entries in key order, beginning with the
grsmallest keys.
gr
gr<pre>
grdrop n = <a>fromDistinctAscList</a> . <a>drop</a> n . <a>toAscList</a>
gr</pre>
drop :: Int -> Map k a -> Map k a

-- | <i>O(log n)</i>. Split a map at a particular index.
gr
gr<pre>
grsplitAt !n !xs = (<a>take</a> n xs, <a>drop</a> n xs)
gr</pre>
splitAt :: Int -> Map k a -> (Map k a, Map k a)

-- | <i>O(log n)</i>. The minimal key of the map. Returns <a>Nothing</a> if
grthe map is empty.
gr
gr<pre>
grlookupMin (fromList [(5,"a"), (3,"b")]) == Just (3,"b")
grfindMin empty = Nothing
gr</pre>
lookupMin :: Map k a -> Maybe (k, a)

-- | <i>O(log n)</i>. The maximal key of the map. Returns <a>Nothing</a> if
grthe map is empty.
gr
gr<pre>
grlookupMax (fromList [(5,"a"), (3,"b")]) == Just (5,"a")
grlookupMax empty = Nothing
gr</pre>
lookupMax :: Map k a -> Maybe (k, a)

-- | <i>O(log n)</i>. The minimal key of the map. Calls <a>error</a> if the
grmap is empty.
gr
gr<pre>
grfindMin (fromList [(5,"a"), (3,"b")]) == (3,"b")
grfindMin empty                            Error: empty map has no minimal element
gr</pre>
findMin :: Map k a -> (k, a)
findMax :: Map k a -> (k, a)

-- | <i>O(log n)</i>. Delete the minimal key. Returns an empty map if the
grmap is empty.
gr
gr<pre>
grdeleteMin (fromList [(5,"a"), (3,"b"), (7,"c")]) == fromList [(5,"a"), (7,"c")]
grdeleteMin empty == empty
gr</pre>
deleteMin :: Map k a -> Map k a

-- | <i>O(log n)</i>. Delete the maximal key. Returns an empty map if the
grmap is empty.
gr
gr<pre>
grdeleteMax (fromList [(5,"a"), (3,"b"), (7,"c")]) == fromList [(3,"b"), (5,"a")]
grdeleteMax empty == empty
gr</pre>
deleteMax :: Map k a -> Map k a

-- | <i>O(log n)</i>. Delete and find the minimal element.
gr
gr<pre>
grdeleteFindMin (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((3,"b"), fromList[(5,"a"), (10,"c")])
grdeleteFindMin                                            Error: can not return the minimal element of an empty map
gr</pre>
deleteFindMin :: Map k a -> ((k, a), Map k a)

-- | <i>O(log n)</i>. Delete and find the maximal element.
gr
gr<pre>
grdeleteFindMax (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((10,"c"), fromList [(3,"b"), (5,"a")])
grdeleteFindMax empty                                      Error: can not return the maximal element of an empty map
gr</pre>
deleteFindMax :: Map k a -> ((k, a), Map k a)

-- | <i>O(log n)</i>. Update the value at the minimal key.
gr
gr<pre>
grupdateMin (\ a -&gt; Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "Xb"), (5, "a")]
grupdateMin (\ _ -&gt; Nothing)         (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateMin :: (a -> Maybe a) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Update the value at the maximal key.
gr
gr<pre>
grupdateMax (\ a -&gt; Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "Xa")]
grupdateMax (\ _ -&gt; Nothing)         (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr</pre>
updateMax :: (a -> Maybe a) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Update the value at the minimal key.
gr
gr<pre>
grupdateMinWithKey (\ k a -&gt; Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"3:b"), (5,"a")]
grupdateMinWithKey (\ _ _ -&gt; Nothing)                     (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateMinWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Update the value at the maximal key.
gr
gr<pre>
grupdateMaxWithKey (\ k a -&gt; Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"b"), (5,"5:a")]
grupdateMaxWithKey (\ _ _ -&gt; Nothing)                     (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr</pre>
updateMaxWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Retrieves the value associated with minimal key of
grthe map, and the map stripped of that element, or <a>Nothing</a> if
grpassed an empty map.
gr
gr<pre>
grminView (fromList [(5,"a"), (3,"b")]) == Just ("b", singleton 5 "a")
grminView empty == Nothing
gr</pre>
minView :: Map k a -> Maybe (a, Map k a)

-- | <i>O(log n)</i>. Retrieves the value associated with maximal key of
grthe map, and the map stripped of that element, or <a>Nothing</a> if
grpassed an empty map.
gr
gr<pre>
grmaxView (fromList [(5,"a"), (3,"b")]) == Just ("a", singleton 3 "b")
grmaxView empty == Nothing
gr</pre>
maxView :: Map k a -> Maybe (a, Map k a)

-- | <i>O(log n)</i>. Retrieves the minimal (key,value) pair of the map,
grand the map stripped of that element, or <a>Nothing</a> if passed an
grempty map.
gr
gr<pre>
grminViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((3,"b"), singleton 5 "a")
grminViewWithKey empty == Nothing
gr</pre>
minViewWithKey :: Map k a -> Maybe ((k, a), Map k a)

-- | <i>O(log n)</i>. Retrieves the maximal (key,value) pair of the map,
grand the map stripped of that element, or <a>Nothing</a> if passed an
grempty map.
gr
gr<pre>
grmaxViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((5,"a"), singleton 3 "b")
grmaxViewWithKey empty == Nothing
gr</pre>
maxViewWithKey :: Map k a -> Maybe ((k, a), Map k a)

-- | <i>O(n)</i>. Show the tree that implements the map. The tree is shown
grin a compressed, hanging format. See <a>showTreeWith</a>.

-- | <i>Deprecated: <a>showTree</a> is now in
gr<a>Data.Map.Internal.Debug</a></i>
showTree :: (Show k, Show a) => Map k a -> String

-- | <i>O(n)</i>. The expression (<tt><a>showTreeWith</a> showelem hang
grwide map</tt>) shows the tree that implements the map. Elements are
grshown using the <tt>showElem</tt> function. If <tt>hang</tt> is
gr<a>True</a>, a <i>hanging</i> tree is shown otherwise a rotated tree
gris shown. If <tt>wide</tt> is <a>True</a>, an extra wide version is
grshown.
gr
gr<pre>
grMap&gt; let t = fromDistinctAscList [(x,()) | x &lt;- [1..5]]
grMap&gt; putStrLn $ showTreeWith (\k x -&gt; show (k,x)) True False t
gr(4,())
gr+--(2,())
gr|  +--(1,())
gr|  +--(3,())
gr+--(5,())
gr
grMap&gt; putStrLn $ showTreeWith (\k x -&gt; show (k,x)) True True t
gr(4,())
gr|
gr+--(2,())
gr|  |
gr|  +--(1,())
gr|  |
gr|  +--(3,())
gr|
gr+--(5,())
gr
grMap&gt; putStrLn $ showTreeWith (\k x -&gt; show (k,x)) False True t
gr+--(5,())
gr|
gr(4,())
gr|
gr|  +--(3,())
gr|  |
gr+--(2,())
gr   |
gr   +--(1,())
gr</pre>

-- | <i>Deprecated: <a>showTreeWith</a> is now in
gr<a>Data.Map.Internal.Debug</a></i>
showTreeWith :: (k -> a -> String) -> Bool -> Bool -> Map k a -> String

-- | <i>O(n)</i>. Test if the internal map structure is valid.
gr
gr<pre>
grvalid (fromAscList [(3,"b"), (5,"a")]) == True
grvalid (fromAscList [(5,"a"), (3,"b")]) == False
gr</pre>
valid :: Ord k => Map k a -> Bool


-- | <h1>Finite Maps (strict interface)</h1>
gr
grThe <tt><a>Map</a> k v</tt> type represents a finite map (sometimes
grcalled a dictionary) from keys of type <tt>k</tt> to values of type
gr<tt>v</tt>.
gr
grEach function in this module is careful to force values before
grinstalling them in a <a>Map</a>. This is usually more efficient when
grlaziness is not necessary. When laziness <i>is</i> required, use the
grfunctions in <a>Data.Map.Lazy</a>.
gr
grIn particular, the functions in this module obey the following law:
gr
gr<ul>
gr<li>If all values stored in all maps in the arguments are in WHNF,
grthen all values stored in all maps in the results will be in WHNF once
grthose maps are evaluated.</li>
gr</ul>
gr
grWhen deciding if this is the correct data structure to use, consider:
gr
gr<ul>
gr<li>If you are using <tt>Int</tt> keys, you will get much better
grperformance for most operations using <a>Data.IntMap.Strict</a>.</li>
gr<li>If you don't care about ordering, consider use
gr<tt>Data.HashMap.Strict</tt> from the <a>unordered-containers</a>
grpackage instead.</li>
gr</ul>
gr
grFor a walkthrough of the most commonly used functions see the <a>maps
grintroduction</a>.
gr
grThis module is intended to be imported qualified, to avoid name
grclashes with Prelude functions:
gr
gr<pre>
grimport qualified Data.Map.Strict as Map
gr</pre>
gr
grNote that the implementation is generally <i>left-biased</i>.
grFunctions that take two maps as arguments and combine them, such as
gr<a>union</a> and <a>intersection</a>, prefer the values in the first
grargument to those in the second.
gr
gr<h2>Detailed performance information</h2>
gr
grThe amortized running time is given for each operation, with <i>n</i>
grreferring to the number of entries in the map.
gr
grBenchmarks comparing <a>Data.Map.Strict</a> with other dictionary
grimplementations can be found at
gr<a>https://github.com/haskell-perf/dictionaries</a>.
gr
gr<h2>Warning</h2>
gr
grThe size of a <a>Map</a> must not exceed <tt>maxBound::Int</tt>.
grViolation of this condition is not detected and if the size limit is
grexceeded, its behaviour is undefined.
gr
grThe <a>Map</a> type is shared between the lazy and strict modules,
grmeaning that the same <a>Map</a> value can be passed to functions in
grboth modules. This means that the <tt>Functor</tt>,
gr<tt>Traversable</tt> and <tt>Data</tt> instances are the same as for
grthe <a>Data.Map.Lazy</a> module, so if they are used on strict maps,
grthe resulting maps may contain suspended values (thunks).
gr
gr<h2>Implementation</h2>
gr
grThe implementation of <a>Map</a> is based on <i>size balanced</i>
grbinary trees (or trees of <i>bounded balance</i>) as described by:
gr
gr<ul>
gr<li>Stephen Adams, "<i>Efficient sets: a balancing act</i>", Journal
grof Functional Programming 3(4):553-562, October 1993,
gr<a>http://www.swiss.ai.mit.edu/~adams/BB/</a>.</li>
gr<li>J. Nievergelt and E.M. Reingold, "<i>Binary search trees of
grbounded balance</i>", SIAM journal of computing 2(1), March 1973.</li>
gr</ul>
gr
grBounds for <a>union</a>, <a>intersection</a>, and <a>difference</a>
grare as given by
gr
gr<ul>
gr<li>Guy Blelloch, Daniel Ferizovic, and Yihan Sun, "<i>Just Join for
grParallel Ordered Sets</i>",
gr<a>https://arxiv.org/abs/1602.02120v3</a>.</li>
gr</ul>
module Data.Map.Strict

-- | A Map from keys <tt>k</tt> to values <tt>a</tt>.
data Map k a

-- | <i>O(log n)</i>. Find the value at a key. Calls <a>error</a> when the
grelement can not be found.
gr
gr<pre>
grfromList [(5,'a'), (3,'b')] ! 1    Error: element not in the map
grfromList [(5,'a'), (3,'b')] ! 5 == 'a'
gr</pre>
(!) :: Ord k => Map k a -> k -> a
infixl 9 !

-- | <i>O(log n)</i>. Find the value at a key. Returns <a>Nothing</a> when
grthe element can not be found.
gr
gr<pre>
grfromList [(5, 'a'), (3, 'b')] !? 1 == Nothing
gr</pre>
gr
gr<pre>
grfromList [(5, 'a'), (3, 'b')] !? 5 == Just 'a'
gr</pre>
(!?) :: Ord k => Map k a -> k -> Maybe a
infixl 9 !?

-- | Same as <a>difference</a>.
(\\) :: Ord k => Map k a -> Map k b -> Map k a
infixl 9 \\

-- | <i>O(1)</i>. Is the map empty?
gr
gr<pre>
grData.Map.null (empty)           == True
grData.Map.null (singleton 1 'a') == False
gr</pre>
null :: Map k a -> Bool

-- | <i>O(1)</i>. The number of elements in the map.
gr
gr<pre>
grsize empty                                   == 0
grsize (singleton 1 'a')                       == 1
grsize (fromList([(1,'a'), (2,'c'), (3,'b')])) == 3
gr</pre>
size :: Map k a -> Int

-- | <i>O(log n)</i>. Is the key a member of the map? See also
gr<a>notMember</a>.
gr
gr<pre>
grmember 5 (fromList [(5,'a'), (3,'b')]) == True
grmember 1 (fromList [(5,'a'), (3,'b')]) == False
gr</pre>
member :: Ord k => k -> Map k a -> Bool

-- | <i>O(log n)</i>. Is the key not a member of the map? See also
gr<a>member</a>.
gr
gr<pre>
grnotMember 5 (fromList [(5,'a'), (3,'b')]) == False
grnotMember 1 (fromList [(5,'a'), (3,'b')]) == True
gr</pre>
notMember :: Ord k => k -> Map k a -> Bool

-- | <i>O(log n)</i>. Lookup the value at a key in the map.
gr
grThe function will return the corresponding value as <tt>(<a>Just</a>
grvalue)</tt>, or <a>Nothing</a> if the key isn't in the map.
gr
grAn example of using <tt>lookup</tt>:
gr
gr<pre>
grimport Prelude hiding (lookup)
grimport Data.Map
gr
gremployeeDept = fromList([("John","Sales"), ("Bob","IT")])
grdeptCountry = fromList([("IT","USA"), ("Sales","France")])
grcountryCurrency = fromList([("USA", "Dollar"), ("France", "Euro")])
gr
gremployeeCurrency :: String -&gt; Maybe String
gremployeeCurrency name = do
gr    dept &lt;- lookup name employeeDept
gr    country &lt;- lookup dept deptCountry
gr    lookup country countryCurrency
gr
grmain = do
gr    putStrLn $ "John's currency: " ++ (show (employeeCurrency "John"))
gr    putStrLn $ "Pete's currency: " ++ (show (employeeCurrency "Pete"))
gr</pre>
gr
grThe output of this program:
gr
gr<pre>
grJohn's currency: Just "Euro"
grPete's currency: Nothing
gr</pre>
lookup :: Ord k => k -> Map k a -> Maybe a

-- | <i>O(log n)</i>. The expression <tt>(<a>findWithDefault</a> def k
grmap)</tt> returns the value at key <tt>k</tt> or returns default value
gr<tt>def</tt> when the key is not in the map.
gr
gr<pre>
grfindWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x'
grfindWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a'
gr</pre>
findWithDefault :: Ord k => a -> k -> Map k a -> a

-- | <i>O(log n)</i>. Find largest key smaller than the given one and
grreturn the corresponding (key, value) pair.
gr
gr<pre>
grlookupLT 3 (fromList [(3,'a'), (5,'b')]) == Nothing
grlookupLT 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
gr</pre>
lookupLT :: Ord k => k -> Map k v -> Maybe (k, v)

-- | <i>O(log n)</i>. Find smallest key greater than the given one and
grreturn the corresponding (key, value) pair.
gr
gr<pre>
grlookupGT 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
grlookupGT 5 (fromList [(3,'a'), (5,'b')]) == Nothing
gr</pre>
lookupGT :: Ord k => k -> Map k v -> Maybe (k, v)

-- | <i>O(log n)</i>. Find largest key smaller or equal to the given one
grand return the corresponding (key, value) pair.
gr
gr<pre>
grlookupLE 2 (fromList [(3,'a'), (5,'b')]) == Nothing
grlookupLE 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
grlookupLE 5 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
gr</pre>
lookupLE :: Ord k => k -> Map k v -> Maybe (k, v)

-- | <i>O(log n)</i>. Find smallest key greater or equal to the given one
grand return the corresponding (key, value) pair.
gr
gr<pre>
grlookupGE 3 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
grlookupGE 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
grlookupGE 6 (fromList [(3,'a'), (5,'b')]) == Nothing
gr</pre>
lookupGE :: Ord k => k -> Map k v -> Maybe (k, v)

-- | <i>O(1)</i>. The empty map.
gr
gr<pre>
grempty      == fromList []
grsize empty == 0
gr</pre>
empty :: Map k a

-- | <i>O(1)</i>. A map with a single element.
gr
gr<pre>
grsingleton 1 'a'        == fromList [(1, 'a')]
grsize (singleton 1 'a') == 1
gr</pre>
singleton :: k -> a -> Map k a

-- | <i>O(log n)</i>. Insert a new key and value in the map. If the key is
gralready present in the map, the associated value is replaced with the
grsupplied value. <a>insert</a> is equivalent to <tt><a>insertWith</a>
gr<a>const</a></tt>.
gr
gr<pre>
grinsert 5 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'x')]
grinsert 7 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'a'), (7, 'x')]
grinsert 5 'x' empty                         == singleton 5 'x'
gr</pre>
insert :: Ord k => k -> a -> Map k a -> Map k a

-- | <i>O(log n)</i>. Insert with a function, combining new value and old
grvalue. <tt><a>insertWith</a> f key value mp</tt> will insert the pair
gr(key, value) into <tt>mp</tt> if key does not exist in the map. If the
grkey does exist, the function will insert the pair <tt>(key, f
grnew_value old_value)</tt>.
gr
gr<pre>
grinsertWith (++) 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "xxxa")]
grinsertWith (++) 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
grinsertWith (++) 5 "xxx" empty                         == singleton 5 "xxx"
gr</pre>
insertWith :: Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a

-- | <i>O(log n)</i>. Insert with a function, combining key, new value and
grold value. <tt><a>insertWithKey</a> f key value mp</tt> will insert
grthe pair (key, value) into <tt>mp</tt> if key does not exist in the
grmap. If the key does exist, the function will insert the pair
gr<tt>(key,f key new_value old_value)</tt>. Note that the key passed to
grf is the same key passed to <a>insertWithKey</a>.
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grinsertWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:xxx|a")]
grinsertWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
grinsertWithKey f 5 "xxx" empty                         == singleton 5 "xxx"
gr</pre>
insertWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a

-- | <i>O(log n)</i>. Combines insert operation with old value retrieval.
grThe expression (<tt><a>insertLookupWithKey</a> f k x map</tt>) is a
grpair where the first element is equal to (<tt><a>lookup</a> k
grmap</tt>) and the second element equal to (<tt><a>insertWithKey</a> f
grk x map</tt>).
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grinsertLookupWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:xxx|a")])
grinsertLookupWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a"), (7, "xxx")])
grinsertLookupWithKey f 5 "xxx" empty                         == (Nothing,  singleton 5 "xxx")
gr</pre>
gr
grThis is how to define <tt>insertLookup</tt> using
gr<tt>insertLookupWithKey</tt>:
gr
gr<pre>
grlet insertLookup kx x t = insertLookupWithKey (\_ a _ -&gt; a) kx x t
grinsertLookup 5 "x" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "x")])
grinsertLookup 7 "x" (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a"), (7, "x")])
gr</pre>
insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a, Map k a)

-- | <i>O(log n)</i>. Delete a key and its value from the map. When the key
gris not a member of the map, the original map is returned.
gr
gr<pre>
grdelete 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grdelete 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grdelete 5 empty                         == empty
gr</pre>
delete :: Ord k => k -> Map k a -> Map k a

-- | <i>O(log n)</i>. Update a value at a specific key with the result of
grthe provided function. When the key is not a member of the map, the
groriginal map is returned.
gr
gr<pre>
gradjust ("new " ++) 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
gradjust ("new " ++) 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gradjust ("new " ++) 7 empty                         == empty
gr</pre>
adjust :: Ord k => (a -> a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. Adjust a value at a specific key. When the key is not
gra member of the map, the original map is returned.
gr
gr<pre>
grlet f key x = (show key) ++ ":new " ++ x
gradjustWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
gradjustWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gradjustWithKey f 7 empty                         == empty
gr</pre>
adjustWithKey :: Ord k => (k -> a -> a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. The expression (<tt><a>update</a> f k map</tt>)
grupdates the value <tt>x</tt> at <tt>k</tt> (if it is in the map). If
gr(<tt>f x</tt>) is <a>Nothing</a>, the element is deleted. If it is
gr(<tt><a>Just</a> y</tt>), the key <tt>k</tt> is bound to the new value
gr<tt>y</tt>.
gr
gr<pre>
grlet f x = if x == "a" then Just "new a" else Nothing
grupdate f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
grupdate f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grupdate f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
update :: Ord k => (a -> Maybe a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. The expression (<tt><a>updateWithKey</a> f k
grmap</tt>) updates the value <tt>x</tt> at <tt>k</tt> (if it is in the
grmap). If (<tt>f k x</tt>) is <a>Nothing</a>, the element is deleted.
grIf it is (<tt><a>Just</a> y</tt>), the key <tt>k</tt> is bound to the
grnew value <tt>y</tt>.
gr
gr<pre>
grlet f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
grupdateWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
grupdateWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grupdateWithKey f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. Lookup and update. See also <a>updateWithKey</a>. The
grfunction returns changed value, if it is updated. Returns the original
grkey value if the map entry is deleted.
gr
gr<pre>
grlet f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
grupdateLookupWithKey f 5 (fromList [(5,"a"), (3,"b")]) == (Just "5:new a", fromList [(3, "b"), (5, "5:new a")])
grupdateLookupWithKey f 7 (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a")])
grupdateLookupWithKey f 3 (fromList [(5,"a"), (3,"b")]) == (Just "b", singleton 5 "a")
gr</pre>
updateLookupWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> (Maybe a, Map k a)

-- | <i>O(log n)</i>. The expression (<tt><a>alter</a> f k map</tt>) alters
grthe value <tt>x</tt> at <tt>k</tt>, or absence thereof. <a>alter</a>
grcan be used to insert, delete, or update a value in a <a>Map</a>. In
grshort : <tt><a>lookup</a> k (<a>alter</a> f k m) = f (<a>lookup</a> k
grm)</tt>.
gr
gr<pre>
grlet f _ = Nothing
gralter f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gralter f 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr
grlet f _ = Just "c"
gralter f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "c")]
gralter f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "c")]
gr</pre>
alter :: Ord k => (Maybe a -> Maybe a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. The expression (<tt><a>alterF</a> f k map</tt>)
gralters the value <tt>x</tt> at <tt>k</tt>, or absence thereof.
gr<a>alterF</a> can be used to inspect, insert, delete, or update a
grvalue in a <a>Map</a>. In short: <tt><a>lookup</a> k &lt;$&gt;
gr<a>alterF</a> f k m = f (<a>lookup</a> k m)</tt>.
gr
grExample:
gr
gr<pre>
grinteractiveAlter :: Int -&gt; Map Int String -&gt; IO (Map Int String)
grinteractiveAlter k m = alterF f k m where
gr  f Nothing -&gt; do
gr     putStrLn $ show k ++
gr         " was not found in the map. Would you like to add it?"
gr     getUserResponse1 :: IO (Maybe String)
gr  f (Just old) -&gt; do
gr     putStrLn "The key is currently bound to " ++ show old ++
gr         ". Would you like to change or delete it?"
gr     getUserresponse2 :: IO (Maybe String)
gr</pre>
gr
gr<a>alterF</a> is the most general operation for working with an
grindividual key that may or may not be in a given map. When used with
grtrivial functors like <a>Identity</a> and <a>Const</a>, it is often
grslightly slower than more specialized combinators like <a>lookup</a>
grand <a>insert</a>. However, when the functor is non-trivial and key
grcomparison is not particularly cheap, it is the fastest way.
gr
grNote on rewrite rules:
gr
grThis module includes GHC rewrite rules to optimize <a>alterF</a> for
grthe <a>Const</a> and <a>Identity</a> functors. In general, these rules
grimprove performance. The sole exception is that when using
gr<a>Identity</a>, deleting a key that is already absent takes longer
grthan it would without the rules. If you expect this to occur a very
grlarge fraction of the time, you might consider using a private copy of
grthe <a>Identity</a> type.
gr
grNote: <a>alterF</a> is a flipped version of the <tt>at</tt> combinator
grfrom <a>At</a>.
alterF :: (Functor f, Ord k) => (Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. The expression (<tt><a>union</a>
grt1 t2</tt>) takes the left-biased union of <tt>t1</tt> and
gr<tt>t2</tt>. It prefers <tt>t1</tt> when duplicate keys are
grencountered, i.e. (<tt><a>union</a> == <a>unionWith</a>
gr<a>const</a></tt>).
gr
gr<pre>
grunion (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "a"), (7, "C")]
gr</pre>
union :: Ord k => Map k a -> Map k a -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Union with a combining function.
gr
gr<pre>
grunionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "aA"), (7, "C")]
gr</pre>
unionWith :: Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Union with a combining function.
gr
gr<pre>
grlet f key left_value right_value = (show key) ++ ":" ++ left_value ++ "|" ++ right_value
grunionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "5:a|A"), (7, "C")]
gr</pre>
unionWithKey :: Ord k => (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a

-- | The union of a list of maps: (<tt><a>unions</a> == <a>foldl</a>
gr<a>union</a> <a>empty</a></tt>).
gr
gr<pre>
grunions [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
gr    == fromList [(3, "b"), (5, "a"), (7, "C")]
grunions [(fromList [(5, "A3"), (3, "B3")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "a"), (3, "b")])]
gr    == fromList [(3, "B3"), (5, "A3"), (7, "C")]
gr</pre>
unions :: Ord k => [Map k a] -> Map k a

-- | The union of a list of maps, with a combining operation:
gr(<tt><a>unionsWith</a> f == <a>foldl</a> (<a>unionWith</a> f)
gr<a>empty</a></tt>).
gr
gr<pre>
grunionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
gr    == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")]
gr</pre>
unionsWith :: Ord k => (a -> a -> a) -> [Map k a] -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Difference of two maps. Return
grelements of the first map not existing in the second map.
gr
gr<pre>
grdifference (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 3 "b"
gr</pre>
difference :: Ord k => Map k a -> Map k b -> Map k a

-- | <i>O(n+m)</i>. Difference with a combining function. When two equal
grkeys are encountered, the combining function is applied to the values
grof these keys. If it returns <a>Nothing</a>, the element is discarded
gr(proper set difference). If it returns (<tt><a>Just</a> y</tt>), the
grelement is updated with a new value <tt>y</tt>.
gr
gr<pre>
grlet f al ar = if al == "b" then Just (al ++ ":" ++ ar) else Nothing
grdifferenceWith f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (7, "C")])
gr    == singleton 3 "b:B"
gr</pre>
differenceWith :: Ord k => (a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a

-- | <i>O(n+m)</i>. Difference with a combining function. When two equal
grkeys are encountered, the combining function is applied to the key and
grboth values. If it returns <a>Nothing</a>, the element is discarded
gr(proper set difference). If it returns (<tt><a>Just</a> y</tt>), the
grelement is updated with a new value <tt>y</tt>.
gr
gr<pre>
grlet f k al ar = if al == "b" then Just ((show k) ++ ":" ++ al ++ "|" ++ ar) else Nothing
grdifferenceWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (10, "C")])
gr    == singleton 3 "3:b|B"
gr</pre>
differenceWithKey :: Ord k => (k -> a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Intersection of two maps. Return
grdata in the first map for the keys existing in both maps.
gr(<tt><a>intersection</a> m1 m2 == <a>intersectionWith</a> <a>const</a>
grm1 m2</tt>).
gr
gr<pre>
grintersection (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "a"
gr</pre>
intersection :: Ord k => Map k a -> Map k b -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Intersection with a combining
grfunction.
gr
gr<pre>
grintersectionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "aA"
gr</pre>
intersectionWith :: Ord k => (a -> b -> c) -> Map k a -> Map k b -> Map k c

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Intersection with a combining
grfunction.
gr
gr<pre>
grlet f k al ar = (show k) ++ ":" ++ al ++ "|" ++ ar
grintersectionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "5:a|A"
gr</pre>
intersectionWithKey :: Ord k => (k -> a -> b -> c) -> Map k a -> Map k b -> Map k c

-- | <i>O(n+m)</i>. An unsafe universal combining function.
gr
grWARNING: This function can produce corrupt maps and its results may
grdepend on the internal structures of its inputs. Users should prefer
gr<a>merge</a> or <a>mergeA</a>.
gr
grWhen <a>mergeWithKey</a> is given three arguments, it is inlined to
grthe call site. You should therefore use <a>mergeWithKey</a> only to
grdefine custom combining functions. For example, you could define
gr<a>unionWithKey</a>, <a>differenceWithKey</a> and
gr<a>intersectionWithKey</a> as
gr
gr<pre>
grmyUnionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -&gt; Just (f k x1 x2)) id id m1 m2
grmyDifferenceWithKey f m1 m2 = mergeWithKey f id (const empty) m1 m2
grmyIntersectionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -&gt; Just (f k x1 x2)) (const empty) (const empty) m1 m2
gr</pre>
gr
grWhen calling <tt><a>mergeWithKey</a> combine only1 only2</tt>, a
grfunction combining two <a>Map</a>s is created, such that
gr
gr<ul>
gr<li>if a key is present in both maps, it is passed with both
grcorresponding values to the <tt>combine</tt> function. Depending on
grthe result, the key is either present in the result with specified
grvalue, or is left out;</li>
gr<li>a nonempty subtree present only in the first map is passed to
gr<tt>only1</tt> and the output is added to the result;</li>
gr<li>a nonempty subtree present only in the second map is passed to
gr<tt>only2</tt> and the output is added to the result.</li>
gr</ul>
gr
grThe <tt>only1</tt> and <tt>only2</tt> methods <i>must return a map
grwith a subset (possibly empty) of the keys of the given map</i>. The
grvalues can be modified arbitrarily. Most common variants of
gr<tt>only1</tt> and <tt>only2</tt> are <a>id</a> and <tt><a>const</a>
gr<a>empty</a></tt>, but for example <tt><a>map</a> f</tt> or
gr<tt><a>filterWithKey</a> f</tt> could be used for any <tt>f</tt>.
mergeWithKey :: Ord k => (k -> a -> b -> Maybe c) -> (Map k a -> Map k c) -> (Map k b -> Map k c) -> Map k a -> Map k b -> Map k c

-- | <i>O(n)</i>. Map a function over all values in the map.
gr
gr<pre>
grmap (++ "x") (fromList [(5,"a"), (3,"b")]) == fromList [(3, "bx"), (5, "ax")]
gr</pre>
map :: (a -> b) -> Map k a -> Map k b

-- | <i>O(n)</i>. Map a function over all values in the map.
gr
gr<pre>
grlet f key x = (show key) ++ ":" ++ x
grmapWithKey f (fromList [(5,"a"), (3,"b")]) == fromList [(3, "3:b"), (5, "5:a")]
gr</pre>
mapWithKey :: (k -> a -> b) -> Map k a -> Map k b

-- | <i>O(n)</i>. <tt><a>traverseWithKey</a> f m == <a>fromList</a>
gr<a>$</a> <a>traverse</a> ((k, v) -&gt; (v' -&gt; v' <a>seq</a> (k,v'))
gr<a>$</a> f k v) (<a>toList</a> m)</tt> That is, it behaves much like a
grregular <a>traverse</a> except that the traversing function also has
graccess to the key associated with a value and the values are forced
grbefore they are installed in the result map.
gr
gr<pre>
grtraverseWithKey (\k v -&gt; if odd k then Just (succ v) else Nothing) (fromList [(1, 'a'), (5, 'e')]) == Just (fromList [(1, 'b'), (5, 'f')])
grtraverseWithKey (\k v -&gt; if odd k then Just (succ v) else Nothing) (fromList [(2, 'c')])           == Nothing
gr</pre>
traverseWithKey :: Applicative t => (k -> a -> t b) -> Map k a -> t (Map k b)

-- | <i>O(n)</i>. Traverse keys/values and collect the <a>Just</a> results.
traverseMaybeWithKey :: Applicative f => (k -> a -> f (Maybe b)) -> Map k a -> f (Map k b)

-- | <i>O(n)</i>. The function <a>mapAccum</a> threads an accumulating
grargument through the map in ascending order of keys.
gr
gr<pre>
grlet f a b = (a ++ b, b ++ "X")
grmapAccum f "Everything: " (fromList [(5,"a"), (3,"b")]) == ("Everything: ba", fromList [(3, "bX"), (5, "aX")])
gr</pre>
mapAccum :: (a -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)

-- | <i>O(n)</i>. The function <a>mapAccumWithKey</a> threads an
graccumulating argument through the map in ascending order of keys.
gr
gr<pre>
grlet f a k b = (a ++ " " ++ (show k) ++ "-" ++ b, b ++ "X")
grmapAccumWithKey f "Everything:" (fromList [(5,"a"), (3,"b")]) == ("Everything: 3-b 5-a", fromList [(3, "bX"), (5, "aX")])
gr</pre>
mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)

-- | <i>O(n)</i>. The function <tt>mapAccumR</tt> threads an accumulating
grargument through the map in descending order of keys.
mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)

-- | <i>O(n*log n)</i>. <tt><a>mapKeys</a> f s</tt> is the map obtained by
grapplying <tt>f</tt> to each key of <tt>s</tt>.
gr
grThe size of the result may be smaller if <tt>f</tt> maps two or more
grdistinct keys to the same new key. In this case the value at the
grgreatest of the original keys is retained.
gr
gr<pre>
grmapKeys (+ 1) (fromList [(5,"a"), (3,"b")])                        == fromList [(4, "b"), (6, "a")]
grmapKeys (\ _ -&gt; 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "c"
grmapKeys (\ _ -&gt; 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "c"
gr</pre>
mapKeys :: Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 a

-- | <i>O(n*log n)</i>. <tt><a>mapKeysWith</a> c f s</tt> is the map
grobtained by applying <tt>f</tt> to each key of <tt>s</tt>.
gr
grThe size of the result may be smaller if <tt>f</tt> maps two or more
grdistinct keys to the same new key. In this case the associated values
grwill be combined using <tt>c</tt>. The value at the greater of the two
groriginal keys is used as the first argument to <tt>c</tt>.
gr
gr<pre>
grmapKeysWith (++) (\ _ -&gt; 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "cdab"
grmapKeysWith (++) (\ _ -&gt; 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "cdab"
gr</pre>
mapKeysWith :: Ord k2 => (a -> a -> a) -> (k1 -> k2) -> Map k1 a -> Map k2 a

-- | <i>O(n)</i>. <tt><a>mapKeysMonotonic</a> f s == <a>mapKeys</a> f
grs</tt>, but works only when <tt>f</tt> is strictly monotonic. That is,
grfor any values <tt>x</tt> and <tt>y</tt>, if <tt>x</tt> &lt;
gr<tt>y</tt> then <tt>f x</tt> &lt; <tt>f y</tt>. <i>The precondition is
grnot checked.</i> Semi-formally, we have:
gr
gr<pre>
grand [x &lt; y ==&gt; f x &lt; f y | x &lt;- ls, y &lt;- ls]
gr                    ==&gt; mapKeysMonotonic f s == mapKeys f s
gr    where ls = keys s
gr</pre>
gr
grThis means that <tt>f</tt> maps distinct original keys to distinct
grresulting keys. This function has better performance than
gr<a>mapKeys</a>.
gr
gr<pre>
grmapKeysMonotonic (\ k -&gt; k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")]
grvalid (mapKeysMonotonic (\ k -&gt; k * 2) (fromList [(5,"a"), (3,"b")])) == True
grvalid (mapKeysMonotonic (\ _ -&gt; 1)     (fromList [(5,"a"), (3,"b")])) == False
gr</pre>
mapKeysMonotonic :: (k1 -> k2) -> Map k1 a -> Map k2 a

-- | <i>O(n)</i>. Fold the values in the map using the given
grright-associative binary operator, such that <tt><a>foldr</a> f z ==
gr<a>foldr</a> f z . <a>elems</a></tt>.
gr
grFor example,
gr
gr<pre>
grelems map = foldr (:) [] map
gr</pre>
gr
gr<pre>
grlet f a len = len + (length a)
grfoldr f 0 (fromList [(5,"a"), (3,"bbb")]) == 4
gr</pre>
foldr :: (a -> b -> b) -> b -> Map k a -> b

-- | <i>O(n)</i>. Fold the values in the map using the given
grleft-associative binary operator, such that <tt><a>foldl</a> f z ==
gr<a>foldl</a> f z . <a>elems</a></tt>.
gr
grFor example,
gr
gr<pre>
grelems = reverse . foldl (flip (:)) []
gr</pre>
gr
gr<pre>
grlet f len a = len + (length a)
grfoldl f 0 (fromList [(5,"a"), (3,"bbb")]) == 4
gr</pre>
foldl :: (a -> b -> a) -> a -> Map k b -> a

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grright-associative binary operator, such that <tt><a>foldrWithKey</a> f
grz == <a>foldr</a> (<a>uncurry</a> f) z . <a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grkeys map = foldrWithKey (\k x ks -&gt; k:ks) [] map
gr</pre>
gr
gr<pre>
grlet f k a result = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")"
grfoldrWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (5:a)(3:b)"
gr</pre>
foldrWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grleft-associative binary operator, such that <tt><a>foldlWithKey</a> f
grz == <a>foldl</a> (\z' (kx, x) -&gt; f z' kx x) z .
gr<a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grkeys = reverse . foldlWithKey (\ks k x -&gt; k:ks) []
gr</pre>
gr
gr<pre>
grlet f result k a = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")"
grfoldlWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (3:b)(5:a)"
gr</pre>
foldlWithKey :: (a -> k -> b -> a) -> a -> Map k b -> a

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grmonoid, such that
gr
gr<pre>
gr<a>foldMapWithKey</a> f = <a>fold</a> . <a>mapWithKey</a> f
gr</pre>
gr
grThis can be an asymptotically faster than <a>foldrWithKey</a> or
gr<a>foldlWithKey</a> for some monoids.
foldMapWithKey :: Monoid m => (k -> a -> m) -> Map k a -> m

-- | <i>O(n)</i>. A strict version of <a>foldr</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldr' :: (a -> b -> b) -> b -> Map k a -> b

-- | <i>O(n)</i>. A strict version of <a>foldl</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldl' :: (a -> b -> a) -> a -> Map k b -> a

-- | <i>O(n)</i>. A strict version of <a>foldrWithKey</a>. Each application
grof the operator is evaluated before using the result in the next
grapplication. This function is strict in the starting value.
foldrWithKey' :: (k -> a -> b -> b) -> b -> Map k a -> b

-- | <i>O(n)</i>. A strict version of <a>foldlWithKey</a>. Each application
grof the operator is evaluated before using the result in the next
grapplication. This function is strict in the starting value.
foldlWithKey' :: (a -> k -> b -> a) -> a -> Map k b -> a

-- | <i>O(n)</i>. Return all elements of the map in the ascending order of
grtheir keys. Subject to list fusion.
gr
gr<pre>
grelems (fromList [(5,"a"), (3,"b")]) == ["b","a"]
grelems empty == []
gr</pre>
elems :: Map k a -> [a]

-- | <i>O(n)</i>. Return all keys of the map in ascending order. Subject to
grlist fusion.
gr
gr<pre>
grkeys (fromList [(5,"a"), (3,"b")]) == [3,5]
grkeys empty == []
gr</pre>
keys :: Map k a -> [k]

-- | <i>O(n)</i>. An alias for <a>toAscList</a>. Return all key/value pairs
grin the map in ascending key order. Subject to list fusion.
gr
gr<pre>
grassocs (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
grassocs empty == []
gr</pre>
assocs :: Map k a -> [(k, a)]

-- | <i>O(n)</i>. The set of all keys of the map.
gr
gr<pre>
grkeysSet (fromList [(5,"a"), (3,"b")]) == Data.Set.fromList [3,5]
grkeysSet empty == Data.Set.empty
gr</pre>
keysSet :: Map k a -> Set k

-- | <i>O(n)</i>. Build a map from a set of keys and a function which for
greach key computes its value.
gr
gr<pre>
grfromSet (\k -&gt; replicate k 'a') (Data.Set.fromList [3, 5]) == fromList [(5,"aaaaa"), (3,"aaa")]
grfromSet undefined Data.Set.empty == empty
gr</pre>
fromSet :: (k -> a) -> Set k -> Map k a

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs. Subject to
grlist fusion.
gr
gr<pre>
grtoList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
grtoList empty == []
gr</pre>
toList :: Map k a -> [(k, a)]

-- | <i>O(n*log n)</i>. Build a map from a list of key/value pairs. See
gralso <a>fromAscList</a>. If the list contains more than one value for
grthe same key, the last value for the key is retained.
gr
grIf the keys of the list are ordered, linear-time implementation is
grused, with the performance equal to <a>fromDistinctAscList</a>.
gr
gr<pre>
grfromList [] == empty
grfromList [(5,"a"), (3,"b"), (5, "c")] == fromList [(5,"c"), (3,"b")]
grfromList [(5,"c"), (3,"b"), (5, "a")] == fromList [(5,"a"), (3,"b")]
gr</pre>
fromList :: Ord k => [(k, a)] -> Map k a

-- | <i>O(n*log n)</i>. Build a map from a list of key/value pairs with a
grcombining function. See also <a>fromAscListWith</a>.
gr
gr<pre>
grfromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")]
grfromListWith (++) [] == empty
gr</pre>
fromListWith :: Ord k => (a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n*log n)</i>. Build a map from a list of key/value pairs with a
grcombining function. See also <a>fromAscListWithKey</a>.
gr
gr<pre>
grlet f k a1 a2 = (show k) ++ a1 ++ a2
grfromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "3ab"), (5, "5a5ba")]
grfromListWithKey f [] == empty
gr</pre>
fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs where the
grkeys are in ascending order. Subject to list fusion.
gr
gr<pre>
grtoAscList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
gr</pre>
toAscList :: Map k a -> [(k, a)]

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs where the
grkeys are in descending order. Subject to list fusion.
gr
gr<pre>
grtoDescList (fromList [(5,"a"), (3,"b")]) == [(5,"a"), (3,"b")]
gr</pre>
toDescList :: Map k a -> [(k, a)]

-- | <i>O(n)</i>. Build a map from an ascending list in linear time. <i>The
grprecondition (input list is ascending) is not checked.</i>
gr
gr<pre>
grfromAscList [(3,"b"), (5,"a")]          == fromList [(3, "b"), (5, "a")]
grfromAscList [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "b")]
grvalid (fromAscList [(3,"b"), (5,"a"), (5,"b")]) == True
grvalid (fromAscList [(5,"a"), (3,"b"), (5,"b")]) == False
gr</pre>
fromAscList :: Eq k => [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from an ascending list in linear time with a
grcombining function for equal keys. <i>The precondition (input list is
grascending) is not checked.</i>
gr
gr<pre>
grfromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")]
grvalid (fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")]) == True
grvalid (fromAscListWith (++) [(5,"a"), (3,"b"), (5,"b")]) == False
gr</pre>
fromAscListWith :: Eq k => (a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from an ascending list in linear time with a
grcombining function for equal keys. <i>The precondition (input list is
grascending) is not checked.</i>
gr
gr<pre>
grlet f k a1 a2 = (show k) ++ ":" ++ a1 ++ a2
grfromAscListWithKey f [(3,"b"), (5,"a"), (5,"b"), (5,"b")] == fromList [(3, "b"), (5, "5:b5:ba")]
grvalid (fromAscListWithKey f [(3,"b"), (5,"a"), (5,"b"), (5,"b")]) == True
grvalid (fromAscListWithKey f [(5,"a"), (3,"b"), (5,"b"), (5,"b")]) == False
gr</pre>
fromAscListWithKey :: Eq k => (k -> a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from an ascending list of distinct elements
grin linear time. <i>The precondition is not checked.</i>
gr
gr<pre>
grfromDistinctAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")]
grvalid (fromDistinctAscList [(3,"b"), (5,"a")])          == True
grvalid (fromDistinctAscList [(3,"b"), (5,"a"), (5,"b")]) == False
gr</pre>
fromDistinctAscList :: [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from a descending list in linear time. <i>The
grprecondition (input list is descending) is not checked.</i>
gr
gr<pre>
grfromDescList [(5,"a"), (3,"b")]          == fromList [(3, "b"), (5, "a")]
grfromDescList [(5,"a"), (5,"b"), (3,"a")] == fromList [(3, "b"), (5, "b")]
grvalid (fromDescList [(5,"a"), (5,"b"), (3,"b")]) == True
grvalid (fromDescList [(5,"a"), (3,"b"), (5,"b")]) == False
gr</pre>
fromDescList :: Eq k => [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from a descending list in linear time with a
grcombining function for equal keys. <i>The precondition (input list is
grdescending) is not checked.</i>
gr
gr<pre>
grfromDescListWith (++) [(5,"a"), (5,"b"), (3,"b")] == fromList [(3, "b"), (5, "ba")]
grvalid (fromDescListWith (++) [(5,"a"), (5,"b"), (3,"b")]) == True
grvalid (fromDescListWith (++) [(5,"a"), (3,"b"), (5,"b")]) == False
gr</pre>
fromDescListWith :: Eq k => (a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from a descending list in linear time with a
grcombining function for equal keys. <i>The precondition (input list is
grdescending) is not checked.</i>
gr
gr<pre>
grlet f k a1 a2 = (show k) ++ ":" ++ a1 ++ a2
grfromDescListWithKey f [(5,"a"), (5,"b"), (5,"b"), (3,"b")] == fromList [(3, "b"), (5, "5:b5:ba")]
grvalid (fromDescListWithKey f [(5,"a"), (5,"b"), (5,"b"), (3,"b")]) == True
grvalid (fromDescListWithKey f [(5,"a"), (3,"b"), (5,"b"), (5,"b")]) == False
gr</pre>
fromDescListWithKey :: Eq k => (k -> a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from a descending list of distinct elements
grin linear time. <i>The precondition is not checked.</i>
gr
gr<pre>
grfromDistinctDescList [(5,"a"), (3,"b")] == fromList [(3, "b"), (5, "a")]
grvalid (fromDistinctDescList [(5,"a"), (3,"b")])          == True
grvalid (fromDistinctDescList [(5,"a"), (3,"b"), (3,"a")]) == False
gr</pre>
fromDistinctDescList :: [(k, a)] -> Map k a

-- | <i>O(n)</i>. Filter all values that satisfy the predicate.
gr
gr<pre>
grfilter (&gt; "a") (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grfilter (&gt; "x") (fromList [(5,"a"), (3,"b")]) == empty
grfilter (&lt; "a") (fromList [(5,"a"), (3,"b")]) == empty
gr</pre>
filter :: (a -> Bool) -> Map k a -> Map k a

-- | <i>O(n)</i>. Filter all keys/values that satisfy the predicate.
gr
gr<pre>
grfilterWithKey (\k _ -&gt; k &gt; 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
filterWithKey :: (k -> a -> Bool) -> Map k a -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Restrict a <a>Map</a> to only
grthose keys found in a <a>Set</a>.
gr
gr<pre>
grm `<tt>restrictKeys'</tt> s = <a>filterWithKey</a> (k _ -&gt; k `<a>member'</a> s) m
grm `<tt>restrictKeys'</tt> s = m `<tt>intersect</tt> <a>fromSet</a> (const ()) s
gr</pre>
restrictKeys :: Ord k => Map k a -> Set k -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Remove all keys in a <a>Set</a>
grfrom a <a>Map</a>.
gr
gr<pre>
grm `<tt>withoutKeys'</tt> s = <a>filterWithKey</a> (k _ -&gt; k `<a>notMember'</a> s) m
grm `<tt>withoutKeys'</tt> s = m `<tt>difference'</tt> <a>fromSet</a> (const ()) s
gr</pre>
withoutKeys :: Ord k => Map k a -> Set k -> Map k a

-- | <i>O(n)</i>. Partition the map according to a predicate. The first map
grcontains all elements that satisfy the predicate, the second all
grelements that fail the predicate. See also <a>split</a>.
gr
gr<pre>
grpartition (&gt; "a") (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a")
grpartition (&lt; "x") (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
grpartition (&gt; "x") (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
gr</pre>
partition :: (a -> Bool) -> Map k a -> (Map k a, Map k a)

-- | <i>O(n)</i>. Partition the map according to a predicate. The first map
grcontains all elements that satisfy the predicate, the second all
grelements that fail the predicate. See also <a>split</a>.
gr
gr<pre>
grpartitionWithKey (\ k _ -&gt; k &gt; 3) (fromList [(5,"a"), (3,"b")]) == (singleton 5 "a", singleton 3 "b")
grpartitionWithKey (\ k _ -&gt; k &lt; 7) (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
grpartitionWithKey (\ k _ -&gt; k &gt; 7) (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
gr</pre>
partitionWithKey :: (k -> a -> Bool) -> Map k a -> (Map k a, Map k a)

-- | <i>O(log n)</i>. Take while a predicate on the keys holds. The user is
grresponsible for ensuring that for all keys <tt>j</tt> and <tt>k</tt>
grin the map, <tt>j &lt; k ==&gt; p j &gt;= p k</tt>. See note at
gr<a>spanAntitone</a>.
gr
gr<pre>
grtakeWhileAntitone p = <a>fromDistinctAscList</a> . <a>takeWhile</a> (p . fst) . <a>toList</a>
grtakeWhileAntitone p = <a>filterWithKey</a> (k _ -&gt; p k)
gr</pre>
takeWhileAntitone :: (k -> Bool) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Drop while a predicate on the keys holds. The user is
grresponsible for ensuring that for all keys <tt>j</tt> and <tt>k</tt>
grin the map, <tt>j &lt; k ==&gt; p j &gt;= p k</tt>. See note at
gr<a>spanAntitone</a>.
gr
gr<pre>
grdropWhileAntitone p = <a>fromDistinctAscList</a> . <a>dropWhile</a> (p . fst) . <a>toList</a>
grdropWhileAntitone p = <a>filterWithKey</a> (k -&gt; not (p k))
gr</pre>
dropWhileAntitone :: (k -> Bool) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Divide a map at the point where a predicate on the
grkeys stops holding. The user is responsible for ensuring that for all
grkeys <tt>j</tt> and <tt>k</tt> in the map, <tt>j &lt; k ==&gt; p j
gr&gt;= p k</tt>.
gr
gr<pre>
grspanAntitone p xs = (<a>takeWhileAntitone</a> p xs, <a>dropWhileAntitone</a> p xs)
grspanAntitone p xs = partition p xs
gr</pre>
gr
grNote: if <tt>p</tt> is not actually antitone, then
gr<tt>spanAntitone</tt> will split the map at some <i>unspecified</i>
grpoint where the predicate switches from holding to not holding (where
grthe predicate is seen to hold before the first key and to fail after
grthe last key).
spanAntitone :: (k -> Bool) -> Map k a -> (Map k a, Map k a)

-- | <i>O(n)</i>. Map values and collect the <a>Just</a> results.
gr
gr<pre>
grlet f x = if x == "a" then Just "new a" else Nothing
grmapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"
gr</pre>
mapMaybe :: (a -> Maybe b) -> Map k a -> Map k b

-- | <i>O(n)</i>. Map keys/values and collect the <a>Just</a> results.
gr
gr<pre>
grlet f k _ = if k &lt; 5 then Just ("key : " ++ (show k)) else Nothing
grmapMaybeWithKey f (fromList [(5,"a"), (3,"b")]) == singleton 3 "key : 3"
gr</pre>
mapMaybeWithKey :: (k -> a -> Maybe b) -> Map k a -> Map k b

-- | <i>O(n)</i>. Map values and separate the <a>Left</a> and <a>Right</a>
grresults.
gr
gr<pre>
grlet f a = if a &lt; "c" then Left a else Right a
grmapEither f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (fromList [(3,"b"), (5,"a")], fromList [(1,"x"), (7,"z")])
gr
grmapEither (\ a -&gt; Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (empty, fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr</pre>
mapEither :: (a -> Either b c) -> Map k a -> (Map k b, Map k c)

-- | <i>O(n)</i>. Map keys/values and separate the <a>Left</a> and
gr<a>Right</a> results.
gr
gr<pre>
grlet f k a = if k &lt; 5 then Left (k * 2) else Right (a ++ a)
grmapEitherWithKey f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (fromList [(1,2), (3,6)], fromList [(5,"aa"), (7,"zz")])
gr
grmapEitherWithKey (\_ a -&gt; Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (empty, fromList [(1,"x"), (3,"b"), (5,"a"), (7,"z")])
gr</pre>
mapEitherWithKey :: (k -> a -> Either b c) -> Map k a -> (Map k b, Map k c)

-- | <i>O(log n)</i>. The expression (<tt><a>split</a> k map</tt>) is a
grpair <tt>(map1,map2)</tt> where the keys in <tt>map1</tt> are smaller
grthan <tt>k</tt> and the keys in <tt>map2</tt> larger than <tt>k</tt>.
grAny key equal to <tt>k</tt> is found in neither <tt>map1</tt> nor
gr<tt>map2</tt>.
gr
gr<pre>
grsplit 2 (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3,"b"), (5,"a")])
grsplit 3 (fromList [(5,"a"), (3,"b")]) == (empty, singleton 5 "a")
grsplit 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a")
grsplit 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", empty)
grsplit 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], empty)
gr</pre>
split :: Ord k => k -> Map k a -> (Map k a, Map k a)

-- | <i>O(log n)</i>. The expression (<tt><a>splitLookup</a> k map</tt>)
grsplits a map just like <a>split</a> but also returns <tt><a>lookup</a>
grk map</tt>.
gr
gr<pre>
grsplitLookup 2 (fromList [(5,"a"), (3,"b")]) == (empty, Nothing, fromList [(3,"b"), (5,"a")])
grsplitLookup 3 (fromList [(5,"a"), (3,"b")]) == (empty, Just "b", singleton 5 "a")
grsplitLookup 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Nothing, singleton 5 "a")
grsplitLookup 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Just "a", empty)
grsplitLookup 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], Nothing, empty)
gr</pre>
splitLookup :: Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)

-- | <i>O(1)</i>. Decompose a map into pieces based on the structure of the
grunderlying tree. This function is useful for consuming a map in
grparallel.
gr
grNo guarantee is made as to the sizes of the pieces; an internal, but
grdeterministic process determines this. However, it is guaranteed that
grthe pieces returned will be in ascending order (all elements in the
grfirst submap less than all elements in the second, and so on).
gr
grExamples:
gr
gr<pre>
grsplitRoot (fromList (zip [1..6] ['a'..])) ==
gr  [fromList [(1,'a'),(2,'b'),(3,'c')],fromList [(4,'d')],fromList [(5,'e'),(6,'f')]]
gr</pre>
gr
gr<pre>
grsplitRoot empty == []
gr</pre>
gr
grNote that the current implementation does not return more than three
grsubmaps, but you should not depend on this behaviour because it can
grchange in the future without notice.
splitRoot :: Map k b -> [Map k b]

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. This function is defined as
gr(<tt><a>isSubmapOf</a> = <a>isSubmapOfBy</a> (==)</tt>).
isSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. The expression
gr(<tt><a>isSubmapOfBy</a> f t1 t2</tt>) returns <a>True</a> if all keys
grin <tt>t1</tt> are in tree <tt>t2</tt>, and when <tt>f</tt> returns
gr<a>True</a> when applied to their respective values. For example, the
grfollowing expressions are all <a>True</a>:
gr
gr<pre>
grisSubmapOfBy (==) (fromList [('a',1)]) (fromList [('a',1),('b',2)])
grisSubmapOfBy (&lt;=) (fromList [('a',1)]) (fromList [('a',1),('b',2)])
grisSubmapOfBy (==) (fromList [('a',1),('b',2)]) (fromList [('a',1),('b',2)])
gr</pre>
gr
grBut the following are all <a>False</a>:
gr
gr<pre>
grisSubmapOfBy (==) (fromList [('a',2)]) (fromList [('a',1),('b',2)])
grisSubmapOfBy (&lt;)  (fromList [('a',1)]) (fromList [('a',1),('b',2)])
grisSubmapOfBy (==) (fromList [('a',1),('b',2)]) (fromList [('a',1)])
gr</pre>
isSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Is this a proper submap? (ie. a
grsubmap but not equal). Defined as (<tt><a>isProperSubmapOf</a> =
gr<a>isProperSubmapOfBy</a> (==)</tt>).
isProperSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Is this a proper submap? (ie. a
grsubmap but not equal). The expression (<tt><a>isProperSubmapOfBy</a> f
grm1 m2</tt>) returns <a>True</a> when <tt>m1</tt> and <tt>m2</tt> are
grnot equal, all keys in <tt>m1</tt> are in <tt>m2</tt>, and when
gr<tt>f</tt> returns <a>True</a> when applied to their respective
grvalues. For example, the following expressions are all <a>True</a>:
gr
gr<pre>
grisProperSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
grisProperSubmapOfBy (&lt;=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
gr</pre>
gr
grBut the following are all <a>False</a>:
gr
gr<pre>
grisProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])
grisProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
grisProperSubmapOfBy (&lt;)  (fromList [(1,1)])       (fromList [(1,1),(2,2)])
gr</pre>
isProperSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool

-- | <i>O(log n)</i>. Lookup the <i>index</i> of a key, which is its
grzero-based index in the sequence sorted by keys. The index is a number
grfrom <i>0</i> up to, but not including, the <a>size</a> of the map.
gr
gr<pre>
grisJust (lookupIndex 2 (fromList [(5,"a"), (3,"b")]))   == False
grfromJust (lookupIndex 3 (fromList [(5,"a"), (3,"b")])) == 0
grfromJust (lookupIndex 5 (fromList [(5,"a"), (3,"b")])) == 1
grisJust (lookupIndex 6 (fromList [(5,"a"), (3,"b")]))   == False
gr</pre>
lookupIndex :: Ord k => k -> Map k a -> Maybe Int

-- | <i>O(log n)</i>. Return the <i>index</i> of a key, which is its
grzero-based index in the sequence sorted by keys. The index is a number
grfrom <i>0</i> up to, but not including, the <a>size</a> of the map.
grCalls <a>error</a> when the key is not a <a>member</a> of the map.
gr
gr<pre>
grfindIndex 2 (fromList [(5,"a"), (3,"b")])    Error: element is not in the map
grfindIndex 3 (fromList [(5,"a"), (3,"b")]) == 0
grfindIndex 5 (fromList [(5,"a"), (3,"b")]) == 1
grfindIndex 6 (fromList [(5,"a"), (3,"b")])    Error: element is not in the map
gr</pre>
findIndex :: Ord k => k -> Map k a -> Int

-- | <i>O(log n)</i>. Retrieve an element by its <i>index</i>, i.e. by its
grzero-based index in the sequence sorted by keys. If the <i>index</i>
gris out of range (less than zero, greater or equal to <a>size</a> of
grthe map), <a>error</a> is called.
gr
gr<pre>
grelemAt 0 (fromList [(5,"a"), (3,"b")]) == (3,"b")
grelemAt 1 (fromList [(5,"a"), (3,"b")]) == (5, "a")
grelemAt 2 (fromList [(5,"a"), (3,"b")])    Error: index out of range
gr</pre>
elemAt :: Int -> Map k a -> (k, a)

-- | <i>O(log n)</i>. Update the element at <i>index</i>. Calls
gr<a>error</a> when an invalid index is used.
gr
gr<pre>
grupdateAt (\ _ _ -&gt; Just "x") 0    (fromList [(5,"a"), (3,"b")]) == fromList [(3, "x"), (5, "a")]
grupdateAt (\ _ _ -&gt; Just "x") 1    (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "x")]
grupdateAt (\ _ _ -&gt; Just "x") 2    (fromList [(5,"a"), (3,"b")])    Error: index out of range
grupdateAt (\ _ _ -&gt; Just "x") (-1) (fromList [(5,"a"), (3,"b")])    Error: index out of range
grupdateAt (\_ _  -&gt; Nothing)  0    (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
grupdateAt (\_ _  -&gt; Nothing)  1    (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grupdateAt (\_ _  -&gt; Nothing)  2    (fromList [(5,"a"), (3,"b")])    Error: index out of range
grupdateAt (\_ _  -&gt; Nothing)  (-1) (fromList [(5,"a"), (3,"b")])    Error: index out of range
gr</pre>
updateAt :: (k -> a -> Maybe a) -> Int -> Map k a -> Map k a

-- | <i>O(log n)</i>. Delete the element at <i>index</i>, i.e. by its
grzero-based index in the sequence sorted by keys. If the <i>index</i>
gris out of range (less than zero, greater or equal to <a>size</a> of
grthe map), <a>error</a> is called.
gr
gr<pre>
grdeleteAt 0  (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
grdeleteAt 1  (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grdeleteAt 2 (fromList [(5,"a"), (3,"b")])     Error: index out of range
grdeleteAt (-1) (fromList [(5,"a"), (3,"b")])  Error: index out of range
gr</pre>
deleteAt :: Int -> Map k a -> Map k a

-- | Take a given number of entries in key order, beginning with the
grsmallest keys.
gr
gr<pre>
grtake n = <a>fromDistinctAscList</a> . <a>take</a> n . <a>toAscList</a>
gr</pre>
take :: Int -> Map k a -> Map k a

-- | Drop a given number of entries in key order, beginning with the
grsmallest keys.
gr
gr<pre>
grdrop n = <a>fromDistinctAscList</a> . <a>drop</a> n . <a>toAscList</a>
gr</pre>
drop :: Int -> Map k a -> Map k a

-- | <i>O(log n)</i>. Split a map at a particular index.
gr
gr<pre>
grsplitAt !n !xs = (<a>take</a> n xs, <a>drop</a> n xs)
gr</pre>
splitAt :: Int -> Map k a -> (Map k a, Map k a)

-- | <i>O(log n)</i>. The minimal key of the map. Returns <a>Nothing</a> if
grthe map is empty.
gr
gr<pre>
grlookupMin (fromList [(5,"a"), (3,"b")]) == Just (3,"b")
grfindMin empty = Nothing
gr</pre>
lookupMin :: Map k a -> Maybe (k, a)

-- | <i>O(log n)</i>. The maximal key of the map. Returns <a>Nothing</a> if
grthe map is empty.
gr
gr<pre>
grlookupMax (fromList [(5,"a"), (3,"b")]) == Just (5,"a")
grlookupMax empty = Nothing
gr</pre>
lookupMax :: Map k a -> Maybe (k, a)

-- | <i>O(log n)</i>. The minimal key of the map. Calls <a>error</a> if the
grmap is empty.
gr
gr<pre>
grfindMin (fromList [(5,"a"), (3,"b")]) == (3,"b")
grfindMin empty                            Error: empty map has no minimal element
gr</pre>
findMin :: Map k a -> (k, a)
findMax :: Map k a -> (k, a)

-- | <i>O(log n)</i>. Delete the minimal key. Returns an empty map if the
grmap is empty.
gr
gr<pre>
grdeleteMin (fromList [(5,"a"), (3,"b"), (7,"c")]) == fromList [(5,"a"), (7,"c")]
grdeleteMin empty == empty
gr</pre>
deleteMin :: Map k a -> Map k a

-- | <i>O(log n)</i>. Delete the maximal key. Returns an empty map if the
grmap is empty.
gr
gr<pre>
grdeleteMax (fromList [(5,"a"), (3,"b"), (7,"c")]) == fromList [(3,"b"), (5,"a")]
grdeleteMax empty == empty
gr</pre>
deleteMax :: Map k a -> Map k a

-- | <i>O(log n)</i>. Delete and find the minimal element.
gr
gr<pre>
grdeleteFindMin (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((3,"b"), fromList[(5,"a"), (10,"c")])
grdeleteFindMin                                            Error: can not return the minimal element of an empty map
gr</pre>
deleteFindMin :: Map k a -> ((k, a), Map k a)

-- | <i>O(log n)</i>. Delete and find the maximal element.
gr
gr<pre>
grdeleteFindMax (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((10,"c"), fromList [(3,"b"), (5,"a")])
grdeleteFindMax empty                                      Error: can not return the maximal element of an empty map
gr</pre>
deleteFindMax :: Map k a -> ((k, a), Map k a)

-- | <i>O(log n)</i>. Update the value at the minimal key.
gr
gr<pre>
grupdateMin (\ a -&gt; Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "Xb"), (5, "a")]
grupdateMin (\ _ -&gt; Nothing)         (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateMin :: (a -> Maybe a) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Update the value at the maximal key.
gr
gr<pre>
grupdateMax (\ a -&gt; Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "Xa")]
grupdateMax (\ _ -&gt; Nothing)         (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr</pre>
updateMax :: (a -> Maybe a) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Update the value at the minimal key.
gr
gr<pre>
grupdateMinWithKey (\ k a -&gt; Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"3:b"), (5,"a")]
grupdateMinWithKey (\ _ _ -&gt; Nothing)                     (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateMinWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Update the value at the maximal key.
gr
gr<pre>
grupdateMaxWithKey (\ k a -&gt; Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"b"), (5,"5:a")]
grupdateMaxWithKey (\ _ _ -&gt; Nothing)                     (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr</pre>
updateMaxWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Retrieves the value associated with minimal key of
grthe map, and the map stripped of that element, or <a>Nothing</a> if
grpassed an empty map.
gr
gr<pre>
grminView (fromList [(5,"a"), (3,"b")]) == Just ("b", singleton 5 "a")
grminView empty == Nothing
gr</pre>
minView :: Map k a -> Maybe (a, Map k a)

-- | <i>O(log n)</i>. Retrieves the value associated with maximal key of
grthe map, and the map stripped of that element, or <a>Nothing</a> if
grpassed an empty map.
gr
gr<pre>
grmaxView (fromList [(5,"a"), (3,"b")]) == Just ("a", singleton 3 "b")
grmaxView empty == Nothing
gr</pre>
maxView :: Map k a -> Maybe (a, Map k a)

-- | <i>O(log n)</i>. Retrieves the minimal (key,value) pair of the map,
grand the map stripped of that element, or <a>Nothing</a> if passed an
grempty map.
gr
gr<pre>
grminViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((3,"b"), singleton 5 "a")
grminViewWithKey empty == Nothing
gr</pre>
minViewWithKey :: Map k a -> Maybe ((k, a), Map k a)

-- | <i>O(log n)</i>. Retrieves the maximal (key,value) pair of the map,
grand the map stripped of that element, or <a>Nothing</a> if passed an
grempty map.
gr
gr<pre>
grmaxViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((5,"a"), singleton 3 "b")
grmaxViewWithKey empty == Nothing
gr</pre>
maxViewWithKey :: Map k a -> Maybe ((k, a), Map k a)

-- | <i>O(n)</i>. Show the tree that implements the map. The tree is shown
grin a compressed, hanging format. See <a>showTreeWith</a>.

-- | <i>Deprecated: <a>showTree</a> is now in
gr<a>Data.Map.Internal.Debug</a></i>
showTree :: (Show k, Show a) => Map k a -> String

-- | <i>O(n)</i>. The expression (<tt><a>showTreeWith</a> showelem hang
grwide map</tt>) shows the tree that implements the map. Elements are
grshown using the <tt>showElem</tt> function. If <tt>hang</tt> is
gr<a>True</a>, a <i>hanging</i> tree is shown otherwise a rotated tree
gris shown. If <tt>wide</tt> is <a>True</a>, an extra wide version is
grshown.
gr
gr<pre>
grMap&gt; let t = fromDistinctAscList [(x,()) | x &lt;- [1..5]]
grMap&gt; putStrLn $ showTreeWith (\k x -&gt; show (k,x)) True False t
gr(4,())
gr+--(2,())
gr|  +--(1,())
gr|  +--(3,())
gr+--(5,())
gr
grMap&gt; putStrLn $ showTreeWith (\k x -&gt; show (k,x)) True True t
gr(4,())
gr|
gr+--(2,())
gr|  |
gr|  +--(1,())
gr|  |
gr|  +--(3,())
gr|
gr+--(5,())
gr
grMap&gt; putStrLn $ showTreeWith (\k x -&gt; show (k,x)) False True t
gr+--(5,())
gr|
gr(4,())
gr|
gr|  +--(3,())
gr|  |
gr+--(2,())
gr   |
gr   +--(1,())
gr</pre>

-- | <i>Deprecated: <a>showTreeWith</a> is now in
gr<a>Data.Map.Internal.Debug</a></i>
showTreeWith :: (k -> a -> String) -> Bool -> Bool -> Map k a -> String

-- | <i>O(n)</i>. Test if the internal map structure is valid.
gr
gr<pre>
grvalid (fromAscList [(3,"b"), (5,"a")]) == True
grvalid (fromAscList [(5,"a"), (3,"b")]) == False
gr</pre>
valid :: Ord k => Map k a -> Bool


-- | This module defines an API for writing functions that merge two maps.
grThe key functions are <a>merge</a> and <a>mergeA</a>. Each of these
grcan be used with several different "merge tactics".
gr
grThe <a>merge</a> and <a>mergeA</a> functions are shared by the lazy
grand strict modules. Only the choice of merge tactics determines
grstrictness. If you use <a>mapMissing</a> from this module then the
grresults will be forced before they are inserted. If you use
gr<a>mapMissing</a> from <a>Data.Map.Merge.Lazy</a> then they will not.
gr
gr<h2>Efficiency note</h2>
gr
grThe <tt>Category</tt>, <a>Applicative</a>, and <a>Monad</a> instances
grfor <a>WhenMissing</a> tactics are included because they are valid.
grHowever, they are inefficient in many cases and should usually be
gravoided. The instances for <a>WhenMatched</a> tactics should not pose
grany major efficiency problems.
module Data.Map.Merge.Strict

-- | A tactic for dealing with keys present in one map but not the other in
gr<a>merge</a>.
gr
grA tactic of type <tt> SimpleWhenMissing k x z </tt> is an abstract
grrepresentation of a function of type <tt> k -&gt; x -&gt; Maybe z
gr</tt>.
type SimpleWhenMissing = WhenMissing Identity

-- | A tactic for dealing with keys present in both maps in <a>merge</a>.
gr
grA tactic of type <tt> SimpleWhenMatched k x y z </tt> is an abstract
grrepresentation of a function of type <tt> k -&gt; x -&gt; y -&gt;
grMaybe z </tt>.
type SimpleWhenMatched = WhenMatched Identity

-- | Merge two maps.
gr
gr<tt>merge</tt> takes two <a>WhenMissing</a> tactics, a
gr<a>WhenMatched</a> tactic and two maps. It uses the tactics to merge
grthe maps. Its behavior is best understood via its fundamental tactics,
gr<a>mapMaybeMissing</a> and <a>zipWithMaybeMatched</a>.
gr
grConsider
gr
gr<pre>
grmerge (mapMaybeMissing g1)
gr             (mapMaybeMissing g2)
gr             (zipWithMaybeMatched f)
gr             m1 m2
gr</pre>
gr
grTake, for example,
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>), (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 = [(1, "one"), (2, "two"), (4, "three")]
gr</pre>
gr
gr<tt>merge</tt> will first '<tt>align'</tt> these maps by key:
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>),               (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 =           [(1, "one"), (2, "two"),          (4, "three")]
gr</pre>
gr
grIt will then pass the individual entries and pairs of entries to
gr<tt>g1</tt>, <tt>g2</tt>, or <tt>f</tt> as appropriate:
gr
gr<pre>
grmaybes = [g1 0 <tt>a</tt>, f 1 <tt>b</tt> "one", g2 2 "two", g1 3 <tt>c</tt>, f 4 <tt>d</tt> "three"]
gr</pre>
gr
grThis produces a <a>Maybe</a> for each key:
gr
gr<pre>
grkeys =     0        1          2           3        4
grresults = [Nothing, Just True, Just False, Nothing, Just True]
gr</pre>
gr
grFinally, the <tt>Just</tt> results are collected into a map:
gr
gr<pre>
grreturn value = [(1, True), (2, False), (4, True)]
gr</pre>
gr
grThe other tactics below are optimizations or simplifications of
gr<a>mapMaybeMissing</a> for special cases. Most importantly,
gr
gr<ul>
gr<li><a>dropMissing</a> drops all the keys.</li>
gr<li><a>preserveMissing</a> leaves all the entries alone.</li>
gr</ul>
gr
grWhen <a>merge</a> is given three arguments, it is inlined at the call
grsite. To prevent excessive inlining, you should typically use
gr<a>merge</a> to define your custom combining functions.
gr
grExamples:
gr
gr<pre>
grunionWithKey f = merge preserveMissing preserveMissing (zipWithMatched f)
gr</pre>
gr
gr<pre>
grintersectionWithKey f = merge dropMissing dropMissing (zipWithMatched f)
gr</pre>
gr
gr<pre>
grdifferenceWith f = merge diffPreserve diffDrop f
gr</pre>
gr
gr<pre>
grsymmetricDifference = merge diffPreserve diffPreserve (\ _ _ _ -&gt; Nothing)
gr</pre>
gr
gr<pre>
grmapEachPiece f g h = merge (diffMapWithKey f) (diffMapWithKey g)
gr</pre>
merge :: Ord k => SimpleWhenMissing k a c -> SimpleWhenMissing k b c -> SimpleWhenMatched k a b c -> Map k a -> Map k b -> Map k c

-- | When a key is found in both maps, apply a function to the key and
grvalues and maybe use the result in the merged map.
gr
gr<pre>
grzipWithMaybeMatched :: (k -&gt; x -&gt; y -&gt; Maybe z)
gr                    -&gt; SimpleWhenMatched k x y z
gr</pre>
zipWithMaybeMatched :: Applicative f => (k -> x -> y -> Maybe z) -> WhenMatched f k x y z

-- | When a key is found in both maps, apply a function to the key and
grvalues and use the result in the merged map.
gr
gr<pre>
grzipWithMatched :: (k -&gt; x -&gt; y -&gt; z)
gr               -&gt; SimpleWhenMatched k x y z
gr</pre>
zipWithMatched :: Applicative f => (k -> x -> y -> z) -> WhenMatched f k x y z

-- | Map over the entries whose keys are missing from the other map,
groptionally removing some. This is the most powerful
gr<a>SimpleWhenMissing</a> tactic, but others are usually more
grefficient.
gr
gr<pre>
grmapMaybeMissing :: (k -&gt; x -&gt; Maybe y) -&gt; SimpleWhenMissing k x y
gr</pre>
gr
gr<pre>
grmapMaybeMissing f = traverseMaybeMissing (\k x -&gt; pure (f k x))
gr</pre>
gr
grbut <tt>mapMaybeMissing</tt> uses fewer unnecessary <a>Applicative</a>
groperations.
mapMaybeMissing :: Applicative f => (k -> x -> Maybe y) -> WhenMissing f k x y

-- | Drop all the entries whose keys are missing from the other map.
gr
gr<pre>
grdropMissing :: SimpleWhenMissing k x y
gr</pre>
gr
gr<pre>
grdropMissing = mapMaybeMissing (\_ _ -&gt; Nothing)
gr</pre>
gr
grbut <tt>dropMissing</tt> is much faster.
dropMissing :: Applicative f => WhenMissing f k x y

-- | Preserve, unchanged, the entries whose keys are missing from the other
grmap.
gr
gr<pre>
grpreserveMissing :: SimpleWhenMissing k x x
gr</pre>
gr
gr<pre>
grpreserveMissing = Merge.Lazy.mapMaybeMissing (\_ x -&gt; Just x)
gr</pre>
gr
grbut <tt>preserveMissing</tt> is much faster.
preserveMissing :: Applicative f => WhenMissing f k x x

-- | Map over the entries whose keys are missing from the other map.
gr
gr<pre>
grmapMissing :: (k -&gt; x -&gt; y) -&gt; SimpleWhenMissing k x y
gr</pre>
gr
gr<pre>
grmapMissing f = mapMaybeMissing (\k x -&gt; Just $ f k x)
gr</pre>
gr
grbut <tt>mapMissing</tt> is somewhat faster.
mapMissing :: Applicative f => (k -> x -> y) -> WhenMissing f k x y

-- | Filter the entries whose keys are missing from the other map.
gr
gr<pre>
grfilterMissing :: (k -&gt; x -&gt; Bool) -&gt; SimpleWhenMissing k x x
gr</pre>
gr
gr<pre>
grfilterMissing f = Merge.Lazy.mapMaybeMissing $ \k x -&gt; guard (f k x) *&gt; Just x
gr</pre>
gr
grbut this should be a little faster.
filterMissing :: Applicative f => (k -> x -> Bool) -> WhenMissing f k x x

-- | A tactic for dealing with keys present in one map but not the other in
gr<a>merge</a> or <a>mergeA</a>.
gr
grA tactic of type <tt> WhenMissing f k x z </tt> is an abstract
grrepresentation of a function of type <tt> k -&gt; x -&gt; f (Maybe z)
gr</tt>.
data WhenMissing f k x y

-- | A tactic for dealing with keys present in both maps in <a>merge</a> or
gr<a>mergeA</a>.
gr
grA tactic of type <tt> WhenMatched f k x y z </tt> is an abstract
grrepresentation of a function of type <tt> k -&gt; x -&gt; y -&gt; f
gr(Maybe z) </tt>.
data WhenMatched f k x y z

-- | An applicative version of <a>merge</a>.
gr
gr<tt>mergeA</tt> takes two <a>WhenMissing</a> tactics, a
gr<a>WhenMatched</a> tactic and two maps. It uses the tactics to merge
grthe maps. Its behavior is best understood via its fundamental tactics,
gr<a>traverseMaybeMissing</a> and <a>zipWithMaybeAMatched</a>.
gr
grConsider
gr
gr<pre>
grmergeA (traverseMaybeMissing g1)
gr              (traverseMaybeMissing g2)
gr              (zipWithMaybeAMatched f)
gr              m1 m2
gr</pre>
gr
grTake, for example,
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>), (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 = [(1, "one"), (2, "two"), (4, "three")]
gr</pre>
gr
gr<tt>mergeA</tt> will first '<tt>align'</tt> these maps by key:
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>),               (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 =           [(1, "one"), (2, "two"),          (4, "three")]
gr</pre>
gr
grIt will then pass the individual entries and pairs of entries to
gr<tt>g1</tt>, <tt>g2</tt>, or <tt>f</tt> as appropriate:
gr
gr<pre>
gractions = [g1 0 <tt>a</tt>, f 1 <tt>b</tt> "one", g2 2 "two", g1 3 <tt>c</tt>, f 4 <tt>d</tt> "three"]
gr</pre>
gr
grNext, it will perform the actions in the <tt>actions</tt> list in
grorder from left to right.
gr
gr<pre>
grkeys =     0        1          2           3        4
grresults = [Nothing, Just True, Just False, Nothing, Just True]
gr</pre>
gr
grFinally, the <tt>Just</tt> results are collected into a map:
gr
gr<pre>
grreturn value = [(1, True), (2, False), (4, True)]
gr</pre>
gr
grThe other tactics below are optimizations or simplifications of
gr<a>traverseMaybeMissing</a> for special cases. Most importantly,
gr
gr<ul>
gr<li><a>dropMissing</a> drops all the keys.</li>
gr<li><a>preserveMissing</a> leaves all the entries alone.</li>
gr<li><a>mapMaybeMissing</a> does not use the <a>Applicative</a>
grcontext.</li>
gr</ul>
gr
grWhen <a>mergeA</a> is given three arguments, it is inlined at the call
grsite. To prevent excessive inlining, you should generally only use
gr<a>mergeA</a> to define custom combining functions.
mergeA :: (Applicative f, Ord k) => WhenMissing f k a c -> WhenMissing f k b c -> WhenMatched f k a b c -> Map k a -> Map k b -> f (Map k c)

-- | When a key is found in both maps, apply a function to the key and
grvalues, perform the resulting action, and maybe use the result in the
grmerged map.
gr
grThis is the fundamental <a>WhenMatched</a> tactic.
zipWithMaybeAMatched :: Applicative f => (k -> x -> y -> f (Maybe z)) -> WhenMatched f k x y z

-- | When a key is found in both maps, apply a function to the key and
grvalues to produce an action and use its result in the merged map.
zipWithAMatched :: Applicative f => (k -> x -> y -> f z) -> WhenMatched f k x y z

-- | Traverse over the entries whose keys are missing from the other map,
groptionally producing values to put in the result. This is the most
grpowerful <a>WhenMissing</a> tactic, but others are usually more
grefficient.
traverseMaybeMissing :: Applicative f => (k -> x -> f (Maybe y)) -> WhenMissing f k x y

-- | Traverse over the entries whose keys are missing from the other map.
traverseMissing :: Applicative f => (k -> x -> f y) -> WhenMissing f k x y

-- | Filter the entries whose keys are missing from the other map using
grsome <a>Applicative</a> action.
gr
gr<pre>
grfilterAMissing f = Merge.Lazy.traverseMaybeMissing $
gr  k x -&gt; (b -&gt; guard b *&gt; Just x) <a>$</a> f k x
gr</pre>
gr
grbut this should be a little faster.
filterAMissing :: Applicative f => (k -> x -> f Bool) -> WhenMissing f k x x

-- | Map covariantly over a <tt><a>WhenMissing</a> f k x</tt>.
mapWhenMissing :: Functor f => (a -> b) -> WhenMissing f k x a -> WhenMissing f k x b

-- | Map covariantly over a <tt><a>WhenMatched</a> f k x y</tt>.
mapWhenMatched :: Functor f => (a -> b) -> WhenMatched f k x y a -> WhenMatched f k x y b

-- | Along with zipWithMaybeAMatched, witnesses the isomorphism between
gr<tt>WhenMatched f k x y z</tt> and <tt>k -&gt; x -&gt; y -&gt; f
gr(Maybe z)</tt>.
runWhenMatched :: WhenMatched f k x y z -> k -> x -> y -> f (Maybe z)

-- | Along with traverseMaybeMissing, witnesses the isomorphism between
gr<tt>WhenMissing f k x y</tt> and <tt>k -&gt; x -&gt; f (Maybe y)</tt>.
runWhenMissing :: WhenMissing f k x y -> k -> x -> f (Maybe y)


-- | <h1>Finite Maps (lazy interface)</h1>
gr
grThe <tt><a>Map</a> k v</tt> type represents a finite map (sometimes
grcalled a dictionary) from keys of type <tt>k</tt> to values of type
gr<tt>v</tt>. A <a>Map</a> is strict in its keys but lazy in its values.
gr
grThe functions in <a>Data.Map.Strict</a> are careful to force values
grbefore installing them in a <a>Map</a>. This is usually more efficient
grin cases where laziness is not essential. The functions in this module
grdo not do so.
gr
grWhen deciding if this is the correct data structure to use, consider:
gr
gr<ul>
gr<li>If you are using <tt>Int</tt> keys, you will get much better
grperformance for most operations using <a>Data.IntMap.Lazy</a>.</li>
gr<li>If you don't care about ordering, consider using
gr<tt>Data.HashMap.Lazy</tt> from the <a>unordered-containers</a>
grpackage instead.</li>
gr</ul>
gr
grFor a walkthrough of the most commonly used functions see the <a>maps
grintroduction</a>.
gr
grThis module is intended to be imported qualified, to avoid name
grclashes with Prelude functions:
gr
gr<pre>
grimport qualified Data.Map.Lazy as Map
gr</pre>
gr
grNote that the implementation is generally <i>left-biased</i>.
grFunctions that take two maps as arguments and combine them, such as
gr<a>union</a> and <a>intersection</a>, prefer the values in the first
grargument to those in the second.
gr
gr<h2>Detailed performance information</h2>
gr
grThe amortized running time is given for each operation, with <i>n</i>
grreferring to the number of entries in the map.
gr
grBenchmarks comparing <a>Data.Map.Lazy</a> with other dictionary
grimplementations can be found at
gr<a>https://github.com/haskell-perf/dictionaries</a>.
gr
gr<h2>Warning</h2>
gr
grThe size of a <a>Map</a> must not exceed <tt>maxBound::Int</tt>.
grViolation of this condition is not detected and if the size limit is
grexceeded, its behaviour is undefined.
gr
gr<h2>Implementation</h2>
gr
grThe implementation of <a>Map</a> is based on <i>size balanced</i>
grbinary trees (or trees of <i>bounded balance</i>) as described by:
gr
gr<ul>
gr<li>Stephen Adams, "<i>Efficient sets: a balancing act</i>", Journal
grof Functional Programming 3(4):553-562, October 1993,
gr<a>http://www.swiss.ai.mit.edu/~adams/BB/</a>.</li>
gr<li>J. Nievergelt and E.M. Reingold, "<i>Binary search trees of
grbounded balance</i>", SIAM journal of computing 2(1), March 1973.</li>
gr</ul>
gr
grBounds for <a>union</a>, <a>intersection</a>, and <a>difference</a>
grare as given by
gr
gr<ul>
gr<li>Guy Blelloch, Daniel Ferizovic, and Yihan Sun, "<i>Just Join for
grParallel Ordered Sets</i>",
gr<a>https://arxiv.org/abs/1602.02120v3</a>.</li>
gr</ul>
module Data.Map.Lazy

-- | A Map from keys <tt>k</tt> to values <tt>a</tt>.
data Map k a

-- | <i>O(log n)</i>. Find the value at a key. Calls <a>error</a> when the
grelement can not be found.
gr
gr<pre>
grfromList [(5,'a'), (3,'b')] ! 1    Error: element not in the map
grfromList [(5,'a'), (3,'b')] ! 5 == 'a'
gr</pre>
(!) :: Ord k => Map k a -> k -> a
infixl 9 !

-- | <i>O(log n)</i>. Find the value at a key. Returns <a>Nothing</a> when
grthe element can not be found.
gr
gr<pre>
grfromList [(5, 'a'), (3, 'b')] !? 1 == Nothing
gr</pre>
gr
gr<pre>
grfromList [(5, 'a'), (3, 'b')] !? 5 == Just 'a'
gr</pre>
(!?) :: Ord k => Map k a -> k -> Maybe a
infixl 9 !?

-- | Same as <a>difference</a>.
(\\) :: Ord k => Map k a -> Map k b -> Map k a
infixl 9 \\

-- | <i>O(1)</i>. Is the map empty?
gr
gr<pre>
grData.Map.null (empty)           == True
grData.Map.null (singleton 1 'a') == False
gr</pre>
null :: Map k a -> Bool

-- | <i>O(1)</i>. The number of elements in the map.
gr
gr<pre>
grsize empty                                   == 0
grsize (singleton 1 'a')                       == 1
grsize (fromList([(1,'a'), (2,'c'), (3,'b')])) == 3
gr</pre>
size :: Map k a -> Int

-- | <i>O(log n)</i>. Is the key a member of the map? See also
gr<a>notMember</a>.
gr
gr<pre>
grmember 5 (fromList [(5,'a'), (3,'b')]) == True
grmember 1 (fromList [(5,'a'), (3,'b')]) == False
gr</pre>
member :: Ord k => k -> Map k a -> Bool

-- | <i>O(log n)</i>. Is the key not a member of the map? See also
gr<a>member</a>.
gr
gr<pre>
grnotMember 5 (fromList [(5,'a'), (3,'b')]) == False
grnotMember 1 (fromList [(5,'a'), (3,'b')]) == True
gr</pre>
notMember :: Ord k => k -> Map k a -> Bool

-- | <i>O(log n)</i>. Lookup the value at a key in the map.
gr
grThe function will return the corresponding value as <tt>(<a>Just</a>
grvalue)</tt>, or <a>Nothing</a> if the key isn't in the map.
gr
grAn example of using <tt>lookup</tt>:
gr
gr<pre>
grimport Prelude hiding (lookup)
grimport Data.Map
gr
gremployeeDept = fromList([("John","Sales"), ("Bob","IT")])
grdeptCountry = fromList([("IT","USA"), ("Sales","France")])
grcountryCurrency = fromList([("USA", "Dollar"), ("France", "Euro")])
gr
gremployeeCurrency :: String -&gt; Maybe String
gremployeeCurrency name = do
gr    dept &lt;- lookup name employeeDept
gr    country &lt;- lookup dept deptCountry
gr    lookup country countryCurrency
gr
grmain = do
gr    putStrLn $ "John's currency: " ++ (show (employeeCurrency "John"))
gr    putStrLn $ "Pete's currency: " ++ (show (employeeCurrency "Pete"))
gr</pre>
gr
grThe output of this program:
gr
gr<pre>
grJohn's currency: Just "Euro"
grPete's currency: Nothing
gr</pre>
lookup :: Ord k => k -> Map k a -> Maybe a

-- | <i>O(log n)</i>. The expression <tt>(<a>findWithDefault</a> def k
grmap)</tt> returns the value at key <tt>k</tt> or returns default value
gr<tt>def</tt> when the key is not in the map.
gr
gr<pre>
grfindWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x'
grfindWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a'
gr</pre>
findWithDefault :: Ord k => a -> k -> Map k a -> a

-- | <i>O(log n)</i>. Find largest key smaller than the given one and
grreturn the corresponding (key, value) pair.
gr
gr<pre>
grlookupLT 3 (fromList [(3,'a'), (5,'b')]) == Nothing
grlookupLT 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
gr</pre>
lookupLT :: Ord k => k -> Map k v -> Maybe (k, v)

-- | <i>O(log n)</i>. Find smallest key greater than the given one and
grreturn the corresponding (key, value) pair.
gr
gr<pre>
grlookupGT 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
grlookupGT 5 (fromList [(3,'a'), (5,'b')]) == Nothing
gr</pre>
lookupGT :: Ord k => k -> Map k v -> Maybe (k, v)

-- | <i>O(log n)</i>. Find largest key smaller or equal to the given one
grand return the corresponding (key, value) pair.
gr
gr<pre>
grlookupLE 2 (fromList [(3,'a'), (5,'b')]) == Nothing
grlookupLE 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
grlookupLE 5 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
gr</pre>
lookupLE :: Ord k => k -> Map k v -> Maybe (k, v)

-- | <i>O(log n)</i>. Find smallest key greater or equal to the given one
grand return the corresponding (key, value) pair.
gr
gr<pre>
grlookupGE 3 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
grlookupGE 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
grlookupGE 6 (fromList [(3,'a'), (5,'b')]) == Nothing
gr</pre>
lookupGE :: Ord k => k -> Map k v -> Maybe (k, v)

-- | <i>O(1)</i>. The empty map.
gr
gr<pre>
grempty      == fromList []
grsize empty == 0
gr</pre>
empty :: Map k a

-- | <i>O(1)</i>. A map with a single element.
gr
gr<pre>
grsingleton 1 'a'        == fromList [(1, 'a')]
grsize (singleton 1 'a') == 1
gr</pre>
singleton :: k -> a -> Map k a

-- | <i>O(log n)</i>. Insert a new key and value in the map. If the key is
gralready present in the map, the associated value is replaced with the
grsupplied value. <a>insert</a> is equivalent to <tt><a>insertWith</a>
gr<a>const</a></tt>.
gr
gr<pre>
grinsert 5 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'x')]
grinsert 7 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'a'), (7, 'x')]
grinsert 5 'x' empty                         == singleton 5 'x'
gr</pre>
insert :: Ord k => k -> a -> Map k a -> Map k a

-- | <i>O(log n)</i>. Insert with a function, combining new value and old
grvalue. <tt><a>insertWith</a> f key value mp</tt> will insert the pair
gr(key, value) into <tt>mp</tt> if key does not exist in the map. If the
grkey does exist, the function will insert the pair <tt>(key, f
grnew_value old_value)</tt>.
gr
gr<pre>
grinsertWith (++) 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "xxxa")]
grinsertWith (++) 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
grinsertWith (++) 5 "xxx" empty                         == singleton 5 "xxx"
gr</pre>
insertWith :: Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a

-- | <i>O(log n)</i>. Insert with a function, combining key, new value and
grold value. <tt><a>insertWithKey</a> f key value mp</tt> will insert
grthe pair (key, value) into <tt>mp</tt> if key does not exist in the
grmap. If the key does exist, the function will insert the pair
gr<tt>(key,f key new_value old_value)</tt>. Note that the key passed to
grf is the same key passed to <a>insertWithKey</a>.
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grinsertWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:xxx|a")]
grinsertWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
grinsertWithKey f 5 "xxx" empty                         == singleton 5 "xxx"
gr</pre>
insertWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a

-- | <i>O(log n)</i>. Combines insert operation with old value retrieval.
grThe expression (<tt><a>insertLookupWithKey</a> f k x map</tt>) is a
grpair where the first element is equal to (<tt><a>lookup</a> k
grmap</tt>) and the second element equal to (<tt><a>insertWithKey</a> f
grk x map</tt>).
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grinsertLookupWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:xxx|a")])
grinsertLookupWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a"), (7, "xxx")])
grinsertLookupWithKey f 5 "xxx" empty                         == (Nothing,  singleton 5 "xxx")
gr</pre>
gr
grThis is how to define <tt>insertLookup</tt> using
gr<tt>insertLookupWithKey</tt>:
gr
gr<pre>
grlet insertLookup kx x t = insertLookupWithKey (\_ a _ -&gt; a) kx x t
grinsertLookup 5 "x" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "x")])
grinsertLookup 7 "x" (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a"), (7, "x")])
gr</pre>
insertLookupWithKey :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a, Map k a)

-- | <i>O(log n)</i>. Delete a key and its value from the map. When the key
gris not a member of the map, the original map is returned.
gr
gr<pre>
grdelete 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grdelete 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grdelete 5 empty                         == empty
gr</pre>
delete :: Ord k => k -> Map k a -> Map k a

-- | <i>O(log n)</i>. Update a value at a specific key with the result of
grthe provided function. When the key is not a member of the map, the
groriginal map is returned.
gr
gr<pre>
gradjust ("new " ++) 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
gradjust ("new " ++) 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gradjust ("new " ++) 7 empty                         == empty
gr</pre>
adjust :: Ord k => (a -> a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. Adjust a value at a specific key. When the key is not
gra member of the map, the original map is returned.
gr
gr<pre>
grlet f key x = (show key) ++ ":new " ++ x
gradjustWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
gradjustWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gradjustWithKey f 7 empty                         == empty
gr</pre>
adjustWithKey :: Ord k => (k -> a -> a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. The expression (<tt><a>update</a> f k map</tt>)
grupdates the value <tt>x</tt> at <tt>k</tt> (if it is in the map). If
gr(<tt>f x</tt>) is <a>Nothing</a>, the element is deleted. If it is
gr(<tt><a>Just</a> y</tt>), the key <tt>k</tt> is bound to the new value
gr<tt>y</tt>.
gr
gr<pre>
grlet f x = if x == "a" then Just "new a" else Nothing
grupdate f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
grupdate f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grupdate f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
update :: Ord k => (a -> Maybe a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. The expression (<tt><a>updateWithKey</a> f k
grmap</tt>) updates the value <tt>x</tt> at <tt>k</tt> (if it is in the
grmap). If (<tt>f k x</tt>) is <a>Nothing</a>, the element is deleted.
grIf it is (<tt><a>Just</a> y</tt>), the key <tt>k</tt> is bound to the
grnew value <tt>y</tt>.
gr
gr<pre>
grlet f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
grupdateWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
grupdateWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grupdateWithKey f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. Lookup and update. See also <a>updateWithKey</a>. The
grfunction returns changed value, if it is updated. Returns the original
grkey value if the map entry is deleted.
gr
gr<pre>
grlet f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
grupdateLookupWithKey f 5 (fromList [(5,"a"), (3,"b")]) == (Just "5:new a", fromList [(3, "b"), (5, "5:new a")])
grupdateLookupWithKey f 7 (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a")])
grupdateLookupWithKey f 3 (fromList [(5,"a"), (3,"b")]) == (Just "b", singleton 5 "a")
gr</pre>
updateLookupWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a -> (Maybe a, Map k a)

-- | <i>O(log n)</i>. The expression (<tt><a>alter</a> f k map</tt>) alters
grthe value <tt>x</tt> at <tt>k</tt>, or absence thereof. <a>alter</a>
grcan be used to insert, delete, or update a value in a <a>Map</a>. In
grshort : <tt><a>lookup</a> k (<a>alter</a> f k m) = f (<a>lookup</a> k
grm)</tt>.
gr
gr<pre>
grlet f _ = Nothing
gralter f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gralter f 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr
grlet f _ = Just "c"
gralter f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "c")]
gralter f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "c")]
gr</pre>
alter :: Ord k => (Maybe a -> Maybe a) -> k -> Map k a -> Map k a

-- | <i>O(log n)</i>. The expression (<tt><a>alterF</a> f k map</tt>)
gralters the value <tt>x</tt> at <tt>k</tt>, or absence thereof.
gr<a>alterF</a> can be used to inspect, insert, delete, or update a
grvalue in a <a>Map</a>. In short: <tt><a>lookup</a> k &lt;$&gt;
gr<a>alterF</a> f k m = f (<a>lookup</a> k m)</tt>.
gr
grExample:
gr
gr<pre>
grinteractiveAlter :: Int -&gt; Map Int String -&gt; IO (Map Int String)
grinteractiveAlter k m = alterF f k m where
gr  f Nothing -&gt; do
gr     putStrLn $ show k ++
gr         " was not found in the map. Would you like to add it?"
gr     getUserResponse1 :: IO (Maybe String)
gr  f (Just old) -&gt; do
gr     putStrLn "The key is currently bound to " ++ show old ++
gr         ". Would you like to change or delete it?"
gr     getUserresponse2 :: IO (Maybe String)
gr</pre>
gr
gr<a>alterF</a> is the most general operation for working with an
grindividual key that may or may not be in a given map. When used with
grtrivial functors like <a>Identity</a> and <a>Const</a>, it is often
grslightly slower than more specialized combinators like <a>lookup</a>
grand <a>insert</a>. However, when the functor is non-trivial and key
grcomparison is not particularly cheap, it is the fastest way.
gr
grNote on rewrite rules:
gr
grThis module includes GHC rewrite rules to optimize <a>alterF</a> for
grthe <a>Const</a> and <a>Identity</a> functors. In general, these rules
grimprove performance. The sole exception is that when using
gr<a>Identity</a>, deleting a key that is already absent takes longer
grthan it would without the rules. If you expect this to occur a very
grlarge fraction of the time, you might consider using a private copy of
grthe <a>Identity</a> type.
gr
grNote: <a>alterF</a> is a flipped version of the <tt>at</tt> combinator
grfrom <a>At</a>.
alterF :: (Functor f, Ord k) => (Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. The expression (<tt><a>union</a>
grt1 t2</tt>) takes the left-biased union of <tt>t1</tt> and
gr<tt>t2</tt>. It prefers <tt>t1</tt> when duplicate keys are
grencountered, i.e. (<tt><a>union</a> == <a>unionWith</a>
gr<a>const</a></tt>).
gr
gr<pre>
grunion (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "a"), (7, "C")]
gr</pre>
union :: Ord k => Map k a -> Map k a -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Union with a combining function.
gr
gr<pre>
grunionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "aA"), (7, "C")]
gr</pre>
unionWith :: Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Union with a combining function.
gr
gr<pre>
grlet f key left_value right_value = (show key) ++ ":" ++ left_value ++ "|" ++ right_value
grunionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "5:a|A"), (7, "C")]
gr</pre>
unionWithKey :: Ord k => (k -> a -> a -> a) -> Map k a -> Map k a -> Map k a

-- | The union of a list of maps: (<tt><a>unions</a> == <a>foldl</a>
gr<a>union</a> <a>empty</a></tt>).
gr
gr<pre>
grunions [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
gr    == fromList [(3, "b"), (5, "a"), (7, "C")]
grunions [(fromList [(5, "A3"), (3, "B3")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "a"), (3, "b")])]
gr    == fromList [(3, "B3"), (5, "A3"), (7, "C")]
gr</pre>
unions :: Ord k => [Map k a] -> Map k a

-- | The union of a list of maps, with a combining operation:
gr(<tt><a>unionsWith</a> f == <a>foldl</a> (<a>unionWith</a> f)
gr<a>empty</a></tt>).
gr
gr<pre>
grunionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
gr    == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")]
gr</pre>
unionsWith :: Ord k => (a -> a -> a) -> [Map k a] -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Difference of two maps. Return
grelements of the first map not existing in the second map.
gr
gr<pre>
grdifference (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 3 "b"
gr</pre>
difference :: Ord k => Map k a -> Map k b -> Map k a

-- | <i>O(n+m)</i>. Difference with a combining function. When two equal
grkeys are encountered, the combining function is applied to the values
grof these keys. If it returns <a>Nothing</a>, the element is discarded
gr(proper set difference). If it returns (<tt><a>Just</a> y</tt>), the
grelement is updated with a new value <tt>y</tt>.
gr
gr<pre>
grlet f al ar = if al == "b" then Just (al ++ ":" ++ ar) else Nothing
grdifferenceWith f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (7, "C")])
gr    == singleton 3 "b:B"
gr</pre>
differenceWith :: Ord k => (a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a

-- | <i>O(n+m)</i>. Difference with a combining function. When two equal
grkeys are encountered, the combining function is applied to the key and
grboth values. If it returns <a>Nothing</a>, the element is discarded
gr(proper set difference). If it returns (<tt><a>Just</a> y</tt>), the
grelement is updated with a new value <tt>y</tt>.
gr
gr<pre>
grlet f k al ar = if al == "b" then Just ((show k) ++ ":" ++ al ++ "|" ++ ar) else Nothing
grdifferenceWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (10, "C")])
gr    == singleton 3 "3:b|B"
gr</pre>
differenceWithKey :: Ord k => (k -> a -> b -> Maybe a) -> Map k a -> Map k b -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Intersection of two maps. Return
grdata in the first map for the keys existing in both maps.
gr(<tt><a>intersection</a> m1 m2 == <a>intersectionWith</a> <a>const</a>
grm1 m2</tt>).
gr
gr<pre>
grintersection (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "a"
gr</pre>
intersection :: Ord k => Map k a -> Map k b -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Intersection with a combining
grfunction.
gr
gr<pre>
grintersectionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "aA"
gr</pre>
intersectionWith :: Ord k => (a -> b -> c) -> Map k a -> Map k b -> Map k c

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Intersection with a combining
grfunction.
gr
gr<pre>
grlet f k al ar = (show k) ++ ":" ++ al ++ "|" ++ ar
grintersectionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "5:a|A"
gr</pre>
intersectionWithKey :: Ord k => (k -> a -> b -> c) -> Map k a -> Map k b -> Map k c

-- | <i>O(n+m)</i>. An unsafe general combining function.
gr
grWARNING: This function can produce corrupt maps and its results may
grdepend on the internal structures of its inputs. Users should prefer
gr<a>merge</a> or <a>mergeA</a>.
gr
grWhen <a>mergeWithKey</a> is given three arguments, it is inlined to
grthe call site. You should therefore use <a>mergeWithKey</a> only to
grdefine custom combining functions. For example, you could define
gr<a>unionWithKey</a>, <a>differenceWithKey</a> and
gr<a>intersectionWithKey</a> as
gr
gr<pre>
grmyUnionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -&gt; Just (f k x1 x2)) id id m1 m2
grmyDifferenceWithKey f m1 m2 = mergeWithKey f id (const empty) m1 m2
grmyIntersectionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -&gt; Just (f k x1 x2)) (const empty) (const empty) m1 m2
gr</pre>
gr
grWhen calling <tt><a>mergeWithKey</a> combine only1 only2</tt>, a
grfunction combining two <a>Map</a>s is created, such that
gr
gr<ul>
gr<li>if a key is present in both maps, it is passed with both
grcorresponding values to the <tt>combine</tt> function. Depending on
grthe result, the key is either present in the result with specified
grvalue, or is left out;</li>
gr<li>a nonempty subtree present only in the first map is passed to
gr<tt>only1</tt> and the output is added to the result;</li>
gr<li>a nonempty subtree present only in the second map is passed to
gr<tt>only2</tt> and the output is added to the result.</li>
gr</ul>
gr
grThe <tt>only1</tt> and <tt>only2</tt> methods <i>must return a map
grwith a subset (possibly empty) of the keys of the given map</i>. The
grvalues can be modified arbitrarily. Most common variants of
gr<tt>only1</tt> and <tt>only2</tt> are <a>id</a> and <tt><a>const</a>
gr<a>empty</a></tt>, but for example <tt><a>map</a> f</tt>,
gr<tt><a>filterWithKey</a> f</tt>, or <tt><a>mapMaybeWithKey</a> f</tt>
grcould be used for any <tt>f</tt>.
mergeWithKey :: Ord k => (k -> a -> b -> Maybe c) -> (Map k a -> Map k c) -> (Map k b -> Map k c) -> Map k a -> Map k b -> Map k c

-- | <i>O(n)</i>. Map a function over all values in the map.
gr
gr<pre>
grmap (++ "x") (fromList [(5,"a"), (3,"b")]) == fromList [(3, "bx"), (5, "ax")]
gr</pre>
map :: (a -> b) -> Map k a -> Map k b

-- | <i>O(n)</i>. Map a function over all values in the map.
gr
gr<pre>
grlet f key x = (show key) ++ ":" ++ x
grmapWithKey f (fromList [(5,"a"), (3,"b")]) == fromList [(3, "3:b"), (5, "5:a")]
gr</pre>
mapWithKey :: (k -> a -> b) -> Map k a -> Map k b

-- | <i>O(n)</i>. <tt><a>traverseWithKey</a> f m == <a>fromList</a>
gr<a>$</a> <a>traverse</a> ((k, v) -&gt; (,) k <a>$</a> f k v)
gr(<a>toList</a> m)</tt> That is, behaves exactly like a regular
gr<a>traverse</a> except that the traversing function also has access to
grthe key associated with a value.
gr
gr<pre>
grtraverseWithKey (\k v -&gt; if odd k then Just (succ v) else Nothing) (fromList [(1, 'a'), (5, 'e')]) == Just (fromList [(1, 'b'), (5, 'f')])
grtraverseWithKey (\k v -&gt; if odd k then Just (succ v) else Nothing) (fromList [(2, 'c')])           == Nothing
gr</pre>
traverseWithKey :: Applicative t => (k -> a -> t b) -> Map k a -> t (Map k b)

-- | <i>O(n)</i>. Traverse keys/values and collect the <a>Just</a> results.
traverseMaybeWithKey :: Applicative f => (k -> a -> f (Maybe b)) -> Map k a -> f (Map k b)

-- | <i>O(n)</i>. The function <a>mapAccum</a> threads an accumulating
grargument through the map in ascending order of keys.
gr
gr<pre>
grlet f a b = (a ++ b, b ++ "X")
grmapAccum f "Everything: " (fromList [(5,"a"), (3,"b")]) == ("Everything: ba", fromList [(3, "bX"), (5, "aX")])
gr</pre>
mapAccum :: (a -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)

-- | <i>O(n)</i>. The function <a>mapAccumWithKey</a> threads an
graccumulating argument through the map in ascending order of keys.
gr
gr<pre>
grlet f a k b = (a ++ " " ++ (show k) ++ "-" ++ b, b ++ "X")
grmapAccumWithKey f "Everything:" (fromList [(5,"a"), (3,"b")]) == ("Everything: 3-b 5-a", fromList [(3, "bX"), (5, "aX")])
gr</pre>
mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)

-- | <i>O(n)</i>. The function <tt>mapAccumR</tt> threads an accumulating
grargument through the map in descending order of keys.
mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> Map k b -> (a, Map k c)

-- | <i>O(n*log n)</i>. <tt><a>mapKeys</a> f s</tt> is the map obtained by
grapplying <tt>f</tt> to each key of <tt>s</tt>.
gr
grThe size of the result may be smaller if <tt>f</tt> maps two or more
grdistinct keys to the same new key. In this case the value at the
grgreatest of the original keys is retained.
gr
gr<pre>
grmapKeys (+ 1) (fromList [(5,"a"), (3,"b")])                        == fromList [(4, "b"), (6, "a")]
grmapKeys (\ _ -&gt; 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "c"
grmapKeys (\ _ -&gt; 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "c"
gr</pre>
mapKeys :: Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 a

-- | <i>O(n*log n)</i>. <tt><a>mapKeysWith</a> c f s</tt> is the map
grobtained by applying <tt>f</tt> to each key of <tt>s</tt>.
gr
grThe size of the result may be smaller if <tt>f</tt> maps two or more
grdistinct keys to the same new key. In this case the associated values
grwill be combined using <tt>c</tt>. The value at the greater of the two
groriginal keys is used as the first argument to <tt>c</tt>.
gr
gr<pre>
grmapKeysWith (++) (\ _ -&gt; 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "cdab"
grmapKeysWith (++) (\ _ -&gt; 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "cdab"
gr</pre>
mapKeysWith :: Ord k2 => (a -> a -> a) -> (k1 -> k2) -> Map k1 a -> Map k2 a

-- | <i>O(n)</i>. <tt><a>mapKeysMonotonic</a> f s == <a>mapKeys</a> f
grs</tt>, but works only when <tt>f</tt> is strictly monotonic. That is,
grfor any values <tt>x</tt> and <tt>y</tt>, if <tt>x</tt> &lt;
gr<tt>y</tt> then <tt>f x</tt> &lt; <tt>f y</tt>. <i>The precondition is
grnot checked.</i> Semi-formally, we have:
gr
gr<pre>
grand [x &lt; y ==&gt; f x &lt; f y | x &lt;- ls, y &lt;- ls]
gr                    ==&gt; mapKeysMonotonic f s == mapKeys f s
gr    where ls = keys s
gr</pre>
gr
grThis means that <tt>f</tt> maps distinct original keys to distinct
grresulting keys. This function has better performance than
gr<a>mapKeys</a>.
gr
gr<pre>
grmapKeysMonotonic (\ k -&gt; k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")]
grvalid (mapKeysMonotonic (\ k -&gt; k * 2) (fromList [(5,"a"), (3,"b")])) == True
grvalid (mapKeysMonotonic (\ _ -&gt; 1)     (fromList [(5,"a"), (3,"b")])) == False
gr</pre>
mapKeysMonotonic :: (k1 -> k2) -> Map k1 a -> Map k2 a

-- | <i>O(n)</i>. Fold the values in the map using the given
grright-associative binary operator, such that <tt><a>foldr</a> f z ==
gr<a>foldr</a> f z . <a>elems</a></tt>.
gr
grFor example,
gr
gr<pre>
grelems map = foldr (:) [] map
gr</pre>
gr
gr<pre>
grlet f a len = len + (length a)
grfoldr f 0 (fromList [(5,"a"), (3,"bbb")]) == 4
gr</pre>
foldr :: (a -> b -> b) -> b -> Map k a -> b

-- | <i>O(n)</i>. Fold the values in the map using the given
grleft-associative binary operator, such that <tt><a>foldl</a> f z ==
gr<a>foldl</a> f z . <a>elems</a></tt>.
gr
grFor example,
gr
gr<pre>
grelems = reverse . foldl (flip (:)) []
gr</pre>
gr
gr<pre>
grlet f len a = len + (length a)
grfoldl f 0 (fromList [(5,"a"), (3,"bbb")]) == 4
gr</pre>
foldl :: (a -> b -> a) -> a -> Map k b -> a

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grright-associative binary operator, such that <tt><a>foldrWithKey</a> f
grz == <a>foldr</a> (<a>uncurry</a> f) z . <a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grkeys map = foldrWithKey (\k x ks -&gt; k:ks) [] map
gr</pre>
gr
gr<pre>
grlet f k a result = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")"
grfoldrWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (5:a)(3:b)"
gr</pre>
foldrWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grleft-associative binary operator, such that <tt><a>foldlWithKey</a> f
grz == <a>foldl</a> (\z' (kx, x) -&gt; f z' kx x) z .
gr<a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grkeys = reverse . foldlWithKey (\ks k x -&gt; k:ks) []
gr</pre>
gr
gr<pre>
grlet f result k a = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")"
grfoldlWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (3:b)(5:a)"
gr</pre>
foldlWithKey :: (a -> k -> b -> a) -> a -> Map k b -> a

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grmonoid, such that
gr
gr<pre>
gr<a>foldMapWithKey</a> f = <a>fold</a> . <a>mapWithKey</a> f
gr</pre>
gr
grThis can be an asymptotically faster than <a>foldrWithKey</a> or
gr<a>foldlWithKey</a> for some monoids.
foldMapWithKey :: Monoid m => (k -> a -> m) -> Map k a -> m

-- | <i>O(n)</i>. A strict version of <a>foldr</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldr' :: (a -> b -> b) -> b -> Map k a -> b

-- | <i>O(n)</i>. A strict version of <a>foldl</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldl' :: (a -> b -> a) -> a -> Map k b -> a

-- | <i>O(n)</i>. A strict version of <a>foldrWithKey</a>. Each application
grof the operator is evaluated before using the result in the next
grapplication. This function is strict in the starting value.
foldrWithKey' :: (k -> a -> b -> b) -> b -> Map k a -> b

-- | <i>O(n)</i>. A strict version of <a>foldlWithKey</a>. Each application
grof the operator is evaluated before using the result in the next
grapplication. This function is strict in the starting value.
foldlWithKey' :: (a -> k -> b -> a) -> a -> Map k b -> a

-- | <i>O(n)</i>. Return all elements of the map in the ascending order of
grtheir keys. Subject to list fusion.
gr
gr<pre>
grelems (fromList [(5,"a"), (3,"b")]) == ["b","a"]
grelems empty == []
gr</pre>
elems :: Map k a -> [a]

-- | <i>O(n)</i>. Return all keys of the map in ascending order. Subject to
grlist fusion.
gr
gr<pre>
grkeys (fromList [(5,"a"), (3,"b")]) == [3,5]
grkeys empty == []
gr</pre>
keys :: Map k a -> [k]

-- | <i>O(n)</i>. An alias for <a>toAscList</a>. Return all key/value pairs
grin the map in ascending key order. Subject to list fusion.
gr
gr<pre>
grassocs (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
grassocs empty == []
gr</pre>
assocs :: Map k a -> [(k, a)]

-- | <i>O(n)</i>. The set of all keys of the map.
gr
gr<pre>
grkeysSet (fromList [(5,"a"), (3,"b")]) == Data.Set.fromList [3,5]
grkeysSet empty == Data.Set.empty
gr</pre>
keysSet :: Map k a -> Set k

-- | <i>O(n)</i>. Build a map from a set of keys and a function which for
greach key computes its value.
gr
gr<pre>
grfromSet (\k -&gt; replicate k 'a') (Data.Set.fromList [3, 5]) == fromList [(5,"aaaaa"), (3,"aaa")]
grfromSet undefined Data.Set.empty == empty
gr</pre>
fromSet :: (k -> a) -> Set k -> Map k a

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs. Subject to
grlist fusion.
gr
gr<pre>
grtoList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
grtoList empty == []
gr</pre>
toList :: Map k a -> [(k, a)]

-- | <i>O(n*log n)</i>. Build a map from a list of key/value pairs. See
gralso <a>fromAscList</a>. If the list contains more than one value for
grthe same key, the last value for the key is retained.
gr
grIf the keys of the list are ordered, linear-time implementation is
grused, with the performance equal to <a>fromDistinctAscList</a>.
gr
gr<pre>
grfromList [] == empty
grfromList [(5,"a"), (3,"b"), (5, "c")] == fromList [(5,"c"), (3,"b")]
grfromList [(5,"c"), (3,"b"), (5, "a")] == fromList [(5,"a"), (3,"b")]
gr</pre>
fromList :: Ord k => [(k, a)] -> Map k a

-- | <i>O(n*log n)</i>. Build a map from a list of key/value pairs with a
grcombining function. See also <a>fromAscListWith</a>.
gr
gr<pre>
grfromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")]
grfromListWith (++) [] == empty
gr</pre>
fromListWith :: Ord k => (a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n*log n)</i>. Build a map from a list of key/value pairs with a
grcombining function. See also <a>fromAscListWithKey</a>.
gr
gr<pre>
grlet f k a1 a2 = (show k) ++ a1 ++ a2
grfromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "3ab"), (5, "5a5ba")]
grfromListWithKey f [] == empty
gr</pre>
fromListWithKey :: Ord k => (k -> a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs where the
grkeys are in ascending order. Subject to list fusion.
gr
gr<pre>
grtoAscList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
gr</pre>
toAscList :: Map k a -> [(k, a)]

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs where the
grkeys are in descending order. Subject to list fusion.
gr
gr<pre>
grtoDescList (fromList [(5,"a"), (3,"b")]) == [(5,"a"), (3,"b")]
gr</pre>
toDescList :: Map k a -> [(k, a)]

-- | <i>O(n)</i>. Build a map from an ascending list in linear time. <i>The
grprecondition (input list is ascending) is not checked.</i>
gr
gr<pre>
grfromAscList [(3,"b"), (5,"a")]          == fromList [(3, "b"), (5, "a")]
grfromAscList [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "b")]
grvalid (fromAscList [(3,"b"), (5,"a"), (5,"b")]) == True
grvalid (fromAscList [(5,"a"), (3,"b"), (5,"b")]) == False
gr</pre>
fromAscList :: Eq k => [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from an ascending list in linear time with a
grcombining function for equal keys. <i>The precondition (input list is
grascending) is not checked.</i>
gr
gr<pre>
grfromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")]
grvalid (fromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")]) == True
grvalid (fromAscListWith (++) [(5,"a"), (3,"b"), (5,"b")]) == False
gr</pre>
fromAscListWith :: Eq k => (a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from an ascending list in linear time with a
grcombining function for equal keys. <i>The precondition (input list is
grascending) is not checked.</i>
gr
gr<pre>
grlet f k a1 a2 = (show k) ++ ":" ++ a1 ++ a2
grfromAscListWithKey f [(3,"b"), (5,"a"), (5,"b"), (5,"b")] == fromList [(3, "b"), (5, "5:b5:ba")]
grvalid (fromAscListWithKey f [(3,"b"), (5,"a"), (5,"b"), (5,"b")]) == True
grvalid (fromAscListWithKey f [(5,"a"), (3,"b"), (5,"b"), (5,"b")]) == False
gr</pre>
fromAscListWithKey :: Eq k => (k -> a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from an ascending list of distinct elements
grin linear time. <i>The precondition is not checked.</i>
gr
gr<pre>
grfromDistinctAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")]
grvalid (fromDistinctAscList [(3,"b"), (5,"a")])          == True
grvalid (fromDistinctAscList [(3,"b"), (5,"a"), (5,"b")]) == False
gr</pre>
fromDistinctAscList :: [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from a descending list in linear time. <i>The
grprecondition (input list is descending) is not checked.</i>
gr
gr<pre>
grfromDescList [(5,"a"), (3,"b")]          == fromList [(3, "b"), (5, "a")]
grfromDescList [(5,"a"), (5,"b"), (3,"b")] == fromList [(3, "b"), (5, "b")]
grvalid (fromDescList [(5,"a"), (5,"b"), (3,"b")]) == True
grvalid (fromDescList [(5,"a"), (3,"b"), (5,"b")]) == False
gr</pre>
fromDescList :: Eq k => [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from a descending list in linear time with a
grcombining function for equal keys. <i>The precondition (input list is
grdescending) is not checked.</i>
gr
gr<pre>
grfromDescListWith (++) [(5,"a"), (5,"b"), (3,"b")] == fromList [(3, "b"), (5, "ba")]
grvalid (fromDescListWith (++) [(5,"a"), (5,"b"), (3,"b")]) == True
grvalid (fromDescListWith (++) [(5,"a"), (3,"b"), (5,"b")]) == False
gr</pre>
fromDescListWith :: Eq k => (a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from a descending list in linear time with a
grcombining function for equal keys. <i>The precondition (input list is
grdescending) is not checked.</i>
gr
gr<pre>
grlet f k a1 a2 = (show k) ++ ":" ++ a1 ++ a2
grfromDescListWithKey f [(5,"a"), (5,"b"), (5,"b"), (3,"b")] == fromList [(3, "b"), (5, "5:b5:ba")]
grvalid (fromDescListWithKey f [(5,"a"), (5,"b"), (5,"b"), (3,"b")]) == True
grvalid (fromDescListWithKey f [(5,"a"), (3,"b"), (5,"b"), (5,"b")]) == False
gr</pre>
fromDescListWithKey :: Eq k => (k -> a -> a -> a) -> [(k, a)] -> Map k a

-- | <i>O(n)</i>. Build a map from a descending list of distinct elements
grin linear time. <i>The precondition is not checked.</i>
gr
gr<pre>
grfromDistinctDescList [(5,"a"), (3,"b")] == fromList [(3, "b"), (5, "a")]
grvalid (fromDistinctDescList [(5,"a"), (3,"b")])          == True
grvalid (fromDistinctDescList [(5,"a"), (5,"b"), (3,"b")]) == False
gr</pre>
fromDistinctDescList :: [(k, a)] -> Map k a

-- | <i>O(n)</i>. Filter all values that satisfy the predicate.
gr
gr<pre>
grfilter (&gt; "a") (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grfilter (&gt; "x") (fromList [(5,"a"), (3,"b")]) == empty
grfilter (&lt; "a") (fromList [(5,"a"), (3,"b")]) == empty
gr</pre>
filter :: (a -> Bool) -> Map k a -> Map k a

-- | <i>O(n)</i>. Filter all keys/values that satisfy the predicate.
gr
gr<pre>
grfilterWithKey (\k _ -&gt; k &gt; 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
filterWithKey :: (k -> a -> Bool) -> Map k a -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Restrict a <a>Map</a> to only
grthose keys found in a <a>Set</a>.
gr
gr<pre>
grm `<tt>restrictKeys'</tt> s = <a>filterWithKey</a> (k _ -&gt; k `<a>member'</a> s) m
grm `<tt>restrictKeys'</tt> s = m `<tt>intersect</tt> <a>fromSet</a> (const ()) s
gr</pre>
restrictKeys :: Ord k => Map k a -> Set k -> Map k a

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Remove all keys in a <a>Set</a>
grfrom a <a>Map</a>.
gr
gr<pre>
grm `<tt>withoutKeys'</tt> s = <a>filterWithKey</a> (k _ -&gt; k `<a>notMember'</a> s) m
grm `<tt>withoutKeys'</tt> s = m `<tt>difference'</tt> <a>fromSet</a> (const ()) s
gr</pre>
withoutKeys :: Ord k => Map k a -> Set k -> Map k a

-- | <i>O(n)</i>. Partition the map according to a predicate. The first map
grcontains all elements that satisfy the predicate, the second all
grelements that fail the predicate. See also <a>split</a>.
gr
gr<pre>
grpartition (&gt; "a") (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a")
grpartition (&lt; "x") (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
grpartition (&gt; "x") (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
gr</pre>
partition :: (a -> Bool) -> Map k a -> (Map k a, Map k a)

-- | <i>O(n)</i>. Partition the map according to a predicate. The first map
grcontains all elements that satisfy the predicate, the second all
grelements that fail the predicate. See also <a>split</a>.
gr
gr<pre>
grpartitionWithKey (\ k _ -&gt; k &gt; 3) (fromList [(5,"a"), (3,"b")]) == (singleton 5 "a", singleton 3 "b")
grpartitionWithKey (\ k _ -&gt; k &lt; 7) (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
grpartitionWithKey (\ k _ -&gt; k &gt; 7) (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
gr</pre>
partitionWithKey :: (k -> a -> Bool) -> Map k a -> (Map k a, Map k a)

-- | <i>O(log n)</i>. Take while a predicate on the keys holds. The user is
grresponsible for ensuring that for all keys <tt>j</tt> and <tt>k</tt>
grin the map, <tt>j &lt; k ==&gt; p j &gt;= p k</tt>. See note at
gr<a>spanAntitone</a>.
gr
gr<pre>
grtakeWhileAntitone p = <a>fromDistinctAscList</a> . <a>takeWhile</a> (p . fst) . <a>toList</a>
grtakeWhileAntitone p = <a>filterWithKey</a> (k _ -&gt; p k)
gr</pre>
takeWhileAntitone :: (k -> Bool) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Drop while a predicate on the keys holds. The user is
grresponsible for ensuring that for all keys <tt>j</tt> and <tt>k</tt>
grin the map, <tt>j &lt; k ==&gt; p j &gt;= p k</tt>. See note at
gr<a>spanAntitone</a>.
gr
gr<pre>
grdropWhileAntitone p = <a>fromDistinctAscList</a> . <a>dropWhile</a> (p . fst) . <a>toList</a>
grdropWhileAntitone p = <a>filterWithKey</a> (k -&gt; not (p k))
gr</pre>
dropWhileAntitone :: (k -> Bool) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Divide a map at the point where a predicate on the
grkeys stops holding. The user is responsible for ensuring that for all
grkeys <tt>j</tt> and <tt>k</tt> in the map, <tt>j &lt; k ==&gt; p j
gr&gt;= p k</tt>.
gr
gr<pre>
grspanAntitone p xs = (<a>takeWhileAntitone</a> p xs, <a>dropWhileAntitone</a> p xs)
grspanAntitone p xs = partition p xs
gr</pre>
gr
grNote: if <tt>p</tt> is not actually antitone, then
gr<tt>spanAntitone</tt> will split the map at some <i>unspecified</i>
grpoint where the predicate switches from holding to not holding (where
grthe predicate is seen to hold before the first key and to fail after
grthe last key).
spanAntitone :: (k -> Bool) -> Map k a -> (Map k a, Map k a)

-- | <i>O(n)</i>. Map values and collect the <a>Just</a> results.
gr
gr<pre>
grlet f x = if x == "a" then Just "new a" else Nothing
grmapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"
gr</pre>
mapMaybe :: (a -> Maybe b) -> Map k a -> Map k b

-- | <i>O(n)</i>. Map keys/values and collect the <a>Just</a> results.
gr
gr<pre>
grlet f k _ = if k &lt; 5 then Just ("key : " ++ (show k)) else Nothing
grmapMaybeWithKey f (fromList [(5,"a"), (3,"b")]) == singleton 3 "key : 3"
gr</pre>
mapMaybeWithKey :: (k -> a -> Maybe b) -> Map k a -> Map k b

-- | <i>O(n)</i>. Map values and separate the <a>Left</a> and <a>Right</a>
grresults.
gr
gr<pre>
grlet f a = if a &lt; "c" then Left a else Right a
grmapEither f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (fromList [(3,"b"), (5,"a")], fromList [(1,"x"), (7,"z")])
gr
grmapEither (\ a -&gt; Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (empty, fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr</pre>
mapEither :: (a -> Either b c) -> Map k a -> (Map k b, Map k c)

-- | <i>O(n)</i>. Map keys/values and separate the <a>Left</a> and
gr<a>Right</a> results.
gr
gr<pre>
grlet f k a = if k &lt; 5 then Left (k * 2) else Right (a ++ a)
grmapEitherWithKey f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (fromList [(1,2), (3,6)], fromList [(5,"aa"), (7,"zz")])
gr
grmapEitherWithKey (\_ a -&gt; Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (empty, fromList [(1,"x"), (3,"b"), (5,"a"), (7,"z")])
gr</pre>
mapEitherWithKey :: (k -> a -> Either b c) -> Map k a -> (Map k b, Map k c)

-- | <i>O(log n)</i>. The expression (<tt><a>split</a> k map</tt>) is a
grpair <tt>(map1,map2)</tt> where the keys in <tt>map1</tt> are smaller
grthan <tt>k</tt> and the keys in <tt>map2</tt> larger than <tt>k</tt>.
grAny key equal to <tt>k</tt> is found in neither <tt>map1</tt> nor
gr<tt>map2</tt>.
gr
gr<pre>
grsplit 2 (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3,"b"), (5,"a")])
grsplit 3 (fromList [(5,"a"), (3,"b")]) == (empty, singleton 5 "a")
grsplit 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a")
grsplit 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", empty)
grsplit 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], empty)
gr</pre>
split :: Ord k => k -> Map k a -> (Map k a, Map k a)

-- | <i>O(log n)</i>. The expression (<tt><a>splitLookup</a> k map</tt>)
grsplits a map just like <a>split</a> but also returns <tt><a>lookup</a>
grk map</tt>.
gr
gr<pre>
grsplitLookup 2 (fromList [(5,"a"), (3,"b")]) == (empty, Nothing, fromList [(3,"b"), (5,"a")])
grsplitLookup 3 (fromList [(5,"a"), (3,"b")]) == (empty, Just "b", singleton 5 "a")
grsplitLookup 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Nothing, singleton 5 "a")
grsplitLookup 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Just "a", empty)
grsplitLookup 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], Nothing, empty)
gr</pre>
splitLookup :: Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)

-- | <i>O(1)</i>. Decompose a map into pieces based on the structure of the
grunderlying tree. This function is useful for consuming a map in
grparallel.
gr
grNo guarantee is made as to the sizes of the pieces; an internal, but
grdeterministic process determines this. However, it is guaranteed that
grthe pieces returned will be in ascending order (all elements in the
grfirst submap less than all elements in the second, and so on).
gr
grExamples:
gr
gr<pre>
grsplitRoot (fromList (zip [1..6] ['a'..])) ==
gr  [fromList [(1,'a'),(2,'b'),(3,'c')],fromList [(4,'d')],fromList [(5,'e'),(6,'f')]]
gr</pre>
gr
gr<pre>
grsplitRoot empty == []
gr</pre>
gr
grNote that the current implementation does not return more than three
grsubmaps, but you should not depend on this behaviour because it can
grchange in the future without notice.
splitRoot :: Map k b -> [Map k b]

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. This function is defined as
gr(<tt><a>isSubmapOf</a> = <a>isSubmapOfBy</a> (==)</tt>).
isSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. The expression
gr(<tt><a>isSubmapOfBy</a> f t1 t2</tt>) returns <a>True</a> if all keys
grin <tt>t1</tt> are in tree <tt>t2</tt>, and when <tt>f</tt> returns
gr<a>True</a> when applied to their respective values. For example, the
grfollowing expressions are all <a>True</a>:
gr
gr<pre>
grisSubmapOfBy (==) (fromList [('a',1)]) (fromList [('a',1),('b',2)])
grisSubmapOfBy (&lt;=) (fromList [('a',1)]) (fromList [('a',1),('b',2)])
grisSubmapOfBy (==) (fromList [('a',1),('b',2)]) (fromList [('a',1),('b',2)])
gr</pre>
gr
grBut the following are all <a>False</a>:
gr
gr<pre>
grisSubmapOfBy (==) (fromList [('a',2)]) (fromList [('a',1),('b',2)])
grisSubmapOfBy (&lt;)  (fromList [('a',1)]) (fromList [('a',1),('b',2)])
grisSubmapOfBy (==) (fromList [('a',1),('b',2)]) (fromList [('a',1)])
gr</pre>
isSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Is this a proper submap? (ie. a
grsubmap but not equal). Defined as (<tt><a>isProperSubmapOf</a> =
gr<a>isProperSubmapOfBy</a> (==)</tt>).
isProperSubmapOf :: (Ord k, Eq a) => Map k a -> Map k a -> Bool

-- | <i>O(m*log(n/m + 1)), m &lt;= n</i>. Is this a proper submap? (ie. a
grsubmap but not equal). The expression (<tt><a>isProperSubmapOfBy</a> f
grm1 m2</tt>) returns <a>True</a> when <tt>m1</tt> and <tt>m2</tt> are
grnot equal, all keys in <tt>m1</tt> are in <tt>m2</tt>, and when
gr<tt>f</tt> returns <a>True</a> when applied to their respective
grvalues. For example, the following expressions are all <a>True</a>:
gr
gr<pre>
grisProperSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
grisProperSubmapOfBy (&lt;=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
gr</pre>
gr
grBut the following are all <a>False</a>:
gr
gr<pre>
grisProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])
grisProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
grisProperSubmapOfBy (&lt;)  (fromList [(1,1)])       (fromList [(1,1),(2,2)])
gr</pre>
isProperSubmapOfBy :: Ord k => (a -> b -> Bool) -> Map k a -> Map k b -> Bool

-- | <i>O(log n)</i>. Lookup the <i>index</i> of a key, which is its
grzero-based index in the sequence sorted by keys. The index is a number
grfrom <i>0</i> up to, but not including, the <a>size</a> of the map.
gr
gr<pre>
grisJust (lookupIndex 2 (fromList [(5,"a"), (3,"b")]))   == False
grfromJust (lookupIndex 3 (fromList [(5,"a"), (3,"b")])) == 0
grfromJust (lookupIndex 5 (fromList [(5,"a"), (3,"b")])) == 1
grisJust (lookupIndex 6 (fromList [(5,"a"), (3,"b")]))   == False
gr</pre>
lookupIndex :: Ord k => k -> Map k a -> Maybe Int

-- | <i>O(log n)</i>. Return the <i>index</i> of a key, which is its
grzero-based index in the sequence sorted by keys. The index is a number
grfrom <i>0</i> up to, but not including, the <a>size</a> of the map.
grCalls <a>error</a> when the key is not a <a>member</a> of the map.
gr
gr<pre>
grfindIndex 2 (fromList [(5,"a"), (3,"b")])    Error: element is not in the map
grfindIndex 3 (fromList [(5,"a"), (3,"b")]) == 0
grfindIndex 5 (fromList [(5,"a"), (3,"b")]) == 1
grfindIndex 6 (fromList [(5,"a"), (3,"b")])    Error: element is not in the map
gr</pre>
findIndex :: Ord k => k -> Map k a -> Int

-- | <i>O(log n)</i>. Retrieve an element by its <i>index</i>, i.e. by its
grzero-based index in the sequence sorted by keys. If the <i>index</i>
gris out of range (less than zero, greater or equal to <a>size</a> of
grthe map), <a>error</a> is called.
gr
gr<pre>
grelemAt 0 (fromList [(5,"a"), (3,"b")]) == (3,"b")
grelemAt 1 (fromList [(5,"a"), (3,"b")]) == (5, "a")
grelemAt 2 (fromList [(5,"a"), (3,"b")])    Error: index out of range
gr</pre>
elemAt :: Int -> Map k a -> (k, a)

-- | <i>O(log n)</i>. Update the element at <i>index</i>, i.e. by its
grzero-based index in the sequence sorted by keys. If the <i>index</i>
gris out of range (less than zero, greater or equal to <a>size</a> of
grthe map), <a>error</a> is called.
gr
gr<pre>
grupdateAt (\ _ _ -&gt; Just "x") 0    (fromList [(5,"a"), (3,"b")]) == fromList [(3, "x"), (5, "a")]
grupdateAt (\ _ _ -&gt; Just "x") 1    (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "x")]
grupdateAt (\ _ _ -&gt; Just "x") 2    (fromList [(5,"a"), (3,"b")])    Error: index out of range
grupdateAt (\ _ _ -&gt; Just "x") (-1) (fromList [(5,"a"), (3,"b")])    Error: index out of range
grupdateAt (\_ _  -&gt; Nothing)  0    (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
grupdateAt (\_ _  -&gt; Nothing)  1    (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grupdateAt (\_ _  -&gt; Nothing)  2    (fromList [(5,"a"), (3,"b")])    Error: index out of range
grupdateAt (\_ _  -&gt; Nothing)  (-1) (fromList [(5,"a"), (3,"b")])    Error: index out of range
gr</pre>
updateAt :: (k -> a -> Maybe a) -> Int -> Map k a -> Map k a

-- | <i>O(log n)</i>. Delete the element at <i>index</i>, i.e. by its
grzero-based index in the sequence sorted by keys. If the <i>index</i>
gris out of range (less than zero, greater or equal to <a>size</a> of
grthe map), <a>error</a> is called.
gr
gr<pre>
grdeleteAt 0  (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
grdeleteAt 1  (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grdeleteAt 2 (fromList [(5,"a"), (3,"b")])     Error: index out of range
grdeleteAt (-1) (fromList [(5,"a"), (3,"b")])  Error: index out of range
gr</pre>
deleteAt :: Int -> Map k a -> Map k a

-- | Take a given number of entries in key order, beginning with the
grsmallest keys.
gr
gr<pre>
grtake n = <a>fromDistinctAscList</a> . <a>take</a> n . <a>toAscList</a>
gr</pre>
take :: Int -> Map k a -> Map k a

-- | Drop a given number of entries in key order, beginning with the
grsmallest keys.
gr
gr<pre>
grdrop n = <a>fromDistinctAscList</a> . <a>drop</a> n . <a>toAscList</a>
gr</pre>
drop :: Int -> Map k a -> Map k a

-- | <i>O(log n)</i>. Split a map at a particular index.
gr
gr<pre>
grsplitAt !n !xs = (<a>take</a> n xs, <a>drop</a> n xs)
gr</pre>
splitAt :: Int -> Map k a -> (Map k a, Map k a)

-- | <i>O(log n)</i>. The minimal key of the map. Returns <a>Nothing</a> if
grthe map is empty.
gr
gr<pre>
grlookupMin (fromList [(5,"a"), (3,"b")]) == Just (3,"b")
grfindMin empty = Nothing
gr</pre>
lookupMin :: Map k a -> Maybe (k, a)

-- | <i>O(log n)</i>. The maximal key of the map. Returns <a>Nothing</a> if
grthe map is empty.
gr
gr<pre>
grlookupMax (fromList [(5,"a"), (3,"b")]) == Just (5,"a")
grlookupMax empty = Nothing
gr</pre>
lookupMax :: Map k a -> Maybe (k, a)

-- | <i>O(log n)</i>. The minimal key of the map. Calls <a>error</a> if the
grmap is empty.
gr
gr<pre>
grfindMin (fromList [(5,"a"), (3,"b")]) == (3,"b")
grfindMin empty                            Error: empty map has no minimal element
gr</pre>
findMin :: Map k a -> (k, a)
findMax :: Map k a -> (k, a)

-- | <i>O(log n)</i>. Delete the minimal key. Returns an empty map if the
grmap is empty.
gr
gr<pre>
grdeleteMin (fromList [(5,"a"), (3,"b"), (7,"c")]) == fromList [(5,"a"), (7,"c")]
grdeleteMin empty == empty
gr</pre>
deleteMin :: Map k a -> Map k a

-- | <i>O(log n)</i>. Delete the maximal key. Returns an empty map if the
grmap is empty.
gr
gr<pre>
grdeleteMax (fromList [(5,"a"), (3,"b"), (7,"c")]) == fromList [(3,"b"), (5,"a")]
grdeleteMax empty == empty
gr</pre>
deleteMax :: Map k a -> Map k a

-- | <i>O(log n)</i>. Delete and find the minimal element.
gr
gr<pre>
grdeleteFindMin (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((3,"b"), fromList[(5,"a"), (10,"c")])
grdeleteFindMin                                            Error: can not return the minimal element of an empty map
gr</pre>
deleteFindMin :: Map k a -> ((k, a), Map k a)

-- | <i>O(log n)</i>. Delete and find the maximal element.
gr
gr<pre>
grdeleteFindMax (fromList [(5,"a"), (3,"b"), (10,"c")]) == ((10,"c"), fromList [(3,"b"), (5,"a")])
grdeleteFindMax empty                                      Error: can not return the maximal element of an empty map
gr</pre>
deleteFindMax :: Map k a -> ((k, a), Map k a)

-- | <i>O(log n)</i>. Update the value at the minimal key.
gr
gr<pre>
grupdateMin (\ a -&gt; Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "Xb"), (5, "a")]
grupdateMin (\ _ -&gt; Nothing)         (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateMin :: (a -> Maybe a) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Update the value at the maximal key.
gr
gr<pre>
grupdateMax (\ a -&gt; Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "Xa")]
grupdateMax (\ _ -&gt; Nothing)         (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr</pre>
updateMax :: (a -> Maybe a) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Update the value at the minimal key.
gr
gr<pre>
grupdateMinWithKey (\ k a -&gt; Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"3:b"), (5,"a")]
grupdateMinWithKey (\ _ _ -&gt; Nothing)                     (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateMinWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Update the value at the maximal key.
gr
gr<pre>
grupdateMaxWithKey (\ k a -&gt; Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"b"), (5,"5:a")]
grupdateMaxWithKey (\ _ _ -&gt; Nothing)                     (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr</pre>
updateMaxWithKey :: (k -> a -> Maybe a) -> Map k a -> Map k a

-- | <i>O(log n)</i>. Retrieves the value associated with minimal key of
grthe map, and the map stripped of that element, or <a>Nothing</a> if
grpassed an empty map.
gr
gr<pre>
grminView (fromList [(5,"a"), (3,"b")]) == Just ("b", singleton 5 "a")
grminView empty == Nothing
gr</pre>
minView :: Map k a -> Maybe (a, Map k a)

-- | <i>O(log n)</i>. Retrieves the value associated with maximal key of
grthe map, and the map stripped of that element, or <a>Nothing</a> if
grpassed an empty map.
gr
gr<pre>
grmaxView (fromList [(5,"a"), (3,"b")]) == Just ("a", singleton 3 "b")
grmaxView empty == Nothing
gr</pre>
maxView :: Map k a -> Maybe (a, Map k a)

-- | <i>O(log n)</i>. Retrieves the minimal (key,value) pair of the map,
grand the map stripped of that element, or <a>Nothing</a> if passed an
grempty map.
gr
gr<pre>
grminViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((3,"b"), singleton 5 "a")
grminViewWithKey empty == Nothing
gr</pre>
minViewWithKey :: Map k a -> Maybe ((k, a), Map k a)

-- | <i>O(log n)</i>. Retrieves the maximal (key,value) pair of the map,
grand the map stripped of that element, or <a>Nothing</a> if passed an
grempty map.
gr
gr<pre>
grmaxViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((5,"a"), singleton 3 "b")
grmaxViewWithKey empty == Nothing
gr</pre>
maxViewWithKey :: Map k a -> Maybe ((k, a), Map k a)

-- | <i>O(n)</i>. Show the tree that implements the map. The tree is shown
grin a compressed, hanging format. See <a>showTreeWith</a>.

-- | <i>Deprecated: <a>showTree</a> is now in
gr<a>Data.Map.Internal.Debug</a></i>
showTree :: (Show k, Show a) => Map k a -> String

-- | <i>O(n)</i>. The expression (<tt><a>showTreeWith</a> showelem hang
grwide map</tt>) shows the tree that implements the map. Elements are
grshown using the <tt>showElem</tt> function. If <tt>hang</tt> is
gr<a>True</a>, a <i>hanging</i> tree is shown otherwise a rotated tree
gris shown. If <tt>wide</tt> is <a>True</a>, an extra wide version is
grshown.
gr
gr<pre>
grMap&gt; let t = fromDistinctAscList [(x,()) | x &lt;- [1..5]]
grMap&gt; putStrLn $ showTreeWith (\k x -&gt; show (k,x)) True False t
gr(4,())
gr+--(2,())
gr|  +--(1,())
gr|  +--(3,())
gr+--(5,())
gr
grMap&gt; putStrLn $ showTreeWith (\k x -&gt; show (k,x)) True True t
gr(4,())
gr|
gr+--(2,())
gr|  |
gr|  +--(1,())
gr|  |
gr|  +--(3,())
gr|
gr+--(5,())
gr
grMap&gt; putStrLn $ showTreeWith (\k x -&gt; show (k,x)) False True t
gr+--(5,())
gr|
gr(4,())
gr|
gr|  +--(3,())
gr|  |
gr+--(2,())
gr   |
gr   +--(1,())
gr</pre>

-- | <i>Deprecated: <a>showTreeWith</a> is now in
gr<a>Data.Map.Internal.Debug</a></i>
showTreeWith :: (k -> a -> String) -> Bool -> Bool -> Map k a -> String

-- | <i>O(n)</i>. Test if the internal map structure is valid.
gr
gr<pre>
grvalid (fromAscList [(3,"b"), (5,"a")]) == True
grvalid (fromAscList [(5,"a"), (3,"b")]) == False
gr</pre>
valid :: Ord k => Map k a -> Bool


-- | <i>Note:</i> You should use <a>Data.Map.Strict</a> instead of this
grmodule if:
gr
gr<ul>
gr<li>You will eventually need all the values stored.</li>
gr<li>The stored values don't represent large virtual data structures to
grbe lazily computed.</li>
gr</ul>
gr
grAn efficient implementation of ordered maps from keys to values
gr(dictionaries).
gr
grThese modules are intended to be imported qualified, to avoid name
grclashes with Prelude functions, e.g.
gr
gr<pre>
grimport qualified Data.Map as Map
gr</pre>
gr
grThe implementation of <a>Map</a> is based on <i>size balanced</i>
grbinary trees (or trees of <i>bounded balance</i>) as described by:
gr
gr<ul>
gr<li>Stephen Adams, "<i>Efficient sets: a balancing act</i>", Journal
grof Functional Programming 3(4):553-562, October 1993,
gr<a>http://www.swiss.ai.mit.edu/~adams/BB/</a>.</li>
gr<li>J. Nievergelt and E.M. Reingold, "<i>Binary search trees of
grbounded balance</i>", SIAM journal of computing 2(1), March 1973.</li>
gr</ul>
gr
grBounds for <a>union</a>, <a>intersection</a>, and <a>difference</a>
grare as given by
gr
gr<ul>
gr<li>Guy Blelloch, Daniel Ferizovic, and Yihan Sun, "<i>Just Join for
grParallel Ordered Sets</i>",
gr<a>https://arxiv.org/abs/1602.02120v3</a>.</li>
gr</ul>
gr
grNote that the implementation is <i>left-biased</i> -- the elements of
gra first argument are always preferred to the second, for example in
gr<a>union</a> or <a>insert</a>.
gr
gr<i>Warning</i>: The size of the map must not exceed
gr<tt>maxBound::Int</tt>. Violation of this condition is not detected
grand if the size limit is exceeded, its behaviour is undefined.
gr
grOperation comments contain the operation time complexity in the Big-O
grnotation (<a>http://en.wikipedia.org/wiki/Big_O_notation</a>).
module Data.Map

-- | <i>O(log n)</i>. Same as <a>insertWith</a>, but the value being
grinserted to the map is evaluated to WHNF beforehand.
gr
grFor example, to update a counter:
gr
gr<pre>
grinsertWith' (+) k 1 m
gr</pre>

-- | <i>Deprecated: As of version 0.5, replaced by <a>insertWith</a>.</i>
insertWith' :: Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a

-- | <i>O(log n)</i>. Same as <a>insertWithKey</a>, but the value being
grinserted to the map is evaluated to WHNF beforehand.

-- | <i>Deprecated: As of version 0.5, replaced by
gr<a>insertWithKey</a>.</i>
insertWithKey' :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> Map k a

-- | <i>O(log n)</i>. Same as <a>insertLookupWithKey</a>, but the value
grbeing inserted to the map is evaluated to WHNF beforehand.

-- | <i>Deprecated: As of version 0.5, replaced by
gr<a>insertLookupWithKey</a>.</i>
insertLookupWithKey' :: Ord k => (k -> a -> a -> a) -> k -> a -> Map k a -> (Maybe a, Map k a)

-- | <i>O(n)</i>. Fold the values in the map using the given
grright-associative binary operator. This function is an equivalent of
gr<a>foldr</a> and is present for compatibility only.

-- | <i>Deprecated: As of version 0.5, replaced by <a>foldr</a>.</i>
fold :: (a -> b -> b) -> b -> Map k a -> b

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grright-associative binary operator. This function is an equivalent of
gr<a>foldrWithKey</a> and is present for compatibility only.

-- | <i>Deprecated: As of version 0.4, replaced by <a>foldrWithKey</a>.</i>
foldWithKey :: (k -> a -> b -> b) -> b -> Map k a -> b


-- | <h1>WARNING</h1>
gr
grThis module is considered <b>internal</b>.
gr
grThe Package Versioning Policy <b>does not apply</b>.
gr
grThis contents of this module may change <b>in any way whatsoever</b>
grand <b>without any warning</b> between minor versions of this package.
gr
grAuthors importing this module are expected to track development
grclosely.
gr
gr<h1>Description</h1>
gr
grAn efficient implementation of integer sets.
gr
grThese modules are intended to be imported qualified, to avoid name
grclashes with Prelude functions, e.g.
gr
gr<pre>
grimport Data.IntSet (IntSet)
grimport qualified Data.IntSet as IntSet
gr</pre>
gr
grThe implementation is based on <i>big-endian patricia trees</i>. This
grdata structure performs especially well on binary operations like
gr<a>union</a> and <a>intersection</a>. However, my benchmarks show that
grit is also (much) faster on insertions and deletions when compared to
gra generic size-balanced set implementation (see <a>Data.Set</a>).
gr
gr<ul>
gr<li>Chris Okasaki and Andy Gill, "<i>Fast Mergeable Integer Maps</i>",
grWorkshop on ML, September 1998, pages 77-86,
gr<a>http://citeseer.ist.psu.edu/okasaki98fast.html</a></li>
gr<li>D.R. Morrison, "/PATRICIA -- Practical Algorithm To Retrieve
grInformation Coded In Alphanumeric/", Journal of the ACM, 15(4),
grOctober 1968, pages 514-534.</li>
gr</ul>
gr
grAdditionally, this implementation places bitmaps in the leaves of the
grtree. Their size is the natural size of a machine word (32 or 64 bits)
grand greatly reduce memory footprint and execution times for dense
grsets, e.g. sets where it is likely that many values lie close to each
grother. The asymptotics are not affected by this optimization.
gr
grMany operations have a worst-case complexity of <i>O(min(n,W))</i>.
grThis means that the operation can become linear in the number of
grelements with a maximum of <i>W</i> -- the number of bits in an
gr<a>Int</a> (32 or 64).
module Data.IntSet.Internal

-- | A set of integers.
data IntSet
Bin :: {-# UNPACK #-} !Prefix -> {-# UNPACK #-} !Mask -> !IntSet -> !IntSet -> IntSet
Tip :: {-# UNPACK #-} !Prefix -> {-# UNPACK #-} !BitMap -> IntSet
Nil :: IntSet
type Key = Int
type Prefix = Int
type Mask = Int
type BitMap = Word

-- | <i>O(n+m)</i>. See <a>difference</a>.
(\\) :: IntSet -> IntSet -> IntSet
infixl 9 \\

-- | <i>O(1)</i>. Is the set empty?
null :: IntSet -> Bool

-- | <i>O(n)</i>. Cardinality of the set.
size :: IntSet -> Int

-- | <i>O(min(n,W))</i>. Is the value a member of the set?
member :: Key -> IntSet -> Bool

-- | <i>O(min(n,W))</i>. Is the element not in the set?
notMember :: Key -> IntSet -> Bool

-- | <i>O(log n)</i>. Find largest element smaller than the given one.
gr
gr<pre>
grlookupLT 3 (fromList [3, 5]) == Nothing
grlookupLT 5 (fromList [3, 5]) == Just 3
gr</pre>
lookupLT :: Key -> IntSet -> Maybe Key

-- | <i>O(log n)</i>. Find smallest element greater than the given one.
gr
gr<pre>
grlookupGT 4 (fromList [3, 5]) == Just 5
grlookupGT 5 (fromList [3, 5]) == Nothing
gr</pre>
lookupGT :: Key -> IntSet -> Maybe Key

-- | <i>O(log n)</i>. Find largest element smaller or equal to the given
grone.
gr
gr<pre>
grlookupLE 2 (fromList [3, 5]) == Nothing
grlookupLE 4 (fromList [3, 5]) == Just 3
grlookupLE 5 (fromList [3, 5]) == Just 5
gr</pre>
lookupLE :: Key -> IntSet -> Maybe Key

-- | <i>O(log n)</i>. Find smallest element greater or equal to the given
grone.
gr
gr<pre>
grlookupGE 3 (fromList [3, 5]) == Just 3
grlookupGE 4 (fromList [3, 5]) == Just 5
grlookupGE 6 (fromList [3, 5]) == Nothing
gr</pre>
lookupGE :: Key -> IntSet -> Maybe Key

-- | <i>O(n+m)</i>. Is this a subset? <tt>(s1 <a>isSubsetOf</a> s2)</tt>
grtells whether <tt>s1</tt> is a subset of <tt>s2</tt>.
isSubsetOf :: IntSet -> IntSet -> Bool

-- | <i>O(n+m)</i>. Is this a proper subset? (ie. a subset but not equal).
isProperSubsetOf :: IntSet -> IntSet -> Bool

-- | <i>O(n+m)</i>. Check whether two sets are disjoint (i.e. their
grintersection is empty).
gr
gr<pre>
grdisjoint (fromList [2,4,6])   (fromList [1,3])     == True
grdisjoint (fromList [2,4,6,8]) (fromList [2,3,5,7]) == False
grdisjoint (fromList [1,2])     (fromList [1,2,3,4]) == False
grdisjoint (fromList [])        (fromList [])        == True
gr</pre>
disjoint :: IntSet -> IntSet -> Bool

-- | <i>O(1)</i>. The empty set.
empty :: IntSet

-- | <i>O(1)</i>. A set of one element.
singleton :: Key -> IntSet

-- | <i>O(min(n,W))</i>. Add a value to the set. There is no left- or right
grbias for IntSets.
insert :: Key -> IntSet -> IntSet

-- | <i>O(min(n,W))</i>. Delete a value in the set. Returns the original
grset when the value was not present.
delete :: Key -> IntSet -> IntSet

-- | <i>O(n+m)</i>. The union of two sets.
union :: IntSet -> IntSet -> IntSet

-- | The union of a list of sets.
unions :: [IntSet] -> IntSet

-- | <i>O(n+m)</i>. Difference between two sets.
difference :: IntSet -> IntSet -> IntSet

-- | <i>O(n+m)</i>. The intersection of two sets.
intersection :: IntSet -> IntSet -> IntSet

-- | <i>O(n)</i>. Filter all elements that satisfy some predicate.
filter :: (Key -> Bool) -> IntSet -> IntSet

-- | <i>O(n)</i>. partition the set according to some predicate.
partition :: (Key -> Bool) -> IntSet -> (IntSet, IntSet)

-- | <i>O(min(n,W))</i>. The expression (<tt><a>split</a> x set</tt>) is a
grpair <tt>(set1,set2)</tt> where <tt>set1</tt> comprises the elements
grof <tt>set</tt> less than <tt>x</tt> and <tt>set2</tt> comprises the
grelements of <tt>set</tt> greater than <tt>x</tt>.
gr
gr<pre>
grsplit 3 (fromList [1..5]) == (fromList [1,2], fromList [4,5])
gr</pre>
split :: Key -> IntSet -> (IntSet, IntSet)

-- | <i>O(min(n,W))</i>. Performs a <a>split</a> but also returns whether
grthe pivot element was found in the original set.
splitMember :: Key -> IntSet -> (IntSet, Bool, IntSet)

-- | <i>O(1)</i>. Decompose a set into pieces based on the structure of the
grunderlying tree. This function is useful for consuming a set in
grparallel.
gr
grNo guarantee is made as to the sizes of the pieces; an internal, but
grdeterministic process determines this. However, it is guaranteed that
grthe pieces returned will be in ascending order (all elements in the
grfirst submap less than all elements in the second, and so on).
gr
grExamples:
gr
gr<pre>
grsplitRoot (fromList [1..120]) == [fromList [1..63],fromList [64..120]]
grsplitRoot empty == []
gr</pre>
gr
grNote that the current implementation does not return more than two
grsubsets, but you should not depend on this behaviour because it can
grchange in the future without notice. Also, the current version does
grnot continue splitting all the way to individual singleton sets -- it
grstops at some point.
splitRoot :: IntSet -> [IntSet]

-- | <i>O(n*min(n,W))</i>. <tt><a>map</a> f s</tt> is the set obtained by
grapplying <tt>f</tt> to each element of <tt>s</tt>.
gr
grIt's worth noting that the size of the result may be smaller if, for
grsome <tt>(x,y)</tt>, <tt>x /= y &amp;&amp; f x == f y</tt>
map :: (Key -> Key) -> IntSet -> IntSet

-- | <i>O(n)</i>. Fold the elements in the set using the given
grright-associative binary operator, such that <tt><a>foldr</a> f z ==
gr<a>foldr</a> f z . <a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grtoAscList set = foldr (:) [] set
gr</pre>
foldr :: (Key -> b -> b) -> b -> IntSet -> b

-- | <i>O(n)</i>. Fold the elements in the set using the given
grleft-associative binary operator, such that <tt><a>foldl</a> f z ==
gr<a>foldl</a> f z . <a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grtoDescList set = foldl (flip (:)) [] set
gr</pre>
foldl :: (a -> Key -> a) -> a -> IntSet -> a

-- | <i>O(n)</i>. A strict version of <a>foldr</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldr' :: (Key -> b -> b) -> b -> IntSet -> b

-- | <i>O(n)</i>. A strict version of <a>foldl</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldl' :: (a -> Key -> a) -> a -> IntSet -> a

-- | <i>O(n)</i>. Fold the elements in the set using the given
grright-associative binary operator. This function is an equivalent of
gr<a>foldr</a> and is present for compatibility only.
gr
gr<i>Please note that fold will be deprecated in the future and
grremoved.</i>
fold :: (Key -> b -> b) -> b -> IntSet -> b

-- | <i>O(min(n,W))</i>. The minimal element of the set.
findMin :: IntSet -> Key

-- | <i>O(min(n,W))</i>. The maximal element of a set.
findMax :: IntSet -> Key

-- | <i>O(min(n,W))</i>. Delete the minimal element. Returns an empty set
grif the set is empty.
gr
grNote that this is a change of behaviour for consistency with
gr<a>Set</a> – versions prior to 0.5 threw an error if the <a>IntSet</a>
grwas already empty.
deleteMin :: IntSet -> IntSet

-- | <i>O(min(n,W))</i>. Delete the maximal element. Returns an empty set
grif the set is empty.
gr
grNote that this is a change of behaviour for consistency with
gr<a>Set</a> – versions prior to 0.5 threw an error if the <a>IntSet</a>
grwas already empty.
deleteMax :: IntSet -> IntSet

-- | <i>O(min(n,W))</i>. Delete and find the minimal element.
gr
gr<pre>
grdeleteFindMin set = (findMin set, deleteMin set)
gr</pre>
deleteFindMin :: IntSet -> (Key, IntSet)

-- | <i>O(min(n,W))</i>. Delete and find the maximal element.
gr
gr<pre>
grdeleteFindMax set = (findMax set, deleteMax set)
gr</pre>
deleteFindMax :: IntSet -> (Key, IntSet)

-- | <i>O(min(n,W))</i>. Retrieves the maximal key of the set, and the set
grstripped of that element, or <a>Nothing</a> if passed an empty set.
maxView :: IntSet -> Maybe (Key, IntSet)

-- | <i>O(min(n,W))</i>. Retrieves the minimal key of the set, and the set
grstripped of that element, or <a>Nothing</a> if passed an empty set.
minView :: IntSet -> Maybe (Key, IntSet)

-- | <i>O(n)</i>. An alias of <a>toAscList</a>. The elements of a set in
grascending order. Subject to list fusion.
elems :: IntSet -> [Key]

-- | <i>O(n)</i>. Convert the set to a list of elements. Subject to list
grfusion.
toList :: IntSet -> [Key]

-- | <i>O(n*min(n,W))</i>. Create a set from a list of integers.
fromList :: [Key] -> IntSet

-- | <i>O(n)</i>. Convert the set to an ascending list of elements. Subject
grto list fusion.
toAscList :: IntSet -> [Key]

-- | <i>O(n)</i>. Convert the set to a descending list of elements. Subject
grto list fusion.
toDescList :: IntSet -> [Key]

-- | <i>O(n)</i>. Build a set from an ascending list of elements. <i>The
grprecondition (input list is ascending) is not checked.</i>
fromAscList :: [Key] -> IntSet

-- | <i>O(n)</i>. Build a set from an ascending list of distinct elements.
gr<i>The precondition (input list is strictly ascending) is not
grchecked.</i>
fromDistinctAscList :: [Key] -> IntSet

-- | <i>O(n)</i>. Show the tree that implements the set. The tree is shown
grin a compressed, hanging format.
showTree :: IntSet -> String

-- | <i>O(n)</i>. The expression (<tt><a>showTreeWith</a> hang wide
grmap</tt>) shows the tree that implements the set. If <tt>hang</tt> is
gr<a>True</a>, a <i>hanging</i> tree is shown otherwise a rotated tree
gris shown. If <tt>wide</tt> is <a>True</a>, an extra wide version is
grshown.
showTreeWith :: Bool -> Bool -> IntSet -> String
match :: Int -> Prefix -> Mask -> Bool
suffixBitMask :: Int
prefixBitMask :: Int
bitmapOf :: Int -> BitMap
zero :: Int -> Mask -> Bool
instance GHC.Exts.IsList Data.IntSet.Internal.IntSet
instance GHC.Base.Monoid Data.IntSet.Internal.IntSet
instance GHC.Base.Semigroup Data.IntSet.Internal.IntSet
instance Data.Data.Data Data.IntSet.Internal.IntSet
instance GHC.Classes.Eq Data.IntSet.Internal.IntSet
instance GHC.Classes.Ord Data.IntSet.Internal.IntSet
instance GHC.Show.Show Data.IntSet.Internal.IntSet
instance GHC.Read.Read Data.IntSet.Internal.IntSet
instance Control.DeepSeq.NFData Data.IntSet.Internal.IntSet


-- | An efficient implementation of integer sets.
gr
grThese modules are intended to be imported qualified, to avoid name
grclashes with Prelude functions, e.g.
gr
gr<pre>
grimport Data.IntSet (IntSet)
grimport qualified Data.IntSet as IntSet
gr</pre>
gr
grThe implementation is based on <i>big-endian patricia trees</i>. This
grdata structure performs especially well on binary operations like
gr<a>union</a> and <a>intersection</a>. However, my benchmarks show that
grit is also (much) faster on insertions and deletions when compared to
gra generic size-balanced set implementation (see <a>Data.Set</a>).
gr
gr<ul>
gr<li>Chris Okasaki and Andy Gill, "<i>Fast Mergeable Integer Maps</i>",
grWorkshop on ML, September 1998, pages 77-86,
gr<a>http://citeseer.ist.psu.edu/okasaki98fast.html</a></li>
gr<li>D.R. Morrison, "/PATRICIA -- Practical Algorithm To Retrieve
grInformation Coded In Alphanumeric/", Journal of the ACM, 15(4),
grOctober 1968, pages 514-534.</li>
gr</ul>
gr
grAdditionally, this implementation places bitmaps in the leaves of the
grtree. Their size is the natural size of a machine word (32 or 64 bits)
grand greatly reduce memory footprint and execution times for dense
grsets, e.g. sets where it is likely that many values lie close to each
grother. The asymptotics are not affected by this optimization.
gr
grMany operations have a worst-case complexity of <i>O(min(n,W))</i>.
grThis means that the operation can become linear in the number of
grelements with a maximum of <i>W</i> -- the number of bits in an
gr<a>Int</a> (32 or 64).
module Data.IntSet

-- | A set of integers.
data IntSet
type Key = Int

-- | <i>O(n+m)</i>. See <a>difference</a>.
(\\) :: IntSet -> IntSet -> IntSet
infixl 9 \\

-- | <i>O(1)</i>. Is the set empty?
null :: IntSet -> Bool

-- | <i>O(n)</i>. Cardinality of the set.
size :: IntSet -> Int

-- | <i>O(min(n,W))</i>. Is the value a member of the set?
member :: Key -> IntSet -> Bool

-- | <i>O(min(n,W))</i>. Is the element not in the set?
notMember :: Key -> IntSet -> Bool

-- | <i>O(log n)</i>. Find largest element smaller than the given one.
gr
gr<pre>
grlookupLT 3 (fromList [3, 5]) == Nothing
grlookupLT 5 (fromList [3, 5]) == Just 3
gr</pre>
lookupLT :: Key -> IntSet -> Maybe Key

-- | <i>O(log n)</i>. Find smallest element greater than the given one.
gr
gr<pre>
grlookupGT 4 (fromList [3, 5]) == Just 5
grlookupGT 5 (fromList [3, 5]) == Nothing
gr</pre>
lookupGT :: Key -> IntSet -> Maybe Key

-- | <i>O(log n)</i>. Find largest element smaller or equal to the given
grone.
gr
gr<pre>
grlookupLE 2 (fromList [3, 5]) == Nothing
grlookupLE 4 (fromList [3, 5]) == Just 3
grlookupLE 5 (fromList [3, 5]) == Just 5
gr</pre>
lookupLE :: Key -> IntSet -> Maybe Key

-- | <i>O(log n)</i>. Find smallest element greater or equal to the given
grone.
gr
gr<pre>
grlookupGE 3 (fromList [3, 5]) == Just 3
grlookupGE 4 (fromList [3, 5]) == Just 5
grlookupGE 6 (fromList [3, 5]) == Nothing
gr</pre>
lookupGE :: Key -> IntSet -> Maybe Key

-- | <i>O(n+m)</i>. Is this a subset? <tt>(s1 <a>isSubsetOf</a> s2)</tt>
grtells whether <tt>s1</tt> is a subset of <tt>s2</tt>.
isSubsetOf :: IntSet -> IntSet -> Bool

-- | <i>O(n+m)</i>. Is this a proper subset? (ie. a subset but not equal).
isProperSubsetOf :: IntSet -> IntSet -> Bool

-- | <i>O(n+m)</i>. Check whether two sets are disjoint (i.e. their
grintersection is empty).
gr
gr<pre>
grdisjoint (fromList [2,4,6])   (fromList [1,3])     == True
grdisjoint (fromList [2,4,6,8]) (fromList [2,3,5,7]) == False
grdisjoint (fromList [1,2])     (fromList [1,2,3,4]) == False
grdisjoint (fromList [])        (fromList [])        == True
gr</pre>
disjoint :: IntSet -> IntSet -> Bool

-- | <i>O(1)</i>. The empty set.
empty :: IntSet

-- | <i>O(1)</i>. A set of one element.
singleton :: Key -> IntSet

-- | <i>O(min(n,W))</i>. Add a value to the set. There is no left- or right
grbias for IntSets.
insert :: Key -> IntSet -> IntSet

-- | <i>O(min(n,W))</i>. Delete a value in the set. Returns the original
grset when the value was not present.
delete :: Key -> IntSet -> IntSet

-- | <i>O(n+m)</i>. The union of two sets.
union :: IntSet -> IntSet -> IntSet

-- | The union of a list of sets.
unions :: [IntSet] -> IntSet

-- | <i>O(n+m)</i>. Difference between two sets.
difference :: IntSet -> IntSet -> IntSet

-- | <i>O(n+m)</i>. The intersection of two sets.
intersection :: IntSet -> IntSet -> IntSet

-- | <i>O(n)</i>. Filter all elements that satisfy some predicate.
filter :: (Key -> Bool) -> IntSet -> IntSet

-- | <i>O(n)</i>. partition the set according to some predicate.
partition :: (Key -> Bool) -> IntSet -> (IntSet, IntSet)

-- | <i>O(min(n,W))</i>. The expression (<tt><a>split</a> x set</tt>) is a
grpair <tt>(set1,set2)</tt> where <tt>set1</tt> comprises the elements
grof <tt>set</tt> less than <tt>x</tt> and <tt>set2</tt> comprises the
grelements of <tt>set</tt> greater than <tt>x</tt>.
gr
gr<pre>
grsplit 3 (fromList [1..5]) == (fromList [1,2], fromList [4,5])
gr</pre>
split :: Key -> IntSet -> (IntSet, IntSet)

-- | <i>O(min(n,W))</i>. Performs a <a>split</a> but also returns whether
grthe pivot element was found in the original set.
splitMember :: Key -> IntSet -> (IntSet, Bool, IntSet)

-- | <i>O(1)</i>. Decompose a set into pieces based on the structure of the
grunderlying tree. This function is useful for consuming a set in
grparallel.
gr
grNo guarantee is made as to the sizes of the pieces; an internal, but
grdeterministic process determines this. However, it is guaranteed that
grthe pieces returned will be in ascending order (all elements in the
grfirst submap less than all elements in the second, and so on).
gr
grExamples:
gr
gr<pre>
grsplitRoot (fromList [1..120]) == [fromList [1..63],fromList [64..120]]
grsplitRoot empty == []
gr</pre>
gr
grNote that the current implementation does not return more than two
grsubsets, but you should not depend on this behaviour because it can
grchange in the future without notice. Also, the current version does
grnot continue splitting all the way to individual singleton sets -- it
grstops at some point.
splitRoot :: IntSet -> [IntSet]

-- | <i>O(n*min(n,W))</i>. <tt><a>map</a> f s</tt> is the set obtained by
grapplying <tt>f</tt> to each element of <tt>s</tt>.
gr
grIt's worth noting that the size of the result may be smaller if, for
grsome <tt>(x,y)</tt>, <tt>x /= y &amp;&amp; f x == f y</tt>
map :: (Key -> Key) -> IntSet -> IntSet

-- | <i>O(n)</i>. Fold the elements in the set using the given
grright-associative binary operator, such that <tt><a>foldr</a> f z ==
gr<a>foldr</a> f z . <a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grtoAscList set = foldr (:) [] set
gr</pre>
foldr :: (Key -> b -> b) -> b -> IntSet -> b

-- | <i>O(n)</i>. Fold the elements in the set using the given
grleft-associative binary operator, such that <tt><a>foldl</a> f z ==
gr<a>foldl</a> f z . <a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grtoDescList set = foldl (flip (:)) [] set
gr</pre>
foldl :: (a -> Key -> a) -> a -> IntSet -> a

-- | <i>O(n)</i>. A strict version of <a>foldr</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldr' :: (Key -> b -> b) -> b -> IntSet -> b

-- | <i>O(n)</i>. A strict version of <a>foldl</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldl' :: (a -> Key -> a) -> a -> IntSet -> a

-- | <i>O(n)</i>. Fold the elements in the set using the given
grright-associative binary operator. This function is an equivalent of
gr<a>foldr</a> and is present for compatibility only.
gr
gr<i>Please note that fold will be deprecated in the future and
grremoved.</i>
fold :: (Key -> b -> b) -> b -> IntSet -> b

-- | <i>O(min(n,W))</i>. The minimal element of the set.
findMin :: IntSet -> Key

-- | <i>O(min(n,W))</i>. The maximal element of a set.
findMax :: IntSet -> Key

-- | <i>O(min(n,W))</i>. Delete the minimal element. Returns an empty set
grif the set is empty.
gr
grNote that this is a change of behaviour for consistency with
gr<a>Set</a> – versions prior to 0.5 threw an error if the <a>IntSet</a>
grwas already empty.
deleteMin :: IntSet -> IntSet

-- | <i>O(min(n,W))</i>. Delete the maximal element. Returns an empty set
grif the set is empty.
gr
grNote that this is a change of behaviour for consistency with
gr<a>Set</a> – versions prior to 0.5 threw an error if the <a>IntSet</a>
grwas already empty.
deleteMax :: IntSet -> IntSet

-- | <i>O(min(n,W))</i>. Delete and find the minimal element.
gr
gr<pre>
grdeleteFindMin set = (findMin set, deleteMin set)
gr</pre>
deleteFindMin :: IntSet -> (Key, IntSet)

-- | <i>O(min(n,W))</i>. Delete and find the maximal element.
gr
gr<pre>
grdeleteFindMax set = (findMax set, deleteMax set)
gr</pre>
deleteFindMax :: IntSet -> (Key, IntSet)

-- | <i>O(min(n,W))</i>. Retrieves the maximal key of the set, and the set
grstripped of that element, or <a>Nothing</a> if passed an empty set.
maxView :: IntSet -> Maybe (Key, IntSet)

-- | <i>O(min(n,W))</i>. Retrieves the minimal key of the set, and the set
grstripped of that element, or <a>Nothing</a> if passed an empty set.
minView :: IntSet -> Maybe (Key, IntSet)

-- | <i>O(n)</i>. An alias of <a>toAscList</a>. The elements of a set in
grascending order. Subject to list fusion.
elems :: IntSet -> [Key]

-- | <i>O(n)</i>. Convert the set to a list of elements. Subject to list
grfusion.
toList :: IntSet -> [Key]

-- | <i>O(n*min(n,W))</i>. Create a set from a list of integers.
fromList :: [Key] -> IntSet

-- | <i>O(n)</i>. Convert the set to an ascending list of elements. Subject
grto list fusion.
toAscList :: IntSet -> [Key]

-- | <i>O(n)</i>. Convert the set to a descending list of elements. Subject
grto list fusion.
toDescList :: IntSet -> [Key]

-- | <i>O(n)</i>. Build a set from an ascending list of elements. <i>The
grprecondition (input list is ascending) is not checked.</i>
fromAscList :: [Key] -> IntSet

-- | <i>O(n)</i>. Build a set from an ascending list of distinct elements.
gr<i>The precondition (input list is strictly ascending) is not
grchecked.</i>
fromDistinctAscList :: [Key] -> IntSet

-- | <i>O(n)</i>. Show the tree that implements the set. The tree is shown
grin a compressed, hanging format.
showTree :: IntSet -> String

-- | <i>O(n)</i>. The expression (<tt><a>showTreeWith</a> hang wide
grmap</tt>) shows the tree that implements the set. If <tt>hang</tt> is
gr<a>True</a>, a <i>hanging</i> tree is shown otherwise a rotated tree
gris shown. If <tt>wide</tt> is <a>True</a>, an extra wide version is
grshown.
showTreeWith :: Bool -> Bool -> IntSet -> String


-- | <h1>WARNING</h1>
gr
grThis module is considered <b>internal</b>.
gr
grThe Package Versioning Policy <b>does not apply</b>.
gr
grThis contents of this module may change <b>in any way whatsoever</b>
grand <b>without any warning</b> between minor versions of this package.
gr
grAuthors importing this module are expected to track development
grclosely.
gr
gr<h1>Description</h1>
gr
grThis defines the data structures and core (hidden) manipulations on
grrepresentations.
module Data.IntMap.Internal

-- | A map of integers to values <tt>a</tt>.
data IntMap a
Bin :: {-# UNPACK #-} !Prefix -> {-# UNPACK #-} !Mask -> !(IntMap a) -> !(IntMap a) -> IntMap a
Tip :: {-# UNPACK #-} !Key -> a -> IntMap a
Nil :: IntMap a
type Key = Int

-- | <i>O(min(n,W))</i>. Find the value at a key. Calls <a>error</a> when
grthe element can not be found.
gr
gr<pre>
grfromList [(5,'a'), (3,'b')] ! 1    Error: element not in the map
grfromList [(5,'a'), (3,'b')] ! 5 == 'a'
gr</pre>
(!) :: IntMap a -> Key -> a

-- | <i>O(min(n,W))</i>. Find the value at a key. Returns <a>Nothing</a>
grwhen the element can not be found.
gr
gr<pre>
grfromList [(5,'a'), (3,'b')] !? 1 == Nothing
grfromList [(5,'a'), (3,'b')] !? 5 == Just 'a'
gr</pre>
(!?) :: IntMap a -> Key -> Maybe a
infixl 9 !?

-- | Same as <a>difference</a>.
(\\) :: IntMap a -> IntMap b -> IntMap a
infixl 9 \\

-- | <i>O(1)</i>. Is the map empty?
gr
gr<pre>
grData.IntMap.null (empty)           == True
grData.IntMap.null (singleton 1 'a') == False
gr</pre>
null :: IntMap a -> Bool

-- | <i>O(n)</i>. Number of elements in the map.
gr
gr<pre>
grsize empty                                   == 0
grsize (singleton 1 'a')                       == 1
grsize (fromList([(1,'a'), (2,'c'), (3,'b')])) == 3
gr</pre>
size :: IntMap a -> Int

-- | <i>O(min(n,W))</i>. Is the key a member of the map?
gr
gr<pre>
grmember 5 (fromList [(5,'a'), (3,'b')]) == True
grmember 1 (fromList [(5,'a'), (3,'b')]) == False
gr</pre>
member :: Key -> IntMap a -> Bool

-- | <i>O(min(n,W))</i>. Is the key not a member of the map?
gr
gr<pre>
grnotMember 5 (fromList [(5,'a'), (3,'b')]) == False
grnotMember 1 (fromList [(5,'a'), (3,'b')]) == True
gr</pre>
notMember :: Key -> IntMap a -> Bool

-- | <i>O(min(n,W))</i>. Lookup the value at a key in the map. See also
gr<a>lookup</a>.
lookup :: Key -> IntMap a -> Maybe a

-- | <i>O(min(n,W))</i>. The expression <tt>(<a>findWithDefault</a> def k
grmap)</tt> returns the value at key <tt>k</tt> or returns <tt>def</tt>
grwhen the key is not an element of the map.
gr
gr<pre>
grfindWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x'
grfindWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a'
gr</pre>
findWithDefault :: a -> Key -> IntMap a -> a

-- | <i>O(log n)</i>. Find largest key smaller than the given one and
grreturn the corresponding (key, value) pair.
gr
gr<pre>
grlookupLT 3 (fromList [(3,'a'), (5,'b')]) == Nothing
grlookupLT 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
gr</pre>
lookupLT :: Key -> IntMap a -> Maybe (Key, a)

-- | <i>O(log n)</i>. Find smallest key greater than the given one and
grreturn the corresponding (key, value) pair.
gr
gr<pre>
grlookupGT 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
grlookupGT 5 (fromList [(3,'a'), (5,'b')]) == Nothing
gr</pre>
lookupGT :: Key -> IntMap a -> Maybe (Key, a)

-- | <i>O(log n)</i>. Find largest key smaller or equal to the given one
grand return the corresponding (key, value) pair.
gr
gr<pre>
grlookupLE 2 (fromList [(3,'a'), (5,'b')]) == Nothing
grlookupLE 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
grlookupLE 5 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
gr</pre>
lookupLE :: Key -> IntMap a -> Maybe (Key, a)

-- | <i>O(log n)</i>. Find smallest key greater or equal to the given one
grand return the corresponding (key, value) pair.
gr
gr<pre>
grlookupGE 3 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
grlookupGE 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
grlookupGE 6 (fromList [(3,'a'), (5,'b')]) == Nothing
gr</pre>
lookupGE :: Key -> IntMap a -> Maybe (Key, a)

-- | <i>O(1)</i>. The empty map.
gr
gr<pre>
grempty      == fromList []
grsize empty == 0
gr</pre>
empty :: IntMap a

-- | <i>O(1)</i>. A map of one element.
gr
gr<pre>
grsingleton 1 'a'        == fromList [(1, 'a')]
grsize (singleton 1 'a') == 1
gr</pre>
singleton :: Key -> a -> IntMap a

-- | <i>O(min(n,W))</i>. Insert a new key/value pair in the map. If the key
gris already present in the map, the associated value is replaced with
grthe supplied value, i.e. <a>insert</a> is equivalent to
gr<tt><a>insertWith</a> <a>const</a></tt>.
gr
gr<pre>
grinsert 5 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'x')]
grinsert 7 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'a'), (7, 'x')]
grinsert 5 'x' empty                         == singleton 5 'x'
gr</pre>
insert :: Key -> a -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Insert with a combining function.
gr<tt><a>insertWith</a> f key value mp</tt> will insert the pair (key,
grvalue) into <tt>mp</tt> if key does not exist in the map. If the key
grdoes exist, the function will insert <tt>f new_value old_value</tt>.
gr
gr<pre>
grinsertWith (++) 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "xxxa")]
grinsertWith (++) 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
grinsertWith (++) 5 "xxx" empty                         == singleton 5 "xxx"
gr</pre>
insertWith :: (a -> a -> a) -> Key -> a -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Insert with a combining function.
gr<tt><a>insertWithKey</a> f key value mp</tt> will insert the pair
gr(key, value) into <tt>mp</tt> if key does not exist in the map. If the
grkey does exist, the function will insert <tt>f key new_value
grold_value</tt>.
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grinsertWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:xxx|a")]
grinsertWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
grinsertWithKey f 5 "xxx" empty                         == singleton 5 "xxx"
gr</pre>
insertWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. The expression (<tt><a>insertLookupWithKey</a> f k
grx map</tt>) is a pair where the first element is equal to
gr(<tt><a>lookup</a> k map</tt>) and the second element equal to
gr(<tt><a>insertWithKey</a> f k x map</tt>).
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grinsertLookupWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:xxx|a")])
grinsertLookupWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a"), (7, "xxx")])
grinsertLookupWithKey f 5 "xxx" empty                         == (Nothing,  singleton 5 "xxx")
gr</pre>
gr
grThis is how to define <tt>insertLookup</tt> using
gr<tt>insertLookupWithKey</tt>:
gr
gr<pre>
grlet insertLookup kx x t = insertLookupWithKey (\_ a _ -&gt; a) kx x t
grinsertLookup 5 "x" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "x")])
grinsertLookup 7 "x" (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a"), (7, "x")])
gr</pre>
insertLookupWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> (Maybe a, IntMap a)

-- | <i>O(min(n,W))</i>. Delete a key and its value from the map. When the
grkey is not a member of the map, the original map is returned.
gr
gr<pre>
grdelete 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grdelete 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grdelete 5 empty                         == empty
gr</pre>
delete :: Key -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Adjust a value at a specific key. When the key is
grnot a member of the map, the original map is returned.
gr
gr<pre>
gradjust ("new " ++) 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
gradjust ("new " ++) 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gradjust ("new " ++) 7 empty                         == empty
gr</pre>
adjust :: (a -> a) -> Key -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Adjust a value at a specific key. When the key is
grnot a member of the map, the original map is returned.
gr
gr<pre>
grlet f key x = (show key) ++ ":new " ++ x
gradjustWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
gradjustWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gradjustWithKey f 7 empty                         == empty
gr</pre>
adjustWithKey :: (Key -> a -> a) -> Key -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. The expression (<tt><a>update</a> f k map</tt>)
grupdates the value <tt>x</tt> at <tt>k</tt> (if it is in the map). If
gr(<tt>f x</tt>) is <a>Nothing</a>, the element is deleted. If it is
gr(<tt><a>Just</a> y</tt>), the key <tt>k</tt> is bound to the new value
gr<tt>y</tt>.
gr
gr<pre>
grlet f x = if x == "a" then Just "new a" else Nothing
grupdate f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
grupdate f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grupdate f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
update :: (a -> Maybe a) -> Key -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. The expression (<tt><a>update</a> f k map</tt>)
grupdates the value <tt>x</tt> at <tt>k</tt> (if it is in the map). If
gr(<tt>f k x</tt>) is <a>Nothing</a>, the element is deleted. If it is
gr(<tt><a>Just</a> y</tt>), the key <tt>k</tt> is bound to the new value
gr<tt>y</tt>.
gr
gr<pre>
grlet f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
grupdateWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
grupdateWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grupdateWithKey f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Lookup and update. The function returns original
grvalue, if it is updated. This is different behavior than
gr<a>updateLookupWithKey</a>. Returns the original key value if the map
grentry is deleted.
gr
gr<pre>
grlet f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
grupdateLookupWithKey f 5 (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:new a")])
grupdateLookupWithKey f 7 (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a")])
grupdateLookupWithKey f 3 (fromList [(5,"a"), (3,"b")]) == (Just "b", singleton 5 "a")
gr</pre>
updateLookupWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> (Maybe a, IntMap a)

-- | <i>O(min(n,W))</i>. The expression (<tt><a>alter</a> f k map</tt>)
gralters the value <tt>x</tt> at <tt>k</tt>, or absence thereof.
gr<a>alter</a> can be used to insert, delete, or update a value in an
gr<a>IntMap</a>. In short : <tt><a>lookup</a> k (<a>alter</a> f k m) = f
gr(<a>lookup</a> k m)</tt>.
alter :: (Maybe a -> Maybe a) -> Key -> IntMap a -> IntMap a

-- | <i>O(log n)</i>. The expression (<tt><a>alterF</a> f k map</tt>)
gralters the value <tt>x</tt> at <tt>k</tt>, or absence thereof.
gr<a>alterF</a> can be used to inspect, insert, delete, or update a
grvalue in an <a>IntMap</a>. In short : <tt><a>lookup</a> k <a>$</a>
gr<a>alterF</a> f k m = f (<a>lookup</a> k m)</tt>.
gr
grExample:
gr
gr<pre>
grinteractiveAlter :: Int -&gt; IntMap String -&gt; IO (IntMap String)
grinteractiveAlter k m = alterF f k m where
gr  f Nothing -&gt; do
gr     putStrLn $ show k ++
gr         " was not found in the map. Would you like to add it?"
gr     getUserResponse1 :: IO (Maybe String)
gr  f (Just old) -&gt; do
gr     putStrLn "The key is currently bound to " ++ show old ++
gr         ". Would you like to change or delete it?"
gr     getUserresponse2 :: IO (Maybe String)
gr</pre>
gr
gr<a>alterF</a> is the most general operation for working with an
grindividual key that may or may not be in a given map.
gr
grNote: <a>alterF</a> is a flipped version of the <tt>at</tt> combinator
grfrom <a>At</a>.
alterF :: Functor f => (Maybe a -> f (Maybe a)) -> Key -> IntMap a -> f (IntMap a)

-- | <i>O(n+m)</i>. The (left-biased) union of two maps. It prefers the
grfirst map when duplicate keys are encountered, i.e. (<tt><a>union</a>
gr== <a>unionWith</a> <a>const</a></tt>).
gr
gr<pre>
grunion (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "a"), (7, "C")]
gr</pre>
union :: IntMap a -> IntMap a -> IntMap a

-- | <i>O(n+m)</i>. The union with a combining function.
gr
gr<pre>
grunionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "aA"), (7, "C")]
gr</pre>
unionWith :: (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a

-- | <i>O(n+m)</i>. The union with a combining function.
gr
gr<pre>
grlet f key left_value right_value = (show key) ++ ":" ++ left_value ++ "|" ++ right_value
grunionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "5:a|A"), (7, "C")]
gr</pre>
unionWithKey :: (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a

-- | The union of a list of maps.
gr
gr<pre>
grunions [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
gr    == fromList [(3, "b"), (5, "a"), (7, "C")]
grunions [(fromList [(5, "A3"), (3, "B3")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "a"), (3, "b")])]
gr    == fromList [(3, "B3"), (5, "A3"), (7, "C")]
gr</pre>
unions :: [IntMap a] -> IntMap a

-- | The union of a list of maps, with a combining operation.
gr
gr<pre>
grunionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
gr    == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")]
gr</pre>
unionsWith :: (a -> a -> a) -> [IntMap a] -> IntMap a

-- | <i>O(n+m)</i>. Difference between two maps (based on keys).
gr
gr<pre>
grdifference (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 3 "b"
gr</pre>
difference :: IntMap a -> IntMap b -> IntMap a

-- | <i>O(n+m)</i>. Difference with a combining function.
gr
gr<pre>
grlet f al ar = if al == "b" then Just (al ++ ":" ++ ar) else Nothing
grdifferenceWith f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (7, "C")])
gr    == singleton 3 "b:B"
gr</pre>
differenceWith :: (a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a

-- | <i>O(n+m)</i>. Difference with a combining function. When two equal
grkeys are encountered, the combining function is applied to the key and
grboth values. If it returns <a>Nothing</a>, the element is discarded
gr(proper set difference). If it returns (<tt><a>Just</a> y</tt>), the
grelement is updated with a new value <tt>y</tt>.
gr
gr<pre>
grlet f k al ar = if al == "b" then Just ((show k) ++ ":" ++ al ++ "|" ++ ar) else Nothing
grdifferenceWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (10, "C")])
gr    == singleton 3 "3:b|B"
gr</pre>
differenceWithKey :: (Key -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a

-- | <i>O(n+m)</i>. The (left-biased) intersection of two maps (based on
grkeys).
gr
gr<pre>
grintersection (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "a"
gr</pre>
intersection :: IntMap a -> IntMap b -> IntMap a

-- | <i>O(n+m)</i>. The intersection with a combining function.
gr
gr<pre>
grintersectionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "aA"
gr</pre>
intersectionWith :: (a -> b -> c) -> IntMap a -> IntMap b -> IntMap c

-- | <i>O(n+m)</i>. The intersection with a combining function.
gr
gr<pre>
grlet f k al ar = (show k) ++ ":" ++ al ++ "|" ++ ar
grintersectionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "5:a|A"
gr</pre>
intersectionWithKey :: (Key -> a -> b -> c) -> IntMap a -> IntMap b -> IntMap c

-- | A tactic for dealing with keys present in one map but not the other in
gr<a>merge</a>.
gr
grA tactic of type <tt>SimpleWhenMissing x z</tt> is an abstract
grrepresentation of a function of type <tt>Key -&gt; x -&gt; Maybe
grz</tt>.
type SimpleWhenMissing = WhenMissing Identity

-- | A tactic for dealing with keys present in both maps in <a>merge</a>.
gr
grA tactic of type <tt>SimpleWhenMatched x y z</tt> is an abstract
grrepresentation of a function of type <tt>Key -&gt; x -&gt; y -&gt;
grMaybe z</tt>.
type SimpleWhenMatched = WhenMatched Identity

-- | Along with zipWithMaybeAMatched, witnesses the isomorphism between
gr<tt>WhenMatched f x y z</tt> and <tt>Key -&gt; x -&gt; y -&gt; f
gr(Maybe z)</tt>.
runWhenMatched :: WhenMatched f x y z -> Key -> x -> y -> f (Maybe z)

-- | Along with traverseMaybeMissing, witnesses the isomorphism between
gr<tt>WhenMissing f x y</tt> and <tt>Key -&gt; x -&gt; f (Maybe y)</tt>.
runWhenMissing :: WhenMissing f x y -> Key -> x -> f (Maybe y)

-- | Merge two maps.
gr
gr<tt>merge</tt> takes two <a>WhenMissing</a> tactics, a
gr<a>WhenMatched</a> tactic and two maps. It uses the tactics to merge
grthe maps. Its behavior is best understood via its fundamental tactics,
gr<a>mapMaybeMissing</a> and <a>zipWithMaybeMatched</a>.
gr
grConsider
gr
gr<pre>
grmerge (mapMaybeMissing g1)
gr             (mapMaybeMissing g2)
gr             (zipWithMaybeMatched f)
gr             m1 m2
gr</pre>
gr
grTake, for example,
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>), (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 = [(1, "one"), (2, "two"), (4, "three")]
gr</pre>
gr
gr<tt>merge</tt> will first '<tt>align'</tt> these maps by key:
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>),               (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 =           [(1, "one"), (2, "two"),          (4, "three")]
gr</pre>
gr
grIt will then pass the individual entries and pairs of entries to
gr<tt>g1</tt>, <tt>g2</tt>, or <tt>f</tt> as appropriate:
gr
gr<pre>
grmaybes = [g1 0 <tt>a</tt>, f 1 <tt>b</tt> "one", g2 2 "two", g1 3 <tt>c</tt>, f 4 <tt>d</tt> "three"]
gr</pre>
gr
grThis produces a <a>Maybe</a> for each key:
gr
gr<pre>
grkeys =     0        1          2           3        4
grresults = [Nothing, Just True, Just False, Nothing, Just True]
gr</pre>
gr
grFinally, the <tt>Just</tt> results are collected into a map:
gr
gr<pre>
grreturn value = [(1, True), (2, False), (4, True)]
gr</pre>
gr
grThe other tactics below are optimizations or simplifications of
gr<a>mapMaybeMissing</a> for special cases. Most importantly,
gr
gr<ul>
gr<li><a>dropMissing</a> drops all the keys.</li>
gr<li><a>preserveMissing</a> leaves all the entries alone.</li>
gr</ul>
gr
grWhen <a>merge</a> is given three arguments, it is inlined at the call
grsite. To prevent excessive inlining, you should typically use
gr<a>merge</a> to define your custom combining functions.
gr
grExamples:
gr
gr<pre>
grunionWithKey f = merge preserveMissing preserveMissing (zipWithMatched f)
gr</pre>
gr
gr<pre>
grintersectionWithKey f = merge dropMissing dropMissing (zipWithMatched f)
gr</pre>
gr
gr<pre>
grdifferenceWith f = merge diffPreserve diffDrop f
gr</pre>
gr
gr<pre>
grsymmetricDifference = merge diffPreserve diffPreserve (\ _ _ _ -&gt; Nothing)
gr</pre>
gr
gr<pre>
grmapEachPiece f g h = merge (diffMapWithKey f) (diffMapWithKey g)
gr</pre>
merge :: SimpleWhenMissing a c -> SimpleWhenMissing b c -> SimpleWhenMatched a b c -> IntMap a -> IntMap b -> IntMap c

-- | When a key is found in both maps, apply a function to the key and
grvalues and maybe use the result in the merged map.
gr
gr<pre>
grzipWithMaybeMatched
gr  :: (Key -&gt; x -&gt; y -&gt; Maybe z)
gr  -&gt; SimpleWhenMatched x y z
gr</pre>
zipWithMaybeMatched :: Applicative f => (Key -> x -> y -> Maybe z) -> WhenMatched f x y z

-- | When a key is found in both maps, apply a function to the key and
grvalues and use the result in the merged map.
gr
gr<pre>
grzipWithMatched
gr  :: (Key -&gt; x -&gt; y -&gt; z)
gr  -&gt; SimpleWhenMatched x y z
gr</pre>
zipWithMatched :: Applicative f => (Key -> x -> y -> z) -> WhenMatched f x y z

-- | Map over the entries whose keys are missing from the other map,
groptionally removing some. This is the most powerful
gr<a>SimpleWhenMissing</a> tactic, but others are usually more
grefficient.
gr
gr<pre>
grmapMaybeMissing :: (Key -&gt; x -&gt; Maybe y) -&gt; SimpleWhenMissing x y
gr</pre>
gr
gr<pre>
grmapMaybeMissing f = traverseMaybeMissing (\k x -&gt; pure (f k x))
gr</pre>
gr
grbut <tt>mapMaybeMissing</tt> uses fewer unnecessary <a>Applicative</a>
groperations.
mapMaybeMissing :: Applicative f => (Key -> x -> Maybe y) -> WhenMissing f x y

-- | Drop all the entries whose keys are missing from the other map.
gr
gr<pre>
grdropMissing :: SimpleWhenMissing x y
gr</pre>
gr
gr<pre>
grdropMissing = mapMaybeMissing (\_ _ -&gt; Nothing)
gr</pre>
gr
grbut <tt>dropMissing</tt> is much faster.
dropMissing :: Applicative f => WhenMissing f x y

-- | Preserve, unchanged, the entries whose keys are missing from the other
grmap.
gr
gr<pre>
grpreserveMissing :: SimpleWhenMissing x x
gr</pre>
gr
gr<pre>
grpreserveMissing = Merge.Lazy.mapMaybeMissing (\_ x -&gt; Just x)
gr</pre>
gr
grbut <tt>preserveMissing</tt> is much faster.
preserveMissing :: Applicative f => WhenMissing f x x

-- | Map over the entries whose keys are missing from the other map.
gr
gr<pre>
grmapMissing :: (k -&gt; x -&gt; y) -&gt; SimpleWhenMissing x y
gr</pre>
gr
gr<pre>
grmapMissing f = mapMaybeMissing (\k x -&gt; Just $ f k x)
gr</pre>
gr
grbut <tt>mapMissing</tt> is somewhat faster.
mapMissing :: Applicative f => (Key -> x -> y) -> WhenMissing f x y

-- | Filter the entries whose keys are missing from the other map.
gr
gr<pre>
grfilterMissing :: (k -&gt; x -&gt; Bool) -&gt; SimpleWhenMissing x x
gr</pre>
gr
gr<pre>
grfilterMissing f = Merge.Lazy.mapMaybeMissing $ \k x -&gt; guard (f k x) *&gt; Just x
gr</pre>
gr
grbut this should be a little faster.
filterMissing :: Applicative f => (Key -> x -> Bool) -> WhenMissing f x x

-- | A tactic for dealing with keys present in one map but not the other in
gr<a>merge</a> or <a>mergeA</a>.
gr
grA tactic of type <tt>WhenMissing f k x z</tt> is an abstract
grrepresentation of a function of type <tt>Key -&gt; x -&gt; f (Maybe
grz)</tt>.
data WhenMissing f x y
WhenMissing :: IntMap x -> f (IntMap y) -> Key -> x -> f (Maybe y) -> WhenMissing f x y
[missingSubtree] :: WhenMissing f x y -> IntMap x -> f (IntMap y)
[missingKey] :: WhenMissing f x y -> Key -> x -> f (Maybe y)

-- | A tactic for dealing with keys present in both maps in <a>merge</a> or
gr<a>mergeA</a>.
gr
grA tactic of type <tt>WhenMatched f x y z</tt> is an abstract
grrepresentation of a function of type <tt>Key -&gt; x -&gt; y -&gt; f
gr(Maybe z)</tt>.
newtype WhenMatched f x y z
WhenMatched :: Key -> x -> y -> f (Maybe z) -> WhenMatched f x y z
[matchedKey] :: WhenMatched f x y z -> Key -> x -> y -> f (Maybe z)

-- | An applicative version of <a>merge</a>.
gr
gr<tt>mergeA</tt> takes two <a>WhenMissing</a> tactics, a
gr<a>WhenMatched</a> tactic and two maps. It uses the tactics to merge
grthe maps. Its behavior is best understood via its fundamental tactics,
gr<a>traverseMaybeMissing</a> and <a>zipWithMaybeAMatched</a>.
gr
grConsider
gr
gr<pre>
grmergeA (traverseMaybeMissing g1)
gr              (traverseMaybeMissing g2)
gr              (zipWithMaybeAMatched f)
gr              m1 m2
gr</pre>
gr
grTake, for example,
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>), (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 = [(1, "one"), (2, "two"), (4, "three")]
gr</pre>
gr
gr<tt>mergeA</tt> will first '<tt>align'</tt> these maps by key:
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>),               (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 =           [(1, "one"), (2, "two"),          (4, "three")]
gr</pre>
gr
grIt will then pass the individual entries and pairs of entries to
gr<tt>g1</tt>, <tt>g2</tt>, or <tt>f</tt> as appropriate:
gr
gr<pre>
gractions = [g1 0 <tt>a</tt>, f 1 <tt>b</tt> "one", g2 2 "two", g1 3 <tt>c</tt>, f 4 <tt>d</tt> "three"]
gr</pre>
gr
grNext, it will perform the actions in the <tt>actions</tt> list in
grorder from left to right.
gr
gr<pre>
grkeys =     0        1          2           3        4
grresults = [Nothing, Just True, Just False, Nothing, Just True]
gr</pre>
gr
grFinally, the <tt>Just</tt> results are collected into a map:
gr
gr<pre>
grreturn value = [(1, True), (2, False), (4, True)]
gr</pre>
gr
grThe other tactics below are optimizations or simplifications of
gr<a>traverseMaybeMissing</a> for special cases. Most importantly,
gr
gr<ul>
gr<li><a>dropMissing</a> drops all the keys.</li>
gr<li><a>preserveMissing</a> leaves all the entries alone.</li>
gr<li><a>mapMaybeMissing</a> does not use the <a>Applicative</a>
grcontext.</li>
gr</ul>
gr
grWhen <a>mergeA</a> is given three arguments, it is inlined at the call
grsite. To prevent excessive inlining, you should generally only use
gr<a>mergeA</a> to define custom combining functions.
mergeA :: (Applicative f) => WhenMissing f a c -> WhenMissing f b c -> WhenMatched f a b c -> IntMap a -> IntMap b -> f (IntMap c)

-- | When a key is found in both maps, apply a function to the key and
grvalues, perform the resulting action, and maybe use the result in the
grmerged map.
gr
grThis is the fundamental <a>WhenMatched</a> tactic.
zipWithMaybeAMatched :: (Key -> x -> y -> f (Maybe z)) -> WhenMatched f x y z

-- | When a key is found in both maps, apply a function to the key and
grvalues to produce an action and use its result in the merged map.
zipWithAMatched :: Applicative f => (Key -> x -> y -> f z) -> WhenMatched f x y z

-- | Traverse over the entries whose keys are missing from the other map,
groptionally producing values to put in the result. This is the most
grpowerful <a>WhenMissing</a> tactic, but others are usually more
grefficient.
traverseMaybeMissing :: Applicative f => (Key -> x -> f (Maybe y)) -> WhenMissing f x y

-- | Traverse over the entries whose keys are missing from the other map.
traverseMissing :: Applicative f => (Key -> x -> f y) -> WhenMissing f x y

-- | Filter the entries whose keys are missing from the other map using
grsome <a>Applicative</a> action.
gr
gr<pre>
grfilterAMissing f = Merge.Lazy.traverseMaybeMissing $
gr  \k x -&gt; (\b -&gt; guard b *&gt; Just x) &lt;$&gt; f k x
gr</pre>
gr
grbut this should be a little faster.
filterAMissing :: Applicative f => (Key -> x -> f Bool) -> WhenMissing f x x

-- | <i>O(n+m)</i>. A high-performance universal combining function. Using
gr<a>mergeWithKey</a>, all combining functions can be defined without
grany loss of efficiency (with exception of <a>union</a>,
gr<a>difference</a> and <a>intersection</a>, where sharing of some nodes
gris lost with <a>mergeWithKey</a>).
gr
grPlease make sure you know what is going on when using
gr<a>mergeWithKey</a>, otherwise you can be surprised by unexpected code
grgrowth or even corruption of the data structure.
gr
grWhen <a>mergeWithKey</a> is given three arguments, it is inlined to
grthe call site. You should therefore use <a>mergeWithKey</a> only to
grdefine your custom combining functions. For example, you could define
gr<a>unionWithKey</a>, <a>differenceWithKey</a> and
gr<a>intersectionWithKey</a> as
gr
gr<pre>
grmyUnionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -&gt; Just (f k x1 x2)) id id m1 m2
grmyDifferenceWithKey f m1 m2 = mergeWithKey f id (const empty) m1 m2
grmyIntersectionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -&gt; Just (f k x1 x2)) (const empty) (const empty) m1 m2
gr</pre>
gr
grWhen calling <tt><a>mergeWithKey</a> combine only1 only2</tt>, a
grfunction combining two <a>IntMap</a>s is created, such that
gr
gr<ul>
gr<li>if a key is present in both maps, it is passed with both
grcorresponding values to the <tt>combine</tt> function. Depending on
grthe result, the key is either present in the result with specified
grvalue, or is left out;</li>
gr<li>a nonempty subtree present only in the first map is passed to
gr<tt>only1</tt> and the output is added to the result;</li>
gr<li>a nonempty subtree present only in the second map is passed to
gr<tt>only2</tt> and the output is added to the result.</li>
gr</ul>
gr
grThe <tt>only1</tt> and <tt>only2</tt> methods <i>must return a map
grwith a subset (possibly empty) of the keys of the given map</i>. The
grvalues can be modified arbitrarily. Most common variants of
gr<tt>only1</tt> and <tt>only2</tt> are <a>id</a> and <tt><a>const</a>
gr<a>empty</a></tt>, but for example <tt><a>map</a> f</tt> or
gr<tt><a>filterWithKey</a> f</tt> could be used for any <tt>f</tt>.
mergeWithKey :: (Key -> a -> b -> Maybe c) -> (IntMap a -> IntMap c) -> (IntMap b -> IntMap c) -> IntMap a -> IntMap b -> IntMap c
mergeWithKey' :: (Prefix -> Mask -> IntMap c -> IntMap c -> IntMap c) -> (IntMap a -> IntMap b -> IntMap c) -> (IntMap a -> IntMap c) -> (IntMap b -> IntMap c) -> IntMap a -> IntMap b -> IntMap c

-- | <i>O(n)</i>. Map a function over all values in the map.
gr
gr<pre>
grmap (++ "x") (fromList [(5,"a"), (3,"b")]) == fromList [(3, "bx"), (5, "ax")]
gr</pre>
map :: (a -> b) -> IntMap a -> IntMap b

-- | <i>O(n)</i>. Map a function over all values in the map.
gr
gr<pre>
grlet f key x = (show key) ++ ":" ++ x
grmapWithKey f (fromList [(5,"a"), (3,"b")]) == fromList [(3, "3:b"), (5, "5:a")]
gr</pre>
mapWithKey :: (Key -> a -> b) -> IntMap a -> IntMap b

-- | <i>O(n)</i>. <tt><a>traverseWithKey</a> f s == <a>fromList</a>
gr<a>$</a> <a>traverse</a> ((k, v) -&gt; (,) k <a>$</a> f k v)
gr(<a>toList</a> m)</tt> That is, behaves exactly like a regular
gr<a>traverse</a> except that the traversing function also has access to
grthe key associated with a value.
gr
gr<pre>
grtraverseWithKey (\k v -&gt; if odd k then Just (succ v) else Nothing) (fromList [(1, 'a'), (5, 'e')]) == Just (fromList [(1, 'b'), (5, 'f')])
grtraverseWithKey (\k v -&gt; if odd k then Just (succ v) else Nothing) (fromList [(2, 'c')])           == Nothing
gr</pre>
traverseWithKey :: Applicative t => (Key -> a -> t b) -> IntMap a -> t (IntMap b)

-- | <i>O(n)</i>. The function <tt><a>mapAccum</a></tt> threads an
graccumulating argument through the map in ascending order of keys.
gr
gr<pre>
grlet f a b = (a ++ b, b ++ "X")
grmapAccum f "Everything: " (fromList [(5,"a"), (3,"b")]) == ("Everything: ba", fromList [(3, "bX"), (5, "aX")])
gr</pre>
mapAccum :: (a -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)

-- | <i>O(n)</i>. The function <tt><a>mapAccumWithKey</a></tt> threads an
graccumulating argument through the map in ascending order of keys.
gr
gr<pre>
grlet f a k b = (a ++ " " ++ (show k) ++ "-" ++ b, b ++ "X")
grmapAccumWithKey f "Everything:" (fromList [(5,"a"), (3,"b")]) == ("Everything: 3-b 5-a", fromList [(3, "bX"), (5, "aX")])
gr</pre>
mapAccumWithKey :: (a -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)

-- | <i>O(n)</i>. The function <tt><tt>mapAccumR</tt></tt> threads an
graccumulating argument through the map in descending order of keys.
mapAccumRWithKey :: (a -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)

-- | <i>O(n*min(n,W))</i>. <tt><a>mapKeys</a> f s</tt> is the map obtained
grby applying <tt>f</tt> to each key of <tt>s</tt>.
gr
grThe size of the result may be smaller if <tt>f</tt> maps two or more
grdistinct keys to the same new key. In this case the value at the
grgreatest of the original keys is retained.
gr
gr<pre>
grmapKeys (+ 1) (fromList [(5,"a"), (3,"b")])                        == fromList [(4, "b"), (6, "a")]
grmapKeys (\ _ -&gt; 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "c"
grmapKeys (\ _ -&gt; 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "c"
gr</pre>
mapKeys :: (Key -> Key) -> IntMap a -> IntMap a

-- | <i>O(n*min(n,W))</i>. <tt><a>mapKeysWith</a> c f s</tt> is the map
grobtained by applying <tt>f</tt> to each key of <tt>s</tt>.
gr
grThe size of the result may be smaller if <tt>f</tt> maps two or more
grdistinct keys to the same new key. In this case the associated values
grwill be combined using <tt>c</tt>.
gr
gr<pre>
grmapKeysWith (++) (\ _ -&gt; 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "cdab"
grmapKeysWith (++) (\ _ -&gt; 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "cdab"
gr</pre>
mapKeysWith :: (a -> a -> a) -> (Key -> Key) -> IntMap a -> IntMap a

-- | <i>O(n*min(n,W))</i>. <tt><a>mapKeysMonotonic</a> f s ==
gr<a>mapKeys</a> f s</tt>, but works only when <tt>f</tt> is strictly
grmonotonic. That is, for any values <tt>x</tt> and <tt>y</tt>, if
gr<tt>x</tt> &lt; <tt>y</tt> then <tt>f x</tt> &lt; <tt>f y</tt>. <i>The
grprecondition is not checked.</i> Semi-formally, we have:
gr
gr<pre>
grand [x &lt; y ==&gt; f x &lt; f y | x &lt;- ls, y &lt;- ls]
gr                    ==&gt; mapKeysMonotonic f s == mapKeys f s
gr    where ls = keys s
gr</pre>
gr
grThis means that <tt>f</tt> maps distinct original keys to distinct
grresulting keys. This function has slightly better performance than
gr<a>mapKeys</a>.
gr
gr<pre>
grmapKeysMonotonic (\ k -&gt; k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")]
gr</pre>
mapKeysMonotonic :: (Key -> Key) -> IntMap a -> IntMap a

-- | <i>O(n)</i>. Fold the values in the map using the given
grright-associative binary operator, such that <tt><a>foldr</a> f z ==
gr<a>foldr</a> f z . <a>elems</a></tt>.
gr
grFor example,
gr
gr<pre>
grelems map = foldr (:) [] map
gr</pre>
gr
gr<pre>
grlet f a len = len + (length a)
grfoldr f 0 (fromList [(5,"a"), (3,"bbb")]) == 4
gr</pre>
foldr :: (a -> b -> b) -> b -> IntMap a -> b

-- | <i>O(n)</i>. Fold the values in the map using the given
grleft-associative binary operator, such that <tt><a>foldl</a> f z ==
gr<a>foldl</a> f z . <a>elems</a></tt>.
gr
grFor example,
gr
gr<pre>
grelems = reverse . foldl (flip (:)) []
gr</pre>
gr
gr<pre>
grlet f len a = len + (length a)
grfoldl f 0 (fromList [(5,"a"), (3,"bbb")]) == 4
gr</pre>
foldl :: (a -> b -> a) -> a -> IntMap b -> a

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grright-associative binary operator, such that <tt><a>foldrWithKey</a> f
grz == <a>foldr</a> (<a>uncurry</a> f) z . <a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grkeys map = foldrWithKey (\k x ks -&gt; k:ks) [] map
gr</pre>
gr
gr<pre>
grlet f k a result = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")"
grfoldrWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (5:a)(3:b)"
gr</pre>
foldrWithKey :: (Key -> a -> b -> b) -> b -> IntMap a -> b

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grleft-associative binary operator, such that <tt><a>foldlWithKey</a> f
grz == <a>foldl</a> (\z' (kx, x) -&gt; f z' kx x) z .
gr<a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grkeys = reverse . foldlWithKey (\ks k x -&gt; k:ks) []
gr</pre>
gr
gr<pre>
grlet f result k a = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")"
grfoldlWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (3:b)(5:a)"
gr</pre>
foldlWithKey :: (a -> Key -> b -> a) -> a -> IntMap b -> a

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grmonoid, such that
gr
gr<pre>
gr<a>foldMapWithKey</a> f = <a>fold</a> . <a>mapWithKey</a> f
gr</pre>
gr
grThis can be an asymptotically faster than <a>foldrWithKey</a> or
gr<a>foldlWithKey</a> for some monoids.
foldMapWithKey :: Monoid m => (Key -> a -> m) -> IntMap a -> m

-- | <i>O(n)</i>. A strict version of <a>foldr</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldr' :: (a -> b -> b) -> b -> IntMap a -> b

-- | <i>O(n)</i>. A strict version of <a>foldl</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldl' :: (a -> b -> a) -> a -> IntMap b -> a

-- | <i>O(n)</i>. A strict version of <a>foldrWithKey</a>. Each application
grof the operator is evaluated before using the result in the next
grapplication. This function is strict in the starting value.
foldrWithKey' :: (Key -> a -> b -> b) -> b -> IntMap a -> b

-- | <i>O(n)</i>. A strict version of <a>foldlWithKey</a>. Each application
grof the operator is evaluated before using the result in the next
grapplication. This function is strict in the starting value.
foldlWithKey' :: (a -> Key -> b -> a) -> a -> IntMap b -> a

-- | <i>O(n)</i>. Return all elements of the map in the ascending order of
grtheir keys. Subject to list fusion.
gr
gr<pre>
grelems (fromList [(5,"a"), (3,"b")]) == ["b","a"]
grelems empty == []
gr</pre>
elems :: IntMap a -> [a]

-- | <i>O(n)</i>. Return all keys of the map in ascending order. Subject to
grlist fusion.
gr
gr<pre>
grkeys (fromList [(5,"a"), (3,"b")]) == [3,5]
grkeys empty == []
gr</pre>
keys :: IntMap a -> [Key]

-- | <i>O(n)</i>. An alias for <a>toAscList</a>. Returns all key/value
grpairs in the map in ascending key order. Subject to list fusion.
gr
gr<pre>
grassocs (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
grassocs empty == []
gr</pre>
assocs :: IntMap a -> [(Key, a)]

-- | <i>O(n*min(n,W))</i>. The set of all keys of the map.
gr
gr<pre>
grkeysSet (fromList [(5,"a"), (3,"b")]) == Data.IntSet.fromList [3,5]
grkeysSet empty == Data.IntSet.empty
gr</pre>
keysSet :: IntMap a -> IntSet

-- | <i>O(n)</i>. Build a map from a set of keys and a function which for
greach key computes its value.
gr
gr<pre>
grfromSet (\k -&gt; replicate k 'a') (Data.IntSet.fromList [3, 5]) == fromList [(5,"aaaaa"), (3,"aaa")]
grfromSet undefined Data.IntSet.empty == empty
gr</pre>
fromSet :: (Key -> a) -> IntSet -> IntMap a

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs. Subject to
grlist fusion.
gr
gr<pre>
grtoList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
grtoList empty == []
gr</pre>
toList :: IntMap a -> [(Key, a)]

-- | <i>O(n*min(n,W))</i>. Create a map from a list of key/value pairs.
gr
gr<pre>
grfromList [] == empty
grfromList [(5,"a"), (3,"b"), (5, "c")] == fromList [(5,"c"), (3,"b")]
grfromList [(5,"c"), (3,"b"), (5, "a")] == fromList [(5,"a"), (3,"b")]
gr</pre>
fromList :: [(Key, a)] -> IntMap a

-- | <i>O(n*min(n,W))</i>. Create a map from a list of key/value pairs with
gra combining function. See also <a>fromAscListWith</a>.
gr
gr<pre>
grfromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"c")] == fromList [(3, "ab"), (5, "cba")]
grfromListWith (++) [] == empty
gr</pre>
fromListWith :: (a -> a -> a) -> [(Key, a)] -> IntMap a

-- | <i>O(n*min(n,W))</i>. Build a map from a list of key/value pairs with
gra combining function. See also fromAscListWithKey'.
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grfromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"c")] == fromList [(3, "3:a|b"), (5, "5:c|5:b|a")]
grfromListWithKey f [] == empty
gr</pre>
fromListWithKey :: (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs where the
grkeys are in ascending order. Subject to list fusion.
gr
gr<pre>
grtoAscList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
gr</pre>
toAscList :: IntMap a -> [(Key, a)]

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs where the
grkeys are in descending order. Subject to list fusion.
gr
gr<pre>
grtoDescList (fromList [(5,"a"), (3,"b")]) == [(5,"a"), (3,"b")]
gr</pre>
toDescList :: IntMap a -> [(Key, a)]

-- | <i>O(n)</i>. Build a map from a list of key/value pairs where the keys
grare in ascending order.
gr
gr<pre>
grfromAscList [(3,"b"), (5,"a")]          == fromList [(3, "b"), (5, "a")]
grfromAscList [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "b")]
gr</pre>
fromAscList :: [(Key, a)] -> IntMap a

-- | <i>O(n)</i>. Build a map from a list of key/value pairs where the keys
grare in ascending order, with a combining function on equal keys.
gr<i>The precondition (input list is ascending) is not checked.</i>
gr
gr<pre>
grfromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")]
gr</pre>
fromAscListWith :: (a -> a -> a) -> [(Key, a)] -> IntMap a

-- | <i>O(n)</i>. Build a map from a list of key/value pairs where the keys
grare in ascending order, with a combining function on equal keys.
gr<i>The precondition (input list is ascending) is not checked.</i>
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grfromAscListWithKey f [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "5:b|a")]
gr</pre>
fromAscListWithKey :: (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a

-- | <i>O(n)</i>. Build a map from a list of key/value pairs where the keys
grare in ascending order and all distinct. <i>The precondition (input
grlist is strictly ascending) is not checked.</i>
gr
gr<pre>
grfromDistinctAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")]
gr</pre>
fromDistinctAscList :: forall a. [(Key, a)] -> IntMap a

-- | <i>O(n)</i>. Filter all values that satisfy some predicate.
gr
gr<pre>
grfilter (&gt; "a") (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grfilter (&gt; "x") (fromList [(5,"a"), (3,"b")]) == empty
grfilter (&lt; "a") (fromList [(5,"a"), (3,"b")]) == empty
gr</pre>
filter :: (a -> Bool) -> IntMap a -> IntMap a

-- | <i>O(n)</i>. Filter all keys/values that satisfy some predicate.
gr
gr<pre>
grfilterWithKey (\k _ -&gt; k &gt; 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
filterWithKey :: (Key -> a -> Bool) -> IntMap a -> IntMap a

-- | <i>O(n+m)</i>. The restriction of a map to the keys in a set.
gr
gr<pre>
grm <a>restrictKeys</a> s = <a>filterWithKey</a> (k _ -&gt; k `<a>member'</a> s) m
gr</pre>
restrictKeys :: IntMap a -> IntSet -> IntMap a

-- | <i>O(n+m)</i>. Remove all the keys in a given set from a map.
gr
gr<pre>
grm <a>withoutKeys</a> s = <a>filterWithKey</a> (k _ -&gt; k `<a>notMember'</a> s) m
gr</pre>
withoutKeys :: IntMap a -> IntSet -> IntMap a

-- | <i>O(n)</i>. Partition the map according to some predicate. The first
grmap contains all elements that satisfy the predicate, the second all
grelements that fail the predicate. See also <a>split</a>.
gr
gr<pre>
grpartition (&gt; "a") (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a")
grpartition (&lt; "x") (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
grpartition (&gt; "x") (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
gr</pre>
partition :: (a -> Bool) -> IntMap a -> (IntMap a, IntMap a)

-- | <i>O(n)</i>. Partition the map according to some predicate. The first
grmap contains all elements that satisfy the predicate, the second all
grelements that fail the predicate. See also <a>split</a>.
gr
gr<pre>
grpartitionWithKey (\ k _ -&gt; k &gt; 3) (fromList [(5,"a"), (3,"b")]) == (singleton 5 "a", singleton 3 "b")
grpartitionWithKey (\ k _ -&gt; k &lt; 7) (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
grpartitionWithKey (\ k _ -&gt; k &gt; 7) (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
gr</pre>
partitionWithKey :: (Key -> a -> Bool) -> IntMap a -> (IntMap a, IntMap a)

-- | <i>O(n)</i>. Map values and collect the <a>Just</a> results.
gr
gr<pre>
grlet f x = if x == "a" then Just "new a" else Nothing
grmapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"
gr</pre>
mapMaybe :: (a -> Maybe b) -> IntMap a -> IntMap b

-- | <i>O(n)</i>. Map keys/values and collect the <a>Just</a> results.
gr
gr<pre>
grlet f k _ = if k &lt; 5 then Just ("key : " ++ (show k)) else Nothing
grmapMaybeWithKey f (fromList [(5,"a"), (3,"b")]) == singleton 3 "key : 3"
gr</pre>
mapMaybeWithKey :: (Key -> a -> Maybe b) -> IntMap a -> IntMap b

-- | <i>O(n)</i>. Map values and separate the <a>Left</a> and <a>Right</a>
grresults.
gr
gr<pre>
grlet f a = if a &lt; "c" then Left a else Right a
grmapEither f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (fromList [(3,"b"), (5,"a")], fromList [(1,"x"), (7,"z")])
gr
grmapEither (\ a -&gt; Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (empty, fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr</pre>
mapEither :: (a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)

-- | <i>O(n)</i>. Map keys/values and separate the <a>Left</a> and
gr<a>Right</a> results.
gr
gr<pre>
grlet f k a = if k &lt; 5 then Left (k * 2) else Right (a ++ a)
grmapEitherWithKey f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (fromList [(1,2), (3,6)], fromList [(5,"aa"), (7,"zz")])
gr
grmapEitherWithKey (\_ a -&gt; Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (empty, fromList [(1,"x"), (3,"b"), (5,"a"), (7,"z")])
gr</pre>
mapEitherWithKey :: (Key -> a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)

-- | <i>O(min(n,W))</i>. The expression (<tt><a>split</a> k map</tt>) is a
grpair <tt>(map1,map2)</tt> where all keys in <tt>map1</tt> are lower
grthan <tt>k</tt> and all keys in <tt>map2</tt> larger than <tt>k</tt>.
grAny key equal to <tt>k</tt> is found in neither <tt>map1</tt> nor
gr<tt>map2</tt>.
gr
gr<pre>
grsplit 2 (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3,"b"), (5,"a")])
grsplit 3 (fromList [(5,"a"), (3,"b")]) == (empty, singleton 5 "a")
grsplit 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a")
grsplit 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", empty)
grsplit 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], empty)
gr</pre>
split :: Key -> IntMap a -> (IntMap a, IntMap a)

-- | <i>O(min(n,W))</i>. Performs a <a>split</a> but also returns whether
grthe pivot key was found in the original map.
gr
gr<pre>
grsplitLookup 2 (fromList [(5,"a"), (3,"b")]) == (empty, Nothing, fromList [(3,"b"), (5,"a")])
grsplitLookup 3 (fromList [(5,"a"), (3,"b")]) == (empty, Just "b", singleton 5 "a")
grsplitLookup 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Nothing, singleton 5 "a")
grsplitLookup 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Just "a", empty)
grsplitLookup 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], Nothing, empty)
gr</pre>
splitLookup :: Key -> IntMap a -> (IntMap a, Maybe a, IntMap a)

-- | <i>O(1)</i>. Decompose a map into pieces based on the structure of the
grunderlying tree. This function is useful for consuming a map in
grparallel.
gr
grNo guarantee is made as to the sizes of the pieces; an internal, but
grdeterministic process determines this. However, it is guaranteed that
grthe pieces returned will be in ascending order (all elements in the
grfirst submap less than all elements in the second, and so on).
gr
grExamples:
gr
gr<pre>
grsplitRoot (fromList (zip [1..6::Int] ['a'..])) ==
gr  [fromList [(1,'a'),(2,'b'),(3,'c')],fromList [(4,'d'),(5,'e'),(6,'f')]]
gr</pre>
gr
gr<pre>
grsplitRoot empty == []
gr</pre>
gr
grNote that the current implementation does not return more than two
grsubmaps, but you should not depend on this behaviour because it can
grchange in the future without notice.
splitRoot :: IntMap a -> [IntMap a]

-- | <i>O(n+m)</i>. Is this a submap? Defined as (<tt><a>isSubmapOf</a> =
gr<a>isSubmapOfBy</a> (==)</tt>).
isSubmapOf :: Eq a => IntMap a -> IntMap a -> Bool

-- | <i>O(n+m)</i>. The expression (<tt><a>isSubmapOfBy</a> f m1 m2</tt>)
grreturns <a>True</a> if all keys in <tt>m1</tt> are in <tt>m2</tt>, and
grwhen <tt>f</tt> returns <a>True</a> when applied to their respective
grvalues. For example, the following expressions are all <a>True</a>:
gr
gr<pre>
grisSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
grisSubmapOfBy (&lt;=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
grisSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])
gr</pre>
gr
grBut the following are all <a>False</a>:
gr
gr<pre>
grisSubmapOfBy (==) (fromList [(1,2)]) (fromList [(1,1),(2,2)])
grisSubmapOfBy (&lt;) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
grisSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
gr</pre>
isSubmapOfBy :: (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool

-- | <i>O(n+m)</i>. Is this a proper submap? (ie. a submap but not equal).
grDefined as (<tt><a>isProperSubmapOf</a> = <a>isProperSubmapOfBy</a>
gr(==)</tt>).
isProperSubmapOf :: Eq a => IntMap a -> IntMap a -> Bool

-- | <i>O(n+m)</i>. Is this a proper submap? (ie. a submap but not equal).
grThe expression (<tt><a>isProperSubmapOfBy</a> f m1 m2</tt>) returns
gr<a>True</a> when <tt>m1</tt> and <tt>m2</tt> are not equal, all keys
grin <tt>m1</tt> are in <tt>m2</tt>, and when <tt>f</tt> returns
gr<a>True</a> when applied to their respective values. For example, the
grfollowing expressions are all <a>True</a>:
gr
gr<pre>
grisProperSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
grisProperSubmapOfBy (&lt;=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
gr</pre>
gr
grBut the following are all <a>False</a>:
gr
gr<pre>
grisProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])
grisProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
grisProperSubmapOfBy (&lt;)  (fromList [(1,1)])       (fromList [(1,1),(2,2)])
gr</pre>
isProperSubmapOfBy :: (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool

-- | <i>O(min(n,W))</i>. The minimal key of the map. Returns <a>Nothing</a>
grif the map is empty.
lookupMin :: IntMap a -> Maybe (Key, a)

-- | <i>O(min(n,W))</i>. The maximal key of the map. Returns <a>Nothing</a>
grif the map is empty.
lookupMax :: IntMap a -> Maybe (Key, a)

-- | <i>O(min(n,W))</i>. The minimal key of the map. Calls <a>error</a> if
grthe map is empty. Use <a>minViewWithKey</a> if the map may be empty.
findMin :: IntMap a -> (Key, a)

-- | <i>O(min(n,W))</i>. The maximal key of the map. Calls <a>error</a> if
grthe map is empty. Use <a>maxViewWithKey</a> if the map may be empty.
findMax :: IntMap a -> (Key, a)

-- | <i>O(min(n,W))</i>. Delete the minimal key. Returns an empty map if
grthe map is empty.
gr
grNote that this is a change of behaviour for consistency with
gr<a>Map</a> – versions prior to 0.5 threw an error if the <a>IntMap</a>
grwas already empty.
deleteMin :: IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Delete the maximal key. Returns an empty map if
grthe map is empty.
gr
grNote that this is a change of behaviour for consistency with
gr<a>Map</a> – versions prior to 0.5 threw an error if the <a>IntMap</a>
grwas already empty.
deleteMax :: IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Delete and find the minimal element. This function
grthrows an error if the map is empty. Use <a>minViewWithKey</a> if the
grmap may be empty.
deleteFindMin :: IntMap a -> ((Key, a), IntMap a)

-- | <i>O(min(n,W))</i>. Delete and find the maximal element. This function
grthrows an error if the map is empty. Use <a>maxViewWithKey</a> if the
grmap may be empty.
deleteFindMax :: IntMap a -> ((Key, a), IntMap a)

-- | <i>O(min(n,W))</i>. Update the value at the minimal key.
gr
gr<pre>
grupdateMin (\ a -&gt; Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "Xb"), (5, "a")]
grupdateMin (\ _ -&gt; Nothing)         (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateMin :: (a -> Maybe a) -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Update the value at the maximal key.
gr
gr<pre>
grupdateMax (\ a -&gt; Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "Xa")]
grupdateMax (\ _ -&gt; Nothing)         (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr</pre>
updateMax :: (a -> Maybe a) -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Update the value at the minimal key.
gr
gr<pre>
grupdateMinWithKey (\ k a -&gt; Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"3:b"), (5,"a")]
grupdateMinWithKey (\ _ _ -&gt; Nothing)                     (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateMinWithKey :: (Key -> a -> Maybe a) -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Update the value at the maximal key.
gr
gr<pre>
grupdateMaxWithKey (\ k a -&gt; Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"b"), (5,"5:a")]
grupdateMaxWithKey (\ _ _ -&gt; Nothing)                     (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr</pre>
updateMaxWithKey :: (Key -> a -> Maybe a) -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Retrieves the minimal key of the map, and the map
grstripped of that element, or <a>Nothing</a> if passed an empty map.
minView :: IntMap a -> Maybe (a, IntMap a)

-- | <i>O(min(n,W))</i>. Retrieves the maximal key of the map, and the map
grstripped of that element, or <a>Nothing</a> if passed an empty map.
maxView :: IntMap a -> Maybe (a, IntMap a)

-- | <i>O(min(n,W))</i>. Retrieves the minimal (key,value) pair of the map,
grand the map stripped of that element, or <a>Nothing</a> if passed an
grempty map.
gr
gr<pre>
grminViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((3,"b"), singleton 5 "a")
grminViewWithKey empty == Nothing
gr</pre>
minViewWithKey :: IntMap a -> Maybe ((Key, a), IntMap a)

-- | <i>O(min(n,W))</i>. Retrieves the maximal (key,value) pair of the map,
grand the map stripped of that element, or <a>Nothing</a> if passed an
grempty map.
gr
gr<pre>
grmaxViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((5,"a"), singleton 3 "b")
grmaxViewWithKey empty == Nothing
gr</pre>
maxViewWithKey :: IntMap a -> Maybe ((Key, a), IntMap a)

-- | <i>O(n)</i>. Show the tree that implements the map. The tree is shown
grin a compressed, hanging format.
showTree :: Show a => IntMap a -> String

-- | <i>O(n)</i>. The expression (<tt><a>showTreeWith</a> hang wide
grmap</tt>) shows the tree that implements the map. If <tt>hang</tt> is
gr<a>True</a>, a <i>hanging</i> tree is shown otherwise a rotated tree
gris shown. If <tt>wide</tt> is <a>True</a>, an extra wide version is
grshown.
showTreeWith :: Show a => Bool -> Bool -> IntMap a -> String
type Mask = Int
type Prefix = Int
type Nat = Word
natFromInt :: Key -> Nat
intFromNat :: Nat -> Key
link :: Prefix -> IntMap a -> Prefix -> IntMap a -> IntMap a
bin :: Prefix -> Mask -> IntMap a -> IntMap a -> IntMap a
binCheckLeft :: Prefix -> Mask -> IntMap a -> IntMap a -> IntMap a
binCheckRight :: Prefix -> Mask -> IntMap a -> IntMap a -> IntMap a

-- | Should this key follow the left subtree of a <a>Bin</a> with switching
grbit <tt>m</tt>? N.B., the answer is only valid when <tt>match i p
grm</tt> is true.
zero :: Key -> Mask -> Bool

-- | Does the key <tt>i</tt> differ from the prefix <tt>p</tt> before
grgetting to the switching bit <tt>m</tt>?
nomatch :: Key -> Prefix -> Mask -> Bool

-- | Does the key <tt>i</tt> match the prefix <tt>p</tt> (up to but not
grincluding bit <tt>m</tt>)?
match :: Key -> Prefix -> Mask -> Bool

-- | The prefix of key <tt>i</tt> up to (but not including) the switching
grbit <tt>m</tt>.
mask :: Key -> Mask -> Prefix

-- | The prefix of key <tt>i</tt> up to (but not including) the switching
grbit <tt>m</tt>.
maskW :: Nat -> Nat -> Prefix

-- | Does the left switching bit specify a shorter prefix?
shorter :: Mask -> Mask -> Bool

-- | The first switching bit where the two prefixes disagree.
branchMask :: Prefix -> Prefix -> Mask

-- | Return a word where only the highest bit is set.
highestBitMask :: Word -> Word

-- | Map covariantly over a <tt><a>WhenMissing</a> f x</tt>.
mapWhenMissing :: (Applicative f, Monad f) => (a -> b) -> WhenMissing f x a -> WhenMissing f x b

-- | Map covariantly over a <tt><a>WhenMatched</a> f x y</tt>.
mapWhenMatched :: Functor f => (a -> b) -> WhenMatched f x y a -> WhenMatched f x y b

-- | Map contravariantly over a <tt><a>WhenMissing</a> f _ x</tt>.
lmapWhenMissing :: (b -> a) -> WhenMissing f a x -> WhenMissing f b x

-- | Map contravariantly over a <tt><a>WhenMatched</a> f _ y z</tt>.
contramapFirstWhenMatched :: (b -> a) -> WhenMatched f a y z -> WhenMatched f b y z

-- | Map contravariantly over a <tt><a>WhenMatched</a> f x _ z</tt>.
contramapSecondWhenMatched :: (b -> a) -> WhenMatched f x a z -> WhenMatched f x b z

-- | Map covariantly over a <tt><a>WhenMissing</a> f x</tt>, using only a
gr'Functor f' constraint.
mapGentlyWhenMissing :: Functor f => (a -> b) -> WhenMissing f x a -> WhenMissing f x b

-- | Map covariantly over a <tt><a>WhenMatched</a> f k x</tt>, using only a
gr'Functor f' constraint.
mapGentlyWhenMatched :: Functor f => (a -> b) -> WhenMatched f x y a -> WhenMatched f x y b
instance GHC.Base.Functor f => GHC.Base.Functor (Data.IntMap.Internal.WhenMatched f x y)
instance (GHC.Base.Monad f, GHC.Base.Applicative f) => Control.Category.Category (Data.IntMap.Internal.WhenMatched f x)
instance (GHC.Base.Monad f, GHC.Base.Applicative f) => GHC.Base.Applicative (Data.IntMap.Internal.WhenMatched f x y)
instance (GHC.Base.Monad f, GHC.Base.Applicative f) => GHC.Base.Monad (Data.IntMap.Internal.WhenMatched f x y)
instance (GHC.Base.Applicative f, GHC.Base.Monad f) => GHC.Base.Functor (Data.IntMap.Internal.WhenMissing f x)
instance (GHC.Base.Applicative f, GHC.Base.Monad f) => Control.Category.Category (Data.IntMap.Internal.WhenMissing f)
instance (GHC.Base.Applicative f, GHC.Base.Monad f) => GHC.Base.Applicative (Data.IntMap.Internal.WhenMissing f x)
instance (GHC.Base.Applicative f, GHC.Base.Monad f) => GHC.Base.Monad (Data.IntMap.Internal.WhenMissing f x)
instance GHC.Base.Monoid (Data.IntMap.Internal.IntMap a)
instance GHC.Base.Semigroup (Data.IntMap.Internal.IntMap a)
instance Data.Foldable.Foldable Data.IntMap.Internal.IntMap
instance Data.Traversable.Traversable Data.IntMap.Internal.IntMap
instance Control.DeepSeq.NFData a => Control.DeepSeq.NFData (Data.IntMap.Internal.IntMap a)
instance Data.Data.Data a => Data.Data.Data (Data.IntMap.Internal.IntMap a)
instance GHC.Exts.IsList (Data.IntMap.Internal.IntMap a)
instance GHC.Classes.Eq a => GHC.Classes.Eq (Data.IntMap.Internal.IntMap a)
instance Data.Functor.Classes.Eq1 Data.IntMap.Internal.IntMap
instance GHC.Classes.Ord a => GHC.Classes.Ord (Data.IntMap.Internal.IntMap a)
instance Data.Functor.Classes.Ord1 Data.IntMap.Internal.IntMap
instance GHC.Base.Functor Data.IntMap.Internal.IntMap
instance GHC.Show.Show a => GHC.Show.Show (Data.IntMap.Internal.IntMap a)
instance Data.Functor.Classes.Show1 Data.IntMap.Internal.IntMap
instance GHC.Read.Read e => GHC.Read.Read (Data.IntMap.Internal.IntMap e)
instance Data.Functor.Classes.Read1 Data.IntMap.Internal.IntMap


-- | This module defines an API for writing functions that merge two maps.
grThe key functions are <a>merge</a> and <a>mergeA</a>. Each of these
grcan be used with several different "merge tactics".
gr
grThe <a>merge</a> and <a>mergeA</a> functions are shared by the lazy
grand strict modules. Only the choice of merge tactics determines
grstrictness. If you use <a>mapMissing</a> from this module then the
grresults will be forced before they are inserted. If you use
gr<a>mapMissing</a> from <a>Data.Map.Merge.Lazy</a> then they will not.
gr
gr<h2>Efficiency note</h2>
gr
grThe <tt>Category</tt>, <a>Applicative</a>, and <a>Monad</a> instances
grfor <a>WhenMissing</a> tactics are included because they are valid.
grHowever, they are inefficient in many cases and should usually be
gravoided. The instances for <a>WhenMatched</a> tactics should not pose
grany major efficiency problems.
module Data.IntMap.Merge.Strict

-- | A tactic for dealing with keys present in one map but not the other in
gr<a>merge</a>.
gr
grA tactic of type <tt>SimpleWhenMissing x z</tt> is an abstract
grrepresentation of a function of type <tt>Key -&gt; x -&gt; Maybe
grz</tt>.
type SimpleWhenMissing = WhenMissing Identity

-- | A tactic for dealing with keys present in both maps in <a>merge</a>.
gr
grA tactic of type <tt>SimpleWhenMatched x y z</tt> is an abstract
grrepresentation of a function of type <tt>Key -&gt; x -&gt; y -&gt;
grMaybe z</tt>.
type SimpleWhenMatched = WhenMatched Identity

-- | Merge two maps.
gr
gr<tt>merge</tt> takes two <a>WhenMissing</a> tactics, a
gr<a>WhenMatched</a> tactic and two maps. It uses the tactics to merge
grthe maps. Its behavior is best understood via its fundamental tactics,
gr<a>mapMaybeMissing</a> and <a>zipWithMaybeMatched</a>.
gr
grConsider
gr
gr<pre>
grmerge (mapMaybeMissing g1)
gr             (mapMaybeMissing g2)
gr             (zipWithMaybeMatched f)
gr             m1 m2
gr</pre>
gr
grTake, for example,
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>), (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 = [(1, "one"), (2, "two"), (4, "three")]
gr</pre>
gr
gr<tt>merge</tt> will first '<tt>align'</tt> these maps by key:
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>),               (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 =           [(1, "one"), (2, "two"),          (4, "three")]
gr</pre>
gr
grIt will then pass the individual entries and pairs of entries to
gr<tt>g1</tt>, <tt>g2</tt>, or <tt>f</tt> as appropriate:
gr
gr<pre>
grmaybes = [g1 0 <tt>a</tt>, f 1 <tt>b</tt> "one", g2 2 "two", g1 3 <tt>c</tt>, f 4 <tt>d</tt> "three"]
gr</pre>
gr
grThis produces a <a>Maybe</a> for each key:
gr
gr<pre>
grkeys =     0        1          2           3        4
grresults = [Nothing, Just True, Just False, Nothing, Just True]
gr</pre>
gr
grFinally, the <tt>Just</tt> results are collected into a map:
gr
gr<pre>
grreturn value = [(1, True), (2, False), (4, True)]
gr</pre>
gr
grThe other tactics below are optimizations or simplifications of
gr<a>mapMaybeMissing</a> for special cases. Most importantly,
gr
gr<ul>
gr<li><a>dropMissing</a> drops all the keys.</li>
gr<li><a>preserveMissing</a> leaves all the entries alone.</li>
gr</ul>
gr
grWhen <a>merge</a> is given three arguments, it is inlined at the call
grsite. To prevent excessive inlining, you should typically use
gr<a>merge</a> to define your custom combining functions.
gr
grExamples:
gr
gr<pre>
grunionWithKey f = merge preserveMissing preserveMissing (zipWithMatched f)
gr</pre>
gr
gr<pre>
grintersectionWithKey f = merge dropMissing dropMissing (zipWithMatched f)
gr</pre>
gr
gr<pre>
grdifferenceWith f = merge diffPreserve diffDrop f
gr</pre>
gr
gr<pre>
grsymmetricDifference = merge diffPreserve diffPreserve (\ _ _ _ -&gt; Nothing)
gr</pre>
gr
gr<pre>
grmapEachPiece f g h = merge (diffMapWithKey f) (diffMapWithKey g)
gr</pre>
merge :: SimpleWhenMissing a c -> SimpleWhenMissing b c -> SimpleWhenMatched a b c -> IntMap a -> IntMap b -> IntMap c

-- | When a key is found in both maps, apply a function to the key and
grvalues and maybe use the result in the merged map.
gr
gr<pre>
grzipWithMaybeMatched
gr  :: (Key -&gt; x -&gt; y -&gt; Maybe z)
gr  -&gt; SimpleWhenMatched x y z
gr</pre>
zipWithMaybeMatched :: Applicative f => (Key -> x -> y -> Maybe z) -> WhenMatched f x y z

-- | When a key is found in both maps, apply a function to the key and
grvalues and use the result in the merged map.
gr
gr<pre>
grzipWithMatched
gr  :: (Key -&gt; x -&gt; y -&gt; z)
gr  -&gt; SimpleWhenMatched x y z
gr</pre>
zipWithMatched :: Applicative f => (Key -> x -> y -> z) -> WhenMatched f x y z

-- | Map over the entries whose keys are missing from the other map,
groptionally removing some. This is the most powerful
gr<a>SimpleWhenMissing</a> tactic, but others are usually more
grefficient.
gr
gr<pre>
grmapMaybeMissing :: (Key -&gt; x -&gt; Maybe y) -&gt; SimpleWhenMissing x y
gr</pre>
gr
gr<pre>
grmapMaybeMissing f = traverseMaybeMissing (\k x -&gt; pure (f k x))
gr</pre>
gr
grbut <tt>mapMaybeMissing</tt> uses fewer unnecessary <a>Applicative</a>
groperations.
mapMaybeMissing :: Applicative f => (Key -> x -> Maybe y) -> WhenMissing f x y

-- | Drop all the entries whose keys are missing from the other map.
gr
gr<pre>
grdropMissing :: SimpleWhenMissing x y
gr</pre>
gr
gr<pre>
grdropMissing = mapMaybeMissing (\_ _ -&gt; Nothing)
gr</pre>
gr
grbut <tt>dropMissing</tt> is much faster.
dropMissing :: Applicative f => WhenMissing f x y

-- | Preserve, unchanged, the entries whose keys are missing from the other
grmap.
gr
gr<pre>
grpreserveMissing :: SimpleWhenMissing x x
gr</pre>
gr
gr<pre>
grpreserveMissing = Merge.Lazy.mapMaybeMissing (\_ x -&gt; Just x)
gr</pre>
gr
grbut <tt>preserveMissing</tt> is much faster.
preserveMissing :: Applicative f => WhenMissing f x x

-- | Map over the entries whose keys are missing from the other map.
gr
gr<pre>
grmapMissing :: (k -&gt; x -&gt; y) -&gt; SimpleWhenMissing x y
gr</pre>
gr
gr<pre>
grmapMissing f = mapMaybeMissing (\k x -&gt; Just $ f k x)
gr</pre>
gr
grbut <tt>mapMissing</tt> is somewhat faster.
mapMissing :: Applicative f => (Key -> x -> y) -> WhenMissing f x y

-- | Filter the entries whose keys are missing from the other map.
gr
gr<pre>
grfilterMissing :: (k -&gt; x -&gt; Bool) -&gt; SimpleWhenMissing x x
gr</pre>
gr
gr<pre>
grfilterMissing f = Merge.Lazy.mapMaybeMissing $ \k x -&gt; guard (f k x) *&gt; Just x
gr</pre>
gr
grbut this should be a little faster.
filterMissing :: Applicative f => (Key -> x -> Bool) -> WhenMissing f x x

-- | A tactic for dealing with keys present in one map but not the other in
gr<a>merge</a> or <a>mergeA</a>.
gr
grA tactic of type <tt>WhenMissing f k x z</tt> is an abstract
grrepresentation of a function of type <tt>Key -&gt; x -&gt; f (Maybe
grz)</tt>.
data WhenMissing f x y

-- | A tactic for dealing with keys present in both maps in <a>merge</a> or
gr<a>mergeA</a>.
gr
grA tactic of type <tt>WhenMatched f x y z</tt> is an abstract
grrepresentation of a function of type <tt>Key -&gt; x -&gt; y -&gt; f
gr(Maybe z)</tt>.
data WhenMatched f x y z

-- | An applicative version of <a>merge</a>.
gr
gr<tt>mergeA</tt> takes two <a>WhenMissing</a> tactics, a
gr<a>WhenMatched</a> tactic and two maps. It uses the tactics to merge
grthe maps. Its behavior is best understood via its fundamental tactics,
gr<a>traverseMaybeMissing</a> and <a>zipWithMaybeAMatched</a>.
gr
grConsider
gr
gr<pre>
grmergeA (traverseMaybeMissing g1)
gr              (traverseMaybeMissing g2)
gr              (zipWithMaybeAMatched f)
gr              m1 m2
gr</pre>
gr
grTake, for example,
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>), (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 = [(1, "one"), (2, "two"), (4, "three")]
gr</pre>
gr
gr<tt>mergeA</tt> will first '<tt>align'</tt> these maps by key:
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>),               (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 =           [(1, "one"), (2, "two"),          (4, "three")]
gr</pre>
gr
grIt will then pass the individual entries and pairs of entries to
gr<tt>g1</tt>, <tt>g2</tt>, or <tt>f</tt> as appropriate:
gr
gr<pre>
gractions = [g1 0 <tt>a</tt>, f 1 <tt>b</tt> "one", g2 2 "two", g1 3 <tt>c</tt>, f 4 <tt>d</tt> "three"]
gr</pre>
gr
grNext, it will perform the actions in the <tt>actions</tt> list in
grorder from left to right.
gr
gr<pre>
grkeys =     0        1          2           3        4
grresults = [Nothing, Just True, Just False, Nothing, Just True]
gr</pre>
gr
grFinally, the <tt>Just</tt> results are collected into a map:
gr
gr<pre>
grreturn value = [(1, True), (2, False), (4, True)]
gr</pre>
gr
grThe other tactics below are optimizations or simplifications of
gr<a>traverseMaybeMissing</a> for special cases. Most importantly,
gr
gr<ul>
gr<li><a>dropMissing</a> drops all the keys.</li>
gr<li><a>preserveMissing</a> leaves all the entries alone.</li>
gr<li><a>mapMaybeMissing</a> does not use the <a>Applicative</a>
grcontext.</li>
gr</ul>
gr
grWhen <a>mergeA</a> is given three arguments, it is inlined at the call
grsite. To prevent excessive inlining, you should generally only use
gr<a>mergeA</a> to define custom combining functions.
mergeA :: (Applicative f) => WhenMissing f a c -> WhenMissing f b c -> WhenMatched f a b c -> IntMap a -> IntMap b -> f (IntMap c)

-- | When a key is found in both maps, apply a function to the key and
grvalues, perform the resulting action, and maybe use the result in the
grmerged map.
gr
grThis is the fundamental <a>WhenMatched</a> tactic.
zipWithMaybeAMatched :: (Key -> x -> y -> f (Maybe z)) -> WhenMatched f x y z

-- | When a key is found in both maps, apply a function to the key and
grvalues to produce an action and use its result in the merged map.
zipWithAMatched :: Applicative f => (Key -> x -> y -> f z) -> WhenMatched f x y z

-- | Traverse over the entries whose keys are missing from the other map,
groptionally producing values to put in the result. This is the most
grpowerful <a>WhenMissing</a> tactic, but others are usually more
grefficient.
traverseMaybeMissing :: Applicative f => (Key -> x -> f (Maybe y)) -> WhenMissing f x y

-- | Traverse over the entries whose keys are missing from the other map.
traverseMissing :: Applicative f => (Key -> x -> f y) -> WhenMissing f x y

-- | Filter the entries whose keys are missing from the other map using
grsome <a>Applicative</a> action.
gr
gr<pre>
grfilterAMissing f = Merge.Lazy.traverseMaybeMissing $
gr  \k x -&gt; (\b -&gt; guard b *&gt; Just x) &lt;$&gt; f k x
gr</pre>
gr
grbut this should be a little faster.
filterAMissing :: Applicative f => (Key -> x -> f Bool) -> WhenMissing f x x

-- | Map covariantly over a <tt><a>WhenMissing</a> f x</tt>.
mapWhenMissing :: (Applicative f, Monad f) => (a -> b) -> WhenMissing f x a -> WhenMissing f x b

-- | Map covariantly over a <tt><a>WhenMatched</a> f x y</tt>.
mapWhenMatched :: Functor f => (a -> b) -> WhenMatched f x y a -> WhenMatched f x y b

-- | Along with zipWithMaybeAMatched, witnesses the isomorphism between
gr<tt>WhenMatched f x y z</tt> and <tt>Key -&gt; x -&gt; y -&gt; f
gr(Maybe z)</tt>.
runWhenMatched :: WhenMatched f x y z -> Key -> x -> y -> f (Maybe z)

-- | Along with traverseMaybeMissing, witnesses the isomorphism between
gr<tt>WhenMissing f x y</tt> and <tt>Key -&gt; x -&gt; f (Maybe y)</tt>.
runWhenMissing :: WhenMissing f x y -> Key -> x -> f (Maybe y)


-- | This module defines an API for writing functions that merge two maps.
grThe key functions are <a>merge</a> and <a>mergeA</a>. Each of these
grcan be used with several different "merge tactics".
gr
grThe <a>merge</a> and <a>mergeA</a> functions are shared by the lazy
grand strict modules. Only the choice of merge tactics determines
grstrictness. If you use <a>mapMissing</a> from
gr<a>Data.Map.Merge.Strict</a> then the results will be forced before
grthey are inserted. If you use <a>mapMissing</a> from this module then
grthey will not.
gr
gr<h2>Efficiency note</h2>
gr
grThe <tt>Category</tt>, <a>Applicative</a>, and <a>Monad</a> instances
grfor <a>WhenMissing</a> tactics are included because they are valid.
grHowever, they are inefficient in many cases and should usually be
gravoided. The instances for <a>WhenMatched</a> tactics should not pose
grany major efficiency problems.
module Data.IntMap.Merge.Lazy

-- | A tactic for dealing with keys present in one map but not the other in
gr<a>merge</a>.
gr
grA tactic of type <tt>SimpleWhenMissing x z</tt> is an abstract
grrepresentation of a function of type <tt>Key -&gt; x -&gt; Maybe
grz</tt>.
type SimpleWhenMissing = WhenMissing Identity

-- | A tactic for dealing with keys present in both maps in <a>merge</a>.
gr
grA tactic of type <tt>SimpleWhenMatched x y z</tt> is an abstract
grrepresentation of a function of type <tt>Key -&gt; x -&gt; y -&gt;
grMaybe z</tt>.
type SimpleWhenMatched = WhenMatched Identity

-- | Merge two maps.
gr
gr<tt>merge</tt> takes two <a>WhenMissing</a> tactics, a
gr<a>WhenMatched</a> tactic and two maps. It uses the tactics to merge
grthe maps. Its behavior is best understood via its fundamental tactics,
gr<a>mapMaybeMissing</a> and <a>zipWithMaybeMatched</a>.
gr
grConsider
gr
gr<pre>
grmerge (mapMaybeMissing g1)
gr             (mapMaybeMissing g2)
gr             (zipWithMaybeMatched f)
gr             m1 m2
gr</pre>
gr
grTake, for example,
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>), (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 = [(1, "one"), (2, "two"), (4, "three")]
gr</pre>
gr
gr<tt>merge</tt> will first '<tt>align'</tt> these maps by key:
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>),               (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 =           [(1, "one"), (2, "two"),          (4, "three")]
gr</pre>
gr
grIt will then pass the individual entries and pairs of entries to
gr<tt>g1</tt>, <tt>g2</tt>, or <tt>f</tt> as appropriate:
gr
gr<pre>
grmaybes = [g1 0 <tt>a</tt>, f 1 <tt>b</tt> "one", g2 2 "two", g1 3 <tt>c</tt>, f 4 <tt>d</tt> "three"]
gr</pre>
gr
grThis produces a <a>Maybe</a> for each key:
gr
gr<pre>
grkeys =     0        1          2           3        4
grresults = [Nothing, Just True, Just False, Nothing, Just True]
gr</pre>
gr
grFinally, the <tt>Just</tt> results are collected into a map:
gr
gr<pre>
grreturn value = [(1, True), (2, False), (4, True)]
gr</pre>
gr
grThe other tactics below are optimizations or simplifications of
gr<a>mapMaybeMissing</a> for special cases. Most importantly,
gr
gr<ul>
gr<li><a>dropMissing</a> drops all the keys.</li>
gr<li><a>preserveMissing</a> leaves all the entries alone.</li>
gr</ul>
gr
grWhen <a>merge</a> is given three arguments, it is inlined at the call
grsite. To prevent excessive inlining, you should typically use
gr<a>merge</a> to define your custom combining functions.
gr
grExamples:
gr
gr<pre>
grunionWithKey f = merge preserveMissing preserveMissing (zipWithMatched f)
gr</pre>
gr
gr<pre>
grintersectionWithKey f = merge dropMissing dropMissing (zipWithMatched f)
gr</pre>
gr
gr<pre>
grdifferenceWith f = merge diffPreserve diffDrop f
gr</pre>
gr
gr<pre>
grsymmetricDifference = merge diffPreserve diffPreserve (\ _ _ _ -&gt; Nothing)
gr</pre>
gr
gr<pre>
grmapEachPiece f g h = merge (diffMapWithKey f) (diffMapWithKey g)
gr</pre>
merge :: SimpleWhenMissing a c -> SimpleWhenMissing b c -> SimpleWhenMatched a b c -> IntMap a -> IntMap b -> IntMap c

-- | When a key is found in both maps, apply a function to the key and
grvalues and maybe use the result in the merged map.
gr
gr<pre>
grzipWithMaybeMatched
gr  :: (Key -&gt; x -&gt; y -&gt; Maybe z)
gr  -&gt; SimpleWhenMatched x y z
gr</pre>
zipWithMaybeMatched :: Applicative f => (Key -> x -> y -> Maybe z) -> WhenMatched f x y z

-- | When a key is found in both maps, apply a function to the key and
grvalues and use the result in the merged map.
gr
gr<pre>
grzipWithMatched
gr  :: (Key -&gt; x -&gt; y -&gt; z)
gr  -&gt; SimpleWhenMatched x y z
gr</pre>
zipWithMatched :: Applicative f => (Key -> x -> y -> z) -> WhenMatched f x y z

-- | Map over the entries whose keys are missing from the other map,
groptionally removing some. This is the most powerful
gr<a>SimpleWhenMissing</a> tactic, but others are usually more
grefficient.
gr
gr<pre>
grmapMaybeMissing :: (Key -&gt; x -&gt; Maybe y) -&gt; SimpleWhenMissing x y
gr</pre>
gr
gr<pre>
grmapMaybeMissing f = traverseMaybeMissing (\k x -&gt; pure (f k x))
gr</pre>
gr
grbut <tt>mapMaybeMissing</tt> uses fewer unnecessary <a>Applicative</a>
groperations.
mapMaybeMissing :: Applicative f => (Key -> x -> Maybe y) -> WhenMissing f x y

-- | Drop all the entries whose keys are missing from the other map.
gr
gr<pre>
grdropMissing :: SimpleWhenMissing x y
gr</pre>
gr
gr<pre>
grdropMissing = mapMaybeMissing (\_ _ -&gt; Nothing)
gr</pre>
gr
grbut <tt>dropMissing</tt> is much faster.
dropMissing :: Applicative f => WhenMissing f x y

-- | Preserve, unchanged, the entries whose keys are missing from the other
grmap.
gr
gr<pre>
grpreserveMissing :: SimpleWhenMissing x x
gr</pre>
gr
gr<pre>
grpreserveMissing = Merge.Lazy.mapMaybeMissing (\_ x -&gt; Just x)
gr</pre>
gr
grbut <tt>preserveMissing</tt> is much faster.
preserveMissing :: Applicative f => WhenMissing f x x

-- | Map over the entries whose keys are missing from the other map.
gr
gr<pre>
grmapMissing :: (k -&gt; x -&gt; y) -&gt; SimpleWhenMissing x y
gr</pre>
gr
gr<pre>
grmapMissing f = mapMaybeMissing (\k x -&gt; Just $ f k x)
gr</pre>
gr
grbut <tt>mapMissing</tt> is somewhat faster.
mapMissing :: Applicative f => (Key -> x -> y) -> WhenMissing f x y

-- | Filter the entries whose keys are missing from the other map.
gr
gr<pre>
grfilterMissing :: (k -&gt; x -&gt; Bool) -&gt; SimpleWhenMissing x x
gr</pre>
gr
gr<pre>
grfilterMissing f = Merge.Lazy.mapMaybeMissing $ \k x -&gt; guard (f k x) *&gt; Just x
gr</pre>
gr
grbut this should be a little faster.
filterMissing :: Applicative f => (Key -> x -> Bool) -> WhenMissing f x x

-- | A tactic for dealing with keys present in one map but not the other in
gr<a>merge</a> or <a>mergeA</a>.
gr
grA tactic of type <tt>WhenMissing f k x z</tt> is an abstract
grrepresentation of a function of type <tt>Key -&gt; x -&gt; f (Maybe
grz)</tt>.
data WhenMissing f x y

-- | A tactic for dealing with keys present in both maps in <a>merge</a> or
gr<a>mergeA</a>.
gr
grA tactic of type <tt>WhenMatched f x y z</tt> is an abstract
grrepresentation of a function of type <tt>Key -&gt; x -&gt; y -&gt; f
gr(Maybe z)</tt>.
data WhenMatched f x y z

-- | An applicative version of <a>merge</a>.
gr
gr<tt>mergeA</tt> takes two <a>WhenMissing</a> tactics, a
gr<a>WhenMatched</a> tactic and two maps. It uses the tactics to merge
grthe maps. Its behavior is best understood via its fundamental tactics,
gr<a>traverseMaybeMissing</a> and <a>zipWithMaybeAMatched</a>.
gr
grConsider
gr
gr<pre>
grmergeA (traverseMaybeMissing g1)
gr              (traverseMaybeMissing g2)
gr              (zipWithMaybeAMatched f)
gr              m1 m2
gr</pre>
gr
grTake, for example,
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>), (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 = [(1, "one"), (2, "two"), (4, "three")]
gr</pre>
gr
gr<tt>mergeA</tt> will first '<tt>align'</tt> these maps by key:
gr
gr<pre>
grm1 = [(0, <tt>a</tt>), (1, <tt>b</tt>),               (3,<tt>c</tt>), (4, <tt>d</tt>)]
grm2 =           [(1, "one"), (2, "two"),          (4, "three")]
gr</pre>
gr
grIt will then pass the individual entries and pairs of entries to
gr<tt>g1</tt>, <tt>g2</tt>, or <tt>f</tt> as appropriate:
gr
gr<pre>
gractions = [g1 0 <tt>a</tt>, f 1 <tt>b</tt> "one", g2 2 "two", g1 3 <tt>c</tt>, f 4 <tt>d</tt> "three"]
gr</pre>
gr
grNext, it will perform the actions in the <tt>actions</tt> list in
grorder from left to right.
gr
gr<pre>
grkeys =     0        1          2           3        4
grresults = [Nothing, Just True, Just False, Nothing, Just True]
gr</pre>
gr
grFinally, the <tt>Just</tt> results are collected into a map:
gr
gr<pre>
grreturn value = [(1, True), (2, False), (4, True)]
gr</pre>
gr
grThe other tactics below are optimizations or simplifications of
gr<a>traverseMaybeMissing</a> for special cases. Most importantly,
gr
gr<ul>
gr<li><a>dropMissing</a> drops all the keys.</li>
gr<li><a>preserveMissing</a> leaves all the entries alone.</li>
gr<li><a>mapMaybeMissing</a> does not use the <a>Applicative</a>
grcontext.</li>
gr</ul>
gr
grWhen <a>mergeA</a> is given three arguments, it is inlined at the call
grsite. To prevent excessive inlining, you should generally only use
gr<a>mergeA</a> to define custom combining functions.
mergeA :: (Applicative f) => WhenMissing f a c -> WhenMissing f b c -> WhenMatched f a b c -> IntMap a -> IntMap b -> f (IntMap c)

-- | When a key is found in both maps, apply a function to the key and
grvalues, perform the resulting action, and maybe use the result in the
grmerged map.
gr
grThis is the fundamental <a>WhenMatched</a> tactic.
zipWithMaybeAMatched :: (Key -> x -> y -> f (Maybe z)) -> WhenMatched f x y z

-- | When a key is found in both maps, apply a function to the key and
grvalues to produce an action and use its result in the merged map.
zipWithAMatched :: Applicative f => (Key -> x -> y -> f z) -> WhenMatched f x y z

-- | Traverse over the entries whose keys are missing from the other map,
groptionally producing values to put in the result. This is the most
grpowerful <a>WhenMissing</a> tactic, but others are usually more
grefficient.
traverseMaybeMissing :: Applicative f => (Key -> x -> f (Maybe y)) -> WhenMissing f x y

-- | Traverse over the entries whose keys are missing from the other map.
traverseMissing :: Applicative f => (Key -> x -> f y) -> WhenMissing f x y

-- | Filter the entries whose keys are missing from the other map using
grsome <a>Applicative</a> action.
gr
gr<pre>
grfilterAMissing f = Merge.Lazy.traverseMaybeMissing $
gr  \k x -&gt; (\b -&gt; guard b *&gt; Just x) &lt;$&gt; f k x
gr</pre>
gr
grbut this should be a little faster.
filterAMissing :: Applicative f => (Key -> x -> f Bool) -> WhenMissing f x x

-- | Map covariantly over a <tt><a>WhenMissing</a> f x</tt>.
mapWhenMissing :: (Applicative f, Monad f) => (a -> b) -> WhenMissing f x a -> WhenMissing f x b

-- | Map covariantly over a <tt><a>WhenMatched</a> f x y</tt>.
mapWhenMatched :: Functor f => (a -> b) -> WhenMatched f x y a -> WhenMatched f x y b

-- | Map contravariantly over a <tt><a>WhenMissing</a> f _ x</tt>.
lmapWhenMissing :: (b -> a) -> WhenMissing f a x -> WhenMissing f b x

-- | Map contravariantly over a <tt><a>WhenMatched</a> f _ y z</tt>.
contramapFirstWhenMatched :: (b -> a) -> WhenMatched f a y z -> WhenMatched f b y z

-- | Map contravariantly over a <tt><a>WhenMatched</a> f x _ z</tt>.
contramapSecondWhenMatched :: (b -> a) -> WhenMatched f x a z -> WhenMatched f x b z

-- | Along with zipWithMaybeAMatched, witnesses the isomorphism between
gr<tt>WhenMatched f x y z</tt> and <tt>Key -&gt; x -&gt; y -&gt; f
gr(Maybe z)</tt>.
runWhenMatched :: WhenMatched f x y z -> Key -> x -> y -> f (Maybe z)

-- | Along with traverseMaybeMissing, witnesses the isomorphism between
gr<tt>WhenMissing f x y</tt> and <tt>Key -&gt; x -&gt; f (Maybe y)</tt>.
runWhenMissing :: WhenMissing f x y -> Key -> x -> f (Maybe y)


-- | An efficient implementation of maps from integer keys to values
gr(dictionaries).
gr
grAPI of this module is strict in both the keys and the values. If you
grneed value-lazy maps, use <a>Data.IntMap.Lazy</a> instead. The
gr<a>IntMap</a> type itself is shared between the lazy and strict
grmodules, meaning that the same <a>IntMap</a> value can be passed to
grfunctions in both modules (although that is rarely needed).
gr
grThese modules are intended to be imported qualified, to avoid name
grclashes with Prelude functions, e.g.
gr
gr<pre>
grimport Data.IntMap.Strict (IntMap)
grimport qualified Data.IntMap.Strict as IntMap
gr</pre>
gr
grThe implementation is based on <i>big-endian patricia trees</i>. This
grdata structure performs especially well on binary operations like
gr<a>union</a> and <a>intersection</a>. However, my benchmarks show that
grit is also (much) faster on insertions and deletions when compared to
gra generic size-balanced map implementation (see <a>Data.Map</a>).
gr
gr<ul>
gr<li>Chris Okasaki and Andy Gill, "<i>Fast Mergeable Integer Maps</i>",
grWorkshop on ML, September 1998, pages 77-86,
gr<a>http://citeseer.ist.psu.edu/okasaki98fast.html</a></li>
gr<li>D.R. Morrison, "/PATRICIA -- Practical Algorithm To Retrieve
grInformation Coded In Alphanumeric/", Journal of the ACM, 15(4),
grOctober 1968, pages 514-534.</li>
gr</ul>
gr
grOperation comments contain the operation time complexity in the Big-O
grnotation <a>http://en.wikipedia.org/wiki/Big_O_notation</a>. Many
groperations have a worst-case complexity of <i>O(min(n,W))</i>. This
grmeans that the operation can become linear in the number of elements
grwith a maximum of <i>W</i> -- the number of bits in an <a>Int</a> (32
gror 64).
gr
grBe aware that the <a>Functor</a>, <a>Traversable</a> and <tt>Data</tt>
grinstances are the same as for the <a>Data.IntMap.Lazy</a> module, so
grif they are used on strict maps, the resulting maps will be lazy.
module Data.IntMap.Strict

-- | A map of integers to values <tt>a</tt>.
data IntMap a
type Key = Int

-- | <i>O(min(n,W))</i>. Find the value at a key. Calls <a>error</a> when
grthe element can not be found.
gr
gr<pre>
grfromList [(5,'a'), (3,'b')] ! 1    Error: element not in the map
grfromList [(5,'a'), (3,'b')] ! 5 == 'a'
gr</pre>
(!) :: IntMap a -> Key -> a

-- | <i>O(min(n,W))</i>. Find the value at a key. Returns <a>Nothing</a>
grwhen the element can not be found.
gr
gr<pre>
grfromList [(5,'a'), (3,'b')] !? 1 == Nothing
grfromList [(5,'a'), (3,'b')] !? 5 == Just 'a'
gr</pre>
(!?) :: IntMap a -> Key -> Maybe a
infixl 9 !?

-- | Same as <a>difference</a>.
(\\) :: IntMap a -> IntMap b -> IntMap a
infixl 9 \\

-- | <i>O(1)</i>. Is the map empty?
gr
gr<pre>
grData.IntMap.null (empty)           == True
grData.IntMap.null (singleton 1 'a') == False
gr</pre>
null :: IntMap a -> Bool

-- | <i>O(n)</i>. Number of elements in the map.
gr
gr<pre>
grsize empty                                   == 0
grsize (singleton 1 'a')                       == 1
grsize (fromList([(1,'a'), (2,'c'), (3,'b')])) == 3
gr</pre>
size :: IntMap a -> Int

-- | <i>O(min(n,W))</i>. Is the key a member of the map?
gr
gr<pre>
grmember 5 (fromList [(5,'a'), (3,'b')]) == True
grmember 1 (fromList [(5,'a'), (3,'b')]) == False
gr</pre>
member :: Key -> IntMap a -> Bool

-- | <i>O(min(n,W))</i>. Is the key not a member of the map?
gr
gr<pre>
grnotMember 5 (fromList [(5,'a'), (3,'b')]) == False
grnotMember 1 (fromList [(5,'a'), (3,'b')]) == True
gr</pre>
notMember :: Key -> IntMap a -> Bool

-- | <i>O(min(n,W))</i>. Lookup the value at a key in the map. See also
gr<a>lookup</a>.
lookup :: Key -> IntMap a -> Maybe a

-- | <i>O(min(n,W))</i>. The expression <tt>(<a>findWithDefault</a> def k
grmap)</tt> returns the value at key <tt>k</tt> or returns <tt>def</tt>
grwhen the key is not an element of the map.
gr
gr<pre>
grfindWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x'
grfindWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a'
gr</pre>
findWithDefault :: a -> Key -> IntMap a -> a

-- | <i>O(log n)</i>. Find largest key smaller than the given one and
grreturn the corresponding (key, value) pair.
gr
gr<pre>
grlookupLT 3 (fromList [(3,'a'), (5,'b')]) == Nothing
grlookupLT 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
gr</pre>
lookupLT :: Key -> IntMap a -> Maybe (Key, a)

-- | <i>O(log n)</i>. Find smallest key greater than the given one and
grreturn the corresponding (key, value) pair.
gr
gr<pre>
grlookupGT 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
grlookupGT 5 (fromList [(3,'a'), (5,'b')]) == Nothing
gr</pre>
lookupGT :: Key -> IntMap a -> Maybe (Key, a)

-- | <i>O(log n)</i>. Find largest key smaller or equal to the given one
grand return the corresponding (key, value) pair.
gr
gr<pre>
grlookupLE 2 (fromList [(3,'a'), (5,'b')]) == Nothing
grlookupLE 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
grlookupLE 5 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
gr</pre>
lookupLE :: Key -> IntMap a -> Maybe (Key, a)

-- | <i>O(log n)</i>. Find smallest key greater or equal to the given one
grand return the corresponding (key, value) pair.
gr
gr<pre>
grlookupGE 3 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
grlookupGE 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
grlookupGE 6 (fromList [(3,'a'), (5,'b')]) == Nothing
gr</pre>
lookupGE :: Key -> IntMap a -> Maybe (Key, a)

-- | <i>O(1)</i>. The empty map.
gr
gr<pre>
grempty      == fromList []
grsize empty == 0
gr</pre>
empty :: IntMap a

-- | <i>O(1)</i>. A map of one element.
gr
gr<pre>
grsingleton 1 'a'        == fromList [(1, 'a')]
grsize (singleton 1 'a') == 1
gr</pre>
singleton :: Key -> a -> IntMap a

-- | <i>O(min(n,W))</i>. Insert a new key/value pair in the map. If the key
gris already present in the map, the associated value is replaced with
grthe supplied value, i.e. <a>insert</a> is equivalent to
gr<tt><a>insertWith</a> <a>const</a></tt>.
gr
gr<pre>
grinsert 5 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'x')]
grinsert 7 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'a'), (7, 'x')]
grinsert 5 'x' empty                         == singleton 5 'x'
gr</pre>
insert :: Key -> a -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Insert with a combining function.
gr<tt><a>insertWith</a> f key value mp</tt> will insert the pair (key,
grvalue) into <tt>mp</tt> if key does not exist in the map. If the key
grdoes exist, the function will insert <tt>f new_value old_value</tt>.
gr
gr<pre>
grinsertWith (++) 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "xxxa")]
grinsertWith (++) 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
grinsertWith (++) 5 "xxx" empty                         == singleton 5 "xxx"
gr</pre>
insertWith :: (a -> a -> a) -> Key -> a -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Insert with a combining function.
gr<tt><a>insertWithKey</a> f key value mp</tt> will insert the pair
gr(key, value) into <tt>mp</tt> if key does not exist in the map. If the
grkey does exist, the function will insert <tt>f key new_value
grold_value</tt>.
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grinsertWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:xxx|a")]
grinsertWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
grinsertWithKey f 5 "xxx" empty                         == singleton 5 "xxx"
gr</pre>
gr
grIf the key exists in the map, this function is lazy in <tt>x</tt> but
grstrict in the result of <tt>f</tt>.
insertWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. The expression (<tt><a>insertLookupWithKey</a> f k
grx map</tt>) is a pair where the first element is equal to
gr(<tt><a>lookup</a> k map</tt>) and the second element equal to
gr(<tt><a>insertWithKey</a> f k x map</tt>).
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grinsertLookupWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:xxx|a")])
grinsertLookupWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a"), (7, "xxx")])
grinsertLookupWithKey f 5 "xxx" empty                         == (Nothing,  singleton 5 "xxx")
gr</pre>
gr
grThis is how to define <tt>insertLookup</tt> using
gr<tt>insertLookupWithKey</tt>:
gr
gr<pre>
grlet insertLookup kx x t = insertLookupWithKey (\_ a _ -&gt; a) kx x t
grinsertLookup 5 "x" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "x")])
grinsertLookup 7 "x" (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a"), (7, "x")])
gr</pre>
insertLookupWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> (Maybe a, IntMap a)

-- | <i>O(min(n,W))</i>. Delete a key and its value from the map. When the
grkey is not a member of the map, the original map is returned.
gr
gr<pre>
grdelete 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grdelete 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grdelete 5 empty                         == empty
gr</pre>
delete :: Key -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Adjust a value at a specific key. When the key is
grnot a member of the map, the original map is returned.
gr
gr<pre>
gradjust ("new " ++) 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
gradjust ("new " ++) 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gradjust ("new " ++) 7 empty                         == empty
gr</pre>
adjust :: (a -> a) -> Key -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Adjust a value at a specific key. When the key is
grnot a member of the map, the original map is returned.
gr
gr<pre>
grlet f key x = (show key) ++ ":new " ++ x
gradjustWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
gradjustWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gradjustWithKey f 7 empty                         == empty
gr</pre>
adjustWithKey :: (Key -> a -> a) -> Key -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. The expression (<tt><a>update</a> f k map</tt>)
grupdates the value <tt>x</tt> at <tt>k</tt> (if it is in the map). If
gr(<tt>f x</tt>) is <a>Nothing</a>, the element is deleted. If it is
gr(<tt><a>Just</a> y</tt>), the key <tt>k</tt> is bound to the new value
gr<tt>y</tt>.
gr
gr<pre>
grlet f x = if x == "a" then Just "new a" else Nothing
grupdate f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
grupdate f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grupdate f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
update :: (a -> Maybe a) -> Key -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. The expression (<tt><a>update</a> f k map</tt>)
grupdates the value <tt>x</tt> at <tt>k</tt> (if it is in the map). If
gr(<tt>f k x</tt>) is <a>Nothing</a>, the element is deleted. If it is
gr(<tt><a>Just</a> y</tt>), the key <tt>k</tt> is bound to the new value
gr<tt>y</tt>.
gr
gr<pre>
grlet f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
grupdateWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
grupdateWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grupdateWithKey f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Lookup and update. The function returns original
grvalue, if it is updated. This is different behavior than
gr<a>updateLookupWithKey</a>. Returns the original key value if the map
grentry is deleted.
gr
gr<pre>
grlet f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
grupdateLookupWithKey f 5 (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:new a")])
grupdateLookupWithKey f 7 (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a")])
grupdateLookupWithKey f 3 (fromList [(5,"a"), (3,"b")]) == (Just "b", singleton 5 "a")
gr</pre>
updateLookupWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> (Maybe a, IntMap a)

-- | <i>O(min(n,W))</i>. The expression (<tt><a>alter</a> f k map</tt>)
gralters the value <tt>x</tt> at <tt>k</tt>, or absence thereof.
gr<a>alter</a> can be used to insert, delete, or update a value in an
gr<a>IntMap</a>. In short : <tt><a>lookup</a> k (<a>alter</a> f k m) = f
gr(<a>lookup</a> k m)</tt>.
alter :: (Maybe a -> Maybe a) -> Key -> IntMap a -> IntMap a

-- | <i>O(log n)</i>. The expression (<tt><a>alterF</a> f k map</tt>)
gralters the value <tt>x</tt> at <tt>k</tt>, or absence thereof.
gr<a>alterF</a> can be used to inspect, insert, delete, or update a
grvalue in an <a>IntMap</a>. In short : <tt><a>lookup</a> k <a>$</a>
gr<a>alterF</a> f k m = f (<a>lookup</a> k m)</tt>.
gr
grExample:
gr
gr<pre>
grinteractiveAlter :: Int -&gt; IntMap String -&gt; IO (IntMap String)
grinteractiveAlter k m = alterF f k m where
gr  f Nothing -&gt; do
gr     putStrLn $ show k ++
gr         " was not found in the map. Would you like to add it?"
gr     getUserResponse1 :: IO (Maybe String)
gr  f (Just old) -&gt; do
gr     putStrLn "The key is currently bound to " ++ show old ++
gr         ". Would you like to change or delete it?"
gr     getUserresponse2 :: IO (Maybe String)
gr</pre>
gr
gr<a>alterF</a> is the most general operation for working with an
grindividual key that may or may not be in a given map.
alterF :: Functor f => (Maybe a -> f (Maybe a)) -> Key -> IntMap a -> f (IntMap a)

-- | <i>O(n+m)</i>. The (left-biased) union of two maps. It prefers the
grfirst map when duplicate keys are encountered, i.e. (<tt><a>union</a>
gr== <a>unionWith</a> <a>const</a></tt>).
gr
gr<pre>
grunion (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "a"), (7, "C")]
gr</pre>
union :: IntMap a -> IntMap a -> IntMap a

-- | <i>O(n+m)</i>. The union with a combining function.
gr
gr<pre>
grunionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "aA"), (7, "C")]
gr</pre>
unionWith :: (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a

-- | <i>O(n+m)</i>. The union with a combining function.
gr
gr<pre>
grlet f key left_value right_value = (show key) ++ ":" ++ left_value ++ "|" ++ right_value
grunionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "5:a|A"), (7, "C")]
gr</pre>
unionWithKey :: (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a

-- | The union of a list of maps.
gr
gr<pre>
grunions [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
gr    == fromList [(3, "b"), (5, "a"), (7, "C")]
grunions [(fromList [(5, "A3"), (3, "B3")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "a"), (3, "b")])]
gr    == fromList [(3, "B3"), (5, "A3"), (7, "C")]
gr</pre>
unions :: [IntMap a] -> IntMap a

-- | The union of a list of maps, with a combining operation.
gr
gr<pre>
grunionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
gr    == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")]
gr</pre>
unionsWith :: (a -> a -> a) -> [IntMap a] -> IntMap a

-- | <i>O(n+m)</i>. Difference between two maps (based on keys).
gr
gr<pre>
grdifference (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 3 "b"
gr</pre>
difference :: IntMap a -> IntMap b -> IntMap a

-- | <i>O(n+m)</i>. Difference with a combining function.
gr
gr<pre>
grlet f al ar = if al == "b" then Just (al ++ ":" ++ ar) else Nothing
grdifferenceWith f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (7, "C")])
gr    == singleton 3 "b:B"
gr</pre>
differenceWith :: (a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a

-- | <i>O(n+m)</i>. Difference with a combining function. When two equal
grkeys are encountered, the combining function is applied to the key and
grboth values. If it returns <a>Nothing</a>, the element is discarded
gr(proper set difference). If it returns (<tt><a>Just</a> y</tt>), the
grelement is updated with a new value <tt>y</tt>.
gr
gr<pre>
grlet f k al ar = if al == "b" then Just ((show k) ++ ":" ++ al ++ "|" ++ ar) else Nothing
grdifferenceWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (10, "C")])
gr    == singleton 3 "3:b|B"
gr</pre>
differenceWithKey :: (Key -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a

-- | <i>O(n+m)</i>. The (left-biased) intersection of two maps (based on
grkeys).
gr
gr<pre>
grintersection (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "a"
gr</pre>
intersection :: IntMap a -> IntMap b -> IntMap a

-- | <i>O(n+m)</i>. The intersection with a combining function.
gr
gr<pre>
grintersectionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "aA"
gr</pre>
intersectionWith :: (a -> b -> c) -> IntMap a -> IntMap b -> IntMap c

-- | <i>O(n+m)</i>. The intersection with a combining function.
gr
gr<pre>
grlet f k al ar = (show k) ++ ":" ++ al ++ "|" ++ ar
grintersectionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "5:a|A"
gr</pre>
intersectionWithKey :: (Key -> a -> b -> c) -> IntMap a -> IntMap b -> IntMap c

-- | <i>O(n+m)</i>. A high-performance universal combining function. Using
gr<a>mergeWithKey</a>, all combining functions can be defined without
grany loss of efficiency (with exception of <a>union</a>,
gr<a>difference</a> and <a>intersection</a>, where sharing of some nodes
gris lost with <a>mergeWithKey</a>).
gr
grPlease make sure you know what is going on when using
gr<a>mergeWithKey</a>, otherwise you can be surprised by unexpected code
grgrowth or even corruption of the data structure.
gr
grWhen <a>mergeWithKey</a> is given three arguments, it is inlined to
grthe call site. You should therefore use <a>mergeWithKey</a> only to
grdefine your custom combining functions. For example, you could define
gr<a>unionWithKey</a>, <a>differenceWithKey</a> and
gr<a>intersectionWithKey</a> as
gr
gr<pre>
grmyUnionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -&gt; Just (f k x1 x2)) id id m1 m2
grmyDifferenceWithKey f m1 m2 = mergeWithKey f id (const empty) m1 m2
grmyIntersectionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -&gt; Just (f k x1 x2)) (const empty) (const empty) m1 m2
gr</pre>
gr
grWhen calling <tt><a>mergeWithKey</a> combine only1 only2</tt>, a
grfunction combining two <a>IntMap</a>s is created, such that
gr
gr<ul>
gr<li>if a key is present in both maps, it is passed with both
grcorresponding values to the <tt>combine</tt> function. Depending on
grthe result, the key is either present in the result with specified
grvalue, or is left out;</li>
gr<li>a nonempty subtree present only in the first map is passed to
gr<tt>only1</tt> and the output is added to the result;</li>
gr<li>a nonempty subtree present only in the second map is passed to
gr<tt>only2</tt> and the output is added to the result.</li>
gr</ul>
gr
grThe <tt>only1</tt> and <tt>only2</tt> methods <i>must return a map
grwith a subset (possibly empty) of the keys of the given map</i>. The
grvalues can be modified arbitrarily. Most common variants of
gr<tt>only1</tt> and <tt>only2</tt> are <a>id</a> and <tt><a>const</a>
gr<a>empty</a></tt>, but for example <tt><a>map</a> f</tt> or
gr<tt><a>filterWithKey</a> f</tt> could be used for any <tt>f</tt>.
mergeWithKey :: (Key -> a -> b -> Maybe c) -> (IntMap a -> IntMap c) -> (IntMap b -> IntMap c) -> IntMap a -> IntMap b -> IntMap c

-- | <i>O(n)</i>. Map a function over all values in the map.
gr
gr<pre>
grmap (++ "x") (fromList [(5,"a"), (3,"b")]) == fromList [(3, "bx"), (5, "ax")]
gr</pre>
map :: (a -> b) -> IntMap a -> IntMap b

-- | <i>O(n)</i>. Map a function over all values in the map.
gr
gr<pre>
grlet f key x = (show key) ++ ":" ++ x
grmapWithKey f (fromList [(5,"a"), (3,"b")]) == fromList [(3, "3:b"), (5, "5:a")]
gr</pre>
mapWithKey :: (Key -> a -> b) -> IntMap a -> IntMap b

-- | <i>O(n)</i>. <tt><a>traverseWithKey</a> f s == <a>fromList</a>
gr<a>$</a> <a>traverse</a> ((k, v) -&gt; (,) k <a>$</a> f k v)
gr(<a>toList</a> m)</tt> That is, behaves exactly like a regular
gr<a>traverse</a> except that the traversing function also has access to
grthe key associated with a value.
gr
gr<pre>
grtraverseWithKey (\k v -&gt; if odd k then Just (succ v) else Nothing) (fromList [(1, 'a'), (5, 'e')]) == Just (fromList [(1, 'b'), (5, 'f')])
grtraverseWithKey (\k v -&gt; if odd k then Just (succ v) else Nothing) (fromList [(2, 'c')])           == Nothing
gr</pre>
traverseWithKey :: Applicative t => (Key -> a -> t b) -> IntMap a -> t (IntMap b)

-- | <i>O(n)</i>. The function <tt><a>mapAccum</a></tt> threads an
graccumulating argument through the map in ascending order of keys.
gr
gr<pre>
grlet f a b = (a ++ b, b ++ "X")
grmapAccum f "Everything: " (fromList [(5,"a"), (3,"b")]) == ("Everything: ba", fromList [(3, "bX"), (5, "aX")])
gr</pre>
mapAccum :: (a -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)

-- | <i>O(n)</i>. The function <tt><a>mapAccumWithKey</a></tt> threads an
graccumulating argument through the map in ascending order of keys.
gr
gr<pre>
grlet f a k b = (a ++ " " ++ (show k) ++ "-" ++ b, b ++ "X")
grmapAccumWithKey f "Everything:" (fromList [(5,"a"), (3,"b")]) == ("Everything: 3-b 5-a", fromList [(3, "bX"), (5, "aX")])
gr</pre>
mapAccumWithKey :: (a -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)

-- | <i>O(n)</i>. The function <tt><tt>mapAccumR</tt></tt> threads an
graccumulating argument through the map in descending order of keys.
mapAccumRWithKey :: (a -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)

-- | <i>O(n*min(n,W))</i>. <tt><a>mapKeys</a> f s</tt> is the map obtained
grby applying <tt>f</tt> to each key of <tt>s</tt>.
gr
grThe size of the result may be smaller if <tt>f</tt> maps two or more
grdistinct keys to the same new key. In this case the value at the
grgreatest of the original keys is retained.
gr
gr<pre>
grmapKeys (+ 1) (fromList [(5,"a"), (3,"b")])                        == fromList [(4, "b"), (6, "a")]
grmapKeys (\ _ -&gt; 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "c"
grmapKeys (\ _ -&gt; 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "c"
gr</pre>
mapKeys :: (Key -> Key) -> IntMap a -> IntMap a

-- | <i>O(n*log n)</i>. <tt><a>mapKeysWith</a> c f s</tt> is the map
grobtained by applying <tt>f</tt> to each key of <tt>s</tt>.
gr
grThe size of the result may be smaller if <tt>f</tt> maps two or more
grdistinct keys to the same new key. In this case the associated values
grwill be combined using <tt>c</tt>.
gr
gr<pre>
grmapKeysWith (++) (\ _ -&gt; 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "cdab"
grmapKeysWith (++) (\ _ -&gt; 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "cdab"
gr</pre>
mapKeysWith :: (a -> a -> a) -> (Key -> Key) -> IntMap a -> IntMap a

-- | <i>O(n*min(n,W))</i>. <tt><a>mapKeysMonotonic</a> f s ==
gr<a>mapKeys</a> f s</tt>, but works only when <tt>f</tt> is strictly
grmonotonic. That is, for any values <tt>x</tt> and <tt>y</tt>, if
gr<tt>x</tt> &lt; <tt>y</tt> then <tt>f x</tt> &lt; <tt>f y</tt>. <i>The
grprecondition is not checked.</i> Semi-formally, we have:
gr
gr<pre>
grand [x &lt; y ==&gt; f x &lt; f y | x &lt;- ls, y &lt;- ls]
gr                    ==&gt; mapKeysMonotonic f s == mapKeys f s
gr    where ls = keys s
gr</pre>
gr
grThis means that <tt>f</tt> maps distinct original keys to distinct
grresulting keys. This function has slightly better performance than
gr<a>mapKeys</a>.
gr
gr<pre>
grmapKeysMonotonic (\ k -&gt; k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")]
gr</pre>
mapKeysMonotonic :: (Key -> Key) -> IntMap a -> IntMap a

-- | <i>O(n)</i>. Fold the values in the map using the given
grright-associative binary operator, such that <tt><a>foldr</a> f z ==
gr<a>foldr</a> f z . <a>elems</a></tt>.
gr
grFor example,
gr
gr<pre>
grelems map = foldr (:) [] map
gr</pre>
gr
gr<pre>
grlet f a len = len + (length a)
grfoldr f 0 (fromList [(5,"a"), (3,"bbb")]) == 4
gr</pre>
foldr :: (a -> b -> b) -> b -> IntMap a -> b

-- | <i>O(n)</i>. Fold the values in the map using the given
grleft-associative binary operator, such that <tt><a>foldl</a> f z ==
gr<a>foldl</a> f z . <a>elems</a></tt>.
gr
grFor example,
gr
gr<pre>
grelems = reverse . foldl (flip (:)) []
gr</pre>
gr
gr<pre>
grlet f len a = len + (length a)
grfoldl f 0 (fromList [(5,"a"), (3,"bbb")]) == 4
gr</pre>
foldl :: (a -> b -> a) -> a -> IntMap b -> a

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grright-associative binary operator, such that <tt><a>foldrWithKey</a> f
grz == <a>foldr</a> (<a>uncurry</a> f) z . <a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grkeys map = foldrWithKey (\k x ks -&gt; k:ks) [] map
gr</pre>
gr
gr<pre>
grlet f k a result = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")"
grfoldrWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (5:a)(3:b)"
gr</pre>
foldrWithKey :: (Key -> a -> b -> b) -> b -> IntMap a -> b

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grleft-associative binary operator, such that <tt><a>foldlWithKey</a> f
grz == <a>foldl</a> (\z' (kx, x) -&gt; f z' kx x) z .
gr<a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grkeys = reverse . foldlWithKey (\ks k x -&gt; k:ks) []
gr</pre>
gr
gr<pre>
grlet f result k a = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")"
grfoldlWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (3:b)(5:a)"
gr</pre>
foldlWithKey :: (a -> Key -> b -> a) -> a -> IntMap b -> a

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grmonoid, such that
gr
gr<pre>
gr<a>foldMapWithKey</a> f = <a>fold</a> . <a>mapWithKey</a> f
gr</pre>
gr
grThis can be an asymptotically faster than <a>foldrWithKey</a> or
gr<a>foldlWithKey</a> for some monoids.
foldMapWithKey :: Monoid m => (Key -> a -> m) -> IntMap a -> m

-- | <i>O(n)</i>. A strict version of <a>foldr</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldr' :: (a -> b -> b) -> b -> IntMap a -> b

-- | <i>O(n)</i>. A strict version of <a>foldl</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldl' :: (a -> b -> a) -> a -> IntMap b -> a

-- | <i>O(n)</i>. A strict version of <a>foldrWithKey</a>. Each application
grof the operator is evaluated before using the result in the next
grapplication. This function is strict in the starting value.
foldrWithKey' :: (Key -> a -> b -> b) -> b -> IntMap a -> b

-- | <i>O(n)</i>. A strict version of <a>foldlWithKey</a>. Each application
grof the operator is evaluated before using the result in the next
grapplication. This function is strict in the starting value.
foldlWithKey' :: (a -> Key -> b -> a) -> a -> IntMap b -> a

-- | <i>O(n)</i>. Return all elements of the map in the ascending order of
grtheir keys. Subject to list fusion.
gr
gr<pre>
grelems (fromList [(5,"a"), (3,"b")]) == ["b","a"]
grelems empty == []
gr</pre>
elems :: IntMap a -> [a]

-- | <i>O(n)</i>. Return all keys of the map in ascending order. Subject to
grlist fusion.
gr
gr<pre>
grkeys (fromList [(5,"a"), (3,"b")]) == [3,5]
grkeys empty == []
gr</pre>
keys :: IntMap a -> [Key]

-- | <i>O(n)</i>. An alias for <a>toAscList</a>. Returns all key/value
grpairs in the map in ascending key order. Subject to list fusion.
gr
gr<pre>
grassocs (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
grassocs empty == []
gr</pre>
assocs :: IntMap a -> [(Key, a)]

-- | <i>O(n*min(n,W))</i>. The set of all keys of the map.
gr
gr<pre>
grkeysSet (fromList [(5,"a"), (3,"b")]) == Data.IntSet.fromList [3,5]
grkeysSet empty == Data.IntSet.empty
gr</pre>
keysSet :: IntMap a -> IntSet

-- | <i>O(n)</i>. Build a map from a set of keys and a function which for
greach key computes its value.
gr
gr<pre>
grfromSet (\k -&gt; replicate k 'a') (Data.IntSet.fromList [3, 5]) == fromList [(5,"aaaaa"), (3,"aaa")]
grfromSet undefined Data.IntSet.empty == empty
gr</pre>
fromSet :: (Key -> a) -> IntSet -> IntMap a

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs. Subject to
grlist fusion.
gr
gr<pre>
grtoList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
grtoList empty == []
gr</pre>
toList :: IntMap a -> [(Key, a)]

-- | <i>O(n*min(n,W))</i>. Create a map from a list of key/value pairs.
gr
gr<pre>
grfromList [] == empty
grfromList [(5,"a"), (3,"b"), (5, "c")] == fromList [(5,"c"), (3,"b")]
grfromList [(5,"c"), (3,"b"), (5, "a")] == fromList [(5,"a"), (3,"b")]
gr</pre>
fromList :: [(Key, a)] -> IntMap a

-- | <i>O(n*min(n,W))</i>. Create a map from a list of key/value pairs with
gra combining function. See also <a>fromAscListWith</a>.
gr
gr<pre>
grfromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")]
grfromListWith (++) [] == empty
gr</pre>
fromListWith :: (a -> a -> a) -> [(Key, a)] -> IntMap a

-- | <i>O(n*min(n,W))</i>. Build a map from a list of key/value pairs with
gra combining function. See also fromAscListWithKey'.
gr
gr<pre>
grfromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")] == fromList [(3, "ab"), (5, "aba")]
grfromListWith (++) [] == empty
gr</pre>
fromListWithKey :: (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs where the
grkeys are in ascending order. Subject to list fusion.
gr
gr<pre>
grtoAscList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
gr</pre>
toAscList :: IntMap a -> [(Key, a)]

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs where the
grkeys are in descending order. Subject to list fusion.
gr
gr<pre>
grtoDescList (fromList [(5,"a"), (3,"b")]) == [(5,"a"), (3,"b")]
gr</pre>
toDescList :: IntMap a -> [(Key, a)]

-- | <i>O(n)</i>. Build a map from a list of key/value pairs where the keys
grare in ascending order.
gr
gr<pre>
grfromAscList [(3,"b"), (5,"a")]          == fromList [(3, "b"), (5, "a")]
grfromAscList [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "b")]
gr</pre>
fromAscList :: [(Key, a)] -> IntMap a

-- | <i>O(n)</i>. Build a map from a list of key/value pairs where the keys
grare in ascending order, with a combining function on equal keys.
gr<i>The precondition (input list is ascending) is not checked.</i>
gr
gr<pre>
grfromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")]
gr</pre>
fromAscListWith :: (a -> a -> a) -> [(Key, a)] -> IntMap a

-- | <i>O(n)</i>. Build a map from a list of key/value pairs where the keys
grare in ascending order, with a combining function on equal keys.
gr<i>The precondition (input list is ascending) is not checked.</i>
gr
gr<pre>
grfromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")]
gr</pre>
fromAscListWithKey :: (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a

-- | <i>O(n)</i>. Build a map from a list of key/value pairs where the keys
grare in ascending order and all distinct. <i>The precondition (input
grlist is strictly ascending) is not checked.</i>
gr
gr<pre>
grfromDistinctAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")]
gr</pre>
fromDistinctAscList :: [(Key, a)] -> IntMap a

-- | <i>O(n)</i>. Filter all values that satisfy some predicate.
gr
gr<pre>
grfilter (&gt; "a") (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grfilter (&gt; "x") (fromList [(5,"a"), (3,"b")]) == empty
grfilter (&lt; "a") (fromList [(5,"a"), (3,"b")]) == empty
gr</pre>
filter :: (a -> Bool) -> IntMap a -> IntMap a

-- | <i>O(n)</i>. Filter all keys/values that satisfy some predicate.
gr
gr<pre>
grfilterWithKey (\k _ -&gt; k &gt; 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
filterWithKey :: (Key -> a -> Bool) -> IntMap a -> IntMap a

-- | <i>O(n+m)</i>. The restriction of a map to the keys in a set.
gr
gr<pre>
grm <a>restrictKeys</a> s = <a>filterWithKey</a> (k _ -&gt; k `<a>member'</a> s) m
gr</pre>
restrictKeys :: IntMap a -> IntSet -> IntMap a

-- | <i>O(n+m)</i>. Remove all the keys in a given set from a map.
gr
gr<pre>
grm <a>withoutKeys</a> s = <a>filterWithKey</a> (k _ -&gt; k `<a>notMember'</a> s) m
gr</pre>
withoutKeys :: IntMap a -> IntSet -> IntMap a

-- | <i>O(n)</i>. Partition the map according to some predicate. The first
grmap contains all elements that satisfy the predicate, the second all
grelements that fail the predicate. See also <a>split</a>.
gr
gr<pre>
grpartition (&gt; "a") (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a")
grpartition (&lt; "x") (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
grpartition (&gt; "x") (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
gr</pre>
partition :: (a -> Bool) -> IntMap a -> (IntMap a, IntMap a)

-- | <i>O(n)</i>. Partition the map according to some predicate. The first
grmap contains all elements that satisfy the predicate, the second all
grelements that fail the predicate. See also <a>split</a>.
gr
gr<pre>
grpartitionWithKey (\ k _ -&gt; k &gt; 3) (fromList [(5,"a"), (3,"b")]) == (singleton 5 "a", singleton 3 "b")
grpartitionWithKey (\ k _ -&gt; k &lt; 7) (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
grpartitionWithKey (\ k _ -&gt; k &gt; 7) (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
gr</pre>
partitionWithKey :: (Key -> a -> Bool) -> IntMap a -> (IntMap a, IntMap a)

-- | <i>O(n)</i>. Map values and collect the <a>Just</a> results.
gr
gr<pre>
grlet f x = if x == "a" then Just "new a" else Nothing
grmapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"
gr</pre>
mapMaybe :: (a -> Maybe b) -> IntMap a -> IntMap b

-- | <i>O(n)</i>. Map keys/values and collect the <a>Just</a> results.
gr
gr<pre>
grlet f k _ = if k &lt; 5 then Just ("key : " ++ (show k)) else Nothing
grmapMaybeWithKey f (fromList [(5,"a"), (3,"b")]) == singleton 3 "key : 3"
gr</pre>
mapMaybeWithKey :: (Key -> a -> Maybe b) -> IntMap a -> IntMap b

-- | <i>O(n)</i>. Map values and separate the <a>Left</a> and <a>Right</a>
grresults.
gr
gr<pre>
grlet f a = if a &lt; "c" then Left a else Right a
grmapEither f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (fromList [(3,"b"), (5,"a")], fromList [(1,"x"), (7,"z")])
gr
grmapEither (\ a -&gt; Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (empty, fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr</pre>
mapEither :: (a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)

-- | <i>O(n)</i>. Map keys/values and separate the <a>Left</a> and
gr<a>Right</a> results.
gr
gr<pre>
grlet f k a = if k &lt; 5 then Left (k * 2) else Right (a ++ a)
grmapEitherWithKey f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (fromList [(1,2), (3,6)], fromList [(5,"aa"), (7,"zz")])
gr
grmapEitherWithKey (\_ a -&gt; Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (empty, fromList [(1,"x"), (3,"b"), (5,"a"), (7,"z")])
gr</pre>
mapEitherWithKey :: (Key -> a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)

-- | <i>O(min(n,W))</i>. The expression (<tt><a>split</a> k map</tt>) is a
grpair <tt>(map1,map2)</tt> where all keys in <tt>map1</tt> are lower
grthan <tt>k</tt> and all keys in <tt>map2</tt> larger than <tt>k</tt>.
grAny key equal to <tt>k</tt> is found in neither <tt>map1</tt> nor
gr<tt>map2</tt>.
gr
gr<pre>
grsplit 2 (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3,"b"), (5,"a")])
grsplit 3 (fromList [(5,"a"), (3,"b")]) == (empty, singleton 5 "a")
grsplit 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a")
grsplit 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", empty)
grsplit 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], empty)
gr</pre>
split :: Key -> IntMap a -> (IntMap a, IntMap a)

-- | <i>O(min(n,W))</i>. Performs a <a>split</a> but also returns whether
grthe pivot key was found in the original map.
gr
gr<pre>
grsplitLookup 2 (fromList [(5,"a"), (3,"b")]) == (empty, Nothing, fromList [(3,"b"), (5,"a")])
grsplitLookup 3 (fromList [(5,"a"), (3,"b")]) == (empty, Just "b", singleton 5 "a")
grsplitLookup 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Nothing, singleton 5 "a")
grsplitLookup 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Just "a", empty)
grsplitLookup 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], Nothing, empty)
gr</pre>
splitLookup :: Key -> IntMap a -> (IntMap a, Maybe a, IntMap a)

-- | <i>O(1)</i>. Decompose a map into pieces based on the structure of the
grunderlying tree. This function is useful for consuming a map in
grparallel.
gr
grNo guarantee is made as to the sizes of the pieces; an internal, but
grdeterministic process determines this. However, it is guaranteed that
grthe pieces returned will be in ascending order (all elements in the
grfirst submap less than all elements in the second, and so on).
gr
grExamples:
gr
gr<pre>
grsplitRoot (fromList (zip [1..6::Int] ['a'..])) ==
gr  [fromList [(1,'a'),(2,'b'),(3,'c')],fromList [(4,'d'),(5,'e'),(6,'f')]]
gr</pre>
gr
gr<pre>
grsplitRoot empty == []
gr</pre>
gr
grNote that the current implementation does not return more than two
grsubmaps, but you should not depend on this behaviour because it can
grchange in the future without notice.
splitRoot :: IntMap a -> [IntMap a]

-- | <i>O(n+m)</i>. Is this a submap? Defined as (<tt><a>isSubmapOf</a> =
gr<a>isSubmapOfBy</a> (==)</tt>).
isSubmapOf :: Eq a => IntMap a -> IntMap a -> Bool

-- | <i>O(n+m)</i>. The expression (<tt><a>isSubmapOfBy</a> f m1 m2</tt>)
grreturns <a>True</a> if all keys in <tt>m1</tt> are in <tt>m2</tt>, and
grwhen <tt>f</tt> returns <a>True</a> when applied to their respective
grvalues. For example, the following expressions are all <a>True</a>:
gr
gr<pre>
grisSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
grisSubmapOfBy (&lt;=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
grisSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])
gr</pre>
gr
grBut the following are all <a>False</a>:
gr
gr<pre>
grisSubmapOfBy (==) (fromList [(1,2)]) (fromList [(1,1),(2,2)])
grisSubmapOfBy (&lt;) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
grisSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
gr</pre>
isSubmapOfBy :: (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool

-- | <i>O(n+m)</i>. Is this a proper submap? (ie. a submap but not equal).
grDefined as (<tt><a>isProperSubmapOf</a> = <a>isProperSubmapOfBy</a>
gr(==)</tt>).
isProperSubmapOf :: Eq a => IntMap a -> IntMap a -> Bool

-- | <i>O(n+m)</i>. Is this a proper submap? (ie. a submap but not equal).
grThe expression (<tt><a>isProperSubmapOfBy</a> f m1 m2</tt>) returns
gr<a>True</a> when <tt>m1</tt> and <tt>m2</tt> are not equal, all keys
grin <tt>m1</tt> are in <tt>m2</tt>, and when <tt>f</tt> returns
gr<a>True</a> when applied to their respective values. For example, the
grfollowing expressions are all <a>True</a>:
gr
gr<pre>
grisProperSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
grisProperSubmapOfBy (&lt;=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
gr</pre>
gr
grBut the following are all <a>False</a>:
gr
gr<pre>
grisProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])
grisProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
grisProperSubmapOfBy (&lt;)  (fromList [(1,1)])       (fromList [(1,1),(2,2)])
gr</pre>
isProperSubmapOfBy :: (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool

-- | <i>O(min(n,W))</i>. The minimal key of the map. Returns <a>Nothing</a>
grif the map is empty.
lookupMin :: IntMap a -> Maybe (Key, a)

-- | <i>O(min(n,W))</i>. The maximal key of the map. Returns <a>Nothing</a>
grif the map is empty.
lookupMax :: IntMap a -> Maybe (Key, a)

-- | <i>O(min(n,W))</i>. The minimal key of the map. Calls <a>error</a> if
grthe map is empty. Use <a>minViewWithKey</a> if the map may be empty.
findMin :: IntMap a -> (Key, a)

-- | <i>O(min(n,W))</i>. The maximal key of the map. Calls <a>error</a> if
grthe map is empty. Use <a>maxViewWithKey</a> if the map may be empty.
findMax :: IntMap a -> (Key, a)

-- | <i>O(min(n,W))</i>. Delete the minimal key. Returns an empty map if
grthe map is empty.
gr
grNote that this is a change of behaviour for consistency with
gr<a>Map</a> – versions prior to 0.5 threw an error if the <a>IntMap</a>
grwas already empty.
deleteMin :: IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Delete the maximal key. Returns an empty map if
grthe map is empty.
gr
grNote that this is a change of behaviour for consistency with
gr<a>Map</a> – versions prior to 0.5 threw an error if the <a>IntMap</a>
grwas already empty.
deleteMax :: IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Delete and find the minimal element. This function
grthrows an error if the map is empty. Use <a>minViewWithKey</a> if the
grmap may be empty.
deleteFindMin :: IntMap a -> ((Key, a), IntMap a)

-- | <i>O(min(n,W))</i>. Delete and find the maximal element. This function
grthrows an error if the map is empty. Use <a>maxViewWithKey</a> if the
grmap may be empty.
deleteFindMax :: IntMap a -> ((Key, a), IntMap a)

-- | <i>O(log n)</i>. Update the value at the minimal key.
gr
gr<pre>
grupdateMin (\ a -&gt; Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "Xb"), (5, "a")]
grupdateMin (\ _ -&gt; Nothing)         (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateMin :: (a -> Maybe a) -> IntMap a -> IntMap a

-- | <i>O(log n)</i>. Update the value at the maximal key.
gr
gr<pre>
grupdateMax (\ a -&gt; Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "Xa")]
grupdateMax (\ _ -&gt; Nothing)         (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr</pre>
updateMax :: (a -> Maybe a) -> IntMap a -> IntMap a

-- | <i>O(log n)</i>. Update the value at the minimal key.
gr
gr<pre>
grupdateMinWithKey (\ k a -&gt; Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"3:b"), (5,"a")]
grupdateMinWithKey (\ _ _ -&gt; Nothing)                     (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateMinWithKey :: (Key -> a -> Maybe a) -> IntMap a -> IntMap a

-- | <i>O(log n)</i>. Update the value at the maximal key.
gr
gr<pre>
grupdateMaxWithKey (\ k a -&gt; Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"b"), (5,"5:a")]
grupdateMaxWithKey (\ _ _ -&gt; Nothing)                     (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr</pre>
updateMaxWithKey :: (Key -> a -> Maybe a) -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Retrieves the minimal key of the map, and the map
grstripped of that element, or <a>Nothing</a> if passed an empty map.
minView :: IntMap a -> Maybe (a, IntMap a)

-- | <i>O(min(n,W))</i>. Retrieves the maximal key of the map, and the map
grstripped of that element, or <a>Nothing</a> if passed an empty map.
maxView :: IntMap a -> Maybe (a, IntMap a)

-- | <i>O(min(n,W))</i>. Retrieves the minimal (key,value) pair of the map,
grand the map stripped of that element, or <a>Nothing</a> if passed an
grempty map.
gr
gr<pre>
grminViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((3,"b"), singleton 5 "a")
grminViewWithKey empty == Nothing
gr</pre>
minViewWithKey :: IntMap a -> Maybe ((Key, a), IntMap a)

-- | <i>O(min(n,W))</i>. Retrieves the maximal (key,value) pair of the map,
grand the map stripped of that element, or <a>Nothing</a> if passed an
grempty map.
gr
gr<pre>
grmaxViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((5,"a"), singleton 3 "b")
grmaxViewWithKey empty == Nothing
gr</pre>
maxViewWithKey :: IntMap a -> Maybe ((Key, a), IntMap a)

-- | <i>O(n)</i>. Show the tree that implements the map. The tree is shown
grin a compressed, hanging format.

-- | <i>Deprecated: These debugging functions will be removed from this
grmodule. They are available from Data.IntMap.Internal.Debug.</i>
showTree :: Show a => IntMap a -> String

-- | <i>O(n)</i>. The expression (<tt><a>showTreeWith</a> hang wide
grmap</tt>) shows the tree that implements the map. If <tt>hang</tt> is
gr<a>True</a>, a <i>hanging</i> tree is shown otherwise a rotated tree
gris shown. If <tt>wide</tt> is <a>True</a>, an extra wide version is
grshown.

-- | <i>Deprecated: These debugging functions will be removed from this
grmodule. They are available from Data.IntMap.Internal.Debug.</i>
showTreeWith :: Show a => Bool -> Bool -> IntMap a -> String


-- | An efficient implementation of maps from integer keys to values
gr(dictionaries).
gr
grAPI of this module is strict in the keys, but lazy in the values. If
gryou need value-strict maps, use <a>Data.IntMap.Strict</a> instead. The
gr<a>IntMap</a> type itself is shared between the lazy and strict
grmodules, meaning that the same <a>IntMap</a> value can be passed to
grfunctions in both modules (although that is rarely needed).
gr
grThese modules are intended to be imported qualified, to avoid name
grclashes with Prelude functions, e.g.
gr
gr<pre>
grimport Data.IntMap.Lazy (IntMap)
grimport qualified Data.IntMap.Lazy as IntMap
gr</pre>
gr
grThe implementation is based on <i>big-endian patricia trees</i>. This
grdata structure performs especially well on binary operations like
gr<a>union</a> and <a>intersection</a>. However, my benchmarks show that
grit is also (much) faster on insertions and deletions when compared to
gra generic size-balanced map implementation (see <a>Data.Map</a>).
gr
gr<ul>
gr<li>Chris Okasaki and Andy Gill, "<i>Fast Mergeable Integer Maps</i>",
grWorkshop on ML, September 1998, pages 77-86,
gr<a>http://citeseer.ist.psu.edu/okasaki98fast.html</a></li>
gr<li>D.R. Morrison, "/PATRICIA -- Practical Algorithm To Retrieve
grInformation Coded In Alphanumeric/", Journal of the ACM, 15(4),
grOctober 1968, pages 514-534.</li>
gr</ul>
gr
grOperation comments contain the operation time complexity in the Big-O
grnotation <a>http://en.wikipedia.org/wiki/Big_O_notation</a>. Many
groperations have a worst-case complexity of <i>O(min(n,W))</i>. This
grmeans that the operation can become linear in the number of elements
grwith a maximum of <i>W</i> -- the number of bits in an <a>Int</a> (32
gror 64).
module Data.IntMap.Lazy

-- | A map of integers to values <tt>a</tt>.
data IntMap a
type Key = Int

-- | <i>O(min(n,W))</i>. Find the value at a key. Calls <a>error</a> when
grthe element can not be found.
gr
gr<pre>
grfromList [(5,'a'), (3,'b')] ! 1    Error: element not in the map
grfromList [(5,'a'), (3,'b')] ! 5 == 'a'
gr</pre>
(!) :: IntMap a -> Key -> a

-- | <i>O(min(n,W))</i>. Find the value at a key. Returns <a>Nothing</a>
grwhen the element can not be found.
gr
gr<pre>
grfromList [(5,'a'), (3,'b')] !? 1 == Nothing
grfromList [(5,'a'), (3,'b')] !? 5 == Just 'a'
gr</pre>
(!?) :: IntMap a -> Key -> Maybe a
infixl 9 !?

-- | Same as <a>difference</a>.
(\\) :: IntMap a -> IntMap b -> IntMap a
infixl 9 \\

-- | <i>O(1)</i>. Is the map empty?
gr
gr<pre>
grData.IntMap.null (empty)           == True
grData.IntMap.null (singleton 1 'a') == False
gr</pre>
null :: IntMap a -> Bool

-- | <i>O(n)</i>. Number of elements in the map.
gr
gr<pre>
grsize empty                                   == 0
grsize (singleton 1 'a')                       == 1
grsize (fromList([(1,'a'), (2,'c'), (3,'b')])) == 3
gr</pre>
size :: IntMap a -> Int

-- | <i>O(min(n,W))</i>. Is the key a member of the map?
gr
gr<pre>
grmember 5 (fromList [(5,'a'), (3,'b')]) == True
grmember 1 (fromList [(5,'a'), (3,'b')]) == False
gr</pre>
member :: Key -> IntMap a -> Bool

-- | <i>O(min(n,W))</i>. Is the key not a member of the map?
gr
gr<pre>
grnotMember 5 (fromList [(5,'a'), (3,'b')]) == False
grnotMember 1 (fromList [(5,'a'), (3,'b')]) == True
gr</pre>
notMember :: Key -> IntMap a -> Bool

-- | <i>O(min(n,W))</i>. Lookup the value at a key in the map. See also
gr<a>lookup</a>.
lookup :: Key -> IntMap a -> Maybe a

-- | <i>O(min(n,W))</i>. The expression <tt>(<a>findWithDefault</a> def k
grmap)</tt> returns the value at key <tt>k</tt> or returns <tt>def</tt>
grwhen the key is not an element of the map.
gr
gr<pre>
grfindWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x'
grfindWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a'
gr</pre>
findWithDefault :: a -> Key -> IntMap a -> a

-- | <i>O(log n)</i>. Find largest key smaller than the given one and
grreturn the corresponding (key, value) pair.
gr
gr<pre>
grlookupLT 3 (fromList [(3,'a'), (5,'b')]) == Nothing
grlookupLT 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
gr</pre>
lookupLT :: Key -> IntMap a -> Maybe (Key, a)

-- | <i>O(log n)</i>. Find smallest key greater than the given one and
grreturn the corresponding (key, value) pair.
gr
gr<pre>
grlookupGT 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
grlookupGT 5 (fromList [(3,'a'), (5,'b')]) == Nothing
gr</pre>
lookupGT :: Key -> IntMap a -> Maybe (Key, a)

-- | <i>O(log n)</i>. Find largest key smaller or equal to the given one
grand return the corresponding (key, value) pair.
gr
gr<pre>
grlookupLE 2 (fromList [(3,'a'), (5,'b')]) == Nothing
grlookupLE 4 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
grlookupLE 5 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
gr</pre>
lookupLE :: Key -> IntMap a -> Maybe (Key, a)

-- | <i>O(log n)</i>. Find smallest key greater or equal to the given one
grand return the corresponding (key, value) pair.
gr
gr<pre>
grlookupGE 3 (fromList [(3,'a'), (5,'b')]) == Just (3, 'a')
grlookupGE 4 (fromList [(3,'a'), (5,'b')]) == Just (5, 'b')
grlookupGE 6 (fromList [(3,'a'), (5,'b')]) == Nothing
gr</pre>
lookupGE :: Key -> IntMap a -> Maybe (Key, a)

-- | <i>O(1)</i>. The empty map.
gr
gr<pre>
grempty      == fromList []
grsize empty == 0
gr</pre>
empty :: IntMap a

-- | <i>O(1)</i>. A map of one element.
gr
gr<pre>
grsingleton 1 'a'        == fromList [(1, 'a')]
grsize (singleton 1 'a') == 1
gr</pre>
singleton :: Key -> a -> IntMap a

-- | <i>O(min(n,W))</i>. Insert a new key/value pair in the map. If the key
gris already present in the map, the associated value is replaced with
grthe supplied value, i.e. <a>insert</a> is equivalent to
gr<tt><a>insertWith</a> <a>const</a></tt>.
gr
gr<pre>
grinsert 5 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'x')]
grinsert 7 'x' (fromList [(5,'a'), (3,'b')]) == fromList [(3, 'b'), (5, 'a'), (7, 'x')]
grinsert 5 'x' empty                         == singleton 5 'x'
gr</pre>
insert :: Key -> a -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Insert with a combining function.
gr<tt><a>insertWith</a> f key value mp</tt> will insert the pair (key,
grvalue) into <tt>mp</tt> if key does not exist in the map. If the key
grdoes exist, the function will insert <tt>f new_value old_value</tt>.
gr
gr<pre>
grinsertWith (++) 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "xxxa")]
grinsertWith (++) 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
grinsertWith (++) 5 "xxx" empty                         == singleton 5 "xxx"
gr</pre>
insertWith :: (a -> a -> a) -> Key -> a -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Insert with a combining function.
gr<tt><a>insertWithKey</a> f key value mp</tt> will insert the pair
gr(key, value) into <tt>mp</tt> if key does not exist in the map. If the
grkey does exist, the function will insert <tt>f key new_value
grold_value</tt>.
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grinsertWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:xxx|a")]
grinsertWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a"), (7, "xxx")]
grinsertWithKey f 5 "xxx" empty                         == singleton 5 "xxx"
gr</pre>
insertWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. The expression (<tt><a>insertLookupWithKey</a> f k
grx map</tt>) is a pair where the first element is equal to
gr(<tt><a>lookup</a> k map</tt>) and the second element equal to
gr(<tt><a>insertWithKey</a> f k x map</tt>).
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grinsertLookupWithKey f 5 "xxx" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:xxx|a")])
grinsertLookupWithKey f 7 "xxx" (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a"), (7, "xxx")])
grinsertLookupWithKey f 5 "xxx" empty                         == (Nothing,  singleton 5 "xxx")
gr</pre>
gr
grThis is how to define <tt>insertLookup</tt> using
gr<tt>insertLookupWithKey</tt>:
gr
gr<pre>
grlet insertLookup kx x t = insertLookupWithKey (\_ a _ -&gt; a) kx x t
grinsertLookup 5 "x" (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "x")])
grinsertLookup 7 "x" (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a"), (7, "x")])
gr</pre>
insertLookupWithKey :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> (Maybe a, IntMap a)

-- | <i>O(min(n,W))</i>. Delete a key and its value from the map. When the
grkey is not a member of the map, the original map is returned.
gr
gr<pre>
grdelete 5 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grdelete 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grdelete 5 empty                         == empty
gr</pre>
delete :: Key -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Adjust a value at a specific key. When the key is
grnot a member of the map, the original map is returned.
gr
gr<pre>
gradjust ("new " ++) 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
gradjust ("new " ++) 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gradjust ("new " ++) 7 empty                         == empty
gr</pre>
adjust :: (a -> a) -> Key -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Adjust a value at a specific key. When the key is
grnot a member of the map, the original map is returned.
gr
gr<pre>
grlet f key x = (show key) ++ ":new " ++ x
gradjustWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
gradjustWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
gradjustWithKey f 7 empty                         == empty
gr</pre>
adjustWithKey :: (Key -> a -> a) -> Key -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. The expression (<tt><a>update</a> f k map</tt>)
grupdates the value <tt>x</tt> at <tt>k</tt> (if it is in the map). If
gr(<tt>f x</tt>) is <a>Nothing</a>, the element is deleted. If it is
gr(<tt><a>Just</a> y</tt>), the key <tt>k</tt> is bound to the new value
gr<tt>y</tt>.
gr
gr<pre>
grlet f x = if x == "a" then Just "new a" else Nothing
grupdate f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "new a")]
grupdate f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grupdate f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
update :: (a -> Maybe a) -> Key -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. The expression (<tt><a>update</a> f k map</tt>)
grupdates the value <tt>x</tt> at <tt>k</tt> (if it is in the map). If
gr(<tt>f k x</tt>) is <a>Nothing</a>, the element is deleted. If it is
gr(<tt><a>Just</a> y</tt>), the key <tt>k</tt> is bound to the new value
gr<tt>y</tt>.
gr
gr<pre>
grlet f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
grupdateWithKey f 5 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "5:new a")]
grupdateWithKey f 7 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "a")]
grupdateWithKey f 3 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Lookup and update. The function returns original
grvalue, if it is updated. This is different behavior than
gr<a>updateLookupWithKey</a>. Returns the original key value if the map
grentry is deleted.
gr
gr<pre>
grlet f k x = if x == "a" then Just ((show k) ++ ":new a") else Nothing
grupdateLookupWithKey f 5 (fromList [(5,"a"), (3,"b")]) == (Just "a", fromList [(3, "b"), (5, "5:new a")])
grupdateLookupWithKey f 7 (fromList [(5,"a"), (3,"b")]) == (Nothing,  fromList [(3, "b"), (5, "a")])
grupdateLookupWithKey f 3 (fromList [(5,"a"), (3,"b")]) == (Just "b", singleton 5 "a")
gr</pre>
updateLookupWithKey :: (Key -> a -> Maybe a) -> Key -> IntMap a -> (Maybe a, IntMap a)

-- | <i>O(min(n,W))</i>. The expression (<tt><a>alter</a> f k map</tt>)
gralters the value <tt>x</tt> at <tt>k</tt>, or absence thereof.
gr<a>alter</a> can be used to insert, delete, or update a value in an
gr<a>IntMap</a>. In short : <tt><a>lookup</a> k (<a>alter</a> f k m) = f
gr(<a>lookup</a> k m)</tt>.
alter :: (Maybe a -> Maybe a) -> Key -> IntMap a -> IntMap a

-- | <i>O(log n)</i>. The expression (<tt><a>alterF</a> f k map</tt>)
gralters the value <tt>x</tt> at <tt>k</tt>, or absence thereof.
gr<a>alterF</a> can be used to inspect, insert, delete, or update a
grvalue in an <a>IntMap</a>. In short : <tt><a>lookup</a> k <a>$</a>
gr<a>alterF</a> f k m = f (<a>lookup</a> k m)</tt>.
gr
grExample:
gr
gr<pre>
grinteractiveAlter :: Int -&gt; IntMap String -&gt; IO (IntMap String)
grinteractiveAlter k m = alterF f k m where
gr  f Nothing -&gt; do
gr     putStrLn $ show k ++
gr         " was not found in the map. Would you like to add it?"
gr     getUserResponse1 :: IO (Maybe String)
gr  f (Just old) -&gt; do
gr     putStrLn "The key is currently bound to " ++ show old ++
gr         ". Would you like to change or delete it?"
gr     getUserresponse2 :: IO (Maybe String)
gr</pre>
gr
gr<a>alterF</a> is the most general operation for working with an
grindividual key that may or may not be in a given map.
gr
grNote: <a>alterF</a> is a flipped version of the <tt>at</tt> combinator
grfrom <a>At</a>.
alterF :: Functor f => (Maybe a -> f (Maybe a)) -> Key -> IntMap a -> f (IntMap a)

-- | <i>O(n+m)</i>. The (left-biased) union of two maps. It prefers the
grfirst map when duplicate keys are encountered, i.e. (<tt><a>union</a>
gr== <a>unionWith</a> <a>const</a></tt>).
gr
gr<pre>
grunion (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "a"), (7, "C")]
gr</pre>
union :: IntMap a -> IntMap a -> IntMap a

-- | <i>O(n+m)</i>. The union with a combining function.
gr
gr<pre>
grunionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "aA"), (7, "C")]
gr</pre>
unionWith :: (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a

-- | <i>O(n+m)</i>. The union with a combining function.
gr
gr<pre>
grlet f key left_value right_value = (show key) ++ ":" ++ left_value ++ "|" ++ right_value
grunionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == fromList [(3, "b"), (5, "5:a|A"), (7, "C")]
gr</pre>
unionWithKey :: (Key -> a -> a -> a) -> IntMap a -> IntMap a -> IntMap a

-- | The union of a list of maps.
gr
gr<pre>
grunions [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
gr    == fromList [(3, "b"), (5, "a"), (7, "C")]
grunions [(fromList [(5, "A3"), (3, "B3")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "a"), (3, "b")])]
gr    == fromList [(3, "B3"), (5, "A3"), (7, "C")]
gr</pre>
unions :: [IntMap a] -> IntMap a

-- | The union of a list of maps, with a combining operation.
gr
gr<pre>
grunionsWith (++) [(fromList [(5, "a"), (3, "b")]), (fromList [(5, "A"), (7, "C")]), (fromList [(5, "A3"), (3, "B3")])]
gr    == fromList [(3, "bB3"), (5, "aAA3"), (7, "C")]
gr</pre>
unionsWith :: (a -> a -> a) -> [IntMap a] -> IntMap a

-- | <i>O(n+m)</i>. Difference between two maps (based on keys).
gr
gr<pre>
grdifference (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 3 "b"
gr</pre>
difference :: IntMap a -> IntMap b -> IntMap a

-- | <i>O(n+m)</i>. Difference with a combining function.
gr
gr<pre>
grlet f al ar = if al == "b" then Just (al ++ ":" ++ ar) else Nothing
grdifferenceWith f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (7, "C")])
gr    == singleton 3 "b:B"
gr</pre>
differenceWith :: (a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a

-- | <i>O(n+m)</i>. Difference with a combining function. When two equal
grkeys are encountered, the combining function is applied to the key and
grboth values. If it returns <a>Nothing</a>, the element is discarded
gr(proper set difference). If it returns (<tt><a>Just</a> y</tt>), the
grelement is updated with a new value <tt>y</tt>.
gr
gr<pre>
grlet f k al ar = if al == "b" then Just ((show k) ++ ":" ++ al ++ "|" ++ ar) else Nothing
grdifferenceWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (3, "B"), (10, "C")])
gr    == singleton 3 "3:b|B"
gr</pre>
differenceWithKey :: (Key -> a -> b -> Maybe a) -> IntMap a -> IntMap b -> IntMap a

-- | <i>O(n+m)</i>. The (left-biased) intersection of two maps (based on
grkeys).
gr
gr<pre>
grintersection (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "a"
gr</pre>
intersection :: IntMap a -> IntMap b -> IntMap a

-- | <i>O(n+m)</i>. The intersection with a combining function.
gr
gr<pre>
grintersectionWith (++) (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "aA"
gr</pre>
intersectionWith :: (a -> b -> c) -> IntMap a -> IntMap b -> IntMap c

-- | <i>O(n+m)</i>. The intersection with a combining function.
gr
gr<pre>
grlet f k al ar = (show k) ++ ":" ++ al ++ "|" ++ ar
grintersectionWithKey f (fromList [(5, "a"), (3, "b")]) (fromList [(5, "A"), (7, "C")]) == singleton 5 "5:a|A"
gr</pre>
intersectionWithKey :: (Key -> a -> b -> c) -> IntMap a -> IntMap b -> IntMap c

-- | <i>O(n+m)</i>. A high-performance universal combining function. Using
gr<a>mergeWithKey</a>, all combining functions can be defined without
grany loss of efficiency (with exception of <a>union</a>,
gr<a>difference</a> and <a>intersection</a>, where sharing of some nodes
gris lost with <a>mergeWithKey</a>).
gr
grPlease make sure you know what is going on when using
gr<a>mergeWithKey</a>, otherwise you can be surprised by unexpected code
grgrowth or even corruption of the data structure.
gr
grWhen <a>mergeWithKey</a> is given three arguments, it is inlined to
grthe call site. You should therefore use <a>mergeWithKey</a> only to
grdefine your custom combining functions. For example, you could define
gr<a>unionWithKey</a>, <a>differenceWithKey</a> and
gr<a>intersectionWithKey</a> as
gr
gr<pre>
grmyUnionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -&gt; Just (f k x1 x2)) id id m1 m2
grmyDifferenceWithKey f m1 m2 = mergeWithKey f id (const empty) m1 m2
grmyIntersectionWithKey f m1 m2 = mergeWithKey (\k x1 x2 -&gt; Just (f k x1 x2)) (const empty) (const empty) m1 m2
gr</pre>
gr
grWhen calling <tt><a>mergeWithKey</a> combine only1 only2</tt>, a
grfunction combining two <a>IntMap</a>s is created, such that
gr
gr<ul>
gr<li>if a key is present in both maps, it is passed with both
grcorresponding values to the <tt>combine</tt> function. Depending on
grthe result, the key is either present in the result with specified
grvalue, or is left out;</li>
gr<li>a nonempty subtree present only in the first map is passed to
gr<tt>only1</tt> and the output is added to the result;</li>
gr<li>a nonempty subtree present only in the second map is passed to
gr<tt>only2</tt> and the output is added to the result.</li>
gr</ul>
gr
grThe <tt>only1</tt> and <tt>only2</tt> methods <i>must return a map
grwith a subset (possibly empty) of the keys of the given map</i>. The
grvalues can be modified arbitrarily. Most common variants of
gr<tt>only1</tt> and <tt>only2</tt> are <a>id</a> and <tt><a>const</a>
gr<a>empty</a></tt>, but for example <tt><a>map</a> f</tt> or
gr<tt><a>filterWithKey</a> f</tt> could be used for any <tt>f</tt>.
mergeWithKey :: (Key -> a -> b -> Maybe c) -> (IntMap a -> IntMap c) -> (IntMap b -> IntMap c) -> IntMap a -> IntMap b -> IntMap c

-- | <i>O(n)</i>. Map a function over all values in the map.
gr
gr<pre>
grmap (++ "x") (fromList [(5,"a"), (3,"b")]) == fromList [(3, "bx"), (5, "ax")]
gr</pre>
map :: (a -> b) -> IntMap a -> IntMap b

-- | <i>O(n)</i>. Map a function over all values in the map.
gr
gr<pre>
grlet f key x = (show key) ++ ":" ++ x
grmapWithKey f (fromList [(5,"a"), (3,"b")]) == fromList [(3, "3:b"), (5, "5:a")]
gr</pre>
mapWithKey :: (Key -> a -> b) -> IntMap a -> IntMap b

-- | <i>O(n)</i>. <tt><a>traverseWithKey</a> f s == <a>fromList</a>
gr<a>$</a> <a>traverse</a> ((k, v) -&gt; (,) k <a>$</a> f k v)
gr(<a>toList</a> m)</tt> That is, behaves exactly like a regular
gr<a>traverse</a> except that the traversing function also has access to
grthe key associated with a value.
gr
gr<pre>
grtraverseWithKey (\k v -&gt; if odd k then Just (succ v) else Nothing) (fromList [(1, 'a'), (5, 'e')]) == Just (fromList [(1, 'b'), (5, 'f')])
grtraverseWithKey (\k v -&gt; if odd k then Just (succ v) else Nothing) (fromList [(2, 'c')])           == Nothing
gr</pre>
traverseWithKey :: Applicative t => (Key -> a -> t b) -> IntMap a -> t (IntMap b)

-- | <i>O(n)</i>. The function <tt><a>mapAccum</a></tt> threads an
graccumulating argument through the map in ascending order of keys.
gr
gr<pre>
grlet f a b = (a ++ b, b ++ "X")
grmapAccum f "Everything: " (fromList [(5,"a"), (3,"b")]) == ("Everything: ba", fromList [(3, "bX"), (5, "aX")])
gr</pre>
mapAccum :: (a -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)

-- | <i>O(n)</i>. The function <tt><a>mapAccumWithKey</a></tt> threads an
graccumulating argument through the map in ascending order of keys.
gr
gr<pre>
grlet f a k b = (a ++ " " ++ (show k) ++ "-" ++ b, b ++ "X")
grmapAccumWithKey f "Everything:" (fromList [(5,"a"), (3,"b")]) == ("Everything: 3-b 5-a", fromList [(3, "bX"), (5, "aX")])
gr</pre>
mapAccumWithKey :: (a -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)

-- | <i>O(n)</i>. The function <tt><tt>mapAccumR</tt></tt> threads an
graccumulating argument through the map in descending order of keys.
mapAccumRWithKey :: (a -> Key -> b -> (a, c)) -> a -> IntMap b -> (a, IntMap c)

-- | <i>O(n*min(n,W))</i>. <tt><a>mapKeys</a> f s</tt> is the map obtained
grby applying <tt>f</tt> to each key of <tt>s</tt>.
gr
grThe size of the result may be smaller if <tt>f</tt> maps two or more
grdistinct keys to the same new key. In this case the value at the
grgreatest of the original keys is retained.
gr
gr<pre>
grmapKeys (+ 1) (fromList [(5,"a"), (3,"b")])                        == fromList [(4, "b"), (6, "a")]
grmapKeys (\ _ -&gt; 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "c"
grmapKeys (\ _ -&gt; 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "c"
gr</pre>
mapKeys :: (Key -> Key) -> IntMap a -> IntMap a

-- | <i>O(n*min(n,W))</i>. <tt><a>mapKeysWith</a> c f s</tt> is the map
grobtained by applying <tt>f</tt> to each key of <tt>s</tt>.
gr
grThe size of the result may be smaller if <tt>f</tt> maps two or more
grdistinct keys to the same new key. In this case the associated values
grwill be combined using <tt>c</tt>.
gr
gr<pre>
grmapKeysWith (++) (\ _ -&gt; 1) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 1 "cdab"
grmapKeysWith (++) (\ _ -&gt; 3) (fromList [(1,"b"), (2,"a"), (3,"d"), (4,"c")]) == singleton 3 "cdab"
gr</pre>
mapKeysWith :: (a -> a -> a) -> (Key -> Key) -> IntMap a -> IntMap a

-- | <i>O(n*min(n,W))</i>. <tt><a>mapKeysMonotonic</a> f s ==
gr<a>mapKeys</a> f s</tt>, but works only when <tt>f</tt> is strictly
grmonotonic. That is, for any values <tt>x</tt> and <tt>y</tt>, if
gr<tt>x</tt> &lt; <tt>y</tt> then <tt>f x</tt> &lt; <tt>f y</tt>. <i>The
grprecondition is not checked.</i> Semi-formally, we have:
gr
gr<pre>
grand [x &lt; y ==&gt; f x &lt; f y | x &lt;- ls, y &lt;- ls]
gr                    ==&gt; mapKeysMonotonic f s == mapKeys f s
gr    where ls = keys s
gr</pre>
gr
grThis means that <tt>f</tt> maps distinct original keys to distinct
grresulting keys. This function has slightly better performance than
gr<a>mapKeys</a>.
gr
gr<pre>
grmapKeysMonotonic (\ k -&gt; k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")]
gr</pre>
mapKeysMonotonic :: (Key -> Key) -> IntMap a -> IntMap a

-- | <i>O(n)</i>. Fold the values in the map using the given
grright-associative binary operator, such that <tt><a>foldr</a> f z ==
gr<a>foldr</a> f z . <a>elems</a></tt>.
gr
grFor example,
gr
gr<pre>
grelems map = foldr (:) [] map
gr</pre>
gr
gr<pre>
grlet f a len = len + (length a)
grfoldr f 0 (fromList [(5,"a"), (3,"bbb")]) == 4
gr</pre>
foldr :: (a -> b -> b) -> b -> IntMap a -> b

-- | <i>O(n)</i>. Fold the values in the map using the given
grleft-associative binary operator, such that <tt><a>foldl</a> f z ==
gr<a>foldl</a> f z . <a>elems</a></tt>.
gr
grFor example,
gr
gr<pre>
grelems = reverse . foldl (flip (:)) []
gr</pre>
gr
gr<pre>
grlet f len a = len + (length a)
grfoldl f 0 (fromList [(5,"a"), (3,"bbb")]) == 4
gr</pre>
foldl :: (a -> b -> a) -> a -> IntMap b -> a

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grright-associative binary operator, such that <tt><a>foldrWithKey</a> f
grz == <a>foldr</a> (<a>uncurry</a> f) z . <a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grkeys map = foldrWithKey (\k x ks -&gt; k:ks) [] map
gr</pre>
gr
gr<pre>
grlet f k a result = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")"
grfoldrWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (5:a)(3:b)"
gr</pre>
foldrWithKey :: (Key -> a -> b -> b) -> b -> IntMap a -> b

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grleft-associative binary operator, such that <tt><a>foldlWithKey</a> f
grz == <a>foldl</a> (\z' (kx, x) -&gt; f z' kx x) z .
gr<a>toAscList</a></tt>.
gr
grFor example,
gr
gr<pre>
grkeys = reverse . foldlWithKey (\ks k x -&gt; k:ks) []
gr</pre>
gr
gr<pre>
grlet f result k a = result ++ "(" ++ (show k) ++ ":" ++ a ++ ")"
grfoldlWithKey f "Map: " (fromList [(5,"a"), (3,"b")]) == "Map: (3:b)(5:a)"
gr</pre>
foldlWithKey :: (a -> Key -> b -> a) -> a -> IntMap b -> a

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grmonoid, such that
gr
gr<pre>
gr<a>foldMapWithKey</a> f = <a>fold</a> . <a>mapWithKey</a> f
gr</pre>
gr
grThis can be an asymptotically faster than <a>foldrWithKey</a> or
gr<a>foldlWithKey</a> for some monoids.
foldMapWithKey :: Monoid m => (Key -> a -> m) -> IntMap a -> m

-- | <i>O(n)</i>. A strict version of <a>foldr</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldr' :: (a -> b -> b) -> b -> IntMap a -> b

-- | <i>O(n)</i>. A strict version of <a>foldl</a>. Each application of the
groperator is evaluated before using the result in the next application.
grThis function is strict in the starting value.
foldl' :: (a -> b -> a) -> a -> IntMap b -> a

-- | <i>O(n)</i>. A strict version of <a>foldrWithKey</a>. Each application
grof the operator is evaluated before using the result in the next
grapplication. This function is strict in the starting value.
foldrWithKey' :: (Key -> a -> b -> b) -> b -> IntMap a -> b

-- | <i>O(n)</i>. A strict version of <a>foldlWithKey</a>. Each application
grof the operator is evaluated before using the result in the next
grapplication. This function is strict in the starting value.
foldlWithKey' :: (a -> Key -> b -> a) -> a -> IntMap b -> a

-- | <i>O(n)</i>. Return all elements of the map in the ascending order of
grtheir keys. Subject to list fusion.
gr
gr<pre>
grelems (fromList [(5,"a"), (3,"b")]) == ["b","a"]
grelems empty == []
gr</pre>
elems :: IntMap a -> [a]

-- | <i>O(n)</i>. Return all keys of the map in ascending order. Subject to
grlist fusion.
gr
gr<pre>
grkeys (fromList [(5,"a"), (3,"b")]) == [3,5]
grkeys empty == []
gr</pre>
keys :: IntMap a -> [Key]

-- | <i>O(n)</i>. An alias for <a>toAscList</a>. Returns all key/value
grpairs in the map in ascending key order. Subject to list fusion.
gr
gr<pre>
grassocs (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
grassocs empty == []
gr</pre>
assocs :: IntMap a -> [(Key, a)]

-- | <i>O(n*min(n,W))</i>. The set of all keys of the map.
gr
gr<pre>
grkeysSet (fromList [(5,"a"), (3,"b")]) == Data.IntSet.fromList [3,5]
grkeysSet empty == Data.IntSet.empty
gr</pre>
keysSet :: IntMap a -> IntSet

-- | <i>O(n)</i>. Build a map from a set of keys and a function which for
greach key computes its value.
gr
gr<pre>
grfromSet (\k -&gt; replicate k 'a') (Data.IntSet.fromList [3, 5]) == fromList [(5,"aaaaa"), (3,"aaa")]
grfromSet undefined Data.IntSet.empty == empty
gr</pre>
fromSet :: (Key -> a) -> IntSet -> IntMap a

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs. Subject to
grlist fusion.
gr
gr<pre>
grtoList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
grtoList empty == []
gr</pre>
toList :: IntMap a -> [(Key, a)]

-- | <i>O(n*min(n,W))</i>. Create a map from a list of key/value pairs.
gr
gr<pre>
grfromList [] == empty
grfromList [(5,"a"), (3,"b"), (5, "c")] == fromList [(5,"c"), (3,"b")]
grfromList [(5,"c"), (3,"b"), (5, "a")] == fromList [(5,"a"), (3,"b")]
gr</pre>
fromList :: [(Key, a)] -> IntMap a

-- | <i>O(n*min(n,W))</i>. Create a map from a list of key/value pairs with
gra combining function. See also <a>fromAscListWith</a>.
gr
gr<pre>
grfromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"c")] == fromList [(3, "ab"), (5, "cba")]
grfromListWith (++) [] == empty
gr</pre>
fromListWith :: (a -> a -> a) -> [(Key, a)] -> IntMap a

-- | <i>O(n*min(n,W))</i>. Build a map from a list of key/value pairs with
gra combining function. See also fromAscListWithKey'.
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grfromListWithKey f [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"c")] == fromList [(3, "3:a|b"), (5, "5:c|5:b|a")]
grfromListWithKey f [] == empty
gr</pre>
fromListWithKey :: (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs where the
grkeys are in ascending order. Subject to list fusion.
gr
gr<pre>
grtoAscList (fromList [(5,"a"), (3,"b")]) == [(3,"b"), (5,"a")]
gr</pre>
toAscList :: IntMap a -> [(Key, a)]

-- | <i>O(n)</i>. Convert the map to a list of key/value pairs where the
grkeys are in descending order. Subject to list fusion.
gr
gr<pre>
grtoDescList (fromList [(5,"a"), (3,"b")]) == [(5,"a"), (3,"b")]
gr</pre>
toDescList :: IntMap a -> [(Key, a)]

-- | <i>O(n)</i>. Build a map from a list of key/value pairs where the keys
grare in ascending order.
gr
gr<pre>
grfromAscList [(3,"b"), (5,"a")]          == fromList [(3, "b"), (5, "a")]
grfromAscList [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "b")]
gr</pre>
fromAscList :: [(Key, a)] -> IntMap a

-- | <i>O(n)</i>. Build a map from a list of key/value pairs where the keys
grare in ascending order, with a combining function on equal keys.
gr<i>The precondition (input list is ascending) is not checked.</i>
gr
gr<pre>
grfromAscListWith (++) [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "ba")]
gr</pre>
fromAscListWith :: (a -> a -> a) -> [(Key, a)] -> IntMap a

-- | <i>O(n)</i>. Build a map from a list of key/value pairs where the keys
grare in ascending order, with a combining function on equal keys.
gr<i>The precondition (input list is ascending) is not checked.</i>
gr
gr<pre>
grlet f key new_value old_value = (show key) ++ ":" ++ new_value ++ "|" ++ old_value
grfromAscListWithKey f [(3,"b"), (5,"a"), (5,"b")] == fromList [(3, "b"), (5, "5:b|a")]
gr</pre>
fromAscListWithKey :: (Key -> a -> a -> a) -> [(Key, a)] -> IntMap a

-- | <i>O(n)</i>. Build a map from a list of key/value pairs where the keys
grare in ascending order and all distinct. <i>The precondition (input
grlist is strictly ascending) is not checked.</i>
gr
gr<pre>
grfromDistinctAscList [(3,"b"), (5,"a")] == fromList [(3, "b"), (5, "a")]
gr</pre>
fromDistinctAscList :: forall a. [(Key, a)] -> IntMap a

-- | <i>O(n)</i>. Filter all values that satisfy some predicate.
gr
gr<pre>
grfilter (&gt; "a") (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
grfilter (&gt; "x") (fromList [(5,"a"), (3,"b")]) == empty
grfilter (&lt; "a") (fromList [(5,"a"), (3,"b")]) == empty
gr</pre>
filter :: (a -> Bool) -> IntMap a -> IntMap a

-- | <i>O(n)</i>. Filter all keys/values that satisfy some predicate.
gr
gr<pre>
grfilterWithKey (\k _ -&gt; k &gt; 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
filterWithKey :: (Key -> a -> Bool) -> IntMap a -> IntMap a

-- | <i>O(n+m)</i>. The restriction of a map to the keys in a set.
gr
gr<pre>
grm <a>restrictKeys</a> s = <a>filterWithKey</a> (k _ -&gt; k `<a>member'</a> s) m
gr</pre>
restrictKeys :: IntMap a -> IntSet -> IntMap a

-- | <i>O(n+m)</i>. Remove all the keys in a given set from a map.
gr
gr<pre>
grm <a>withoutKeys</a> s = <a>filterWithKey</a> (k _ -&gt; k `<a>notMember'</a> s) m
gr</pre>
withoutKeys :: IntMap a -> IntSet -> IntMap a

-- | <i>O(n)</i>. Partition the map according to some predicate. The first
grmap contains all elements that satisfy the predicate, the second all
grelements that fail the predicate. See also <a>split</a>.
gr
gr<pre>
grpartition (&gt; "a") (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a")
grpartition (&lt; "x") (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
grpartition (&gt; "x") (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
gr</pre>
partition :: (a -> Bool) -> IntMap a -> (IntMap a, IntMap a)

-- | <i>O(n)</i>. Partition the map according to some predicate. The first
grmap contains all elements that satisfy the predicate, the second all
grelements that fail the predicate. See also <a>split</a>.
gr
gr<pre>
grpartitionWithKey (\ k _ -&gt; k &gt; 3) (fromList [(5,"a"), (3,"b")]) == (singleton 5 "a", singleton 3 "b")
grpartitionWithKey (\ k _ -&gt; k &lt; 7) (fromList [(5,"a"), (3,"b")]) == (fromList [(3, "b"), (5, "a")], empty)
grpartitionWithKey (\ k _ -&gt; k &gt; 7) (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3, "b"), (5, "a")])
gr</pre>
partitionWithKey :: (Key -> a -> Bool) -> IntMap a -> (IntMap a, IntMap a)

-- | <i>O(n)</i>. Map values and collect the <a>Just</a> results.
gr
gr<pre>
grlet f x = if x == "a" then Just "new a" else Nothing
grmapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"
gr</pre>
mapMaybe :: (a -> Maybe b) -> IntMap a -> IntMap b

-- | <i>O(n)</i>. Map keys/values and collect the <a>Just</a> results.
gr
gr<pre>
grlet f k _ = if k &lt; 5 then Just ("key : " ++ (show k)) else Nothing
grmapMaybeWithKey f (fromList [(5,"a"), (3,"b")]) == singleton 3 "key : 3"
gr</pre>
mapMaybeWithKey :: (Key -> a -> Maybe b) -> IntMap a -> IntMap b

-- | <i>O(n)</i>. Map values and separate the <a>Left</a> and <a>Right</a>
grresults.
gr
gr<pre>
grlet f a = if a &lt; "c" then Left a else Right a
grmapEither f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (fromList [(3,"b"), (5,"a")], fromList [(1,"x"), (7,"z")])
gr
grmapEither (\ a -&gt; Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (empty, fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr</pre>
mapEither :: (a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)

-- | <i>O(n)</i>. Map keys/values and separate the <a>Left</a> and
gr<a>Right</a> results.
gr
gr<pre>
grlet f k a = if k &lt; 5 then Left (k * 2) else Right (a ++ a)
grmapEitherWithKey f (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (fromList [(1,2), (3,6)], fromList [(5,"aa"), (7,"zz")])
gr
grmapEitherWithKey (\_ a -&gt; Right a) (fromList [(5,"a"), (3,"b"), (1,"x"), (7,"z")])
gr    == (empty, fromList [(1,"x"), (3,"b"), (5,"a"), (7,"z")])
gr</pre>
mapEitherWithKey :: (Key -> a -> Either b c) -> IntMap a -> (IntMap b, IntMap c)

-- | <i>O(min(n,W))</i>. The expression (<tt><a>split</a> k map</tt>) is a
grpair <tt>(map1,map2)</tt> where all keys in <tt>map1</tt> are lower
grthan <tt>k</tt> and all keys in <tt>map2</tt> larger than <tt>k</tt>.
grAny key equal to <tt>k</tt> is found in neither <tt>map1</tt> nor
gr<tt>map2</tt>.
gr
gr<pre>
grsplit 2 (fromList [(5,"a"), (3,"b")]) == (empty, fromList [(3,"b"), (5,"a")])
grsplit 3 (fromList [(5,"a"), (3,"b")]) == (empty, singleton 5 "a")
grsplit 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", singleton 5 "a")
grsplit 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", empty)
grsplit 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], empty)
gr</pre>
split :: Key -> IntMap a -> (IntMap a, IntMap a)

-- | <i>O(min(n,W))</i>. Performs a <a>split</a> but also returns whether
grthe pivot key was found in the original map.
gr
gr<pre>
grsplitLookup 2 (fromList [(5,"a"), (3,"b")]) == (empty, Nothing, fromList [(3,"b"), (5,"a")])
grsplitLookup 3 (fromList [(5,"a"), (3,"b")]) == (empty, Just "b", singleton 5 "a")
grsplitLookup 4 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Nothing, singleton 5 "a")
grsplitLookup 5 (fromList [(5,"a"), (3,"b")]) == (singleton 3 "b", Just "a", empty)
grsplitLookup 6 (fromList [(5,"a"), (3,"b")]) == (fromList [(3,"b"), (5,"a")], Nothing, empty)
gr</pre>
splitLookup :: Key -> IntMap a -> (IntMap a, Maybe a, IntMap a)

-- | <i>O(1)</i>. Decompose a map into pieces based on the structure of the
grunderlying tree. This function is useful for consuming a map in
grparallel.
gr
grNo guarantee is made as to the sizes of the pieces; an internal, but
grdeterministic process determines this. However, it is guaranteed that
grthe pieces returned will be in ascending order (all elements in the
grfirst submap less than all elements in the second, and so on).
gr
grExamples:
gr
gr<pre>
grsplitRoot (fromList (zip [1..6::Int] ['a'..])) ==
gr  [fromList [(1,'a'),(2,'b'),(3,'c')],fromList [(4,'d'),(5,'e'),(6,'f')]]
gr</pre>
gr
gr<pre>
grsplitRoot empty == []
gr</pre>
gr
grNote that the current implementation does not return more than two
grsubmaps, but you should not depend on this behaviour because it can
grchange in the future without notice.
splitRoot :: IntMap a -> [IntMap a]

-- | <i>O(n+m)</i>. Is this a submap? Defined as (<tt><a>isSubmapOf</a> =
gr<a>isSubmapOfBy</a> (==)</tt>).
isSubmapOf :: Eq a => IntMap a -> IntMap a -> Bool

-- | <i>O(n+m)</i>. The expression (<tt><a>isSubmapOfBy</a> f m1 m2</tt>)
grreturns <a>True</a> if all keys in <tt>m1</tt> are in <tt>m2</tt>, and
grwhen <tt>f</tt> returns <a>True</a> when applied to their respective
grvalues. For example, the following expressions are all <a>True</a>:
gr
gr<pre>
grisSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
grisSubmapOfBy (&lt;=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
grisSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])
gr</pre>
gr
grBut the following are all <a>False</a>:
gr
gr<pre>
grisSubmapOfBy (==) (fromList [(1,2)]) (fromList [(1,1),(2,2)])
grisSubmapOfBy (&lt;) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
grisSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
gr</pre>
isSubmapOfBy :: (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool

-- | <i>O(n+m)</i>. Is this a proper submap? (ie. a submap but not equal).
grDefined as (<tt><a>isProperSubmapOf</a> = <a>isProperSubmapOfBy</a>
gr(==)</tt>).
isProperSubmapOf :: Eq a => IntMap a -> IntMap a -> Bool

-- | <i>O(n+m)</i>. Is this a proper submap? (ie. a submap but not equal).
grThe expression (<tt><a>isProperSubmapOfBy</a> f m1 m2</tt>) returns
gr<a>True</a> when <tt>m1</tt> and <tt>m2</tt> are not equal, all keys
grin <tt>m1</tt> are in <tt>m2</tt>, and when <tt>f</tt> returns
gr<a>True</a> when applied to their respective values. For example, the
grfollowing expressions are all <a>True</a>:
gr
gr<pre>
grisProperSubmapOfBy (==) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
grisProperSubmapOfBy (&lt;=) (fromList [(1,1)]) (fromList [(1,1),(2,2)])
gr</pre>
gr
grBut the following are all <a>False</a>:
gr
gr<pre>
grisProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1),(2,2)])
grisProperSubmapOfBy (==) (fromList [(1,1),(2,2)]) (fromList [(1,1)])
grisProperSubmapOfBy (&lt;)  (fromList [(1,1)])       (fromList [(1,1),(2,2)])
gr</pre>
isProperSubmapOfBy :: (a -> b -> Bool) -> IntMap a -> IntMap b -> Bool

-- | <i>O(min(n,W))</i>. The minimal key of the map. Returns <a>Nothing</a>
grif the map is empty.
lookupMin :: IntMap a -> Maybe (Key, a)

-- | <i>O(min(n,W))</i>. The maximal key of the map. Returns <a>Nothing</a>
grif the map is empty.
lookupMax :: IntMap a -> Maybe (Key, a)

-- | <i>O(min(n,W))</i>. The minimal key of the map. Calls <a>error</a> if
grthe map is empty. Use <a>minViewWithKey</a> if the map may be empty.
findMin :: IntMap a -> (Key, a)

-- | <i>O(min(n,W))</i>. The maximal key of the map. Calls <a>error</a> if
grthe map is empty. Use <a>maxViewWithKey</a> if the map may be empty.
findMax :: IntMap a -> (Key, a)

-- | <i>O(min(n,W))</i>. Delete the minimal key. Returns an empty map if
grthe map is empty.
gr
grNote that this is a change of behaviour for consistency with
gr<a>Map</a> – versions prior to 0.5 threw an error if the <a>IntMap</a>
grwas already empty.
deleteMin :: IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Delete the maximal key. Returns an empty map if
grthe map is empty.
gr
grNote that this is a change of behaviour for consistency with
gr<a>Map</a> – versions prior to 0.5 threw an error if the <a>IntMap</a>
grwas already empty.
deleteMax :: IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Delete and find the minimal element. This function
grthrows an error if the map is empty. Use <a>minViewWithKey</a> if the
grmap may be empty.
deleteFindMin :: IntMap a -> ((Key, a), IntMap a)

-- | <i>O(min(n,W))</i>. Delete and find the maximal element. This function
grthrows an error if the map is empty. Use <a>maxViewWithKey</a> if the
grmap may be empty.
deleteFindMax :: IntMap a -> ((Key, a), IntMap a)

-- | <i>O(min(n,W))</i>. Update the value at the minimal key.
gr
gr<pre>
grupdateMin (\ a -&gt; Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "Xb"), (5, "a")]
grupdateMin (\ _ -&gt; Nothing)         (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateMin :: (a -> Maybe a) -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Update the value at the maximal key.
gr
gr<pre>
grupdateMax (\ a -&gt; Just ("X" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "Xa")]
grupdateMax (\ _ -&gt; Nothing)         (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr</pre>
updateMax :: (a -> Maybe a) -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Update the value at the minimal key.
gr
gr<pre>
grupdateMinWithKey (\ k a -&gt; Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"3:b"), (5,"a")]
grupdateMinWithKey (\ _ _ -&gt; Nothing)                     (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
gr</pre>
updateMinWithKey :: (Key -> a -> Maybe a) -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Update the value at the maximal key.
gr
gr<pre>
grupdateMaxWithKey (\ k a -&gt; Just ((show k) ++ ":" ++ a)) (fromList [(5,"a"), (3,"b")]) == fromList [(3,"b"), (5,"5:a")]
grupdateMaxWithKey (\ _ _ -&gt; Nothing)                     (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
gr</pre>
updateMaxWithKey :: (Key -> a -> Maybe a) -> IntMap a -> IntMap a

-- | <i>O(min(n,W))</i>. Retrieves the minimal key of the map, and the map
grstripped of that element, or <a>Nothing</a> if passed an empty map.
minView :: IntMap a -> Maybe (a, IntMap a)

-- | <i>O(min(n,W))</i>. Retrieves the maximal key of the map, and the map
grstripped of that element, or <a>Nothing</a> if passed an empty map.
maxView :: IntMap a -> Maybe (a, IntMap a)

-- | <i>O(min(n,W))</i>. Retrieves the minimal (key,value) pair of the map,
grand the map stripped of that element, or <a>Nothing</a> if passed an
grempty map.
gr
gr<pre>
grminViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((3,"b"), singleton 5 "a")
grminViewWithKey empty == Nothing
gr</pre>
minViewWithKey :: IntMap a -> Maybe ((Key, a), IntMap a)

-- | <i>O(min(n,W))</i>. Retrieves the maximal (key,value) pair of the map,
grand the map stripped of that element, or <a>Nothing</a> if passed an
grempty map.
gr
gr<pre>
grmaxViewWithKey (fromList [(5,"a"), (3,"b")]) == Just ((5,"a"), singleton 3 "b")
grmaxViewWithKey empty == Nothing
gr</pre>
maxViewWithKey :: IntMap a -> Maybe ((Key, a), IntMap a)

-- | <i>O(n)</i>. Show the tree that implements the map. The tree is shown
grin a compressed, hanging format.

-- | <i>Deprecated: These debugging functions will be removed from this
grmodule. They are available from Data.IntMap.Internal.Debug.</i>
showTree :: Show a => IntMap a -> String

-- | <i>O(n)</i>. The expression (<tt><a>showTreeWith</a> hang wide
grmap</tt>) shows the tree that implements the map. If <tt>hang</tt> is
gr<a>True</a>, a <i>hanging</i> tree is shown otherwise a rotated tree
gris shown. If <tt>wide</tt> is <a>True</a>, an extra wide version is
grshown.

-- | <i>Deprecated: These debugging functions will be removed from this
grmodule. They are available from Data.IntMap.Internal.Debug.</i>
showTreeWith :: Show a => Bool -> Bool -> IntMap a -> String


-- | An efficient implementation of maps from integer keys to values
gr(dictionaries).
gr
grThis module re-exports the value lazy <a>Data.IntMap.Lazy</a> API,
grplus several deprecated value strict functions. Please note that these
grfunctions have different strictness properties than those in
gr<a>Data.IntMap.Strict</a>: they only evaluate the result of the
grcombining function. For example, the default value to
gr<a>insertWith'</a> is only evaluated if the combining function is
grcalled and uses it.
gr
grThese modules are intended to be imported qualified, to avoid name
grclashes with Prelude functions, e.g.
gr
gr<pre>
grimport Data.IntMap (IntMap)
grimport qualified Data.IntMap as IntMap
gr</pre>
gr
grThe implementation is based on <i>big-endian patricia trees</i>. This
grdata structure performs especially well on binary operations like
gr<a>union</a> and <a>intersection</a>. However, my benchmarks show that
grit is also (much) faster on insertions and deletions when compared to
gra generic size-balanced map implementation (see <a>Data.Map</a>).
gr
gr<ul>
gr<li>Chris Okasaki and Andy Gill, "<i>Fast Mergeable Integer Maps</i>",
grWorkshop on ML, September 1998, pages 77-86,
gr<a>http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.37.5452</a></li>
gr<li>D.R. Morrison, "/PATRICIA -- Practical Algorithm To Retrieve
grInformation Coded In Alphanumeric/", Journal of the ACM, 15(4),
grOctober 1968, pages 514-534.</li>
gr</ul>
gr
grOperation comments contain the operation time complexity in the Big-O
grnotation <a>http://en.wikipedia.org/wiki/Big_O_notation</a>. Many
groperations have a worst-case complexity of <i>O(min(n,W))</i>. This
grmeans that the operation can become linear in the number of elements
grwith a maximum of <i>W</i> -- the number of bits in an <tt>Int</tt>
gr(32 or 64).
module Data.IntMap

-- | <i>O(log n)</i>. Same as <a>insertWith</a>, but the result of the
grcombining function is evaluated to WHNF before inserted to the map.

-- | <i>Deprecated: As of version 0.5, replaced by <a>insertWith</a>.</i>
insertWith' :: (a -> a -> a) -> Key -> a -> IntMap a -> IntMap a

-- | <i>O(log n)</i>. Same as <a>insertWithKey</a>, but the result of the
grcombining function is evaluated to WHNF before inserted to the map.

-- | <i>Deprecated: As of version 0.5, replaced by
gr<a>insertWithKey</a>.</i>
insertWithKey' :: (Key -> a -> a -> a) -> Key -> a -> IntMap a -> IntMap a

-- | <i>O(n)</i>. Fold the values in the map using the given
grright-associative binary operator. This function is an equivalent of
gr<a>foldr</a> and is present for compatibility only.

-- | <i>Deprecated: As of version 0.5, replaced by <a>foldr</a>.</i>
fold :: (a -> b -> b) -> b -> IntMap a -> b

-- | <i>O(n)</i>. Fold the keys and values in the map using the given
grright-associative binary operator. This function is an equivalent of
gr<a>foldrWithKey</a> and is present for compatibility only.

-- | <i>Deprecated: As of version 0.5, replaced by <a>foldrWithKey</a>.</i>
foldWithKey :: (Key -> a -> b -> b) -> b -> IntMap a -> b

module Data.IntMap.Internal.Debug

-- | <i>O(n)</i>. Show the tree that implements the map. The tree is shown
grin a compressed, hanging format.
showTree :: Show a => IntMap a -> String

-- | <i>O(n)</i>. The expression (<tt><a>showTreeWith</a> hang wide
grmap</tt>) shows the tree that implements the map. If <tt>hang</tt> is
gr<a>True</a>, a <i>hanging</i> tree is shown otherwise a rotated tree
gris shown. If <tt>wide</tt> is <a>True</a>, an extra wide version is
grshown.
showTreeWith :: Show a => Bool -> Bool -> IntMap a -> String
