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