Parsing bug in GHC 6.4.1 ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Parsing bug in GHC 6.4.1 ?

Bruno Oliveira-5
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


_______________________________________________
Haskell mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell
Reply | Threaded
Open this post in threaded view
|

Re: Parsing bug in GHC 6.4.1 ?

Cale Gibbard
On 08/01/06, Bruno Oliveira <[hidden email]> 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
_______________________________________________
Haskell mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell
Reply | Threaded
Open this post in threaded view
|

Re: Parsing bug in GHC 6.4.1 ?

Peter Simons
In reply to this post by Bruno Oliveira-5
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

_______________________________________________
Haskell mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell
Reply | Threaded
Open this post in threaded view
|

Re: Re: Parsing bug in GHC 6.4.1 ?

Bruno Oliveira-5
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


_______________________________________________
Haskell mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell
Reply | Threaded
Open this post in threaded view
|

Re: Re: Parsing bug in GHC 6.4.1 ?

Cale Gibbard
On 08/01/06, Bruno Oliveira <[hidden email]> 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.
_______________________________________________
Haskell mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell