【发布时间】:2011-09-25 07:52:50
【问题描述】:
你好 Haskellers 和 Haskellettes,
我已经在 Haskell 上摆弄了很长一段时间,但是我无法完全掌握这个类的概念。在以下示例中,我的数据类型为 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)
导致
Type constructor `Val' used as a class In the definition
of data constructor `Leaf' In the data type declaration for `ExprTree'
我也试过了
data ExprTree' = Leaf {lab::Label, val::Val}
...
但随机更改类型签名 - 听起来既不高效也不提供启发。
现在据我所知Num a 表示 Num 类的东西,但这不是数据类型的实例 - 并且不允许我编译。
那么我必须做什么才能使ExprTree 定义明确。
提前感谢您的提示和想法!
编辑:
1) 感谢您的快速解答!
2) 我将val::(Val a)=>a 更改为val::Val a
我有类似的想法 - 但随后出现错误:Not in scope type variable a 发生
你还有什么建议吗??
【问题讨论】:
-
注意答案包括左侧的类型变量
a:data ExprTree a = ... -
伙计,有时我比一块石头还笨 - 谢谢 ;-)