[Haskell-cafe] Harder than you'd think

Andrew Coppin andrewcoppin at btinternet.com
Sun Jun 13 08:09:24 EDT 2010

So the other day I was writing some code, and I ended up wanting to have 
a collection of data indexed in more than one way. In other words, I 
wanted fast lookup with several different keys.

Initially I built something using two Data.Map objects to represent the 
two lookup keys, but then I needed up needing more keys per value than 
that, so I decided I should write a small library to abstract the whole 

What I ended up writing is this: 

It sorta kinda works, but man, take a look at the first line. That's *a 
lot* of type-system abuse to make it go. And it strikes me that if you 
had two keys, both of which happen to have the same type (e.g., 
String)... what then?

On the other hand, it's not like you can go

  lookup :: KeyID -> Key -> Container -> Maybe Value

since the type of both the container and the key depend on which key you 
want to access.

Man, this is way, way harder than I realised!

Does anybody have a less-insane way of doing this?

More information about the Haskell-Cafe mailing list