caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Mutually recursive types in different modules
@ 2008-07-14 16:50 Andre Nathan
  2008-07-14 17:04 ` [Caml-list] " Jon Harrop
  2008-07-14 17:43 ` Martin Jambon
  0 siblings, 2 replies; 6+ messages in thread
From: Andre Nathan @ 2008-07-14 16:50 UTC (permalink / raw)
  To: caml-list

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


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

* Re: [Caml-list] Mutually recursive types in different modules
  2008-07-14 16:50 Mutually recursive types in different modules Andre Nathan
@ 2008-07-14 17:04 ` Jon Harrop
  2008-07-14 17:50   ` Andre Nathan
  2008-07-14 17:57   ` Arthur Chan
  2008-07-14 17:43 ` Martin Jambon
  1 sibling, 2 replies; 6+ messages in thread
From: Jon Harrop @ 2008-07-14 17:04 UTC (permalink / raw)
  To: caml-list

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


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

* Re: [Caml-list] Mutually recursive types in different modules
  2008-07-14 16:50 Mutually recursive types in different modules Andre Nathan
  2008-07-14 17:04 ` [Caml-list] " Jon Harrop
@ 2008-07-14 17:43 ` Martin Jambon
  1 sibling, 0 replies; 6+ messages in thread
From: Martin Jambon @ 2008-07-14 17:43 UTC (permalink / raw)
  To: Andre Nathan; +Cc: caml-list

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/


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

* Re: [Caml-list] Mutually recursive types in different modules
  2008-07-14 17:04 ` [Caml-list] " Jon Harrop
@ 2008-07-14 17:50   ` Andre Nathan
  2008-07-14 17:57   ` Arthur Chan
  1 sibling, 0 replies; 6+ messages in thread
From: Andre Nathan @ 2008-07-14 17:50 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

On Mon, 2008-07-14 at 18:04 +0100, Jon Harrop wrote:
> Yes. See the OCaml Journal article "Tricks with recursion: knots, modules and 
> polymorphism" or Google for the phrase "untying the recursive knot".

Thanks for the pointer. I've found the discussion at

   http://tech.groups.yahoo.com/group/ocaml_beginners/message/9062

where your and Rémi's suggestions were extremely helpful.

Best,
Andre


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

* Re: [Caml-list] Mutually recursive types in different modules
  2008-07-14 17:04 ` [Caml-list] " Jon Harrop
  2008-07-14 17:50   ` Andre Nathan
@ 2008-07-14 17:57   ` Arthur Chan
  2008-07-14 18:04     ` Andre Nathan
  1 sibling, 1 reply; 6+ messages in thread
From: Arthur Chan @ 2008-07-14 17:57 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1598 bytes --]

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).



On Mon, Jul 14, 2008 at 10:04 AM, Jon Harrop <jon@ffconsultancy.com> wrote:

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



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

[-- Attachment #2: Type: text/html, Size: 2480 bytes --]

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

* Re: [Caml-list] Mutually recursive types in different modules
  2008-07-14 17:57   ` Arthur Chan
@ 2008-07-14 18:04     ` Andre Nathan
  0 siblings, 0 replies; 6+ messages in thread
From: Andre Nathan @ 2008-07-14 18:04 UTC (permalink / raw)
  To: Arthur Chan; +Cc: Jon Harrop, caml-list

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


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

end of thread, other threads:[~2008-07-14 18:04 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-14 16:50 Mutually recursive types in different modules Andre Nathan
2008-07-14 17:04 ` [Caml-list] " Jon Harrop
2008-07-14 17:50   ` Andre Nathan
2008-07-14 17:57   ` Arthur Chan
2008-07-14 18:04     ` Andre Nathan
2008-07-14 17:43 ` Martin Jambon

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).