| author | wenzelm |
| Sun, 18 Sep 2011 13:39:33 +0200 | |
| changeset 44962 | 5554ed48b13f |
| parent 42102 | fcfd07f122d4 |
| child 45778 | df6e210fb44c |
| permissions | -rw-r--r-- |
| 39348 | 1 |
(* ========================================================================= *) |
2 |
(* MOSCOW ML SPECIFIC FUNCTIONS *) |
|
| 39502 | 3 |
(* Copyright (c) 2002 Joe Hurd, distributed under the BSD License *) |
| 39348 | 4 |
(* ========================================================================= *) |
5 |
||
6 |
structure Portable :> Portable = |
|
7 |
struct |
|
8 |
||
9 |
(* ------------------------------------------------------------------------- *) |
|
10 |
(* The ML implementation. *) |
|
11 |
(* ------------------------------------------------------------------------- *) |
|
12 |
||
13 |
val ml = "mosml"; |
|
14 |
||
15 |
(* ------------------------------------------------------------------------- *) |
|
16 |
(* Pointer equality using the run-time system. *) |
|
17 |
(* ------------------------------------------------------------------------- *) |
|
18 |
||
19 |
local |
|
20 |
val address : 'a -> int = Obj.magic |
|
21 |
in |
|
22 |
fun pointerEqual (x : 'a, y : 'a) = address x = address y |
|
23 |
end; |
|
24 |
||
25 |
(* ------------------------------------------------------------------------- *) |
|
|
39443
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
26 |
(* Marking critical sections of code. *) |
| 39348 | 27 |
(* ------------------------------------------------------------------------- *) |
28 |
||
|
39443
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
29 |
fun critical f () = f (); |
|
39429
126b879df319
move "CRITICAL" to "PortableXxx", where it belongs and used to be;
blanchet
parents:
39353
diff
changeset
|
30 |
|
|
126b879df319
move "CRITICAL" to "PortableXxx", where it belongs and used to be;
blanchet
parents:
39353
diff
changeset
|
31 |
(* ------------------------------------------------------------------------- *) |
| 39348 | 32 |
(* Generating random values. *) |
33 |
(* ------------------------------------------------------------------------- *) |
|
34 |
||
35 |
local |
|
36 |
val gen = Random.newgenseed 1.0; |
|
37 |
in |
|
38 |
fun randomInt max = Random.range (0,max) gen; |
|
39 |
||
40 |
fun randomReal () = Random.random gen; |
|
41 |
end; |
|
42 |
||
43 |
fun randomBool () = randomInt 2 = 0; |
|
44 |
||
45 |
fun randomWord () = |
|
46 |
let |
|
47 |
val h = Word.fromInt (randomInt 65536) |
|
48 |
and l = Word.fromInt (randomInt 65536) |
|
49 |
in |
|
50 |
Word.orb (Word.<< (h,0w16), l) |
|
51 |
end; |
|
52 |
||
|
39443
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
53 |
(* ------------------------------------------------------------------------- *) |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
54 |
(* Timing function applications. *) |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
55 |
(* ------------------------------------------------------------------------- *) |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
56 |
|
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
57 |
val time = Mosml.time; |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
58 |
|
| 39348 | 59 |
end |
60 |
||
61 |
(* ------------------------------------------------------------------------- *) |
|
62 |
(* Ensuring that interruptions (SIGINTs) are actually seen by the *) |
|
63 |
(* linked executable as Interrupt exceptions. *) |
|
64 |
(* ------------------------------------------------------------------------- *) |
|
65 |
||
66 |
prim_val catch_interrupt : bool -> unit = 1 "sys_catch_break"; |
|
67 |
val _ = catch_interrupt true; |
|
68 |
||
69 |
(* ------------------------------------------------------------------------- *) |
|
| 42102 | 70 |
(* Forcing fully qualified names of functions with generic names. *) |
|
39443
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
71 |
(* ------------------------------------------------------------------------- *) |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
72 |
|
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
73 |
(*BasicDebug |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
74 |
val explode = () |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
75 |
and foldl = () |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
76 |
and foldr = () |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
77 |
and implode = () |
| 42102 | 78 |
and map = () |
79 |
and null = () |
|
|
39443
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
80 |
and print = (); |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
81 |
*) |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
82 |
|
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
83 |
(* ------------------------------------------------------------------------- *) |
| 39348 | 84 |
(* Ad-hoc upgrading of the Moscow ML basis library. *) |
85 |
(* ------------------------------------------------------------------------- *) |
|
86 |
||
87 |
fun Array_copy {src,dst,di} =
|
|
88 |
let |
|
89 |
open Array |
|
90 |
in |
|
91 |
copy {src = src, si = 0, len = NONE, dst = dst, di = di}
|
|
92 |
end; |
|
93 |
||
94 |
fun Array_foldli f b v = |
|
95 |
let |
|
96 |
open Array |
|
97 |
in |
|
98 |
foldli f b (v,0,NONE) |
|
99 |
end; |
|
100 |
||
101 |
fun Array_foldri f b v = |
|
102 |
let |
|
103 |
open Array |
|
104 |
in |
|
105 |
foldri f b (v,0,NONE) |
|
106 |
end; |
|
107 |
||
108 |
fun Array_modifyi f a = |
|
109 |
let |
|
110 |
open Array |
|
111 |
in |
|
112 |
modifyi f (a,0,NONE) |
|
113 |
end; |
|
114 |
||
115 |
fun OS_Process_isSuccess s = s = OS.Process.success; |
|
116 |
||
| 42102 | 117 |
fun String_concatWith s = |
118 |
let |
|
119 |
fun add (x,l) = s :: x :: l |
|
120 |
in |
|
121 |
fn [] => "" |
|
122 |
| x :: xs => |
|
123 |
let |
|
124 |
val xs = List.foldl add [] (rev xs) |
|
125 |
in |
|
126 |
String.concat (x :: xs) |
|
127 |
end |
|
128 |
end; |
|
|
39443
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
129 |
|
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
130 |
fun String_isSubstring p s = |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
131 |
let |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
132 |
val sizeP = size p |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
133 |
and sizeS = size s |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
134 |
in |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
135 |
if sizeP > sizeS then false |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
136 |
else if sizeP = sizeS then p = s |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
137 |
else |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
138 |
let |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
139 |
fun check i = String.substring (s,i,sizeP) = p |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
140 |
|
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
141 |
fun checkn i = check i orelse (i > 0 andalso checkn (i - 1)) |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
142 |
in |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
143 |
checkn (sizeS - sizeP) |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
144 |
end |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
145 |
end; |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
146 |
|
| 39348 | 147 |
fun String_isSuffix p s = |
148 |
let |
|
149 |
val sizeP = size p |
|
150 |
and sizeS = size s |
|
151 |
in |
|
152 |
sizeP <= sizeS andalso |
|
153 |
String.extract (s, sizeS - sizeP, NONE) = p |
|
154 |
end; |
|
155 |
||
156 |
fun Substring_full s = |
|
157 |
let |
|
158 |
open Substring |
|
159 |
in |
|
160 |
all s |
|
161 |
end; |
|
162 |
||
163 |
fun TextIO_inputLine h = |
|
164 |
let |
|
165 |
open TextIO |
|
166 |
in |
|
167 |
case inputLine h of "" => NONE | s => SOME s |
|
168 |
end; |
|
169 |
||
170 |
fun Vector_foldli f b v = |
|
171 |
let |
|
172 |
open Vector |
|
173 |
in |
|
174 |
foldli f b (v,0,NONE) |
|
175 |
end; |
|
176 |
||
177 |
fun Vector_mapi f v = |
|
178 |
let |
|
179 |
open Vector |
|
180 |
in |
|
181 |
mapi f (v,0,NONE) |
|
182 |
end; |