caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* recursive records with weak hashtbl
@ 2009-03-04 13:31 Vsevolod Fedorov
  2009-03-04 13:55 ` [Caml-list] " Florent Ouchet
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Vsevolod Fedorov @ 2009-03-04 13:31 UTC (permalink / raw)
  To: caml-list

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.
Thanks in advance.

Seva


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] recursive records with weak hashtbl
  2009-03-04 13:31 recursive records with weak hashtbl Vsevolod Fedorov
@ 2009-03-04 13:55 ` Florent Ouchet
  2009-03-04 14:00 ` Virgile Prevosto
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Florent Ouchet @ 2009-03-04 13:55 UTC (permalink / raw)
  To: caml-list

Vsevolod Fedorov a e'crit :
<snip>
> Is it possible at all?
>   
Yes, you have to use "type .. and .. ;;" statements

for instance:
type a =
{
id : int ;
mutable field_1 : string;
mutable field_b : b;
}
and b =
{
id : int;
mutable field_2 : string;
field_a_list : a array; (* they referenced me *)
};;
> Is it possible with A and B declarations in separate files?
>   
I don't think so.

- Florent


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] recursive records with weak hashtbl
  2009-03-04 13:31 recursive records with weak hashtbl Vsevolod Fedorov
  2009-03-04 13:55 ` [Caml-list] " Florent Ouchet
@ 2009-03-04 14:00 ` Virgile Prevosto
  2009-03-04 14:00 ` Martin Jambon
  2009-03-04 14:11 ` Christophe TROESTLER
  3 siblings, 0 replies; 5+ messages in thread
From: Virgile Prevosto @ 2009-03-04 14:00 UTC (permalink / raw)
  To: caml-list

Hello,

Le mer. 04 mars 2009 16:31:02 CET,
Vsevolod Fedorov <sevaAtWork@mail.ru> a écrit :

> 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?

mutually recursive types are defined like this:
type a = { ... } 
and b = { ... }

> Is it possible with A and B declarations in separate files?

not directly, but you can use a polymorphic type to break the recursion.
Assuming that a is defined in a.ml and b in b.ml and that a.ml depends
upon b.ml, you'd have:
- b.ml -
type 'a b = { ... a_list: 'a Weak.t }
- a.ml -
type a = { ... mutable b: a B.b }

BTW, I'd say that such a topic would be better on the ocaml beginners
list (see reference below)

Regards,
-- 
E tutto per oggi, a la prossima volta.
Virgile


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] recursive records with weak hashtbl
  2009-03-04 13:31 recursive records with weak hashtbl Vsevolod Fedorov
  2009-03-04 13:55 ` [Caml-list] " Florent Ouchet
  2009-03-04 14:00 ` Virgile Prevosto
@ 2009-03-04 14:00 ` Martin Jambon
  2009-03-04 14:11 ` Christophe TROESTLER
  3 siblings, 0 replies; 5+ messages in thread
From: Martin Jambon @ 2009-03-04 14:00 UTC (permalink / raw)
  To: Vsevolod Fedorov; +Cc: caml-list

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/


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] recursive records with weak hashtbl
  2009-03-04 13:31 recursive records with weak hashtbl Vsevolod Fedorov
                   ` (2 preceding siblings ...)
  2009-03-04 14:00 ` Martin Jambon
@ 2009-03-04 14:11 ` Christophe TROESTLER
  3 siblings, 0 replies; 5+ messages in thread
From: Christophe TROESTLER @ 2009-03-04 14:11 UTC (permalink / raw)
  To: sevaAtWork; +Cc: caml-list

On Wed, 4 Mar 2009 16:31:02 +0300, Vsevolod Fedorov wrote:
> 
> 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?

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

ChriS


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-03-04 14:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-04 13:31 recursive records with weak hashtbl Vsevolod Fedorov
2009-03-04 13:55 ` [Caml-list] " Florent Ouchet
2009-03-04 14:00 ` Virgile Prevosto
2009-03-04 14:00 ` Martin Jambon
2009-03-04 14:11 ` Christophe TROESTLER

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).