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