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