author | blanchet |
Mon, 26 Oct 2009 09:14:29 +0100 | |
changeset 33201 | e3d741e9d2fe |
parent 32966 | 5b21661fe618 |
child 37784 | 1d639d28832c |
permissions | -rw-r--r-- |
14815 | 1 |
(* Title: Pure/General/output.ML |
2 |
Author: Makarius, Hagia Maria Sion Abbey (Jerusalem) |
|
3 |
||
22585 | 4 |
Output channels and timing messages. |
14815 | 5 |
*) |
6 |
||
7 |
signature BASIC_OUTPUT = |
|
8 |
sig |
|
23660
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
9 |
type output = string |
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
10 |
val writeln: string -> unit |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
11 |
val priority: string -> unit |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
12 |
val tracing: string -> unit |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
13 |
val warning: string -> unit |
32738 | 14 |
val tolerate_legacy_features: bool Unsynchronized.ref |
22826 | 15 |
val legacy_feature: string -> unit |
25686
bfa774974b6c
cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents:
25682
diff
changeset
|
16 |
val cond_timeit: bool -> string -> (unit -> 'a) -> 'a |
14815 | 17 |
val timeit: (unit -> 'a) -> 'a |
18 |
val timeap: ('a -> 'b) -> 'a -> 'b |
|
19 |
val timeap_msg: string -> ('a -> 'b) -> 'a -> 'b |
|
32738 | 20 |
val timing: bool Unsynchronized.ref |
14815 | 21 |
end; |
22 |
||
23 |
signature OUTPUT = |
|
24 |
sig |
|
25 |
include BASIC_OUTPUT |
|
23660
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
26 |
val default_output: string -> output * int |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
27 |
val default_escape: output -> string |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
28 |
val add_mode: string -> (string -> output * int) -> (output -> string) -> unit |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
29 |
val output_width: string -> output * int |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
30 |
val output: string -> output |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
31 |
val escape: output -> string |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
32 |
val std_output: output -> unit |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
33 |
val std_error: output -> unit |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
34 |
val writeln_default: output -> unit |
32738 | 35 |
val writeln_fn: (output -> unit) Unsynchronized.ref |
36 |
val priority_fn: (output -> unit) Unsynchronized.ref |
|
37 |
val tracing_fn: (output -> unit) Unsynchronized.ref |
|
38 |
val warning_fn: (output -> unit) Unsynchronized.ref |
|
39 |
val error_fn: (output -> unit) Unsynchronized.ref |
|
40 |
val debug_fn: (output -> unit) Unsynchronized.ref |
|
41 |
val prompt_fn: (output -> unit) Unsynchronized.ref |
|
42 |
val status_fn: (output -> unit) Unsynchronized.ref |
|
25845
c448a5f15f31
added explicit prompt channel (prompt_fn/prompt);
wenzelm
parents:
25686
diff
changeset
|
43 |
val error_msg: string -> unit |
c448a5f15f31
added explicit prompt channel (prompt_fn/prompt);
wenzelm
parents:
25686
diff
changeset
|
44 |
val prompt: string -> unit |
27605 | 45 |
val status: string -> unit |
32738 | 46 |
val debugging: bool Unsynchronized.ref |
32966
5b21661fe618
indicate CRITICAL nature of various setmp combinators;
wenzelm
parents:
32738
diff
changeset
|
47 |
val no_warnings_CRITICAL: ('a -> 'b) -> 'a -> 'b |
22130 | 48 |
val debug: (unit -> string) -> unit |
14815 | 49 |
end; |
50 |
||
51 |
structure Output: OUTPUT = |
|
52 |
struct |
|
53 |
||
23616 | 54 |
(** print modes **) |
14881 | 55 |
|
23660
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
56 |
type output = string; (*raw system output*) |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
57 |
|
23616 | 58 |
fun default_output s = (s, size s); |
23660
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
59 |
fun default_escape (s: output) = s; |
14815 | 60 |
|
23616 | 61 |
local |
62 |
val default = {output = default_output, escape = default_escape}; |
|
32738 | 63 |
val modes = Unsynchronized.ref (Symtab.make [("", default)]); |
23616 | 64 |
in |
23922
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
23862
diff
changeset
|
65 |
fun add_mode name output escape = CRITICAL (fn () => |
32738 | 66 |
Unsynchronized.change modes (Symtab.update_new (name, {output = output, escape = escape}))); |
23616 | 67 |
fun get_mode () = |
24612 | 68 |
the_default default (Library.get_first (Symtab.lookup (! modes)) (print_mode_value ())); |
23616 | 69 |
end; |
14815 | 70 |
|
19265 | 71 |
fun output_width x = #output (get_mode ()) x; |
14815 | 72 |
val output = #1 o output_width; |
23727 | 73 |
|
23616 | 74 |
fun escape x = #escape (get_mode ()) x; |
14815 | 75 |
|
76 |
||
77 |
||
78 |
(** output channels **) |
|
79 |
||
14984 | 80 |
(* output primitives -- normally NOT used directly!*) |
81 |
||
24058 | 82 |
fun std_output s = NAMED_CRITICAL "IO" (fn () => |
23922
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
23862
diff
changeset
|
83 |
(TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut)); |
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
23862
diff
changeset
|
84 |
|
24058 | 85 |
fun std_error s = NAMED_CRITICAL "IO" (fn () => |
23922
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
23862
diff
changeset
|
86 |
(TextIO.output (TextIO.stdErr, s); TextIO.flushOut TextIO.stdErr)); |
14815 | 87 |
|
27605 | 88 |
fun writeln_default "" = () |
89 |
| writeln_default s = std_output (suffix "\n" s); |
|
14815 | 90 |
|
14984 | 91 |
|
92 |
(* Isabelle output channels *) |
|
93 |
||
32738 | 94 |
val writeln_fn = Unsynchronized.ref writeln_default; |
95 |
val priority_fn = Unsynchronized.ref (fn s => ! writeln_fn s); |
|
96 |
val tracing_fn = Unsynchronized.ref (fn s => ! writeln_fn s); |
|
97 |
val warning_fn = Unsynchronized.ref (std_output o suffix "\n" o prefix_lines "### "); |
|
98 |
val error_fn = Unsynchronized.ref (std_output o suffix "\n" o prefix_lines "*** "); |
|
99 |
val debug_fn = Unsynchronized.ref (std_output o suffix "\n" o prefix_lines "::: "); |
|
100 |
val prompt_fn = Unsynchronized.ref std_output; |
|
101 |
val status_fn = Unsynchronized.ref (fn _: string => ()); |
|
14815 | 102 |
|
103 |
fun writeln s = ! writeln_fn (output s); |
|
104 |
fun priority s = ! priority_fn (output s); |
|
105 |
fun tracing s = ! tracing_fn (output s); |
|
106 |
fun warning s = ! warning_fn (output s); |
|
25845
c448a5f15f31
added explicit prompt channel (prompt_fn/prompt);
wenzelm
parents:
25686
diff
changeset
|
107 |
fun error_msg s = ! error_fn (output s); |
c448a5f15f31
added explicit prompt channel (prompt_fn/prompt);
wenzelm
parents:
25686
diff
changeset
|
108 |
fun prompt s = ! prompt_fn (output s); |
27605 | 109 |
fun status s = ! status_fn (output s); |
15190 | 110 |
|
32738 | 111 |
val tolerate_legacy_features = Unsynchronized.ref true; |
22826 | 112 |
fun legacy_feature s = |
26291 | 113 |
(if ! tolerate_legacy_features then warning else error) ("Legacy feature! " ^ s); |
22826 | 114 |
|
32966
5b21661fe618
indicate CRITICAL nature of various setmp combinators;
wenzelm
parents:
32738
diff
changeset
|
115 |
fun no_warnings_CRITICAL f = setmp_CRITICAL warning_fn (K ()) f; |
16191 | 116 |
|
32738 | 117 |
val debugging = Unsynchronized.ref false; |
22130 | 118 |
fun debug s = if ! debugging then ! debug_fn (output (s ())) else () |
15190 | 119 |
|
14815 | 120 |
|
121 |
||
122 |
(** timing **) |
|
123 |
||
25686
bfa774974b6c
cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents:
25682
diff
changeset
|
124 |
(*conditional timing with message*) |
bfa774974b6c
cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents:
25682
diff
changeset
|
125 |
fun cond_timeit flag msg e = |
14815 | 126 |
if flag then |
23862 | 127 |
let |
128 |
val start = start_timing (); |
|
25686
bfa774974b6c
cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents:
25682
diff
changeset
|
129 |
val result = Exn.capture e (); |
30187
b92b3375e919
end_timing: generalized result -- message plus with explicit time values;
wenzelm
parents:
29606
diff
changeset
|
130 |
val end_msg = #message (end_timing start); |
25686
bfa774974b6c
cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents:
25682
diff
changeset
|
131 |
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
|
132 |
in Exn.release result end |
bfa774974b6c
cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents:
25682
diff
changeset
|
133 |
else e (); |
25667 | 134 |
|
23862 | 135 |
(*unconditional timing*) |
25686
bfa774974b6c
cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents:
25682
diff
changeset
|
136 |
fun timeit e = cond_timeit true "" e; |
14815 | 137 |
|
138 |
(*timed application function*) |
|
139 |
fun timeap f x = timeit (fn () => f x); |
|
25686
bfa774974b6c
cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents:
25682
diff
changeset
|
140 |
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
|
141 |
|
bfa774974b6c
cond_timeit: added message argument, use Exn.capture/release;
wenzelm
parents:
25682
diff
changeset
|
142 |
(*global timing mode*) |
32738 | 143 |
val timing = Unsynchronized.ref false; |
14815 | 144 |
|
145 |
end; |
|
146 |
||
32738 | 147 |
structure Basic_Output: BASIC_OUTPUT = Output; |
148 |
open Basic_Output; |