{-# LANGUAGE Safe #-}

-- Copyright (c) 2014, Eric Mertens
--
-- All rights reserved.
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions are met:
--
--     * Redistributions of source code must retain the above copyright
--       notice, this list of conditions and the following disclaimer.
--
--     * Redistributions in binary form must reproduce the above
--       copyright notice, this list of conditions and the following
--       disclaimer in the documentation and/or other materials provided
--       with the distribution.
--
--     * Neither the name of Eric Mertens nor the names of other
--       contributors may be used to endorse or promote products derived
--       from this software without specific prior written permission.
--
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

{- |

This module implements an interface for working with maps.

For primitive types, like 'Int', the library automatically selects
an efficient implementation (e.g., an "IntMap").

For complex structured types, the library uses an implementation
based on tries: this is useful when using large and similar keys where
comparing for order may become expensive, and storing the distinct
keys would be inefficient.

The 'OrdKey' type allows for maps with complex keys,
where the keys are compared based on order, rather than using the
trie implementation.

All methods of 'TrieKey' can be derived automatically using
a 'GHC.Generics.Generic' instance.

@
data Demo = DemoC1 'Int' | DemoC2 'Int' 'Char'  deriving 'GHC.Generics.Generic'

instance 'TrieKey' Demo
@

-}

module Data.GenericTrie
  (
  -- * Trie interface
    Trie
  , TrieKey
  , ShowTrieKey

  -- ** Construction
  , empty
  , singleton
  , fromList
  , fromListWith
  , fromListWith'

  -- ** Updates
  , alter
  , insert
  , insertWith
  , insertWith'
  , delete
  , at

  -- ** Queries
  , member
  , notMember
  , null
  , lookup

  -- ** Folding
  , foldWithKey
  , fold
  , toList

  -- ** Traversing
  , traverseWithKey
  , traverseMaybeWithKey
  , mapMaybe
  , mapMaybeWithKey
  , filter
  , filterWithKey

  -- ** Combining maps
  , union
  , unionWith
  , unionWithKey
  , intersection
  , intersectionWith
  , intersectionWithKey
  , difference
  , differenceWith
  , differenceWithKey

  -- * Keys using 'Ord'
  , OrdKey(..)
  ) where

import Control.Applicative (Applicative)
import Data.List (foldl')
import Data.Maybe (isNothing, isJust)
import Prelude hiding (lookup, null, filter)

import Data.GenericTrie.Internal

------------------------------------------------------------------------------
-- Various helpers
------------------------------------------------------------------------------

-- | Construct a trie from a list of key-value pairs
fromList :: TrieKey k => [(k,v)] -> Trie k v
fromList :: [(k, v)] -> Trie k v
fromList = (Trie k v -> (k, v) -> Trie k v)
-> Trie k v -> [(k, v)] -> Trie k v
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Trie k v
acc (k
k,v
v) -> k -> v -> Trie k v -> Trie k v
forall k a. TrieKey k => k -> a -> Trie k a -> Trie k a
insert k
k v
v Trie k v
acc) Trie k v
forall k a. TrieKey k => Trie k a
empty

-- | Construct a trie from a list of key-value pairs.
-- The given function is used to combine values at the
-- same key.
fromListWith :: TrieKey k => (v -> v -> v) -> [(k,v)] -> Trie k v
fromListWith :: (v -> v -> v) -> [(k, v)] -> Trie k v
fromListWith v -> v -> v
f = (Trie k v -> (k, v) -> Trie k v)
-> Trie k v -> [(k, v)] -> Trie k v
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Trie k v
acc (k
k,v
v) -> (v -> v -> v) -> k -> v -> Trie k v -> Trie k v
forall k v.
TrieKey k =>
(v -> v -> v) -> k -> v -> Trie k v -> Trie k v
insertWith v -> v -> v
f k
k v
v Trie k v
acc) Trie k v
forall k a. TrieKey k => Trie k a
empty

