snoyberg / mono-traversable

Type classes for mapping, folding, and traversing monomorphic containers
153 stars 63 forks source link

Add polymorphic container functions #93

Closed GallagherCommaJack closed 8 years ago

GallagherCommaJack commented 8 years ago

Is there any good reason to keep the list specialization of functions like ___FromList and __ToList?

If we actually want people to be able to use vectors and sequences instead of lists everywhere, it seems like we shouldn't make them convert back and forth when they want to construct or destruct containers.

snoyberg commented 8 years ago

I see two reasons:

  1. Performance. The underlying Map/IntMap/HashMap/etc functions all work in terms of lists. Converting to a Vector or something else adds a performance overhead that may not be obvious from the polymorphic type signature.
  2. Type inference. Since it's common to convert to a list and then do something like a fold, returning a polymorphic value may be too much type freedom and annoyingly require type signatures.

On Thu, Mar 24, 2016 at 10:40 PM, Jack Gallagher notifications@github.com wrote:

Is there any good reason to keep the list specialization of functions like _FromList and ToList?

If we actually want people to be able to use vectors and sequences instead of lists everywhere, it seems like we shouldn't make them convert back and forth when they want to construct or destruct containers.

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/snoyberg/mono-traversable/issues/93

GallagherCommaJack commented 8 years ago
  1. two different ideas jump out at me:
    • This seems like the sort of double-fold that a compiler ought to be able to optimize away. I don't have the right expertise yet to figure out if GHC is indeed accomplishing this, but I put decent odds on it being possible with some well-crafted rewrite rules.
    • It's probably worth looking at the underlying implementations to see how plausible it is to port them to classier types (if they're just folds there's no reason for lists to have all the fun). That said, this sets off alarms for potentially unnecessary duplication of functionality.
  2. makes sense - looking through the code that made me post this issue, I realize I didn't actually need anything but list output.