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