Opened 3 years ago

Last modified 3 years ago

#12120 new bug

GHC accepts invalid Haskell: `class Eq (a Int) => C a where`

Reported by: thomie Owned by:
Priority: lowest Milestone:
Component: Compiler Version: 8.0.1
Keywords: report-impact Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Documentation bug Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


From the Haskell 2010 report chapter 4,

  • Class and instance declarations:
    | 	class [scontext =>] ...
    | 	instance [scontext =>] ...
  • Normal type signatures:
    	vars :: [context =>] ...

Notice the difference between scontext (with s) and context (without s).

scontext 	→ 	simpleclass
	| 	( simpleclass1 , … , simpleclassn ) 	    (n ≥ 0)
simpleclass 	→ 	qtycls tyvar
context 	→ 	class
	| 	( class1 , … , classn ) 	    (n ≥ 0)
class 	→ 	qtycls tyvar
	| 	qtycls ( tyvar atype1 … atypen ) 	    (n ≥ 1)

GHC seems to ignore this difference, and happily accepts class Eq (a Int) => C a where. Hugs (Version: September 2006) reports for that same example:

Illegal Haskell 98 class constraint in class declaration
*** Constraint : Eq (a Int)

Change History (2)

comment:1 Changed 3 years ago by thomie

I didn't see this mentioned in It probably should be, so that the Haskell 2020 commitee sees it, and perhaps lifts this restriction on class and instance declarations.

Edit: GHC 8 also accepts the following program without -XFlexibleContexts:

data Apply f a = Apply (f a)
instance Show (f a) => Show (Apply f a) where
  show = error "write me"


Last edited 3 years ago by thomie (previous) (diff)

comment:2 Changed 3 years ago by simonpj

Yes, as mentioned in #12121, I think this is a bug in GHC. Fixing it will probably mean that some working programs break, which will make people upset. Maybe we should just document it as a bug that we don't propose to fix?

Note: See TracTickets for help on using tickets.