src/Pure/Isar/toplevel.ML
author wenzelm
Tue Mar 11 22:31:09 2008 +0100 (2008-03-11)
changeset 26256 3e7939e978c6
parent 26081 fbdb1161b4b0
child 26257 707969e76f5c
permissions -rw-r--r--
added exception CONTEXT, indicating context of another exception;
exn_message: explicit context, *not* ML_Context.get_context;
toplevel_error/apply: explicit exn_context;
explicit Markup.location for exn_info (cf. at_command);
fixed spelling;
tuned;
wenzelm@5828
     1
(*  Title:      Pure/Isar/toplevel.ML
wenzelm@5828
     2
    ID:         $Id$
wenzelm@5828
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5828
     4
wenzelm@5828
     5
The Isabelle/Isar toplevel.
wenzelm@5828
     6
*)
wenzelm@5828
     7
wenzelm@5828
     8
signature TOPLEVEL =
wenzelm@5828
     9
sig
wenzelm@19063
    10
  exception UNDEF
wenzelm@20963
    11
  type generic_theory
wenzelm@18589
    12
  type node
wenzelm@20963
    13
  val theory_node: node -> generic_theory option
wenzelm@18589
    14
  val proof_node: node -> ProofHistory.T option
wenzelm@20963
    15
  val cases_node: (generic_theory -> 'a) -> (Proof.state -> 'a) -> node -> 'a
wenzelm@20963
    16
  val presentation_context: node option -> xstring option -> Proof.context
wenzelm@5828
    17
  type state
wenzelm@7732
    18
  val is_toplevel: state -> bool
wenzelm@18589
    19
  val is_theory: state -> bool
wenzelm@18589
    20
  val is_proof: state -> bool
wenzelm@17076
    21
  val level: state -> int
wenzelm@6689
    22
  val node_history_of: state -> node History.T
wenzelm@5828
    23
  val node_of: state -> node
wenzelm@20963
    24
  val node_case: (generic_theory -> 'a) -> (Proof.state -> 'a) -> state -> 'a
wenzelm@21506
    25
  val context_of: state -> Proof.context
wenzelm@22089
    26
  val generic_theory_of: state -> generic_theory
wenzelm@5828
    27
  val theory_of: state -> theory
wenzelm@5828
    28
  val proof_of: state -> Proof.state
wenzelm@18589
    29
  val proof_position_of: state -> int
wenzelm@21007
    30
  val enter_proof_body: state -> Proof.state
wenzelm@16815
    31
  val print_state_context: state -> unit
wenzelm@16815
    32
  val print_state: bool -> state -> unit
wenzelm@16682
    33
  val quiet: bool ref
wenzelm@16682
    34
  val debug: bool ref
wenzelm@17321
    35
  val interact: bool ref
wenzelm@16682
    36
  val timing: bool ref
wenzelm@16682
    37
  val profiling: int ref
wenzelm@16815
    38
  val skip_proofs: bool ref
wenzelm@25584
    39
  val crashes: exn list ref
wenzelm@5828
    40
  exception TERMINATE
wenzelm@5990
    41
  exception RESTART
wenzelm@26256
    42
  exception CONTEXT of Proof.context * exn
wenzelm@24055
    43
  exception TOPLEVEL_ERROR
wenzelm@20128
    44
  val exn_message: exn -> string
