HORIZON HASKELLDocslts/ghc-9.14.xd82dee82026-10-12Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · d82dee8 · 2026-10-12

Modulebytestring-trie-0.2.7.7Haskell2010

Data.Trie

An efficient implementation of finite maps from strings to values. The implementation is based on big-endian patricia trees, like Data.IntMap. We first trie on the elements of Data.ByteString and then trie on the big-endian bit representation of those elements. For further details on the latter, see

  • Chris Okasaki and Andy Gill, "Fast Mergeable Integer Maps", Workshop on ML, September 1998, pages 77-86, http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.37.5452

  • D.R. Morrison, "PATRICIA -- Practical Algorithm To Retrieve Information Coded In Alphanumeric", Journal of the ACM, 15(4), October 1968, pages 514-534.

This module aims to provide an austere interface, while being detailed enough for most users. For an extended interface with many additional functions, see Data.Trie.Convenience. For functions that give more detailed (potentially abstraction-breaking) access to the data structure, or for experimental functions which aren't quite ready for the public API, see Data.Trie.Internal.

  • 1 type
  • 30 values

Data type

1 declaration
datadata Trie a
#

A map from ByteStrings to a. For all the generic functions, note that tries are strict in the Maybe but not in a.

The Monad instance is strange. If a key k1 is a prefix of other keys, then results from binding the value at k1 will override values from longer keys when they collide. If this is useful for anything, or if there's a more sensible instance, I'd be curious to know.

Instances19Monad, Functor, Applicative, Foldable, Traversable, Eq1, …
  • Monad TrieDefined in bytestring-trie-0.2.7.7 · Data.Trie.Internal
  • Functor TrieDefined in bytestring-trie-0.2.7.7 · Data.Trie.Internal
  • Applicative TrieDefined in bytestring-trie-0.2.7.7 · Data.Trie.Internal
  • Foldable TrieDefined in bytestring-trie-0.2.7.7 · Data.Trie.Internal
  • Traversable TrieDefined in bytestring-trie-0.2.7.7 · Data.Trie.Internal
  • Eq1 TrieDefined in bytestring-trie-0.2.7.7 · Data.Trie.Internal
  • Ord1 TrieDefined in bytestring-trie-0.2.7.7 · Data.Trie.Internal

    Warning: This instance suffers unnecessarily from Bug #25.

  • Read1 TrieDefined in bytestring-trie-0.2.7.7 · Data.Trie.Internal
  • Show1 TrieDefined in bytestring-trie-0.2.7.7 · Data.Trie.Internal

    Warning: This instance suffers Bug #25.

  • IsList (Trie a)Defined in bytestring-trie-0.2.7.7 · Data.Trie.Internal

    Warning: The toList method of this instance suffers Bug #25.

  • Eq a => Eq (Trie a)Defined in bytestring-trie-0.2.7.7 · Data.Trie.Internal
  • Ord a => Ord (Trie a)Defined in bytestring-trie-0.2.7.7 · Data.Trie.Internal

    Warning: This instance suffers unnecessarily from Bug #25.

  • Read a => Read (Trie a)Defined in bytestring-trie-0.2.7.7 · Data.Trie.Internal
  • Show a => Show (Trie a)Defined in bytestring-trie-0.2.7.7 · Data.Trie.Internal

    Warning: This instance suffers Bug #25.

  • Semigroup a => Semigroup (Trie a)Defined in bytestring-trie-0.2.7.7 · Data.Trie.Internal
  • Monoid a => Monoid (Trie a)Defined in bytestring-trie-0.2.7.7 · Data.Trie.Internal
  • NFData a => NFData (Trie a)Defined in bytestring-trie-0.2.7.7 · Data.Trie.Internal
  • Binary a => Binary (Trie a)Defined in bytestring-trie-0.2.7.7 · Data.Trie.Internal
  • type Item (Trie a) = (ByteString, a)Defined in bytestring-trie-0.2.7.7 · Data.Trie.Internal

