help wanted with type error message

Simon Peyton-Jones
Thu, 5 Apr 2001 01:15:43 -0700

| A row has a formula and a weight.
| > data Formula f =3D> Row a =3D Row (f a, Weight)
| Line 14 is the definition of Row.  Can anyone explain the=20
| proper use of a context in a data definition?

All the type variables in the constructor arguments must be=20
introduced as type constructor args.  Thus, for example:

	data Formula f =3D> Row a f =3D Row (f a, Weight)

woudl be ok.  But I bet you wanted to say "f a is a formula, that has
exactly the operations of class Formula available".  You probably don't
want the 'f' to appear in the type (Row a f). =20

In that case you need an exisitential type (not Haskell 98), which GHC=20
and Hugs support thus:

	data Row a =3D  forall f. Formula f =3D> Row (f a, Weight)

The existential is written 'forall' because the type of the Row
constructor really is
	Row :: forall a f. Formula f =3D> (f a, Weight) -> Row a

Now you can pattern-match thus:

	f :: Row a -> Environment a -> Bool
	f (Row (x,w)) =3D eval x env

A context on the LHS of a data type declaration is (in my opinion) one
of Haskell's
few blunders.  It makes the constructor into an overloaded function,

	Row :: (Formula f) =3D> (f a, Weight) -> Row a

That forces (Formula f) to hold at the call site of Row, but when you
match it isn't.  So it simply makes fewer programs typecheck without
expressiveness.  Indeed, as you found, it's positively misleading.