Ocaml 可选参数的本地抽象类型和默认值

Ocaml 可选参数的本地抽象类型和默认值,ocaml,gadt,locally-abstract-type,Ocaml,Gadt,Locally Abstract Type,我正在为矩阵类型编写一个接口,其中包含两种可能的内部格式。以下是一个简化版本: type csc (* compressed-sparse-column format *) type csr (* compressed-sparse-row format *) type _ fmt = | CSC : csc fmt | CSR : csr fmt type 's matrix = { sparsetype : 's fmt } let newmat (type s) ?(fmt : s

我正在为矩阵类型编写一个接口,其中包含两种可能的内部格式。以下是一个简化版本:

type csc (* compressed-sparse-column format *)
type csr (* compressed-sparse-row format *)

type _ fmt =
| CSC : csc fmt
| CSR : csr fmt

type 's matrix = { sparsetype : 's fmt }

let newmat (type s) ?(fmt : s fmt = CSC) () = { sparsetype = fmt }
这不起作用,因为默认值
CSC
不是
s fmt
类型

这种想法似乎不可能奏效,因为不可能为签名中的类型变量(即签名)指定默认值

val newmat : ?(fmt:'s sformat) -> unit -> 's matrix
当未明确指定
fmt
时,可能需要指定
's=csc

有办法绕过这个限制吗


期望OCaml接受这样的定义是不合理的吗?

想要这样做并不是不合理的(我自己也曾多次想要这样做),但OCaml不接受这样的定义,而且理由相当充分。它是 在有人在场的情况下很难与推理结合起来 高阶函数

所以,我恐怕你必须坚持非选择性的论点,或者 几个功能