author | wenzelm |
Tue, 24 Jul 2007 19:44:35 +0200 | |
changeset 23963 | c2ee97a963db |
parent 23939 | e543359fe8b6 |
child 24054 | 0eacec17e8e7 |
permissions | -rw-r--r-- |
14815 | 1 |
(* Title: Pure/General/output.ML |
2 |
ID: $Id$ |
|
3 |
Author: Makarius, Hagia Maria Sion Abbey (Jerusalem) |
|
4 |
||
22585 | 5 |
Output channels and timing messages. |
14815 | 6 |
*) |
7 |
||
8 |
signature BASIC_OUTPUT = |
|
9 |
sig |
|
23660
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
10 |
type output = string |
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
11 |
val writeln: string -> unit |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
12 |
val priority: string -> unit |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
13 |
val tracing: string -> unit |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
14 |
val warning: string -> unit |
22826 | 15 |
val tolerate_legacy_features: bool ref |
16 |
val legacy_feature: string -> unit |
|
14869 | 17 |
val timing: bool ref |
14815 | 18 |
val cond_timeit: bool -> (unit -> 'a) -> 'a |
19 |
val timeit: (unit -> 'a) -> 'a |
|
20 |
val timeap: ('a -> 'b) -> 'a -> 'b |
|
21 |
val timeap_msg: string -> ('a -> 'b) -> 'a -> 'b |
|
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
22 |
val time_accumulator: string -> ('a -> 'b) -> 'a -> 'b |
14815 | 23 |
end; |
24 |
||
25 |
signature OUTPUT = |
|
26 |
sig |
|
27 |
include BASIC_OUTPUT |
|
23660
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
28 |
val default_output: string -> output * int |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
29 |
val default_escape: output -> string |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
30 |
val add_mode: string -> (string -> output * int) -> (output -> string) -> unit |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
31 |
val output_width: string -> output * int |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
32 |
val output: string -> output |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
33 |
val escape: output -> string |
23727 | 34 |
val escape_malformed: string -> string |
23660
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
35 |
val std_output: output -> unit |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
36 |
val std_error: output -> unit |
22585 | 37 |
val immediate_output: string -> unit |
23660
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
38 |
val writeln_default: output -> unit |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
39 |
val writeln_fn: (output -> unit) ref |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
40 |
val priority_fn: (output -> unit) ref |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
41 |
val tracing_fn: (output -> unit) ref |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
42 |
val warning_fn: (output -> unit) ref |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
43 |
val error_fn: (output -> unit) ref |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
44 |
val debug_fn: (output -> unit) ref |
22585 | 45 |
val debugging: bool ref |
46 |
val no_warnings: ('a -> 'b) -> 'a -> 'b |
|
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
47 |
exception TOPLEVEL_ERROR |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
48 |
val do_toplevel_errors: bool ref |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
49 |
val toplevel_errors: ('a -> 'b) -> 'a -> 'b |
18716 | 50 |
val ML_errors: ('a -> 'b) -> 'a -> 'b |
22130 | 51 |
val debug: (unit -> string) -> unit |
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
52 |
val error_msg: string -> unit |
20926 | 53 |
val ml_output: (string -> unit) * (string -> unit) |
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
54 |
val accumulated_time: unit -> unit |
14815 | 55 |
end; |
56 |
||
57 |
structure Output: OUTPUT = |
|
58 |
struct |
|
59 |
||
23616 | 60 |
(** print modes **) |
14881 | 61 |
|
23660
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
62 |
type output = string; (*raw system output*) |
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
63 |
|
23616 | 64 |
fun default_output s = (s, size s); |
23660
18765718cf62
type output = string indicates raw system output;
wenzelm
parents:
23616
diff
changeset
|
65 |
fun default_escape (s: output) = s; |
14815 | 66 |
|
23616 | 67 |
local |
68 |
val default = {output = default_output, escape = default_escape}; |
|
69 |
val modes = ref (Symtab.make [("", default)]); |
|
70 |
in |
|
23922
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
23862
diff
changeset
|
71 |
fun add_mode name output escape = CRITICAL (fn () => |
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
23862
diff
changeset
|
72 |
change modes (Symtab.update_new (name, {output = output, escape = escape}))); |
23616 | 73 |
fun get_mode () = |
74 |
the_default default (Library.get_first (Symtab.lookup (! modes)) (! print_mode)); |
|
75 |
end; |
|
14815 | 76 |
|
19265 | 77 |
fun output_width x = #output (get_mode ()) x; |
14815 | 78 |
val output = #1 o output_width; |
23727 | 79 |
|
23616 | 80 |
fun escape x = #escape (get_mode ()) x; |
23727 | 81 |
val escape_malformed = escape o translate_string output; |
14815 | 82 |
|
83 |
||
84 |
||
85 |
(** output channels **) |
|
86 |
||
14984 | 87 |
(* output primitives -- normally NOT used directly!*) |
88 |
||
23922
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
23862
diff
changeset
|
89 |
fun std_output s = CRITICAL (fn () => |
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
23862
diff
changeset
|
90 |
(TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut)); |
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
23862
diff
changeset
|
91 |
|
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
23862
diff
changeset
|
92 |
fun std_error s = CRITICAL (fn () => |
707639e9497d
marked some CRITICAL sections (for multithreading);
wenzelm
parents:
23862
diff
changeset
|
93 |
(TextIO.output (TextIO.stdErr, s); TextIO.flushOut TextIO.stdErr)); |
14815 | 94 |
|
14984 | 95 |
val immediate_output = std_output o output; |
14815 | 96 |
val writeln_default = std_output o suffix "\n"; |
97 |
||
14984 | 98 |
|
99 |
(* Isabelle output channels *) |
|
100 |
||
14815 | 101 |
val writeln_fn = ref writeln_default; |
102 |
val priority_fn = ref (fn s => ! writeln_fn s); |
|
103 |
val tracing_fn = ref (fn s => ! writeln_fn s); |
|
104 |
val warning_fn = ref (std_output o suffix "\n" o prefix_lines "### "); |
|
105 |
val error_fn = ref (std_output o suffix "\n" o prefix_lines "*** "); |
|
15190 | 106 |
val debug_fn = ref (std_output o suffix "\n" o prefix_lines "::: "); |
14815 | 107 |
|
108 |
fun writeln s = ! writeln_fn (output s); |
|
109 |
fun priority s = ! priority_fn (output s); |
|
110 |
fun tracing s = ! tracing_fn (output s); |
|
111 |
fun warning s = ! warning_fn (output s); |
|
15190 | 112 |
|
22826 | 113 |
val tolerate_legacy_features = ref true; |
114 |
fun legacy_feature s = |
|
115 |
(if ! tolerate_legacy_features then warning else error) ("Legacy feature: " ^ s); |
|
116 |
||
16191 | 117 |
fun no_warnings f = setmp warning_fn (K ()) f; |
118 |
||
22130 | 119 |
val debugging = ref false; |
120 |
fun debug s = if ! debugging then ! debug_fn (output (s ())) else () |
|
15190 | 121 |
|
14815 | 122 |
fun error_msg s = ! error_fn (output s); |
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
123 |
|
20926 | 124 |
val ml_output = (writeln, error_msg); |
125 |
||
14815 | 126 |
|
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
127 |
(* toplevel errors *) |
14815 | 128 |
|
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
129 |
exception TOPLEVEL_ERROR; |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
130 |
|
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
131 |
val do_toplevel_errors = ref true; |
14815 | 132 |
|
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
133 |
fun toplevel_errors f x = |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
134 |
if ! do_toplevel_errors then |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
135 |
f x handle ERROR msg => (error_msg msg; raise TOPLEVEL_ERROR) |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
136 |
else f x; |
14815 | 137 |
|
18716 | 138 |
fun ML_errors f = setmp do_toplevel_errors true (toplevel_errors f); |
139 |
||
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
140 |
|
14815 | 141 |
|
142 |
(** timing **) |
|
143 |
||
14869 | 144 |
(*global timing mode*) |
145 |
val timing = ref false; |
|
146 |
||
23862 | 147 |
(*conditional timing*) |
14815 | 148 |
fun cond_timeit flag f = |
149 |
if flag then |
|
23862 | 150 |
let |
151 |
val start = start_timing (); |
|
152 |
val result = f (); |
|
22585 | 153 |
in warning (end_timing start); result end |
14815 | 154 |
else f (); |
155 |
||
23862 | 156 |
(*unconditional timing*) |
14815 | 157 |
fun timeit x = cond_timeit true x; |
158 |
||
159 |
(*timed application function*) |
|
160 |
fun timeap f x = timeit (fn () => f x); |
|
22585 | 161 |
fun timeap_msg s f x = (warning s; timeap f x); |
14815 | 162 |
|
14978 | 163 |
|
164 |
(* accumulated timing *) |
|
165 |
||
166 |
local |
|
167 |
||
168 |
datatype time_info = TI of |
|
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
169 |
{name: string, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
170 |
timer: Timer.cpu_timer, |
14978 | 171 |
sys: Time.time, |
172 |
usr: Time.time, |
|
173 |
gc: Time.time, |
|
174 |
count: int}; |
|
175 |
||
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
176 |
fun time_init name = ref (TI |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
177 |
{name = name, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
178 |
timer = Timer.startCPUTimer (), |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
179 |
sys = Time.zeroTime, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
180 |
usr = Time.zeroTime, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
181 |
gc = Time.zeroTime, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
182 |
count = 0}); |
14978 | 183 |
|
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
184 |
fun time_reset (r as ref (TI {name, ...})) = r := ! (time_init name); |
14978 | 185 |
|
186 |
fun time_check (ref (TI r)) = r; |
|
187 |
||
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
188 |
fun time_add ti f x = |
14978 | 189 |
let |
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
190 |
fun add_diff time time1 time2 = |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
191 |
Time.+ (time, Time.- (time2, time1) handle Time.Time => Time.zeroTime); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
192 |
val {name, timer, sys, usr, gc, count} = time_check ti; |
21295
63552bc99cfb
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
20926
diff
changeset
|
193 |
val (sys1, usr1, gc1) = check_timer timer; |
23963
c2ee97a963db
moved exception capture/release to structure Exn;
wenzelm
parents:
23939
diff
changeset
|
194 |
val result = Exn.capture f x; |
21295
63552bc99cfb
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
20926
diff
changeset
|
195 |
val (sys2, usr2, gc2) = check_timer timer; |
14978 | 196 |
in |
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
197 |
ti := TI |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
198 |
{name = name, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
199 |
timer = timer, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
200 |
sys = add_diff sys sys1 sys2, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
201 |
usr = add_diff usr usr1 usr2, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
202 |
gc = add_diff gc gc1 gc2, |
14978 | 203 |
count = count + 1}; |
23963
c2ee97a963db
moved exception capture/release to structure Exn;
wenzelm
parents:
23939
diff
changeset
|
204 |
Exn.release result |
14978 | 205 |
end; |
206 |
||
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
207 |
fun time_finish ti = |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
208 |
let |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
209 |
fun secs prfx time = prfx ^ Time.toString time; |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
210 |
val {name, timer, sys, usr, gc, count} = time_check ti; |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
211 |
in |
22585 | 212 |
warning ("Total of " ^ quote name ^ ": " ^ |
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
213 |
secs "User " usr ^ secs " GC " gc ^ secs " All " (Time.+ (sys, Time.+ (usr, gc))) ^ |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
214 |
" secs in " ^ string_of_int count ^ " calls"); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
215 |
time_reset ti |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
216 |
end; |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
217 |
|
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
218 |
val time_finish_hooks = ref ([]: (unit -> unit) list); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
219 |
|
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
220 |
in |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
221 |
|
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
222 |
fun time_accumulator name = |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
223 |
let val ti = time_init name in |
23939 | 224 |
CRITICAL (fn () => change time_finish_hooks (cons (fn () => time_finish ti))); |
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
225 |
time_add ti |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
226 |
end; |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
227 |
|
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
228 |
fun accumulated_time () = List.app (fn f => f ()) (! time_finish_hooks); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
229 |
|
14978 | 230 |
end; |
231 |
||
14815 | 232 |
end; |
233 |
||
234 |
structure BasicOutput: BASIC_OUTPUT = Output; |
|
235 |
open BasicOutput; |