src/Pure/ML-Systems/polyml_common.ML
author wenzelm
Sun Mar 01 23:36:12 2009 +0100 (2009-03-01)
changeset 30190 479806475f3c
parent 30187 b92b3375e919
child 30619 0226c07352db
permissions -rw-r--r--
use long names for old-style fold combinators;
     1 (*  Title:      Pure/ML-Systems/polyml_common.ML
     2 
     3 Compatibility file for Poly/ML -- common part for 4.x and 5.x.
     4 *)
     5 
     6 exception Interrupt = SML90.Interrupt;
     7 
     8 use "ML-Systems/exn.ML";
     9 use "ML-Systems/multithreading.ML";
    10 use "ML-Systems/time_limit.ML";
    11 use "ML-Systems/system_shell.ML";
    12 use "ML-Systems/ml_name_space.ML";
    13 
    14 
    15 (** ML system and platform related **)
    16 
    17 val forget_structure = PolyML.Compiler.forgetStructure;
    18 
    19 
    20 (* Compiler options *)
    21 
    22 val ml_system_fix_ints = false;
    23 
    24 PolyML.Compiler.printInAlphabeticalOrder := false;
    25 PolyML.Compiler.maxInlineSize := 80;
    26 
    27 
    28 (* old Poly/ML emulation *)
    29 
    30 local
    31   val orig_exit = exit;
    32 in
    33   open PolyML;
    34   val exit = orig_exit;
    35   fun quit () = exit 0;
    36 end;
    37 
    38 
    39 (* restore old-style character / string functions *)
    40 
    41 val ord = SML90.ord;
    42 val chr = SML90.chr;
    43 val explode = SML90.explode;
    44 val implode = SML90.implode;
    45 
    46 
    47 (* compiler-independent timing functions *)
    48 
    49 fun start_timing () =
    50   let
    51     val timer = Timer.startCPUTimer ();
    52     val time = Timer.checkCPUTimer timer;
    53   in (timer, time) end;
    54 
    55 fun end_timing (timer, {sys, usr}) =
    56   let
    57     open Time;  (*...for Time.toString, Time.+ and Time.- *)
    58     val {sys = sys2, usr = usr2} = Timer.checkCPUTimer timer;
    59     val user = usr2 - usr;
    60     val all = user + sys2 - sys;
    61     val message = "User " ^ toString user ^ "  All "^ toString all ^ " secs" handle Time => "";
    62   in {message = message, user = user, all = all} end;
    63 
    64 fun check_timer timer =
    65   let
    66     val {sys, usr} = Timer.checkCPUTimer timer;
    67     val gc = Timer.checkGCTime timer;    (* FIXME already included in usr? *)
    68   in (sys, usr, gc) end;
    69 
    70 
    71 (* prompts *)
    72 
    73 fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2);
    74 
    75 
    76 (* toplevel pretty printing (see also Pure/pure_setup.ML) *)
    77 
    78 fun make_pp _ pprint (str, blk, brk, en) _ _ obj =
    79   pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0),
    80     fn () => brk (99999, 0), en);
    81 
    82 (*print depth*)
    83 local
    84   val depth = ref 10;
    85 in
    86   fun get_print_depth () = ! depth;
    87   fun print_depth n = (depth := n; PolyML.print_depth n);
    88 end;
    89 
    90 
    91 
    92 (** interrupts **)
    93 
    94 local
    95 
    96 val sig_int = 2;
    97 val default_handler = Signal.SIG_HANDLE (fn _ => Process.interruptConsoleProcesses ());
    98 
    99 val _ = Signal.signal (sig_int, default_handler);
   100 val _ = PolyML.onEntry (fn () => (Signal.signal (sig_int, default_handler); ()));
   101 
   102 fun change_signal new_handler f x =
   103   let
   104     (*RACE wrt. other signals!*)
   105     val old_handler = Signal.signal (sig_int, new_handler);
   106     val result = Exn.capture (f old_handler) x;
   107     val _ = Signal.signal (sig_int, old_handler);
   108   in Exn.release result end;
   109 
   110 in
   111 
   112 fun interruptible f = change_signal default_handler (fn _ => f);
   113 
   114 fun uninterruptible f =
   115   change_signal Signal.SIG_IGN
   116     (fn old_handler => f (fn g => change_signal old_handler (fn _ => g)));
   117 
   118 end;
   119 
   120 
   121 
   122 (** OS related **)
   123 
   124 (* current directory *)
   125 
   126 val cd = OS.FileSys.chDir;
   127 val pwd = OS.FileSys.getDir;
   128 
   129 fun process_id () =
   130   Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord (Posix.ProcEnv.getpid ())));
   131 
   132 
   133 (* getenv *)
   134 
   135 fun getenv var =
   136   (case OS.Process.getEnv var of
   137     NONE => ""
   138   | SOME txt => txt);
   139 
   140 
   141 (* profile execution *)
   142 
   143 fun profile 0 f x = f x
   144   | profile n f x =
   145       let
   146         val _ = RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler n;
   147         val res = Exn.capture f x;
   148         val _ = RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler 0;
   149       in Exn.release res end;
   150