Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Stricter version of #use ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-03-24 (22:23)
From: Zheng Li <zheng_li@u...>
Subject: Re: Stricter version of #use ?

On 3/24/2009 7:00 PM, Harrison, John R wrote:
> Then I want the following to stop immediately on the failure inside
> "" and hence not evaluate the "y = 2" line in "" as it
> currently does:
>            Objective Caml version 3.10.0
>    # #use "";;
>    val x : int = 1
>    val u : int = 3
>    Exception: Failure "X"
>    val y : int = 2

I agree this doesn't look like an intuitive semantics.

> On the other hand, I'd want it to keep the results of the x = 1 and
> u = 3 lines, not roll back everything. Is this easy to accomplish?

I just want to say rolling back everything is another 
consistent/intuitive semantics.

> (By the way, I'm ultimately interested in using this together with
> camlp5, and possibly with the new camlp4, if that makes a difference.)

You may try the following code snippet. It's not a total solution but an 
ad-hoc workaround. It only deals with recursive "#use" like in your 
example (e.g., "#load" operation inside a "#use" script will still 
behave the same as before), though it's not difficult to adapt the other 
primitives similarly ("#load" is probably the only one wanted, let me 
know if you need any help on that).

The only advantage of this method is that you don't have to modify the 
compiler code. You can just copy&paste the code into your toplevel 
sessions and go, or add it into your .ocamlinit file if you need it all 
the time.

Hashtbl.replace Toploop.directive_table "use"
   (Toploop.Directive_string begin
      let is_top = ref true in
      fun name ->
        let top_env = !is_top in
        is_top := false;
        let succ = Toploop.use_file Format.std_formatter name in
        is_top := top_env;
        if not !is_top && not succ then raise Exit