src/Pure/ML-Systems/smlnj.ML
author wenzelm
Mon Dec 11 19:05:23 2006 +0100 (2006-12-11)
changeset 21770 ea6f846d8c4b
parent 21298 6d2306b2376d
child 22144 c33450acd873
permissions -rw-r--r--
added use_file;
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@10914
    99
fun use_text (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@21298
   111
      (Control.Print.out := out_orig; err (output ()); raise exn);
wenzelm@21298
   112
    Control.Print.out := out_orig;
wenzelm@10914
   113
    if verbose then print (output ()) else ()
wenzelm@5090
   114
  end;
wenzelm@4403
   115
wenzelm@21770
   116
fun use_file _ _ name = use name;
wenzelm@21770
   117
wenzelm@21770
   118
wenzelm@4403
   119
wenzelm@5816
   120
(** interrupts **)
wenzelm@5816
   121
wenzelm@12990
   122
exception Interrupt;
wenzelm@12990
   123
wenzelm@5816
   124
local
wenzelm@5816
   125
wenzelm@12990
   126
fun capture f x = ((f x): unit; NONE) handle exn => SOME exn;
wenzelm@5816
   127
wenzelm@12990
   128
fun release NONE = ()
wenzelm@12990
   129
  | release (SOME exn) = raise exn;
wenzelm@5816
   130
wenzelm@5816
   131
in
wenzelm@5816
   132
wenzelm@12990
   133
fun ignore_interrupt f x =
wenzelm@5816
   134
  let
wenzelm@12990
   135
    val old_handler = Signals.setHandler (Signals.sigINT, Signals.IGNORE);
wenzelm@12990
   136
    val result = capture f x;
wenzelm@12990
   137
    val _ = Signals.setHandler (Signals.sigINT, old_handler);
wenzelm@12990
   138
  in release result end;
wenzelm@5816
   139
wenzelm@12990
   140
fun raise_interrupt f x =
wenzelm@12990
   141
  let
wenzelm@5816
   142
    val interrupted = ref false;
wenzelm@12990
   143
    val result = ref NONE;
wenzelm@12990
   144
    val old_handler = Signals.inqHandler Signals.sigINT;
wenzelm@5816
   145
  in
wenzelm@12990
   146
    SMLofNJ.Cont.callcc (fn cont =>
wenzelm@12990
   147
      (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => (interrupted := true; cont)));
wenzelm@12990
   148
      result := capture f x));
wenzelm@12990
   149
    Signals.setHandler (Signals.sigINT, old_handler);
wenzelm@12990
   150
    if ! interrupted then raise Interrupt else release (! result)
wenzelm@12990
   151
  end;
wenzelm@5816
   152
wenzelm@5816
   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@17529
   161
wenzelm@21298
   162
  fun inputLine is =
wenzelm@21298
   163
    (case TextIO.inputLine is of
wenzelm@21298
   164
      SOME str => str
wenzelm@21298
   165
    | NONE => "")
wenzelm@21298
   166
    handle IO.Io _ => raise Interrupt;
wenzelm@21298
   167
end;
wenzelm@17511
   168
webertj@18790
   169
webertj@18790
   170
(* bounded time execution *)
webertj@18790
   171
wenzelm@21298
   172
fun interrupt_timeout time f x =
wenzelm@21298
   173
  TimeLimit.timeLimit time f x
wenzelm@21298
   174
    handle TimeLimit.TimeOut => raise Interrupt;
webertj@18790
   175
webertj@18790
   176
wenzelm@16542
   177
(** Signal handling: emulation of the Poly/ML Signal structure. Note that types
paulson@15702
   178
    Posix.Signal.signal and Signals.signal differ **)
paulson@15702
   179
paulson@15702
   180
structure IsaSignal =
paulson@15702
   181
struct
paulson@15702
   182
paulson@15702
   183
datatype sig_handle = SIG_DFL | SIG_IGN | SIG_HANDLE of Signals.signal -> unit;
paulson@15702
   184
wenzelm@16542
   185
