-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/
-- | Atom (or symbol) datatype for fast comparision and sorting.
--
-- This module provides an abstract datatype for atoms, such that:
--
--
-- - Each atom string is only in memory once
-- - O(n) creation time
-- - O(1) equality-comparison
-- - O(1) (in practice) ord-comparison
-- - Ord-comparison results are independent on evaluation
-- order
--
--
-- This module is thread-safe.
@package simple-atom
@version 0.2
-- | Symbols without a central symbol table.
--
-- Symbols provide the following efficient operations:
--
--
-- - O(1) equality comparison (in practise)
-- - O(1) ordering comparison (in practise)
-- - O(n) creation where N is the size of the symbol
-- descriptor.
--
--
-- Many implementations often have the additional property that each
-- symbol descriptor only exists once in memory. This implementation
-- slightly relaxes this property:
--
--
-- - A symbol descriptor is guaranteed to exists only once in memory if
-- it has been created using the same symbol table. Furthermore, if two
-- symbols created from different symbol tables are compared and their
-- descriptors turn out to be equal, the symbols will share the
-- descriptor after the comparison.
--
--
-- This allows the following additional properties not present in
-- conventional implementations:
--
--
-- - No space leak. The symbol table can be discarded at any time.
-- - Symbols created using different symbol tables can be compared
-- reliably.
-- - No global lock. (TODO: Well we might need one in the case of
-- hash-collisions, but a lock-free implementation might be
-- possible.)
--
--
-- Inspired by Richard OKeefes message to Erlang's eeps mailing
-- list http://www.erlang.org/cgi-bin/ezmlm-cgi/5/057, which in
-- turn was inspired by the Logix implementation of Flat Concurrent
-- Prolog.
module Data.Atom.UF
-- | A symbol.
--
-- Note that the ordering on a is not preserved on
-- Symbol a. Symbols are ordered by their hashes, and only if
-- the hashes are equal will the ordering on a be used. We have:
--
--
-- x == y ==> intern x == intern y
--
-- let sx = intern x
-- sy = intern y
-- in
-- (sx < sy) == ((symbolHash sy < symbolHash sx) ||
-- symbolHash sy == symbolHash sx && x < y)
--
data Symbol a
-- | Create a new local symbol. For best performance use internInto
-- together with a symbol table / map.
intern :: (a -> Word64) -> a -> Symbol a
-- | Insert a symbol into an existing table.
internInto :: SymTab s => (a -> Word64) -> s a -> a -> (s a, Symbol a)
class SymTab s
lookupSymbol :: SymTab s => s a -> a -> Maybe (Symbol a)
insertSymbol :: SymTab s => a -> (Symbol a) -> s a -> s a
-- | Returns the hash of the symbol.
symbolHash :: Symbol a -> Word64
instance Show a => Show (Symbol a)
instance Ord a => Ord (Symbol a)
instance Ord a => Eq (Symbol a)
module Data.Atom.Simple
-- | A Symbol. This is essentially a String, but with
-- different performance characteristics:
--
--
-- - O(n) creation time (using insert)
-- - O(1) equality comparison.
-- - O(1) comparison (in practice). The result of
-- compare is independent of evaluation order.
--
--
-- It is currently implemented as follows.
--
--
-- - Each symbol contains a unique integer, which allows O(1)
-- comparison.
-- - Each symbol contains an infinite chain of hashes, these are used
-- for comparison. In practice, it is very rare that more than the first
-- of those hashes is ever evaluated. The first hash is cached, so that
-- most comparisons will not need any indirections.
-- - The String representation of the symbol. Use show to
-- return it. At any time, there will be only one symbol of a given name
-- in memory.
--
data Symbol
-- | Turn a String into a Symbol.
--
-- Note, however, that this function contains a space leak. It has
-- internal state (the symbol table) but is referentially transparent.
-- Unfortunately, there is no way to delete items from the symbol table.
--
-- (This function is, of course, thread-safe.)
intern :: String -> Symbol
instance Ord Symbol
instance Eq Symbol
instance Show Symbol