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/ -------
On Tue, 29 Mar 2005, Johannes Waldmann wrote:
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) ]"
Since the Show and Read instances generated by the 'deriving' clause generate formatters and parsers for Haskell code it would be consistent to implement custom Show and Read instances the same way. What if not Haskell compatibility may be the criterion for the design of a Show instance? I encountered the same problem for my physical units modules. I started with a Show instance which generates strings like "2.3 km", but this can't be used as input in GHCi, again. Instead I have to write '2.3*kilo*meter' or 'read "2.3 km"'. I think that it would be better to let generate 'show' the result '2.3*kilo*meter' and I should do the conversion to traditional format using another formatter/lexer pair of routines. But how would I format numbers? Using 'show'? But 'show' formats complex numbers as 'a :+ b'. :-(
On Tue, 29 Mar 2005 13:20:12 +0200, Johannes Waldmann <waldmann@imn.htwk-leipzig.de> wrote:
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?
The magic is rather simple since toList is simly aliased to "toAscList". This is probably better changed to use toAscList. Cheers, JP.
participants (3)
-
Henning Thielemann -
Jean-Philippe Bernardy -
Johannes Waldmann