OCaml 编译器需要不相关的自定义类型

OCaml compiler expects an irrelevant custom type

type int_tree =
  | IntLeaf
  | IntNode of int * int option * int_tree * int_tree * int_tree 

let empty_int_tree = IntLeaf

let rec int_insert x t = match t with
  IntLeaf -> (x, None, IntLeaf, IntLeaf, IntLeaf) |
  IntNode(a,None,c,d,e) -> if x<a then IntNode(x, Some a, c, d, e) else if x>a then IntNode(a, Some x, c, d, e) else t |
  IntNode(a, Some b, c, d, e) -> if x<a then IntNode(a, Some b, int_insert x c, d, e)
  else if x>a && x<b then IntNode(a, Some b, c, int_insert x d, e)
  else if x>b then IntNode(a,Some b, c, d, int_insert x e)
  else t

在上面的代码中,行

"IntNode(a,None,c,d,e) -> if x<a then IntNode(x, Some a, c, d, e) else if x>a then IntNode(a, Some x, c, d, e) else t |"

抛出一个错误

Error: This expression has type int_tree
       but an expression was expected of type
         int * 'a option * int_tree * int_tree * int_tree

IntNode(x, Some a, c, d, e) 部分带有下划线。这个表达式应该是 int_tree 类型,而函数 returns 是 int_tree,那么为什么编译器想要类型为 "int * 'a option * int_tree * int_tree * int_tree”?这甚至不是一种有意义的类型。如何阻止编译器将 a、b、c、d、e... 误解为通用类型?通常你在 match 语句中编写类似 h::t 的东西没有问题,编译器知道你所说的 h 和 t.

的意思

原来解决方案就在我眼皮底下。第一行是错误的。它需要与第一行相同的类型,解决方案是在那里添加 IntNode 以使其成为

IntLeaf -> IntNode(x, None, IntLeaf, IntLeaf, IntLeaf) |