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