我試圖做到這一點,以便在控制臺中
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 = Vertex 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
任何幫助深表感謝!
uj5u.com熱心網友回復:
fromInteger x = Vertex x
問題是x :: Integer那里,不是Num a => a。改為這樣做:
fromInteger x = Vertex (fromInteger x)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/474024.html
標籤:哈斯克尔
