简体   繁体   中英

Is there a standard immutable “Map” (e.g. Data.Map, Data.HashMap) typeclass in Haskell?

It seems that the idea of a dictionary is one that gives rise to many different implementations, understandably. Since this is Haskell, I'll focus on immutable maps/hashes/dicts. I'd like to be able to use a typeclass for a little library I'm working on and allow users to choose their Hash/Map/Dict implementation.

If there isn't a standard typeclass, it seems that providing a typeclass via hackage might be useful, and failing that, one could (as a library/API provider) still find benefit from using the hackage package or one's own typeclass to allow users to choose a given implementation for a Map.

Assuming I haven't missed a typeclass for Dicts, is there anything in particular about immutable dictionaries that would not go well with typeclasses in Haskell?

Update

The more general question Why is Haskell missing "obvious" Typeclasses provides a lot of useful insight that goes a long way towards answering this question, but doesn't offer practical solutions to this particular case.

There're some unsolvable problems with having typeclass for the Map -like data structures. Fortunately, Backpack helps. You can see the interface for Map via Backpack here:

I've implemented this solution and a blog post about such interface:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM