
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.