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