由函子构建的两个模块之间的类型关系
我定义了几个签名和模块,如下:
module type MATRIX =
sig
type 'a t
val init: 'a -> 'a t
end
module type MMM =
sig
type 'a t
end
module type AMATRIX =
sig
include MATRIX
module Mmm : MMM
module Matrix: MATRIX
val mmm_of_amatrix: 'a t -> int -> int -> 'a Mmm.t
end
module type AREAMMM =
sig
type t
module Mmm: MMM
val make: int Mmm.t -> t
end
module MatrixArray: MATRIX =
struct
type 'a t = 'a array array
let init (e: 'a) : 'a t = failwith "to do"
end
module MmmArray: MMM =
struct
type 'a t = 'a array array
end
和两个函子:
module AMatrixFun: functor (Mmm: MMM) -> functor (Matrix: MATRIX) -> AMATRIX
with
module Mmm = Mmm
and
module Matrix = Matrix =
functor (Mmm: MMM) -> functor (Matrix: MATRIX) ->
struct
include MatrixArray
module Mmm = Mmm
module Matrix = Matrix
let mmm_of_amatrix (m: 'a t) (nr_i: int) (nc_i: int) : 'a Mmm.t = failwith "to do"
end
module AreaMmmFun : functor (Mmm: MMM) -> AREAMMM
with module Mmm = Mmm =
functor (Mmm: MMM) ->
struct
type t
module Mmm = Mmm
let make (x: int Mmm.t) : t = failwith "to do"
end
以及带有一些模块的应用函子:
module AMatrix = AMatrixFun(MmmArray)(MatrixArray)
module AreaMmm = AreaMmmFun(MmmArray)
let a = AMatrix.mmm_of_amatrix (AMatrix.init 5) 0 0
let b = AreaMmm.make a
编译在这段代码上运行良好。但是,如果我将最后一部分更改为
module AMatrix : AMATRIX = AMatrixFun(MmmArray)(MatrixArray)
module AreaMmm : AREAMMM = AreaMmmFun(MmmArray)
let a = AMatrix.mmm_of_amatrix (AMatrix.init 5) 0 0
let b = AreaMmm.make a
编译在最后一行停止,并给出:
File "lib/tools.ml", line 69, characters 21-22:
Error: This expression has type int AMatrix.Mmm.t
but an expression was expected of type int AreaMmm.Mmm.t
我真的想将 : AMATRIX
添加到 AMatrix
和 : AREAMMM
到 AreaMmm
,因为这确保我这两个模块受到这两个签名的约束。有人可以帮忙吗?
I have defined several signatures and modules as follows:
module type MATRIX =
sig
type 'a t
val init: 'a -> 'a t
end
module type MMM =
sig
type 'a t
end
module type AMATRIX =
sig
include MATRIX
module Mmm : MMM
module Matrix: MATRIX
val mmm_of_amatrix: 'a t -> int -> int -> 'a Mmm.t
end
module type AREAMMM =
sig
type t
module Mmm: MMM
val make: int Mmm.t -> t
end
module MatrixArray: MATRIX =
struct
type 'a t = 'a array array
let init (e: 'a) : 'a t = failwith "to do"
end
module MmmArray: MMM =
struct
type 'a t = 'a array array
end
And two functors:
module AMatrixFun: functor (Mmm: MMM) -> functor (Matrix: MATRIX) -> AMATRIX
with
module Mmm = Mmm
and
module Matrix = Matrix =
functor (Mmm: MMM) -> functor (Matrix: MATRIX) ->
struct
include MatrixArray
module Mmm = Mmm
module Matrix = Matrix
let mmm_of_amatrix (m: 'a t) (nr_i: int) (nc_i: int) : 'a Mmm.t = failwith "to do"
end
module AreaMmmFun : functor (Mmm: MMM) -> AREAMMM
with module Mmm = Mmm =
functor (Mmm: MMM) ->
struct
type t
module Mmm = Mmm
let make (x: int Mmm.t) : t = failwith "to do"
end
And applied functors with some modules:
module AMatrix = AMatrixFun(MmmArray)(MatrixArray)
module AreaMmm = AreaMmmFun(MmmArray)
let a = AMatrix.mmm_of_amatrix (AMatrix.init 5) 0 0
let b = AreaMmm.make a
The compilation works fine on this piece of code. However, if I change the last part to
module AMatrix : AMATRIX = AMatrixFun(MmmArray)(MatrixArray)
module AreaMmm : AREAMMM = AreaMmmFun(MmmArray)
let a = AMatrix.mmm_of_amatrix (AMatrix.init 5) 0 0
let b = AreaMmm.make a
the compilation stops at the last line, and gives me:
File "lib/tools.ml", line 69, characters 21-22:
Error: This expression has type int AMatrix.Mmm.t
but an expression was expected of type int AreaMmm.Mmm.t
I really want to add : AMATRIX
to AMatrix
and : AREAMMM
to AreaMmm
, because that ensures me that these two modules are constrained with these two signatures. Could anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我仍然不知道你想要实现什么,但如果你的目标是在模块之间实现某种继承(正如你在之前的一些线程标题中所说),你可能需要使用递归模块。
例如,您首先定义模块层次结构:这里
S
是初始模块签名,可以通过S1
和S2
扩展:然后,您创建具体的实现,它准确定义了
'a t
,并实现了S1
和S2
所需的所有函数:然后,您将类型
'a t
抽象出来通过使用正确的签名来限制实施。您在这里需要递归模块(请注意rec
关键字):您可以测试您的结果:
它按预期返回
0
。I have still no idea of what you try to achieve, but if your goal is to implement some kind of inheritance between you modules (as you said in some previous thread title), you may need to use recursive modules.
So for instance, you start by defining your module hierarchy: here
S
is the initial module signature, which can extended byS1
andS2
:Then, you create your concrete implementation, which define exactly what is
'a t
, and which implements all the functions needed by bothS1
andS2
:Then, you abstract the type
'a t
away by constraining the implementation with the right signatures. You need recursive modules here (note therec
keyword):And you can test your result:
which returns
0
as expected.您的应用程序需要知道
AMatrix.Mmm.t
与AreaMmm.Mmm.t
相同,因为否则您将无法在另一个模块提供的函数中使用一个模块创建的值。正如我之前所解释的,如果您断言
module AMatrix : AMATRIX
,那么您将丢弃AMATRIX< 中未包含的有关
AMatrix
内容的任何其他信息/代码>。特别是,由于AMATRIX
没有提及AMatrix.Mmm.t
与AreaMmm.Mmm.t
相同,因此该信息会丢失并且该代码无法编译。你应该断言的是:
Your application needs to know that
AMatrix.Mmm.t
is the same asAreaMmm.Mmm.t
, because otherwise you will not be able to use values created by one module in functions provided by the other module.As I previously explained, if you assert that
module AMatrix : AMATRIX
then you are throwing away any additional information about the contents ofAMatrix
that is not contained inAMATRIX
. In particular, sinceAMATRIX
does not mention thatAMatrix.Mmm.t
is the same asAreaMmm.Mmm.t
, that information is lost and the code does not compile.What you should assert instead is that: