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