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