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