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