src/Pure/General/output.ML
author wenzelm
Mon, 25 Oct 2010 20:24:13 +0200
changeset 40131 7cbebd636e79
parent 40125 da45a2f45870
child 40132 7ee65dbffa31
permissions -rw-r--r--
explicitly qualify type Output.output, which is a slightly odd internal feature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/output.ML
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius, Hagia Maria Sion Abbey (Jerusalem)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     3
22585
16af5cb012e7 cleaned-up Output functions;
wenzelm
parents: 22469
diff changeset
     4
Output channels and timing messages.
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     5
*)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     6
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     7
signature BASIC_OUTPUT =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     8
sig
18682
216692feebab removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents: 17539
diff changeset
     9
  val writeln: string -> unit
216692feebab removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents: 17539
diff changeset
    10
  val priority: string -> unit
216692feebab removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents: 17539
diff changeset
    11
  val tracing: string -> unit
216692feebab removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents: 17539
diff changeset
    12
  val warning: string -> unit
22826
0f4c501a691e explicit treatment of legacy_features;
wenzelm
parents: 22699
diff changeset
    13
  val legacy_feature: string -> unit
25686
bfa774974b6c cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents: 25682
diff changeset
    14
  val cond_timeit: bool -> string -> (unit -> 'a) -> 'a
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    15
  val timeit: (unit -> 'a) -> 'a
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    16
  val timeap: ('a -> 'b) -> 'a -> 'b
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    17
  val timeap_msg: string -> ('a -> 'b) -> 'a -> 'b
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31685
diff changeset
    18
  val timing: bool Unsynchronized.ref
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    19
end;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    20
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    21
signature OUTPUT =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    22
sig
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    23
  include BASIC_OUTPUT
40131
7cbebd636e79 explicitly qualify type Output.output, which is a slightly odd internal feature;
wenzelm
parents: 40125
diff changeset
    24
  type output = string
23660
18765718cf62 type output = string indicates raw system output;
wenzelm
parents: 23616
diff changeset
    25
  val default_output: string -> output * int
18765718cf62 type output = string indicates raw system output;
wenzelm
parents: 23616
diff changeset
    26
  val default_escape: output -> string
18765718cf62 type output = string indicates raw system output;
wenzelm
parents: 23616
diff changeset
    27
  val add_mode: string -> (string -> output * int) -> (output -> string) -> unit
18765718cf62 type output = string indicates raw system output;
wenzelm
parents: 23616
diff changeset
    28
  val output_width: string -> output * int
18765718cf62 type output = string indicates raw system output;
wenzelm
parents: 23616
diff changeset
    29
  val output: string -> output
18765718cf62 type output = string indicates raw system output;
wenzelm
parents: 23616
diff changeset
    30
  val escape: output -> string
39733
6d373e9dcb9d renamed raw output primitives to emphasize their meaning -- not to be used in user-space;
wenzelm
parents: 38839
diff changeset
    31
  val raw_stdout: output -> unit
6d373e9dcb9d renamed raw output primitives to emphasize their meaning -- not to be used in user-space;
wenzelm
parents: 38839
diff changeset
    32
  val raw_stderr: output -> unit
6d373e9dcb9d renamed raw output primitives to emphasize their meaning -- not to be used in user-space;
wenzelm
parents: 38839
diff changeset
    33
  val raw_writeln: output -> unit
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31685
diff changeset
    34
  val writeln_fn: (output -> unit) Unsynchronized.ref
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31685
diff changeset
    35
  val priority_fn: (output -> unit) Unsynchronized.ref
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31685
diff changeset
    36
  val tracing_fn: (output -> unit) Unsynchronized.ref
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31685
diff changeset
    37
  val warning_fn: (output -> unit) Unsynchronized.ref
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31685
diff changeset
    38
  val error_fn: (output -> unit) Unsynchronized.ref
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31685
diff changeset
    39
  val prompt_fn: (output -> unit) Unsynchronized.ref
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31685
diff changeset
    40
  val status_fn: (output -> unit) Unsynchronized.ref
38236
d8c7be27e01d explicitly distinguish Output.status (essential feedback) vs. Output.report (useful markup);
wenzelm
parents: 37784
diff changeset
    41
  val report_fn: (output -> unit) Unsynchronized.ref
25845
c448a5f15f31 added explicit prompt channel (prompt_fn/prompt);
wenzelm
parents: 25686
diff changeset
    42
  val error_msg: string -> unit
c448a5f15f31 added explicit prompt channel (prompt_fn/prompt);
wenzelm
parents: 25686
diff changeset
    43
  val prompt: string -> unit
27605
2c281958e45d added status channel;
wenzelm
parents: 26291
diff changeset
    44
  val status: string -> unit
38236
d8c7be27e01d explicitly distinguish Output.status (essential feedback) vs. Output.report (useful markup);
wenzelm
parents: 37784
diff changeset
    45
  val report: string -> unit
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    46
end;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    47
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    48
structure Output: OUTPUT =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    49
struct
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    50
23616
ba6deff7d214 renamed raw to escape;
wenzelm
parents: 22826
diff changeset
    51
(** print modes **)
14881
e1f501a14159 added has_mode; handle_error: output raw;
wenzelm
parents: 14869
diff changeset
    52
23660
18765718cf62 type output = string indicates raw system output;
wenzelm
parents: 23616
diff changeset
    53
type output = string;  (*raw system output*)
18765718cf62 type output = string indicates raw system output;
wenzelm
parents: 23616
diff changeset
    54
23616
ba6deff7d214 renamed raw to escape;
wenzelm
parents: 22826
diff changeset
    55
fun default_output s = (s, size s);
23660
18765718cf62 type output = string indicates raw system output;
wenzelm
parents: 23616
diff changeset
    56
fun default_escape (s: output) = s;
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    57
23616
ba6deff7d214 renamed raw to escape;
wenzelm
parents: 22826
diff changeset
    58
local
ba6deff7d214 renamed raw to escape;
wenzelm
parents: 22826
diff changeset
    59
  val default = {output = default_output, escape = default_escape};
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31685
diff changeset
    60
  val modes = Unsynchronized.ref (Symtab.make [("", default)]);
23616
ba6deff7d214 renamed raw to escape;
wenzelm
parents: 22826
diff changeset
    61
in
23922
707639e9497d marked some CRITICAL sections (for multithreading);
wenzelm
parents: 23862
diff changeset
    62
  fun add_mode name output escape = CRITICAL (fn () =>
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31685
diff changeset
    63
    Unsynchronized.change modes (Symtab.update_new (name, {output = output, escape = escape})));
23616
ba6deff7d214 renamed raw to escape;
wenzelm
parents: 22826
diff changeset
    64
  fun get_mode () =
24612
d1b315bdb8d7 avoid direct access to print_mode;
wenzelm
parents: 24058
diff changeset
    65
    the_default default (Library.get_first (Symtab.lookup (! modes)) (print_mode_value ()));
23616
ba6deff7d214 renamed raw to escape;
wenzelm
parents: 22826
diff changeset
    66
end;
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    67
19265
cae36e16f3c0 Output.add_mode: keyword component;
wenzelm
parents: 18716
diff changeset
    68
fun output_width x = #output (get_mode ()) x;
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    69
val output = #1 o output_width;
23727
39f8d1480d55 added escape_malformed (failsafe);
wenzelm
parents: 23660
diff changeset
    70
23616
ba6deff7d214 renamed raw to escape;
wenzelm
parents: 22826
diff changeset
    71
fun escape x = #escape (get_mode ()) x;
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    72
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    73
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    74
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    75
(** output channels **)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    76
39733
6d373e9dcb9d renamed raw output primitives to emphasize their meaning -- not to be used in user-space;
wenzelm
parents: 38839
diff changeset
    77
(* raw output primitives -- not to be used in user-space *)
14984
edbc81e60809 immediate_output;
wenzelm
parents: 14978
diff changeset
    78
39733
6d373e9dcb9d renamed raw output primitives to emphasize their meaning -- not to be used in user-space;
wenzelm
parents: 38839
diff changeset
    79
fun raw_stdout s = (TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut);
6d373e9dcb9d renamed raw output primitives to emphasize their meaning -- not to be used in user-space;
wenzelm
parents: 38839
diff changeset
    80
fun raw_stderr s = (TextIO.output (TextIO.stdErr, s); TextIO.flushOut TextIO.stdErr);
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    81
39733
6d373e9dcb9d renamed raw output primitives to emphasize their meaning -- not to be used in user-space;
wenzelm
parents: 38839
diff changeset
    82
fun raw_writeln "" = ()
6d373e9dcb9d renamed raw output primitives to emphasize their meaning -- not to be used in user-space;
wenzelm
parents: 38839
diff changeset
    83
  | raw_writeln s = raw_stdout (suffix "\n" s);  (*atomic output!*)
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    84
14984
edbc81e60809 immediate_output;
wenzelm
parents: 14978
diff changeset
    85
edbc81e60809 immediate_output;
wenzelm
parents: 14978
diff changeset
    86
(* Isabelle output channels *)
edbc81e60809 immediate_output;
wenzelm
parents: 14978
diff changeset
    87
39733
6d373e9dcb9d renamed raw output primitives to emphasize their meaning -- not to be used in user-space;
wenzelm
parents: 38839
diff changeset
    88
val writeln_fn = Unsynchronized.ref raw_writeln;
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31685
diff changeset
    89
val priority_fn = Unsynchronized.ref (fn s => ! writeln_fn s);
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31685
diff changeset
    90
val tracing_fn = Unsynchronized.ref (fn s => ! writeln_fn s);
39733
6d373e9dcb9d renamed raw output primitives to emphasize their meaning -- not to be used in user-space;
wenzelm
parents: 38839
diff changeset
    91
val warning_fn = Unsynchronized.ref (raw_stdout o suffix "\n" o prefix_lines "### ");
6d373e9dcb9d renamed raw output primitives to emphasize their meaning -- not to be used in user-space;
wenzelm
parents: 38839
diff changeset
    92
val error_fn = Unsynchronized.ref (raw_stdout o suffix "\n" o prefix_lines "*** ");
6d373e9dcb9d renamed raw output primitives to emphasize their meaning -- not to be used in user-space;
wenzelm
parents: 38839
diff changeset
    93
val prompt_fn = Unsynchronized.ref raw_stdout;
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31685
diff changeset
    94
val status_fn = Unsynchronized.ref (fn _: string => ());
38236
d8c7be27e01d explicitly distinguish Output.status (essential feedback) vs. Output.report (useful markup);
wenzelm
parents: 37784
diff changeset
    95
val report_fn = Unsynchronized.ref (fn _: string => ());
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    96
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    97
fun writeln s = ! writeln_fn (output s);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    98
fun priority s = ! priority_fn (output s);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    99
fun tracing s = ! tracing_fn (output s);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   100
fun warning s = ! warning_fn (output s);
25845
c448a5f15f31 added explicit prompt channel (prompt_fn/prompt);
wenzelm
parents: 25686
diff changeset
   101
fun error_msg s = ! error_fn (output s);
c448a5f15f31 added explicit prompt channel (prompt_fn/prompt);
wenzelm
parents: 25686
diff changeset
   102
fun prompt s = ! prompt_fn (output s);
27605
2c281958e45d added status channel;
wenzelm
parents: 26291
diff changeset
   103
fun status s = ! status_fn (output s);
38236
d8c7be27e01d explicitly distinguish Output.status (essential feedback) vs. Output.report (useful markup);
wenzelm
parents: 37784
diff changeset
   104
fun report s = ! report_fn (output s);
15190
b6788dbd2ef9 Add info and debug output channels.
aspinall
parents: 15010
diff changeset
   105
37784
1d639d28832c removed impractical tolerate_legacy_features flag;
wenzelm
parents: 32966
diff changeset
   106
fun legacy_feature s = warning ("Legacy feature! " ^ s);
22826
0f4c501a691e explicit treatment of legacy_features;
wenzelm
parents: 22699
diff changeset
   107
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   108
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   109
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   110
(** timing **)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   111
25686
bfa774974b6c cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents: 25682
diff changeset
   112
(*conditional timing with message*)
bfa774974b6c cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents: 25682
diff changeset
   113
fun cond_timeit flag msg e =
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   114
  if flag then
23862
wenzelm
parents: 23727
diff changeset
   115
    let
wenzelm
parents: 23727
diff changeset
   116
      val start = start_timing ();
25686
bfa774974b6c cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents: 25682
diff changeset
   117
      val result = Exn.capture e ();
30187
b92b3375e919 end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents: 29606
diff changeset
   118
      val end_msg = #message (end_timing start);
25686
bfa774974b6c cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents: 25682
diff changeset
   119
      val _ = warning (if msg = "" then end_msg else msg ^ "\n" ^ end_msg);
bfa774974b6c cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents: 25682
diff changeset
   120
    in Exn.release result end
bfa774974b6c cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents: 25682
diff changeset
   121
  else e ();
25667
a089038c1893 improved semantics of timeapp_msg
haftmann
parents: 25640
diff changeset
   122
23862
wenzelm
parents: 23727
diff changeset
   123
(*unconditional timing*)
25686
bfa774974b6c cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents: 25682
diff changeset
   124
fun timeit e = cond_timeit true "" e;
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   125
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   126
(*timed application function*)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   127
fun timeap f x = timeit (fn () => f x);
25686
bfa774974b6c cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents: 25682
diff changeset
   128
fun timeap_msg msg f x = cond_timeit true msg (fn () => f x);
bfa774974b6c cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents: 25682
diff changeset
   129
bfa774974b6c cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents: 25682
diff changeset
   130
(*global timing mode*)
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31685
diff changeset
   131
val timing = Unsynchronized.ref false;
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   132
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   133
end;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   134
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31685
diff changeset
   135
structure Basic_Output: BASIC_OUTPUT = Output;
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31685
diff changeset
   136
open Basic_Output;