src/Pure/ML-Systems/smlnj.ML
author wenzelm
Sun Sep 07 17:46:43 2008 +0200 (2008-09-07)
changeset 28151 61f9c918b410
parent 26885 cfd5eb167706
child 28268 ac8431ecd57e
permissions -rw-r--r--
explicit use of universal.ML and dummy_thread.ML;
wenzelm@4403
     1
(*  Title:      Pure/ML-Systems/smlnj.ML
wenzelm@4403
     2
    ID:         $Id$
wenzelm@4403
     3
wenzelm@5708
     4
Compatibility file for Standard ML of New Jersey 110 or later.
wenzelm@4403
     5
*)
wenzelm@4403
     6
wenzelm@24599
     7
use "ML-Systems/proper_int.ML";
wenzelm@24599
     8
use "ML-Systems/overloading_smlnj.ML";
wenzelm@23965
     9
use "ML-Systems/exn.ML";
wenzelm@25732
    10
use "ML-Systems/universal.ML";
wenzelm@28151
    11
use "ML-Systems/thread_dummy.ML";
wenzelm@24688
    12
use "ML-Systems/multithreading.ML";
wenzelm@26220
    13
use "ML-Systems/system_shell.ML";
wenzelm@23921
    14
wenzelm@16542
    15
wenzelm@16542
    16
(*low-level pointer equality*)
wenzelm@16502
    17
wenzelm@21298
    18
CM.autoload "$smlnj/init/init.cmi";
wenzelm@21298
    19
val pointer_eq = InlineT.ptreql;
paulson@16528
    20
wenzelm@16502
    21
wenzelm@4403
    22
(* restore old-style character / string functions *)
wenzelm@4403
    23
wenzelm@24145
    24
val ord     = mk_int o SML90.ord;
wenzelm@24145
    25
val chr     = SML90.chr o dest_int;
paulson@10725
    26
val explode = SML90.explode;
paulson@10725
    27
val implode = SML90.implode;
wenzelm@4403
    28
wenzelm@4403
    29
wenzelm@4403
    30
(* New Jersey ML parameters *)
wenzelm@4403
    31
wenzelm@4403
    32
val _ =
wenzelm@21298
    33
 (Control.Print.printLength := 1000;
wenzelm@21298
    34
  Control.Print.printDepth := 350;
wenzelm@21298
    35
  Control.Print.stringDepth := 250;
wenzelm@21298
    36
  Control.Print.signatures := 2;
wenzelm@21298
    37
  Control.MC.matchRedundantError := false);
wenzelm@4403
    38
wenzelm@4403
    39
wenzelm@4403
    40
(* Poly/ML emulation *)
wenzelm@4403
    41
wenzelm@24599
    42
val exit = exit o dest_int;
wenzelm@4403
    43
fun quit () = exit 0;
wenzelm@4403
    44
wenzelm@4403
    45
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
wenzelm@24329
    46
local
wenzelm@24599
    47
  val depth = ref (10: int);
wenzelm@24329
    48
in
wenzelm@24329
    49
  fun get_print_depth () = ! depth;
wenzelm@24329
    50
  fun print_depth n =
wenzelm@24329
    51
   (depth := n;
wenzelm@24329
    52
    Control.Print.printDepth := dest_int n div 2;
wenzelm@24329
    53
    Control.Print.printLength := dest_int n);
wenzelm@24329
    54
end;
wenzelm@4403
    55
wenzelm@26474
    56
wenzelm@5816
    57
(* compiler-independent timing functions *)
wenzelm@4403
    58
wenzelm@21298
    59
fun start_timing () =
wenzelm@21298
    60
  let val CPUtimer = Timer.startCPUTimer();
wenzelm@21298
    61
      val time = Timer.checkCPUTimer(CPUtimer)
wenzelm@21298
    62
  in  (CPUtimer,time)  end;
wenzelm@21298
    63
wenzelm@21298
    64
fun end_timing (CPUtimer, {sys,usr}) =
wenzelm@21298
    65
  let open Time  (*...for Time.toString, Time.+ and Time.- *)