wenzelm@20128
    45
  val program: (unit -> 'a) -> 'a
wenzelm@16682
    46
  type transition
wenzelm@6689
    47
  val undo_limit: bool -> int option
wenzelm@5828
    48
  val empty: transition
wenzelm@14923
    49
  val name_of: transition -> string
wenzelm@14923
    50
  val source_of: transition -> OuterLex.token list option
wenzelm@5828
    51
  val name: string -> transition -> transition
wenzelm@5828
    52
  val position: Position.T -> transition -> transition
wenzelm@14923
    53
  val source: OuterLex.token list -> transition -> transition
wenzelm@5828
    54
  val interactive: bool -> transition -> transition
wenzelm@5828
    55
  val print: transition -> transition
wenzelm@16607
    56
  val print': string -> transition -> transition
wenzelm@17363
    57
  val three_buffersN: string
wenzelm@17363
    58
  val print3: transition -> transition
wenzelm@9010
    59
  val no_timing: transition -> transition
wenzelm@22056
    60
  val init_theory: (bool -> theory) -> (theory -> unit) -> (theory -> unit) ->
wenzelm@22056
    61
    transition -> transition
wenzelm@25441
    62
  val init_empty: (state -> bool) -> (unit -> unit) -> transition -> transition
wenzelm@6689
    63
  val exit: transition -> transition
wenzelm@21958
    64
  val undo_exit: transition -> transition
wenzelm@6689
    65
  val kill: transition -> transition
wenzelm@20128
    66
  val history: (node History.T -> node History.T) -> transition -> transition
wenzelm@5828
    67
  val keep: (state -> unit) -> transition -> transition
wenzelm@7612
    68
  val keep': (bool -> state -> unit) -> transition -> transition
wenzelm@5828
    69
  val imperative: (unit -> unit) -> transition -> transition
wenzelm@5828
    70
  val theory: (theory -> theory) -> transition -> transition
wenzelm@7612
    71
  val theory': (bool -> theory -> theory) -> transition -> transition
wenzelm@20985
    72
  val begin_local_theory: bool -> (theory -> local_theory) -> transition -> transition
wenzelm@21007
    73
  val end_local_theory: transition -> transition
wenzelm@20963
    74
  val local_theory: xstring option -> (local_theory -> local_theory) -> transition -> transition
wenzelm@20963
    75
  val present_local_theory: xstring option -> (bool -> node -> unit) -> transition -> transition
berghofe@24453
    76
  val local_theory_to_proof': xstring option -> (bool -> local_theory -> Proof.state) ->
berghofe@24453
    77
    transition -> transition
wenzelm@21007
    78
  val local_theory_to_proof: xstring option -> (local_theory -> Proof.state) ->
wenzelm@21007
    79
    transition -> transition
wenzelm@17363
    80
  val theory_to_proof: (theory -> Proof.state) -> transition -> transition
wenzelm@21007
    81
  val end_proof: (bool -> Proof.state -> Proof.context) -> transition -> transition
wenzelm@21007
    82
  val forget_proof: transition -> transition
wenzelm@21177
    83
  val present_proof: (bool -> node -> unit) -> transition -> transition
wenzelm@21177
    84
  val proofs': (bool -> Proof.state -> Proof.state Seq.seq) -> transition -> transition
wenzelm@17904
    85
  val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
wenzelm@21177
    86
  val proofs: (Proof.state -> Proof.state Seq.seq) -> transition -> transition
wenzelm@21177
    87
  val proof: (Proof.state -> Proof.state) -> transition -> transition
wenzelm@16815
    88
  val actual_proof: (ProofHistory.T -> ProofHistory.T) -> transition -> transition
wenzelm@16815
    89
  val skip_proof: (int History.T -> int History.T) -> transition -> transition
wenzelm@17904
    90
  val skip_proof_to_theory: (int -> bool) -> transition -> transition
wenzelm@9512
    91
  val unknown_theory: transition -> transition
wenzelm@9512
    92
  val unknown_proof: transition -> transition
wenzelm@9512
    93
  val unknown_context: transition -> transition
wenzelm@17076
    94
  val present_excursion: (transition * (state -> state -> 'a -> 'a)) list -> 'a -> 'a
wenzelm@5828
    95
  val excursion: transition list -> unit
wenzelm@5828
    96
  val set_state: state -> unit
wenzelm@5828
    97
  val get_state: unit -> state
wenzelm@5828
    98
  val exn: unit -> (exn * string) option
wenzelm@5828
    99
  val >> : transition -> bool
wenzelm@14985
   100
  val >>> : transition list -> unit
wenzelm@21958
   101
  val init_state: unit -> unit
nipkow@14091
   102
  type 'a isar
wenzelm@25527
   103
  val loop: bool -> 'a isar -> unit
wenzelm@5828
   104
end;
wenzelm@5828
   105
wenzelm@6965
   106
structure Toplevel: TOPLEVEL =
wenzelm@5828
   107
struct
wenzelm@5828
   108
wenzelm@21958
   109
wenzelm@5828
   110
(** toplevel state **)
wenzelm@5828
   111
wenzelm@19063
   112
exception UNDEF;
wenzelm@19063
   113
wenzelm@19063
   114
wenzelm@21294
   115
(* local theory wrappers *)
wenzelm@5828
   116
wenzelm@20963
   117
type generic_theory = Context.generic;    (*theory or local_theory*)
wenzelm@20963
   118
wenzelm@25292
   119
val loc_init = TheoryTarget.context;
wenzelm@21294
   120
val loc_exit = ProofContext.theory_of o LocalTheory.exit;
wenzelm@21294
   121
wenzelm@25292
   122
fun loc_begin loc (Context.Theory thy) = loc_init (the_default "-" loc) thy
wenzelm@21294
   123
  | loc_begin NONE (Context.Proof lthy) = lthy
haftmann@25269
   124
  | loc_begin (SOME loc) (Context.Proof lthy) = loc_init loc (loc_exit lthy);
wenzelm@21294
   125
wenzelm@21294
   126
fun loc_finish _ (Context.Theory _) = Context.Theory o loc_exit
wenzelm@21294
   127
  | loc_finish NONE (Context.Proof _) = Context.Proof o LocalTheory.restore
wenzelm@25292
   128
  | loc_finish (SOME _) (Context.Proof lthy) = fn lthy' =>
wenzelm@25292
   129
      Context.Proof (LocalTheory.reinit (LocalTheory.raw_theory (K (loc_exit lthy')) lthy));
wenzelm@21294
   130
wenzelm@21294
   131
wenzelm@21958
   132
(* datatype node *)
wenzelm@21294
   133
wenzelm@5828
   134
datatype node =
wenzelm@20963
   135
  Theory of generic_theory * Proof.context option | (*theory with presentation context*)
wenzelm@21007
   136
  Proof of ProofHistory.T * ((Proof.context -> generic_theory) * generic_theory) |
wenzelm@21007
   137
    (*history of proof states, finish, original theory*)
wenzelm@21007
   138
  SkipProof of int History.T * (generic_theory * generic_theory);
wenzelm@18563
   139
    (*history of proof depths, resulting theory, original theory*)
wenzelm@5828
   140
wenzelm@22056
   141
val the_global_theory = fn Theory (Context.Theory thy, _) => thy | _ => raise UNDEF;
wenzelm@20963
   142
val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
wenzelm@18589
   143
val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
wenzelm@18589
   144
wenzelm@20963
   145
fun cases_node f _ (Theory (gthy, _)) = f gthy
wenzelm@19063
   146
  | cases_node _ g (Proof (prf, _)) = g (ProofHistory.current prf)
wenzelm@21007
   147
  | cases_node f _ (SkipProof (_, (gthy, _))) = f gthy;
wenzelm@19063
   148
wenzelm@20963
   149
fun presentation_context (SOME (Theory (_, SOME ctxt))) NONE = ctxt
wenzelm@20963
   150
  | presentation_context (SOME node) NONE = cases_node Context.proof_of Proof.context_of node
wenzelm@20963
   151
  | presentation_context (SOME node) (SOME loc) =
haftmann@25269
   152
      loc_init loc (cases_node Context.theory_of Proof.theory_of node)
wenzelm@20963
   153
  | presentation_context NONE _ = raise UNDEF;
wenzelm@19063
   154
wenzelm@21958
   155
wenzelm@21958
   156
(* datatype state *)
wenzelm@21958
   157
wenzelm@22056
   158
type state_info = node History.T * ((theory -> unit) * (theory -> unit));
wenzelm@5828
   159
wenzelm@21958
   160
datatype state =
wenzelm@21958
   161
  Toplevel of state_info option |  (*outer toplevel, leftover end state*)
wenzelm@21958
   162
  State of state_info;
wenzelm@5828
   163
wenzelm@21958
   164
val toplevel = Toplevel NONE;
wenzelm@21958
   165
wenzelm@21958
   166
fun is_toplevel (Toplevel _) = true
wenzelm@7732
   167
  | is_toplevel _ = false;
wenzelm@7732
   168
wenzelm@21958
   169
fun level (Toplevel _) = 0
wenzelm@21958
   170
  | level (State (node, _)) =
wenzelm@17076
   171
      (case History.current node of
wenzelm@21310
   172
        Theory _ => 0
wenzelm@21310
   173
      | Proof (prf, _) => Proof.level (ProofHistory.current prf)
wenzelm@21310
   174
      | SkipProof (h, _) => History.current h + 1);   (*different notion of proof depth!*)
wenzelm@17076
   175
wenzelm@21958
   176
fun str_of_state (Toplevel _) = "at top level"
wenzelm@21958
   177
  | str_of_state (State (node, _)) =
wenzelm@16815
   178
      (case History.current node of
wenzelm@20963
   179
        Theory (Context.Theory _, _) => "in theory mode"
wenzelm@20963
   180
      | Theory (Context.Proof _, _) => "in local theory mode"
wenzelm@16815
   181
      | Proof _ => "in proof mode"
wenzelm@16815
   182
      | SkipProof _ => "in skipped proof mode");
wenzelm@5946
   183
wenzelm@5946
   184
wenzelm@5828
   185
(* top node *)
wenzelm@5828
   186
wenzelm@21958
   187
fun node_history_of (Toplevel _) = raise UNDEF
wenzelm@21958
   188
  | node_history_of (State (node, _)) = node;
wenzelm@6689
   189
wenzelm@6689
   190
val node_of = History.current o node_history_of;
wenzelm@5828
   191
wenzelm@18589
   192
fun is_theory state = not (is_toplevel state) andalso is_some (theory_node (node_of state));
wenzelm@18589
   193
fun is_proof state = not (is_toplevel state) andalso is_some (proof_node (node_of state));
wenzelm@18589
   194
wenzelm@19063
   195
fun node_case f g state = cases_node f g (node_of state);
wenzelm@5828
   196
wenzelm@21506
   197
val context_of = node_case Context.proof_of Proof.context_of;
wenzelm@22089
   198
val generic_theory_of = node_case I (Context.Proof o Proof.context_of);
wenzelm@20963
   199
val theory_of = node_case Context.theory_of Proof.theory_of;
wenzelm@18589
   200
val proof_of = node_case (fn _ => raise UNDEF) I;
wenzelm@17208
   201
wenzelm@18589
   202
fun proof_position_of state =
wenzelm@18589
   203
  (case node_of state of
wenzelm@18589
   204
    Proof (prf, _) => ProofHistory.position prf
wenzelm@18589
   205
  | _ => raise UNDEF);
wenzelm@6664
   206
wenzelm@21007
   207
val enter_proof_body = node_case (Proof.init o Context.proof_of) Proof.enter_forward;
wenzelm@5828
   208
wenzelm@5828
   209
wenzelm@16815
   210
(* print state *)
wenzelm@16815
   211
wenzelm@25292
   212
val pretty_context = LocalTheory.pretty o Context.cases (TheoryTarget.init NONE) I;
wenzelm@16815
   213
wenzelm@23640
   214
fun print_state_context state =
wenzelm@24795
   215
  (case try node_of state of
wenzelm@21506
   216
    NONE => []
wenzelm@24795
   217
  | SOME (Theory (gthy, _)) => pretty_context gthy
wenzelm@24795
   218
  | SOME (Proof (_, (_, gthy))) => pretty_context gthy
wenzelm@24795
   219
  | SOME (SkipProof (_, (gthy, _))) => pretty_context gthy)
wenzelm@23640
   220
  |> Pretty.chunks |> Pretty.writeln;
wenzelm@16815
   221
wenzelm@23640
   222
fun print_state prf_only state =
wenzelm@23701
   223
  (case try node_of state of
wenzelm@23701
   224
    NONE => []
wenzelm@23701
   225
  | SOME (Theory (gthy, _)) => if prf_only then [] else pretty_context gthy
wenzelm@23701
   226
  | SOME (Proof (prf, _)) =>
wenzelm@23701
   227
      Proof.pretty_state (ProofHistory.position prf) (ProofHistory.current prf)
wenzelm@23701
   228
  | SOME (SkipProof (h, _)) =>
wenzelm@23701
   229
      [Pretty.str ("skipped proof: depth " ^ string_of_int (History.current h))])
wenzelm@23701
   230
  |> Pretty.markup_chunks Markup.state |> Pretty.writeln;
wenzelm@16815
   231
wenzelm@16815
   232
wenzelm@15668
   233
wenzelm@5828
   234
(** toplevel transitions **)
wenzelm@5828
   235
wenzelm@16682
   236
val quiet = ref false;
wenzelm@22135
   237
val debug = Output.debugging;
wenzelm@17321
   238
val interact = ref false;
wenzelm@16682
   239
val timing = Output.timing;
wenzelm@16682
   240
val profiling = ref 0;
wenzelm@16815
   241
val skip_proofs = ref false;
wenzelm@25584
   242
val crashes = ref ([]: exn list);
wenzelm@16682
   243
wenzelm@5828
   244
exception TERMINATE;
wenzelm@5990
   245
exception RESTART;
wenzelm@7022
   246
exception EXCURSION_FAIL of exn * string;
wenzelm@6689
   247
exception FAILURE of state * exn;
wenzelm@24055
   248
exception TOPLEVEL_ERROR;
wenzelm@5828
   249
wenzelm@20128
   250
wenzelm@20128
   251
(* print exceptions *)
wenzelm@20128
   252
wenzelm@26256
   253
exception CONTEXT of Proof.context * exn;
wenzelm@26256
   254
wenzelm@26256
   255
fun exn_context NONE exn = exn
wenzelm@26256
   256
  | exn_context (SOME ctxt) exn = CONTEXT (ctxt, exn);
wenzelm@26256
   257
wenzelm@20128
   258
local
wenzelm@20128
   259
wenzelm@26256
   260
fun if_context NONE _ _ = []
wenzelm@26256
   261
  | if_context (SOME ctxt) f xs = map (f ctxt) xs;
wenzelm@20128
   262
wenzelm@20128
   263
fun raised name [] = "exception " ^ name ^ " raised"
wenzelm@20128
   264
  | raised name [msg] = "exception " ^ name ^ " raised: " ^ msg
wenzelm@20128
   265
  | raised name msgs = cat_lines (("exception " ^ name ^ " raised:") :: msgs);
wenzelm@20128
   266
wenzelm@20128
   267
in
wenzelm@20128
   268
wenzelm@26256
   269
fun exn_message e =
wenzelm@26256
   270
  let
wenzelm@26256
   271
    val detailed = ! debug;
wenzelm@26256
   272
wenzelm@26256
   273
    fun exn_msg _ (CONTEXT (ctxt, exn)) = exn_msg (SOME ctxt) exn
wenzelm@26256
   274
      | exn_msg ctxt (Exn.EXCEPTIONS (exns, "")) = cat_lines (map (exn_msg ctxt) exns)
wenzelm@26256
   275
      | exn_msg ctxt (Exn.EXCEPTIONS (exns, msg)) = cat_lines (map (exn_msg ctxt) exns @ [msg])
wenzelm@26256
   276
      | exn_msg ctxt (EXCURSION_FAIL (exn, loc)) = exn_msg ctxt exn ^
wenzelm@26256
   277
          Output.escape (Markup.enclose Markup.location (Output.output ("\n" ^ loc)))
wenzelm@26256
   278
      | exn_msg _ TERMINATE = "Exit."
wenzelm@26256
   279
      | exn_msg _ RESTART = "Restart."
wenzelm@26256
   280
      | exn_msg _ Interrupt = "Interrupt."
wenzelm@26256
   281
      | exn_msg _ TimeLimit.TimeOut = "Timeout."
wenzelm@26256
   282
      | exn_msg _ TOPLEVEL_ERROR = "Error."
wenzelm@26256
   283
      | exn_msg _ (SYS_ERROR msg) = "## SYSTEM ERROR ##\n" ^ msg
wenzelm@26256
   284
      | exn_msg _ (ERROR msg) = msg
wenzelm@26256
   285
      | exn_msg _ (Fail msg) = raised "Fail" [msg]
wenzelm@26256
   286
      | exn_msg _ (THEORY (msg, thys)) =
wenzelm@26256
   287
          raised "THEORY" (msg :: (if detailed then map Context.str_of_thy thys else []))
wenzelm@26256
   288
      | exn_msg _ (Syntax.AST (msg, asts)) = raised "AST" (msg ::
wenzelm@26256
   289
            (if detailed then map (Pretty.string_of o Syntax.pretty_ast) asts else []))
wenzelm@26256
   290
      | exn_msg ctxt (TYPE (msg, Ts, ts)) = raised "TYPE" (msg ::
wenzelm@26256
   291
            (if detailed then
wenzelm@26256
   292
              if_context ctxt Syntax.string_of_typ Ts @ if_context ctxt Syntax.string_of_term ts
wenzelm@26256
   293
             else []))
wenzelm@26256
   294
      | exn_msg ctxt (TERM (msg, ts)) = raised "TERM" (msg ::
wenzelm@26256
   295
            (if detailed then if_context ctxt Syntax.string_of_term ts else []))
wenzelm@26256
   296
      | exn_msg ctxt (THM (msg, i, thms)) = raised ("THM " ^ string_of_int i) (msg ::
wenzelm@26256
   297
            (if detailed then if_context ctxt ProofContext.string_of_thm thms else []))
wenzelm@26256
   298
      | exn_msg _ exn = raised (General.exnMessage exn) []
wenzelm@26256
   299
  in exn_msg NONE e end;
wenzelm@26256
   300
wenzelm@26256
   301
fun print_exn exn_info = Output.error_msg (exn_message (EXCURSION_FAIL exn_info));
wenzelm@20128
   302
wenzelm@20128
   303
end;
wenzelm@20128
   304
wenzelm@20128
   305
wenzelm@20128
   306
(* controlled execution *)
wenzelm@20128
   307
wenzelm@20128
   308
local
wenzelm@20128
   309
wenzelm@18685
   310
fun debugging f x =
wenzelm@23940
   311
  if ! debug then exception_trace (fn () => f x)
wenzelm@18685
   312
  else f x;
wenzelm@18685
   313
wenzelm@26256
   314
fun toplevel_error f x =
wenzelm@26256
   315
  let val ctxt = try ML_Context.the_local_context () in
wenzelm@26256
   316
    f x handle exn =>
wenzelm@26256
   317
      (Output.error_msg (exn_message (exn_context ctxt exn)); raise TOPLEVEL_ERROR)
wenzelm@26256
   318
  end;
wenzelm@24055
   319
wenzelm@20128
   320
in
wenzelm@20128
   321
wenzelm@20128
   322
fun controlled_execution f =
wenzelm@20128
   323
  f
wenzelm@20128
   324
  |> debugging
wenzelm@24055
   325
  |> interruptible;
wenzelm@20128
   326
wenzelm@20128
   327
fun program f =
wenzelm@24055
   328
 (f
wenzelm@24055
   329
  |> debugging
wenzelm@24055
   330
  |> toplevel_error) ();
wenzelm@20128
   331
wenzelm@20128
   332
end;
wenzelm@20128
   333
wenzelm@5828
   334
wenzelm@16815
   335
(* node transactions and recovery from stale theories *)
wenzelm@6689
   336
wenzelm@16815
   337
(*NB: proof commands should be non-destructive!*)
wenzelm@7022
   338
wenzelm@6689
   339
local
wenzelm@6689
   340
wenzelm@16452
   341
fun is_stale state = Context.is_stale (theory_of state) handle UNDEF => false;
wenzelm@6689
   342
wenzelm@26256
   343
val stale_theory = ERROR "Stale theory encountered after successful execution!";
wenzelm@16815
   344
wenzelm@23363
   345
fun map_theory f = History.map_current
wenzelm@21177
   346
  (fn Theory (gthy, _) => Theory (Context.mapping f (LocalTheory.raw_theory f) gthy, NONE)
wenzelm@21177
   347
    | node => node);
wenzelm@6689
   348
skalberg@15531
   349
fun return (result, NONE) = result
skalberg@15531
   350
  | return (result, SOME exn) = raise FAILURE (result, exn);
wenzelm@7022
   351
wenzelm@6689
   352
in
wenzelm@6689
   353
wenzelm@23363
   354
fun transaction hist pos f (node, term) =
wenzelm@20128
   355
  let
wenzelm@21177
   356
    val cont_node = map_theory Theory.checkpoint node;
wenzelm@21177
   357
    val back_node = map_theory Theory.copy cont_node;
wenzelm@21958
   358
    fun state nd = State (nd, term);
wenzelm@20128
   359
    fun normal_state nd = (state nd, NONE);
wenzelm@20128
   360
    fun error_state nd exn = (state nd, SOME exn);
wenzelm@6689
   361
wenzelm@20128
   362
    val (result, err) =
wenzelm@20128
   363
      cont_node
wenzelm@20128
   364
      |> (f
wenzelm@23363
   365
          |> (if hist then History.apply' (History.current back_node) else History.map_current)
wenzelm@20128
   366
          |> controlled_execution)
wenzelm@20128
   367
      |> normal_state
wenzelm@20128
   368
      handle exn => error_state cont_node exn;
wenzelm@20128
   369
  in
wenzelm@20128
   370
    if is_stale result
wenzelm@20128
   371
    then return (error_state back_node (the_default stale_theory err))
wenzelm@20128
   372
    else return (result, err)
wenzelm@20128
   373
  end;
wenzelm@6689
   374
wenzelm@6689
   375
end;
wenzelm@6689
   376
wenzelm@6689
   377
wenzelm@6689
   378
(* primitive transitions *)
wenzelm@6689
   379
wenzelm@18563
   380
(*Note: Recovery from stale theories is provided only for theory-level
wenzelm@18589
   381
  operations via Transaction.  Other node or state operations should
wenzelm@18589
   382
  not touch theories at all.  Interrupts are enabled only for Keep and
wenzelm@18589
   383
  Transaction.*)
wenzelm@5828
   384
wenzelm@5828
   385
datatype trans =
wenzelm@22056
   386
  Init of (bool -> theory) * ((theory -> unit) * (theory -> unit)) |
wenzelm@21958
   387
                                                    (*init node; with exit/kill operation*)
wenzelm@25441
   388
  InitEmpty of (state -> bool) * (unit -> unit) |   (*init empty toplevel*)
wenzelm@21958
   389
  Exit |                                            (*conclude node -- deferred until init*)
wenzelm@21958
   390
  UndoExit |                                        (*continue after conclusion*)
wenzelm@21958
   391
  Kill |                                            (*abort node*)
wenzelm@21958
   392
  History of node History.T -> node History.T |     (*history operation (undo etc.)*)
wenzelm@21958
   393
  Keep of bool -> state -> unit |                   (*peek at state*)
wenzelm@21958
   394
  Transaction of bool * (bool -> node -> node);     (*node transaction*)
wenzelm@6689
   395
skalberg@15531
   396
fun undo_limit int = if int then NONE else SOME 0;
wenzelm@6689
   397
wenzelm@22056
   398
fun safe_exit (Toplevel (SOME (node, (exit, _)))) =
wenzelm@22056
   399
    (case try the_global_theory (History.current node) of
wenzelm@25219
   400
      SOME thy => controlled_execution exit thy
wenzelm@22056
   401
    | NONE => ())
wenzelm@22056
   402
  | safe_exit _ = ();
wenzelm@21958
   403
wenzelm@6689
   404
local
wenzelm@5828
   405
wenzelm@21958
   406
fun keep_state int f = controlled_execution (fn x => tap (f int) x);
wenzelm@21958
   407
wenzelm@23363
   408
fun apply_tr int _ (Init (f, term)) (state as Toplevel _) =
wenzelm@22056
   409
      let val node = Theory (Context.Theory (f int), NONE)
wenzelm@22056
   410
      in safe_exit state; State (History.init (undo_limit int) node, term) end
wenzelm@25441
   411
  | apply_tr int _ (InitEmpty (check, f)) (state as Toplevel _) =
wenzelm@25441
   412
      if check state then (safe_exit state; keep_state int (fn _ => fn _ => f ()) toplevel)
wenzelm@25441
   413
      else raise UNDEF
wenzelm@23363
   414
  | apply_tr _ _ Exit (State (node, term)) =
wenzelm@22056
   415
      (the_global_theory (History.current node); Toplevel (SOME (node, term)))
wenzelm@23363
   416
  | apply_tr _ _ UndoExit (Toplevel (SOME state_info)) = State state_info
wenzelm@23363
   417
  | apply_tr _ _ Kill (State (node, (_, kill))) =
wenzelm@22056
   418
      (kill (the_global_theory (History.current node)); toplevel)
wenzelm@23363
   419
  | apply_tr _ _ (History f) (State (node, term)) = State (f node, term)
wenzelm@23363
   420
  | apply_tr int _ (Keep f) state = keep_state int f state
wenzelm@23363
   421
  | apply_tr int pos (Transaction (hist, f)) (State state) =
wenzelm@23363
   422
      transaction hist pos (fn x => f int x) state
wenzelm@23363
   423
  | apply_tr _ _ _ _ = raise UNDEF;
wenzelm@5828
   424
wenzelm@23363
   425
fun apply_union _ _ [] state = raise FAILURE (state, UNDEF)
wenzelm@23363
   426
  | apply_union int pos (tr :: trs) state =
wenzelm@23363
   427
      apply_tr int pos tr state
wenzelm@23363
   428
        handle UNDEF => apply_union int pos trs state
wenzelm@23363
   429
          | FAILURE (alt_state, UNDEF) => apply_union int pos trs alt_state
wenzelm@6689
   430
          | exn as FAILURE _ => raise exn
wenzelm@6689
   431
          | exn => raise FAILURE (state, exn);
wenzelm@6689
   432
wenzelm@6689
   433
in
wenzelm@6689
   434
wenzelm@23363
   435
fun apply_trans int pos trs state = (apply_union int pos trs state, NONE)
skalberg@15531
   436
  handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
wenzelm@6689
   437
wenzelm@6689
   438
end;
wenzelm@5828
   439
wenzelm@5828
   440
wenzelm@5828
   441
(* datatype transition *)
wenzelm@5828
   442
wenzelm@5828
   443
datatype transition = Transition of
wenzelm@16815
   444
 {name: string,                        (*command name*)
wenzelm@16815
   445
  pos: Position.T,                     (*source position*)
wenzelm@16815
   446
  source: OuterLex.token list option,  (*source text*)
wenzelm@16815
   447
  int_only: bool,                      (*interactive-only*)
wenzelm@16815
   448
  print: string list,                  (*print modes (union)*)
wenzelm@16815
   449
  no_timing: bool,                     (*suppress timing*)
wenzelm@16815
   450
  trans: trans list};                  (*primitive transitions (union)*)
wenzelm@5828
   451
wenzelm@25960
   452
fun make_transition (name, pos, source, int_only, print, no_timing, trans) =
wenzelm@25960
   453
  Transition {name = name, pos = pos, source = source,
wenzelm@14923
   454
    int_only = int_only, print = print, no_timing = no_timing, trans = trans};
wenzelm@5828
   455
wenzelm@25960
   456
fun map_transition f (Transition {name, pos, source, int_only, print, no_timing, trans}) =
wenzelm@25960
   457
  make_transition (f (name, pos, source, int_only, print, no_timing, trans));
wenzelm@5828
   458
wenzelm@25960
   459
val empty = make_transition ("<unknown>", Position.none, NONE, false, [], false, []);
wenzelm@14923
   460
wenzelm@14923
   461
fun name_of (Transition {name, ...}) = name;
wenzelm@14923
   462
fun source_of (Transition {source, ...}) = source;
wenzelm@5828
   463
wenzelm@5828
   464
wenzelm@5828
   465
(* diagnostics *)
wenzelm@5828
   466
wenzelm@5828
   467
fun str_of_transition (Transition {name, pos, ...}) = quote name ^ Position.str_of pos;
wenzelm@5828
   468
wenzelm@5828
   469
fun command_msg msg tr = msg ^ "command " ^ str_of_transition tr;
wenzelm@5828
   470
fun at_command tr = command_msg "At " tr ^ ".";
wenzelm@5828
   471
wenzelm@5828
   472
fun type_error tr state =
wenzelm@18685
   473
  ERROR (command_msg "Illegal application of " tr ^ " " ^ str_of_state state);
wenzelm@5828
   474
wenzelm@5828
   475
wenzelm@5828
   476
(* modify transitions *)
wenzelm@5828
   477
wenzelm@25960
   478
fun name nm = map_transition (fn (_, pos, source, int_only, print, no_timing, trans) =>
wenzelm@25960
   479
  (nm, pos, source, int_only, print, no_timing, trans));
wenzelm@5828
   480
wenzelm@25960
   481
fun position pos = map_transition (fn (name, _, source, int_only, print, no_timing, trans) =>
wenzelm@25960
   482
  (name, pos, source, int_only, print, no_timing, trans));
wenzelm@9010
   483
wenzelm@25960
   484
fun source src = map_transition (fn (name, pos, _, int_only, print, no_timing, trans) =>
wenzelm@25960
   485
  (name, pos, SOME src, int_only, print, no_timing, trans));
wenzelm@5828
   486
wenzelm@25960
   487
fun interactive int_only = map_transition (fn (name, pos, source, _, print, no_timing, trans) =>
wenzelm@25960
   488
  (name, pos, source, int_only, print, no_timing, trans));
wenzelm@14923
   489
wenzelm@25960
   490
val no_timing = map_transition (fn (name, pos, source, int_only, print, _, trans) =>
wenzelm@25960
   491
  (name, pos, source, int_only, print, true, trans));
wenzelm@17363
   492
wenzelm@25960
   493
fun add_trans tr = map_transition (fn (name, pos, source, int_only, print, no_timing, trans) =>
wenzelm@25960
   494
  (name, pos, source, int_only, print, no_timing, trans @ [tr]));
wenzelm@17363
   495
wenzelm@25960
   496
fun print' mode = map_transition (fn (name, pos, source, int_only, print, no_timing, trans) =>
wenzelm@25960
   497
  (name, pos, source, int_only, insert (op =) mode print, no_timing, trans));
wenzelm@16607
   498
wenzelm@16607
   499
val print = print' "";
wenzelm@5828
   500
wenzelm@17363
   501
val three_buffersN = "three_buffers";
wenzelm@17363
   502
val print3 = print' three_buffersN;
wenzelm@5828
   503
wenzelm@5828
   504
wenzelm@21007
   505
(* basic transitions *)
wenzelm@5828
   506
wenzelm@22056
   507
fun init_theory f exit kill = add_trans (Init (f, (exit, kill)));
wenzelm@25441
   508
fun init_empty check f = add_trans (InitEmpty (check, f));
wenzelm@6689
   509
val exit = add_trans Exit;
wenzelm@21958
   510
val undo_exit = add_trans UndoExit;
wenzelm@6689
   511
val kill = add_trans Kill;
wenzelm@20128
   512
val history = add_trans o History;
wenzelm@7612
   513
val keep' = add_trans o Keep;
wenzelm@18592
   514
fun map_current f = add_trans (Transaction (false, f));
wenzelm@18592
   515
fun app_current f = add_trans (Transaction (true, f));
wenzelm@5828
   516
wenzelm@7612
   517
fun keep f = add_trans (Keep (fn _ => f));
wenzelm@5828
   518
fun imperative f = keep (fn _ => f ());
wenzelm@5828
   519
wenzelm@21007
   520
val unknown_theory = imperative (fn () => warning "Unknown theory context");
wenzelm@21007
   521
val unknown_proof = imperative (fn () => warning "Unknown proof context");
wenzelm@21007
   522
val unknown_context = imperative (fn () => warning "Unknown context");
wenzelm@15668
   523
wenzelm@21007
   524
wenzelm@21007
   525
(* theory transitions *)
wenzelm@15668
   526
wenzelm@20963
   527
fun theory' f = app_current (fn int =>
wenzelm@20963
   528
  (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (f int thy), NONE)
wenzelm@20963
   529
    | _ => raise UNDEF));
wenzelm@20963
   530
wenzelm@20963
   531
fun theory f = theory' (K f);
wenzelm@20963
   532
wenzelm@21294
   533
fun begin_local_theory begin f = app_current (fn _ =>
wenzelm@20963
   534
  (fn Theory (Context.Theory thy, _) =>
wenzelm@20963
   535
        let
wenzelm@20985
   536
          val lthy = f thy;
wenzelm@21294
   537
          val gthy = if begin then Context.Proof lthy else Context.Theory (loc_exit lthy);
wenzelm@21294
   538
        in Theory (gthy, SOME lthy) end
wenzelm@20963
   539
    | _ => raise UNDEF));
wenzelm@17076
   540
wenzelm@21294
   541
val end_local_theory = app_current (fn _ =>
wenzelm@21294
   542
  (fn Theory (Context.Proof lthy, _) => Theory (Context.Theory (loc_exit lthy), SOME lthy)
wenzelm@21007
   543
    | _ => raise UNDEF));
wenzelm@21007
   544
wenzelm@21007
   545
local
wenzelm@21007
   546
wenzelm@20963
   547
fun local_theory_presentation loc f g = app_current (fn int =>
wenzelm@21294
   548
  (fn Theory (gthy, _) =>
wenzelm@21294
   549
        let
wenzelm@21294
   550
          val finish = loc_finish loc gthy;
wenzelm@25960
   551
          val lthy' = f (loc_begin loc gthy);
wenzelm@21294
   552
        in Theory (finish lthy', SOME lthy') end
wenzelm@20963
   553
    | _ => raise UNDEF) #> tap (g int));
wenzelm@15668
   554
wenzelm@21007
   555
in
wenzelm@21007
   556
wenzelm@20963
   557
fun local_theory loc f = local_theory_presentation loc f (K I);
wenzelm@20963
   558
fun present_local_theory loc g = local_theory_presentation loc I g;
wenzelm@18955
   559
wenzelm@21007
   560
end;
wenzelm@21007
   561
wenzelm@21007
   562
wenzelm@21007
   563
(* proof transitions *)
wenzelm@21007
   564
wenzelm@21007
   565
fun end_proof f = map_current (fn int =>
wenzelm@24795
   566
  (fn Proof (prf, (finish, _)) =>
wenzelm@21007
   567
        let val state = ProofHistory.current prf in
wenzelm@21007
   568
          if can (Proof.assert_bottom true) state then
wenzelm@21007
   569
            let
wenzelm@21007
   570
              val ctxt' = f int state;
wenzelm@21007
   571
              val gthy' = finish ctxt';
wenzelm@21007
   572
            in Theory (gthy', SOME ctxt') end
wenzelm@21007
   573
          else raise UNDEF
wenzelm@21007
   574
        end
wenzelm@21007
   575
    | SkipProof (h, (gthy, _)) =>
wenzelm@21007
   576
        if History.current h = 0 then Theory (gthy, NONE) else raise UNDEF
wenzelm@21007
   577
    | _ => raise UNDEF));
wenzelm@21007
   578
wenzelm@21294
   579
local
wenzelm@21294
   580
wenzelm@21294
   581
fun begin_proof init finish = app_current (fn int =>
wenzelm@21294
   582
  (fn Theory (gthy, _) =>
wenzelm@21294
   583
    let
berghofe@24453
   584
      val prf = init int gthy;
wenzelm@21294
   585
      val schematic = Proof.schematic_goal prf;
wenzelm@21294
   586
    in
wenzelm@21294
   587
      if ! skip_proofs andalso schematic then
wenzelm@21294
   588
        warning "Cannot skip proof of schematic goal statement"
wenzelm@21294
   589
      else ();
wenzelm@21294
   590
      if ! skip_proofs andalso not schematic then
wenzelm@21294
   591
        SkipProof
wenzelm@21294
   592
          (History.init (undo_limit int) 0, (finish gthy (Proof.global_skip_proof int prf), gthy))
wenzelm@21294
   593
      else Proof (ProofHistory.init (undo_limit int) prf, (finish gthy, gthy))
wenzelm@21294
   594
    end
wenzelm@21294
   595
  | _ => raise UNDEF));
wenzelm@21294
   596
wenzelm@21294
   597
in
wenzelm@21294
   598
wenzelm@24780
   599
fun local_theory_to_proof' loc f = begin_proof
wenzelm@25960
   600
  (fn int => fn gthy => f int (loc_begin loc gthy))
wenzelm@24780
   601
  (loc_finish loc);
wenzelm@24780
   602
berghofe@24453
   603
fun local_theory_to_proof loc f = local_theory_to_proof' loc (K f);
wenzelm@21294
   604
wenzelm@21294
   605
fun theory_to_proof f = begin_proof
wenzelm@24780
   606
  (K (fn Context.Theory thy => f thy | _ => raise UNDEF))
wenzelm@24780
   607
  (K (Context.Theory o ProofContext.theory_of));
wenzelm@21294
   608
wenzelm@21294
   609
end;
wenzelm@21294
   610
wenzelm@21007
   611
val forget_proof = map_current (fn _ =>
wenzelm@21007
   612
  (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   613
    | SkipProof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   614
    | _ => raise UNDEF));
wenzelm@21007
   615
wenzelm@21177
   616
fun present_proof f = map_current (fn int =>
wenzelm@21177
   617
  (fn Proof (prf, x) => Proof (ProofHistory.apply I prf, x)
wenzelm@21177
   618
    | SkipProof (h, x) => SkipProof (History.apply I h, x)
wenzelm@21177
   619
    | _ => raise UNDEF) #> tap (f int));
wenzelm@21177
   620
wenzelm@17904
   621
fun proofs' f = map_current (fn int =>
wenzelm@21007
   622
  (fn Proof (prf, x) => Proof (ProofHistory.applys (f int) prf, x)
wenzelm@21007
   623
    | SkipProof (h, x) => SkipProof (History.apply I h, x)
wenzelm@16815
   624
    | _ => raise UNDEF));
wenzelm@15668
   625
wenzelm@17904
   626
fun proof' f = proofs' (Seq.single oo f);
wenzelm@17904
   627
val proofs = proofs' o K;
wenzelm@6689
   628
val proof = proof' o K;
wenzelm@16815
   629
wenzelm@16815
   630
fun actual_proof f = map_current (fn _ =>
wenzelm@21007
   631
  (fn Proof (prf, x) => Proof (f prf, x)
wenzelm@20963
   632
    | _ => raise UNDEF));
wenzelm@16815
   633
wenzelm@16815
   634
fun skip_proof f = map_current (fn _ =>
wenzelm@21007
   635
  (fn SkipProof (h, x) => SkipProof (f h, x)
wenzelm@18563
   636
    | _ => raise UNDEF));
wenzelm@18563
   637
wenzelm@16815
   638
fun skip_proof_to_theory p = map_current (fn _ =>
wenzelm@21007
   639
  (fn SkipProof (h, (gthy, _)) =>
wenzelm@21007
   640
    if p (History.current h) then Theory (gthy, NONE)
wenzelm@17904
   641
    else raise UNDEF
wenzelm@17904
   642
  | _ => raise UNDEF));
wenzelm@5828
   643
wenzelm@5828
   644
wenzelm@5828
   645
wenzelm@5828
   646
(** toplevel transactions **)
wenzelm@5828
   647
wenzelm@25960
   648
(* thread position *)
wenzelm@25799
   649
wenzelm@25960
   650
fun setmp_thread_position (Transition {pos, ...}) f x =
wenzelm@25819
   651
  Position.setmp_thread_data pos f x;
wenzelm@25799
   652
wenzelm@25799
   653
wenzelm@5828
   654
(* apply transitions *)
wenzelm@5828
   655
wenzelm@6664
   656
local
wenzelm@6664
   657
wenzelm@25799
   658
fun app int (tr as Transition {trans, pos, int_only, print, no_timing, ...}) =
wenzelm@25819
   659
  setmp_thread_position tr (fn state =>
wenzelm@25799
   660
    let
wenzelm@25799
   661
      val _ =
wenzelm@25799
   662
        if not int andalso int_only then warning (command_msg "Interactive-only " tr)
wenzelm@25799
   663
        else ();
wenzelm@16682
   664
wenzelm@25799
   665
      fun do_timing f x = (warning (command_msg "" tr); timeap f x);
wenzelm@25799
   666
      fun do_profiling f x = profile (! profiling) f x;
wenzelm@25799
   667
wenzelm@26256
   668
      val (result, status) =
wenzelm@25799
   669
         state |> (apply_trans int pos trans
wenzelm@25799
   670
          |> (if ! profiling > 0 andalso not no_timing then do_profiling else I)
wenzelm@25799
   671
          |> (if ! profiling > 0 orelse ! timing andalso not no_timing then do_timing else I));
wenzelm@25799
   672
      val _ =
wenzelm@25799
   673
        if int andalso not (! quiet) andalso exists (member (op =) print) ("" :: print_mode_value ())
wenzelm@25799
   674
        then print_state false result else ();
wenzelm@26256
   675
wenzelm@26256
   676
    in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) status) end);
wenzelm@6664
   677
wenzelm@6664
   678
in
wenzelm@5828
   679
wenzelm@6689
   680
fun apply int tr st =
wenzelm@26256
   681
  let val ctxt = try context_of st in
wenzelm@26256
   682
    (case app int tr st of
wenzelm@26256
   683
      (_, SOME TERMINATE) => NONE
wenzelm@26256
   684
    | (_, SOME RESTART) => SOME (toplevel, NONE)
wenzelm@26256
   685
    | (state', SOME (EXCURSION_FAIL exn_info)) => SOME (state', SOME exn_info)
wenzelm@26256
   686
    | (state', SOME exn) => SOME (state', SOME (exn_context ctxt exn, at_command tr))
wenzelm@26256
   687
    | (state', NONE) => SOME (state', NONE))
wenzelm@26256
   688
  end;
wenzelm@6664
   689
wenzelm@6664
   690
end;
wenzelm@5828
   691
wenzelm@5828
   692
wenzelm@17076
   693
(* excursion: toplevel -- apply transformers/presentation -- toplevel *)
wenzelm@5828
   694
wenzelm@6664
   695
local
wenzelm@6664
   696
wenzelm@5828
   697
fun excur [] x = x
wenzelm@17076
   698
  | excur ((tr, pr) :: trs) (st, res) =
wenzelm@17321
   699
      (case apply (! interact) tr st of
skalberg@15531
   700
        SOME (st', NONE) =>
wenzelm@18685
   701
          excur trs (st', pr st st' res handle exn =>
wenzelm@10324
   702
            raise EXCURSION_FAIL (exn, "Presentation failed\n" ^ at_command tr))
skalberg@15531
   703
      | SOME (st', SOME exn_info) => raise EXCURSION_FAIL exn_info
skalberg@15531
   704
      | NONE => raise EXCURSION_FAIL (TERMINATE, at_command tr));
wenzelm@5828
   705
wenzelm@17076
   706
fun no_pr _ _ _ = ();
wenzelm@17076
   707
wenzelm@6664
   708
in
wenzelm@6664
   709
wenzelm@17076
   710
fun present_excursion trs res =
wenzelm@21958
   711
  (case excur trs (toplevel, res) of
wenzelm@22056
   712
    (state as Toplevel _, res') => (safe_exit state; res')
wenzelm@18685
   713
  | _ => error "Unfinished development at end of input")
wenzelm@9134
   714
  handle exn => error (exn_message exn);
wenzelm@9134
   715
wenzelm@17076
   716
fun excursion trs = present_excursion (map (rpair no_pr) trs) ();
wenzelm@7062
   717
wenzelm@6664
   718
end;
wenzelm@6664
   719
wenzelm@5828
   720
wenzelm@5828
   721
wenzelm@5828
   722
(** interactive transformations **)
wenzelm@5828
   723
wenzelm@5828
   724
(* the global state reference *)
wenzelm@5828
   725
skalberg@15531
   726
val global_state = ref (toplevel, NONE: (exn * string) option);
wenzelm@5828
   727
skalberg@15531
   728
fun set_state state = global_state := (state, NONE);
wenzelm@5828
   729
fun get_state () = fst (! global_state);
wenzelm@5828
   730
fun exn () = snd (! global_state);
wenzelm@5828
   731
wenzelm@5828
   732
wenzelm@24295
   733
(* apply transformers to global state --- NOT THREAD-SAFE! *)
wenzelm@5828
   734
wenzelm@14985
   735
nonfix >> >>>;
wenzelm@5828
   736
wenzelm@24295
   737
fun >> tr =
wenzelm@5828
   738
  (case apply true tr (get_state ()) of
skalberg@15531
   739
    NONE => false
skalberg@15531
   740
  | SOME (state', exn_info) =>
wenzelm@5828
   741
      (global_state := (state', exn_info);
wenzelm@25809
   742
        (case exn_info of
wenzelm@25809
   743
          NONE => ()
wenzelm@25819
   744
        | SOME err => setmp_thread_position tr print_exn err);
wenzelm@24295
   745
        true));
wenzelm@5828
   746
wenzelm@14985
   747
fun >>> [] = ()
wenzelm@14985
   748
  | >>> (tr :: trs) = if >> tr then >>> trs else ();
wenzelm@14985
   749
wenzelm@25441
   750
fun init_state () = (>> (init_empty (K true) (K ()) empty); ());
wenzelm@21958
   751
wenzelm@21958
   752
wenzelm@21958
   753
(* the Isar source of transitions *)
wenzelm@21958
   754
wenzelm@21958
   755
type 'a isar =
wenzelm@21958
   756
  (transition, (transition option,
wenzelm@21958
   757
    (OuterLex.token, (OuterLex.token option, (OuterLex.token, (OuterLex.token,
wenzelm@21958
   758
      Position.T * (Symbol.symbol, (string, 'a) Source.source) Source.source)
wenzelm@21958
   759
          Source.source) Source.source) Source.source) Source.source) Source.source) Source.source;
wenzelm@21958
   760
wenzelm@25527
   761
local
wenzelm@25527
   762
wenzelm@17513
   763
(*Spurious interrupts ahead!  Race condition?*)
wenzelm@17513
   764
fun get_interrupt src = SOME (Source.get_single src) handle Interrupt => NONE;
wenzelm@7602
   765
wenzelm@25527
   766
fun raw_loop secure src =
wenzelm@25527
   767
  let
wenzelm@25527
   768
    fun check_secure () =
wenzelm@25527
   769
      (if secure then warning "Secure loop -- cannot exit to ML" else (); secure);
wenzelm@25527
   770
  in
wenzelm@25847
   771
    (case get_interrupt (Source.set_prompt Source.default_prompt src) of
wenzelm@25527
   772
      NONE => (writeln "\nInterrupt."; raw_loop secure src)
wenzelm@25527
   773
    | SOME NONE => if secure then quit () else ()
wenzelm@25527
   774
    | SOME (SOME (tr, src')) => if >> tr orelse check_secure () then raw_loop secure src' else ())
wenzelm@25584
   775
    handle exn => (CRITICAL (fn () => change crashes (cons exn));
wenzelm@25584
   776
      warning "Recovering after Isar toplevel crash -- see also Toplevel.crashes";
wenzelm@25584
   777
      raw_loop secure src)
wenzelm@23640
   778
  end;
wenzelm@5828
   779
wenzelm@25527
   780
in
wenzelm@25527
   781
wenzelm@26081
   782
fun loop secure src = uninterruptible (fn _ => raw_loop secure) src;
wenzelm@5828
   783
wenzelm@5828
   784
end;
wenzelm@25527
   785
wenzelm@25527
   786
end;