src/Pure/ML-Systems/smlnj.ML
author wenzelm
Fri Oct 03 21:06:36 2008 +0200 (2008-10-03)
changeset 28488 18fea7e88ea1
parent 28443 de653f1ad78b
child 29564 f8b933a62151
permissions -rw-r--r--
removed obsolete Posix/Signal compatibility wrappers;
plain process_id function;
     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 exception Interrupt;
     8 
     9 use "ML-Systems/proper_int.ML";
    10 use "ML-Systems/overloading_smlnj.ML";
    11 use "ML-Systems/exn.ML";
    12 use "ML-Systems/universal.ML";
    13 use "ML-Systems/thread_dummy.ML";
    14 use "ML-Systems/multithreading.ML";
    15 use "ML-Systems/system_shell.ML";
    16 use "ML-Systems/ml_name_space.ML";
    17 
    18 
    19 (*low-level pointer equality*)
    20 
    21 CM.autoload "$smlnj/init/init.cmi";
    22 val pointer_eq = InlineT.ptreql;
    23 
    24 
    25 (* restore old-style character / string functions *)
    26 
    27 val ord     = mk_int o SML90.ord;
    28 val chr     = SML90.chr o dest_int;
    29 val explode = SML90.explode;
    30 val implode = SML90.implode;
    31 
    32 
    33 (* New Jersey ML parameters *)
    34 
    35 val _ =
    36  (Control.Print.printLength := 1000;
    37   Control.Print.printDepth := 350;
    38   Control.Print.stringDepth := 250;
    39   Control.Print.signatures := 2;
    40   Control.MC.matchRedundantError := false);
    41 
    42 
    43 (* Poly/ML emulation *)
    44 
    45 val exit = exit o dest_int;
    46 fun quit () = exit 0;
    47 
    48 (*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
    49 local
    50   val depth = ref (10: int);
    51 in
    52   fun get_print_depth () = ! depth;
    53   fun print_depth n =
    54    (depth := n;
    55     Control.Print.printDepth := dest_int n div 2;
    56     Control.Print.printLength := dest_int n);
    57 end;
    58 
    59 
    60 (* compiler-independent timing functions *)
    61 
    62 fun start_timing () =
    63   let val CPUtimer = Timer.startCPUTimer();
    64       val time = Timer.checkCPUTimer(CPUtimer)
    65   in  (CPUtimer,time)  end;
    66 
    67 fun end_timing (CPUtimer, {sys,usr}) =
    68   let open Time  (*...for Time.toString, Time.+ and Time.- *)
    69       val {sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
    70   in  "User " ^ toString (usr2-usr) ^
    71       "  All "^ toString (sys2-sys + usr2-usr) ^
    72       " secs"
    73       handle Time => ""
    74   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);