src/Pure/Isar/toplevel.ML
author wenzelm
Thu Jan 03 22:25:15 2008 +0100 (2008-01-03)
changeset 25819 e6feb08b7f4b
parent 25809 decb98ff92dd
child 25847 713519ba6860
permissions -rw-r--r--
replaced thread_properties by simplified version in position.ML;
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@24055
    42
  exception TOPLEVEL_ERROR
wenzelm@20128
    43
  val exn_message: exn -> string
wenzelm@20128
    44
  val program: (unit -> 'a) -> 'a
wenzelm@16682
    45
  type transition
wenzelm@6689
    46
  val undo_limit: bool -> int option
wenzelm@5828
    47
  val empty: transition
wenzelm@14923
    48
  val name_of: transition -> string
wenzelm@25796
    49
  val properties_of: transition -> Markup.property list
wenzelm@14923
    50
  val source_of: transition -> OuterLex.token list option
wenzelm@5828
    51
  val name: string -> transition -> transition
wenzelm@25796
    52
  val properties: Markup.property list -> transition -> transition
wenzelm@5828
    53
  val position: Position.T -> transition -> transition
wenzelm@14923
    54
  val source: OuterLex.token list -> transition -> transition
wenzelm@5828
    55
  val interactive: bool -> transition -> transition
wenzelm@5828
    56
  val print: transition -> transition
wenzelm@16607
    57
  val print': string -> transition -> transition
wenzelm@17363
    58
  val three_buffersN: string
wenzelm@17363
    59
  val print3: transition -> transition
wenzelm@9010
    60
  val no_timing: transition -> transition
wenzelm@22056
    61
  val init_theory: (bool -> theory) -> (theory -> unit) -> (theory -> unit) ->
wenzelm@22056
    62
    transition -> transition
wenzelm@25441
    63
  val init_empty: (state -> bool) -> (unit -> unit) -> transition -> transition
wenzelm@6689
    64
  val exit: transition -> transition
wenzelm@21958
    65
  val undo_exit: transition -> transition
wenzelm@6689
    66
  val kill: transition -> transition
wenzelm@20128
    67
  val history: (node History.T -> node History.T) -> transition -> transition
wenzelm@5828
    68
  val keep: (state -> unit) -> transition -> transition
wenzelm@7612
    69
  val keep': (bool -> state -> unit) -> transition -> transition
wenzelm@5828
    70
  val imperative: (unit -> unit) -> transition -> transition
wenzelm@5828
    71
  val theory: (theory -> 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@20128
   254
local
wenzelm@20128
   255
wenzelm@20128
   256
fun with_context f xs =
wenzelm@22095
   257
  (case ML_Context.get_context () of NONE => []
wenzelm@22089
   258
  | SOME context => map (f (Context.proof_of context)) xs);
wenzelm@20128
   259
wenzelm@20128
   260
fun raised name [] = "exception " ^ name ^ " raised"
wenzelm@20128
   261
  | raised name [msg] = "exception " ^ name ^ " raised: " ^ msg
wenzelm@20128
   262
  | raised name msgs = cat_lines (("exception " ^ name ^ " raised:") :: msgs);
wenzelm@20128
   263
wenzelm@20128
   264
fun exn_msg _ TERMINATE = "Exit."
wenzelm@20128
   265
  | exn_msg _ RESTART = "Restart."
wenzelm@20128
   266
  | exn_msg _ Interrupt = "Interrupt."
wenzelm@24055
   267
  | exn_msg _ TOPLEVEL_ERROR = "Error."
wenzelm@20128
   268
  | exn_msg _ (SYS_ERROR msg) = "## SYSTEM ERROR ##\n" ^ msg
wenzelm@20128
   269
  | exn_msg _ (ERROR msg) = msg
wenzelm@23975
   270
  | exn_msg detailed (Exn.EXCEPTIONS (exns, "")) = cat_lines (map (exn_msg detailed) exns)
wenzelm@23963
   271
  | exn_msg detailed (Exn.EXCEPTIONS (exns, msg)) = cat_lines (map (exn_msg detailed) exns @ [msg])
wenzelm@20128
   272
  | exn_msg detailed (EXCURSION_FAIL (exn, msg)) = cat_lines [exn_msg detailed exn, msg]
wenzelm@20128
   273
  | exn_msg false (THEORY (msg, _)) = msg
wenzelm@20128
   274
  | exn_msg true (THEORY (msg, thys)) = raised "THEORY" (msg :: map Context.str_of_thy thys)
wenzelm@20128
   275
  | exn_msg false (Syntax.AST (msg, _)) = raised "AST" [msg]
wenzelm@20128
   276
  | exn_msg true (Syntax.AST (msg, asts)) =
wenzelm@20128
   277
      raised "AST" (msg :: map (Pretty.string_of o Syntax.pretty_ast) asts)
wenzelm@20128
   278
  | exn_msg false (TYPE (msg, _, _)) = raised "TYPE" [msg]
wenzelm@20128
   279
  | exn_msg true (TYPE (msg, Ts, ts)) = raised "TYPE" (msg ::
wenzelm@24920
   280
        with_context Syntax.string_of_typ Ts @ with_context Syntax.string_of_term ts)
wenzelm@20128
   281
  | exn_msg false (TERM (msg, _)) = raised "TERM" [msg]
wenzelm@22089
   282
  | exn_msg true (TERM (msg, ts)) =
wenzelm@24920
   283
      raised "TERM" (msg :: with_context Syntax.string_of_term ts)
wenzelm@20128
   284
  | exn_msg false (THM (msg, _, _)) = raised "THM" [msg]
wenzelm@20128
   285
  | exn_msg true (THM (msg, i, thms)) =
wenzelm@22089
   286
      raised ("THM " ^ string_of_int i) (msg :: with_context ProofContext.string_of_thm thms)
wenzelm@20128
   287
  | exn_msg _ Option.Option = raised "Option" []
wenzelm@20128
   288
  | exn_msg _ Library.UnequalLengths = raised "UnequalLengths" []
wenzelm@20128
   289
  | exn_msg _ Empty = raised "Empty" []
wenzelm@20128
   290
  | exn_msg _ Subscript = raised "Subscript" []
wenzelm@20128
   291
  | exn_msg _ (Fail msg) = raised "Fail" [msg]
wenzelm@23940
   292
  | exn_msg _ exn = General.exnMessage exn;
wenzelm@20128
   293
wenzelm@20128
   294
in
wenzelm@20128
   295
wenzelm@20128
   296
fun exn_message exn = exn_msg (! debug) exn;
wenzelm@25809
   297
fun print_exn (exn, s) = Output.error_msg (cat_lines [exn_message exn, s]);
wenzelm@20128
   298
wenzelm@20128
   299
end;
wenzelm@20128
   300
wenzelm@20128
   301
wenzelm@20128
   302
(* controlled execution *)
wenzelm@20128
   303
wenzelm@20128
   304
local
wenzelm@20128
   305
wenzelm@18685
   306
fun debugging f x =
wenzelm@23940
   307
  if ! debug then exception_trace (fn () => f x)
wenzelm@18685
   308
  else f x;
wenzelm@18685
   309
wenzelm@20128
   310
fun interruptible f x =
wenzelm@25219
   311
  let val y = ref NONE
wenzelm@25219
   312
  in raise_interrupt (fn () => y := SOME (f x)) (); the (! y) end;
wenzelm@20128
   313
wenzelm@24055
   314
fun toplevel_error f x = f x
wenzelm@24055
   315
  handle exn => (Output.error_msg (exn_message exn); raise TOPLEVEL_ERROR);
wenzelm@24055
   316
wenzelm@20128
   317
in
wenzelm@20128
   318
wenzelm@20128
   319
fun controlled_execution f =
wenzelm@20128
   320
  f
wenzelm@20128
   321
  |> debugging
wenzelm@24055
   322
  |> interruptible;
wenzelm@20128
   323
wenzelm@20128
   324
fun program f =
wenzelm@24055
   325
 (f
wenzelm@24055
   326
  |> debugging
wenzelm@24055
   327
  |> toplevel_error) ();
wenzelm@20128
   328
wenzelm@20128
   329
end;
wenzelm@20128
   330
wenzelm@5828
   331
wenzelm@16815
   332
(* node transactions and recovery from stale theories *)
wenzelm@6689
   333
wenzelm@16815
   334
(*NB: proof commands should be non-destructive!*)
wenzelm@7022
   335
wenzelm@6689
   336
local
wenzelm@6689
   337
wenzelm@16452
   338
fun is_stale state = Context.is_stale (theory_of state) handle UNDEF => false;
wenzelm@6689
   339
wenzelm@18685
   340
val stale_theory = ERROR "Stale theory encountered after succesful execution!";
wenzelm@16815
   341
wenzelm@23363
   342
fun map_theory f = History.map_current
wenzelm@21177
   343
  (fn Theory (gthy, _) => Theory (Context.mapping f (LocalTheory.raw_theory f) gthy, NONE)
wenzelm@21177
   344
    | node => node);
wenzelm@6689
   345
wenzelm@23363
   346
fun context_position pos = History.map_current
wenzelm@24795
   347
  (fn Theory (gthy, ctxt) => Theory (ContextPosition.put pos gthy, ctxt)
wenzelm@23363
   348
    | Proof (prf, x) =>
wenzelm@24795
   349
        Proof (ProofHistory.map_current (Proof.map_context (ContextPosition.put_ctxt pos)) prf, x)
wenzelm@23363
   350
    | node => node);
wenzelm@23363
   351
skalberg@15531
   352
fun return (result, NONE) = result
skalberg@15531
   353
  | return (result, SOME exn) = raise FAILURE (result, exn);
wenzelm@7022
   354
wenzelm@6689
   355
in
wenzelm@6689
   356
wenzelm@23363
   357
fun transaction hist pos f (node, term) =
wenzelm@20128
   358
  let
wenzelm@21177
   359
    val cont_node = map_theory Theory.checkpoint node;
wenzelm@21177
   360
    val back_node = map_theory Theory.copy cont_node;
wenzelm@21958
   361
    fun state nd = State (nd, term);
wenzelm@20128
   362
    fun normal_state nd = (state nd, NONE);
wenzelm@20128
   363
    fun error_state nd exn = (state nd, SOME exn);
wenzelm@6689
   364
wenzelm@20128
   365
    val (result, err) =
wenzelm@20128
   366
      cont_node
wenzelm@23363
   367
      |> context_position pos
wenzelm@24795
   368
      |> map_theory Theory.checkpoint
wenzelm@20128
   369
      |> (f
wenzelm@23363
   370
          |> (if hist then History.apply' (History.current back_node) else History.map_current)
wenzelm@20128
   371
          |> controlled_execution)
wenzelm@23363
   372
      |> context_position Position.none
wenzelm@20128
   373
      |> normal_state
wenzelm@20128
   374
      handle exn => error_state cont_node exn;
wenzelm@20128
   375
  in
wenzelm@20128
   376
    if is_stale result
wenzelm@20128
   377
    then return (error_state back_node (the_default stale_theory err))
wenzelm@20128
   378
    else return (result, err)
wenzelm@20128
   379
  end;
wenzelm@6689
   380
wenzelm@6689
   381
end;
wenzelm@6689
   382
wenzelm@6689
   383
wenzelm@6689
   384
(* primitive transitions *)
wenzelm@6689
   385
wenzelm@18563
   386
(*Note: Recovery from stale theories is provided only for theory-level
wenzelm@18589
   387
  operations via Transaction.  Other node or state operations should
wenzelm@18589
   388
  not touch theories at all.  Interrupts are enabled only for Keep and
wenzelm@18589
   389
  Transaction.*)
wenzelm@5828
   390
wenzelm@5828
   391
datatype trans =
wenzelm@22056
   392
  Init of (bool -> theory) * ((theory -> unit) * (theory -> unit)) |
wenzelm@21958
   393
                                                    (*init node; with exit/kill operation*)
wenzelm@25441
   394
  InitEmpty of (state -> bool) * (unit -> unit) |   (*init empty toplevel*)
wenzelm@21958
   395
  Exit |                                            (*conclude node -- deferred until init*)
wenzelm@21958
   396
  UndoExit |                                        (*continue after conclusion*)
wenzelm@21958
   397
  Kill |                                            (*abort node*)
wenzelm@21958
   398
  History of node History.T -> node History.T |     (*history operation (undo etc.)*)
wenzelm@21958
   399
  Keep of bool -> state -> unit |                   (*peek at state*)
wenzelm@21958
   400
  Transaction of bool * (bool -> node -> node);     (*node transaction*)
wenzelm@6689
   401
skalberg@15531
   402
fun undo_limit int = if int then NONE else SOME 0;
wenzelm@6689
   403
wenzelm@22056
   404
fun safe_exit (Toplevel (SOME (node, (exit, _)))) =
wenzelm@22056
   405
    (case try the_global_theory (History.current node) of
wenzelm@25219
   406
      SOME thy => controlled_execution exit thy
wenzelm@22056
   407
    | NONE => ())
wenzelm@22056
   408
  | safe_exit _ = ();
wenzelm@21958
   409
wenzelm@6689
   410
local
wenzelm@5828
   411
wenzelm@21958
   412
fun keep_state int f = controlled_execution (fn x => tap (f int) x);
wenzelm@21958
   413
wenzelm@23363
   414
fun apply_tr int _ (Init (f, term)) (state as Toplevel _) =
wenzelm@22056
   415
      let val node = Theory (Context.Theory (f int), NONE)
wenzelm@22056
   416
      in safe_exit state; State (History.init (undo_limit int) node, term) end
wenzelm@25441
   417
  | apply_tr int _ (InitEmpty (check, f)) (state as Toplevel _) =
wenzelm@25441
   418
      if check state then (safe_exit state; keep_state int (fn _ => fn _ => f ()) toplevel)
wenzelm@25441
   419
      else raise UNDEF
wenzelm@23363
   420
  | apply_tr _ _ Exit (State (node, term)) =
wenzelm@22056
   421
      (the_global_theory (History.current node); Toplevel (SOME (node, term)))
wenzelm@23363
   422
  | apply_tr _ _ UndoExit (Toplevel (SOME state_info)) = State state_info
wenzelm@23363
   423
  | apply_tr _ _ Kill (State (node, (_, kill))) =
wenzelm@22056
   424
      (kill (the_global_theory (History.current node)); toplevel)
wenzelm@23363
   425
  | apply_tr _ _ (History f) (State (node, term)) = State (f node, term)
wenzelm@23363
   426
  | apply_tr int _ (Keep f) state = keep_state int f state
wenzelm@23363
   427
  | apply_tr int pos (Transaction (hist, f)) (State state) =
wenzelm@23363
   428
      transaction hist pos (fn x => f int x) state
wenzelm@23363
   429
  | apply_tr _ _ _ _ = raise UNDEF;
wenzelm@5828
   430
wenzelm@23363
   431
fun apply_union _ _ [] state = raise FAILURE (state, UNDEF)
wenzelm@23363
   432
  | apply_union int pos (tr :: trs) state =
wenzelm@23363
   433
      apply_tr int pos tr state
wenzelm@23363
   434
        handle UNDEF => apply_union int pos trs state
wenzelm@23363
   435
          | FAILURE (alt_state, UNDEF) => apply_union int pos trs alt_state
wenzelm@6689
   436
          | exn as FAILURE _ => raise exn
wenzelm@6689
   437
          | exn => raise FAILURE (state, exn);
wenzelm@6689
   438
wenzelm@6689
   439
in
wenzelm@6689
   440
wenzelm@23363
   441
fun apply_trans int pos trs state = (apply_union int pos trs state, NONE)
skalberg@15531
   442
  handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
wenzelm@6689
   443
wenzelm@6689
   444
end;
wenzelm@5828
   445
wenzelm@5828
   446
wenzelm@5828
   447
(* datatype transition *)
wenzelm@5828
   448
wenzelm@5828
   449
datatype transition = Transition of
wenzelm@16815
   450
 {name: string,                        (*command name*)
wenzelm@25796
   451
  props: Markup.property list,         (*properties*)
wenzelm@16815
   452
  pos: Position.T,                     (*source position*)
wenzelm@16815
   453
  source: OuterLex.token list option,  (*source text*)
wenzelm@16815
   454
  int_only: bool,                      (*interactive-only*)
wenzelm@16815
   455
  print: string list,                  (*print modes (union)*)
wenzelm@16815
   456
  no_timing: bool,                     (*suppress timing*)
wenzelm@16815
   457
  trans: trans list};                  (*primitive transitions (union)*)
wenzelm@5828
   458
wenzelm@25796
   459
fun make_transition (name, props, pos, source, int_only, print, no_timing, trans) =
wenzelm@25796
   460
  Transition {name = name, props = props, pos = pos, source = source,
wenzelm@14923
   461
    int_only = int_only, print = print, no_timing = no_timing, trans = trans};
wenzelm@5828
   462
wenzelm@25796
   463
fun map_transition f (Transition {name, props, pos, source, int_only, print, no_timing, trans}) =
wenzelm@25796
   464
  make_transition (f (name, props, pos, source, int_only, print, no_timing, trans));
wenzelm@5828
   465
wenzelm@25796
   466
val empty = make_transition ("<unknown>", [], Position.none, NONE, false, [], false, []);
wenzelm@14923
   467
wenzelm@14923
   468
fun name_of (Transition {name, ...}) = name;
wenzelm@25796
   469
fun properties_of (Transition {props, ...}) = props;
wenzelm@14923
   470
fun source_of (Transition {source, ...}) = source;
wenzelm@5828
   471
wenzelm@5828
   472
wenzelm@5828
   473
(* diagnostics *)
wenzelm@5828
   474
wenzelm@5828
   475
fun str_of_transition (Transition {name, pos, ...}) = quote name ^ Position.str_of pos;
wenzelm@5828
   476
wenzelm@5828
   477
fun command_msg msg tr = msg ^ "command " ^ str_of_transition tr;
wenzelm@5828
   478
fun at_command tr = command_msg "At " tr ^ ".";
wenzelm@5828
   479
wenzelm@5828
   480
fun type_error tr state =
wenzelm@18685
   481
  ERROR (command_msg "Illegal application of " tr ^ " " ^ str_of_state state);
wenzelm@5828
   482
wenzelm@5828
   483
wenzelm@5828
   484
(* modify transitions *)
wenzelm@5828
   485
wenzelm@25796
   486
fun name nm =
wenzelm@25796
   487
  map_transition (fn (_, props, pos, source, int_only, print, no_timing, trans) =>
wenzelm@25796
   488
    (nm, props, pos, source, int_only, print, no_timing, trans));
wenzelm@25796
   489
wenzelm@25796
   490
fun properties props =
wenzelm@25796
   491
  map_transition (fn (name, _, pos, source, int_only, print, no_timing, trans) =>
wenzelm@25796
   492
    (name, props, pos, source, int_only, print, no_timing, trans));
wenzelm@5828
   493
wenzelm@25796
   494
fun position pos =
wenzelm@25796
   495
  map_transition (fn (name, props, _, source, int_only, print, no_timing, trans) =>
wenzelm@25796
   496
    (name, props, pos, source, int_only, print, no_timing, trans));
wenzelm@9010
   497
wenzelm@25796
   498
fun source src =
wenzelm@25796
   499
  map_transition (fn (name, props, pos, _, int_only, print, no_timing, trans) =>
wenzelm@25796
   500
    (name, props, pos, SOME src, int_only, print, no_timing, trans));
wenzelm@5828
   501
wenzelm@25796
   502
fun interactive int_only =
wenzelm@25796
   503
  map_transition (fn (name, props, pos, source, _, print, no_timing, trans) =>
wenzelm@25796
   504
    (name, props, pos, source, int_only, print, no_timing, trans));
wenzelm@14923
   505
wenzelm@25796
   506
val no_timing =
wenzelm@25796
   507
  map_transition (fn (name, props, pos, source, int_only, print, _, trans) =>
wenzelm@25796
   508
    (name, props, pos, source, int_only, print, true, trans));
wenzelm@17363
   509
wenzelm@25796
   510
fun add_trans tr =
wenzelm@25796
   511
  map_transition (fn (name, props, pos, source, int_only, print, no_timing, trans) =>
wenzelm@25796
   512
    (name, props, pos, source, int_only, print, no_timing, trans @ [tr]));
wenzelm@17363
   513
wenzelm@25796
   514
fun print' mode =
wenzelm@25796
   515
  map_transition (fn (name, props, pos, source, int_only, print, no_timing, trans) =>
wenzelm@25796
   516
    (name, props, pos, source, int_only, insert (op =) mode print, no_timing, trans));
wenzelm@16607
   517
wenzelm@16607
   518
val print = print' "";
wenzelm@5828
   519
wenzelm@17363
   520
val three_buffersN = "three_buffers";
wenzelm@17363
   521
val print3 = print' three_buffersN;
wenzelm@5828
   522
wenzelm@5828
   523
wenzelm@21007
   524
(* basic transitions *)
wenzelm@5828
   525
wenzelm@22056
   526
fun init_theory f exit kill = add_trans (Init (f, (exit, kill)));
wenzelm@25441
   527
fun init_empty check f = add_trans (InitEmpty (check, f));
wenzelm@6689
   528
val exit = add_trans Exit;
wenzelm@21958
   529
val undo_exit = add_trans UndoExit;
wenzelm@6689
   530
val kill = add_trans Kill;
wenzelm@20128
   531
val history = add_trans o History;
wenzelm@7612
   532
val keep' = add_trans o Keep;
wenzelm@18592
   533
fun map_current f = add_trans (Transaction (false, f));
wenzelm@18592
   534
fun app_current f = add_trans (Transaction (true, f));
wenzelm@5828
   535
wenzelm@7612
   536
fun keep f = add_trans (Keep (fn _ => f));
wenzelm@5828
   537
fun imperative f = keep (fn _ => f ());
wenzelm@5828
   538
wenzelm@21007
   539
val unknown_theory = imperative (fn () => warning "Unknown theory context");
wenzelm@21007
   540
val unknown_proof = imperative (fn () => warning "Unknown proof context");
wenzelm@21007
   541
val unknown_context = imperative (fn () => warning "Unknown context");
wenzelm@15668
   542
wenzelm@21007
   543
wenzelm@21007
   544
(* theory transitions *)
wenzelm@15668
   545
wenzelm@20963
   546
fun theory' f = app_current (fn int =>
wenzelm@20963
   547
  (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (f int thy), NONE)
wenzelm@20963
   548
    | _ => raise UNDEF));
wenzelm@20963
   549
wenzelm@20963
   550
fun theory f = theory' (K f);
wenzelm@20963
   551
wenzelm@21294
   552
fun begin_local_theory begin f = app_current (fn _ =>
wenzelm@20963
   553
  (fn Theory (Context.Theory thy, _) =>
wenzelm@20963
   554
        let
wenzelm@20985
   555
          val lthy = f thy;
wenzelm@21294
   556
          val gthy = if begin then Context.Proof lthy else Context.Theory (loc_exit lthy);
wenzelm@21294
   557
        in Theory (gthy, SOME lthy) end
wenzelm@20963
   558
    | _ => raise UNDEF));
wenzelm@17076
   559
wenzelm@21294
   560
val end_local_theory = app_current (fn _ =>
wenzelm@21294
   561
  (fn Theory (Context.Proof lthy, _) => Theory (Context.Theory (loc_exit lthy), SOME lthy)
wenzelm@21007
   562
    | _ => raise UNDEF));
wenzelm@21007
   563
wenzelm@21007
   564
local
wenzelm@21007
   565
wenzelm@20963
   566
fun local_theory_presentation loc f g = app_current (fn int =>
wenzelm@21294
   567
  (fn Theory (gthy, _) =>
wenzelm@21294
   568
        let
wenzelm@24780
   569
          val pos = ContextPosition.get (Context.proof_of gthy);
wenzelm@21294
   570
          val finish = loc_finish loc gthy;
wenzelm@24795
   571
          val lthy' = f (ContextPosition.put_ctxt pos (loc_begin loc gthy));
wenzelm@21294
   572
        in Theory (finish lthy', SOME lthy') end
wenzelm@20963
   573
    | _ => raise UNDEF) #> tap (g int));
wenzelm@15668
   574
wenzelm@21007
   575
in
wenzelm@21007
   576
wenzelm@20963
   577
fun local_theory loc f = local_theory_presentation loc f (K I);
wenzelm@20963
   578
fun present_local_theory loc g = local_theory_presentation loc I g;
wenzelm@18955
   579
wenzelm@21007
   580
end;
wenzelm@21007
   581
wenzelm@21007
   582
wenzelm@21007
   583
(* proof transitions *)
wenzelm@21007
   584
wenzelm@21007
   585
fun end_proof f = map_current (fn int =>
wenzelm@24795
   586
  (fn Proof (prf, (finish, _)) =>
wenzelm@21007
   587
        let val state = ProofHistory.current prf in
wenzelm@21007
   588
          if can (Proof.assert_bottom true) state then
wenzelm@21007
   589
            let
wenzelm@21007
   590
              val ctxt' = f int state;
wenzelm@21007
   591
              val gthy' = finish ctxt';
wenzelm@21007
   592
            in Theory (gthy', SOME ctxt') end
wenzelm@21007
   593
          else raise UNDEF
wenzelm@21007
   594
        end
wenzelm@21007
   595
    | SkipProof (h, (gthy, _)) =>
wenzelm@21007
   596
        if History.current h = 0 then Theory (gthy, NONE) else raise UNDEF
wenzelm@21007
   597
    | _ => raise UNDEF));
wenzelm@21007
   598
wenzelm@21294
   599
local
wenzelm@21294
   600
wenzelm@21294
   601
fun begin_proof init finish = app_current (fn int =>
wenzelm@21294
   602
  (fn Theory (gthy, _) =>
wenzelm@21294
   603
    let
berghofe@24453
   604
      val prf = init int gthy;
wenzelm@21294
   605
      val schematic = Proof.schematic_goal prf;
wenzelm@21294
   606
    in
wenzelm@21294
   607
      if ! skip_proofs andalso schematic then
wenzelm@21294
   608
        warning "Cannot skip proof of schematic goal statement"
wenzelm@21294
   609
      else ();
wenzelm@21294
   610
      if ! skip_proofs andalso not schematic then
wenzelm@21294
   611
        SkipProof
wenzelm@21294
   612
          (History.init (undo_limit int) 0, (finish gthy (Proof.global_skip_proof int prf), gthy))
wenzelm@21294
   613
      else Proof (ProofHistory.init (undo_limit int) prf, (finish gthy, gthy))
wenzelm@21294
   614
    end
wenzelm@21294
   615
  | _ => raise UNDEF));
wenzelm@21294
   616
wenzelm@21294
   617
in
wenzelm@21294
   618
wenzelm@24780
   619
fun local_theory_to_proof' loc f = begin_proof
wenzelm@24780
   620
  (fn int => fn gthy =>
wenzelm@24795
   621
    f int (ContextPosition.put_ctxt (ContextPosition.get (Context.proof_of gthy))
wenzelm@24795
   622
      (loc_begin loc gthy)))
wenzelm@24780
   623
  (loc_finish loc);
wenzelm@24780
   624
berghofe@24453
   625
fun local_theory_to_proof loc f = local_theory_to_proof' loc (K f);
wenzelm@21294
   626
wenzelm@21294
   627
fun theory_to_proof f = begin_proof
wenzelm@24780
   628
  (K (fn Context.Theory thy => f thy | _ => raise UNDEF))
wenzelm@24780
   629
  (K (Context.Theory o ProofContext.theory_of));
wenzelm@21294
   630
wenzelm@21294
   631
end;
wenzelm@21294
   632
wenzelm@21007
   633
val forget_proof = map_current (fn _ =>
wenzelm@21007
   634
  (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   635
    | SkipProof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   636
    | _ => raise UNDEF));
wenzelm@21007
   637
wenzelm@21177
   638
fun present_proof f = map_current (fn int =>
wenzelm@21177
   639
  (fn Proof (prf, x) => Proof (ProofHistory.apply I prf, x)
wenzelm@21177
   640
    | SkipProof (h, x) => SkipProof (History.apply I h, x)
wenzelm@21177
   641
    | _ => raise UNDEF) #> tap (f int));
wenzelm@21177
   642
wenzelm@17904
   643
fun proofs' f = map_current (fn int =>
wenzelm@21007
   644
  (fn Proof (prf, x) => Proof (ProofHistory.applys (f int) prf, x)
wenzelm@21007
   645
    | SkipProof (h, x) => SkipProof (History.apply I h, x)
wenzelm@16815
   646
    | _ => raise UNDEF));
wenzelm@15668
   647
wenzelm@17904
   648
fun proof' f = proofs' (Seq.single oo f);
wenzelm@17904
   649
val proofs = proofs' o K;
wenzelm@6689
   650
val proof = proof' o K;
wenzelm@16815
   651
wenzelm@16815
   652
fun actual_proof f = map_current (fn _ =>
wenzelm@21007
   653
  (fn Proof (prf, x) => Proof (f prf, x)
wenzelm@20963
   654
    | _ => raise UNDEF));
wenzelm@16815
   655
wenzelm@16815
   656
fun skip_proof f = map_current (fn _ =>
wenzelm@21007
   657
  (fn SkipProof (h, x) => SkipProof (f h, x)
wenzelm@18563
   658
    | _ => raise UNDEF));
wenzelm@18563
   659
wenzelm@16815
   660
fun skip_proof_to_theory p = map_current (fn _ =>
wenzelm@21007
   661
  (fn SkipProof (h, (gthy, _)) =>
wenzelm@21007
   662
    if p (History.current h) then Theory (gthy, NONE)
wenzelm@17904
   663
    else raise UNDEF
wenzelm@17904
   664
  | _ => raise UNDEF));
wenzelm@5828
   665
wenzelm@5828
   666
wenzelm@5828
   667
wenzelm@5828
   668
(** toplevel transactions **)
wenzelm@5828
   669
wenzelm@25799
   670
(* thread transition properties *)
wenzelm@25799
   671
wenzelm@25819
   672
fun setmp_thread_position (Transition {props, pos, ...}) f x =
wenzelm@25819
   673
  Position.setmp_thread_data pos f x;
wenzelm@25799
   674
wenzelm@25799
   675
wenzelm@5828
   676
(* apply transitions *)
wenzelm@5828
   677
wenzelm@6664
   678
local
wenzelm@6664
   679
wenzelm@25799
   680
fun app int (tr as Transition {trans, pos, int_only, print, no_timing, ...}) =
wenzelm@25819
   681
  setmp_thread_position tr (fn state =>
wenzelm@25799
   682
    let
wenzelm@25799
   683
      val _ =
wenzelm@25799
   684
        if not int andalso int_only then warning (command_msg "Interactive-only " tr)
wenzelm@25799
   685
        else ();
wenzelm@16682
   686
wenzelm@25799
   687
      fun do_timing f x = (warning (command_msg "" tr); timeap f x);
wenzelm@25799
   688
      fun do_profiling f x = profile (! profiling) f x;
wenzelm@25799
   689
wenzelm@25799
   690
      val (result, opt_exn) =
wenzelm@25799
   691
         state |> (apply_trans int pos trans
wenzelm@25799
   692
          |> (if ! profiling > 0 andalso not no_timing then do_profiling else I)
wenzelm@25799
   693
          |> (if ! profiling > 0 orelse ! timing andalso not no_timing then do_timing else I));
wenzelm@25799
   694
      val _ =
wenzelm@25799
   695
        if int andalso not (! quiet) andalso exists (member (op =) print) ("" :: print_mode_value ())
wenzelm@25799
   696
        then print_state false result else ();
wenzelm@25799
   697
    in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) opt_exn) end);
