help wanted with type error message
I'm learning Haskell, and I'm having trouble understanding the meaning of a `context' as applied to the declaration of an algebraic datatype. I would like to say the following: A formula is a predicate over environments.
class Formula f where eval :: f a -> Environment a -> Bool -- evaluate the formula eq :: Var -> a -> f a -- basic equality (&) :: f a -> f a -> f a -- conjunction
type Environment a = Var -> a type Var = String
A row has a formula and a weight.
data Formula f => Row a = Row (f a, Weight) type Weight = Float
Hugs rejects this program: ERROR "hard.lhs" (line 14): Undefined type variable "f" Line 14 is the definition of Row. Can anyone explain the proper use of a context in a data definition? Thanks, Norman
Norman Ramsey wrote:
data Formula f => Row a = Row (f a, Weight) type Weight = Float
Hugs rejects this program:
ERROR "hard.lhs" (line 14): Undefined type variable "f"
Right, f is not bound in the declaration of Row. Only the occurances of type variables after the type constructor are binding occurances. The fix is to turn Row into a binary constructor:
data Formula f => Row f a = Row (f a, Weight)
BTW, contexts have no real meaning in data declarations - the declaration above does not prevent you from writing
type T = Row [] Int
Cheers, - Andreas -- Andreas Rossberg, rossberg@ps.uni-sb.de "Computer games don't affect kids. If Pac Man affected us as kids, we would all be running around in darkened rooms, munching pills, and listening to repetitive music."
participants (2)
-
Andreas Rossberg -
Norman Ramsey