├── .github └── workflows │ └── Cabal.yml ├── .gitignore ├── Changelog.md ├── LICENSE ├── README.md ├── Setup.hs ├── default.nix ├── monoidal-containers.cabal └── src └── Data ├── HashMap └── Monoidal.hs ├── IntMap ├── Monoidal.hs └── Monoidal │ └── Strict.hs └── Map ├── Monoidal.hs └── Monoidal └── Strict.hs /.github/workflows/Cabal.yml: -------------------------------------------------------------------------------- 1 | name: Cabal 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | build: 7 | strategy: 8 | fail-fast: false 9 | matrix: 10 | os: [ubuntu-latest] 11 | cabal: ['latest'] 12 | ghc: ['8.6', '8.8', '8.10', '9.0', '9.2', '9.4', '9.6', '9.8', '9.10', '9.12'] 13 | include: 14 | - ghc: '9.10' 15 | os: 'macos-latest' 16 | - ghc: '8.2' 17 | os: 'ubuntu-latest' 18 | cabal: 3.12 19 | - ghc: '8.4' 20 | os: 'ubuntu-latest' 21 | cabal: 3.12 22 | runs-on: ${{ matrix.os }} 23 | 24 | name: GHC ${{ matrix.ghc }} on ${{ matrix.os }} 25 | steps: 26 | - uses: actions/checkout@v4 27 | 28 | - name: Set up GHC ${{ matrix.ghc-version }} 29 | uses: haskell-actions/setup@v2 30 | id: setup 31 | with: 32 | ghc-version: ${{ matrix.ghc }} 33 | cabal-version: ${{ matrix.cabal }} 34 | 35 | - name: Configure the build 36 | run: | 37 | cabal configure --enable-tests --enable-benchmarks --enable-documentation 38 | cabal build all --dry-run 39 | # The last step generates dist-newstyle/cache/plan.json for the cache key. 40 | 41 | - name: Restore cached dependencies 42 | uses: actions/cache/restore@v4 43 | id: cache 44 | env: 45 | key: ${{ runner.os }}-ghc-${{ steps.setup.outputs.ghc-version }}-cabal-${{ steps.setup.outputs.cabal-version }} 46 | with: 47 | path: ${{ steps.setup.outputs.cabal-store }} 48 | key: ${{ env.key }}-plan-${{ hashFiles('**/plan.json') }} 49 | restore-keys: ${{ env.key }}- 50 | 51 | - name: Install dependencies 52 | # If we had an exact cache hit, the dependencies will be up to date. 53 | if: steps.cache.outputs.cache-hit != 'true' 54 | run: cabal build all --only-dependencies 55 | 56 | # Cache dependencies already here, so that we do not have to rebuild them should the subsequent steps fail. 57 | - name: Save cached dependencies 58 | uses: actions/cache/save@v4 59 | # If we had an exact cache hit, trying to save the cache would error because of key clash. 60 | if: steps.cache.outputs.cache-hit != 'true' 61 | with: 62 | path: ${{ steps.setup.outputs.cabal-store }} 63 | key: ${{ steps.cache.outputs.cache-primary-key }} 64 | 65 | - name: Build 66 | run: cabal build all 67 | 68 | - name: Run tests 69 | run: cabal test all 70 | 71 | - name: Check cabal file 72 | run: cabal check 73 | 74 | - name: Build documentation 75 | run: cabal haddock all 76 | if: matrix.ghc != '8.2' && matrix.ghc != '8.4' 77 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | dist-newstyle 3 | cabal.config 4 | cabal.project.local 5 | .ghc.environment.* 6 | -------------------------------------------------------------------------------- /Changelog.md: -------------------------------------------------------------------------------- 1 | # Monoidal containers 2 | 3 | # 0.6.6.0 4 | 5 | * [Support ghc 9.12](https://github.com/bgamari/monoidal-containers/pull/108) 6 | * [Allow hashable 1.5](https://github.com/bgamari/monoidal-containers/pull/107] 7 | 8 | # 0.6.5.0 9 | 10 | * [Support for GHC 9.10](https://github.com/bgamari/monoidal-containers/pull/105) 11 | 12 | # 0.6.4.0 13 | 14 | * [Bump upper bound of `these`](https://github.com/bgamari/monoidal-containers/pull/95) 15 | * [Bump upper bound of `semialign`](https://github.com/bgamari/monoidal-containers/pull/96) 16 | * [Build with GHC 9.6](https://github.com/bgamari/monoidal-containers/pull/97) 17 | 18 | # 0.6.3.0 19 | 20 | * [Bump upper bounds of aeson, base, and lens](https://github.com/bgamari/monoidal-containers/pull/92) 21 | 22 | # 0.6.2.0 23 | 24 | * [Remove cyclic definition of `!?`](https://github.com/bgamari/monoidal-containers/pull/88) 25 | * [Bump lens and hashable version bounds](https://github.com/bgamari/monoidal-containers/pull/87) 26 | 27 | # 0.6.1.0 28 | 29 | * Drop official support for GHC 7.8.4 30 | * Require containers 0.5.9 or greater. 31 | * Support lens-5, semialign-1.2, and aeson-2 32 | * MonoidalMap and MonoidalMap.Strict: Add several functions added to Data.Map since 0.5 up to 0.5.9: 33 | `take`, `drop`, `splitAt`, `lookupMin`, `lookupMax`, `restrictKeys`, 34 | `withoutKeys`, `fromDescList`, `fromDescListWith`, `fromDescListWithKey`, 35 | `fromDistinctDescList`, `takeWhileAntitone`, `dropWhileAntitone`, 36 | `spanAntitone`, and `traverseMaybeWithKey` 37 | * Add instances of `Filterable` and `Witherable` from the `witherable` package 38 | * Add `Control.Lens.Wrapped.Rewrapped` instances. 39 | 40 | # 0.6.0.1 41 | 42 | * Support semialign-1.1 43 | 44 | # 0.6 45 | 46 | * 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). 47 | * Fix the argument order of Data.HashMap.Monoidal.insert 48 | * Remove Data.HashMap.Monoidal.insertOrReplace as it is now identical to Data.HashMap.Monoidal.insert 49 | * Added Data.HashMap.Monoidal.insertWith and Data.HashMap.Monoidal.fromListWith 50 | 51 | # 0.5.0.1 52 | 53 | * Add a flag, `split-these`, to select whether to use the new "these"/"semialign" packages or the older combined "these" package. 54 | * Add default.nix to make it easier to hack on this package in nix 55 | 56 | # 0.5.0.0 57 | 58 | * Added Data.IntMap.Monoidal and Data.IntMap.Monoidal.Strict, corresponding to Data.IntMap and Data.IntMap.Strict 59 | * 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`. 60 | * Add Align instances and, for sufficiently recent versions of `these`, Semialign instances 61 | * Support `these` 0.8.0 62 | 63 | # 0.4.0.0 64 | 65 | General changes: 66 | 67 | * Added support for `unordered-containers < 0.2.8` 68 | * Added many more functions in `Data.Map.[Strict.]Monoid` 69 | 70 | Weakened `Monoid` constraints to `Semigroup` whenever possible as enabled by the 71 | [Semigroup-Monoid 72 | proposal](https://prime.haskell.org/wiki/Libraries/Proposals/SemigroupMonoid). 73 | This includes, 74 | 75 | * the `Monoid` instance of `MonoidalHashMap` and `MonoidalMap` 76 | * the `IsList` instance of `MonoidalHashMap` and `MonoidalMap` 77 | * the `modifyDef` and `mapKeys` functions of `MonoidalHashMap` 78 | 79 | 80 | # 0.3 and earlier 81 | 82 | Pre-history 83 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, Ben Gamari 2 | 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above 12 | copyright notice, this list of conditions and the following 13 | disclaimer in the documentation and/or other materials provided 14 | with the distribution. 15 | 16 | * Neither the name of Ben Gamari nor the names of other 17 | contributors may be used to endorse or promote products derived 18 | from this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # monoidal-containers 2 | [![Haskell](https://img.shields.io/badge/language-Haskell-orange.svg)](https://haskell.org) [![Hackage](https://img.shields.io/hackage/v/monoidal-containers.svg)](https://hackage.haskell.org/package/monoidal-containers) [![Hackage CI](https://matrix.hackage.haskell.org/api/v2/packages/monoidal-containers/badge)](https://matrix.hackage.haskell.org/#/package/monoidal-containers) [![Github CI](https://github.com/bgamari/monoidal-containers/workflows/Cabal/badge.svg)](https://github.com/bgamari/monoidal-containers/actions) [![BSD3 License](https://img.shields.io/badge/license-BSD3-blue.svg)](https://github.com/bgamari/monoidal-containers/blob/master/LICENSE) 3 | 4 | 5 | 6 | Often when working with the containers offered by the `containers` and 7 | `unordered-containers` packages one would prefer the monoidal structure 8 | of the values to be used when resolving conflicts between keys when merging 9 | structures. Sadly, these are not the semantics offered by the provided 10 | instances. This package provides `newtypes` with an appropriate set of 11 | instances and utility functions to make them usable. 12 | 13 | ## Important Note 14 | 15 | 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`. 16 | -------------------------------------------------------------------------------- /Setup.hs: -------------------------------------------------------------------------------- 1 | import Distribution.Simple 2 | main = defaultMain 3 | -------------------------------------------------------------------------------- /default.nix: -------------------------------------------------------------------------------- 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 | } 17 | -------------------------------------------------------------------------------- /monoidal-containers.cabal: -------------------------------------------------------------------------------- 1 | name: monoidal-containers 2 | version: 0.6.6.0 3 | synopsis: 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 | @ 17 | 18 | homepage: http://github.com/bgamari/monoidal-containers 19 | license: BSD3 20 | license-file: LICENSE 21 | author: Ben Gamari 22 | maintainer: ben@smart-cactus.org 23 | copyright: (c) 2014 Ben Gamari 24 | category: Data 25 | build-type: Simple 26 | cabal-version: >=1.10 27 | extra-source-files: Changelog.md 28 | tested-with: 29 | GHC ==8.2 || ==8.4 || ==8.6 || ==8.8 || ==8.10 || ==9.0 || ==9.2 || ==9.4 30 | || ==9.6 || ==9.10 || ==9.12 31 | 32 | source-repository head 33 | type: git 34 | location: https://github.com/bgamari/monoidal-containers 35 | 36 | flag split-these 37 | description: Use split these/semialign packages 38 | manual: False 39 | default: True 40 | 41 | library 42 | exposed-modules: 43 | Data.HashMap.Monoidal 44 | Data.IntMap.Monoidal 45 | Data.IntMap.Monoidal.Strict 46 | Data.Map.Monoidal 47 | Data.Map.Monoidal.Strict 48 | 49 | other-extensions: 50 | CPP 51 | DeriveDataTypeable 52 | DeriveTraversable 53 | GeneralizedNewtypeDeriving 54 | MultiParamTypeClasses 55 | 56 | build-depends: 57 | aeson >=1.0 && <2.3 58 | , base >=4.7 && <4.22 59 | , containers >=0.5.9 && <0.8 60 | , deepseq >=1.3 && <1.6 61 | , hashable >=1.2 && <1.6 62 | , lens >=4.4 && <5.4 63 | , newtype >=0.2 && <0.3 64 | , unordered-containers >=0.2 && <0.3 65 | , witherable >=0.4 && <0.6 66 | 67 | if flag(split-these) 68 | build-depends: 69 | semialign >=1 && <1.4 70 | , these >=1 && <1.3 71 | 72 | else 73 | build-depends: these >=0.7 && <0.9 74 | 75 | hs-source-dirs: src 76 | default-language: Haskell2010 77 | -------------------------------------------------------------------------------- /src/Data/HashMap/Monoidal.hs: -------------------------------------------------------------------------------- 1 | {-# LANGUAGE CPP #-} 2 | {-# LANGUAGE MultiParamTypeClasses #-} 3 | {-# LANGUAGE TypeFamilies #-} 4 | {-# LANGUAGE FlexibleInstances #-} 5 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} 6 | {-# LANGUAGE DeriveTraversable #-} 7 | {-# LANGUAGE DeriveDataTypeable #-} 8 | 9 | -- | This module provides a 'Data.HashMap' variant which uses the value's 10 | -- 'Monoid' instance to accumulate conflicting entries when merging 11 | -- 'Map's. 12 | -- 13 | -- While some functions mirroring those of 'Data.HashMap' are provided 14 | -- here for convenience, more specialized needs will likely want to use 15 | -- either the 'Newtype' or 'Wrapped' instances to manipulate the 16 | -- underlying 'Map'. 17 | 18 | module Data.HashMap.Monoidal 19 | ( MonoidalHashMap(..) 20 | -- * Often-needed functions 21 | , toList 22 | , fromList 23 | , fromListWith 24 | , singleton 25 | , size 26 | , member 27 | , notMember 28 | , lookup 29 | , lookupM 30 | , elems 31 | , keys 32 | , delete 33 | , mapKeys 34 | , insert 35 | , insertWith 36 | , modify 37 | , modifyDef 38 | , map 39 | , filterWithKey 40 | ) where 41 | 42 | import Prelude hiding (lookup, map) 43 | import Data.Maybe (fromMaybe) 44 | import Data.Semigroup 45 | import Data.Foldable (Foldable) 46 | import Control.Applicative (pure) 47 | import Data.Data (Data) 48 | import Data.Typeable (Typeable) 49 | 50 | #if MIN_VERSION_base(4,7,0) 51 | import qualified GHC.Exts as Exts 52 | #endif 53 | 54 | #if MIN_VERSION_base(4,9,0) 55 | import Data.Functor.Classes (Eq1) 56 | #endif 57 | 58 | import Control.DeepSeq 59 | import qualified Data.HashMap.Strict as M 60 | import Data.Hashable (Hashable) 61 | #if MIN_VERSION_unordered_containers(0,2,8) 62 | import Data.Hashable.Lifted (Hashable1) 63 | #endif 64 | import Control.Lens 65 | import Control.Newtype 66 | import Data.Align 67 | #ifdef MIN_VERSION_semialign 68 | import Data.Semialign (Unalign) 69 | #if MIN_VERSION_semialign(1,1,0) 70 | import Data.Zip (Zip) 71 | #endif 72 | #endif 73 | import qualified Witherable 74 | 75 | -- | A 'HashMap' with monoidal accumulation 76 | newtype MonoidalHashMap k a = MonoidalHashMap { getMonoidalHashMap :: M.HashMap k a } 77 | deriving ( Show, Read, Functor, Eq, NFData 78 | , Foldable, Traversable, Data, Typeable, Hashable, Align 79 | #if MIN_VERSION_base(4,9,0) 80 | , Eq1 81 | #endif 82 | #if MIN_VERSION_unordered_containers(0,2,8) 83 | , Hashable1 84 | #endif 85 | #if MIN_VERSION_these(0,8,0) 86 | , Semialign 87 | #endif 88 | #ifdef MIN_VERSION_semialign 89 | , Unalign 90 | #if MIN_VERSION_semialign(1,1,0) 91 | , Zip 92 | #endif 93 | #endif 94 | , Witherable.Filterable 95 | ) 96 | 97 | type instance Index (MonoidalHashMap k a) = k 98 | type instance IxValue (MonoidalHashMap k a) = a 99 | instance (Eq k, Hashable k) => Ixed (MonoidalHashMap k a) where 100 | ix k f (MonoidalHashMap m) = case M.lookup k m of 101 | Just v -> f v <&> \v' -> MonoidalHashMap (M.insert k v' m) 102 | Nothing -> pure (MonoidalHashMap m) 103 | {-# INLINE ix #-} 104 | 105 | instance (Eq k, Hashable k) => At (MonoidalHashMap k a) where 106 | at k f (MonoidalHashMap m) = f mv <&> \r -> case r of 107 | Nothing -> maybe (MonoidalHashMap m) (const (MonoidalHashMap $ M.delete k m)) mv 108 | Just v' -> MonoidalHashMap $ M.insert k v' m 109 | where mv = M.lookup k m 110 | {-# INLINE at #-} 111 | 112 | instance Each (MonoidalHashMap k a) (MonoidalHashMap k b) a b 113 | 114 | instance (Eq k, Hashable k) => FunctorWithIndex k (MonoidalHashMap k) 115 | instance (Eq k, Hashable k) => FoldableWithIndex k (MonoidalHashMap k) 116 | instance (Eq k, Hashable k) => TraversableWithIndex k (MonoidalHashMap k) where 117 | itraverse f (MonoidalHashMap m) = fmap MonoidalHashMap $ itraverse f m 118 | {-# INLINE itraverse #-} 119 | 120 | instance AsEmpty (MonoidalHashMap k a) where 121 | _Empty = nearly (MonoidalHashMap M.empty) (M.null . unpack) 122 | {-# INLINE _Empty #-} 123 | 124 | instance Wrapped (MonoidalHashMap k a) where 125 | type Unwrapped (MonoidalHashMap k a) = M.HashMap k a 126 | _Wrapped' = iso unpack pack 127 | {-# INLINE _Wrapped' #-} 128 | 129 | instance (Eq k, Hashable k) => Rewrapped (M.HashMap k a) (MonoidalHashMap k a) 130 | 131 | instance (Eq k, Hashable k) => Rewrapped (MonoidalHashMap k a) (M.HashMap k a) 132 | 133 | instance (Eq k, Hashable k, Semigroup a) => Semigroup (MonoidalHashMap k a) where 134 | MonoidalHashMap a <> MonoidalHashMap b = MonoidalHashMap $ M.unionWith (<>) a b 135 | {-# INLINE (<>) #-} 136 | 137 | instance (Eq k, Hashable k, Semigroup a) => Monoid (MonoidalHashMap k a) where 138 | mempty = MonoidalHashMap mempty 139 | {-# INLINE mempty #-} 140 | #if !(MIN_VERSION_base(4,11,0)) 141 | mappend (MonoidalHashMap a) (MonoidalHashMap b) = MonoidalHashMap $ M.unionWith (<>) a b 142 | {-# INLINE mappend #-} 143 | #endif 144 | 145 | instance Newtype (MonoidalHashMap k a) (M.HashMap k a) where 146 | pack = MonoidalHashMap 147 | {-# INLINE pack #-} 148 | unpack (MonoidalHashMap a) = a 149 | {-# INLINE unpack #-} 150 | 151 | #if MIN_VERSION_base(4,7,0) 152 | instance (Eq k, Hashable k, Semigroup a) => Exts.IsList (MonoidalHashMap k a) where 153 | type Item (MonoidalHashMap k a) = (k, a) 154 | fromList = MonoidalHashMap . M.fromList 155 | {-# INLINE fromList #-} 156 | toList = M.toList . unpack 157 | {-# INLINE toList #-} 158 | #endif 159 | 160 | instance (Eq k, Hashable k) => Witherable.Witherable (MonoidalHashMap k) 161 | 162 | -- | /O(1)/. A map with a single element. 163 | singleton :: (Eq k, Hashable k) => k -> a -> MonoidalHashMap k a 164 | singleton k a = MonoidalHashMap $ M.singleton k a 165 | {-# INLINE singleton #-} 166 | 167 | -- | /O(1)/. The number of elements in the map. 168 | size :: MonoidalHashMap k a -> Int 169 | size = M.size . unpack 170 | {-# INLINE size #-} 171 | 172 | -- | /O(log n)/. Is the key a member of the map? See also 'notMember'. 173 | member :: (Eq k, Hashable k) => k -> MonoidalHashMap k a -> Bool 174 | member k = M.member k . unpack 175 | {-# INLINE member #-} 176 | 177 | -- | /O(log n)/. Is the key not a member of the map? See also 'member'. 178 | notMember :: (Eq k, Hashable k) => k -> MonoidalHashMap k a -> Bool 179 | notMember k = not . M.member k . unpack 180 | {-# INLINE notMember #-} 181 | 182 | -- | /O(log n)/ Return the value to which the specified key is mapped, 183 | -- or 'Nothing' if this map contains no mapping for the key. 184 | lookup :: (Eq k, Hashable k) => k -> MonoidalHashMap k v -> Maybe v 185 | lookup k = M.lookup k . unpack 186 | {-# INLINE lookup #-} 187 | 188 | -- | /O(log n)/ Return the value to which the specified key is mapped, 189 | -- or mempty if this map contains no mapping for the key. 190 | lookupM :: (Eq k, Hashable k, Monoid v) => k -> MonoidalHashMap k v -> v 191 | lookupM k = fromMaybe mempty . M.lookup k . unpack 192 | {-# INLINE lookupM #-} 193 | 194 | -- | /O(log n)/. Delete a key and its value from the map. When the key is not 195 | -- a member of the map, the original map is returned. 196 | delete :: (Eq k, Hashable k) => k -> MonoidalHashMap k a -> MonoidalHashMap k a 197 | delete k = _Wrapping' MonoidalHashMap %~ M.delete k 198 | {-# INLINE delete #-} 199 | 200 | -- | /O(n)/. 201 | -- Return a list of this map's values. The list is produced lazily. 202 | elems :: MonoidalHashMap k a -> [a] 203 | elems = M.elems . unpack 204 | {-# INLINE elems #-} 205 | 206 | -- | /O(n)/. Return all keys of the map in ascending order. Subject to list 207 | -- fusion. 208 | keys :: MonoidalHashMap k a -> [k] 209 | keys = M.keys . unpack 210 | {-# INLINE keys #-} 211 | 212 | -- | /O(n*log n)/. Construct a map with the supplied mappings. If the list 213 | -- contains duplicate mappings, values will be replaced. 214 | fromList :: (Eq k, Hashable k) => [(k,a)] -> MonoidalHashMap k a 215 | fromList = pack . M.fromList 216 | {-# INLINE fromList #-} 217 | 218 | -- | /O(n*log n)/. Construct a map with the supplied mappings. If the list 219 | -- contains duplicate mappings, values will be merged using the provided combining function. 220 | fromListWith :: (Eq k, Hashable k) => (a -> a -> a) -> [(k,a)] -> MonoidalHashMap k a 221 | fromListWith f = pack . M.fromListWith f 222 | {-# INLINE fromListWith #-} 223 | 224 | 225 | -- | /O(n*log n)/. Return a list of this map's elements. The list is produced 226 | -- lazily. The order of its elements is unspecified. 227 | toList :: MonoidalHashMap k a -> [(k,a)] 228 | toList = M.toList . unpack 229 | {-# INLINE toList #-} 230 | 231 | -- | /O(log n)/. Insert a value on some key, if it exists replace the value. 232 | insert :: (Hashable k, Eq k) 233 | => k 234 | -> a 235 | -> MonoidalHashMap k a 236 | -> MonoidalHashMap k a 237 | insert k x = pack 238 | . M.insert k x 239 | . unpack 240 | 241 | -- | /O(log n)/. Insert a value on some key, if it exists apply the combining function. 242 | insertWith :: (Hashable k, Eq k) 243 | => (a -> a -> a) 244 | -> k 245 | -> a 246 | -> MonoidalHashMap k a 247 | -> MonoidalHashMap k a 248 | insertWith f k x = pack 249 | . M.insertWith f k x 250 | . unpack 251 | 252 | -- | /O(log n)/. Modify a value on some key with a function, if value 253 | -- under key doesn't exist -- use mempty. 254 | modify :: (Monoid a, Hashable k, Eq k) 255 | => (a -> a) 256 | -> k -> MonoidalHashMap k a 257 | -> MonoidalHashMap k a 258 | modify f k = pack 259 | . M.insertWith (\_ old -> f old) k (f mempty) 260 | . unpack 261 | {-# INLINE modify #-} 262 | 263 | -- | /O(log n)/. Modify a value on some key with a function, providing 264 | -- a default value if that key doesn't exist. 265 | modifyDef :: (Hashable k, Eq k) 266 | => a -> (a -> a) 267 | -> k -> MonoidalHashMap k a 268 | -> MonoidalHashMap k a 269 | modifyDef d f k = pack 270 | . M.insertWith (\_ old -> f old) k d 271 | . unpack 272 | {-# INLINE modifyDef #-} 273 | 274 | -- | /O(n)/. Map a function to each key of a map, if it will result 275 | -- in duplicated mappings, their values will be merged in unspecified order 276 | mapKeys :: (Hashable k, Eq k, Hashable k', Eq k') 277 | => (k -> k') -> MonoidalHashMap k a -> MonoidalHashMap k' a 278 | mapKeys f = fromList 279 | . fmap (\(k, v) -> (f k, v)) 280 | . toList 281 | {-# INLINE mapKeys #-} 282 | 283 | -- | /O(n)/ Filter this map by retaining only elements satisfying a 284 | -- predicate. 285 | filterWithKey :: (k -> v -> Bool) -> MonoidalHashMap k v -> MonoidalHashMap k v 286 | filterWithKey pred = pack . M.filterWithKey pred . unpack 287 | {-# INLINE filterWithKey #-} 288 | 289 | -- | /O(n)/ Transform this map by applying a function to every value. 290 | map :: (v1 -> v2) -> MonoidalHashMap k v1 -> MonoidalHashMap k v2 291 | map f = pack . M.map f . unpack 292 | {-# INLINE map #-} 293 | -------------------------------------------------------------------------------- /src/Data/IntMap/Monoidal.hs: -------------------------------------------------------------------------------- 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 (Foldable(..), lookup, map, 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 | import Data.Functor.Classes 151 | import Data.Align 152 | #ifdef MIN_VERSION_semialign 153 | import Data.Semialign (Unalign) 154 | #if MIN_VERSION_semialign(1,1,0) 155 | import Data.Zip (Zip) 156 | #endif 157 | #endif 158 | import qualified Witherable 159 | 160 | -- | An 'IntMap' with monoidal accumulation 161 | newtype MonoidalIntMap a = MonoidalIntMap { getMonoidalIntMap :: M.IntMap a } 162 | deriving (Show, Read, Functor, Eq, Ord, NFData, 163 | Foldable, Traversable, 164 | FromJSON, ToJSON, FromJSON1, ToJSON1, 165 | Data, Typeable, Align 166 | #if MIN_VERSION_these(0,8,0) 167 | , Semialign 168 | #endif 169 | #ifdef MIN_VERSION_semialign 170 | , Unalign 171 | #if MIN_VERSION_semialign(1,1,0) 172 | , Zip 173 | #endif 174 | #endif 175 | , Witherable.Filterable 176 | ) 177 | 178 | deriving instance Eq1 MonoidalIntMap 179 | deriving instance Ord1 MonoidalIntMap 180 | deriving instance Show1 MonoidalIntMap 181 | 182 | type instance Index (MonoidalIntMap a) = Int 183 | type instance IxValue (MonoidalIntMap a) = a 184 | instance Ixed (MonoidalIntMap a) where 185 | ix k f (MonoidalIntMap m) = case M.lookup k m of 186 | Just v -> f v <&> \v' -> MonoidalIntMap (M.insert k v' m) 187 | Nothing -> pure (MonoidalIntMap m) 188 | {-# INLINE ix #-} 189 | 190 | instance At (MonoidalIntMap a) where 191 | at k f (MonoidalIntMap m) = f mv <&> \r -> case r of 192 | Nothing -> maybe (MonoidalIntMap m) (const (MonoidalIntMap $ M.delete k m)) mv 193 | Just v' -> MonoidalIntMap $ M.insert k v' m 194 | where mv = M.lookup k m 195 | {-# INLINE at #-} 196 | 197 | instance Each (MonoidalIntMap a) (MonoidalIntMap b) a b 198 | 199 | instance FunctorWithIndex Int MonoidalIntMap 200 | instance FoldableWithIndex Int MonoidalIntMap 201 | instance TraversableWithIndex Int MonoidalIntMap where 202 | itraverse f (MonoidalIntMap m) = fmap MonoidalIntMap $ itraverse f m 203 | {-# INLINE itraverse #-} 204 | 205 | instance TraverseMin Int MonoidalIntMap where 206 | traverseMin f (MonoidalIntMap m) = fmap MonoidalIntMap $ traverseMin f m 207 | {-# INLINE traverseMin #-} 208 | instance TraverseMax Int MonoidalIntMap where 209 | traverseMax f (MonoidalIntMap m) = fmap MonoidalIntMap $ traverseMax f m 210 | {-# INLINE traverseMax #-} 211 | 212 | instance AsEmpty (MonoidalIntMap a) where 213 | _Empty = nearly (MonoidalIntMap M.empty) (M.null . unpack) 214 | {-# INLINE _Empty #-} 215 | 216 | instance Wrapped (MonoidalIntMap a) where 217 | type Unwrapped (MonoidalIntMap a) = M.IntMap a 218 | _Wrapped' = iso unpack pack 219 | {-# INLINE _Wrapped' #-} 220 | 221 | instance Semigroup a => Semigroup (MonoidalIntMap a) where 222 | MonoidalIntMap a <> MonoidalIntMap b = MonoidalIntMap $ M.unionWith (<>) a b 223 | {-# INLINE (<>) #-} 224 | 225 | instance Semigroup a => Monoid (MonoidalIntMap a) where 226 | mempty = MonoidalIntMap mempty 227 | {-# INLINE mempty #-} 228 | #if !(MIN_VERSION_base(4,11,0)) 229 | mappend (MonoidalIntMap a) (MonoidalIntMap b) = MonoidalIntMap $ M.unionWith (<>) a b 230 | {-# INLINE mappend #-} 231 | #endif 232 | 233 | instance Newtype (MonoidalIntMap a) (M.IntMap a) where 234 | pack = MonoidalIntMap 235 | {-# INLINE pack #-} 236 | unpack (MonoidalIntMap a) = a 237 | {-# INLINE unpack #-} 238 | 239 | #if MIN_VERSION_base(4,7,0) 240 | instance Semigroup a => IsList.IsList (MonoidalIntMap a) where 241 | type Item (MonoidalIntMap a) = (Int, a) 242 | fromList = MonoidalIntMap . M.fromListWith (<>) 243 | {-# INLINE fromList #-} 244 | toList = M.toList . unpack 245 | {-# INLINE toList #-} 246 | #endif 247 | 248 | instance Witherable.Witherable MonoidalIntMap 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 | 652 | -------------------------------------------------------------------------------- /src/Data/IntMap/Monoidal/Strict.hs: -------------------------------------------------------------------------------- 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 (Foldable(..), lookup, map, 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 | import Data.Functor.Classes 151 | import Data.Align 152 | #ifdef MIN_VERSION_semialign 153 | import Data.Semialign (Unalign) 154 | #if MIN_VERSION_semialign(1,1,0) 155 | import Data.Zip (Zip) 156 | #endif 157 | #endif 158 | import qualified Witherable 159 | 160 | -- | An 'IntMap' with monoidal accumulation 161 | newtype MonoidalIntMap a = MonoidalIntMap { getMonoidalIntMap :: M.IntMap a } 162 | deriving (Show, Read, Functor, Eq, Ord, NFData, 163 | Foldable, Traversable, 164 | FromJSON, ToJSON, FromJSON1, ToJSON1, 165 | Data, Typeable, Align 166 | #if MIN_VERSION_these(0,8,0) 167 | , Semialign 168 | #endif 169 | #ifdef MIN_VERSION_semialign 170 | , Unalign 171 | #if MIN_VERSION_semialign(1,1,0) 172 | , Zip 173 | #endif 174 | #endif 175 | , Witherable.Filterable 176 | ) 177 | 178 | deriving instance Eq1 MonoidalIntMap 179 | deriving instance Ord1 MonoidalIntMap 180 | deriving instance Show1 MonoidalIntMap 181 | 182 | type instance Index (MonoidalIntMap a) = Int 183 | type instance IxValue (MonoidalIntMap a) = a 184 | instance Ixed (MonoidalIntMap a) where 185 | ix k f (MonoidalIntMap m) = case M.lookup k m of 186 | Just v -> f v <&> \v' -> MonoidalIntMap (M.insert k v' m) 187 | Nothing -> pure (MonoidalIntMap m) 188 | {-# INLINE ix #-} 189 | 190 | instance At (MonoidalIntMap a) where 191 | at k f (MonoidalIntMap m) = f mv <&> \r -> case r of 192 | Nothing -> maybe (MonoidalIntMap m) (const (MonoidalIntMap $ M.delete k m)) mv 193 | Just v' -> MonoidalIntMap $ M.insert k v' m 194 | where mv = M.lookup k m 195 | {-# INLINE at #-} 196 | 197 | instance Each (MonoidalIntMap a) (MonoidalIntMap b) a b 198 | 199 | instance FunctorWithIndex Int MonoidalIntMap 200 | instance FoldableWithIndex Int MonoidalIntMap 201 | instance TraversableWithIndex Int MonoidalIntMap where 202 | itraverse f (MonoidalIntMap m) = fmap MonoidalIntMap $ itraverse f m 203 | {-# INLINE itraverse #-} 204 | 205 | instance TraverseMin Int MonoidalIntMap where 206 | traverseMin f (MonoidalIntMap m) = fmap MonoidalIntMap $ traverseMin f m 207 | {-# INLINE traverseMin #-} 208 | instance TraverseMax Int MonoidalIntMap where 209 | traverseMax f (MonoidalIntMap m) = fmap MonoidalIntMap $ traverseMax f m 210 | {-# INLINE traverseMax #-} 211 | 212 | instance AsEmpty (MonoidalIntMap a) where 213 | _Empty = nearly (MonoidalIntMap M.empty) (M.null . unpack) 214 | {-# INLINE _Empty #-} 215 | 216 | instance Wrapped (MonoidalIntMap a) where 217 | type Unwrapped (MonoidalIntMap a) = M.IntMap a 218 | _Wrapped' = iso unpack pack 219 | {-# INLINE _Wrapped' #-} 220 | 221 | instance Semigroup a => Semigroup (MonoidalIntMap a) where 222 | MonoidalIntMap a <> MonoidalIntMap b = MonoidalIntMap $ M.unionWith (<>) a b 223 | {-# INLINE (<>) #-} 224 | 225 | instance Semigroup a => Monoid (MonoidalIntMap a) where 226 | mempty = MonoidalIntMap mempty 227 | {-# INLINE mempty #-} 228 | #if !(MIN_VERSION_base(4,11,0)) 229 | mappend (MonoidalIntMap a) (MonoidalIntMap b) = MonoidalIntMap $ M.unionWith (<>) a b 230 | {-# INLINE mappend #-} 231 | #endif 232 | 233 | instance Newtype (MonoidalIntMap a) (M.IntMap a) where 234 | pack = MonoidalIntMap 235 | {-# INLINE pack #-} 236 | unpack (MonoidalIntMap a) = a 237 | {-# INLINE unpack #-} 238 | 239 | #if MIN_VERSION_base(4,7,0) 240 | instance Semigroup a => IsList.IsList (MonoidalIntMap a) where 241 | type Item (MonoidalIntMap a) = (Int, a) 242 | fromList = MonoidalIntMap . M.fromListWith (<>) 243 | {-# INLINE fromList #-} 244 | toList = M.toList . unpack 245 | {-# INLINE toList #-} 246 | #endif 247 | 248 | instance Witherable.Witherable MonoidalIntMap 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 | 652 | -------------------------------------------------------------------------------- /src/Data/Map/Monoidal.hs: -------------------------------------------------------------------------------- 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.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 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 | , (\\) 34 | , adjust 35 | , adjustWithKey 36 | , alter 37 | , delete 38 | , deleteAt 39 | , take 40 | , drop 41 | , splitAt 42 | , lookupMin 43 | , lookupMax 44 | , deleteFindMax 45 | , deleteFindMin 46 | , deleteMax 47 | , deleteMin 48 | , difference 49 | , differenceWith 50 | , differenceWithKey 51 | , elemAt 52 | , empty 53 | , filter 54 | , filterWithKey 55 | , restrictKeys 56 | , withoutKeys 57 | , findIndex 58 | , findMax 59 | , findMin 60 | , foldMapWithKey 61 | , foldl 62 | , foldl' 63 | , foldlWithKey 64 | , foldlWithKey' 65 | , foldr 66 | , foldr' 67 | , foldrWithKey 68 | , foldrWithKey' 69 | , fromAscList 70 | , fromAscListWith 71 | , fromAscListWithKey 72 | , fromDistinctAscList 73 | , fromDistinctList 74 | , fromDescList 75 | , fromDescListWith 76 | , fromDescListWithKey 77 | , fromDistinctDescList 78 | , fromList 79 | , fromListWith 80 | , fromListWithKey 81 | , fromSet 82 | , insert 83 | , insertLookupWithKey 84 | , insertWith 85 | , insertWithKey 86 | , intersectionWith 87 | , intersectionWithKey 88 | , isProperSubmapOf 89 | , isProperSubmapOfBy 90 | , isSubmapOf 91 | , isSubmapOfBy 92 | , keysSet 93 | , lookup 94 | , lookupGE 95 | , lookupGT 96 | , lookupIndex 97 | , lookupLE 98 | , lookupLT 99 | , map 100 | , mapAccum 101 | , mapAccumRWithKey 102 | , mapAccumWithKey 103 | , mapEither 104 | , mapEitherWithKey 105 | , mapKeys 106 | , mapKeysMonotonic 107 | , mapKeysWith 108 | , mapMaybe 109 | , mapMaybeWithKey 110 | , mapWithKey 111 | , maxView 112 | , maxViewWithKey 113 | , mergeWithKey 114 | , minView 115 | , minViewWithKey 116 | , null 117 | , partition 118 | , partitionWithKey 119 | , takeWhileAntitone 120 | , dropWhileAntitone 121 | , spanAntitone 122 | , split 123 | , splitLookup 124 | , splitRoot 125 | , toAscList 126 | , toDescList 127 | , toList 128 | , traverseWithKey 129 | , traverseMaybeWithKey 130 | , unionWith 131 | , unionWithKey 132 | , unionsWith 133 | , update 134 | , updateAt 135 | , updateLookupWithKey 136 | , updateMax 137 | , updateMaxWithKey 138 | , updateMin 139 | , updateMinWithKey 140 | , updateWithKey 141 | , valid 142 | -- , showTree 143 | -- , showTreeWith 144 | ) where 145 | 146 | import Prelude hiding (Foldable(..), lookup, map, filter, take, drop, splitAt) 147 | 148 | import Data.Coerce (coerce) 149 | import Data.Set (Set) 150 | import Data.Semigroup 151 | import Data.Foldable (Foldable) 152 | import Data.Traversable (Traversable) 153 | import Control.Applicative (Applicative, pure) 154 | import Data.Data (Data) 155 | import Data.Typeable (Typeable) 156 | 157 | #if MIN_VERSION_base(4,7,0) 158 | import qualified GHC.Exts as IsList 159 | #endif 160 | 161 | import Control.DeepSeq 162 | import qualified Data.Map as M 163 | import Control.Lens 164 | import Control.Newtype 165 | import Data.Aeson(FromJSON, ToJSON, FromJSON1, ToJSON1) 166 | import Data.Functor.Classes 167 | import Data.Align 168 | #ifdef MIN_VERSION_semialign 169 | import Data.Semialign (Unalign) 170 | #if MIN_VERSION_semialign(1,1,0) 171 | import Data.Zip (Zip) 172 | #endif 173 | #endif 174 | import qualified Witherable 175 | 176 | -- | A 'Map' with monoidal accumulation 177 | newtype MonoidalMap k a = MonoidalMap { getMonoidalMap :: M.Map k a } 178 | deriving ( Show, Read, Functor, Eq, Ord, NFData 179 | , Foldable, Traversable 180 | , FromJSON, ToJSON, FromJSON1, ToJSON1 181 | , Data, Typeable, Align 182 | #if MIN_VERSION_these(0,8,0) 183 | , Semialign 184 | #endif 185 | #ifdef MIN_VERSION_semialign 186 | , Unalign 187 | #if MIN_VERSION_semialign(1,1,0) 188 | , Zip 189 | #endif 190 | #endif 191 | , Witherable.Filterable 192 | ) 193 | 194 | deriving instance (Ord k) => Eq1 (MonoidalMap k) 195 | deriving instance (Ord k) => Ord1 (MonoidalMap k) 196 | deriving instance (Show k) => Show1 (MonoidalMap k) 197 | 198 | type instance Index (MonoidalMap k a) = k 199 | type instance IxValue (MonoidalMap k a) = a 200 | instance Ord k => Ixed (MonoidalMap k a) where 201 | ix k f (MonoidalMap m) = case M.lookup k m of 202 | Just v -> f v <&> \v' -> MonoidalMap (M.insert k v' m) 203 | Nothing -> pure (MonoidalMap m) 204 | {-# INLINE ix #-} 205 | 206 | instance Ord k => At (MonoidalMap k a) where 207 | at k f (MonoidalMap m) = f mv <&> \r -> case r of 208 | Nothing -> maybe (MonoidalMap m) (const (MonoidalMap $ M.delete k m)) mv 209 | Just v' -> MonoidalMap $ M.insert k v' m 210 | where mv = M.lookup k m 211 | {-# INLINE at #-} 212 | 213 | instance Each (MonoidalMap k a) (MonoidalMap k b) a b 214 | 215 | instance FunctorWithIndex k (MonoidalMap k) 216 | instance FoldableWithIndex k (MonoidalMap k) 217 | instance TraversableWithIndex k (MonoidalMap k) where 218 | itraverse f (MonoidalMap m) = fmap MonoidalMap $ itraverse f m 219 | {-# INLINE itraverse #-} 220 | 221 | instance Ord k => TraverseMin k (MonoidalMap k) where 222 | traverseMin f (MonoidalMap m) = fmap MonoidalMap $ traverseMin f m 223 | {-# INLINE traverseMin #-} 224 | instance Ord k => TraverseMax k (MonoidalMap k) where 225 | traverseMax f (MonoidalMap m) = fmap MonoidalMap $ traverseMax f m 226 | {-# INLINE traverseMax #-} 227 | 228 | instance AsEmpty (MonoidalMap k a) where 229 | _Empty = nearly (MonoidalMap M.empty) (M.null . unpack) 230 | {-# INLINE _Empty #-} 231 | 232 | instance Wrapped (MonoidalMap k a) where 233 | type Unwrapped (MonoidalMap k a) = M.Map k a 234 | _Wrapped' = iso unpack pack 235 | {-# INLINE _Wrapped' #-} 236 | 237 | instance Ord k => Rewrapped (M.Map k a) (MonoidalMap k a) 238 | 239 | instance Ord k => Rewrapped (MonoidalMap k a) (M.Map k a) 240 | 241 | instance (Ord k, Semigroup a) => Semigroup (MonoidalMap k a) where 242 | MonoidalMap a <> MonoidalMap b = MonoidalMap $ M.unionWith (<>) a b 243 | {-# INLINE (<>) #-} 244 | 245 | instance (Ord k, Semigroup a) => Monoid (MonoidalMap k a) where 246 | mempty = MonoidalMap mempty 247 | {-# INLINE mempty #-} 248 | #if !(MIN_VERSION_base(4,11,0)) 249 | mappend (MonoidalMap a) (MonoidalMap b) = MonoidalMap $ M.unionWith (<>) a b 250 | {-# INLINE mappend #-} 251 | #endif 252 | 253 | instance Newtype (MonoidalMap k a) (M.Map k a) where 254 | pack = MonoidalMap 255 | {-# INLINE pack #-} 256 | unpack (MonoidalMap a) = a 257 | {-# INLINE unpack #-} 258 | 259 | #if MIN_VERSION_base(4,7,0) 260 | instance (Ord k, Semigroup a) => IsList.IsList (MonoidalMap k a) where 261 | type Item (MonoidalMap k a) = (k, a) 262 | fromList = MonoidalMap . M.fromListWith (<>) 263 | {-# INLINE fromList #-} 264 | toList = M.toList . unpack 265 | {-# INLINE toList #-} 266 | #endif 267 | 268 | instance Ord k => Witherable.Witherable (MonoidalMap k) 269 | 270 | -- | /O(1)/. A map with a single element. 271 | singleton :: k -> a -> MonoidalMap k a 272 | singleton k a = MonoidalMap $ M.singleton k a 273 | {-# INLINE singleton #-} 274 | 275 | -- | /O(1)/. The number of elements in the map. 276 | size :: MonoidalMap k a -> Int 277 | size = M.size . unpack 278 | {-# INLINE size #-} 279 | 280 | -- | /O(log n)/. Is the key a member of the map? See also 'notMember'. 281 | member :: Ord k => k -> MonoidalMap k a -> Bool 282 | member k = M.member k . unpack 283 | {-# INLINE member #-} 284 | 285 | -- | /O(log n)/. Is the key not a member of the map? See also 'member'. 286 | notMember :: Ord k => k -> MonoidalMap k a -> Bool 287 | notMember k = not . M.member k . unpack 288 | {-# INLINE notMember #-} 289 | 290 | -- | /O(log n)/. The expression @('findWithDefault' def k map)@ returns 291 | -- the value at key @k@ or returns default value @def@ 292 | -- when the key is not in the map. 293 | findWithDefault :: Ord k => a -> k -> MonoidalMap k a -> a 294 | findWithDefault def k = M.findWithDefault def k . unpack 295 | {-# INLINE findWithDefault #-} 296 | 297 | -- | /O(log n)/. Delete a key and its value from the map. When the key is not 298 | -- a member of the map, the original map is returned. 299 | delete :: Ord k => k -> MonoidalMap k a -> MonoidalMap k a 300 | delete k = _Wrapping' MonoidalMap %~ M.delete k 301 | {-# INLINE delete #-} 302 | 303 | -- | /O(n)/. Return all elements of the map and their keys 304 | assocs :: MonoidalMap k a -> [(k,a)] 305 | assocs = M.assocs . unpack 306 | {-# INLINE assocs #-} 307 | 308 | -- | /O(n)/. Return all elements of the map in the ascending order of their 309 | -- keys. Subject to list fusion. 310 | elems :: MonoidalMap k a -> [a] 311 | elems = M.elems . unpack 312 | {-# INLINE elems #-} 313 | 314 | -- | /O(n)/. Return all keys of the map in ascending order. Subject to list 315 | -- fusion. 316 | keys :: MonoidalMap k a -> [k] 317 | keys = M.keys . unpack 318 | {-# INLINE keys #-} 319 | 320 | (!?) :: forall k a. Ord k => MonoidalMap k a -> k -> Maybe a 321 | (!?) = coerce ((M.!?) :: M.Map k a -> k -> Maybe a) 322 | infixl 9 !? 323 | {-# INLINE (!?) #-} 324 | 325 | 326 | (!) :: forall k a. Ord k => MonoidalMap k a -> k -> a 327 | (!) = coerce ((M.!) :: M.Map k a -> k -> a) 328 | infixl 9 ! 329 | 330 | (\\) :: forall k a b. Ord k => MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k a 331 | (\\) = coerce ((M.\\) :: M.Map k a -> M.Map k b -> M.Map k a) 332 | infixl 9 \\ -- 333 | 334 | null :: forall k a. MonoidalMap k a -> Bool 335 | null = coerce (M.null :: M.Map k a -> Bool) 336 | {-# INLINE null #-} 337 | 338 | lookup :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe a 339 | lookup = coerce (M.lookup :: k -> M.Map k a -> Maybe a) 340 | {-# INLINE lookup #-} 341 | 342 | lookupLT :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe (k, a) 343 | lookupLT = coerce (M.lookupLT :: k -> M.Map k a -> Maybe (k,a)) 344 | {-# INLINE lookupLT #-} 345 | 346 | lookupGT :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe (k, a) 347 | lookupGT = coerce (M.lookupGT :: k -> M.Map k a -> Maybe (k,a)) 348 | {-# INLINE lookupGT #-} 349 | 350 | lookupLE :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe (k, a) 351 | lookupLE = coerce (M.lookupLE :: k -> M.Map k a -> Maybe (k,a)) 352 | {-# INLINE lookupLE #-} 353 | 354 | lookupGE :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe (k, a) 355 | lookupGE = coerce (M.lookupGE :: k -> M.Map k a -> Maybe (k,a)) 356 | {-# INLINE lookupGE #-} 357 | 358 | empty :: forall k a. MonoidalMap k a 359 | empty = coerce (M.empty :: M.Map k a) 360 | {-# INLINE empty #-} 361 | 362 | insert :: forall k a. Ord k => k -> a -> MonoidalMap k a -> MonoidalMap k a 363 | insert = coerce (M.insert :: k -> a -> M.Map k a -> M.Map k a) 364 | {-# INLINE insert #-} 365 | 366 | insertWith :: forall k a. Ord k => (a -> a -> a) -> k -> a -> MonoidalMap k a -> MonoidalMap k a 367 | insertWith = coerce (M.insertWith :: (a -> a -> a) -> k -> a -> M.Map k a -> M.Map k a) 368 | {-# INLINE insertWith #-} 369 | 370 | insertWithKey :: forall k a. Ord k => (k -> a -> a -> a) -> k -> a -> MonoidalMap k a -> MonoidalMap k a 371 | insertWithKey = coerce (M.insertWithKey :: (k -> a -> a -> a) -> k -> a -> M.Map k a -> M.Map k a) 372 | {-# INLINE insertWithKey #-} 373 | 374 | insertLookupWithKey :: forall k a. Ord k => (k -> a -> a -> a) -> k -> a -> MonoidalMap k a -> (Maybe a, MonoidalMap k a) 375 | insertLookupWithKey = coerce (M.insertLookupWithKey :: (k -> a -> a -> a) -> k -> a -> M.Map k a -> (Maybe a, M.Map k a)) 376 | {-# INLINE insertLookupWithKey #-} 377 | 378 | adjust :: forall k a. Ord k => (a -> a) -> k -> MonoidalMap k a -> MonoidalMap k a 379 | adjust = coerce (M.adjust :: (a -> a) -> k -> M.Map k a -> M.Map k a) 380 | {-# INLINE adjust #-} 381 | 382 | adjustWithKey :: forall k a. Ord k => (k -> a -> a) -> k -> MonoidalMap k a -> MonoidalMap k a 383 | adjustWithKey = coerce (M.adjustWithKey :: (k -> a -> a) -> k -> M.Map k a -> M.Map k a) 384 | {-# INLINE adjustWithKey #-} 385 | 386 | update :: forall k a. Ord k => (a -> Maybe a) -> k -> MonoidalMap k a -> MonoidalMap k a 387 | update = coerce (M.update :: (a -> Maybe a) -> k -> M.Map k a -> M.Map k a) 388 | {-# INLINE update #-} 389 | 390 | updateWithKey :: forall k a. Ord k => (k -> a -> Maybe a) -> k -> MonoidalMap k a -> MonoidalMap k a 391 | updateWithKey = coerce (M.updateWithKey :: (k -> a -> Maybe a) -> k -> M.Map k a -> M.Map k a) 392 | {-# INLINE updateWithKey #-} 393 | 394 | updateLookupWithKey :: forall k a. Ord k => (k -> a -> Maybe a) -> k -> MonoidalMap k a -> (Maybe a, MonoidalMap k a) 395 | updateLookupWithKey = coerce (M.updateLookupWithKey :: (k -> a -> Maybe a) -> k -> M.Map k a -> (Maybe a, M.Map k a)) 396 | {-# INLINE updateLookupWithKey #-} 397 | 398 | alter :: forall k a. Ord k => (Maybe a -> Maybe a) -> k -> MonoidalMap k a -> MonoidalMap k a 399 | alter = coerce (M.alter :: (Maybe a -> Maybe a) -> k -> M.Map k a -> M.Map k a) 400 | {-# INLINE alter #-} 401 | 402 | unionWith :: forall k a. Ord k => (a -> a -> a) -> MonoidalMap k a -> MonoidalMap k a -> MonoidalMap k a 403 | unionWith = coerce (M.unionWith :: (a -> a -> a) -> M.Map k a -> M.Map k a -> M.Map k a) 404 | {-# INLINE unionWith #-} 405 | 406 | unionWithKey :: forall k a. Ord k => (k -> a -> a -> a) -> MonoidalMap k a -> MonoidalMap k a -> MonoidalMap k a 407 | unionWithKey = coerce (M.unionWithKey :: (k -> a -> a -> a) -> M.Map k a -> M.Map k a -> M.Map k a) 408 | {-# INLINE unionWithKey #-} 409 | 410 | unionsWith :: forall k a. Ord k => (a -> a -> a) -> [MonoidalMap k a] -> MonoidalMap k a 411 | unionsWith = coerce (M.unionsWith :: (a -> a -> a) -> [M.Map k a] -> M.Map k a) 412 | {-# INLINE unionsWith #-} 413 | 414 | difference :: forall k a b. Ord k => MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k a 415 | difference = (\\) 416 | {-# INLINE difference #-} 417 | 418 | differenceWith :: forall k a b. Ord k => (a -> b -> Maybe a) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k a 419 | differenceWith = coerce (M.differenceWith :: (a -> b -> Maybe a) -> M.Map k a -> M.Map k b -> M.Map k a) 420 | {-# INLINE differenceWith #-} 421 | 422 | differenceWithKey :: forall k a b. Ord k => (k -> a -> b -> Maybe a) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k a 423 | differenceWithKey = coerce (M.differenceWithKey :: (k -> a -> b -> Maybe a) -> M.Map k a -> M.Map k b -> M.Map k a) 424 | {-# INLINE differenceWithKey #-} 425 | 426 | intersectionWith :: forall k a b c. Ord k => (a -> b -> c) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k c 427 | intersectionWith = coerce (M.intersectionWith :: (a -> b -> c) -> M.Map k a -> M.Map k b -> M.Map k c) 428 | {-# INLINE intersectionWith #-} 429 | 430 | intersectionWithKey :: forall k a b c. Ord k => (k -> a -> b -> c) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k c 431 | intersectionWithKey = coerce (M.intersectionWithKey :: (k -> a -> b -> c) -> M.Map k a -> M.Map k b -> M.Map k c) 432 | {-# INLINE intersectionWithKey #-} 433 | 434 | 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 435 | 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) 436 | {-# INLINE mergeWithKey #-} 437 | 438 | map :: (a -> b) -> MonoidalMap k a -> MonoidalMap k b 439 | map = fmap 440 | {-# INLINE map #-} 441 | 442 | mapWithKey :: forall k a b. (k -> a -> b) -> MonoidalMap k a -> MonoidalMap k b 443 | mapWithKey = coerce (M.mapWithKey :: (k -> a -> b) -> M.Map k a -> M.Map k b) 444 | {-# INLINE mapWithKey #-} 445 | 446 | traverseWithKey :: Applicative t => (k -> a -> t b) -> MonoidalMap k a -> t (MonoidalMap k b) 447 | traverseWithKey = itraverse 448 | {-# INLINE traverseWithKey #-} 449 | 450 | traverseMaybeWithKey :: forall f k a b. Applicative f => (k -> a -> f (Maybe b)) -> MonoidalMap k a -> f (MonoidalMap k b) 451 | traverseMaybeWithKey f m = coerce <$> M.traverseMaybeWithKey f (coerce m) 452 | {-# INLINE traverseMaybeWithKey #-} 453 | 454 | mapAccum :: forall k a b c. (a -> b -> (a, c)) -> a -> MonoidalMap k b -> (a, MonoidalMap k c) 455 | mapAccum = coerce (M.mapAccum :: (a -> b -> (a, c)) -> a -> M.Map k b -> (a, M.Map k c)) 456 | {-# INLINE mapAccum #-} 457 | 458 | mapAccumWithKey :: forall k a b c. (a -> k -> b -> (a, c)) -> a -> MonoidalMap k b -> (a, MonoidalMap k c) 459 | mapAccumWithKey = coerce (M.mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> M.Map k b -> (a, M.Map k c)) 460 | {-# INLINE mapAccumWithKey #-} 461 | 462 | mapAccumRWithKey :: forall k a b c. (a -> k -> b -> (a, c)) -> a -> MonoidalMap k b -> (a, MonoidalMap k c) 463 | mapAccumRWithKey = coerce (M.mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> M.Map k b -> (a, M.Map k c)) 464 | {-# INLINE mapAccumRWithKey #-} 465 | 466 | mapKeys :: forall k1 k2 a. Ord k2 => (k1 -> k2) -> MonoidalMap k1 a -> MonoidalMap k2 a 467 | mapKeys = coerce (M.mapKeys :: (k1 -> k2) -> M.Map k1 a -> M.Map k2 a) 468 | {-# INLINE mapKeys #-} 469 | 470 | mapKeysWith :: forall k1 k2 a. Ord k2 => (a -> a -> a) -> (k1 -> k2) -> MonoidalMap k1 a -> MonoidalMap k2 a 471 | mapKeysWith = coerce (M.mapKeysWith :: (a -> a -> a) -> (k1 -> k2) -> M.Map k1 a -> M.Map k2 a) 472 | {-# INLINE mapKeysWith #-} 473 | 474 | -- | /O(n)/. 475 | -- @'mapKeysMonotonic' f s == 'mapKeys' f s@, but works only when @f@ 476 | -- is strictly increasing (both monotonic and injective). 477 | -- That is, for any values @x@ and @y@, if @x@ < @y@ then @f x@ < @f y@ 478 | -- and @f@ is injective (i.e. it never maps two input keys to the same output key). 479 | -- /The precondition is not checked./ 480 | -- Semi-formally, we have: 481 | -- 482 | -- > and [x < y ==> f x < f y | x <- ls, y <- ls] 483 | -- > ==> mapKeysMonotonic f s == mapKeys f s 484 | -- > where ls = keys s 485 | -- 486 | -- This means that @f@ maps distinct original keys to distinct resulting keys. 487 | -- This function has better performance than 'mapKeys'. 488 | -- 489 | -- > mapKeysMonotonic (\ k -> k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")] 490 | -- > valid (mapKeysMonotonic (\ k -> k * 2) (fromList [(5,"a"), (3,"b")])) == True 491 | -- > valid (mapKeysMonotonic (\ _ -> 1) (fromList [(5,"a"), (3,"b")])) == False 492 | mapKeysMonotonic :: forall k1 k2 a. (k1 -> k2) -> MonoidalMap k1 a -> MonoidalMap k2 a 493 | mapKeysMonotonic = coerce (M.mapKeysMonotonic :: (k1 -> k2) -> M.Map k1 a -> M.Map k2 a) 494 | {-# INLINE mapKeysMonotonic #-} 495 | 496 | foldr :: forall k a b. (a -> b -> b) -> b -> MonoidalMap k a -> b 497 | foldr = coerce (M.foldr :: (a -> b -> b) -> b -> M.Map k a -> b) 498 | {-# INLINE foldr #-} 499 | 500 | foldl :: forall k a b. (a -> b -> a) -> a -> MonoidalMap k b -> a 501 | foldl = coerce (M.foldl :: (a -> b -> a) -> a -> M.Map k b -> a) 502 | {-# INLINE foldl #-} 503 | 504 | foldrWithKey :: forall k a b. (k -> a -> b -> b) -> b -> MonoidalMap k a -> b 505 | foldrWithKey = coerce (M.foldrWithKey :: (k -> a -> b -> b) -> b -> M.Map k a -> b) 506 | {-# INLINE foldrWithKey #-} 507 | 508 | foldlWithKey :: forall k a b. (a -> k -> b -> a) -> a -> MonoidalMap k b -> a 509 | foldlWithKey = coerce (M.foldlWithKey :: (a -> k -> b -> a) -> a -> M.Map k b -> a) 510 | {-# INLINE foldlWithKey #-} 511 | 512 | foldMapWithKey :: forall k a m. Monoid m => (k -> a -> m) -> MonoidalMap k a -> m 513 | foldMapWithKey = coerce (M.foldMapWithKey :: Monoid m => (k -> a -> m) -> M.Map k a -> m) 514 | {-# INLINE foldMapWithKey #-} 515 | 516 | foldr' :: forall k a b. (a -> b -> b) -> b -> MonoidalMap k a -> b 517 | foldr' = coerce (M.foldr' :: (a -> b -> b) -> b -> M.Map k a -> b) 518 | {-# INLINE foldr' #-} 519 | 520 | foldl' :: forall k a b. (a -> b -> a) -> a -> MonoidalMap k b -> a 521 | foldl' = coerce (M.foldl' :: (a -> b -> a) -> a -> M.Map k b -> a) 522 | {-# INLINE foldl' #-} 523 | 524 | foldrWithKey' :: forall k a b. (k -> a -> b -> b) -> b -> MonoidalMap k a -> b 525 | foldrWithKey' = coerce (M.foldrWithKey' :: (k -> a -> b -> b) -> b -> M.Map k a -> b) 526 | {-# INLINE foldrWithKey' #-} 527 | 528 | foldlWithKey' :: forall k a b. (a -> k -> b -> a) -> a -> MonoidalMap k b -> a 529 | foldlWithKey' = coerce (M.foldlWithKey' :: (a -> k -> b -> a) -> a -> M.Map k b -> a) 530 | {-# INLINE foldlWithKey' #-} 531 | 532 | keysSet :: forall k a. MonoidalMap k a -> Set k 533 | keysSet = coerce (M.keysSet :: M.Map k a -> Set k) 534 | {-# INLINE keysSet #-} 535 | 536 | fromSet :: forall k a. (k -> a) -> Set k -> MonoidalMap k a 537 | fromSet = coerce (M.fromSet :: (k -> a) -> Set k -> M.Map k a) 538 | {-# INLINE fromSet #-} 539 | 540 | toList :: forall k a. MonoidalMap k a -> [(k, a)] 541 | toList = coerce (M.toList :: M.Map k a -> [(k, a)]) 542 | {-# INLINE toList #-} 543 | 544 | fromList :: forall k a. Ord k => [(k, a)] -> MonoidalMap k a 545 | fromList = coerce (M.fromList :: [(k, a)] -> M.Map k a) 546 | {-# INLINE fromList #-} 547 | 548 | fromListWith :: forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> MonoidalMap k a 549 | fromListWith = coerce (M.fromListWith :: (a -> a -> a) -> [(k, a)] -> M.Map k a) 550 | {-# INLINE fromListWith #-} 551 | 552 | fromListWithKey :: forall k a. Ord k => (k -> a -> a -> a) -> [(k, a)] -> MonoidalMap k a 553 | fromListWithKey = coerce (M.fromListWithKey :: (k -> a -> a -> a) -> [(k, a)] -> M.Map k a) 554 | {-# INLINE fromListWithKey #-} 555 | 556 | toAscList :: forall k a. MonoidalMap k a -> [(k, a)] 557 | toAscList = coerce (M.toAscList :: M.Map k a -> [(k, a)]) 558 | {-# INLINE toAscList #-} 559 | 560 | toDescList :: forall k a. MonoidalMap k a -> [(k, a)] 561 | toDescList = coerce (M.toDescList :: M.Map k a -> [(k, a)]) 562 | {-# INLINE toDescList #-} 563 | 564 | fromAscList :: forall k a. Eq k => [(k, a)] -> MonoidalMap k a 565 | fromAscList = coerce (M.fromAscList :: [(k, a)] -> M.Map k a) 566 | {-# INLINE fromAscList #-} 567 | 568 | fromAscListWith :: forall k a. Eq k => (a -> a -> a) -> [(k, a)] -> MonoidalMap k a 569 | fromAscListWith = coerce (M.fromAscListWith :: (a -> a -> a) -> [(k, a)] -> M.Map k a) 570 | {-# INLINE fromAscListWith #-} 571 | 572 | fromAscListWithKey :: forall k a. Eq k => (k -> a -> a -> a) -> [(k, a)] -> MonoidalMap k a 573 | fromAscListWithKey = coerce (M.fromAscListWithKey :: (k -> a -> a -> a) -> [(k, a)] -> M.Map k a) 574 | {-# INLINE fromAscListWithKey #-} 575 | 576 | fromDistinctAscList :: forall k a. [(k, a)] -> MonoidalMap k a 577 | fromDistinctAscList = coerce (M.fromDistinctAscList :: [(k, a)] -> M.Map k a) 578 | {-# INLINE fromDistinctAscList #-} 579 | 580 | fromDistinctList :: forall k a. Ord k => [(k, a)] -> MonoidalMap k a 581 | fromDistinctList = coerce (M.fromList :: [(k, a)] -> M.Map k a) 582 | {-# INLINE fromDistinctList #-} 583 | 584 | fromDescList :: forall k a. Eq k => [(k, a)] -> MonoidalMap k a 585 | fromDescList = coerce (M.fromDescList :: [(k, a)] -> M.Map k a) 586 | {-# INLINE fromDescList #-} 587 | 588 | fromDescListWith :: forall k a. Eq k => (a -> a -> a) -> [(k, a)] -> MonoidalMap k a 589 | fromDescListWith = coerce (M.fromDescListWith :: (a -> a -> a) -> [(k, a)] -> M.Map k a) 590 | {-# INLINE fromDescListWith #-} 591 | 592 | fromDescListWithKey :: forall k a. Eq k => (k -> a -> a -> a) -> [(k, a)] -> MonoidalMap k a 593 | fromDescListWithKey = coerce (M.fromDescListWithKey :: (k -> a -> a -> a) -> [(k, a)] -> M.Map k a) 594 | {-# INLINE fromDescListWithKey #-} 595 | 596 | fromDistinctDescList :: forall k a. [(k, a)] -> MonoidalMap k a 597 | fromDistinctDescList = coerce (M.fromDistinctDescList :: [(k, a)] -> M.Map k a) 598 | {-# INLINE fromDistinctDescList #-} 599 | 600 | filter :: forall k a. (a -> Bool) -> MonoidalMap k a -> MonoidalMap k a 601 | filter = coerce (M.filter :: (a -> Bool) -> M.Map k a -> M.Map k a) 602 | {-# INLINE filter #-} 603 | 604 | filterWithKey :: forall k a. (k -> a -> Bool) -> MonoidalMap k a -> MonoidalMap k a 605 | filterWithKey = coerce (M.filterWithKey :: (k -> a -> Bool) -> M.Map k a -> M.Map k a) 606 | {-# INLINE filterWithKey #-} 607 | 608 | restrictKeys :: forall k a. Ord k => MonoidalMap k a -> Set k -> MonoidalMap k a 609 | restrictKeys = coerce (M.restrictKeys :: M.Map k a -> Set k -> M.Map k a) 610 | {-# INLINE restrictKeys #-} 611 | 612 | withoutKeys :: forall k a. Ord k => MonoidalMap k a -> Set k -> MonoidalMap k a 613 | withoutKeys = coerce (M.withoutKeys :: M.Map k a -> Set k -> M.Map k a) 614 | {-# INLINE withoutKeys #-} 615 | 616 | partition :: forall k a. (a -> Bool) -> MonoidalMap k a -> (MonoidalMap k a, MonoidalMap k a) 617 | partition = coerce (M.partition :: (a -> Bool) -> M.Map k a -> (M.Map k a, M.Map k a)) 618 | {-# INLINE partition #-} 619 | 620 | partitionWithKey :: forall k a. (k -> a -> Bool) -> MonoidalMap k a -> (MonoidalMap k a, MonoidalMap k a) 621 | partitionWithKey = coerce (M.partitionWithKey :: (k -> a -> Bool) -> M.Map k a -> (M.Map k a, M.Map k a)) 622 | {-# INLINE partitionWithKey #-} 623 | 624 | takeWhileAntitone :: forall k a. (k -> Bool) -> MonoidalMap k a -> MonoidalMap k a 625 | takeWhileAntitone = coerce (M.takeWhileAntitone :: (k -> Bool) -> M.Map k a -> M.Map k a) 626 | {-# INLINE takeWhileAntitone #-} 627 | 628 | dropWhileAntitone :: forall k a. (k -> Bool) -> MonoidalMap k a -> MonoidalMap k a 629 | dropWhileAntitone = coerce (M.dropWhileAntitone :: (k -> Bool) -> M.Map k a -> M.Map k a) 630 | {-# INLINE dropWhileAntitone #-} 631 | 632 | spanAntitone :: forall k a. (k -> Bool) -> MonoidalMap k a -> (MonoidalMap k a, MonoidalMap k a) 633 | spanAntitone = coerce (M.spanAntitone :: (k -> Bool) -> M.Map k a -> (M.Map k a, M.Map k a)) 634 | {-# INLINE spanAntitone #-} 635 | 636 | mapMaybe :: forall k a b. (a -> Maybe b) -> MonoidalMap k a -> MonoidalMap k b 637 | mapMaybe = coerce (M.mapMaybe :: (a -> Maybe b) -> M.Map k a -> M.Map k b) 638 | {-# INLINE mapMaybe #-} 639 | 640 | mapMaybeWithKey :: forall k a b. (k -> a -> Maybe b) -> MonoidalMap k a -> MonoidalMap k b 641 | mapMaybeWithKey = coerce (M.mapMaybeWithKey :: (k -> a -> Maybe b) -> M.Map k a -> M.Map k b) 642 | {-# INLINE mapMaybeWithKey #-} 643 | 644 | mapEither :: forall k a b c. (a -> Either b c) -> MonoidalMap k a -> (MonoidalMap k b, MonoidalMap k c) 645 | mapEither = coerce (M.mapEither :: (a -> Either b c) -> M.Map k a -> (M.Map k b, M.Map k c)) 646 | {-# INLINE mapEither #-} 647 | 648 | mapEitherWithKey :: forall k a b c. (k -> a -> Either b c) -> MonoidalMap k a -> (MonoidalMap k b, MonoidalMap k c) 649 | mapEitherWithKey = coerce (M.mapEitherWithKey :: (k -> a -> Either b c) -> M.Map k a -> (M.Map k b, M.Map k c)) 650 | {-# INLINE mapEitherWithKey #-} 651 | 652 | split :: forall k a. Ord k => k -> MonoidalMap k a -> (MonoidalMap k a, MonoidalMap k a) 653 | split = coerce (M.split :: k -> M.Map k a -> (M.Map k a, M.Map k a)) 654 | {-# INLINE split #-} 655 | 656 | splitLookup :: forall k a. Ord k => k -> MonoidalMap k a -> (MonoidalMap k a, Maybe a, MonoidalMap k a) 657 | splitLookup = coerce (M.splitLookup :: k -> M.Map k a -> (M.Map k a, Maybe a, M.Map k a)) 658 | {-# INLINE splitLookup #-} 659 | 660 | splitRoot :: forall k a. MonoidalMap k a -> [MonoidalMap k a] 661 | splitRoot = coerce (M.splitRoot :: M.Map k a -> [M.Map k a]) 662 | {-# INLINE splitRoot #-} 663 | 664 | isSubmapOf :: forall k a. (Ord k, Eq a) => MonoidalMap k a -> MonoidalMap k a -> Bool 665 | isSubmapOf = coerce (M.isSubmapOf :: M.Map k a -> M.Map k a -> Bool) 666 | {-# INLINE isSubmapOf #-} 667 | 668 | isSubmapOfBy :: forall k a b. Ord k => (a -> b -> Bool) -> MonoidalMap k a -> MonoidalMap k b -> Bool 669 | isSubmapOfBy = coerce (M.isSubmapOfBy :: (a -> b -> Bool) -> M.Map k a -> M.Map k b -> Bool) 670 | {-# INLINE isSubmapOfBy #-} 671 | 672 | isProperSubmapOf :: forall k a. (Ord k, Eq a) => MonoidalMap k a -> MonoidalMap k a -> Bool 673 | isProperSubmapOf = coerce (M.isProperSubmapOf :: M.Map k a -> M.Map k a -> Bool) 674 | {-# INLINE isProperSubmapOf #-} 675 | 676 | isProperSubmapOfBy :: forall k a b. Ord k => (a -> b -> Bool) -> MonoidalMap k a -> MonoidalMap k b -> Bool 677 | isProperSubmapOfBy = coerce (M.isProperSubmapOfBy :: (a -> b -> Bool) -> M.Map k a -> M.Map k b -> Bool) 678 | {-# INLINE isProperSubmapOfBy #-} 679 | 680 | lookupIndex :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe Int 681 | lookupIndex = coerce (M.lookupIndex :: k -> M.Map k a -> Maybe Int) 682 | {-# INLINE lookupIndex #-} 683 | 684 | findIndex :: forall k a. Ord k => k -> MonoidalMap k a -> Int 685 | findIndex = coerce (M.findIndex :: k -> M.Map k a -> Int) 686 | {-# INLINE findIndex #-} 687 | 688 | elemAt :: forall k a. Int -> MonoidalMap k a -> (k, a) 689 | elemAt = coerce (M.elemAt :: Int -> M.Map k a -> (k, a)) 690 | {-# INLINE elemAt #-} 691 | 692 | updateAt :: forall k a. (k -> a -> Maybe a) -> Int -> MonoidalMap k a -> MonoidalMap k a 693 | updateAt = coerce (M.updateAt :: (k -> a -> Maybe a) -> Int -> M.Map k a -> M.Map k a) 694 | {-# INLINE updateAt #-} 695 | 696 | deleteAt :: forall k a. Int -> MonoidalMap k a -> MonoidalMap k a 697 | deleteAt = coerce (M.deleteAt :: Int -> M.Map k a -> M.Map k a) 698 | {-# INLINE deleteAt #-} 699 | 700 | take :: forall k a. Int -> MonoidalMap k a -> MonoidalMap k a 701 | take = coerce (M.take :: Int -> M.Map k a -> M.Map k a) 702 | {-# INLINE take #-} 703 | 704 | drop :: forall k a. Int -> MonoidalMap k a -> MonoidalMap k a 705 | drop = coerce (M.drop :: Int -> M.Map k a -> M.Map k a) 706 | {-# INLINE drop #-} 707 | 708 | splitAt :: forall k a. Int -> MonoidalMap k a -> (MonoidalMap k a, MonoidalMap k a) 709 | splitAt = coerce (M.splitAt :: Int -> M.Map k a -> (M.Map k a, M.Map k a)) 710 | {-# INLINE splitAt #-} 711 | 712 | lookupMin :: forall k a. MonoidalMap k a -> Maybe (k, a) 713 | lookupMin = coerce (M.lookupMin :: M.Map k a -> Maybe (k, a)) 714 | {-# INLINE lookupMin #-} 715 | 716 | lookupMax :: forall k a. MonoidalMap k a -> Maybe (k, a) 717 | lookupMax = coerce (M.lookupMax :: M.Map k a -> Maybe (k, a)) 718 | {-# INLINE lookupMax #-} 719 | 720 | findMin :: forall k a. MonoidalMap k a -> (k, a) 721 | findMin = coerce (M.findMin :: M.Map k a -> (k, a)) 722 | {-# INLINE findMin #-} 723 | 724 | findMax :: forall k a. MonoidalMap k a -> (k, a) 725 | findMax = coerce (M.findMax :: M.Map k a -> (k, a)) 726 | {-# INLINE findMax #-} 727 | 728 | deleteMin :: forall k a. MonoidalMap k a -> MonoidalMap k a 729 | deleteMin = coerce (M.deleteMin :: M.Map k a -> M.Map k a) 730 | {-# INLINE deleteMin #-} 731 | 732 | deleteMax :: forall k a. MonoidalMap k a -> MonoidalMap k a 733 | deleteMax = coerce (M.deleteMax :: M.Map k a -> M.Map k a) 734 | {-# INLINE deleteMax #-} 735 | 736 | deleteFindMin :: forall k a. MonoidalMap k a -> ((k, a), MonoidalMap k a) 737 | deleteFindMin = coerce (M.deleteFindMin :: M.Map k a -> ((k, a), M.Map k a)) 738 | {-# INLINE deleteFindMin #-} 739 | 740 | deleteFindMax :: forall k a. MonoidalMap k a -> ((k, a), MonoidalMap k a) 741 | deleteFindMax = coerce (M.deleteFindMax :: M.Map k a -> ((k, a), M.Map k a)) 742 | {-# INLINE deleteFindMax #-} 743 | 744 | updateMin :: forall k a. (a -> Maybe a) -> MonoidalMap k a -> MonoidalMap k a 745 | updateMin = coerce (M.updateMin :: (a -> Maybe a) -> M.Map k a -> M.Map k a) 746 | {-# INLINE updateMin #-} 747 | 748 | updateMax :: forall k a. (a -> Maybe a) -> MonoidalMap k a -> MonoidalMap k a 749 | updateMax = coerce (M.updateMax :: (a -> Maybe a) -> M.Map k a -> M.Map k a) 750 | {-# INLINE updateMax #-} 751 | 752 | updateMinWithKey :: forall k a. (k -> a -> Maybe a) -> MonoidalMap k a -> MonoidalMap k a 753 | updateMinWithKey = coerce (M.updateMinWithKey :: (k -> a -> Maybe a) -> M.Map k a -> M.Map k a) 754 | {-# INLINE updateMinWithKey #-} 755 | 756 | updateMaxWithKey :: forall k a. (k -> a -> Maybe a) -> MonoidalMap k a -> MonoidalMap k a 757 | updateMaxWithKey = coerce (M.updateMaxWithKey :: (k -> a -> Maybe a) -> M.Map k a -> M.Map k a) 758 | {-# INLINE updateMaxWithKey #-} 759 | 760 | minView :: forall k a. MonoidalMap k a -> Maybe (a, MonoidalMap k a) 761 | minView = coerce (M.minView :: M.Map k a -> Maybe (a, M.Map k a)) 762 | {-# INLINE minView #-} 763 | 764 | maxView :: forall k a. MonoidalMap k a -> Maybe (a, MonoidalMap k a) 765 | maxView = coerce (M.maxView :: M.Map k a -> Maybe (a, M.Map k a)) 766 | {-# INLINE maxView #-} 767 | 768 | minViewWithKey :: forall k a. MonoidalMap k a -> Maybe ((k, a), MonoidalMap k a) 769 | minViewWithKey = coerce (M.minViewWithKey :: M.Map k a -> Maybe ((k, a), M.Map k a)) 770 | {-# INLINE minViewWithKey #-} 771 | 772 | maxViewWithKey :: forall k a. MonoidalMap k a -> Maybe ((k, a), MonoidalMap k a) 773 | maxViewWithKey = coerce (M.maxViewWithKey :: M.Map k a -> Maybe ((k, a), M.Map k a)) 774 | {-# INLINE maxViewWithKey #-} 775 | 776 | -- showTree :: forall k a. (Show k, Show a) => MonoidalMap k a -> String 777 | -- showTree = coerce (M.showTree :: (Show k, Show a) => M.Map k a -> String) 778 | -- {-# INLINE showTree #-} 779 | 780 | -- showTreeWith :: forall k a. (k -> a -> String) -> Bool -> Bool -> MonoidalMap k a -> String 781 | -- showTreeWith = coerce (M.showTreeWith :: (k -> a -> String) -> Bool -> Bool -> M.Map k a -> String) 782 | -- {-# INLINE showTreeWith #-} 783 | 784 | valid :: forall k a. Ord k => MonoidalMap k a -> Bool 785 | valid = coerce (M.valid :: Ord k => M.Map k a -> Bool) 786 | {-# INLINE valid #-} 787 | -------------------------------------------------------------------------------- /src/Data/Map/Monoidal/Strict.hs: -------------------------------------------------------------------------------- 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.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 | , (\\) 34 | , adjust 35 | , adjustWithKey 36 | , alter 37 | , delete 38 | , deleteAt 39 | , take 40 | , drop 41 | , splitAt 42 | , lookupMin 43 | , lookupMax 44 | , deleteFindMax 45 | , deleteFindMin 46 | , deleteMax 47 | , deleteMin 48 | , difference 49 | , differenceWith 50 | , differenceWithKey 51 | , elemAt 52 | , empty 53 | , filter 54 | , filterWithKey 55 | , restrictKeys 56 | , withoutKeys 57 | , findIndex 58 | , findMax 59 | , findMin 60 | , foldMapWithKey 61 | , foldl 62 | , foldl' 63 | , foldlWithKey 64 | , foldlWithKey' 65 | , foldr 66 | , foldr' 67 | , foldrWithKey 68 | , foldrWithKey' 69 | , fromAscList 70 | , fromAscListWith 71 | , fromAscListWithKey 72 | , fromDistinctAscList 73 | , fromDistinctList 74 | , fromDescList 75 | , fromDescListWith 76 | , fromDescListWithKey 77 | , fromDistinctDescList 78 | , fromList 79 | , fromListWith 80 | , fromListWithKey 81 | , fromSet 82 | , insert 83 | , insertLookupWithKey 84 | , insertWith 85 | , insertWithKey 86 | , intersectionWith 87 | , intersectionWithKey 88 | , isProperSubmapOf 89 | , isProperSubmapOfBy 90 | , isSubmapOf 91 | , isSubmapOfBy 92 | , keysSet 93 | , lookup 94 | , lookupGE 95 | , lookupGT 96 | , lookupIndex 97 | , lookupLE 98 | , lookupLT 99 | , map 100 | , mapAccum 101 | , mapAccumRWithKey 102 | , mapAccumWithKey 103 | , mapEither 104 | , mapEitherWithKey 105 | , mapKeys 106 | , mapKeysMonotonic 107 | , mapKeysWith 108 | , mapMaybe 109 | , mapMaybeWithKey 110 | , mapWithKey 111 | , maxView 112 | , maxViewWithKey 113 | , mergeWithKey 114 | , minView 115 | , minViewWithKey 116 | , null 117 | , partition 118 | , partitionWithKey 119 | , takeWhileAntitone 120 | , dropWhileAntitone 121 | , spanAntitone 122 | , split 123 | , splitLookup 124 | , splitRoot 125 | , toAscList 126 | , toDescList 127 | , toList 128 | , traverseWithKey 129 | , traverseMaybeWithKey 130 | , unionWith 131 | , unionWithKey 132 | , unionsWith 133 | , update 134 | , updateAt 135 | , updateLookupWithKey 136 | , updateMax 137 | , updateMaxWithKey 138 | , updateMin 139 | , updateMinWithKey 140 | , updateWithKey 141 | , valid 142 | -- , showTree 143 | -- , showTreeWith 144 | ) where 145 | 146 | import Prelude hiding (Foldable(..), lookup, map, filter, take, drop, splitAt) 147 | 148 | import Data.Coerce (coerce) 149 | import Data.Set (Set) 150 | import Data.Semigroup 151 | import Data.Foldable (Foldable) 152 | import Data.Traversable (Traversable) 153 | import Control.Applicative (Applicative, pure) 154 | import Data.Data (Data) 155 | import Data.Typeable (Typeable) 156 | 157 | #if MIN_VERSION_base(4,7,0) 158 | import qualified GHC.Exts as IsList 159 | #endif 160 | 161 | import Control.DeepSeq 162 | import qualified Data.Map.Strict as M 163 | import Control.Lens 164 | import Control.Newtype 165 | import Data.Aeson(FromJSON, ToJSON, FromJSON1, ToJSON1) 166 | import Data.Functor.Classes 167 | import Data.Align 168 | #ifdef MIN_VERSION_semialign 169 | import Data.Semialign (Unalign) 170 | #if MIN_VERSION_semialign(1,1,0) 171 | import Data.Zip (Zip) 172 | #endif 173 | #endif 174 | import qualified Witherable 175 | 176 | -- | A 'Map' with monoidal accumulation 177 | newtype MonoidalMap k a = MonoidalMap { getMonoidalMap :: M.Map k a } 178 | deriving ( Show, Read, Functor, Eq, Ord, NFData 179 | , Foldable, Traversable 180 | , FromJSON, ToJSON, FromJSON1, ToJSON1 181 | , Data, Typeable, Align 182 | #if MIN_VERSION_these(0,8,0) 183 | , Semialign 184 | #endif 185 | #ifdef MIN_VERSION_semialign 186 | , Unalign 187 | #if MIN_VERSION_semialign(1,1,0) 188 | , Zip 189 | #endif 190 | #endif 191 | , Witherable.Filterable 192 | ) 193 | 194 | deriving instance (Ord k) => Eq1 (MonoidalMap k) 195 | deriving instance (Ord k) => Ord1 (MonoidalMap k) 196 | deriving instance (Show k) => Show1 (MonoidalMap k) 197 | 198 | type instance Index (MonoidalMap k a) = k 199 | type instance IxValue (MonoidalMap k a) = a 200 | instance Ord k => Ixed (MonoidalMap k a) where 201 | ix k f (MonoidalMap m) = case M.lookup k m of 202 | Just v -> f v <&> \v' -> MonoidalMap (M.insert k v' m) 203 | Nothing -> pure (MonoidalMap m) 204 | {-# INLINE ix #-} 205 | 206 | instance Ord k => At (MonoidalMap k a) where 207 | at k f (MonoidalMap m) = f mv <&> \r -> case r of 208 | Nothing -> maybe (MonoidalMap m) (const (MonoidalMap $ M.delete k m)) mv 209 | Just v' -> MonoidalMap $ M.insert k v' m 210 | where mv = M.lookup k m 211 | {-# INLINE at #-} 212 | 213 | instance Each (MonoidalMap k a) (MonoidalMap k b) a b 214 | 215 | instance FunctorWithIndex k (MonoidalMap k) 216 | instance FoldableWithIndex k (MonoidalMap k) 217 | instance TraversableWithIndex k (MonoidalMap k) where 218 | itraverse f (MonoidalMap m) = fmap MonoidalMap $ itraverse f m 219 | {-# INLINE itraverse #-} 220 | 221 | instance Ord k => TraverseMin k (MonoidalMap k) where 222 | traverseMin f (MonoidalMap m) = fmap MonoidalMap $ traverseMin f m 223 | {-# INLINE traverseMin #-} 224 | instance Ord k => TraverseMax k (MonoidalMap k) where 225 | traverseMax f (MonoidalMap m) = fmap MonoidalMap $ traverseMax f m 226 | {-# INLINE traverseMax #-} 227 | 228 | instance AsEmpty (MonoidalMap k a) where 229 | _Empty = nearly (MonoidalMap M.empty) (M.null . unpack) 230 | {-# INLINE _Empty #-} 231 | 232 | instance Wrapped (MonoidalMap k a) where 233 | type Unwrapped (MonoidalMap k a) = M.Map k a 234 | _Wrapped' = iso unpack pack 235 | {-# INLINE _Wrapped' #-} 236 | 237 | instance Ord k => Rewrapped (M.Map k a) (MonoidalMap k a) 238 | 239 | instance Ord k => Rewrapped (MonoidalMap k a) (M.Map k a) 240 | 241 | instance (Ord k, Semigroup a) => Semigroup (MonoidalMap k a) where 242 | MonoidalMap a <> MonoidalMap b = MonoidalMap $ M.unionWith (<>) a b 243 | {-# INLINE (<>) #-} 244 | 245 | instance (Ord k, Semigroup a) => Monoid (MonoidalMap k a) where 246 | mempty = MonoidalMap mempty 247 | {-# INLINE mempty #-} 248 | #if !(MIN_VERSION_base(4,11,0)) 249 | mappend (MonoidalMap a) (MonoidalMap b) = MonoidalMap $ M.unionWith (<>) a b 250 | {-# INLINE mappend #-} 251 | #endif 252 | 253 | instance Newtype (MonoidalMap k a) (M.Map k a) where 254 | pack = MonoidalMap 255 | {-# INLINE pack #-} 256 | unpack (MonoidalMap a) = a 257 | {-# INLINE unpack #-} 258 | 259 | #if MIN_VERSION_base(4,7,0) 260 | instance (Ord k, Semigroup a) => IsList.IsList (MonoidalMap k a) where 261 | type Item (MonoidalMap k a) = (k, a) 262 | fromList = MonoidalMap . M.fromListWith (<>) 263 | {-# INLINE fromList #-} 264 | toList = M.toList . unpack 265 | {-# INLINE toList #-} 266 | #endif 267 | 268 | instance Ord k => Witherable.Witherable (MonoidalMap k) 269 | 270 | -- | /O(1)/. A map with a single element. 271 | singleton :: k -> a -> MonoidalMap k a 272 | singleton k a = MonoidalMap $ M.singleton k a 273 | {-# INLINE singleton #-} 274 | 275 | -- | /O(1)/. The number of elements in the map. 276 | size :: MonoidalMap k a -> Int 277 | size = M.size . unpack 278 | {-# INLINE size #-} 279 | 280 | -- | /O(log n)/. Is the key a member of the map? See also 'notMember'. 281 | member :: Ord k => k -> MonoidalMap k a -> Bool 282 | member k = M.member k . unpack 283 | {-# INLINE member #-} 284 | 285 | -- | /O(log n)/. Is the key not a member of the map? See also 'member'. 286 | notMember :: Ord k => k -> MonoidalMap k a -> Bool 287 | notMember k = not . M.member k . unpack 288 | {-# INLINE notMember #-} 289 | 290 | -- | /O(log n)/. The expression @('findWithDefault' def k map)@ returns 291 | -- the value at key @k@ or returns default value @def@ 292 | -- when the key is not in the map. 293 | findWithDefault :: Ord k => a -> k -> MonoidalMap k a -> a 294 | findWithDefault def k = M.findWithDefault def k . unpack 295 | {-# INLINE findWithDefault #-} 296 | 297 | -- | /O(log n)/. Delete a key and its value from the map. When the key is not 298 | -- a member of the map, the original map is returned. 299 | delete :: Ord k => k -> MonoidalMap k a -> MonoidalMap k a 300 | delete k = _Wrapping' MonoidalMap %~ M.delete k 301 | {-# INLINE delete #-} 302 | 303 | -- | /O(n)/. Return all elements of the map and their keys 304 | assocs :: MonoidalMap k a -> [(k,a)] 305 | assocs = M.assocs . unpack 306 | {-# INLINE assocs #-} 307 | 308 | -- | /O(n)/. Return all elements of the map in the ascending order of their 309 | -- keys. Subject to list fusion. 310 | elems :: MonoidalMap k a -> [a] 311 | elems = M.elems . unpack 312 | {-# INLINE elems #-} 313 | 314 | -- | /O(n)/. Return all keys of the map in ascending order. Subject to list 315 | -- fusion. 316 | keys :: MonoidalMap k a -> [k] 317 | keys = M.keys . unpack 318 | {-# INLINE keys #-} 319 | 320 | (!?) :: forall k a. Ord k => MonoidalMap k a -> k -> Maybe a 321 | (!?) = coerce ((M.!?) :: M.Map k a -> k -> Maybe a) 322 | infixl 9 !? 323 | {-# INLINE (!?) #-} 324 | 325 | (!) :: forall k a. Ord k => MonoidalMap k a -> k -> a 326 | (!) = coerce ((M.!) :: M.Map k a -> k -> a) 327 | infixl 9 ! 328 | 329 | (\\) :: forall k a b. Ord k => MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k a 330 | (\\) = coerce ((M.\\) :: M.Map k a -> M.Map k b -> M.Map k a) 331 | infixl 9 \\ -- 332 | 333 | null :: forall k a. MonoidalMap k a -> Bool 334 | null = coerce (M.null :: M.Map k a -> Bool) 335 | {-# INLINE null #-} 336 | 337 | lookup :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe a 338 | lookup = coerce (M.lookup :: k -> M.Map k a -> Maybe a) 339 | {-# INLINE lookup #-} 340 | 341 | lookupLT :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe (k, a) 342 | lookupLT = coerce (M.lookupLT :: k -> M.Map k a -> Maybe (k,a)) 343 | {-# INLINE lookupLT #-} 344 | 345 | lookupGT :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe (k, a) 346 | lookupGT = coerce (M.lookupGT :: k -> M.Map k a -> Maybe (k,a)) 347 | {-# INLINE lookupGT #-} 348 | 349 | lookupLE :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe (k, a) 350 | lookupLE = coerce (M.lookupLE :: k -> M.Map k a -> Maybe (k,a)) 351 | {-# INLINE lookupLE #-} 352 | 353 | lookupGE :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe (k, a) 354 | lookupGE = coerce (M.lookupGE :: k -> M.Map k a -> Maybe (k,a)) 355 | {-# INLINE lookupGE #-} 356 | 357 | empty :: forall k a. MonoidalMap k a 358 | empty = coerce (M.empty :: M.Map k a) 359 | {-# INLINE empty #-} 360 | 361 | insert :: forall k a. Ord k => k -> a -> MonoidalMap k a -> MonoidalMap k a 362 | insert = coerce (M.insert :: k -> a -> M.Map k a -> M.Map k a) 363 | {-# INLINE insert #-} 364 | 365 | insertWith :: forall k a. Ord k => (a -> a -> a) -> k -> a -> MonoidalMap k a -> MonoidalMap k a 366 | insertWith = coerce (M.insertWith :: (a -> a -> a) -> k -> a -> M.Map k a -> M.Map k a) 367 | {-# INLINE insertWith #-} 368 | 369 | insertWithKey :: forall k a. Ord k => (k -> a -> a -> a) -> k -> a -> MonoidalMap k a -> MonoidalMap k a 370 | insertWithKey = coerce (M.insertWithKey :: (k -> a -> a -> a) -> k -> a -> M.Map k a -> M.Map k a) 371 | {-# INLINE insertWithKey #-} 372 | 373 | insertLookupWithKey :: forall k a. Ord k => (k -> a -> a -> a) -> k -> a -> MonoidalMap k a -> (Maybe a, MonoidalMap k a) 374 | insertLookupWithKey = coerce (M.insertLookupWithKey :: (k -> a -> a -> a) -> k -> a -> M.Map k a -> (Maybe a, M.Map k a)) 375 | {-# INLINE insertLookupWithKey #-} 376 | 377 | adjust :: forall k a. Ord k => (a -> a) -> k -> MonoidalMap k a -> MonoidalMap k a 378 | adjust = coerce (M.adjust :: (a -> a) -> k -> M.Map k a -> M.Map k a) 379 | {-# INLINE adjust #-} 380 | 381 | adjustWithKey :: forall k a. Ord k => (k -> a -> a) -> k -> MonoidalMap k a -> MonoidalMap k a 382 | adjustWithKey = coerce (M.adjustWithKey :: (k -> a -> a) -> k -> M.Map k a -> M.Map k a) 383 | {-# INLINE adjustWithKey #-} 384 | 385 | update :: forall k a. Ord k => (a -> Maybe a) -> k -> MonoidalMap k a -> MonoidalMap k a 386 | update = coerce (M.update :: (a -> Maybe a) -> k -> M.Map k a -> M.Map k a) 387 | {-# INLINE update #-} 388 | 389 | updateWithKey :: forall k a. Ord k => (k -> a -> Maybe a) -> k -> MonoidalMap k a -> MonoidalMap k a 390 | updateWithKey = coerce (M.updateWithKey :: (k -> a -> Maybe a) -> k -> M.Map k a -> M.Map k a) 391 | {-# INLINE updateWithKey #-} 392 | 393 | updateLookupWithKey :: forall k a. Ord k => (k -> a -> Maybe a) -> k -> MonoidalMap k a -> (Maybe a, MonoidalMap k a) 394 | updateLookupWithKey = coerce (M.updateLookupWithKey :: (k -> a -> Maybe a) -> k -> M.Map k a -> (Maybe a, M.Map k a)) 395 | {-# INLINE updateLookupWithKey #-} 396 | 397 | alter :: forall k a. Ord k => (Maybe a -> Maybe a) -> k -> MonoidalMap k a -> MonoidalMap k a 398 | alter = coerce (M.alter :: (Maybe a -> Maybe a) -> k -> M.Map k a -> M.Map k a) 399 | {-# INLINE alter #-} 400 | 401 | unionWith :: forall k a. Ord k => (a -> a -> a) -> MonoidalMap k a -> MonoidalMap k a -> MonoidalMap k a 402 | unionWith = coerce (M.unionWith :: (a -> a -> a) -> M.Map k a -> M.Map k a -> M.Map k a) 403 | {-# INLINE unionWith #-} 404 | 405 | unionWithKey :: forall k a. Ord k => (k -> a -> a -> a) -> MonoidalMap k a -> MonoidalMap k a -> MonoidalMap k a 406 | unionWithKey = coerce (M.unionWithKey :: (k -> a -> a -> a) -> M.Map k a -> M.Map k a -> M.Map k a) 407 | {-# INLINE unionWithKey #-} 408 | 409 | unionsWith :: forall k a. Ord k => (a -> a -> a) -> [MonoidalMap k a] -> MonoidalMap k a 410 | unionsWith = coerce (M.unionsWith :: (a -> a -> a) -> [M.Map k a] -> M.Map k a) 411 | {-# INLINE unionsWith #-} 412 | 413 | difference :: forall k a b. Ord k => MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k a 414 | difference = (\\) 415 | {-# INLINE difference #-} 416 | 417 | differenceWith :: forall k a b. Ord k => (a -> b -> Maybe a) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k a 418 | differenceWith = coerce (M.differenceWith :: (a -> b -> Maybe a) -> M.Map k a -> M.Map k b -> M.Map k a) 419 | {-# INLINE differenceWith #-} 420 | 421 | differenceWithKey :: forall k a b. Ord k => (k -> a -> b -> Maybe a) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k a 422 | differenceWithKey = coerce (M.differenceWithKey :: (k -> a -> b -> Maybe a) -> M.Map k a -> M.Map k b -> M.Map k a) 423 | {-# INLINE differenceWithKey #-} 424 | 425 | intersectionWith :: forall k a b c. Ord k => (a -> b -> c) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k c 426 | intersectionWith = coerce (M.intersectionWith :: (a -> b -> c) -> M.Map k a -> M.Map k b -> M.Map k c) 427 | {-# INLINE intersectionWith #-} 428 | 429 | intersectionWithKey :: forall k a b c. Ord k => (k -> a -> b -> c) -> MonoidalMap k a -> MonoidalMap k b -> MonoidalMap k c 430 | intersectionWithKey = coerce (M.intersectionWithKey :: (k -> a -> b -> c) -> M.Map k a -> M.Map k b -> M.Map k c) 431 | {-# INLINE intersectionWithKey #-} 432 | 433 | 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 434 | 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) 435 | {-# INLINE mergeWithKey #-} 436 | 437 | map :: (a -> b) -> MonoidalMap k a -> MonoidalMap k b 438 | map = fmap 439 | {-# INLINE map #-} 440 | 441 | mapWithKey :: forall k a b. (k -> a -> b) -> MonoidalMap k a -> MonoidalMap k b 442 | mapWithKey = coerce (M.mapWithKey :: (k -> a -> b) -> M.Map k a -> M.Map k b) 443 | {-# INLINE mapWithKey #-} 444 | 445 | traverseWithKey :: Applicative t => (k -> a -> t b) -> MonoidalMap k a -> t (MonoidalMap k b) 446 | traverseWithKey = itraverse 447 | {-# INLINE traverseWithKey #-} 448 | 449 | traverseMaybeWithKey :: forall f k a b. Applicative f => (k -> a -> f (Maybe b)) -> MonoidalMap k a -> f (MonoidalMap k b) 450 | traverseMaybeWithKey f m = coerce <$> M.traverseMaybeWithKey f (coerce m) 451 | {-# INLINE traverseMaybeWithKey #-} 452 | 453 | mapAccum :: forall k a b c. (a -> b -> (a, c)) -> a -> MonoidalMap k b -> (a, MonoidalMap k c) 454 | mapAccum = coerce (M.mapAccum :: (a -> b -> (a, c)) -> a -> M.Map k b -> (a, M.Map k c)) 455 | {-# INLINE mapAccum #-} 456 | 457 | mapAccumWithKey :: forall k a b c. (a -> k -> b -> (a, c)) -> a -> MonoidalMap k b -> (a, MonoidalMap k c) 458 | mapAccumWithKey = coerce (M.mapAccumWithKey :: (a -> k -> b -> (a, c)) -> a -> M.Map k b -> (a, M.Map k c)) 459 | {-# INLINE mapAccumWithKey #-} 460 | 461 | mapAccumRWithKey :: forall k a b c. (a -> k -> b -> (a, c)) -> a -> MonoidalMap k b -> (a, MonoidalMap k c) 462 | mapAccumRWithKey = coerce (M.mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> M.Map k b -> (a, M.Map k c)) 463 | {-# INLINE mapAccumRWithKey #-} 464 | 465 | mapKeys :: forall k1 k2 a. Ord k2 => (k1 -> k2) -> MonoidalMap k1 a -> MonoidalMap k2 a 466 | mapKeys = coerce (M.mapKeys :: (k1 -> k2) -> M.Map k1 a -> M.Map k2 a) 467 | {-# INLINE mapKeys #-} 468 | 469 | mapKeysWith :: forall k1 k2 a. Ord k2 => (a -> a -> a) -> (k1 -> k2) -> MonoidalMap k1 a -> MonoidalMap k2 a 470 | mapKeysWith = coerce (M.mapKeysWith :: (a -> a -> a) -> (k1 -> k2) -> M.Map k1 a -> M.Map k2 a) 471 | {-# INLINE mapKeysWith #-} 472 | 473 | -- | /O(n)/. 474 | -- @'mapKeysMonotonic' f s == 'mapKeys' f s@, but works only when @f@ 475 | -- is strictly increasing (both monotonic and injective). 476 | -- That is, for any values @x@ and @y@, if @x@ < @y@ then @f x@ < @f y@ 477 | -- and @f@ is injective (i.e. it never maps two input keys to the same output key). 478 | -- /The precondition is not checked./ 479 | -- Semi-formally, we have: 480 | -- 481 | -- > and [x < y ==> f x < f y | x <- ls, y <- ls] 482 | -- > ==> mapKeysMonotonic f s == mapKeys f s 483 | -- > where ls = keys s 484 | -- 485 | -- This means that @f@ maps distinct original keys to distinct resulting keys. 486 | -- This function has better performance than 'mapKeys'. 487 | -- 488 | -- > mapKeysMonotonic (\ k -> k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")] 489 | -- > valid (mapKeysMonotonic (\ k -> k * 2) (fromList [(5,"a"), (3,"b")])) == True 490 | -- > valid (mapKeysMonotonic (\ _ -> 1) (fromList [(5,"a"), (3,"b")])) == False 491 | mapKeysMonotonic :: forall k1 k2 a. (k1 -> k2) -> MonoidalMap k1 a -> MonoidalMap k2 a 492 | mapKeysMonotonic = coerce (M.mapKeysMonotonic :: (k1 -> k2) -> M.Map k1 a -> M.Map k2 a) 493 | {-# INLINE mapKeysMonotonic #-} 494 | 495 | foldr :: forall k a b. (a -> b -> b) -> b -> MonoidalMap k a -> b 496 | foldr = coerce (M.foldr :: (a -> b -> b) -> b -> M.Map k a -> b) 497 | {-# INLINE foldr #-} 498 | 499 | foldl :: forall k a b. (a -> b -> a) -> a -> MonoidalMap k b -> a 500 | foldl = coerce (M.foldl :: (a -> b -> a) -> a -> M.Map k b -> a) 501 | {-# INLINE foldl #-} 502 | 503 | foldrWithKey :: forall k a b. (k -> a -> b -> b) -> b -> MonoidalMap k a -> b 504 | foldrWithKey = coerce (M.foldrWithKey :: (k -> a -> b -> b) -> b -> M.Map k a -> b) 505 | {-# INLINE foldrWithKey #-} 506 | 507 | foldlWithKey :: forall k a b. (a -> k -> b -> a) -> a -> MonoidalMap k b -> a 508 | foldlWithKey = coerce (M.foldlWithKey :: (a -> k -> b -> a) -> a -> M.Map k b -> a) 509 | {-# INLINE foldlWithKey #-} 510 | 511 | foldMapWithKey :: forall k a m. Monoid m => (k -> a -> m) -> MonoidalMap k a -> m 512 | foldMapWithKey = coerce (M.foldMapWithKey :: Monoid m => (k -> a -> m) -> M.Map k a -> m) 513 | {-# INLINE foldMapWithKey #-} 514 | 515 | foldr' :: forall k a b. (a -> b -> b) -> b -> MonoidalMap k a -> b 516 | foldr' = coerce (M.foldr' :: (a -> b -> b) -> b -> M.Map k a -> b) 517 | {-# INLINE foldr' #-} 518 | 519 | foldl' :: forall k a b. (a -> b -> a) -> a -> MonoidalMap k b -> a 520 | foldl' = coerce (M.foldl' :: (a -> b -> a) -> a -> M.Map k b -> a) 521 | {-# INLINE foldl' #-} 522 | 523 | foldrWithKey' :: forall k a b. (k -> a -> b -> b) -> b -> MonoidalMap k a -> b 524 | foldrWithKey' = coerce (M.foldrWithKey' :: (k -> a -> b -> b) -> b -> M.Map k a -> b) 525 | {-# INLINE foldrWithKey' #-} 526 | 527 | foldlWithKey' :: forall k a b. (a -> k -> b -> a) -> a -> MonoidalMap k b -> a 528 | foldlWithKey' = coerce (M.foldlWithKey' :: (a -> k -> b -> a) -> a -> M.Map k b -> a) 529 | {-# INLINE foldlWithKey' #-} 530 | 531 | keysSet :: forall k a. MonoidalMap k a -> Set k 532 | keysSet = coerce (M.keysSet :: M.Map k a -> Set k) 533 | {-# INLINE keysSet #-} 534 | 535 | fromSet :: forall k a. (k -> a) -> Set k -> MonoidalMap k a 536 | fromSet = coerce (M.fromSet :: (k -> a) -> Set k -> M.Map k a) 537 | {-# INLINE fromSet #-} 538 | 539 | toList :: forall k a. MonoidalMap k a -> [(k, a)] 540 | toList = coerce (M.toList :: M.Map k a -> [(k, a)]) 541 | {-# INLINE toList #-} 542 | 543 | fromList :: forall k a. Ord k => [(k, a)] -> MonoidalMap k a 544 | fromList = coerce (M.fromList :: [(k, a)] -> M.Map k a) 545 | {-# INLINE fromList #-} 546 | 547 | fromListWith :: forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> MonoidalMap k a 548 | fromListWith = coerce (M.fromListWith :: (a -> a -> a) -> [(k, a)] -> M.Map k a) 549 | {-# INLINE fromListWith #-} 550 | 551 | fromListWithKey :: forall k a. Ord k => (k -> a -> a -> a) -> [(k, a)] -> MonoidalMap k a 552 | fromListWithKey = coerce (M.fromListWithKey :: (k -> a -> a -> a) -> [(k, a)] -> M.Map k a) 553 | {-# INLINE fromListWithKey #-} 554 | 555 | toAscList :: forall k a. MonoidalMap k a -> [(k, a)] 556 | toAscList = coerce (M.toAscList :: M.Map k a -> [(k, a)]) 557 | {-# INLINE toAscList #-} 558 | 559 | toDescList :: forall k a. MonoidalMap k a -> [(k, a)] 560 | toDescList = coerce (M.toDescList :: M.Map k a -> [(k, a)]) 561 | {-# INLINE toDescList #-} 562 | 563 | fromAscList :: forall k a. Eq k => [(k, a)] -> MonoidalMap k a 564 | fromAscList = coerce (M.fromAscList :: [(k, a)] -> M.Map k a) 565 | {-# INLINE fromAscList #-} 566 | 567 | fromAscListWith :: forall k a. Eq k => (a -> a -> a) -> [(k, a)] -> MonoidalMap k a 568 | fromAscListWith = coerce (M.fromAscListWith :: (a -> a -> a) -> [(k, a)] -> M.Map k a) 569 | {-# INLINE fromAscListWith #-} 570 | 571 | fromAscListWithKey :: forall k a. Eq k => (k -> a -> a -> a) -> [(k, a)] -> MonoidalMap k a 572 | fromAscListWithKey = coerce (M.fromAscListWithKey :: (k -> a -> a -> a) -> [(k, a)] -> M.Map k a) 573 | {-# INLINE fromAscListWithKey #-} 574 | 575 | fromDistinctAscList :: forall k a. [(k, a)] -> MonoidalMap k a 576 | fromDistinctAscList = coerce (M.fromDistinctAscList :: [(k, a)] -> M.Map k a) 577 | {-# INLINE fromDistinctAscList #-} 578 | 579 | fromDistinctList :: forall k a. Ord k => [(k, a)] -> MonoidalMap k a 580 | fromDistinctList = coerce (M.fromList :: [(k, a)] -> M.Map k a) 581 | {-# INLINE fromDistinctList #-} 582 | 583 | fromDescList :: forall k a. Eq k => [(k, a)] -> MonoidalMap k a 584 | fromDescList = coerce (M.fromDescList :: [(k, a)] -> M.Map k a) 585 | {-# INLINE fromDescList #-} 586 | 587 | fromDescListWith :: forall k a. Eq k => (a -> a -> a) -> [(k, a)] -> MonoidalMap k a 588 | fromDescListWith = coerce (M.fromDescListWith :: (a -> a -> a) -> [(k, a)] -> M.Map k a) 589 | {-# INLINE fromDescListWith #-} 590 | 591 | fromDescListWithKey :: forall k a. Eq k => (k -> a -> a -> a) -> [(k, a)] -> MonoidalMap k a 592 | fromDescListWithKey = coerce (M.fromDescListWithKey :: (k -> a -> a -> a) -> [(k, a)] -> M.Map k a) 593 | {-# INLINE fromDescListWithKey #-} 594 | 595 | fromDistinctDescList :: forall k a. [(k, a)] -> MonoidalMap k a 596 | fromDistinctDescList = coerce (M.fromDistinctDescList :: [(k, a)] -> M.Map k a) 597 | {-# INLINE fromDistinctDescList #-} 598 | 599 | filter :: forall k a. (a -> Bool) -> MonoidalMap k a -> MonoidalMap k a 600 | filter = coerce (M.filter :: (a -> Bool) -> M.Map k a -> M.Map k a) 601 | {-# INLINE filter #-} 602 | 603 | filterWithKey :: forall k a. (k -> a -> Bool) -> MonoidalMap k a -> MonoidalMap k a 604 | filterWithKey = coerce (M.filterWithKey :: (k -> a -> Bool) -> M.Map k a -> M.Map k a) 605 | {-# INLINE filterWithKey #-} 606 | 607 | restrictKeys :: forall k a. Ord k => MonoidalMap k a -> Set k -> MonoidalMap k a 608 | restrictKeys = coerce (M.restrictKeys :: M.Map k a -> Set k -> M.Map k a) 609 | {-# INLINE restrictKeys #-} 610 | 611 | withoutKeys :: forall k a. Ord k => MonoidalMap k a -> Set k -> MonoidalMap k a 612 | withoutKeys = coerce (M.withoutKeys :: M.Map k a -> Set k -> M.Map k a) 613 | {-# INLINE withoutKeys #-} 614 | 615 | partition :: forall k a. (a -> Bool) -> MonoidalMap k a -> (MonoidalMap k a, MonoidalMap k a) 616 | partition = coerce (M.partition :: (a -> Bool) -> M.Map k a -> (M.Map k a, M.Map k a)) 617 | {-# INLINE partition #-} 618 | 619 | partitionWithKey :: forall k a. (k -> a -> Bool) -> MonoidalMap k a -> (MonoidalMap k a, MonoidalMap k a) 620 | partitionWithKey = coerce (M.partitionWithKey :: (k -> a -> Bool) -> M.Map k a -> (M.Map k a, M.Map k a)) 621 | {-# INLINE partitionWithKey #-} 622 | 623 | takeWhileAntitone :: forall k a. (k -> Bool) -> MonoidalMap k a -> MonoidalMap k a 624 | takeWhileAntitone = coerce (M.takeWhileAntitone :: (k -> Bool) -> M.Map k a -> M.Map k a) 625 | {-# INLINE takeWhileAntitone #-} 626 | 627 | dropWhileAntitone :: forall k a. (k -> Bool) -> MonoidalMap k a -> MonoidalMap k a 628 | dropWhileAntitone = coerce (M.dropWhileAntitone :: (k -> Bool) -> M.Map k a -> M.Map k a) 629 | {-# INLINE dropWhileAntitone #-} 630 | 631 | spanAntitone :: forall k a. (k -> Bool) -> MonoidalMap k a -> (MonoidalMap k a, MonoidalMap k a) 632 | spanAntitone = coerce (M.spanAntitone :: (k -> Bool) -> M.Map k a -> (M.Map k a, M.Map k a)) 633 | {-# INLINE spanAntitone #-} 634 | 635 | mapMaybe :: forall k a b. (a -> Maybe b) -> MonoidalMap k a -> MonoidalMap k b 636 | mapMaybe = coerce (M.mapMaybe :: (a -> Maybe b) -> M.Map k a -> M.Map k b) 637 | {-# INLINE mapMaybe #-} 638 | 639 | mapMaybeWithKey :: forall k a b. (k -> a -> Maybe b) -> MonoidalMap k a -> MonoidalMap k b 640 | mapMaybeWithKey = coerce (M.mapMaybeWithKey :: (k -> a -> Maybe b) -> M.Map k a -> M.Map k b) 641 | {-# INLINE mapMaybeWithKey #-} 642 | 643 | mapEither :: forall k a b c. (a -> Either b c) -> MonoidalMap k a -> (MonoidalMap k b, MonoidalMap k c) 644 | mapEither = coerce (M.mapEither :: (a -> Either b c) -> M.Map k a -> (M.Map k b, M.Map k c)) 645 | {-# INLINE mapEither #-} 646 | 647 | mapEitherWithKey :: forall k a b c. (k -> a -> Either b c) -> MonoidalMap k a -> (MonoidalMap k b, MonoidalMap k c) 648 | mapEitherWithKey = coerce (M.mapEitherWithKey :: (k -> a -> Either b c) -> M.Map k a -> (M.Map k b, M.Map k c)) 649 | {-# INLINE mapEitherWithKey #-} 650 | 651 | split :: forall k a. Ord k => k -> MonoidalMap k a -> (MonoidalMap k a, MonoidalMap k a) 652 | split = coerce (M.split :: k -> M.Map k a -> (M.Map k a, M.Map k a)) 653 | {-# INLINE split #-} 654 | 655 | splitLookup :: forall k a. Ord k => k -> MonoidalMap k a -> (MonoidalMap k a, Maybe a, MonoidalMap k a) 656 | splitLookup = coerce (M.splitLookup :: k -> M.Map k a -> (M.Map k a, Maybe a, M.Map k a)) 657 | {-# INLINE splitLookup #-} 658 | 659 | splitRoot :: forall k a. MonoidalMap k a -> [MonoidalMap k a] 660 | splitRoot = coerce (M.splitRoot :: M.Map k a -> [M.Map k a]) 661 | {-# INLINE splitRoot #-} 662 | 663 | isSubmapOf :: forall k a. (Ord k, Eq a) => MonoidalMap k a -> MonoidalMap k a -> Bool 664 | isSubmapOf = coerce (M.isSubmapOf :: M.Map k a -> M.Map k a -> Bool) 665 | {-# INLINE isSubmapOf #-} 666 | 667 | isSubmapOfBy :: forall k a b. Ord k => (a -> b -> Bool) -> MonoidalMap k a -> MonoidalMap k b -> Bool 668 | isSubmapOfBy = coerce (M.isSubmapOfBy :: (a -> b -> Bool) -> M.Map k a -> M.Map k b -> Bool) 669 | {-# INLINE isSubmapOfBy #-} 670 | 671 | isProperSubmapOf :: forall k a. (Ord k, Eq a) => MonoidalMap k a -> MonoidalMap k a -> Bool 672 | isProperSubmapOf = coerce (M.isProperSubmapOf :: M.Map k a -> M.Map k a -> Bool) 673 | {-# INLINE isProperSubmapOf #-} 674 | 675 | isProperSubmapOfBy :: forall k a b. Ord k => (a -> b -> Bool) -> MonoidalMap k a -> MonoidalMap k b -> Bool 676 | isProperSubmapOfBy = coerce (M.isProperSubmapOfBy :: (a -> b -> Bool) -> M.Map k a -> M.Map k b -> Bool) 677 | {-# INLINE isProperSubmapOfBy #-} 678 | 679 | lookupIndex :: forall k a. Ord k => k -> MonoidalMap k a -> Maybe Int 680 | lookupIndex = coerce (M.lookupIndex :: k -> M.Map k a -> Maybe Int) 681 | {-# INLINE lookupIndex #-} 682 | 683 | findIndex :: forall k a. Ord k => k -> MonoidalMap k a -> Int 684 | findIndex = coerce (M.findIndex :: k -> M.Map k a -> Int) 685 | {-# INLINE findIndex #-} 686 | 687 | elemAt :: forall k a. Int -> MonoidalMap k a -> (k, a) 688 | elemAt = coerce (M.elemAt :: Int -> M.Map k a -> (k, a)) 689 | {-# INLINE elemAt #-} 690 | 691 | updateAt :: forall k a. (k -> a -> Maybe a) -> Int -> MonoidalMap k a -> MonoidalMap k a 692 | updateAt = coerce (M.updateAt :: (k -> a -> Maybe a) -> Int -> M.Map k a -> M.Map k a) 693 | {-# INLINE updateAt #-} 694 | 695 | deleteAt :: forall k a. Int -> MonoidalMap k a -> MonoidalMap k a 696 | deleteAt = coerce (M.deleteAt :: Int -> M.Map k a -> M.Map k a) 697 | {-# INLINE deleteAt #-} 698 | 699 | take :: forall k a. Int -> MonoidalMap k a -> MonoidalMap k a 700 | take = coerce (M.take :: Int -> M.Map k a -> M.Map k a) 701 | {-# INLINE take #-} 702 | 703 | drop :: forall k a. Int -> MonoidalMap k a -> MonoidalMap k a 704 | drop = coerce (M.drop :: Int -> M.Map k a -> M.Map k a) 705 | {-# INLINE drop #-} 706 | 707 | splitAt :: forall k a. Int -> MonoidalMap k a -> (MonoidalMap k a, MonoidalMap k a) 708 | splitAt = coerce (M.splitAt :: Int -> M.Map k a -> (M.Map k a, M.Map k a)) 709 | {-# INLINE splitAt #-} 710 | 711 | lookupMin :: forall k a. MonoidalMap k a -> Maybe (k, a) 712 | lookupMin = coerce (M.lookupMin :: M.Map k a -> Maybe (k, a)) 713 | {-# INLINE lookupMin #-} 714 | 715 | lookupMax :: forall k a. MonoidalMap k a -> Maybe (k, a) 716 | lookupMax = coerce (M.lookupMax :: M.Map k a -> Maybe (k, a)) 717 | {-# INLINE lookupMax #-} 718 | 719 | findMin :: forall k a. MonoidalMap k a -> (k, a) 720 | findMin = coerce (M.findMin :: M.Map k a -> (k, a)) 721 | {-# INLINE findMin #-} 722 | 723 | findMax :: forall k a. MonoidalMap k a -> (k, a) 724 | findMax = coerce (M.findMax :: M.Map k a -> (k, a)) 725 | {-# INLINE findMax #-} 726 | 727 | deleteMin :: forall k a. MonoidalMap k a -> MonoidalMap k a 728 | deleteMin = coerce (M.deleteMin :: M.Map k a -> M.Map k a) 729 | {-# INLINE deleteMin #-} 730 | 731 | deleteMax :: forall k a. MonoidalMap k a -> MonoidalMap k a 732 | deleteMax = coerce (M.deleteMax :: M.Map k a -> M.Map k a) 733 | {-# INLINE deleteMax #-} 734 | 735 | deleteFindMin :: forall k a. MonoidalMap k a -> ((k, a), MonoidalMap k a) 736 | deleteFindMin = coerce (M.deleteFindMin :: M.Map k a -> ((k, a), M.Map k a)) 737 | {-# INLINE deleteFindMin #-} 738 | 739 | deleteFindMax :: forall k a. MonoidalMap k a -> ((k, a), MonoidalMap k a) 740 | deleteFindMax = coerce (M.deleteFindMax :: M.Map k a -> ((k, a), M.Map k a)) 741 | {-# INLINE deleteFindMax #-} 742 | 743 | updateMin :: forall k a. (a -> Maybe a) -> MonoidalMap k a -> MonoidalMap k a 744 | updateMin = coerce (M.updateMin :: (a -> Maybe a) -> M.Map k a -> M.Map k a) 745 | {-# INLINE updateMin #-} 746 | 747 | updateMax :: forall k a. (a -> Maybe a) -> MonoidalMap k a -> MonoidalMap k a 748 | updateMax = coerce (M.updateMax :: (a -> Maybe a) -> M.Map k a -> M.Map k a) 749 | {-# INLINE updateMax #-} 750 | 751 | updateMinWithKey :: forall k a. (k -> a -> Maybe a) -> MonoidalMap k a -> MonoidalMap k a 752 | updateMinWithKey = coerce (M.updateMinWithKey :: (k -> a -> Maybe a) -> M.Map k a -> M.Map k a) 753 | {-# INLINE updateMinWithKey #-} 754 | 755 | updateMaxWithKey :: forall k a. (k -> a -> Maybe a) -> MonoidalMap k a -> MonoidalMap k a 756 | updateMaxWithKey = coerce (M.updateMaxWithKey :: (k -> a -> Maybe a) -> M.Map k a -> M.Map k a) 757 | {-# INLINE updateMaxWithKey #-} 758 | 759 | minView :: forall k a. MonoidalMap k a -> Maybe (a, MonoidalMap k a) 760 | minView = coerce (M.minView :: M.Map k a -> Maybe (a, M.Map k a)) 761 | {-# INLINE minView #-} 762 | 763 | maxView :: forall k a. MonoidalMap k a -> Maybe (a, MonoidalMap k a) 764 | maxView = coerce (M.maxView :: M.Map k a -> Maybe (a, M.Map k a)) 765 | {-# INLINE maxView #-} 766 | 767 | minViewWithKey :: forall k a. MonoidalMap k a -> Maybe ((k, a), MonoidalMap k a) 768 | minViewWithKey = coerce (M.minViewWithKey :: M.Map k a -> Maybe ((k, a), M.Map k a)) 769 | {-# INLINE minViewWithKey #-} 770 | 771 | maxViewWithKey :: forall k a. MonoidalMap k a -> Maybe ((k, a), MonoidalMap k a) 772 | maxViewWithKey = coerce (M.maxViewWithKey :: M.Map k a -> Maybe ((k, a), M.Map k a)) 773 | {-# INLINE maxViewWithKey #-} 774 | 775 | -- showTree :: forall k a. (Show k, Show a) => MonoidalMap k a -> String 776 | -- showTree = coerce (M.showTree :: (Show k, Show a) => M.Map k a -> String) 777 | -- {-# INLINE showTree #-} 778 | 779 | -- showTreeWith :: forall k a. (k -> a -> String) -> Bool -> Bool -> MonoidalMap k a -> String 780 | -- showTreeWith = coerce (M.showTreeWith :: (k -> a -> String) -> Bool -> Bool -> M.Map k a -> String) 781 | -- {-# INLINE showTreeWith #-} 782 | 783 | valid :: forall k a. Ord k => MonoidalMap k a -> Bool 784 | valid = coerce (M.valid :: Ord k => M.Map k a -> Bool) 785 | {-# INLINE valid #-} 786 | --------------------------------------------------------------------------------