src/Tools/Metis/src/PortableMlton.sml
author blanchet
Mon, 13 Sep 2010 21:09:43 +0200
changeset 39348 6f9c9899f99f
child 39349 2d0a4361c3ef
permissions -rw-r--r--
new version of the Metis files
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39348
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     1
(* ========================================================================= *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     2
(* MLTON SPECIFIC FUNCTIONS                                                  *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     3
(* Copyright (c) 2002-2006 Joe Hurd, distributed under the GNU GPL version 2 *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     4
(* ========================================================================= *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     5
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     6
structure Portable :> Portable =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     7
struct
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     8
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
     9
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    10
(* The ML implementation.                                                    *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    11
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    12
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    13
val ml = "mlton";
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    14
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    15
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    16
(* Pointer equality using the run-time system.                               *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    17
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    18
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    19
val pointerEqual = MLton.eq;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    20
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    21
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    22
(* Timing function applications.                                             *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    23
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    24
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    25
fun time f x =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    26
    let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    27
      fun p t =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    28
          let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    29
            val s = Time.fmt 3 t
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    30
          in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    31
            case size (List.last (String.fields (fn x => x = #".") s)) of
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    32
              3 => s
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    33
            | 2 => s ^ "0"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    34
            | 1 => s ^ "00"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    35
            | _ => raise Fail "Portable.time"
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    36
          end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    37
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    38
      val c = Timer.startCPUTimer ()
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    39
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    40
      val r = Timer.startRealTimer ()
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    41
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    42
      fun pt () =
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    43
          let
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    44
            val {usr,sys} = Timer.checkCPUTimer c
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    45
            val real = Timer.checkRealTimer r
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    46
          in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    47
            print
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    48
              ("User: " ^ p usr ^ "  System: " ^ p sys ^
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    49
               "  Real: " ^ p real ^ "\n")
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    50
          end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    51
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    52
      val y = f x handle e => (pt (); raise e)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    53
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    54
      val () = pt ()
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    55
    in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    56
      y
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    57
    end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    58
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    59
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    60
(* Generating random values.                                                 *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    61
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    62
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    63
fun randomWord () = MLton.Random.rand ();
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    64
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    65
fun randomBool () = Word.andb (randomWord (),0w1) = 0w0;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    66
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    67
fun randomInt 1 = 0
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    68
  | randomInt 2 = Word.toInt (Word.andb (randomWord (), 0w1))
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    69
  | randomInt 4 = Word.toInt (Word.andb (randomWord (), 0w3))
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    70
  | randomInt n = Word.toInt (Word.mod (randomWord (), Word.fromInt n));
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    71
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    72
local
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    73
  fun wordToReal w = Real.fromInt (Word.toInt (Word.>> (w,0w1)))
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    74
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    75
  val normalizer = 1.0 / wordToReal (Word.notb 0w0);
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    76
in
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    77
  fun randomReal () = normalizer * wordToReal (randomWord ());
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    78
end;
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    79
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    80
end
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    81
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    82
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    83
(* Quotations a la Moscow ML.                                                *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    84
(* ------------------------------------------------------------------------- *)
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    85
6f9c9899f99f new version of the Metis files
blanchet
parents:
diff changeset
    86
datatype 'a frag = QUOTE of string | ANTIQUOTE of 'a;