[Haskell-cafe] Key-Parametrized Lookup Table
alexanderforemny at gmail.com
Tue Jul 31 12:13:21 CEST 2012
I am currently thinking that a problem of mine would best be solved if
there was a Map-like data structure in which the value returned is
parametrized over the lookup type.
I wonder is this makes sense and if such a data structure exists or if
it could be created while still being well typed. I essentially want
to statically define a scope of Key values and dynamically define a
list of keys.
> -- Scope of possible keys.
> type Label = String
> data Key a where
> KeyStr :: Label -> Key String
> KeyInt :: Label -> Key Int
> KeyChoice :: Label -> [a] -> Key a
> -- Some key values, to be extended at runtime.
> strKey "Some String"
> strKey' "Another String"
> intKey "Some integer"
> choiceKey "Chose one" [ "a", "b", "c" ] :: KeyChoice String
Now I need a data structure to possibly associate a value to the key.
> data MapG = ...
> type Value a = a
> insert :: Key a -> Value a -> MapG Key Value -> MapG Key Value
> lookup :: Key a -> MapG Key Value -> Maybe (Value a)
I tried implementing this with multiple Map k a's. I tried adding a
phantom type on some storage type of to implement KeyChoice as of type
Key Int, but I ran into troubles with this approach. I wonder if
Dynamic or Type Families could achieve this, but I am quite at a loss
and would like to hear your opinion.
I did try to search for this a bit, but I don't quite know how to
phrase my problem. I'd like to apologize in advance if this question
has been asked already.
More information about the Haskell-Cafe