caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* mutually recursive modules
@ 2008-03-22 19:22 Jacques Le Normand
  2008-03-22 19:37 ` [Caml-list] " Daniel Bünzli
  0 siblings, 1 reply; 10+ messages in thread
From: Jacques Le Normand @ 2008-03-22 19:22 UTC (permalink / raw)
  To: caml-list

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

Hello caml-list
is it possible to put mutually recursive modules in different files?
--Jacques

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

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

* Re: [Caml-list] mutually recursive modules
  2008-03-22 19:22 mutually recursive modules Jacques Le Normand
@ 2008-03-22 19:37 ` Daniel Bünzli
  2008-03-22 21:20   ` ketti
  0 siblings, 1 reply; 10+ messages in thread
From: Daniel Bünzli @ 2008-03-22 19:37 UTC (permalink / raw)
  To: caml-list caml-list


Le 22 mars 08 à 20:22, Jacques Le Normand a écrit :

> is it possible to put mutually recursive modules in different files?

No.

Daniel


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

* Re: [Caml-list] mutually recursive modules
  2008-03-22 19:37 ` [Caml-list] " Daniel Bünzli
@ 2008-03-22 21:20   ` ketti
  2008-03-23 16:59     ` Jacques Le Normand
  0 siblings, 1 reply; 10+ messages in thread
From: ketti @ 2008-03-22 21:20 UTC (permalink / raw)
  To: Daniel Bünzli; +Cc: caml-list caml-list

>  > is it possible to put mutually recursive modules in different files?
>
>  No.

Yes.
If you wrap them in functors.


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

* Re: [Caml-list] mutually recursive modules
  2008-03-22 21:20   ` ketti
@ 2008-03-23 16:59     ` Jacques Le Normand
  2008-03-23 18:37       ` ketti
  0 siblings, 1 reply; 10+ messages in thread
From: Jacques Le Normand @ 2008-03-23 16:59 UTC (permalink / raw)
  To: caml-list caml-list

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

how would I do that?

On Sat, Mar 22, 2008 at 5:20 PM, ketti <kattlachan@gmail.com> wrote:

> >  > is it possible to put mutually recursive modules in different files?
> >
> >  No.
>
> Yes.
> If you wrap them in functors.
>
> _______________________________________________
> 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
>

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

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

* Re: [Caml-list] mutually recursive modules
  2008-03-23 16:59     ` Jacques Le Normand
@ 2008-03-23 18:37       ` ketti
  2008-03-24  9:21         ` rossberg
  0 siblings, 1 reply; 10+ messages in thread
From: ketti @ 2008-03-23 18:37 UTC (permalink / raw)
  To: Jacques Le Normand; +Cc: caml-list caml-list

2008/3/23 Jacques Le Normand <rathereasy@gmail.com>:
> how would I do that?
>

You have to factor out the types of the modules.
Lets say we have modules A and B which are mutually recursive.
A needs to know the signature of B and vise verse.

So we define the signatures somewhere visible to both A and B:

module type A_T = sig ... end
module type B_T = sig ... end

Now in A and B we can write something like this:

module MkA = functor (B: B_T) -> struct
  ...
end

And then we can tie it together in a third file:

module MkA = A.MkA
module MkB = B.MkB
module rec A : A_T = MkA(B)
and B : B_T = MkB(A)

Done. ^^ (unless i made mistakes)

Does someone know of a less cumbersome way?


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

* Re: [Caml-list] mutually recursive modules
  2008-03-23 18:37       ` ketti
@ 2008-03-24  9:21         ` rossberg
  0 siblings, 0 replies; 10+ messages in thread
From: rossberg @ 2008-03-24  9:21 UTC (permalink / raw)
  To: caml-list

"ketti" <kattlachan@gmail.com> wrote:
>
> You have to factor out the types of the modules.
> Lets say we have modules A and B which are mutually recursive.
> A needs to know the signature of B and vise verse.
>
> So we define the signatures somewhere visible to both A and B:
>
> module type A_T = sig ... end
> module type B_T = sig ... end

Note that this only works if the signatures themselves aren't mutually
recursive - which often is the case. Unfortunately, there is no solution
that works in the general case.

