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