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