<br>Hello, I made some trial and error with ghci to make it happy. I'm not really sure this has the type safety you asked.<br><br>{-# LANGUAGE TypeFamilies, ExistentialQuantification, FlexibleContexts #-}<br><br>import Prelude hiding (lookup)<br>
import Data.Typeable<br><br>class Typeable a => Key a where<br> type Value a :: *<br> <br>data Assoc = forall a . (Typeable (Value a),Key a) => Assoc a (Value a)<br><br>insert :: (Typeable (Value a), Key a) => a -> Value a -> [Assoc] -> [Assoc]<br>
insert k v = (Assoc k v :)<br><br>lookup :: (Typeable (Value a), Eq a, Key a) => a -> [Assoc] -> Value a<br>lookup k [] = error "noassoc"<br>lookup k ((Assoc k' v):xs) = case cast k' of<br> Nothing -> lookup k xs<br>
Just k'' -> if k'' == k then case cast v of<br> Nothing -> error "nocast"<br> Just v' -> v'<br> else lookup k xs<br><div class="gmail_quote">
<br>I've tried without the typeclass with no luck.<br>For some reasons <br><br>type family Key a :: *<br>type family Value a :: *<br><br>and adding Typeable (Key a) to the contexts and Key 'a' in place of 'a' leads to a lot of type errors.<br>
Maybe it's possible with more help.<br><br>Hope I got it right.<br><br>Regards<br>paolino<br><br>2012/7/31 Alexander Foremny <span dir="ltr"><<a href="mailto:alexanderforemny@gmail.com" target="_blank">alexanderforemny@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello list,<br>
<br>
I am currently thinking that a problem of mine would best be solved if<br>
there was a Map-like data structure in which the value returned is<br>
parametrized over the lookup type.<br>
<br>
I wonder is this makes sense and if such a data structure exists or if<br>
it could be created while still being well typed. I essentially want<br>
to statically define a scope of Key values and dynamically define a<br>
list of keys.<br>
<br>
> -- Scope of possible keys.<br>
> type Label = String<br>
> data Key a where<br>
> KeyStr :: Label -> Key String<br>
> KeyInt :: Label -> Key Int<br>
> KeyChoice :: Label -> [a] -> Key a<br>
<br>
> -- Some key values, to be extended at runtime.<br>
> strKey "Some String"<br>
> strKey' "Another String"<br>
> intKey "Some integer"<br>
> choiceKey "Chose one" [ "a", "b", "c" ] :: KeyChoice String<br>
<br>
Now I need a data structure to possibly associate a value to the key.<br>
<br>
> data MapG = ...<br>
> type Value a = a<br>
> insert :: Key a -> Value a -> MapG Key Value -> MapG Key Value<br>
> lookup :: Key a -> MapG Key Value -> Maybe (Value a)<br>
<br>
I tried implementing this with multiple Map k a's. I tried adding a<br>
phantom type on some storage type of to implement KeyChoice as of type<br>
Key Int, but I ran into troubles with this approach. I wonder if<br>
Dynamic or Type Families could achieve this, but I am quite at a loss<br>
and would like to hear your opinion.<br>
<br>
I did try to search for this a bit, but I don't quite know how to<br>
phrase my problem. I'd like to apologize in advance if this question<br>
has been asked already.<br>
<br>
Regards,<br>
Alexander Foremny<br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br>