Local modules only make sense when they are used locally. You cannot return a value whose type depends on the local module (what woud that mean?). So in your example, LabelSet should not be a local module, as it is used in the return type of the "get" method.

For a simpler example of this problem:
# let f x =
 let module M = Map.Make(struct type t = int let compare = compare end) in
 M.singleton 1 x;;
Error: This `let module' expression has type 'a M.t
In this type, the locally bound module name M escapes its scope

If you need to use a set, you must request an ordering on your labels. The type-safe way to do this in OCaml is to use modules, so you should define your class in a functor, rather than as a parametrized class.

# module Tracker (Label : Set.OrderedType) = struct
    module LabelSet = Set.Make(Label)
    class tracker size = object
      method get (_ : int) = LabelSet.empty
    end
  end;;

You could consider this an issue with ordering-parametrized functors: they require code using them to get functorized as well, which may in turn require more important changes in your application. But starting from 3.12 it's easier to contain functorization locally, and I think this design is viable. The other possibility is to use a polymorphic datatype that relies on the apparently polymorphic comparison function, but this is less type-safe: you cannot use your own comparison function, so you won't be able to use labels containing cycles or closures, and you may mistakenly mix result sets from different trackers.

On Mon, May 23, 2011 at 1:17 PM, Hakan Suka <hakan40us@yahoo.com> wrote:
> Hi everyone,
>
> I tried the ocaml-beginners list with this question but did not get a good
> solution, so I am retrying here.
>
> I want to create a parameterised class that takes as input a polymorphic "label"
>
> type and internally stores information using a Set of that type. However, I
> cannot get the functor to work inside a class, so I wonder whether this is
> possible.
>
> For example, the following works:
>
> module LabelSet =
> Set.Make (struct type t = int let compare = compare end)
>
> class ['a] tracker size =
> object
>
> val nums_to_track_tbl : (int32, LabelSet.t) Hashtbl.t =
> Hashtbl.create size
>
> method get pos = Hashtbl.find_all nums_to_track_tbl pos
> end
>
> But can I have a local module definition so that the I can have a tracker not
> only for integers, but for any type being passed as input? The following does
> not work:
>
> class ['a] tracker size =
> let module LabelSet =
> Set.Make (struct type t = 'a let compare = compare end)
> in
> object
> val nums_to_track_tbl : (int32, LabelSet.t) Hashtbl.t =
> Hashtbl.create size
>
> method get pos = Hashtbl.find_all nums_to_track_tbl pos
>
> end
>
> If this is not possible, would it work in Ocaml 3.12 with first-class modules?
>
> I have seen the following code in the manual which seems fairly close of what I
> would need inside the parametrised class, but not sure if I can use the output
> of make_set inside the module. I haven't tested this because I have Ocaml 3.11.2
>
> let make_set (type s) cmp =
> let module S = Set.Make(struct
> type t = s
> let compare = cmp
> end) in
> (module S : Set.S with type elt = s)
>
> If that does not work, any suggestions on how to implement this?
>
> Thanks,
> Hakan
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>