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