From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by c5ff346549e7 (Postfix) with ESMTPS id 215B45D5 for ; Fri, 5 Mar 2021 09:45:27 +0000 (UTC) X-IronPort-AV: E=Sophos;i="5.81,224,1610406000"; d="scan'208,217";a="496296437" Received: from prod-listesu18.inria.fr (HELO sympa.inria.fr) ([128.93.162.160]) by mail2-relais-roc.national.inria.fr with ESMTP; 05 Mar 2021 10:45:24 +0100 Received: by sympa.inria.fr (Postfix, from userid 20132) id 77398E1D3B; Fri, 5 Mar 2021 10:45:24 +0100 (CET) Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id B178BE0171 for ; Fri, 5 Mar 2021 10:45:16 +0100 (CET) Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=jdimino@janestreet.com; spf=Pass smtp.mailfrom=jdimino@janestreet.com; spf=None smtp.helo=postmaster@mxout2.mail.janestreet.com IronPort-PHdr: =?us-ascii?q?9a23=3AJryTvhQU+wubSGSYlalz9c/Fsdpsv+yvbD5Q0YIu?= =?us-ascii?q?jvd0So/mwa67ZhWEt8tkgFKBZ4jH8fUM07OQ7/mxHzVYvN3Y6StKWacPfidNsd?= =?us-ascii?q?8RkQ0kDZzNImzAB9muURYHGt9fXkRu5XCxPBsdMs//Y1rPvi/6tmZKSV3wOgVv?= =?us-ascii?q?O+v6BJPZgdip2OCu4Z3TZBhDiCagbb9oIxi6sAHcutMLjYd+Kqs9xQbFrmZGdu?= =?us-ascii?q?9L2W5mOFWfkgrm6Myt5pBj6SNQu/wg985ET6r3erkzQKJbAjo7LW07/dXnuhbf?= =?us-ascii?q?QwSB4HscSXgWnQFTAwfZ9hH6X4z+vTX8u+FgxSSVJ8z2TbQzWTS/86dmTQLjhS?= =?us-ascii?q?kbOzIl9mzcl9d9h7xHrh2/uxN/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NOTwc6KYDQw/Z/C9/oWwLBes2FSFS8?= =?us-ascii?q?TdWgHz4xVc4xw8UQbEljB9WulAzM3y2vA7MNlryLAoI78r7E0nX/JMtx0WvK2L?= =?us-ascii?q?UhglU8XMdDKXGqirR9+gXXH4LJll+Wl6eudaQSxi7N82KDwHKMvE5FUQ5wVKTF?= =?us-ascii?q?XXYYZkbNr9T2/EXCQKG1CbQ7KAtO19CNKrdQZtL1ilVGQe/vOM7CbGKph2ewGR?= =?us-ascii?q?GIy6uQY4XwfmUd2DzRCEwFkwAI4XaLLhM+Byeko2LGFjNiD1PvY0X28el/sny3?= =?us-ascii?q?VEE0zxvZJ3Fmgpm4/B8ehf3UYuga3/pQsTwoqh11BxC61NLfTdOL4Qh5KvZye9?= =?us-ascii?q?Q4tXVKz2/ctgE1Ep2lLqZlzgocfwVyvkXp/xd+DIhbjdInoW9sxw13f/HLmGhd?= =?us-ascii?q?fi+ViMijcobcLXP/qVX2M/aPiwPule2O86JK08wW7lDqvQWnDE0nqis10dBT3m?= =?us-ascii?q?CA/JjHEEwZVpejCx9rpSg/nKnTZ2wG362RzWdlaPfmsDbE1sk1HuYo1lCreNIN?= =?us-ascii?q?aPrZRj+3KNUTAo2VEMJvm1WtaUhUbuVC87MoZYWjfvqCnqyiOut7gCirgHgB64?= =?us-ascii?q?d4gBqB?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0CtAwAL/UFgl0/IaSZiDgiFY1c5MYRBg?= =?us-ascii?q?R6QOwODGZEViB0LAQMBDSgMBAEBgViCdQKBegIdBwEENBMCEAEBBQEBAQIBAwM?= =?us-ascii?q?EARMBAQEBAQEBAQkWBoYRDYI4KQGDYwEBAwESEQQZAQExBgEECwkCCzcCAiISA?= =?us-ascii?q?QUBHAYTFA6CTgGCZiEDDJIwjxKBBD2KPnZ/M4MEAQEGR4VMgTwJEoEnhnwBAYZ?= =?us-ascii?q?EJhyBSUKBR4JkPoJRCwKCDYJpgl+CShcbDBwiPgEvIYEALQecRIwpkU6DCIlAk?= =?us-ascii?q?mQihG6eeoZNl0iCB4QJjiiEThAjgR4qgXozGggbFTsxBoIyCUcZDY4oEAmDJSi?= =?us-ascii?q?DBoEahXhBQTICNgIGAQkBAQMJjkQBAQ?= X-IPAS-Result: =?us-ascii?q?A0CtAwAL/UFgl0/IaSZiDgiFY1c5MYRBgR6QOwODGZEViB0?= =?us-ascii?q?LAQMBDSgMBAEBgViCdQKBegIdBwEENBMCEAEBBQEBAQIBAwMEARMBAQEBAQEBA?= =?us-ascii?q?QkWBoYRDYI4KQGDYwEBAwESEQQZAQExBgEECwkCCzcCAiISAQUBHAYTFA6CTgG?= =?us-ascii?q?CZiEDDJIwjxKBBD2KPnZ/M4MEAQEGR4VMgTwJEoEnhnwBAYZEJhyBSUKBR4JkP?= =?us-ascii?q?oJRCwKCDYJpgl+CShcbDBwiPgEvIYEALQecRIwpkU6DCIlAkmQihG6eeoZNl0i?= =?us-ascii?q?CB4QJjiiEThAjgR4qgXozGggbFTsxBoIyCUcZDY4oEAmDJSiDBoEahXhBQTICN?= =?us-ascii?q?gIGAQkBAQMJjkQBAQ?= X-IronPort-AV: E=Sophos;i="5.81,224,1610406000"; d="scan'208,217";a="496296350" X-MGA-submission: =?us-ascii?q?MDGT8YuRQQaH7cS1BGe15LIa7skPa064FFinEM?= =?us-ascii?q?sZ8oQfoO2opDnvyMIYPabnyaSlyOvI+ju/G1EY22n5eM2ZMO9rscYH+r?= =?us-ascii?q?qJfS4nQUOzvOJnBwCHWdnsM1dmVGl1L2zopSLqHdjuoSWG+Kx1nUvXiM?= =?us-ascii?q?PyAm70CrKt9mFefFnzwMm6Lg=3D=3D?= Received: from mxout2.mail.janestreet.com ([38.105.200.79]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 05 Mar 2021 10:45:07 +0100 Received: from mail-ed1-f72.google.com ([209.85.208.72]) by mxgoog2.mail.janestreet.com with esmtps (TLS1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.94) id 1lI714-0007P4-Tz for caml-list@inria.fr; Fri, 05 Mar 2021 04:45:03 -0500 Received: by mail-ed1-f72.google.com with SMTP id l23so659257edt.23 for ; Fri, 05 Mar 2021 01:45:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=janestreet.com; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Pk9+ar41PSK5Mt4S/xGoqGLSeSqNq9TtuXUmOGGOhZA=; b=QsRWGGNgqOHEldUKhwJz3relTTu8lxSr6pnjKBMykNa7azdgW/1tqGTIjYhQ+OB+6u vz3LdfxxVTEKo5oYsyTTc37d/Hdk0sy1HOcqEUFzZWIzy5VrXK/xi6om4w1Km/OqRtvj 223M//BJKnhJJHatI05iJ/1j1iJTaVyaCklUc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Pk9+ar41PSK5Mt4S/xGoqGLSeSqNq9TtuXUmOGGOhZA=; b=qwGWaohOpjFYzabVLjktX2zbQQyQasTZMPohHF/uPppOwuM0shQgRGXpUtxRpnTJNy Z5dWgDXfu1/4y0JXD6TNrBiT8oHXBJrOj86oLKsLJnsZjOqCWstecMaEVeXOPuYEMmLp IVGvhN/mBRPuYkao2RA0aig2tfjtAZEub7754aMofyxOM0LcmIEr8W+CY2W6CXSecP4N TifHI+cWidOFnC393TGgZmebqqF/vCfQWcOOBuaKupazmlb/Rj2XCRKpxmsMLUk3FEP4 umruG+Arz+e6LbfRihcj3zCIqBcvvd1rS1NPBKbSVxhrP+vpxe719refJxw/mt7qtjNN K5qA== X-Gm-Message-State: AOAM530mAX8vxi6PjWkkmHnP/Z1u3pJs/ZdigfnRcFLwkwEcm9K4YToU XWBw0qzkXrN+6mFzb99gxOtPEDIFKZVOXOGyKVlcuUUqA3KPTnzTWTeI4/ULha+NnBxHpEkxnJP 9xnJJTSP32GWnV9doNRiBXd67bw== X-Received: by 2002:a17:906:7194:: with SMTP id h20mr1530447ejk.154.1614937501739; Fri, 05 Mar 2021 01:45:01 -0800 (PST) X-Google-Smtp-Source: ABdhPJxW7xdgSp0VEDNFZClAYd8RAKVcwDTR4CfBQqJNgrk6qpHueQyna4F8lEprc//YV/vTqgjA1r2q0bggPokQX0E= X-Received: by 2002:a17:906:7194:: with SMTP id h20mr1530432ejk.154.1614937501480; Fri, 05 Mar 2021 01:45:01 -0800 (PST) MIME-Version: 1.0 References: <20210305050509.GA97697@pllab.is.ocha.ac.jp> In-Reply-To: <20210305050509.GA97697@pllab.is.ocha.ac.jp> From: Jeremie Dimino Date: Fri, 5 Mar 2021 09:44:50 +0000 Message-ID: To: Kenichi Asai Cc: caml users Content-Type: multipart/alternative; boundary="0000000000001c4d7d05bcc6f0ba" Subject: Re: [Caml-list] a simple example of compiler-libs.toplevel? Reply-To: Jeremie Dimino X-Loop: caml-list@inria.fr X-Sequence: 18417 Errors-To: caml-list-owner@inria.fr Precedence: list Precedence: bulk Sender: caml-list-request@inria.fr X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: Archived-At: --0000000000001c4d7d05bcc6f0ba Content-Type: text/plain; charset="UTF-8" Hi Kenichi, It is possible to do this kind of thing. If you only want to change the character for the prompt, you can set the Toploop.read_interactive_input function. Then to create a custom toplevel, you need to call [Topmain.main] and link your program with the compiler-libs.toplevel library. Here is a complete example where I copied and adapted the default [read_interactive_input] function from the compiler libraries, and used dune to build a custom toplevel: $ cat mytop.ml let read_input prompt buffer len = let prompt = if prompt <> "" && prompt.[0] = '#' then String.mapi (fun i c -> if i = 0 then '%' else c) prompt else prompt in output_string stdout prompt; flush stdout; let i = ref 0 in try while true do if !i >= len then raise Exit; let c = input_char stdin in Bytes.set buffer !i c; Option.iter (fun b -> Buffer.add_char b c) !Location.input_phrase_buffer; incr i; if c = '\n' then raise Exit; done; (!i, false) with | End_of_file -> (!i, true) | Exit -> (!i, false) let () = Toploop.read_interactive_input := read_input; Topmain.main () $ cat dune (executable (name mytop) (libraries compiler-libs.toplevel) (link_flags :standard -linkall) (modes byte)) $ dune exec ./mytop.exe OCaml version 4.11.1 % 1 + 1;; - : int = 2 FTR, the compiler invocation to build myutop.exe is: $ ocamlc -linkall -I +compiler-libs ocamlcommon.cma ocamlbytecomp.cma ocamltoplevel.cma mytop.ml -o mytop.exe $ ./mytop.exe OCaml version 4.11.1 % 1 + 1;; - : int = 2 If you want to go further, there are few projects that add line edition support to the toplevel, colors etc, using similar methods: - utop: https://github.com/ocaml-community/utop - down: https://erratique.ch/software/down Finally, if you'd like to improve the toplevel API so that doing this kind of thing is easier in the future, a PR is always welcome :) Happy hacking, Jeremie On Fri, Mar 5, 2021 at 5:05 AM Kenichi Asai wrote: > Is there a simple (but complete) example that uses > compiler-libs.toplevel, like creating an OCaml toplevel that uses a > different character for a prompt? When I tried: > > - copy toplevel/topstart.ml to my_topstart.ml and change Topmain in it > to My_topmain > - copy toplevel/topmain.ml to my_topmain.ml and change Toploop in it > to My_toploop > - copy toplevel/toploop.ml to my_toploop.ml and change the prompt > character in it > - optionally, copy toplevel/topdirs.ml to my_topdirs.ml and change > Toploop, etc., to My_toploop (this does not affect the story below) > > I ran into the following error: > > The ocamltoplevel.cma library from compiler-libs cannot be loaded > inside the OCaml toplevel > > When I disable it (by removing the expression that raises this error > in my_toploop.ml), I can launch the new toplevel, but I cannot define > a new name: > > let a = 3 > > results in "Fatal error: a unbound at toplevel". Is it possible to do > this kind of creating a new OCaml toplevel using compiler-libs.toplevel? > Or is the compiler-libs.toplevel intended for something else? > > A bit of background. I am developing an OCaml type debugger: > > http://pllab.is.ocha.ac.jp/~asai/TypeDebugger/ > > and am currently trying to port it to a newer version of OCaml. For > the current type debugger, I inserted code for type debugging into the > main loop in toploop.ml. However, inserting code directly into the > OCaml source requires me to recompile whole (or at least the core of) > the OCaml. If I could do the same with compiler-libs, that would be > great. > > Any help is welcome. (I also welcome the negative information saying > that it is impossible to do this kind of thing. Then, I would fall > back to the old way of compiling whole the OCaml.) > > Thank you in advance. > > Sincerely, > > -- > Kenichi Asai > -- Jeremie --0000000000001c4d7d05bcc6f0ba Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi Kenichi,

