[Haskell-cafe] Help understanding type error

Levi Stephen levi.stephen at optusnet.com.au
Thu Sep 6 23:21:46 EDT 2007


I'm after some help understanding either what I'm doing wrong, or why this error 

I have a data type:

 > data T a = forall b. (Show b) => T b a

and I want to use/extract 'b' from this.

 > extShow (T b _) = b

This gives the following compilation error:

     Inferred type is less polymorphic than expected
       Quantified type variable `b' escapes
     When checking an existential match that binds
	$dShow :: {Show b}
	b :: b
     The pattern(s) have type(s): T t
     The body has type: b
     In the definition of `extShow': extShow (T b _) = b

I tried adding a type signature:

 > extShow' :: (Show b) => T a -> b
 > extShow' (T b _) = b

Now I get a different error:

     Couldn't match expected type `b' (a rigid variable)
	   against inferred type `b1' (a rigid variable)
       `b' is bound by the type signature for `extShow'' at extest.hs:7:18
       `b1' is bound by the pattern for `T' at extest.hs:8:10-14
     In the expression: b
     In the definition of `extShow'': extShow' (T b _) = b

It seems (to newbie me ;) ) like it should be possible to extract the first part 
of T, and deduce that it is an instance of the class 'Show'.

The following seems ok:

 > doShow (T b _) = putStrLn (show b)


More information about the Haskell-Cafe mailing list