src/Pure/NJ1xx.ML
author wenzelm
Fri, 28 Feb 1997 16:38:55 +0100
changeset 2692 484ec6ca0c50
parent 2408 acddf41dbbf7
child 3084 9844abe1de72
permissions -rw-r--r--
added Syntax/token_trans.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 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
    18
fun print_depth n = (Compiler.Control.Print.printDepth := n div 2;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    19
                     Compiler.Control.Print.printLength := n);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    20
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    21
(*Interface for toplevel pretty printers, see also Pure/install_pp.ML*)
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
fun make_pp path pprint =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    24
  let
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    25
    open Compiler.PrettyPrint;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    26
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    27
    fun pp pps obj =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    28
      pprint obj
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    29
        (add_string pps, begin_block pps INCONSISTENT,
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    30
          fn wd => add_break pps (wd, 0), fn () => add_newline pps,
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    31
          fn () => end_block pps);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    32
  in
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    33
    (path, pp)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    34
  end;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    35
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    36
fun install_pp (path, pp) = Compiler.PPTable.install_pp path pp;
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
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    39
(*** New Jersey ML parameters ***)
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
(* Suppresses Garbage Collection messages; doesn't work yet *)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    42
(*System.Runtime.gc 0;*)
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
val _ = (Compiler.Control.Print.printLength := 1000;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    45
         Compiler.Control.Print.printDepth := 350;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    46
         Compiler.Control.Print.stringDepth := 250;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    47
         Compiler.Control.Print.signatures := 2);
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    48
2246
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
    49
(*** Character/string functions which are compatible with 0.93 and Poly/ML ***)
1480
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    50
2246
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
    51
fun ord s = Char.ord (String.sub(s,0));
1480
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    52
val chr = str o Char.chr;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    53
val explode = (map str) o String.explode;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    54
val implode = String.concat;
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    55
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    56
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    57
(*** Timing functions ***)
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
(*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
    60
  prints its runtime. *)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    61
fun cond_timeit flag f =
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    62
  if flag then
2076
ec8857a115af cond_timeit now catches exception Time, which sml/nj
paulson
parents: 1577
diff changeset
    63
    let open Time  (*...for Time.toString, Time.+ and Time.- *)
2101
5d44339454a4 Moving the CPUtimer declaration into cond_timeit should
paulson
parents: 2093
diff changeset
    64
	val CPUtimer = Timer.startCPUTimer();
5d44339454a4 Moving the CPUtimer declaration into cond_timeit should
paulson
parents: 2093
diff changeset
    65
        val {gc=gct1,sys=syst1,usr=usrt1} = Timer.checkCPUTimer(CPUtimer);
1480
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    66
        val result = f();
2076
ec8857a115af cond_timeit now catches exception Time, which sml/nj
paulson
parents: 1577
diff changeset
    67
        val {gc=gct2,sys=syst2,usr=usrt2} = Timer.checkCPUTimer(CPUtimer)
ec8857a115af cond_timeit now catches exception Time, which sml/nj
paulson
parents: 1577
diff changeset
    68
    in  print("User " ^ toString (usrt2-usrt1) ^
ec8857a115af cond_timeit now catches exception Time, which sml/nj
paulson
parents: 1577
diff changeset
    69
              "  GC " ^ toString (gct2-gct1) ^
ec8857a115af cond_timeit now catches exception Time, which sml/nj
paulson
parents: 1577
diff changeset
    70
              "  All "^ toString (syst2-syst1 + usrt2-usrt1 + gct2-gct1) ^
ec8857a115af cond_timeit now catches exception Time, which sml/nj
paulson
parents: 1577
diff changeset
    71
              " secs\n")
ec8857a115af cond_timeit now catches exception Time, which sml/nj
paulson
parents: 1577
diff changeset
    72
	  handle Time => ();
1480
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    73
        result
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    74
    end
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    75
  else f();
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    76
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    77
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    78
(*** File handling ***)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    79
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    80
(*Get time of last modification; if file doesn't exist return an empty string*)
2246
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
    81
fun file_info "" = ""
2304
618b545fe9c4 Simplified file_info using OS.FileSys instead of Posix.FileSys
paulson
parents: 2288
diff changeset
    82
  | file_info name = Time.toString (OS.FileSys.modTime name) handle _ =>"";
1480
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
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    85
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    86
(*** ML command execution ***)
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    87
2288
16e7a5adb679 Made comments more explicit
paulson
parents: 2246
diff changeset
    88
16e7a5adb679 Made comments more explicit
paulson
parents: 2246
diff changeset
    89
(*For version 109.21 and later:
16e7a5adb679 Made comments more explicit
paulson
parents: 2246
diff changeset
    90
val use_string = Compiler.Interact.useStream o TextIO.openString o implode;
16e7a5adb679 Made comments more explicit
paulson
parents: 2246
diff changeset
    91
*)
16e7a5adb679 Made comments more explicit
paulson
parents: 2246
diff changeset
    92
16e7a5adb679 Made comments more explicit
paulson
parents: 2246
diff changeset
    93
(*For versions prior to 109.21:*)
1480
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    94
fun use_string commands = 
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
    95
   Compiler.Interact.use_stream (open_string (implode commands));
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 Unix.execute, but that function
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   102
  doesn't use the PATH.*)
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 = (OS.Process.system (command ^ " > " ^ tmp_name);
2246
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   106
                TextIO.openIn tmp_name);
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   107
      val result = TextIO.inputAll is;
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   108
  in TextIO.closeIn is;
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   109
     OS.FileSys.remove tmp_name;
1480
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
2246
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   113
(*For exporting images.  The short name saves space in Makefiles*)
1480
85ecd3439e01 made Isabelle compatible with SML/NJ 1.09
clasohm
parents:
diff changeset
   114
fun xML filename banner =
2246
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   115
  let open SMLofNJ
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   116
      val runtime = hd (SMLofNJ.getAllArgs())
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   117
      and exec_file = TextIO.openOut filename
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   118
  in 
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   119
     TextIO.output  (*Write a shell script to invoke the actual image*)
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   120
       (exec_file,
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   121
	String.concat
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   122
	["#!/bin/sh\n", runtime, 
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   123
	 " @SMLdebug=/dev/null",  (*suppresses GC messages*)
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   124
	 " @SMLload=", filename, ".heap\n"]);
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   125
     TextIO.closeOut exec_file;
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   126
     OS.Process.system ("chmod a+x " ^ filename);
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   127
     exportML (filename^".heap");
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   128
     print(banner^"\n")
fce7e34db8c8 Compatibility with SML/NJ 109, and some compatibility with later versions
paulson
parents: 2141
diff changeset
   129
  end;
2408
acddf41dbbf7 added needs_filtered_use;
wenzelm
parents: 2304
diff changeset
   130
acddf41dbbf7 added needs_filtered_use;
wenzelm
parents: 2304
diff changeset
   131
acddf41dbbf7 added needs_filtered_use;
wenzelm
parents: 2304
diff changeset
   132
val needs_filtered_use = false;