It is possible to do t= his kind of thing. If you only want to change the character for the prompt,= you can set the Toploop.read_interactive_input function. Then to create a = custom toplevel, you need to call [Topmain.main] and link your program with= the compiler-libs.toplevel library. Here is a complete example where I cop= ied and adapted the default [read_interactive_input] function from the comp= iler libraries, and used dune to build a custom toplevel:

$ cat mytop.ml
let read_input prom= pt buffer len =3D
=C2=A0 let prompt =3D
=C2=A0 =C2=A0 if prompt <&= gt; "" && prompt.[0] =3D '#' then
=C2=A0 =C2= =A0 =C2=A0 String.mapi (fun i c -> if i =3D 0 then '%' else c) p= rompt
=C2=A0 =C2=A0 else
=C2=A0 =C2=A0 =C2=A0 prompt
=C2=A0 in
= =C2=A0 output_string stdout prompt; flush stdout;
=C2=A0 let i =3D ref 0= in
=C2=A0 try
=C2=A0 =C2=A0 while true do
=C2=A0 =C2=A0 =C2=A0 if= !i >=3D len then raise Exit;
=C2=A0 =C2=A0 =C2=A0 let c =3D input_ch= ar stdin in
=C2=A0 =C2=A0 =C2=A0 Bytes.set buffer !i c;
=C2=A0 =C2=A0= =C2=A0 Option.iter (fun b -> Buffer.add_char b c) !Location.input_phras= e_buffer;
=C2=A0 =C2=A0 =C2=A0 incr i;
=C2=A0 =C2=A0 =C2=A0 if c =3D = '\n' then raise Exit;
=C2=A0 =C2=A0 done;
=C2=A0 =C2=A0 (!i, = false)
=C2=A0 with
=C2=A0 | End_of_file ->
=C2=A0 =C2=A0 =C2=A0= (!i, true)
=C2=A0 | Exit ->
=C2=A0 =C2=A0 =C2=A0 (!i, false)
<= br>let () =3D
=C2=A0 Toploop.read_interactive_input :=3D read_input;
= =C2=A0 Topmain.main ()
$ cat dune
(executable
=C2=A0(name mytop)=C2=A0(libraries compiler-libs.toplevel)
=C2=A0(link_flags :standard -= linkall)
=C2=A0(modes byte))
$ dune exec ./mytop.exe
=C2=A0 =C2=A0= =C2=A0 =C2=A0 OCaml version 4.11.1

