src/Pure/ML-Systems/smlnj.ML
author webertj
Mon Jan 23 17:29:52 2006 +0100 (2006-01-23)
changeset 18760 97aaecb84afe
parent 18384 fa38cca42913
child 18790 418131f631fc
permissions -rw-r--r--
TimeLimit replaced by interrupt_timeout
     1 (*  Title:      Pure/ML-Systems/smlnj.ML
     2     ID:         $Id$
     3     Author:     Carsten Clasohm and Markus Wenzel, TU Muenchen
     4 
     5 Compatibility file for Standard ML of New Jersey 110 or later.
     6 *)
     7 
     8 (case #version_id (Compiler.version) of
     9   [110, x] => if x >= 35 then use "ML-Systems/smlnj-compiler.ML" else ()
    10 | _ => ());
    11 
    12 
    13 (** ML system related **)
    14 
    15 (*low-level pointer equality*)
    16 
    17 (*dummy version -- may get overridden in smlnj-ptreql.ML*)
    18 fun pointer_eq (x:'a, y) = false;
    19 
    20 (case #version_id (Compiler.version) of
    21   [110, x] => if x >= 49 then use "ML-Systems/smlnj-ptreql.ML" else ()
    22 | _ => ());
    23 
    24 
    25 (* restore old-style character / string functions *)
    26 
    27 val ord     = SML90.ord;
    28 val chr     = SML90.chr;
    29 val explode = SML90.explode;
    30 val implode = SML90.implode;
    31 
    32 
    33 (* New Jersey ML parameters *)
    34 
    35 val _ =
    36  (Compiler.Control.Print.printLength := 1000;
    37   Compiler.Control.Print.printDepth := 350;
    38   Compiler.Control.Print.stringDepth := 250;
    39   Compiler.Control.Print.signatures := 2;
    40   Compiler.Control.MC.matchRedundantError := false);
    41 
    42 
    43 (* Poly/ML emulation *)
    44 
    45 fun quit () = exit 0;
    46 
    47 (*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
    48 fun print_depth n =
    49  (Compiler.Control.Print.printDepth := n div 2;
    50   Compiler.Control.Print.printLength := n);
    51 
    52 
    53 (* compiler-independent timing functions *)
    54 
    55 (case #version_id (Compiler.version) of
    56   [110, x] => if x >= 44
    57               then use "ML-Systems/cpu-timer-basis.ML"
    58               else use "ML-Systems/cpu-timer-gc.ML"
    59 | _ => use "ML-Systems/cpu-timer-gc.ML");
    60 
    61 
    62 (* bounded time execution *)
    63 
    64 use "ML-Systems/smlnj-interrupt-timeout.ML";
    65 
    66 
    67 (*prompts*)
    68 fun ml_prompts p1 p2 =
    69   (Compiler.Control.primaryPrompt := p1; Compiler.Control.secondaryPrompt := p2);
    70 
    71 (*dummy implementation*)
    72 fun profile (n: int) f x = f x;
    73 
    74 (*dummy implementation*)
    75 fun exception_trace f = f ();
    76 
    77 (*dummy implementation*)
    78 fun print x = x;
    79 
    80 (* toplevel pretty printing (see also Pure/install_pp.ML) *)
    81 
    82 (case #version_id (Compiler.version) of
    83   [110, x] => if x >= 44
    84               then use "ML-Systems/smlnj-pp-new.ML"
    85               else use "ML-Systems/smlnj-pp-old.ML"
    86 | _ => use "ML-Systems/smlnj-pp-old.ML");
    87 
    88 fun install_pp (path, pp) = Compiler.PPTable.install_pp path pp;
    89 
    90 
    91 (* ML command execution *)
    92 
    93 fun use_text (print, err) verbose txt =
    94   let
    95     val ref out_orig = Compiler.Control.Print.out;
    96 
    97     val out_buffer = ref ([]: string list);
    98     val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
    99     fun output () =
   100       let val str = implode (rev (! out_buffer))
   101       in String.substring (str, 0, Int.max (0, size str - 1)) end;
   102   in
   103     Compiler.Control.Print.out := out;
   104     Compiler.Interact.useStream (TextIO.openString txt) handle exn =>
   105       (Compiler.Control.Print.out := out_orig; err (output ()); raise exn);
   106     Compiler.Control.Print.out := out_orig;
   107     if verbose then print (output ()) else ()
   108   end;
   109 
   110 
   111 
   112 (** interrupts **)
   113 
   114 exception Interrupt;
   115 
   116 local
   117 
   118 fun capture f x = ((f x): unit; NONE) handle exn => SOME exn;
   119 
   120 fun release NONE = ()
   121   | release (SOME exn) = raise exn;
   122 
   123 in
   124 
   125 fun ignore_interrupt f x =
   126   let
   127     val old_handler = Signals.setHandler (Signals.sigINT, Signals.IGNORE);
   128     val result = capture f x;
   129     val _ = Signals.setHandler (Signals.sigINT, old_handler);
   130   in release result end;
   131 
   132 fun raise_interrupt f x =
   133   let
   134     val interrupted = ref false;
   135     val result = ref NONE;
   136     val old_handler = Signals.inqHandler Signals.sigINT;
   137   in
   138     SMLofNJ.Cont.callcc (fn cont =>
   139       (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => (interrupted := true; cont)));
   140       result := capture f x));
   141     Signals.setHandler (Signals.sigINT, old_handler);
   142     if ! interrupted then raise Interrupt else release (! result)
   143   end;
   144 
   145 end;
   146 
   147 (case #version_id (Compiler.version) of
   148   [110, x] => if x >= 44
   149               then use "ML-Systems/smlnj-basis-compat.ML"
   150               else ()
   151 | _ => ());
   152 
   153 
   154 (** Signal handling: emulation of the Poly/ML Signal structure. Note that types
   155     Posix.Signal.signal and Signals.signal differ **)
   156 
   157 structure IsaSignal =
   158 struct
   159 
   160 datatype sig_handle = SIG_DFL | SIG_IGN | SIG_HANDLE of Signals.signal -> unit;
   161 
   162 (*From the SML/NJ documentation: "HANDLER(f) installs a handler for a
   163   signal. When signal is delivered to the process, the execution state
   164   of the current thread will be bundled up as a continuation k, then
   165   f(signal,n,k) will be called. The number n is the number of times
   166   signal has been signalled since the last time f was invoked for it."*)
   167 
   168 fun toAction SIG_DFL = Signals.DEFAULT
   169   | toAction SIG_IGN = Signals.IGNORE
   170   | toAction (SIG_HANDLE iu) =
   171       Signals.HANDLER (fn (signo,_,cont) => (iu signo; cont));
   172 
   173 (*The types are correct, but I'm not sure about the semantics!*)
   174 fun fromAction Signals.DEFAULT = SIG_DFL
   175   | fromAction Signals.IGNORE = SIG_IGN
   176   | fromAction (Signals.HANDLER f) =
   177       SIG_HANDLE (fn signo => SMLofNJ.Cont.callcc (fn k => (f (signo,0,k); ())));
   178 
   179 (*Poly/ML version has type  int * sig_handle -> sig_handle*)
   180 fun signal (signo, sh) = fromAction (Signals.setHandler (signo, toAction sh));
   181 
   182 val usr1 = UnixSignals.sigUSR1
   183 val usr2 = UnixSignals.sigUSR2
   184 val alrm = UnixSignals.sigALRM
   185 val chld = UnixSignals.sigCHLD
   186 val cont = UnixSignals.sigCONT
   187 val int = UnixSignals.sigINT
   188 val quit = UnixSignals.sigQUIT
   189 val term = UnixSignals.sigTERM
   190 
   191 end;
   192 
   193 
   194 (** OS related **)
   195 
   196 (* system command execution *)
   197 
   198 (*execute Unix command which doesn't take any input from stdin and
   199   sends its output to stdout; could be done more easily by Unix.execute,
   200   but that function doesn't use the PATH*)
   201 fun execute command =
   202   let
   203     val tmp_name = OS.FileSys.tmpName ();
   204     val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
   205     val result = TextIO.inputAll is;
   206   in
   207     TextIO.closeIn is;
   208     OS.FileSys.remove tmp_name;
   209     result
   210   end;
   211 
   212 (*plain version; with return code*)
   213 val system = OS.Process.system: string -> int;
   214 
   215 
   216 (*Convert a process ID to a decimal string (chiefly for tracing)*)
   217 fun string_of_pid pid = 
   218     Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid));
   219 
   220 
   221 (* getenv *)
   222 
   223 fun getenv var =
   224   (case OS.Process.getEnv var of
   225     NONE => ""
   226   | SOME txt => txt);