2413
|
1 |
(* Title: Pure/ML-Systems/smlnj-0.93.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Carsten Clasohm, TU Muenchen
|
|
4 |
Copyright 1996 TU Muenchen
|
|
5 |
|
|
6 |
Compatibility file for Standard ML of New Jersey version 0.93.
|
|
7 |
*)
|
|
8 |
|
3594
|
9 |
(*needs the Basis Library emulation*)
|
|
10 |
use "basis.ML";
|
2413
|
11 |
|
7149
|
12 |
structure String =
|
|
13 |
struct
|
|
14 |
fun substring args = String.substring args
|
|
15 |
handle String.Substring => raise Subscript;
|
|
16 |
fun isPrefix s1 s2 = (s1 = substring(s2,0, size s1))
|
|
17 |
handle Subscript => false;
|
14597
|
18 |
fun str (s : string) = s;
|
|
19 |
fun translate f s = implode (map f (explode s));
|
7149
|
20 |
end;
|
|
21 |
|
|
22 |
|
2413
|
23 |
|
3594
|
24 |
(** ML system related **)
|
|
25 |
|
|
26 |
(* New Jersey ML parameters *)
|
|
27 |
|
|
28 |
System.Control.Runtime.gcmessages := 0;
|
|
29 |
|
|
30 |
|
|
31 |
(* Poly/ML emulation *)
|
2413
|
32 |
|
|
33 |
fun quit () = exit 0;
|
|
34 |
|
3594
|
35 |
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
|
|
36 |
fun print_depth n =
|
|
37 |
(System.Control.Print.printDepth := n div 2;
|
|
38 |
System.Control.Print.printLength := n);
|
|
39 |
|
|
40 |
|
4506
|
41 |
|
3594
|
42 |
(* timing *)
|
2413
|
43 |
|
4506
|
44 |
(*Note start point for timing*)
|
|
45 |
fun startTiming() = System.Timer.start_timer();
|
|
46 |
|
|
47 |
(*Finish timing and return string*)
|
3594
|
48 |
local
|
3631
|
49 |
(*print microseconds, suppressing trailing zeroes*)
|
|
50 |
fun umakestring 0 = ""
|
|
51 |
| umakestring n =
|
|
52 |
chr (ord "0" + n div 100000) ^ umakestring (10 * (n mod 100000));
|
4506
|
53 |
|
|
54 |
fun string_of_time (System.Timer.TIME {sec, usec}) =
|
|
55 |
makestring sec ^ "." ^ (if usec=0 then "0" else umakestring usec)
|
|
56 |
|
3594
|
57 |
in
|
4506
|
58 |
|
|
59 |
fun endTiming start =
|
|
60 |
let val nongc = System.Timer.check_timer(start)
|
|
61 |
and gc = System.Timer.check_timer_gc(start);
|
|
62 |
val both = System.Timer.add_time(nongc, gc)
|
|
63 |
in "Non GC " ^ string_of_time nongc ^
|
|
64 |
" GC " ^ string_of_time gc ^
|
|
65 |
" both "^ string_of_time both ^ " secs\n"
|
|
66 |
end
|
3594
|
67 |
end;
|
|
68 |
|
|
69 |
|
4977
|
70 |
(* prompts *)
|
|
71 |
|
|
72 |
fun ml_prompts p1 p2 = ();
|
4506
|
73 |
|
|
74 |
|
3594
|
75 |
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
|
2413
|
76 |
|
|
77 |
fun make_pp path pprint =
|
|
78 |
let
|
|
79 |
open System.PrettyPrint;
|
|
80 |
|
|
81 |
fun pp pps obj =
|
|
82 |
pprint obj
|
|
83 |
(add_string pps, begin_block pps INCONSISTENT,
|
|
84 |
fn wd => add_break pps (wd, 0), fn () => add_newline pps,
|
|
85 |
fn () => end_block pps);
|
|
86 |
in
|
|
87 |
(path, pp)
|
|
88 |
end;
|
|
89 |
|
|
90 |
fun install_pp (path, pp) = System.PrettyPrint.install_pp path pp;
|
|
91 |
|
|
92 |
|
3594
|
93 |
(* ML command execution *)
|
2413
|
94 |
|
7855
|
95 |
fun use_text _ _ = System.Compile.use_stream o open_string;
|
3594
|
96 |
|
2413
|
97 |
|
|
98 |
|
5816
|
99 |
(** interrupts **)
|
|
100 |
|
12988
|
101 |
exception Interrupt;
|
|
102 |
|
5816
|
103 |
local
|
|
104 |
|
13005
|
105 |
fun capture f x = ((f x): unit; NONE) handle exn => SOME exn;
|
5816
|
106 |
|
13005
|
107 |
fun release NONE = ()
|
|
108 |
| release (SOME exn) = raise exn;
|
5816
|
109 |
|
13005
|
110 |
structure Signals = System.Signals;
|
12992
|
111 |
|
5816
|
112 |
in
|
|
113 |
|
13005
|
114 |
fun ignore_interrupt f x =
|
|
115 |
let
|
|
116 |
val old_handler = Signals.inqHandler Signals.SIGINT;
|
|
117 |
val _ = Signals.setHandler (Signals.SIGINT, SOME #2);
|
|
118 |
val result = capture f x;
|
|
119 |
val _ = Signals.setHandler (Signals.SIGINT, old_handler);
|
|
120 |
in release result end;
|
5816
|
121 |
|
12988
|
122 |
fun raise_interrupt f x =
|
5816
|
123 |
let
|
|
124 |
val interrupted = ref false;
|
13005
|
125 |
val result = ref NONE;
|
|
126 |
val old_handler = Signals.inqHandler Signals.SIGINT;
|
5816
|
127 |
in
|
13005
|
128 |
callcc (fn cont =>
|
|
129 |
(Signals.setHandler (Signals.SIGINT, SOME (fn _ => (interrupted := true; cont)));
|
|
130 |
result := capture f x));
|
|
131 |
Signals.setHandler (Signals.SIGINT, old_handler);
|
|
132 |
if ! interrupted then raise Interrupt else release (! result)
|
|
133 |
end;
|
5816
|
134 |
|
|
135 |
end;
|
|
136 |
|
|
137 |
|
3594
|
138 |
(** OS related **)
|
2413
|
139 |
|
3594
|
140 |
(* system command execution *)
|
2413
|
141 |
|
3594
|
142 |
(*execute Unix command which doesn't take any input from stdin and
|
|
143 |
sends its output to stdout; could be done more easily by IO.execute,
|
|
144 |
but that function seems to be buggy in SML/NJ 0.93*)
|
|
145 |
fun execute command =
|
|
146 |
let
|
4494
|
147 |
val tmp_name = "/tmp/isabelle-execute";
|
3594
|
148 |
val is = (System.system (command ^ " > " ^ tmp_name); open_in tmp_name);
|
|
149 |
val result = input (is, 999999);
|
|
150 |
in
|
|
151 |
close_in is;
|
|
152 |
System.Unsafe.SysIO.unlink tmp_name;
|
|
153 |
result
|
|
154 |
end;
|
2413
|
155 |
|
7855
|
156 |
(*plain version; with return code*)
|
|
157 |
fun system cmd = System.system cmd div 256;
|
|
158 |
|
2413
|
159 |
|
3594
|
160 |
(* file handling *)
|
2413
|
161 |
|
6227
|
162 |
(*get time of last modification*)
|
2413
|
163 |
local
|
3594
|
164 |
open System.Timer System.Unsafe.SysIO;
|
2413
|
165 |
in
|
6227
|
166 |
fun file_info name = makestring (mtime (PATH name)) handle _ => "";
|
2413
|
167 |
end;
|
|
168 |
|
|
169 |
structure OS =
|
3594
|
170 |
struct
|
2413
|
171 |
structure FileSys =
|
3594
|
172 |
struct
|
|
173 |
val chDir = System.Directory.cd;
|
|
174 |
val remove = System.Unsafe.SysIO.unlink;
|
|
175 |
val getDir = System.Directory.getWD;
|
2413
|
176 |
end;
|
3594
|
177 |
end;
|
2413
|
178 |
|
3594
|
179 |
(*redefine to flush its output immediately -- temporary patch suggested
|
5816
|
180 |
by Kim Dam Petersen*) (* FIXME !? *)
|
3594
|
181 |
val output = fn (s, t) => (output (s, t); flush_out s);
|
2413
|
182 |
|
|
183 |
|
3594
|
184 |
(* getenv *)
|
2413
|
185 |
|
3594
|
186 |
local
|
|
187 |
fun drop_last [] = []
|
|
188 |
| drop_last [x] = []
|
|
189 |
| drop_last (x :: xs) = x :: drop_last xs;
|
2413
|
190 |
|
3594
|
191 |
val drop_last_char = implode o drop_last o explode;
|
|
192 |
in
|
|
193 |
fun getenv var = drop_last_char
|
|
194 |
(execute ("env | grep '^" ^ var ^ "=' | sed -e 's/" ^ var ^ "=//'"));
|
|
195 |
end;
|