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