尝试在另一个模块中使用模块时签名不匹配

时间:2017-12-01 21:01:18

标签: ocaml

我有一个问题,我有一个模块试图使用另一个模块,但我收到一个错误声称有一个签名不匹配,我不知道为什么。我很确定我这样做是对的。这是一些代码:

module type ITEM =
sig
    type item
    val leq : item * item -> bool
    val initial : item
end
module type HEAP =
sig
  type item
  type tree
  exception InitHeap
  val depth : tree -> int
  val initHeap : int -> tree
  val insert : item * tree -> tree
  val isHeap : tree -> bool
  val maxHeap : tree -> item
  val replace : item * tree -> item * tree
  val size : tree -> int
  val top : tree -> item
end

module Heap (Item: ITEM) : HEAP =
struct 
              type item = Item.item

    let leq(p, q) : bool = Item.leq(p,q)

    let max(p,q) = if leq(p,q) then q else p
    and min(p,q) = if leq(p,q) then p else q

    let intmax((p : int),q) = if p <= q then q else p

    type tree =
      | L of item
      | N of item * tree * tree

   exception InitHeap

   let rec initHeap n =
       if (n < 1) then raise InitHeap
       else if n = 1 then L Item.initial
            else let t = initHeap(n - 1)
                 in N (Item.initial, t, t)

    let rec top t =
      match t with
      | (L i) -> i
      | N (i,_,_) -> i


    let rec isHeap t =
      match t with
      | (L _) -> true
      | (N(i,l,r)) ->
        leq(i,top l) && leq(i,top r) && isHeap l && isHeap r

    let rec depth t =
      match t with
      | (L _) -> 1
      | N(i,l,r) -> 1 + intmax(depth l,depth r)

   let rec replace (i,h) = (top h, insert(i,h))
   and insert (i, h) =
     match h with
     | L _ -> L i
     | N (_,l,r) ->
       if leq(i,min(top l,top r))
       then N(i,l,r)
       else if leq((top l),(top r))
            then N(top l,insert(i,l),r)
            else N(top r,l,insert(i,r))

   let rec size h =
     match h with
     | L _ -> 1
     | N (_,l,r) -> 1 + size l + size r

   let rec maxHeap h =
     match h with
     | (L i) -> i
     | N (_,l,r) -> max(maxHeap l, maxHeap r)
end 

所以Heap包含了一堆只对Heap进行简单操作的函数,但出于某种原因,ocaml认为HEAP的签名应该包含ITEM的函数,但我只想将ITEM函数拉入HEAP 我得到的错误:

Error: Signature mismatch:
   Modules do not match:
     sig val leq : int * int -> bool end
   is not included in
     ITEM
   The value `initial' is required but not provided
   File "lab13.ml", line 28, characters 8-26: Expected declaration
   The type `item' is required but not provided
   File "lab13.ml", line 26, characters 8-17: Expected declaration

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

你很可能写了

module type HEAD = functor (Head:ITEM) -> sig … end  

(而不是使用module type HEAD = functor (Head:HEAD) -> sig … end模块类型的HEAD,这是一种类型错误)

当你意味着

module type HEAD = sig … end  

添加functor(HEAD:ITEM) -> …部分会使HEAD成为签名或仿函数。因此

module Heap (Item: ITEM) : HEAP

相同
module Heap (Item: ITEM) : functor(Heap:HEAP) -> sig … end

换句话说,您添加的签名使Heap成为更高阶的仿函数;这显然不是实施的情况。 不幸的是,现在缺少仿函数存在的错误消息,而且类型检查器暂时没有详细说明此特定情况下的错误。

HEAD模块类型重写为

module type HEAD = sig … end

应该解决这个问题。

相关问题