Ticket #3958 (closed bug: invalid)

Opened 3 years ago

Last modified 3 years ago

method definitions fail to refer to method-context classes

Reported by: visionete Owned by:
Priority: normal Milestone:
Component: Compiler (Type checker) Version: 6.12.1
Keywords: Cc:
Operating System: Linux Architecture: x86_64 (amd64)
Type of failure: GHC rejects valid program Difficulty:
Test Case: Blocked By:
Blocking: Related Tickets:

Description

Sorry in advance if this is my lack of understanding...
The following code fails:

class Cfoo a where
    f :: Int -> a -> Int

data Foo = F Int

instance Cfoo Foo where
    f m (F n) = m + n

data Bar = B Int

class Cbar a where
    g :: (Cfoo b) => a -> b

instance Cbar Bar where
    g (B m) = F m

with error:

Couldn't match expected type `b' against inferred type `Foo'
      `b' is a rigid type variable bound by
          the type signature for `g' at [...]
    In the expression: F m
    In the definition of `g': g (B m) = F m
    In the instance declaration for `Cbar Bar'

It's as if the "instance Cfoo Foo" statement is ignored when processing "instance CBar Bar"

Change History

Changed 3 years ago by simonpj

  • status changed from new to closed
  • resolution set to invalid

The error message is quite right. 'g' has type

g :: forall a b.  (CBar a, CFoo b) => a -> b

In the instnace for CBar Bar, g must therefore have type

g :: forall b. CFoo b => Bar -> b

But it doesn't. The right hand side (F f) fixes the result type as Foo, not b.

Simon

Changed 3 years ago by visionete

  • status changed from closed to new
  • resolution invalid deleted
  • patch set to 0

Hello Simon, thanks for the response.

I'm afraid though that I don't understand what's meant by "the type b" as used above.
`b' is a type variable, to be substituted for a type at some point. I maintain that since:

(1) (F m) is of type Foo in the RHS of the definition of g in the body of the instance Cbar bar statement,
(2) Foo is an instance of CFoo,

that the type variable `b' should bind to the type Foo in the body of the instance Cbar bar statement.
I fail to understand what is meant by binding `b' rigidly to itself[?!] in the class Cbar statement.

Either this would appear to be a serious bug or I'm seriously confused! :-) Please help!

Declan

Changed 3 years ago by igloo

  • status changed from new to closed
  • resolution set to invalid

When someone calls

g :: (Cfoo b) => Bar -> b

they get to choose what type to use for b. They can choose any type for which there is a Cfoo instance.

For example,

fromInteger 5

has type

(Num a) => a

so I can use it with type Int or Double. fromInteger's implementation can't always return an Int.

If that doesn't clarify it, I suggest you ask on the  haskell-cafe mailing list.

Changed 3 years ago by visionete

Yes, but not at compile time... I had presumed that such type relations would want to have been supported. I see now, thanks

Note: See TracTickets for help on using tickets.