1

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.

bbarker
  • 11,636
  • 9
  • 38
  • 62

1 Answers1

2

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:

Shersh
  • 9,019
  • 3
  • 33
  • 61
  • I had no idea that module Signatures existed, I can see this being very useful (especially for people wanting to avoid more "advanced" FP or Category Theory – bbarker Oct 17 '18 at 13:07
  • One of the motivation behind Backpack is to keep simple Haskell. Having typeclass for `Map` most likely will involve advance features like `TypeFamilies`, so Backpack really shines here! – Shersh Oct 17 '18 at 14:05
  • Sorry if I missed something obvious as I'm rather new to Haskell development, but I can't seem to find it on hackage. Is there a suggested way to use the project code externally? I saw https://github.com/haskell/cabal/issues/4206 so assume you may be waiting on that to publish to hackage. – bbarker Oct 17 '18 at 16:49
  • Hmmm, I now see that backpacks are more than a conceptual idea and require using next.hackage (currently), but I don't see the packages there either. – bbarker Oct 17 '18 at 17:02
  • @bbarker It's not on Hackage because maintaining it on Hackage requires to upload 20 packages. I would like to wait for _multiple public libraries_ support to be implemented first (it's almost done and will be merged to `cabal` in nearest days). Meanwhile you can depend on GitHub repository, `cabal` allows to specify dependency on GitHub directly. – Shersh Oct 18 '18 at 04:00