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