[Hugs-users] overloading operators

Scott Turner p.turner at computer.org
Tue Apr 12 23:32:08 EDT 2005


On 2005 April 12 Tuesday 23:12, Alfredo Paz-Valderrama wrote:
> If i write this in hugs:
>
> 1 / 2
>
> I get 0.5 by answer, but the / operator signature is:
>
> float -> float -> float

1 and 2 are overloaded. They can be Integer, Float, etc.

You can think of 2 as meaning (fromInteger 2). FromInteger is defined in the 
Prelude to convert an Integer to any numeric type.

Haskell organizes overloading using type classes. The type class called Num 
contains fromInteger, +, -, and *.  So these operators along with the integer 
literals 1,2, etc. can be used with any numeric type. You can define your own 
numeric type (for example polynomials) and use 1,2, and + with that.

Actually, the / operator is not limited to Float, either. It is overloaded for 
the type class "Fractional", which includes Double and Rational.  Hugs will 
tell you the type is
   Float -> Float -> Float
because there's a defaulting mechanism so that if multiple types are possible, 
Hugs can choose one and give you an answer.


More information about the Hugs-Users mailing list