模块:函子中的类型问题
我对以下代码中的类型有疑问(一些简单的模块功能图实现)。似乎各类型都过着自己的生活。
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的情况有点乱;
Error
从未定义过,我认为是一些拼写错误。如果您提供已编译的代码,将会更有帮助。把它简化一下,但语法上是正确的。我只能根据有限的信息和常见的陷阱做出推测。了解
Vertex
和Edge
的签名将非常有帮助,如果在签名
Edge
中,类型为t
的定义与您提供的Edge
实现中的定义相同,然后您可以将变体与E.Edge
和E 相匹配。 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
andEdge
If in the signature
Edge
the typet
is defined the same as in the implementation ofEdge
that you give, then you can match the variant withE.Edge
andE.NotaEdge
. If the typet
is abstract (the only information in the signature istype 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.