Copyright | © 2018 Andy Morris |
---|---|
License | BSD-3-Clause |
Maintainer | hello@andy-morris.xyz |
Stability | experimental |
Portability | GHC internals; weak pointers & finalizers; stable names |
Safe Haskell | None |
Language | Haskell2010 |
Hash-consing, or interning, is a way to gain constant-time equality testing (and hashing) for potentially-large data types such as strings or abstract syntax trees. Internally a table of live values is kept, and newly-constructed values are looked up in the table to check if they already exist. If they do, then the existing one is reused (along with a tag). The table is pruned using finalisers when these tagged values are garbage collected.
This library should be thread- and exception-safe.
Documentation
class (Eq a, Hashable a) => HashCons a Source #
Types which support hash-consing.
There are some restrictions on types for which this class makes sense:
- The type must have no type variables: an instance for
T Int
would be fine, but not forT a
. (There must also be no constraints, but that is unlikely to be a problem if all instances are ground.) - Equality and hashing must consider all data in a value. It need not necessarily be structural equality, but a subterm should not simply be ignored. (An example of why someone might want to ave equality ignore parts of a type is annotations in an abstract syntax tree.)
A value which has been given a unique tag.
Eq (HC a) Source # | \(\mathcal{O}(1)\) using the tag |
Ord a => Ord (HC a) Source # | Checks the tag for equality first, and otherwise falls back to the underlying type's ordering |
(Read a, HashCons a) => Read (HC a) Source # | Reads an underlying value and caches it |
Show a => Show (HC a) Source # | Shows the underlying value |
(Storable a, HashCons a) => Storable (HC a) Source # | Stores the underlying value, and re-caches it on retrieval |
NFData a => NFData (HC a) Source # | Also evaluates the underlying value |
MkWeak (HC a) Source # | |
Hashable (HC a) Source # | \(\mathcal{O}(1)\) using the tag |
MemoArg (HC a) Source # | |
type Key (HC a) Source # | |
type CanFinalize (HC a) Source # | |
A tag for a value. Tags are unique among values which are simultaneously alive. They also don't keep the corresponding value alive on their own.