author | wenzelm |
Wed, 09 Jun 2004 18:56:47 +0200 | |
changeset 14911 | 396a1f4b9c14 |
parent 14881 | e1f501a14159 |
child 14955 | 08ee855c1d94 |
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 |
|
45 |
end; |
|
46 |
||
47 |
signature OUTPUT = |
|
48 |
sig |
|
49 |
include BASIC_OUTPUT |
|
14881 | 50 |
val has_mode: string -> bool |
14815 | 51 |
exception MISSING_DEFAULT_OUTPUT |
52 |
val output_width: string -> string * real |
|
53 |
val output: string -> string |
|
54 |
val indent: string * int -> string |
|
55 |
val raw: string -> string |
|
56 |
val add_mode: string -> |
|
57 |
(string -> string * real) * (string * int -> string) * (string -> string) -> unit |
|
58 |
end; |
|
59 |
||
60 |
structure Output: OUTPUT = |
|
61 |
struct |
|
62 |
||
63 |
(** print modes **) |
|
64 |
||
65 |
val print_mode = ref ([]: string list); |
|
66 |
||
14881 | 67 |
fun has_mode s = s mem_string ! print_mode; |
68 |
||
14815 | 69 |
val modes = ref (Symtab.empty: |
70 |
((string -> string * real) * (string * int -> string) * (string -> string)) |
|
71 |
Symtab.table); |
|
72 |
||
73 |
exception MISSING_DEFAULT_OUTPUT; |
|
74 |
||
75 |
fun lookup_mode name = Symtab.lookup (! modes, name); |
|
76 |
||
77 |
fun get_mode () = |
|
78 |
(case Library.get_first lookup_mode (! print_mode) of Some p => p |
|
79 |
| None => |
|
80 |
(case lookup_mode "" of Some p => p |
|
81 |
| None => raise MISSING_DEFAULT_OUTPUT)); |
|
82 |
||
83 |
fun output_width x = #1 (get_mode ()) x; |
|
84 |
val output = #1 o output_width; |
|
85 |
fun indent x = #2 (get_mode ()) x; |
|
86 |
fun raw x = #3 (get_mode ()) x; |
|
87 |
||
88 |
||
89 |
||
90 |
(** output channels **) |
|
91 |
||
92 |
(*process channels -- normally NOT used directly!*) |
|
93 |
fun std_output s = (TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut); |
|
94 |
fun std_error s = (TextIO.output (TextIO.stdErr, s); TextIO.flushOut TextIO.stdErr); |
|
95 |
||
96 |
val writeln_default = std_output o suffix "\n"; |
|
97 |
||
98 |
(*hooks for Isabelle channels*) |
|
99 |
val writeln_fn = ref writeln_default; |
|
100 |
val priority_fn = ref (fn s => ! writeln_fn s); |
|
101 |
val tracing_fn = ref (fn s => ! writeln_fn s); |
|
102 |
val warning_fn = ref (std_output o suffix "\n" o prefix_lines "### "); |
|
103 |
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
|
104 |
val panic_fn = ref (std_output o suffix "\n" o prefix_lines "!!! "); |
14815 | 105 |
|
106 |
fun writeln s = ! writeln_fn (output s); |
|
107 |
fun priority s = ! priority_fn (output s); |
|
108 |
fun tracing s = ! tracing_fn (output s); |
|
109 |
fun warning s = ! warning_fn (output s); |
|
110 |
fun error_msg s = ! error_fn (output s); |
|
14862
a43f9e2c6332
Add panic function which exits Isabelle immediately.
aspinall
parents:
14815
diff
changeset
|
111 |
fun panic_msg s = ! panic_fn (output s); |
14815 | 112 |
|
14911 | 113 |
|
14815 | 114 |
(* add_mode *) |
115 |
||
116 |
fun add_mode name m = |
|
117 |
(if is_none (lookup_mode name) then () |
|
118 |
else warning ("Redeclaration of symbol print mode: " ^ quote name); |
|
119 |
modes := Symtab.update ((name, m), ! modes)); |
|
120 |
||
121 |
||
14911 | 122 |
(* produce errors *) |
14815 | 123 |
|
124 |
fun error s = (error_msg s; raise ERROR); |
|
125 |
fun sys_error msg = error ("## SYSTEM ERROR ##\n" ^ msg); |
|
14862
a43f9e2c6332
Add panic function which exits Isabelle immediately.
aspinall
parents:
14815
diff
changeset
|
126 |
fun panic s = (panic_msg ("## SYSTEM EXIT ##\n" ^ s); exit 1); |
14815 | 127 |
|
128 |
fun assert p msg = if p then () else error msg; |
|
129 |
fun deny p msg = if p then error msg else (); |
|
130 |
||
131 |
(*Assert pred for every member of l, generating a message if pred fails*) |
|
132 |
fun assert_all pred l msg_fn = |
|
133 |
let fun asl [] = () |
|
134 |
| asl (x::xs) = if pred x then asl xs else error (msg_fn x) |
|
135 |
in asl l end; |
|
136 |
||
137 |
fun overwrite_warn (args as (alist, (a, _))) msg = |
|
138 |
(if is_none (assoc (alist, a)) then () else warning msg; |
|
139 |
overwrite args); |
|
140 |
||
141 |
||
142 |
||
143 |
(** handle errors **) |
|
144 |
||
145 |
datatype 'a error = |
|
146 |
Error of string | |
|
147 |
OK of 'a; |
|
148 |
||
149 |
fun get_error (Error msg) = Some msg |
|
150 |
| get_error _ = None; |
|
151 |
||
152 |
fun get_ok (OK x) = Some x |
|
153 |
| get_ok _ = None; |
|
154 |
||
155 |
fun handle_error f x = |
|
156 |
let |
|
157 |
val buffer = ref ([]: string list); |
|
14881 | 158 |
fun store_msg s = buffer := ! buffer @ [raw s]; |
14815 | 159 |
fun err_msg () = if not (null (! buffer)) then error_msg (cat_lines (! buffer)) else (); |
160 |
in |
|
14869 | 161 |
(case Result (setmp error_fn store_msg f x) handle exn => Exn exn of |
14815 | 162 |
Result y => (err_msg (); OK y) |
163 |
| Exn ERROR => Error (cat_lines (! buffer)) |
|
164 |
| Exn exn => (err_msg (); raise exn)) |
|
165 |
end; |
|
166 |
||
167 |
||
168 |
(* transform ERROR into ERROR_MESSAGE *) |
|
169 |
||
170 |
exception ERROR_MESSAGE of string; |
|
171 |
||
172 |
fun transform_error f x = |
|
173 |
(case handle_error f x of |
|
174 |
OK y => y |
|
175 |
| Error msg => raise ERROR_MESSAGE msg); |
|
176 |
||
177 |
||
178 |
(* transform any exception, including ERROR *) |
|
179 |
||
180 |
fun transform_failure exn f x = |
|
181 |
transform_error f x handle Interrupt => raise Interrupt | e => raise exn e; |
|
182 |
||
183 |
||
184 |
||
185 |
(** timing **) |
|
186 |
||
14869 | 187 |
(*global timing mode*) |
188 |
val timing = ref false; |
|
189 |
||
14815 | 190 |
(*a conditional timing function: applies f to () and, if the flag is true, |
191 |
prints its runtime on warning channel*) |
|
192 |
fun cond_timeit flag f = |
|
193 |
if flag then |
|
194 |
let val start = startTiming() |
|
195 |
val result = f () |
|
14911 | 196 |
in warning (endTiming start); result end |
14815 | 197 |
else f (); |
198 |
||
199 |
(*unconditional timing function*) |
|
200 |
fun timeit x = cond_timeit true x; |
|
201 |
||
202 |
(*timed application function*) |
|
203 |
fun timeap f x = timeit (fn () => f x); |
|
204 |
fun timeap_msg s f x = (warning s; timeap f x); |
|
205 |
||
206 |
end; |
|
207 |
||
208 |
structure BasicOutput: BASIC_OUTPUT = Output; |
|
209 |
open BasicOutput; |