Previous week Up Next week


Here is the latest Caml Weekly News, for the week of February 22 to March 01, 2011.

  1. Calling the toplevel from the toplevel
  2. [OCaml Meeting 2011] inscription is opened
  3. Other Caml News

Calling the toplevel from the toplevel


rixed asked and Guillaume Yziquel suggested:
> I'm trying to build an application that's run from the toplevel, using
> the toplevel to customize the application from itself (for instance,
> setting some global parameters using the toplevel to configure the
> application behavior while it's running).
> But I cannot allow the toplevel to read directly from stdin nor to
> write anything into stdout (since my application uses ncurses).
> I've looked for various ways to do this, and the simpler I found to
> prevent the toplevel to use stdout is to call myself
> Toploop.execute_phrase with a custom formatter (so that I can display
> the output where and how I want).
> Up to now all seams to work except for minor annoyances :
> - I cannot start the application directly by linking the custom toplevel
>   with something like "let _ = start_application ()" but I have to call
>   "start_application();;" from the toplevel manually (or from the
>   .ocamlinit file), otherwise the application bindings are not
>   available.

For this, you can change the Toploop.read_interactive_input reference to
what you want to control what you input to the toplevel. You therefore
do not need to consider workarounds such as calling evaluation functions
from code evaluated from the toplevel itself.

But it's a bit surprising that application bindings are not available
when calling start_application from some code that is being loaded. If
you insist on loading a .cmo instead of a .cma, the .cmo code is
executed when loaded, while code in the .cma often seems to be executed
only when required. That may solve your issue. Dunno.

You've also got a Toplevel.toplevel_startup_hook ref that may be useful.

> - I cannot let the user uses the toplevel directives "use" and "load"
>   because both writes into stdout whatever the formatter passed to
>   Toploop.execute_phrase (for "use" this is easily solvable by shadowing
>   the toplevel implementation by another one that call Toploop.use_file
>   with my own formatter, but for "load" I would have to copy a lot of
>   code from
> I'd like to know if it is safe to call the Toploop evaluation functions
> from code evaluated from the toplevel itself ? Or if someone can suggest
> a better way to prevent the toplevel from using stdout ?

Well, the Toploop module provides some interesting values to control

val parse_toplevel_phrase : (Lexing.lexbuf -> Parsetree.toplevel_phrase)
val parse_use_file : (Lexing.lexbuf -> Parsetree.toplevel_phrase list)
val print_location : formatter -> Location.t -> unit
val print_error : formatter -> Location.t -> unit
val print_warning : Location.t -> formatter -> Warnings.t -> unit
val input_name : string ref

val print_out_value :
  (formatter -> Outcometree.out_value -> unit) ref
val print_out_type :
  (formatter -> Outcometree.out_type -> unit) ref
val print_out_class_type :
  (formatter -> Outcometree.out_class_type -> unit) ref
val print_out_module_type :
  (formatter -> Outcometree.out_module_type -> unit) ref
val print_out_sig_item :
  (formatter -> Outcometree.out_sig_item -> unit) ref
val print_out_signature :
  (formatter -> Outcometree.out_sig_item list -> unit) ref
val print_out_phrase :
  (formatter -> Outcometree.out_phrase -> unit) ref

I'd also look in the toplevel code of lwt, specifically in the source
code of package. Contains quite a lot of interesting toplevel
tricks that would likely be of use to you. I'm not so sure, but it seems
to have some amount of control over toplevel output.
Jérémie Dimino also replied:
> - I cannot let the user uses the toplevel directives "use" and "load"
>   because both writes into stdout whatever the formatter passed to
>   Toploop.execute_phrase (for "use" this is easily solvable by shadowing
>   the toplevel implementation by another one that call Toploop.use_file
>   with my own formatter, but for "load" I would have to copy a lot of
>   code from

Why not use Topdirs.dir_load with a custom formatter ?

If you are interested, i started some time ago an emacs-like editor
which can run in both curses and gtk mode and which integrates a
toplevel. I add to face the same problems. I solved them by using
Toploop.execute_phrase directly and redefining all directives.

The code is available here:;a=summary

The file that may interest you is src/core/

Also another problem i had is that Toploop.execute_phrase does not
prints errors on the given formatter but raises an exception instead,
and the printer used in Toploop.loop (Errors.report_error) is in the
module Errors for which the cmi is not available. I used a hack which
consists on parsing the output of Toploop.use_silently (file, function eval_command).

[OCaml Meeting 2011] inscription is opened


Sylvain Le Gall announced:
This mail is a copy of a post you can find here:


For the fourth time, I am proud to invite all OCaml enthusiasts to join
us at OCaml Meeting 2011 in Paris.

This year event takes place in Paris on Friday 15th April 2011.
Subscription is opened now and will be closed on Friday 8th April 2011.

As last year, participants are invited to give a talk on what they are
doing with OCaml, submit a description of your talk on the wiki or
contact me.

The meeting is sponsored by INRIA CAML Consortium and organized
by OCamlCore. Participation for lunch is covered by the Consortium, you
just need to subscribe. The facility can only host 70 people, so we will
have to filter the list of participants if there are more people. We
will give priority to people giving a talk and coming from abroad.

For people who need further information, you can contact me (see the
wiki for contact details).

Further information:

Hope to see a lot of you
Sylvain Le Gall on behalf of the OCaml Meeting organization team

p.s. the registration website is made with Ocsigen and connected to the
forge, so you need to be logged in the OCaml Forge to be able to

Other Caml News

From the ocamlcore planet blog:
Thanks to Alp Mestan, we now include in the Caml Weekly News the links to the
recent posts from the ocamlcore planet blog at

OCaml Meeting 2011, inscription is opened:


OCaml Debian News: API:

Video tutorials for the Coq proof assistant:

Old cwn

If you happen to miss a CWN, you can send me a message and I'll mail it to you, or go take a look at the archive or the RSS feed of the archives.

If you also wish to receive it every week by mail, you may subscribe online.

Alan Schmitt