src/Pure/ML-Systems/smlnj.ML
author wenzelm
Fri Aug 03 16:28:24 2007 +0200 (2007-08-03)
changeset 24145 c6402b61d44a
parent 23965 f93e509659c1
child 24290 5607b8b752bb
permissions -rw-r--r--
preparations for proper type int;
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@24145
     7
fun mk_int (i: int) = i;
wenzelm@24145
     8
fun dest_int (i: int) = i;
wenzelm@24145
     9
(*use "ML-Systems/proper_int.ML";*)
wenzelm@24145
    10
wenzelm@23965
    11
use "ML-Systems/exn.ML";
wenzelm@23965
    12
use "ML-Systems/multithreading_dummy.ML";
wenzelm@23921
    13
wenzelm@16542
    14
wenzelm@4403
    15
(** ML system related **)
wenzelm@4403
    16
wenzelm@16542
    17
(*low-level pointer equality*)
wenzelm@16502
    18
wenzelm@21298
    19
CM.autoload "$smlnj/init/init.cmi";
wenzelm@21298
    20
val pointer_eq = InlineT.ptreql;
paulson@16528
    21
wenzelm@16502
    22
wenzelm@4403
    23
(* restore old-style character / string functions *)
wenzelm@4403
    24
wenzelm@24145
    25
val ord     = mk_int o SML90.ord;
wenzelm@24145
    26
val chr     = SML90.chr o dest_int;
paulson@10725
    27
val explode = SML90.explode;
paulson@10725
    28
val implode = SML90.implode;
wenzelm@4403
    29
wenzelm@4403
    30
wenzelm@4403
    31
(* New Jersey ML parameters *)
wenzelm@4403
    32
wenzelm@4403
    33
val _ =
wenzelm@21298
    34
 (Control.Print.printLength := 1000;
wenzelm@21298
    35
  Control.Print.printDepth := 350;
wenzelm@21298
    36
  Control.Print.stringDepth := 250;
wenzelm@21298
    37
  Control.Print.signatures := 2;
wenzelm@21298
    38
  Control.MC.matchRedundantError := false);
wenzelm@4403
    39
wenzelm@4403
    40
wenzelm@4403
    41
(* Poly/ML emulation *)
wenzelm@4403
    42
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@4403
    46
fun print_depth n =
wenzelm@24145
    47
 (Control.Print.printDepth := (dest_int n) div 2;
wenzelm@24145
    48
  Control.Print.printLength := dest_int n);
wenzelm@4403
    49
wenzelm@4403
    50
wenzelm@5816
    51
(* compiler-independent timing functions *)
wenzelm@4403
    52
wenzelm@21298
    53
fun start_timing () =
wenzelm@21298
    54
  let val CPUtimer = Timer.startCPUTimer();
wenzelm@21298
    55
      val time = Timer.checkCPUTimer(CPUtimer)
wenzelm@21298
    56
  in  (CPUtimer,time)  end;
wenzelm@21298
    57
wenzelm@21298
    58
fun end_timing (CPUtimer, {sys,usr}) =
wenzelm@21298
    59
  let open Time  (*...for Time.toString, Time.+ and Time.- *)
