Opened 7 years ago

Closed 7 years ago

Last modified 6 years ago

#2237 closed bug (invalid)

qualified names not permitted everywhere in instances

Reported by: claus Owned by:
Priority: normal Milestone:
Component: Compiler (Parser) Version: 6.9
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Test Case:
Blocked By: Blocking:
Related Tickets: Differential Revisions:

Description

consider using a class that is imported qualified only:

  • we cannot define instance methods, as their qualified names are not accepted (this restriction makes sense at top-level, but not in instances)
  • we cannot refer to instances in instance contexts, as their qualified names are not accepted (they are accepted in class contexts, and -XFlexibleInstances accepts them in instance contexts)

Attachments (1)

QI.hs (255 bytes) - added by claus 7 years ago.
qualified names in instance contexts and method definitions

Download all attachments as: .zip

Change History (9)

Changed 7 years ago by claus

qualified names in instance contexts and method definitions

comment:1 follow-up: Changed 7 years ago by simonpj

  • difficulty set to Unknown

Looks fine to me. In your example program

  • You don't need the qualification on fmap; it's unambiguous without.
  • The instance for Dope really isn't H98 and is rejected for that reason.

This works:

module Foo where

import qualified Prelude

data T a = MkT

instance (Prelude.Show a) => Prelude.Show (T a) where
  show = Prelude.error "urk"

class Prelude.Show a => Dope a
instance Prelude.Show a => Dope (T a)

comment:2 in reply to: ↑ 1 Changed 7 years ago by claus

Replying to simonpj:

Looks fine to me. In your example program

  • You don't need the qualification on fmap; it's unambiguous without.

but it isn't in scope! isn't it a bug that i can define fmap if it isn't in scope, but not Prelude.fmap, even though it is? and the unambiguity doesn't go all that far, either:

module QI where

import Prelude hiding (Functor(..))
import qualified Prelude (Functor(..))

data X a = X a deriving Show

instance Prelude.Functor X where 
  fmap f (X a) = X (f a)
    where q = (reverse fmap,Prelude.fmap not [True],reverse QI.fmap)

fmap = "fmap"

in q, fmap appears to refer to QI.fmap, not to Prelude.fmap, as it does in the fmap definition..

  • The instance for Dope really isn't H98 and is rejected for that reason.

oops, yes. sorry about that (i'm constantly amazed at what isn't H98!-).

comment:3 Changed 7 years ago by simonpj

GHC is faithfully implementing Section 4.3.2 of the Haskell Report. You nay not like it, but that's what it's doing. I think. If, having read the section (esp the sentence "This rule is identical to...") then get back to me.

S

comment:4 follow-up: Changed 7 years ago by simonpj

  • Resolution set to invalid
  • Status changed from new to closed

OK I think we are agreeing that GHC implements the spec. We can argue separately about whether the language should be changed!

Simon

comment:5 in reply to: ↑ 4 Changed 7 years ago by claus

Replying to simonpj:

OK I think we are agreeing that GHC implements the spec. We can argue separately about whether the language should be changed!

yes, agreed. one suggestion: could GHC point to the spec for this error message in the case of an instance method definition? something like

Qualified name in instance method definition: ..
please refer to Section 4.3.2 of the Haskell 98 report

as part of the message would have spared me the confusion, by pointing out that (a) the compiler is aware of the non-toplevel context and (b) there is a spec reason for the restriction.

comment:6 Changed 7 years ago by simonpj

Good suggestion. I had a look, but it's really not convenient to do this. Haskell never allows a qualified name in a binding position, and it's the code that parses bindings that emits the error message. Of course this'd be fixable by adding some more context information, but it looks like more than just adding a boolean flag to me.

So I think other feature requests are probably higher priority. But if you'd like to have a go yourself, the error comes from RdrHsSyn.checkFunBind.

Simon

comment:7 Changed 6 years ago by simonmar

  • Architecture changed from Unknown to Unknown/Multiple

comment:8 Changed 6 years ago by simonmar

  • Operating System changed from Unknown to Unknown/Multiple
Note: See TracTickets for help on using tickets.