- Andreas



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

* Re: [Caml-list] mutually recursive modules
  2006-01-17 10:21 ` [Caml-list] " Jacques Garrigue
@ 2006-01-17 12:13   ` skaller
  0 siblings, 0 replies; 10+ messages in thread
From: skaller @ 2006-01-17 12:13 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: catphive.lists, caml-list

On Tue, 2006-01-17 at 19:21 +0900, Jacques Garrigue wrote:

> However, other type definitions (like sums and records) are not
> structural, so you cannot move them around. So most people just define
> all their exported types in a single module, which can be seen by all
> the program. You can avoid it, but this makes life simpler.

Unfortunately, this idea is very nice but doesn't work
with the current Ocaml toolset due to the fact the parser
generates the token type.

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net


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

* Re: [Caml-list] mutually recursive modules
  2006-01-17  8:55 Brendan Miller
@ 2006-01-17 10:21 ` Jacques Garrigue
  2006-01-17 12:13   ` skaller
  0 siblings, 1 reply; 10+ messages in thread
From: Jacques Garrigue @ 2006-01-17 10:21 UTC (permalink / raw)
  To: catphive.lists; +Cc: caml-list

From: Brendan Miller <catphive.lists@gmail.com>

> I'm new to ocaml.
> 
> My understanding is that the ocaml compiler does not allow for
> circular dependancy of modules. What is the design reason for this?

In ocaml modules do not only define functions, but also values.
It means that they require computation at initialization time.
If two modules recurse with each other, which one should be
initialized first? What happens when one accesses an uninitialized
value?
C has one answer: no initialization in modules, and (IIRC) random
values when unitialized.
Java has another answer: somewhat arbitrary initialization order, and
each type has a default value.
In ocaml initialization happens, and there are no default values, so
there is no good answer to what should happen with mutual
recursion. So this is prohibited.

> This is annoying. Of course I need a way to make classes from
> different modules recur. Is there a syntax for specifying an interface
> to the type I'm recurring with without that type actually being in the
> same module? That would be somewhat cumbersome, but would actually
> more properly separate the types than the code above.

Yes, you can define interfaces with "class type".
Since class types are "structural", i.e. two identical definitions in
different places are seen as equal, this allows recursion at the type
level.
However, other type definitions (like sums and records) are not
structural, so you cannot move them around. So most people just define
all their exported types in a single module, which can be seen by all
the program. You can avoid it, but this makes life simpler.

Jacques


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

* Re: [Caml-list] mutually recursive modules
  2005-12-02 10:15 Jonathan Roewen
@ 2005-12-06 17:33 ` Alain Frisch
  0 siblings, 0 replies; 10+ messages in thread
From: Alain Frisch @ 2005-12-06 17:33 UTC (permalink / raw)
  To: Jonathan Roewen; +Cc: caml-list

Jonathan Roewen wrote:
> Can one make mutually recursive modules (inside the one .ml file of course)?

Yes, see Section 7.9 of the manual.

-- Alain


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

* [Caml-list] mutually recursive modules
@ 2005-12-02 10:15 Jonathan Roewen
  2005-12-06 17:33 ` Alain Frisch
  0 siblings, 1 reply; 10+ messages in thread
From: Jonathan Roewen @ 2005-12-02 10:15 UTC (permalink / raw)
  To: caml-list

Hi,

Can one make mutually recursive modules (inside the one .ml file of course)?

Jonathan


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

end of thread, other threads:[~2008-03-24  9:21 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-22 19:22 mutually recursive modules Jacques Le Normand
2008-03-22 19:37 ` [Caml-list] " Daniel Bünzli
2008-03-22 21:20   ` ketti
2008-03-23 16:59     ` Jacques Le Normand
2008-03-23 18:37       ` ketti
2008-03-24  9:21         ` rossberg
  -- strict thread matches above, loose matches on Subject: below --
2006-01-17  8:55 Brendan Miller
2006-01-17 10:21 ` [Caml-list] " Jacques Garrigue
2006-01-17 12:13   ` skaller
2005-12-02 10:15 Jonathan Roewen
2005-12-06 17:33 ` Alain Frisch

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