Data.Map questions

Johannes Waldmann waldmann at imn.htwk-leipzig.de
Tue Mar 29 06:20:12 EST 2005


Data.Map  (in ghc-6.4) has instances for Ord and of Show
(which FiniteMap didn't have).

What does Ord guarantee?

I hope it is a total ordering compatible with  (==)
(so that Maps could be elements of Sets etc.)
If so, perhaps there should be a comment in the documentation.
 From reading the source ghc-6.4/libraries/base/Data/Map.hs
it is not clear why Eq uses toAscList but Ord uses toList -
so perhaps there is some hidden magic?

What if I don't like the Show instance?

(That seems to be a fundamental question of library design/usage.)
I prefer to have  show x   return some string
that can be pasted back into the source code (!)
and gives the correct result. So, for FiniteMap I had sth. like
show fm = "listToFM [ (1, True), (2, False) ]"

It seems I cannot hide the Show instance from Data.Map (it is always in 
scope, http://www.haskell.org/onlinereport/modules.html#sect5.4)
Perhaps it would be better to have the Show instance
in a separate module (that is not automatically imported).

Because if I want to use a Map with my preferred Show instance now,
I would have to declare my own newtype and instantiate that. -
Maybe that's better design anyway (not exposing the concrete 
implementation). It still fixes one implementation, though.

The next step would be to use a type class,
but this severely blows up the type signatures.
With this respect, "corresponding" Java programs really look better,
allowing  Map<Foo,Bar> f = new HashMap<Foo,Bar>();
where Map is the interface, and HashMap is one implementation,
and the rest of the program only knows that  f  implements the 
interface. Yes I know that syntax is only possible because their type 
classes (interfaces) have exactly one parameter ...

Best regards,
-- 
-- Johannes Waldmann -- Tel/Fax (0341) 3076 6479/80 --
---- http://www.imn.htwk-leipzig.de/~waldmann/ -------



More information about the Libraries mailing list