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