2012-02-01 40 views
0

我仍然与我的设计和实现奋力内置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 -> 'a Matrix.t -> 'a Mmm.t * 'a Matrix.t。由于它涉及几个并行模块的类型,我的主要问题是我应该在哪些模块中定义它们。

在上面的代码,作为一个尝试,我在ViDbmFungAMatrixFun实施f。编译停在(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可以AreaMmmFunMmmArray.t其他被迫AMatrixArray东西...有没有办法解决呢?

同样,我认为主要的问题是在哪里定义fg,有没有人可以帮忙?

+0

您的示例中缺少模块'Mmt'。 – nlucaroni 2012-02-01 20:49:21

+0

对不起,应该是'嗯',我已经修好了...... – SoftTimur 2012-02-01 20:56:57

回答

0

我会说你确实没有达到正确的定义水平。 MatrixMmm是独立的,并放在一起做一个AMatrix. Here you try to define a function that speaks about both矩阵andin a FooMmmFun functor, which only knows about, not矩阵; using the particular instance MatrixArray`是解决不了问题,你会得到一个类型错误试图做到这一点。

你必须要在它知道这两个Matrix(或ViDbm)和Mmm(或AreaMmm)的水平来定义f。这是我的建议,在申请AREAMMM签名后添加到您的代码中。它大多为AMatrix定义了一个函数化图层,就像您为MatrixMMM所做的那样。 PS:你可以为顶部/底部关闭的类型定义参数类型,以避免构造函数名称的重复。任何模块外:

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

然后你就可以定义为VIDBM.tint Matrix.t order,并AREAMMM.tint Mmm.t order,而不是有两个不兼容的类型和构造的家庭(MtopDtop ...)。

1

我不确定你想达到什么目的,但正如你所说,指的是AreaMmm函数中的具体AMatrix实例没有意义。只有两种解决方案:您需要通过AMATRIX with module Mmm = Mmm的实例参数化AreaMmm仿函数,或者您必须在仿函数内部构造一个合适的实例。