wenzelm@21298
    66
      val {sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
wenzelm@21298
    67
  in  "User " ^ toString (usr2-usr) ^
wenzelm@21298
    68
      "  All "^ toString (sys2-sys + usr2-usr) ^
wenzelm@21298
    69
      " secs"
wenzelm@21298
    70
      handle Time => ""
wenzelm@21298
    71
  end;
wenzelm@21298
    72
wenzelm@21298
    73
fun check_timer timer =
wenzelm@21298
    74
  let
wenzelm@21298
    75
    val {sys, usr} = Timer.checkCPUTimer timer;
wenzelm@21298
    76
    val gc = Timer.checkGCTime timer;    (* FIXME already included in usr? *)
wenzelm@21298
    77
  in (sys, usr, gc) end;
wenzelm@4403
    78
wenzelm@4403
    79
wenzelm@16660
    80
(*prompts*)
wenzelm@4977
    81
fun ml_prompts p1 p2 =
wenzelm@21298
    82
  (Control.primaryPrompt := p1; Control.secondaryPrompt := p2);
wenzelm@4977
    83
wenzelm@17511
    84
(*dummy implementation*)
wenzelm@16681
    85
fun profile (n: int) f x = f x;
wenzelm@16681
    86
wenzelm@17511
    87
(*dummy implementation*)
wenzelm@16681
    88
fun exception_trace f = f ();
wenzelm@4977
    89
haftmann@18384
    90
(*dummy implementation*)
haftmann@18384
    91
fun print x = x;
wenzelm@16681
    92
obua@23770
    93
(*dummy implementation*)
obua@23770
    94
fun makestring x = "dummy string for SML New Jersey";
obua@23770
    95
wenzelm@21298
    96
wenzelm@24290
    97
(* toplevel pretty printing (see also Pure/pure_setup.ML) *)
wenzelm@4403
    98
wenzelm@21298
    99
fun make_pp path pprint =
wenzelm@21298
   100
  let
wenzelm@21298
   101
    open PrettyPrint;
wenzelm@4403
   102
wenzelm@21298
   103
    fun pp pps obj =
wenzelm@21298
   104
      pprint obj
wenzelm@24145
   105
        (string pps, openHOVBox pps o Rel o dest_int,
wenzelm@24145
   106
          fn wd => break pps {nsp=dest_int wd, offset=0}, fn () => newline pps,
wenzelm@21298
   107
          fn () => closeBox pps);
wenzelm@21298
   108
  in (path, pp) end;
wenzelm@21298
   109
wenzelm@21298
   110
fun install_pp (path, pp) = CompilerPPTable.install_pp path pp;
wenzelm@4403
   111
wenzelm@4403
   112
wenzelm@4403
   113
(* ML command execution *)
wenzelm@4403
   114
wenzelm@26884
   115
fun use_text (tune: string -> string) _ (line: int, name) (print, err) verbose txt =
wenzelm@5090
   116
  let
wenzelm@21298
   117
    val ref out_orig = Control.Print.out;
wenzelm@5090
   118
wenzelm@5090
   119
    val out_buffer = ref ([]: string list);
wenzelm@5090
   120
    val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
wenzelm@10914
   121
    fun output () =
wenzelm@7890
   122
      let val str = implode (rev (! out_buffer))
wenzelm@10914
   123
      in String.substring (str, 0, Int.max (0, size str - 1)) end;
wenzelm@5090
   124
  in
wenzelm@21298
   125
    Control.Print.out := out;
wenzelm@24599
   126
    Backend.Interact.useStream (TextIO.openString (tune txt)) handle exn =>
wenzelm@22144
   127
      (Control.Print.out := out_orig;
wenzelm@22144
   128
        err ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn);
wenzelm@21298
   129
    Control.Print.out := out_orig;
wenzelm@10914
   130
    if verbose then print (output ()) else ()
wenzelm@5090
   131
  end;
wenzelm@4403
   132
wenzelm@26885
   133
fun use_file tune str_of_pos output verbose name =
wenzelm@24599
   134
  let
wenzelm@24599
   135
    val instream = TextIO.openIn name;
wenzelm@26504
   136
    val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
wenzelm@26885
   137
  in use_text tune str_of_pos (1, name) output verbose txt end;
wenzelm@21770
   138
wenzelm@26474
   139
fun forget_structure name =
wenzelm@26885
   140
  use_text (fn x => x) (fn _ => "") (1, "ML") (TextIO.print, fn s => raise Fail s) false
wenzelm@26474
   141
    ("structure " ^ name ^ " = struct end");
wenzelm@26474
   142
wenzelm@21770
   143
wenzelm@4403
   144
wenzelm@5816
   145
(** interrupts **)
wenzelm@5816
   146
wenzelm@12990
   147
exception Interrupt;
wenzelm@12990
   148
wenzelm@26084
   149
local
wenzelm@26084
   150
wenzelm@26084
   151
fun change_signal new_handler f x =
wenzelm@5816
   152
  let
wenzelm@26084
   153
    val old_handler = Signals.setHandler (Signals.sigINT, new_handler);
wenzelm@26084
   154
    val result = Exn.capture (f old_handler) x;
wenzelm@12990
   155
    val _ = Signals.setHandler (Signals.sigINT, old_handler);
wenzelm@23965
   156
  in Exn.release result end;
wenzelm@5816
   157
wenzelm@26084
   158
in
wenzelm@26084
   159
wenzelm@26084
   160
fun interruptible (f: 'a -> 'b) x =
wenzelm@12990
   161
  let
wenzelm@26084
   162
    val result = ref (Exn.Exn Interrupt: 'b Exn.result);
wenzelm@12990
   163
    val old_handler = Signals.inqHandler Signals.sigINT;
wenzelm@5816
   164
  in
wenzelm@12990
   165
    SMLofNJ.Cont.callcc (fn cont =>
wenzelm@26084
   166
      (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => cont));
wenzelm@26084
   167
        result := Exn.capture f x));
