src/Pure/NJ1xx.ML
author paulson
Wed, 20 Mar 1996 18:39:59 +0100
changeset 1591 7fa0265dcd13
parent 1577 a84cc626ea69
child 2076 ec8857a115af
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/NJ1xx.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 1.xx.
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
fun exit 0 = OS.Process.exit OS.Process.success
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    14
  | exit _ = OS.Process.exit OS.Process.failure;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    15
fun quit () = exit 0;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    16
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    17
(*To change the current directory*)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    18
val cd = OS.FileSys.chDir;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    19
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    20
(*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
    21
fun print_depth n = (Compiler.Control.Print.printDepth := n div 2;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    22
                     Compiler.Control.Print.printLength := n);
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
(*Interface for toplevel pretty printers, see also Pure/install_pp.ML*)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    25
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    26
fun make_pp path pprint =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    27
  let
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    28
    open Compiler.PrettyPrint;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    29
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    30
    fun pp pps obj =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    31
      pprint obj
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    32
        (add_string pps, begin_block pps INCONSISTENT,
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    33
          fn wd => add_break pps (wd, 0), fn () => add_newline pps,
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    34
          fn () => end_block pps);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    35
  in
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    36
    (path, pp)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    37
  end;
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
fun install_pp (path, pp) = Compiler.PPTable.install_pp path pp;
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
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    42
(*** New Jersey ML parameters ***)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    43
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    44
(* Suppresses Garbage Collection messages; doesn't work yet *)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    45
(*System.Runtime.gc 0;*)
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
val _ = (Compiler.Control.Print.printLength := 1000;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    48
         Compiler.Control.Print.printDepth := 350;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    49
         Compiler.Control.Print.stringDepth := 250;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    50
         Compiler.Control.Print.signatures := 2);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    51
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    52
(*** Character/string functions which are compatibel with 0.93 and Poly/ML ***)
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
val ord = Char.ord o hd o explode;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    55
val chr = str o Char.chr;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    56
val explode = (map str) o String.explode;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    57
val implode = String.concat;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    58
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    59
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    60
(*** Timing functions ***)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    61
1492
4e617b8f97ab cond_timeit now uses totalCPUTimer instead of starting new ones every
clasohm
parents: 1480
diff changeset
    62
val CPUtimer = Timer.totalCPUTimer();
4e617b8f97ab cond_timeit now uses totalCPUTimer instead of starting new ones every
clasohm
parents: 1480
diff changeset
    63
1480
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    64
(*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
    65
  prints its runtime. *)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    66
fun cond_timeit flag f =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    67
  if flag then
1492
4e617b8f97ab cond_timeit now uses totalCPUTimer instead of starting new ones every
clasohm
parents: 1480
diff changeset
    68
    let open Time;
1480
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    69
        open Timer;
1492
4e617b8f97ab cond_timeit now uses totalCPUTimer instead of starting new ones every
clasohm
parents: 1480
diff changeset
    70
        val {gc=gct1,sys=syst1,usr=usrt1} = checkCPUTimer(CPUtimer);
1480
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    71
        val result = f();
1492
4e617b8f97ab cond_timeit now uses totalCPUTimer instead of starting new ones every
clasohm
parents: 1480
diff changeset
    72
        val {gc=gct2,sys=syst2,usr=usrt2} = checkCPUTimer(CPUtimer)
4e617b8f97ab cond_timeit now uses totalCPUTimer instead of starting new ones every
clasohm
parents: 1480
diff changeset
    73
    in  print("Non GC " ^ toString (usrt2-usrt1) ^
4e617b8f97ab cond_timeit now uses totalCPUTimer instead of starting new ones every
clasohm
parents: 1480
diff changeset
    74
              "   GC " ^ toString (gct2-gct1) ^
4e617b8f97ab cond_timeit now uses totalCPUTimer instead of starting new ones every
clasohm
parents: 1480
diff changeset
    75
              "  both+sys "^ toString (syst2-syst1 + usrt2-usrt1 + gct2-gct1) ^
4e617b8f97ab cond_timeit now uses totalCPUTimer instead of starting new ones every
clasohm
parents: 1480
diff changeset
    76
              " secs\n");
1480
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    77
        result
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    78
    end
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    79
  else f();
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    80
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    81
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    82
(*** File handling ***)
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
(*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
    85
local
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    86
    open Timer;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    87
    open Posix.FileSys;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    88
in
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    89
  fun file_info "" = ""
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    90
    | file_info name = Time.toString (ST.mtime (stat name))  handle _ => "";
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    91
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    92
  val delete_file = unlink;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    93
end;
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
(*Get pathname of current working directory *)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    96
fun pwd () = OS.FileSys.getDir ();
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    97
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
(*** ML command execution ***)
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
fun use_string commands = 
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   102
   Compiler.Interact.use_stream (open_string (implode commands));
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   103
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   104
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   105
(*** System command execution ***)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   106
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   107
(*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
   108
  sends its output to stdout.
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   109
  This could be done more easily by Unix.execute, but that function
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   110
  doesn't use the PATH.*)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   111
fun execute command =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   112
  let val tmp_name = "isa_converted.tmp"
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   113
      val is = (OS.Process.system (command ^ " > " ^ tmp_name);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   114
                open_in tmp_name);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   115
      val result = input (is, 999999);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   116
  in close_in is;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   117
     delete_file tmp_name;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   118
     result
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   119
  end;
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
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   122
(*For use in Makefiles -- saves space*)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   123
fun xML filename banner =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   124
  let val runtime = List.hd (SMLofNJ.getAllArgs())
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   125
      val exec_file = IO.open_out filename
1577
a84cc626ea69 added @SMLdebug=/dev/null to supress GC messages
clasohm
parents: 1492
diff changeset
   126
1480
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   127
      val _ = IO.output (exec_file,
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   128
			 String.concat
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   129
			 ["#!/bin/sh\n",
1577
a84cc626ea69 added @SMLdebug=/dev/null to supress GC messages
clasohm
parents: 1492
diff changeset
   130
			  runtime, " @SMLdebug=/dev/null @SMLload=", filename,
a84cc626ea69 added @SMLdebug=/dev/null to supress GC messages
clasohm
parents: 1492
diff changeset
   131
                          ".heap\n"])
a84cc626ea69 added @SMLdebug=/dev/null to supress GC messages
clasohm
parents: 1492
diff changeset
   132
                             (*"@SMLdebug=..." sends GC messages to /dev/null*)
a84cc626ea69 added @SMLdebug=/dev/null to supress GC messages
clasohm
parents: 1492
diff changeset
   133
1480
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   134
	val _ = IO.close_out exec_file;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   135
	val _ = OS.Process.system ("chmod a+x " ^ filename)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   136
    in exportML (filename^".heap");
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   137
       print(banner^"\n")
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   138
    end;