caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Syntactic inclusion of a.ml in b.ml ?
@ 2005-04-08 17:41 Sébastien Hinderer
  2005-04-09 10:35 ` [Caml-list] " Richard Jones
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Sébastien Hinderer @ 2005-04-08 17:41 UTC (permalink / raw)
  To: caml-list

Dear all,

(How) is it possible to include syntactically a file a.ml in a file
b.ml ?

One method that seems to w)rk is to rename b.ml to b.ml.c,
and then have in b.ml.c a line saying
#include "a.ml"
And with this, gcc -E b.ml.c > b.ml
produces a file that ocamlc can apparently handle.

But is this considered a good solution, or is some better solution
available ?

Many thanks in advance,
Sébastien.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Syntactic inclusion of a.ml in b.ml ?
  2005-04-08 17:41 Syntactic inclusion of a.ml in b.ml ? Sébastien Hinderer
@ 2005-04-09 10:35 ` Richard Jones
  2005-04-09 13:20   ` Sébastien Hinderer
  2005-04-09 17:51   ` [Caml-list] " Robert Roessler
  2005-04-09 11:55 ` Olivier Andrieu
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 12+ messages in thread
From: Richard Jones @ 2005-04-09 10:35 UTC (permalink / raw)
  To: caml-list

On Fri, Apr 08, 2005 at 07:41:42PM +0200, Sébastien Hinderer wrote:
> (How) is it possible to include syntactically a file a.ml in a file
> b.ml ?
> 
> One method that seems to w)rk is to rename b.ml to b.ml.c,
> and then have in b.ml.c a line saying
> #include "a.ml"
> And with this, gcc -E b.ml.c > b.ml
> produces a file that ocamlc can apparently handle.

I'm not 100% clear on what you want to do.

A common requirement is to split a large module into a number of
smaller files, which is then compiled back into a single large module.
This can be done using a preprocessor (such as cpp) - see the -pp
option to the compiler.  Often it's better just to use a single large
file and a capable editor, with "folding"[1] capabilities.

Another one is to include the symbols from one module in another.
This can be done using the 'include' directive in OCaml, eg:

-- a.ml ----
let foo = 1
------------

-- b.ml ----
include A
let bar = 2
------------

Now, if compiled in the correct order, module B will export symbols
'foo' and 'bar'.

'include' and 'open' are very similar.  The difference is that
'include' causes the symbols imported to be (re-)exported.  'open A'
on the other hand makes the symbols in A available inside B, but they
are not exported in B's interface.

Another option is to use the -pack argument when linking [not
supported on all platforms].  This causes modules to be nested inside
a "super-module".

For example,

  ocamlc -pack -o c.cmo a.cmo b.cmo

(IIRC) creates a module called C containing C.A and C.B modules.

Rich.

[1] http://www.moria.de/~michael/fe/folding.html

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Syntactic inclusion of a.ml in b.ml ?
  2005-04-08 17:41 Syntactic inclusion of a.ml in b.ml ? Sébastien Hinderer
  2005-04-09 10:35 ` [Caml-list] " Richard Jones
