[英]OCaml: circularity between variant type and module definition
I'm switching from Haskell to OCaml but I'm having some problems. 我正在从Haskell切换到OCaml,但我遇到了一些问题。 For instance, I need a type definition for regular expressions. 例如,我需要正则表达式的类型定义。 I do so with: 我这样做:
type re = EmptySet
| EmptyWord
| Symb of char
| Star of re
| Conc of re list
| Or of (RegExpSet.t * bool) ;;
The elements inside the Or are in a set (RegExpSet), so I define it next (and also a map function): Or中的元素在一个集合(RegExpSet)中,所以我接下来定义它(以及map函数):
module RegExpOrder : Set.OrderedType =
struct
let compare = Pervasives.compare
type t = re
end
module RegExpSet = Set.Make( RegExpOrder )
module RegExpMap = Map.Make( RegExpOrder )
However, when I do "ocaml [name of file]" I get: 但是,当我做“ocaml [文件名]”时,我得到:
Error: Unbound module RegExpSet
in the line of "Or" in the definition of "re". 在“re”的定义中的“Or”行。
If I swap these definitions, that is, if I write the modules definitions before the re type definitions I obviously get: 如果我交换这些定义,也就是说,如果我在重新定义类型之前编写模块定义,我显然会得到:
Error: Unbound type constructor re
in the line of "type t = re". 在“type t = re”的行中。
How can I solve this? 我怎么解决这个问题? Thanks! 谢谢!
You can try to use recursive modules . 您可以尝试使用递归模块 。 For instance, the following compiles: 例如,以下编译:
module rec M :
sig type re = EmptySet
| EmptyWord
| Symb of char
| Star of re
| Conc of re list
| Or of (RegExpSet.t * bool)
end =
struct
type re = EmptySet
| EmptyWord
| Symb of char
| Star of re
| Conc of re list
| Or of (RegExpSet.t * bool) ;;
end
and RegExpOrder : Set.OrderedType =
struct
let compare = Pervasives.compare
type t = M.re
end
and RegExpSet : (Set.S with type elt = M.re) = Set.Make( RegExpOrder )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.