utop
utop copied to clipboard
Allow customised utops to access values from earlier in the program
Hi, apologies if this is already possible, but I searched high and low on the internet about this, and couldn't find anything, nor does anything in the utop or ocaml documentation point me to how to do this.
You have an example on how to embed the utop toploop into a custom program, which works nicely for me - thanks! However, I'd like to have values from earlier in the program, be already defined when the toploop starts - so that e.g. the user can type #show_val final_state_of_my_program;;
etc and explore it a bit. I used to do this quite often in python with the code snippet import code; code.interact(local=locals())
- it Just Works, and is a very nice and easy-to-use "poor man's debugger" that requires no extra learning on how to use a debugger.
Would it be simple to support this sort of thing in utop? I don't mind having to pass in these values explicitly, e.g. using syntax like UTop_main.main_with_idents [("name1", val1); ("name2", val2)]
or something like that.
That's something I've always wanted to have. The hard part is getting the right typing environment to start the toplevel, but now that we have cmt files it's feasible.
I gave it a quick try with an example (05a0816d95b6d73d4f4e94eb38df601f6a479fc7) and it seems to work
Wow that was quick, thanks! I tried to use it in infinity0/ocaml-bjsim@f7a1ec4778c639a9d4106030298b459ca4e467e9. It compiles, but when I run it I get
bjsim: internal error, uncaught exception:
Failure("Couldn't find location in cmt file")
Raised at file "pervasives.ml", line 30, characters 22-33
Called from unknown location
Called from unknown location
If I touch bjsim.mli
like you've done in your example, then re-build and re-run, I get this instead:
bjsim.ml:81:11 bjsim.ml:81:11
bjsim: internal error, uncaught exception:
Envaux.Error(_)
Called from unknown location
[..] many more times
Called from unknown location
Called from file "src/lib/uTop_main.cppo.ml", line 1426, characters 14-65
Called from unknown location
Called from unknown location
You can build it with e.g. ./autogen && make BUILDFLAGS=bjsim.byte && OCAMLRUNPARAM=b ./bjsim.byte --repl -- J86
. (I believe I don't need the _tags
file like in your example, I'm passing those flags via oasis already, and the previous commit with plain UTop_main.main
works.)
OK, I got it to work. I'm not sure why we need to add an empty mli...
I fixed utop to report a proper error and add the provided search_path
properly. Then to get your example to work I did the following: added _build/src
to ~search_path
and bound number
as follow:
let number = 12345 in
UTop_main.interact ... V ("number", number) ...
This bit is a bit tedious: basically the toplevel will see everything that's in scope when interact
is called, but it can only access values that have been passed. It might be possible to get V ("number", 12345)
working, I'll have to think about it
Nice, that works for me now. Thanks! For my purposes I'm OK with the explicit passing, but agree it could be nicer yes.
I think this is not exactly the same, but related: Is there a simple way to refer to values that were returned for previous prompts in a utop session? I'm thinking of _
or Out[i]
in Ipython or %
in Mathematica...
opening a new issue for that.