author | haftmann |
Mon, 19 Mar 2007 11:59:36 +0100 | |
changeset 22469 | 365bce31e051 |
parent 22220 | 6dc8d0dca678 |
child 22585 | 16af5cb012e7 |
permissions | -rw-r--r-- |
14815 | 1 |
(* Title: Pure/General/output.ML |
2 |
ID: $Id$ |
|
3 |
Author: Makarius, Hagia Maria Sion Abbey (Jerusalem) |
|
4 |
||
14911 | 5 |
Output channels and diagnostic messages. |
14815 | 6 |
*) |
7 |
||
8 |
signature BASIC_OUTPUT = |
|
9 |
sig |
|
10 |
val print_mode: string list ref |
|
11 |
val std_output: string -> unit |
|
12 |
val std_error: string -> unit |
|
14984 | 13 |
val immediate_output: string -> unit |
16191 | 14 |
val writeln_default: string -> unit |
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
15 |
val writeln_fn: (string -> unit) ref (*default output (in messages window)*) |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
16 |
val priority_fn: (string -> unit) ref (*high-priority (maybe modal/pop-up; must be displayed)*) |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
17 |
val tracing_fn: (string -> unit) ref (*tracing message (possibly in tracing window)*) |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
18 |
val warning_fn: (string -> unit) ref (*display warning of non-fatal situation*) |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
19 |
val error_fn: (string -> unit) ref (*display fatal error (possibly modal msg)*) |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
20 |
val panic_fn: (string -> unit) ref (*unrecoverable fatal error; exits system!*) |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
21 |
val info_fn: (string -> unit) ref (*incidental information message (e.g. timing)*) |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
22 |
val debug_fn: (string -> unit) ref (*internal debug messages*) |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
23 |
val writeln: string -> unit |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
24 |
val priority: string -> unit |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
25 |
val tracing: string -> unit |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
26 |
val warning: string -> unit |
22130 | 27 |
val debugging: bool ref |
16191 | 28 |
val no_warnings: ('a -> 'b) -> 'a -> 'b |
14869 | 29 |
val timing: bool ref |
14815 | 30 |
val cond_timeit: bool -> (unit -> 'a) -> 'a |
31 |
val timeit: (unit -> 'a) -> 'a |
|
32 |
val timeap: ('a -> 'b) -> 'a -> 'b |
|
33 |
val timeap_msg: string -> ('a -> 'b) -> 'a -> 'b |
|
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
34 |
val time_accumulator: string -> ('a -> 'b) -> 'a -> 'b |
14815 | 35 |
end; |
36 |
||
37 |
signature OUTPUT = |
|
38 |
sig |
|
39 |
include BASIC_OUTPUT |
|
14881 | 40 |
val has_mode: string -> bool |
14815 | 41 |
val output_width: string -> string * real |
42 |
val output: string -> string |
|
19265 | 43 |
val keyword_width: bool -> string -> string * real |
44 |
val keyword: bool -> string -> string |
|
14815 | 45 |
val indent: string * int -> string |
46 |
val raw: string -> string |
|
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 |
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
51 |
val panic: string -> unit |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
52 |
val info: string -> unit |
22130 | 53 |
val debug: (unit -> string) -> unit |
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
54 |
val error_msg: string -> unit |
20926 | 55 |
val ml_output: (string -> unit) * (string -> unit) |
14815 | 56 |
val add_mode: string -> |
19265 | 57 |
(string -> string * real) * (bool -> string -> string * real) * |
58 |
(string * int -> string) * (string -> string) -> unit |
|
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
59 |
val accumulated_time: unit -> unit |
14815 | 60 |
end; |
61 |
||
62 |
structure Output: OUTPUT = |
|
63 |
struct |
|
64 |
||
65 |
(** print modes **) |
|
66 |
||
67 |
val print_mode = ref ([]: string list); |
|
68 |
||
20664 | 69 |
fun has_mode s = member (op =) (! print_mode) s; |
14881 | 70 |
|
14955 | 71 |
type mode_fns = |
19265 | 72 |
{output: string -> string * real, |
73 |
keyword: bool -> string -> string * real, |
|
14955 | 74 |
indent: string * int -> string, |
75 |
raw: string -> string}; |
|
76 |
||
77 |
val modes = ref (Symtab.empty: mode_fns Symtab.table); |
|
14815 | 78 |
|
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
79 |
fun lookup_mode name = Symtab.lookup (! modes) name; |
14815 | 80 |
|
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
81 |
exception MISSING_DEFAULT_OUTPUT; |
14815 | 82 |
|
83 |
fun get_mode () = |
|
15531 | 84 |
(case Library.get_first lookup_mode (! print_mode) of SOME p => p |
85 |
| NONE => |
|
86 |
(case lookup_mode "" of SOME p => p |
|
87 |
| NONE => raise MISSING_DEFAULT_OUTPUT)); (*sys_error would require output again!*) |
|
14815 | 88 |
|
19265 | 89 |
fun output_width x = #output (get_mode ()) x; |
14815 | 90 |
val output = #1 o output_width; |
19265 | 91 |
fun keyword_width b x = #keyword (get_mode ()) b x; |
92 |
val keyword = #1 oo keyword_width; |
|
14955 | 93 |
fun indent x = #indent (get_mode ()) x; |
94 |
fun raw x = #raw (get_mode ()) x; |
|
14815 | 95 |
|
96 |
||
97 |
||
98 |
(** output channels **) |
|
99 |
||
14984 | 100 |
(* output primitives -- normally NOT used directly!*) |
101 |
||
14815 | 102 |
fun std_output s = (TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut); |
103 |
fun std_error s = (TextIO.output (TextIO.stdErr, s); TextIO.flushOut TextIO.stdErr); |
|
104 |
||
14984 | 105 |
val immediate_output = std_output o output; |
14815 | 106 |
val writeln_default = std_output o suffix "\n"; |
107 |
||
14984 | 108 |
|
109 |
(* Isabelle output channels *) |
|
110 |
||
14815 | 111 |
val writeln_fn = ref writeln_default; |
112 |
val priority_fn = ref (fn s => ! writeln_fn s); |
|
113 |
val tracing_fn = ref (fn s => ! writeln_fn s); |
|
114 |
val warning_fn = ref (std_output o suffix "\n" o prefix_lines "### "); |
|
115 |
val error_fn = ref (std_output o suffix "\n" o prefix_lines "*** "); |
|
14862
a43f9e2c6332
Add panic function which exits Isabelle immediately.
aspinall
parents:
14815
diff
changeset
|
116 |
val panic_fn = ref (std_output o suffix "\n" o prefix_lines "!!! "); |
15190 | 117 |
val info_fn = ref (std_output o suffix "\n" o prefix_lines "+++ "); |
118 |
val debug_fn = ref (std_output o suffix "\n" o prefix_lines "::: "); |
|
14815 | 119 |
|
120 |
fun writeln s = ! writeln_fn (output s); |
|
121 |
fun priority s = ! priority_fn (output s); |
|
122 |
fun tracing s = ! tracing_fn (output s); |
|
123 |
fun warning s = ! warning_fn (output s); |
|
15190 | 124 |
fun info s = ! info_fn (output s); |
125 |
||
16191 | 126 |
fun no_warnings f = setmp warning_fn (K ()) f; |
127 |
||
22130 | 128 |
val debugging = ref false; |
129 |
fun debug s = if ! debugging then ! debug_fn (output (s ())) else () |
|
15190 | 130 |
|
14815 | 131 |
fun error_msg s = ! error_fn (output s); |
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
132 |
|
14862
a43f9e2c6332
Add panic function which exits Isabelle immediately.
aspinall
parents:
14815
diff
changeset
|
133 |
fun panic_msg s = ! panic_fn (output s); |
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
134 |
fun panic s = (panic_msg ("## SYSTEM EXIT ##\n" ^ s); exit 1); |
14815 | 135 |
|
20926 | 136 |
val ml_output = (writeln, error_msg); |
137 |
||
14815 | 138 |
|
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
139 |
(* toplevel errors *) |
14815 | 140 |
|
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
141 |
exception TOPLEVEL_ERROR; |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
142 |
|
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
143 |
val do_toplevel_errors = ref true; |
14815 | 144 |
|
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
145 |
fun toplevel_errors f x = |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
146 |
if ! do_toplevel_errors then |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
147 |
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
|
148 |
else f x; |
14815 | 149 |
|
18716 | 150 |
fun ML_errors f = setmp do_toplevel_errors true (toplevel_errors f); |
151 |
||
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
152 |
|
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
153 |
(* add_mode *) |
14815 | 154 |
|
19265 | 155 |
fun add_mode name (output, keyword, indent, raw) = |
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
156 |
(if is_none (lookup_mode name) then () |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
157 |
else warning ("Redeclaration of symbol print mode: " ^ quote name); |
19265 | 158 |
change modes (Symtab.update (name, |
159 |
{output = output, keyword = keyword, indent = indent, raw = raw}))); |
|
14815 | 160 |
|
161 |
||
162 |
||
163 |
(** timing **) |
|
164 |
||
14869 | 165 |
(*global timing mode*) |
166 |
val timing = ref false; |
|
167 |
||
14815 | 168 |
(*a conditional timing function: applies f to () and, if the flag is true, |
169 |
prints its runtime on warning channel*) |
|
170 |
fun cond_timeit flag f = |
|
171 |
if flag then |
|
21295
63552bc99cfb
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
20926
diff
changeset
|
172 |
let val start = start_timing () |
14815 | 173 |
val result = f () |
21295
63552bc99cfb
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
20926
diff
changeset
|
174 |
in info (end_timing start); result end |
14815 | 175 |
else f (); |
176 |
||
177 |
(*unconditional timing function*) |
|
178 |
fun timeit x = cond_timeit true x; |
|
179 |
||
180 |
(*timed application function*) |
|
181 |
fun timeap f x = timeit (fn () => f x); |
|
15190 | 182 |
fun timeap_msg s f x = (info s; timeap f x); |
14815 | 183 |
|
14978 | 184 |
|
185 |
(* accumulated timing *) |
|
186 |
||
187 |
local |
|
188 |
||
189 |
datatype time_info = TI of |
|
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
190 |
{name: string, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
191 |
timer: Timer.cpu_timer, |
14978 | 192 |
sys: Time.time, |
193 |
usr: Time.time, |
|
194 |
gc: Time.time, |
|
195 |
count: int}; |
|
196 |
||
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
197 |
fun time_init name = ref (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.startCPUTimer (), |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
200 |
sys = Time.zeroTime, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
201 |
usr = Time.zeroTime, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
202 |
gc = Time.zeroTime, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
203 |
count = 0}); |
14978 | 204 |
|
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
205 |
fun time_reset (r as ref (TI {name, ...})) = r := ! (time_init name); |
14978 | 206 |
|
207 |
fun time_check (ref (TI r)) = r; |
|
208 |
||
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
209 |
fun time_add ti f x = |
14978 | 210 |
let |
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
211 |
fun add_diff time time1 time2 = |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
212 |
Time.+ (time, Time.- (time2, time1) handle Time.Time => Time.zeroTime); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
213 |
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
|
214 |
val (sys1, usr1, gc1) = check_timer timer; |
14978 | 215 |
val result = capture f x; |
21295
63552bc99cfb
tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents:
20926
diff
changeset
|
216 |
val (sys2, usr2, gc2) = check_timer timer; |
14978 | 217 |
in |
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
218 |
ti := TI |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
219 |
{name = name, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
220 |
timer = timer, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
221 |
sys = add_diff sys sys1 sys2, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
222 |
usr = add_diff usr usr1 usr2, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
223 |
gc = add_diff gc gc1 gc2, |
14978 | 224 |
count = count + 1}; |
225 |
release result |
|
226 |
end; |
|
227 |
||
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
228 |
fun time_finish ti = |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
229 |
let |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
230 |
fun secs prfx time = prfx ^ Time.toString time; |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
231 |
val {name, timer, sys, usr, gc, count} = time_check ti; |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
232 |
in |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
233 |
info ("Total of " ^ quote name ^ ": " ^ |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
234 |
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
|
235 |
" secs in " ^ string_of_int count ^ " calls"); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
236 |
time_reset ti |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
237 |
end; |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
238 |
|
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
239 |
val time_finish_hooks = ref ([]: (unit -> unit) list); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
240 |
|
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
241 |
in |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
242 |
|
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
243 |
fun time_accumulator name = |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
244 |
let val ti = time_init name in |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
245 |
change time_finish_hooks (cons (fn () => time_finish ti)); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
246 |
time_add ti |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
247 |
end; |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
248 |
|
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
249 |
fun accumulated_time () = List.app (fn f => f ()) (! time_finish_hooks); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
250 |
|
14978 | 251 |
end; |
252 |
||
14815 | 253 |
end; |
254 |
||
255 |
structure BasicOutput: BASIC_OUTPUT = Output; |
|
256 |
open BasicOutput; |