Haskell中实际类型的错误预期类型

时间:2017-02-21 00:09:02

标签: haskell types compiler-errors functional-programming

我在Haskell中声明了这些类型:

data Tree x = Node x (Tree x) (Tree x)
          |Leef
          deriving Show

data Color = R | N
    deriving (Show, Eq)

data TreeRN x = Tree (Color,x)
    deriving Show

和这个功能:

equilibre :: TreeRN a -> TreeRN a
equilibre (Node (N,z) (Node (R,y) (Node (R,x) a b) c) d) = (Node (R,y) (Node (N,x) a b) (Node (N,z) c d))
equilibre (Node (N,z) (Node (R,x) a (Node (R,y) b c)) d) = (Node (R,y) (Node (N,x) a b) (Node (N,z) c d))
equilibre (Node (N,x) a (Node (R,z) (Node (R,y) b c) d)) = (Node (R,y) (Node (N,x) a b) (Node (N,z) c d))
equilibre (Node (N,x) a (Node (R,y) b (Node (R,z) c d))) = (Node (R,y) (Node (N,x) a b) (Node (N,z) c d))

这是我的问题:

Couldn't match expected type ‘TreeRN a’
            with actual type ‘Tree (Color, t3)’
Relevant bindings include
  d :: Tree (Color, t3) (bound at test.hs:15:53)
  c :: Tree (Color, t3) (bound at test.hs:15:51)
  z :: t3 (bound at test.hs:15:48)
  b :: Tree (Color, t3) (bound at test.hs:15:37)
  y :: t3 (bound at test.hs:15:34)
  a :: Tree (Color, t3) (bound at test.hs:15:23)
  equilibre :: TreeRN a -> TreeRN a (bound at test.hs:12:1)
  (Some bindings suppressed; use -fmax-relevant-binds=N or -fno-max-relevant-binds)
In the expression:
  (Node (R, y) (Node (N, x) a b) (Node (N, z) c d))
In an equation for ‘equilibre’:
    equilibre (Node (N, x) a (Node (R, y) b (Node (R, z) c d)))
      = (Node (R, y) (Node (N, x) a b) (Node (N, z) c d))
Failed, modules loaded: none.

通常当我遇到这种错误时,我理解为什么,我可以纠正我的功能,但在这里我很失望

Couldn't match expected type ‘TreeRN a’
            with actual type ‘Tree (Color, t3)’

虽然我声明的类型TreeRN是相同的(对我来说),所以我需要回答为什么编译器会说' t3'这是什么意思?它与Tree(Color,a)真的不同吗?

1 个答案:

答案 0 :(得分:3)

您希望TreeRN x成为Tree (Color, x)的同义词。我认为您刚刚使用了错误的关键字 - data定义了一种新的代数数据类型。尝试

type TreeRN x = Tree (Color, x)

您还需要删除对类型同义词无效的deriving子句。

相关问题