Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* [Caml-list] utop execution of multiple OCaml files
@ 2025-08-15  0:50 Kenichi Asai
  2025-08-15 11:37 ` Kate
  0 siblings, 1 reply; 3+ messages in thread
From: Kenichi Asai @ 2025-08-15  0:50 UTC (permalink / raw)
  To: caml-list

Suppose I have a.ml that uses definitions in b.ml (and possibly more).
To compile these files, all I need to do is to prepare a dune file
that mentions only the main file: (executable (name a)), and type
"dune build".

But before I compile the whole program, I often want to play with
functions in a.ml using OCaml toplevel, like utop.  If b.ml were
registered as a library, I could do it by "dune utop ." and then "#use
a.ml".  But to do so, I need to create a directory for the library and
create another dune file for it.

Is there a way to load all the modules into utop, just as simple as
typing "dune build" for the compilation case?

Perhaps, there is a tool to analyze the dependency and produce a list
of commands like the following?

#mod_use "b.ml";;
#mod_use "a.ml";;

-- 
Kenichi Asai

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

* Re: [Caml-list] utop execution of multiple OCaml files
  2025-08-15  0:50 [Caml-list] utop execution of multiple OCaml files Kenichi Asai
@ 2025-08-15 11:37 ` Kate
  2025-08-17 11:20   ` Kenichi Asai
  0 siblings, 1 reply; 3+ messages in thread
From: Kate @ 2025-08-15 11:37 UTC (permalink / raw)
  To: Kenichi Asai, caml-list

I'm not exactly sure if there is an easy way to do this for you but

#use_output "your command";;

in the default ocaml toplevel (i'm not sure that exists in utop) should 
be useful. The command can be any shell command.
For example "dune top" will output the right #directory and #load, but 
this only works for libraries the same way "dune utop" does.

So maybe something like:

#use_output "dune top";;
#use_output "for f in src/*.ml; do echo \"#mod_use \\\"$f\\\";\";done";;

The first "dune top" will load any dependencies you may have and the 
shell command will load all of the ml files from src.

I hope this helps,
Kate

On 8/15/25 01:50, Kenichi Asai wrote:
> Suppose I have a.ml that uses definitions in b.ml (and possibly more).
> To compile these files, all I need to do is to prepare a dune file
> that mentions only the main file: (executable (name a)), and type
> "dune build".
> 
> But before I compile the whole program, I often want to play with
> functions in a.ml using OCaml toplevel, like utop.  If b.ml were
> registered as a library, I could do it by "dune utop ." and then "#use
> a.ml".  But to do so, I need to create a directory for the library and
> create another dune file for it.
> 
> Is there a way to load all the modules into utop, just as simple as
> typing "dune build" for the compilation case?
> 
> Perhaps, there is a tool to analyze the dependency and produce a list
> of commands like the following?
> 
> #mod_use "b.ml";;
> #mod_use "a.ml";;
> 


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

* Re: [Caml-list] utop execution of multiple OCaml files
  2025-08-15 11:37 ` Kate
@ 2025-08-17 11:20   ` Kenichi Asai
  0 siblings, 0 replies; 3+ messages in thread
From: Kenichi Asai @ 2025-08-17 11:20 UTC (permalink / raw)
  To: Kate; +Cc: caml-list

Thank you.  #use_output appears to be quite powerful (a bit dangerous
perhaps?) but it is good to know it exists.  I found that if I can
enumerate all the required files, "ocamldep -sort" can topologically
sort them.  I could somehow enumerate them (with the help of
generative AI) and write a script to #mod_use them, but not quite.

I can't help but feel that dune has all the required information
internally and what I am doing is quite wasteful.  "dune exec" would
compile all the required files and produce an executable.  Instead of
producing an executable, I suppose it would not be very difficult to
create a toplevel, just like ocamlmktop does.

Why don't we have such a toplevel?  In a educational setting, I think
it is quite valuable to have a toplevel with custom libraries for
particular courses...

-- 
Kenichi Asai


On Fri, Aug 15, 2025 at 12:37:52PM +0100,
 Kate wrote:

> I'm not exactly sure if there is an easy way to do this for you but
> 
> #use_output "your command";;
> 
> in the default ocaml toplevel (i'm not sure that exists in utop) should be
> useful. The command can be any shell command.
> For example "dune top" will output the right #directory and #load, but this
> only works for libraries the same way "dune utop" does.
> 
> So maybe something like:
> 
> #use_output "dune top";;
> #use_output "for f in src/*.ml; do echo \"#mod_use \\\"$f\\\";\";done";;
> 
> The first "dune top" will load any dependencies you may have and the shell
> command will load all of the ml files from src.
> 
> I hope this helps,
> Kate
> 
> On 8/15/25 01:50, Kenichi Asai wrote:
> > Suppose I have a.ml that uses definitions in b.ml (and possibly more).
> > To compile these files, all I need to do is to prepare a dune file
> > that mentions only the main file: (executable (name a)), and type
> > "dune build".
> > 
> > But before I compile the whole program, I often want to play with
> > functions in a.ml using OCaml toplevel, like utop.  If b.ml were
> > registered as a library, I could do it by "dune utop ." and then "#use
> > a.ml".  But to do so, I need to create a directory for the library and
> > create another dune file for it.
> > 
> > Is there a way to load all the modules into utop, just as simple as
> > typing "dune build" for the compilation case?
> > 
> > Perhaps, there is a tool to analyze the dependency and produce a list
> > of commands like the following?
> > 
> > #mod_use "b.ml";;
> > #mod_use "a.ml";;

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

end of thread, other threads:[~2025-08-17 11:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-08-15  0:50 [Caml-list] utop execution of multiple OCaml files Kenichi Asai
2025-08-15 11:37 ` Kate
2025-08-17 11:20   ` Kenichi Asai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox