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