author | wenzelm |
Sat, 19 Dec 2015 19:52:52 +0100 | |
changeset 61869 | ba466ac335e3 |
parent 61862 | e2a9e46ac0fb |
child 61885 | acdfc76a6c33 |
permissions | -rw-r--r-- |
4403 | 1 |
(* Title: Pure/ML-Systems/smlnj.ML |
2 |
||
50910 | 3 |
Compatibility file for Standard ML of New Jersey. |
4403 | 4 |
*) |
5 |
||
54342
fbcaa9f08879
avoid non-portable int constant -- make SML/NJ happy;
wenzelm
parents:
53709
diff
changeset
|
6 |
val io_buffer_size = 4096; |
44249
64620f1d6f87
identify parallel exceptions where they emerge first -- to achieve unique results within evaluation graph;
wenzelm
parents:
43948
diff
changeset
|
7 |
use "ML-Systems/proper_int.ML"; |
64620f1d6f87
identify parallel exceptions where they emerge first -- to achieve unique results within evaluation graph;
wenzelm
parents:
43948
diff
changeset
|
8 |
|
28443 | 9 |
exception Interrupt; |
31427
5a07cc86675d
reraise exceptions to preserve original position (ML system specific);
wenzelm
parents:
31321
diff
changeset
|
10 |
fun reraise exn = raise exn; |
28443 | 11 |
|
56627 | 12 |
fun exit rc = Posix.Process.exit (Word8.fromInt rc); |
13 |
fun quit () = exit 0; |
|
14 |
||
24599
7b0ecf9a9055
use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents:
24329
diff
changeset
|
15 |
use "ML-Systems/overloading_smlnj.ML"; |
34136 | 16 |
use "General/exn.ML"; |
35014
a725ff6ead26
explicit representation of single-assignment variables;
wenzelm
parents:
35010
diff
changeset
|
17 |
use "ML-Systems/single_assignment.ML"; |
25732 | 18 |
use "ML-Systems/universal.ML"; |
28151
61f9c918b410
explicit use of universal.ML and dummy_thread.ML;
wenzelm
parents:
26885
diff
changeset
|
19 |
use "ML-Systems/thread_dummy.ML"; |
24688
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24599
diff
changeset
|
20 |
use "ML-Systems/multithreading.ML"; |
60923 | 21 |
use "ML-Systems/ml_stack_dummy.ML"; |
60897 | 22 |
use "ML-Systems/ml_pretty.ML"; |
28268
ac8431ecd57e
use_text/use_file now depend on explicit ML name space;
wenzelm
parents:
28151
diff
changeset
|
23 |
use "ML-Systems/ml_name_space.ML"; |
38635
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents:
36162
diff
changeset
|
24 |
structure PolyML = struct end; |
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents:
36162
diff
changeset
|
25 |
use "ML-Systems/pp_dummy.ML"; |
30672
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
wenzelm
parents:
30626
diff
changeset
|
26 |
use "ML-Systems/use_context.ML"; |
56435
28b34e8e4a80
approximate ML antiquotation @{here} for Isabelle/Pure bootstrap;
wenzelm
parents:
56285
diff
changeset
|
27 |
use "ML-Systems/ml_positions.ML"; |
23921
947152add153
added compatibility file for ML systems without multithreading;
wenzelm
parents:
23826
diff
changeset
|
28 |
|
16542 | 29 |
|
42012
2c3fe3cbebae
structure Timing: covers former start_timing/end_timing and Output.timeit etc;
wenzelm
parents:
41411
diff
changeset
|
30 |
val seconds = Time.fromReal; |
2c3fe3cbebae
structure Timing: covers former start_timing/end_timing and Output.timeit etc;
wenzelm
parents:
41411
diff
changeset
|
31 |
|
16542 | 32 |
(*low-level pointer equality*) |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
33 |
CM.autoload "$smlnj/init/init.cmi"; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
34 |
val pointer_eq = InlineT.ptreql; |
16528 | 35 |
|
16502 | 36 |
|
4403 | 37 |
(* restore old-style character / string functions *) |
38 |
||
40627
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
40393
diff
changeset
|
39 |
val ord = mk_int o SML90.ord; |
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
40393
diff
changeset
|
40 |
val chr = SML90.chr o dest_int; |
becf5d5187cc
renamed raw "explode" function to "raw_explode" to emphasize its meaning;
wenzelm
parents:
40393
diff
changeset
|
41 |
val raw_explode = SML90.explode; |
10725 | 42 |
val implode = SML90.implode; |
4403 | 43 |
|
44 |
||
45 |
(* New Jersey ML parameters *) |
|
46 |
||
47 |
val _ = |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
48 |
(Control.Print.printLength := 1000; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
49 |
Control.Print.printDepth := 350; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
50 |
Control.Print.stringDepth := 250; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
51 |
Control.Print.signatures := 2; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
52 |
Control.MC.matchRedundantError := false); |
4403 | 53 |
|
54 |
||
55 |
(* Poly/ML emulation *) |
|
56 |
||
57 |
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*) |
|
24329 | 58 |
local |
24599
7b0ecf9a9055
use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents:
24329
diff
changeset
|
59 |
val depth = ref (10: int); |
24329 | 60 |
in |
56281 | 61 |
fun get_default_print_depth () = ! depth; |
56285 | 62 |
fun default_print_depth n = |
24329 | 63 |
(depth := n; |
64 |
Control.Print.printDepth := dest_int n div 2; |
|
65 |
Control.Print.printLength := dest_int n); |
|
56285 | 66 |
val _ = default_print_depth 10; |
24329 | 67 |
end; |
4403 | 68 |
|
59127
723b11f8ffbf
more careful handling of auxiliary environment structure -- allow nested ML evaluation;
wenzelm
parents:
59055
diff
changeset
|
69 |
fun ml_make_string (_: string) = "(fn _ => \"?\")"; |
36162
0bd034a80a9a
added ML antiquotation @{make_string}, which produces proper pretty printed version in Poly/ML 5.3.0 or later;
wenzelm
parents:
35014
diff
changeset
|
70 |
|
26474 | 71 |
|
16660 | 72 |
(*prompts*) |
4977 | 73 |
fun ml_prompts p1 p2 = |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
74 |
(Control.primaryPrompt := p1; Control.secondaryPrompt := p2); |
4977 | 75 |
|
17511 | 76 |
(*dummy implementation*) |
16681 | 77 |
fun profile (n: int) f x = f x; |
78 |
||
17511 | 79 |
(*dummy implementation*) |
59055
5a7157b8e870
more informative failure of protocol commands, with exception trace;
wenzelm
parents:
56627
diff
changeset
|
80 |
fun print_exception_trace (_: exn -> string) (_: string -> unit) f = f (); |
4977 | 81 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
82 |
|
4403 | 83 |
(* ML command execution *) |
84 |
||
60956 | 85 |
fun use_text ({tune_source, print, error, ...}: use_context) |
86 |
{line, file, verbose, debug = _: bool} text = |
|
5090 | 87 |
let |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
88 |
val ref out_orig = Control.Print.out; |
5090 | 89 |
|
90 |
val out_buffer = ref ([]: string list); |
|
91 |
val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())}; |
|
10914 | 92 |
fun output () = |
7890 | 93 |
let val str = implode (rev (! out_buffer)) |
10914 | 94 |
in String.substring (str, 0, Int.max (0, size str - 1)) end; |
5090 | 95 |
in |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
96 |
Control.Print.out := out; |
60956 | 97 |
Backend.Interact.useStream (TextIO.openString (tune_source (ml_positions line file text))) |
56435
28b34e8e4a80
approximate ML antiquotation @{here} for Isabelle/Pure bootstrap;
wenzelm
parents:
56285
diff
changeset
|
98 |
handle exn => |
28b34e8e4a80
approximate ML antiquotation @{here} for Isabelle/Pure bootstrap;
wenzelm
parents:
56285
diff
changeset
|
99 |
(Control.Print.out := out_orig; |
60955 | 100 |
error ((if file = "" then "" else "Error in " ^ file ^ "\n") ^ output ()); raise exn); |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
101 |
Control.Print.out := out_orig; |
10914 | 102 |
if verbose then print (output ()) else () |
5090 | 103 |
end; |
4403 | 104 |
|
60956 | 105 |
fun use_file context {verbose, debug} file = |
24599
7b0ecf9a9055
use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents:
24329
diff
changeset
|
106 |
let |
60955 | 107 |
val instream = TextIO.openIn file; |
60956 | 108 |
val text = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream); |
109 |
in use_text context {line = 1, file = file, verbose = verbose, debug = debug} text end; |
|
21770 | 110 |
|
111 |
||
30626 | 112 |
(* toplevel pretty printing *) |
113 |
||
114 |
fun ml_pprint pps = |
|
115 |
let |
|
116 |
fun str "" = () |
|
117 |
| str s = PrettyPrint.string pps s; |
|
61869 | 118 |
fun pprint (ML_Pretty.Block ((bg, en), consistent, ind, prts)) = |
119 |
(str bg; |
|
120 |
(if consistent then PrettyPrint.openHVBox else PrettyPrint.openHOVBox) pps |
|
121 |
(PrettyPrint.Rel (dest_int ind)); |
|
122 |
List.app pprint prts; PrettyPrint.closeBox pps; |
|
123 |
str en) |
|
30626 | 124 |
| pprint (ML_Pretty.String (s, _)) = str s |
61862
e2a9e46ac0fb
support pretty break indent, like underlying ML systems;
wenzelm
parents:
60965
diff
changeset
|
125 |
| pprint (ML_Pretty.Break (false, width, offset)) = |
e2a9e46ac0fb
support pretty break indent, like underlying ML systems;
wenzelm
parents:
60965
diff
changeset
|
126 |
PrettyPrint.break pps {nsp = dest_int width, offset = dest_int offset} |
e2a9e46ac0fb
support pretty break indent, like underlying ML systems;
wenzelm
parents:
60965
diff
changeset
|
127 |
| pprint (ML_Pretty.Break (true, _, _)) = PrettyPrint.newline pps; |
30626 | 128 |
in pprint end; |
129 |
||
30672
beaadd5af500
more systematic type use_context, with particular values ML_Parse.global_context and ML_Context.local_context;
wenzelm
parents:
30626
diff
changeset
|
130 |
fun toplevel_pp context path pp = |
60956 | 131 |
use_text context {line = 1, file = "pp", verbose = false, debug = false} |
30626 | 132 |
("CompilerPPTable.install_pp [" ^ String.concatWith "," (map (fn s => "\"" ^ s ^ "\"") path) ^ |
133 |
"] (fn pps => ml_pprint pps o Pretty.to_ML o (" ^ pp ^ "))"); |
|
134 |
||
135 |
||
4403 | 136 |
|
5816 | 137 |
(** interrupts **) |
138 |
||
26084
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
139 |
local |
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
140 |
|
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
141 |
fun change_signal new_handler f x = |
5816 | 142 |
let |
26084
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
143 |
val old_handler = Signals.setHandler (Signals.sigINT, new_handler); |
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
144 |
val result = Exn.capture (f old_handler) x; |
12990 | 145 |
val _ = Signals.setHandler (Signals.sigINT, old_handler); |
23965
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents:
23921
diff
changeset
|
146 |
in Exn.release result end; |
5816 | 147 |
|
26084
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
148 |
in |
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
149 |
|
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
150 |
fun interruptible (f: 'a -> 'b) x = |
12990 | 151 |
let |
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
38635
diff
changeset
|
152 |
val result = ref (Exn.interrupt_exn: 'b Exn.result); |
12990 | 153 |
val old_handler = Signals.inqHandler Signals.sigINT; |
5816 | 154 |
in |
12990 | 155 |
SMLofNJ.Cont.callcc (fn cont => |
26084
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
156 |
(Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => cont)); |
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
157 |
result := Exn.capture f x)); |
12990 | 158 |
Signals.setHandler (Signals.sigINT, old_handler); |
26084
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
159 |
Exn.release (! result) |
12990 | 160 |
end; |
5816 | 161 |
|
26084
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
162 |
fun uninterruptible f = |
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
163 |
change_signal Signals.IGNORE |
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
164 |
(fn old_handler => f (fn g => change_signal old_handler (fn _ => g))); |
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
165 |
|
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
166 |
end; |
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
167 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
168 |
|
39616
8052101883c3
renamed setmp_noncritical to Unsynchronized.setmp to emphasize its meaning;
wenzelm
parents:
39585
diff
changeset
|
169 |
use "ML-Systems/unsynchronized.ML"; |
60745
d86b4cd0f1ec
clarified ML compiler parameters: always provide PolyML.Compiler.CPDebug, ignore global default;
wenzelm
parents:
60729
diff
changeset
|
170 |
use "ML-Systems/ml_debugger.ML"; |
39616
8052101883c3
renamed setmp_noncritical to Unsynchronized.setmp to emphasize its meaning;
wenzelm
parents:
39585
diff
changeset
|
171 |
|
48416 | 172 |
|
173 |
(* ML system operations *) |
|
174 |
||
60964
fdb82e722f8a
keep native CInterface to make SHA1 work properly;
wenzelm
parents:
60956
diff
changeset
|
175 |
fun ml_platform_path (s: string) = s; |
60965 | 176 |
fun ml_standard_path (s: string) = s; |
60964
fdb82e722f8a
keep native CInterface to make SHA1 work properly;
wenzelm
parents:
60956
diff
changeset
|
177 |
|
43948 | 178 |
use "ML-Systems/ml_system.ML"; |
179 |
||
48416 | 180 |
structure ML_System = |
181 |
struct |
|
182 |
||
183 |
open ML_System; |
|
184 |
||
185 |
fun save_state name = |
|
186 |
if SMLofNJ.exportML name then () |
|
187 |
else OS.FileSys.rename {old = name ^ "." ^ platform, new = name}; |
|
188 |
||
189 |
end; |