定义由函子构建的 2 个并行模块上的函数
我仍在努力解决我的设计和实现问题,认为它取得了进展...
首先,我定义了 2 个基本签名和 2 个模块:
module type MATRIX = sig type 'a t end
module MatrixArray: MATRIX = struct
type 'a t = 'a array array
end
module type MMM = sig type 'a t end
module MmmArray: MMM = struct
type 'a t = 'a array array
end
然后我定义了 3 个签名、3 个函子并将它们与上面的基本模块一起应用:
module type AMATRIX = sig
include MATRIX
module Mmm : MMM
module Matrix: MATRIX
val g: 'a Mmm.t -> 'a Matrix.t -> 'a Mmm.t * 'a Matrix.t
end
module AMatrixFun (Mmm: MMM) (Matrix: MATRIX) : AMATRIX with module Mmm = Mmm and module Matrix = Matrix = struct
include MatrixArray
module Mmm = Mmm
module Matrix = Matrix
let g (mmm: 'a Mmm.t) (dbm: 'a Matrix.t) : 'a Mmm.t * 'a Matrix.t = failwith "to do"
end
module AMatrixArray = AMatrixFun(MmmArray)(MatrixArray)
module type VIDBM = sig
module Matrix: MATRIX
type t = | Dtop | Dbot | D of int Matrix.t
end
module ViDbmFun (Matrix: MATRIX) : VIDBM with module Matrix = Matrix = struct
module Matrix = Matrix
type t = | Dtop | Dbot | D of int Matrix.t
end
module ViDbmArray = ViDbmFun(MatrixArray)
module type AREAMMM = sig
module Mmm: MMM
type t = | Mtop | Mbot | M of int Mmm.t
end
module AreaMmmFun (Mmm: MMM) : AREAMMM with module Mmm = Mmm = struct
module Mmm = Mmm
type t = | Mtop | Mbot | M of int Mmm.t
let f (am: t) (vd: ViDbmArray.t) : t * ViDbmArray.t =
let (M mmm), (ViDbmArray.D dbm) = am, vd in
(AMatrixArray.g mmm dbm);
failwith "to do"
end
module AreaMmmArray = AreaMmmFun(MmmArray)
实际上我需要定义一个函数f: AreaMmmArray.t -> ViDbmArray.t-> AreaMmmArray.t * ViDbmArray.t
需要另一个函数 g: 'a Mmm.t ->; '矩阵.t -> 'a Mmm.t * 'a Matrix.t
。由于涉及到多个并行模块的类型,所以我主要的问题是应该在哪些模块中定义它们。
在上面的代码中,作为尝试,我在 ViDbmFun
中实现了 f
,在 AMatrixFun
中实现了 g
。编译在 (AMatrixArray.g mmm dbm);
处停止,并给出:
Error: This expression has type int Mmm.t = int Mmm.t
but an expression was expected of type
'a AMatrixArray.Mmm.t = 'a MmmArray.t
我认为该错误是合理的,因为 int Mmm.t
in AreaMmmFun
> 可能是 AMatrixArray
中强制的 MmmArray.t
以外的东西...有没有办法解决这个问题?
再说一遍,我认为主要问题是在哪里定义 f
和 g
,有人可以帮忙吗?
I am still struggling with my design and implementation, thought it progresses...
First, I have defined 2 basic signatures and 2 modules:
module type MATRIX = sig type 'a t end
module MatrixArray: MATRIX = struct
type 'a t = 'a array array
end
module type MMM = sig type 'a t end
module MmmArray: MMM = struct
type 'a t = 'a array array
end
Then I have defined 3 signatures, 3 functors and applied them with the basic modules above:
module type AMATRIX = sig
include MATRIX
module Mmm : MMM
module Matrix: MATRIX
val g: 'a Mmm.t -> 'a Matrix.t -> 'a Mmm.t * 'a Matrix.t
end
module AMatrixFun (Mmm: MMM) (Matrix: MATRIX) : AMATRIX with module Mmm = Mmm and module Matrix = Matrix = struct
include MatrixArray
module Mmm = Mmm
module Matrix = Matrix
let g (mmm: 'a Mmm.t) (dbm: 'a Matrix.t) : 'a Mmm.t * 'a Matrix.t = failwith "to do"
end
module AMatrixArray = AMatrixFun(MmmArray)(MatrixArray)
module type VIDBM = sig
module Matrix: MATRIX
type t = | Dtop | Dbot | D of int Matrix.t
end
module ViDbmFun (Matrix: MATRIX) : VIDBM with module Matrix = Matrix = struct
module Matrix = Matrix
type t = | Dtop | Dbot | D of int Matrix.t
end
module ViDbmArray = ViDbmFun(MatrixArray)
module type AREAMMM = sig
module Mmm: MMM
type t = | Mtop | Mbot | M of int Mmm.t
end
module AreaMmmFun (Mmm: MMM) : AREAMMM with module Mmm = Mmm = struct
module Mmm = Mmm
type t = | Mtop | Mbot | M of int Mmm.t
let f (am: t) (vd: ViDbmArray.t) : t * ViDbmArray.t =
let (M mmm), (ViDbmArray.D dbm) = am, vd in
(AMatrixArray.g mmm dbm);
failwith "to do"
end
module AreaMmmArray = AreaMmmFun(MmmArray)
Actually I need to define a function f: AreaMmmArray.t -> ViDbmArray.t -> AreaMmmArray.t * ViDbmArray.t
which requires another function g: 'a Mmm.t -> 'a Matrix.t -> 'a Mmm.t * 'a Matrix.t
. As it involves the types of several parallel modules, my major question is in which modules I should define them.
In the code above, as a try, I have implemented f
in ViDbmFun
and g
in AMatrixFun
. The compilation stops at (AMatrixArray.g mmm dbm);
and gives me:
Error: This expression has type int Mmm.t = int Mmm.t
but an expression was expected of type
'a AMatrixArray.Mmm.t = 'a MmmArray.t
I think the error is reasonable, because int Mmm.t
in AreaMmmFun
may be something other than MmmArray.t
forced in AMatrixArray
... Is there a way to work around this?
Again, I think the major question is where to define f
and g
, could anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定您想要实现什么,但正如您所说,引用
AreaMmm
函子中的特定AMatrix
实例没有意义。只有两种解决方案:您要么需要在带有模块 Mmm = Mmm 的 AMATRIX 实例上参数化AreaMmm
函子,要么必须在函子内构造一个合适的实例。I'm not sure what you are trying to achieve, but as you say, referring to the specific
AMatrix
instance in theAreaMmm
functor does not make sense. There are only two solutions: you either need to parameterize theAreaMmm
functor over an instance ofAMATRIX with module Mmm = Mmm
, or you have to construct a suitable instance inside the functor.我想说你确实没有达到正确的定义水平。
Matrix
和Mmm
是独立的,放在一起构成一个AMatrix。在这里,您尝试在
FooMmmFun函子中定义一个同时涉及
Matrix和
Mmm的函数,该函子只知道
Mmm<代码>,而不是矩阵<代码>;使用特定实例MatrixArray` 不是解决方案,并且尝试执行此操作时会出现类型错误。您必须在了解
Matrix
(或ViDbm
)和Mmm
(或<代码>AreaMmm)。这是我的建议,在AREAMMM
签名声明之后添加到您的代码中。它主要为AMatrix
定义一个函子化层,就像您为Matrix
和MMM
所做的那样。PS:您可以为顶部/底部封闭的类型定义参数类型,以避免构造函数名称重复。在任何模块之外:
然后您可以将
VIDBM.t
定义为int Matrix.t order
,将AREAMMM.t
定义为int Mmm .t order
,而不是使用两个不兼容的类型和构造函数系列(Mtop
、Dtop
...)。I'd say you are not at the right level of definition indeed.
Matrix
andMmm
are independent, and put together to make anAMatrix. Here you try to define a function that speaks about both
Matrixand
Mmmin a
FooMmmFunfunctor, which only knows about
Mmm, not
Matrix; using the particular instance
MatrixArray` is not the solution, and you get a type error trying to do that.You have to define
f
at a level that knows about bothMatrix
(orViDbm
) andMmm
(orAreaMmm
). Here is my suggestion, to add to your code after the declaration of theAREAMMM
signature. It mostly define a functorized layer forAMatrix
, as you did forMatrix
andMMM
.PS: you could define a parametric type for types closed by top/bottom, to avoid duplication of constructor names. Outside of any module:
Then you can define
VIDBM.t
asint Matrix.t order
, andAREAMMM.t
asint Mmm.t order
, instead of having two incompatible types and families of constructors (Mtop
,Dtop
...).