@ 2005-04-09 11:55 ` Olivier Andrieu
  2005-04-09 12:54 ` sejourne_kevin
  2005-04-09 21:16 ` Martin Jambon
  3 siblings, 0 replies; 12+ messages in thread
From: Olivier Andrieu @ 2005-04-09 11:55 UTC (permalink / raw)
  To: Sebastien.Hinderer; +Cc: caml-list

[-- Attachment #1: Type: Text/Plain, Size: 745 bytes --]

 > Sébastien Hinderer [Fri, 8 Apr 2005]:
 > Dear all,
 > 
 > (How) is it possible to include syntactically a file a.ml in a file
 > b.ml ?
 > 
 > One method that seems to w)rk is to rename b.ml to b.ml.c,
 > and then have in b.ml.c a line saying
 > #include "a.ml"
 > And with this, gcc -E b.ml.c > b.ml
 > produces a file that ocamlc can apparently handle.
 > 
 > But is this considered a good solution, 

not really : IIRC ocaml doesn't follow the same syntactic conventions
as C and the C preprocessor could report errors on valid caml code

 > or is some better solution available ?

you could use camlp4 : the attached syntax extension does this. (Mind
that it works only for parsing, the printer apparently gets confused).

-- 
   Olivier

[-- Attachment #2: pa_inc.ml --]
[-- Type: Text/Plain, Size: 403 bytes --]

#load "pa_extend.cmo";;

let include_file file =
  let ic = open_in file in
  let (implem, _) = !Pcaml.parse_implem (Stream.of_channel ic) in
  close_in ic ;
  (implem, true)

EXTEND
  GLOBAL: Pcaml.implem ;

  Pcaml.implem:
    [ [ "INCLUDE" ; file = STRING ; ";;" -> include_file file
    ] ];
END

(* Local Variables: *)
(* compile-command: "ocamlc -pp camlp4o -I +camlp4 -c pa_inc.ml" *)
(* End: *)

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Syntactic inclusion of a.ml in b.ml ?
  2005-04-08 17:41 Syntactic inclusion of a.ml in b.ml ? Sébastien Hinderer
  2005-04-09 10:35 ` [Caml-list] " Richard Jones
  2005-04-09 11:55 ` Olivier Andrieu
@ 2005-04-09 12:54 ` sejourne_kevin
  2005-04-09 21:16 ` Martin Jambon
  3 siblings, 0 replies; 12+ messages in thread
From: sejourne_kevin @ 2005-04-09 12:54 UTC (permalink / raw)
  To: Sébastien Hinderer; +Cc: caml-list

Sébastien Hinderer a écrit :
> Dear all,
> 
> (How) is it possible to include syntactically a file a.ml in a file
> b.ml ?
> 
> One method that seems to w)rk is to rename b.ml to b.ml.c,
> and then have in b.ml.c a line saying
> #include "a.ml"
> And with this, gcc -E b.ml.c > b.ml
> produces a file that ocamlc can apparently handle.
> 
> But is this considered a good solution, or is some better solution
> available ?
> 
> Many thanks in advance,
> Sébastien.

If you want to use a pre-processor, you can do things like that:
(* ocamlc -pp /lib/cpp test.ml *)
#define P(x) (fst x) (snd x) (fst x)
#define S(x) (snd x)
let x = ("world","hello");;
Printf.printf "%s %s %s %s\n" S(x) P(x);;


or use camlp4
for the use of include :
http://caml.inria.fr/pub/docs/manual-ocaml/manual019.html#@manual.kwd167




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Syntactic inclusion of a.ml in b.ml ?
  2005-04-09 10:35 ` [Caml-list] " Richard Jones
@ 2005-04-09 13:20   ` Sébastien Hinderer
  2005-04-09 14:45     ` [Caml-list] " Radu Grigore
  2005-04-09 17:51   ` [Caml-list] " Robert Roessler
  1 sibling, 1 reply; 12+ messages in thread
From: Sébastien Hinderer @ 2005-04-09 13:20 UTC (permalink / raw)
  To: caml-list, caml-list

Dear Richard, dear all,

> I'm not 100% clear on what you want to do.

Sorry, I'll try to be more precise.

If I would like to incluee a.ml in b.ml syntactically, it's not to work
with small files, but rather, becuse the code in a.ml is
automatically generated by a script, by parsing a C source file.

The C source code contains something like :

typedef enum {
  A,
  B,
  ...,
  Z
} e;

The script produces the a.ml file, which contains:

| A
| B
| ...
| Z

And then I would like to be able to do something like this in b.ml :

type t =
#include "a.ml"

Thans a lot to those who already replied.

Cheers,
Sébastien.



> 
> A common requirement is to split a large module into a number of
> smaller files, which is then compiled back into a single large module.
> This can be done using a preprocessor (such as cpp) - see the -pp
> option to the compiler.  Often it's better just to use a single large
> file and a capable editor, with "folding"[1] capabilities.
> 
> Another one is to include the symbols from one module in another.
> This can be done using the 'include' directive in OCaml, eg:
> 
> -- a.ml ----
> let foo = 1
> ------------
> 
> -- b.ml ----
> include A
> let bar = 2
> ------------
> 
> Now, if compiled in the correct order, module B will export symbols
> 'foo' and 'bar'.
> 
> 'include' and 'open' are very similar.  The difference is that
> 'include' causes the symbols imported to be (re-)exported.  'open A'
> on the other hand makes the symbols in A available inside B, but they
> are not exported in B's interface.
> 
> Another option is to use the -pack argument when linking [not
> supported on all platforms].  This causes modules to be nested inside
> a "super-module".
> 
> For example,
> 
>   ocamlc -pack -o c.cmo a.cmo b.cmo
> 
> (IIRC) creates a module called C containing C.A and C.B modules.
> 
> Rich.
> 
> [1] http://www.moria.de/~michael/fe/folding.html
> 


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Re: Syntactic inclusion of a.ml in b.ml ?
  2005-04-09 13:20   ` Sébastien Hinderer
