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