Re: [Haskell-cafe] Unnecessarily strict implementations

Ivan Lazar Miljenovic schrieb:
On 3 September 2010 04:57, Arie Peterson
wrote: On Thu, 2 Sep 2010 19:30:17 +0200, Daniel Fischer
wrote: Why would one consider using Ord for Map an abuse? A kludge, for performance reasons, but an abuse? Because it forces one to declare Ord instances for types which have no natural ordering. It is useful to *not* have such instances, in order to catch programming errors.
What precisely do you mean by natural ordering?
E.g. I wanted to have a Set of Gaussian (complex) integers, but I did not want to define an Ord instance for them, because writing a < (b :: Gaussian) is a bug with high probability.
A separate type class for types which can be ordered in some (possibly arbitrary) way, for use in Data.Map, would remedy this.
Sure... except that the way Data.Map and Data.Set are implemented is by a binary tree, and you typically want some kind of ordering for those.
How is a type class that represents arbitrary ordering any different from what we already have? The notation might not be the best if you consider the ordering to be arbitrary, but what else would you use? "isArbitrarilyBefore :: (ArbitraryOrdering a) => a -> a -> Bool" ?
Yes, something this way. (<) suggests a notion of magnitude for me, which some orderings do not have.

On 03/09/10 11:11, Henning Thielemann wrote:
Ivan Lazar Miljenovic schrieb:
On 3 September 2010 04:57, Arie Peterson
wrote: On Thu, 2 Sep 2010 19:30:17 +0200, Daniel Fischer
wrote: Why would one consider using Ord for Map an abuse? A kludge, for performance reasons, but an abuse? Because it forces one to declare Ord instances for types which have no natural ordering. It is useful to *not* have such instances, in order to catch programming errors.
What precisely do you mean by natural ordering?
E.g. I wanted to have a Set of Gaussian (complex) integers, but I did not want to define an Ord instance for them, because writing a < (b :: Gaussian) is a bug with high probability.
Isn't this what newtype is good for? Instead of declaring Ord Gaussian to get Set Gaussian and risking the bug you describe, create newtype GaussianInSet = G Gaussian, declare Ord GaussianInSet and use Set GaussianInSet. Thanks, Neil.

Neil Brown schrieb:
On 03/09/10 11:11, Henning Thielemann wrote:
E.g. I wanted to have a Set of Gaussian (complex) integers, but I did not want to define an Ord instance for them, because writing a < (b :: Gaussian) is a bug with high probability.
Isn't this what newtype is good for? Instead of declaring Ord Gaussian to get Set Gaussian and risking the bug you describe, create newtype GaussianInSet = G Gaussian, declare Ord GaussianInSet and use Set GaussianInSet.
If I use a newtype then I need to lift the numeric operations to that newtype, and then chances are great that I use (<) with wrong expectations on the newtyped Gaussians. My concrete application was an implementation of partial fractions, where I used a Map from a root and its multiplicity in the denominator to the numerator. E.g. (3x+1)/(x+4)^2 + 5/(x-7) is represented by {(4,2) -> polynomial [1,3], (-7,1) -> polynomial [5]} In order to support complex numbers (in this case not only Gaussian integers) and not forcing an Ord instance, I introduced a new type class, like ArbitraryOrdered, used a newtype to map this class to Ord and used this for the Map that represents the partial fraction.

On 10-09-03 06:11 AM, Henning Thielemann wrote:
Yes, something this way. (<) suggests a notion of magnitude for me, which some orderings do not have.
Like for example -1000 has a larger magnitude than -0.0001, therefore you also reject the common ordering -1000 < -0.0001? http://groups.google.com/group/alt.algebra.help/browse_frm/thread/87ca396337...
participants (4)
-
Albert Y. C. Lai
-
Henning Thielemann
-
Henning Thielemann
-
Neil Brown