5

我已经定义了 2 个模块,Zone并且是需要调用函数的原因函数列表:ZonesZonesZoneZonesZone

module Zone = struct
 type t = 
 { ...
   prop: bool }
 ...
end

modules Zones = struct
  type t =
  | ZSbot
  | ZS of Zone.t list
  | ZStop
  ...
end

原则文件all.ml使用ZonesMis模块, mis.ml包含适用于两者的函数Zone.tZones.t例如val Mis.make : Zone.t -> Zones.t.

open Zones
open Mis
type t = { zs: Zones.t }
...
Mis.make z

现在,我想为propof提供更多选择Zone。所以我定义了一个接口和 2个PROPERTY模块TypeFormula匹配它,这样我就可以为. 现在我可以想象新的几种可能性:Zone... prop: Property.t ...all.ml

(* 1 *)
open Zones
module ZonesType = ZonesFun(Type)
module ZonesFormula = ZonesFun(Formula)
type t = { zstype: ZonesType.t; zsformula: ZonesFormula }

(* 3 *)
open Zones
module ZonesType = ZonesFun(ZoneFun(Type))
module ZonesFormula = ZonesFun(ZoneFun(Formula))
type t = { zstype: ZonesType.t; zsformula: ZonesFormula }

(* 4 *)
open Zones
module ZoneType = ZoneFun(Type)
module ZoneFormula = ZoneFun(Formula)
module ZonesType = ZonesFun(ZoneType)
module ZonesFormula = ZonesFun(ZoneFormula)
type t = { zstype: ZonesType.t; zsformula: ZonesFormula }

ZonesFun虽然 3 个选项的和的签名ZoneFun不同,但这种实现可以确保ZoneXXX.t并且ZonesXXX.t是连贯的。现在一个大问题是如何改变Mis

1)如果我制作一个仿函数MisFun: PROPERTY -> MIS,并在里面构建ZoneXXXand ZonesXXX。它无法知道MisXXX.Zone.tZone.tof相同all.ml或与ofMisXXX.Zones.t相同。Zones.tall.ml

2)如果我做一个仿函数MisFun: Zone -> Zones -> MIS,它不能知道MisXXX.Zone.t并且MisXXX.Zones.t是连贯的。

有谁知道如何解决1)和2)?

4

1 回答 1

3

在选项(1)中,您是否ZoneFun在里面申请ZonesFun

假设,我认为选择在(1)和(3)/(4)之间(这似乎是相同的)。选择哪一个取决于您是否需要能够访问Zone外部创建的模块ZoneFun(您需要(4))或不((1)工作正常)。

更新回复更新:

如果我正确理解了您的问题,那么在我看来,它Mis也必须成为函子。此外,它的签名可以指定类型,如

val make: ZoneFun(X).t -> ZonesFun(X).t

X函子参数在哪里。

(顺便说一句,我仍然认为(3)和(4)之间没有区别,除了你命名了辅助模块。)

更新 2:

我的猜测是,您在 OCaml 的模块类型检查器中遇到了一个古老而不幸的错误(请参阅caml list 上的讨论)。以下应该可以工作,但不能:

module type PROP = sig type t end
module type ZONE = sig type t end
module MakeZone (P : PROP) = struct type t = {p : P.t} end
module MakeZones (Z : ZONE) = struct type t = ZS of Z.t list end

module MakeMisc (P : PROP) :
sig
  val make : MakeZone(P).t -> MakeZones(MakeZone(P)).t
end =
struct
  module Zone = MakeZone(P)
  module Zones = MakeZones(Zone)
  let make z = Zones.ZS [z]
end

module Type = struct type t = T end
module Formula = struct type t = F end
module ZoneType = MakeZone(Type)
module ZoneFormula = MakeZone(Formula)
module ZonesType = MakeZones(ZoneType)
module ZonesFormula = MakeZones(ZoneFormula)
module MiscType = MakeMisc(Type)
module MiscFormula = MakeMisc(Formula)
let zst = MiscType.make {ZoneType.p = Type.T}
let zsf = MiscFormula.make {ZoneFormula.p = Formula.F}

MakeMisc您可以通过如下方式输入类型注释来强制使其工作:

module MakeMisc (P : PROP) :
sig
  val make : MakeZone(P).t -> MakeZones(MakeZone(P)).t
end =
struct
  module Zone : sig type t = MakeZone(P).t end = MakeZone(P)
  module Zones :
    sig type t = MakeZones(MakeZone(P)).t = ZS of MakeZone(P).t list end =
    MakeZones(MakeZone(P))
  let make z = Zones.ZS [z]
end

但显然,这不是很愉快。

但是,无论如何,在 ML 中表达共享的常用方法是通过fibration,您可以在签名中抽象地命名要与之共享的类型或模块,并相应地对其进行细化。然后您可以将Zone其转换为函子Zones的附加参数:Misc

module type PROP = sig type t end
module type ZONE = sig type prop type t = {p : prop} end
module type ZONES = sig type zone type t = ZS of zone list end
module MakeZone (P : PROP) = struct type prop = P.t type t = {p : prop} end
module MakeZones (Z : ZONE) = struct type zone = Z.t type t = ZS of zone list end

module MakeMisc
  (P : PROP) (Z : ZONE with type prop = P.t) (Zs : ZONES with type zone = Z.t) :
sig
  val make : Z.t -> Zs.t
end =
struct
  let make z = Zs.ZS [z]
end

module Type = struct type t = T end
module Formula = struct type t = F end
module ZoneType = MakeZone(Type)
module ZoneFormula = MakeZone(Formula)
module ZonesType = MakeZones(ZoneType)
module ZonesFormula = MakeZones(ZoneFormula)
module MiscType = MakeMisc(Type)(ZoneType)(ZonesType)
module MiscFormula = MakeMisc(Formula)(ZoneFormula)(ZonesFormula)
let zst = MiscType.make {ZoneType.p = Type.T}
let zsf = MiscFormula.make {ZoneFormula.p = Formula.F}
于 2012-12-18T17:14:17.750 回答