gpt4 book ai didi

types - 模块 : type problem in functor

转载 作者:行者123 更新时间:2023-12-04 06:53:01 25 4
gpt4 key购买 nike

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

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

当我尝试与 Edge(l,n,m) 匹配时,正好在函数 suc 中:#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

...

最佳答案

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

了解 VertexEdge 的签名会非常有帮助

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

关于types - 模块 : type problem in functor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6031697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com