[Haskell-cafe] Rigid type variable error
Jason Dagit
dagit at codersbase.com
Sat Jun 27 02:58:11 EDT 2009
On Fri, Jun 26, 2009 at 8:48 PM, Darryn <djreid at aapt.net.au> wrote:
> From:
> Darryn <djreid at aapt.net.au>
> To:
> beginners at haskell.org
> Subject:
> Rigid type variables match error
> Date:
> Sat, 27 Jun 2009 10:18:13 +0930
>
>
> 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.
>
> Thanks in advance for any help anyone can provide.
Let me rephrase your definitions slightly:
instance (B b1) => A (Ainst b1) where
a1 = I
-- a2 :: (B b1, A a) => a -> a
a2 = J
-- a3 :: (B b1, A a) => b -> a
a3 = K -- Error!
-- a3 = K `asTypeOf` a3 -- Error even with this!
This is the way the type system sees your types. Notice that in the type
signature of a3, it has no way to know that b1 is the same as b, and worse
yet, b is totally free and b1 is not (must be an instance of B), so it
thinks they cannot be unified. Which is to say, in general they may range
over different types.
I would recommend removing the type class constraint from the definition of
Ainst. I haven't tried it, but I think that will make life easier for you.
Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090627/88874689/attachment.html
More information about the Haskell-Cafe
mailing list