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