在haskell中的num实例中定义frominteger函数
我试图做到这一点,以便在控制台中以下
1 :: algebraicgraph int
将产生
顶点1
data AlgebraicGraph a
= Empty
| Vertex a
| Overlay (AlgebraicGraph a) (AlgebraicGraph a)
| Connect (AlgebraicGraph a) (AlgebraicGraph a)
instance Num a => Num (AlgebraicGraph a) where
fromInteger x = Vertex x
(+) x y = Overlay x y
(*) x y = Connect x y
但是,当我尝试Frominteger X =顶点X时,我得到的是
Couldn't match type `a' with `Integer'
`a' is a rigid type variable bound by
the instance declaration
at AlgebraicGraph.hs:120:10-40
Expected type: AlgebraicGraph a
Actual type: AlgebraicGraph Integer
* In the expression: Vertex x
In an equation for `fromInteger': fromInteger x = Vertex x
In the instance declaration for `Num (AlgebraicGraph a)'
* Relevant bindings include
fromInteger :: Integer -> AlgebraicGraph a
任何帮助都非常感谢!
I tried to make it so that in the console the following
1 :: AlgebraicGraph Int
Will produce
Vertex 1
data AlgebraicGraph a
= Empty
| Vertex a
| Overlay (AlgebraicGraph a) (AlgebraicGraph a)
| Connect (AlgebraicGraph a) (AlgebraicGraph a)
instance Num a => Num (AlgebraicGraph a) where
fromInteger x = Vertex x
(+) x y = Overlay x y
(*) x y = Connect x y
But when I try fromInteger x = Vertex x, what I get is
Couldn't match type `a' with `Integer'
`a' is a rigid type variable bound by
the instance declaration
at AlgebraicGraph.hs:120:10-40
Expected type: AlgebraicGraph a
Actual type: AlgebraicGraph Integer
* In the expression: Vertex x
In an equation for `fromInteger': fromInteger x = Vertex x
In the instance declaration for `Num (AlgebraicGraph a)'
* Relevant bindings include
fromInteger :: Integer -> AlgebraicGraph a
Any help is much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
X :: Integer
而不是num a =>
。而是这样做:The problem is that
x :: Integer
there, notNum a => a
. Do this instead: