Revamping the numeric classes
Dylan Thurston
dpt@math.harvard.edu
Thu, 8 Feb 2001 12:43:08 -0500
On Thu, Feb 08, 2001 at 11:24:49AM +0000, Jerzy Karczmarczuk wrote:
> First, a general remark which has nothing to do with Num.
>
> PLEASE WATCH YOUR DESTINATION ADDRESSES
> People send regularly their postings to haskell-cafe with
> several private receiver addresses, which is a bit annoying
> when you click "reply all"...
Yes, apologies. The way the lists do the headers make it very easy to
reply to individuals, and hard to reply to the list.
> And, last, but very high on my check-list:
>
> The implicit coercion of numeric constants: 3.14 -=->> (fromDouble
> 3.14)
> etc. is sick. (Or was; I still didn't install the last version of GHC,
> and with Hugs it is bad). The decision is taken by the compiler
> internally,
> and it doesn't care at all about the fact that in my prelude
> I have eliminated the Num class and redefined fromDouble, fromInt, etc.
Can't you just put "default ()" at the top of each module?
I suppose you still have the problem that a numeric literal "5" means
"Prelude.fromInteger 5". Can't you define your types to be instances
of Prelude.Num, with no operations defined except Prelude.fromInteger?
> Dylan Thurston terminates his previous posting about Num with:
>
> > Footnotes:
> > [1] Except for the lack of abs and signum, which should be in some
> > other class. I have to think about their semantics before I can say
> > where they belong.
>
> Now, signum and abs seem to be quite distincts beasts. Signum seem to
> require Ord (and a generic zero...).
>
> Abs from the mathematical point of view constitutes a *norm*. Now,
> frankly, I haven't the slightest idea how to cast this concept into
> Haskell class hierarchy in a sufficiently general way...
This was one thing I liked with the Haskell hierarchy: the observation
that "signum" of real numbers is very much like "argument" of complex
numbers. abs and signum in Haskell satisfy an implicit law:
abs x * signum x = x [1]
So signum can be defined anywhere you can define abs (except that it's
not a continuous function, so is not terribly well-defined). A
default definition for signum x might read
signum x = let a = abs x in if (a == 0) then 0 else x / abs x
(Possibly signum is the wrong name. What is the standard name for
this operation for, e.g., matrices?) [Er, on second thoughts, it's
not as well-defined as I thought. Abs x needs to be in a field for
the definition above to work.]
> I'll tell you anyway that if you try to "sanitize" the numeric
> classes, if you separate additive structures and the multiplication,
> if you finally define abstract Vectors over some field of scalars,
> and if you demand the existence of a generic normalization for your
> vectors, than *most probably* you will need multiparametric classes
> with dependencies.
Multiparametric classes, certainly (for Vectors, at least).
Fortunately, they will be in Haskell 2 with high probability. I'm not
convinced about dependencies yet.
> Jerzy Karczmarczuk
> Caen, France
Best,
Dylan Thurston
Footnotes:
[1] I'm not sure what I mean by "=" there, since I do not believe
these should be forced to be instances of Eq. For clearer cases,
consider the various Monad laws, e.g.,
join . join = join . map join
(Hope I got that right.) What does "=" mean there? Some sort of
denotational equality, I suppose.