Version française
Home     About     Download     Resources     Contact us    
Browse thread
mutually recursive modules
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@m...>
Subject: Re: [Caml-list] mutually recursive modules
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