author | wenzelm |
Sat, 07 Apr 2012 16:41:59 +0200 | |
changeset 47389 | e8552cba702d |
parent 45778 | df6e210fb44c |
child 72004 | 913162a47d9f |
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 = () |
|
45778 | 80 |
and print = () |
81 |
and rev = (); |
|
39443
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 |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
84 |
(* ------------------------------------------------------------------------- *) |
39348 | 85 |
(* Ad-hoc upgrading of the Moscow ML basis library. *) |
86 |
(* ------------------------------------------------------------------------- *) |
|
87 |
||
88 |
fun Array_copy {src,dst,di} = |
|
89 |
let |
|
90 |
open Array |
|
91 |
in |
|
92 |
copy {src = src, si = 0, len = NONE, dst = dst, di = di} |
|
93 |
end; |
|
94 |
||
95 |
fun Array_foldli f b v = |
|
96 |
let |
|
97 |
open Array |
|
98 |
in |
|
99 |
foldli f b (v,0,NONE) |
|
100 |
end; |
|
101 |
||
102 |
fun Array_foldri f b v = |
|
103 |
let |
|
104 |
open Array |
|
105 |
in |
|
106 |
foldri f b (v,0,NONE) |
|
107 |
end; |
|
108 |
||
109 |
fun Array_modifyi f a = |
|
110 |
let |
|
111 |
open Array |
|
112 |
in |
|
113 |
modifyi f (a,0,NONE) |
|
114 |
end; |
|
115 |
||
116 |
fun OS_Process_isSuccess s = s = OS.Process.success; |
|
117 |
||
42102 | 118 |
fun String_concatWith s = |
119 |
let |
|
120 |
fun add (x,l) = s :: x :: l |
|
121 |
in |
|
122 |
fn [] => "" |
|
123 |
| x :: xs => |
|
124 |
let |
|
45778 | 125 |
val xs = List.foldl add [] (List.rev xs) |
42102 | 126 |
in |
127 |
String.concat (x :: xs) |
|
128 |
end |
|
129 |
end; |
|
39443
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
130 |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
131 |
fun String_isSubstring p s = |
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
132 |
let |
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
133 |
val sizeP = size p |
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
134 |
and sizeS = size s |
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
135 |
in |
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
136 |
if sizeP > sizeS then false |
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
137 |
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
|
138 |
else |
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
139 |
let |
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
140 |
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
|
141 |
|
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
142 |
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
|
143 |
in |
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
144 |
checkn (sizeS - sizeP) |
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 |
end; |
e330437cd22a
copied the unmodified official Metis 2.3 (15 Sept. 2010) sources into Isabelle
blanchet
parents:
39429
diff
changeset
|
147 |
|
39348 | 148 |
fun String_isSuffix p s = |
149 |
let |
|
150 |
val sizeP = size p |
|
151 |
and sizeS = size s |
|
152 |
in |
|
153 |
sizeP <= sizeS andalso |
|
154 |
String.extract (s, sizeS - sizeP, NONE) = p |
|
155 |
end; |
|
156 |
||
157 |
fun Substring_full s = |
|
158 |
let |
|
159 |
open Substring |
|
160 |
in |
|
161 |
all s |
|
162 |
end; |
|
163 |
||
164 |
fun TextIO_inputLine h = |
|
165 |
let |
|
166 |
open TextIO |
|
167 |
in |
|
168 |
case inputLine h of "" => NONE | s => SOME s |
|
169 |
end; |
|
170 |
||
171 |
fun Vector_foldli f b v = |
|
172 |
let |
|
173 |
open Vector |
|
174 |
in |
|
175 |
foldli f b (v,0,NONE) |
|
176 |
end; |
|
177 |
||
178 |
fun Vector_mapi f v = |
|
179 |
let |
|
180 |
open Vector |
|
181 |
in |
|
182 |
mapi f (v,0,NONE) |
|
183 |
end; |