Version française
Home     About     Download     Resources     Contact us    
Browse thread
Local module as let binding around class def?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Brian Rogoff <bpr@b...>
Subject: Local module as let binding around class def?
This question was probably asked before, but I couldn't find the answer
from a quick perusal of the list. 

I take it that it is not legal to use a local module instantiation as 
the "let binding" around a class definition. Could someone explain the 
reason for the restriction, and the nicest workaround? This comes up when 
I want to make OO renamings for functorial ADTs. To be concrete, if I have 
a functorially defined ADT "Seq", like Set in the standard lib, and I'd
like to make an class Oseq.c, which has a Seq.t representation and whose
methods are just the various Seq functions, why can't the definition use the
functor instantiation in local module definitions, instead of copying all
of the functor code into the oseq.ml file (my current workaround)?

(* oseq.ml *)

class c () = 
  let module Element = struct type t = int end in
  let module SeqRep  = Make(Element) in 
  object
    val s = create ()
    (* Mutators *)
    method addhi (:data : int) = SeqRep.addhi s data
    method addlo (:data : int) = SeqRep.addlo s data

    (* etc, etc., ... *)
  end

(* seq.ml *)

(*** Functorial interface *)

module type ElementType =
  sig
    type t
  end

(* The input signature of the functor [Seq.Make].*)

module type S =
  sig
    type elem_t
    type t

    (* Constructors *)
    val create: unit -> t
    val make: unit -> t
    val of_array: elem_t array -> t
    val of_list: elem_t list -> t

    (* Mutators *)
    val addhi: t -> elem_t -> unit
    val addlo: t -> elem_t -> unit
    val remhi: t -> unit
    val remlo: t -> unit
    val set: t -> int -> elem_t -> unit

    (* Selectors *)
    val get: t -> int -> elem_t
    val to_array: t -> elem_t array
    val to_list: t -> elem_t list
    val last: t -> elem_t
    val length: t -> int
  end

module Make(E : ElementType) : (S with type elem_t = E.t) = 
  struct
    type elem_t = E.t
    type t =
   (* etc .... *)
  end