src/Pure/NJ093.ML
author paulson
Wed, 27 Nov 1996 10:45:58 +0100
changeset 2242 fa8c6c695a97
parent 2190 97a2d44a8013
child 2408 acddf41dbbf7
permissions -rw-r--r--
Basis library emulation, especially of I/O operations
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
2190
97a2d44a8013 Introduction of structure Int
paulson
parents: 1480
diff changeset
     9
2242
fa8c6c695a97 Basis library emulation, especially of I/O operations
paulson
parents: 2190
diff changeset
    10
use"basis.ML";
2190
97a2d44a8013 Introduction of structure Int
paulson
parents: 1480
diff changeset
    11
1480
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    12
(*** Poly/ML emulation ***)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    13
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    14
(*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
    15
val exit = System.Unsafe.CInterface.exit;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    16
fun quit () = exit 0;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    17
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    18
(*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
    19
fun print_depth n = (System.Control.Print.printDepth := n div 2;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    20
                     System.Control.Print.printLength := n);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    21
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    22
(*Interface for toplevel pretty printers, see also Pure/install_pp.ML*)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    23
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    24
fun make_pp path pprint =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    25
  let
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    26
    open System.PrettyPrint;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    27
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    28
    fun pp pps obj =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    29
      pprint obj
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    30
        (add_string pps, begin_block pps INCONSISTENT,
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    31
          fn wd => add_break pps (wd, 0), fn () => add_newline pps,
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    32
          fn () => end_block pps);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    33
  in
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    34
    (path, pp)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    35
  end;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    36
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    37
fun install_pp (path, pp) = System.PrettyPrint.install_pp path pp;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    38
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
(*** New Jersey ML parameters ***)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    41
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    42
(* Suppresses Garbage Collection messages;  default was 2 *)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    43
System.Control.Runtime.gcmessages := 0;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    44
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
(*** Timing functions ***)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    47
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    48
(*Print microseconds, suppressing trailing zeroes*)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    49
fun umakestring 0 = ""
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    50
  | umakestring n = chr(ord "0" + n div 100000) ^
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    51
                    umakestring(10 * (n mod 100000));
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    52
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    53
(*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
    54
  prints its runtime. *)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    55
fun cond_timeit flag f =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    56
  if flag then
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    57
    let fun string_of_time (System.Timer.TIME {sec, usec}) =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    58
            makestring sec ^ "." ^ (if usec=0 then "0" else umakestring usec)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    59
        open System.Timer;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    60
        val start = start_timer()
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    61
        val result = f();
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    62
        val nongc = check_timer(start)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    63
        and gc = check_timer_gc(start);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    64
        val both = add_time(nongc, gc)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    65
    in  print("Non GC " ^ string_of_time nongc ^
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    66
               "   GC " ^ string_of_time gc ^
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    67
               "  both "^ string_of_time both ^ " secs\n");
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    68
        result
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    69
    end
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    70
  else f();
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    71
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
(*** File handling ***)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    74
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    75
(*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
    76
local
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    77
    open System.Timer;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    78
    open System.Unsafe.SysIO;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    79
in
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    80
  fun file_info "" = ""
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    81
    | file_info name = makestring (mtime (PATH name))  handle _ => "";
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    82
end;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    83
2242
fa8c6c695a97 Basis library emulation, especially of I/O operations
paulson
parents: 2190
diff changeset
    84
structure OS =
fa8c6c695a97 Basis library emulation, especially of I/O operations
paulson
parents: 2190
diff changeset
    85
  struct
fa8c6c695a97 Basis library emulation, especially of I/O operations
paulson
parents: 2190
diff changeset
    86
  structure FileSys =
fa8c6c695a97 Basis library emulation, especially of I/O operations
paulson
parents: 2190
diff changeset
    87
    struct
fa8c6c695a97 Basis library emulation, especially of I/O operations
paulson
parents: 2190
diff changeset
    88
    val OS.FileSys.chDir = System.Directory.cd
fa8c6c695a97 Basis library emulation, especially of I/O operations
paulson
parents: 2190
diff changeset
    89
    val remove = System.Unsafe.SysIO.unlink       (*Delete a file *)
fa8c6c695a97 Basis library emulation, especially of I/O operations
paulson
parents: 2190
diff changeset
    90
    val getDir = System.Directory.getWD           (*path of working directory*)
fa8c6c695a97 Basis library emulation, especially of I/O operations
paulson
parents: 2190
diff changeset
    91
    end
fa8c6c695a97 Basis library emulation, especially of I/O operations
paulson
parents: 2190
diff changeset
    92
  end;
1480
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    93
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    94
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    95
(*** ML command execution ***)
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
fun use_string commands = 
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    98
  System.Compile.use_stream (open_string (implode commands));
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    99
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   100
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   101
(*** System command execution ***)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   102
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   103
(*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
   104
  sends its output to stdout.
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   105
  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
   106
  seems to be buggy in SML/NJ 0.93.*)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   107
fun execute command =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   108
  let val tmp_name = "isa_converted.tmp"
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   109
      val is = (System.system (command ^ " > " ^ tmp_name);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   110
                open_in tmp_name);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   111
      val result = input (is, 999999);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   112
  in close_in is;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   113
     delete_file tmp_name;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   114
     result
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   115
  end;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   116
2242
fa8c6c695a97 Basis library emulation, especially of I/O operations
paulson
parents: 2190
diff changeset
   117
(*redefine to flush its output immediately -- temporary patch suggested
fa8c6c695a97 Basis library emulation, especially of I/O operations
paulson
parents: 2190
diff changeset
   118
  by Kim Dam Petersen*)
fa8c6c695a97 Basis library emulation, especially of I/O operations
paulson
parents: 2190
diff changeset
   119
val output = fn(s, t) => (output(s, t); flush_out s);
1480
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   120
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   121
(*For use in Makefiles -- saves space*)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   122
fun xML filename banner = (exportML filename;  print(banner^"\n"));