author | wenzelm |
Thu, 19 Jan 2006 21:22:21 +0100 | |
changeset 18716 | bb4af2bdd17b |
parent 18682 | 216692feebab |
child 19265 | cae36e16f3c0 |
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 |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
27 |
val update_warn: ('a * 'a -> bool) -> string -> ('a * 'b) -> ('a * 'b) list -> ('a * 'b) list |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
28 |
val overwrite_warn: (''a * 'b) list * (''a * 'b) -> string -> (''a * 'b) list |
16191 | 29 |
val show_debug_msgs: bool ref |
30 |
val no_warnings: ('a -> 'b) -> 'a -> 'b |
|
14869 | 31 |
val timing: bool ref |
14815 | 32 |
val cond_timeit: bool -> (unit -> 'a) -> 'a |
33 |
val timeit: (unit -> 'a) -> 'a |
|
34 |
val timeap: ('a -> 'b) -> 'a -> 'b |
|
35 |
val timeap_msg: string -> ('a -> 'b) -> 'a -> 'b |
|
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
36 |
val time_accumulator: string -> ('a -> 'b) -> 'a -> 'b |
14815 | 37 |
end; |
38 |
||
39 |
signature OUTPUT = |
|
40 |
sig |
|
41 |
include BASIC_OUTPUT |
|
14881 | 42 |
val has_mode: string -> bool |
14815 | 43 |
val output_width: string -> string * real |
44 |
val output: string -> string |
|
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 |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
53 |
val debug: string -> unit |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
54 |
val error_msg: string -> unit |
14815 | 55 |
val add_mode: string -> |
56 |
(string -> string * real) * (string * int -> string) * (string -> string) -> unit |
|
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
57 |
val accumulated_time: unit -> unit |
14815 | 58 |
end; |
59 |
||
60 |
structure Output: OUTPUT = |
|
61 |
struct |
|
62 |
||
63 |
(** print modes **) |
|
64 |
||
65 |
val print_mode = ref ([]: string list); |
|
66 |
||
14881 | 67 |
fun has_mode s = s mem_string ! print_mode; |
68 |
||
14955 | 69 |
type mode_fns = |
70 |
{output_width: string -> string * real, |
|
71 |
indent: string * int -> string, |
|
72 |
raw: string -> string}; |
|
73 |
||
74 |
val modes = ref (Symtab.empty: mode_fns Symtab.table); |
|
14815 | 75 |
|
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
76 |
fun lookup_mode name = Symtab.lookup (! modes) name; |
14815 | 77 |
|
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
78 |
exception MISSING_DEFAULT_OUTPUT; |
14815 | 79 |
|
80 |
fun get_mode () = |
|
15531 | 81 |
(case Library.get_first lookup_mode (! print_mode) of SOME p => p |
82 |
| NONE => |
|
83 |
(case lookup_mode "" of SOME p => p |
|
84 |
| NONE => raise MISSING_DEFAULT_OUTPUT)); (*sys_error would require output again!*) |
|
14815 | 85 |
|
14955 | 86 |
fun output_width x = #output_width (get_mode ()) x; |
14815 | 87 |
val output = #1 o output_width; |
14955 | 88 |
fun indent x = #indent (get_mode ()) x; |
89 |
fun raw x = #raw (get_mode ()) x; |
|
14815 | 90 |
|
91 |
||
92 |
||
93 |
(** output channels **) |
|
94 |
||
14984 | 95 |
(* output primitives -- normally NOT used directly!*) |
96 |
||
14815 | 97 |
fun std_output s = (TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut); |
98 |
fun std_error s = (TextIO.output (TextIO.stdErr, s); TextIO.flushOut TextIO.stdErr); |
|
99 |
||
14984 | 100 |
val immediate_output = std_output o output; |
14815 | 101 |
val writeln_default = std_output o suffix "\n"; |
102 |
||
14984 | 103 |
|
104 |
(* Isabelle output channels *) |
|
105 |
||
14815 | 106 |
val writeln_fn = ref writeln_default; |
107 |
val priority_fn = ref (fn s => ! writeln_fn s); |
|
108 |
val tracing_fn = ref (fn s => ! writeln_fn s); |
|
109 |
val warning_fn = ref (std_output o suffix "\n" o prefix_lines "### "); |
|
110 |
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
|
111 |
val panic_fn = ref (std_output o suffix "\n" o prefix_lines "!!! "); |
15190 | 112 |
val info_fn = ref (std_output o suffix "\n" o prefix_lines "+++ "); |
113 |
val debug_fn = ref (std_output o suffix "\n" o prefix_lines "::: "); |
|
14815 | 114 |
|
115 |
fun writeln s = ! writeln_fn (output s); |
|
116 |
fun priority s = ! priority_fn (output s); |
|
117 |
fun tracing s = ! tracing_fn (output s); |
|
118 |
fun warning s = ! warning_fn (output s); |
|
15190 | 119 |
fun info s = ! info_fn (output s); |
120 |
||
16191 | 121 |
fun no_warnings f = setmp warning_fn (K ()) f; |
122 |
||
15190 | 123 |
val show_debug_msgs = ref false; |
16191 | 124 |
fun debug s = if ! show_debug_msgs then ! debug_fn (output s) else () |
15190 | 125 |
|
14815 | 126 |
fun error_msg s = ! error_fn (output s); |
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
127 |
|
14862
a43f9e2c6332
Add panic function which exits Isabelle immediately.
aspinall
parents:
14815
diff
changeset
|
128 |
fun panic_msg s = ! panic_fn (output s); |
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
129 |
fun panic s = (panic_msg ("## SYSTEM EXIT ##\n" ^ s); exit 1); |
14815 | 130 |
|
131 |
||
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
132 |
(* toplevel errors *) |
14815 | 133 |
|
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
134 |
exception TOPLEVEL_ERROR; |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
135 |
|
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
136 |
val do_toplevel_errors = ref true; |
14815 | 137 |
|
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
138 |
fun toplevel_errors f x = |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
139 |
if ! do_toplevel_errors then |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
140 |
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
|
141 |
else f x; |
14815 | 142 |
|
18716 | 143 |
fun ML_errors f = setmp do_toplevel_errors true (toplevel_errors f); |
144 |
||
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
145 |
|
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
146 |
(* AList operations *) |
14815 | 147 |
|
17539 | 148 |
fun overwrite (al, p as (key, _)) = |
149 |
let fun over ((q as (keyi, _)) :: pairs) = |
|
150 |
if keyi = key then p :: pairs else q :: (over pairs) |
|
151 |
| over [] = [p] |
|
152 |
in over al end; |
|
153 |
||
14815 | 154 |
fun overwrite_warn (args as (alist, (a, _))) msg = |
17521 | 155 |
(if is_none (AList.lookup (op =) alist a) then () else warning msg; |
14815 | 156 |
overwrite args); |
157 |
||
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
158 |
fun update_warn eq msg (kv as (key, value)) xs = |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
159 |
(if (not o AList.defined eq xs) key then () else warning msg; |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
160 |
AList.update eq kv xs); |
14815 | 161 |
|
162 |
||
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
163 |
(* add_mode *) |
14815 | 164 |
|
18682
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
165 |
fun add_mode name (f, g, h) = |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
166 |
(if is_none (lookup_mode name) then () |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
167 |
else warning ("Redeclaration of symbol print mode: " ^ quote name); |
216692feebab
removed special ERROR handling stuff (transform_error etc.);
wenzelm
parents:
17539
diff
changeset
|
168 |
modes := Symtab.update (name, {output_width = f, indent = g, raw = h}) (! modes)); |
14815 | 169 |
|
170 |
||
171 |
||
172 |
(** timing **) |
|
173 |
||
14869 | 174 |
(*global timing mode*) |
175 |
val timing = ref false; |
|
176 |
||
14815 | 177 |
(*a conditional timing function: applies f to () and, if the flag is true, |
178 |
prints its runtime on warning channel*) |
|
179 |
fun cond_timeit flag f = |
|
180 |
if flag then |
|
181 |
let val start = startTiming() |
|
182 |
val result = f () |
|
15190 | 183 |
in info (endTiming start); result end |
14815 | 184 |
else f (); |
185 |
||
186 |
(*unconditional timing function*) |
|
187 |
fun timeit x = cond_timeit true x; |
|
188 |
||
189 |
(*timed application function*) |
|
190 |
fun timeap f x = timeit (fn () => f x); |
|
15190 | 191 |
fun timeap_msg s f x = (info s; timeap f x); |
14815 | 192 |
|
14978 | 193 |
|
194 |
(* accumulated timing *) |
|
195 |
||
196 |
local |
|
197 |
||
198 |
datatype time_info = TI of |
|
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
199 |
{name: string, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
200 |
timer: Timer.cpu_timer, |
14978 | 201 |
sys: Time.time, |
202 |
usr: Time.time, |
|
203 |
gc: Time.time, |
|
204 |
count: int}; |
|
205 |
||
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
206 |
fun time_init name = ref (TI |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
207 |
{name = name, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
208 |
timer = Timer.startCPUTimer (), |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
209 |
sys = Time.zeroTime, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
210 |
usr = Time.zeroTime, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
211 |
gc = Time.zeroTime, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
212 |
count = 0}); |
14978 | 213 |
|
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
214 |
fun time_reset (r as ref (TI {name, ...})) = r := ! (time_init name); |
14978 | 215 |
|
216 |
fun time_check (ref (TI r)) = r; |
|
217 |
||
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
218 |
fun time_add ti f x = |
14978 | 219 |
let |
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
220 |
fun add_diff time time1 time2 = |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
221 |
Time.+ (time, Time.- (time2, time1) handle Time.Time => Time.zeroTime); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
222 |
val {name, timer, sys, usr, gc, count} = time_check ti; |
14978 | 223 |
val (sys1, usr1, gc1) = checkTimer timer; |
224 |
val result = capture f x; |
|
225 |
val (sys2, usr2, gc2) = checkTimer timer; |
|
226 |
in |
|
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
227 |
ti := TI |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
228 |
{name = name, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
229 |
timer = timer, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
230 |
sys = add_diff sys sys1 sys2, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
231 |
usr = add_diff usr usr1 usr2, |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
232 |
gc = add_diff gc gc1 gc2, |
14978 | 233 |
count = count + 1}; |
234 |
release result |
|
235 |
end; |
|
236 |
||
16726
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
237 |
fun time_finish ti = |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
238 |
let |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
239 |
fun secs prfx time = prfx ^ Time.toString time; |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
240 |
val {name, timer, sys, usr, gc, count} = time_check ti; |
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 |
info ("Total of " ^ quote name ^ ": " ^ |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
243 |
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
|
244 |
" secs in " ^ string_of_int count ^ " calls"); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
245 |
time_reset ti |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
246 |
end; |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
247 |
|
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
248 |
val time_finish_hooks = ref ([]: (unit -> unit) list); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
249 |
|
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
250 |
in |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
251 |
|
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
252 |
fun time_accumulator name = |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
253 |
let val ti = time_init name in |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
254 |
change time_finish_hooks (cons (fn () => time_finish ti)); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
255 |
time_add ti |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
256 |
end; |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
257 |
|
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
258 |
fun accumulated_time () = List.app (fn f => f ()) (! time_finish_hooks); |
4399016bf13e
added time_accumulator and accumulated_time supercede
wenzelm
parents:
16683
diff
changeset
|
259 |
|
14978 | 260 |
end; |
261 |
||
14815 | 262 |
end; |
263 |
||
264 |
structure BasicOutput: BASIC_OUTPUT = Output; |
|
265 |
open BasicOutput; |