| author | wenzelm | 
| Wed, 10 May 2023 23:28:15 +0200 | |
| changeset 78030 | ec9840c673c3 | 
| parent 77650 | b1ca8975490a | 
| child 78161 | 4b1b7cbb3e9a | 
| permissions | -rw-r--r-- | 
| 65477 | 1 | /* Title: Pure/ML/ml_process.scala | 
| 62544 | 2 | Author: Makarius | 
| 3 | ||
| 62586 | 4 | The raw ML process. | 
| 62544 | 5 | */ | 
| 6 | ||
| 7 | package isabelle | |
| 8 | ||
| 9 | ||
| 73897 | 10 | import java.util.{Map => JMap, HashMap}
 | 
| 62573 | 11 | import java.io.{File => JFile}
 | 
| 12 | ||
| 13 | ||
| 75393 | 14 | object ML_Process {
 | 
| 77650 | 15 | def bootstrap_shasum(): SHA1.Shasum = | 
| 16 |     SHA1.shasum_meta_info(SHA1.digest(Path.explode("$POLYML_EXE")))
 | |
| 17 | ||
| 77414 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 18 | def session_heaps( | 
| 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 19 | store: Sessions.Store, | 
| 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 20 | session_background: Sessions.Background, | 
| 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 21 | logic: String = "" | 
| 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 22 |   ): List[Path] = {
 | 
| 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 23 | val logic_name = Isabelle_System.default_logic(logic) | 
| 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 24 | |
| 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 25 | session_background.sessions_structure.selection(logic_name). | 
| 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 26 | build_requirements(List(logic_name)). | 
| 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 27 | map(store.the_heap) | 
| 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 28 | } | 
| 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 29 | |
| 76729 | 30 | def apply( | 
| 31 | options: Options, | |
| 76657 | 32 | session_background: Sessions.Background, | 
| 77414 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 33 | session_heaps: List[Path], | 
| 71639 
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
 wenzelm parents: 
71598diff
changeset | 34 | use_prelude: List[String] = Nil, | 
| 
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
 wenzelm parents: 
71598diff
changeset | 35 | eval_main: String = "", | 
| 62544 | 36 | args: List[String] = Nil, | 
| 62556 
c115e69f457f
more abstract Session.start, without prover command-line;
 wenzelm parents: 
62548diff
changeset | 37 | modes: List[String] = Nil, | 
| 62573 | 38 | cwd: JFile = null, | 
| 73897 | 39 | env: JMap[String, String] = Isabelle_System.settings(), | 
| 62557 | 40 | redirect: Boolean = false, | 
| 76655 | 41 | cleanup: () => Unit = () => () | 
| 75393 | 42 |   ): Bash.Process = {
 | 
| 62637 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 wenzelm parents: 
62634diff
changeset | 43 | val eval_init = | 
| 77414 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 44 |       if (session_heaps.isEmpty) {
 | 
| 62544 | 45 | List( | 
| 62912 | 46 | """ | 
| 47 | fun chapter (_: string) = (); | |
| 48 | fun section (_: string) = (); | |
| 49 | fun subsection (_: string) = (); | |
| 50 | fun subsubsection (_: string) = (); | |
| 51 | fun paragraph (_: string) = (); | |
| 52 | fun subparagraph (_: string) = (); | |
| 53 | val ML_file = PolyML.use; | |
| 54 | """, | |
| 62544 | 55 | if (Platform.is_windows) | 
| 56 | "fun exit 0 = OS.Process.exit OS.Process.success" + | |
| 57 | " | exit 1 = OS.Process.exit OS.Process.failure" + | |
| 58 | " | exit rc = OS.Process.exit (RunCall.unsafeCast (Word8.fromInt rc))" | |
| 62560 | 59 | else | 
| 60 | "fun exit rc = Posix.Process.exit (Word8.fromInt rc)", | |
| 62629 
1815513a57f1
clarified prompt: "ML" usually means Isabelle/ML;
 wenzelm parents: 
62614diff
changeset | 61 | "PolyML.Compiler.prompt1 := \"Poly/ML> \"", | 
| 
1815513a57f1
clarified prompt: "ML" usually means Isabelle/ML;
 wenzelm parents: 
62614diff
changeset | 62 | "PolyML.Compiler.prompt2 := \"Poly/ML# \"") | 
| 62544 | 63 | } | 
| 77413 | 64 |       else {
 | 
| 62637 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 wenzelm parents: 
62634diff
changeset | 65 | List( | 
| 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 wenzelm parents: 
62634diff
changeset | 66 | "(PolyML.SaveState.loadHierarchy " + | 
| 77414 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 67 | ML_Syntax.print_list( | 
| 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 68 | ML_Syntax.print_string_bytes)(session_heaps.map(File.platform_path)) + | 
| 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 69 | "; PolyML.print_depth 0)") | 
| 77413 | 70 | } | 
| 62544 | 71 | |
| 72 | val eval_modes = | |
| 73 | if (modes.isEmpty) Nil | |
| 66782 | 74 |       else List("Print_Mode.add_modes " + ML_Syntax.print_list(ML_Syntax.print_string_bytes)(modes))
 | 
| 62544 | 75 | |
| 73897 | 76 | |
| 62544 | 77 | // options | 
| 77414 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 78 |     val eval_options = if (session_heaps.isEmpty) Nil else List("Options.load_default ()")
 | 
| 62544 | 79 |     val isabelle_process_options = Isabelle_System.tmp_file("options")
 | 
| 71114 | 80 |     Isabelle_System.chmod("600", File.path(isabelle_process_options))
 | 
| 62544 | 81 | File.write(isabelle_process_options, YXML.string_of_body(options.encode)) | 
| 82 | ||
| 76655 | 83 | // session resources | 
| 77414 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 84 |     val eval_init_session = if (session_heaps.isEmpty) Nil else List("Resources.init_session_env ()")
 | 
| 72637 | 85 |     val init_session = Isabelle_System.tmp_file("init_session")
 | 
| 72615 
f827c3bb6b7f
more scalable: avoid large strings on command-line;
 wenzelm parents: 
72614diff
changeset | 86 |     Isabelle_System.chmod("600", File.path(init_session))
 | 
| 76657 | 87 | File.write(init_session, new Resources(session_background).init_session_yxml) | 
| 65431 
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
 wenzelm parents: 
65420diff
changeset | 88 | |
| 
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
 wenzelm parents: 
65420diff
changeset | 89 | // process | 
| 62544 | 90 | val eval_process = | 
| 71639 
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
 wenzelm parents: 
71598diff
changeset | 91 | proper_string(eval_main).getOrElse( | 
| 77414 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 92 |         if (session_heaps.isEmpty) {
 | 
| 71639 
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
 wenzelm parents: 
71598diff
changeset | 93 |           "PolyML.print_depth " + ML_Syntax.print_int(options.int("ML_print_depth"))
 | 
| 
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
 wenzelm parents: 
71598diff
changeset | 94 | } | 
| 
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
 wenzelm parents: 
71598diff
changeset | 95 | else "Isabelle_Process.init ()") | 
| 62544 | 96 | |
| 62601 
a937889f0086
create ISABELLE_TMP in Scala (despite odd/obsolete chmod in d84b4d39bce1);
 wenzelm parents: 
62600diff
changeset | 97 | // ISABELLE_TMP | 
| 
a937889f0086
create ISABELLE_TMP in Scala (despite odd/obsolete chmod in d84b4d39bce1);
 wenzelm parents: 
62600diff
changeset | 98 |     val isabelle_tmp = Isabelle_System.tmp_dir("process")
 | 
| 71881 | 99 | |
| 75393 | 100 |     val ml_runtime_options = {
 | 
| 64274 | 101 |       val ml_options = Word.explode(Isabelle_System.getenv("ML_OPTIONS"))
 | 
| 69702 | 102 | val ml_options1 = | 
| 103 |         if (ml_options.exists(_.containsSlice("gcthreads"))) ml_options
 | |
| 104 |         else ml_options ::: List("--gcthreads", options.int("threads").toString)
 | |
| 105 | val ml_options2 = | |
| 106 |         if (!Platform.is_windows || ml_options.exists(_.containsSlice("codepage"))) ml_options1
 | |
| 107 |         else ml_options1 ::: List("--codepage", "utf8")
 | |
| 72112 
3546dd4ade74
ML statistics via external process: allows monitoring RTS while ML program sleeps;
 wenzelm parents: 
71881diff
changeset | 108 |       ml_options2 ::: List("--exportstats")
 | 
| 64274 | 109 | } | 
| 110 | ||
| 62545 
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
 wenzelm parents: 
62544diff
changeset | 111 | // bash | 
| 
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
 wenzelm parents: 
62544diff
changeset | 112 | val bash_args = | 
| 64274 | 113 | ml_runtime_options ::: | 
| 72637 | 114 |       (eval_init ::: eval_modes ::: eval_options ::: eval_init_session).flatMap(List("--eval", _)) :::
 | 
| 71639 
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
 wenzelm parents: 
71598diff
changeset | 115 |       use_prelude.flatMap(List("--use", _)) ::: List("--eval", eval_process) ::: args
 | 
| 62546 | 116 | |
| 73897 | 117 | val bash_env = new HashMap(env) | 
| 118 |     bash_env.put("ISABELLE_PROCESS_OPTIONS", File.standard_path(isabelle_process_options))
 | |
| 75590 
99b7638d9177
clarified session resources for bootstrap, notably for Scala functions;
 wenzelm parents: 
75394diff
changeset | 119 |     bash_env.put("ISABELLE_INIT_SESSION", File.standard_path(init_session))
 | 
| 73897 | 120 |     bash_env.put("ISABELLE_TMP", File.standard_path(isabelle_tmp))
 | 
| 121 |     bash_env.put("POLYSTATSDIR", isabelle_tmp.getAbsolutePath)
 | |
| 122 | ||
| 77483 | 123 |     val process_policy = options.string("process_policy")
 | 
| 77480 | 124 | val process_prefix = if_proper(process_policy, process_policy + " ") | 
| 77320 | 125 | |
| 77482 | 126 | Bash.process(process_prefix + "\"$POLYML_EXE\" -q " + Bash.strings(bash_args), | 
| 62614 | 127 | cwd = cwd, | 
| 73897 | 128 | env = bash_env, | 
| 62614 | 129 | redirect = redirect, | 
| 75394 | 130 |       cleanup = { () =>
 | 
| 131 | isabelle_process_options.delete | |
| 132 | init_session.delete | |
| 133 | Isabelle_System.rm_tree(isabelle_tmp) | |
| 134 | cleanup() | |
| 135 | }) | |
| 62544 | 136 | } | 
| 62586 | 137 | |
| 138 | ||
| 62835 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 wenzelm parents: 
62712diff
changeset | 139 | /* Isabelle tool wrapper */ | 
| 62586 | 140 | |
| 72763 | 141 |   val isabelle_tool = Isabelle_Tool("process", "raw ML process (batch mode)",
 | 
| 75394 | 142 | Scala_Project.here, | 
| 143 |     { args =>
 | |
| 144 | var dirs: List[Path] = Nil | |
| 145 | var eval_args: List[String] = Nil | |
| 146 |       var logic = Isabelle_System.getenv("ISABELLE_LOGIC")
 | |
| 147 | var modes: List[String] = Nil | |
| 148 | var options = Options.init() | |
| 62586 | 149 | |
| 75394 | 150 |       val getopts = Getopts("""
 | 
| 62634 
aa3b47b32100
less physical "logic" argument, with option -l like "isabelle console" etc.;
 wenzelm parents: 
62633diff
changeset | 151 | Usage: isabelle process [OPTIONS] | 
| 62586 | 152 | |
| 153 | Options are: | |
| 62677 | 154 | -T THEORY load theory | 
| 62639 | 155 | -d DIR include session directory | 
| 62586 | 156 | -e ML_EXPR evaluate ML expression on startup | 
| 157 | -f ML_FILE evaluate ML file on startup | |
| 62634 
aa3b47b32100
less physical "logic" argument, with option -l like "isabelle console" etc.;
 wenzelm parents: 
62633diff
changeset | 158 | -l NAME logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """) | 
| 62586 | 159 | -m MODE add print mode for output | 
| 160 | -o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) | |
| 161 | ||
| 62634 
aa3b47b32100
less physical "logic" argument, with option -l like "isabelle console" etc.;
 wenzelm parents: 
62633diff
changeset | 162 | Run the raw Isabelle ML process in batch mode. | 
| 62586 | 163 | """, | 
| 75394 | 164 | "T:" -> (arg => | 
| 165 |           eval_args = eval_args ::: List("--eval", "use_thy " + ML_Syntax.print_string_bytes(arg))),
 | |
| 166 | "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))), | |
| 167 |         "e:" -> (arg => eval_args = eval_args ::: List("--eval", arg)),
 | |
| 168 |         "f:" -> (arg => eval_args = eval_args ::: List("--use", arg)),
 | |
| 169 | "l:" -> (arg => logic = arg), | |
| 170 | "m:" -> (arg => modes = arg :: modes), | |
| 171 | "o:" -> (arg => options = options + arg)) | |
| 62586 | 172 | |
| 75394 | 173 | val more_args = getopts(args) | 
| 174 | if (args.isEmpty || more_args.nonEmpty) getopts.usage() | |
| 62586 | 175 | |
| 76729 | 176 | val store = Sessions.store(options) | 
| 76657 | 177 | val session_background = Sessions.background(options, logic, dirs = dirs).check_errors | 
| 77414 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 178 | val session_heaps = ML_Process.session_heaps(store, session_background, logic = logic) | 
| 75394 | 179 | val result = | 
| 77414 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 180 | ML_Process(options, session_background, session_heaps, args = eval_args, modes = modes) | 
| 
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
 wenzelm parents: 
77413diff
changeset | 181 | .result( | 
| 75394 | 182 | progress_stdout = Output.writeln(_, stdout = true), | 
| 183 | progress_stderr = Output.writeln(_)) | |
| 72557 | 184 | |
| 75394 | 185 | sys.exit(result.rc) | 
| 186 | }) | |
| 62544 | 187 | } |