src/Pure/ML-Systems/smlnj-0.93.ML
changeset 3594 193cc37e6f60
parent 2413 a00f0476e189
child 3595 fb25f00d1c70
equal deleted inserted replaced
3593:f53de7618ef8 3594:193cc37e6f60
     4     Copyright   1996  TU Muenchen
     4     Copyright   1996  TU Muenchen
     5 
     5 
     6 Compatibility file for Standard ML of New Jersey version 0.93.
     6 Compatibility file for Standard ML of New Jersey version 0.93.
     7 *)
     7 *)
     8 
     8 
     9 
     9 (*needs the Basis Library emulation*)
    10 use"basis.ML";
    10 use "basis.ML";
    11 
    11 
    12 
    12 
    13 (*** Poly/ML emulation ***)
    13 (** ML system related **)
       
    14 
       
    15 (* New Jersey ML parameters *)
       
    16 
       
    17 System.Control.Runtime.gcmessages := 0;
       
    18 
       
    19 
       
    20 (* Poly/ML emulation *)
    14 
    21 
    15 fun quit () = exit 0;
    22 fun quit () = exit 0;
    16 
    23 
    17 (*To limit the printing depth [divided by 2 for comparibility with Poly/ML]*)
    24 (*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
    18 fun print_depth n = (System.Control.Print.printDepth := n div 2;
    25 fun print_depth n =
    19                      System.Control.Print.printLength := n);
    26  (System.Control.Print.printDepth := n div 2;
       
    27   System.Control.Print.printLength := n);
    20 
    28 
    21 (*Interface for toplevel pretty printers, see also Pure/install_pp.ML*)
    29 
       
    30 (* timing *)
       
    31 
       
    32 local
       
    33   (*print microseconds, suppressing trailing zeroes*)
       
    34   fun umakestring 0 = ""
       
    35     | umakestring n =
       
    36         chr (ord "0" + n div 100000) ^ umakestring (10 * (n mod 100000));
       
    37 in
       
    38   (*a conditional timing function: applies f to () and, if the flag is true,
       
    39     prints its runtime*)
       
    40   fun cond_timeit flag f =
       
    41     if flag then
       
    42       let fun string_of_time (System.Timer.TIME {sec, usec}) =
       
    43               makestring sec ^ "." ^ (if usec=0 then "0" else umakestring usec)
       
    44           open System.Timer;
       
    45           val start = start_timer()
       
    46           val result = f();
       
    47           val nongc = check_timer(start)
       
    48           and gc = check_timer_gc(start);
       
    49           val both = add_time(nongc, gc)
       
    50       in  print("Non GC " ^ string_of_time nongc ^
       
    51                  "   GC " ^ string_of_time gc ^
       
    52                  "  both "^ string_of_time both ^ " secs\n");
       
    53           result
       
    54       end
       
    55     else f();
       
    56 end;
       
    57 
       
    58 
       
    59 (* toplevel pretty printing (see also Pure/install_pp.ML) *)
    22 
    60 
    23 fun make_pp path pprint =
    61 fun make_pp path pprint =
    24   let
    62   let
    25     open System.PrettyPrint;
    63     open System.PrettyPrint;
    26 
    64 
    34   end;
    72   end;
    35 
    73 
    36 fun install_pp (path, pp) = System.PrettyPrint.install_pp path pp;
    74 fun install_pp (path, pp) = System.PrettyPrint.install_pp path pp;
    37 
    75 
    38 
    76 
    39 (*** New Jersey ML parameters ***)
    77 (* ML command execution *)
    40 
    78 
    41 (* Suppresses Garbage Collection messages;  default was 2 *)
    79 fun use_string commands =
    42 System.Control.Runtime.gcmessages := 0;
    80   System.Compile.use_stream (open_string (implode commands));
    43 
    81 
    44 
    82 
    45 (*** Timing functions ***)
       
    46 
    83 
    47 (*Print microseconds, suppressing trailing zeroes*)
    84 (** OS related **)
    48 fun umakestring 0 = ""
       
    49   | umakestring n = chr(ord "0" + n div 100000) ^
       
    50                     umakestring(10 * (n mod 100000));
       
    51 
    85 
    52 (*A conditional timing function: applies f to () and, if the flag is true,
    86 (* system command execution *)
    53   prints its runtime. *)
    87 
    54 fun cond_timeit flag f =
    88 (*execute Unix command which doesn't take any input from stdin and
    55   if flag then
    89   sends its output to stdout; could be done more easily by IO.execute,
    56     let fun string_of_time (System.Timer.TIME {sec, usec}) =
    90   but that function seems to be buggy in SML/NJ 0.93*)
    57             makestring sec ^ "." ^ (if usec=0 then "0" else umakestring usec)
    91 fun execute command =
    58         open System.Timer;
    92   let
    59         val start = start_timer()
    93     val tmp_name = "/tmp/.isabelle-execute";	(*let's hope we win the race!*)
    60         val result = f();
    94     val is = (System.system (command ^ " > " ^ tmp_name); open_in tmp_name);
    61         val nongc = check_timer(start)
    95     val result = input (is, 999999);
    62         and gc = check_timer_gc(start);
    96   in
    63         val both = add_time(nongc, gc)
    97     close_in is;
    64     in  print("Non GC " ^ string_of_time nongc ^
    98     System.Unsafe.SysIO.unlink tmp_name;
    65                "   GC " ^ string_of_time gc ^
    99     result
    66                "  both "^ string_of_time both ^ " secs\n");
   100   end;
    67         result
       
    68     end
       
    69   else f();
       
    70 
   101 
    71 
   102 
    72 (*** File handling ***)
   103 (* file handling *)
    73 
   104 
    74 (*Get time of last modification; if file doesn't exist return an empty string*)
   105 (*Get time of last modification; if file doesn't exist return an empty string*)
    75 local
   106 local
    76     open System.Timer;
   107   open System.Timer System.Unsafe.SysIO;
    77     open System.Unsafe.SysIO;
       
    78 in
   108 in
    79   fun file_info "" = ""
   109   fun file_info "" = ""
    80     | file_info name = makestring (mtime (PATH name))  handle _ => "";
   110     | file_info name = makestring (mtime (PATH name)) handle _ => "";
    81 end;
   111 end;
    82 
   112 
    83 structure OS =
   113 structure OS =
       
   114 struct
       
   115   structure FileSys =
    84   struct
   116   struct
    85   structure FileSys =
   117     val chDir = System.Directory.cd;
    86     struct
   118     val remove = System.Unsafe.SysIO.unlink;
    87     val chDir = System.Directory.cd
   119     val getDir = System.Directory.getWD;
    88     val remove = System.Unsafe.SysIO.unlink       (*Delete a file *)
       
    89     val getDir = System.Directory.getWD           (*path of working directory*)
       
    90     end
       
    91   end;
   120   end;
       
   121 end;
       
   122 
       
   123 (*redefine to flush its output immediately -- temporary patch suggested
       
   124   by Kim Dam Petersen*)		(* FIXME !? *)
       
   125 val output = fn (s, t) => (output (s, t); flush_out s);
    92 
   126 
    93 
   127 
    94 (*** ML command execution ***)
   128 (* getenv *)
    95 
   129 
    96 fun use_string commands = 
   130 local
    97   System.Compile.use_stream (open_string (implode commands));
   131   fun drop_last [] = []
       
   132     | drop_last [x] = []
       
   133     | drop_last (x :: xs) = x :: drop_last xs;
       
   134 
       
   135   val drop_last_char = implode o drop_last o explode;
       
   136 in
       
   137   fun getenv var = drop_last_char
       
   138     (execute ("env | grep '^" ^ var ^ "=' | sed -e 's/" ^ var ^ "=//'"));
       
   139 end;
    98 
   140 
    99 
   141 
   100 (*** System command execution ***)
   142 (* non-ASCII input (see also Thy/symbol_input.ML) *)
   101 
       
   102 (*Execute an Unix command which doesn't take any input from stdin and
       
   103   sends its output to stdout.
       
   104   This could be done more easily by IO.execute, but that function
       
   105   seems to be buggy in SML/NJ 0.93.*)
       
   106 fun execute command =
       
   107   let val tmp_name = "isa_converted.tmp"
       
   108       val is = (System.system (command ^ " > " ^ tmp_name);
       
   109                 open_in tmp_name);
       
   110       val result = input (is, 999999);
       
   111   in close_in is;
       
   112      OS.FileSys.remove tmp_name;
       
   113      result
       
   114   end;
       
   115 
       
   116 (*redefine to flush its output immediately -- temporary patch suggested
       
   117   by Kim Dam Petersen*)
       
   118 val output = fn(s, t) => (output(s, t); flush_out s);
       
   119 
       
   120 
       
   121 (* symbol input *)
       
   122 
   143 
   123 val needs_filtered_use = false;
   144 val needs_filtered_use = false;