Section 4.2.1 of the Haskell Report 1.2 gives the following example of
an algebraic datatype:

        data Eq a => Set a = NilSet | ConsSet a (Set a) deriving Eq

Its constructor types are

        NilSet  :: Forall a . Set a
        ConsSet :: Forall a . Eq a => a -> Set a -> Set a

The type of NilSet has an empty context, since the context of the type
of a data constructor is restricted to those type variables free in
the component types of this data constructor.  The purpose of this is
to avoid constructing a dictionary for an unspecified type.

It seems that all three Haskell compilers known to me (hbc, ghc, Yale)
infer the type

        NilSet  :: Forall a . Eq a => Set a

for the constructor NilSet, as seen below.  Consequently, the
expression

        NilSet == NilSet

leads to ambiguous overloading and produces a type error.

Was this changed after the report was published or is it a coincidence
that all three compilers forget to restrict the context?  Any
clarification will be appreciated.

-Konstantin [[EMAIL PROTECTED]]


$ hbi
Welcome to interactive Haskell B. version 0.999.3 SPARC 1993 Mar  2!
Loading prelude... 1116 values, 72 types found.
Type "help;" to get help.
> data Eq a => Set a = NilSet | ConsSet a (Set a) deriving Eq;
data (Eq a) => Set a = NilSet | ConsSet a (Set a) deriving (Eq)
> NilSet == NilSet;
[55] Ambiguously overloaded tyvar(s) in class(es) Eq in  show ((==) NilSet NilSet)
> whatis NilSet;
constructor NilSet :: (Eq a) => Set a


$ glhc
Enter your Haskell program, end with ^D (on a line of its own):
data Eq a => Set a = NilSet | ConsSet a (Set a) deriving Eq
v = NilSet == NilSet
main = done
"/tmp/ghc18277.hs", line 2: Ambiguous overloading:
    class "Eq", type "a" (at a use of an overloaded identifier: ==)
Fail: Compilation errors found
glhc: execution of the Haskell compiler had trouble


$ haskell
Yale Haskell Y2.0-b4   CMU Common Lisp version 16f on SPARCstation
Type :? for help.
Main> data Eq a => Set a = NilSet | ConsSet a (Set a) deriving Eq
Main> :save
Main> = (NilSet == NilSet)
Main> :eval
[NON-DEFAULTABLE-AMBIGUOUS-CONTEXT] Phase error in phase TYPE:
An ambiguous context, (Eq), cannot be defaulted.
Ambiguity in call to ==
Error occurred at line 3 in file interactive.

Reply via email to