包含模块继承的函子不起作用 [英] A functor including an inheritance of modules does not work

查看:85
本文介绍了包含模块继承的函子不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在为模块的设计和实现而苦苦挣扎,我定义了以下内容:

I am still struggling with my design and implementation of modules, I have defined the following:

module type MATRIX =
sig
  type 'a t
  val init: 'a -> 'a t
  val print: 'a t -> unit
end

module type MMM =
sig
  type 'a t
end

module type AMATRIX =
sig
  include MATRIX
  module Mmm : MMM
  module Matrix: MATRIX
  val matrix_of_amatrix: 'a t -> int -> int -> 'a Matrix.t
end

module MatrixArray: MATRIX =
struct
  type 'a t = 'a array array
  let init (e: 'a) : 'a t = failwith "to do"
  let print (x: 'a t) : unit = failwith "to do"
end

module MmmArray: MMM =
struct
  type 'a t = 'a array array
end

还有一个继承了模块的函子:

And a functor with an inheritance of modules:

module AMatrixArrayFun: functor (Mmm: MMM) -> functor (Matrix: MATRIX) -> AMATRIX =
  functor (Mmm: MMM) ->
    functor (Matrix: MATRIX) ->
struct
  include MatrixArray
  module Mmm = Mmm
  module Matrix = Matrix

  let matrix_of_amatrix (m: 'a t) (nr_i: int) (nc_i: int) : 'a Matrix.t = failwith "to do"
end

module AMatrixArray = AMatrixArrayFun(MmmArray)(MatrixArray)

let () =  MatrixArray.print (AMatrixArray.matrix_of_amatrix (AMatrixArray.init 5) 0 0)

编译停止在最后一行,并显示错误消息:

The compilation stop at the last line, and gives an error:

Error: This expression has type
         int AMatrixArray.Matrix.t =
           int AMatrixArrayFun(MmmArray)(MatrixArray).Matrix.t
       but an expression was expected of type 'a MatrixArray.t

因此,似乎编译器无法识别int AMatrixArrayFun(MmmArray)(MatrixArray).Matrix.t实际上是'a MatrixArray.t,但这正是函子的用途,对吗?也许是模块的继承使事情变得复杂了?

So it seems that the compiler does not recognize that int AMatrixArrayFun(MmmArray)(MatrixArray).Matrix.t is actually 'a MatrixArray.t, but this is exactly what a functor is meant to do, right? Maybe it is the inheritance of modules which complicates the thing?

有人可以帮忙吗?

推荐答案

您已将AMatrixArrayFun仿函数的签名强制为:

You have coerced the signature of your AMatrixArrayFun functor to be:

functor (Mmm: MMM) -> functor (Matrix: MATRIX) -> AMATRIX

编写此命令将消除返回的模块与MmmMatrix参数之间的任何类型关系.仅允许OCaml知道它是AMATRIX,但不允许知道它的Matrix子模块实际上是作为函子参数传递的Matrix模块.

Writing this eliminates any type relationships between the returned modules and the Mmm and Matrix argument. OCaml is only allowed to known that it's an AMATRIX, but not that its Matrix submodule was actually the Matrix module passed as a functor argument.

您需要将此信息添加到仿函数签名中:

You need to add this information to the functor signature :

functor (Mmm: MMM) -> functor (Matrix: MATRIX) -> 
  AMATRIX with module Mmm = Mmm and  module Matrix = Matrix

这篇关于包含模块继承的函子不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