author | haftmann |
Wed, 21 Sep 2005 10:32:24 +0200 | |
changeset 17540 | f662416aa5f2 |
parent 17539 | b2ce48df4d4c |
child 18682 | 216692feebab |
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 |
15 |
val writeln_fn: (string -> unit) ref |
|
16 |
val priority_fn: (string -> unit) ref |
|
17 |
val tracing_fn: (string -> unit) ref |
|
18 |
val warning_fn: (string -> unit) ref |
|
19 |
val error_fn: (string -> unit) ref |
|
20 |
val panic_fn: (string -> unit) ref |
|
21 |
val info_fn: (string -> unit) ref |
|
22 |
val debug_fn: (string -> unit) ref |
|
23 |
val writeln: string -> unit (*default output (in messages window)*) |
|
24 |
val priority: string -> unit (*high-priority (maybe modal/pop-up; must be displayed)*) |
|
25 |
val tracing: string -> unit (*tracing message (possibly in tracing window)*) |
|
26 |
val warning: string -> unit (*display warning of non-fatal situation*) |
|
27 |
val error_msg: string -> unit (*display fatal error (possibly modal msg)*) |
|
28 |
val error: string -> 'a (*display message as above, raise exn*) |
|
29 |
val sys_error: string -> 'a (*internal fatal error condition; raise exn*) |
|
30 |
val panic: string -> unit (*unrecoverable fatal error; exits system!*) |
|
31 |
val info: string -> unit (*incidental information message (e.g. timing)*) |
|
32 |
val debug: string -> unit (*internal debug messages*) |
|
33 |
val show_debug_msgs: bool ref |
|
34 |
val no_warnings: ('a -> 'b) -> 'a -> 'b |
|
14815 | 35 |
val assert: bool -> string -> unit |
36 |
val deny: bool -> string -> unit |
|
37 |
val assert_all: ('a -> bool) -> 'a list -> ('a -> string) -> unit |
|
17539 | 38 |
val update_warn: ('a * 'a -> bool) -> string -> ('a * 'b) |
39 |
-> ('a * 'b) list -> ('a * 'b) list |
|
14815 | 40 |
val overwrite_warn: (''a * 'b) list * (''a * 'b) -> string -> (''a * 'b) list |
41 |
datatype 'a error = Error of string | OK of 'a |
|
42 |
val get_error: 'a error -> string option |
|
43 |
val get_ok: 'a error -> 'a option |
|
44 |
val handle_error: ('a -> 'b) -> 'a -> 'b error |
|
45 |
exception ERROR_MESSAGE of string |
|
46 |
val transform_error: ('a -> 'b) -> 'a -> 'b |
|
47 |
val transform_failure: (exn -> exn) -> ('a -> 'b) -> 'a -> 'b |
|
14869 | 48 |
val timing: bool ref |
14815 | 49 |
val cond_timeit: bool -> (unit -> 'a) -> 'a |
50 |
val timeit: (unit -> 'a) -> 'a |
|
51 |
val timeap: ('a -> 'b) -> 'a -> 'b |
|
52 |
val timeap_msg: string -> ('a -> 'b) -> 'a -> 'b |
|
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
53 |
val time_accumulator: string -> ('a -> 'b) -> 'a -> 'b |
14815 | 54 |
end; |
55 |
||
56 |
signature OUTPUT = |
|
57 |
sig |
|
58 |
include BASIC_OUTPUT |
|
14881 | 59 |
val has_mode: string -> bool |
14815 | 60 |
exception MISSING_DEFAULT_OUTPUT |
61 |
val output_width: string -> string * real |
|
62 |
val output: string -> string |
|
63 |
val indent: string * int -> string |
|
64 |
val raw: string -> string |
|
65 |
val add_mode: string -> |
|
66 |
(string -> string * real) * (string * int -> string) * (string -> string) -> unit |
|
16683 | 67 |
val transform_exceptions: bool ref |
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
68 |
val accumulated_time: unit -> unit |
14815 | 69 |
end; |
70 |
||
71 |
structure Output: OUTPUT = |
|
72 |
struct |
|
73 |
||
74 |
(** print modes **) |
|
75 |
||
76 |
val print_mode = ref ([]: string list); |
|
77 |
||
14881 | 78 |
fun has_mode s = s mem_string ! print_mode; |
79 |
||
14955 | 80 |
type mode_fns = |
81 |
{output_width: string -> string * real, |
|
82 |
indent: string * int -> string, |
|
83 |
raw: string -> string}; |
|
84 |
||
85 |
val modes = ref (Symtab.empty: mode_fns Symtab.table); |
|
14815 | 86 |
|
87 |
exception MISSING_DEFAULT_OUTPUT; |
|
88 |
||
17412 | 89 |
fun lookup_mode name = Symtab.lookup (! modes) name; |
14815 | 90 |
|
91 |
fun get_mode () = |
|
15531 | 92 |
(case Library.get_first lookup_mode (! print_mode) of SOME p => p |
93 |
| NONE => |
|
94 |
(case lookup_mode "" of SOME p => p |
|
95 |
| NONE => raise MISSING_DEFAULT_OUTPUT)); (*sys_error would require output again!*) |
|
14815 | 96 |
|
14955 | 97 |
fun output_width x = #output_width (get_mode ()) x; |
14815 | 98 |
val output = #1 o output_width; |
14955 | 99 |
fun indent x = #indent (get_mode ()) x; |
100 |
fun raw x = #raw (get_mode ()) x; |
|
14815 | 101 |
|
102 |
||
103 |
||
104 |
(** output channels **) |
|
105 |
||
14984 | 106 |
(* output primitives -- normally NOT used directly!*) |
107 |
||
14815 | 108 |
fun std_output s = (TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut); |
109 |
fun std_error s = (TextIO.output (TextIO.stdErr, s); TextIO.flushOut TextIO.stdErr); |
|
110 |
||
14984 | 111 |
val immediate_output = std_output o output; |
14815 | 112 |
val writeln_default = std_output o suffix "\n"; |
113 |
||
14984 | 114 |
|
115 |
(* Isabelle output channels *) |
|
116 |
||
14815 | 117 |
val writeln_fn = ref writeln_default; |
118 |
val priority_fn = ref (fn s => ! writeln_fn s); |
|
119 |
val tracing_fn = ref (fn s => ! writeln_fn s); |
|
120 |
val warning_fn = ref (std_output o suffix "\n" o prefix_lines "### "); |
|
121 |
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
|
122 |
val panic_fn = ref (std_output o suffix "\n" o prefix_lines "!!! "); |
15190 | 123 |
val info_fn = ref (std_output o suffix "\n" o prefix_lines "+++ "); |
124 |
val debug_fn = ref (std_output o suffix "\n" o prefix_lines "::: "); |
|
14815 | 125 |
|
126 |
fun writeln s = ! writeln_fn (output s); |
|
127 |
fun priority s = ! priority_fn (output s); |
|
128 |
fun tracing s = ! tracing_fn (output s); |
|
129 |
fun warning s = ! warning_fn (output s); |
|
15190 | 130 |
fun info s = ! info_fn (output s); |
131 |
||
16191 | 132 |
fun no_warnings f = setmp warning_fn (K ()) f; |
133 |
||
15190 | 134 |
val show_debug_msgs = ref false; |
16191 | 135 |
fun debug s = if ! show_debug_msgs then ! debug_fn (output s) else () |
15190 | 136 |
|
14815 | 137 |
fun error_msg s = ! error_fn (output s); |
14862
a43f9e2c6332
Add panic function which exits Isabelle immediately.
aspinall
parents:
14815
diff
changeset
|
138 |
fun panic_msg s = ! panic_fn (output s); |
14815 | 139 |
|
16191 | 140 |
|
14815 | 141 |
(* add_mode *) |
142 |
||
14955 | 143 |
fun add_mode name (f, g, h) = |
14815 | 144 |
(if is_none (lookup_mode name) then () |
145 |
else warning ("Redeclaration of symbol print mode: " ^ quote name); |
|
17412 | 146 |
modes := Symtab.update (name, {output_width = f, indent = g, raw = h}) (! modes)); |
14815 | 147 |
|
148 |
||
14911 | 149 |
(* produce errors *) |
14815 | 150 |
|
151 |
fun error s = (error_msg s; raise ERROR); |
|
152 |
fun sys_error msg = error ("## SYSTEM ERROR ##\n" ^ msg); |
|
14862
a43f9e2c6332
Add panic function which exits Isabelle immediately.
aspinall
parents:
14815
diff
changeset
|
153 |
fun panic s = (panic_msg ("## SYSTEM EXIT ##\n" ^ s); exit 1); |
14815 | 154 |
|
155 |
fun assert p msg = if p then () else error msg; |
|
156 |
fun deny p msg = if p then error msg else (); |
|
157 |
||
158 |
(*Assert pred for every member of l, generating a message if pred fails*) |
|
159 |
fun assert_all pred l msg_fn = |
|
160 |
let fun asl [] = () |
|
161 |
| asl (x::xs) = if pred x then asl xs else error (msg_fn x) |
|
162 |
in asl l end; |
|
163 |
||
17539 | 164 |
fun overwrite (al, p as (key, _)) = |
165 |
let fun over ((q as (keyi, _)) :: pairs) = |
|
166 |
if keyi = key then p :: pairs else q :: (over pairs) |
|
167 |
| over [] = [p] |
|
168 |
in over al end; |
|
169 |
||
14815 | 170 |
fun overwrite_warn (args as (alist, (a, _))) msg = |
17521 | 171 |
(if is_none (AList.lookup (op =) alist a) then () else warning msg; |
14815 | 172 |
overwrite args); |
173 |
||
17521 | 174 |
fun update_warn eq msg (kv as (key, value)) xs = ( |
175 |
if (not o AList.defined eq xs) key then () else warning msg; |
|
176 |
AList.update eq kv xs |
|
177 |
) |
|
14815 | 178 |
|
179 |
(** handle errors **) |
|
180 |
||
181 |
datatype 'a error = |
|
182 |
Error of string | |
|
183 |
OK of 'a; |
|
184 |
||
15531 | 185 |
fun get_error (Error msg) = SOME msg |
186 |
| get_error _ = NONE; |
|
14815 | 187 |
|
15531 | 188 |
fun get_ok (OK x) = SOME x |
189 |
| get_ok _ = NONE; |
|
14815 | 190 |
|
191 |
fun handle_error f x = |
|
192 |
let |
|
193 |
val buffer = ref ([]: string list); |
|
14881 | 194 |
fun store_msg s = buffer := ! buffer @ [raw s]; |
14815 | 195 |
fun err_msg () = if not (null (! buffer)) then error_msg (cat_lines (! buffer)) else (); |
196 |
in |
|
14869 | 197 |
(case Result (setmp error_fn store_msg f x) handle exn => Exn exn of |
14815 | 198 |
Result y => (err_msg (); OK y) |
199 |
| Exn ERROR => Error (cat_lines (! buffer)) |
|
200 |
| Exn exn => (err_msg (); raise exn)) |
|
201 |
end; |
|
202 |
||
203 |
||
204 |
(* transform ERROR into ERROR_MESSAGE *) |
|
205 |
||
16683 | 206 |
val transform_exceptions = ref true; |
207 |
||
14815 | 208 |
exception ERROR_MESSAGE of string; |
209 |
||
210 |
fun transform_error f x = |
|
16683 | 211 |
if ! transform_exceptions then |
212 |
(case handle_error f x of |
|
213 |
OK y => y |
|
214 |
| Error msg => raise ERROR_MESSAGE msg) |
|
215 |
else f x; |
|
14815 | 216 |
|
217 |
||
218 |
(* transform any exception, including ERROR *) |
|
219 |
||
220 |
fun transform_failure exn f x = |
|
16683 | 221 |
if ! transform_exceptions then |
222 |
transform_error f x handle Interrupt => raise Interrupt | e => raise exn e |
|
223 |
else f x; |
|
14815 | 224 |
|
225 |
||
226 |
||
227 |
(** timing **) |
|
228 |
||
14869 | 229 |
(*global timing mode*) |
230 |
val timing = ref false; |
|
231 |
||
14815 | 232 |
(*a conditional timing function: applies f to () and, if the flag is true, |
233 |
prints its runtime on warning channel*) |
|
234 |
fun cond_timeit flag f = |
|
235 |
if flag then |
|
236 |
let val start = startTiming() |
|
237 |
val result = f () |
|
15190 | 238 |
in info (endTiming start); result end |
14815 | 239 |
else f (); |
240 |
||
241 |
(*unconditional timing function*) |
|
242 |
fun timeit x = cond_timeit true x; |
|
243 |
||
244 |
(*timed application function*) |
|
245 |
fun timeap f x = timeit (fn () => f x); |
|
15190 | 246 |
fun timeap_msg s f x = (info s; timeap f x); |
14815 | 247 |
|
14978 | 248 |
|
249 |
(* accumulated timing *) |
|
250 |
||
251 |
local |
|
252 |
||
253 |
datatype time_info = TI of |
|
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
254 |
{name: string, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
255 |
timer: Timer.cpu_timer, |
14978 | 256 |
sys: Time.time, |
257 |
usr: Time.time, |
|
258 |
gc: Time.time, |
|
259 |
count: int}; |
|
260 |
||
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
261 |
fun time_init name = ref (TI |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
262 |
{name = name, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
263 |
timer = Timer.startCPUTimer (), |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
264 |
sys = Time.zeroTime, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
265 |
usr = Time.zeroTime, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
266 |
gc = Time.zeroTime, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
267 |
count = 0}); |
14978 | 268 |
|
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
269 |
fun time_reset (r as ref (TI {name, ...})) = r := ! (time_init name); |
14978 | 270 |
|
271 |
fun time_check (ref (TI r)) = r; |
|
272 |
||
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
273 |
fun time_add ti f x = |
14978 | 274 |
let |
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
275 |
fun add_diff time time1 time2 = |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
276 |
Time.+ (time, Time.- (time2, time1) handle Time.Time => Time.zeroTime); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
277 |
val {name, timer, sys, usr, gc, count} = time_check ti; |
14978 | 278 |
val (sys1, usr1, gc1) = checkTimer timer; |
279 |
val result = capture f x; |
|
280 |
val (sys2, usr2, gc2) = checkTimer timer; |
|
281 |
in |
|
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
282 |
ti := TI |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
283 |
{name = name, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
284 |
timer = timer, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
285 |
sys = add_diff sys sys1 sys2, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
286 |
usr = add_diff usr usr1 usr2, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
287 |
gc = add_diff gc gc1 gc2, |
14978 | 288 |
count = count + 1}; |
289 |
release result |
|
290 |
end; |
|
291 |
||
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
292 |
fun time_finish ti = |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
293 |
let |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
294 |
fun secs prfx time = prfx ^ Time.toString time; |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
295 |
val {name, timer, sys, usr, gc, count} = time_check ti; |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
296 |
in |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
297 |
info ("Total of " ^ quote name ^ ": " ^ |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
298 |
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
|
299 |
" secs in " ^ string_of_int count ^ " calls"); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
300 |
time_reset ti |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
301 |
end; |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
302 |
|
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
303 |
val time_finish_hooks = ref ([]: (unit -> unit) list); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
304 |
|
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
305 |
in |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
306 |
|
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
307 |
fun time_accumulator name = |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
308 |
let val ti = time_init name in |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
309 |
change time_finish_hooks (cons (fn () => time_finish ti)); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
310 |
time_add ti |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
311 |
end; |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
312 |
|
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
313 |
fun accumulated_time () = List.app (fn f => f ()) (! time_finish_hooks); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
314 |
|
14978 | 315 |
end; |
316 |
||
14815 | 317 |
end; |
318 |
||
319 |
structure BasicOutput: BASIC_OUTPUT = Output; |
|
320 |
open BasicOutput; |