Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] Mutually recursive types in different modules

16 views
Skip to first unread message

Andre Nathan

unread,
Jul 14, 2008, 12:50:10 PM7/14/08
to caml...@inria.fr
Hello

Say I have the following type definition:

type a = { x: int; foo: b } and b = { y: int; bar: a }

Is it possible to define types a and b in their own files (thus in
modules A and B) and still allow them to be mutually recursive?

Thanks in advance,
Andre

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Jon Harrop

unread,
Jul 14, 2008, 1:05:43 PM7/14/08
to caml...@yquem.inria.fr
On Monday 14 July 2008 17:50:02 Andre Nathan wrote:
> Hello
>
> Say I have the following type definition:
>
> type a = { x: int; foo: b } and b = { y: int; bar: a }
>
> Is it possible to define types a and b in their own files (thus in
> modules A and B) and still allow them to be mutually recursive?

Yes. See the OCaml Journal article "Tricks with recursion: knots, modules and
polymorphism" or Google for the phrase "untying the recursive knot".

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e

Martin Jambon

unread,
Jul 14, 2008, 1:47:29 PM7/14/08
to Andre Nathan, caml...@inria.fr
On Mon, 14 Jul 2008, Andre Nathan wrote:

> Hello
>
> Say I have the following type definition:
>
> type a = { x: int; foo: b } and b = { y: int; bar: a }
>
> Is it possible to define types a and b in their own files (thus in
> modules A and B) and still allow them to be mutually recursive?

No.
The usual way to proceed is to use one file for the type definitions and
as many files as you want for the implementation.

If after that you want to make the types abstract for external use, you
can use the -pack option of ocamlc or ocamlopt. It lets you define a
super module with its own .mli file.


Martin

--
http://wink.com/profile/mjambon
http://mjambon.com/

Arthur Chan

unread,
Jul 14, 2008, 1:57:45 PM7/14/08
to Jon Harrop, caml...@yquem.inria.fr
As Jon demonstrates it is "possible," but I do not think it truly achieves
what you are trying to do. What is your use case? I do not recommend
trying to make types recursive across modules. Even recursively defined
modules have reared their ugly end on me. It is equivalent to writing
non-orthogonal code, in the functional paradigm (see Pragmatic Programmer).

--
-----------------------------------------------------------------------
(\__/)
(='.'=)This is Bunny. Copy and paste Bunny into your
(")_(")signature to help him gain world domination.
------------------------------------------------------------------------

Andre Nathan

unread,
Jul 14, 2008, 2:04:52 PM7/14/08
to Arthur Chan, caml...@yquem.inria.fr
On Mon, 2008-07-14 at 10:57 -0700, Arthur Chan wrote:
> As Jon demonstrates it is "possible," but I do not think it truly
> achieves what you are trying to do. What is your use case? I do not
> recommend trying to make types recursive across modules. Even
> recursively defined modules have reared their ugly end on me. It is
> equivalent to writing non-orthogonal code, in the functional paradigm
> (see Pragmatic Programmer).

I had a graph type that depended on a vertex type (Vertex.t), which in
its turn is a record with a graph field (of type Graph.t).

I parametrized the Graph.t type and then used it in the Vertex module as
"vertex Graph.t". I guess I have a better design now.

Thanks,
Andre

0 new messages