[Haskell-cafe] Adding Ord constraint to instance Monad Set?

MR K P SCHUPKE k.schupke at imperial.ac.uk
Wed Mar 31 14:42:07 EST 2004


> 	data Either a =3D
> 		Num a =3D> Left a
> 		Ord a =3D> Right a

This is a mistake because the same variable is being constrained in 
two different ways. The only way both constrains can be satisfied 
is if a is both in Num and Ord. This can be written:

	data (Num a,Ord a) => Either a = Left a | Right a

Which makes it a lot clearer that the constaints apply to both
the Left and Right constructor. 

You have to remember that a is a polymorphic variable... now the
first line [Num a => Left a] says that a has the type (forall a . Num a)
but the second line [Ord a => Right a] says a has the type (forall a . Ord a)
These are not the same type, so when they are combined (remember a scopes over
the whole type definition) there is a type error because:

	(forall a . Ord a) is not the same as (forall a . Num a)

	Regards,
	Keean.


More information about the Haskell-Cafe mailing list