src/Pure/General/output.ML
author wenzelm
Wed, 09 Jun 2004 18:56:47 +0200
changeset 14911 396a1f4b9c14
parent 14881 e1f501a14159
child 14955 08ee855c1d94
permissions -rw-r--r--
tuned comment;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/output.ML
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     3
    Author:     Makarius, Hagia Maria Sion Abbey (Jerusalem)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     5
14911
396a1f4b9c14 tuned comment;
wenzelm
parents: 14881
diff changeset
     6
Output channels and diagnostic messages.
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     7
*)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     8
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     9
signature BASIC_OUTPUT =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    10
sig
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    11
  val print_mode: string list ref
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    12
  val std_output: string -> unit
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    13
  val std_error: string -> unit
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    14
  val writeln_default: string -> unit
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    15
  val writeln_fn: (string -> unit) ref
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    16
  val priority_fn: (string -> unit) ref
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    17
  val tracing_fn: (string -> unit) ref
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    18
  val warning_fn: (string -> unit) ref
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    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
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    21
  val writeln: string -> unit
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    22
  val priority: string -> unit
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    23
  val tracing: string -> unit
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    24
  val warning: string -> unit
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    25
  val error_msg: string -> unit
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    26
  val error: string -> 'a
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    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
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    29
  val assert: bool -> string -> unit
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    30
  val deny: bool -> string -> unit
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    31
  val assert_all: ('a -> bool) -> 'a list -> ('a -> string) -> unit
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    32
  val overwrite_warn: (''a * 'b) list * (''a * 'b) -> string -> (''a * 'b) list
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    33
  datatype 'a error = Error of string | OK of 'a
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    34
  val get_error: 'a error -> string option
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    35
  val get_ok: 'a error -> 'a option
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    36
  val handle_error: ('a -> 'b) -> 'a -> 'b error
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    37
  exception ERROR_MESSAGE of string
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    38
  val transform_error: ('a -> 'b) -> 'a -> 'b
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    39
  val transform_failure: (exn -> exn) -> ('a -> 'b) -> 'a -> 'b
14869
544be18288e6 moved exception handling back to library.ML;
wenzelm
parents: 14862
diff changeset
    40
  val timing: bool ref
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    41
  val cond_timeit: bool -> (unit -> 'a) -> 'a
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    42
  val timeit: (unit -> 'a) -> 'a
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    43
  val timeap: ('a -> 'b) -> 'a -> 'b
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    44
  val timeap_msg: string -> ('a -> 'b) -> 'a -> 'b
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    45
end;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    46
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    47
signature OUTPUT =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    48
sig
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    49
  include BASIC_OUTPUT
14881
e1f501a14159 added has_mode; handle_error: output raw;
wenzelm
parents: 14869
diff changeset
    50
  val has_mode: string -> bool
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    51
  exception MISSING_DEFAULT_OUTPUT
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    52
  val output_width: string -> string * real
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    53
  val output: string -> string
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    54
  val indent: string * int -> string
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    55
  val raw: string -> string
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    56
  val add_mode: string ->
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    57
    (string -> string * real) * (string * int -> string) * (string -> string) -> unit
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    58
end;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    59
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    60
structure Output: OUTPUT =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    61
struct
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    62
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    63
(** print modes **)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    64
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    65
val print_mode = ref ([]: string list);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    66
14881
e1f501a14159 added has_mode; handle_error: output raw;
wenzelm
parents: 14869
diff changeset
    67
fun has_mode s = s mem_string ! print_mode;
e1f501a14159 added has_mode; handle_error: output raw;
wenzelm
parents: 14869
diff changeset
    68
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    69
val modes = ref (Symtab.empty:
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    70
  ((string -> string * real) * (string * int -> string) * (string -> string))
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    71
    Symtab.table);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    72
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    73
exception MISSING_DEFAULT_OUTPUT;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    74
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    75
fun lookup_mode name = Symtab.lookup (! modes, name);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    76
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    77
fun get_mode () =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    78
  (case Library.get_first lookup_mode (! print_mode) of Some p => p
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    79
  | None =>
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    80
      (case lookup_mode "" of Some p => p
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    81
      | None => raise MISSING_DEFAULT_OUTPUT));
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    82
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    83
fun output_width x = #1 (get_mode ()) x;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    84
val output = #1 o output_width;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    85
fun indent x = #2 (get_mode ()) x;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    86
fun raw x = #3 (get_mode ()) x;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    87
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    88
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    89
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    90
(** output channels **)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    91
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    92
(*process channels -- normally NOT used directly!*)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    93
fun std_output s = (TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    94
fun std_error s = (TextIO.output (TextIO.stdErr, s); TextIO.flushOut TextIO.stdErr);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    95
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    96
val writeln_default = std_output o suffix "\n";
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    97
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    98
(*hooks for Isabelle channels*)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    99
val writeln_fn = ref writeln_default;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   100
val priority_fn = ref (fn s => ! writeln_fn s);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   101
val tracing_fn = ref (fn s => ! writeln_fn s);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   102
val warning_fn = ref (std_output o suffix "\n" o prefix_lines "### ");
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   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
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   105
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   106
fun writeln s = ! writeln_fn (output s);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   107
fun priority s = ! priority_fn (output s);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   108
fun tracing s = ! tracing_fn (output s);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   109
fun warning s = ! warning_fn (output s);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   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
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   112
14911
396a1f4b9c14 tuned comment;
wenzelm
parents: 14881
diff changeset
   113
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   114
(* add_mode *)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   115
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   116
fun add_mode name m =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   117
 (if is_none (lookup_mode name) then ()
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   118
  else warning ("Redeclaration of symbol print mode: " ^ quote name);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   119
  modes := Symtab.update ((name, m), ! modes));
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   120
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   121
14911
396a1f4b9c14 tuned comment;
wenzelm
parents: 14881
diff changeset
   122
(* produce errors *)
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   123
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   124
fun error s = (error_msg s; raise ERROR);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   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
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   127
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   128
fun assert p msg = if p then () else error msg;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   129
fun deny p msg = if p then error msg else ();
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   130
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   131
(*Assert pred for every member of l, generating a message if pred fails*)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   132
fun assert_all pred l msg_fn =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   133
  let fun asl [] = ()
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   134
        | asl (x::xs) = if pred x then asl xs else error (msg_fn x)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   135
  in asl l end;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   136
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   137
fun overwrite_warn (args as (alist, (a, _))) msg =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   138
 (if is_none (assoc (alist, a)) then () else warning msg;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   139
  overwrite args);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   140
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   141
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   142
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   143
(** handle errors  **)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   144
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   145
datatype 'a error =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   146
  Error of string |
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   147
  OK of 'a;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   148
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   149
fun get_error (Error msg) = Some msg
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   150
  | get_error _ = None;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   151
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   152
fun get_ok (OK x) = Some x
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   153
  | get_ok _ = None;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   154
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   155
fun handle_error f x =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   156
  let
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   157
    val buffer = ref ([]: string list);
14881
e1f501a14159 added has_mode; handle_error: output raw;
wenzelm
parents: 14869
diff changeset
   158
    fun store_msg s = buffer := ! buffer @ [raw s];
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   159
    fun err_msg () = if not (null (! buffer)) then error_msg (cat_lines (! buffer)) else ();
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   160
  in
14869
544be18288e6 moved exception handling back to library.ML;
wenzelm
parents: 14862
diff changeset
   161
    (case Result (setmp error_fn store_msg f x) handle exn => Exn exn of
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   162
      Result y => (err_msg (); OK y)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   163
    | Exn ERROR => Error (cat_lines (! buffer))
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   164
    | Exn exn => (err_msg (); raise exn))
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   165
  end;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   166
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   167
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   168
(* transform ERROR into ERROR_MESSAGE *)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   169
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   170
exception ERROR_MESSAGE of string;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   171
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   172
fun transform_error f x =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   173
  (case handle_error f x of
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   174
    OK y => y
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   175
  | Error msg => raise ERROR_MESSAGE msg);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   176
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   177
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   178
(* transform any exception, including ERROR *)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   179
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   180
fun transform_failure exn f x =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   181
  transform_error f x handle Interrupt => raise Interrupt | e => raise exn e;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   182
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   183
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   184
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   185
(** timing **)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   186
14869
544be18288e6 moved exception handling back to library.ML;
wenzelm
parents: 14862
diff changeset
   187
(*global timing mode*)
544be18288e6 moved exception handling back to library.ML;
wenzelm
parents: 14862
diff changeset
   188
val timing = ref false;
544be18288e6 moved exception handling back to library.ML;
wenzelm
parents: 14862
diff changeset
   189
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   190
(*a conditional timing function: applies f to () and, if the flag is true,
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   191
  prints its runtime on warning channel*)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   192
fun cond_timeit flag f =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   193
  if flag then
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   194
    let val start = startTiming()
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   195
        val result = f ()
14911
396a1f4b9c14 tuned comment;
wenzelm
parents: 14881
diff changeset
   196
    in warning (endTiming start); result end
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   197
  else f ();
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   198
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   199
(*unconditional timing function*)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   200
fun timeit x = cond_timeit true x;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   201
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   202
(*timed application function*)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   203
fun timeap f x = timeit (fn () => f x);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   204
fun timeap_msg s f x = (warning s; timeap f x);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   205
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   206
end;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   207
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   208
structure BasicOutput: BASIC_OUTPUT = Output;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   209
open BasicOutput;