List Info

Thread: "ocaml_beginners"::[] Loading modules




"ocaml_beginners"::[] Loading modules
user name
2006-12-13 03:08:19

Hi,
this is probably a very stupid question but somehow I am stuck here. I
have written several modules where the more central ones are based on
several of the peripheral ones. Now if I run this in the toploop I can
use the functions from the modules without a prefix <Modulename>. if I
use open. However, if I want to compile I still seem to need the open
but the module name as well. Is it possible to open the module in a way
that I can also use the functions from the modules without a prefix
like in the toploop? It's no problem to ensure that the function names
stay unique.

Cheers,
Christian

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Loading modules
user name
2006-12-13 03:45:06

add: open Modulename

open doesn't load the code of a module into the runtime (toploop or
otherwise) -- it uses cmi files to determine the interface of a
module.

in toplevel, #load does that job. at compiler, specifying the .cmo /
.cmx on the command line does the equivalent of #load.

Jonathan

On 12/13/06, Christian Lerrahn < ocaml%40penpal4u.net">ocamlpenpal4u.net> wrote:
&gt; Hi,
> this is probably a very stupid question but somehow I am stuck here. I
> have written several modules where the more central ones are based on
> several of the peripheral ones. Now if I run this in the toploop I can
> use the functions from the modules without a prefix <Modulename>. if I
> use open. However, if I want to compile I still seem to need the open
> but the module name as well. Is it possible to open the module in a way
> that I can also use the functions from the modules without a prefix
&gt; like in the toploop? It's no problem to ensure that the function names
>; stay unique.
&gt;
> Cheers,
&gt; Christian
>
>
> Archives up to November 11, 2006 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
> The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
> Attachments are banned and you're asked to be polite, avoid flames etc.
> Yahoo! Groups Links
>;
>
&gt;
>

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Loading modules
user name
2006-12-13 03:44:38

On Wed, 13 Dec 2006, Christian Lerrahn wrote:

> Hi,
> this is probably a very stupid question but somehow I am stuck here. I
> have written several modules where the more central ones are based on
> several of the peripheral ones. Now if I run this in the toploop I can
> use the functions from the modules without a prefix <Modulename>. if I
> use open. However, if I want to compile I still seem to need the open
> but the module name as well. Is it possible to open the module in a way
> that I can also use the functions from the modules without a prefix
&gt; like in the toploop? It's no problem to ensure that the function names
>; stay unique.

I don't understand. Maybe you can show us some code, I am sure it's a very
simple problem.
Normally it works like this:

(* file a.ml *)
let hello () = print_endline "hello"

(* file b.ml *)
A.hello ();;
open A;;
hello ();;

Martin

--
Martin Jambon, PhD
http://martin.jambon.free.fr

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Loading modules
user name
2006-12-13 04:05:40

Hi Jonathan,
thanks for the reply but I'm even more confused now. This is what I have

open Mod1;;
module Mod2 = ...
function1 a = Mod1.function2;;
...

This works in both the toploop and for compiling. However, if I make
this

open Mod1;;
module Mod2 = ...
function1 a = function2;;
...

The toploop will still execute it but the compiler will cann function2
and unbound value. As it does accept the prefixed version, apparently it
has loaded the other module from its file. This is not the case if I
don't have an open statement which results in e.g. "Unbound constructor
Mod1.Constructor1&quot;. Do I have to add the Mod1.? I'd like to avoid that
if possible.

Cheers,
Christian

Am Wed, 13 Dec 2006 16:45:06 +1300
schrieb "Jonathan Roewen&quot; < jonathan.roewen%40gmail.com">jonathan.roewengmail.com>:

&gt; add: open Modulename
>
> open doesn't load the code of a module into the runtime (toploop or
> otherwise) -- it uses cmi files to determine the interface of a
> module.
&gt;
> in toplevel, #load does that job. at compiler, specifying the .cmo /
> .cmx on the command line does the equivalent of #load.
&gt;
> Jonathan
>
> On 12/13/06, Christian Lerrahn < ocaml%40penpal4u.net">ocamlpenpal4u.net> wrote:
&gt; > Hi,
> > this is probably a very stupid question but somehow I am stuck
>; > here. I have written several modules where the more central ones
> > are based on several of the peripheral ones. Now if I run this in
> > the toploop I can use the functions from the modules without a
> > prefix <Modulename>. if I use open. However, if I want to compile I
> > still seem to need the open but the module name as well. Is it
> > possible to open the module in a way that I can also use the
> > functions from the modules without a prefix like in the toploop?
> > It's no problem to ensure that the function names stay unique.
&gt; >
> > Cheers,
&gt; > Christian
> >
> >
> > Archives up to November 11, 2006 are also downloadable at
> > http://www.connettivo.net/cntprojects/ocaml_beginners/ The archives
> > of the very official ocaml list (the seniors' one) can be found at
> > http://caml.inria.fr Attachments are banned and you're asked to be
> > polite, avoid flames etc. Yahoo! Groups Links
>; >
> >
> >
> >

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Loading modules
user name
2006-12-13 05:50:05

ooh, I think I see your problem.

a file is a module, formed from the filename.

so file mod1.ml:

let function1 .... = ...

file mod2.ml:

open Mod1

(* do something with *) function1 (* params *)

having file mod1.ml:

module Mod1 = struct
let function1 ... = ....
end

results in two modules. module from file Mod1, then nested module,
Mod1, so without open Mod1, function1 is Mod1.Mod1.function1, with
open Mod1, you can omit the first Mod1, giving Mod1.function1 =P

Jonathan

On 12/13/06, Christian Lerrahn < ocaml%40penpal4u.net">ocamlpenpal4u.net> wrote:
&gt; Hi Jonathan,
> thanks for the reply but I'm even more confused now. This is what I have
>
> open Mod1;;
&gt; module Mod2 = ...
> function1 a = Mod1.function2;;
&gt; ...
>
> This works in both the toploop and for compiling. However, if I make
> this
>
> open Mod1;;
&gt; module Mod2 = ...
> function1 a = function2;;
> ...
>
> The toploop will still execute it but the compiler will cann function2
> and unbound value. As it does accept the prefixed version, apparently it
> has loaded the other module from its file. This is not the case if I
> don't have an open statement which results in e.g. "Unbound constructor
> Mod1.Constructor1&quot;. Do I have to add the Mod1.? I'd like to avoid that
> if possible.
>
> Cheers,
&gt; Christian
>
> Am Wed, 13 Dec 2006 16:45:06 +1300
> schrieb "Jonathan Roewen&quot; < jonathan.roewen%40gmail.com">jonathan.roewengmail.com>:
>;
> > add: open Modulename
> >
> > open doesn't load the code of a module into the runtime (toploop or
> > otherwise) -- it uses cmi files to determine the interface of a
> > module.
&gt; >
> > in toplevel, #load does that job. at compiler, specifying the .cmo /
> > .cmx on the command line does the equivalent of #load.
&gt; >
> > Jonathan
> >
> > On 12/13/06, Christian Lerrahn < ocaml%40penpal4u.net">ocamlpenpal4u.net> wrote:
&gt; > > Hi,
> > > this is probably a very stupid question but somehow I am stuck
>; > > here. I have written several modules where the more central ones
> > > are based on several of the peripheral ones. Now if I run this in
> > > the toploop I can use the functions from the modules without a
> > > prefix <Modulename>. if I use open. However, if I want to compile I
> > > still seem to need the open but the module name as well. Is it
> > > possible to open the module in a way that I can also use the
> > > functions from the modules without a prefix like in the toploop?
> > > It's no problem to ensure that the function names stay unique.
&gt; > >
> > > Cheers,
&gt; > > Christian
> > >
> > >
> > > Archives up to November 11, 2006 are also downloadable at
> > > http://www.connettivo.net/cntprojects/ocaml_beginners/ The archives
> > > of the very official ocaml list (the seniors' one) can be found at
> > > http://caml.inria.fr Attachments are banned and you're asked to be
> > > polite, avoid flames etc. Yahoo! Groups Links
>; > >
> > >
> > >
> > >
>
>
>; Archives up to November 11, 2006 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
> The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
> Attachments are banned and you're asked to be polite, avoid flames etc.
> Yahoo! Groups Links
>;
>
&gt;
>

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Modules tutorial
user name
2006-12-13 20:33:46

Since this question is frequently asked by beginners, I took the liberty
of rewriting the "Modules" tutorial at the www.ocaml-tutorial.org wiki:

http://www.ocaml-tutorial.org/modules

Martin

On Wed, 13 Dec 2006, Jonathan Roewen wrote:

> ooh, I think I see your problem.
>
> a file is a module, formed from the filename.
>
> so file mod1.ml:
>
> let function1 .... = ...
>
> file mod2.ml:
>
> open Mod1
>
> (* do something with *) function1 (* params *)
>
> having file mod1.ml:
>
> module Mod1 = struct
&gt; let function1 ... = ....
> end
>
> results in two modules. module from file Mod1, then nested module,
&gt; Mod1, so without open Mod1, function1 is Mod1.Mod1.function1, with
> open Mod1, you can omit the first Mod1, giving Mod1.function1 =P
>
> Jonathan
>
>
> On 12/13/06, Christian Lerrahn < ocaml%40penpal4u.net">ocamlpenpal4u.net> wrote:
&gt; > Hi Jonathan,
> > thanks for the reply but I'm even more confused now. This is what I have
> >
> > open Mod1;;
&gt; > module Mod2 = ...
> > function1 a = Mod1.function2;;
&gt; > ...
> >
> > This works in both the toploop and for compiling. However, if I make
> > this
> >
> > open Mod1;;
&gt; > module Mod2 = ...
> > function1 a = function2;;
> > ...
> >
> > The toploop will still execute it but the compiler will cann function2
> > and unbound value. As it does accept the prefixed version, apparently it
> > has loaded the other module from its file. This is not the case if I
> > don't have an open statement which results in e.g. "Unbound constructor
> > Mod1.Constructor1&quot;. Do I have to add the Mod1.? I'd like to avoid that
> > if possible.
> >
> > Cheers,
&gt; > Christian
> >
> > Am Wed, 13 Dec 2006 16:45:06 +1300
> > schrieb "Jonathan Roewen&quot; < jonathan.roewen%40gmail.com">jonathan.roewengmail.com>:
>; >
> > > add: open Modulename
> > >
> > > open doesn't load the code of a module into the runtime (toploop or
> > > otherwise) -- it uses cmi files to determine the interface of a
> > > module.
&gt; > >
> > > in toplevel, #load does that job. at compiler, specifying the .cmo /
> > > .cmx on the command line does the equivalent of #load.
&gt; > >
> > > Jonathan
> > >
> > > On 12/13/06, Christian Lerrahn < ocaml%40penpal4u.net">ocamlpenpal4u.net> wrote:
&gt; > > > Hi,
> > > > this is probably a very stupid question but somehow I am stuck
>; > > > here. I have written several modules where the more central ones
> > > > are based on several of the peripheral ones. Now if I run this in
> > > > the toploop I can use the functions from the modules without a
> > > > prefix <Modulename>. if I use open. However, if I want to compile I
> > > > still seem to need the open but the module name as well. Is it
> > > > possible to open the module in a way that I can also use the
> > > > functions from the modules without a prefix like in the toploop?
> > > > It's no problem to ensure that the function names stay unique.
&gt; > > >
> > > > Cheers,
&gt; > > > Christian
> > > >
> > > >
> > > > Archives up to November 11, 2006 are also downloadable at
> > > > http://www.connettivo.net/cntprojects/ocaml_beginners/ The archives
> > > > of the very official ocaml list (the seniors' one) can be found at
> > > > http://caml.inria.fr Attachments are banned and you're asked to be
> > > > polite, avoid flames etc. Yahoo! Groups Links
>; > > >
> > > >
> > > >
> > > >
> >
> >
> > Archives up to November 11, 2006 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
> > The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
> > Attachments are banned and you're asked to be polite, avoid flames etc.
> > Yahoo! Groups Links
>; >
> >
> >
> >
>
>
>; Archives up to November 11, 2006 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
> The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr
> Attachments are banned and you're asked to be polite, avoid flames etc.
> Yahoo! Groups Links
>;
>
&gt;
>

--
Martin Jambon, PhD
http://martin.jambon.free.fr

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Modules tutorial
user name
2006-12-13 22:28:23

Hi Martin.
&gt; Since this question is frequently asked by beginners, I took the
> liberty of rewriting the "Modules" tutorial at the
> www.ocaml-tutorial.org wiki:
>;
> http://www.ocaml-tutorial.org/modules

Thanks for that. That's where I first looked for the solution. However,
I could also have guessed the answer from what Jason Hickey writes in
his introduction about files and modules, although it is not too obvious
from that.

Cheers,
Christian

__._,_.___
.

__,_._,___
[1-7]

about | contact  Other archives ( Real Estate discussion Medical topics )