Basic functions

4 declarations
valueempty :: Trie a
#

\mathcal{O}(1). Construct the empty trie.

valuenull :: Trie a -> Bool
#

\mathcal{O}(1). Is the trie empty?

valuesize :: Trie a -> Int
#

\mathcal{O}(n). Get count of elements in trie.

Conversion functions

5 declarations
valuefromList :: [(ByteString, a)] -> Trie a
#

Convert association list into a trie. On key conflict, values earlier in the list shadow later ones.

valuetoListBy :: (ByteString -> a -> b) -> Trie a -> [b]
#

Convert a trie into a list using a function. Resulting values are in key-sorted order.

Warning: This function suffers Bug #25.

valuetoList :: Trie a -> [(ByteString, a)]
#

Convert trie into association list. The list is ordered according to the keys.

Warning: This function suffers Bug #25.

valueelems :: Trie a -> [a]
#

Return all values in the trie, in key-sorted order.

Note: Prior to version 0.2.7, this function suffered Bug #25; but it no longer does.

Query functions

7 declarations
valuelookupBy :: (Maybe a -> Trie a -> b) -> ByteString -> Trie a -> b
#

Generic function to find a value (if it exists) and the subtrie rooted at the prefix.

valuematches :: Trie a -> ByteString -> [(ByteString, a, ByteString)]
#

Given a query, find all prefixes with associated values in the trie, and return their (prefix, value, remainder) triples in order from shortest prefix to longest. This function is a good producer for list fusion.

Simple modification

6 declarations
valueinsert :: ByteString -> a -> Trie a -> Trie a
#

Insert a new key. If the key is already present, overrides the old value

valueadjust :: (a -> a) -> ByteString -> Trie a -> Trie a
#

Apply a function to the value at a key. If the key is not present, then the trie is returned unaltered.

valueadjustBy
  1. :: ByteString -> a -> a -> a
  2. -> ByteString
  3. -> a
  4. -> Trie a
  5. -> Trie a
#

Alter the value associated with a given key. If the key is not present, then the trie is returned unaltered. See alterBy if you are interested in inserting new keys or deleting old keys. Because this function does not need to worry about changing the trie structure, it is somewhat faster than alterBy.

Note: Prior to version 0.2.6 this function was exported from Data.Trie.Internal instead.

Combining tries

6 declarations
valuemergeBy :: (a -> a -> Maybe a) -> Trie a -> Trie a -> Trie a
#

Take the union of two tries, using a function to resolve collisions. This can only define the space of functions between union and symmetric difference but, with those two, all set operations can be defined (albeit inefficiently).

valueunionL :: Trie a -> Trie a -> Trie a
#

Take the union of two tries, resolving conflicts by choosing the value from the left trie.

valueunionR :: Trie a -> Trie a -> Trie a
#

Take the union of two tries, resolving conflicts by choosing the value from the right trie.

valueintersectBy :: (a -> b -> Maybe c) -> Trie a -> Trie b -> Trie c
#

Take the intersection of two tries, using a function to resolve collisions.

valueintersectL :: Trie a -> Trie b -> Trie a
#

Take the intersection of two tries, with values from the left trie.

valueintersectR :: Trie a -> Trie b -> Trie b
#

Take the intersection of two tries, with values from the right trie.

Mapping functions

2 declarations
valuefilterMap :: (a -> Maybe b) -> Trie a -> Trie b
#

Apply a function to all values, potentially removing them.

Laws
Fission

filterMap f ≡ fmap (fromJust . f) . filter (isJust . f)

Fusion

fmap f . filter g ≡ filterMap (\v -> f v <$ guard (g v))

Conservation

filterMap (Just . f) ≡ fmap f

Composition

filterMap f . filterMap g ≡ filterMap (f <=< g)

The fission/fusion laws are essentially the same, they differ only in which direction is more "natural" for use as a rewrite rule. The conservation law is just a special case of fusion, but it's a particularly helpful one to take note of.