[Haskell] Per-type function namespaces (was: Data.Set whishes)

Ketil Malde ketil+haskell at ii.uib.no
Thu Feb 26 20:06:49 EST 2004

ozone at algorithm.com.au writes:

> addToFM :: Ord key => FiniteMap key elt -> key -> elt -> FiniteMap key
> elt

> addToSet :: Ord a => Set a -> a -> Set a

> So, how can you come up with a type class which provides a
> polymorphic 'add' function, considering you don't even know how many
> parameters each data type's individual add function uses?

Why, by chea^H^H^Hurrying, of course:

    class Collection a b | a -> b where
        add :: a -> b -> a

    instance Collection Set a where
        add = addToSet

    instance Collection FiniteMap k e where
        add fm (k,e) = addToFM fm k e

But I take your point, this could be hard to do in the general case.
E.g. 'delete' would probably only want a key.

If I haven't seen further, it is by standing in the footprints of giants

More information about the Haskell mailing list