【问题标题】:OCaml sharing constraintsOCaml 共享约束
【发布时间】:2019-12-07 23:43:13
【问题描述】:

我正在阅读Real World Ocaml 中有关共享约束的信息,但无法通过更改模块类型签名来了解有关公开endpoint 工作的部分。

open Core

module type Interval_intf = sig
  type t
  type endpoint
  val create : endpoint -> endpoint -> t
  val is_empty : t -> bool
  val contains : t -> endpoint -> bool
  val intersect : t -> t -> t
end

module Make_interval(Endpoint: Comparable): Interval_intf = struct
  type endpoint = Endpoint.t

  type t =
    | Interval of Endpoint.t * Endpoint.t
    | Empty

  let create low high =
    if Endpoint.compare low high > 0 then Empty
    else Interval (low, high)

  let is_empty = function
    | Empty -> true
    | Interval _ -> false

  let contains t x =
    match t with
    | Empty -> false
    | Interval (l, h) -> Endpoint.compare x l >= 0 && Endpoint.compare x h <= 0

  let intersect t1 t2 =
    let min x y = if Endpoint.compare x y <= 0 then x else y in
    let max x y = if Endpoint.compare x y >= 0 then x else y in
    match t1, t2 with
    | Empty, _ | _, Empty -> Empty
    | Interval (l1, h1), Interval (l2, h2) -> create (max l1 l2) (min h1 h2)
end

module Int_interval_intf : (Interval_intf with type endpoint = int) = Make_interval(Int)

这给出了一个错误:

Error: Signature mismatch:
       ...
       Type declarations do not match:
         type endpoint = Make_interval(Core_kernel__Int).endpoint
       is not included in
         type endpoint = int

这里有什么问题?

这是带有 OCaml 4.06.0 和 Core v0.11.3 的。

【问题讨论】:

    标签: ocaml


    【解决方案1】:

    Make_interval 的结果类型的模块类型约束太不透明。实际上,约束是使类型 endpoint 抽象。而且由于它们不是在Interval_intf 签名中产生endpoint 的函数 需要endpoint 作为输入,这使得生成的模块无用。

    module Make_interval(Endpoint: Comparable): Interval_intf with type endpoint = ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多