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