Ocaml 为什么可以';t多态性变体的闭合子集类型对照超集检查?

Ocaml 为什么可以';t多态性变体的闭合子集类型对照超集检查?,ocaml,Ocaml,变体[`A |`B]与类似[`A |`B |`C]的超集类型不兼容 我理解除非将添加到子集或将[`a]=fun->`a 让foo:bool->[`A |`B]=返回A 接受实现似乎非常安全,因为声明的foo类型是实现类型(即return\u a类型)的严格超集。但是(如预期)它不会进行类型检查: Error: This expression has type bool -> [ `A ] but an expression was expected of type bool ->

变体
[`A |`B]
与类似
[`A |`B |`C]
的超集类型不兼容

我理解除非将
添加到子集或将
[`a]=fun->`a
让foo:bool->[`A |`B]=返回A
接受实现似乎非常安全,因为声明的
foo
类型是实现类型(即
return\u a
类型)的严格超集。但是(如预期)它不会进行类型检查:

Error: This expression has type bool -> [ `A ]
but an expression was expected of type bool -> [ `A | `B ]
The first variant type does not allow tag(s) `B
在我看来,它实际上是一种比

return\u a:bool->[`a]=fun\uu->`a
让foo:bool->[<`A |`B]=返回A
哪一个进行类型检查


这种对多态变体使用的限制是否仅仅是对类型推断工作方式的限制,还是有实际理由将第一个片段标记为类型错误?

类型
[`a |`B]
[`a |`B |`C]
的一个子类型。OCaml支持子类型,但必须是显式的

# type abc = [`A | `B | `C];;
type abc = [ `A | `B | `C ]
# type ab = [`A | `B];;
type ab = [ `A | `B ]
# let f (x: abc) = 14;;
val f : abc -> int = <fun>
# let (x: ab) = `A;;
val x : ab = `A
# f x;;
Error: This expression has type ab but an expression was expected of type abc      
       The first variant type does not allow tag(s) `C
# f (x :> abc);;
- : int = 14
#
#键入abc=[`A |`B |`C];;
输入abc=[`A |`B |`C]
#ab型=[`A |`B];;
ab型=[`A |`B]
#设f(x:abc)=14;;
val f:abc->int=
#设(x:ab)=`A;;
val x:ab=`A
#fx;;
错误:此表达式的类型为ab,但应为abc类型的表达式
第一个变量类型不允许标记'C'
#f(x:>abc);;
-:int=14
#

是的,我理解。我的问题更多的是,这种限制是防止了实际的错误,还是更多的是ocaml用来统一类型的算法的产物。