Types 定义由函子构建的2个并行模块上的函数

Types 定义由函子构建的2个并行模块上的函数,types,module,ocaml,functor,Types,Module,Ocaml,Functor,我仍然在为我的设计和实现而奋斗,虽然它在进步 首先,我定义了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

我仍然在为我的设计和实现而奋斗,虽然它在进步

首先,我定义了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->'a Matrix.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
mmmfun
可能不是
MmmArray.t
强制进入
AMatrixArray
。。。有办法解决这个问题吗


同样,我认为主要的问题是在哪里定义
f
g
,有人能帮忙吗?

我不确定您想要实现什么,但正如您所说,在
区域mmm
functor中引用特定的
AMatrix
实例没有意义。只有两种解决方案:要么需要在模块为Mmm=Mmm的
AMATRIX的实例上参数化
AreaMmm
函子,要么必须在函子内部构造一个合适的实例。

我想说,您的定义级别确实不正确
矩阵
Mmm
是独立的,它们组合在一起构成
AMatrix。在这里,您尝试定义一个函数,该函数在一个
foomfun
函子中同时涉及
Matrix
Mmm
,它只知道
Mmm
,而不知道
Matrix
;使用特定实例
MatrixArray`不是解决方案,尝试这样做时会出现类型错误

您必须在了解
Matrix
(或
ViDbm
)和
Mmm
(或
AreaMmm
)的级别定义
f
。以下是我的建议,在声明
AREAMMM
签名后添加到您的代码中。它主要为
AMatrix
定义一个功能化层,就像您为
Matrix
MMM
所做的那样

(* does not speak about `f` as no `Matrix` is available *)
module AreaMmmFun (Mmm: MMM) : AREAMMM with module Mmm = Mmm = struct
  module Mmm = Mmm
  type t = | Mtop | Mbot | M of int Mmm.t
end
module AreaMmmArray  = AreaMmmFun(MmmArray)

(* a functor over `AMatrix`, that knows both `Matrix` and `Mmm`,
   so we can define `f` here. I don't know which signature you want. *)
module AMatrixFun (AMatrix : AMATRIX)
= struct
  module ViDbm = ViDbmFun(AMatrix.Matrix)
  module AreaMmm = AreaMmmFun(AMatrix.Mmm)

  let f (am: AreaMmm.t) (vd: ViDbm.t) : AreaMmm.t * ViDbm.t =
    let (AreaMmm.M mmm), (ViDbm.D dbm) = am, vd in
    (AMatrix.g mmm dbm);
    failwith "to do"
end

module AmatrixFooArray = AMatrixFun(AMatrixArray)
PS:您可以为按顶部/底部关闭的类型定义参数化类型,以避免重复构造函数名称。在任何模块之外:

type 'a order = Top | Bot | D of 'a

然后您可以将
VIDBM.t
定义为
int-Matrix.t-order
,将
AREAMMM.t
定义为
int-Mmm.t-order
,而不是使用两个不兼容的构造函数类型和族(
Mtop
Dtop
)。

您的示例中缺少模块
Mmt
。抱歉,应该是
Mmm
,我已经修好了。。。