OCaml: circuit between option and module definition

I am moving from Haskell to OCaml, but I am having problems. For example, I need a type definition for regular expressions. I am doing this with:

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

The elements inside Or are in the set (RegExpSet), so I define it as follows (as well as the display function):

 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 [file name]", I get:

 Error: Unbound module RegExpSet 

in the string "Or" in the definition of "re".

If I change these definitions, that is, if I write module definitions before definitions of type re, I obviously get:

 Error: Unbound type constructor re 

in the line "type t = re".

How can i solve this? Thanks!

+7
source share
1 answer

You can use recursive modules . For example, the following compilations:

 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 ) 
+9
source

All Articles