3

I have built several modules including EEA which match module type PROP. Now I would like to build 2 functors which looks like follows:

(* zone.ml *)
module type ZONE =
sig
  ...
end
module ZoneFun (Prop : PROP) = struct
  type t =
    { i: int;
      pal: ZonesEEA.t }
  ...
end

(* zones.ml *)
module ZonesFun (Zone: ZONE) = struct
  type t = Zone.t list
  ...
end

Therefore, the functors would allow me to build modules:

(* modules.ml *)
open EEA
open Zone
open Zones
module ZoneEEA = ZoneFun(EEA)
module ZonesEEA = ZonesFun(ZoneEEA)

However, the code does not work because there is a recursion: ZoneFun requires ZonesEEA.t, which comes from ZoneEEA, thus ZoneFun...

Does anyone have an idea to restructure the code to realize this recursion?

4

1 回答 1

0

您需要某种跨模块的递归。这会产生循环依赖,并且不能像在 OCaml 中那样完成。但是,如果您可以抽象palin的类型ZoneFun,您就可以做到这一点。我正在向ZoneFun函子添加一个参数,从中获取类型pal。我正在调用这个参数Z,并给它 type ZONES,因为它稍后将被实例化ZonesEEA

(* zone.ml *)
module type ZONES = sig
  type t
  ...
end

module ZoneFun (Prop : PROP) (Z : ZONES) = struct
  type t =
    { i: int;
      pal: Z.t }
  ...
end

然后,ZoneEEAZonesEEA将是相互递归的模块:

(* modules.ml *)
open EEA
open Zone
open Zones

module rec ZoneEEA : ZONE = ZoneFun(EEA)(ZonesEEA)
and ZonesEEA : ZONES = ZonesFun(ZoneEEA)
于 2013-08-30T23:08:54.820 回答