隐藏签名中的类型参数

Tho*_*mas 3 ocaml functional-programming

有没有理由说OCaml无法在签名中展开中间参数化类型?

例如:

(* foo.ml *)
type 'a internal = Foo of 'a
type t = string internal
Run Code Online (Sandbox Code Playgroud)

和:

(* foo.mli *)
type t = Foo of string
Run Code Online (Sandbox Code Playgroud)

给出错误.

我想这与内存表示有时可能不同的事实相关,但我想知道在向OCaml错误跟踪器提交错误报告之前是否有更深层次的原因...

And*_*erg 5

只要类型是结构类型,它就可以,例如:

type 'a t = 'a * int
type u = string t
Run Code Online (Sandbox Code Playgroud)

会匹配

type u = string * int
Run Code Online (Sandbox Code Playgroud)

但是,变体(和记录)是OCaml 中的标称类型.也就是说,这种类型的每个声明都引入了一个新的类型名称.签名中的名义类型规范只能与名义类型声明匹配,并且它们必须具有等效定义.在您的示例中也不是这种情况,因此不被接受.(这是OCaml的一个微妙角落.结构类型别名和名义类型定义共享相同语法的事实无济于事.)

FWIW,你也可以重新标识名义类型:

type 'a t = Foo of 'a
type 'a u = 'a t = Foo of 'a
Run Code Online (Sandbox Code Playgroud)

会匹配

type 'a u = Foo of 'a
Run Code Online (Sandbox Code Playgroud)

但是这也不允许你改变结构或参数,所以对你的情况没有帮助.