wenzelm@12990
   168
    Signals.setHandler (Signals.sigINT, old_handler);
wenzelm@26084
   169
    Exn.release (! result)
wenzelm@12990
   170
  end;
wenzelm@5816
   171
wenzelm@26084
   172
fun uninterruptible f =
wenzelm@26084
   173
  change_signal Signals.IGNORE
wenzelm@26084
   174
    (fn old_handler => f (fn g => change_signal old_handler (fn _ => g)));
wenzelm@26084
   175
wenzelm@26084
   176
end;
wenzelm@26084
   177
wenzelm@21298
   178
wenzelm@21298
   179
(* basis library fixes *)
wenzelm@21298
   180
wenzelm@21298
   181
structure TextIO =
wenzelm@21298
   182
struct
wenzelm@21298
   183
  open TextIO;
wenzelm@23139
   184
  fun inputLine is = TextIO.inputLine is
wenzelm@21298
   185
    handle IO.Io _ => raise Interrupt;
wenzelm@21298
   186
end;
wenzelm@17511
   187
webertj@18790
   188
wenzelm@16542
   189
(** Signal handling: emulation of the Poly/ML Signal structure. Note that types
paulson@15702
   190
    Posix.Signal.signal and Signals.signal differ **)
paulson@15702
   191
paulson@15702
   192
structure IsaSignal =
paulson@15702
   193
struct
paulson@15702
   194
paulson@15702
   195
datatype sig_handle = SIG_DFL | SIG_IGN | SIG_HANDLE of Signals.signal -> unit;
paulson@15702
   196
wenzelm@16542
   197
(*From the SML/NJ documentation: "HANDLER(f) installs a handler for a
wenzelm@16542
   198
  signal. When signal is delivered to the process, the execution state
wenzelm@16542
   199
  of the current thread will be bundled up as a continuation k, then
wenzelm@16542
   200
  f(signal,n,k) will be called. The number n is the number of times
wenzelm@16542
   201
  signal has been signalled since the last time f was invoked for it."*)
paulson@15702
   202
paulson@15702
   203
fun toAction SIG_DFL = Signals.DEFAULT
paulson@15702
   204
  | toAction SIG_IGN = Signals.IGNORE
wenzelm@16542
   205
  | toAction (SIG_HANDLE iu) =
paulson@15702
   206
      Signals.HANDLER (fn (signo,_,cont) => (iu signo; cont));
wenzelm@16542
   207
paulson@15702
   208
(*The types are correct, but I'm not sure about the semantics!*)
paulson@15702
   209
fun fromAction Signals.DEFAULT = SIG_DFL
paulson@15702
   210
  | fromAction Signals.IGNORE = SIG_IGN
wenzelm@16542
   211
  | fromAction (Signals.HANDLER f) =
paulson@15702
   212
      SIG_HANDLE (fn signo => SMLofNJ.Cont.callcc (fn k => (f (signo,0,k); ())));
wenzelm@16542
   213
paulson@15702
   214
(*Poly/ML version has type  int * sig_handle -> sig_handle*)
paulson@15702
   215
fun signal (signo, sh) = fromAction (Signals.setHandler (signo, toAction sh));
paulson@15702
   216
paulson@15702
   217
val usr1 = UnixSignals.sigUSR1
paulson@15702
   218
val usr2 = UnixSignals.sigUSR2
paulson@17763
   219
val alrm = UnixSignals.sigALRM
paulson@17763
   220
val chld = UnixSignals.sigCHLD
paulson@17763
   221
val cont = UnixSignals.sigCONT
paulson@17763
   222
val int = UnixSignals.sigINT
paulson@17763
   223
val quit = UnixSignals.sigQUIT
paulson@17763
   224
val term = UnixSignals.sigTERM
paulson@15702
   225
paulson@15702
   226
end;
wenzelm@5816
   227
wenzelm@5816
   228
wenzelm@4403
   229
(** OS related **)
wenzelm@4403
   230
wenzelm@23826
   231
(* current directory *)
wenzelm@23826
   232
wenzelm@23826
   233
val cd = OS.FileSys.chDir;
wenzelm@23826
   234
val pwd = OS.FileSys.getDir;
wenzelm@23826
   235
wenzelm@23826
   236
wenzelm@4403
   237
(* system command execution *)
wenzelm@4403
   238
wenzelm@26220
   239
val system_out = (fn (output, rc) => (output, mk_int rc)) o system_out;
wenzelm@7855
   240
wenzelm@4403
   241
wenzelm@17824
   242
(*Convert a process ID to a decimal string (chiefly for tracing)*)
wenzelm@21298
   243
fun string_of_pid pid =
wenzelm@26220
   244
  Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid));
wenzelm@17824
   245
wenzelm@17824
   246
wenzelm@4403
   247
(* getenv *)
wenzelm@4403
   248
wenzelm@4403
   249
fun getenv var =
wenzelm@4403
   250
  (case OS.Process.getEnv var of
wenzelm@4403
   251
    NONE => ""
wenzelm@4403
   252
  | SOME txt => txt);