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