caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Accessing values calculated by Toploop.execute_phrase
@ 2016-02-11 14:11 Philippe Veber
  2016-02-11 14:28 ` Thomas Refis
  0 siblings, 1 reply; 4+ messages in thread
From: Philippe Veber @ 2016-02-11 14:11 UTC (permalink / raw)
  To: caml users

[-- Attachment #1: Type: text/plain, Size: 950 bytes --]

Dear list,

  I'm having quite some fun interacting with the toplevel using `Toploop`
and co., and I am now looking for a way to get the values created during a
call to `Toploop.execute_phrase`, which sadly only returns a boolean. I
would like to get these values as `Obj.t` values, so that I could perform
additional computations for certain types.

I'm almost there but not yet:
  - I can find which values were created and what's their type by
collecting `Outcometree.out_path` values when they are printed, but this
doesn't give access to the actual value (represented as an `Obj.t`).
  - I could use `Toploop.getvalue`, but then I could not get results of
evaluations that were not bound to a variable.

Of course I know what I'm trying to do is highly unsafe, so I should
probably think of some other design. Still, out of curiosity, I'd be
interested if somebody knew a solution to this (I mean, besides "Don't do
it" :).

Cheers,
  Philippe.

[-- Attachment #2: Type: text/html, Size: 1159 bytes --]

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

* Re: [Caml-list] Accessing values calculated by Toploop.execute_phrase
  2016-02-11 14:11 [Caml-list] Accessing values calculated by Toploop.execute_phrase Philippe Veber
@ 2016-02-11 14:28 ` Thomas Refis
  2016-02-11 15:37   ` Philippe Veber
  0 siblings, 1 reply; 4+ messages in thread
From: Thomas Refis @ 2016-02-11 14:28 UTC (permalink / raw)
  To: Philippe Veber; +Cc: caml users

2016-02-11 14:11 GMT+00:00 Philippe Veber <philippe.veber@gmail.com>:
>   I'm having quite some fun interacting with the toplevel using `Toploop`
> and co., and I am now looking for a way to get the values created during a
> call to `Toploop.execute_phrase`, which sadly only returns a boolean. I
> would like to get these values as `Obj.t` values, so that I could perform
> additional computations for certain types.
>
> I'm almost there but not yet:
>   - I can find which values were created and what's their type by collecting
> `Outcometree.out_path` values when they are printed, but this doesn't give
> access to the actual value (represented as an `Obj.t`).
>   - I could use `Toploop.getvalue`, but then I could not get results of
> evaluations that were not bound to a variable.

Well, you can hack around the final problem by rewriting the
expression you feed to [execute_phrase] so the result is always bound
to a variable.
[Toploop.execute_phrase] already has a bit of code to check whether
the result is a value not bound to any name (
https://github.com/ocaml/ocaml/blob/trunk/toplevel/toploop.ml#L263 ),
you could do something similar on your side. This works on the
typedtree but you can do the same on the parsetree. Or you could type
your expression beforehand, reuse that exact piece of code, but only
when the value is of the type you want (since you apparently want to
do extra computations "for certain types").

Thomas.

PS: https://www.youtube.com/watch?v=p8oi6M4z_e0

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

* Re: [Caml-list] Accessing values calculated by Toploop.execute_phrase
  2016-02-11 14:28 ` Thomas Refis
@ 2016-02-11 15:37   ` Philippe Veber
  2016-02-11 15:42     ` Thomas Refis
  0 siblings, 1 reply; 4+ messages in thread
From: Philippe Veber @ 2016-02-11 15:37 UTC (permalink / raw)
  To: Thomas Refis; +Cc: caml users

[-- Attachment #1: Type: text/plain, Size: 213 bytes --]

> PS: https://www.youtube.com/watch?v=p8oi6M4z_e0
>

Fair enough, I guess I had it coming :)

Thanks for the hint anyway! I guess I'll rather deal with `out_phrase`
values to spare me a few disgraceful segfaults.

[-- Attachment #2: Type: text/html, Size: 611 bytes --]

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

* Re: [Caml-list] Accessing values calculated by Toploop.execute_phrase
  2016-02-11 15:37   ` Philippe Veber
@ 2016-02-11 15:42     ` Thomas Refis
  0 siblings, 0 replies; 4+ messages in thread
From: Thomas Refis @ 2016-02-11 15:42 UTC (permalink / raw)
  To: Philippe Veber; +Cc: caml users

2016-02-11 15:37 GMT+00:00 Philippe Veber <philippe.veber@gmail.com>:
> Fair enough, I guess I had it coming :)
>
> Thanks for the hint anyway! I guess I'll rather deal with `out_phrase`
> values to spare me a few disgraceful segfaults.

To be fair, utop does what I described (more or less) :
https://github.com/diml/utop/blob/master/src/lib/uTop_main.cppo.ml#L645

Hopefully someday Toploop will be reorganized a bit to make easier for
people to write their own toplevel. Someday. Hopefully.

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

end of thread, other threads:[~2016-02-11 15:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-11 14:11 [Caml-list] Accessing values calculated by Toploop.execute_phrase Philippe Veber
2016-02-11 14:28 ` Thomas Refis
2016-02-11 15:37   ` Philippe Veber
2016-02-11 15:42     ` Thomas Refis

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