使用开放联合键入定义

ZJa*_*nes 5 ocaml types variant

1)我有一个开放的联合定义如下:

type 'a choice = [> `One | `Other ] as 'a
Run Code Online (Sandbox Code Playgroud)

然后我尝试定义一个类型choice_list:

type choice_list = choice list
Run Code Online (Sandbox Code Playgroud)

这不起作用.如何定义一个或多个组件是开放联合的类型?

2)如果我放弃创建choice_list类型,只需使用a choice list,当我尝试使用选择列表编写接口/签名语句时,

val choice_handler : choice list -> int
Run Code Online (Sandbox Code Playgroud)

编译器抱怨说type 'a choice = 'a constraint 'a = [> `One | `Other ] is not included in type infection_state. They have different arities.

我的问题是,如何在接口/签名中编写选择列表的类型声明.

Jef*_*eld 9

编译器试图告诉您这choice是一个参数化类型.在类型级别,它具有1的arity.换句话说,您需要提供一个类型参数.您已将参数约束为子类型[`One|`Other],但除此之外,它可以是任何类型:

# ([`One; `Third] : 'a choice list);;
- : [> `One | `Other | `Third ] choice list = [`One; `Third]
Run Code Online (Sandbox Code Playgroud)

如果要定义选项列表,则额外类型必须来自某个地方.即,它必须是新类型的参数:

# type 'a choice_list = 'a choice list;;
type 'a choice_list = 'a choice list constraint 'a = [> `One | `Other ]
Run Code Online (Sandbox Code Playgroud)

(根据我的经验,这些结构很快变得棘手.)