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 |
|
|
6 |
Output channels and diagnostics messages.
|
|
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
|
|
20 |
val writeln: string -> unit
|
|
21 |
val priority: string -> unit
|
|
22 |
val tracing: string -> unit
|
|
23 |
val warning: string -> unit
|
|
24 |
val error_msg: string -> unit
|
|
25 |
val error: string -> 'a
|
|
26 |
val sys_error: string -> 'a
|
|
27 |
val assert: bool -> string -> unit
|
|
28 |
val deny: bool -> string -> unit
|
|
29 |
val assert_all: ('a -> bool) -> 'a list -> ('a -> string) -> unit
|
|
30 |
val overwrite_warn: (''a * 'b) list * (''a * 'b) -> string -> (''a * 'b) list
|
|
31 |
datatype 'a error = Error of string | OK of 'a
|
|
32 |
val get_error: 'a error -> string option
|
|
33 |
val get_ok: 'a error -> 'a option
|
|
34 |
datatype 'a result = Result of 'a | Exn of exn
|
|
35 |
val get_result: 'a result -> 'a option
|
|
36 |
val get_exn: 'a result -> exn 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
|
|
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 |
val timing: bool ref
|
|
46 |
end;
|
|
47 |
|
|
48 |
signature OUTPUT =
|
|
49 |
sig
|
|
50 |
include BASIC_OUTPUT
|
|
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 |
|
|
67 |
val modes = ref (Symtab.empty:
|
|
68 |
((string -> string * real) * (string * int -> string) * (string -> string))
|
|
69 |
Symtab.table);
|
|
70 |
|
|
71 |
exception MISSING_DEFAULT_OUTPUT;
|
|
72 |
|
|
73 |
fun lookup_mode name = Symtab.lookup (! modes, name);
|
|
74 |
|
|
75 |
fun get_mode () =
|
|
76 |
(case Library.get_first lookup_mode (! print_mode) of Some p => p
|
|
77 |
| None =>
|
|
78 |
(case lookup_mode "" of Some p => p
|
|
79 |
| None => raise MISSING_DEFAULT_OUTPUT));
|
|
80 |
|
|
81 |
fun output_width x = #1 (get_mode ()) x;
|
|
82 |
val output = #1 o output_width;
|
|
83 |
fun indent x = #2 (get_mode ()) x;
|
|
84 |
fun raw x = #3 (get_mode ()) x;
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
(** output channels **)
|
|
89 |
|
|
90 |
(*process channels -- normally NOT used directly!*)
|
|
91 |
fun std_output s = (TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut);
|
|
92 |
fun std_error s = (TextIO.output (TextIO.stdErr, s); TextIO.flushOut TextIO.stdErr);
|
|
93 |
|
|
94 |
val writeln_default = std_output o suffix "\n";
|
|
95 |
|
|
96 |
(*hooks for Isabelle channels*)
|
|
97 |
val writeln_fn = ref writeln_default;
|
|
98 |
val priority_fn = ref (fn s => ! writeln_fn s);
|
|
99 |
val tracing_fn = ref (fn s => ! writeln_fn s);
|
|
100 |
val warning_fn = ref (std_output o suffix "\n" o prefix_lines "### ");
|
|
101 |
val error_fn = ref (std_output o suffix "\n" o prefix_lines "*** ");
|
|
102 |
|
|
103 |
fun writeln s = ! writeln_fn (output s);
|
|
104 |
fun priority s = ! priority_fn (output s);
|
|
105 |
fun tracing s = ! tracing_fn (output s);
|
|
106 |
fun warning s = ! warning_fn (output s);
|
|
107 |
fun error_msg s = ! error_fn (output s);
|
|
108 |
|
|
109 |
|
|
110 |
(* add_mode *)
|
|
111 |
|
|
112 |
fun add_mode name m =
|
|
113 |
(if is_none (lookup_mode name) then ()
|
|
114 |
else warning ("Redeclaration of symbol print mode: " ^ quote name);
|
|
115 |
modes := Symtab.update ((name, m), ! modes));
|
|
116 |
|
|
117 |
|
|
118 |
(* error/warning forms *)
|
|
119 |
|
|
120 |
fun error s = (error_msg s; raise ERROR);
|
|
121 |
fun sys_error msg = error ("## SYSTEM ERROR ##\n" ^ msg);
|
|
122 |
|
|
123 |
fun assert p msg = if p then () else error msg;
|
|
124 |
fun deny p msg = if p then error msg else ();
|
|
125 |
|
|
126 |
(*Assert pred for every member of l, generating a message if pred fails*)
|
|
127 |
fun assert_all pred l msg_fn =
|
|
128 |
let fun asl [] = ()
|
|
129 |
| asl (x::xs) = if pred x then asl xs else error (msg_fn x)
|
|
130 |
in asl l end;
|
|
131 |
|
|
132 |
fun overwrite_warn (args as (alist, (a, _))) msg =
|
|
133 |
(if is_none (assoc (alist, a)) then () else warning msg;
|
|
134 |
overwrite args);
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
(** handle errors **)
|
|
139 |
|
|
140 |
datatype 'a error =
|
|
141 |
Error of string |
|
|
142 |
OK of 'a;
|
|
143 |
|
|
144 |
fun get_error (Error msg) = Some msg
|
|
145 |
| get_error _ = None;
|
|
146 |
|
|
147 |
fun get_ok (OK x) = Some x
|
|
148 |
| get_ok _ = None;
|
|
149 |
|
|
150 |
datatype 'a result =
|
|
151 |
Result of 'a |
|
|
152 |
Exn of exn;
|
|
153 |
|
|
154 |
fun get_result (Result x) = Some x
|
|
155 |
| get_result _ = None;
|
|
156 |
|
|
157 |
fun get_exn (Exn exn) = Some exn
|
|
158 |
| get_exn _ = None;
|
|
159 |
|
|
160 |
fun handle_error f x =
|
|
161 |
let
|
|
162 |
val buffer = ref ([]: string list);
|
|
163 |
fun capture s = buffer := ! buffer @ [s];
|
|
164 |
fun err_msg () = if not (null (! buffer)) then error_msg (cat_lines (! buffer)) else ();
|
|
165 |
in
|
|
166 |
(case Result (setmp error_fn capture f x) handle exn => Exn exn of
|
|
167 |
Result y => (err_msg (); OK y)
|
|
168 |
| Exn ERROR => Error (cat_lines (! buffer))
|
|
169 |
| Exn exn => (err_msg (); raise exn))
|
|
170 |
end;
|
|
171 |
|
|
172 |
|
|
173 |
(* transform ERROR into ERROR_MESSAGE *)
|
|
174 |
|
|
175 |
exception ERROR_MESSAGE of string;
|
|
176 |
|
|
177 |
fun transform_error f x =
|
|
178 |
(case handle_error f x of
|
|
179 |
OK y => y
|
|
180 |
| Error msg => raise ERROR_MESSAGE msg);
|
|
181 |
|
|
182 |
|
|
183 |
(* transform any exception, including ERROR *)
|
|
184 |
|
|
185 |
fun transform_failure exn f x =
|
|
186 |
transform_error f x handle Interrupt => raise Interrupt | e => raise exn e;
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
(** timing **)
|
|
191 |
|
|
192 |
(*a conditional timing function: applies f to () and, if the flag is true,
|
|
193 |
prints its runtime on warning channel*)
|
|
194 |
fun cond_timeit flag f =
|
|
195 |
if flag then
|
|
196 |
let val start = startTiming()
|
|
197 |
val result = f ()
|
|
198 |
in warning (endTiming start); result end
|
|
199 |
else f ();
|
|
200 |
|
|
201 |
(*unconditional timing function*)
|
|
202 |
fun timeit x = cond_timeit true x;
|
|
203 |
|
|
204 |
(*timed application function*)
|
|
205 |
fun timeap f x = timeit (fn () => f x);
|
|
206 |
fun timeap_msg s f x = (warning s; timeap f x);
|
|
207 |
|
|
208 |
(*global timing mode*)
|
|
209 |
val timing = ref false;
|
|
210 |
|
|
211 |
end;
|
|
212 |
|
|
213 |
structure BasicOutput: BASIC_OUTPUT = Output;
|
|
214 |
open BasicOutput;
|