author | wenzelm |
Sat, 21 Mar 2009 15:08:00 +0100 | |
changeset 30619 | 0226c07352db |
parent 30187 | b92b3375e919 |
child 30625 | d53d1a16d5ee |
permissions | -rw-r--r-- |
9254 | 1 |
(* Title: Pure/ML-Systems/mosml.ML |
2 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
29284 | 3 |
Author: Makarius |
9254 | 4 |
|
17491 | 5 |
Compatibility file for Moscow ML 2.01 |
9254 | 6 |
|
17491 | 7 |
NOTE: saving images does not work; may run it interactively as follows: |
9254 | 8 |
|
23826
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22252
diff
changeset
|
9 |
$ cd Isabelle/src/Pure |
29284 | 10 |
$ isabelle-process RAW_ML_SYSTEM |
9254 | 11 |
> val ml_system = "mosml"; |
12 |
> use "ML-Systems/mosml.ML"; |
|
13 |
> use "ROOT.ML"; |
|
16470 | 14 |
> Session.finish (); |
23836 | 15 |
> cd "../FOL"; |
16470 | 16 |
> use "ROOT.ML"; |
24808 | 17 |
> Session.finish (); |
18 |
> cd "../ZF"; |
|
19 |
> use "ROOT.ML"; |
|
9254 | 20 |
*) |
21 |
||
22 |
(** ML system related **) |
|
23 |
||
24597 | 24 |
val ml_system_fix_ints = false; |
25 |
||
26474 | 26 |
fun forget_structure _ = (); |
27 |
||
16517 | 28 |
load "Obj"; |
29284 | 29 |
load "Word"; |
9254 | 30 |
load "Bool"; |
31 |
load "Int"; |
|
32 |
load "Real"; |
|
33 |
load "ListPair"; |
|
34 |
load "OS"; |
|
35 |
load "Process"; |
|
36 |
load "FileSys"; |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
37 |
load "IO"; |
29285 | 38 |
load "CharVector"; |
9254 | 39 |
|
28443 | 40 |
exception Interrupt; |
41 |
||
23965
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents:
23921
diff
changeset
|
42 |
use "ML-Systems/exn.ML"; |
25732 | 43 |
use "ML-Systems/universal.ML"; |
28489
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
44 |
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:
24597
diff
changeset
|
45 |
use "ML-Systems/multithreading.ML"; |
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24597
diff
changeset
|
46 |
use "ML-Systems/time_limit.ML"; |
28268
ac8431ecd57e
use_text/use_file now depend on explicit ML name space;
wenzelm
parents:
27003
diff
changeset
|
47 |
use "ML-Systems/ml_name_space.ML"; |
30619 | 48 |
use "ML-Systems/ml_pretty.ML"; |
23921
947152add153
added compatibility file for ML systems without multithreading;
wenzelm
parents:
23836
diff
changeset
|
49 |
|
947152add153
added compatibility file for ML systems without multithreading;
wenzelm
parents:
23836
diff
changeset
|
50 |
|
16517 | 51 |
(*low-level pointer equality*) |
52 |
local val cast : 'a -> int = Obj.magic |
|
53 |
in fun pointer_eq (x:'a, y:'a) = (cast x = cast y) end; |
|
54 |
||
16469 | 55 |
(*proper implementation available?*) |
56 |
structure IntInf = |
|
57 |
struct |
|
24808 | 58 |
fun divMod (x, y) = (x div y, x mod y); |
29284 | 59 |
|
60 |
local |
|
61 |
fun log 1 a = a |
|
62 |
| log n a = log (n div 2) (a + 1); |
|
63 |
in |
|
64 |
fun log2 n = if n > 0 then log n 0 else raise Domain; |
|
65 |
end; |
|
66 |
||
16469 | 67 |
open Int; |
68 |
end; |
|
69 |
||
27003 | 70 |
structure Substring = |
71 |
struct |
|
72 |
open Substring; |
|
73 |
val full = all; |
|
74 |
end; |
|
75 |
||
16469 | 76 |
structure Real = |
77 |
struct |
|
78 |
open Real; |
|
79 |
val realFloor = real o floor; |
|
80 |
end; |
|
81 |
||
17491 | 82 |
structure String = |
83 |
struct |
|
84 |
fun isSuffix s1 s2 = |
|
85 |
let val n1 = size s1 and n2 = size s2 |
|
86 |
in if n1 = n2 then s1 = s2 else n1 <= n2 andalso String.substring (s2, n2 - n1, n1) = s1 end; |
|
87 |
open String; |
|
88 |
end; |
|
89 |
||
16469 | 90 |
structure Time = |
91 |
struct |
|
92 |
open Time; |
|
93 |
fun toString t = Time.toString t |
|
94 |
handle Overflow => Real.toString (Time.toReal t); (*workaround Y2004 bug*) |
|
95 |
end; |
|
96 |
||
9254 | 97 |
structure OS = |
16469 | 98 |
struct |
9254 | 99 |
open OS |
28489
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
100 |
structure Process = |
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
101 |
struct |
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
102 |
open Process |
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
103 |
fun sleep _ = raise Fail "OS.Process.sleep undefined" |
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
104 |
end; |
9254 | 105 |
structure FileSys = FileSys |
16469 | 106 |
end; |
16993 | 107 |
|
22252 | 108 |
exception Option = Option.Option; |
109 |
||
110 |
||
9254 | 111 |
(*limit the printing depth*) |
24329 | 112 |
local |
113 |
val depth = ref 10; |
|
114 |
in |
|
115 |
fun get_print_depth () = ! depth; |
|
116 |
fun print_depth n = |
|
117 |
(depth := n; |
|
118 |
Meta.printDepth := n div 2; |
|
119 |
Meta.printLength := n); |
|
120 |
end; |
|
9254 | 121 |
|
24290 | 122 |
(*interface for toplevel pretty printers, see also Pure/pure_setup.ML*) |
9254 | 123 |
(*the documentation refers to an installPP but I couldn't fine it!*) |
124 |
fun make_pp path pprint = (); |
|
125 |
fun install_pp _ = (); |
|
126 |
||
16681 | 127 |
(*dummy implementation*) |
128 |
fun ml_prompts p1 p2 = (); |
|
129 |
||
130 |
(*dummy implementation*) |
|
16660 | 131 |
fun profile (n: int) f x = f x; |
132 |
||
18384 | 133 |
(*dummy implementation*) |
16681 | 134 |
fun exception_trace f = f (); |
9254 | 135 |
|
18384 | 136 |
(*dummy implementation*) |
137 |
fun print x = x; |
|
138 |
||
9254 | 139 |
|
140 |
(** Compiler-independent timing functions **) |
|
141 |
||
142 |
load "Timer"; |
|
143 |
||
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
144 |
fun start_timing () = |
30187
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29285
diff
changeset
|
145 |
let |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29285
diff
changeset
|
146 |
val timer = Timer.startCPUTimer (); |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29285
diff
changeset
|
147 |
val time = Timer.checkCPUTimer timer; |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29285
diff
changeset
|
148 |
in (timer, time) end; |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
149 |
|
30187
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29285
diff
changeset
|
150 |
fun end_timing (timer, {gc, sys, usr}) = |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29285
diff
changeset
|
151 |
let |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29285
diff
changeset
|
152 |
open Time; (*...for Time.toString, Time.+ and Time.- *) |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29285
diff
changeset
|
153 |
val {gc = gc2, sys = sys2, usr = usr2} = Timer.checkCPUTimer timer; |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29285
diff
changeset
|
154 |
val user = usr2 - usr + gc2 - gc; |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29285
diff
changeset
|
155 |
val all = user + sys2 - sys; |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29285
diff
changeset
|
156 |
val message = "User " ^ toString user ^ " All "^ toString all ^ " secs" handle Time => ""; |
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29285
diff
changeset
|
157 |
in {message = message, user = user, all = all} end; |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
158 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
159 |
fun check_timer timer = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
160 |
let val {sys, usr, gc} = Timer.checkCPUTimer timer |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
161 |
in (sys, usr, gc) end; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
162 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
163 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
164 |
(* SML basis library fixes *) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
165 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
166 |
structure TextIO = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
167 |
struct |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
168 |
fun canInput _ = raise IO.Io {cause = Fail "undefined", function = "canInput", name = ""}; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
169 |
open TextIO; |
23836 | 170 |
fun inputLine is = |
171 |
let val s = TextIO.inputLine is |
|
172 |
in if s = "" then NONE else SOME s end; |
|
28489
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
173 |
fun getOutstream _ = (); |
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
174 |
structure StreamIO = |
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
175 |
struct |
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
176 |
fun setBufferMode _ = (); |
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
177 |
end |
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
178 |
end; |
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
179 |
|
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
180 |
structure IO = |
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
181 |
struct |
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
182 |
open IO; |
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
183 |
val BLOCK_BUF = (); |
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
184 |
end; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
185 |
|
9254 | 186 |
|
187 |
(* ML command execution *) |
|
188 |
||
189 |
(*Can one redirect 'use' directly to an instream?*) |
|
28268
ac8431ecd57e
use_text/use_file now depend on explicit ML name space;
wenzelm
parents:
27003
diff
changeset
|
190 |
fun use_text (tune: string -> string) _ _ _ _ _ txt = |
9254 | 191 |
let |
192 |
val tmp_name = FileSys.tmpName (); |
|
193 |
val tmp_file = TextIO.openOut tmp_name; |
|
194 |
in |
|
24808 | 195 |
TextIO.output (tmp_file, tune txt); |
9254 | 196 |
TextIO.closeOut tmp_file; |
197 |
use tmp_name; |
|
198 |
FileSys.remove tmp_name |
|
199 |
end; |
|
200 |
||
28268
ac8431ecd57e
use_text/use_file now depend on explicit ML name space;
wenzelm
parents:
27003
diff
changeset
|
201 |
fun use_file _ _ _ _ _ name = use name; |
21770 | 202 |
|
9254 | 203 |
|
204 |
||
16993 | 205 |
(** interrupts **) (*Note: may get into race conditions*) |
9254 | 206 |
|
207 |
(* FIXME proper implementation available? *) |
|
208 |
||
26084
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
209 |
fun interruptible f x = f x; |
a7475459c740
replaced ignore/raise_interrupt by more flexible (un)interruptible combinators;
wenzelm
parents:
25732
diff
changeset
|
210 |
fun uninterruptible f x = f (fn (g: 'c -> 'd) => g) x; |
9254 | 211 |
|
212 |
||
213 |
||
214 |
(** OS related **) |
|
215 |
||
26223
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
216 |
(*dummy implementation*) |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
217 |
structure Posix = |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
218 |
struct |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
219 |
structure ProcEnv = |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
220 |
struct |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
221 |
fun getpid () = 0; |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
222 |
end; |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
223 |
end; |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
224 |
|
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
225 |
local |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
226 |
|
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
227 |
fun read_file name = |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
228 |
let val is = TextIO.openIn name |
26504 | 229 |
in Exn.release (Exn.capture TextIO.inputAll is before TextIO.closeIn is) end; |
26223
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
230 |
|
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
231 |
fun write_file name txt = |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
232 |
let val os = TextIO.openOut name |
26504 | 233 |
in Exn.release (Exn.capture TextIO.output (os, txt) before TextIO.closeOut os) end; |
26223
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
234 |
|
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
235 |
in |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
236 |
|
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
237 |
fun system_out script = |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
238 |
let |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
239 |
val script_name = OS.FileSys.tmpName (); |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
240 |
val _ = write_file script_name script; |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
241 |
|
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
242 |
val output_name = OS.FileSys.tmpName (); |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
243 |
|
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
244 |
val status = |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
245 |
OS.Process.system ("perl -w \"$ISABELLE_HOME/lib/scripts/system.pl\" nogroup " ^ |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
246 |
script_name ^ " /dev/null " ^ output_name); |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
247 |
val rc = if status = OS.Process.success then 0 else 1; |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
248 |
|
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
249 |
val output = read_file output_name handle IO.Io _ => ""; |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
250 |
val _ = OS.FileSys.remove script_name handle OS.SysErr _ => (); |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
251 |
val _ = OS.FileSys.remove output_name handle OS.SysErr _ => (); |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
252 |
in (output, rc) end; |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
253 |
|
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
254 |
end; |
f4a1a96cc07c
specific system_out (MosML lacks structure Posix);
wenzelm
parents:
26220
diff
changeset
|
255 |
|
23826
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22252
diff
changeset
|
256 |
val cd = OS.FileSys.chDir; |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22252
diff
changeset
|
257 |
val pwd = OS.FileSys.getDir; |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22252
diff
changeset
|
258 |
|
28489
404649964f09
removed obsolete Posix/Signal compatibility wrappers;
wenzelm
parents:
28443
diff
changeset
|
259 |
val process_id = Int.toString o Posix.ProcEnv.getpid; |
17824 | 260 |
|
9254 | 261 |
fun getenv var = |
262 |
(case Process.getEnv var of |
|
263 |
NONE => "" |
|
264 |
| SOME txt => txt); |