Parsing bug in GHC 6.4.1 ?
Hello, 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: Parsing.lhs:7:5: Pattern bindings (except simple variables) not allowed in instance declarations 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 behave. Cheers, Bruno
On 08/01/06, Bruno Oliveira <bruno.oliveira@comlab.ox.ac.uk> wrote:
Hello,
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:
Parsing.lhs:7:5: Pattern bindings (except simple variables) not allowed in instance declarations 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 behave.
Cheers,
Bruno
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. - Cale
Bruno Oliveira writes:
class Foo o where (:+) :: o -> o -> o
The Haskell report specifies in section "2.4 Identifiers and Operators": An operator symbol starting with a colon is a constructor. Think of ':' as a character that is interpreted as "uppercase"; you can't use it to start a function or variable name. Peter
On 08 Jan 2006 16:37:47 +0100, Peter Simons wrote:
Bruno Oliveira writes:
class Foo o where (:+) :: o -> o -> o
The Haskell report specifies in section "2.4 Identifiers and Operators":
An operator symbol starting with a colon is a constructor.
Think of ':' as a character that is interpreted as "uppercase"; you can't use it to start a function or variable name.
Then this declaration should be rejected as invalid, right? That's why I think this is a parsing bug... Cheers, Bruno
On 08/01/06, Bruno Oliveira <bruno.oliveira@comlab.ox.ac.uk> wrote:
On 08 Jan 2006 16:37:47 +0100, Peter Simons wrote:
Bruno Oliveira writes:
class Foo o where (:+) :: o -> o -> o
The Haskell report specifies in section "2.4 Identifiers and Operators":
An operator symbol starting with a colon is a constructor.
Think of ':' as a character that is interpreted as "uppercase"; you can't use it to start a function or variable name.
Then this declaration should be rejected as invalid, right?
That's why I think this is a parsing bug...
Cheers,
Bruno
Hmm... yeah, it does seem like a parsing bug. The relevant rules from the report are: ---- topdecl -> ... | class [scontext =>] tycls tyvar [where cdecls] ... cdecls -> { cdecl1 ; ... ; cdecln } (n>=0) cdecl -> gendecl | ... gendecl -> vars :: [context =>] type (type signature) | ... vars -> var1 , ..., varn (n>=1) var -> varid | ( varsym ) varsym -> ( symbol {symbol | :})<reservedop | dashes> ---- So it should not be permitting a type declaration for something starting with a colon, since symbol does not match colon.
participants (3)
-
Bruno Oliveira -
Cale Gibbard -
Peter Simons