Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  type class for custom numeric types (Ashish Agarwal)
   2. Re:  type class for custom numeric types (Stephen Tetley)


----------------------------------------------------------------------

Message: 1
Date: Thu, 8 Apr 2010 22:42:48 -0400
From: Ashish Agarwal <[email protected]>
Subject: [Haskell-beginners] type class for custom numeric types
To: Haskell-beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

I'd like to define an abstract type T, and some how make the following work:

> 3 :: T
-- allow integer literals to be read as values of type T

> 100 :: T
error: value out of bounds for type T
-- place some restrictions on which integers are valid T values

To get the first feature, is my only option to make T an instance of the Num
class? If so, I'd have to make fromIntegral return error for out of bounds
values. Is there any problem in doing that? The only law I find mentioned
for instances of Num is: abs x * signum x == x. Is this the complete
requirement?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100408/285d69cd/attachment-0001.html

------------------------------

Message: 2
Date: Fri, 9 Apr 2010 09:30:04 +0100
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] type class for custom numeric types
Cc: Haskell-beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hello Ashish

Yes - to get integer literals for a type, you have to make that type
an instance of Num.

Making fromIntegral throw an out-of-bounds error is as much a problem
as using error anywhere in your program. You get into the usual
trade-off between robustness (handling errors, never dying) and
clarity - often robustness isn't the primary virtue. If you wanted,
you could use coercion in the fromIntegral implementation - say values
over 100 are truncated to 100 rather than throw an error.

Ideally a Num type should support the usual properties of addition,
multiplication etc. (associative, commutative, identity for mult, ...)
there is no enforcement of this though, and there are some strange Num
instances "in the wild".

Best wishes

Stephen


------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 22, Issue 13
*****************************************

Reply via email to