很抱歉发布如此长的、不可编译的代码。但是,尽管阅读了关于 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
... ETC。现在,我想在一个单独的源文件中编写几个对这些数据结构进行操作的函数,并且它们应该可以从外部访问。但是,我不知道如何声明这些函数的类型。像这样的东西:
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;;
它完全按照我的意图工作。
最佳答案
你到底想做什么?您是否希望您的函数通过广告类型(例如 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 中,有一个新的 Shiny 的东西叫做 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
打包成一个值。)在某些情况下它可以补充使用仿函数,但由于它是新的,有点复杂(在使用一流模块方面存在重大限制)并且可能会在下一个语言版本中有所改变,我建议保持久经考验的仿函数构造。
关于module - Ocaml 仿函数、模块和子模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6907435/