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