Ocaml函数,模块和子模块

时间:2011-08-02 04:57:00

标签: module ocaml functor

发布这样长而不可编辑的代码的道歉。但是尽管在ocaml的仿函数上阅读了stackoverflow上的几个问题和答案,我还是得不到如何解决这个问题:

假设我有一个非常抽象的数据结构:

ads.mli

module type ENTRY = sig
    type t
    val get_index : t -> int
    val compare : t -> t -> int
end

module type T = sig
    type entry
    type t
    val create : unit -> t
    val insert : entry -> t -> t
    val delete : entry -> t -> t
end
基于此,我可以通过传递函数在这些抽象实现上建立具体的数据结构。例如,我做了:

concrete_ads.mli

module Make (Entry: Ads.ENTRY) : (ads.T with type entry = Entry.t)

这项工作,我现在可以在其他源文件中使用我的实现,例如:

module AT = Concrete_ads.Make( 
    type t = int * int;; 
    let get_index = fst;; 
    let to_string = (fun (x,y) -> Printf "%i, %i" x y);; 
end);;

然后,使用如下的实现:

let at = AT.create () in
let ati = AT.insert (1,2) at in
let atd = AT.delete (1,2) ati in

......等等。

现在,我想在一个单独的源文件中编写几个对这些数据结构进行操作的函数,它们应该可以从外部访问。但是,我不知道如何声明这些函数的类型。像这样:

search.mli

val search : Int -> Ads.T -> int list

但是,编译时我得到:

 Failure: "invalid long identifier type"
然后,我认为我需要专门声明adt的模块为 search.mli中的子模块,类似于:

search.mli

module AD = Ads;;
 ...
val search : Int -> AD.T -> int list

但是,我明白了:

Parse error: [module_declaration] expected after [a_UIDENT] (in [sig_item])

我在这里缺少什么?我觉得我要么失败了语法,要么没有完全掌握Functors,Modules和Submodules的概念......

编辑非常感谢您的解释,加油!通过你的例子,我能够写出我想要的东西。我将在此处发布以便澄清,因为似乎很多关于ocaml中的仿函数的混淆。

实际上我想使函数抽象为Ads.T,但需要Ads.T.t的特定类型。

我现在有search.mli

module Make (T : Ads.T with type entry = int * int) : sig
    val search : T.t -> int -> int
end;;

而且,在search.ml

module Make (T : Ads.T with type entry = int * int) : sig
    val search : T.t -> int -> int 
end = struct
    (* actual implementation of search *)
end;;

它完全符合我的预期。

1 个答案:

答案 0 :(得分:6)

你准备做什么?您是希望通过广告类型(例如Ads.T.t)或广告模块(例如Ads.T)对您的功能进行参数化?

在这两种情况下,您都应该将这些泛型函数包装在模块中:

module Generic (Ad : Ads.T) : sig
  val search : int -> Ad.t -> int list
end = struct
  let search _ _ = assert false
end

然后,您可以轻松地实例化它们,例如。使用您的Conrete_ads模块:

module AT = Concrete_ads.make(struct ... end)
module Lib = Generic(AT)
let foo = Lib.search 2 (AT.create ())

当然,如果你只是希望你的函数在特定的具体类型上进行参数化:

val search : int -> AT.t -> int list

PS:在您对AT的定义中,您忘记了仿函数的struct模块参数中的struct .. end

PPS:使用OCaml 3.12,有一个名为first-class modules的新亮点,允许将模块作为值参数传递给函数

val search : int -> (module Ad.T) -> int list

let search n ad_module =
  let module Ad = (val ad_module : Ad.T) in
  ... Ad.foo ..

... search 2 (module AT : Ad.T) ...

(解释:module S,作为类型表达式,是签名S的“已知模块”的值的类型; (val t : S),作为模块表达式,是模块已使用签名t打包到值S。这里我将ad_module作为值并将其解压缩到本地Ad模块中,然后可以将其用作函数内的任何其他模块。最后,(module M : S)是一个术语表达式,它将模块M与签名S打包成一个值。)

在某些情况下它可以使用仿函数进行补充,但是因为它是新的,有点复杂(对使用一流模块有一些非平凡的限制)并且可能会在下一种语言中改变一点版本,我建议保持经过验证的仿函数构造。

相关问题