src/Pure/ML-Systems/alice.ML
author wenzelm
Tue Apr 03 12:12:42 2007 +0200 (2007-04-03)
changeset 22563 78fb2af1a5c3
child 22575 2ed8a11f3172
permissions -rw-r--r--
Compatibility file for Alice 1.3 -- experimental!
wenzelm@22563
     1
(*  Title:      Pure/ML-Systems/alice.ML
wenzelm@22563
     2
    ID:         $Id$
wenzelm@22563
     3
wenzelm@22563
     4
Compatibility file for Alice 1.3.
wenzelm@22563
     5
val ml_system = "alice";
wenzelm@22563
     6
use "ML-Systems/alice.ML";
wenzelm@22563
     7
wenzelm@22563
     8
*)
wenzelm@22563
     9
wenzelm@22563
    10
fun exit 0 = (OS.Process.exit OS.Process.success): unit
wenzelm@22563
    11
  | exit _ = OS.Process.exit OS.Process.failure;
wenzelm@22563
    12
wenzelm@22563
    13
wenzelm@22563
    14
(** ML system related **)
wenzelm@22563
    15
wenzelm@22563
    16
(*low-level pointer equality*)
wenzelm@22563
    17
fun pointer_eq (_: 'a, _: 'a) = false;
wenzelm@22563
    18
wenzelm@22563
    19
wenzelm@22563
    20
(* restore old-style character / string functions *)
wenzelm@22563
    21
wenzelm@22563
    22
exception Ord;
wenzelm@22563
    23
fun ord "" = raise Ord
wenzelm@22563
    24
  | ord s = Char.ord (String.sub (s, 0));
wenzelm@22563
    25
wenzelm@22563
    26
val chr = Char.toString o chr;
wenzelm@22563
    27
val explode = map Char.toString o String.explode;
wenzelm@22563
    28
val implode = String.concat;
wenzelm@22563
    29
wenzelm@22563
    30
wenzelm@22563
    31
(* Poly/ML emulation *)
wenzelm@22563
    32
wenzelm@22563
    33
fun quit () = exit 0;
wenzelm@22563
    34
wenzelm@22563
    35
fun print_depth n = Print.depth := n;
wenzelm@22563
    36
wenzelm@22563
    37
wenzelm@22563
    38
(* compiler-independent timing functions *)
wenzelm@22563
    39
wenzelm@22563
    40
structure Timer =
wenzelm@22563
    41
struct
wenzelm@22563
    42
  open Timer;
wenzelm@22563
    43
  type cpu_timer = unit;
wenzelm@22563
    44
  fun startCPUTimer () = ();
wenzelm@22563
    45
  fun checkCPUTimer () = {sys = Time.zeroTime, usr = Time.zeroTime};
wenzelm@22563
    46
  fun checkGCTime () = Time.zeroTime;
wenzelm@22563
    47
end;
wenzelm@22563
    48
wenzelm@22563
    49
fun start_timing () =
wenzelm@22563
    50
  let val CPUtimer = Timer.startCPUTimer();
wenzelm@22563
    51
      val time = Timer.checkCPUTimer(CPUtimer)
wenzelm@22563
    52
  in  (CPUtimer,time)  end;
wenzelm@22563
    53
wenzelm@22563
    54
fun end_timing (CPUtimer, {sys,usr}) =
wenzelm@22563
    55
  let open Time  (*...for Time.toString, Time.+ and Time.- *)