wenzelm@21298
    60
      val {sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
wenzelm@21298
    61
  in  "User " ^ toString (usr2-usr) ^
wenzelm@21298
    62
      "  All "^ toString (sys2-sys + usr2-usr) ^
wenzelm@21298
    63
      " secs"
wenzelm@21298
    64
      handle Time => ""
wenzelm@21298
    65
  end;
wenzelm@21298
    66
wenzelm@21298
    67
fun check_timer timer =
wenzelm@21298
    68
  let
wenzelm@21298
    69
    val {sys, usr} = Timer.checkCPUTimer timer;
wenzelm@21298
    70
    val gc = Timer.checkGCTime timer;    (* FIXME already included in usr? *)
wenzelm@21298
    71
  in (sys, usr, gc) end;
wenzelm@4403
    72
wenzelm@4403
    73
wenzelm@16660
    74
(*prompts*)
wenzelm@4977
    75
fun ml_prompts p1 p2 =
wenzelm@21298
    76
  (Control.primaryPrompt := p1; Control.secondaryPrompt := p2);
wenzelm@4977
    77
wenzelm@17511
    78
(*dummy implementation*)
wenzelm@16681
    79
fun profile (n: int) f x = f x;
wenzelm@16681
    80
wenzelm@17511
    81
(*dummy implementation*)
wenzelm@16681
    82
fun exception_trace f = f ();
wenzelm@4977
    83
haftmann@18384
    84
(*dummy implementation*)
haftmann@18384
    85
fun print x = x;
wenzelm@16681
    86
obua@23770
    87
(*dummy implementation*)
obua@23770
    88
fun makestring x = "dummy string for SML New Jersey";
obua@23770
    89
wenzelm@21298
    90
wenzelm@4403
    91
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
wenzelm@4403
    92
wenzelm@21298
    93
fun make_pp path pprint =
wenzelm@21298
    94
  let
wenzelm@21298
    95
    open PrettyPrint;
wenzelm@4403
    96
wenzelm@21298
    97
    fun pp pps obj =
wenzelm@21298
    98
      pprint obj
wenzelm@24145
    99
        (string pps, openHOVBox pps o Rel o dest_int,
wenzelm@24145
   100
          fn wd => break pps {nsp=dest_int wd, offset=0}, fn () => newline pps,
wenzelm@21298
   101
          fn () => closeBox pps);
wenzelm@21298
   102
  in (path, pp) end;
wenzelm@21298
   103
wenzelm@21298
   104
fun install_pp (path, pp) = CompilerPPTable.install_pp path pp;
wenzelm@4403
   105
wenzelm@4403
   106
wenzelm@4403
   107
(* ML command execution *)
wenzelm@4403
   108
wenzelm@22144
   109
fun use_text name (print, err) verbose txt =
wenzelm@5090
   110
  let
wenzelm@21298
   111
    val ref out_orig = Control.Print.out;
wenzelm@5090
   112
wenzelm@5090
   113
    val out_buffer = ref ([]: string list);
wenzelm@5090
   114
    val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
wenzelm@10914
   115
    fun output () =
wenzelm@7890
   116
      let val str = implode (rev (! out_buffer))
wenzelm@10914
   117
      in String.substring (str, 0, Int.max (0, size str - 1)) end;
wenzelm@5090
   118
  in
wenzelm@21298
   119
    Control.Print.out := out;
wenzelm@21298
   120
    Backend.Interact.useStream (TextIO.openString txt) handle exn =>
wenzelm@22144
   121
      (Control.Print.out := out_orig;
wenzelm@22144
   122
        err ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn);
wenzelm@21298
   123
    Control.Print.out := out_orig;
wenzelm@10914
   124
    if verbose then print (output ()) else ()
wenzelm@5090
   125
  end;
wenzelm@4403
   126
wenzelm@21770
   127
fun use_file _ _ name = use name;
wenzelm@21770
   128
wenzelm@21770
   129
wenzelm@4403
   130
wenzelm@5816
   131
(** interrupts **)
wenzelm@5816
   132
wenzelm@12990
   133
exception Interrupt;
wenzelm@12990
   134
wenzelm@12990
   135
fun ignore_interrupt f x =
wenzelm@5816
   136
  let
wenzelm@12990
   137
    val old_handler = Signals.setHandler (Signals.sigINT, Signals.IGNORE);
wenzelm@23965
   138
    val result = Exn.capture f x;
wenzelm@12990
   139
    val _ = Signals.setHandler (Signals.sigINT, old_handler);
wenzelm@23965
   140
  in Exn.release result end;
wenzelm@5816
   141
wenzelm@12990
   142
fun raise_interrupt f x =
wenzelm@12990
   143
  let
wenzelm@5816
   144
    val interrupted = ref false;
wenzelm@23965
   145
    val result = ref (Exn.Result ());
wenzelm@12990
   146
    val old_handler = Signals.inqHandler Signals.sigINT;
wenzelm@5816
   147
  in
wenzelm@12990
   148
    SMLofNJ.Cont.callcc (fn cont =>
wenzelm@12990
   149
      (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => (interrupted := true; cont)));
wenzelm@23965
   150
      result := Exn.capture f x));
wenzelm@12990
   151
    Signals.setHandler (Signals.sigINT, old_handler);
wenzelm@23965
   152
    if ! interrupted then raise Interrupt else Exn.release (! result)
wenzelm@12990
   153
  end;
wenzelm@5816
   154
wenzelm@21298
   155
wenzelm@21298
   156
(* basis library fixes *)
wenzelm@21298
   157
wenzelm@21298
   158
structure TextIO =
wenzelm@21298
   159
struct
wenzelm@21298
   160
  open TextIO;
wenzelm@23139
   161
  fun inputLine is = TextIO.inputLine is
wenzelm@21298
   162
    handle IO.Io _ => raise Interrupt;
wenzelm@21298
   163
end;
wenzelm@17511
   164
webertj@18790
   165
webertj@18790
   166
(* bounded time execution *)
webertj@18790
   167
wenzelm@21298
   168
fun interrupt_timeout time f x =
wenzelm@21298
   169
  TimeLimit.timeLimit time f x
wenzelm@21298
   170
    handle TimeLimit.TimeOut => raise Interrupt;
webertj@18790
   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);