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