% 1 + 1;;
- : int =3D 2
FTR, the compiler invocation to build myutop.exe is:
$ o= camlc -linkall -I +compiler-libs ocamlcommon.cma ocamlbytecomp.cma ocamltop= level.cma mytop.ml -o mytop.exe
$ ./myto= p.exe
=C2=A0 =C2=A0 =C2=A0 =C2=A0 OCaml version 4.11.1

% 1 + 1;;<= br>- : int =3D 2

If you want to go further, there are few p= rojects that add line edition support to the toplevel, colors=C2=A0etc, usi= ng similar methods:
-= down:=C2=A0https://erratiqu= e.ch/software/down

Finally, if you'd like to i= mprove the toplevel API so that doing this kind of thing is easier in the f= uture, a PR is always welcome :)

Happy hacking,
<= div class=3D"gmail_default" style=3D"font-family:arial,helvetica,sans-serif= ">
Jeremie



On Fri, Mar 5, 2021 = at 5:05 AM Kenichi Asai <asai@is.o= cha.ac.jp> wrote:
Is there a simple (but complete) example that uses
compiler-libs.toplevel, like creating an OCaml toplevel that uses a
different character for a prompt?=C2=A0 When I tried:

- copy toplevel/topstart.ml to my_topstart.ml and change Topmain in it
=C2=A0 to My_topmain
- copy toplevel/topmain.ml to my_topmain.ml and change Toploop in it
=C2=A0 to My_toploop
- copy toplevel/toploop.ml to my_toploop.ml and change the prompt
=C2=A0 character in it
- optionally, copy toplevel/topdirs.ml to my_topdirs.ml and change
=C2=A0 Toploop, etc., to My_toploop (this does not affect the story below)<= br>
I ran into the following error:

