src/Pure/General/output.ML
author haftmann
Tue, 20 Sep 2005 16:17:34 +0200
changeset 17521 0f1c48de39f5
parent 17412 e26cb20ef0cc
child 17539 b2ce48df4d4c
permissions -rw-r--r--
introduced AList module in favor of assoc etc.
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
14911
396a1f4b9c14 tuned comment;
wenzelm
parents: 14881
diff changeset
     5
Output channels and diagnostic messages.
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     6
*)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     7
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     8
signature BASIC_OUTPUT =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
     9
sig
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    10
  val print_mode: string list ref
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    11
  val std_output: string -> unit
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    12
  val std_error: string -> unit
14984
edbc81e60809 immediate_output;
wenzelm
parents: 14978
diff changeset
    13
  val immediate_output: string -> unit
16191
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    14
  val writeln_default: string -> unit
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    15
  val writeln_fn: (string -> unit) ref
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    16
  val priority_fn: (string -> unit) ref
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    17
  val tracing_fn: (string -> unit) ref
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    18
  val warning_fn: (string -> unit) ref
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    19
  val error_fn: (string -> unit) ref
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    20
  val panic_fn: (string -> unit) ref
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    21
  val info_fn: (string -> unit) ref
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    22
  val debug_fn: (string -> unit) ref
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    23
  val writeln: string -> unit           (*default output (in messages window)*)
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    24
  val priority: string -> unit          (*high-priority (maybe modal/pop-up; must be displayed)*)
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    25
  val tracing: string -> unit           (*tracing message (possibly in tracing window)*)
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    26
  val warning: string -> unit           (*display warning of non-fatal situation*)
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    27
  val error_msg: string -> unit         (*display fatal error (possibly modal msg)*)
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    28
  val error: string -> 'a               (*display message as above, raise exn*)
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    29
  val sys_error: string -> 'a           (*internal fatal error condition; raise exn*)
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    30
  val panic: string -> unit             (*unrecoverable fatal error; exits system!*)
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    31
  val info: string -> unit              (*incidental information message (e.g. timing)*)
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    32
  val debug: string -> unit             (*internal debug messages*)
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    33
  val show_debug_msgs: bool ref
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
    34
  val no_warnings: ('a -> 'b) -> 'a -> 'b
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    35
  val assert: bool -> string -> unit
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    36
  val deny: bool -> string -> unit
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    37
  val assert_all: ('a -> bool) -> 'a list -> ('a -> string) -> unit
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    38
  val overwrite_warn: (''a * 'b) list * (''a * 'b) -> string -> (''a * 'b) list
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    39
  datatype 'a error = Error of string | OK of 'a
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    40
  val get_error: 'a error -> string option
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    41
  val get_ok: 'a error -> 'a option
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    42
  val handle_error: ('a -> 'b) -> 'a -> 'b error
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    43
  exception ERROR_MESSAGE of string
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    44
  val transform_error: ('a -> 'b) -> 'a -> 'b
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    45
  val transform_failure: (exn -> exn) -> ('a -> 'b) -> 'a -> 'b
14869
544be18288e6 moved exception handling back to library.ML;
wenzelm
parents: 14862
diff changeset
    46
  val timing: bool ref
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    47
  val cond_timeit: bool -> (unit -> 'a) -> 'a
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    48
  val timeit: (unit -> 'a) -> 'a
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    49
  val timeap: ('a -> 'b) -> 'a -> 'b
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    50
  val timeap_msg: string -> ('a -> 'b) -> 'a -> 'b
16726
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
    51
  val time_accumulator: string -> ('a -> 'b) -> 'a -> 'b
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    52
end;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    53
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    54
signature OUTPUT =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    55
sig
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    56
  include BASIC_OUTPUT
14881
e1f501a14159 added has_mode; handle_error: output raw;
wenzelm
parents: 14869
diff changeset
    57
  val has_mode: string -> bool
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    58
  exception MISSING_DEFAULT_OUTPUT
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    59
  val output_width: string -> string * real
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    60
  val output: string -> string
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    61
  val indent: string * int -> string
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    62
  val raw: string -> string
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    63
  val add_mode: string ->
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    64
    (string -> string * real) * (string * int -> string) * (string -> string) -> unit
16683
f1ea17a4f222 added transform_exceptions: bool ref;
wenzelm
parents: 16191
diff changeset
    65
  val transform_exceptions: bool ref
16726
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
    66
  val accumulated_time: unit -> unit
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    67
end;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    68
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    69
structure Output: OUTPUT =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    70
struct
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    71
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    72
(** print modes **)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    73
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    74
val print_mode = ref ([]: string list);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    75
14881
e1f501a14159 added has_mode; handle_error: output raw;
wenzelm
parents: 14869
diff changeset
    76
fun has_mode s = s mem_string ! print_mode;
e1f501a14159 added has_mode; handle_error: output raw;
wenzelm
parents: 14869
diff changeset
    77
14955
wenzelm
parents: 14911
diff changeset
    78
type mode_fns =
wenzelm
parents: 14911
diff changeset
    79
 {output_width: string -> string * real,
wenzelm
parents: 14911
diff changeset
    80
  indent: string * int -> string,
wenzelm
parents: 14911
diff changeset
    81
  raw: string -> string};
wenzelm
parents: 14911
diff changeset
    82
wenzelm
parents: 14911
diff changeset
    83
val modes = ref (Symtab.empty: mode_fns Symtab.table);
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    84
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    85
exception MISSING_DEFAULT_OUTPUT;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    86
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
    87
fun lookup_mode name = Symtab.lookup (! modes) name;
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    88
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    89
fun get_mode () =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15190
diff changeset
    90
  (case Library.get_first lookup_mode (! print_mode) of SOME p => p
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15190
diff changeset
    91
  | NONE =>
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15190
diff changeset
    92
      (case lookup_mode "" of SOME p => p
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15190
diff changeset
    93
      | NONE => raise MISSING_DEFAULT_OUTPUT));  (*sys_error would require output again!*)
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    94
14955
wenzelm
parents: 14911
diff changeset
    95
fun output_width x = #output_width (get_mode ()) x;
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    96
val output = #1 o output_width;
14955
wenzelm
parents: 14911
diff changeset
    97
fun indent x = #indent (get_mode ()) x;
wenzelm
parents: 14911
diff changeset
    98
fun raw x = #raw (get_mode ()) x;
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
    99
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   100
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   101
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   102
(** output channels **)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   103
14984
edbc81e60809 immediate_output;
wenzelm
parents: 14978
diff changeset
   104
(* output primitives -- normally NOT used directly!*)
edbc81e60809 immediate_output;
wenzelm
parents: 14978
diff changeset
   105
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   106
fun std_output s = (TextIO.output (TextIO.stdOut, s); TextIO.flushOut TextIO.stdOut);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   107
fun std_error s = (TextIO.output (TextIO.stdErr, s); TextIO.flushOut TextIO.stdErr);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   108
14984
edbc81e60809 immediate_output;
wenzelm
parents: 14978
diff changeset
   109
val immediate_output = std_output o output;
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   110
val writeln_default = std_output o suffix "\n";
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   111
14984
edbc81e60809 immediate_output;
wenzelm
parents: 14978
diff changeset
   112
edbc81e60809 immediate_output;
wenzelm
parents: 14978
diff changeset
   113
(* Isabelle output channels *)
edbc81e60809 immediate_output;
wenzelm
parents: 14978
diff changeset
   114
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   115
val writeln_fn = ref writeln_default;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   116
val priority_fn = ref (fn s => ! writeln_fn s);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   117
val tracing_fn = ref (fn s => ! writeln_fn s);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   118
val warning_fn = ref (std_output o suffix "\n" o prefix_lines "### ");
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   119
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
   120
val panic_fn = ref (std_output o suffix "\n" o prefix_lines "!!! ");
15190
b6788dbd2ef9 Add info and debug output channels.
aspinall
parents: 15010
diff changeset
   121
val info_fn = ref (std_output o suffix "\n" o prefix_lines  "+++ ");
b6788dbd2ef9 Add info and debug output channels.
aspinall
parents: 15010
diff changeset
   122
val debug_fn = ref (std_output o suffix "\n" o prefix_lines "::: ");
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   123
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   124
fun writeln s = ! writeln_fn (output s);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   125
fun priority s = ! priority_fn (output s);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   126
fun tracing s = ! tracing_fn (output s);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   127
fun warning s = ! warning_fn (output s);
15190
b6788dbd2ef9 Add info and debug output channels.
aspinall
parents: 15010
diff changeset
   128
fun info s = ! info_fn (output s);
b6788dbd2ef9 Add info and debug output channels.
aspinall
parents: 15010
diff changeset
   129
16191
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
   130
fun no_warnings f = setmp warning_fn (K ()) f;
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
   131
15190
b6788dbd2ef9 Add info and debug output channels.
aspinall
parents: 15010
diff changeset
   132
val show_debug_msgs = ref false;
16191
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
   133
fun debug s = if ! show_debug_msgs then ! debug_fn (output s) else ()
15190
b6788dbd2ef9 Add info and debug output channels.
aspinall
parents: 15010
diff changeset
   134
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   135
fun error_msg s = ! error_fn (output s);
14862
a43f9e2c6332 Add panic function which exits Isabelle immediately.
aspinall
parents: 14815
diff changeset
   136
fun panic_msg s = ! panic_fn (output s);
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   137
16191
9d503d6fcbb1 added no_warnings;
wenzelm
parents: 15531
diff changeset
   138
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   139
(* add_mode *)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   140
14955
wenzelm
parents: 14911
diff changeset
   141
fun add_mode name (f, g, h) =
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   142
 (if is_none (lookup_mode name) then ()
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   143
  else warning ("Redeclaration of symbol print mode: " ^ quote name);
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17221
diff changeset
   144
  modes := Symtab.update (name, {output_width = f, indent = g, raw = h}) (! modes));
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   145
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   146
14911
396a1f4b9c14 tuned comment;
wenzelm
parents: 14881
diff changeset
   147
(* produce errors *)
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   148
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   149
fun error s = (error_msg s; raise ERROR);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   150
fun sys_error msg = error ("## SYSTEM ERROR ##\n" ^ msg);
14862
a43f9e2c6332 Add panic function which exits Isabelle immediately.
aspinall
parents: 14815
diff changeset
   151
fun panic s = (panic_msg ("## SYSTEM EXIT ##\n" ^ s); exit 1);
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   152
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   153
fun assert p msg = if p then () else error msg;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   154
fun deny p msg = if p then error msg else ();
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   155
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   156
(*Assert pred for every member of l, generating a message if pred fails*)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   157
fun assert_all pred l msg_fn =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   158
  let fun asl [] = ()
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   159
        | asl (x::xs) = if pred x then asl xs else error (msg_fn x)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   160
  in asl l end;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   161
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   162
fun overwrite_warn (args as (alist, (a, _))) msg =
17521
0f1c48de39f5 introduced AList module in favor of assoc etc.
haftmann
parents: 17412
diff changeset
   163
 (if is_none (AList.lookup (op =) alist a) then () else warning msg;
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   164
  overwrite args);
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   165
17521
0f1c48de39f5 introduced AList module in favor of assoc etc.
haftmann
parents: 17412
diff changeset
   166
fun update_warn eq msg (kv as (key, value)) xs = (
0f1c48de39f5 introduced AList module in favor of assoc etc.
haftmann
parents: 17412
diff changeset
   167
  if (not o AList.defined eq xs) key then () else warning msg;
0f1c48de39f5 introduced AList module in favor of assoc etc.
haftmann
parents: 17412
diff changeset
   168
  AList.update eq kv xs
0f1c48de39f5 introduced AList module in favor of assoc etc.
haftmann
parents: 17412
diff changeset
   169
)
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   170
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   171
(** handle errors  **)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   172
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   173
datatype 'a error =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   174
  Error of string |
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   175
  OK of 'a;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   176
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15190
diff changeset
   177
fun get_error (Error msg) = SOME msg
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15190
diff changeset
   178
  | get_error _ = NONE;
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   179
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15190
diff changeset
   180
fun get_ok (OK x) = SOME x
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15190
diff changeset
   181
  | get_ok _ = NONE;
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   182
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   183
fun handle_error f x =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   184
  let
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   185
    val buffer = ref ([]: string list);
14881
e1f501a14159 added has_mode; handle_error: output raw;
wenzelm
parents: 14869
diff changeset
   186
    fun store_msg s = buffer := ! buffer @ [raw s];
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   187
    fun err_msg () = if not (null (! buffer)) then error_msg (cat_lines (! buffer)) else ();
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   188
  in
14869
544be18288e6 moved exception handling back to library.ML;
wenzelm
parents: 14862
diff changeset
   189
    (case Result (setmp error_fn store_msg f x) handle exn => Exn exn of
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   190
      Result y => (err_msg (); OK y)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   191
    | Exn ERROR => Error (cat_lines (! buffer))
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   192
    | Exn exn => (err_msg (); raise exn))
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   193
  end;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   194
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   195
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   196
(* transform ERROR into ERROR_MESSAGE *)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   197
16683
f1ea17a4f222 added transform_exceptions: bool ref;
wenzelm
parents: 16191
diff changeset
   198
val transform_exceptions = ref true;
f1ea17a4f222 added transform_exceptions: bool ref;
wenzelm
parents: 16191
diff changeset
   199
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   200
exception ERROR_MESSAGE of string;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   201
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   202
fun transform_error f x =
16683
f1ea17a4f222 added transform_exceptions: bool ref;
wenzelm
parents: 16191
diff changeset
   203
  if ! transform_exceptions then
f1ea17a4f222 added transform_exceptions: bool ref;
wenzelm
parents: 16191
diff changeset
   204
    (case handle_error f x of
f1ea17a4f222 added transform_exceptions: bool ref;
wenzelm
parents: 16191
diff changeset
   205
      OK y => y
f1ea17a4f222 added transform_exceptions: bool ref;
wenzelm
parents: 16191
diff changeset
   206
    | Error msg => raise ERROR_MESSAGE msg)
f1ea17a4f222 added transform_exceptions: bool ref;
wenzelm
parents: 16191
diff changeset
   207
  else f x;
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   208
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   209
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   210
(* transform any exception, including ERROR *)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   211
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   212
fun transform_failure exn f x =
16683
f1ea17a4f222 added transform_exceptions: bool ref;
wenzelm
parents: 16191
diff changeset
   213
  if ! transform_exceptions then
f1ea17a4f222 added transform_exceptions: bool ref;
wenzelm
parents: 16191
diff changeset
   214
    transform_error f x handle Interrupt => raise Interrupt | e => raise exn e
f1ea17a4f222 added transform_exceptions: bool ref;
wenzelm
parents: 16191
diff changeset
   215
  else f x;
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   216
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   217
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   218
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   219
(** timing **)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   220
14869
544be18288e6 moved exception handling back to library.ML;
wenzelm
parents: 14862
diff changeset
   221
(*global timing mode*)
544be18288e6 moved exception handling back to library.ML;
wenzelm
parents: 14862
diff changeset
   222
val timing = ref false;
544be18288e6 moved exception handling back to library.ML;
wenzelm
parents: 14862
diff changeset
   223
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   224
(*a conditional timing function: applies f to () and, if the flag is true,
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   225
  prints its runtime on warning channel*)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   226
fun cond_timeit flag f =
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   227
  if flag then
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   228
    let val start = startTiming()
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   229
        val result = f ()
15190
b6788dbd2ef9 Add info and debug output channels.
aspinall
parents: 15010
diff changeset
   230
    in info (endTiming start); result end
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   231
  else f ();
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   232
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   233
(*unconditional timing function*)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   234
fun timeit x = cond_timeit true x;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   235
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   236
(*timed application function*)
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   237
fun timeap f x = timeit (fn () => f x);
15190
b6788dbd2ef9 Add info and debug output channels.
aspinall
parents: 15010
diff changeset
   238
fun timeap_msg s f x = (info s; timeap f x);
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   239
14978
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   240
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   241
(* accumulated timing *)
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   242
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   243
local
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   244
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   245
datatype time_info = TI of
16726
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   246
  {name: string,
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   247
   timer: Timer.cpu_timer,
14978
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   248
   sys: Time.time,
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   249
   usr: Time.time,
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   250
   gc: Time.time,
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   251
   count: int};
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   252
16726
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   253
fun time_init name = ref (TI
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   254
 {name = name,
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   255
  timer = Timer.startCPUTimer (),
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   256
  sys = Time.zeroTime,
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   257
  usr = Time.zeroTime,
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   258
  gc = Time.zeroTime,
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   259
  count = 0});
14978
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   260
16726
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   261
fun time_reset (r as ref (TI {name, ...})) = r := ! (time_init name);
14978
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   262
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   263
fun time_check (ref (TI r)) = r;
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   264
16726
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   265
fun time_add ti f x =
14978
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   266
  let
16726
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   267
    fun add_diff time time1 time2 =
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   268
      Time.+ (time, Time.- (time2, time1) handle Time.Time => Time.zeroTime);
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   269
    val {name, timer, sys, usr, gc, count} = time_check ti;
14978
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   270
    val (sys1, usr1, gc1) = checkTimer timer;
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   271
    val result = capture f x;
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   272
    val (sys2, usr2, gc2) = checkTimer timer;
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   273
  in
16726
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   274
    ti := TI
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   275
     {name = name,
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   276
      timer = timer,
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   277
      sys = add_diff sys sys1 sys2,
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   278
      usr = add_diff usr usr1 usr2,
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   279
      gc = add_diff gc gc1 gc2,
14978
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   280
      count = count + 1};
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   281
    release result
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   282
  end;
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   283
16726
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   284
fun time_finish ti =
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   285
  let
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   286
    fun secs prfx time = prfx ^ Time.toString time;
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   287
    val {name, timer, sys, usr, gc, count} = time_check ti;
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   288
  in
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   289
    info ("Total of " ^ quote name ^ ": " ^
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   290
      secs "User " usr ^ secs "  GC " gc ^ secs "  All " (Time.+ (sys, Time.+ (usr, gc))) ^
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   291
      " secs in " ^ string_of_int count ^ " calls");
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   292
    time_reset ti
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   293
  end;
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   294
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   295
val time_finish_hooks = ref ([]: (unit -> unit) list);
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   296
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   297
in
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   298
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   299
fun time_accumulator name =
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   300
  let val ti = time_init name in
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   301
    change time_finish_hooks (cons (fn () => time_finish ti));
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   302
    time_add ti
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   303
  end;
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   304
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   305
fun accumulated_time () = List.app (fn f => f ()) (! time_finish_hooks);
4399016bf13e added time_accumulator and accumulated_time supercede
wenzelm
parents: 16683
diff changeset
   306
14978
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   307
end;
108ce0289c35 added accumulated timing;
wenzelm
parents: 14955
diff changeset
   308
14815
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   309
end;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   310
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   311
structure BasicOutput: BASIC_OUTPUT = Output;
77a509d83163 output channels;
wenzelm
parents:
diff changeset
   312
open BasicOutput;