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

Re: "ocaml_beginners"::[] Re: difference between top-level and batch compiler

Expand Messages
  • William D. Neumann
    On Mon, 07 Apr 2008 15:34:44 -0000, alain_coste wrote ... OK, here is your problem. When you have a source file myModule.ml, and you compile it with ocamlc or
    Message 1 of 19 , Apr 7 9:15 AM
      On Mon, 07 Apr 2008 15:34:44 -0000, alain_coste wrote
      > --- In ocaml_beginners@yahoogroups.com, "William D. Neumann"
      > <wneumann@...> wrote:
      > >
      >
      > Thank you for your answer. I tried your example, and got the results
      > I was describing in my first post, not your results.
      > This is a copy of my files, to be sure....
      >
      > ############# m.ml ################
      > module M = struct
      > let x = 1
      > end

      OK, here is your problem. When you have a source file myModule.ml, and you
      compile it with ocamlc or ocamlopt, OCaml puts the contents of the file in a
      module MyModulefile (note the capitalization of the first letter),
      essentially implicitly wrapping it with:

      module MyModule = struct

      {file contents here}

      end

      So with your m.ml, when you compile it, you end up with what looks like:

      module M = struct
      module M = struct
      let x = 1
      end
      end

      So the value is fully qualified as M.M.x, and performing open M results in
      the value being named M.x. This is more or less covered in sections 2.5 and
      8.3 of the manual, but it could be made a bit more explicit.

      --

      William D. Neumann
    • Richard Jones
      ... ^^^^^^^^^^^^ William means MyModule . Rich. -- Richard Jones Red Hat
      Message 2 of 19 , Apr 7 11:39 AM
        On Mon, Apr 07, 2008 at 11:15:50AM -0500, William D. Neumann wrote:
        > OK, here is your problem. When you have a source file myModule.ml, and you
        > compile it with ocamlc or ocamlopt, OCaml puts the contents of the file in a
        > module MyModulefile (note the capitalization of the first letter),
        ^^^^^^^^^^^^

        William means 'MyModule'.

        Rich.

        --
        Richard Jones
        Red Hat
      • Ashish Agarwal
        ... That should not be happening. Is this within the module you are opening in? If in a client module, make sure to note the difference between include and
        Message 3 of 19 , Apr 7 12:00 PM
          > I must BOTH open M AND write M.id to access id.

          That should not be happening. Is this within the module you are opening in?
          If in a client module, make sure to note the difference between "include"
          and "open".


          On Mon, Apr 7, 2008 at 8:24 AM, alain_coste <alaincoste@...>
          wrote:

          > Hello,
          > It seems that the open directive doesn't play the same role when I
          > use the top-level ocaml and the batch compiler ocamlc:
          > With top-level, I can:
          > - either open module M, then access the identifier id defined in M
          > through its simple name id
          > - or access id through the dot notation M.id
          > When I try to compile the same program with ocamlc, I must BOTH open
          > M AND write M.id to access id.
          >
          > I didn't find any mention of this in the doc, which describes the
          > first alternative as the good one. Is there something I have missed,
          > or if not what is the solution to overcome this ?
          >
          > A related question: I like to give significant (which means long)
          > names to my modules. But when I use them, I find it interesting to
          > have short names to use in dot notation. Suppose I have defined the
          > module My_module. To use it, I write:
          >
          > module A_module = struct
          > module M = My_module
          >
          > let x = M.id
          > end
          >
          > That's the way I proceeded in SML.
          > It seems also possible in OCaml, but when I load the
          > file "a_module.ml" with use, I get the complete signature of
          > My_module on the screen: much noise, it becomes difficult to see
          > error messages....
          > Is there another way to proceed? I tried the
          > let module M = My_module in...
          > but this works inside an expression, not a structure.
          >
          >
          >


          [Non-text portions of this message have been removed]
        • Xiaofeng Yang
          I ve tried to define an open and closed type using the sentences as followed: type n_type = [ `Float of float | `Integer of int ];; (*
          Message 4 of 19 , Apr 8 8:28 AM
            I've tried to define an open and closed type using the sentences as
            followed:

            type n_type = [> `Float of float | `Integer of int ];; (* <-- Unbound
            type parameter [..] *)
            type 'a n_type = [> `Float of float | `Integer of int ] as 'a;;(*
            Unbound type parameter [..] *)
            type n_type1 = [< `Float of float | `Integer of int ];;(* Unbound type
            parameter [..] *)
            type 'a n_type1 = [< `Float of float | `Integer of int ] as 'a;;(*
            Unbound type parameter [..] *)

            but it always said "Unbound type parameter" and pointed to the [> ... ]
            and [< ... ]...
            Why ? How can I define an open/closed union type ?

            Regards,
            stlxv
          • Martin Jambon
            ... You were close :-) type a n_type = a constraint a = [ `Float of float | `Integer of int ] Martin -- http://wink.com/profile/mjambon http://mjambon.com
            Message 5 of 19 , Apr 8 8:38 AM
              On Tue, 8 Apr 2008, Xiaofeng Yang wrote:

              > I've tried to define an open and closed type using the sentences as
              > followed:
              >
              > type n_type = [> `Float of float | `Integer of int ];; (* <-- Unbound
              > type parameter [..] *)
              > type 'a n_type = [> `Float of float | `Integer of int ] as 'a;;(*
              > Unbound type parameter [..] *)
              > type n_type1 = [< `Float of float | `Integer of int ];;(* Unbound type
              > parameter [..] *)
              > type 'a n_type1 = [< `Float of float | `Integer of int ] as 'a;;(*
              > Unbound type parameter [..] *)
              >
              > but it always said "Unbound type parameter" and pointed to the [> ... ]
              > and [< ... ]...
              > Why ? How can I define an open/closed union type ?

              You were close :-)

              type 'a n_type = 'a
              constraint 'a = [> `Float of float | `Integer of int ]



              Martin

              --
              http://wink.com/profile/mjambon
              http://mjambon.com
            • Xiaofeng Yang
              It works! Thanks a lot! stlxv
              Message 6 of 19 , Apr 8 8:44 AM
                It works! Thanks a lot!


                stlxv


                Martin Jambon wrote:

                > On Tue, 8 Apr 2008, Xiaofeng Yang wrote:
                >
                > > I've tried to define an open and closed type using the sentences as
                > > followed:
                > >
                > > type n_type = [> `Float of float | `Integer of int ];; (* <-- Unbound
                > > type parameter [..] *)
                > > type 'a n_type = [> `Float of float | `Integer of int ] as 'a;;(*
                > > Unbound type parameter [..] *)
                > > type n_type1 = [< `Float of float | `Integer of int ];;(* Unbound type
                > > parameter [..] *)
                > > type 'a n_type1 = [< `Float of float | `Integer of int ] as 'a;;(*
                > > Unbound type parameter [..] *)
                > >
                > > but it always said "Unbound type parameter" and pointed to the [> ... ]
                > > and [< ... ]...
                > > Why ? How can I define an open/closed union type ?
                >
                > You were close :-)
                >
                > type 'a n_type = 'a
                > constraint 'a = [> `Float of float | `Integer of int ]
                >
                > Martin
                >
                > --
                > http://wink.com/profile/mjambon <http://wink.com/profile/mjambon>
                > http://mjambon.com <http://mjambon.com>
                >
                >
              • alain_coste
                ... and you ... file in a ... Things are clear now; I saw the pages of the manual, but I interpreted them in the wrong way: I thought I had to give to the file
                Message 7 of 19 , Apr 8 6:23 PM
                  --- In ocaml_beginners@yahoogroups.com, "William D. Neumann"
                  <wneumann@...> wrote:
                  > OK, here is your problem. When you have a source file myModule.ml,
                  and you
                  > compile it with ocamlc or ocamlopt, OCaml puts the contents of the
                  file in a
                  > module MyModulefile (note the capitalization of the first letter),
                  > essentially implicitly wrapping it with:
                  >
                  Things are clear now; I saw the pages of the manual, but I
                  interpreted them in the wrong way: I thought I had to give to the
                  file a name derived from the name of the module (as in the gnat ADA
                  compiler for instance).

                  But I still feel that it's not possible to use the same source files
                  with the top-level (and directive #use) and the compiler:
                  Suppose file m2.ml depends on file m1.ml.
                  - To compile m2.ml, I must have a directive open M1 (or use dot
                  notation M1.id) inside m2.ml.
                  - So, to "load" those files in the top-level with #use "m1.ml" and
                  #use "m2.ml", I must put the declarations of m1.ml inside
                  module M1 = struct...end, which brings back to my first problem.
                  - The only solution I found is to load the .cmo in the top-level
                  with the directive #load, but I cannot reload a .cmo as soon as the
                  interface has changed; so I kill ocaml and restart it.

                  I have found a solution to use files without printing the signatures:
                  Toploop.use_silently Format.std_formatter "file.ml";;
                  seems to work.
                • alain_coste
                  ... and you ... file in a ... Things are clear now; I saw the pages of the manual, but I interpreted them in the wrong way: I thought I had to give to the file
                  Message 8 of 19 , Apr 8 6:26 PM
                    --- In ocaml_beginners@yahoogroups.com, "William D. Neumann"
                    <wneumann@...> wrote:
                    > OK, here is your problem. When you have a source file myModule.ml,
                    and you
                    > compile it with ocamlc or ocamlopt, OCaml puts the contents of the
                    file in a
                    > module MyModulefile (note the capitalization of the first letter),
                    > essentially implicitly wrapping it with:
                    >
                    Things are clear now; I saw the pages of the manual, but I
                    interpreted them in the wrong way: I thought I had to give to the
                    file a name derived from the name of the module (as in the gnat ADA
                    compiler for instance).

                    But I still feel that it's not possible to use the same source files
                    with the top-level (and directive #use) and the compiler:
                    Suppose file m2.ml depends on file m1.ml.
                    - To compile m2.ml, I must have a directive open M1 (or use dot
                    notation M1.id) inside m2.ml.
                    - So, to "load" those files in the top-level with #use "m1.ml" and
                    #use "m2.ml", I must put the declarations of m1.ml inside
                    module M1 = struct...end, which brings back to my first problem.
                    - The only solution I found is to load the .cmo in the top-level
                    with the directive #load, but I cannot reload a .cmo as soon as the
                    interface has changed; so I kill ocaml and restart it.

                    I have found a solution to use files without printing the signatures:
                    Toploop.use_silently Format.std_formatter "file.ml";;
                    seems to work.
                  • Remi Vanicat
                    ... Indeed. If a file is intended to be compiled, you should not #use it in the ocaml toplevel, but load it s compiled cmo with #load #use is only intended for
                    Message 9 of 19 , Apr 9 1:51 AM
                      "alain_coste" <alaincoste@...> writes:

                      > But I still feel that it's not possible to use the same source files
                      > with the top-level (and directive #use)

                      Indeed. If a file is intended to be compiled, you should not #use it
                      in the ocaml toplevel, but load it's compiled cmo with #load

                      #use is only intended for code specific to the toplevel: for example
                      you could make a loadall.ml file with

                      #load "foo.cmo"
                      #load "bar.cmo"
                      ....

                      in it.

                      --
                      RĂ©mi Vanicat
                    • Peng Zang
                      ... Hash: SHA1 ... Indeed, this difference between how the toplevel works and how the batch build process works has always irked me. The #load directive for
                      Message 10 of 19 , Apr 9 6:24 AM
                        -----BEGIN PGP SIGNED MESSAGE-----
                        Hash: SHA1


                        On Tuesday 08 April 2008 09:23:46 pm alain_coste wrote:
                        > But I still feel that it's not possible to use the same source files
                        > with the top-level (and directive #use) and the compiler:
                        > Suppose file m2.ml depends on file m1.ml.
                        > - To compile m2.ml, I must have a directive open M1 (or use dot
                        > notation M1.id) inside m2.ml.
                        > - So, to "load" those files in the top-level with #use "m1.ml" and
                        > #use "m2.ml", I must put the declarations of m1.ml inside
                        > module M1 = struct...end, which brings back to my first problem.
                        > - The only solution I found is to load the .cmo in the top-level
                        > with the directive #load, but I cannot reload a .cmo as soon as the
                        > interface has changed; so I kill ocaml and restart it.


                        Indeed, this difference between how the toplevel works and how the batch build
                        process works has always irked me.

                        The "#load" directive for example does not pick up dependencies, you have to
                        do it manually. This means you need to keep a "toplevel load file" around in
                        addition to your Makefile.

                        And as you mentioned there is no way to "compile" inside the toplevel. "#use"
                        gives you the wrong namespace and "#load" doesn't allow you to reload if
                        something has changed.

                        Finally, toplevel directives cannot be put into source files because the batch
                        build process doesn't understand them and will barf.

                        Dispite these shortcomings though, I still think the toplevel is great. I use
                        it all the time. You can work around these shortcomings as you have found by
                        just using #load and restarting the ocaml toplevel when those files change.
                        I find that it is not so bad. 1) I tend to work in one file until the
                        interface has stabilized enough for me to put it in its own file. Thus the
                        files that I #load do not change that often. 2) restarting the ocaml
                        toplevel can be automated so I hardly notice. As to the toplevel directives,
                        I hide them in comments so that the batch compiler will not pick them up. I
                        have taught Emacs however, to recognize them. This allows Emacs to identify
                        such hidden directives, unwrap them from the comments, and send them to the
                        toplevel as usual.


                        Peng
                        -----BEGIN PGP SIGNATURE-----
                        Version: GnuPG v2.0.7 (GNU/Linux)

                        iD8DBQFH/MODfIRcEFL/JewRApgxAJ9ENiGPkZsnJTND7AsKOTKuAhV0qgCgrHjZ
                        X105uvsA7LvKPHn0PTfZ+ds=
                        =FDLJ
                        -----END PGP SIGNATURE-----
                      • alain_coste
                        ... batch build ... you have to ... around in ... toplevel. #use ... reload if ... Hello, Happy to see somebody else being annoyed with the different ways
                        Message 11 of 19 , Apr 10 1:30 AM
                          --- In ocaml_beginners@yahoogroups.com, Peng Zang <peng.zang@...>
                          wrote:
                          > Indeed, this difference between how the toplevel works and how the
                          batch build
                          > process works has always irked me.
                          >
                          > The "#load" directive for example does not pick up dependencies,
                          you have to
                          > do it manually. This means you need to keep a "toplevel load file"
                          around in
                          > addition to your Makefile.
                          >
                          > And as you mentioned there is no way to "compile" inside the
                          toplevel. "#use"
                          > gives you the wrong namespace and "#load" doesn't allow you to
                          reload if
                          > something has changed.
                          >
                          Hello,
                          Happy to see somebody else being annoyed with the different ways of
                          using OCaml!
                          What do you mean by "#use gives the wrong namespace" ?

                          After some experiments, I went to the conclusion that the best way to
                          do my development was to work with the top level, and #use directives:
                          - each module Mxxxx is placed in a file mxxxx.ml, and enclosed in
                          a module Mxxxx = struct...end construct.
                          - inside module Myyyyy I can either open Mxxxx or declare module
                          Mx = Mxxxx to use "short" dot notation Mx.id.
                          - as far as I "load" the files with #use OCaml seems OK, and the
                          toplevel always access the last version of my modules.
                          - now, to produce a stand-alone executable, I have programmed
                          xemacs to: 1) copy the .ml files in another directory, stripping all
                          the "module Mxxxx = struct...end" constructs. 2) send to a Windows
                          command interpreter the command(s) to compile and link those files.
                          So I no longer #load files, and the fact to compile in another
                          directory keeps "my" source directory clean, with only the files I
                          put in it, and not all those .cmi, .cmo, ... added by OCaml.
                          Do you think my method is correct? I supposed that completly
                          separating toplevel and compilation would be safe, but perhaps I made
                          things more complex than they are...
                        • Peng Zang
                          ... Hash: SHA1 ... I just mean if you have a file like this: - -------- begin foo.ml ------------ let x = 1 - -------- end foo.ml -------------- If you compile
                          Message 12 of 19 , Apr 10 7:00 PM
                            -----BEGIN PGP SIGNED MESSAGE-----
                            Hash: SHA1


                            On Thursday 10 April 2008 04:30:21 am alain_coste wrote:
                            > What do you mean by "#use gives the wrong namespace" ?

                            I just mean if you have a file like this:

                            - -------- begin foo.ml ------------

                            let x = 1

                            - -------- end foo.ml --------------

                            If you compile it and "#load" it, then you access the value with "Foo.x".
                            However if you do the "#use" command then you access the value with "x".


                            > After some experiments, I went to the conclusion that the best way to
                            > do my development was to work with the top level, and #use directives:
                            > - each module Mxxxx is placed in a file mxxxx.ml, and enclosed in
                            > a module Mxxxx = struct...end construct.
                            > - inside module Myyyyy I can either open Mxxxx or declare module
                            > Mx = Mxxxx to use "short" dot notation Mx.id.
                            > - as far as I "load" the files with #use OCaml seems OK, and the
                            > toplevel always access the last version of my modules.
                            > - now, to produce a stand-alone executable, I have programmed
                            > xemacs to: 1) copy the .ml files in another directory, stripping all
                            > the "module Mxxxx = struct...end" constructs. 2) send to a Windows
                            > command interpreter the command(s) to compile and link those files.
                            > So I no longer #load files, and the fact to compile in another
                            > directory keeps "my" source directory clean, with only the files I
                            > put in it, and not all those .cmi, .cmo, ... added by OCaml.
                            > Do you think my method is correct? I supposed that completly
                            > separating toplevel and compilation would be safe, but perhaps I made
                            > things more complex than they are...

                            Actually, that sounds like a pretty good idea. I might try that out. You
                            still have to maintain a list of "#use" at the beginning of your files
                            though, correct? I've found it somewhat annoying to maintain the
                            dependencies. If you have any suggestions for that, let me know.

                            Peng
                            -----BEGIN PGP SIGNATURE-----
                            Version: GnuPG v2.0.7 (GNU/Linux)

                            iD8DBQFH/sZRfIRcEFL/JewRAvmeAJ9ITNLVOX8xvFUp3wJUkNntEtqxSwCgqIYI
                            wulrKnJn3alxMqfX4OojgDI=
                            =jwjt
                            -----END PGP SIGNATURE-----
                          • alain_coste
                            ... You ... Hello, You are right, I have to write the list of #use, with the partial order of dependencies; perhaps it could be possible to program emacs to do
                            Message 13 of 19 , Apr 12 3:30 PM
                              --- In ocaml_beginners@yahoogroups.com, Peng Zang <peng.zang@...> wrote:
                              >
                              >
                              > Actually, that sounds like a pretty good idea. I might try that out.
                              You
                              > still have to maintain a list of "#use" at the beginning of your files
                              > though, correct? I've found it somewhat annoying to maintain the
                              > dependencies. If you have any suggestions for that, let me know.
                              >
                              > Peng

                              Hello,
                              You are right, I have to write the list of #use, with the partial
                              order of dependencies; perhaps it could be possible to program emacs
                              to do this, at least for the simpler cases, but it involves much
                              regexp search to track all the references to other modules in the
                              source. I am not sure it is worth the job. The compilation manager
                              of Sml-NJ does this, but it interacts with the compiler for syntax
                              analysis, and it is a non trivial piece of code...
                              However, it's easy to ask emacs to issue the compile-link command
                              automatically from the list of #use. At least I have not to maintain
                              TWO lists, one for top level and one for ocamlc or ocamlopt. The idea
                              is to work classically with the top-level, and have an automated mean
                              of generating the stand alone executable.
                            • Peng Zang
                              ... Hash: SHA1 ... Ahhh, nice. That works, I ll have to give it a try. Thanks, Peng ... Version: GnuPG v2.0.7 (GNU/Linux)
                              Message 14 of 19 , Apr 12 7:27 PM
                                -----BEGIN PGP SIGNED MESSAGE-----
                                Hash: SHA1


                                On Saturday 12 April 2008 06:30:32 pm alain_coste wrote:
                                > --- In ocaml_beginners@yahoogroups.com, Peng Zang <peng.zang@...> wrote:
                                > > Actually, that sounds like a pretty good idea. I might try that out.
                                >
                                > You
                                >
                                > > still have to maintain a list of "#use" at the beginning of your files
                                > > though, correct? I've found it somewhat annoying to maintain the
                                > > dependencies. If you have any suggestions for that, let me know.
                                > >
                                > > Peng
                                >
                                > Hello,
                                > You are right, I have to write the list of #use, with the partial
                                > order of dependencies; perhaps it could be possible to program emacs
                                > to do this, at least for the simpler cases, but it involves much
                                > regexp search to track all the references to other modules in the
                                > source. I am not sure it is worth the job. The compilation manager
                                > of Sml-NJ does this, but it interacts with the compiler for syntax
                                > analysis, and it is a non trivial piece of code...
                                > However, it's easy to ask emacs to issue the compile-link command
                                > automatically from the list of #use. At least I have not to maintain
                                > TWO lists, one for top level and one for ocamlc or ocamlopt. The idea
                                > is to work classically with the top-level, and have an automated mean
                                > of generating the stand alone executable.


                                Ahhh, nice. That works, I'll have to give it a try. Thanks,

                                Peng
                                -----BEGIN PGP SIGNATURE-----
                                Version: GnuPG v2.0.7 (GNU/Linux)

                                iD8DBQFIAW+GfIRcEFL/JewRAiAkAJwN1UBEt8HVpUuOrBDwg0AyEyDmKgCggxbd
                                dxoviyRuCvaRIYjbyaDC4ko=
                                =J3dG
                                -----END PGP SIGNATURE-----
                              • Peng Zang
                                ... Hash: SHA1 ... Actually, to make things even easier, I think I ll write a patch to the toplevel to add a directive called #usec . This will act exactly
                                Message 15 of 19 , Apr 12 7:38 PM
                                  -----BEGIN PGP SIGNED MESSAGE-----
                                  Hash: SHA1


                                  On Saturday 12 April 2008 06:30:32 pm alain_coste wrote:
                                  > --- In ocaml_beginners@yahoogroups.com, Peng Zang <peng.zang@...> wrote:
                                  > > Actually, that sounds like a pretty good idea. I might try that out.
                                  >
                                  > You
                                  >
                                  > > still have to maintain a list of "#use" at the beginning of your files
                                  > > though, correct? I've found it somewhat annoying to maintain the
                                  > > dependencies. If you have any suggestions for that, let me know.
                                  > >
                                  > > Peng
                                  >
                                  > Hello,
                                  > You are right, I have to write the list of #use, with the partial
                                  > order of dependencies; perhaps it could be possible to program emacs
                                  > to do this, at least for the simpler cases, but it involves much
                                  > regexp search to track all the references to other modules in the
                                  > source. I am not sure it is worth the job. The compilation manager
                                  > of Sml-NJ does this, but it interacts with the compiler for syntax
                                  > analysis, and it is a non trivial piece of code...
                                  > However, it's easy to ask emacs to issue the compile-link command
                                  > automatically from the list of #use. At least I have not to maintain
                                  > TWO lists, one for top level and one for ocamlc or ocamlopt. The idea
                                  > is to work classically with the top-level, and have an automated mean
                                  > of generating the stand alone executable.


                                  Actually, to make things even easier, I think I'll write a patch to the
                                  toplevel to add a directive called "#usec". This will act exactly like #use,
                                  but within the name space of the filename. So that, for example, after doing
                                  #usec "foo.ml" you can refer to things in foo as Foo.xxx

                                  Peng
                                  -----BEGIN PGP SIGNATURE-----
                                  Version: GnuPG v2.0.7 (GNU/Linux)

                                  iD8DBQFIAXI1fIRcEFL/JewRAk1UAKCmn/A+lBkfnRPKAChcLjesgf3JbwCfcHOg
                                  Ipc9zom5Za2ys8DOfEwaLBQ=
                                  =ZgT5
                                  -----END PGP SIGNATURE-----
                                Your message has been successfully submitted and would be delivered to recipients shortly.