| author | paulson | 
| Thu, 29 Dec 2022 22:14:25 +0000 | |
| changeset 76823 | 8a17349143df | 
| parent 76729 | b045b40a65cc | 
| child 77320 | 7a6fa60298cd | 
| 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 {
 | 
| 76729 | 15 | def apply( | 
| 16 | store: Sessions.Store, | |
| 17 | options: Options, | |
| 76657 | 18 | session_background: Sessions.Background, | 
| 62634 
aa3b47b32100
less physical "logic" argument, with option -l like "isabelle console" etc.;
 wenzelm parents: 
62633diff
changeset | 19 | logic: String = "", | 
| 71639 
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
 wenzelm parents: 
71598diff
changeset | 20 | raw_ml_system: Boolean = false, | 
| 
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
 wenzelm parents: 
71598diff
changeset | 21 | 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 | 22 | eval_main: String = "", | 
| 62544 | 23 | args: List[String] = Nil, | 
| 62556 
c115e69f457f
more abstract Session.start, without prover command-line;
 wenzelm parents: 
62548diff
changeset | 24 | modes: List[String] = Nil, | 
| 62573 | 25 | cwd: JFile = null, | 
| 73897 | 26 | env: JMap[String, String] = Isabelle_System.settings(), | 
| 62557 | 27 | redirect: Boolean = false, | 
| 76655 | 28 | cleanup: () => Unit = () => () | 
| 75393 | 29 |   ): Bash.Process = {
 | 
| 62637 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 wenzelm parents: 
62634diff
changeset | 30 | val logic_name = Isabelle_System.default_logic(logic) | 
| 68209 | 31 | |
| 62637 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 wenzelm parents: 
62634diff
changeset | 32 | val heaps: List[String] = | 
| 62643 | 33 | if (raw_ml_system) Nil | 
| 62637 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 wenzelm parents: 
62634diff
changeset | 34 |       else {
 | 
| 76657 | 35 | session_background.sessions_structure.selection(logic_name). | 
| 70683 
8c7706b053c7
find theory files via session structure: much faster Prover IDE startup;
 wenzelm parents: 
69831diff
changeset | 36 | build_requirements(List(logic_name)). | 
| 71598 | 37 | map(a => File.platform_path(store.the_heap(a))) | 
| 62544 | 38 | } | 
| 39 | ||
| 62637 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 wenzelm parents: 
62634diff
changeset | 40 | val eval_init = | 
| 62634 
aa3b47b32100
less physical "logic" argument, with option -l like "isabelle console" etc.;
 wenzelm parents: 
62633diff
changeset | 41 |       if (heaps.isEmpty) {
 | 
| 62544 | 42 | List( | 
| 62912 | 43 | """ | 
| 44 | fun chapter (_: string) = (); | |
| 45 | fun section (_: string) = (); | |
| 46 | fun subsection (_: string) = (); | |
| 47 | fun subsubsection (_: string) = (); | |
| 48 | fun paragraph (_: string) = (); | |
| 49 | fun subparagraph (_: string) = (); | |
| 50 | val ML_file = PolyML.use; | |
| 51 | """, | |
| 62544 | 52 | if (Platform.is_windows) | 
| 53 | "fun exit 0 = OS.Process.exit OS.Process.success" + | |
| 54 | " | exit 1 = OS.Process.exit OS.Process.failure" + | |
| 55 | " | exit rc = OS.Process.exit (RunCall.unsafeCast (Word8.fromInt rc))" | |
| 62560 | 56 | else | 
| 57 | "fun exit rc = Posix.Process.exit (Word8.fromInt rc)", | |
| 62629 
1815513a57f1
clarified prompt: "ML" usually means Isabelle/ML;
 wenzelm parents: 
62614diff
changeset | 58 | "PolyML.Compiler.prompt1 := \"Poly/ML> \"", | 
| 
1815513a57f1
clarified prompt: "ML" usually means Isabelle/ML;
 wenzelm parents: 
62614diff
changeset | 59 | "PolyML.Compiler.prompt2 := \"Poly/ML# \"") | 
| 62544 | 60 | } | 
| 62637 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 wenzelm parents: 
62634diff
changeset | 61 | else | 
| 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 wenzelm parents: 
62634diff
changeset | 62 | List( | 
| 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 wenzelm parents: 
62634diff
changeset | 63 | "(PolyML.SaveState.loadHierarchy " + | 
| 66782 | 64 | ML_Syntax.print_list(ML_Syntax.print_string_bytes)(heaps) + | 
| 62637 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 wenzelm parents: 
62634diff
changeset | 65 | "; PolyML.print_depth 0) handle exn => (TextIO.output (TextIO.stdErr, General.exnMessage exn ^ " + | 
| 66782 | 66 |           ML_Syntax.print_string_bytes(": " + logic_name + "\n") +
 | 
| 62637 
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
 wenzelm parents: 
62634diff
changeset | 67 | "); OS.Process.exit OS.Process.failure)") | 
| 62544 | 68 | |
| 69 | val eval_modes = | |
| 70 | if (modes.isEmpty) Nil | |
| 66782 | 71 |       else List("Print_Mode.add_modes " + ML_Syntax.print_list(ML_Syntax.print_string_bytes)(modes))
 | 
| 62544 | 72 | |
| 73897 | 73 | |
| 62544 | 74 | // options | 
| 75591 | 75 |     val eval_options = if (heaps.isEmpty) Nil else List("Options.load_default ()")
 | 
| 62544 | 76 |     val isabelle_process_options = Isabelle_System.tmp_file("options")
 | 
| 71114 | 77 |     Isabelle_System.chmod("600", File.path(isabelle_process_options))
 | 
| 62544 | 78 | File.write(isabelle_process_options, YXML.string_of_body(options.encode)) | 
| 79 | ||
| 76655 | 80 | // session resources | 
| 81 |     val eval_init_session = if (raw_ml_system) Nil else List("Resources.init_session_env ()")
 | |
| 72637 | 82 |     val init_session = Isabelle_System.tmp_file("init_session")
 | 
| 72615 
f827c3bb6b7f
more scalable: avoid large strings on command-line;
 wenzelm parents: 
72614diff
changeset | 83 |     Isabelle_System.chmod("600", File.path(init_session))
 | 
| 76657 | 84 | 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 | 85 | |
| 
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
 wenzelm parents: 
65420diff
changeset | 86 | // process | 
| 62544 | 87 | val eval_process = | 
| 71639 
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
 wenzelm parents: 
71598diff
changeset | 88 | proper_string(eval_main).getOrElse( | 
| 
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
 wenzelm parents: 
71598diff
changeset | 89 |         if (heaps.isEmpty) {
 | 
| 
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
 wenzelm parents: 
71598diff
changeset | 90 |           "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 | 91 | } | 
| 
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
 wenzelm parents: 
71598diff
changeset | 92 | else "Isabelle_Process.init ()") | 
| 62544 | 93 | |
| 62601 
a937889f0086
create ISABELLE_TMP in Scala (despite odd/obsolete chmod in d84b4d39bce1);
 wenzelm parents: 
62600diff
changeset | 94 | // ISABELLE_TMP | 
| 
a937889f0086
create ISABELLE_TMP in Scala (despite odd/obsolete chmod in d84b4d39bce1);
 wenzelm parents: 
62600diff
changeset | 95 |     val isabelle_tmp = Isabelle_System.tmp_dir("process")
 | 
| 71881 | 96 | |
| 75393 | 97 |     val ml_runtime_options = {
 | 
| 64274 | 98 |       val ml_options = Word.explode(Isabelle_System.getenv("ML_OPTIONS"))
 | 
| 69702 | 99 | val ml_options1 = | 
| 100 |         if (ml_options.exists(_.containsSlice("gcthreads"))) ml_options
 | |
| 101 |         else ml_options ::: List("--gcthreads", options.int("threads").toString)
 | |
| 102 | val ml_options2 = | |
| 103 |         if (!Platform.is_windows || ml_options.exists(_.containsSlice("codepage"))) ml_options1
 | |
| 104 |         else ml_options1 ::: List("--codepage", "utf8")
 | |
| 72112 
3546dd4ade74
ML statistics via external process: allows monitoring RTS while ML program sleeps;
 wenzelm parents: 
71881diff
changeset | 105 |       ml_options2 ::: List("--exportstats")
 | 
| 64274 | 106 | } | 
| 107 | ||
| 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 | 108 | // 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 | 109 | val bash_args = | 
| 64274 | 110 | ml_runtime_options ::: | 
| 72637 | 111 |       (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 | 112 |       use_prelude.flatMap(List("--use", _)) ::: List("--eval", eval_process) ::: args
 | 
| 62546 | 113 | |
| 73897 | 114 | val bash_env = new HashMap(env) | 
| 115 |     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 | 116 |     bash_env.put("ISABELLE_INIT_SESSION", File.standard_path(init_session))
 | 
| 73897 | 117 |     bash_env.put("ISABELLE_TMP", File.standard_path(isabelle_tmp))
 | 
| 118 |     bash_env.put("POLYSTATSDIR", isabelle_tmp.getAbsolutePath)
 | |
| 119 | ||
| 63986 
c7a4b03727ae
options for process policy, notably for multiprocessor machines;
 wenzelm parents: 
62912diff
changeset | 120 | Bash.process( | 
| 73604 | 121 |       options.string("ML_process_policy") + """ "$ML_HOME/poly" -q """ +
 | 
| 64304 | 122 | Bash.strings(bash_args), | 
| 62614 | 123 | cwd = cwd, | 
| 73897 | 124 | env = bash_env, | 
| 62614 | 125 | redirect = redirect, | 
| 75394 | 126 |       cleanup = { () =>
 | 
| 127 | isabelle_process_options.delete | |
| 128 | init_session.delete | |
| 129 | Isabelle_System.rm_tree(isabelle_tmp) | |
| 130 | cleanup() | |
| 131 | }) | |
| 62544 | 132 | } | 
| 62586 | 133 | |
| 134 | ||
| 62835 
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
 wenzelm parents: 
62712diff
changeset | 135 | /* Isabelle tool wrapper */ | 
| 62586 | 136 | |
| 72763 | 137 |   val isabelle_tool = Isabelle_Tool("process", "raw ML process (batch mode)",
 | 
| 75394 | 138 | Scala_Project.here, | 
| 139 |     { args =>
 | |
| 140 | var dirs: List[Path] = Nil | |
| 141 | var eval_args: List[String] = Nil | |
| 142 |       var logic = Isabelle_System.getenv("ISABELLE_LOGIC")
 | |
| 143 | var modes: List[String] = Nil | |
| 144 | var options = Options.init() | |
| 62586 | 145 | |
| 75394 | 146 |       val getopts = Getopts("""
 | 
| 62634 
aa3b47b32100
less physical "logic" argument, with option -l like "isabelle console" etc.;
 wenzelm parents: 
62633diff
changeset | 147 | Usage: isabelle process [OPTIONS] | 
| 62586 | 148 | |
| 149 | Options are: | |
| 62677 | 150 | -T THEORY load theory | 
| 62639 | 151 | -d DIR include session directory | 
| 62586 | 152 | -e ML_EXPR evaluate ML expression on startup | 
| 153 | -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 | 154 | -l NAME logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """) | 
| 62586 | 155 | -m MODE add print mode for output | 
| 156 | -o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) | |
| 157 | ||
| 62634 
aa3b47b32100
less physical "logic" argument, with option -l like "isabelle console" etc.;
 wenzelm parents: 
62633diff
changeset | 158 | Run the raw Isabelle ML process in batch mode. | 
| 62586 | 159 | """, | 
| 75394 | 160 | "T:" -> (arg => | 
| 161 |           eval_args = eval_args ::: List("--eval", "use_thy " + ML_Syntax.print_string_bytes(arg))),
 | |
| 162 | "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))), | |
| 163 |         "e:" -> (arg => eval_args = eval_args ::: List("--eval", arg)),
 | |
| 164 |         "f:" -> (arg => eval_args = eval_args ::: List("--use", arg)),
 | |
| 165 | "l:" -> (arg => logic = arg), | |
| 166 | "m:" -> (arg => modes = arg :: modes), | |
| 167 | "o:" -> (arg => options = options + arg)) | |
| 62586 | 168 | |
| 75394 | 169 | val more_args = getopts(args) | 
| 170 | if (args.isEmpty || more_args.nonEmpty) getopts.usage() | |
| 62586 | 171 | |
| 76729 | 172 | val store = Sessions.store(options) | 
| 76657 | 173 | val session_background = Sessions.background(options, logic, dirs = dirs).check_errors | 
| 75394 | 174 | val result = | 
| 76729 | 175 | ML_Process(store, options, session_background, | 
| 76657 | 176 | logic = logic, args = eval_args, modes = modes).result( | 
| 75394 | 177 | progress_stdout = Output.writeln(_, stdout = true), | 
| 178 | progress_stderr = Output.writeln(_)) | |
| 72557 | 179 | |
| 75394 | 180 | sys.exit(result.rc) | 
| 181 | }) | |
| 62544 | 182 | } |