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