src/Pure/ML-Systems/smlnj.ML
author wenzelm
Thu Dec 20 21:11:58 2007 +0100 (2007-12-20)
changeset 25732 308315ee2b6d
parent 24688 a5754ca5c510
child 26084 a7475459c740
permissions -rw-r--r--
added ML-Systems/universal.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/proper_int.ML";
     8 use "ML-Systems/overloading_smlnj.ML";
     9 use "ML-Systems/exn.ML";
    10 use "ML-Systems/universal.ML";
    11 use "ML-Systems/multithreading.ML";
    12 
    13 
    14 (*low-level pointer equality*)
    15 
    16 CM.autoload "$smlnj/init/init.cmi";
    17 val pointer_eq = InlineT.ptreql;
    18 
    19 
    20 (* restore old-style character / string functions *)
    21 
    22 val ord     = mk_int o SML90.ord;
    23 val chr     = SML90.chr o dest_int;
    24 val explode = SML90.explode;
    25 val implode = SML90.implode;
    26 
    27 
    28 (* New Jersey ML parameters *)
    29 
    30 val _ =
    31  (Control.Print.printLength := 1000;
    32   Control.Print.printDepth := 350;
    33   Control.Print.stringDepth := 250;
    34   Control.Print.signatures := 2;
    35   Control.MC.matchRedundantError := false);
    36 
    37 
    38 (* Poly/ML emulation *)
    39 
    40 val exit = exit o dest_int;
    41 fun quit () = exit 0;
    42 
    43 (*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
    44 local
    45   val depth = ref (10: int);
    46 in
    47   fun get_print_depth () = ! depth;
    48   fun print_depth n =
    49    (depth := n;
    50     Control.Print.printDepth := dest_int n div 2;
    51     Control.Print.printLength := dest_int n);
    52 end;
    53 
    54 (* compiler-independent timing functions *)
    55 
    56 fun start_timing () =
    57   let val CPUtimer = Timer.startCPUTimer();
    58       val time = Timer.checkCPUTimer(CPUtimer)
    59   in  (CPUtimer,time)  end;
    60 
    61 fun end_timing (CPUtimer, {sys,usr}) =
    62   let open Time  (*...for Time.toString, Time.+ and Time.- *)
    63       val {sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
    64   in  "User " ^ toString (usr2-usr) ^
    65       "  All "^ toString (sys2-sys + usr2-usr) ^
    66       " secs"
    67       handle Time => ""
    68   end;
    69 
    70 fun check_timer timer =
    71   let
    72     val {sys, usr} = Timer.checkCPUTimer timer;
    73     val gc = Timer.checkGCTime timer;    (* FIXME already included in usr? *)
    74   in (sys, usr, gc) end;
    75 
    76 
    77 (*prompts*)
    78 fun ml_prompts p1 p2 =
    79   (Control.primaryPrompt := p1; Control.secondaryPrompt := p2);
    80 
    81 (*dummy implementation*)
    82 fun profile (n: int) f x = f x;
    83 
    84 (*dummy implementation*)
    85 fun exception_trace f = f ();
    86 
    87 (*dummy implementation*)
    88 fun print x = x;
    89 
    90 (*dummy implementation*)
    91 fun makestring x = "dummy string for SML New Jersey";
    92 
    93 
    94 (* toplevel pretty printing (see also Pure/pure_setup.ML) *)
    95 
    96 fun make_pp path pprint =
    97   let
    98     open PrettyPrint;
    99 
   100     fun pp pps obj =
   101       pprint obj
   102         (string pps, openHOVBox pps o Rel o dest_int,
   103           fn wd => break pps {nsp=dest_int wd, offset=0}, fn () => newline pps,
   104           fn () => closeBox pps);
   105   in (path, pp) end;
   106 
   107 fun install_pp (path, pp) = CompilerPPTable.install_pp path pp;
   108 
   109 
   110 (* ML command execution *)
   111 
   112 fun use_text (tune: string -> string) name (print, err) verbose txt =
   113   let
   114     val ref out_orig = Control.Print.out;
   115 
   116     val out_buffer = ref ([]: string list);
   117     val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
   118     fun output () =
   119       let val str = implode (rev (! out_buffer))
   120       in String.substring (str, 0, Int.max (0, size str - 1)) end;
   121   in
   122     Control.Print.out := out;
   123     Backend.Interact.useStream (TextIO.openString (tune txt)) handle exn =>
   124       (Control.Print.out := out_orig;
   125         err ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn);
   126     Control.Print.out := out_orig;
   127     if verbose then print (output ()) else ()
   128   end;
   129 
   130 fun use_file tune output verbose name =
   131   let
   132     val instream = TextIO.openIn name;
   133     val txt = TextIO.inputAll instream before TextIO.closeIn instream;
   134   in use_text tune name output verbose txt end;
   135 
   136 
   137 
   138 (** interrupts **)
   139 
   140 exception Interrupt;
   141 
   142 fun ignore_interrupt f x =
   143   let
   144     val old_handler = Signals.setHandler (Signals.sigINT, Signals.IGNORE);
   145     val result = Exn.capture f x;
   146     val _ = Signals.setHandler (Signals.sigINT, old_handler);
   147   in Exn.release result end;
   148 
   149 fun raise_interrupt f x =
   150   let
   151     val interrupted = ref false;
   152     val result = ref (Exn.Result ());
   153     val old_handler = Signals.inqHandler Signals.sigINT;
   154   in
   155     SMLofNJ.Cont.callcc (fn cont =>
   156       (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => (interrupted := true; cont)));
   157       result := Exn.capture f x));
   158     Signals.setHandler (Signals.sigINT, old_handler);
   159     if ! interrupted then raise Interrupt else Exn.release (! result)
   160   end;
   161 
   162 
   163 (* basis library fixes *)
   164 
   165 structure TextIO =
   166 struct
   167   open TextIO;
   168   fun inputLine is = TextIO.inputLine is
   169     handle IO.Io _ => raise Interrupt;
   170 end;
   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);