author | wenzelm |
Mon, 24 Sep 2007 13:52:50 +0200 | |
changeset 24688 | a5754ca5c510 |
parent 24597 | cbf2c5cf335e |
child 24808 | 7c9a970d7ab5 |
permissions | -rw-r--r-- |
9254 | 1 |
(* Title: Pure/ML-Systems/mosml.ML |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 1999 University of Cambridge |
|
5 |
||
17491 | 6 |
Compatibility file for Moscow ML 2.01 |
9254 | 7 |
|
17491 | 8 |
NOTE: saving images does not work; may run it interactively as follows: |
9254 | 9 |
|
23826
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22252
diff
changeset
|
10 |
$ cd Isabelle/src/Pure |
9254 | 11 |
$ isabelle RAW_ML_SYSTEM |
12 |
> val ml_system = "mosml"; |
|
13 |
> use "ML-Systems/mosml.ML"; |
|
14 |
> use "ROOT.ML"; |
|
16470 | 15 |
> Session.finish (); |
23836 | 16 |
> cd "../FOL"; |
16470 | 17 |
> use "ROOT.ML"; |
9254 | 18 |
*) |
19 |
||
20 |
(** ML system related **) |
|
21 |
||
24597 | 22 |
val ml_system_fix_ints = false; |
23 |
||
16517 | 24 |
load "Obj"; |
9254 | 25 |
load "Bool"; |
26 |
load "Int"; |
|
27 |
load "Real"; |
|
28 |
load "ListPair"; |
|
29 |
load "OS"; |
|
30 |
load "Process"; |
|
31 |
load "FileSys"; |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
32 |
load "IO"; |
9254 | 33 |
|
23965
f93e509659c1
ML-Systems/exn.ML, ML-Systems/multithreading_dummy.ML;
wenzelm
parents:
23921
diff
changeset
|
34 |
use "ML-Systems/exn.ML"; |
24688
a5754ca5c510
replaced interrupt_timeout by TimeLimit.timeLimit (available on SML/NJ and Poly/ML 5.1);
wenzelm
parents:
24597
diff
changeset
|
35 |
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
|
36 |
use "ML-Systems/time_limit.ML"; |
23921
947152add153
added compatibility file for ML systems without multithreading;
wenzelm
parents:
23836
diff
changeset
|
37 |
|
947152add153
added compatibility file for ML systems without multithreading;
wenzelm
parents:
23836
diff
changeset
|
38 |
|
16517 | 39 |
(*low-level pointer equality*) |
40 |
local val cast : 'a -> int = Obj.magic |
|
41 |
in fun pointer_eq (x:'a, y:'a) = (cast x = cast y) end; |
|
42 |
||
16469 | 43 |
(*proper implementation available?*) |
44 |
structure IntInf = |
|
45 |
struct |
|
46 |
open Int; |
|
47 |
end; |
|
48 |
||
49 |
structure Real = |
|
50 |
struct |
|
51 |
open Real; |
|
52 |
val realFloor = real o floor; |
|
53 |
end; |
|
54 |
||
17491 | 55 |
structure String = |
56 |
struct |
|
57 |
fun isSuffix s1 s2 = |
|
58 |
let val n1 = size s1 and n2 = size s2 |
|
59 |
in if n1 = n2 then s1 = s2 else n1 <= n2 andalso String.substring (s2, n2 - n1, n1) = s1 end; |
|
60 |
open String; |
|
61 |
end; |
|
62 |
||
16469 | 63 |
structure Time = |
64 |
struct |
|
65 |
open Time; |
|
66 |
fun toString t = Time.toString t |
|
67 |
handle Overflow => Real.toString (Time.toReal t); (*workaround Y2004 bug*) |
|
68 |
end; |
|
69 |
||
9254 | 70 |
structure OS = |
16469 | 71 |
struct |
9254 | 72 |
open OS |
73 |
structure Process = Process |
|
74 |
structure FileSys = FileSys |
|
16469 | 75 |
end; |
16993 | 76 |
|
22252 | 77 |
exception Option = Option.Option; |
78 |
||
79 |
||
9254 | 80 |
(*limit the printing depth*) |
24329 | 81 |
local |
82 |
val depth = ref 10; |
|
83 |
in |
|
84 |
fun get_print_depth () = ! depth; |
|
85 |
fun print_depth n = |
|
86 |
(depth := n; |
|
87 |
Meta.printDepth := n div 2; |
|
88 |
Meta.printLength := n); |
|
89 |
end; |
|
9254 | 90 |
|
24290 | 91 |
(*interface for toplevel pretty printers, see also Pure/pure_setup.ML*) |
9254 | 92 |
(*the documentation refers to an installPP but I couldn't fine it!*) |
93 |
fun make_pp path pprint = (); |
|
94 |
fun install_pp _ = (); |
|
95 |
||
16681 | 96 |
(*dummy implementation*) |
97 |
fun ml_prompts p1 p2 = (); |
|
98 |
||
99 |
(*dummy implementation*) |
|
16660 | 100 |
fun profile (n: int) f x = f x; |
101 |
||
18384 | 102 |
(*dummy implementation*) |
16681 | 103 |
fun exception_trace f = f (); |
9254 | 104 |
|
18384 | 105 |
(*dummy implementation*) |
106 |
fun print x = x; |
|
107 |
||
9254 | 108 |
|
109 |
(** Compiler-independent timing functions **) |
|
110 |
||
111 |
load "Timer"; |
|
112 |
||
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
113 |
fun start_timing () = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
114 |
let val CPUtimer = Timer.startCPUTimer(); |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
115 |
val time = Timer.checkCPUTimer(CPUtimer) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
116 |
in (CPUtimer,time) end; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
117 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
118 |
fun end_timing (CPUtimer, {gc,sys,usr}) = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
119 |
let open Time (*...for Time.toString, Time.+ and Time.- *) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
120 |
val {gc=gc2,sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
121 |
in "User " ^ toString (usr2-usr) ^ |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
122 |
" GC " ^ toString (gc2-gc) ^ |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
123 |
" All "^ toString (sys2-sys + usr2-usr + gc2-gc) ^ |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
124 |
" secs" |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
125 |
handle Time => "" |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
126 |
end; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
127 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
128 |
fun check_timer timer = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
129 |
let val {sys, usr, gc} = Timer.checkCPUTimer timer |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
130 |
in (sys, usr, gc) end; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
131 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
132 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
133 |
(* SML basis library fixes *) |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
134 |
|
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
135 |
structure TextIO = |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
136 |
struct |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
137 |
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
|
138 |
open TextIO; |
23836 | 139 |
fun inputLine is = |
140 |
let val s = TextIO.inputLine is |
|
141 |
in if s = "" then NONE else SOME s end; |
|
21298
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
142 |
end; |
6d2306b2376d
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
21280
diff
changeset
|
143 |
|
9254 | 144 |
|
145 |
(* ML command execution *) |
|
146 |
||
147 |
(*Can one redirect 'use' directly to an instream?*) |
|
22144 | 148 |
fun use_text _ _ _ txt = |
9254 | 149 |
let |
150 |
val tmp_name = FileSys.tmpName (); |
|
151 |
val tmp_file = TextIO.openOut tmp_name; |
|
152 |
in |
|
153 |
TextIO.output (tmp_file, txt); |
|
154 |
TextIO.closeOut tmp_file; |
|
155 |
use tmp_name; |
|
156 |
FileSys.remove tmp_name |
|
157 |
end; |
|
158 |
||
22252 | 159 |
fun use_file _ _ name = use name; |
21770 | 160 |
|
9254 | 161 |
|
162 |
||
16993 | 163 |
(** interrupts **) (*Note: may get into race conditions*) |
9254 | 164 |
|
165 |
(* FIXME proper implementation available? *) |
|
166 |
||
167 |
exception Interrupt; |
|
168 |
||
16993 | 169 |
fun ignore_interrupt f x = f x; |
12988 | 170 |
fun raise_interrupt f x = f x; |
9254 | 171 |
|
172 |
||
173 |
||
174 |
(** OS related **) |
|
175 |
||
23826
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22252
diff
changeset
|
176 |
(* current directory *) |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22252
diff
changeset
|
177 |
|
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22252
diff
changeset
|
178 |
val cd = OS.FileSys.chDir; |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22252
diff
changeset
|
179 |
val pwd = OS.FileSys.getDir; |
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22252
diff
changeset
|
180 |
|
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22252
diff
changeset
|
181 |
|
9254 | 182 |
(* system command execution *) |
183 |
||
184 |
(*execute Unix command which doesn't take any input from stdin and |
|
185 |
sends its output to stdout; could be done more easily by Unix.execute, |
|
186 |
but that function doesn't use the PATH*) |
|
187 |
fun execute command = |
|
188 |
let |
|
189 |
val tmp_name = FileSys.tmpName (); |
|
16993 | 190 |
val is = (Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name); |
9254 | 191 |
val result = TextIO.inputAll is; |
192 |
in |
|
193 |
TextIO.closeIn is; |
|
194 |
FileSys.remove tmp_name; |
|
195 |
result |
|
196 |
end; |
|
197 |
||
198 |
(*plain version; with return code*) |
|
199 |
fun system cmd = |
|
200 |
if Process.system cmd = Process.success then 0 else 1; |
|
201 |
||
202 |
||
17824 | 203 |
val string_of_pid = Int.toString; |
204 |
||
205 |
||
9254 | 206 |
(* getenv *) |
207 |
||
208 |
fun getenv var = |
|
209 |
(case Process.getEnv var of |
|
210 |
NONE => "" |
|
211 |
| SOME txt => txt); |