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

Module spanning multiple files

Expand Messages
  • ospeovedos
    Is there any way that I can make a module (.ml) span several files? I have a module that has gotten a bit difficult to hack, which has the following basic
    Message 1 of 15 , Sep 16, 2005
    • 0 Attachment
      Is there any way that I can make a module (.ml) span several files? I
      have a module that has gotten a bit difficult to hack, which has the
      following basic structure:

      A whole bunch of little functions
      One gigantic object
      Another gigantic object

      I'd like to separate these three parts into three different files but
      still keep them in the same module. (if neccessary I'd be willing to
      give each object their own submodule, but I don't know how to separate
      them either...)
    • Jon Harrop
      ... Have you tried pulling 3 modules together into one using include , e.g.: include A include B include C -- Dr Jon D Harrop, Flying Frog Consultancy Ltd.
      Message 2 of 15 , Sep 17, 2005
      • 0 Attachment
        On Saturday 17 September 2005 07:04, ospeovedos wrote:
        > Is there any way that I can make a module (.ml) span several files? I
        > have a module that has gotten a bit difficult to hack, which has the
        > following basic structure:
        >
        > A whole bunch of little functions
        > One gigantic object
        > Another gigantic object
        >
        > I'd like to separate these three parts into three different files but
        > still keep them in the same module. (if neccessary I'd be willing to
        > give each object their own submodule, but I don't know how to separate
        > them either...)

        Have you tried pulling 3 modules together into one using "include", e.g.:

        include A
        include B
        include C

        --
        Dr Jon D Harrop, Flying Frog Consultancy Ltd.
        Objective CAML for Scientists
        http://www.ffconsultancy.com/products/ocaml_for_scientists
      • Matt Gushee
        ... That doesn t really make it clear, since you don t state the dependencies. But let s assume that the little functions are miscellaneous utility
        Message 3 of 15 , Sep 17, 2005
        • 0 Attachment
          ospeovedos wrote:
          > Is there any way that I can make a module (.ml) span several files? I
          > have a module that has gotten a bit difficult to hack, which has the
          > following basic structure:
          >
          > A whole bunch of little functions
          > One gigantic object
          > Another gigantic object

          That doesn't really make it clear, since you don't state the
          dependencies. But let's assume that the "little functions" are
          miscellaneous utility functions, and the two gigantic objects depend on
          them (BTW, do you mean objects in a strict technical sense--i.e., class
          instances--or something else?). Then object B depends on object A.

          You can more-or-less do what you want using the 'include' directive.

          util.ml:

          [ various utility functions ]

          a.ml:

          include Util

          [ object A ]

          b.ml:

          include A

          [ object B ]

          This way, the components are separated for development purposes, but B
          will appear externally as one big module that includes everything.

          > give each object their own submodule, but I don't know how to separate
          > them either...)

          If you give specific examples (i.e. post some code), I'm sure you'll get
          lots of advice on how to modularize your code better.

          Hope this helps.

          --
          Matt Gushee
          Englewood, CO, USA
        • Damien Guichard
          That also happened to me. The solution i have adopted is: * create 3 separate modules A,B,C * create the big module D like this: module D = struct include A;;
          Message 4 of 15 , Sep 17, 2005
          • 0 Attachment
            That also happened to me.

            The solution i have adopted is:
            * create 3 separate modules A,B,C
            * create the big module D like this:

            module D = struct

            include A;;
            include B;;
            include C;;

            end;;


            I hope it helps,

            - damien


            --- In ocaml_beginners@yahoogroups.com, "ospeovedos" <cedilla@g...> wrote:
            > Is there any way that I can make a module (.ml) span several files? I
            > have a module that has gotten a bit difficult to hack, which has the
            > following basic structure:
            >
            > A whole bunch of little functions
            > One gigantic object
            > Another gigantic object
            >
            > I'd like to separate these three parts into three different files but
            > still keep them in the same module. (if neccessary I'd be willing to
            > give each object their own submodule, but I don't know how to separate
            > them either...)
          • ospeovedos
            ... files? I ... the ... depend on ... class ... By object I actually meant class definitions, but I was tired and couldn t think of the right word. ... but B
            Message 5 of 15 , Sep 17, 2005
            • 0 Attachment
              --- In ocaml_beginners@yahoogroups.com, Matt Gushee <matt@g...> wrote:
              > ospeovedos wrote:
              > > Is there any way that I can make a module (.ml) span several
              files? I
              > > have a module that has gotten a bit difficult to hack, which has
              the
              > > following basic structure:
              > >
              > > A whole bunch of little functions
              > > One gigantic object
              > > Another gigantic object
              >
              > That doesn't really make it clear, since you don't state the
              > dependencies. But let's assume that the "little functions" are
              > miscellaneous utility functions, and the two gigantic objects
              depend on
              > them (BTW, do you mean objects in a strict technical sense--i.e.,
              class
              > instances--or something else?). Then object B depends on object A.

              By object I actually meant class definitions, but I was tired and
              couldn't think of the right word.

              >
              > You can more-or-less do what you want using the 'include' directive.
              >
              > util.ml:
              > [ various utility functions ]
              >
              > a.ml:
              > include Util
              > [ object A ]
              >
              > b.ml:
              > include A
              > [ object B ]
              >
              > This way, the components are separated for development purposes,
              but B
              > will appear externally as one big module that includes everything.

              That works! Thanks. I had completely overlooked the include function.

              I'm actually writing an OCaml interface to libjpeg, with the ability
              to read and write raw DCT coefficients for lossless JPEG transcoding.
              My two classes are the encode_jpeg and the decode_jpeg class.
              My little helper functions are things like colorspace conversion,
              upsampling, DCT/IDCT, etc.. Actually, as I look at my code, I notice
              most of my helper functions are after the two classes.

              Nothing in either of those three parts really depends on each other,
              but they all depend on my type definitions at the beginning.

              I don't want separate modules for each of the parts since the
              interface is fairly simple, but the code is horrendous so I need
              multiple source files.

              Thanks again for your input!

              - Reed
            • ospeovedos
              ... I ve been trying something like that, but I can t quite get it. I need something that can use the same type definitions. So, something like this: module D
              Message 6 of 15 , Sep 17, 2005
              • 0 Attachment
                --- In ocaml_beginners@yahoogroups.com, "Damien Guichard"
                <alphablock@w...> wrote:
                > That also happened to me.
                >
                > The solution i have adopted is:
                > * create 3 separate modules A,B,C
                > * create the big module D like this:
                >
                > module D = struct
                >
                > include A;;
                > include B;;
                > include C;;
                >
                > end;;

                I've been trying something like that, but I can't quite get it. I need
                something that can use the same type definitions. So, something like
                this:
                module D = struct
                type aType = Stuff | OtherStuff;;
                include A;;
                include B;;
                include C;;
                end;;
                And I need aType to be available to A, B, and C. However, in order for
                any one of the included modules to use the type, I need "include D" at
                the beginning of each of A,B,C. That introduces a circularity which
                OCaml does not appreciate... Any way to do this?

                Thanks,
                Reed
              • Jon Harrop
                ... Your example does not require a cyclic dependency, you can just factor the type out into yet another module, call it Z: z.ml: type aType = ... a.ml: ...
                Message 7 of 15 , Sep 17, 2005
                • 0 Attachment
                  On Saturday 17 September 2005 20:34, ospeovedos wrote:
                  > I've been trying something like that, but I can't quite get it. I need
                  > something that can use the same type definitions. So, something like
                  > this:
                  > module D = struct
                  > type aType = Stuff | OtherStuff;;
                  > include A;;
                  > include B;;
                  > include C;;
                  > end;;
                  > And I need aType to be available to A, B, and C. However, in order for
                  > any one of the included modules to use the type, I need "include D" at
                  > the beginning of each of A,B,C. That introduces a circularity which
                  > OCaml does not appreciate... Any way to do this?

                  Your example does not require a cyclic dependency, you can just factor the
                  type out into yet another module, call it Z:

                  z.ml:

                  type aType = ...

                  a.ml:

                  ... Z.aType ...

                  or "type aType = z.aType", or "open Z" or "include Z".

                  d.ml:

                  include A
                  include B
                  include C

                  --
                  Dr Jon D Harrop, Flying Frog Consultancy Ltd.
                  Objective CAML for Scientists
                  http://www.ffconsultancy.com/products/ocaml_for_scientists
                • Matthew O'Connor
                  ... You can put all of your types into one module this is either opened or included by all of the modules that need those types. i.e.: types.ml type t = ...
                  Message 8 of 15 , Sep 17, 2005
                  • 0 Attachment
                    ospeovedos wrote:
                    > --- In ocaml_beginners@yahoogroups.com, "Damien Guichard"
                    > <alphablock@w...> wrote:
                    >
                    >>That also happened to me.
                    >>
                    >>The solution i have adopted is:
                    >>* create 3 separate modules A,B,C
                    >>* create the big module D like this:
                    >>
                    >>module D = struct
                    >>
                    >>include A;;
                    >>include B;;
                    >>include C;;
                    >>
                    >>end;;
                    >
                    >
                    > I've been trying something like that, but I can't quite get it. I need
                    > something that can use the same type definitions. So, something like
                    > this:
                    > module D = struct
                    > type aType = Stuff | OtherStuff;;
                    > include A;;
                    > include B;;
                    > include C;;
                    > end;;
                    > And I need aType to be available to A, B, and C. However, in order for
                    > any one of the included modules to use the type, I need "include D" at
                    > the beginning of each of A,B,C. That introduces a circularity which
                    > OCaml does not appreciate... Any way to do this?
                    >
                    > Thanks,
                    > Reed

                    You can put all of your types into one module this is either opened or
                    included by all of the modules that need those types.

                    i.e.:

                    types.ml
                    type t = ...

                    a.ml
                    open Types
                    ...

                    b.ml
                    open Types
                    ...

                    final.ml
                    include Types
                    include A
                    include B
                    ...

                    This way all of the modules use the types defined in Types. And all of
                    the other modules are exported through Final.

                    hth, matt.
                  • Matt Gushee
                    ... Have you looked at CamlImages? I m pretty sure it has a libjpeg interface. Of course, if yours offers significant improvements or you are just doing it for
                    Message 9 of 15 , Sep 17, 2005
                    • 0 Attachment
                      ospeovedos wrote:

                      > I'm actually writing an OCaml interface to libjpeg,

                      Have you looked at CamlImages? I'm pretty sure it has a libjpeg
                      interface. Of course, if yours offers significant improvements or you
                      are just doing it for practice, go for it!

                      --
                      Matt Gushee
                      Englewood, CO, USA
                    • ospeovedos
                      ... That works! I had tried it with adding include Z at the top of each of A,B,C, but then OCaml complains of multiple definitions of the types. However,
                      Message 10 of 15 , Sep 17, 2005
                      • 0 Attachment
                        --- In ocaml_beginners@yahoogroups.com, Jon Harrop <jon@f...> wrote:
                        > z.ml:
                        >
                        > type aType = ...
                        >
                        > a.ml:
                        >
                        > ... Z.aType ...
                        >
                        > or "type aType = z.aType", or "open Z" or "include Z".
                        >
                        > d.ml:
                        >
                        > include A
                        > include B
                        > include C

                        That works! I had tried it with adding "include Z" at the top of each
                        of A,B,C, but then OCaml complains of multiple definitions of the
                        types. However, using "open Z" at the top of all the files works
                        perfectly.
                        Thanks!

                        - Reed
                      • ospeovedos
                        ... you ... I have indeed looked at it, and it does have a libjpeg interface. However, it is a much higher level interface than mine. The CamlImages interface
                        Message 11 of 15 , Sep 17, 2005
                        • 0 Attachment
                          --- In ocaml_beginners@yahoogroups.com, Matt Gushee <matt@g...> wrote:
                          > ospeovedos wrote:
                          >
                          > > I'm actually writing an OCaml interface to libjpeg,
                          >
                          > Have you looked at CamlImages? I'm pretty sure it has a libjpeg
                          > interface. Of course, if yours offers significant improvements or
                          you
                          > are just doing it for practice, go for it!
                          >
                          > --
                          > Matt Gushee
                          > Englewood, CO, USA

                          I have indeed looked at it, and it does have a libjpeg interface.
                          However, it is a much higher level interface than mine. The
                          CamlImages interface is more or less just "read this file" and "write
                          that file," all in 8-bit-per-component precision. That's perfectly
                          fine for most applications, but I wanted something which could
                          1) do all the trandformations and output the image in 64-bit floating
                          point, and
                          2) access the internal representation of the JPEG image in order to
                          losslessly transcode the image.

                          I also have colorspace conversion routines, the ability to write your
                          own progressive scan script, and a multitude of
                          upsampling/downsampling options (mine supports noninteger sampling
                          ratios, something which not even libjpeg itself can do!)

                          I started out trying to write my own JPEG parser entirely in OCaml,
                          and got about 95% done with the reading before I got stuck. That was
                          mainly just to learn how JPEG compresses, so I didn't mind scrapping
                          all of mine and using libjpeg to make it actually work.
                        • Oliver Bandel
                          On Sun, Sep 18, 2005 at 12:56:27AM -0000, ospeovedos wrote: [...] ... Will you put the sources as a library and give free access to it? Would be nice. Ciao,
                          Message 12 of 15 , Sep 18, 2005
                          • 0 Attachment
                            On Sun, Sep 18, 2005 at 12:56:27AM -0000, ospeovedos wrote:
                            [...]
                            > I have indeed looked at it, and it does have a libjpeg interface.
                            > However, it is a much higher level interface than mine. The
                            > CamlImages interface is more or less just "read this file" and "write
                            > that file," all in 8-bit-per-component precision. That's perfectly
                            > fine for most applications, but I wanted something which could
                            > 1) do all the trandformations and output the image in 64-bit floating
                            > point, and
                            > 2) access the internal representation of the JPEG image in order to
                            > losslessly transcode the image.
                            >
                            > I also have colorspace conversion routines, the ability to write your
                            > own progressive scan script, and a multitude of
                            > upsampling/downsampling options (mine supports noninteger sampling
                            > ratios, something which not even libjpeg itself can do!)
                            >
                            > I started out trying to write my own JPEG parser entirely in OCaml,
                            > and got about 95% done with the reading before I got stuck. That was
                            > mainly just to learn how JPEG compresses, so I didn't mind scrapping
                            > all of mine and using libjpeg to make it actually work.

                            Will you put the sources as a library and give free access to it?

                            Would be nice.

                            Ciao,
                            Oliver
                          • Oliver Bandel
                            ... Why not have a types.ml where you define your types that are necessary to some or all modules and then use a open Types in the modules that need it, to
                            Message 13 of 15 , Sep 18, 2005
                            • 0 Attachment
                              On Sat, Sep 17, 2005 at 07:34:58PM -0000, ospeovedos wrote:
                              > --- In ocaml_beginners@yahoogroups.com, "Damien Guichard"
                              > <alphablock@w...> wrote:
                              > > That also happened to me.
                              > >
                              > > The solution i have adopted is:
                              > > * create 3 separate modules A,B,C
                              > > * create the big module D like this:
                              > >
                              > > module D = struct
                              > >
                              > > include A;;
                              > > include B;;
                              > > include C;;
                              > >
                              > > end;;
                              >
                              > I've been trying something like that, but I can't quite get it. I need
                              > something that can use the same type definitions. So, something like
                              > this:
                              > module D = struct
                              > type aType = Stuff | OtherStuff;;
                              > include A;;
                              > include B;;
                              > include C;;
                              > end;;
                              > And I need aType to be available to A, B, and C. However, in order for
                              > any one of the included modules to use the type, I need "include D" at
                              > the beginning of each of A,B,C. That introduces a circularity which
                              > OCaml does not appreciate... Any way to do this?

                              Why not have a types.ml where you define your types that are
                              necessary to some or all modules and then use a

                              open Types

                              in the modules that need it,
                              to have access to these types?

                              I had need such a construction as I used a certain type
                              in a main-module as well as in the lexer/parser written
                              with ocamllex/ocamlyacc.

                              As the parser-function gives back a list of a certain type,
                              and using a definition only inside the *.mll or *.mly
                              or the main.ml could not work, I used a separated
                              types.ml.

                              I didn't liked that solution, but it works.

                              Going this way seems to me to be more clean than using the
                              solution with include.
                              Why to include the whole code, if you only want to access
                              values of a certain type? Then it's enough to use a separated
                              types.ml and it's sufficient to use open then.

                              Ciao,
                              Oliver
                            • ospeovedos
                              ... If I do release it, it will be free and open-source. I don t know if/when I will release it, though. It s pretty much in the debugging stage right now,
                              Message 14 of 15 , Sep 18, 2005
                              • 0 Attachment
                                --- In ocaml_beginners@yahoogroups.com, Oliver Bandel <oliver@f...>
                                wrote:
                                > Will you put the sources as a library and give free access to it?
                                >
                                > Would be nice.
                                >
                                > Ciao,
                                > Oliver

                                If I do release it, it will be free and open-source. I don't know
                                if/when I will release it, though. It's pretty much in the "debugging"
                                stage right now, and if I ever think it's ready for release, I will.
                                (maybe in a week or two... but maybe not)

                                - Reed
                              • ospeovedos
                                As a bit of a follow-up: I just successfully split my JPEG module up into 9 distinct files. I figured that my miscellaneous functions are distinct enough to
                                Message 15 of 15 , Sep 18, 2005
                                • 0 Attachment
                                  As a bit of a follow-up:

                                  I just successfully split my JPEG module up into 9 distinct files. I
                                  figured that my "miscellaneous functions" are distinct enough to
                                  warrant more splitting. Now I have types, utils, encode, decode,
                                  upsampling, downsampling, colorspace conversions, etc. in their own
                                  files.

                                  I have one "master" file which has nothing more than:
                                  include Jpeg_types;;
                                  include Jpeg_utils;;
                                  include Jpeg_encode;;
                                  include Jpeg_decode;;
                                  ...

                                  Then at the top of all my sub-files I say:
                                  open Jpeg_types;;
                                  open Jpeg_utils;;

                                  and everything works out!

                                  This has the added benifit that I don't need to recompile the whole
                                  thing if I change one function, just the file that it's in. That makes
                                  life much easier (at the expense of a nasty-looking makefile).

                                  Thanks to everybody that responded!
                                  - Reed
                                Your message has been successfully submitted and would be delivered to recipients shortly.