The ocamltoplevel.cma library from compiler-libs cannot be loaded
inside the OCaml toplevel

When I disable it (by removing the expression that raises this error
in my= _toploop.ml), I can launch the new toplevel, but I cannot define
a new name:

let a =3D 3

results in "Fatal error: a unbound at toplevel".=C2=A0 Is it poss= ible to do
this kind of creating a new OCaml toplevel using compiler-libs.toplevel? Or is the compiler-libs.toplevel intended for something else?

A bit of background.=C2=A0 I am developing an OCaml type debugger:

http://pllab.is.ocha.ac.jp/~asai/TypeDebugger/

and am currently trying to port it to a newer version of OCaml.=C2=A0 For the current type debugger, I inserted code for type debugging into the
main loop in toploop.ml.=C2=A0 However, inserting code directly into the
OCaml source requires me to recompile whole (or at least the core of)
the OCaml.=C2=A0 If I could do the same with compiler-libs, that would be great.

Any help is welcome.=C2=A0 (I also welcome the negative information saying<= br> that it is impossible to do this kind of thing.=C2=A0 Then, I would fall back to the old way of compiling whole the OCaml.)

Thank you in advance.

Sincerely,

--
Kenichi Asai


--
Jeremie
--0000000000001c4d7d05bcc6f0ba--