Hello Haskellers and Haskellettes,
I’ve been fiddling around with Haskell for quite a while but there is this concept of classes I cannot quite grasp. In the following example I have the datatype of ExprTree
data Val a = Num a | Var String deriving (Eq, Ord, Show)
data ExprTree = Leaf {lab::Label, val::(Val a)=> a}
| Node {lab::Label, fun::Fun, lBranch::ExprTree, rBranch::ExprTree}
deriving(Eq,Ord)
which leads to
Type constructor `Val' used as a class In the definition
of data constructor `Leaf' In the data type declaration for `ExprTree'
i also tried
data ExprTree' = Leaf {lab::Label, val::Val}
...
but randomly changing type signature – neither sounds efficent nor provides enlightenment.
now as far as i know Num a denotes something of class Num but is this is no instance of a datatype – and doesn’t let me compile.
So what do i have to do in order to make ExprTree well defined.
Thanks in advance for hints and ideas!
Edit:
1) Thanks for the fast answers!
2) I changed the val::(Val a)=>a to val::Val a
i had something similar in mind – but then the error: Not in scope type variable a occurs
do you have additional advice ??
The correct type would be
Since the type
Valrequires an additional type parameter, you need to provide one whenever you use it*. I used a type variable, just as in the initial definition; that requires the variable to also be named as a parameter toExprTree. (The other possibilities would be to use a concrete type such asIntorMaybe String, etc., or to use an existential type; neither makes sense here.)What you actually used was a typeclass context (“class” is just shorthand for “typeclass”).
Valis a type, not a typeclass, so it’s not legal there.* This isn’t quite true; you need a type of kind
*. Kinds are the types of types:Inthas kind*,Val ahas kind*,Valhas kind* -> *. That is, by itselfValis a type function which requires a parameter in order to become a full type.