您无法在OCaml中定义一个类型族,arrays of length n
其中n
可以具有任意长度。然而,可以使用其他机制来确保您只有GpuVector.sub
兼容长度的阵列。
最容易实现的机制是为长度为9的GpuVector
定义一个特殊模块,您可以通过使用函数来概括9。这里是一个模块GpuVectorFixedLength
的示例实现:
module GpuVectorFixedLength =
struct
module type P =
sig
val length : int
end
module type S =
sig
type t
val length : int
val create : int -> t
val sub : t -> t -> t
end
module Make(Parameter:P): S =
struct
type t = GpuVector.t
let length = Parameter.length
let create x = GpuVector.create length x
let sub = GpuVector.sub
end
end
您可以使用此说,例如
module GpuVectorHuge = GpuVectorFixedLength.Make(struct let length = 10_000_000 end)
module GpuVectorTiny = GpuVectorFixedLength.Make(struct let length = 9 end)
let x = GpuVectorHuge.create 1
let y = GpuVectorTiny.create 1
的z
的定义,然后由编译器拒绝:
let z = GpuVector.sub x y
^
Error: This expression has type GpuVectorHuge.t
but an expression was expected of type int array
因此,我们在类型系统中成功地反映了具有相同长度的两个数组的属性。您可以利用模块包含来快速实现完整的仿函数。
OCaml本身没有依赖类型,它可以用来静态检查数组长度不匹配。你可以做的一件事是使用幻像类型,它携带一个幻像编码数组长度。这些编码可以使用CamlP4从整型常量自动生成......但这不是依赖类型,可能不足以满足您的需求。 – camlspotter
@camlspotter我刚刚找到你的评论。有类似的问题。你能提供一些关于将自然数编码为类型的提示/指针吗? – krokodil
@ krokodil,现在你可以尝试一巴掌,就像Pierre在他的回答中指出的那样。 – camlspotter