src/Pure/NJ093.ML
author paulson
Wed, 20 Mar 1996 18:39:59 +0100
changeset 1591 7fa0265dcd13
parent 1480 85ecd3439e01
child 2190 97a2d44a8013
permissions -rw-r--r--
New module for display/printing operations, taken from drule.ML
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1480
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
     1
(*  Title:      Pure/NJ093.ML
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
     2
    ID:         $Id$
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
     3
    Author:     Carsten Clasohm, TU Muenchen
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
     4
    Copyright   1996  TU Muenchen
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
     5
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
     6
Compatibility file for Standard ML of New Jersey version 0.93.
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
     7
*)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
     8
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
     9
(*** Poly/ML emulation ***)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    10
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    11
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    12
(*To exit the system with an exit code -- an alternative to ^D *)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    13
val exit = System.Unsafe.CInterface.exit;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    14
fun quit () = exit 0;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    15
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    16
(*To change the current directory*)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    17
val cd = System.Directory.cd;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    18
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    19
(*To limit the printing depth [divided by 2 for comparibility with Poly/ML]*)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    20
fun print_depth n = (System.Control.Print.printDepth := n div 2;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    21
                     System.Control.Print.printLength := n);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    22
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    23
(*Interface for toplevel pretty printers, see also Pure/install_pp.ML*)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    24
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    25
fun make_pp path pprint =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    26
  let
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    27
    open System.PrettyPrint;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    28
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    29
    fun pp pps obj =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    30
      pprint obj
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    31
        (add_string pps, begin_block pps INCONSISTENT,
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    32
          fn wd => add_break pps (wd, 0), fn () => add_newline pps,
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    33
          fn () => end_block pps);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    34
  in
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    35
    (path, pp)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    36
  end;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    37
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    38
fun install_pp (path, pp) = System.PrettyPrint.install_pp path pp;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    39
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    40
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    41
(*** New Jersey ML parameters ***)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    42
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    43
(* Suppresses Garbage Collection messages;  default was 2 *)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    44
System.Control.Runtime.gcmessages := 0;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    45
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    46
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    47
(*** Timing functions ***)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    48
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    49
(*Print microseconds, suppressing trailing zeroes*)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    50
fun umakestring 0 = ""
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    51
  | umakestring n = chr(ord "0" + n div 100000) ^
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    52
                    umakestring(10 * (n mod 100000));
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    53
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    54
(*A conditional timing function: applies f to () and, if the flag is true,
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    55
  prints its runtime. *)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    56
fun cond_timeit flag f =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    57
  if flag then
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    58
    let fun string_of_time (System.Timer.TIME {sec, usec}) =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    59
            makestring sec ^ "." ^ (if usec=0 then "0" else umakestring usec)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    60
        open System.Timer;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    61
        val start = start_timer()
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    62
        val result = f();
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    63
        val nongc = check_timer(start)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    64
        and gc = check_timer_gc(start);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    65
        val both = add_time(nongc, gc)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    66
    in  print("Non GC " ^ string_of_time nongc ^
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    67
               "   GC " ^ string_of_time gc ^
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    68
               "  both "^ string_of_time both ^ " secs\n");
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    69
        result
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    70
    end
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    71
  else f();
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    72
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    73
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    74
(*** File handling ***)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    75
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    76
(*Get time of last modification; if file doesn't exist return an empty string*)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    77
local
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    78
    open System.Timer;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    79
    open System.Unsafe.SysIO;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    80
in
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    81
  fun file_info "" = ""
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    82
    | file_info name = makestring (mtime (PATH name))  handle _ => "";
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    83
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    84
  val delete_file = unlink;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    85
end;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    86
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    87
(*Get pathname of current working directory *)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    88
fun pwd () = System.Directory.getWD ();
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    89
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    90
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    91
(*** ML command execution ***)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    92
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    93
fun use_string commands = 
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    94
  System.Compile.use_stream (open_string (implode commands));
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    95
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    96
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    97
(*** System command execution ***)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    98
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    99
(*Execute an Unix command which doesn't take any input from stdin and
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   100
  sends its output to stdout.
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   101
  This could be done more easily by IO.execute, but that function
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   102
  seems to be buggy in SML/NJ 0.93.*)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   103
fun execute command =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   104
  let val tmp_name = "isa_converted.tmp"
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   105
      val is = (System.system (command ^ " > " ^ tmp_name);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   106
                open_in tmp_name);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   107
      val result = input (is, 999999);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   108
  in close_in is;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   109
     delete_file tmp_name;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   110
     result
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   111
  end;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   112
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   113
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   114
(*For use in Makefiles -- saves space*)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   115
fun xML filename banner = (exportML filename;  print(banner^"\n"));