Safe Haskell | None |
---|---|
Language | Haskell2010 |
- data Store kvs
- emptyStore :: Store []
- type (</) k v = HasKey k v ~ AlreadyHasKey k
- add :: k </ kvs => proxy k -> v -> Store kvs -> Store ((k := v) : kvs)
- mkDict :: Store kvs -> Dict kvs
- data Dict kvs
- emptyDict :: Dict []
- class Member k v kvs | k kvs -> v
- get :: Member k v kvs => proxy k -> Dict kvs -> v
- class ShowDict kvs
- data KV v = Symbol := v
- type family Members kvs prms :: Constraint
- data GetResult
- data AddResult
store
emptyStore :: Store [] Source
type (</) k v = HasKey k v ~ AlreadyHasKey k Source
'not elem key' constraint(ghc >= 7.8)
add :: k </ kvs => proxy k -> v -> Store kvs -> Store ((k := v) : kvs) Source
O(1) add key value pair to dictionary.
>>>
let a = add (Proxy :: Proxy "foo") (12 :: Int) emptyStore
>>>
a
Store {foo = 12 :: Int}
>>>
add (Proxy :: Proxy "bar") "baz" a
Store {bar = "baz" :: [Char], foo = 12 :: Int}
dictionary
heterogeneous dictionary
get
operation only allowed.
get :: Member k v kvs => proxy k -> Dict kvs -> v Source
O(1) (>= ghc-7.8), O(n) (< ghc-7.8) get key from dictionary
>>>
let d = mkDict $ add (Proxy :: Proxy "foo") 12 $ add (Proxy :: Proxy "bar") "baz" emptyStore
>>>
get (Proxy :: Proxy "foo") d
12>>>
get (Proxy :: Proxy "bar") d
"baz"
types
showDict
(kind) key-value pair
(Member k v kvs, (~) GetResult (Index i) (Ix k ((:) (KV *) ((:=) * k' v') kvs)), KnownNat i) => Member k v ((:) (KV *) ((:=) * k' v') kvs) | |
Member k v ((:) (KV *) ((:=) * k v) kvs) | |
ShowDict ([] (KV *)) | |
(KnownSymbol k, Typeable * v, Show v, ShowDict kvs) => ShowDict ((:) (KV *) ((:=) * k v) kvs) | |
type Members ([] (KV *)) prms = () | |
type Members ((:) (KV *) ((:=) * k v) kvs) prms = (Member k v prms, Members kvs prms) |
convenient
type family Members kvs prms :: Constraint Source
type family to constraint multi kvs.
Members ["foo" := Int, "bar" := Double] prms == (Member "foo" Int prms, Member "bar" Double prms)
pretty print type errors
(kind) pretty print type error of get
used only >= ghc-7.8
> get (Proxy :: Proxy "b") (mkDict $ add (Proxy :: Proxy "a") "a" emptyStore) Couldn't match type ‘'Key "b"’ with ‘'NotInDicrionary i’
(kind) pretty print type error of add
.
> add (Proxy :: Proxy "a") 12 $ add (Proxy :: Proxy "a") "a" emptyStore Couldn't match type ‘'Dictionary’ with ‘'AlreadyHasKey "a"’