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