wenzelm@22563
    56
      val {sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
wenzelm@22563
    57
  in  "User " ^ toString (usr2-usr) ^
wenzelm@22563
    58
      "  All "^ toString (sys2-sys + usr2-usr) ^
wenzelm@22563
    59
      " secs"
wenzelm@22563
    60
      handle Time => ""
wenzelm@22563
    61
  end;
wenzelm@22563
    62
wenzelm@22563
    63
fun check_timer timer =
wenzelm@22563
    64
  let
wenzelm@22563
    65
    val {sys, usr} = Timer.checkCPUTimer timer;
wenzelm@22563
    66
    val gc = Timer.checkGCTime timer;    (* FIXME already included in usr? *)
wenzelm@22563
    67
  in (sys, usr, gc) end;
wenzelm@22563
    68
wenzelm@22563
    69
wenzelm@22563
    70
(*prompts*)
wenzelm@22563
    71
fun ml_prompts p1 p2 = ();
wenzelm@22563
    72
wenzelm@22563
    73
(*dummy implementation*)
wenzelm@22563
    74
fun profile (n: int) f x = f x;
wenzelm@22563
    75
wenzelm@22563
    76
(*dummy implementation*)
wenzelm@22563
    77
fun exception_trace f = f ();
wenzelm@22563
    78
wenzelm@22563
    79
(*dummy implementation*)
wenzelm@22563
    80
fun print x = x;
wenzelm@22563
    81
wenzelm@22563
    82
wenzelm@22563
    83
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
wenzelm@22563
    84
wenzelm@22563
    85
fun make_pp path pprint = (path, pprint);
wenzelm@22563
    86
fun install_pp (path, pp) = ();
wenzelm@22563
    87
wenzelm@22563
    88
wenzelm@22563
    89
(* ML command execution *)
wenzelm@22563
    90
wenzelm@22563
    91
fun use_text name (print, err) verbose txt = (Compiler.eval txt; ());
wenzelm@22563
    92
wenzelm@22563
    93
fun use_file _ _ name = use name;
wenzelm@22563
    94
wenzelm@22563
    95
wenzelm@22563
    96
wenzelm@22563
    97
(** interrupts **)
wenzelm@22563
    98
wenzelm@22563
    99
exception Interrupt;
wenzelm@22563
   100
wenzelm@22563
   101
fun ignore_interrupt f x = f x;
wenzelm@22563
   102
fun raise_interrupt f x = f x;
wenzelm@22563
   103
wenzelm@22563
   104
wenzelm@22563
   105
(* basis library fixes *)
wenzelm@22563
   106
wenzelm@22563
   107
structure TextIO =
wenzelm@22563
   108
struct
wenzelm@22563
   109
  open TextIO;
wenzelm@22563
   110
wenzelm@22563
   111
  fun inputLine is =
wenzelm@22563
   112
    (case TextIO.inputLine is of
wenzelm@22563
   113
      SOME str => str
wenzelm@22563
   114
    | NONE => "")
wenzelm@22563
   115
    handle IO.Io _ => raise Interrupt;
wenzelm@22563
   116
end;
wenzelm@22563
   117
wenzelm@22563
   118
wenzelm@22563
   119
(* bounded time execution *)
wenzelm@22563
   120
wenzelm@22563
   121
(*dummy implementation*)
wenzelm@22563
   122
fun interrupt_timeout time f x =
wenzelm@22563
   123
  f x;
wenzelm@22563
   124
wenzelm@22563
   125
wenzelm@22563
   126
wenzelm@22563
   127
(** OS related **)
wenzelm@22563
   128
wenzelm@22563
   129
(* system command execution *)
wenzelm@22563
   130
wenzelm@22563
   131
(*execute Unix command which doesn't take any input from stdin and
wenzelm@22563
   132
  sends its output to stdout; could be done more easily by Unix.execute,
wenzelm@22563
   133
  but that function doesn't use the PATH*)
wenzelm@22563
   134
fun execute command =
wenzelm@22563
   135
  let
wenzelm@22563
   136
    val tmp_name = OS.FileSys.tmpName ();
wenzelm@22563
   137
    val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
wenzelm@22563
   138
    val result = TextIO.inputAll is;
wenzelm@22563
   139
  in
wenzelm@22563
   140
    TextIO.closeIn is;
wenzelm@22563
   141
    OS.FileSys.remove tmp_name;
wenzelm@22563
   142
    result
wenzelm@22563
   143
  end;
wenzelm@22563
   144
wenzelm@22563
   145
(*plain version; with return code*)
wenzelm@22563
   146
val system = OS.Process.system: string -> int;
wenzelm@22563
   147
wenzelm@22563
   148
structure OS =
wenzelm@22563
   149
struct
wenzelm@22563
   150
  open OS;
wenzelm@22563
   151
  structure FileSys =
wenzelm@22563
   152
  struct
wenzelm@22563
   153
    fun fileId name =
wenzelm@22563
   154
      (case execute ("perl -e '@_ = stat(q:" ^ name ^ ":); print $_[1]'") of
wenzelm@22563
   155
        "" => raise Fail "OS.FileSys.fileId"   (* FIXME IO.Io!? *)
wenzelm@22563
   156
      | s => (case Int.fromString s of NONE => raise Fail "OS.FileSys.fileId" | SOME i => i));
wenzelm@22563
   157
    val compare = Int.compare;
wenzelm@22563
   158
    open FileSys;
wenzelm@22563
   159
  end;
wenzelm@22563
   160
end;
wenzelm@22563
   161
wenzelm@22563
   162
wenzelm@22563
   163
(* getenv *)
wenzelm@22563
   164
wenzelm@22563
   165
fun getenv var =
wenzelm@22563
   166
  (case OS.Process.getEnv var of
wenzelm@22563
   167
    NONE => ""
wenzelm@22563
   168
  | SOME txt => txt);