[Haskell-cafe] What is a number. (Was: Num instances for 2-dimensional types)

Jason Dagit dagit at codersbase.com
Tue Oct 6 02:49:08 EDT 2009

On Mon, Oct 5, 2009 at 11:02 AM, Sönke Hahn <shahn at cs.tu-berlin.de> wrote:

> >
> > On 5 Oct 2009, at 21:06, Lennart Augustsson wrote:
> > > OK, "just pairs" have no arithmetic, but one way of defining
> > > arithmetic is to treat the pairs as complex numbers.  Or as mantissa
> > > and exponent.  Or as something else.  So there's nothing wrong, IMO,
> > > to make pairs an instance of Num if you so desire.  (Though I'd
> > > probably introduce a new type.)
> I was looking for one intuitive way of instantiating pairs of numbers as
> Num.
> One where i wouldn't have to look in the documentation to remember what (+)
> does. So considering all these examples of what pairs of numbers could be,
> i
> agree with Miguel Mitrofanov: "It's something that should not be done."
> Two questions remain, i think:
> 1. How can i have some notion of addition and subtraction for something
> that i
> don't want to put in the Num class?
> That's easy: I could just write a class that defines binary operators (like
> (+~) and (-~), for example). I don't get  the fanciness of (+), but that's
> okay.

I don't know how well it works in practice, but you could hide the prelude
in your module and then import it qualified.  This would allow your module
to redefine the normal symbols as the operators you want them to be.

> 2. How can i use numeric literals to construct values, whose types are not
> in
> the Num class?

This I don't know how to do without a pre-processor or maybe Template
Haskell would work.

> Haskell could provide a class IsNumber (like IsString for string literals).
> Is
> that something we would want?

Seems like IsBoolean would also be useful if you went this route.  And
perhaps, IsList :)  I'm sure we could think of more.  I can see how they
would be handy for anyone making an eDSL, especially if combine with view

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20091006/05f0122a/attachment.html

More information about the Haskell-Cafe mailing list