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