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

发布于 2025-01-01 15:37:16 字数 2355 浏览 7 评论 0原文

我仍在努力解决我的设计和实现问题,认为它取得了进展...

首先,我定义了 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 以外的东西...有没有办法解决这个问题?

再说一遍,我认为主要问题是在哪里定义 fg,有人可以帮忙吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸵鸟症 2025-01-08 15:37:16

我不确定您想要实现什么,但正如您所说,引用 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 the AreaMmm functor does not make sense. There are only two solutions: you either need to parameterize the AreaMmm functor over an instance of AMATRIX with module Mmm = Mmm, or you have to construct a suitable instance inside the functor.

心头的小情儿 2025-01-08 15:37:16

我想说你确实没有达到正确的定义水平。 MatrixMmm 是独立的,放在一起构成一个 AMatrix。在这里,您尝试在FooMmmFun函子中定义一个同时涉及MatrixMmm的函数,该函子只知道Mmm<代码>,而不是矩阵<代码>;使用特定实例MatrixArray` 不是解决方案,并且尝试执行此操作时会出现类型错误。

您必须在了解Matrix(或ViDbm)和Mmm(或<代码>AreaMmm)。这是我的建议,在 AREAMMM 签名声明之后添加到您的代码中。它主要为 AMatrix 定义一个函子化层,就像您为 MatrixMMM 所做的那样。

(* 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,而不是使用两个不兼容的类型和构造函数系列(MtopDtop...)。

I'd say you are not at the right level of definition indeed. Matrix and Mmm are independent, and put together to make an AMatrix. Here you try to define a function that speaks about bothMatrixandMmmin aFooMmmFunfunctor, which only knows aboutMmm, notMatrix; using the particular instanceMatrixArray` 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 both Matrix (or ViDbm) and Mmm (or AreaMmm). Here is my suggestion, to add to your code after the declaration of the AREAMMM signature. It mostly define a functorized layer for AMatrix, as you did for Matrix and 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: you could define a parametric type for types closed by top/bottom, to avoid duplication of constructor names. Outside of any module:

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

Then you can define VIDBM.t as int Matrix.t order, and AREAMMM.t as int Mmm.t order, instead of having two incompatible types and families of constructors (Mtop, Dtop...).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文