git.iko.soy/monoidal-containers
on master help sign in
11 files changed+2911−126
since splitsnapshots
unifiedsplit
.gitignore+3 .travis.yml+141−59 Changelog.md+45 README.md+4 default.nix+16 monoidal-containers.cabal+37−7 src/Data/HashMap/Monoidal.hs+89−30 src/Data/IntMap/Monoidal.hs+651 src/Data/IntMap/Monoidal/Strict.hs+651 src/Data/Map/Monoidal.hs+573−30 src/Data/Map/Monoidal/Strict.hs+701
.gitignore+3
@@ -1,2 +1,5 @@
11dist
2+dist-newstyle
23cabal.config
4+cabal.project.local
5+.ghc.environment.*
.travis.yml+141−59
@@ -1,71 +1,153 @@
1−# This file has been generated by `make_travis_yml_2.hs`
2−# see https://github.com/hvr/multi-ghc-travis for more information
1+# This Travis job script has been generated by a script via
2+#
3+# haskell-ci 'monoidal-containers.cabal' '-o' '.travis.yml'
4+#
5+# To regenerate the script (for example after adjusting tested-with) run
6+#
7+# haskell-ci regenerate
8+#
9+# For more information, see https://github.com/haskell-CI/haskell-ci
10+#
11+# version: 0.10.3
12+#
13+version: ~> 1.0
314language: c
4−sudo: false
5−
15+os: linux
16+dist: xenial
17+git:
18+ # whether to recursively clone submodules
19+ submodules: false
620cache:
721 directories:
822 - $HOME/.cabal/packages
923 - $HOME/.cabal/store
10−
24+ - $HOME/.hlint
1125before_cache:
12− - rm -fv $HOME/.cabal/packages/hackage.haskell.org/build-reports.log
13− - rm -fv $HOME/.cabal/packages/hackage.haskell.org/00-index.tar
14−
15−matrix:
26+ - rm -fv $CABALHOME/packages/hackage.haskell.org/build-reports.log
27+ # remove files that are regenerated by 'cabal update'
28+ - rm -fv $CABALHOME/packages/hackage.haskell.org/00-index.*
29+ - rm -fv $CABALHOME/packages/hackage.haskell.org/*.json
30+ - rm -fv $CABALHOME/packages/hackage.haskell.org/01-index.cache
31+ - rm -fv $CABALHOME/packages/hackage.haskell.org/01-index.tar
32+ - rm -fv $CABALHOME/packages/hackage.haskell.org/01-index.tar.idx
33+ - rm -rfv $CABALHOME/packages/head.hackage
34+jobs:
1635 include:
17− - env: CABALVER=1.24 GHCVER=7.4.2
18− compiler: ": #GHC 7.4.2"
19− addons: {apt: {packages: [cabal-install-1.24,ghc-7.4.2], sources: [hvr-ghc]}}
20− - env: CABALVER=1.24 GHCVER=7.6.3
21− compiler: ": #GHC 7.6.3"
22− addons: {apt: {packages: [cabal-install-1.24,ghc-7.6.3], sources: [hvr-ghc]}}
23− - env: CABALVER=1.24 GHCVER=7.8.4
24− compiler: ": #GHC 7.8.4"
25− addons: {apt: {packages: [cabal-install-1.24,ghc-7.8.4], sources: [hvr-ghc]}}
26− - env: CABALVER=1.24 GHCVER=8.0.1
27− compiler: ": #GHC 8.0.1"
28− addons: {apt: {packages: [cabal-install-1.24,ghc-8.0.1], sources: [hvr-ghc]}}
29−
36+ - compiler: ghc-8.10.2
37+ addons: {"apt":{"sources":[{"sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main","key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286"}],"packages":["ghc-8.10.2","cabal-install-3.2"]}}
38+ os: linux
39+ - compiler: ghc-8.8.4
40+ addons: {"apt":{"sources":[{"sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main","key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286"}],"packages":["ghc-8.8.4","cabal-install-3.2"]}}
41+ os: linux
42+ - compiler: ghc-8.6.5
43+ addons: {"apt":{"sources":[{"sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main","key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286"}],"packages":["ghc-8.6.5","cabal-install-3.2"]}}
44+ os: linux
45+ - compiler: ghc-8.4.3
46+ addons: {"apt":{"sources":[{"sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main","key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286"}],"packages":["ghc-8.4.3","cabal-install-3.2"]}}
47+ os: linux
48+ - compiler: ghc-8.2.2
49+ addons: {"apt":{"sources":[{"sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main","key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286"}],"packages":["ghc-8.2.2","cabal-install-3.2"]}}
50+ os: linux
51+ - compiler: ghc-8.0.2
52+ addons: {"apt":{"sources":[{"sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main","key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286"}],"packages":["ghc-8.0.2","cabal-install-3.2"]}}
53+ os: linux
54+ - compiler: ghc-7.8.4
55+ addons: {"apt":{"sources":[{"sourceline":"deb http://ppa.launchpad.net/hvr/ghc/ubuntu xenial main","key_url":"https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x063dab2bdc0b3f9fcebc378bff3aeacef6f88286"}],"packages":["ghc-7.8.4","cabal-install-3.2"]}}
56+ os: linux
3057before_install:
31− - unset CC
32− - export PATH=/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$PATH
33−
58+ - HC=$(echo "/opt/$CC/bin/ghc" | sed 's/-/\//')
59+ - WITHCOMPILER="-w $HC"
60+ - HADDOCK=$(echo "/opt/$CC/bin/haddock" | sed 's/-/\//')
61+ - HCPKG="$HC-pkg"
62+ - unset CC
63+ - CABAL=/opt/ghc/bin/cabal
64+ - CABALHOME=$HOME/.cabal
65+ - export PATH="$CABALHOME/bin:$PATH"
66+ - TOP=$(pwd)
67+ - "HCNUMVER=$(${HC} --numeric-version|perl -ne '/^(\\d+)\\.(\\d+)\\.(\\d+)(\\.(\\d+))?$/; print(10000 * $1 + 100 * $2 + ($3 == 0 ? $5 != 1 : $3))')"
68+ - echo $HCNUMVER
69+ - CABAL="$CABAL -vnormal+nowrap"
70+ - set -o pipefail
71+ - TEST=--enable-tests
72+ - BENCH=--enable-benchmarks
73+ - HEADHACKAGE=false
74+ - rm -f $CABALHOME/config
75+ - |
76+ echo "verbose: normal +nowrap +markoutput" >> $CABALHOME/config
77+ echo "remote-build-reporting: anonymous" >> $CABALHOME/config
78+ echo "write-ghc-environment-files: always" >> $CABALHOME/config
79+ echo "remote-repo-cache: $CABALHOME/packages" >> $CABALHOME/config
80+ echo "logs-dir: $CABALHOME/logs" >> $CABALHOME/config
81+ echo "world-file: $CABALHOME/world" >> $CABALHOME/config
82+ echo "extra-prog-path: $CABALHOME/bin" >> $CABALHOME/config
83+ echo "symlink-bindir: $CABALHOME/bin" >> $CABALHOME/config
84+ echo "installdir: $CABALHOME/bin" >> $CABALHOME/config
85+ echo "build-summary: $CABALHOME/logs/build.log" >> $CABALHOME/config
86+ echo "store-dir: $CABALHOME/store" >> $CABALHOME/config
87+ echo "install-dirs user" >> $CABALHOME/config
88+ echo " prefix: $CABALHOME" >> $CABALHOME/config
89+ echo "repository hackage.haskell.org" >> $CABALHOME/config
90+ echo " url: http://hackage.haskell.org/" >> $CABALHOME/config
3491install:
35− - cabal --version
36− - BENCH=${BENCH---enable-benchmarks}
37− - TEST=${TEST---enable-tests}
38− - echo "$(ghc --version) [$(ghc --print-project-git-commit-id 2> /dev/null || echo '?')]"
39− - if [ -f $HOME/.cabal/packages/hackage.haskell.org/00-index.tar.gz ];
40− then
41− zcat $HOME/.cabal/packages/hackage.haskell.org/00-index.tar.gz >
42− $HOME/.cabal/packages/hackage.haskell.org/00-index.tar;
43− fi
44− - travis_retry cabal update -v
45− - sed -i 's/^jobs:/-- jobs:/' ${HOME}/.cabal/config
46− - cabal new-build ${TEST} ${BENCH} --dep
47−
48−# Here starts the actual work to be performed for the package under test;
49−# any command which exits with a non-zero exit code causes the build to fail.
92+ - ${CABAL} --version
93+ - echo "$(${HC} --version) [$(${HC} --print-project-git-commit-id 2> /dev/null || echo '?')]"
94+ - |
95+ echo "program-default-options" >> $CABALHOME/config
96+ echo " ghc-options: $GHCJOBS +RTS -M6G -RTS" >> $CABALHOME/config
97+ - cat $CABALHOME/config
98+ - rm -fv cabal.project cabal.project.local cabal.project.freeze
99+ - travis_retry ${CABAL} v2-update -v
100+ # Generate cabal.project
101+ - rm -rf cabal.project cabal.project.local cabal.project.freeze
102+ - touch cabal.project
103+ - |
104+ echo "packages: ." >> cabal.project
105+ - if [ $HCNUMVER -ge 80200 ] ; then echo 'package monoidal-containers' >> cabal.project ; fi
106+ - "if [ $HCNUMVER -ge 80200 ] ; then echo ' ghc-options: -Werror=missing-methods' >> cabal.project ; fi"
107+ - |
108+ - "for pkg in $($HCPKG list --simple-output); do echo $pkg | sed 's/-[^-]*$//' | (grep -vE -- '^(monoidal-containers)$' || true) | sed 's/^/constraints: /' | sed 's/$/ installed/' >> cabal.project.local; done"
109+ - cat cabal.project || true
110+ - cat cabal.project.local || true
111+ - if [ -f "./configure.ac" ]; then (cd "." && autoreconf -i); fi
112+ - ${CABAL} v2-freeze $WITHCOMPILER ${TEST} ${BENCH}
113+ - "cat cabal.project.freeze | sed -E 's/^(constraints: *| *)//' | sed 's/any.//'"
114+ - rm cabal.project.freeze
115+ - travis_wait 40 ${CABAL} v2-build $WITHCOMPILER ${TEST} ${BENCH} --dep -j2 all
116+ - travis_wait 40 ${CABAL} v2-build $WITHCOMPILER --disable-tests --disable-benchmarks --dep -j2 all
50117script:
51− - if [ -f configure.ac ]; then autoreconf -i; fi
52− # this builds all libraries and executables (including tests/benchmarks)
53− - cabal new-build ${TEST} ${BENCH} -v2 # -v2 provides useful information for debugging
54−
55− # there's no 'cabal new-test' yet, so let's emulate for now
56− - TESTS=( $(awk 'tolower($0) ~ /^test-suite / { print $2 }' *.cabal) );
57− RC=true; for T in ${TESTS[@]}; do echo "== $T ==";
58− if dist-newstyle/build/*/build/$T/$T; then echo "= $T OK =";
59− else echo "= $T FAILED ="; RC=false; fi; done; $RC
60− - cabal sdist # test that a source-distribution can be generated
61−
62− # Check that the resulting source distribution can be built w/o and w tests
63− - SRC_BASENAME=$(cabal info . | awk '{print $2;exit}')
64− - tar -C dist/ -xf dist/$SRC_BASENAME.tar.gz
65− - "echo 'packages: *.cabal' > dist/$SRC_BASENAME/cabal.project"
66− - cd dist/$SRC_BASENAME/
67− - cabal new-build --disable-tests --disable-benchmarks
68− - rm -rf ./dist-newstyle
69− - cabal new-build ${TEST} ${BENCH}
118+ - DISTDIR=$(mktemp -d /tmp/dist-test.XXXX)
119+ # Packaging...
120+ - ${CABAL} v2-sdist all
121+ # Unpacking...
122+ - mv dist-newstyle/sdist/*.tar.gz ${DISTDIR}/
123+ - cd ${DISTDIR} || false
124+ - find . -maxdepth 1 -type f -name '*.tar.gz' -exec tar -xvf '{}' \;
125+ - find . -maxdepth 1 -type f -name '*.tar.gz' -exec rm '{}' \;
126+ - PKGDIR_monoidal_containers="$(find . -maxdepth 1 -type d -regex '.*/monoidal-containers-[0-9.]*')"
127+ # Generate cabal.project
128+ - rm -rf cabal.project cabal.project.local cabal.project.freeze
129+ - touch cabal.project
130+ - |
131+ echo "packages: ${PKGDIR_monoidal_containers}" >> cabal.project
132+ - if [ $HCNUMVER -ge 80200 ] ; then echo 'package monoidal-containers' >> cabal.project ; fi
133+ - "if [ $HCNUMVER -ge 80200 ] ; then echo ' ghc-options: -Werror=missing-methods' >> cabal.project ; fi"
134+ - |
135+ - "for pkg in $($HCPKG list --simple-output); do echo $pkg | sed 's/-[^-]*$//' | (grep -vE -- '^(monoidal-containers)$' || true) | sed 's/^/constraints: /' | sed 's/$/ installed/' >> cabal.project.local; done"
136+ - cat cabal.project || true
137+ - cat cabal.project.local || true
138+ # Building...
139+ # this builds all libraries and executables (without tests/benchmarks)
140+ - ${CABAL} v2-build $WITHCOMPILER --disable-tests --disable-benchmarks all
141+ # Building with tests and benchmarks...
142+ # build & run tests, build benchmarks
143+ - ${CABAL} v2-build $WITHCOMPILER ${TEST} ${BENCH} all
144+ # cabal check...
145+ - (cd ${PKGDIR_monoidal_containers} && ${CABAL} -vnormal check)
146+ # haddock...
147+ - ${CABAL} v2-haddock $WITHCOMPILER --with-haddock $HADDOCK ${TEST} ${BENCH} all
148+ # Building without installed constraints for packages in global-db...
149+ - rm -f cabal.project.local
150+ - ${CABAL} v2-build $WITHCOMPILER --disable-tests --disable-benchmarks all
70151
152+# REGENDATA ("0.10.3",["monoidal-containers.cabal","-o",".travis.yml"])
71153# EOF
Changelog.mdnew file+45
@@ -0,0 +1,45 @@
1+# Monoidal containers
2+
3+# 0.6.0.1
4+
5+ * Support semialign-1.1
6+
7+# 0.6
8+
9+ * Deprecates 0.5.* and reverts behavior of fromList, insert, mapKeys, etc. to match behavior in Data.Map. The only difference in behavior between Data.Map.Monoidal.MonoidalMap and Data.Map.Map is now the semigroup and monoid instances (as was the case in 0.4 and earlier).
10+ * Fix the argument order of Data.HashMap.Monoidal.insert
11+ * Remove Data.HashMap.Monoidal.insertOrReplace as it is now identical to Data.HashMap.Monoidal.insert
12+ * Added Data.HashMap.Monoidal.insertWith and Data.HashMap.Monoidal.fromListWith
13+
14+# 0.5.0.1
15+
16+ * Add a flag, `split-these`, to select whether to use the new "these"/"semialign" packages or the older combined "these" package.
17+ * Add default.nix to make it easier to hack on this package in nix
18+
19+# 0.5.0.0
20+
21+ * Added Data.IntMap.Monoidal and Data.IntMap.Monoidal.Strict, corresponding to Data.IntMap and Data.IntMap.Strict
22+ * Make `fromList`, `insert`, and `mapKeys` from `Data.Map.Monoidal` and `Data.Map.Monoidal.Strict` require `Semigroup` on values to properly capture monoidal behavior instead of reverting to the left-biased semantics of `Data.Map`.
23+ * Add Align instances and, for sufficiently recent versions of `these`, Semialign instances
24+ * Support `these` 0.8.0
25+
26+# 0.4.0.0
27+
28+General changes:
29+
30+ * Added support for `unordered-containers < 0.2.8`
31+ * Added many more functions in `Data.Map.[Strict.]Monoid`
32+
33+Weakened `Monoid` constraints to `Semigroup` whenever possible as enabled by the
34+[Semigroup-Monoid
35+proposal](https://prime.haskell.org/wiki/Libraries/Proposals/SemigroupMonoid).
36+This includes,
37+
38+ * the `Monoid` instance of `MonoidalHashMap` and `MonoidalMap`
39+ * the `IsList` instance of `MonoidalHashMap` and `MonoidalMap`
40+ * the `modifyDef` and `mapKeys` functions of `MonoidalHashMap`
41+
42+
43+# 0.3 and earlier
44+
45+Pre-history
README.md+4
@@ -6,3 +6,7 @@ of the values to be used when resolving conflicts between keys when merging
66structures. Sadly, these are not the semantics offered by the provided
77instances. This package provides `newtypes` with an appropriate set of
88instances and utility functions to make them usable.
9+
10+## Important Note
11+
12+This library is designed to be a drop-in replacement for `Data.Map` and similar. Absolutely no other semantic changes are introduced in functions like `fromList` or `insert`. They are still left-biased like `Data.Map`'s. The only difference is in instances for `Semigroup`.
default.nixnew file+16
@@ -0,0 +1,16 @@
1+{ mkDerivation, aeson, base, containers, deepseq, hashable, lens
2+, newtype, semialign, semigroups, stdenv, these
3+, unordered-containers
4+}:
5+mkDerivation {
6+ pname = "monoidal-containers";
7+ version = "0.6";
8+ src = ./.;
9+ libraryHaskellDepends = [
10+ aeson base containers deepseq hashable lens newtype semialign
11+ semigroups these unordered-containers
12+ ];
13+ homepage = "http://github.com/bgamari/monoidal-containers";
14+ description = "Containers with monoidal accumulation";
15+ license = stdenv.lib.licenses.bsd3;
16+}
monoidal-containers.cabal+37−7
@@ -1,7 +1,19 @@
11name: monoidal-containers
2−version: 0.1.2.5
2+version: 0.6.0.1
33synopsis: Containers with monoidal accumulation
4−description: Containers with monoidal accumulation
4+description:
5+ Containers with merging via monoidal accumulation. The 'Monoid' instances
6+ provided by the @containers@ and @unordered-containers@ packages merge
7+ structures in a left-biased manner instead of using the underlying monoidal
8+ structure of the value.
9+ .
10+ This package wraps the types provided by these packages, but provides @Monoid@
11+ instances implemented in terms of the value type's 'mappend'. For instance,
12+ the @Monoid@ @Map@ instance looks like,
13+ .
14+ @
15+ instance (Ord k, Semigroup a) => Monoid (MonoidalMap k a)
16+ @
517homepage: http://github.com/bgamari/monoidal-containers
618license: BSD3
719license-file: LICENSE
@@ -11,26 +23,44 @@ copyright: (c) 2014 Ben Gamari
1123category: Data
1224build-type: Simple
1325cabal-version: >=1.10
14−tested-with: GHC ==7.4.2, GHC ==7.6.3, GHC ==7.8.4, GHC ==8.0.1
26+extra-source-files: Changelog.md
27+tested-with: GHC ==7.8.4, GHC ==8.0.2, GHC ==8.2.2, GHC ==8.4.3, GHC ==8.6.5, GHC ==8.8.4, GHC ==8.10.2
1528
1629source-repository head
1730 type: git
1831 location: git://github.com/bgamari/monoidal-containers
1932
33+flag split-these
34+ description: Use split these/semialign packages
35+ manual: False
36+ default: True
37+
2038library
2139 exposed-modules: Data.Map.Monoidal
2240 Data.HashMap.Monoidal
41+ Data.IntMap.Monoidal
42+ Data.Map.Monoidal.Strict
43+ Data.IntMap.Monoidal.Strict
2344 other-extensions: CPP,
2445 MultiParamTypeClasses,
2546 GeneralizedNewtypeDeriving,
2647 DeriveTraversable,
2748 DeriveDataTypeable
28− build-depends: base >=4.7 && <4.10,
49+ build-depends: base >=4.7 && <4.16,
50+ aeson >=1.0 && <1.6,
51+ containers >=0.5 && <0.7,
2952 deepseq >=1.3 && <1.5,
30− containers >=0.5 && <0.6,
3153 unordered-containers >= 0.2 && < 0.3,
32− hashable >= 1.2 && < 1.3,
54+ hashable >= 1.2 && < 1.4,
3355 lens >=4.4 && <5,
34− newtype >=0.2 && <0.3
56+ newtype >=0.2 && <0.3,
57+ semigroups >= 0.18 && < 0.20
58+
59+ if flag(split-these)
60+ build-depends: these >= 1 && <1.2,
61+ semialign >=1 && <1.2
62+ else
63+ build-depends: these >= 0.7 && <0.9
64+
3565 hs-source-dirs: src
3666 default-language: Haskell2010
src/Data/HashMap/Monoidal.hs+89−30
@@ -16,10 +16,11 @@
1616-- underlying 'Map'.
1717
1818module Data.HashMap.Monoidal
19− ( MonoidalHashMap
19+ ( MonoidalHashMap(..)
2020 -- * Often-needed functions
2121 , toList
2222 , fromList
23+ , fromListWith
2324 , singleton
2425 , size
2526 , member
@@ -30,6 +31,8 @@ module Data.HashMap.Monoidal
3031 , keys
3132 , delete
3233 , mapKeys
34+ , insert
35+ , insertWith
3336 , modify
3437 , modifyDef
3538 , map
@@ -38,7 +41,7 @@ module Data.HashMap.Monoidal
3841
3942import Prelude hiding (lookup, map)
4043import Data.Maybe (fromMaybe)
41−import Data.Monoid
44+import Data.Semigroup
4245import Data.Foldable (Foldable)
4346import Control.Applicative (pure)
4447import Data.Data (Data)
@@ -51,27 +54,49 @@ import qualified GHC.Exts as Exts
5154import Control.DeepSeq
5255import qualified Data.HashMap.Strict as M
5356import Data.Hashable (Hashable)
57+#if MIN_VERSION_unordered_containers(0,2,8)
58+import Data.Hashable.Lifted (Hashable1)
59+#endif
5460import Control.Lens
5561import Control.Newtype
62+import Data.Align
63+#ifdef MIN_VERSION_semialign
64+import Data.Semialign (Unalign)
65+#if MIN_VERSION_semialign(1,1,0)
66+import Data.Zip (Zip)
67+#endif
68+#endif
5669
5770-- | A 'HashMap' with monoidal accumulation
58−newtype MonoidalHashMap k a = MM (M.HashMap k a)
59− deriving (Show, Read, Functor, Eq, NFData,
60− Foldable, Traversable,
61− Data, Typeable)
71+newtype MonoidalHashMap k a = MonoidalHashMap { getMonoidalHashMap :: M.HashMap k a }
72+ deriving ( Show, Read, Functor, Eq, NFData
73+ , Foldable, Traversable, Data, Typeable, Hashable, Align
74+#if MIN_VERSION_unordered_containers(0,2,8)
75+ , Hashable1
76+#endif
77+#if MIN_VERSION_these(0,8,0)
78+ , Semialign
79+#endif
80+#ifdef MIN_VERSION_semialign
81+ , Unalign
82+#if MIN_VERSION_semialign(1,1,0)
83+ , Zip
84+#endif
85+#endif
86+ )
6287
6388type instance Index (MonoidalHashMap k a) = k
6489type instance IxValue (MonoidalHashMap k a) = a
6590instance (Eq k, Hashable k) => Ixed (MonoidalHashMap k a) where
66− ix k f (MM m) = case M.lookup k m of
67− Just v -> f v <&> \v' -> MM (M.insert k v' m)
68− Nothing -> pure (MM m)
91+ ix k f (MonoidalHashMap m) = case M.lookup k m of
92+ Just v -> f v <&> \v' -> MonoidalHashMap (M.insert k v' m)
93+ Nothing -> pure (MonoidalHashMap m)
6994 {-# INLINE ix #-}
7095
7196instance (Eq k, Hashable k) => At (MonoidalHashMap k a) where
72− at k f (MM m) = f mv <&> \r -> case r of
73− Nothing -> maybe (MM m) (const (MM $ M.delete k m)) mv
74− Just v' -> MM $ M.insert k v' m
97+ at k f (MonoidalHashMap m) = f mv <&> \r -> case r of
98+ Nothing -> maybe (MonoidalHashMap m) (const (MonoidalHashMap $ M.delete k m)) mv
99+ Just v' -> MonoidalHashMap $ M.insert k v' m
75100 where mv = M.lookup k m
76101 {-# INLINE at #-}
77102
@@ -80,11 +105,11 @@ instance Each (MonoidalHashMap k a) (MonoidalHashMap k b) a b
80105instance (Eq k, Hashable k) => FunctorWithIndex k (MonoidalHashMap k)
81106instance (Eq k, Hashable k) => FoldableWithIndex k (MonoidalHashMap k)
82107instance (Eq k, Hashable k) => TraversableWithIndex k (MonoidalHashMap k) where
83− itraverse f (MM m) = fmap MM $ itraverse f m
108+ itraverse f (MonoidalHashMap m) = fmap MonoidalHashMap $ itraverse f m
84109 {-# INLINE itraverse #-}
85110
86111instance AsEmpty (MonoidalHashMap k a) where
87− _Empty = nearly (MM M.empty) (M.null . unpack)
112+ _Empty = nearly (MonoidalHashMap M.empty) (M.null . unpack)
88113 {-# INLINE _Empty #-}
89114
90115instance Wrapped (MonoidalHashMap k a) where
@@ -92,22 +117,28 @@ instance Wrapped (MonoidalHashMap k a) where
92117 _Wrapped' = iso unpack pack
93118 {-# INLINE _Wrapped' #-}
94119
95−instance (Eq k, Hashable k, Monoid a) => Monoid (MonoidalHashMap k a) where
96− mempty = MM mempty
120+instance (Eq k, Hashable k, Semigroup a) => Semigroup (MonoidalHashMap k a) where
121+ MonoidalHashMap a <> MonoidalHashMap b = MonoidalHashMap $ M.unionWith (<>) a b
122+ {-# INLINE (<>) #-}
123+
124+instance (Eq k, Hashable k, Semigroup a) => Monoid (MonoidalHashMap k a) where
125+ mempty = MonoidalHashMap mempty
97126 {-# INLINE mempty #-}
98− MM a `mappend` MM b = MM $ M.unionWith mappend a b
127+#if !(MIN_VERSION_base(4,11,0))
128+ mappend (MonoidalHashMap a) (MonoidalHashMap b) = MonoidalHashMap $ M.unionWith (<>) a b
99129 {-# INLINE mappend #-}
130+#endif
100131
101132instance Newtype (MonoidalHashMap k a) (M.HashMap k a) where
102− pack = MM
133+ pack = MonoidalHashMap
103134 {-# INLINE pack #-}
104− unpack (MM a) = a
135+ unpack (MonoidalHashMap a) = a
105136 {-# INLINE unpack #-}
106137
107138#if MIN_VERSION_base(4,7,0)
108−instance (Eq k, Hashable k, Monoid a) => Exts.IsList (MonoidalHashMap k a) where
139+instance (Eq k, Hashable k, Semigroup a) => Exts.IsList (MonoidalHashMap k a) where
109140 type Item (MonoidalHashMap k a) = (k, a)
110− fromList = MM . M.fromListWith mappend
141+ fromList = MonoidalHashMap . M.fromList
111142 {-# INLINE fromList #-}
112143 toList = M.toList . unpack
113144 {-# INLINE toList #-}
@@ -115,7 +146,7 @@ instance (Eq k, Hashable k, Monoid a) => Exts.IsList (MonoidalHashMap k a) where
115146
116147-- | /O(1)/. A map with a single element.
117148singleton :: (Eq k, Hashable k) => k -> a -> MonoidalHashMap k a
118−singleton k a = MM $ M.singleton k a
149+singleton k a = MonoidalHashMap $ M.singleton k a
119150{-# INLINE singleton #-}
120151
121152-- | /O(1)/. The number of elements in the map.
@@ -148,12 +179,11 @@ lookupM k = fromMaybe mempty . M.lookup k . unpack
148179-- | /O(log n)/. Delete a key and its value from the map. When the key is not
149180-- a member of the map, the original map is returned.
150181delete :: (Eq k, Hashable k) => k -> MonoidalHashMap k a -> MonoidalHashMap k a
151−delete k = _Wrapping' MM %~ M.delete k
182+delete k = _Wrapping' MonoidalHashMap %~ M.delete k
152183{-# INLINE delete #-}
153184
154185-- | /O(n)/.
155−-- Return all elements of the map in the ascending order of their keys.
156−-- Subject to list fusion.
186+-- Return a list of this map's values. The list is produced lazily.
157187elems :: MonoidalHashMap k a -> [a]
158188elems = M.elems . unpack
159189{-# INLINE elems #-}
@@ -165,17 +195,45 @@ keys = M.keys . unpack
165195{-# INLINE keys #-}
166196
167197-- | /O(n*log n)/. Construct a map with the supplied mappings. If the list
168−-- contains duplicate mappings, the later mappings take precedence.
198+-- contains duplicate mappings, values will be replaced.
169199fromList :: (Eq k, Hashable k) => [(k,a)] -> MonoidalHashMap k a
170200fromList = pack . M.fromList
171201{-# INLINE fromList #-}
172202
173203-- | /O(n*log n)/. Construct a map with the supplied mappings. If the list
174−-- contains duplicate mappings, the later mappings take precedence.
204+-- contains duplicate mappings, values will be merged using the provided combining function.
205+fromListWith :: (Eq k, Hashable k) => (a -> a -> a) -> [(k,a)] -> MonoidalHashMap k a
206+fromListWith f = pack . M.fromListWith f
207+{-# INLINE fromListWith #-}
208+
209+
210+-- | /O(n*log n)/. Return a list of this map's elements. The list is produced
211+-- lazily. The order of its elements is unspecified.
175212toList :: MonoidalHashMap k a -> [(k,a)]
176213toList = M.toList . unpack
177214{-# INLINE toList #-}
178215
216+-- | /O(log n)/. Insert a value on some key, if it exists replace the value.
217+insert :: (Hashable k, Eq k)
218+ => k
219+ -> a
220+ -> MonoidalHashMap k a
221+ -> MonoidalHashMap k a
222+insert k x = pack
223+ . M.insert k x
224+ . unpack
225+
226+-- | /O(log n)/. Insert a value on some key, if it exists apply the combining function.
227+insertWith :: (Hashable k, Eq k)
228+ => (a -> a -> a)
229+ -> k
230+ -> a
231+ -> MonoidalHashMap k a
232+ -> MonoidalHashMap k a
233+insertWith f k x = pack
234+ . M.insertWith f k x
235+ . unpack
236+
179237-- | /O(log n)/. Modify a value on some key with a function, if value
180238-- under key doesn't exist -- use mempty.
181239modify :: (Monoid a, Hashable k, Eq k)
@@ -189,7 +247,7 @@ modify f k = pack
189247
190248-- | /O(log n)/. Modify a value on some key with a function, providing
191249-- a default value if that key doesn't exist.
192−modifyDef :: (Monoid a, Hashable k, Eq k)
250+modifyDef :: (Hashable k, Eq k)
193251 => a -> (a -> a)
194252 -> k -> MonoidalHashMap k a
195253 -> MonoidalHashMap k a
@@ -198,8 +256,9 @@ modifyDef d f k = pack
198256 . unpack
199257{-# INLINE modifyDef #-}
200258
201−-- | /O(n)/. Map a function to each key of a map
202−mapKeys :: (Monoid a, Hashable k, Eq k, Hashable k', Eq k')
259+-- | /O(n)/. Map a function to each key of a map, if it will result
260+-- in duplicated mappings, their values will be merged in unspecified order
261+mapKeys :: (Hashable k, Eq k, Hashable k', Eq k')
203262 => (k -> k') -> MonoidalHashMap k a -> MonoidalHashMap k' a
204263mapKeys f = fromList
205264 . fmap (\(k, v) -> (f k, v))
src/Data/IntMap/Monoidal.hsnew file+651
@@ -0,0 +1,651 @@
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE ScopedTypeVariables #-}
3+{-# LANGUAGE MultiParamTypeClasses #-}
4+{-# LANGUAGE TypeFamilies #-}
5+{-# LANGUAGE FlexibleInstances #-}
6+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
7+{-# LANGUAGE DeriveTraversable #-}
8+{-# LANGUAGE DeriveDataTypeable #-}
9+{-# LANGUAGE StandaloneDeriving #-}
10+
11+-- | This module provides a 'Data.IntMap' variant which uses the value's
12+-- 'Monoid' instance to accumulate conflicting entries when merging
13+-- 'Map's.
14+--
15+-- While some functions mirroring those of 'Data.IntMap' are provided
16+-- here for convenience, more specialized needs will likely want to use
17+-- either the 'Newtype' or 'Wrapped' instances to manipulate the
18+-- underlying 'Map'.
19+
20+module Data.IntMap.Monoidal
21+ ( MonoidalIntMap(..)
22+ -- * Often-needed functions
23+ , singleton
24+ , size
25+ , member
26+ , notMember
27+ , findWithDefault
28+ , assocs
29+ , elems
30+ , keys
31+ , (!)
32+ , (\\)
33+ , adjust
34+ , adjustWithKey
35+ , alter
36+ , delete
37+-- , deleteAt
38+ , deleteFindMax
39+ , deleteFindMin
40+ , deleteMax
41+ , deleteMin
42+ , difference
43+ , differenceWith
44+ , differenceWithKey
45+-- , elemAt
46+ , empty
47+ , filter
48+ , filterWithKey
49+-- , findIndex
50+ , findMax
51+ , findMin
52+ , foldMapWithKey
53+ , foldl
54+ , foldl'
55+ , foldlWithKey
56+ , foldlWithKey'
57+ , foldr
58+ , foldr'
59+ , foldrWithKey
60+ , foldrWithKey'
61+ , fromAscList
62+ , fromAscListWith
63+ , fromAscListWithKey
64+ , fromDistinctAscList
65+ , fromDistinctList
66+ , fromList
67+ , fromListWith
68+ , fromListWithKey
69+ , fromSet
70+ , insert
71+ , insertLookupWithKey
72+ , insertWith
73+ , insertWithKey
74+ , intersectionWith
75+ , intersectionWithKey
76+ , isProperSubmapOf
77+ , isProperSubmapOfBy
78+ , isSubmapOf
79+ , isSubmapOfBy
80+ , keysSet
81+ , lookup
82+ , lookupGE
83+ , lookupGT
84+-- , lookupIndex
85+ , lookupLE
86+ , lookupLT
87+ , map
88+ , mapAccum
89+ , mapAccumRWithKey
90+ , mapAccumWithKey
91+ , mapEither
92+ , mapEitherWithKey
93+ , mapKeys
94+ , mapKeysMonotonic
95+ , mapKeysWith
96+ , mapMaybe
97+ , mapMaybeWithKey
98+ , mapWithKey
99+ , maxView
100+ , maxViewWithKey
101+ , mergeWithKey
102+ , minView
103+ , minViewWithKey
104+ , null
105+ , partition
106+ , partitionWithKey
107+ , split
108+ , splitLookup
109+ , splitRoot
110+ , toAscList
111+ , toDescList
112+ , toList
113+ , traverseWithKey
114+ , unionWith
115+ , unionWithKey
116+ , unionsWith
117+ , update
118+-- , updateAt
119+ , updateLookupWithKey
120+ , updateMax
121+ , updateMaxWithKey
122+ , updateMin
123+ , updateMinWithKey
124+ , updateWithKey
125+-- , valid
126+ -- , showTree
127+ -- , showTreeWith
128+ ) where
129+
130+import Prelude hiding (null, lookup, map, foldl, foldr, filter)
131+
132+import Data.Coerce (coerce)
133+import Data.IntSet (IntSet)
134+import Data.Semigroup
135+import Data.Foldable (Foldable)
136+import Data.Traversable (Traversable)
137+import Control.Applicative (Applicative, pure)
138+import Data.Data (Data)
139+import Data.Typeable (Typeable)
140+
141+#if MIN_VERSION_base(4,7,0)
142+import qualified GHC.Exts as IsList
143+#endif
144+
145+import Control.DeepSeq
146+import qualified Data.IntMap as M
147+import Control.Lens
148+import Control.Newtype
149+import Data.Aeson(FromJSON, ToJSON, FromJSON1, ToJSON1)
150+#if MIN_VERSION_containers(0,5,9)
151+import Data.Functor.Classes
152+#endif
153+import Data.Align
154+#ifdef MIN_VERSION_semialign
155+import Data.Semialign (Unalign)
156+#if MIN_VERSION_semialign(1,1,0)
157+import Data.Zip (Zip)
158+#endif
159+#endif
160+
161+-- | An 'IntMap' with monoidal accumulation
162+newtype MonoidalIntMap a = MonoidalIntMap { getMonoidalIntMap :: M.IntMap a }
163+ deriving (Show, Read, Functor, Eq, Ord, NFData,
164+ Foldable, Traversable,
165+ FromJSON, ToJSON, FromJSON1, ToJSON1,
166+ Data, Typeable, Align
167+#if MIN_VERSION_these(0,8,0)
168+ , Semialign
169+#endif
170+#ifdef MIN_VERSION_semialign
171+ , Unalign
172+#if MIN_VERSION_semialign(1,1,0)
173+ , Zip
174+#endif
175+#endif
176+ )
177+
178+#if MIN_VERSION_containers(0,5,9)
179+deriving instance Eq1 MonoidalIntMap
180+deriving instance Ord1 MonoidalIntMap
181+deriving instance Show1 MonoidalIntMap
182+#endif
183+
184+type instance Index (MonoidalIntMap a) = Int
185+type instance IxValue (MonoidalIntMap a) = a
186+instance Ixed (MonoidalIntMap a) where
187+ ix k f (MonoidalIntMap m) = case M.lookup k m of
188+ Just v -> f v <&> \v' -> MonoidalIntMap (M.insert k v' m)
189+ Nothing -> pure (MonoidalIntMap m)
190+ {-# INLINE ix #-}
191+
192+instance At (MonoidalIntMap a) where
193+ at k f (MonoidalIntMap m) = f mv <&> \r -> case r of
194+ Nothing -> maybe (MonoidalIntMap m) (const (MonoidalIntMap $ M.delete k m)) mv
195+ Just v' -> MonoidalIntMap $ M.insert k v' m
196+ where mv = M.lookup k m
197+ {-# INLINE at #-}
198+
199+instance Each (MonoidalIntMap a) (MonoidalIntMap b) a b
200+
201+instance FunctorWithIndex Int MonoidalIntMap
202+instance FoldableWithIndex Int MonoidalIntMap
203+instance TraversableWithIndex Int MonoidalIntMap where
204+ itraverse f (MonoidalIntMap m) = fmap MonoidalIntMap $ itraverse f m
205+ {-# INLINE itraverse #-}
206+
207+instance TraverseMin Int MonoidalIntMap where
208+ traverseMin f (MonoidalIntMap m) = fmap MonoidalIntMap $ traverseMin f m
209+ {-# INLINE traverseMin #-}
210+instance TraverseMax Int MonoidalIntMap where
211+ traverseMax f (MonoidalIntMap m) = fmap MonoidalIntMap $ traverseMax f m
212+ {-# INLINE traverseMax #-}
213+
214+instance AsEmpty (MonoidalIntMap a) where
215+ _Empty = nearly (MonoidalIntMap M.empty) (M.null . unpack)
216+ {-# INLINE _Empty #-}
217+
218+instance Wrapped (MonoidalIntMap a) where
219+ type Unwrapped (MonoidalIntMap a) = M.IntMap a
220+ _Wrapped' = iso unpack pack
221+ {-# INLINE _Wrapped' #-}
222+
223+instance Semigroup a => Semigroup (MonoidalIntMap a) where
224+ MonoidalIntMap a <> MonoidalIntMap b = MonoidalIntMap $ M.unionWith (<>) a b
225+ {-# INLINE (<>) #-}
226+
227+instance Semigroup a => Monoid (MonoidalIntMap a) where
228+ mempty = MonoidalIntMap mempty
229+ {-# INLINE mempty #-}
230+#if !(MIN_VERSION_base(4,11,0))
231+ mappend (MonoidalIntMap a) (MonoidalIntMap b) = MonoidalIntMap $ M.unionWith (<>) a b
232+ {-# INLINE mappend #-}
233+#endif
234+
235+instance Newtype (MonoidalIntMap a) (M.IntMap a) where
236+ pack = MonoidalIntMap
237+ {-# INLINE pack #-}
238+ unpack (MonoidalIntMap a) = a
239+ {-# INLINE unpack #-}
240+
241+#if MIN_VERSION_base(4,7,0)
242+instance Semigroup a => IsList.IsList (MonoidalIntMap a) where
243+ type Item (MonoidalIntMap a) = (Int, a)
244+ fromList = MonoidalIntMap . M.fromListWith (<>)
245+ {-# INLINE fromList #-}
246+ toList = M.toList . unpack
247+ {-# INLINE toList #-}
248+#endif
249+
250+-- | /O(1)/. A map with a single element.
251+singleton :: Int -> a -> MonoidalIntMap a
252+singleton k a = MonoidalIntMap $ M.singleton k a
253+{-# INLINE singleton #-}
254+
255+-- | /O(1)/. The number of elements in the map.
256+size :: MonoidalIntMap a -> Int
257+size = M.size . unpack
258+{-# INLINE size #-}
259+
260+-- | /O(log n)/. Is the key a member of the map? See also 'notMember'.
261+member :: Int -> MonoidalIntMap a -> Bool
262+member k = M.member k . unpack
263+{-# INLINE member #-}
264+
265+-- | /O(log n)/. Is the key not a member of the map? See also 'member'.
266+notMember :: Int -> MonoidalIntMap a -> Bool
267+notMember k = not . M.member k . unpack
268+{-# INLINE notMember #-}
269+
270+-- | /O(log n)/. The expression @('findWithDefault' def k map)@ returns
271+-- the value at key @k@ or returns default value @def@
272+-- when the key is not in the map.
273+findWithDefault :: forall a. a -> Int -> MonoidalIntMap a -> a
274+findWithDefault def k = M.findWithDefault def k . unpack
275+{-# INLINE findWithDefault #-}
276+
277+-- | /O(log n)/. Delete a key and its value from the map. When the key is not
278+-- a member of the map, the original map is returned.
279+delete :: Int -> MonoidalIntMap a -> MonoidalIntMap a
280+delete k = _Wrapping' MonoidalIntMap %~ M.delete k
281+{-# INLINE delete #-}
282+
283+-- | /O(n)/. Return all elements of the map and their keys
284+assocs :: MonoidalIntMap a -> [(Int,a)]
285+assocs = M.assocs . unpack
286+{-# INLINE assocs #-}
287+
288+-- | /O(n)/. Return all elements of the map in the ascending order of their
289+-- keys. Subject to list fusion.
290+elems :: MonoidalIntMap a -> [a]
291+elems = M.elems . unpack
292+{-# INLINE elems #-}
293+
294+-- | /O(n)/. Return all keys of the map in ascending order. Subject to list
295+-- fusion.
296+keys :: MonoidalIntMap a -> [Int]
297+keys = M.keys . unpack
298+{-# INLINE keys #-}
299+
300+(!) :: forall a. MonoidalIntMap a -> Int -> a
301+(!) = coerce ((M.!) :: M.IntMap a -> Int -> a)
302+infixl 9 !
303+
304+(\\) :: forall a b. MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap a
305+(\\) = coerce ((M.\\) :: M.IntMap a -> M.IntMap b -> M.IntMap a)
306+infixl 9 \\ --
307+
308+null :: forall a. MonoidalIntMap a -> Bool
309+null = coerce (M.null :: M.IntMap a -> Bool)
310+{-# INLINE null #-}
311+
312+lookup :: forall a. Int -> MonoidalIntMap a -> Maybe a
313+lookup = coerce (M.lookup :: Int -> M.IntMap a -> Maybe a)
314+{-# INLINE lookup #-}
315+
316+lookupLT :: forall a. Int -> MonoidalIntMap a -> Maybe (Int, a)
317+lookupLT = coerce (M.lookupLT :: Int -> M.IntMap a -> Maybe (Int,a))
318+{-# INLINE lookupLT #-}
319+
320+lookupGT :: forall a. Int -> MonoidalIntMap a -> Maybe (Int, a)
321+lookupGT = coerce (M.lookupGT :: Int -> M.IntMap a -> Maybe (Int,a))
322+{-# INLINE lookupGT #-}
323+
324+lookupLE :: forall a. Int -> MonoidalIntMap a -> Maybe (Int, a)
325+lookupLE = coerce (M.lookupLE :: Int -> M.IntMap a -> Maybe (Int,a))
326+{-# INLINE lookupLE #-}
327+
328+lookupGE :: forall a. Int -> MonoidalIntMap a -> Maybe (Int, a)
329+lookupGE = coerce (M.lookupGE :: Int -> M.IntMap a -> Maybe (Int,a))
330+{-# INLINE lookupGE #-}
331+
332+empty :: forall a. MonoidalIntMap a
333+empty = coerce (M.empty :: M.IntMap a)
334+{-# INLINE empty #-}
335+
336+insert :: forall a. Int -> a -> MonoidalIntMap a -> MonoidalIntMap a
337+insert = coerce (M.insert :: Int -> a -> M.IntMap a -> M.IntMap a)
338+{-# INLINE insert #-}
339+
340+insertWith :: forall a. (a -> a -> a) -> Int -> a -> MonoidalIntMap a -> MonoidalIntMap a
341+insertWith = coerce (M.insertWith :: (a -> a -> a) -> Int -> a -> M.IntMap a -> M.IntMap a)
342+{-# INLINE insertWith #-}
343+
344+insertWithKey :: forall a. (Int -> a -> a -> a) -> Int -> a -> MonoidalIntMap a -> MonoidalIntMap a
345+insertWithKey = coerce (M.insertWithKey :: (Int -> a -> a -> a) -> Int -> a -> M.IntMap a -> M.IntMap a)
346+{-# INLINE insertWithKey #-}
347+
348+insertLookupWithKey :: forall a. (Int -> a -> a -> a) -> Int -> a -> MonoidalIntMap a -> (Maybe a, MonoidalIntMap a)
349+insertLookupWithKey = coerce (M.insertLookupWithKey :: (Int -> a -> a -> a) -> Int -> a -> M.IntMap a -> (Maybe a, M.IntMap a))
350+{-# INLINE insertLookupWithKey #-}
351+
352+adjust :: forall a. (a -> a) -> Int -> MonoidalIntMap a -> MonoidalIntMap a
353+adjust = coerce (M.adjust :: (a -> a) -> Int -> M.IntMap a -> M.IntMap a)
354+{-# INLINE adjust #-}
355+
356+adjustWithKey :: forall a. (Int -> a -> a) -> Int -> MonoidalIntMap a -> MonoidalIntMap a
357+adjustWithKey = coerce (M.adjustWithKey :: (Int -> a -> a) -> Int -> M.IntMap a -> M.IntMap a)
358+{-# INLINE adjustWithKey #-}
359+
360+update :: forall a. (a -> Maybe a) -> Int -> MonoidalIntMap a -> MonoidalIntMap a
361+update = coerce (M.update :: (a -> Maybe a) -> Int -> M.IntMap a -> M.IntMap a)
362+{-# INLINE update #-}
363+
364+updateWithKey :: forall a. (Int -> a -> Maybe a) -> Int -> MonoidalIntMap a -> MonoidalIntMap a
365+updateWithKey = coerce (M.updateWithKey :: (Int -> a -> Maybe a) -> Int -> M.IntMap a -> M.IntMap a)
366+{-# INLINE updateWithKey #-}
367+
368+updateLookupWithKey :: forall a. (Int -> a -> Maybe a) -> Int -> MonoidalIntMap a -> (Maybe a, MonoidalIntMap a)
369+updateLookupWithKey = coerce (M.updateLookupWithKey :: (Int -> a -> Maybe a) -> Int -> M.IntMap a -> (Maybe a, M.IntMap a))
370+{-# INLINE updateLookupWithKey #-}
371+
372+alter :: forall a. (Maybe a -> Maybe a) -> Int -> MonoidalIntMap a -> MonoidalIntMap a
373+alter = coerce (M.alter :: (Maybe a -> Maybe a) -> Int -> M.IntMap a -> M.IntMap a)
374+{-# INLINE alter #-}
375+
376+unionWith :: forall a. (a -> a -> a) -> MonoidalIntMap a -> MonoidalIntMap a -> MonoidalIntMap a
377+unionWith = coerce (M.unionWith :: (a -> a -> a) -> M.IntMap a -> M.IntMap a -> M.IntMap a)
378+{-# INLINE unionWith #-}
379+
380+unionWithKey :: forall a. (Int -> a -> a -> a) -> MonoidalIntMap a -> MonoidalIntMap a -> MonoidalIntMap a
381+unionWithKey = coerce (M.unionWithKey :: (Int -> a -> a -> a) -> M.IntMap a -> M.IntMap a -> M.IntMap a)
382+{-# INLINE unionWithKey #-}
383+
384+unionsWith :: forall a. (a -> a -> a) -> [MonoidalIntMap a] -> MonoidalIntMap a
385+unionsWith = coerce (M.unionsWith :: (a -> a -> a) -> [M.IntMap a] -> M.IntMap a)
386+{-# INLINE unionsWith #-}
387+
388+difference :: forall a b. MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap a
389+difference = (\\)
390+{-# INLINE difference #-}
391+
392+differenceWith :: forall a b. (a -> b -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap a
393+differenceWith = coerce (M.differenceWith :: (a -> b -> Maybe a) -> M.IntMap a -> M.IntMap b -> M.IntMap a)
394+{-# INLINE differenceWith #-}
395+
396+differenceWithKey :: forall a b. (Int -> a -> b -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap a
397+differenceWithKey = coerce (M.differenceWithKey :: (Int -> a -> b -> Maybe a) -> M.IntMap a -> M.IntMap b -> M.IntMap a)
398+{-# INLINE differenceWithKey #-}
399+
400+intersectionWith :: forall a b c. (a -> b -> c) -> MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap c
401+intersectionWith = coerce (M.intersectionWith :: (a -> b -> c) -> M.IntMap a -> M.IntMap b -> M.IntMap c)
402+{-# INLINE intersectionWith #-}
403+
404+intersectionWithKey :: forall a b c. (Int -> a -> b -> c) -> MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap c
405+intersectionWithKey = coerce (M.intersectionWithKey :: (Int -> a -> b -> c) -> M.IntMap a -> M.IntMap b -> M.IntMap c)
406+{-# INLINE intersectionWithKey #-}
407+
408+mergeWithKey :: forall a b c. (Int -> a -> b -> Maybe c) -> (MonoidalIntMap a -> MonoidalIntMap c) -> (MonoidalIntMap b -> MonoidalIntMap c) -> MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap c
409+mergeWithKey = coerce (M.mergeWithKey :: (Int -> a -> b -> Maybe c) -> (M.IntMap a -> M.IntMap c) -> (M.IntMap b -> M.IntMap c) -> M.IntMap a -> M.IntMap b -> M.IntMap c)
410+{-# INLINE mergeWithKey #-}
411+
412+map :: (a -> b) -> MonoidalIntMap a -> MonoidalIntMap b
413+map = fmap
414+{-# INLINE map #-}
415+
416+mapWithKey :: forall a b. (Int -> a -> b) -> MonoidalIntMap a -> MonoidalIntMap b
417+mapWithKey = coerce (M.mapWithKey :: (Int -> a -> b) -> M.IntMap a -> M.IntMap b)
418+{-# INLINE mapWithKey #-}
419+
420+traverseWithKey :: Applicative t => (Int -> a -> t b) -> MonoidalIntMap a -> t (MonoidalIntMap b)
421+traverseWithKey = itraverse
422+{-# INLINE traverseWithKey #-}
423+
424+mapAccum :: forall a b c. (a -> b -> (a, c)) -> a -> MonoidalIntMap b -> (a, MonoidalIntMap c)
425+mapAccum = coerce (M.mapAccum :: (a -> b -> (a, c)) -> a -> M.IntMap b -> (a, M.IntMap c))
426+{-# INLINE mapAccum #-}
427+
428+mapAccumWithKey :: forall a b c. (a -> Int -> b -> (a, c)) -> a -> MonoidalIntMap b -> (a, MonoidalIntMap c)
429+mapAccumWithKey = coerce (M.mapAccumWithKey :: (a -> Int -> b -> (a, c)) -> a -> M.IntMap b -> (a, M.IntMap c))
430+{-# INLINE mapAccumWithKey #-}
431+
432+mapAccumRWithKey :: forall a b c. (a -> Int -> b -> (a, c)) -> a -> MonoidalIntMap b -> (a, MonoidalIntMap c)
433+mapAccumRWithKey = coerce (M.mapAccumRWithKey :: (a -> Int -> b -> (a, c)) -> a -> M.IntMap b -> (a, M.IntMap c))
434+{-# INLINE mapAccumRWithKey #-}
435+
436+mapKeys :: forall a. (Int -> Int) -> MonoidalIntMap a -> MonoidalIntMap a
437+mapKeys = coerce (M.mapKeys :: (Int -> Int) -> M.IntMap a -> M.IntMap a)
438+{-# INLINE mapKeys #-}
439+
440+mapKeysWith :: forall a. (a -> a -> a) -> (Int -> Int) -> MonoidalIntMap a -> MonoidalIntMap a
441+mapKeysWith = coerce (M.mapKeysWith :: (a -> a -> a) -> (Int -> Int) -> M.IntMap a -> M.IntMap a)
442+{-# INLINE mapKeysWith #-}
443+
444+mapKeysMonotonic :: forall a. (Int -> Int) -> MonoidalIntMap a -> MonoidalIntMap a
445+mapKeysMonotonic = coerce (M.mapKeysMonotonic :: (Int -> Int) -> M.IntMap a -> M.IntMap a)
446+{-# INLINE mapKeysMonotonic #-}
447+
448+foldr :: forall a b. (a -> b -> b) -> b -> MonoidalIntMap a -> b
449+foldr = coerce (M.foldr :: (a -> b -> b) -> b -> M.IntMap a -> b)
450+{-# INLINE foldr #-}
451+
452+foldl :: forall a b. (a -> b -> a) -> a -> MonoidalIntMap b -> a
453+foldl = coerce (M.foldl :: (a -> b -> a) -> a -> M.IntMap b -> a)
454+{-# INLINE foldl #-}
455+
456+foldrWithKey :: forall a b. (Int -> a -> b -> b) -> b -> MonoidalIntMap a -> b
457+foldrWithKey = coerce (M.foldrWithKey :: (Int -> a -> b -> b) -> b -> M.IntMap a -> b)
458+{-# INLINE foldrWithKey #-}
459+
460+foldlWithKey :: forall a b. (a -> Int -> b -> a) -> a -> MonoidalIntMap b -> a
461+foldlWithKey = coerce (M.foldlWithKey :: (a -> Int -> b -> a) -> a -> M.IntMap b -> a)
462+{-# INLINE foldlWithKey #-}
463+
464+foldMapWithKey :: forall a m. Monoid m => (Int -> a -> m) -> MonoidalIntMap a -> m
465+foldMapWithKey = coerce (M.foldMapWithKey :: Monoid m => (Int -> a -> m) -> M.IntMap a -> m)
466+{-# INLINE foldMapWithKey #-}
467+
468+foldr' :: forall a b. (a -> b -> b) -> b -> MonoidalIntMap a -> b
469+foldr' = coerce (M.foldr' :: (a -> b -> b) -> b -> M.IntMap a -> b)
470+{-# INLINE foldr' #-}
471+
472+foldl' :: forall a b. (a -> b -> a) -> a -> MonoidalIntMap b -> a
473+foldl' = coerce (M.foldl' :: (a -> b -> a) -> a -> M.IntMap b -> a)
474+{-# INLINE foldl' #-}
475+
476+foldrWithKey' :: forall a b. (Int -> a -> b -> b) -> b -> MonoidalIntMap a -> b
477+foldrWithKey' = coerce (M.foldrWithKey' :: (Int -> a -> b -> b) -> b -> M.IntMap a -> b)
478+{-# INLINE foldrWithKey' #-}
479+
480+foldlWithKey' :: forall a b. (a -> Int -> b -> a) -> a -> MonoidalIntMap b -> a
481+foldlWithKey' = coerce (M.foldlWithKey' :: (a -> Int -> b -> a) -> a -> M.IntMap b -> a)
482+{-# INLINE foldlWithKey' #-}
483+
484+keysSet :: forall a. MonoidalIntMap a -> IntSet
485+keysSet = coerce (M.keysSet :: M.IntMap a -> IntSet)
486+{-# INLINE keysSet #-}
487+
488+fromSet :: forall a. (Int -> a) -> IntSet -> MonoidalIntMap a
489+fromSet = coerce (M.fromSet :: (Int -> a) -> IntSet -> M.IntMap a)
490+{-# INLINE fromSet #-}
491+
492+toList :: forall a. MonoidalIntMap a -> [(Int, a)]
493+toList = coerce (M.toList :: M.IntMap a -> [(Int, a)])
494+{-# INLINE toList #-}
495+
496+fromList :: forall a. [(Int, a)] -> MonoidalIntMap a
497+fromList = coerce (M.fromList :: [(Int, a)] -> M.IntMap a)
498+{-# INLINE fromList #-}
499+
500+fromListWith :: forall a. (a -> a -> a) -> [(Int, a)] -> MonoidalIntMap a
501+fromListWith = coerce (M.fromListWith :: (a -> a -> a) -> [(Int, a)] -> M.IntMap a)
502+{-# INLINE fromListWith #-}
503+
504+fromListWithKey :: forall a. (Int -> a -> a -> a) -> [(Int, a)] -> MonoidalIntMap a
505+fromListWithKey = coerce (M.fromListWithKey :: (Int -> a -> a -> a) -> [(Int, a)] -> M.IntMap a)
506+{-# INLINE fromListWithKey #-}
507+
508+toAscList :: forall a. MonoidalIntMap a -> [(Int, a)]
509+toAscList = coerce (M.toAscList :: M.IntMap a -> [(Int, a)])
510+{-# INLINE toAscList #-}
511+
512+toDescList :: forall a. MonoidalIntMap a -> [(Int, a)]
513+toDescList = coerce (M.toDescList :: M.IntMap a -> [(Int, a)])
514+{-# INLINE toDescList #-}
515+
516+fromAscList :: forall a. [(Int, a)] -> MonoidalIntMap a
517+fromAscList = coerce (M.fromAscList :: [(Int, a)] -> M.IntMap a)
518+{-# INLINE fromAscList #-}
519+
520+fromAscListWith :: forall a. (a -> a -> a) -> [(Int, a)] -> MonoidalIntMap a
521+fromAscListWith = coerce (M.fromAscListWith :: (a -> a -> a) -> [(Int, a)] -> M.IntMap a)
522+{-# INLINE fromAscListWith #-}
523+
524+fromAscListWithKey :: forall a. (Int -> a -> a -> a) -> [(Int, a)] -> MonoidalIntMap a
525+fromAscListWithKey = coerce (M.fromAscListWithKey :: (Int -> a -> a -> a) -> [(Int, a)] -> M.IntMap a)
526+{-# INLINE fromAscListWithKey #-}
527+
528+fromDistinctAscList :: forall a. [(Int, a)] -> MonoidalIntMap a
529+fromDistinctAscList = coerce (M.fromDistinctAscList :: [(Int, a)] -> M.IntMap a)
530+{-# INLINE fromDistinctAscList #-}
531+
532+fromDistinctList :: forall a. [(Int, a)] -> MonoidalIntMap a
533+fromDistinctList = coerce (M.fromList :: [(Int, a)] -> M.IntMap a)
534+{-# INLINE fromDistinctList #-}
535+
536+filter :: forall a. (a -> Bool) -> MonoidalIntMap a -> MonoidalIntMap a
537+filter = coerce (M.filter :: (a -> Bool) -> M.IntMap a -> M.IntMap a)
538+{-# INLINE filter #-}
539+
540+filterWithKey :: forall a. (Int -> a -> Bool) -> MonoidalIntMap a -> MonoidalIntMap a
541+filterWithKey = coerce (M.filterWithKey :: (Int -> a -> Bool) -> M.IntMap a -> M.IntMap a)
542+{-# INLINE filterWithKey #-}
543+
544+partition :: forall a. (a -> Bool) -> MonoidalIntMap a -> (MonoidalIntMap a, MonoidalIntMap a)
545+partition = coerce (M.partition :: (a -> Bool) -> M.IntMap a -> (M.IntMap a, M.IntMap a))
546+{-# INLINE partition #-}
547+
548+partitionWithKey :: forall a. (Int -> a -> Bool) -> MonoidalIntMap a -> (MonoidalIntMap a, MonoidalIntMap a)
549+partitionWithKey = coerce (M.partitionWithKey :: (Int -> a -> Bool) -> M.IntMap a -> (M.IntMap a, M.IntMap a))
550+{-# INLINE partitionWithKey #-}
551+
552+mapMaybe :: forall a b. (a -> Maybe b) -> MonoidalIntMap a -> MonoidalIntMap b
553+mapMaybe = coerce (M.mapMaybe :: (a -> Maybe b) -> M.IntMap a -> M.IntMap b)
554+{-# INLINE mapMaybe #-}
555+
556+mapMaybeWithKey :: forall a b. (Int -> a -> Maybe b) -> MonoidalIntMap a -> MonoidalIntMap b
557+mapMaybeWithKey = coerce (M.mapMaybeWithKey :: (Int -> a -> Maybe b) -> M.IntMap a -> M.IntMap b)
558+{-# INLINE mapMaybeWithKey #-}
559+
560+mapEither :: forall a b c. (a -> Either b c) -> MonoidalIntMap a -> (MonoidalIntMap b, MonoidalIntMap c)
561+mapEither = coerce (M.mapEither :: (a -> Either b c) -> M.IntMap a -> (M.IntMap b, M.IntMap c))
562+{-# INLINE mapEither #-}
563+
564+mapEitherWithKey :: forall a b c. (Int -> a -> Either b c) -> MonoidalIntMap a -> (MonoidalIntMap b, MonoidalIntMap c)
565+mapEitherWithKey = coerce (M.mapEitherWithKey :: (Int -> a -> Either b c) -> M.IntMap a -> (M.IntMap b, M.IntMap c))
566+{-# INLINE mapEitherWithKey #-}
567+
568+split :: forall a. Int -> MonoidalIntMap a -> (MonoidalIntMap a, MonoidalIntMap a)
569+split = coerce (M.split :: Int -> M.IntMap a -> (M.IntMap a, M.IntMap a))
570+{-# INLINE split #-}
571+
572+splitLookup :: forall a. Int -> MonoidalIntMap a -> (MonoidalIntMap a, Maybe a, MonoidalIntMap a)
573+splitLookup = coerce (M.splitLookup :: Int -> M.IntMap a -> (M.IntMap a, Maybe a, M.IntMap a))
574+{-# INLINE splitLookup #-}
575+
576+splitRoot :: forall a. MonoidalIntMap a -> [MonoidalIntMap a]
577+splitRoot = coerce (M.splitRoot :: M.IntMap a -> [M.IntMap a])
578+{-# INLINE splitRoot #-}
579+
580+isSubmapOf :: forall a. Eq a => MonoidalIntMap a -> MonoidalIntMap a -> Bool
581+isSubmapOf = coerce (M.isSubmapOf :: M.IntMap a -> M.IntMap a -> Bool)
582+{-# INLINE isSubmapOf #-}
583+
584+isSubmapOfBy :: forall a b. (a -> b -> Bool) -> MonoidalIntMap a -> MonoidalIntMap b -> Bool
585+isSubmapOfBy = coerce (M.isSubmapOfBy :: (a -> b -> Bool) -> M.IntMap a -> M.IntMap b -> Bool)
586+{-# INLINE isSubmapOfBy #-}
587+
588+isProperSubmapOf :: forall a. Eq a => MonoidalIntMap a -> MonoidalIntMap a -> Bool
589+isProperSubmapOf = coerce (M.isProperSubmapOf :: M.IntMap a -> M.IntMap a -> Bool)
590+{-# INLINE isProperSubmapOf #-}
591+
592+isProperSubmapOfBy :: forall a b. (a -> b -> Bool) -> MonoidalIntMap a -> MonoidalIntMap b -> Bool
593+isProperSubmapOfBy = coerce (M.isProperSubmapOfBy :: (a -> b -> Bool) -> M.IntMap a -> M.IntMap b -> Bool)
594+{-# INLINE isProperSubmapOfBy #-}
595+
596+findMin :: forall a. MonoidalIntMap a -> (Int, a)
597+findMin = coerce (M.findMin :: M.IntMap a -> (Int, a))
598+{-# INLINE findMin #-}
599+
600+findMax :: forall a. MonoidalIntMap a -> (Int, a)
601+findMax = coerce (M.findMax :: M.IntMap a -> (Int, a))
602+{-# INLINE findMax #-}
603+
604+deleteMin :: forall a. MonoidalIntMap a -> MonoidalIntMap a
605+deleteMin = coerce (M.deleteMin :: M.IntMap a -> M.IntMap a)
606+{-# INLINE deleteMin #-}
607+
608+deleteMax :: forall a. MonoidalIntMap a -> MonoidalIntMap a
609+deleteMax = coerce (M.deleteMax :: M.IntMap a -> M.IntMap a)
610+{-# INLINE deleteMax #-}
611+
612+deleteFindMin :: forall a. MonoidalIntMap a -> ((Int, a), MonoidalIntMap a)
613+deleteFindMin = coerce (M.deleteFindMin :: M.IntMap a -> ((Int, a), M.IntMap a))
614+{-# INLINE deleteFindMin #-}
615+
616+deleteFindMax :: forall a. MonoidalIntMap a -> ((Int, a), MonoidalIntMap a)
617+deleteFindMax = coerce (M.deleteFindMax :: M.IntMap a -> ((Int, a), M.IntMap a))
618+{-# INLINE deleteFindMax #-}
619+
620+updateMin :: forall a. (a -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap a
621+updateMin = coerce (M.updateMin :: (a -> Maybe a) -> M.IntMap a -> M.IntMap a)
622+{-# INLINE updateMin #-}
623+
624+updateMax :: forall a. (a -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap a
625+updateMax = coerce (M.updateMax :: (a -> Maybe a) -> M.IntMap a -> M.IntMap a)
626+{-# INLINE updateMax #-}
627+
628+updateMinWithKey :: forall a. (Int -> a -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap a
629+updateMinWithKey = coerce (M.updateMinWithKey :: (Int -> a -> Maybe a) -> M.IntMap a -> M.IntMap a)
630+{-# INLINE updateMinWithKey #-}
631+
632+updateMaxWithKey :: forall a. (Int -> a -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap a
633+updateMaxWithKey = coerce (M.updateMaxWithKey :: (Int -> a -> Maybe a) -> M.IntMap a -> M.IntMap a)
634+{-# INLINE updateMaxWithKey #-}
635+
636+minView :: forall a. MonoidalIntMap a -> Maybe (a, MonoidalIntMap a)
637+minView = coerce (M.minView :: M.IntMap a -> Maybe (a, M.IntMap a))
638+{-# INLINE minView #-}
639+
640+maxView :: forall a. MonoidalIntMap a -> Maybe (a, MonoidalIntMap a)
641+maxView = coerce (M.maxView :: M.IntMap a -> Maybe (a, M.IntMap a))
642+{-# INLINE maxView #-}
643+
644+minViewWithKey :: forall a. MonoidalIntMap a -> Maybe ((Int, a), MonoidalIntMap a)
645+minViewWithKey = coerce (M.minViewWithKey :: M.IntMap a -> Maybe ((Int, a), M.IntMap a))
646+{-# INLINE minViewWithKey #-}
647+
648+maxViewWithKey :: forall a. MonoidalIntMap a -> Maybe ((Int, a), MonoidalIntMap a)
649+maxViewWithKey = coerce (M.maxViewWithKey :: M.IntMap a -> Maybe ((Int, a), M.IntMap a))
650+{-# INLINE maxViewWithKey #-}
651+
src/Data/IntMap/Monoidal/Strict.hsnew file+651
@@ -0,0 +1,651 @@
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE ScopedTypeVariables #-}
3+{-# LANGUAGE MultiParamTypeClasses #-}
4+{-# LANGUAGE TypeFamilies #-}
5+{-# LANGUAGE FlexibleInstances #-}
6+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
7+{-# LANGUAGE DeriveTraversable #-}
8+{-# LANGUAGE DeriveDataTypeable #-}
9+{-# LANGUAGE StandaloneDeriving #-}
10+
11+-- | This module provides a 'Data.IntMap' variant which uses the value's
12+-- 'Monoid' instance to accumulate conflicting entries when merging
13+-- 'Map's.
14+--
15+-- While some functions mirroring those of 'Data.IntMap' are provided
16+-- here for convenience, more specialized needs will likely want to use
17+-- either the 'Newtype' or 'Wrapped' instances to manipulate the
18+-- underlying 'Map'.
19+
20+module Data.IntMap.Monoidal.Strict
21+ ( MonoidalIntMap(..)
22+ -- * Often-needed functions
23+ , singleton
24+ , size
25+ , member
26+ , notMember
27+ , findWithDefault
28+ , assocs
29+ , elems
30+ , keys
31+ , (!)
32+ , (\\)
33+ , adjust
34+ , adjustWithKey
35+ , alter
36+ , delete
37+-- , deleteAt
38+ , deleteFindMax
39+ , deleteFindMin
40+ , deleteMax
41+ , deleteMin
42+ , difference
43+ , differenceWith
44+ , differenceWithKey
45+-- , elemAt
46+ , empty
47+ , filter
48+ , filterWithKey
49+-- , findIndex
50+ , findMax
51+ , findMin
52+ , foldMapWithKey
53+ , foldl
54+ , foldl'
55+ , foldlWithKey
56+ , foldlWithKey'
57+ , foldr
58+ , foldr'
59+ , foldrWithKey
60+ , foldrWithKey'
61+ , fromAscList
62+ , fromAscListWith
63+ , fromAscListWithKey
64+ , fromDistinctAscList
65+ , fromDistinctList
66+ , fromList
67+ , fromListWith
68+ , fromListWithKey
69+ , fromSet
70+ , insert
71+ , insertLookupWithKey
72+ , insertWith
73+ , insertWithKey
74+ , intersectionWith
75+ , intersectionWithKey
76+ , isProperSubmapOf
77+ , isProperSubmapOfBy
78+ , isSubmapOf
79+ , isSubmapOfBy
80+ , keysSet
81+ , lookup
82+ , lookupGE
83+ , lookupGT
84+-- , lookupIndex
85+ , lookupLE
86+ , lookupLT
87+ , map
88+ , mapAccum
89+ , mapAccumRWithKey
90+ , mapAccumWithKey
91+ , mapEither
92+ , mapEitherWithKey
93+ , mapKeys
94+ , mapKeysMonotonic
95+ , mapKeysWith
96+ , mapMaybe
97+ , mapMaybeWithKey
98+ , mapWithKey
99+ , maxView
100+ , maxViewWithKey
101+ , mergeWithKey
102+ , minView
103+ , minViewWithKey
104+ , null
105+ , partition
106+ , partitionWithKey
107+ , split
108+ , splitLookup
109+ , splitRoot
110+ , toAscList
111+ , toDescList
112+ , toList
113+ , traverseWithKey
114+ , unionWith
115+ , unionWithKey
116+ , unionsWith
117+ , update
118+-- , updateAt
119+ , updateLookupWithKey
120+ , updateMax
121+ , updateMaxWithKey
122+ , updateMin
123+ , updateMinWithKey
124+ , updateWithKey
125+-- , valid
126+ -- , showTree
127+ -- , showTreeWith
128+ ) where
129+
130+import Prelude hiding (null, lookup, map, foldl, foldr, filter)
131+
132+import Data.Coerce (coerce)
133+import Data.IntSet (IntSet)
134+import Data.Semigroup
135+import Data.Foldable (Foldable)
136+import Data.Traversable (Traversable)
137+import Control.Applicative (Applicative, pure)
138+import Data.Data (Data)
139+import Data.Typeable (Typeable)
140+
141+#if MIN_VERSION_base(4,7,0)
142+import qualified GHC.Exts as IsList
143+#endif
144+
145+import Control.DeepSeq
146+import qualified Data.IntMap.Strict as M
147+import Control.Lens
148+import Control.Newtype
149+import Data.Aeson(FromJSON, ToJSON, FromJSON1, ToJSON1)
150+#if MIN_VERSION_containers(0,5,9)
151+import Data.Functor.Classes
152+#endif
153+import Data.Align
154+#ifdef MIN_VERSION_semialign
155+import Data.Semialign (Unalign)
156+#if MIN_VERSION_semialign(1,1,0)
157+import Data.Zip (Zip)
158+#endif
159+#endif
160+
161+-- | An 'IntMap' with monoidal accumulation
162+newtype MonoidalIntMap a = MonoidalIntMap { getMonoidalIntMap :: M.IntMap a }
163+ deriving (Show, Read, Functor, Eq, Ord, NFData,
164+ Foldable, Traversable,
165+ FromJSON, ToJSON, FromJSON1, ToJSON1,
166+ Data, Typeable, Align
167+#if MIN_VERSION_these(0,8,0)
168+ , Semialign
169+#endif
170+#ifdef MIN_VERSION_semialign
171+ , Unalign
172+#if MIN_VERSION_semialign(1,1,0)
173+ , Zip
174+#endif
175+#endif
176+ )
177+
178+#if MIN_VERSION_containers(0,5,9)
179+deriving instance Eq1 MonoidalIntMap
180+deriving instance Ord1 MonoidalIntMap
181+deriving instance Show1 MonoidalIntMap
182+#endif
183+
184+type instance Index (MonoidalIntMap a) = Int
185+type instance IxValue (MonoidalIntMap a) = a
186+instance Ixed (MonoidalIntMap a) where
187+ ix k f (MonoidalIntMap m) = case M.lookup k m of
188+ Just v -> f v <&> \v' -> MonoidalIntMap (M.insert k v' m)
189+ Nothing -> pure (MonoidalIntMap m)
190+ {-# INLINE ix #-}
191+
192+instance At (MonoidalIntMap a) where
193+ at k f (MonoidalIntMap m) = f mv <&> \r -> case r of
194+ Nothing -> maybe (MonoidalIntMap m) (const (MonoidalIntMap $ M.delete k m)) mv
195+ Just v' -> MonoidalIntMap $ M.insert k v' m
196+ where mv = M.lookup k m
197+ {-# INLINE at #-}
198+
199+instance Each (MonoidalIntMap a) (MonoidalIntMap b) a b
200+
201+instance FunctorWithIndex Int MonoidalIntMap
202+instance FoldableWithIndex Int MonoidalIntMap
203+instance TraversableWithIndex Int MonoidalIntMap where
204+ itraverse f (MonoidalIntMap m) = fmap MonoidalIntMap $ itraverse f m
205+ {-# INLINE itraverse #-}
206+
207+instance TraverseMin Int MonoidalIntMap where
208+ traverseMin f (MonoidalIntMap m) = fmap MonoidalIntMap $ traverseMin f m
209+ {-# INLINE traverseMin #-}
210+instance TraverseMax Int MonoidalIntMap where
211+ traverseMax f (MonoidalIntMap m) = fmap MonoidalIntMap $ traverseMax f m
212+ {-# INLINE traverseMax #-}
213+
214+instance AsEmpty (MonoidalIntMap a) where
215+ _Empty = nearly (MonoidalIntMap M.empty) (M.null . unpack)
216+ {-# INLINE _Empty #-}
217+
218+instance Wrapped (MonoidalIntMap a) where
219+ type Unwrapped (MonoidalIntMap a) = M.IntMap a
220+ _Wrapped' = iso unpack pack
221+ {-# INLINE _Wrapped' #-}
222+
223+instance Semigroup a => Semigroup (MonoidalIntMap a) where
224+ MonoidalIntMap a <> MonoidalIntMap b = MonoidalIntMap $ M.unionWith (<>) a b
225+ {-# INLINE (<>) #-}
226+
227+instance Semigroup a => Monoid (MonoidalIntMap a) where
228+ mempty = MonoidalIntMap mempty
229+ {-# INLINE mempty #-}
230+#if !(MIN_VERSION_base(4,11,0))
231+ mappend (MonoidalIntMap a) (MonoidalIntMap b) = MonoidalIntMap $ M.unionWith (<>) a b
232+ {-# INLINE mappend #-}
233+#endif
234+
235+instance Newtype (MonoidalIntMap a) (M.IntMap a) where
236+ pack = MonoidalIntMap
237+ {-# INLINE pack #-}
238+ unpack (MonoidalIntMap a) = a
239+ {-# INLINE unpack #-}
240+
241+#if MIN_VERSION_base(4,7,0)
242+instance Semigroup a => IsList.IsList (MonoidalIntMap a) where
243+ type Item (MonoidalIntMap a) = (Int, a)
244+ fromList = MonoidalIntMap . M.fromListWith (<>)
245+ {-# INLINE fromList #-}
246+ toList = M.toList . unpack
247+ {-# INLINE toList #-}
248+#endif
249+
250+-- | /O(1)/. A map with a single element.
251+singleton :: Int -> a -> MonoidalIntMap a
252+singleton k a = MonoidalIntMap $ M.singleton k a
253+{-# INLINE singleton #-}
254+
255+-- | /O(1)/. The number of elements in the map.
256+size :: MonoidalIntMap a -> Int
257+size = M.size . unpack
258+{-# INLINE size #-}
259+
260+-- | /O(log n)/. Is the key a member of the map? See also 'notMember'.
261+member :: Int -> MonoidalIntMap a -> Bool
262+member k = M.member k . unpack
263+{-# INLINE member #-}
264+
265+-- | /O(log n)/. Is the key not a member of the map? See also 'member'.
266+notMember :: Int -> MonoidalIntMap a -> Bool
267+notMember k = not . M.member k . unpack
268+{-# INLINE notMember #-}
269+
270+-- | /O(log n)/. The expression @('findWithDefault' def k map)@ returns
271+-- the value at key @k@ or returns default value @def@
272+-- when the key is not in the map.
273+findWithDefault :: forall a. a -> Int -> MonoidalIntMap a -> a
274+findWithDefault def k = M.findWithDefault def k . unpack
275+{-# INLINE findWithDefault #-}
276+
277+-- | /O(log n)/. Delete a key and its value from the map. When the key is not
278+-- a member of the map, the original map is returned.
279+delete :: Int -> MonoidalIntMap a -> MonoidalIntMap a
280+delete k = _Wrapping' MonoidalIntMap %~ M.delete k
281+{-# INLINE delete #-}
282+
283+-- | /O(n)/. Return all elements of the map and their keys
284+assocs :: MonoidalIntMap a -> [(Int,a)]
285+assocs = M.assocs . unpack
286+{-# INLINE assocs #-}
287+
288+-- | /O(n)/. Return all elements of the map in the ascending order of their
289+-- keys. Subject to list fusion.
290+elems :: MonoidalIntMap a -> [a]
291+elems = M.elems . unpack
292+{-# INLINE elems #-}
293+
294+-- | /O(n)/. Return all keys of the map in ascending order. Subject to list
295+-- fusion.
296+keys :: MonoidalIntMap a -> [Int]
297+keys = M.keys . unpack
298+{-# INLINE keys #-}
299+
300+(!) :: forall a. MonoidalIntMap a -> Int -> a
301+(!) = coerce ((M.!) :: M.IntMap a -> Int -> a)
302+infixl 9 !
303+
304+(\\) :: forall a b. MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap a
305+(\\) = coerce ((M.\\) :: M.IntMap a -> M.IntMap b -> M.IntMap a)
306+infixl 9 \\ --
307+
308+null :: forall a. MonoidalIntMap a -> Bool
309+null = coerce (M.null :: M.IntMap a -> Bool)
310+{-# INLINE null #-}
311+
312+lookup :: forall a. Int -> MonoidalIntMap a -> Maybe a
313+lookup = coerce (M.lookup :: Int -> M.IntMap a -> Maybe a)
314+{-# INLINE lookup #-}
315+
316+lookupLT :: forall a. Int -> MonoidalIntMap a -> Maybe (Int, a)
317+lookupLT = coerce (M.lookupLT :: Int -> M.IntMap a -> Maybe (Int,a))
318+{-# INLINE lookupLT #-}
319+
320+lookupGT :: forall a. Int -> MonoidalIntMap a -> Maybe (Int, a)
321+lookupGT = coerce (M.lookupGT :: Int -> M.IntMap a -> Maybe (Int,a))
322+{-# INLINE lookupGT #-}
323+
324+lookupLE :: forall a. Int -> MonoidalIntMap a -> Maybe (Int, a)
325+lookupLE = coerce (M.lookupLE :: Int -> M.IntMap a -> Maybe (Int,a))
326+{-# INLINE lookupLE #-}
327+
328+lookupGE :: forall a. Int -> MonoidalIntMap a -> Maybe (Int, a)
329+lookupGE = coerce (M.lookupGE :: Int -> M.IntMap a -> Maybe (Int,a))
330+{-# INLINE lookupGE #-}
331+
332+empty :: forall a. MonoidalIntMap a
333+empty = coerce (M.empty :: M.IntMap a)
334+{-# INLINE empty #-}
335+
336+insert :: forall a. Int -> a -> MonoidalIntMap a -> MonoidalIntMap a
337+insert = coerce (M.insert :: Int -> a -> M.IntMap a -> M.IntMap a)
338+{-# INLINE insert #-}
339+
340+insertWith :: forall a. (a -> a -> a) -> Int -> a -> MonoidalIntMap a -> MonoidalIntMap a
341+insertWith = coerce (M.insertWith :: (a -> a -> a) -> Int -> a -> M.IntMap a -> M.IntMap a)
342+{-# INLINE insertWith #-}
343+
344+insertWithKey :: forall a. (Int -> a -> a -> a) -> Int -> a -> MonoidalIntMap a -> MonoidalIntMap a
345+insertWithKey = coerce (M.insertWithKey :: (Int -> a -> a -> a) -> Int -> a -> M.IntMap a -> M.IntMap a)
346+{-# INLINE insertWithKey #-}
347+
348+insertLookupWithKey :: forall a. (Int -> a -> a -> a) -> Int -> a -> MonoidalIntMap a -> (Maybe a, MonoidalIntMap a)
349+insertLookupWithKey = coerce (M.insertLookupWithKey :: (Int -> a -> a -> a) -> Int -> a -> M.IntMap a -> (Maybe a, M.IntMap a))
350+{-# INLINE insertLookupWithKey #-}
351+
352+adjust :: forall a. (a -> a) -> Int -> MonoidalIntMap a -> MonoidalIntMap a
353+adjust = coerce (M.adjust :: (a -> a) -> Int -> M.IntMap a -> M.IntMap a)
354+{-# INLINE adjust #-}
355+
356+adjustWithKey :: forall a. (Int -> a -> a) -> Int -> MonoidalIntMap a -> MonoidalIntMap a
357+adjustWithKey = coerce (M.adjustWithKey :: (Int -> a -> a) -> Int -> M.IntMap a -> M.IntMap a)
358+{-# INLINE adjustWithKey #-}
359+
360+update :: forall a. (a -> Maybe a) -> Int -> MonoidalIntMap a -> MonoidalIntMap a
361+update = coerce (M.update :: (a -> Maybe a) -> Int -> M.IntMap a -> M.IntMap a)
362+{-# INLINE update #-}
363+
364+updateWithKey :: forall a. (Int -> a -> Maybe a) -> Int -> MonoidalIntMap a -> MonoidalIntMap a
365+updateWithKey = coerce (M.updateWithKey :: (Int -> a -> Maybe a) -> Int -> M.IntMap a -> M.IntMap a)
366+{-# INLINE updateWithKey #-}
367+
368+updateLookupWithKey :: forall a. (Int -> a -> Maybe a) -> Int -> MonoidalIntMap a -> (Maybe a, MonoidalIntMap a)
369+updateLookupWithKey = coerce (M.updateLookupWithKey :: (Int -> a -> Maybe a) -> Int -> M.IntMap a -> (Maybe a, M.IntMap a))
370+{-# INLINE updateLookupWithKey #-}
371+
372+alter :: forall a. (Maybe a -> Maybe a) -> Int -> MonoidalIntMap a -> MonoidalIntMap a
373+alter = coerce (M.alter :: (Maybe a -> Maybe a) -> Int -> M.IntMap a -> M.IntMap a)
374+{-# INLINE alter #-}
375+
376+unionWith :: forall a. (a -> a -> a) -> MonoidalIntMap a -> MonoidalIntMap a -> MonoidalIntMap a
377+unionWith = coerce (M.unionWith :: (a -> a -> a) -> M.IntMap a -> M.IntMap a -> M.IntMap a)
378+{-# INLINE unionWith #-}
379+
380+unionWithKey :: forall a. (Int -> a -> a -> a) -> MonoidalIntMap a -> MonoidalIntMap a -> MonoidalIntMap a
381+unionWithKey = coerce (M.unionWithKey :: (Int -> a -> a -> a) -> M.IntMap a -> M.IntMap a -> M.IntMap a)
382+{-# INLINE unionWithKey #-}
383+
384+unionsWith :: forall a. (a -> a -> a) -> [MonoidalIntMap a] -> MonoidalIntMap a
385+unionsWith = coerce (M.unionsWith :: (a -> a -> a) -> [M.IntMap a] -> M.IntMap a)
386+{-# INLINE unionsWith #-}
387+
388+difference :: forall a b. MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap a
389+difference = (\\)
390+{-# INLINE difference #-}
391+
392+differenceWith :: forall a b. (a -> b -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap a
393+differenceWith = coerce (M.differenceWith :: (a -> b -> Maybe a) -> M.IntMap a -> M.IntMap b -> M.IntMap a)
394+{-# INLINE differenceWith #-}
395+
396+differenceWithKey :: forall a b. (Int -> a -> b -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap a
397+differenceWithKey = coerce (M.differenceWithKey :: (Int -> a -> b -> Maybe a) -> M.IntMap a -> M.IntMap b -> M.IntMap a)
398+{-# INLINE differenceWithKey #-}
399+
400+intersectionWith :: forall a b c. (a -> b -> c) -> MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap c
401+intersectionWith = coerce (M.intersectionWith :: (a -> b -> c) -> M.IntMap a -> M.IntMap b -> M.IntMap c)
402+{-# INLINE intersectionWith #-}
403+
404+intersectionWithKey :: forall a b c. (Int -> a -> b -> c) -> MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap c
405+intersectionWithKey = coerce (M.intersectionWithKey :: (Int -> a -> b -> c) -> M.IntMap a -> M.IntMap b -> M.IntMap c)
406+{-# INLINE intersectionWithKey #-}
407+
408+mergeWithKey :: forall a b c. (Int -> a -> b -> Maybe c) -> (MonoidalIntMap a -> MonoidalIntMap c) -> (MonoidalIntMap b -> MonoidalIntMap c) -> MonoidalIntMap a -> MonoidalIntMap b -> MonoidalIntMap c
409+mergeWithKey = coerce (M.mergeWithKey :: (Int -> a -> b -> Maybe c) -> (M.IntMap a -> M.IntMap c) -> (M.IntMap b -> M.IntMap c) -> M.IntMap a -> M.IntMap b -> M.IntMap c)
410+{-# INLINE mergeWithKey #-}
411+
412+map :: (a -> b) -> MonoidalIntMap a -> MonoidalIntMap b
413+map = fmap
414+{-# INLINE map #-}
415+
416+mapWithKey :: forall a b. (Int -> a -> b) -> MonoidalIntMap a -> MonoidalIntMap b
417+mapWithKey = coerce (M.mapWithKey :: (Int -> a -> b) -> M.IntMap a -> M.IntMap b)
418+{-# INLINE mapWithKey #-}
419+
420+traverseWithKey :: Applicative t => (Int -> a -> t b) -> MonoidalIntMap a -> t (MonoidalIntMap b)
421+traverseWithKey = itraverse
422+{-# INLINE traverseWithKey #-}
423+
424+mapAccum :: forall a b c. (a -> b -> (a, c)) -> a -> MonoidalIntMap b -> (a, MonoidalIntMap c)
425+mapAccum = coerce (M.mapAccum :: (a -> b -> (a, c)) -> a -> M.IntMap b -> (a, M.IntMap c))
426+{-# INLINE mapAccum #-}
427+
428+mapAccumWithKey :: forall a b c. (a -> Int -> b -> (a, c)) -> a -> MonoidalIntMap b -> (a, MonoidalIntMap c)
429+mapAccumWithKey = coerce (M.mapAccumWithKey :: (a -> Int -> b -> (a, c)) -> a -> M.IntMap b -> (a, M.IntMap c))
430+{-# INLINE mapAccumWithKey #-}
431+
432+mapAccumRWithKey :: forall a b c. (a -> Int -> b -> (a, c)) -> a -> MonoidalIntMap b -> (a, MonoidalIntMap c)
433+mapAccumRWithKey = coerce (M.mapAccumRWithKey :: (a -> Int -> b -> (a, c)) -> a -> M.IntMap b -> (a, M.IntMap c))
434+{-# INLINE mapAccumRWithKey #-}
435+
436+mapKeys :: forall a. (Int -> Int) -> MonoidalIntMap a -> MonoidalIntMap a
437+mapKeys = coerce (M.mapKeys :: (Int -> Int) -> M.IntMap a -> M.IntMap a)
438+{-# INLINE mapKeys #-}
439+
440+mapKeysWith :: forall a. (a -> a -> a) -> (Int -> Int) -> MonoidalIntMap a -> MonoidalIntMap a
441+mapKeysWith = coerce (M.mapKeysWith :: (a -> a -> a) -> (Int -> Int) -> M.IntMap a -> M.IntMap a)
442+{-# INLINE mapKeysWith #-}
443+
444+mapKeysMonotonic :: forall a. (Int -> Int) -> MonoidalIntMap a -> MonoidalIntMap a
445+mapKeysMonotonic = coerce (M.mapKeysMonotonic :: (Int -> Int) -> M.IntMap a -> M.IntMap a)
446+{-# INLINE mapKeysMonotonic #-}
447+
448+foldr :: forall a b. (a -> b -> b) -> b -> MonoidalIntMap a -> b
449+foldr = coerce (M.foldr :: (a -> b -> b) -> b -> M.IntMap a -> b)
450+{-# INLINE foldr #-}
451+
452+foldl :: forall a b. (a -> b -> a) -> a -> MonoidalIntMap b -> a
453+foldl = coerce (M.foldl :: (a -> b -> a) -> a -> M.IntMap b -> a)
454+{-# INLINE foldl #-}
455+
456+foldrWithKey :: forall a b. (Int -> a -> b -> b) -> b -> MonoidalIntMap a -> b
457+foldrWithKey = coerce (M.foldrWithKey :: (Int -> a -> b -> b) -> b -> M.IntMap a -> b)
458+{-# INLINE foldrWithKey #-}
459+
460+foldlWithKey :: forall a b. (a -> Int -> b -> a) -> a -> MonoidalIntMap b -> a
461+foldlWithKey = coerce (M.foldlWithKey :: (a -> Int -> b -> a) -> a -> M.IntMap b -> a)
462+{-# INLINE foldlWithKey #-}
463+
464+foldMapWithKey :: forall a m. Monoid m => (Int -> a -> m) -> MonoidalIntMap a -> m
465+foldMapWithKey = coerce (M.foldMapWithKey :: Monoid m => (Int -> a -> m) -> M.IntMap a -> m)
466+{-# INLINE foldMapWithKey #-}
467+
468+foldr' :: forall a b. (a -> b -> b) -> b -> MonoidalIntMap a -> b
469+foldr' = coerce (M.foldr' :: (a -> b -> b) -> b -> M.IntMap a -> b)
470+{-# INLINE foldr' #-}
471+
472+foldl' :: forall a b. (a -> b -> a) -> a -> MonoidalIntMap b -> a
473+foldl' = coerce (M.foldl' :: (a -> b -> a) -> a -> M.IntMap b -> a)
474+{-# INLINE foldl' #-}
475+
476+foldrWithKey' :: forall a b. (Int -> a -> b -> b) -> b -> MonoidalIntMap a -> b
477+foldrWithKey' = coerce (M.foldrWithKey' :: (Int -> a -> b -> b) -> b -> M.IntMap a -> b)
478+{-# INLINE foldrWithKey' #-}
479+
480+foldlWithKey' :: forall a b. (a -> Int -> b -> a) -> a -> MonoidalIntMap b -> a
481+foldlWithKey' = coerce (M.foldlWithKey' :: (a -> Int -> b -> a) -> a -> M.IntMap b -> a)
482+{-# INLINE foldlWithKey' #-}
483+
484+keysSet :: forall a. MonoidalIntMap a -> IntSet
485+keysSet = coerce (M.keysSet :: M.IntMap a -> IntSet)
486+{-# INLINE keysSet #-}
487+
488+fromSet :: forall a. (Int -> a) -> IntSet -> MonoidalIntMap a
489+fromSet = coerce (M.fromSet :: (Int -> a) -> IntSet -> M.IntMap a)
490+{-# INLINE fromSet #-}
491+
492+toList :: forall a. MonoidalIntMap a -> [(Int, a)]
493+toList = coerce (M.toList :: M.IntMap a -> [(Int, a)])
494+{-# INLINE toList #-}
495+
496+fromList :: forall a. [(Int, a)] -> MonoidalIntMap a
497+fromList = coerce (M.fromList :: [(Int, a)] -> M.IntMap a)
498+{-# INLINE fromList #-}
499+
500+fromListWith :: forall a. (a -> a -> a) -> [(Int, a)] -> MonoidalIntMap a
501+fromListWith = coerce (M.fromListWith :: (a -> a -> a) -> [(Int, a)] -> M.IntMap a)
502+{-# INLINE fromListWith #-}
503+
504+fromListWithKey :: forall a. (Int -> a -> a -> a) -> [(Int, a)] -> MonoidalIntMap a
505+fromListWithKey = coerce (M.fromListWithKey :: (Int -> a -> a -> a) -> [(Int, a)] -> M.IntMap a)
506+{-# INLINE fromListWithKey #-}
507+
508+toAscList :: forall a. MonoidalIntMap a -> [(Int, a)]
509+toAscList = coerce (M.toAscList :: M.IntMap a -> [(Int, a)])
510+{-# INLINE toAscList #-}
511+
512+toDescList :: forall a. MonoidalIntMap a -> [(Int, a)]
513+toDescList = coerce (M.toDescList :: M.IntMap a -> [(Int, a)])
514+{-# INLINE toDescList #-}
515+
516+fromAscList :: forall a. [(Int, a)] -> MonoidalIntMap a
517+fromAscList = coerce (M.fromAscList :: [(Int, a)] -> M.IntMap a)
518+{-# INLINE fromAscList #-}
519+
520+fromAscListWith :: forall a. (a -> a -> a) -> [(Int, a)] -> MonoidalIntMap a
521+fromAscListWith = coerce (M.fromAscListWith :: (a -> a -> a) -> [(Int, a)] -> M.IntMap a)
522+{-# INLINE fromAscListWith #-}
523+
524+fromAscListWithKey :: forall a. (Int -> a -> a -> a) -> [(Int, a)] -> MonoidalIntMap a
525+fromAscListWithKey = coerce (M.fromAscListWithKey :: (Int -> a -> a -> a) -> [(Int, a)] -> M.IntMap a)
526+{-# INLINE fromAscListWithKey #-}
527+
528+fromDistinctAscList :: forall a. [(Int, a)] -> MonoidalIntMap a
529+fromDistinctAscList = coerce (M.fromDistinctAscList :: [(Int, a)] -> M.IntMap a)
530+{-# INLINE fromDistinctAscList #-}
531+
532+fromDistinctList :: forall a. [(Int, a)] -> MonoidalIntMap a
533+fromDistinctList = coerce (M.fromList :: [(Int, a)] -> M.IntMap a)
534+{-# INLINE fromDistinctList #-}
535+
536+filter :: forall a. (a -> Bool) -> MonoidalIntMap a -> MonoidalIntMap a
537+filter = coerce (M.filter :: (a -> Bool) -> M.IntMap a -> M.IntMap a)
538+{-# INLINE filter #-}
539+
540+filterWithKey :: forall a. (Int -> a -> Bool) -> MonoidalIntMap a -> MonoidalIntMap a
541+filterWithKey = coerce (M.filterWithKey :: (Int -> a -> Bool) -> M.IntMap a -> M.IntMap a)
542+{-# INLINE filterWithKey #-}
543+
544+partition :: forall a. (a -> Bool) -> MonoidalIntMap a -> (MonoidalIntMap a, MonoidalIntMap a)
545+partition = coerce (M.partition :: (a -> Bool) -> M.IntMap a -> (M.IntMap a, M.IntMap a))
546+{-# INLINE partition #-}
547+
548+partitionWithKey :: forall a. (Int -> a -> Bool) -> MonoidalIntMap a -> (MonoidalIntMap a, MonoidalIntMap a)
549+partitionWithKey = coerce (M.partitionWithKey :: (Int -> a -> Bool) -> M.IntMap a -> (M.IntMap a, M.IntMap a))
550+{-# INLINE partitionWithKey #-}
551+
552+mapMaybe :: forall a b. (a -> Maybe b) -> MonoidalIntMap a -> MonoidalIntMap b
553+mapMaybe = coerce (M.mapMaybe :: (a -> Maybe b) -> M.IntMap a -> M.IntMap b)
554+{-# INLINE mapMaybe #-}
555+
556+mapMaybeWithKey :: forall a b. (Int -> a -> Maybe b) -> MonoidalIntMap a -> MonoidalIntMap b
557+mapMaybeWithKey = coerce (M.mapMaybeWithKey :: (Int -> a -> Maybe b) -> M.IntMap a -> M.IntMap b)
558+{-# INLINE mapMaybeWithKey #-}
559+
560+mapEither :: forall a b c. (a -> Either b c) -> MonoidalIntMap a -> (MonoidalIntMap b, MonoidalIntMap c)
561+mapEither = coerce (M.mapEither :: (a -> Either b c) -> M.IntMap a -> (M.IntMap b, M.IntMap c))
562+{-# INLINE mapEither #-}
563+
564+mapEitherWithKey :: forall a b c. (Int -> a -> Either b c) -> MonoidalIntMap a -> (MonoidalIntMap b, MonoidalIntMap c)
565+mapEitherWithKey = coerce (M.mapEitherWithKey :: (Int -> a -> Either b c) -> M.IntMap a -> (M.IntMap b, M.IntMap c))
566+{-# INLINE mapEitherWithKey #-}
567+
568+split :: forall a. Int -> MonoidalIntMap a -> (MonoidalIntMap a, MonoidalIntMap a)
569+split = coerce (M.split :: Int -> M.IntMap a -> (M.IntMap a, M.IntMap a))
570+{-# INLINE split #-}
571+
572+splitLookup :: forall a. Int -> MonoidalIntMap a -> (MonoidalIntMap a, Maybe a, MonoidalIntMap a)
573+splitLookup = coerce (M.splitLookup :: Int -> M.IntMap a -> (M.IntMap a, Maybe a, M.IntMap a))
574+{-# INLINE splitLookup #-}
575+
576+splitRoot :: forall a. MonoidalIntMap a -> [MonoidalIntMap a]
577+splitRoot = coerce (M.splitRoot :: M.IntMap a -> [M.IntMap a])
578+{-# INLINE splitRoot #-}
579+
580+isSubmapOf :: forall a. Eq a => MonoidalIntMap a -> MonoidalIntMap a -> Bool
581+isSubmapOf = coerce (M.isSubmapOf :: M.IntMap a -> M.IntMap a -> Bool)
582+{-# INLINE isSubmapOf #-}
583+
584+isSubmapOfBy :: forall a b. (a -> b -> Bool) -> MonoidalIntMap a -> MonoidalIntMap b -> Bool
585+isSubmapOfBy = coerce (M.isSubmapOfBy :: (a -> b -> Bool) -> M.IntMap a -> M.IntMap b -> Bool)
586+{-# INLINE isSubmapOfBy #-}
587+
588+isProperSubmapOf :: forall a. Eq a => MonoidalIntMap a -> MonoidalIntMap a -> Bool
589+isProperSubmapOf = coerce (M.isProperSubmapOf :: M.IntMap a -> M.IntMap a -> Bool)
590+{-# INLINE isProperSubmapOf #-}
591+
592+isProperSubmapOfBy :: forall a b. (a -> b -> Bool) -> MonoidalIntMap a -> MonoidalIntMap b -> Bool
593+isProperSubmapOfBy = coerce (M.isProperSubmapOfBy :: (a -> b -> Bool) -> M.IntMap a -> M.IntMap b -> Bool)
594+{-# INLINE isProperSubmapOfBy #-}
595+
596+findMin :: forall a. MonoidalIntMap a -> (Int, a)
597+findMin = coerce (M.findMin :: M.IntMap a -> (Int, a))
598+{-# INLINE findMin #-}
599+
600+findMax :: forall a. MonoidalIntMap a -> (Int, a)
601+findMax = coerce (M.findMax :: M.IntMap a -> (Int, a))
602+{-# INLINE findMax #-}
603+
604+deleteMin :: forall a. MonoidalIntMap a -> MonoidalIntMap a
605+deleteMin = coerce (M.deleteMin :: M.IntMap a -> M.IntMap a)
606+{-# INLINE deleteMin #-}
607+
608+deleteMax :: forall a. MonoidalIntMap a -> MonoidalIntMap a
609+deleteMax = coerce (M.deleteMax :: M.IntMap a -> M.IntMap a)
610+{-# INLINE deleteMax #-}
611+
612+deleteFindMin :: forall a. MonoidalIntMap a -> ((Int, a), MonoidalIntMap a)
613+deleteFindMin = coerce (M.deleteFindMin :: M.IntMap a -> ((Int, a), M.IntMap a))
614+{-# INLINE deleteFindMin #-}
615+
616+deleteFindMax :: forall a. MonoidalIntMap a -> ((Int, a), MonoidalIntMap a)
617+deleteFindMax = coerce (M.deleteFindMax :: M.IntMap a -> ((Int, a), M.IntMap a))
618+{-# INLINE deleteFindMax #-}
619+
620+updateMin :: forall a. (a -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap a
621+updateMin = coerce (M.updateMin :: (a -> Maybe a) -> M.IntMap a -> M.IntMap a)
622+{-# INLINE updateMin #-}
623+
624+updateMax :: forall a. (a -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap a
625+updateMax = coerce (M.updateMax :: (a -> Maybe a) -> M.IntMap a -> M.IntMap a)
626+{-# INLINE updateMax #-}
627+
628+updateMinWithKey :: forall a. (Int -> a -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap a
629+updateMinWithKey = coerce (M.updateMinWithKey :: (Int -> a -> Maybe a) -> M.IntMap a -> M.IntMap a)
630+{-# INLINE updateMinWithKey #-}
631+
632+updateMaxWithKey :: forall a. (Int -> a -> Maybe a) -> MonoidalIntMap a -> MonoidalIntMap a
633+updateMaxWithKey = coerce (M.updateMaxWithKey :: (Int -> a -> Maybe a) -> M.IntMap a -> M.IntMap a)
634+{-# INLINE updateMaxWithKey #-}
635+
636+minView :: forall a. MonoidalIntMap a -> Maybe (a, MonoidalIntMap a)
637+minView = coerce (M.minView :: M.IntMap a -> Maybe (a, M.IntMap a))
638+{-# INLINE minView #-}
639+
640+maxView :: forall a. MonoidalIntMap a -> Maybe (a, MonoidalIntMap a)
641+maxView = coerce (M.maxView :: M.IntMap a -> Maybe (a, M.IntMap a))
642+{-# INLINE maxView #-}
643+
644+minViewWithKey :: forall a. MonoidalIntMap a -> Maybe ((Int, a), MonoidalIntMap a)
645+minViewWithKey = coerce (M.minViewWithKey :: M.IntMap a -> Maybe ((Int, a), M.IntMap a))
646+{-# INLINE minViewWithKey #-}
647+
648+maxViewWithKey :: forall a. MonoidalIntMap a -> Maybe ((Int, a), MonoidalIntMap a)
649+maxViewWithKey = coerce (M.maxViewWithKey :: M.IntMap a -> Maybe ((Int, a), M.IntMap a))
650+{-# INLINE maxViewWithKey #-}
651+
src/Data/Map/Monoidal.hs+573−30
@@ -1,10 +1,12 @@
11{-# LANGUAGE CPP #-}
2+{-# LANGUAGE ScopedTypeVariables #-}
23{-# LANGUAGE MultiParamTypeClasses #-}
34{-# LANGUAGE TypeFamilies #-}
45{-# LANGUAGE FlexibleInstances #-}
56{-# LANGUAGE GeneralizedNewtypeDeriving #-}
67{-# LANGUAGE DeriveTraversable #-}
78{-# LANGUAGE DeriveDataTypeable #-}
9+{-# LANGUAGE StandaloneDeriving #-}
810
911-- | This module provides a 'Data.Map' variant which uses the value's
1012-- 'Monoid' instance to accumulate conflicting entries when merging
@@ -16,7 +18,7 @@
1618-- underlying 'Map'.
1719
1820module Data.Map.Monoidal
19− ( MonoidalMap
21+ ( MonoidalMap(..)
2022 -- * Often-needed functions
2123 , singleton
2224 , size
@@ -26,9 +28,110 @@ module Data.Map.Monoidal
2628 , assocs
2729 , elems
2830 , keys
31+ , (!)
32+ , (\\)
33+ , adjust
34+ , adjustWithKey
35+ , alter
36+ , delete
37+ , deleteAt
38+ , deleteFindMax
39+ , deleteFindMin
40+ , deleteMax
41+ , deleteMin
42+ , difference
43+ , differenceWith
44+ , differenceWithKey
45+ , elemAt
46+ , empty
47+ , filter
48+ , filterWithKey
49+ , findIndex
50+ , findMax
51+ , findMin
52+ , foldMapWithKey
53+ , foldl
54+ , foldl'
55+ , foldlWithKey
56+ , foldlWithKey'
57+ , foldr
58+ , foldr'
59+ , foldrWithKey
60+ , foldrWithKey'
61+ , fromAscList
62+ , fromAscListWith
63+ , fromAscListWithKey
64+ , fromDistinctAscList
65+ , fromDistinctList
66+ , fromList
67+ , fromListWith
68+ , fromListWithKey
69+ , fromSet
70+ , insert
71+ , insertLookupWithKey
72+ , insertWith
73+ , insertWithKey
74+ , intersectionWith
75+ , intersectionWithKey
76+ , isProperSubmapOf
77+ , isProperSubmapOfBy
78+ , isSubmapOf
79+ , isSubmapOfBy
80+ , keysSet
81+ , lookup
82+ , lookupGE
83+ , lookupGT
84+ , lookupIndex
85+ , lookupLE
86+ , lookupLT
87+ , map
88+ , mapAccum
89+ , mapAccumRWithKey
90+ , mapAccumWithKey
91+ , mapEither
92+ , mapEitherWithKey
93+ , mapKeys
94+ , mapKeysMonotonic
95+ , mapKeysWith
96+ , mapMaybe
97+ , mapMaybeWithKey
98+ , mapWithKey
99+ , maxView
100+ , maxViewWithKey
101+ , mergeWithKey
102+ , minView
103+ , minViewWithKey
104+ , null
105+ , partition
106+ , partitionWithKey
107+ , split
108+ , splitLookup
109+ , splitRoot
110+ , toAscList
111+ , toDescList
112+ , toList
113+ , traverseWithKey
114+ , unionWith
115+ , unionWithKey
116+ , unionsWith
117+ , update
118+ , updateAt
119+ , updateLookupWithKey
120+ , updateMax
121+ , updateMaxWithKey
122+ , updateMin
123+ , updateMinWithKey
124+ , updateWithKey
125+ , valid
126+ -- , showTree
127+ -- , showTreeWith
29128 ) where
30129
31−import Data.Monoid
130+import Prelude hiding (null, lookup, map, foldl, foldr, filter)
131+
132+import Data.Coerce (coerce)
133+import Data.Set (Set)
134+import Data.Semigroup
32135import Data.Foldable (Foldable)
33136import Data.Traversable (Traversable)
34137import Control.Applicative (Applicative, pure)
@@ -36,52 +139,80 @@ import Data.Data (Data)
36139import Data.Typeable (Typeable)
37140
38141#if MIN_VERSION_base(4,7,0)
39−import GHC.Exts (IsList(..))
142+import qualified GHC.Exts as IsList
40143#endif
41144
42145import Control.DeepSeq
43146import qualified Data.Map as M
44147import Control.Lens
45148import Control.Newtype
149+import Data.Aeson(FromJSON, ToJSON, FromJSON1, ToJSON1)
150+#if MIN_VERSION_containers(0,5,9)
151+import Data.Functor.Classes
152+#endif
153+import Data.Align
154+#ifdef MIN_VERSION_semialign
155+import Data.Semialign (Unalign)
156+#if MIN_VERSION_semialign(1,1,0)
157+import Data.Zip (Zip)
158+#endif
159+#endif
46160
47161-- | A 'Map' with monoidal accumulation
48−newtype MonoidalMap k a = MM (M.Map k a)
49− deriving (Show, Read, Functor, Eq, Ord, NFData,
50− Foldable, Traversable,
51− Data, Typeable)
162+newtype MonoidalMap k a = MonoidalMap { getMonoidalMap :: M.Map k a }
163+ deriving ( Show, Read, Functor, Eq, Ord, NFData
164+ , Foldable, Traversable
165+ , FromJSON, ToJSON, FromJSON1, ToJSON1
166+ , Data, Typeable, Align
167+#if MIN_VERSION_these(0,8,0)
168+ , Semialign
169+#endif
170+#ifdef MIN_VERSION_semialign
171+ , Unalign
172+#if MIN_VERSION_semialign(1,1,0)
173+ , Zip
174+#endif
175+#endif
176+ )
177+
178+#if MIN_VERSION_containers(0,5,9)
179+deriving instance (Ord k) => Eq1 (MonoidalMap k)
180+deriving instance (Ord k) => Ord1 (MonoidalMap k)
181+deriving instance (Show k) => Show1 (MonoidalMap k)
182+#endif
52183
53184type instance Index (MonoidalMap k a) = k
54185type instance IxValue (MonoidalMap k a) = a
55186instance Ord k => Ixed (MonoidalMap k a) where
56− ix k f (MM m) = case M.lookup k m of
57− Just v -> f v <&> \v' -> MM (M.insert k v' m)
58− Nothing -> pure (MM m)
187+ ix k f (MonoidalMap m) = case M.lookup k m of
188+ Just v -> f v <&> \v' -> MonoidalMap (M.insert k v' m)
189+ Nothing -> pure (MonoidalMap m)
59190 {-# INLINE ix #-}
60191
61192instance Ord k => At (MonoidalMap k a) where
62− at k f (MM m) = f mv <&> \r -> case r of
63− Nothing -> maybe (MM m) (const (MM $ M.delete k m)) mv
64− Just v' -> MM $ M.insert k v' m
193+ at k f (MonoidalMap m) = f mv <&> \r -> case r of
194+ Nothing -> maybe (MonoidalMap m) (const (MonoidalMap $ M.delete k m)) mv
195+ Just v' -> MonoidalMap $ M.insert k v' m
65196 where mv = M.lookup k m
66197 {-# INLINE at #-}
67198
68199instance Each (MonoidalMap k a) (MonoidalMap k b) a b
69200
70−instance Ord k => FunctorWithIndex k (MonoidalMap k)
71−instance Ord k => FoldableWithIndex k (MonoidalMap k)
72−instance Ord k => TraversableWithIndex k (MonoidalMap k) where
73− itraverse f (MM m) = fmap MM $ itraverse f m
201+instance FunctorWithIndex k (MonoidalMap k)
202+instance FoldableWithIndex k (MonoidalMap k)
203+instance TraversableWithIndex k (MonoidalMap k) where
204+ itraverse f (MonoidalMap m) = fmap MonoidalMap $ itraverse f m
74205 {-# INLINE itraverse #-}
75206
76207instance Ord k => TraverseMin k (MonoidalMap k) where
77− traverseMin f (MM m) = fmap MM $ traverseMin f m
208+ traverseMin f (MonoidalMap m) = fmap MonoidalMap $ traverseMin f m
78209 {-# INLINE traverseMin #-}
79210instance Ord k => TraverseMax k (MonoidalMap k) where
80− traverseMax f (MM m) = fmap MM $ traverseMax f m
211+ traverseMax f (MonoidalMap m) = fmap MonoidalMap $ traverseMax f m
81212 {-# INLINE traverseMax #-}
82213
83214instance AsEmpty (MonoidalMap k a) where
84− _Empty = nearly (MM M.empty) (M.null . unpack)
215+ _Empty = nearly (MonoidalMap M.empty) (M.null . unpack)
85216 {-# INLINE _Empty #-}
86217
87218instance Wrapped (MonoidalMap k a) where
@@ -89,30 +220,36 @@ instance Wrapped (MonoidalMap k a) where
89220 _Wrapped' = iso unpack pack
90221 {-# INLINE _Wrapped' #-}
91222
92−instance (Ord k, Monoid a) => Monoid (MonoidalMap k a) where
93− mempty = MM mempty
223+instance (Ord k, Semigroup a) => Semigroup (MonoidalMap k a) where
224+ MonoidalMap a <> MonoidalMap b = MonoidalMap $ M.unionWith (<>) a b
225+ {-# INLINE (<>) #-}
226+
227+instance (Ord k, Semigroup a) => Monoid (MonoidalMap k a) where
228+ mempty = MonoidalMap mempty
94229 {-# INLINE mempty #-}
95− MM a `mappend` MM b = MM $ M.unionWith mappend a b
230+#if !(MIN_VERSION_base(4,11,0))
231+ mappend (MonoidalMap a) (MonoidalMap b) = MonoidalMap $ M.unionWith (<>) a b
96232 {-# INLINE mappend #-}
233+#endif
97234
98235instance Newtype (MonoidalMap k a) (M.Map k a) where
99− pack = MM
236+ pack = MonoidalMap
100237 {-# INLINE pack #-}
101− unpack (MM a) = a
238+ unpack (MonoidalMap a) = a
102239 {-# INLINE unpack #-}
103240
104241#if MIN_VERSION_base(4,7,0)
105−instance (Ord k, Monoid a) => IsList (MonoidalMap k a) where
242+instance (Ord k, Semigroup a) => IsList.IsList (MonoidalMap k a) where
106243 type Item (MonoidalMap k a) = (k, a)
107− fromList = MM . M.fromListWith mappend
244+ fromList = MonoidalMap . M.fromListWith (<>)
108245 {-# INLINE fromList #-}
109246 toList = M.toList . unpack
110247 {-# INLINE toList #-}
111248#endif
112249
113250-- | /O(1)/. A map with a single element.
114−singleton :: Ord k => k -> a -> MonoidalMap k a
115−singleton k a = MM $ M.singleton k a
251+singleton :: k -> a -> MonoidalMap k a
252+singleton k a = MonoidalMap $ M.singleton k a
116253{-# INLINE singleton #-}
117254
118255-- | /O(1)/. The number of elements in the map.
@@ -140,7 +277,7 @@ findWithDefault def k = M.findWithDefault def k . unpack
140277-- | /O(log n)/. Delete a key and its value from the map. When the key is not
141278-- a member of the map, the original map is returned.
142279delete :: Ord k => k -> MonoidalMap k a -> MonoidalMap k a
143−delete k = _Wrapping' MM %~ M.delete k
280+delete k = _Wrapping' MonoidalMap %~ M.delete k
144281{-# INLINE delete #-}
145282
146283-- | /O(n)/. Return all elements of the map and their keys
@@ -159,3 +296,409 @@ elems = M.elems . unpack
159296keys :: MonoidalMap k a -> [k]
160297keys = M.keys . unpack
161298{-# INLINE keys #-}
299+
300+
301+
302+(!) :: forall k a. Ord k => MonoidalMap k a -> k -> a
303+(!) = coerce ((M.!) :: M.Map k a -> k -> a)
304+infixl 9 !
305+
306+(\\) :: forall k a b. Ord k => MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k a
307+(\\) = coerce ((M.\\) :: M.Map k a -> M.Map k b -> M.Map k a)
308+infixl 9 \\ --
309+
310+null :: forall k a. MonoidalMap k a -> Bool
311+null = coerce (M.null :: M.Map k a -> Bool)
312+{-# INLINE null #-}
313+
314+lookup :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe a
315+lookup = coerce (M.lookup :: k -> M.Map k a -> Maybe a)
316+{-# INLINE lookup #-}
317+
318+lookupLT :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe (k, a)
319+lookupLT = coerce (M.lookupLT :: k -> M.Map k a -> Maybe (k,a))
320+{-# INLINE lookupLT #-}
321+
322+lookupGT :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe (k, a)
323+lookupGT = coerce (M.lookupGT :: k -> M.Map k a -> Maybe (k,a))
324+{-# INLINE lookupGT #-}
325+
326+lookupLE :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe (k, a)
327+lookupLE = coerce (M.lookupLE :: k -> M.Map k a -> Maybe (k,a))
328+{-# INLINE lookupLE #-}
329+
330+lookupGE :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe (k, a)
331+lookupGE = coerce (M.lookupGE :: k -> M.Map k a -> Maybe (k,a))
332+{-# INLINE lookupGE #-}
333+
334+empty :: forall k a. MonoidalMap k a
335+empty = coerce (M.empty :: M.Map k a)
336+{-# INLINE empty #-}
337+
338+insert :: forall k a. Ord k => k -> a -> MonoidalMap k a -> MonoidalMap k a
339+insert = coerce (M.insert :: k -> a -> M.Map k a -> M.Map k a)
340+{-# INLINE insert #-}
341+
342+insertWith :: forall k a. Ord k => (a -> a -> a) -> k -> a -> MonoidalMap k a -> MonoidalMap k a
343+insertWith = coerce (M.insertWith :: (a -> a -> a) -> k -> a -> M.Map k a -> M.Map k a)
344+{-# INLINE insertWith #-}
345+
346+insertWithKey :: forall k a. Ord k => (k -> a -> a -> a) -> k -> a -> MonoidalMap k a -> MonoidalMap k a
347+insertWithKey = coerce (M.insertWithKey :: (k -> a -> a -> a) -> k -> a -> M.Map k a -> M.Map k a)
348+{-# INLINE insertWithKey #-}
349+
350+insertLookupWithKey :: forall k a. Ord k => (k -> a -> a -> a) -> k -> a -> MonoidalMap k a -> (Maybe a, MonoidalMap k a)
351+insertLookupWithKey = coerce (M.insertLookupWithKey :: (k -> a -> a -> a) -> k -> a -> M.Map k a -> (Maybe a, M.Map k a))
352+{-# INLINE insertLookupWithKey #-}
353+
354+adjust :: forall k a. Ord k => (a -> a) -> k -> MonoidalMap k a -> MonoidalMap k a
355+adjust = coerce (M.adjust :: (a -> a) -> k -> M.Map k a -> M.Map k a)
356+{-# INLINE adjust #-}
357+
358+adjustWithKey :: forall k a. Ord k => (k -> a -> a) -> k -> MonoidalMap k a -> MonoidalMap k a
359+adjustWithKey = coerce (M.adjustWithKey :: (k -> a -> a) -> k -> M.Map k a -> M.Map k a)
360+{-# INLINE adjustWithKey #-}
361+
362+update :: forall k a. Ord k => (a -> Maybe a) -> k -> MonoidalMap k a -> MonoidalMap k a
363+update = coerce (M.update :: (a -> Maybe a) -> k -> M.Map k a -> M.Map k a)
364+{-# INLINE update #-}
365+
366+updateWithKey :: forall k a. Ord k => (k -> a -> Maybe a) -> k -> MonoidalMap k a -> MonoidalMap k a
367+updateWithKey = coerce (M.updateWithKey :: (k -> a -> Maybe a) -> k -> M.Map k a -> M.Map k a)
368+{-# INLINE updateWithKey #-}
369+
370+updateLookupWithKey :: forall k a. Ord k => (k -> a -> Maybe a) -> k -> MonoidalMap k a -> (Maybe a, MonoidalMap k a)
371+updateLookupWithKey = coerce (M.updateLookupWithKey :: (k -> a -> Maybe a) -> k -> M.Map k a -> (Maybe a, M.Map k a))
372+{-# INLINE updateLookupWithKey #-}
373+
374+alter :: forall k a. Ord k => (Maybe a -> Maybe a) -> k -> MonoidalMap k a -> MonoidalMap k a
375+alter = coerce (M.alter :: (Maybe a -> Maybe a) -> k -> M.Map k a -> M.Map k a)
376+{-# INLINE alter #-}
377+
378+unionWith :: forall k a. Ord k => (a -> a -> a) -> MonoidalMap k a -> MonoidalMap k a -> MonoidalMap k a
379+unionWith = coerce (M.unionWith :: (a -> a -> a) -> M.Map k a -> M.Map k a -> M.Map k a)
380+{-# INLINE unionWith #-}
381+
382+unionWithKey :: forall k a. Ord k => (k -> a -> a -> a) -> MonoidalMap k a -> MonoidalMap k a -> MonoidalMap k a
383+unionWithKey = coerce (M.unionWithKey :: (k -> a -> a -> a) -> M.Map k a -> M.Map k a -> M.Map k a)
384+{-# INLINE unionWithKey #-}
385+
386+unionsWith :: forall k a. Ord k => (a -> a -> a) -> [MonoidalMap k a] -> MonoidalMap k a
387+unionsWith = coerce (M.unionsWith :: (a -> a -> a) -> [M.Map k a] -> M.Map k a)
388+{-# INLINE unionsWith #-}
389+
390+difference :: forall k a b. Ord k => MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k a
391+difference = (\\)
392+{-# INLINE difference #-}
393+
394+differenceWith :: forall k a b. Ord k => (a -> b -> Maybe a) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k a
395+differenceWith = coerce (M.differenceWith :: (a -> b -> Maybe a) -> M.Map k a -> M.Map k b -> M.Map k a)
396+{-# INLINE differenceWith #-}
397+
398+differenceWithKey :: forall k a b. Ord k => (k -> a -> b -> Maybe a) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k a
399+differenceWithKey = coerce (M.differenceWithKey :: (k -> a -> b -> Maybe a) -> M.Map k a -> M.Map k b -> M.Map k a)
400+{-# INLINE differenceWithKey #-}
401+
402+intersectionWith :: forall k a b c. Ord k => (a -> b -> c) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k c
403+intersectionWith = coerce (M.intersectionWith :: (a -> b -> c) -> M.Map k a -> M.Map k b -> M.Map k c)
404+{-# INLINE intersectionWith #-}
405+
406+intersectionWithKey :: forall k a b c. Ord k => (k -> a -> b -> c) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k c
407+intersectionWithKey = coerce (M.intersectionWithKey :: (k -> a -> b -> c) -> M.Map k a -> M.Map k b -> M.Map k c)
408+{-# INLINE intersectionWithKey #-}
409+
410+mergeWithKey :: forall k a b c. Ord k => (k -> a -> b -> Maybe c) -> (MonoidalMap k a -> MonoidalMap k c) -> (MonoidalMap k b -> MonoidalMap k c) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k c
411+mergeWithKey = coerce (M.mergeWithKey :: (k -> a -> b -> Maybe c) -> (M.Map k a -> M.Map k c) -> (M.Map k b -> M.Map k c) -> M.Map k a -> M.Map k b -> M.Map k c)
412+{-# INLINE mergeWithKey #-}
413+
414+map :: (a -> b) -> MonoidalMap k a -> MonoidalMap k b
415+map = fmap
416+{-# INLINE map #-}
417+
418+mapWithKey :: forall k a b. (k -> a -> b) -> MonoidalMap k a -> MonoidalMap k b
419+mapWithKey = coerce (M.mapWithKey :: (k -> a -> b) -> M.Map k a -> M.Map k b)
420+{-# INLINE mapWithKey #-}
421+
422+traverseWithKey :: Applicative t => (k -> a -> t b) -> MonoidalMap k a -> t (MonoidalMap k b)
423+traverseWithKey = itraverse
424+{-# INLINE traverseWithKey #-}
425+
426+mapAccum :: forall k a b c. (a -> b -> (a, c)) -> a -> MonoidalMap k b -> (a, MonoidalMap k c)
427+mapAccum = coerce (M.mapAccum :: (a -> b -> (a, c)) -> a -> M.Map k b -> (a, M.Map k c))
428+{-# INLINE mapAccum #-}
429+
430+mapAccumWithKey :: forall k a b c. (a -> k -> b -> (a, c)) -> a -> MonoidalMap k b -> (a, MonoidalMap k c)
431+mapAccumWithKey = coerce (M.mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> M.Map k b -> (a, M.Map k c))
432+{-# INLINE mapAccumWithKey #-}
433+
434+mapAccumRWithKey :: forall k a b c. (a -> k -> b -> (a, c)) -> a -> MonoidalMap k b -> (a, MonoidalMap k c)
435+mapAccumRWithKey = coerce (M.mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> M.Map k b -> (a, M.Map k c))
436+{-# INLINE mapAccumRWithKey #-}
437+
438+mapKeys :: forall k1 k2 a. Ord k2 => (k1 -> k2) -> MonoidalMap k1 a -> MonoidalMap k2 a
439+mapKeys = coerce (M.mapKeys :: (k1 -> k2) -> M.Map k1 a -> M.Map k2 a)
440+{-# INLINE mapKeys #-}
441+
442+mapKeysWith :: forall k1 k2 a. Ord k2 => (a -> a -> a) -> (k1 -> k2) -> MonoidalMap k1 a -> MonoidalMap k2 a
443+mapKeysWith = coerce (M.mapKeysWith :: (a -> a -> a) -> (k1 -> k2) -> M.Map k1 a -> M.Map k2 a)
444+{-# INLINE mapKeysWith #-}
445+
446+-- | /O(n)/.
447+-- @'mapKeysMonotonic' f s == 'mapKeys' f s@, but works only when @f@
448+-- is strictly increasing (both monotonic and injective).
449+-- That is, for any values @x@ and @y@, if @x@ < @y@ then @f x@ < @f y@
450+-- and @f@ is injective (i.e. it never maps two input keys to the same output key).
451+-- /The precondition is not checked./
452+-- Semi-formally, we have:
453+--
454+-- > and [x < y ==> f x < f y | x <- ls, y <- ls]
455+-- > ==> mapKeysMonotonic f s == mapKeys f s
456+-- > where ls = keys s
457+--
458+-- This means that @f@ maps distinct original keys to distinct resulting keys.
459+-- This function has better performance than 'mapKeys'.
460+--
461+-- > mapKeysMonotonic (\ k -> k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")]
462+-- > valid (mapKeysMonotonic (\ k -> k * 2) (fromList [(5,"a"), (3,"b")])) == True
463+-- > valid (mapKeysMonotonic (\ _ -> 1) (fromList [(5,"a"), (3,"b")])) == False
464+mapKeysMonotonic :: forall k1 k2 a. (k1 -> k2) -> MonoidalMap k1 a -> MonoidalMap k2 a
465+mapKeysMonotonic = coerce (M.mapKeysMonotonic :: (k1 -> k2) -> M.Map k1 a -> M.Map k2 a)
466+{-# INLINE mapKeysMonotonic #-}
467+
468+foldr :: forall k a b. (a -> b -> b) -> b -> MonoidalMap k a -> b
469+foldr = coerce (M.foldr :: (a -> b -> b) -> b -> M.Map k a -> b)
470+{-# INLINE foldr #-}
471+
472+foldl :: forall k a b. (a -> b -> a) -> a -> MonoidalMap k b -> a
473+foldl = coerce (M.foldl :: (a -> b -> a) -> a -> M.Map k b -> a)
474+{-# INLINE foldl #-}
475+
476+foldrWithKey :: forall k a b. (k -> a -> b -> b) -> b -> MonoidalMap k a -> b
477+foldrWithKey = coerce (M.foldrWithKey :: (k -> a -> b -> b) -> b -> M.Map k a -> b)
478+{-# INLINE foldrWithKey #-}
479+
480+foldlWithKey :: forall k a b. (a -> k -> b -> a) -> a -> MonoidalMap k b -> a
481+foldlWithKey = coerce (M.foldlWithKey :: (a -> k -> b -> a) -> a -> M.Map k b -> a)
482+{-# INLINE foldlWithKey #-}
483+
484+foldMapWithKey :: forall k a m. Monoid m => (k -> a -> m) -> MonoidalMap k a -> m
485+foldMapWithKey = coerce (M.foldMapWithKey :: Monoid m => (k -> a -> m) -> M.Map k a -> m)
486+{-# INLINE foldMapWithKey #-}
487+
488+foldr' :: forall k a b. (a -> b -> b) -> b -> MonoidalMap k a -> b
489+foldr' = coerce (M.foldr' :: (a -> b -> b) -> b -> M.Map k a -> b)
490+{-# INLINE foldr' #-}
491+
492+foldl' :: forall k a b. (a -> b -> a) -> a -> MonoidalMap k b -> a
493+foldl' = coerce (M.foldl' :: (a -> b -> a) -> a -> M.Map k b -> a)
494+{-# INLINE foldl' #-}
495+
496+foldrWithKey' :: forall k a b. (k -> a -> b -> b) -> b -> MonoidalMap k a -> b
497+foldrWithKey' = coerce (M.foldrWithKey' :: (k -> a -> b -> b) -> b -> M.Map k a -> b)
498+{-# INLINE foldrWithKey' #-}
499+
500+foldlWithKey' :: forall k a b. (a -> k -> b -> a) -> a -> MonoidalMap k b -> a
501+foldlWithKey' = coerce (M.foldlWithKey' :: (a -> k -> b -> a) -> a -> M.Map k b -> a)
502+{-# INLINE foldlWithKey' #-}
503+
504+keysSet :: forall k a. MonoidalMap k a -> Set k
505+keysSet = coerce (M.keysSet :: M.Map k a -> Set k)
506+{-# INLINE keysSet #-}
507+
508+fromSet :: forall k a. (k -> a) -> Set k -> MonoidalMap k a
509+fromSet = coerce (M.fromSet :: (k -> a) -> Set k -> M.Map k a)
510+{-# INLINE fromSet #-}
511+
512+toList :: forall k a. MonoidalMap k a -> [(k, a)]
513+toList = coerce (M.toList :: M.Map k a -> [(k, a)])
514+{-# INLINE toList #-}
515+
516+fromList :: forall k a. Ord k => [(k, a)] -> MonoidalMap k a
517+fromList = coerce (M.fromList :: [(k, a)] -> M.Map k a)
518+{-# INLINE fromList #-}
519+
520+fromListWith :: forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> MonoidalMap k a
521+fromListWith = coerce (M.fromListWith :: (a -> a -> a) -> [(k, a)] -> M.Map k a)
522+{-# INLINE fromListWith #-}
523+
524+fromListWithKey :: forall k a. Ord k => (k -> a -> a -> a) -> [(k, a)] -> MonoidalMap k a
525+fromListWithKey = coerce (M.fromListWithKey :: (k -> a -> a -> a) -> [(k, a)] -> M.Map k a)
526+{-# INLINE fromListWithKey #-}
527+
528+toAscList :: forall k a. MonoidalMap k a -> [(k, a)]
529+toAscList = coerce (M.toAscList :: M.Map k a -> [(k, a)])
530+{-# INLINE toAscList #-}
531+
532+toDescList :: forall k a. MonoidalMap k a -> [(k, a)]
533+toDescList = coerce (M.toDescList :: M.Map k a -> [(k, a)])
534+{-# INLINE toDescList #-}
535+
536+fromAscList :: forall k a. Eq k => [(k, a)] -> MonoidalMap k a
537+fromAscList = coerce (M.fromAscList :: [(k, a)] -> M.Map k a)
538+{-# INLINE fromAscList #-}
539+
540+fromAscListWith :: forall k a. Eq k => (a -> a -> a) -> [(k, a)] -> MonoidalMap k a
541+fromAscListWith = coerce (M.fromAscListWith :: (a -> a -> a) -> [(k, a)] -> M.Map k a)
542+{-# INLINE fromAscListWith #-}
543+
544+fromAscListWithKey :: forall k a. Eq k => (k -> a -> a -> a) -> [(k, a)] -> MonoidalMap k a
545+fromAscListWithKey = coerce (M.fromAscListWithKey :: (k -> a -> a -> a) -> [(k, a)] -> M.Map k a)
546+{-# INLINE fromAscListWithKey #-}
547+
548+fromDistinctAscList :: forall k a. [(k, a)] -> MonoidalMap k a
549+fromDistinctAscList = coerce (M.fromDistinctAscList :: [(k, a)] -> M.Map k a)
550+{-# INLINE fromDistinctAscList #-}
551+
552+fromDistinctList :: forall k a. Ord k => [(k, a)] -> MonoidalMap k a
553+fromDistinctList = coerce (M.fromList :: [(k, a)] -> M.Map k a)
554+{-# INLINE fromDistinctList #-}
555+
556+filter :: forall k a. (a -> Bool) -> MonoidalMap k a -> MonoidalMap k a
557+filter = coerce (M.filter :: (a -> Bool) -> M.Map k a -> M.Map k a)
558+{-# INLINE filter #-}
559+
560+filterWithKey :: forall k a. (k -> a -> Bool) -> MonoidalMap k a -> MonoidalMap k a
561+filterWithKey = coerce (M.filterWithKey :: (k -> a -> Bool) -> M.Map k a -> M.Map k a)
562+{-# INLINE filterWithKey #-}
563+
564+partition :: forall k a. (a -> Bool) -> MonoidalMap k a -> (MonoidalMap k a, MonoidalMap k a)
565+partition = coerce (M.partition :: (a -> Bool) -> M.Map k a -> (M.Map k a, M.Map k a))
566+{-# INLINE partition #-}
567+
568+partitionWithKey :: forall k a. (k -> a -> Bool) -> MonoidalMap k a -> (MonoidalMap k a, MonoidalMap k a)
569+partitionWithKey = coerce (M.partitionWithKey :: (k -> a -> Bool) -> M.Map k a -> (M.Map k a, M.Map k a))
570+{-# INLINE partitionWithKey #-}
571+
572+mapMaybe :: forall k a b. (a -> Maybe b) -> MonoidalMap k a -> MonoidalMap k b
573+mapMaybe = coerce (M.mapMaybe :: (a -> Maybe b) -> M.Map k a -> M.Map k b)
574+{-# INLINE mapMaybe #-}
575+
576+mapMaybeWithKey :: forall k a b. (k -> a -> Maybe b) -> MonoidalMap k a -> MonoidalMap k b
577+mapMaybeWithKey = coerce (M.mapMaybeWithKey :: (k -> a -> Maybe b) -> M.Map k a -> M.Map k b)
578+{-# INLINE mapMaybeWithKey #-}
579+
580+mapEither :: forall k a b c. (a -> Either b c) -> MonoidalMap k a -> (MonoidalMap k b, MonoidalMap k c)
581+mapEither = coerce (M.mapEither :: (a -> Either b c) -> M.Map k a -> (M.Map k b, M.Map k c))
582+{-# INLINE mapEither #-}
583+
584+mapEitherWithKey :: forall k a b c. (k -> a -> Either b c) -> MonoidalMap k a -> (MonoidalMap k b, MonoidalMap k c)
585+mapEitherWithKey = coerce (M.mapEitherWithKey :: (k -> a -> Either b c) -> M.Map k a -> (M.Map k b, M.Map k c))
586+{-# INLINE mapEitherWithKey #-}
587+
588+split :: forall k a. Ord k => k -> MonoidalMap k a -> (MonoidalMap k a, MonoidalMap k a)
589+split = coerce (M.split :: k -> M.Map k a -> (M.Map k a, M.Map k a))
590+{-# INLINE split #-}
591+
592+splitLookup :: forall k a. Ord k => k -> MonoidalMap k a -> (MonoidalMap k a, Maybe a, MonoidalMap k a)
593+splitLookup = coerce (M.splitLookup :: k -> M.Map k a -> (M.Map k a, Maybe a, M.Map k a))
594+{-# INLINE splitLookup #-}
595+
596+splitRoot :: forall k a. MonoidalMap k a -> [MonoidalMap k a]
597+splitRoot = coerce (M.splitRoot :: M.Map k a -> [M.Map k a])
598+{-# INLINE splitRoot #-}
599+
600+isSubmapOf :: forall k a. (Ord k, Eq a) => MonoidalMap k a -> MonoidalMap k a -> Bool
601+isSubmapOf = coerce (M.isSubmapOf :: M.Map k a -> M.Map k a -> Bool)
602+{-# INLINE isSubmapOf #-}
603+
604+isSubmapOfBy :: forall k a b. Ord k => (a -> b -> Bool) -> MonoidalMap k a -> MonoidalMap k b -> Bool
605+isSubmapOfBy = coerce (M.isSubmapOfBy :: (a -> b -> Bool) -> M.Map k a -> M.Map k b -> Bool)
606+{-# INLINE isSubmapOfBy #-}
607+
608+isProperSubmapOf :: forall k a. (Ord k, Eq a) => MonoidalMap k a -> MonoidalMap k a -> Bool
609+isProperSubmapOf = coerce (M.isProperSubmapOf :: M.Map k a -> M.Map k a -> Bool)
610+{-# INLINE isProperSubmapOf #-}
611+
612+isProperSubmapOfBy :: forall k a b. Ord k => (a -> b -> Bool) -> MonoidalMap k a -> MonoidalMap k b -> Bool
613+isProperSubmapOfBy = coerce (M.isProperSubmapOfBy :: (a -> b -> Bool) -> M.Map k a -> M.Map k b -> Bool)
614+{-# INLINE isProperSubmapOfBy #-}
615+
616+lookupIndex :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe Int
617+lookupIndex = coerce (M.lookupIndex :: k -> M.Map k a -> Maybe Int)
618+{-# INLINE lookupIndex #-}
619+
620+findIndex :: forall k a. Ord k => k -> MonoidalMap k a -> Int
621+findIndex = coerce (M.findIndex :: k -> M.Map k a -> Int)
622+{-# INLINE findIndex #-}
623+
624+elemAt :: forall k a. Int -> MonoidalMap k a -> (k, a)
625+elemAt = coerce (M.elemAt :: Int -> M.Map k a -> (k, a))
626+{-# INLINE elemAt #-}
627+
628+updateAt :: forall k a. (k -> a -> Maybe a) -> Int -> MonoidalMap k a -> MonoidalMap k a
629+updateAt = coerce (M.updateAt :: (k -> a -> Maybe a) -> Int -> M.Map k a -> M.Map k a)
630+{-# INLINE updateAt #-}
631+
632+deleteAt :: forall k a. Int -> MonoidalMap k a -> MonoidalMap k a
633+deleteAt = coerce (M.deleteAt :: Int -> M.Map k a -> M.Map k a)
634+{-# INLINE deleteAt #-}
635+
636+findMin :: forall k a. MonoidalMap k a -> (k, a)
637+findMin = coerce (M.findMin :: M.Map k a -> (k, a))
638+{-# INLINE findMin #-}
639+
640+findMax :: forall k a. MonoidalMap k a -> (k, a)
641+findMax = coerce (M.findMax :: M.Map k a -> (k, a))
642+{-# INLINE findMax #-}
643+
644+deleteMin :: forall k a. MonoidalMap k a -> MonoidalMap k a
645+deleteMin = coerce (M.deleteMin :: M.Map k a -> M.Map k a)
646+{-# INLINE deleteMin #-}
647+
648+deleteMax :: forall k a. MonoidalMap k a -> MonoidalMap k a
649+deleteMax = coerce (M.deleteMax :: M.Map k a -> M.Map k a)
650+{-# INLINE deleteMax #-}
651+
652+deleteFindMin :: forall k a. MonoidalMap k a -> ((k, a), MonoidalMap k a)
653+deleteFindMin = coerce (M.deleteFindMin :: M.Map k a -> ((k, a), M.Map k a))
654+{-# INLINE deleteFindMin #-}
655+
656+deleteFindMax :: forall k a. MonoidalMap k a -> ((k, a), MonoidalMap k a)
657+deleteFindMax = coerce (M.deleteFindMax :: M.Map k a -> ((k, a), M.Map k a))
658+{-# INLINE deleteFindMax #-}
659+
660+updateMin :: forall k a. (a -> Maybe a) -> MonoidalMap k a -> MonoidalMap k a
661+updateMin = coerce (M.updateMin :: (a -> Maybe a) -> M.Map k a -> M.Map k a)
662+{-# INLINE updateMin #-}
663+
664+updateMax :: forall k a. (a -> Maybe a) -> MonoidalMap k a -> MonoidalMap k a
665+updateMax = coerce (M.updateMax :: (a -> Maybe a) -> M.Map k a -> M.Map k a)
666+{-# INLINE updateMax #-}
667+
668+updateMinWithKey :: forall k a. (k -> a -> Maybe a) -> MonoidalMap k a -> MonoidalMap k a
669+updateMinWithKey = coerce (M.updateMinWithKey :: (k -> a -> Maybe a) -> M.Map k a -> M.Map k a)
670+{-# INLINE updateMinWithKey #-}
671+
672+updateMaxWithKey :: forall k a. (k -> a -> Maybe a) -> MonoidalMap k a -> MonoidalMap k a
673+updateMaxWithKey = coerce (M.updateMaxWithKey :: (k -> a -> Maybe a) -> M.Map k a -> M.Map k a)
674+{-# INLINE updateMaxWithKey #-}
675+
676+minView :: forall k a. MonoidalMap k a -> Maybe (a, MonoidalMap k a)
677+minView = coerce (M.minView :: M.Map k a -> Maybe (a, M.Map k a))
678+{-# INLINE minView #-}
679+
680+maxView :: forall k a. MonoidalMap k a -> Maybe (a, MonoidalMap k a)
681+maxView = coerce (M.maxView :: M.Map k a -> Maybe (a, M.Map k a))
682+{-# INLINE maxView #-}
683+
684+minViewWithKey :: forall k a. MonoidalMap k a -> Maybe ((k, a), MonoidalMap k a)
685+minViewWithKey = coerce (M.minViewWithKey :: M.Map k a -> Maybe ((k, a), M.Map k a))
686+{-# INLINE minViewWithKey #-}
687+
688+maxViewWithKey :: forall k a. MonoidalMap k a -> Maybe ((k, a), MonoidalMap k a)
689+maxViewWithKey = coerce (M.maxViewWithKey :: M.Map k a -> Maybe ((k, a), M.Map k a))
690+{-# INLINE maxViewWithKey #-}
691+
692+-- showTree :: forall k a. (Show k, Show a) => MonoidalMap k a -> String
693+-- showTree = coerce (M.showTree :: (Show k, Show a) => M.Map k a -> String)
694+-- {-# INLINE showTree #-}
695+
696+-- showTreeWith :: forall k a. (k -> a -> String) -> Bool -> Bool -> MonoidalMap k a -> String
697+-- showTreeWith = coerce (M.showTreeWith :: (k -> a -> String) -> Bool -> Bool -> M.Map k a -> String)
698+-- {-# INLINE showTreeWith #-}
699+
700+valid :: forall k a. Ord k => MonoidalMap k a -> Bool
701+valid = coerce (M.valid :: Ord k => M.Map k a -> Bool)
702+{-# INLINE valid #-}
703+
704+
src/Data/Map/Monoidal/Strict.hsnew file+701
@@ -0,0 +1,701 @@
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE MultiParamTypeClasses #-}
3+{-# LANGUAGE TypeFamilies #-}
4+{-# LANGUAGE FlexibleInstances #-}
5+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
6+{-# LANGUAGE DeriveTraversable #-}
7+{-# LANGUAGE DeriveDataTypeable #-}
8+{-# LANGUAGE ScopedTypeVariables #-}
9+{-# LANGUAGE StandaloneDeriving #-}
10+
11+-- | This module provides a 'Data.Map' variant which uses the value's
12+-- 'Monoid' instance to accumulate conflicting entries when merging
13+-- 'Map's.
14+--
15+-- While some functions mirroring those of 'Data.Map' are provided
16+-- here for convenience, more specialized needs will likely want to use
17+-- either the 'Newtype' or 'Wrapped' instances to manipulate the
18+-- underlying 'Map'.
19+
20+module Data.Map.Monoidal.Strict
21+ ( MonoidalMap(..)
22+ -- * Often-needed functions
23+ , singleton
24+ , size
25+ , member
26+ , notMember
27+ , findWithDefault
28+ , assocs
29+ , elems
30+ , keys
31+ , (!)
32+ , (\\)
33+ , adjust
34+ , adjustWithKey
35+ , alter
36+ , delete
37+ , deleteAt
38+ , deleteFindMax
39+ , deleteFindMin
40+ , deleteMax
41+ , deleteMin
42+ , difference
43+ , differenceWith
44+ , differenceWithKey
45+ , elemAt
46+ , empty
47+ , filter
48+ , filterWithKey
49+ , findIndex
50+ , findMax
51+ , findMin
52+ , foldMapWithKey
53+ , foldl
54+ , foldl'
55+ , foldlWithKey
56+ , foldlWithKey'
57+ , foldr
58+ , foldr'
59+ , foldrWithKey
60+ , foldrWithKey'
61+ , fromAscList
62+ , fromAscListWith
63+ , fromAscListWithKey
64+ , fromDistinctAscList
65+ , fromDistinctList
66+ , fromList
67+ , fromListWith
68+ , fromListWithKey
69+ , fromSet
70+ , insert
71+ , insertLookupWithKey
72+ , insertWith
73+ , insertWithKey
74+ , intersectionWith
75+ , intersectionWithKey
76+ , isProperSubmapOf
77+ , isProperSubmapOfBy
78+ , isSubmapOf
79+ , isSubmapOfBy
80+ , keysSet
81+ , lookup
82+ , lookupGE
83+ , lookupGT
84+ , lookupIndex
85+ , lookupLE
86+ , lookupLT
87+ , map
88+ , mapAccum
89+ , mapAccumRWithKey
90+ , mapAccumWithKey
91+ , mapEither
92+ , mapEitherWithKey
93+ , mapKeys
94+ , mapKeysMonotonic
95+ , mapKeysWith
96+ , mapMaybe
97+ , mapMaybeWithKey
98+ , mapWithKey
99+ , maxView
100+ , maxViewWithKey
101+ , mergeWithKey
102+ , minView
103+ , minViewWithKey
104+ , null
105+ , partition
106+ , partitionWithKey
107+ , split
108+ , splitLookup
109+ , splitRoot
110+ , toAscList
111+ , toDescList
112+ , toList
113+ , traverseWithKey
114+ , unionWith
115+ , unionWithKey
116+ , unionsWith
117+ , update
118+ , updateAt
119+ , updateLookupWithKey
120+ , updateMax
121+ , updateMaxWithKey
122+ , updateMin
123+ , updateMinWithKey
124+ , updateWithKey
125+ , valid
126+ -- , showTree
127+ -- , showTreeWith
128+ ) where
129+
130+import Prelude hiding (null, lookup, map, foldl, foldr, filter)
131+
132+import Data.Coerce (coerce)
133+import Data.Semigroup
134+import Data.Foldable (Foldable)
135+import Data.Traversable (Traversable)
136+import Control.Applicative (Applicative, pure)
137+import Data.Data (Data)
138+import Data.Typeable (Typeable)
139+import Data.Set (Set)
140+
141+#if MIN_VERSION_base(4,7,0)
142+import qualified GHC.Exts as IsList
143+#endif
144+
145+import Control.DeepSeq
146+import qualified Data.Map.Strict as M
147+import Control.Lens
148+import Control.Newtype
149+import Data.Aeson(FromJSON, ToJSON, FromJSON1, ToJSON1)
150+#if MIN_VERSION_containers(0,5,9)
151+import Data.Functor.Classes
152+#endif
153+import Data.Align
154+#ifdef MIN_VERSION_semialign
155+import Data.Semialign (Unalign)
156+#if MIN_VERSION_semialign(1,1,0)
157+import Data.Zip (Zip)
158+#endif
159+#endif
160+
161+-- | A 'Map' with monoidal accumulation
162+newtype MonoidalMap k a = MonoidalMap { getMonoidalMap :: M.Map k a }
163+ deriving ( Show, Read, Functor, Eq, Ord, NFData
164+ , Foldable, Traversable
165+ , FromJSON, ToJSON, FromJSON1, ToJSON1
166+ , Data, Typeable, Align
167+#if MIN_VERSION_these(0,8,0)
168+ , Semialign
169+#endif
170+#ifdef MIN_VERSION_semialign
171+ , Unalign
172+#if MIN_VERSION_semialign(1,1,0)
173+ , Zip
174+#endif
175+#endif
176+ )
177+
178+#if MIN_VERSION_containers(0,5,9)
179+deriving instance (Ord k) => Eq1 (MonoidalMap k)
180+deriving instance (Ord k) => Ord1 (MonoidalMap k)
181+deriving instance (Show k) => Show1 (MonoidalMap k)
182+#endif
183+
184+type instance Index (MonoidalMap k a) = k
185+type instance IxValue (MonoidalMap k a) = a
186+instance Ord k => Ixed (MonoidalMap k a) where
187+ ix k f (MonoidalMap m) = case M.lookup k m of
188+ Just v -> f v <&> \v' -> MonoidalMap (M.insert k v' m)
189+ Nothing -> pure (MonoidalMap m)
190+ {-# INLINE ix #-}
191+
192+instance Ord k => At (MonoidalMap k a) where
193+ at k f (MonoidalMap m) = f mv <&> \r -> case r of
194+ Nothing -> maybe (MonoidalMap m) (const (MonoidalMap $ M.delete k m)) mv
195+ Just v' -> MonoidalMap $ M.insert k v' m
196+ where mv = M.lookup k m
197+ {-# INLINE at #-}
198+
199+instance Each (MonoidalMap k a) (MonoidalMap k b) a b
200+
201+instance FunctorWithIndex k (MonoidalMap k)
202+instance FoldableWithIndex k (MonoidalMap k)
203+instance TraversableWithIndex k (MonoidalMap k) where
204+ itraverse f (MonoidalMap m) = fmap MonoidalMap $ itraverse f m
205+ {-# INLINE itraverse #-}
206+
207+instance Ord k => TraverseMin k (MonoidalMap k) where
208+ traverseMin f (MonoidalMap m) = fmap MonoidalMap $ traverseMin f m
209+ {-# INLINE traverseMin #-}
210+instance Ord k => TraverseMax k (MonoidalMap k) where
211+ traverseMax f (MonoidalMap m) = fmap MonoidalMap $ traverseMax f m
212+ {-# INLINE traverseMax #-}
213+
214+instance AsEmpty (MonoidalMap k a) where
215+ _Empty = nearly (MonoidalMap M.empty) (M.null . unpack)
216+ {-# INLINE _Empty #-}
217+
218+instance Wrapped (MonoidalMap k a) where
219+ type Unwrapped (MonoidalMap k a) = M.Map k a
220+ _Wrapped' = iso unpack pack
221+ {-# INLINE _Wrapped' #-}
222+
223+instance (Ord k, Semigroup a) => Semigroup (MonoidalMap k a) where
224+ MonoidalMap a <> MonoidalMap b = MonoidalMap $ M.unionWith (<>) a b
225+ {-# INLINE (<>) #-}
226+
227+instance (Ord k, Semigroup a) => Monoid (MonoidalMap k a) where
228+ mempty = MonoidalMap mempty
229+ {-# INLINE mempty #-}
230+#if !(MIN_VERSION_base(4,11,0))
231+ mappend (MonoidalMap a) (MonoidalMap b) = MonoidalMap $ M.unionWith (<>) a b
232+ {-# INLINE mappend #-}
233+#endif
234+
235+instance Newtype (MonoidalMap k a) (M.Map k a) where
236+ pack = MonoidalMap
237+ {-# INLINE pack #-}
238+ unpack (MonoidalMap a) = a
239+ {-# INLINE unpack #-}
240+
241+#if MIN_VERSION_base(4,7,0)
242+instance (Ord k, Semigroup a) => IsList.IsList (MonoidalMap k a) where
243+ type Item (MonoidalMap k a) = (k, a)
244+ fromList = MonoidalMap . M.fromListWith (<>)
245+ {-# INLINE fromList #-}
246+ toList = M.toList . unpack
247+ {-# INLINE toList #-}
248+#endif
249+
250+-- | /O(1)/. A map with a single element.
251+singleton :: k -> a -> MonoidalMap k a
252+singleton k a = MonoidalMap $ M.singleton k a
253+{-# INLINE singleton #-}
254+
255+-- | /O(1)/. The number of elements in the map.
256+size :: MonoidalMap k a -> Int
257+size = M.size . unpack
258+{-# INLINE size #-}
259+
260+-- | /O(log n)/. Is the key a member of the map? See also 'notMember'.
261+member :: Ord k => k -> MonoidalMap k a -> Bool
262+member k = M.member k . unpack
263+{-# INLINE member #-}
264+
265+-- | /O(log n)/. Is the key not a member of the map? See also 'member'.
266+notMember :: Ord k => k -> MonoidalMap k a -> Bool
267+notMember k = not . M.member k . unpack
268+{-# INLINE notMember #-}
269+
270+-- | /O(log n)/. The expression @('findWithDefault' def k map)@ returns
271+-- the value at key @k@ or returns default value @def@
272+-- when the key is not in the map.
273+findWithDefault :: Ord k => a -> k -> MonoidalMap k a -> a
274+findWithDefault def k = M.findWithDefault def k . unpack
275+{-# INLINE findWithDefault #-}
276+
277+-- | /O(log n)/. Delete a key and its value from the map. When the key is not
278+-- a member of the map, the original map is returned.
279+delete :: Ord k => k -> MonoidalMap k a -> MonoidalMap k a
280+delete k = _Wrapping' MonoidalMap %~ M.delete k
281+{-# INLINE delete #-}
282+
283+-- | /O(n)/. Return all elements of the map and their keys
284+assocs :: MonoidalMap k a -> [(k,a)]
285+assocs = M.assocs . unpack
286+{-# INLINE assocs #-}
287+
288+-- | /O(n)/. Return all elements of the map in the ascending order of their
289+-- keys. Subject to list fusion.
290+elems :: MonoidalMap k a -> [a]
291+elems = M.elems . unpack
292+{-# INLINE elems #-}
293+
294+-- | /O(n)/. Return all keys of the map in ascending order. Subject to list
295+-- fusion.
296+keys :: MonoidalMap k a -> [k]
297+keys = M.keys . unpack
298+{-# INLINE keys #-}
299+
300+
301+(!) :: forall k a. Ord k => MonoidalMap k a -> k -> a
302+(!) = coerce ((M.!) :: M.Map k a -> k -> a)
303+infixl 9 !
304+
305+(\\) :: forall k a b. Ord k => MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k a
306+(\\) = coerce ((M.\\) :: M.Map k a -> M.Map k b -> M.Map k a)
307+infixl 9 \\ --
308+
309+null :: forall k a. MonoidalMap k a -> Bool
310+null = coerce (M.null :: M.Map k a -> Bool)
311+{-# INLINE null #-}
312+
313+lookup :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe a
314+lookup = coerce (M.lookup :: k -> M.Map k a -> Maybe a)
315+{-# INLINE lookup #-}
316+
317+lookupLT :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe (k, a)
318+lookupLT = coerce (M.lookupLT :: k -> M.Map k a -> Maybe (k,a))
319+{-# INLINE lookupLT #-}
320+
321+lookupGT :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe (k, a)
322+lookupGT = coerce (M.lookupGT :: k -> M.Map k a -> Maybe (k,a))
323+{-# INLINE lookupGT #-}
324+
325+lookupLE :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe (k, a)
326+lookupLE = coerce (M.lookupLE :: k -> M.Map k a -> Maybe (k,a))
327+{-# INLINE lookupLE #-}
328+
329+lookupGE :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe (k, a)
330+lookupGE = coerce (M.lookupGE :: k -> M.Map k a -> Maybe (k,a))
331+{-# INLINE lookupGE #-}
332+
333+empty :: forall k a. MonoidalMap k a
334+empty = coerce (M.empty :: M.Map k a)
335+{-# INLINE empty #-}
336+
337+insert :: forall k a. Ord k => k -> a -> MonoidalMap k a -> MonoidalMap k a
338+insert = coerce (M.insert :: k -> a -> M.Map k a -> M.Map k a)
339+{-# INLINE insert #-}
340+
341+insertWith :: forall k a. Ord k => (a -> a -> a) -> k -> a -> MonoidalMap k a -> MonoidalMap k a
342+insertWith = coerce (M.insertWith :: (a -> a -> a) -> k -> a -> M.Map k a -> M.Map k a)
343+{-# INLINE insertWith #-}
344+
345+insertWithKey :: forall k a. Ord k => (k -> a -> a -> a) -> k -> a -> MonoidalMap k a -> MonoidalMap k a
346+insertWithKey = coerce (M.insertWithKey :: (k -> a -> a -> a) -> k -> a -> M.Map k a -> M.Map k a)
347+{-# INLINE insertWithKey #-}
348+
349+insertLookupWithKey :: forall k a. Ord k => (k -> a -> a -> a) -> k -> a -> MonoidalMap k a -> (Maybe a, MonoidalMap k a)
350+insertLookupWithKey = coerce (M.insertLookupWithKey :: (k -> a -> a -> a) -> k -> a -> M.Map k a -> (Maybe a, M.Map k a))
351+{-# INLINE insertLookupWithKey #-}
352+
353+adjust :: forall k a. Ord k => (a -> a) -> k -> MonoidalMap k a -> MonoidalMap k a
354+adjust = coerce (M.adjust :: (a -> a) -> k -> M.Map k a -> M.Map k a)
355+{-# INLINE adjust #-}
356+
357+adjustWithKey :: forall k a. Ord k => (k -> a -> a) -> k -> MonoidalMap k a -> MonoidalMap k a
358+adjustWithKey = coerce (M.adjustWithKey :: (k -> a -> a) -> k -> M.Map k a -> M.Map k a)
359+{-# INLINE adjustWithKey #-}
360+
361+update :: forall k a. Ord k => (a -> Maybe a) -> k -> MonoidalMap k a -> MonoidalMap k a
362+update = coerce (M.update :: (a -> Maybe a) -> k -> M.Map k a -> M.Map k a)
363+{-# INLINE update #-}
364+
365+updateWithKey :: forall k a. Ord k => (k -> a -> Maybe a) -> k -> MonoidalMap k a -> MonoidalMap k a
366+updateWithKey = coerce (M.updateWithKey :: (k -> a -> Maybe a) -> k -> M.Map k a -> M.Map k a)
367+{-# INLINE updateWithKey #-}
368+
369+updateLookupWithKey :: forall k a. Ord k => (k -> a -> Maybe a) -> k -> MonoidalMap k a -> (Maybe a, MonoidalMap k a)
370+updateLookupWithKey = coerce (M.updateLookupWithKey :: (k -> a -> Maybe a) -> k -> M.Map k a -> (Maybe a, M.Map k a))
371+{-# INLINE updateLookupWithKey #-}
372+
373+alter :: forall k a. Ord k => (Maybe a -> Maybe a) -> k -> MonoidalMap k a -> MonoidalMap k a
374+alter = coerce (M.alter :: (Maybe a -> Maybe a) -> k -> M.Map k a -> M.Map k a)
375+{-# INLINE alter #-}
376+
377+unionWith :: forall k a. Ord k => (a -> a -> a) -> MonoidalMap k a -> MonoidalMap k a -> MonoidalMap k a
378+unionWith = coerce (M.unionWith :: (a -> a -> a) -> M.Map k a -> M.Map k a -> M.Map k a)
379+{-# INLINE unionWith #-}
380+
381+unionWithKey :: forall k a. Ord k => (k -> a -> a -> a) -> MonoidalMap k a -> MonoidalMap k a -> MonoidalMap k a
382+unionWithKey = coerce (M.unionWithKey :: (k -> a -> a -> a) -> M.Map k a -> M.Map k a -> M.Map k a)
383+{-# INLINE unionWithKey #-}
384+
385+unionsWith :: forall k a. Ord k => (a -> a -> a) -> [MonoidalMap k a] -> MonoidalMap k a
386+unionsWith = coerce (M.unionsWith :: (a -> a -> a) -> [M.Map k a] -> M.Map k a)
387+{-# INLINE unionsWith #-}
388+
389+difference :: forall k a b. Ord k => MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k a
390+difference = (\\)
391+{-# INLINE difference #-}
392+
393+differenceWith :: forall k a b. Ord k => (a -> b -> Maybe a) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k a
394+differenceWith = coerce (M.differenceWith :: (a -> b -> Maybe a) -> M.Map k a -> M.Map k b -> M.Map k a)
395+{-# INLINE differenceWith #-}
396+
397+differenceWithKey :: forall k a b. Ord k => (k -> a -> b -> Maybe a) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k a
398+differenceWithKey = coerce (M.differenceWithKey :: (k -> a -> b -> Maybe a) -> M.Map k a -> M.Map k b -> M.Map k a)
399+{-# INLINE differenceWithKey #-}
400+
401+intersectionWith :: forall k a b c. Ord k => (a -> b -> c) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k c
402+intersectionWith = coerce (M.intersectionWith :: (a -> b -> c) -> M.Map k a -> M.Map k b -> M.Map k c)
403+{-# INLINE intersectionWith #-}
404+
405+intersectionWithKey :: forall k a b c. Ord k => (k -> a -> b -> c) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k c
406+intersectionWithKey = coerce (M.intersectionWithKey :: (k -> a -> b -> c) -> M.Map k a -> M.Map k b -> M.Map k c)
407+{-# INLINE intersectionWithKey #-}
408+
409+mergeWithKey :: forall k a b c. Ord k => (k -> a -> b -> Maybe c) -> (MonoidalMap k a -> MonoidalMap k c) -> (MonoidalMap k b -> MonoidalMap k c) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k c
410+mergeWithKey = coerce (M.mergeWithKey :: (k -> a -> b -> Maybe c) -> (M.Map k a -> M.Map k c) -> (M.Map k b -> M.Map k c) -> M.Map k a -> M.Map k b -> M.Map k c)
411+{-# INLINE mergeWithKey #-}
412+
413+map :: (a -> b) -> MonoidalMap k a -> MonoidalMap k b
414+map = fmap
415+{-# INLINE map #-}
416+
417+mapWithKey :: forall k a b. (k -> a -> b) -> MonoidalMap k a -> MonoidalMap k b
418+mapWithKey = coerce (M.mapWithKey :: (k -> a -> b) -> M.Map k a -> M.Map k b)
419+{-# INLINE mapWithKey #-}
420+
421+traverseWithKey :: Applicative t => (k -> a -> t b) -> MonoidalMap k a -> t (MonoidalMap k b)
422+traverseWithKey = itraverse
423+{-# INLINE traverseWithKey #-}
424+
425+mapAccum :: forall k a b c. (a -> b -> (a, c)) -> a -> MonoidalMap k b -> (a, MonoidalMap k c)
426+mapAccum = coerce (M.mapAccum :: (a -> b -> (a, c)) -> a -> M.Map k b -> (a, M.Map k c))
427+{-# INLINE mapAccum #-}
428+
429+mapAccumWithKey :: forall k a b c. (a -> k -> b -> (a, c)) -> a -> MonoidalMap k b -> (a, MonoidalMap k c)
430+mapAccumWithKey = coerce (M.mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> M.Map k b -> (a, M.Map k c))
431+{-# INLINE mapAccumWithKey #-}
432+
433+mapAccumRWithKey :: forall k a b c. (a -> k -> b -> (a, c)) -> a -> MonoidalMap k b -> (a, MonoidalMap k c)
434+mapAccumRWithKey = coerce (M.mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> M.Map k b -> (a, M.Map k c))
435+{-# INLINE mapAccumRWithKey #-}
436+
437+mapKeys :: forall k1 k2 a. (Ord k2, Semigroup a) => (k1 -> k2) -> MonoidalMap k1 a -> MonoidalMap k2 a
438+mapKeys = mapKeysWith (<>)
439+{-# INLINE mapKeys #-}
440+
441+mapKeysWith :: forall k1 k2 a. Ord k2 => (a -> a -> a) -> (k1 -> k2) -> MonoidalMap k1 a -> MonoidalMap k2 a
442+mapKeysWith = coerce (M.mapKeysWith :: (a -> a -> a) -> (k1 -> k2) -> M.Map k1 a -> M.Map k2 a)
443+{-# INLINE mapKeysWith #-}
444+
445+-- | /O(n)/.
446+-- @'mapKeysMonotonic' f s == 'mapKeys' f s@, but works only when @f@
447+-- is strictly increasing (both monotonic and injective).
448+-- That is, for any values @x@ and @y@, if @x@ < @y@ then @f x@ < @f y@
449+-- and @f@ is injective (i.e. it never maps two input keys to the same output key).
450+-- /The precondition is not checked./
451+-- Semi-formally, we have:
452+--
453+-- > and [x < y ==> f x < f y | x <- ls, y <- ls]
454+-- > ==> mapKeysMonotonic f s == mapKeys f s
455+-- > where ls = keys s
456+--
457+-- This means that @f@ maps distinct original keys to distinct resulting keys.
458+-- This function has better performance than 'mapKeys'.
459+--
460+-- > mapKeysMonotonic (\ k -> k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")]
461+-- > valid (mapKeysMonotonic (\ k -> k * 2) (fromList [(5,"a"), (3,"b")])) == True
462+-- > valid (mapKeysMonotonic (\ _ -> 1) (fromList [(5,"a"), (3,"b")])) == False
463+mapKeysMonotonic :: forall k1 k2 a. (k1 -> k2) -> MonoidalMap k1 a -> MonoidalMap k2 a
464+mapKeysMonotonic = coerce (M.mapKeysMonotonic :: (k1 -> k2) -> M.Map k1 a -> M.Map k2 a)
465+{-# INLINE mapKeysMonotonic #-}
466+
467+foldr :: forall k a b. (a -> b -> b) -> b -> MonoidalMap k a -> b
468+foldr = coerce (M.foldr :: (a -> b -> b) -> b -> M.Map k a -> b)
469+{-# INLINE foldr #-}
470+
471+foldl :: forall k a b. (a -> b -> a) -> a -> MonoidalMap k b -> a
472+foldl = coerce (M.foldl :: (a -> b -> a) -> a -> M.Map k b -> a)
473+{-# INLINE foldl #-}
474+
475+foldrWithKey :: forall k a b. (k -> a -> b -> b) -> b -> MonoidalMap k a -> b
476+foldrWithKey = coerce (M.foldrWithKey :: (k -> a -> b -> b) -> b -> M.Map k a -> b)
477+{-# INLINE foldrWithKey #-}
478+
479+foldlWithKey :: forall k a b. (a -> k -> b -> a) -> a -> MonoidalMap k b -> a
480+foldlWithKey = coerce (M.foldlWithKey :: (a -> k -> b -> a) -> a -> M.Map k b -> a)
481+{-# INLINE foldlWithKey #-}
482+
483+foldMapWithKey :: forall k a m. Monoid m => (k -> a -> m) -> MonoidalMap k a -> m
484+foldMapWithKey = coerce (M.foldMapWithKey :: Monoid m => (k -> a -> m) -> M.Map k a -> m)
485+{-# INLINE foldMapWithKey #-}
486+
487+foldr' :: forall k a b. (a -> b -> b) -> b -> MonoidalMap k a -> b
488+foldr' = coerce (M.foldr' :: (a -> b -> b) -> b -> M.Map k a -> b)
489+{-# INLINE foldr' #-}
490+
491+foldl' :: forall k a b. (a -> b -> a) -> a -> MonoidalMap k b -> a
492+foldl' = coerce (M.foldl' :: (a -> b -> a) -> a -> M.Map k b -> a)
493+{-# INLINE foldl' #-}
494+
495+foldrWithKey' :: forall k a b. (k -> a -> b -> b) -> b -> MonoidalMap k a -> b
496+foldrWithKey' = coerce (M.foldrWithKey' :: (k -> a -> b -> b) -> b -> M.Map k a -> b)
497+{-# INLINE foldrWithKey' #-}
498+
499+foldlWithKey' :: forall k a b. (a -> k -> b -> a) -> a -> MonoidalMap k b -> a
500+foldlWithKey' = coerce (M.foldlWithKey' :: (a -> k -> b -> a) -> a -> M.Map k b -> a)
501+{-# INLINE foldlWithKey' #-}
502+
503+keysSet :: forall k a. MonoidalMap k a -> Set k
504+keysSet = coerce (M.keysSet :: M.Map k a -> Set k)
505+{-# INLINE keysSet #-}
506+
507+fromSet :: forall k a. (k -> a) -> Set k -> MonoidalMap k a
508+fromSet = coerce (M.fromSet :: (k -> a) -> Set k -> M.Map k a)
509+{-# INLINE fromSet #-}
510+
511+toList :: forall k a. MonoidalMap k a -> [(k, a)]
512+toList = coerce (M.toList :: M.Map k a -> [(k, a)])
513+{-# INLINE toList #-}
514+
515+fromList :: forall k a. (Ord k, Semigroup a) => [(k, a)] -> MonoidalMap k a
516+fromList = fromListWith (<>)
517+{-# INLINE fromList #-}
518+
519+fromListWith :: forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> MonoidalMap k a
520+fromListWith = coerce (M.fromListWith :: (a -> a -> a) -> [(k, a)] -> M.Map k a)
521+{-# INLINE fromListWith #-}
522+
523+fromListWithKey :: forall k a. Ord k => (k -> a -> a -> a) -> [(k, a)] -> MonoidalMap k a
524+fromListWithKey = coerce (M.fromListWithKey :: (k -> a -> a -> a) -> [(k, a)] -> M.Map k a)
525+{-# INLINE fromListWithKey #-}
526+
527+toAscList :: forall k a. MonoidalMap k a -> [(k, a)]
528+toAscList = coerce (M.toAscList :: M.Map k a -> [(k, a)])
529+{-# INLINE toAscList #-}
530+
531+toDescList :: forall k a. MonoidalMap k a -> [(k, a)]
532+toDescList = coerce (M.toDescList :: M.Map k a -> [(k, a)])
533+{-# INLINE toDescList #-}
534+
535+fromAscList :: forall k a. (Eq k, Semigroup a) => [(k, a)] -> MonoidalMap k a
536+fromAscList = fromAscListWith (<>)
537+{-# INLINE fromAscList #-}
538+
539+fromAscListWith :: forall k a. Eq k => (a -> a -> a) -> [(k, a)] -> MonoidalMap k a
540+fromAscListWith = coerce (M.fromAscListWith :: (a -> a -> a) -> [(k, a)] -> M.Map k a)
541+{-# INLINE fromAscListWith #-}
542+
543+fromAscListWithKey :: forall k a. Eq k => (k -> a -> a -> a) -> [(k, a)] -> MonoidalMap k a
544+fromAscListWithKey = coerce (M.fromAscListWithKey :: (k -> a -> a -> a) -> [(k, a)] -> M.Map k a)
545+{-# INLINE fromAscListWithKey #-}
546+
547+fromDistinctAscList :: forall k a. [(k, a)] -> MonoidalMap k a
548+fromDistinctAscList = coerce (M.fromDistinctAscList :: [(k, a)] -> M.Map k a)
549+{-# INLINE fromDistinctAscList #-}
550+
551+fromDistinctList :: forall k a. Ord k => [(k, a)] -> MonoidalMap k a
552+fromDistinctList = coerce (M.fromList :: [(k, a)] -> M.Map k a)
553+{-# INLINE fromDistinctList #-}
554+
555+filter :: forall k a. (a -> Bool) -> MonoidalMap k a -> MonoidalMap k a
556+filter = coerce (M.filter :: (a -> Bool) -> M.Map k a -> M.Map k a)
557+{-# INLINE filter #-}
558+
559+filterWithKey :: forall k a. (k -> a -> Bool) -> MonoidalMap k a -> MonoidalMap k a
560+filterWithKey = coerce (M.filterWithKey :: (k -> a -> Bool) -> M.Map k a -> M.Map k a)
561+{-# INLINE filterWithKey #-}
562+
563+partition :: forall k a. (a -> Bool) -> MonoidalMap k a -> (MonoidalMap k a, MonoidalMap k a)
564+partition = coerce (M.partition :: (a -> Bool) -> M.Map k a -> (M.Map k a, M.Map k a))
565+{-# INLINE partition #-}
566+
567+partitionWithKey :: forall k a. (k -> a -> Bool) -> MonoidalMap k a -> (MonoidalMap k a, MonoidalMap k a)
568+partitionWithKey = coerce (M.partitionWithKey :: (k -> a -> Bool) -> M.Map k a -> (M.Map k a, M.Map k a))
569+{-# INLINE partitionWithKey #-}
570+
571+mapMaybe :: forall k a b. (a -> Maybe b) -> MonoidalMap k a -> MonoidalMap k b
572+mapMaybe = coerce (M.mapMaybe :: (a -> Maybe b) -> M.Map k a -> M.Map k b)
573+{-# INLINE mapMaybe #-}
574+
575+mapMaybeWithKey :: forall k a b. (k -> a -> Maybe b) -> MonoidalMap k a -> MonoidalMap k b
576+mapMaybeWithKey = coerce (M.mapMaybeWithKey :: (k -> a -> Maybe b) -> M.Map k a -> M.Map k b)
577+{-# INLINE mapMaybeWithKey #-}
578+
579+mapEither :: forall k a b c. (a -> Either b c) -> MonoidalMap k a -> (MonoidalMap k b, MonoidalMap k c)
580+mapEither = coerce (M.mapEither :: (a -> Either b c) -> M.Map k a -> (M.Map k b, M.Map k c))
581+{-# INLINE mapEither #-}
582+
583+mapEitherWithKey :: forall k a b c. (k -> a -> Either b c) -> MonoidalMap k a -> (MonoidalMap k b, MonoidalMap k c)
584+mapEitherWithKey = coerce (M.mapEitherWithKey :: (k -> a -> Either b c) -> M.Map k a -> (M.Map k b, M.Map k c))
585+{-# INLINE mapEitherWithKey #-}
586+
587+split :: forall k a. Ord k => k -> MonoidalMap k a -> (MonoidalMap k a, MonoidalMap k a)
588+split = coerce (M.split :: k -> M.Map k a -> (M.Map k a, M.Map k a))
589+{-# INLINE split #-}
590+
591+splitLookup :: forall k a. Ord k => k -> MonoidalMap k a -> (MonoidalMap k a, Maybe a, MonoidalMap k a)
592+splitLookup = coerce (M.splitLookup :: k -> M.Map k a -> (M.Map k a, Maybe a, M.Map k a))
593+{-# INLINE splitLookup #-}
594+
595+splitRoot :: forall k a. MonoidalMap k a -> [MonoidalMap k a]
596+splitRoot = coerce (M.splitRoot :: M.Map k a -> [M.Map k a])
597+{-# INLINE splitRoot #-}
598+
599+isSubmapOf :: forall k a. (Ord k, Eq a) => MonoidalMap k a -> MonoidalMap k a -> Bool
600+isSubmapOf = coerce (M.isSubmapOf :: M.Map k a -> M.Map k a -> Bool)
601+{-# INLINE isSubmapOf #-}
602+
603+isSubmapOfBy :: forall k a b. Ord k => (a -> b -> Bool) -> MonoidalMap k a -> MonoidalMap k b -> Bool
604+isSubmapOfBy = coerce (M.isSubmapOfBy :: (a -> b -> Bool) -> M.Map k a -> M.Map k b -> Bool)
605+{-# INLINE isSubmapOfBy #-}
606+
607+isProperSubmapOf :: forall k a. (Ord k, Eq a) => MonoidalMap k a -> MonoidalMap k a -> Bool
608+isProperSubmapOf = coerce (M.isProperSubmapOf :: M.Map k a -> M.Map k a -> Bool)
609+{-# INLINE isProperSubmapOf #-}
610+
611+isProperSubmapOfBy :: forall k a b. Ord k => (a -> b -> Bool) -> MonoidalMap k a -> MonoidalMap k b -> Bool
612+isProperSubmapOfBy = coerce (M.isProperSubmapOfBy :: (a -> b -> Bool) -> M.Map k a -> M.Map k b -> Bool)
613+{-# INLINE isProperSubmapOfBy #-}
614+
615+lookupIndex :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe Int
616+lookupIndex = coerce (M.lookupIndex :: k -> M.Map k a -> Maybe Int)
617+{-# INLINE lookupIndex #-}
618+
619+findIndex :: forall k a. Ord k => k -> MonoidalMap k a -> Int
620+findIndex = coerce (M.findIndex :: k -> M.Map k a -> Int)
621+{-# INLINE findIndex #-}
622+
623+elemAt :: forall k a. Int -> MonoidalMap k a -> (k, a)
624+elemAt = coerce (M.elemAt :: Int -> M.Map k a -> (k, a))
625+{-# INLINE elemAt #-}
626+
627+updateAt :: forall k a. (k -> a -> Maybe a) -> Int -> MonoidalMap k a -> MonoidalMap k a
628+updateAt = coerce (M.updateAt :: (k -> a -> Maybe a) -> Int -> M.Map k a -> M.Map k a)
629+{-# INLINE updateAt #-}
630+
631+deleteAt :: forall k a. Int -> MonoidalMap k a -> MonoidalMap k a
632+deleteAt = coerce (M.deleteAt :: Int -> M.Map k a -> M.Map k a)
633+{-# INLINE deleteAt #-}
634+
635+findMin :: forall k a. MonoidalMap k a -> (k, a)
636+findMin = coerce (M.findMin :: M.Map k a -> (k, a))
637+{-# INLINE findMin #-}
638+
639+findMax :: forall k a. MonoidalMap k a -> (k, a)
640+findMax = coerce (M.findMax :: M.Map k a -> (k, a))
641+{-# INLINE findMax #-}
642+
643+deleteMin :: forall k a. MonoidalMap k a -> MonoidalMap k a
644+deleteMin = coerce (M.deleteMin :: M.Map k a -> M.Map k a)
645+{-# INLINE deleteMin #-}
646+
647+deleteMax :: forall k a. MonoidalMap k a -> MonoidalMap k a
648+deleteMax = coerce (M.deleteMax :: M.Map k a -> M.Map k a)
649+{-# INLINE deleteMax #-}
650+
651+deleteFindMin :: forall k a. MonoidalMap k a -> ((k, a), MonoidalMap k a)
652+deleteFindMin = coerce (M.deleteFindMin :: M.Map k a -> ((k, a), M.Map k a))
653+{-# INLINE deleteFindMin #-}
654+
655+deleteFindMax :: forall k a. MonoidalMap k a -> ((k, a), MonoidalMap k a)
656+deleteFindMax = coerce (M.deleteFindMax :: M.Map k a -> ((k, a), M.Map k a))
657+{-# INLINE deleteFindMax #-}
658+
659+updateMin :: forall k a. (a -> Maybe a) -> MonoidalMap k a -> MonoidalMap k a
660+updateMin = coerce (M.updateMin :: (a -> Maybe a) -> M.Map k a -> M.Map k a)
661+{-# INLINE updateMin #-}
662+
663+updateMax :: forall k a. (a -> Maybe a) -> MonoidalMap k a -> MonoidalMap k a
664+updateMax = coerce (M.updateMax :: (a -> Maybe a) -> M.Map k a -> M.Map k a)
665+{-# INLINE updateMax #-}
666+
667+updateMinWithKey :: forall k a. (k -> a -> Maybe a) -> MonoidalMap k a -> MonoidalMap k a
668+updateMinWithKey = coerce (M.updateMinWithKey :: (k -> a -> Maybe a) -> M.Map k a -> M.Map k a)
669+{-# INLINE updateMinWithKey #-}
670+
671+updateMaxWithKey :: forall k a. (k -> a -> Maybe a) -> MonoidalMap k a -> MonoidalMap k a
672+updateMaxWithKey = coerce (M.updateMaxWithKey :: (k -> a -> Maybe a) -> M.Map k a -> M.Map k a)
673+{-# INLINE updateMaxWithKey #-}
674+
675+minView :: forall k a. MonoidalMap k a -> Maybe (a, MonoidalMap k a)
676+minView = coerce (M.minView :: M.Map k a -> Maybe (a, M.Map k a))
677+{-# INLINE minView #-}
678+
679+maxView :: forall k a. MonoidalMap k a -> Maybe (a, MonoidalMap k a)
680+maxView = coerce (M.maxView :: M.Map k a -> Maybe (a, M.Map k a))
681+{-# INLINE maxView #-}
682+
683+minViewWithKey :: forall k a. MonoidalMap k a -> Maybe ((k, a), MonoidalMap k a)
684+minViewWithKey = coerce (M.minViewWithKey :: M.Map k a -> Maybe ((k, a), M.Map k a))
685+{-# INLINE minViewWithKey #-}
686+
687+maxViewWithKey :: forall k a. MonoidalMap k a -> Maybe ((k, a), MonoidalMap k a)
688+maxViewWithKey = coerce (M.maxViewWithKey :: M.Map k a -> Maybe ((k, a), M.Map k a))
689+{-# INLINE maxViewWithKey #-}
690+
691+-- showTree :: forall k a. (Show k, Show a) => MonoidalMap k a -> String
692+-- showTree = coerce (M.showTree :: (Show k, Show a) => M.Map k a -> String)
693+-- {-# INLINE showTree #-}
694+
695+-- showTreeWith :: forall k a. (k -> a -> String) -> Bool -> Bool -> MonoidalMap k a -> String
696+-- showTreeWith = coerce (M.showTreeWith :: (k -> a -> String) -> Bool -> Bool -> M.Map k a -> String)
697+-- {-# INLINE showTreeWith #-}
698+
699+valid :: forall k a. Ord k => MonoidalMap k a -> Bool
700+valid = coerce (M.valid :: Ord k => M.Map k a -> Bool)
701+{-# INLINE valid #-}