[Haskell] Parsing bug in GHC 6.4.1 ?
cgibbard at gmail.com
Sun Jan 8 10:33:15 EST 2006
On 08/01/06, Bruno Oliveira <bruno.oliveira at comlab.ox.ac.uk> wrote:
> The following class definition:
> > class Foo o where
> > (:+) :: o -> o -> o
> and even the following function definition:
> > bar f (x,y) = x :+ y
> are accepted by GHC. However, when I try to create
> one instance of Foo:
> > instance Foo Int where
> > x :+ y = x + y
> I get the following error message:
> Pattern bindings (except simple variables) not allowed in instance
> x :+ y = x + y
> The same error still occurs if I change the infix operator to be (:+:).
> However, if I define:
> > class Foo3 o where
> > (<+>) :: o -> o -> o
> > instance Foo3 Int where
> > x <+> y = x + y
> Everything works as expected.
> The only explanation that I have is that this is a (parsing) bug in GHC...
> This is probably related to the fact that
> > (:+) :: Int -> Int -> Int
> > f :+ g = f + g
> is an invalid definition (it complains that ":+" is not a data constructor).
> I have not tried this code in other Haskell compiler (like Hugs) or even
> previous versions of GHC. I would be interested to know how do those
Infix operators which start with a colon are reserved for use as data
constructors. Names starting with an uppercase letter are reserved in
the same way. You can define a type:
data Complex a = a :+ a
and write values of type Complex Double like (1.0 :+ pi), but you
can't use :+ as the name of an ordinary function.
I'm not sure if it's ideal that the class declaration is allowing that
type signature to occur, but afaict, the syntax does permit it.
More information about the Haskell