2014-03-01 225 views
0

首先代碼:簽名不匹配ocaml的

module type ENV_CORE = 
sig 
type variable 
type 'a environment 
exception Unbound_variable 
val empty : unit -> variable 
val bind : 'a -> 'a environment -> 'a environment 
val unbind : variable -> 'a -> 'a environment -> 'a environment 
val is_bound : variable -> 'a environment -> bool 
val lookup : variable -> 'a environment -> bool 
val fold : (variable -> 'a -> 'b -> 'b) -> 'a environment -> 'b -> 'b 
end;; 


module EnvCoreList : ENV_CORE = 

struct 
    type variable = string list 
    type 'a environment = variable * variable -> 'a 
    exception Unbound_variable 
    let empty() = [] 
    let bind elt l = elt::l 
    let rec unbind elt l = 
     match l with 
     |[] -> raise Unbound_variable 
     |a::r -> if (elt = a) 
      then r 
      else a::(unbind elt r) 
    let rec is_bound elt l = 
     match l with 
     |[] -> raise Unbound_variable 
     |a::r -> if (elt = a) 
      then true 
      else is_bound elt r 
    let rec lookup elt l = 
     match l with 
     |[] -> false 
     |a::r -> if (elt = a) 
      then true 
      else lookup elt r 
    let rec fold f rho gamma = 
     match rho with 
     |[] -> gamma 
     |a::r -> f a (fold f r gamma) 
end;; 

當我編譯它,我得到以下錯誤:

Error: Signature mismatch: 
    Modules do not match: 
    sig 
     type variable = string list 
     type 'a environment = variable * variable -> 'a 
     exception Unbound_variable 
     val empty : unit -> 'a list 
     val bind : 'a -> 'a list -> 'a list 
     val unbind : 'a -> 'a list -> 'a list 
     val is_bound : 'a -> 'a list -> bool 
     val lookup : 'a -> 'a list -> bool 
     val fold : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b 
    end 
    is not included in 
    ENV_CORE 
    Values do not match: 
    val bind : 'a -> 'a list -> 'a list 
    is not included in 
    val bind : 'a -> 'a environment -> 'a environment 

什麼我不明白的是更具體的類型是如何不包括在更一般的類型? 我找不到任何類似的問題,並沒有能夠解決這個問題。 Thanx

回答

0

'a list'a environment之間沒有明顯的關係。我不明白爲什麼你會考慮要麼比其他更普遍。

在我看來,你要麼改變你的environment定義您的實現,或者你應該重寫bind,以便它可以在你指定environment類型。

+0

我必須爲心臟'ENV_CORE'環境和模塊('EnvCoreList:ENV_CORE')編寫簽名,該模塊實現具有關聯列表的環境 – user3368964