Overloading and Literal Numerics

CREMIEUX Alain alain.cremieux@cegedim.fr
Thu, 27 Jun 2002 12:49:14 +0200


Jon Fairbairn wrote :
> Hi,
> I am trying to create an overloaded function "à la Java" to be able to
> call it either with a string or a number.
> Ex :
> definePort "http"
> definePort 80
> but I have problem with restrictions in Haskell's type system

> Is there a better solution ?

If we knew /why/ you wanted to do this we might be able to
help.  I can't see why you want to allow Strings, which have
far too wide a range of values, as arguments to something
that takes a port designator as an argument.

data Port = Tcpmux | Nbp | Echo_ddp | Rje | Zip | Echo_tcp | ...
             deriving Enum, ...

instance Num Port where ...

would seem like a better way to me.

  Jón


I am trying to build a functional firewall generator. The first part
describes the available protections (kernel, anti-address spoofing, etc.).
The second desribes every protocol, and the necessary rules if the
corresponding service is enabled (e.g. open the http port...). In the third
one, the user will choose the services he wants to use/open and the static
parameters (for instance the squid port number).
I wanted the user part to be "user-friendly", even if it is an Haskell
program. So the commands
definePort "squidPort" 3128
Seemed more logical than
definePort "squidPort" "3128"

The problem is that the numeric literal 3128 is considered as being a member
of Num class, and not as beeing an Int.
So I can't write a unique function which accepts 1) the string "3128" 2) the
literal numeric 3128   3) the string "3128:3129"(if the user wants to give a
port range, for instance)

This kind of problem is adressed in the paper :
http://www.cse.ogi.edu/~mbs/pub/overloading/ where the "closed" class
extension seems to solve this kind of ambiguous type difficulty. But it does
not seem to have been implemented yet

Alain