(*From the SML/NJ documentation: "HANDLER(f) installs a handler for a
wenzelm@16542
   186
  signal. When signal is delivered to the process, the execution state
wenzelm@16542
   187
  of the current thread will be bundled up as a continuation k, then
wenzelm@16542
   188
  f(signal,n,k) will be called. The number n is the number of times
wenzelm@16542
   189
  signal has been signalled since the last time f was invoked for it."*)
paulson@15702
   190
paulson@15702
   191
fun toAction SIG_DFL = Signals.DEFAULT
paulson@15702
   192
  | toAction SIG_IGN = Signals.IGNORE
wenzelm@16542
   193
  | toAction (SIG_HANDLE iu) =
paulson@15702
   194
      Signals.HANDLER (fn (signo,_,cont) => (iu signo; cont));
wenzelm@16542
   195
paulson@15702
   196
(*The types are correct, but I'm not sure about the semantics!*)
paulson@15702
   197
fun fromAction Signals.DEFAULT = SIG_DFL
paulson@15702
   198
  | fromAction Signals.IGNORE = SIG_IGN
wenzelm@16542
   199
  | fromAction (Signals.HANDLER f) =
paulson@15702
   200
      SIG_HANDLE (fn signo => SMLofNJ.Cont.callcc (fn k => (f (signo,0,k); ())));
wenzelm@16542
   201
paulson@15702
   202
(*Poly/ML version has type  int * sig_handle -> sig_handle*)
paulson@15702
   203
fun signal (signo, sh) = fromAction (Signals.setHandler (signo, toAction sh));
paulson@15702
   204
paulson@15702
   205
val usr1 = UnixSignals.sigUSR1
paulson@15702
   206
val usr2 = UnixSignals.sigUSR2
paulson@17763
   207
val alrm = UnixSignals.sigALRM
paulson@17763
   208
val chld = UnixSignals.sigCHLD
paulson@17763
   209
val cont = UnixSignals.sigCONT
paulson@17763
   210
val int = UnixSignals.sigINT
paulson@17763
   211
val quit = UnixSignals.sigQUIT
paulson@17763
   212
val term = UnixSignals.sigTERM
paulson@15702
   213
paulson@15702
   214
end;
wenzelm@5816
   215
wenzelm@5816
   216
wenzelm@4403
   217
(** OS related **)
wenzelm@4403
   218
wenzelm@4403
   219
(* system command execution *)
wenzelm@4403
   220
wenzelm@4403
   221
(*execute Unix command which doesn't take any input from stdin and
wenzelm@4403
   222
  sends its output to stdout; could be done more easily by Unix.execute,
wenzelm@4403
   223
  but that function doesn't use the PATH*)
wenzelm@4403
   224
fun execute command =
wenzelm@4403
   225
  let
wenzelm@4403
   226
    val tmp_name = OS.FileSys.tmpName ();
wenzelm@4403
   227
    val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
wenzelm@4403
   228
    val result = TextIO.inputAll is;
wenzelm@4403
   229
  in
wenzelm@4403
   230
    TextIO.closeIn is;
wenzelm@4403
   231
    OS.FileSys.remove tmp_name;
wenzelm@4403
   232
    result
wenzelm@4403
   233
  end;
wenzelm@4403
   234
wenzelm@7855
   235
(*plain version; with return code*)
wenzelm@12990
   236
val system = OS.Process.system: string -> int;
wenzelm@7855
   237
wenzelm@4403
   238
wenzelm@17824
   239
(*Convert a process ID to a decimal string (chiefly for tracing)*)
wenzelm@21298
   240
fun string_of_pid pid =
wenzelm@17824
   241
    Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid));
wenzelm@17824
   242
wenzelm@17824
   243
wenzelm@4403
   244
(* getenv *)
wenzelm@4403
   245
wenzelm@4403
   246
fun getenv var =
wenzelm@4403
   247
  (case OS.Process.getEnv var of
wenzelm@4403
   248
    NONE => ""
wenzelm@4403
   249
  | SOME txt => txt);