[Haskell-cafe] Scope of type variables in associated types

Matthew Sackman matthew at wellquite.org
Mon May 21 07:58:06 EDT 2007


On Mon, May 21, 2007 at 12:39:20PM +0100, Simon Peyton-Jones wrote:
> | > | > instance G a b where
> | > | >     data E a = EC b -- this line - the b is not in scope.
> | > | >     wrap = EC
> | > | >     unwrap (EC b) = b
> | > |
> | > | I get "Not in scope: type variable `b'".
> | >
> | > That's a bug.  b should be in scope
> 
> I was wrong.  It's not a bug.  E is supposed to be a type *function*,
> so it can't mention anything on the RHS that is not bound on the LHS.
> We'll improve the documentation.

Ok, thanks for the clarification. One final question then, how could I
rewrite the following in associated types:

class OneStep a b | a -> b
instance OneStep (Cons v t) t

class TwoStep a b | a -> b
instance (OneStep a b, OneStep b c) => TwoStep a c

If the fundep and b is dropped then I get:

class OneStep a
    data OS a :: *
instance OneStep (Cons v t)
    data OS (Cons v t) = t

class TwoStep a
    data TS a :: *
instance (OneStep a, OneStep b) => TwoStep a

This last one can't be right, as I can't express the fact that the OS in
"OneStep a" provides the link with "OneStep b". So is there a way to
achieve this sort of chaining with associated types?

Sure, the fundep version requires undecidable instances, but I've been
writing quite a lot of code lately that requires that!

Many thanks,

Matthew
-- 
Matthew Sackman
http://www.wellquite.org/


More information about the Haskell-Cafe mailing list