caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: catphive.lists@gmail.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] mutually recursive modules
Date: Tue, 17 Jan 2006 19:21:44 +0900 (JST)	[thread overview]
Message-ID: <20060117.192144.95936185.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <3dd983220601170055w5bd95adfjca47847809f3226e@mail.gmail.com>

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


  reply	other threads:[~2006-01-17 10:21 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-01-17  8:55 Brendan Miller
2006-01-17 10:21 ` Jacques Garrigue [this message]
2006-01-17 12:13   ` [Caml-list] " skaller
  -- strict thread matches above, loose matches on Subject: below --
2008-03-22 19:22 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
2005-12-02 10:15 Jonathan Roewen
2005-12-06 17:33 ` Alain Frisch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060117.192144.95936185.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=catphive.lists@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).