-- | Version of 'fromListWith' which is strict in the result of
-- the combining function.
fromListWith' :: TrieKey k => (v -> v -> v) -> [(k,v)] -> Trie k v
fromListWith' :: (v -> v -> v) -> [(k, v)] -> Trie k v
fromListWith' v -> v -> v
f = (Trie k v -> (k, v) -> Trie k v)
-> Trie k v -> [(k, v)] -> Trie k v
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Trie k v
acc (k
k,v
v) -> (v -> v -> v) -> k -> v -> Trie k v -> Trie k v
forall k v.
TrieKey k =>
(v -> v -> v) -> k -> v -> Trie k v -> Trie k v
insertWith' v -> v -> v
f k
k v
v Trie k v
acc) Trie k v
forall k a. TrieKey k => Trie k a
empty

-- | Construct an empty trie
empty :: TrieKey k => Trie k a
empty :: Trie k a
empty = Trie k a
forall k a. TrieKey k => Trie k a
trieEmpty
{-# INLINE empty #-}

-- | Test for an empty trie
null :: TrieKey k => Trie k a -> Bool
null :: Trie k a -> Bool
null = Trie k a -> Bool
forall k a. TrieKey k => Trie k a -> Bool
trieNull
{-# INLINE null #-}

-- | Lookup an element from a trie
lookup :: TrieKey k => k -> Trie k a -> Maybe a
lookup :: k -> Trie k a -> Maybe a
lookup = k -> Trie k a -> Maybe a
forall k a. TrieKey k => k -> Trie k a -> Maybe a
trieLookup
{-# INLINE lookup #-}

-- | Lens for the value at a given key
at :: (Functor f, TrieKey k) => k -> (Maybe a -> f (Maybe a)) -> Trie k a -> f (Trie k a)
at :: k -> (Maybe a -> f (Maybe a)) -> Trie k a -> f (Trie k a)
at k
k Maybe a -> f (Maybe a)
f Trie k a
m = (Maybe a -> Trie k a) -> f (Maybe a) -> f (Trie k a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Maybe a -> Trie k a
aux (Maybe a -> f (Maybe a)
f Maybe a
mv)
  where
  mv :: Maybe a
mv = k -> Trie k a -> Maybe a
forall k a. TrieKey k => k -> Trie k a -> Maybe a
lookup k
k Trie k a
m
  aux :: Maybe a -> Trie k a
aux Maybe a
r = case Maybe a
r of
    Maybe a
Nothing -> Trie k a -> (a -> Trie k a) -> Maybe a -> Trie k a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Trie k a
m (Trie k a -> a -> Trie k a
forall a b. a -> b -> a
const (k -> Trie k a -> Trie k a
forall k a. TrieKey k => k -> Trie k a -> Trie k a
delete k
k Trie k a
m)) Maybe a
mv
    Just a
v' -> k -> a -> Trie k a -> Trie k a
forall k a. TrieKey k => k -> a -> Trie k a -> Trie k a
insert k
k a
v' Trie k a
m

-- | Insert an element into a trie
insert :: TrieKey k => k -> a -> Trie k a -> Trie k a
insert :: k -> a -> Trie k a -> Trie k a
insert = k -> a -> Trie k a -> Trie k a
forall k a. TrieKey k => k -> a -> Trie k a -> Trie k a
trieInsert
{-# INLINE insert #-}

-- | Delete an element from a trie
delete :: TrieKey k => k -> Trie k a -> Trie k a
delete :: k -> Trie k a -> Trie k a
delete = k -> Trie k a -> Trie k a
forall k a. TrieKey k => k -> Trie k a -> Trie k a
trieDelete
{-# INLINE delete #-}

-- | Construct a trie holding a single value
singleton :: TrieKey k => k -> a -> Trie k a
singleton :: k -> a -> Trie k a
singleton = k -> a -> Trie k a
forall k a. TrieKey k => k -> a -> Trie k a
trieSingleton
{-# INLINE singleton #-}

-- | Apply a function to the values of a trie and keep the elements
-- of the trie that result in a 'Just' value.
mapMaybeWithKey :: TrieKey k => (k -> a -> Maybe b) -> Trie k a -> Trie k b
mapMaybeWithKey :: (k -> a -> Maybe b) -> Trie k a -> Trie k b
mapMaybeWithKey = (k -> a -> Maybe b) -> Trie k a -> Trie k b
forall k a b.
TrieKey k =>
(k -> a -> Maybe b) -> Trie k a -> Trie k b
trieMapMaybeWithKey
{-# INLINE mapMaybeWithKey #-}

-- | Perform an action for each value in a trie and keep the elements
-- of the trie that result in a 'Just' value.
traverseMaybeWithKey :: (TrieKey k, Applicative f)
                     => (k -> a -> f (Maybe b)) -> Trie k a -> f (Trie k b)
traverseMaybeWithKey :: (k -> a -> f (Maybe b)) -> Trie k a -> f (Trie k b)
traverseMaybeWithKey = (k -> a -> f (Maybe b)) -> Trie k a -> f (Trie k b)
forall k (f :: * -> *) a b.
(TrieKey k, Applicative f) =>
(k -> a -> f (Maybe b)) -> Trie k a -> f (Trie k b)
trieTraverseMaybeWithKey
{-# INLINE traverseMaybeWithKey #-}

-- | Filter the values of a trie with the given predicate.
filter :: TrieKey k => (a -> Bool) -> Trie k a -> Trie k a
filter :: (a -> Bool) -> Trie k a -> Trie k a
filter a -> Bool
p = (k -> a -> Bool) -> Trie k a -> Trie k a
forall k a. TrieKey k => (k -> a -> Bool) -> Trie k a -> Trie k a
filterWithKey ((a -> Bool) -> k -> a -> Bool
forall a b. a -> b -> a
const a -> Bool
p)

-- | Version of 'filter' where the predicate also gets the key.
filterWithKey :: TrieKey k => (k -> a -> Bool) -> Trie k a -> Trie k a
filterWithKey :: (k -> a -> Bool) -> Trie k a -> Trie k a
filterWithKey k -> a -> Bool
p = (k -> a -> Maybe a) -> Trie k a -> Trie k a
forall k a b.
TrieKey k =>
(k -> a -> Maybe b) -> Trie k a -> Trie k b
mapMaybeWithKey k -> a -> Maybe a
aux
  where
  aux :: k -> a -> Maybe a
aux k
k a
x
    | k -> a -> Bool
p k
k a
x     = a -> Maybe a
forall a. a -> Maybe a
Just a
x
    | Bool
otherwise = Maybe a
forall a. Maybe a
Nothing


-- | Fold a trie with a function of the value
fold :: TrieKey k => (a -> r -> r) -> r -> Trie k a -> r
fold :: (a -> r -> r) -> r -> Trie k a -> r
fold = (k -> a -> r -> r) -> r -> Trie k a -> r
forall k a r. TrieKey k => (k -> a -> r -> r) -> r -> Trie k a -> r
trieFoldWithKey ((k -> a -> r -> r) -> r -> Trie k a -> r)
-> ((a -> r -> r) -> k -> a -> r -> r)
-> (a -> r -> r)
-> r
-> Trie k a
-> r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> r -> r) -> k -> a -> r -> r
forall a b. a -> b -> a
const
{-# INLINE fold #-}

-- | Fold a trie with a function of both key and value
foldWithKey :: TrieKey k => (k -> a -> r -> r) -> r -> Trie k a -> r
foldWithKey :: (k -> a -> r -> r) -> r -> Trie k a -> r
foldWithKey = (k -> a -> r -> r) -> r -> Trie k a -> r
forall k a r. TrieKey k => (k -> a -> r -> r) -> r -> Trie k a -> r
trieFoldWithKey
{-# INLINE foldWithKey #-}

-- | Traverse a trie with a function of both key and value
traverseWithKey :: (TrieKey k, Applicative f) => (k -> a -> f b) -> Trie k a -> f (Trie k b)
traverseWithKey :: (k -> a -> f b) -> Trie k a -> f (Trie k b)
traverseWithKey = (k -> a -> f b) -> Trie k a -> f (Trie k b)
forall k (f :: * -> *) a b.
(TrieKey k, Applicative f) =>
(k -> a -> f b) -> Trie k a -> f (Trie k b)
trieTraverseWithKey
{-# INLINE traverseWithKey #-}

mergeWithKey ::
  TrieKey k =>
  (k -> a -> b -> Maybe c) ->
  (Trie k a -> Trie k c) ->
  (Trie k b -> Trie k c) ->
  Trie k a -> Trie k b -> Trie k c
mergeWithKey :: (k -> a -> b -> Maybe c)
-> (Trie k a -> Trie k c)
-> (Trie k b -> Trie k c)
-> Trie k a
-> Trie k b
-> Trie k c
mergeWithKey = (k -> a -> b -> Maybe c)
-> (Trie k a -> Trie k c)
-> (Trie k b -> Trie k c)
-> Trie k a
-> Trie k b
-> Trie k c
forall k a b c.
TrieKey k =>
(k -> a -> b -> Maybe c)
-> (Trie k a -> Trie k c)
-> (Trie k b -> Trie k c)
-> Trie k a
-> Trie k b
-> Trie k c
trieMergeWithKey
{-# INLINE mergeWithKey #-}

-- | Alter the value at the given key location.
-- The parameter function takes the value stored
-- at the given key, if one exists, and should return a value to insert at
-- that location, or 'Nothing' to delete from that location.
alter :: TrieKey k => k -> (Maybe a -> Maybe a) -> Trie k a -> Trie k a
alter :: k -> (Maybe a -> Maybe a) -> Trie k a -> Trie k a
alter k
k Maybe a -> Maybe a
f Trie k a
t =
  case Maybe a -> Maybe a
f (k -> Trie k a -> Maybe a
forall k a. TrieKey k => k -> Trie k a -> Maybe a
lookup k
k Trie k a
t) of
    Just a
v' -> k -> a -> Trie k a -> Trie k a
forall k a. TrieKey k => k -> a -> Trie k a -> Trie k a
insert k
k a
v' Trie k a
t
    Maybe a
Nothing -> k -> Trie k a -> Trie k a
forall k a. TrieKey k => k -> Trie k a -> Trie k a
delete k
k Trie k a
t

-- | Insert a value at the given key. The combining function is used
-- when a value is already stored at that key. The new value is the
-- first argument to the combining function.
insertWith :: TrieKey k => (v -> v -> v) -> k -> v -> Trie k v -> Trie k v
insertWith :: (v -> v -> v) -> k -> v -> Trie k v -> Trie k v
insertWith v -> v -> v
f k
k v
v = k -> (Maybe v -> Maybe v) -> Trie k v -> Trie k v
forall k a.
TrieKey k =>
k -> (Maybe a -> Maybe a) -> Trie k a -> Trie k a
alter k
k ((Maybe v -> Maybe v) -> Trie k v -> Trie k v)
-> (Maybe v -> Maybe v) -> Trie k v -> Trie k v
forall a b. (a -> b) -> a -> b
$ \Maybe v
mb ->
                      case Maybe v
mb of
                        Just v
v0 -> v -> Maybe v
forall a. a -> Maybe a
Just (v -> v -> v
f v
v v
v0)
                        Maybe v
Nothing -> v -> Maybe v
forall a. a -> Maybe a
Just v
v

-- | Version of 'insertWith' that is strict in the result of combining
-- two elements.
insertWith' :: TrieKey k => (v -> v -> v) -> k -> v -> Trie k v -> Trie k v
insertWith' :: (v -> v -> v) -> k -> v -> Trie k v -> Trie k v
insertWith' v -> v -> v
f k
k v
v = k -> (Maybe v -> Maybe v) -> Trie k v -> Trie k v
forall k a.
TrieKey k =>
k -> (Maybe a -> Maybe a) -> Trie k a -> Trie k a
alter k
k ((Maybe v -> Maybe v) -> Trie k v -> Trie k v)
-> (Maybe v -> Maybe v) -> Trie k v -> Trie k v
forall a b. (a -> b) -> a -> b
$ \Maybe v
mb ->
                      case Maybe v
mb of
                        Just v
v0 -> v -> Maybe v
forall a. a -> Maybe a
Just (v -> Maybe v) -> v -> Maybe v
forall a b. (a -> b) -> a -> b
$! v -> v -> v
f v
v v
v0
                        Maybe v
Nothing -> v -> Maybe v
forall a. a -> Maybe a
Just v
v

-- | Returns 'True' when the 'Trie' has a value stored at the given key.
member :: TrieKey k => k -> Trie k a -> Bool
member :: k -> Trie k a -> Bool
member k
k Trie k a
t = Maybe a -> Bool
forall a. Maybe a -> Bool
isJust (k -> Trie k a -> Maybe a
forall k a. TrieKey k => k -> Trie k a -> Maybe a
lookup k
k Trie k a
t)

-- | Returns 'False' when the 'Trie' has a value stored at the given key.
notMember :: TrieKey k => k -> Trie k a -> Bool
notMember :: k -> Trie k a -> Bool
notMember k
k Trie k a
t = Maybe a -> Bool
forall a. Maybe a -> Bool
isNothing (k -> Trie k a -> Maybe a
forall k a. TrieKey k => k -> Trie k a -> Maybe a
lookup k
k Trie k a
t)

-- | Transform a trie to an association list.
toList :: TrieKey k => Trie k a -> [(k,a)]
toList :: Trie k a -> [(k, a)]
toList = (k -> a -> [(k, a)] -> [(k, a)])
-> [(k, a)] -> Trie k a -> [(k, a)]
forall k a r. TrieKey k => (k -> a -> r -> r) -> r -> Trie k a -> r
foldWithKey (\k
k a
v [(k, a)]
xs -> (k
k,a
v) (k, a) -> [(k, a)] -> [(k, a)]
forall a. a -> [a] -> [a]
: [(k, a)]
xs) []

-- | Left-biased union of two tries
union :: TrieKey k => Trie k a -> Trie k a -> Trie k a
union :: Trie k a -> Trie k a -> Trie k a
union = (k -> a -> a -> Maybe a)
-> (Trie k a -> Trie k a)
-> (Trie k a -> Trie k a)
-> Trie k a
-> Trie k a
-> Trie k a
forall k a b c.
TrieKey k =>
(k -> a -> b -> Maybe c)
-> (Trie k a -> Trie k c)
-> (Trie k b -> Trie k c)
-> Trie k a
-> Trie k b
-> Trie k c
mergeWithKey (\k
_ a
a a
_ -> a -> Maybe a
forall a. a -> Maybe a
Just a
a) Trie k a -> Trie k a
forall a. a -> a
id Trie k a -> Trie k a
forall a. a -> a
id

-- | Union of two tries with function used to merge overlapping elements
unionWith :: TrieKey k => (a -> a -> a) -> Trie k a -> Trie k a -> Trie k a
unionWith :: (a -> a -> a) -> Trie k a -> Trie k a -> Trie k a
unionWith a -> a -> a
f = (k -> a -> a -> Maybe a)
-> (Trie k a -> Trie k a)
-> (Trie k a -> Trie k a)
-> Trie k a
-> Trie k a
-> Trie k a
forall k a b c.
TrieKey k =>
(k -> a -> b -> Maybe c)
-> (Trie k a -> Trie k c)
-> (Trie k b -> Trie k c)
-> Trie k a
-> Trie k b
-> Trie k c
mergeWithKey (\k
_ a
a a
b -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> a -> a
f a
a a
b)) Trie k a -> Trie k a
forall a. a -> a
id Trie k a -> Trie k a
forall a. a -> a
id

-- | Union of two tries with function used to merge overlapping elements along with key
unionWithKey :: TrieKey k => (k -> a -> a -> a) -> Trie k a -> Trie k a -> Trie k a
unionWithKey :: (k -> a -> a -> a) -> Trie k a -> Trie k a -> Trie k a
unionWithKey k -> a -> a -> a
f = (k -> a -> a -> Maybe a)
-> (Trie k a -> Trie k a)
-> (Trie k a -> Trie k a)
-> Trie k a
-> Trie k a
-> Trie k a
forall k a b c.
TrieKey k =>
(k -> a -> b -> Maybe c)
-> (Trie k a -> Trie k c)
-> (Trie k b -> Trie k c)
-> Trie k a
-> Trie k b
-> Trie k c
mergeWithKey (\k
k a
a a
b -> a -> Maybe a
forall a. a -> Maybe a
Just (k -> a -> a -> a
f k
k a
a a
b)) Trie k a -> Trie k a
forall a. a -> a
id Trie k a -> Trie k a
forall a. a -> a
id

-- | Left-biased intersection of two tries
intersection :: TrieKey k => Trie k a -> Trie k b -> Trie k a
intersection :: Trie k a -> Trie k b -> Trie k a
intersection = (k -> a -> b -> Maybe a)
-> (Trie k a -> Trie k a)
-> (Trie k b -> Trie k a)
-> Trie k a
-> Trie k b
-> Trie k a
forall k a b c.
TrieKey k =>
(k -> a -> b -> Maybe c)
-> (Trie k a -> Trie k c)
-> (Trie k b -> Trie k c)
-> Trie k a
-> Trie k b
-> Trie k c
mergeWithKey (\k
_ a
a b
_ -> a -> Maybe a
forall a. a -> Maybe a
Just a
a) (Trie k a -> Trie k a -> Trie k a
forall a b. a -> b -> a
const Trie k a
forall k a. TrieKey k => Trie k a
empty) (Trie k a -> Trie k b -> Trie k a
forall a b. a -> b -> a
const Trie k a
forall k a. TrieKey k => Trie k a
empty)

-- | Intersection of two tries parameterized by a combining function of the
-- values at overlapping keys
intersectionWith :: TrieKey k => (a -> b -> c) -> Trie k a -> Trie k b -> Trie k c
intersectionWith :: (a -> b -> c) -> Trie k a -> Trie k b -> Trie k c
intersectionWith a -> b -> c
f = (k -> a -> b -> Maybe c)
-> (Trie k a -> Trie k c)
-> (Trie k b -> Trie k c)
-> Trie k a
-> Trie k b
-> Trie k c
forall k a b c.
TrieKey k =>
(k -> a -> b -> Maybe c)
-> (Trie k a -> Trie k c)
-> (Trie k b -> Trie k c)
-> Trie k a
-> Trie k b
-> Trie k c
mergeWithKey (\k
_ a
a b
b -> c -> Maybe c
forall a. a -> Maybe a
Just (a -> b -> c
f a
a b
b)) (Trie k c -> Trie k a -> Trie k c
forall a b. a -> b -> a
const Trie k c
forall k a. TrieKey k => Trie k a
empty) (Trie k c -> Trie k b -> Trie k c
forall a b. a -> b -> a
const Trie k c
forall k a. TrieKey k => Trie k a
empty)

-- | Intersection of two tries parameterized by a combining function of the
-- key and the values at overlapping keys
intersectionWithKey :: TrieKey k => (k -> a -> b -> c) -> Trie k a -> Trie k b -> Trie k c
intersectionWithKey :: (k -> a -> b -> c) -> Trie k a -> Trie k b -> Trie k c
intersectionWithKey k -> a -> b -> c
f = (k -> a -> b -> Maybe c)
-> (Trie k a -> Trie k c)
-> (Trie k b -> Trie k c)
-> Trie k a
-> Trie k b
-> Trie k c
forall k a b c.
TrieKey k =>
(k -> a -> b -> Maybe c)
-> (Trie k a -> Trie k c)
-> (Trie k b -> Trie k c)
-> Trie k a
-> Trie k b
-> Trie k c
mergeWithKey (\k
k a
a b
b -> c -> Maybe c
forall a. a -> Maybe a
Just (k -> a -> b -> c
f k
k a
a b
b)) (Trie k c -> Trie k a -> Trie k c
forall a b. a -> b -> a
const Trie k c
forall k a. TrieKey k => Trie k a
empty) (Trie k c -> Trie k b -> Trie k c
forall a b. a -> b -> a
const Trie k c
forall k a. TrieKey k => Trie k a
empty)

-- | Remove the keys of the right trie from the left trie
difference :: TrieKey k => Trie k a -> Trie k b -> Trie k a
difference :: Trie k a -> Trie k b -> Trie k a
difference = (k -> a -> b -> Maybe a)
-> (Trie k a -> Trie k a)
-> (Trie k b -> Trie k a)
-> Trie k a
-> Trie k b
-> Trie k a
forall k a b c.
TrieKey k =>
(k -> a -> b -> Maybe c)
-> (Trie k a -> Trie k c)
-> (Trie k b -> Trie k c)
-> Trie k a
-> Trie k b
-> Trie k c
mergeWithKey (\k
_ a
_ b
_ -> Maybe a
forall a. Maybe a
Nothing) Trie k a -> Trie k a
forall a. a -> a
id (Trie k a -> Trie k b -> Trie k a
forall a b. a -> b -> a
const Trie k a
forall k a. TrieKey k => Trie k a
empty)

-- | Parameterized 'difference' using a custom merge function.
-- Return 'Just' to change the value stored in left trie, or
-- 'Nothing' to remove from the left trie.
differenceWith :: TrieKey k => (a -> b -> Maybe a) -> Trie k a -> Trie k b -> Trie k a
differenceWith :: (a -> b -> Maybe a) -> Trie k a -> Trie k b -> Trie k a
differenceWith a -> b -> Maybe a
f = (k -> a -> b -> Maybe a)
-> (Trie k a -> Trie k a)
-> (Trie k b -> Trie k a)
-> Trie k a
-> Trie k b
-> Trie k a
forall k a b c.
TrieKey k =>
(k -> a -> b -> Maybe c)
-> (Trie k a -> Trie k c)
-> (Trie k b -> Trie k c)
-> Trie k a
-> Trie k b
-> Trie k c
mergeWithKey (\k
_ -> a -> b -> Maybe a
f) Trie k a -> Trie k a
forall a. a -> a
id (Trie k a -> Trie k b -> Trie k a
forall a b. a -> b -> a
const Trie k a
forall k a. TrieKey k => Trie k a
empty)

-- | 'differenceWith' where function also has access to the key
differenceWithKey :: TrieKey k => (k -> a -> b -> Maybe a) -> Trie k a -> Trie k b -> Trie k a
differenceWithKey :: (k -> a -> b -> Maybe a) -> Trie k a -> Trie k b -> Trie k a
differenceWithKey k -> a -> b -> Maybe a
f = (k -> a -> b -> Maybe a)
-> (Trie k a -> Trie k a)
-> (Trie k b -> Trie k a)
-> Trie k a
-> Trie k b
-> Trie k a
forall k a b c.
TrieKey k =>
(k -> a -> b -> Maybe c)
-> (Trie k a -> Trie k c)
-> (Trie k b -> Trie k c)
-> Trie k a
-> Trie k b
-> Trie k c
mergeWithKey k -> a -> b -> Maybe a
f Trie k a -> Trie k a
forall a. a -> a
id (Trie k a -> Trie k b -> Trie k a
forall a b. a -> b -> a
const Trie k a
forall k a. TrieKey k => Trie k a
empty)

-- | Map a function over a trie filtering out elements where function returns 'Nothing'
mapMaybe :: TrieKey k => (a -> Maybe b) -> Trie k a -> Trie k b
mapMaybe :: (a -> Maybe b) -> Trie k a -> Trie k b
mapMaybe a -> Maybe b
f = (k -> a -> Maybe b) -> Trie k a -> Trie k b
forall k a b.
TrieKey k =>
(k -> a -> Maybe b) -> Trie k a -> Trie k b
mapMaybeWithKey (\k
_ -> a -> Maybe b
f)