author | haftmann |
Mon, 02 Oct 2006 23:00:51 +0200 | |
changeset 20835 | 27d049062b56 |
parent 18760 | 97aaecb84afe |
child 21280 | b4aa7daa506b |
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 |
|
16469 | 10 |
$ cd ~~/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 (); |
16 |
> cd "../HOL"; |
|
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"; |
|
30 |
||
16517 | 31 |
(*low-level pointer equality*) |
32 |
local val cast : 'a -> int = Obj.magic |
|
33 |
in fun pointer_eq (x:'a, y:'a) = (cast x = cast y) end; |
|
34 |
||
16469 | 35 |
(*proper implementation available?*) |
36 |
structure IntInf = |
|
37 |
struct |
|
38 |
open Int; |
|
39 |
end; |
|
40 |
||
41 |
structure Real = |
|
42 |
struct |
|
43 |
open Real; |
|
44 |
val realFloor = real o floor; |
|
45 |
end; |
|
46 |
||
17491 | 47 |
structure String = |
48 |
struct |
|
49 |
fun isSuffix s1 s2 = |
|
50 |
let val n1 = size s1 and n2 = size s2 |
|
51 |
in if n1 = n2 then s1 = s2 else n1 <= n2 andalso String.substring (s2, n2 - n1, n1) = s1 end; |
|
52 |
open String; |
|
53 |
end; |
|
54 |
||
16469 | 55 |
structure Time = |
56 |
struct |
|
57 |
open Time; |
|
58 |
fun toString t = Time.toString t |
|
59 |
handle Overflow => Real.toString (Time.toReal t); (*workaround Y2004 bug*) |
|
60 |
end; |
|
61 |
||
9254 | 62 |
structure OS = |
16469 | 63 |
struct |
9254 | 64 |
open OS |
65 |
structure Process = Process |
|
66 |
structure FileSys = FileSys |
|
16469 | 67 |
end; |
16993 | 68 |
|
9254 | 69 |
(*limit the printing depth*) |
70 |
fun print_depth n = |
|
71 |
(Meta.printDepth := n div 2; |
|
72 |
Meta.printLength := n); |
|
73 |
||
74 |
(*interface for toplevel pretty printers, see also Pure/install_pp.ML*) |
|
75 |
(*the documentation refers to an installPP but I couldn't fine it!*) |
|
76 |
fun make_pp path pprint = (); |
|
77 |
fun install_pp _ = (); |
|
78 |
||
16681 | 79 |
(*dummy implementation*) |
80 |
fun ml_prompts p1 p2 = (); |
|
81 |
||
82 |
(*dummy implementation*) |
|
16660 | 83 |
fun profile (n: int) f x = f x; |
84 |
||
18384 | 85 |
(*dummy implementation*) |
16681 | 86 |
fun exception_trace f = f (); |
9254 | 87 |
|
18384 | 88 |
(*dummy implementation*) |
89 |
fun print x = x; |
|
90 |
||
9254 | 91 |
|
92 |
(** Compiler-independent timing functions **) |
|
93 |
||
94 |
load "Timer"; |
|
95 |
||
14519
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12988
diff
changeset
|
96 |
use "ML-Systems/cpu-timer-gc.ML"; |
9254 | 97 |
|
14851
0fc75361e0c7
TimeLimit structure added (no proper implementation yet)
webertj
parents:
14519
diff
changeset
|
98 |
(* bounded time execution *) |
0fc75361e0c7
TimeLimit structure added (no proper implementation yet)
webertj
parents:
14519
diff
changeset
|
99 |
|
0fc75361e0c7
TimeLimit structure added (no proper implementation yet)
webertj
parents:
14519
diff
changeset
|
100 |
(* FIXME proper implementation available? *) |
18760 | 101 |
fun interrupt_timeout time f x = |
102 |
f x; |
|
16517 | 103 |
|
9254 | 104 |
(* ML command execution *) |
105 |
||
106 |
(*Can one redirect 'use' directly to an instream?*) |
|
107 |
fun use_text _ _ txt = |
|
108 |
let |
|
109 |
val tmp_name = FileSys.tmpName (); |
|
110 |
val tmp_file = TextIO.openOut tmp_name; |
|
111 |
in |
|
112 |
TextIO.output (tmp_file, txt); |
|
113 |
TextIO.closeOut tmp_file; |
|
114 |
use tmp_name; |
|
115 |
FileSys.remove tmp_name |
|
116 |
end; |
|
117 |
||
118 |
||
119 |
||
16993 | 120 |
(** interrupts **) (*Note: may get into race conditions*) |
9254 | 121 |
|
122 |
(* FIXME proper implementation available? *) |
|
123 |
||
124 |
exception Interrupt; |
|
125 |
||
16993 | 126 |
fun ignore_interrupt f x = f x; |
12988 | 127 |
fun raise_interrupt f x = f x; |
9254 | 128 |
|
129 |
||
130 |
||
131 |
(** OS related **) |
|
132 |
||
133 |
(* system command execution *) |
|
134 |
||
135 |
(*execute Unix command which doesn't take any input from stdin and |
|
136 |
sends its output to stdout; could be done more easily by Unix.execute, |
|
137 |
but that function doesn't use the PATH*) |
|
138 |
fun execute command = |
|
139 |
let |
|
140 |
val tmp_name = FileSys.tmpName (); |
|
16993 | 141 |
val is = (Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name); |
9254 | 142 |
val result = TextIO.inputAll is; |
143 |
in |
|
144 |
TextIO.closeIn is; |
|
145 |
FileSys.remove tmp_name; |
|
146 |
result |
|
147 |
end; |
|
148 |
||
149 |
(*plain version; with return code*) |
|
150 |
fun system cmd = |
|
151 |
if Process.system cmd = Process.success then 0 else 1; |
|
152 |
||
153 |
||
17824 | 154 |
val string_of_pid = Int.toString; |
155 |
||
156 |
||
9254 | 157 |
(* getenv *) |
158 |
||
159 |
fun getenv var = |
|
160 |
(case Process.getEnv var of |
|
161 |
NONE => "" |
|
162 |
| SOME txt => txt); |