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