wenzelm@6664
   698
wenzelm@6664
   699
in
wenzelm@5828
   700
wenzelm@6689
   701
fun apply int tr st =
wenzelm@6965
   702
  (case app int tr st of
skalberg@15531
   703
    (_, SOME TERMINATE) => NONE
skalberg@15531
   704
  | (_, SOME RESTART) => SOME (toplevel, NONE)
skalberg@15531
   705
  | (state', SOME (EXCURSION_FAIL exn_info)) => SOME (state', SOME exn_info)
skalberg@15531
   706
  | (state', SOME exn) => SOME (state', SOME (exn, at_command tr))
skalberg@15531
   707
  | (state', NONE) => SOME (state', NONE));
wenzelm@6664
   708
wenzelm@6664
   709
end;
wenzelm@5828
   710
wenzelm@5828
   711
wenzelm@17076
   712
(* excursion: toplevel -- apply transformers/presentation -- toplevel *)
wenzelm@5828
   713
wenzelm@6664
   714
local
wenzelm@6664
   715
wenzelm@5828
   716
fun excur [] x = x
wenzelm@17076
   717
  | excur ((tr, pr) :: trs) (st, res) =
wenzelm@17321
   718
      (case apply (! interact) tr st of
skalberg@15531
   719
        SOME (st', NONE) =>
wenzelm@18685
   720
          excur trs (st', pr st st' res handle exn =>
wenzelm@10324
   721
            raise EXCURSION_FAIL (exn, "Presentation failed\n" ^ at_command tr))
skalberg@15531
   722
      | SOME (st', SOME exn_info) => raise EXCURSION_FAIL exn_info
skalberg@15531
   723
      | NONE => raise EXCURSION_FAIL (TERMINATE, at_command tr));
wenzelm@5828
   724
wenzelm@17076
   725
fun no_pr _ _ _ = ();
wenzelm@17076
   726
wenzelm@6664
   727
in
wenzelm@6664
   728
wenzelm@17076
   729
fun present_excursion trs res =
wenzelm@21958
   730
  (case excur trs (toplevel, res) of
wenzelm@22056
   731
    (state as Toplevel _, res') => (safe_exit state; res')
wenzelm@18685
   732
  | _ => error "Unfinished development at end of input")
wenzelm@9134
   733
  handle exn => error (exn_message exn);
wenzelm@9134
   734
wenzelm@17076
   735
fun excursion trs = present_excursion (map (rpair no_pr) trs) ();
wenzelm@7062
   736
wenzelm@6664
   737
end;
wenzelm@6664
   738
wenzelm@5828
   739
wenzelm@5828
   740
wenzelm@5828
   741
(** interactive transformations **)
wenzelm@5828
   742
wenzelm@5828
   743
(* the global state reference *)
wenzelm@5828
   744
skalberg@15531
   745
val global_state = ref (toplevel, NONE: (exn * string) option);
wenzelm@5828
   746
skalberg@15531
   747
fun set_state state = global_state := (state, NONE);
wenzelm@5828
   748
fun get_state () = fst (! global_state);
wenzelm@5828
   749
fun exn () = snd (! global_state);
wenzelm@5828
   750
wenzelm@5828
   751
wenzelm@24295
   752
(* apply transformers to global state --- NOT THREAD-SAFE! *)
wenzelm@5828
   753
wenzelm@14985
   754
nonfix >> >>>;
wenzelm@5828
   755
wenzelm@24295
   756
fun >> tr =
wenzelm@5828
   757
  (case apply true tr (get_state ()) of
skalberg@15531
   758
    NONE => false
skalberg@15531
   759
  | SOME (state', exn_info) =>
wenzelm@5828
   760
      (global_state := (state', exn_info);
wenzelm@25809
   761
        (case exn_info of
wenzelm@25809
   762
          NONE => ()
wenzelm@25819
   763
        | SOME err => setmp_thread_position tr print_exn err);
wenzelm@24295
   764
        true));
wenzelm@5828
   765
wenzelm@14985
   766
fun >>> [] = ()
wenzelm@14985
   767
  | >>> (tr :: trs) = if >> tr then >>> trs else ();
wenzelm@14985
   768
wenzelm@25441
   769
fun init_state () = (>> (init_empty (K true) (K ()) empty); ());
wenzelm@21958
   770
wenzelm@21958
   771
wenzelm@21958
   772
(* the Isar source of transitions *)
wenzelm@21958
   773
wenzelm@21958
   774
type 'a isar =
wenzelm@21958
   775
  (transition, (transition option,
wenzelm@21958
   776
    (OuterLex.token, (OuterLex.token option, (OuterLex.token, (OuterLex.token,
wenzelm@21958
   777
      Position.T * (Symbol.symbol, (string, 'a) Source.source) Source.source)
wenzelm@21958
   778
          Source.source) Source.source) Source.source) Source.source) Source.source) Source.source;
wenzelm@21958
   779
wenzelm@25527
   780
local
wenzelm@25527
   781
wenzelm@17513
   782
(*Spurious interrupts ahead!  Race condition?*)
wenzelm@17513
   783
fun get_interrupt src = SOME (Source.get_single src) handle Interrupt => NONE;
wenzelm@7602
   784
wenzelm@25527
   785
fun raw_loop secure src =
wenzelm@25527
   786
  let
wenzelm@25527
   787
    fun check_secure () =
wenzelm@25527
   788
      (if secure then warning "Secure loop -- cannot exit to ML" else (); secure);
wenzelm@25551
   789
    val prompt = Markup.markup Markup.prompt Source.default_prompt;
wenzelm@25527
   790
  in
wenzelm@23640
   791
    (case get_interrupt (Source.set_prompt prompt src) of
wenzelm@25527
   792
      NONE => (writeln "\nInterrupt."; raw_loop secure src)
wenzelm@25527
   793
    | SOME NONE => if secure then quit () else ()
wenzelm@25527
   794
    | SOME (SOME (tr, src')) => if >> tr orelse check_secure () then raw_loop secure src' else ())
wenzelm@25584
   795
    handle exn => (CRITICAL (fn () => change crashes (cons exn));
wenzelm@25584
   796
      warning "Recovering after Isar toplevel crash -- see also Toplevel.crashes";
wenzelm@25584
   797
      raw_loop secure src)
wenzelm@23640
   798
  end;
wenzelm@5828
   799
wenzelm@25527
   800
in
wenzelm@25527
   801
wenzelm@25527
   802
fun loop secure src = ignore_interrupt (raw_loop secure) src;
wenzelm@5828
   803
wenzelm@5828
   804
end;
wenzelm@25527
   805
wenzelm@25527
   806
end;