src/Pure/Isar/runtime.ML
author wenzelm
Mon, 08 Apr 2013 21:01:59 +0200
changeset 51640 d022e8bd2375
parent 51639 b7f908c99546
child 51653 97de25c51b2d
permissions -rw-r--r--
improved printing of exception CTERM (see also d0f0f37ec346);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/runtime.ML
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
     3
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
     4
Isar toplevel runtime support.
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
     5
*)
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
     6
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
     7
signature RUNTIME =
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
     8
sig
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
     9
  exception UNDEF
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    10
  exception TERMINATE
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    11
  exception EXCURSION_FAIL of exn * string
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    12
  exception TOPLEVEL_ERROR
39513
fce2202892c4 discontinued Output.debug, which belongs to early PGIP experiments (b6788dbd2ef9) and causes just too many problems (like spamming the message channel if it is used by more than one module);
wenzelm
parents: 39439
diff changeset
    13
  val debug: bool Unsynchronized.ref
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    14
  val exn_context: Proof.context option -> exn -> exn
51639
b7f908c99546 prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
wenzelm
parents: 51285
diff changeset
    15
  type exn_info = {exn_position: exn -> Position.T, pretty_exn: exn -> Pretty.T}
51285
0859bd338c9b tuned signature;
wenzelm
parents: 51242
diff changeset
    16
  type error = ((serial * string) * string option)
51639
b7f908c99546 prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
wenzelm
parents: 51285
diff changeset
    17
  val exn_messages_ids: exn_info -> exn -> error list
b7f908c99546 prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
wenzelm
parents: 51285
diff changeset
    18
  val exn_messages: exn_info -> exn -> (serial * string) list
b7f908c99546 prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
wenzelm
parents: 51285
diff changeset
    19
  val exn_message: exn_info -> exn -> string
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    20
  val debugging: ('a -> 'b) -> 'a -> 'b
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    21
  val controlled_execution: ('a -> 'b) -> 'a -> 'b
33603
3713a5208671 generalized Runtime.toplevel_error wrt. output function;
wenzelm
parents: 32091
diff changeset
    22
  val toplevel_error: (exn -> unit) -> ('a -> 'b) -> 'a -> 'b
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    23
end;
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    24
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    25
structure Runtime: RUNTIME =
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    26
struct
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    27
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    28
(** exceptions **)
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    29
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    30
exception UNDEF;
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    31
exception TERMINATE;
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    32
exception EXCURSION_FAIL of exn * string;
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    33
exception TOPLEVEL_ERROR;
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    34
39513
fce2202892c4 discontinued Output.debug, which belongs to early PGIP experiments (b6788dbd2ef9) and causes just too many problems (like spamming the message channel if it is used by more than one module);
wenzelm
parents: 39439
diff changeset
    35
val debug = Unsynchronized.ref false;
fce2202892c4 discontinued Output.debug, which belongs to early PGIP experiments (b6788dbd2ef9) and causes just too many problems (like spamming the message channel if it is used by more than one module);
wenzelm
parents: 39439
diff changeset
    36
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    37
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    38
(* exn_context *)
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    39
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    40
exception CONTEXT of Proof.context * exn;
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    41
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    42
fun exn_context NONE exn = exn
39285
85728a4b5620 avoid extra wrapping for interrupts;
wenzelm
parents: 39238
diff changeset
    43
  | exn_context (SOME ctxt) exn = if Exn.is_interrupt exn then exn else CONTEXT (ctxt, exn);
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    44
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    45
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    46
(* exn_message *)
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    47
51639
b7f908c99546 prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
wenzelm
parents: 51285
diff changeset
    48
type exn_info = {exn_position: exn -> Position.T, pretty_exn: exn -> Pretty.T};
51285
0859bd338c9b tuned signature;
wenzelm
parents: 51242
diff changeset
    49
type error = ((serial * string) * string option);
0859bd338c9b tuned signature;
wenzelm
parents: 51242
diff changeset
    50
50914
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
    51
local
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
    52
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    53
fun if_context NONE _ _ = []
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    54
  | if_context (SOME ctxt) f xs = map (f ctxt) xs;
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    55
50914
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
    56
fun identify exn =
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    57
  let
