author | wenzelm |
Sat, 21 Mar 2009 19:58:44 +0100 | |
changeset 30623 | 9ed1122d6cd2 |
parent 30619 | 0226c07352db |
child 30626 | 248de8dd839e |
permissions | -rw-r--r-- |
4403 | 1 |
(* Title: Pure/ML-Systems/smlnj.ML |
2 |
||
5708 | 3 |
Compatibility file for Standard ML of New Jersey 110 or later. |
4403 | 4 |
*) |
5 |
||
28443 | 6 |
exception Interrupt; |
7 |
||
24599
7b0ecf9a9055
use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents:
24329
diff
changeset
|
8 |
use "ML-Systems/proper_int.ML"; |
7b0ecf9a9055
use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents:
24329
diff
changeset
|
9 |
use "ML-Systems/overloading_smlnj.ML"; |
23965
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents:
23921
diff
changeset
|
10 |
use "ML-Systems/exn.ML"; |
25732 | 11 |
use "ML-Systems/universal.ML"; |
28151
61f9c918b410
explicit use of universal.ML and dummy_thread.ML;
wenzelm
parents:
26885
diff
changeset
|
12 |
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
|
13 |
use "ML-Systems/multithreading.ML"; |
26220 | 14 |
use "ML-Systems/system_shell.ML"; |
28268
ac8431ecd57e
use_text/use_file now depend on explicit ML name space;
wenzelm
parents:
28151
diff
changeset
|
15 |
use "ML-Systems/ml_name_space.ML"; |
30619 | 16 |
use "ML-Systems/ml_pretty.ML"; |
23921
947152add153
added compatibility file for ML systems without multithreading;
wenzelm
parents:
23826
diff
changeset
|
17 |
|
16542 | 18 |
|
19 |
(*low-level pointer equality*) |
|
16502 | 20 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
21 |
CM.autoload "$smlnj/init/init.cmi"; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
22 |
val pointer_eq = InlineT.ptreql; |
16528 | 23 |
|
16502 | 24 |
|
4403 | 25 |
(* restore old-style character / string functions *) |
26 |
||
24145 | 27 |
val ord = mk_int o SML90.ord; |
28 |
val chr = SML90.chr o dest_int; |
|
10725 | 29 |
val explode = SML90.explode; |
30 |
val implode = SML90.implode; |
|
4403 | 31 |
|
32 |
||
33 |
(* New Jersey ML parameters *) |
|
34 |
||
35 |
val _ = |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
36 |
(Control.Print.printLength := 1000; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
37 |
Control.Print.printDepth := 350; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
38 |
Control.Print.stringDepth := 250; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
39 |
Control.Print.signatures := 2; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
40 |
Control.MC.matchRedundantError := false); |
4403 | 41 |
|
42 |
||
43 |
(* Poly/ML emulation *) |
|
44 |
||
24599
7b0ecf9a9055
use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents:
24329
diff
changeset
|
45 |
val exit = exit o dest_int; |
4403 | 46 |
fun quit () = exit 0; |
47 |
||
48 |
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*) |
|
24329 | 49 |
local |
24599
7b0ecf9a9055
use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents:
24329
diff
changeset
|
50 |
val depth = ref (10: int); |
24329 | 51 |
in |
52 |
fun get_print_depth () = ! depth; |
|
53 |
fun print_depth n = |
|
54 |
(depth := n; |
|
55 |
Control.Print.printDepth := dest_int n div 2; |
|
56 |
Control.Print.printLength := dest_int n); |
|
57 |
end; |
|
4403 | 58 |
|
26474 | 59 |
|
5816 | 60 |
(* compiler-independent timing functions *) |
4403 | 61 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
62 |
fun start_timing () = |
30187
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29564
diff
changeset
|
63 |
let |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29564
diff
changeset
|
64 |
val timer = Timer.startCPUTimer (); |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29564
diff
changeset
|
65 |
val time = Timer.checkCPUTimer timer; |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29564
diff
changeset
|
66 |
in (timer, time) end; |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
67 |
|
30187
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29564
diff
changeset
|
68 |
fun end_timing (timer, {sys, usr}) = |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29564
diff
changeset
|
69 |
let |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29564
diff
changeset
|
70 |
open Time; (*...for Time.toString, Time.+ and Time.- *) |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29564
diff
changeset
|
71 |
val {sys = sys2, usr = usr2} = Timer.checkCPUTimer timer; |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29564
diff
changeset
|
72 |
val user = usr2 - usr; |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29564
diff
changeset
|
73 |
val all = user + sys2 - sys; |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29564
diff
changeset
|
74 |
val message = "User " ^ toString user ^ " All "^ toString all ^ " secs" handle Time => ""; |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29564
diff
changeset
|
75 |
in {message = message, user = user, all = all} end; |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
76 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
77 |
fun check_timer timer = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
78 |
let |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
79 |
val {sys, usr} = Timer.checkCPUTimer timer; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
80 |
val gc = Timer.checkGCTime timer; (* FIXME already included in usr? *) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
81 |
in (sys, usr, gc) end; |
4403 | 82 |
|
83 |
||
16660 | 84 |
(*prompts*) |
4977 | 85 |
fun ml_prompts p1 p2 = |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
86 |
(Control.primaryPrompt := p1; Control.secondaryPrompt := p2); |
4977 | 87 |
|
17511 | 88 |
(*dummy implementation*) |
16681 | 89 |
fun profile (n: int) f x = f x; |
90 |
||
17511 | 91 |
(*dummy implementation*) |
16681 | 92 |
fun exception_trace f = f (); |
4977 | 93 |
|
18384 | 94 |
(*dummy implementation*) |
95 |
fun print x = x; |
|
16681 | 96 |
|
23770 | 97 |
(*dummy implementation*) |
98 |
fun makestring x = "dummy string for SML New Jersey"; |
|
99 |
||
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
100 |
|
24290 | 101 |
(* toplevel pretty printing (see also Pure/pure_setup.ML) *) |
4403 | 102 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
103 |
fun make_pp path pprint = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
104 |
let |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
105 |
open PrettyPrint; |
4403 | 106 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
107 |
fun pp pps obj = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
108 |
pprint obj |
24145 | 109 |
(string pps, openHOVBox pps o Rel o dest_int, |
110 |
fn wd => break pps {nsp=dest_int wd, offset=0}, fn () => newline pps, |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
111 |
fn () => closeBox pps); |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
112 |
in (path, pp) end; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
113 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
114 |
fun install_pp (path, pp) = CompilerPPTable.install_pp path pp; |
4403 | 115 |
|
116 |
||
117 |
(* ML command execution *) |
|
118 |
||
28268
ac8431ecd57e
use_text/use_file now depend on explicit ML name space;
wenzelm
parents:
28151
diff
changeset
|
119 |
fun use_text (tune: string -> string) _ _ (line: int, name) (print, err) verbose txt = |
5090 | 120 |
let |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
121 |
val ref out_orig = Control.Print.out; |
5090 | 122 |
|
123 |
val out_buffer = ref ([]: string list); |
|
124 |
val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())}; |
|
10914 | 125 |
fun output () = |
7890 | 126 |
let val str = implode (rev (! out_buffer)) |
10914 | 127 |
in String.substring (str, 0, Int.max (0, size str - 1)) end; |
5090 | 128 |
in |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
129 |
Control.Print.out := out; |
24599
7b0ecf9a9055
use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents:
24329
diff
changeset
|
130 |
Backend.Interact.useStream (TextIO.openString (tune txt)) handle exn => |
22144 | 131 |
(Control.Print.out := out_orig; |
132 |
err ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn); |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
133 |
Control.Print.out := out_orig; |
10914 | 134 |
if verbose then print (output ()) else () |
5090 | 135 |
end; |
4403 | 136 |
|
28268
ac8431ecd57e
use_text/use_file now depend on explicit ML name space;
wenzelm
parents:
28151
diff
changeset
|
137 |
fun use_file tune str_of_pos name_space output verbose name = |
24599
7b0ecf9a9055
use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents:
24329
diff
changeset
|
138 |
let |
7b0ecf9a9055
use_text/file: tune text (cf. ML_Parse.fix_ints);
wenzelm
parents:
24329
diff
changeset
|
139 |
val instream = TextIO.openIn name; |
26504 | 140 |
val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream); |
28268
ac8431ecd57e
use_text/use_file now depend on explicit ML name space;
wenzelm
parents:
28151
diff
changeset
|
141 |
in use_text tune str_of_pos name_space (1, name) output verbose txt end; |
21770 | 142 |
|
26474 | 143 |
fun forget_structure name = |
28284
2161665a0a5d
use_text/use_file now depend on explicit ML name space;
wenzelm
parents:
28268
diff
changeset
|
144 |
use_text (fn x => x) (fn _ => "") () (1, "ML") (TextIO.print, fn s => raise Fail s) false |
26474 | 145 |
("structure " ^ name ^ " = struct end"); |
146 |
||
21770 | 147 |
|
4403 | 148 |
|
5816 | 149 |
(** interrupts **) |
150 |
||
26084
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
151 |
local |
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
152 |
|
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
153 |
fun change_signal new_handler f x = |
5816 | 154 |
let |
26084
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
155 |
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
|
156 |
val result = Exn.capture (f old_handler) x; |
12990 | 157 |
val _ = Signals.setHandler (Signals.sigINT, old_handler); |
23965
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents:
23921
diff
changeset
|
158 |
in Exn.release result end; |
5816 | 159 |
|
26084
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
160 |
in |
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
161 |
|
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
162 |
fun interruptible (f: 'a -> 'b) x = |
12990 | 163 |
let |
26084
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
164 |
val result = ref (Exn.Exn Interrupt: 'b Exn.result); |
12990 | 165 |
val old_handler = Signals.inqHandler Signals.sigINT; |
5816 | 166 |
in |
12990 | 167 |
SMLofNJ.Cont.callcc (fn cont => |
26084
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
168 |
(Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => cont)); |
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
169 |
result := Exn.capture f x)); |
12990 | 170 |
Signals.setHandler (Signals.sigINT, old_handler); |
26084
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
171 |
Exn.release (! result) |
12990 | 172 |
end; |
5816 | 173 |
|
26084
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
174 |
fun uninterruptible f = |
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
175 |
change_signal Signals.IGNORE |
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
176 |
(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
|
177 |
|
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
178 |
end; |
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
179 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
180 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
181 |
(* basis library fixes *) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
182 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
183 |
structure TextIO = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
184 |
struct |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
185 |
open TextIO; |
23139
aa899bce7c3b
TextIO.inputLine: use present SML B library version;
wenzelm
parents:
22144
diff
changeset
|
186 |
fun inputLine is = TextIO.inputLine is |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
187 |
handle IO.Io _ => raise Interrupt; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
188 |
end; |
17511 | 189 |
|
18790
418131f631fc
interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents:
18760
diff
changeset
|
190 |
|
5816 | 191 |
|
4403 | 192 |
(** OS related **) |
193 |
||
23826
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
194 |
(* current directory *) |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
195 |
|
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
196 |
val cd = OS.FileSys.chDir; |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
197 |
val pwd = OS.FileSys.getDir; |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
198 |
|
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
199 |
|
4403 | 200 |
(* system command execution *) |
201 |
||
26220 | 202 |
val system_out = (fn (output, rc) => (output, mk_int rc)) o system_out; |
7855 | 203 |
|
4403 | 204 |
|
17824 | 205 |
(*Convert a process ID to a decimal string (chiefly for tracing)*) |
28488
18fea7e88ea1
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
206 |
fun process_id pid = |
18fea7e88ea1
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
207 |
Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord (Posix.ProcEnv.getpid ()))); |
17824 | 208 |
|
209 |
||
4403 | 210 |
(* getenv *) |
211 |
||
212 |
fun getenv var = |
|
213 |
(case OS.Process.getEnv var of |
|
214 |
NONE => "" |
|
215 |
| SOME txt => txt); |