I'm looking for a monad-free, constant access query O(1) associative array.
Consider the hypothetical type:
data HT k v = ???
I want to construct an immutable structure once:
fromList :: Foldable t, Hashable k => t (k,v) -> HT k v
I want to subsequently query it repeatedly with constant time access::
lookup :: Hashable k => HT k v -> k -> Maybe v
There appears to be two candidate libraries which fall short:
unordered-containers
unordered-containers
contains both strict and lazy variants of the type HashMap
. Both HashMap
s have O(log n) queries as documented by the lookup
function. This query access time appears to be due to the construction of the HashMap
types, which have an internal tree structure allowing for O(log n) insert
functionality. An understandable design trade off for many use-cases, but since I don't need a mutable HashMap
this tradeoff hampers my use-case.
hashtables
hashtables
contains a HashTable
type-class and three instance types with varying table constructions strategies. This library's type-class defines a constant time O(1) lookup
function definition, but it is eternally embedded in the ST
monad. There is no way to "freeze" the stateful HashTable
implementations and have a lookup
function that is not embedded of a stateful monad. The library's type-class interface is well designed when the entire computation is wrapped in a state monad, but this design is unsuitable for my use-case.
Does there exist some other library which defines types and functions which can construct an immutable constant access query O(1) associative array that is not embedded in a stateful monad?
Does there exist some way to wrap or modify these existing hashing-based libraries to produce an immutable constant access query O(1) associative array that is not embedded in a stateful monad?