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