src/Pure/ML-Systems/smlnj.ML
author wenzelm
Tue Sep 20 14:03:40 2005 +0200 (2005-09-20)
changeset 17511 51314f4bd01d
parent 16681 d54dfd724b35
child 17529 a436d89845af
permissions -rw-r--r--
tuned;
     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 (*prompts*)
    63 fun ml_prompts p1 p2 =
    64   (Compiler.Control.primaryPrompt := p1; Compiler.Control.secondaryPrompt := p2);
    65 
    66 (*dummy implementation*)
    67 fun profile (n: int) f x = f x;
    68 
    69 (*dummy implementation*)
    70 fun exception_trace f = f ();
    71 
    72 (case #version_id (Compiler.version) of
    73   [110, x] => if x >= 44
    74               then use "ML-Systems/smlnj-basis-compat.ML"
    75               else ()
    76 | _ => ());
    77 
    78 
    79 (* toplevel pretty printing (see also Pure/install_pp.ML) *)
    80 
    81 (case #version_id (Compiler.version) of
    82   [110, x] => if x >= 44
    83               then use "ML-Systems/smlnj-pp-new.ML"
    84               else use "ML-Systems/smlnj-pp-old.ML"
    85 | _ => use "ML-Systems/smlnj-pp-old.ML");
    86 
    87 fun install_pp (path, pp) = Compiler.PPTable.install_pp path pp;
    88 
    89 
    90 (* ML command execution *)
    91 
    92 fun use_text (print, err) verbose txt =
    93   let
    94     val ref out_orig = Compiler.Control.Print.out;
    95 
    96     val out_buffer = ref ([]: string list);
    97     val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
    98     fun output () =
    99       let val str = implode (rev (! out_buffer))
   100       in String.substring (str, 0, Int.max (0, size str - 1)) end;
   101   in
   102     Compiler.Control.Print.out := out;
   103     Compiler.Interact.useStream (TextIO.openString txt) handle exn =>
   104       (Compiler.Control.Print.out := out_orig; err (output ()); raise exn);
   105     Compiler.Control.Print.out := out_orig;
   106     if verbose then print (output ()) else ()
   107   end;
   108 
   109 
   110 
   111 (** interrupts **)
   112 
   113 exception Interrupt;
   114 
   115 local
   116 
   117 fun capture f x = ((f x): unit; NONE) handle exn => SOME exn;
   118 
   119 fun release NONE = ()
   120   | release (SOME exn) = raise exn;
   121 
   122 in
   123 
   124 fun ignore_interrupt f x =
   125   let
   126     val old_handler = Signals.setHandler (Signals.sigINT, Signals.IGNORE);
   127     val result = capture f x;
   128     val _ = Signals.setHandler (Signals.sigINT, old_handler);
   129   in release result end;
   130 
   131 fun raise_interrupt f x =
   132   let
   133     val interrupted = ref false;
   134     val result = ref NONE;
   135     val old_handler = Signals.inqHandler Signals.sigINT;
   136   in
   137     SMLofNJ.Cont.callcc (fn cont =>
   138       (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => (interrupted := true; cont)));
   139       result := capture f x));
   140     Signals.setHandler (Signals.sigINT, old_handler);
   141     if ! interrupted then raise Interrupt else release (! result)
   142   end;
   143 
   144 end;
   145 
   146 
   147 (** Signal handling: emulation of the Poly/ML Signal structure. Note that types
   148     Posix.Signal.signal and Signals.signal differ **)
   149 
   150 structure IsaSignal =
   151 struct
   152 
   153 datatype sig_handle = SIG_DFL | SIG_IGN | SIG_HANDLE of Signals.signal -> unit;
   154 
   155 (*From the SML/NJ documentation: "HANDLER(f) installs a handler for a
   156   signal. When signal is delivered to the process, the execution state
   157   of the current thread will be bundled up as a continuation k, then
   158   f(signal,n,k) will be called. The number n is the number of times
   159   signal has been signalled since the last time f was invoked for it."*)
   160 
   161 fun toAction SIG_DFL = Signals.DEFAULT
   162   | toAction SIG_IGN = Signals.IGNORE
   163   | toAction (SIG_HANDLE iu) =
   164       Signals.HANDLER (fn (signo,_,cont) => (iu signo; cont));
   165 
   166 (*The types are correct, but I'm not sure about the semantics!*)
   167 fun fromAction Signals.DEFAULT = SIG_DFL
   168   | fromAction Signals.IGNORE = SIG_IGN
   169   | fromAction (Signals.HANDLER f) =
   170       SIG_HANDLE (fn signo => SMLofNJ.Cont.callcc (fn k => (f (signo,0,k); ())));
   171 
   172 (*Poly/ML version has type  int * sig_handle -> sig_handle*)
   173 fun signal (signo, sh) = fromAction (Signals.setHandler (signo, toAction sh));
   174 
   175 val usr1 = UnixSignals.sigUSR1
   176 val usr2 = UnixSignals.sigUSR2
   177 
   178 end;
   179 
   180 
   181 (** OS related **)
   182 
   183 (* system command execution *)
   184 
   185 (*execute Unix command which doesn't take any input from stdin and
   186   sends its output to stdout; could be done more easily by Unix.execute,
   187   but that function doesn't use the PATH*)
   188 fun execute command =
   189   let
   190     val tmp_name = OS.FileSys.tmpName ();
   191     val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
   192     val result = TextIO.inputAll is;
   193   in
   194     TextIO.closeIn is;
   195     OS.FileSys.remove tmp_name;
   196     result
   197   end;
   198 
   199 (*plain version; with return code*)
   200 val system = OS.Process.system: string -> int;
   201 
   202 
   203 (* getenv *)
   204 
   205 fun getenv var =
   206   (case OS.Process.getEnv var of
   207     NONE => ""
   208   | SOME txt => txt);