haskell - Haskell中的实际类型与预期类型错误

标签 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)不同吗?

最佳答案

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

type TreeRN x = Tree (Color, x)

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

关于haskell - Haskell中的实际类型与预期类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42356413/

相关文章:

haskell - 在haskell中为数据类型定义特殊的Ord

types - Go - math.MaxInt64 和类型推断错误

c - 什么是 C 中的 vuint,为什么它在我的微 Controller 中?

compiler-errors - Java中的范围检查编译困难

java - “Cannot find symbol”或 “Cannot resolve symbol”错误是什么意思?

haskell - 使用 rank-N 类型翻转

haskell - 在 Haskell 中评估零函数

haskell - 如何创建类型之间的映射

c# - 如何从抽象类中的静态方法获取当前类型?

java - 找不到符号错误- Swing