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