[Haskell-cafe] containers and maps

John Lato jwlato at gmail.com
Wed Aug 12 20:18:41 EDT 2009


Hello,

The recent discussion regarding One Container Class To Rule Them All
got me to thinking about container classes and map functions.

ListLike (which is really a pretty nice package, and I would like to
see more people use it) provides two map functions, a regular map with
type
> map :: (item -> item') -> full -> full'

and also rigidMap:
> rigidMap :: (item -> item) -> full -> full

I think the utility of rigidMap is clear for anyone who's tried to
solve the container class problem.

Anyway, I had the idea of taking both maps out of ListLike entirely,
and replace them with this:

> class (ListLike full item, ListLike full' item') => Map full item full' item' where
>  map :: (item -> item') -> full -> full'

Wouldn't this be better?  I see the following benefits:
  -instances for polymorphic types like List are trivial
  -instances for monomorphic types (e.g. ByteString) can use the
native map implementation.
  -Instances for types with class restrictions on the elements (e.g.
UVector) can also be written.

On the downside, the class context is much longer, but you'd only need
to write the whole thing when you're actually using map, and then
it'll include the ListLike instance automatically.

Obviously this isn't so much one container class to rule them all as
it is a set of classes that all work together, but as long as it's
sensible that's fine with me.

Thoughts?

John Lato


More information about the Haskell-Cafe mailing list