@ 2005-04-09 14:45     ` Radu Grigore
  2005-04-09 15:49       ` Sébastien Hinderer
  0 siblings, 1 reply; 12+ messages in thread
From: Radu Grigore @ 2005-04-09 14:45 UTC (permalink / raw)
  To: caml-list

On Apr 9, 2005 4:20 PM, Sébastien Hinderer
<Sebastien.Hinderer@ens-lyon.org> wrote:
> The C source code contains something like : [...]
> The script produces the a.ml file, which contains: [...]
> And then I would like to be able to do something like this in b.ml :
> 
> type t =
> #include "a.ml"

Why not modify the script to produce valid a valid ocaml module and
then just open that module?

-- 
regards,
 radu
http://rgrig.blogspot.com/


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Re: Syntactic inclusion of a.ml in b.ml ?
  2005-04-09 14:45     ` [Caml-list] " Radu Grigore
@ 2005-04-09 15:49       ` Sébastien Hinderer
  2005-04-09 21:15         ` [Caml-list] " Radu Grigore
  0 siblings, 1 reply; 12+ messages in thread
From: Sébastien Hinderer @ 2005-04-09 15:49 UTC (permalink / raw)
  To: caml-list, caml-list

> Why not modify the script to produce valid a valid ocaml module and
> then just open that module?

(1) Because it would make the script heavier, IMHO.

(2) Because there are actually two different things that should be
automatically generated :

  (a) The type declaration
  (b) A function to convert it to a string.

And I would like to embed as few Caml code as possible in the script.

Sébastien.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Syntactic inclusion of a.ml in b.ml ?
  2005-04-09 10:35 ` [Caml-list] " Richard Jones
  2005-04-09 13:20   ` Sébastien Hinderer
@ 2005-04-09 17:51   ` Robert Roessler
  2005-04-09 21:27     ` William D.Neumann
  2005-04-10 16:53     ` Richard Jones
  1 sibling, 2 replies; 12+ messages in thread
From: Robert Roessler @ 2005-04-09 17:51 UTC (permalink / raw)
  To: Richard Jones; +Cc: caml-list

Richard Jones wrote:

> ...  Often it's better just to use a single large
> file and a capable editor, with "folding"[1] capabilities.
> ...
> [1] http://www.moria.de/~michael/fe/folding.html

Interesting that you mention this... I am more than ready to implement 
folding in my syntax colorer "LexCaml" for Scintilla/SciTE[1] - all of 
the supporting infrastructure is in place.

But it is not entirely clear *what* should be folded to be useful in 
working with OCaml source.  A language with an obvious structure like 
Python suggests a fairly obvious folding structure.

When I look at OCaml source code, however, I see a *lot* of really 
different free-form coding styles.  The only language element that 
jumps out at me for folding is a simplistic handling of "let" 
bindings... which might not be useful for anything more than to say 
"Now with folding!" :)

Anyone in the OCaml community (or out of it) that has an opinion on 
the Tao of OCaml source code structuring and manipulation is 
encouraged to offer it, and I will attempt to discern a "consensus" 
viewpoint and use that to produce a folding version of LexCaml.

Note that actually showing what you mean, as in "here are folded and 
unfolded versions of this OCaml code" could be helpful in 
communicating any thoughts you may have on the subject.

[1] Current and development versions of LexCaml are available at
http://www.rftp.com/Downloads.shtml - The current release version of 
LexCaml is now part of Scintilla/SciTE versions 1.63 and later, at
http://www.scintilla.org/

Robert Roessler
roessler@rftp.com
http://www.rftp.com


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Re: Re: Syntactic inclusion of a.ml in b.ml ?
  2005-04-09 15:49       ` Sébastien Hinderer
@ 2005-04-09 21:15         ` Radu Grigore
  0 siblings, 0 replies; 12+ messages in thread
From: Radu Grigore @ 2005-04-09 21:15 UTC (permalink / raw)
  To: caml-list

On Apr 9, 2005 6:49 PM, Sébastien Hinderer
<Sebastien.Hinderer@ens-lyon.org> wrote:
> > Why not modify the script to produce valid a valid ocaml module and
> > then just open that module?

> And I would like to embed as few Caml code as possible in the script.

