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