src/Pure/ML-Systems/smlnj.ML
author wenzelm
Sun Mar 01 16:21:33 2009 +0100 (2009-03-01)
changeset 30187 b92b3375e919
parent 29564 f8b933a62151
child 30619 0226c07352db
permissions -rw-r--r--
end_timing: generalized result -- message plus with explicit time values;
     1 (*  Title:      Pure/ML-Systems/smlnj.ML
     2 
     3 Compatibility file for Standard ML of New Jersey 110 or later.
     4 *)
     5 
     6 exception Interrupt;
     7 
     8 use "ML-Systems/proper_int.ML";
     9 use "ML-Systems/overloading_smlnj.ML";
    10 use "ML-Systems/exn.ML";
    11 use "ML-Systems/universal.ML";
    12 use "ML-Systems/thread_dummy.ML";
    13 use "ML-Systems/multithreading.ML";
    14 use "ML-Systems/system_shell.ML";
    15 use "ML-Systems/ml_name_space.ML";
    16 
    17 
    18 (*low-level pointer equality*)
    19 
    20 CM.autoload "$smlnj/init/init.cmi";
    21 val pointer_eq = InlineT.ptreql;
    22 
    23 
    24 (* restore old-style character / string functions *)
    25 
    26 val ord     = mk_int o SML90.ord;
    27 val chr     = SML90.chr o dest_int;
    28 val explode = SML90.explode;
    29 val implode = SML90.implode;
    30 
    31 
    32 (* New Jersey ML parameters *)
    33 
    34 val _ =
    35  (Control.Print.printLength := 1000;
    36   Control.Print.printDepth := 350;
    37   Control.Print.stringDepth := 250;
    38   Control.Print.signatures := 2;
    39   Control.MC.matchRedundantError := false);
    40 
    41 
    42 (* Poly/ML emulation *)
    43 
    44 val exit = exit o dest_int;
    45 fun quit () = exit 0;
    46 
    47 (*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
    48 local
    49   val depth = ref (10: int);
    50 in
    51   fun get_print_depth () = ! depth;
    52   fun print_depth n =
    53    (depth := n;
    54     Control.Print.printDepth := dest_int n div 2;
    55     Control.Print.printLength := dest_int n);
    56 end;
    57 
    58 
    59 (* compiler-independent timing functions *)
    60 
    61 fun start_timing () =
    62   let
    63     val timer = Timer.startCPUTimer ();
    64     val time = Timer.checkCPUTimer timer;
    65   in (timer, time) end;
    66 
    67 fun end_timing (timer, {sys, usr}) =
    68   let
    69     open Time;  (*...for Time.toString, Time.+ and Time.- *)
    70     val {sys = sys2, usr = usr2} = Timer.checkCPUTimer timer;
    71     val user = usr2 - usr;
    72     val all = user + sys2 - sys;
    73     val message = "User " ^ toString user ^ "  All "^ toString all ^ " secs" handle Time => "";
    74   in {message = message, user = user, all = all} end;
    75 
    76 fun check_timer timer =
    77   let
    78     val {sys, usr} = Timer.checkCPUTimer timer;
    79     val gc = Timer.checkGCTime timer;    (* FIXME already included in usr? *)
    80   in (sys, usr, gc) end;
    81 
    82 
    83 (*prompts*)
    84 fun ml_prompts p1 p2 =
    85   (Control.primaryPrompt := p1; Control.secondaryPrompt := p2);
    86 
    87 (*dummy implementation*)
    88 fun profile (n: int) f x = f x;
    89 
    90 (*dummy implementation*)
    91 fun exception_trace f = f ();
    92 
    93 (*dummy implementation*)
    94 fun print x = x;
    95 
    96 (*dummy implementation*)
    97 fun makestring x = "dummy string for SML New Jersey";
    98 
    99 
   100 (* toplevel pretty printing (see also Pure/pure_setup.ML) *)
   101 
   102 fun make_pp path pprint =
   103   let
   104     open PrettyPrint;
   105 
   106     fun pp pps obj =
   107       pprint obj
   108         (string pps, openHOVBox pps o Rel o dest_int,
   109           fn wd => break pps {nsp=dest_int wd, offset=0}, fn () => newline pps,
   110           fn () => closeBox pps);
   111   in (path, pp) end;
   112 
   113 fun install_pp (path, pp) = CompilerPPTable.install_pp path pp;
   114 
   115 
   116 (* ML command execution *)
   117 
   118 fun use_text (tune: string -> string) _ _ (line: int, name) (print, err) verbose txt =
   119   let
   120     val ref out_orig = Control.Print.out;
   121 
   122     val out_buffer = ref ([]: string list);
   123     val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
   124     fun output () =
   125       let val str = implode (rev (! out_buffer))
   126       in String.substring (str, 0, Int.max (0, size str - 1)) end;
   127   in
   128     Control.Print.out := out;
   129     Backend.Interact.useStream (TextIO.openString (tune txt)) handle exn =>
   130       (Control.Print.out := out_orig;
   131         err ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn);
   132     Control.Print.out := out_orig;
   133     if verbose then print (output ()) else ()
   134   end;
   135 
   136 fun use_file tune str_of_pos name_space output verbose name =
   137   let
   138     val instream = TextIO.openIn name;
   139     val txt = Exn.release (Exn.capture TextIO.inputAll instream before TextIO.closeIn instream);
   140   in use_text tune str_of_pos name_space (1, name) output verbose txt end;
   141 
   142 fun forget_structure name =
   143   use_text (fn x => x) (fn _ => "") () (1, "ML") (TextIO.print, fn s => raise Fail s) false
   144     ("structure " ^ name ^ " = struct end");
   145 
   146 
   147 
   148 (** interrupts **)
   149 
   150 local
   151 
   152 fun change_signal new_handler f x =
   153   let
   154     val old_handler = Signals.setHandler (Signals.sigINT, new_handler);
   155     val result = Exn.capture (f old_handler) x;
   156     val _ = Signals.setHandler (Signals.sigINT, old_handler);
   157   in Exn.release result end;
   158 
   159 in
   160 
   161 fun interruptible (f: 'a -> 'b) x =
   162   let
   163     val result = ref (Exn.Exn Interrupt: 'b Exn.result);
   164     val old_handler = Signals.inqHandler Signals.sigINT;
   165   in
   166     SMLofNJ.Cont.callcc (fn cont =>
   167       (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => cont));
   168         result := Exn.capture f x));
   169     Signals.setHandler (Signals.sigINT, old_handler);
   170     Exn.release (! result)
   171   end;
   172 
   173 fun uninterruptible f =
   174   change_signal Signals.IGNORE
   175     (fn old_handler => f (fn g => change_signal old_handler (fn _ => g)));
   176 
   177 end;
   178 
   179 
   180 (* basis library fixes *)
   181 
   182 structure TextIO =
   183 struct
   184   open TextIO;
   185   fun inputLine is = TextIO.inputLine is
   186     handle IO.Io _ => raise Interrupt;
   187 end;
   188 
   189 
   190 
   191 (** OS related **)
   192 
   193 (* current directory *)
   194 
   195 val cd = OS.FileSys.chDir;
   196 val pwd = OS.FileSys.getDir;
   197 
   198 
   199 (* system command execution *)
   200 
   201 val system_out = (fn (output, rc) => (output, mk_int rc)) o system_out;
   202 
   203 
   204 (*Convert a process ID to a decimal string (chiefly for tracing)*)
   205 fun process_id pid =
   206   Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord (Posix.ProcEnv.getpid ())));
   207 
   208 
   209 (* getenv *)
   210 
   211 fun getenv var =
   212   (case OS.Process.getEnv var of
   213     NONE => ""
   214   | SOME txt => txt);