Safe Haskell | None |
---|---|
Language | Haskell2010 |
Fast read-only heterogeneous array.
This module is extracted from web-routing, orginally desgined for high performance type safe routing. The basic idea is:
- Construct a heterogeneous linked-list is O(n), since prepend is O(1).
- Convert it into a heterogeneous array in O(n).
- Following access will be a simple O(1) array indexing, with index computed at compile time so you can't get missing keys.
In theory, it's faster than linked-list based data structures when n is large, but it needs more benchmark to be sure.
Typical usage: a heterogeneous lookup table, indexed by type level string.
> :set -XDataKinds -XQuasiQuotes > let d = mkDict . add [key|foo|] 12 . add [key|bar|] "baz" $ emptyStore > get [key|foo|] d 12 > get [key|bar|] d "baz"
- data Store kvs = Store {}
- emptyStore :: Store '[]
- add :: NotHasKey k kvs => Proxy k -> v -> Store kvs -> Store ((k := v) ': kvs)
- data Dict kvs
- mkDict' :: forall s kvs. Store kvs -> ST s (Dict kvs)
- mkDict :: Store kvs -> Dict kvs
- class InDict k v kvs | k kvs -> v
- get :: InDict k v kvs => Proxy k -> Dict kvs -> v
- key :: QuasiQuoter
- data KV v = Symbol := v
- data KVList kvs where
- type NotHasKey k kvs = AddKey k kvs ~ HasKey k
- type Ix k kvs = Ix' 0 k kvs
- class ShowDict kvs where
Store
emptyStore :: Store '[] Source #
An empty Store
add :: NotHasKey k kvs => Proxy k -> v -> Store kvs -> Store ((k := v) ': kvs) Source #
O(1) add key value pair to Store
.
> let a = add [key|foo|] (12 :: Int) emptyStore > a Store {foo = 12 :: Int} > add [key|bar|] "baz" a Store {bar = "baz" :: [Char], foo = 12 :: Int}
Dict
Read-Only heterogeneous array.
The underline data structure is a boxed array,
support get
operation only.
get :: InDict k v kvs => Proxy k -> Dict kvs -> v Source #
O(1) get value using associated key from Dict
.
re-export from KVList
key :: QuasiQuoter Source #
Quoter for constructing string literal proxy.
[key|foo|] == (Proxy :: Proxy "foo")
(kind) key-value pair
(InDict k v kvs, (~) GetResult (Index i) (Ix k ((:) (KV *) ((:=) * k' v') kvs)), KnownNat i) => InDict k v ((:) (KV *) ((:=) * k' v') kvs) Source # | |
InDict k v ((:) (KV *) ((:=) * k v) kvs) Source # | |
(InDict k v kvs, (~) GetResult (Index i) (Ix k ((:) (KV *) ((:=) * k' v') kvs)), KnownNat i) => InDict k v ((:) (KV *) ((:=) * k' v') kvs) Source # | |
InDict k v ((:) (KV *) ((:=) * k v) kvs) Source # | |
(KnownSymbol k, ToJSON v, ToJSON (DynDict kvs)) => ToJSON (DynDict ((:) (KV *) ((:=) * k v) kvs)) # | |
ToJSON (DynDict ([] (KV *))) # | |
(KnownSymbol k, FromJSON v, FromJSON (DynDict kvs)) => FromJSON (DynDict ((:) (KV *) ((:=) * k v) kvs)) # | |
FromJSON (DynDict ([] (KV *))) # | |
ShowDict ([] (KV *)) Source # | |
(KnownSymbol k, Typeable * v, Show v, ShowDict kvs) => ShowDict ((:) (KV *) ((:=) * k v) kvs) Source # | |
type NotHasKey k kvs = AddKey k kvs ~ HasKey k Source #
Constraint ensure a key will be inserted into Store
.