src/Pure/Isar/runtime.ML
author wenzelm
Thu, 18 Aug 2011 15:15:43 +0200
changeset 44264 c21ecbb028b6
parent 44249 64620f1d6f87
child 44270 3eaad39e520c
permissions -rw-r--r--
tune Par_Exn.make: balance merge; clarified Par_Exn.dest: later exceptions first;
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
38874
4a4d34d2f97b more careful treatment of nested multi-exceptions, collapsing cumulatively empty list to Interrupt;
wenzelm
parents: 37370
diff changeset
    15
  val exn_messages: (exn -> Position.T) -> exn -> string list
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    16
  val exn_message: (exn -> Position.T) -> exn -> string
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    17
  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
    18
  val controlled_execution: ('a -> 'b) -> 'a -> 'b
33603
3713a5208671 generalized Runtime.toplevel_error wrt. output function;
wenzelm
parents: 32091
diff changeset
    19
  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
    20
end;
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    21
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    22
structure Runtime: RUNTIME =
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    23
struct
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
(** exceptions **)
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    26
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    27
exception UNDEF;
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    28
exception TERMINATE;
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    29
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
    30
exception TOPLEVEL_ERROR;
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    31
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
    32
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
    33
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    34
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    35
(* exn_context *)
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    36
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    37
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
    38
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    39
fun exn_context NONE exn = exn
39285
85728a4b5620 avoid extra wrapping for interrupts;
wenzelm
parents: 39238
diff changeset
    40
  | 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
    41
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    42
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    43
(* exn_message *)
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
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
    46
  | 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
    47
38874
4a4d34d2f97b more careful treatment of nested multi-exceptions, collapsing cumulatively empty list to Interrupt;
wenzelm
parents: 37370
diff changeset
    48
