caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* mutually recursive modules
@ 2006-01-17  8:55 Brendan Miller
  2006-01-17 10:21 ` [Caml-list] " Jacques Garrigue
  0 siblings, 1 reply; 4+ messages in thread
From: Brendan Miller @ 2006-01-17  8:55 UTC (permalink / raw)
  To: caml-list

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? I
don't think I've ever seen a language before with this limitation. Is
it just to keep people from writing mutually recursive functions
across modules?

Suppose there are two classes, A and B defined in separate modules.
Does the no circular dependancy rule preclude a doubly linked
relationship between A and B? Let's say I want to create a doubly
linked list with alternating element types.

consider the following code, which fails to compile:

A.mli:

class c :
object
  method set_prev : B.c -> unit
  method get_prev : B.c
  method set_next : B.c -> unit
  method get_next : B.c
end


A.ml

class c =
object
  val mutable m_prev : B.c option = None
  val mutable m_next : B.c option = None

  method set_prev prev = m_prev <- prev
  method get_prev = m_prev
  method set_next next = m_next <- next
  method get_next = m_next
end;;


B.mli

class c :
object
  method set_prev : A.c -> unit
  method get_prev : A.c
  method set_next : A.c -> unit
  method get_next : A.c
end

B.ml

class c =
object
  val mutable m_prev : A.c option = None
  val mutable m_next : A.c option = None
  method set_prev prev = m_prev <- prev
  method get_prev = m_prev
  method set_next next = m_next <- next
  method get_next = m_next
end;;

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.

In general, could someone clarify the rational for making type
recursion so cumbersome?

thx


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

* Re: [Caml-list] mutually recursive modules
  2006-01-17  8:55 mutually recursive modules Brendan Miller
@ 2006-01-17 10:21 ` Jacques Garrigue
  2006-01-17 12:13   ` skaller
  0 siblings, 1 reply; 4+ 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] 4+ 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; 4+ 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] 4+ messages in thread

* mutually recursive modules
@ 2008-03-22 19:22 Jacques Le Normand
  0 siblings, 0 replies; 4+ 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] 4+ messages in thread

end of thread, other threads:[~2008-03-22 19:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-01-17  8:55 mutually recursive modules Brendan Miller
2006-01-17 10:21 ` [Caml-list] " Jacques Garrigue
2006-01-17 12:13   ` skaller
2008-03-22 19:22 Jacques Le Normand

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