39348
|
1 |
(* ========================================================================= *)
|
|
2 |
(* MLTON SPECIFIC FUNCTIONS *)
|
|
3 |
(* Copyright (c) 2002-2006 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 = "mlton";
|
|
14 |
|
|
15 |
(* ------------------------------------------------------------------------- *)
|
|
16 |
(* Pointer equality using the run-time system. *)
|
|
17 |
(* ------------------------------------------------------------------------- *)
|
|
18 |
|
|
19 |
val pointerEqual = MLton.eq;
|
|
20 |
|
|
21 |
(* ------------------------------------------------------------------------- *)
|
|
22 |
(* Timing function applications. *)
|
|
23 |
(* ------------------------------------------------------------------------- *)
|
|
24 |
|
|
25 |
fun time f x =
|
|
26 |
let
|
|
27 |
fun p t =
|
|
28 |
let
|
|
29 |
val s = Time.fmt 3 t
|
|
30 |
in
|
|
31 |
case size (List.last (String.fields (fn x => x = #".") s)) of
|
|
32 |
3 => s
|
|
33 |
| 2 => s ^ "0"
|
|
34 |
| 1 => s ^ "00"
|
|
35 |
| _ => raise Fail "Portable.time"
|
|
36 |
end
|
|
37 |
|
|
38 |
val c = Timer.startCPUTimer ()
|
|
39 |
|
|
40 |
val r = Timer.startRealTimer ()
|
|
41 |
|
|
42 |
fun pt () =
|
|
43 |
let
|
|
44 |
val {usr,sys} = Timer.checkCPUTimer c
|
|
45 |
val real = Timer.checkRealTimer r
|
|
46 |
in
|
|
47 |
print
|
|
48 |
("User: " ^ p usr ^ " System: " ^ p sys ^
|
|
49 |
" Real: " ^ p real ^ "\n")
|
|
50 |
end
|
|
51 |
|
|
52 |
val y = f x handle e => (pt (); raise e)
|
|
53 |
|
|
54 |
val () = pt ()
|
|
55 |
in
|
|
56 |
y
|
|
57 |
end;
|
|
58 |
|
|
59 |
(* ------------------------------------------------------------------------- *)
|
|
60 |
(* Generating random values. *)
|
|
61 |
(* ------------------------------------------------------------------------- *)
|
|
62 |
|
|
63 |
fun randomWord () = MLton.Random.rand ();
|
|
64 |
|
|
65 |
fun randomBool () = Word.andb (randomWord (),0w1) = 0w0;
|
|
66 |
|
|
67 |
fun randomInt 1 = 0
|
|
68 |
| randomInt 2 = Word.toInt (Word.andb (randomWord (), 0w1))
|
|
69 |
| randomInt 4 = Word.toInt (Word.andb (randomWord (), 0w3))
|
|
70 |
| randomInt n = Word.toInt (Word.mod (randomWord (), Word.fromInt n));
|
|
71 |
|
|
72 |
local
|
|
73 |
fun wordToReal w = Real.fromInt (Word.toInt (Word.>> (w,0w1)))
|
|
74 |
|
|
75 |
val normalizer = 1.0 / wordToReal (Word.notb 0w0);
|
|
76 |
in
|
|
77 |
fun randomReal () = normalizer * wordToReal (randomWord ());
|
|
78 |
end;
|
|
79 |
|
|
80 |
end
|
|
81 |
|
|
82 |
(* ------------------------------------------------------------------------- *)
|
|
83 |
(* Quotations a la Moscow ML. *)
|
|
84 |
(* ------------------------------------------------------------------------- *)
|
|
85 |
|
|
86 |
datatype 'a frag = QUOTE of string | ANTIQUOTE of 'a;
|