author | wenzelm |
Tue, 24 Jul 2007 19:44:33 +0200 | |
changeset 23962 | e0358fac0541 |
parent 23921 | 947152add153 |
child 23965 | f93e509659c1 |
permissions | -rw-r--r-- |
4403 | 1 |
(* Title: Pure/ML-Systems/smlnj.ML |
2 |
ID: $Id$ |
|
3 |
||
5708 | 4 |
Compatibility file for Standard ML of New Jersey 110 or later. |
4403 | 5 |
*) |
6 |
||
23921
947152add153
added compatibility file for ML systems without multithreading;
wenzelm
parents:
23826
diff
changeset
|
7 |
use "ML-Systems/no_multithreading.ML"; |
947152add153
added compatibility file for ML systems without multithreading;
wenzelm
parents:
23826
diff
changeset
|
8 |
|
16542 | 9 |
|
4403 | 10 |
(** ML system related **) |
11 |
||
16542 | 12 |
(*low-level pointer equality*) |
16502 | 13 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
14 |
CM.autoload "$smlnj/init/init.cmi"; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
15 |
val pointer_eq = InlineT.ptreql; |
16528 | 16 |
|
16502 | 17 |
|
4403 | 18 |
(* restore old-style character / string functions *) |
19 |
||
10725 | 20 |
val ord = SML90.ord; |
21 |
val chr = SML90.chr; |
|
22 |
val explode = SML90.explode; |
|
23 |
val implode = SML90.implode; |
|
4403 | 24 |
|
25 |
||
26 |
(* New Jersey ML parameters *) |
|
27 |
||
28 |
val _ = |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
29 |
(Control.Print.printLength := 1000; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
30 |
Control.Print.printDepth := 350; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
31 |
Control.Print.stringDepth := 250; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
32 |
Control.Print.signatures := 2; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
33 |
Control.MC.matchRedundantError := false); |
4403 | 34 |
|
35 |
||
36 |
(* Poly/ML emulation *) |
|
37 |
||
38 |
fun quit () = exit 0; |
|
39 |
||
40 |
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*) |
|
41 |
fun print_depth n = |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
42 |
(Control.Print.printDepth := n div 2; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
43 |
Control.Print.printLength := n); |
4403 | 44 |
|
45 |
||
5816 | 46 |
(* compiler-independent timing functions *) |
4403 | 47 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
48 |
fun start_timing () = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
49 |
let val CPUtimer = Timer.startCPUTimer(); |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
50 |
val time = Timer.checkCPUTimer(CPUtimer) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
51 |
in (CPUtimer,time) end; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
52 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
53 |
fun end_timing (CPUtimer, {sys,usr}) = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
54 |
let open Time (*...for Time.toString, Time.+ and Time.- *) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
55 |
val {sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
56 |
in "User " ^ toString (usr2-usr) ^ |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
57 |
" All "^ toString (sys2-sys + usr2-usr) ^ |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
58 |
" secs" |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
59 |
handle Time => "" |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
60 |
end; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
61 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
62 |
fun check_timer timer = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
63 |
let |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
64 |
val {sys, usr} = Timer.checkCPUTimer timer; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
65 |
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
|
66 |
in (sys, usr, gc) end; |
4403 | 67 |
|
68 |
||
16660 | 69 |
(*prompts*) |
4977 | 70 |
fun ml_prompts p1 p2 = |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
71 |
(Control.primaryPrompt := p1; Control.secondaryPrompt := p2); |
4977 | 72 |
|
17511 | 73 |
(*dummy implementation*) |
16681 | 74 |
fun profile (n: int) f x = f x; |
75 |
||
17511 | 76 |
(*dummy implementation*) |
16681 | 77 |
fun exception_trace f = f (); |
4977 | 78 |
|
18384 | 79 |
(*dummy implementation*) |
80 |
fun print x = x; |
|
16681 | 81 |
|
23770 | 82 |
(*dummy implementation*) |
83 |
fun makestring x = "dummy string for SML New Jersey"; |
|
84 |
||
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
85 |
|
4403 | 86 |
(* toplevel pretty printing (see also Pure/install_pp.ML) *) |
87 |
||
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
88 |
fun make_pp path pprint = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
89 |
let |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
90 |
open PrettyPrint; |
4403 | 91 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
92 |
fun pp pps obj = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
93 |
pprint obj |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
94 |
(string pps, openHOVBox pps o Rel, |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
95 |
fn wd => break pps {nsp=wd, offset=0}, fn () => newline pps, |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
96 |
fn () => closeBox pps); |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
97 |
in (path, pp) end; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
98 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
99 |
fun install_pp (path, pp) = CompilerPPTable.install_pp path pp; |
4403 | 100 |
|
101 |
||
102 |
(* ML command execution *) |
|
103 |
||
22144 | 104 |
fun use_text name (print, err) verbose txt = |
5090 | 105 |
let |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
106 |
val ref out_orig = Control.Print.out; |
5090 | 107 |
|
108 |
val out_buffer = ref ([]: string list); |
|
109 |
val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())}; |
|
10914 | 110 |
fun output () = |
7890 | 111 |
let val str = implode (rev (! out_buffer)) |
10914 | 112 |
in String.substring (str, 0, Int.max (0, size str - 1)) end; |
5090 | 113 |
in |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
114 |
Control.Print.out := out; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
115 |
Backend.Interact.useStream (TextIO.openString txt) handle exn => |
22144 | 116 |
(Control.Print.out := out_orig; |
117 |
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
|
118 |
Control.Print.out := out_orig; |
10914 | 119 |
if verbose then print (output ()) else () |
5090 | 120 |
end; |
4403 | 121 |
|
21770 | 122 |
fun use_file _ _ name = use name; |
123 |
||
124 |
||
4403 | 125 |
|
5816 | 126 |
(** interrupts **) |
127 |
||
12990 | 128 |
exception Interrupt; |
129 |
||
5816 | 130 |
local |
131 |
||
12990 | 132 |
fun capture f x = ((f x): unit; NONE) handle exn => SOME exn; |
5816 | 133 |
|
12990 | 134 |
fun release NONE = () |
135 |
| release (SOME exn) = raise exn; |
|
5816 | 136 |
|
137 |
in |
|
138 |
||
12990 | 139 |
fun ignore_interrupt f x = |
5816 | 140 |
let |
12990 | 141 |
val old_handler = Signals.setHandler (Signals.sigINT, Signals.IGNORE); |
142 |
val result = capture f x; |
|
143 |
val _ = Signals.setHandler (Signals.sigINT, old_handler); |
|
144 |
in release result end; |
|
5816 | 145 |
|
12990 | 146 |
fun raise_interrupt f x = |
147 |
let |
|
5816 | 148 |
val interrupted = ref false; |
12990 | 149 |
val result = ref NONE; |
150 |
val old_handler = Signals.inqHandler Signals.sigINT; |
|
5816 | 151 |
in |
12990 | 152 |
SMLofNJ.Cont.callcc (fn cont => |
153 |
(Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => (interrupted := true; cont))); |
|
154 |
result := capture f x)); |
|
155 |
Signals.setHandler (Signals.sigINT, old_handler); |
|
156 |
if ! interrupted then raise Interrupt else release (! result) |
|
157 |
end; |
|
5816 | 158 |
|
159 |
end; |
|
160 |
||
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
161 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
162 |
(* basis library fixes *) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
163 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
164 |
structure TextIO = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
165 |
struct |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
166 |
open TextIO; |
23139
aa899bce7c3b
TextIO.inputLine: use present SML B library version;
wenzelm
parents:
22144
diff
changeset
|
167 |
fun inputLine is = TextIO.inputLine is |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
168 |
handle IO.Io _ => raise Interrupt; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
169 |
end; |
17511 | 170 |
|
18790
418131f631fc
interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents:
18760
diff
changeset
|
171 |
|
418131f631fc
interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents:
18760
diff
changeset
|
172 |
(* bounded time execution *) |
418131f631fc
interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents:
18760
diff
changeset
|
173 |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
174 |
fun interrupt_timeout time f x = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
175 |
TimeLimit.timeLimit time f x |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
176 |
handle TimeLimit.TimeOut => raise Interrupt; |
18790
418131f631fc
interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents:
18760
diff
changeset
|
177 |
|
418131f631fc
interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents:
18760
diff
changeset
|
178 |
|
16542 | 179 |
(** Signal handling: emulation of the Poly/ML Signal structure. Note that types |
15702
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
180 |
Posix.Signal.signal and Signals.signal differ **) |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
181 |
|
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
182 |
structure IsaSignal = |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
183 |
struct |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
184 |
|
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
185 |
datatype sig_handle = SIG_DFL | SIG_IGN | SIG_HANDLE of Signals.signal -> unit; |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
186 |
|
16542 | 187 |
(*From the SML/NJ documentation: "HANDLER(f) installs a handler for a |
188 |
signal. When signal is delivered to the process, the execution state |
|
189 |
of the current thread will be bundled up as a continuation k, then |
|
190 |
f(signal,n,k) will be called. The number n is the number of times |
|
191 |
signal has been signalled since the last time f was invoked for it."*) |
|
15702
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
192 |
|
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
193 |
fun toAction SIG_DFL = Signals.DEFAULT |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
194 |
| toAction SIG_IGN = Signals.IGNORE |
16542 | 195 |
| toAction (SIG_HANDLE iu) = |
15702
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
196 |
Signals.HANDLER (fn (signo,_,cont) => (iu signo; cont)); |
16542 | 197 |
|
15702
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
198 |
(*The types are correct, but I'm not sure about the semantics!*) |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
199 |
fun fromAction Signals.DEFAULT = SIG_DFL |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
200 |
| fromAction Signals.IGNORE = SIG_IGN |
16542 | 201 |
| fromAction (Signals.HANDLER f) = |
15702
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
202 |
SIG_HANDLE (fn signo => SMLofNJ.Cont.callcc (fn k => (f (signo,0,k); ()))); |
16542 | 203 |
|
15702
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
204 |
(*Poly/ML version has type int * sig_handle -> sig_handle*) |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
205 |
fun signal (signo, sh) = fromAction (Signals.setHandler (signo, toAction sh)); |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
206 |
|
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
207 |
val usr1 = UnixSignals.sigUSR1 |
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
208 |
val usr2 = UnixSignals.sigUSR2 |
17763 | 209 |
val alrm = UnixSignals.sigALRM |
210 |
val chld = UnixSignals.sigCHLD |
|
211 |
val cont = UnixSignals.sigCONT |
|
212 |
val int = UnixSignals.sigINT |
|
213 |
val quit = UnixSignals.sigQUIT |
|
214 |
val term = UnixSignals.sigTERM |
|
15702
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
215 |
|
2677db44c795
new signalling primmitives for sml/nj compatibility
paulson
parents:
14981
diff
changeset
|
216 |
end; |
5816 | 217 |
|
218 |
||
4403 | 219 |
(** OS related **) |
220 |
||
23826
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
221 |
(* current directory *) |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
222 |
|
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
223 |
val cd = OS.FileSys.chDir; |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
224 |
val pwd = OS.FileSys.getDir; |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
225 |
|
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
23770
diff
changeset
|
226 |
|
4403 | 227 |
(* system command execution *) |
228 |
||
229 |
(*execute Unix command which doesn't take any input from stdin and |
|
230 |
sends its output to stdout; could be done more easily by Unix.execute, |
|
231 |
but that function doesn't use the PATH*) |
|
232 |
fun execute command = |
|
233 |
let |
|
234 |
val tmp_name = OS.FileSys.tmpName (); |
|
235 |
val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name); |
|
236 |
val result = TextIO.inputAll is; |
|
237 |
in |
|
238 |
TextIO.closeIn is; |
|
239 |
OS.FileSys.remove tmp_name; |
|
240 |
result |
|
241 |
end; |
|
242 |
||
7855 | 243 |
(*plain version; with return code*) |
12990 | 244 |
val system = OS.Process.system: string -> int; |
7855 | 245 |
|
4403 | 246 |
|
17824 | 247 |
(*Convert a process ID to a decimal string (chiefly for tracing)*) |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
18790
diff
changeset
|
248 |
fun string_of_pid pid = |
17824 | 249 |
Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid)); |
250 |
||
251 |
||
4403 | 252 |
(* getenv *) |
253 |
||
254 |
fun getenv var = |
|
255 |
(case OS.Process.getEnv var of |
|
256 |
NONE => "" |
|
257 |
| SOME txt => txt); |