views:

71

answers:

1

Hello, how can I manage to define a Set in OCaml that can contains element of its type too?

To explain the problem I have a type declaration for a lot of data types like

type value =
  Nil
| Int of int
| Float of float
| Complex of Complex.t
| String of string
| Regexp of regexp
| Char of char
| Bool of bool
| Range of (int*int) list
| Tuple of value array
| Lambda of code
| Set of ValueSet.t (* this isn't allowed in my case since module is declared later*)

In addition I declare a concrete module for ValueSet later in the same file:

module ValueSet = Set.Make(struct type t = value let compare = Pervasives.compare end)

The problem is that ValueSet has value as it's elt type but value can be a ValueSet so I'm getting troubles while trying to compile it.

All of these declarations are contained in just a file named types.ml (that has it's own interface types.mli but without any ValueSet module decl since I'm not either sure it's possible).

Can this problem be solved in some way?

+4  A: 

You can use recursive modules. Language manual uses precisely the same example of recursive set type to illustrate this language feature.

rkhayrov
It seems that my OCaml compiler (3.11.0) doesn't support them yet. In any case having this kind of recursive declaration inside `types.ml` will force to have inner modules like `Types.InnerTypes` and `Types.ValueSet` which can be ok for the ValueSet but not for the other one..
Jack
I think I'm slowly managing it to work some way.. I'll probably accept your answer in a short time if I'll be able to make it work :)
Jack
OCaml has had recursive modules since 3.07.
Gilles