在haskell中的num实例中定义frominteger函数

发布于 2025-01-27 14:25:01 字数 1017 浏览 4 评论 0原文

我试图做到这一点,以便在控制台中以下

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原野 2025-02-03 14:25:01
fromInteger x = Vertex x

问题是X :: Integer而不是num a =>。而是这样做:

fromInteger x = Vertex (fromInteger x)
fromInteger x = Vertex x

The problem is that x :: Integer there, not Num a => a. Do this instead:

fromInteger x = Vertex (fromInteger x)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文