| author | wenzelm |
| Sat, 01 Nov 2025 13:56:39 +0100 | |
| changeset 83435 | 0f9bae334ac6 |
| parent 83321 | 7505b5e592b1 |
| 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 |
|
12 |
||
| 75393 | 13 |
object ML_Process {
|
| 82708 | 14 |
/* process */ |
15 |
||
| 76729 | 16 |
def apply( |
17 |
options: Options, |
|
| 76657 | 18 |
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:
77413
diff
changeset
|
19 |
session_heaps: List[Path], |
|
71639
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
wenzelm
parents:
71598
diff
changeset
|
20 |
use_prelude: List[String] = Nil, |
|
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
wenzelm
parents:
71598
diff
changeset
|
21 |
eval_main: String = "", |
| 62544 | 22 |
args: List[String] = Nil, |
|
62556
c115e69f457f
more abstract Session.start, without prover command-line;
wenzelm
parents:
62548
diff
changeset
|
23 |
modes: List[String] = Nil, |
|
80224
db92e0b6a11a
clarified signature: prefer symbolic isabelle.Path over physical java.io.File;
wenzelm
parents:
80124
diff
changeset
|
24 |
cwd: Path = Path.current, |
| 82706 | 25 |
env: JMap[String, String] = Isabelle_System.Settings.env(), |
| 62557 | 26 |
redirect: Boolean = false, |
| 76655 | 27 |
cleanup: () => Unit = () => () |
| 75393 | 28 |
): Bash.Process = {
|
|
79656
10e560f2f580
more robust default: Scala imposes explicit "threads" value on ML, both the Poly/ML RTS and Isabelle/ML;
wenzelm
parents:
79654
diff
changeset
|
29 |
val ml_options = options.standard_ml() |
| 82761 | 30 |
val ml_settings = ML_Settings(ml_options) |
|
79656
10e560f2f580
more robust default: Scala imposes explicit "threads" value on ML, both the Poly/ML RTS and Isabelle/ML;
wenzelm
parents:
79654
diff
changeset
|
31 |
|
|
62637
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62634
diff
changeset
|
32 |
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:
77413
diff
changeset
|
33 |
if (session_heaps.isEmpty) {
|
| 62544 | 34 |
List( |
| 62912 | 35 |
""" |
36 |
fun chapter (_: string) = (); |
|
37 |
fun section (_: string) = (); |
|
38 |
fun subsection (_: string) = (); |
|
39 |
fun subsubsection (_: string) = (); |
|
40 |
fun paragraph (_: string) = (); |
|
41 |
fun subparagraph (_: string) = (); |
|
42 |
val ML_file = PolyML.use; |
|
43 |
""", |
|
| 62544 | 44 |
if (Platform.is_windows) |
45 |
"fun exit 0 = OS.Process.exit OS.Process.success" + |
|
46 |
" | exit 1 = OS.Process.exit OS.Process.failure" + |
|
47 |
" | exit rc = OS.Process.exit (RunCall.unsafeCast (Word8.fromInt rc))" |
|
| 62560 | 48 |
else |
49 |
"fun exit rc = Posix.Process.exit (Word8.fromInt rc)", |
|
|
62629
1815513a57f1
clarified prompt: "ML" usually means Isabelle/ML;
wenzelm
parents:
62614
diff
changeset
|
50 |
"PolyML.Compiler.prompt1 := \"Poly/ML> \"", |
|
1815513a57f1
clarified prompt: "ML" usually means Isabelle/ML;
wenzelm
parents:
62614
diff
changeset
|
51 |
"PolyML.Compiler.prompt2 := \"Poly/ML# \"") |
| 62544 | 52 |
} |
| 77413 | 53 |
else {
|
|
62637
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62634
diff
changeset
|
54 |
List( |
|
0189fe0f6452
support for Poly/ML heap hierarchy, which saves a lot of disk space;
wenzelm
parents:
62634
diff
changeset
|
55 |
"(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:
77413
diff
changeset
|
56 |
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:
77413
diff
changeset
|
57 |
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:
77413
diff
changeset
|
58 |
"; PolyML.print_depth 0)") |
| 77413 | 59 |
} |
| 62544 | 60 |
|
61 |
val eval_modes = |
|
62 |
if (modes.isEmpty) Nil |
|
| 66782 | 63 |
else List("Print_Mode.add_modes " + ML_Syntax.print_list(ML_Syntax.print_string_bytes)(modes))
|
| 62544 | 64 |
|
| 73897 | 65 |
|
| 62544 | 66 |
// options |
|
77414
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents:
77413
diff
changeset
|
67 |
val eval_options = if (session_heaps.isEmpty) Nil else List("Options.load_default ()")
|
| 62544 | 68 |
val isabelle_process_options = Isabelle_System.tmp_file("options")
|
| 78161 | 69 |
File.restrict(File.path(isabelle_process_options)) |
|
79656
10e560f2f580
more robust default: Scala imposes explicit "threads" value on ML, both the Poly/ML RTS and Isabelle/ML;
wenzelm
parents:
79654
diff
changeset
|
70 |
File.write(isabelle_process_options, YXML.string_of_body(ml_options.encode)) |
| 62544 | 71 |
|
| 76655 | 72 |
// 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:
77413
diff
changeset
|
73 |
val eval_init_session = if (session_heaps.isEmpty) Nil else List("Resources.init_session_env ()")
|
| 72637 | 74 |
val init_session = Isabelle_System.tmp_file("init_session")
|
| 78161 | 75 |
File.restrict(File.path(init_session)) |
|
80462
7a1f9e571046
clarified signature: more explicit XML.Body types, more uniform Symbol.encode_yxml;
wenzelm
parents:
80225
diff
changeset
|
76 |
File.write(init_session, YXML.string_of_body(new Resources(session_background).init_session_xml)) |
|
65431
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents:
65420
diff
changeset
|
77 |
|
|
4a3e6cda3b94
provide session base for "isabelle build" and "isabelle console" ML process;
wenzelm
parents:
65420
diff
changeset
|
78 |
// process |
| 62544 | 79 |
val eval_process = |
|
71639
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
wenzelm
parents:
71598
diff
changeset
|
80 |
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:
77413
diff
changeset
|
81 |
if (session_heaps.isEmpty) {
|
|
79656
10e560f2f580
more robust default: Scala imposes explicit "threads" value on ML, both the Poly/ML RTS and Isabelle/ML;
wenzelm
parents:
79654
diff
changeset
|
82 |
"PolyML.print_depth " + ML_Syntax.print_int(ml_options.int("ML_print_depth"))
|
|
71639
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
wenzelm
parents:
71598
diff
changeset
|
83 |
} |
|
ec84f542e411
clarified signature of ML_Process vs. Isabelle_Process: proper support for "isabelle build -P -b";
wenzelm
parents:
71598
diff
changeset
|
84 |
else "Isabelle_Process.init ()") |
| 62544 | 85 |
|
|
62601
a937889f0086
create ISABELLE_TMP in Scala (despite odd/obsolete chmod in d84b4d39bce1);
wenzelm
parents:
62600
diff
changeset
|
86 |
// ISABELLE_TMP |
|
a937889f0086
create ISABELLE_TMP in Scala (despite odd/obsolete chmod in d84b4d39bce1);
wenzelm
parents:
62600
diff
changeset
|
87 |
val isabelle_tmp = Isabelle_System.tmp_dir("process")
|
| 71881 | 88 |
|
| 75393 | 89 |
val ml_runtime_options = {
|
|
82720
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
82716
diff
changeset
|
90 |
val ml_options0 = Word.explode(ml_settings.ml_options) |
| 69702 | 91 |
val ml_options1 = |
| 79654 | 92 |
if (ml_options0.exists(_.containsSlice("gcthreads"))) ml_options0
|
|
79656
10e560f2f580
more robust default: Scala imposes explicit "threads" value on ML, both the Poly/ML RTS and Isabelle/ML;
wenzelm
parents:
79654
diff
changeset
|
93 |
else ml_options0 ::: List("--gcthreads", ml_options.threads().toString)
|
| 69702 | 94 |
val ml_options2 = |
| 79654 | 95 |
if (!Platform.is_windows || ml_options0.exists(_.containsSlice("codepage"))) ml_options1
|
| 69702 | 96 |
else ml_options1 ::: List("--codepage", "utf8")
|
|
72112
3546dd4ade74
ML statistics via external process: allows monitoring RTS while ML program sleeps;
wenzelm
parents:
71881
diff
changeset
|
97 |
ml_options2 ::: List("--exportstats")
|
| 64274 | 98 |
} |
99 |
||
|
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:
62544
diff
changeset
|
100 |
// 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:
62544
diff
changeset
|
101 |
val bash_args = |
| 64274 | 102 |
ml_runtime_options ::: |
| 72637 | 103 |
(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:
71598
diff
changeset
|
104 |
use_prelude.flatMap(List("--use", _)) ::: List("--eval", eval_process) ::: args
|
| 62546 | 105 |
|
| 73897 | 106 |
val bash_env = new HashMap(env) |
|
82720
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
82716
diff
changeset
|
107 |
bash_env.put("ML_PLATFORM", ml_settings.ml_platform)
|
|
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
82716
diff
changeset
|
108 |
bash_env.put("ML_HOME", File.standard_path(ml_settings.ml_home))
|
| 73897 | 109 |
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:
75394
diff
changeset
|
110 |
bash_env.put("ISABELLE_INIT_SESSION", File.standard_path(init_session))
|
| 73897 | 111 |
bash_env.put("ISABELLE_TMP", File.standard_path(isabelle_tmp))
|
112 |
bash_env.put("POLYSTATSDIR", isabelle_tmp.getAbsolutePath)
|
|
113 |
||
|
79656
10e560f2f580
more robust default: Scala imposes explicit "threads" value on ML, both the Poly/ML RTS and Isabelle/ML;
wenzelm
parents:
79654
diff
changeset
|
114 |
val process_policy = ml_options.string("process_policy")
|
| 77480 | 115 |
val process_prefix = if_proper(process_policy, process_policy + " ") |
| 77320 | 116 |
|
|
82720
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
82716
diff
changeset
|
117 |
Bash.process( |
|
956ecf2c07a0
more flexible ML_Settings in Isabelle/Scala, depending on system options and some default settings;
wenzelm
parents:
82716
diff
changeset
|
118 |
process_prefix + File.bash_path(ml_settings.polyml_exe) + " -q " + Bash.strings(bash_args), |
| 62614 | 119 |
cwd = cwd, |
| 73897 | 120 |
env = bash_env, |
| 62614 | 121 |
redirect = redirect, |
| 75394 | 122 |
cleanup = { () =>
|
123 |
isabelle_process_options.delete |
|
124 |
init_session.delete |
|
125 |
Isabelle_System.rm_tree(isabelle_tmp) |
|
126 |
cleanup() |
|
127 |
}) |
|
| 62544 | 128 |
} |
| 62586 | 129 |
|
130 |
||
|
62835
1a9ce1b13b20
prefer internal tool -- assuming that ISABELLE_TMP_PREFIX is created properly by Isabelle_System.isabelle_tmp_prefix;
wenzelm
parents:
62712
diff
changeset
|
131 |
/* Isabelle tool wrapper */ |
| 62586 | 132 |
|
| 72763 | 133 |
val isabelle_tool = Isabelle_Tool("process", "raw ML process (batch mode)",
|
| 75394 | 134 |
Scala_Project.here, |
135 |
{ args =>
|
|
136 |
var dirs: List[Path] = Nil |
|
137 |
var eval_args: List[String] = Nil |
|
|
83435
0f9bae334ac6
more uniform Isabelle_System.default_logic, with subtle change of semantics due to getenv_strict;
wenzelm
parents:
83321
diff
changeset
|
138 |
var logic = Isabelle_System.default_logic() |
| 75394 | 139 |
var modes: List[String] = Nil |
140 |
var options = Options.init() |
|
| 62586 | 141 |
|
| 75394 | 142 |
val getopts = Getopts("""
|
|
62634
aa3b47b32100
less physical "logic" argument, with option -l like "isabelle console" etc.;
wenzelm
parents:
62633
diff
changeset
|
143 |
Usage: isabelle process [OPTIONS] |
| 62586 | 144 |
|
145 |
Options are: |
|
| 62639 | 146 |
-d DIR include session directory |
| 62586 | 147 |
-e ML_EXPR evaluate ML expression on startup |
148 |
-f ML_FILE evaluate ML file on startup |
|
|
62634
aa3b47b32100
less physical "logic" argument, with option -l like "isabelle console" etc.;
wenzelm
parents:
62633
diff
changeset
|
149 |
-l NAME logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """) |
| 62586 | 150 |
-m MODE add print mode for output |
151 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) |
|
152 |
||
|
62634
aa3b47b32100
less physical "logic" argument, with option -l like "isabelle console" etc.;
wenzelm
parents:
62633
diff
changeset
|
153 |
Run the raw Isabelle ML process in batch mode. |
| 62586 | 154 |
""", |
| 75394 | 155 |
"d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))), |
156 |
"e:" -> (arg => eval_args = eval_args ::: List("--eval", arg)),
|
|
157 |
"f:" -> (arg => eval_args = eval_args ::: List("--use", arg)),
|
|
158 |
"l:" -> (arg => logic = arg), |
|
159 |
"m:" -> (arg => modes = arg :: modes), |
|
160 |
"o:" -> (arg => options = options + arg)) |
|
| 62586 | 161 |
|
| 75394 | 162 |
val more_args = getopts(args) |
163 |
if (args.isEmpty || more_args.nonEmpty) getopts.usage() |
|
| 62586 | 164 |
|
| 78178 | 165 |
val store = Store(options) |
| 76657 | 166 |
val session_background = Sessions.background(options, logic, dirs = dirs).check_errors |
| 82752 | 167 |
val session_heaps = store.session_heaps(session_background, logic = logic) |
| 75394 | 168 |
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:
77413
diff
changeset
|
169 |
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:
77413
diff
changeset
|
170 |
.result( |
| 75394 | 171 |
progress_stdout = Output.writeln(_, stdout = true), |
172 |
progress_stderr = Output.writeln(_)) |
|
| 72557 | 173 |
|
| 75394 | 174 |
sys.exit(result.rc) |
175 |
}) |
|
| 62544 | 176 |
} |