Loading ...
Sorry, an error occurred while loading the content.
 

Re: "ocaml_beginners"::[] First start of a program

Expand Messages
  • Richard Jones
    ... Within the file, initialization is top to bottom. Across modules: You have to specify the modules in dependency order when linking. So if B depends on A,
    Message 1 of 5 , Apr 8, 2006
      On Fri, Apr 07, 2006 at 06:34:37PM +0200, Oliver Bandel wrote:
      > And in which order?

      Within the file, initialization is top to bottom.

      Across modules: You have to specify the modules in dependency order
      when linking. So if B depends on A, you have to link them as:

      ocamlc a.cmo b.cmo -o prog

      OCaml doesn't let you have loops in your dependency graph, so you
      can't have a situation where B depends on A and A depends on B, or
      three modules all depending on each other, and so on. The reason for
      this is because OCaml needs to provide safe initialisation - see
      below. The initializations are run in the same order (A before B).

      > Is this always the same order or can it be different, depending
      > on platform or OCaml-version?

      Hopefully - otherwise it'd be a nasty bug :-)

      There's some more on this from Jacques Garrigue, but unfortunately the
      caml.inria.fr webserver is down so I'll have to inline his reply here:

      ----------------------------------------------------------------------
      Date: 2006-01-17 (10:21)
      From: Jacques Garrigue <garrigue@m...>
      Subject: Re: [Caml-list] mutually recursive modules

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


      --
      Richard Jones, CTO Merjis Ltd.
      Merjis - web marketing and technology - http://merjis.com
      Team Notepad - intranets and extranets for business - http://team-notepad.com
    • Oliver Bandel
      Hi, ... [...] OK, thanks. And what is with mutual recursive modules? They are possible since a while. Ciao, Oliver
      Message 2 of 5 , Apr 10, 2006
        Hi,


        On Sat, Apr 08, 2006 at 01:05:15PM +0100, Richard Jones wrote:
        > On Fri, Apr 07, 2006 at 06:34:37PM +0200, Oliver Bandel wrote:
        > > And in which order?
        >
        > Within the file, initialization is top to bottom.
        >
        > Across modules: You have to specify the modules in dependency order
        > when linking. So if B depends on A, you have to link them as:
        >
        > ocamlc a.cmo b.cmo -o prog
        >
        > OCaml doesn't let you have loops in your dependency graph, so you
        > can't have a situation where B depends on A and A depends on B, or
        > three modules all depending on each other, and so on. The reason for
        > this is because OCaml needs to provide safe initialisation - see
        > below. The initializations are run in the same order (A before B).
        >
        > > Is this always the same order or can it be different, depending
        > > on platform or OCaml-version?
        >
        > Hopefully - otherwise it'd be a nasty bug :-)
        >
        > There's some more on this from Jacques Garrigue, but unfortunately the
        > caml.inria.fr webserver is down so I'll have to inline his reply here:
        [...]

        OK, thanks.


        And what is with mutual recursive modules?
        They are possible since a while.


        Ciao,
        Oliver
      • Jonathan Roewen
        ... These are only possible nested inside a module: i.e. Module.A & Module.B. So dependencies between both modules could be worked out systematically without
        Message 3 of 5 , Apr 10, 2006
          > And what is with mutual recursive modules?
          > They are possible since a while.

          These are only possible nested inside a module: i.e. Module.A & Module.B.

          So dependencies between both modules could be worked out
          systematically without any problems (probably similar to mutually
          recursive functions).

          Jonathan
        Your message has been successfully submitted and would be delivered to recipients shortly.