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