2011-05-17 66 views
3

我在下面的代碼中有類型問題(一些簡單的模塊函數圖實現)。看起來類型正在過着自己的生活。模塊:仿函數中的類型問題

我在模塊Edge中實現了type t = NotaEdge | Edge of int*v*v,此模塊中的Graph類變爲type edge = E.t。一切似乎對我來說都很好,除了事實上我無法模式匹配它,導致構造函數邊緣仍然未定義在模塊Graph中。

究竟在功能SUC當我試圖以配合邊緣(L,N,M):#錯誤:未綁定構造邊緣

希望有人能夠提前很好地呈現它,THX :)

module Vertex : Vertex with type label = int = 

struct 

    type t = NotaNode | Node of int 
    type label = int 
    exception No of string 

...

module Edge : Edge with type label = int and type v = Vertex.t = 
struct 

    type v = Vertex.t 
    type t = NotaEdge | Edge of int*v*v 
    type label = int  

    exception No of string 

...

module Graph (E : Edge) (V : Vertex) : Graph with type vertex = V.t and type edge = E.t = 
struct 

    type vertex = V.t 
    type edge = E.t 
    type t = E.t list* V.t list 

    let empty = ([],[]) 

let rec suc (x:edge list) (v1:vertex) = 
    match x with 
     y::ys -> (match y with 
    (*Error-->*)  Edge(l,n,m) -> if n == v1 then m::(suc ys v1) else suc ys v1 
        | _ -> []) 
     |[] -> [] 

    let succ (t1:t) (v1:vertex) = 
    match t1 with 
     (x,_) -> suc x v1 

...

回答

5

這裏的東西有點雜亂; Error從來沒有定義過,我認爲是幾個拼寫錯誤。如果您提供了編譯的代碼,那將會更有幫助。把它放下來,但在語法上是正確的。我只能對有限的信息和常見的陷阱做出猜測。

這將是非常有助於瞭解簽名VertexEdge

如果簽名Edge類型t定義一樣的Edge你給的實施,則可以匹配E.EdgeE.NotaEdge。如果類型t是抽象的(簽名中唯一的信息是type t),那麼您不會(也不應該)能夠以這種方式訪問​​實現或模式匹配。在這種情況下,實現隱藏在簽名後面。在處理仿函數時,這通常很好(並且是有意的),因爲您可以用任何必要和方便的方式實現模塊。

+0

類型t是抽象的(對不起,我沒有發佈簽名),我還沒有找到任何方法來做我想要做的與抽象,所以你建議它工作得很好,當我重新輸入任何地方的確切類型也許不是很優雅,但作品)。我很快就會關閉主題(也許到那時我們會得到一些有趣的東西) – mechu 2011-05-17 15:26:34

+2

使用套筒扳手敲擊指甲沒有任何優雅之處,但它是當你想擰緊一些螺栓。 – nlucaroni 2011-05-17 15:31:25