2013-12-11 52 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;;