[This discussion was taking place on comp.lang.functional.  I've
copied it to the Haskell mailing list.  Basically, the original
question was whether constructors could be overloaded.  My answer
was that they could be, if they were nullary, in a similar way
to numbers, but I haven't thought this out in great detail.]

> Hi,
> 
> "True" is predined in the standard prelude as a boolean constant.
> But I'd like to use "True" as well as a nullary constructor in abstract
> type of Prolog goals:
> 
> data Goal = True | And Goal Goal | Or Goal Goal | Atom Pred Term ....
> 
> Could this overloading be allowed as long as the programmer specifies the 
> type explicitly using "::"?
> 
>       Thanks, Don 

In practice, yes, but I wouldn't want to put such a constraint
into a type inference algorithm (SML has this for numbers, and its
rather ugly).  In a more general sense, I think you can approximate
what you asked for as follows:

        class Bool a where
                True, False :: a

        data Bool = BTrue | BFalse

        instance Bool Bool where
                True = BTrue
                False = BFalse

        data Goal = GTrue | And Goal Goal | Or Goal Goal | Atom Pred Term ....

        instance Bool Goal where
                True :: GTrue

        and True = True
        and (And g1 g2) = g1 && g2

Now the translation of "and" would be:

        data CBool = True | False
        ...
        fromBool.Goal True = GTrue

        and x | x == fromBool True = fromBool True
        and (And g1 g2) = g1 && g2

or with dictionaries and static simplification:

        and x | ==.Goal x GTrue = BTrue
        and (And g1 g2) = g1 && g2

since the types of both argument and result can be inferred in this
case.  In my opinion, the real problems with this approach would be
deciding at definition time which constructors should be overloaded,
and perhaps user confusion when a "constructor" doesn't define a single
type:

        and :: (Bool a, Bool b, Bool c) => a -> b -> c
        and True True = True
        and _ _ = False

I suppose we do already have this with numbers!

Kevin


Reply via email to