Rigid type variables match error

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

Rigid type variables match error

Darryn-2
Hi, I wonder if anyone can explain what is going on here and what to do
about it. I'm fairly new to Haskell, so apologies in advance if my
question seems naive. I've cut my code down to a minimum that reproduces
the problem. I receive an error in GHCI in the following code,
complaining that it cannot match the rigid type variables for the
instance definition for Ainst for the function a3. Can anyone advise
about what to do about it?

------------------------------------------------
class A a where
    a1 :: a
    a2 :: a -> a
    a3 :: (B b) => b -> a

class B b where
    b1 :: Int -> b

data (B b) => Ainst b = I | J (Ainst b) | K b

instance (B b) => A (Ainst b) where
    a1 = I
--  a2 :: (B b, A a) => a -> a
    a2 = J
--  a3 :: (B b, A a) => b -> a
    a3 = K                 -- Error!
--  a3 = K `asTypeOf` a3   -- Error even with this!

data Binst = Val Int

instance B Binst where
    b1 = Val
------------------------------------------------

Test5.hs:17:9:
    Couldn't match expected type `b1' against inferred type `b'
      `b1' is a rigid type variable bound by
           the instance declaration at Test5.hs:12:12
      `b' is a rigid type variable bound by
          the type signature for `a3' at Test5.hs:5:13
      Expected type: b -> Ainst b1
      Inferred type: b -> Ainst b
    In the expression: K `asTypeOf` a3
    In the definition of `a3': a3 = K `asTypeOf` a3
Failed, modules loaded: none.



Reply | Threaded
Open this post in threaded view
|

Rigid type variables match error

Felipe Lessa
On Sat, Jun 27, 2009 at 10:18:02AM +0930, Darryn wrote:
> class A a where
>     a1 :: a
>     a2 :: a -> a
>     a3 :: (B b) => b -> a

It is clear here that 'b' is different from 'a' on 'a3's
definition, right? Now let's rename 'b' to 'c' on your instance:

> -- note that 'a = Ainst c' here.
> instance (B c) => A (Ainst c) where
>     a1 :: Ainst c
>     a1 = I
>     a2 :: Ainst c -> Ainst c
>     a2 = J
>     a3 :: (B b) => b -> Ainst c
>     a3 = ...

Oops!  We have 'K :: (B b) => b -> Ainst b', but 'a3 :: (B b) =>
b -> Ainst c' is more general than that.

Hope that helps,

--
Felipe.