* [Caml-list] expansion of #use?
@ 2020-05-18 11:32 Kenichi Asai
  2020-05-19  9:34 ` Kenichi Asai
  0 siblings, 1 reply; 4+ messages in thread
From: Kenichi Asai @ 2020-05-18 11:32 UTC (permalink / raw)
  To: caml-list
Is there any way to expand the use of #use?  Suppose I have the
following two files:
a.ml:
let a = 3
b.ml:
#use "a.ml";;
print_int a
Given the file name b.ml, I want to obtain:
let a = 3 ;;
print_int a
The -dsource option of ocaml almost does it, but it executes the
program at the same time.  In the above program, it prints 3, too.
So far, I tweak into the OCaml implementation, turn off the execution
in execute_phrase in toploop.ml, and re-compile whole the OCaml.  I
wonder if I can do the same thing simpler than this, possibly using
compiler libs?
Thank you in advance.
PS. I know I should not use #use from the first place.
-- 
Kenichi Asai
^ permalink raw reply	[flat|nested] 4+ messages in thread
* Re: [Caml-list] expansion of #use?
  2020-05-18 11:32 [Caml-list] expansion of #use? Kenichi Asai
@ 2020-05-19  9:34 ` Kenichi Asai
  2020-05-22  5:50   ` Kenichi Asai
  0 siblings, 1 reply; 4+ messages in thread
From: Kenichi Asai @ 2020-05-19  9:34 UTC (permalink / raw)
  To: caml-list
[-- Attachment #1: Type: text/plain, Size: 2142 bytes --]
I tried to implement it myself, using compiler-libs (based on OCaml
4.11.0-alpha1 that I happen to install recently):
https://github.com/kenichi-asai/remove-sharpuse
It can be installed via:
opam pin add remove-sharpuse https://github.com/kenichi-asai/remove-sharpuse.git
However, if I execute (with attached a.ml and b.ml):
$ remove-sharpuse -dsource b.ml
Fatal error: exception Invalid_argument("The ocamltoplevel.cma library from compiler-libs cannot be loaded inside the OCaml toplevel")
What I did is as follows:
- I copied topstart.ml from toplevel/topstart.ml and modified it so
  that it calls My_topmain.main() instead of Topmain.main().
- I copied my_topmain.ml from toplevel/topmain.ml and modified all the
  occurences of Toploop to My_toploop.
- I copied my_toploop.ml from toplevel/toploop.ml and modified
  execute_phrase so that it does not execute Ptop_def case.  That is,
  I inserted "if true then true else" at line 273:
https://github.com/kenichi-asai/remove-sharpuse/blob/4.11.0-alpha1/my_toploop.ml#L273
  so that Ptop_def case is ignored.
If I insert the "if true then true else" line manually into the OCaml
source and compile it via "make world", I can expand #use by passing
"-dsource" to the resulting ocaml.  Can I do the same using
compiler-libs without getting the above error?
Thank you in advance.
-- 
Kenichi Asai
On Mon, May 18, 2020 at 08:32:03PM +0900,
 Kenichi Asai wrote:
> Is there any way to expand the use of #use?  Suppose I have the
> following two files:
> 
> a.ml:
> let a = 3
> 
> b.ml:
> #use "a.ml";;
> print_int a
> 
> Given the file name b.ml, I want to obtain:
> 
> let a = 3 ;;
> print_int a
> 
> The -dsource option of ocaml almost does it, but it executes the
> program at the same time.  In the above program, it prints 3, too.
> 
> So far, I tweak into the OCaml implementation, turn off the execution
> in execute_phrase in toploop.ml, and re-compile whole the OCaml.  I
> wonder if I can do the same thing simpler than this, possibly using
> compiler libs?
> 
> Thank you in advance.
> 
> PS. I know I should not use #use from the first place.
> 
> -- 
> Kenichi Asai
[-- Attachment #2: a.ml --]
[-- Type: text/plain, Size: 14 bytes --]
let a = 1 + 2
[-- Attachment #3: b.ml --]
[-- Type: text/plain, Size: 52 bytes --]
let b = 5;;
#use "a.ml";;
let _ = print_int (a + b)
^ permalink raw reply	[flat|nested] 4+ messages in thread* Re: [Caml-list] expansion of #use?
  2020-05-19  9:34 ` Kenichi Asai
