[Haskell-cafe] Overloading

ok at cs.otago.ac.nz ok at cs.otago.ac.nz
Sun Mar 10 10:16:43 CET 2013


>
> In C++ it is perfectly normal to have overloaded functions like
>
> f : Int -> Int -> Int
> f : Int -> Char -> Int

Something that may not be obvious about Haskell is that
Haskell does NOT have overloaded functions/operators at all.

More precisely, for any identifier and any point in a
Haskell module, there is at most ONE definition of that
identifier that is in scope at that point.

More precisely, we can think of a function has having
two parts: an *interface* which specifies its type and zero
or more *implementations* which specify its behaviour, all
of which must have types that match or are special cases
of that interface.
For any identfier and any point in a Haskell module,
there is at most one INTERFACE for that identifier that
is in scope at that point, so there is no possible doubt
about the type of that identifier.

As an example, the standard Prelude has *one* interface
for +, namely
    (+) :: Num t => t -> t -> t
and it offers a number of implementations of + (in
'instance' declarations) for various types.
There are additional implementations in other modules,
but they all must have types that are instances of this one.

I don't believe that partial (Curried) application has
anything to do with it.  Torsors would need multiparameter
type classes so that
     g + t :: t
     t - t :: g
and so on, but Haskell originally didn't have multiparameter
type classes.





More information about the Haskell-Cafe mailing list