模块:在仿函数中输入问题

mec*_*chu 3 ocaml types functional-programming

我在下面的代码中遇到类型问题(一些简单的模块功能图实现).似乎这些类型生活在自己的生活中.

我已经type t = NotaEdge | Edge of int*v*v在模块Edge中实现了,这个类型在Graph模块中变成了type edge = E.t.对我来说一切似乎都很好,除了事实上我无法在其上进行模式匹配,因为构造函数Edge在模块Graph中仍未定义.

当我尝试与Edge(l,n,m)匹配时,函数完全正常:#Error:Unbound构造函数Edge

希望有人可以很好地展示它,先提前:)

 module Vertex : Vertex with type label = int =

struct

  type t = NotaNode |  Node of int
  type label = int
  exception No of string
Run Code Online (Sandbox Code Playgroud)

...

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
Run Code Online (Sandbox Code Playgroud)

...

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
Run Code Online (Sandbox Code Playgroud)

...

nlu*_*oni 5

这里的事情有点混乱; Error永远不会定义,我认为是一些错别字.如果你提供编译的代码会更有帮助.把它压低,但在语法上是正确的.我只能对有限的信息和常见的陷阱进行猜测.

这将是非常有益知道签名VertexEdge

如果签名中Edge的类型t定义与Edge您给出的实现相同,那么您可以将变量与E.Edge和匹配E.NotaEdge.如果类型t是抽象的(签名中的唯一信息是type t),那么您将不会(并且合理地不应该)以这种方式访问​​实现或模式匹配.在这种情况下,实现隐藏在签名后面.在处理仿函数时,这通常很好(并且打算),因为您可以以任何必要和方便的方式实现模块.

  • 使用套筒扳手敲钉子并没有什么优雅,但是当你想要拧紧一些螺栓时. (2认同)