模块:函子中的类型问题

发布于 2024-11-07 22:36:41 字数 1273 浏览 1 评论 0原文

我对以下代码中的类型有疑问(一些简单的模块功能图实现)。似乎各类型都过着自己的生活。

我有 type t = NotaEdge | int*v*v 的边在 Edge 模块中实现,该类型在模块 Graph 中变为 type edge = Et。对我来说一切似乎都很好,除了事实上我无法对其进行模式匹配,因为构造函数 Edge 在模块 Graph 中仍然未定义。

正是在函数 suc 中,当我尝试与 Edge(l,n,m) 匹配时: #Error: Unbound constructor Edge

希望有人能很好地呈现它,提前谢谢:)

 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

...

I have a problem with types in the following code (some easy module functional graph implementation). It seems that types are living their own lives.

I have type t = NotaEdge | Edge of int*v*v implemented in module Edge, this type in module Graph becomes type edge = E.t. Everything seems fine to me, except fact i can't pattern match on it, cause the constructor Edge is still undefined in module Graph.

Exactly in function suc when i try to match with Edge(l,n,m): #Error: Unbound constructor Edge

Hope someone can present it nicely, thx in advance :)

 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

...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萝莉病 2024-11-14 22:36:41

这里的情况有点乱; Error 从未定义过,我认为是一些拼写错误。如果您提供已编译的代码,将会更有帮助。把它简化一下,但语法上是正确的。我只能根据有限的信息和常见的陷阱做出推测。

了解 VertexEdge 的签名将非常有帮助,

如果在签名 Edge 中,类型为 t 的定义与您提供的 Edge 实现中的定义相同,然后您可以将变体与 E.EdgeE 相匹配。 NotaEdge。如果类型 t 是抽象的(签名中的唯一信息是 type t),那么您将(并且合理地不应该)能够访问实现或模式匹配这样。在这种情况下,实现隐藏在签名后面。在处理函子时,这通常很好(并且是有意的),因为您可以以任何必要且方便的方式实现该模块。

Things are a bit messy here; Error is never defined, and what I presume to be a few typos. It would be much more helpful if you gave code that compiled. Par it down, but syntactically correct. I can only make conjectures on the limited information and common pitfalls.

It would be very helpful to know the signatures to Vertex and Edge

If in the signature Edge the type t is defined the same as in the implementation of Edge that you give, then you can match the variant with E.Edge and E.NotaEdge. If the type t is abstract (the only information in the signature is type t), then you wont (and reasonably should not) be able to access the implementation or pattern match in that way. In this case the implementation is hidden behind the signature. This is usually fine (and intended) when dealing with functors since you can implement the module in any way that is necessary and convenient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文