This is the only reason I understand. However, I think this is not a
serious concern since you can use the "cat" command in the script and
put the "template" caml code in a file. There is no need to "embed"
anything.

-- 
regards,
 radu
http://rgrig.blogspot.com/


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Syntactic inclusion of a.ml in b.ml ?
  2005-04-08 17:41 Syntactic inclusion of a.ml in b.ml ? Sébastien Hinderer
                   ` (2 preceding siblings ...)
  2005-04-09 12:54 ` sejourne_kevin
@ 2005-04-09 21:16 ` Martin Jambon
  3 siblings, 0 replies; 12+ messages in thread
From: Martin Jambon @ 2005-04-09 21:16 UTC (permalink / raw)
  To: Sébastien Hinderer; +Cc: caml-list, Sébastien Hinderer

On Fri, 8 Apr 2005, Sébastien Hinderer wrote:

> Dear all,
>
> (How) is it possible to include syntactically a file a.ml in a file
> b.ml ?
>
> One method that seems to w)rk is to rename b.ml to b.ml.c,
> and then have in b.ml.c a line saying
> #include "a.ml"
> And with this, gcc -E b.ml.c > b.ml
> produces a file that ocamlc can apparently handle.
>
> But is this considered a good solution, or is some better solution
> available ?

You can maybe use camlmix:
  http://martin.jambon.free.fr/toolbox.html#ppocaml
  http://martin.jambon.free.fr/camlmix

Martin

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




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Syntactic inclusion of a.ml in b.ml ?
  2005-04-09 17:51   ` [Caml-list] " Robert Roessler
@ 2005-04-09 21:27     ` William D.Neumann
  2005-04-10 16:53     ` Richard Jones
  1 sibling, 0 replies; 12+ messages in thread
From: William D.Neumann @ 2005-04-09 21:27 UTC (permalink / raw)
  To: Robert Roessler; +Cc: caml-list

On Apr 9, 2005, at 11:51 AM, Robert Roessler wrote:

> When I look at OCaml source code, however, I see a *lot* of really 
> different free-form coding styles.  The only language element that 
> jumps out at me for folding is a simplistic handling of "let" 
> bindings... which might not be useful for anything more than to say 
> "Now with folding!" :)

The folding portion of the OCaml TextMate[1] module I put together also 
allows folding for modules and their signatures (between sig/struct and 
end) and classes (between object and end), and possibly some other 
places that I've forgotten (perhaps begin/end pairs?).

[1] <http://www.macromates.com/>

William D. Neumann

"You've got Rita Marlowe in the palm of your hand."
"Palm of my hand?  You haven't seen Rita Marlowe..."

		-- Will Success Spoil Rock Hunter?


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [Caml-list] Syntactic inclusion of a.ml in b.ml ?
  2005-04-09 17:51   ` [Caml-list] " Robert Roessler
  2005-04-09 21:27     ` William D.Neumann
@ 2005-04-10 16:53     ` Richard Jones
  1 sibling, 0 replies; 12+ messages in thread
From: Richard Jones @ 2005-04-10 16:53 UTC (permalink / raw)
  To: Robert Roessler; +Cc: caml-list

On Sat, Apr 09, 2005 at 10:51:47AM -0700, Robert Roessler wrote:
> When I look at OCaml source code, however, I see a *lot* of really 
> different free-form coding styles.  The only language element that 
> jumps out at me for folding is a simplistic handling of "let" 
> bindings... which might not be useful for anything more than to say 
> "Now with folding!" :)

I tend to write *giant* functions, with multiple levels of nesting, so
that (nested) functions appear as close as possible to their point of
application.  Not sure whether folding helps here ...

Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2005-04-10 16:54 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-04-08 17:41 Syntactic inclusion of a.ml in b.ml ? Sébastien Hinderer
2005-04-09 10:35 ` [Caml-list] " Richard Jones
2005-04-09 13:20   ` Sébastien Hinderer
2005-04-09 14:45     ` [Caml-list] " Radu Grigore
2005-04-09 15:49       ` Sébastien Hinderer
2005-04-09 21:15         ` [Caml-list] " Radu Grigore
2005-04-09 17:51   ` [Caml-list] " Robert Roessler
2005-04-09 21:27     ` William D.Neumann
2005-04-10 16:53     ` Richard Jones
2005-04-09 11:55 ` Olivier Andrieu
2005-04-09 12:54 ` sejourne_kevin
2005-04-09 21:16 ` Martin Jambon

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