Version française
Home     About     Download     Resources     Contact us    
Browse thread
recursive records with weak hashtbl
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Jambon <martin.jambon@e...>
Subject: Re: [Caml-list] recursive records with weak hashtbl
Vsevolod Fedorov wrote:
> Hello!
> 
> I want to define two records referencing each other. First record (A)
> has direct reference to second (B) and second (B) has weak hash table to
> list records A, which have reference to it. For example (pseudo code):
> 
> type a
> {
>   id : int ;
>   mutable field1 : string;
>   mutable b : B;
> }
> type b
> {
>     id : int;
>     mutable field2 : string;
>     a_list : Weak-Hashtbl(a);  (* they referenced me *)
> }
> 
> Is it possible at all?
> Is it possible with A and B declarations in separate files?
> 
> Any hints and references are welcomed.

Assuming your weak hash table module is created by a functor parametrized by
type "a", the problem is solved with recursive modules. They are documented as
a language extension in the reference manual:

  http://caml.inria.fr/pub/docs/manual-ocaml/manual021.html#toc75


Here is some code that compiles and runs without raising the
Undefined_recursive_module exception:


module rec W : Weak.S =
  Weak.Make (
    struct
      type t = X.a
      let equal = ( = )
      let hash = Hashtbl.hash
    end
  )

and X :
sig
  type a = {
    id : int ;
    mutable field1 : string;
    mutable b : b;
  }
  and b = {
    id : int;
    mutable field2 : string;
    a_list : W.t;  (* they referenced me *)
  }
end =
struct
  type a = {
    id : int ;
    mutable field1 : string;
    mutable b : b;
  }
  and b = {
    id : int;
    mutable field2 : string;
    a_list : W.t;  (* they referenced me *)
  }
end



Martin

-- 
http://mjambon.com/