@ 2020-05-22  5:50   ` Kenichi Asai
  2020-05-23 11:00     ` orbifx
  0 siblings, 1 reply; 4+ messages in thread
From: Kenichi Asai @ 2020-05-22  5:50 UTC (permalink / raw)
  To: caml-list
My attempt succeeded!  Here is what I did.  In addition to the
previous e-mail,
- I copied topdirs.ml from toplevel/topdirs.ml and modified all the
  occurences of Toploop to My_toploop.  This loads the function to
  handle #use into the (compiler-libs) toplevel environment.
- I switched off (!) the check for the following error:
> Fatal error: exception Invalid_argument("The ocamltoplevel.cma library from compiler-libs cannot be loaded inside the OCaml toplevel")
  (If this is too dangerous, please let me know.)
- (Some more minor changes.)
The resulting code can be installed via:
opam pin add remove-sharpuse https://github.com/kenichi-asai/remove-sharpuse.git#4.11.0-alpha1
Note that the default branch is now changed to 4.04.0 (for which I
wanted this tool originally).
-- 
Kenichi Asai
On Tue, May 19, 2020 at 06:34:06PM +0900,
 Kenichi Asai wrote:
> I tried to implement it myself, using compiler-libs (based on OCaml
> 4.11.0-alpha1 that I happen to install recently):
> 
> https://github.com/kenichi-asai/remove-sharpuse
> 
> It can be installed via:
> 
> opam pin add remove-sharpuse https://github.com/kenichi-asai/remove-sharpuse.git
> 
> However, if I execute (with attached a.ml and b.ml):
> 
> $ remove-sharpuse -dsource b.ml
> Fatal error: exception Invalid_argument("The ocamltoplevel.cma library from compiler-libs cannot be loaded inside the OCaml toplevel")
> 
> What I did is as follows:
> 
> - I copied topstart.ml from toplevel/topstart.ml and modified it so
>   that it calls My_topmain.main() instead of Topmain.main().
> - I copied my_topmain.ml from toplevel/topmain.ml and modified all the
>   occurences of Toploop to My_toploop.
> - I copied my_toploop.ml from toplevel/toploop.ml and modified
>   execute_phrase so that it does not execute Ptop_def case.  That is,
>   I inserted "if true then true else" at line 273:
> 
> https://github.com/kenichi-asai/remove-sharpuse/blob/4.11.0-alpha1/my_toploop.ml#L273
> 
>   so that Ptop_def case is ignored.
> 
> If I insert the "if true then true else" line manually into the OCaml
> source and compile it via "make world", I can expand #use by passing
> "-dsource" to the resulting ocaml.  Can I do the same using
> compiler-libs without getting the above error?
> 
> Thank you in advance.
> 
> -- 
> Kenichi Asai
> 
> 
> On Mon, May 18, 2020 at 08:32:03PM +0900,
>  Kenichi Asai wrote:
> 
> > Is there any way to expand the use of #use?  Suppose I have the
> > following two files:
> > 
> > a.ml:
> > let a = 3
> > 
> > b.ml:
> > #use "a.ml";;
> > print_int a
> > 
> > Given the file name b.ml, I want to obtain:
> > 
> > let a = 3 ;;
> > print_int a
> > 
> > The -dsource option of ocaml almost does it, but it executes the
> > program at the same time.  In the above program, it prints 3, too.
> > 
> > So far, I tweak into the OCaml implementation, turn off the execution
> > in execute_phrase in toploop.ml, and re-compile whole the OCaml.  I
> > wonder if I can do the same thing simpler than this, possibly using
> > compiler libs?
> > 
> > Thank you in advance.
> > 
> > PS. I know I should not use #use from the first place.
> > 
> > -- 
> > Kenichi Asai
^ permalink raw reply	[flat|nested] 4+ messages in thread
end of thread, other threads:[~2020-05-23 11:00 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-18 11:32 [Caml-list] expansion of #use? Kenichi Asai
2020-05-19  9:34 ` Kenichi Asai
2020-05-22  5:50   ` Kenichi Asai
2020-05-23 11:00     ` orbifx
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox