39348
|
1 |
(* ========================================================================= *)
|
|
2 |
(* MOSCOW ML SPECIFIC FUNCTIONS *)
|
|
3 |
(* Copyright (c) 2002 Joe Hurd, distributed under the GNU GPL version 2 *)
|
|
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 |
(* ------------------------------------------------------------------------- *)
|
|
26 |
(* Timing function applications. *)
|
|
27 |
(* ------------------------------------------------------------------------- *)
|
|
28 |
|
|
29 |
val time = Mosml.time;
|
|
30 |
|
|
31 |
(* ------------------------------------------------------------------------- *)
|
|
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 |
|
|
53 |
end
|
|
54 |
|
|
55 |
(* ------------------------------------------------------------------------- *)
|
|
56 |
(* Ensuring that interruptions (SIGINTs) are actually seen by the *)
|
|
57 |
(* linked executable as Interrupt exceptions. *)
|
|
58 |
(* ------------------------------------------------------------------------- *)
|
|
59 |
|
|
60 |
prim_val catch_interrupt : bool -> unit = 1 "sys_catch_break";
|
|
61 |
val _ = catch_interrupt true;
|
|
62 |
|
|
63 |
(* ------------------------------------------------------------------------- *)
|
|
64 |
(* Ad-hoc upgrading of the Moscow ML basis library. *)
|
|
65 |
(* ------------------------------------------------------------------------- *)
|
|
66 |
|
|
67 |
fun Array_copy {src,dst,di} =
|
|
68 |
let
|
|
69 |
open Array
|
|
70 |
in
|
|
71 |
copy {src = src, si = 0, len = NONE, dst = dst, di = di}
|
|
72 |
end;
|
|
73 |
|
|
74 |
fun Array_foldli f b v =
|
|
75 |
let
|
|
76 |
open Array
|
|
77 |
in
|
|
78 |
foldli f b (v,0,NONE)
|
|
79 |
end;
|
|
80 |
|
|
81 |
fun Array_foldri f b v =
|
|
82 |
let
|
|
83 |
open Array
|
|
84 |
in
|
|
85 |
foldri f b (v,0,NONE)
|
|
86 |
end;
|
|
87 |
|
|
88 |
fun Array_modifyi f a =
|
|
89 |
let
|
|
90 |
open Array
|
|
91 |
in
|
|
92 |
modifyi f (a,0,NONE)
|
|
93 |
end;
|
|
94 |
|
|
95 |
fun OS_Process_isSuccess s = s = OS.Process.success;
|
|
96 |
|
|
97 |
fun String_isSuffix p s =
|
|
98 |
let
|
|
99 |
val sizeP = size p
|
|
100 |
and sizeS = size s
|
|
101 |
in
|
|
102 |
sizeP <= sizeS andalso
|
|
103 |
String.extract (s, sizeS - sizeP, NONE) = p
|
|
104 |
end;
|
|
105 |
|
|
106 |
fun Substring_full s =
|
|
107 |
let
|
|
108 |
open Substring
|
|
109 |
in
|
|
110 |
all s
|
|
111 |
end;
|
|
112 |
|
|
113 |
fun TextIO_inputLine h =
|
|
114 |
let
|
|
115 |
open TextIO
|
|
116 |
in
|
|
117 |
case inputLine h of "" => NONE | s => SOME s
|
|
118 |
end;
|
|
119 |
|
|
120 |
fun Vector_foldli f b v =
|
|
121 |
let
|
|
122 |
open Vector
|
|
123 |
in
|
|
124 |
foldli f b (v,0,NONE)
|
|
125 |
end;
|
|
126 |
|
|
127 |
fun Vector_mapi f v =
|
|
128 |
let
|
|
129 |
open Vector
|
|
130 |
in
|
|
131 |
mapi f (v,0,NONE)
|
|
132 |
end;
|