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