2013-12-11 38 views
1

这是我的函子ORDTYPE。我想在EmptyQueue中使用比较。 我不知道如何注入函子。所有时间我都会收到错误,我签名无效。 我试图声明函数(键:ORDTYPE) - >结构之前,但它是错误的。 我不明白函子的想法。我在OCaml wiki中看到了一些简单的例子,但我不知道如何处理更复杂的事情。OCaml了解更复杂的函数

总之我想在空队列中使用比较器。但我不知道如何处理这个更抽象。

module type ORDTYPE = 
sig 
    type t 
    val compare : t -> t -> int 
end;; 

module Icmp:ORDTYPE with type t= int= 
struct 
    type t = int 
    let compare = fun x y -> if(x< y) then 0 else 1 
end;; 


module type STH= 
sig 
    type priority 
    type 'a t 
    val comp: x -> x->bool 
end;; 


module Emptyqueue (Comp: ORDTYPE): STH with type priority= int = 
    struct 
     type priority = int 
     type 'a t = Empty 
     let comp = fun x y -> Comp.comp x y 

    end;; 

我编辑了我的想法,我应该这样做,但事实并非如此。工作。

回答

2

您错过的是需要在签名STH中定义x。我将使用更清晰的名称elt而不是x。一旦STHelt,我们既可以将其添加到Emptyqueue为好,或使用“破坏性的替代”,即签名修改语法with ... := ...

注重在我的例子“拉直”介绍差异,因为你有一些不匹配这是不明确的纠正。

module type ORDTYPE = 
sig 
    type t 
    val compare : t -> t -> int 
end;; 
module Icmp:ORDTYPE with type t= int= 
struct 
    type t = int 
    let compare = fun x y -> if(x< y) then 0 else 1 
end;; 
module type STH= 
sig 
    type priority 
    type 'a t 
    type elt 
    val comp: elt -> elt -> bool 
end;; 
module Emptyqueue (Comp: ORDTYPE): 
    (STH with type priority= int and type elt := Comp.t) = 
struct 
    type priority = int 
    type 'a t = Empty 
    let comp = fun x y -> Comp.compare x y > 0 
end;;