假设我正在编写矩阵模块
module type MAT =
sig
type dtypes
type 'a vec
type mat
val nan : dtypes
val make : rows:int -> cols:int -> mat
val copy_col : mat -> int -> dtypes vec
val write_col : dtypes vec -> mat -> int -> unit
val row : mat -> int -> dtypes vec
end;;
Run Code Online (Sandbox Code Playgroud)
具体实施
module MyMat(C:CONSTRAINTS) : (MAT with type dtypes = C.dtypes) =
struct
type dtypes = C.dtypes
type 'a vec = 'a array
type mat = C.dtypes vec vec
let nan = C.nan
let make ~rows ~cols = Array.make_matrix rows cols nan
let copy_col mat int =
let rows = Array.length mat in
let copy = Array.make rows nan in
let rec aux n =
if (n = rows) then copy else (copy.(n) <- mat.(n).(int); aux (n + 1))
in aux 0
let write_col vec mat int =
let rows = Array.length mat in
let rec aux n =
if (n = rows) then () else (mat.(n).(int) <- vec.(n);aux (n+1))
in aux 0
let row m n = m.(n)
end;;
Run Code Online (Sandbox Code Playgroud)
在具体实现中,Array模块用于vec和mat.我有三个功能copy_col,write_col并且row其收益从/到矩阵/写切片.我想保持模块抽象的,所以我没有指定的类型'a vec或mat在签名.但是,这隐藏了类型,'a array所以当我使用这些函数时,我无法执行诸如此类的数组操作a.().有没有办法公开抽象类型,MyMat同时仍然保留它MAT?
你想要以下吗?
module MyMat(C:CONSTRAINTS) : (MAT with type dtypes = C.dtypes
and type 'a vec = 'a array) = ...
Run Code Online (Sandbox Code Playgroud)