[Hugs-users] overloading operators

Richard A. O'Keefe ok at cs.otago.ac.nz
Wed Apr 13 18:05:08 EDT 2005


Alfredo Paz-Valderrama <apaz at star.com.pe>
is confused by / in Haskell.
	1 / 2
	
	I get 0.5 by answer, but the / operator signature is:
	
	float -> float -> float
	
No, it certainly is NOT that.  At the Hugs prompt, type

    :type (/)
and you will see the answer
    (/) :: Fractional a => a -> a -> a

That is, for any type "a" which belongs to the Fractional type-class,
there is a version of (/) which takes two arguments of type "a" and
returns a value of type "a".

	1) Why don't fail?
	
Because the definition of Haskell says it mustn't.
Do read a good tutorial, or textbook, or the report.

	2) 1 and 2 are not integers?
	
Not exactly.  Or more precisely, not _just_ integers.
Again, at the Hugs prompt, type

    :type 1
and you will see the answer
    1 :: Num a => a

That is, for any type "a" which belongs to the Num type-class,
1 can be considered as a member of type "a".

More precisely, it is as if integer literals like 1 and 2 belonged
to the type Integer (unbounded integers) but were always wrapped up
as (fromInteger 1) or (fromInteger 2), where

    fromInteger :: Num a => Integer -> a

converts an unbounded integer to whatever numeric type the context
requires.  A similar rule applies to numeric literals with decimal
points.  Technically those are exact unbounded rational numbers with
fromRational wrapped around them.

	3) can this fact be source of confusions?
	
No.  Quite the opposite, in fact.  Haskell seems to cause much less surprise
with arithmetic than any other typed programming language I know.  This all
relies on typeclasses, which are a fundamental and pervasive part of Haskell.



More information about the Hugs-Users mailing list