7

我正在从 Haskell 切换到 OCaml,但我遇到了一些问题。例如,我需要一个正则表达式的类型定义。我这样做:

type re = EmptySet 
    | EmptyWord
    | Symb of char
    | Star of re
    | Conc of re list
    | Or of (RegExpSet.t * bool) ;;

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 ) 

但是,当我执行“ocaml [文件名]”时,我得到:

Error: Unbound module RegExpSet

在“re”的定义中的“Or”行中。

如果我交换这些定义,也就是说,如果我在 re 类型定义之前编写模块定义,我显然会得到:

Error: Unbound type constructor re

在“类型 t = re”的行中。

我该如何解决这个问题?谢谢!

4

1 回答 1

9

您可以尝试使用递归模块。例如,以下编译:

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 )
于 2011-12-18T15:18:26.873 回答