50914
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
    58
    val exn' = Par_Exn.identify [] exn;
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
    59
    val exec_id = Properties.get (Exn_Properties.get exn') Markup.exec_idN;
51242
a8e664e4fb5f identify exceptions more robustly, to allow SML/NJ report toplevel errors without crash;
wenzelm
parents: 50914
diff changeset
    60
    val i = Par_Exn.the_serial exn' handle Option.Option => serial ();
a8e664e4fb5f identify exceptions more robustly, to allow SML/NJ report toplevel errors without crash;
wenzelm
parents: 50914
diff changeset
    61
  in ((i, exn'), exec_id) end;
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents: 50505
diff changeset
    62
50914
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
    63
fun flatten _ (CONTEXT (ctxt, exn)) = flatten (SOME ctxt) exn
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
    64
  | flatten context (Exn.EXCEPTIONS exns) = maps (flatten context) exns
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
    65
  | flatten context exn =
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
    66
      (case Par_Exn.dest exn of
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
    67
        SOME exns => maps (flatten context) exns
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
    68
      | NONE => [(context, identify exn)]);
50911
ee7fe4230642 more explicit treatment of (optional) exception properties, notably for "serial" -- avoid conflict with startPosition = offset;
wenzelm
parents: 50505
diff changeset
    69
50914
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
    70
in
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
    71
51639
b7f908c99546 prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
wenzelm
parents: 51285
diff changeset
    72
fun exn_messages_ids {exn_position, pretty_exn} e =
50914
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
    73
  let
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    74
    fun raised exn name msgs =
48992
0518bf89c777 renamed Position.str_of to Position.here;
wenzelm
parents: 45666
diff changeset
    75
      let val pos = Position.here (exn_position exn) in
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    76
        (case msgs of
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    77
          [] => "exception " ^ name ^ " raised" ^ pos
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    78
        | [msg] => "exception " ^ name ^ " raised" ^ pos ^ ": " ^ msg
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    79
        | _ => cat_lines (("exception " ^ name ^ " raised" ^ pos ^ ":") :: msgs))
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    80
      end;
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    81
50914
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
    82
    fun exn_msgs (context, ((i, exn), id)) =
44270
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 44264
diff changeset
    83
      (case exn of
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 44264
diff changeset
    84
        EXCURSION_FAIL (exn, loc) =>
50914
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
    85
          map (fn ((i, msg), id) => ((i, msg ^ Markup.markup Markup.no_report ("\n" ^ loc)), id))
44270
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 44264
diff changeset
    86
            (sorted_msgs context exn)
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 44264
diff changeset
    87
      | _ =>
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 44264
diff changeset
    88
        let
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 44264
diff changeset
    89
          val msg =
44247
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    90
            (case exn of
44270
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 44264
diff changeset
    91
              TERMINATE => "Exit"
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 44264
diff changeset
    92
            | TimeLimit.TimeOut => "Timeout"
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 44264
diff changeset
    93
            | TOPLEVEL_ERROR => "Error"
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 44264
diff changeset
    94
            | ERROR msg => msg
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 44264
diff changeset
    95
            | Fail msg => raised exn "Fail" [msg]
44247
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    96
            | THEORY (msg, thys) =>
45487
ae60518ac054 simplified Runtime.exn_messages: always print detailed version of low-level exeptions, which should not occur in regular user errors anyway;
wenzelm
parents: 45486
diff changeset
    97
                raised exn "THEORY" (msg :: map Context.str_of_thy thys)
44247
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    98
            | Ast.AST (msg, asts) =>
45487
ae60518ac054 simplified Runtime.exn_messages: always print detailed version of low-level exeptions, which should not occur in regular user errors anyway;
wenzelm
parents: 45486
diff changeset
    99
                raised exn "AST" (msg :: map (Pretty.string_of o Ast.pretty_ast) asts)
44247
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
   100
            | TYPE (msg, Ts, ts) =>
44270
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 44264
diff changeset
   101
                raised exn "TYPE" (msg ::
45487
ae60518ac054 simplified Runtime.exn_messages: always print detailed version of low-level exeptions, which should not occur in regular user errors anyway;
wenzelm
parents: 45486
diff changeset
   102
                  (if_context context Syntax.string_of_typ Ts @
ae60518ac054 simplified Runtime.exn_messages: always print detailed version of low-level exeptions, which should not occur in regular user errors anyway;
wenzelm
parents: 45486
diff changeset
   103
                    if_context context Syntax.string_of_term ts))
44247
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
   104
            | TERM (msg, ts) =>
45487
ae60518ac054 simplified Runtime.exn_messages: always print detailed version of low-level exeptions, which should not occur in regular user errors anyway;
wenzelm
parents: 45486
diff changeset
   105
                raised exn "TERM" (msg :: if_context context Syntax.string_of_term ts)
51640
d022e8bd2375 improved printing of exception CTERM (see also d0f0f37ec346);
wenzelm
parents: 51639
diff changeset
   106
            | CTERM (msg, cts) =>
d022e8bd2375 improved printing of exception CTERM (see also d0f0f37ec346);
wenzelm
parents: 51639
diff changeset
   107
                raised exn "CTERM"
d022e8bd2375 improved printing of exception CTERM (see also d0f0f37ec346);
wenzelm
parents: 51639
diff changeset
   108
                  (msg :: if_context context Syntax.string_of_term (map term_of cts))
44247
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
   109
            | THM (msg, i, thms) =>
45487
ae60518ac054 simplified Runtime.exn_messages: always print detailed version of low-level exeptions, which should not occur in regular user errors anyway;
wenzelm
parents: 45486
diff changeset
   110
                raised exn ("THM " ^ string_of_int i)
ae60518ac054 simplified Runtime.exn_messages: always print detailed version of low-level exeptions, which should not occur in regular user errors anyway;
wenzelm
parents: 45486
diff changeset
   111
                  (msg :: if_context context Display.string_of_thm thms)
51639
b7f908c99546 prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
wenzelm
parents: 51285
diff changeset
   112
            | _ => raised exn (Pretty.string_of (pretty_exn exn)) []);
50914
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
   113
        in [((i, msg), id)] end)
44270
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 44264
diff changeset
   114
      and sorted_msgs context exn =
50914
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
   115
        sort_distinct (int_ord o pairself (fst o fst)) (maps exn_msgs (flatten context exn));
44270
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 44264
diff changeset
   116
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 44264
diff changeset
   117
  in sorted_msgs NONE e end;
38874
4a4d34d2f97b more careful treatment of nested multi-exceptions, collapsing cumulatively empty list to Interrupt;
wenzelm
parents: 37370
diff changeset
   118
50914
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
   119
end;
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
   120
51639
b7f908c99546 prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
wenzelm
parents: 51285
diff changeset
   121
fun exn_messages exn_info exn = map #1 (exn_messages_ids exn_info exn);
50914
fe4714886d92 identify future results more carefully, to avoid odd duplication of error messages, notably from forked goals;
wenzelm
parents: 50911
diff changeset
   122
51639
b7f908c99546 prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
wenzelm
parents: 51285
diff changeset
   123
fun exn_message exn_info exn =
b7f908c99546 prefer pretty_exn where possible -- NB: low-level General.exnMessage may still be used elsewhere (e.g. by the ML compiler itself);
wenzelm
parents: 51285
diff changeset
   124
  (case exn_messages exn_info exn of
38875
c7a66b584147 tuned messages: discontinued spurious full-stops (messages are occasionally composed unexpectedly);
wenzelm
parents: 38874
diff changeset
   125
    [] => "Interrupt"
44270
3eaad39e520c more careful treatment of exception serial numbers, with propagation to message channel;
wenzelm
parents: 44264
diff changeset
   126
  | msgs => cat_lines (map snd msgs));
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   127
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   128
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   129
(** controlled execution **)
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   130
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   131
fun debugging f x =
45486
600682331b79 more detailed exception_trace: in Poly/ML 5.4.x intermediate handlers reset the trace (cf. Poly/ML SVN 1110);
wenzelm
parents: 45197
diff changeset
   132
  if ! debug
600682331b79 more detailed exception_trace: in Poly/ML 5.4.x intermediate handlers reset the trace (cf. Poly/ML SVN 1110);
wenzelm
parents: 45197
diff changeset
   133
  then exception_trace (fn () => f x)
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   134
  else f x;
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   135
41715
22f8c2483bd2 explicit Multithreading.interrupted to ensure that interrupts stay within the boundaries of managed evaluation blocks;
wenzelm
parents: 40318
diff changeset
   136
fun controlled_execution f x =
44295
e43f0ea90c9a more focused use of Multithreading.interrupted: retain interrupts within task group boundary, without loss of information;
wenzelm
parents: 44271
diff changeset
   137
  (f |> debugging |> Future.interruptible_task) x;
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   138
39238
7189a138dd6c refined Runtime.toplevel_error/Document.run_command: let interrupts pass unhindered;
wenzelm
parents: 39232
diff changeset
   139
fun toplevel_error output_exn f x = f x
7189a138dd6c refined Runtime.toplevel_error/Document.run_command: let interrupts pass unhindered;
wenzelm
parents: 39232
diff changeset
   140
  handle exn =>
7189a138dd6c refined Runtime.toplevel_error/Document.run_command: let interrupts pass unhindered;
wenzelm
parents: 39232
diff changeset
   141
    if Exn.is_interrupt exn then reraise exn
7189a138dd6c refined Runtime.toplevel_error/Document.run_command: let interrupts pass unhindered;
wenzelm
parents: 39232
diff changeset
   142
    else
7189a138dd6c refined Runtime.toplevel_error/Document.run_command: let interrupts pass unhindered;
wenzelm
parents: 39232
diff changeset
   143
      let
45197
b6c527c64789 more robust toplevel_error reporting (NB: Context.proof_of on a stale theory crashes ungracefully);
wenzelm
parents: 44295
diff changeset
   144
        val opt_ctxt =
b6c527c64789 more robust toplevel_error reporting (NB: Context.proof_of on a stale theory crashes ungracefully);
wenzelm
parents: 44295
diff changeset
   145
          (case Context.thread_data () of
b6c527c64789 more robust toplevel_error reporting (NB: Context.proof_of on a stale theory crashes ungracefully);
wenzelm
parents: 44295
diff changeset
   146
            NONE => NONE
b6c527c64789 more robust toplevel_error reporting (NB: Context.proof_of on a stale theory crashes ungracefully);
wenzelm
parents: 44295
diff changeset
   147
          | SOME context => try Context.proof_of context);
b6c527c64789 more robust toplevel_error reporting (NB: Context.proof_of on a stale theory crashes ungracefully);
wenzelm
parents: 44295
diff changeset
   148
        val _ = output_exn (exn_context opt_ctxt exn);
39238
7189a138dd6c refined Runtime.toplevel_error/Document.run_command: let interrupts pass unhindered;
wenzelm
parents: 39232
diff changeset
   149
      in raise TOPLEVEL_ERROR end;
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   150
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   151
end;
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   152