fun exn_messages exn_position e =
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    49
  let
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    50
    fun raised exn name msgs =
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    51
      let val pos = Position.str_of (exn_position exn) in
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    52
        (case msgs of
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    53
          [] => "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
    54
        | [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
    55
        | _ => 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
    56
      end;
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    57
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
    58
    val detailed = ! debug;
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
    59
39232
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 38875
diff changeset
    60
    fun exn_msgs context exn =
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 38875
diff changeset
    61
      if Exn.is_interrupt exn then []
69c6d3e87660 more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents: 38875
diff changeset
    62
      else
44247
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    63
        (case Par_Exn.dest exn of
44264
c21ecbb028b6 tune Par_Exn.make: balance merge;
wenzelm
parents: 44249
diff changeset
    64
          SOME exns => maps (exn_msgs context) (rev exns)
44247
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    65
        | NONE =>
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    66
            (case exn of
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    67
              Exn.EXCEPTIONS exns => maps (exn_msgs context) exns
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    68
            | CONTEXT (ctxt, exn) => exn_msgs (SOME ctxt) exn
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    69
            | EXCURSION_FAIL (exn, loc) =>
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    70
                map (fn msg => msg ^ Markup.markup Markup.no_report ("\n" ^ loc))
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    71
                  (exn_msgs context exn)
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    72
            | TERMINATE => ["Exit"]
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    73
            | TimeLimit.TimeOut => ["Timeout"]
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    74
            | TOPLEVEL_ERROR => ["Error"]
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    75
            | ERROR msg => [msg]
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    76
            | Fail msg => [raised exn "Fail" [msg]]
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    77
            | THEORY (msg, thys) =>
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    78
                [raised exn "THEORY" (msg :: (if detailed then map Context.str_of_thy thys else []))]
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    79
            | Ast.AST (msg, asts) =>
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    80
                [raised exn "AST" (msg ::
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    81
                  (if detailed then map (Pretty.string_of o Ast.pretty_ast) asts else []))]
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    82
            | TYPE (msg, Ts, ts) =>
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    83
                [raised exn "TYPE" (msg ::
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    84
                  (if detailed then
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    85
                    if_context context Syntax.string_of_typ Ts @
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    86
                    if_context context Syntax.string_of_term ts
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    87
                   else []))]
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    88
            | TERM (msg, ts) =>
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    89
                [raised exn "TERM" (msg ::
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    90
                  (if detailed then if_context context Syntax.string_of_term ts else []))]
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    91
            | THM (msg, i, thms) =>
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    92
                [raised exn ("THM " ^ string_of_int i) (msg ::
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    93
                  (if detailed then if_context context Display.string_of_thm thms else []))]
270366301bd7 more systematic handling of parallel exceptions;
wenzelm
parents: 43761
diff changeset
    94
            | _ => [raised exn (General.exnMessage exn) []]));
38874
4a4d34d2f97b more careful treatment of nested multi-exceptions, collapsing cumulatively empty list to Interrupt;
wenzelm
parents: 37370
diff changeset
    95
  in exn_msgs NONE e end;
4a4d34d2f97b more careful treatment of nested multi-exceptions, collapsing cumulatively empty list to Interrupt;
wenzelm
parents: 37370
diff changeset
    96
4a4d34d2f97b more careful treatment of nested multi-exceptions, collapsing cumulatively empty list to Interrupt;
wenzelm
parents: 37370
diff changeset
    97
fun exn_message exn_position exn =
4a4d34d2f97b more careful treatment of nested multi-exceptions, collapsing cumulatively empty list to Interrupt;
wenzelm
parents: 37370
diff changeset
    98
  (case exn_messages exn_position exn of
38875
c7a66b584147 tuned messages: discontinued spurious full-stops (messages are occasionally composed unexpectedly);
wenzelm
parents: 38874
diff changeset
    99
    [] => "Interrupt"
38874
4a4d34d2f97b more careful treatment of nested multi-exceptions, collapsing cumulatively empty list to Interrupt;
wenzelm
parents: 37370
diff changeset
   100
  | msgs => cat_lines msgs);
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   101
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   102
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   103
(** controlled execution **)
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   104
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   105
fun debugging f x =
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
   106
  if ! debug then
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   107
    Exn.release (exception_trace (fn () =>
43761
e72ba84ae58f tuned signature -- corresponding to Scala version;
wenzelm
parents: 42224
diff changeset
   108
      Exn.Res (f x) handle
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   109
        exn as UNDEF => Exn.Exn exn
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   110
      | exn as EXCURSION_FAIL _ => Exn.Exn exn))
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   111
  else f x;
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   112
41715
22f8c2483bd2 explicit Multithreading.interrupted to ensure that interrupts stay within the boundaries of managed evaluation blocks;
wenzelm
parents: 40318
diff changeset
   113
fun controlled_execution f x =
22f8c2483bd2 explicit Multithreading.interrupted to ensure that interrupts stay within the boundaries of managed evaluation blocks;
wenzelm
parents: 40318
diff changeset
   114
  ((f |> debugging |> Future.interruptible_task) x before Multithreading.interrupted ());
31476
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   115
39238
7189a138dd6c refined Runtime.toplevel_error/Document.run_command: let interrupts pass unhindered;
wenzelm
parents: 39232
diff changeset
   116
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
   117
  handle exn =>
7189a138dd6c refined Runtime.toplevel_error/Document.run_command: let interrupts pass unhindered;
wenzelm
parents: 39232
diff changeset
   118
    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
   119
    else
7189a138dd6c refined Runtime.toplevel_error/Document.run_command: let interrupts pass unhindered;
wenzelm
parents: 39232
diff changeset
   120
      let
7189a138dd6c refined Runtime.toplevel_error/Document.run_command: let interrupts pass unhindered;
wenzelm
parents: 39232
diff changeset
   121
        val ctxt = Option.map Context.proof_of (Context.thread_data ());
7189a138dd6c refined Runtime.toplevel_error/Document.run_command: let interrupts pass unhindered;
wenzelm
parents: 39232
diff changeset
   122
        val _ = output_exn (exn_context ctxt exn);
7189a138dd6c refined Runtime.toplevel_error/Document.run_command: let interrupts pass unhindered;
wenzelm
parents: 39232
diff changeset
   123
      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
   124
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   125
end;
c5d2899b6de9 moved Isar toplevel runtime support to runtime.ML, which is loaded early (before ml_compiler.ML);
wenzelm
parents:
diff changeset
   126