src/Pure/Isar/toplevel.ML
author wenzelm
Fri Aug 19 23:25:47 2011 +0200 (2011-08-19 ago)
changeset 44304 7ee000ce5390
parent 44270 3eaad39e520c
child 45488 6d71d9e52369
permissions -rw-r--r--
maintain recent future proofs at transaction boundaries;
wenzelm@5828
     1
(*  Title:      Pure/Isar/toplevel.ML
wenzelm@5828
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5828
     3
wenzelm@26602
     4
Isabelle/Isar toplevel transactions.
wenzelm@5828
     5
*)
wenzelm@5828
     6
wenzelm@5828
     7
signature TOPLEVEL =
wenzelm@5828
     8
sig
wenzelm@19063
     9
  exception UNDEF
wenzelm@5828
    10
  type state
wenzelm@26602
    11
  val toplevel: state
wenzelm@7732
    12
  val is_toplevel: state -> bool
wenzelm@18589
    13
  val is_theory: state -> bool
wenzelm@18589
    14
  val is_proof: state -> bool
wenzelm@17076
    15
  val level: state -> int
wenzelm@30398
    16
  val presentation_context_of: state -> Proof.context
wenzelm@30801
    17
  val previous_context_of: state -> Proof.context option
wenzelm@21506
    18
  val context_of: state -> Proof.context
wenzelm@22089
    19
  val generic_theory_of: state -> generic_theory
wenzelm@5828
    20
  val theory_of: state -> theory
wenzelm@5828
    21
  val proof_of: state -> Proof.state
wenzelm@18589
    22
  val proof_position_of: state -> int
wenzelm@37953
    23
  val end_theory: Position.T -> state -> theory
wenzelm@16815
    24
  val print_state_context: state -> unit
wenzelm@16815
    25
  val print_state: bool -> state -> unit
wenzelm@37858
    26
  val pretty_abstract: state -> Pretty.T
wenzelm@32738
    27
  val quiet: bool Unsynchronized.ref
wenzelm@32738
    28
  val debug: bool Unsynchronized.ref
wenzelm@32738
    29
  val interact: bool Unsynchronized.ref
wenzelm@32738
    30
  val timing: bool Unsynchronized.ref
wenzelm@32738
    31
  val profiling: int Unsynchronized.ref
wenzelm@32738
    32
  val skip_proofs: bool Unsynchronized.ref
wenzelm@20128
    33
  val program: (unit -> 'a) -> 'a
wenzelm@33604
    34
  val thread: bool -> (unit -> unit) -> Thread.thread
wenzelm@16682
    35
  type transition
wenzelm@5828
    36
  val empty: transition
wenzelm@38888
    37
  val print_of: transition -> bool
wenzelm@27427
    38
  val name_of: transition -> string
wenzelm@28105
    39
  val pos_of: transition -> Position.T
wenzelm@27500
    40
  val str_of: transition -> string
wenzelm@5828
    41
  val name: string -> transition -> transition
wenzelm@5828
    42
  val position: Position.T -> transition -> transition
wenzelm@5828
    43
  val interactive: bool -> transition -> transition
wenzelm@38888
    44
  val set_print: bool -> transition -> transition
wenzelm@5828
    45
  val print: transition -> transition
wenzelm@9010
    46
  val no_timing: transition -> transition
wenzelm@44187
    47
  val init_theory: (unit -> theory) -> transition -> transition
wenzelm@44187
    48
  val is_init: transition -> bool
wenzelm@44186
    49
  val modify_init: (unit -> theory) -> transition -> transition
wenzelm@6689
    50
  val exit: transition -> transition
wenzelm@5828
    51
  val keep: (state -> unit) -> transition -> transition
wenzelm@7612
    52
  val keep': (bool -> state -> unit) -> transition -> transition
wenzelm@5828
    53
  val imperative: (unit -> unit) -> transition -> transition
wenzelm@27840
    54
  val ignored: Position.T -> transition
wenzelm@27840
    55
  val malformed: Position.T -> string -> transition
wenzelm@5828
    56
  val theory: (theory -> theory) -> transition -> transition
wenzelm@26491
    57
  val generic_theory: (generic_theory -> generic_theory) -> transition -> transition
wenzelm@7612
    58
  val theory': (bool -> theory -> theory) -> transition -> transition
wenzelm@20985
    59
  val begin_local_theory: bool -> (theory -> local_theory) -> transition -> transition
wenzelm@21007
    60
  val end_local_theory: transition -> transition
wenzelm@29380
    61
  val local_theory': xstring option -> (bool -> local_theory -> local_theory) ->
wenzelm@29380
    62
    transition -> transition
wenzelm@20963
    63
  val local_theory: xstring option -> (local_theory -> local_theory) -> transition -> transition
wenzelm@30366
    64
  val present_local_theory: xstring option -> (state -> unit) -> transition -> transition
berghofe@24453
    65
  val local_theory_to_proof': xstring option -> (bool -> local_theory -> Proof.state) ->
berghofe@24453
    66
    transition -> transition
wenzelm@21007
    67
  val local_theory_to_proof: xstring option -> (local_theory -> Proof.state) ->
wenzelm@21007
    68
    transition -> transition
wenzelm@17363
    69
  val theory_to_proof: (theory -> Proof.state) -> transition -> transition
wenzelm@21007
    70
  val end_proof: (bool -> Proof.state -> Proof.context) -> transition -> transition
wenzelm@21007
    71
  val forget_proof: transition -> transition
wenzelm@30366
    72
  val present_proof: (state -> unit) -> transition -> transition
wenzelm@21177
    73
  val proofs': (bool -> Proof.state -> Proof.state Seq.seq) -> transition -> transition
wenzelm@17904
    74
  val proof': (bool -> Proof.state -> Proof.state) -> transition -> transition
wenzelm@21177
    75
  val proofs: (Proof.state -> Proof.state Seq.seq) -> transition -> transition
wenzelm@21177
    76
  val proof: (Proof.state -> Proof.state) -> transition -> transition
wenzelm@33390
    77
  val actual_proof: (Proof_Node.T -> Proof_Node.T) -> transition -> transition
wenzelm@27564
    78
  val skip_proof: (int -> int) -> transition -> transition
wenzelm@17904
    79
  val skip_proof_to_theory: (int -> bool) -> transition -> transition
wenzelm@27427
    80
  val get_id: transition -> string option
wenzelm@27427
    81
  val put_id: string -> transition -> transition
wenzelm@9512
    82
  val unknown_theory: transition -> transition
wenzelm@9512
    83
  val unknown_proof: transition -> transition
wenzelm@9512
    84
  val unknown_context: transition -> transition
wenzelm@28425
    85
  val setmp_thread_position: transition -> ('a -> 'b) -> 'a -> 'b
wenzelm@27606
    86
  val status: transition -> Markup.T -> unit
wenzelm@44270
    87
  val error_msg: transition -> serial * string -> unit
wenzelm@28103
    88
  val add_hook: (transition -> state -> state -> unit) -> unit
wenzelm@26602
    89
  val transition: bool -> transition -> state -> (state * (exn * string) option) option
wenzelm@28425
    90
  val command: transition -> state -> state
wenzelm@42129
    91
  val excursion:
wenzelm@42129
    92
    (transition * transition list) list -> (transition * state) list future list * theory
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@31476
   100
exception UNDEF = Runtime.UNDEF;
wenzelm@19063
   101
wenzelm@19063
   102
wenzelm@21294
   103
(* local theory wrappers *)
wenzelm@5828
   104
haftmann@38350
   105
val loc_init = Named_Target.context_cmd;
wenzelm@33671
   106
val loc_exit = Local_Theory.exit_global;
wenzelm@21294
   107
wenzelm@25292
   108
fun loc_begin loc (Context.Theory thy) = loc_init (the_default "-" loc) thy
wenzelm@21294
   109
  | loc_begin NONE (Context.Proof lthy) = lthy
haftmann@38391
   110
  | loc_begin (SOME loc) (Context.Proof lthy) = (loc_init loc o loc_exit) lthy;
wenzelm@21294
   111
wenzelm@21294
   112
fun loc_finish _ (Context.Theory _) = Context.Theory o loc_exit
wenzelm@33671
   113
  | loc_finish NONE (Context.Proof _) = Context.Proof o Local_Theory.restore
haftmann@38391
   114
  | loc_finish (SOME _) (Context.Proof lthy) = Context.Proof o Named_Target.reinit lthy;
wenzelm@21294
   115
wenzelm@21294
   116
wenzelm@21958
   117
(* datatype node *)
wenzelm@21294
   118
wenzelm@5828
   119
datatype node =
wenzelm@27576
   120
  Theory of generic_theory * Proof.context option
wenzelm@27576
   121
    (*theory with presentation context*) |
wenzelm@33390
   122
  Proof of Proof_Node.T * ((Proof.context -> generic_theory) * generic_theory)
wenzelm@27576
   123
    (*proof node, finish, original theory*) |
wenzelm@27564
   124
  SkipProof of int * (generic_theory * generic_theory);
wenzelm@27564
   125
    (*proof depth, resulting theory, original theory*)
wenzelm@5828
   126
wenzelm@20963
   127
val theory_node = fn Theory (gthy, _) => SOME gthy | _ => NONE;
wenzelm@18589
   128
val proof_node = fn Proof (prf, _) => SOME prf | _ => NONE;
wenzelm@18589
   129
wenzelm@20963
   130
fun cases_node f _ (Theory (gthy, _)) = f gthy
wenzelm@33390
   131
  | cases_node _ g (Proof (prf, _)) = g (Proof_Node.current prf)
wenzelm@21007
   132
  | cases_node f _ (SkipProof (_, (gthy, _))) = f gthy;
wenzelm@19063
   133
wenzelm@29066
   134
val context_node = cases_node Context.proof_of Proof.context_of;
wenzelm@29066
   135
wenzelm@21958
   136
wenzelm@21958
   137
(* datatype state *)
wenzelm@21958
   138
wenzelm@37953
   139
datatype state = State of node option * node option;  (*current, previous*)
wenzelm@5828
   140
wenzelm@27576
   141
val toplevel = State (NONE, NONE);
wenzelm@5828
   142
wenzelm@27576
   143
fun is_toplevel (State (NONE, _)) = true
wenzelm@7732
   144
  | is_toplevel _ = false;
wenzelm@7732
   145
wenzelm@27576
   146
fun level (State (NONE, _)) = 0
wenzelm@37953
   147
  | level (State (SOME (Theory _), _)) = 0
wenzelm@37953
   148
  | level (State (SOME (Proof (prf, _)), _)) = Proof.level (Proof_Node.current prf)
wenzelm@37953
   149
  | level (State (SOME (SkipProof (d, _)), _)) = d + 1;   (*different notion of proof depth!*)
wenzelm@17076
   150
wenzelm@27576
   151
fun str_of_state (State (NONE, _)) = "at top level"
wenzelm@37953
   152
  | str_of_state (State (SOME (Theory (Context.Theory _, _)), _)) = "in theory mode"
wenzelm@37953
   153
  | str_of_state (State (SOME (Theory (Context.Proof _, _)), _)) = "in local theory mode"
wenzelm@37953
   154
  | str_of_state (State (SOME (Proof _), _)) = "in proof mode"
wenzelm@37953
   155
  | str_of_state (State (SOME (SkipProof _), _)) = "in skipped proof mode";
wenzelm@5946
   156
wenzelm@5946
   157
wenzelm@27576
   158
(* current node *)
wenzelm@5828
   159
wenzelm@27576
   160
fun node_of (State (NONE, _)) = raise UNDEF
wenzelm@37953
   161
  | node_of (State (SOME node, _)) = node;
wenzelm@5828
   162
wenzelm@18589
   163
fun is_theory state = not (is_toplevel state) andalso is_some (theory_node (node_of state));
wenzelm@18589
   164
fun is_proof state = not (is_toplevel state) andalso is_some (proof_node (node_of state));
wenzelm@18589
   165
wenzelm@19063
   166
fun node_case f g state = cases_node f g (node_of state);
wenzelm@5828
   167
wenzelm@30398
   168
fun presentation_context_of state =
wenzelm@30398
   169
  (case try node_of state of
wenzelm@30398
   170
    SOME (Theory (_, SOME ctxt)) => ctxt
wenzelm@30398
   171
  | SOME node => context_node node
wenzelm@30398
   172
  | NONE => raise UNDEF);
wenzelm@30366
   173
wenzelm@30801
   174
fun previous_context_of (State (_, NONE)) = NONE
wenzelm@37953
   175
  | previous_context_of (State (_, SOME prev)) = SOME (context_node prev);
wenzelm@30801
   176
wenzelm@21506
   177
val context_of = node_case Context.proof_of Proof.context_of;
wenzelm@22089
   178
val generic_theory_of = node_case I (Context.Proof o Proof.context_of);
wenzelm@20963
   179
val theory_of = node_case Context.theory_of Proof.theory_of;
wenzelm@18589
   180
val proof_of = node_case (fn _ => raise UNDEF) I;
wenzelm@17208
   181
wenzelm@18589
   182
fun proof_position_of state =
wenzelm@18589
   183
  (case node_of state of
wenzelm@33390
   184
    Proof (prf, _) => Proof_Node.position prf
wenzelm@18589
   185
  | _ => raise UNDEF);
wenzelm@6664
   186
wenzelm@43667
   187
fun end_theory _ (State (NONE, SOME (Theory (Context.Theory thy, _)))) = thy
wenzelm@44200
   188
  | end_theory pos (State (NONE, _)) = error ("Missing theory" ^ Position.str_of pos)
wenzelm@44200
   189
  | end_theory pos (State (SOME _, _)) = error ("Unfinished theory" ^ Position.str_of pos);
wenzelm@37953
   190
wenzelm@5828
   191
wenzelm@16815
   192
(* print state *)
wenzelm@16815
   193
haftmann@38388
   194
val pretty_context = Local_Theory.pretty o Context.cases (Named_Target.theory_init) I;
wenzelm@16815
   195
wenzelm@23640
   196
fun print_state_context state =
wenzelm@24795
   197
  (case try node_of state of
wenzelm@21506
   198
    NONE => []
wenzelm@24795
   199
  | SOME (Theory (gthy, _)) => pretty_context gthy
wenzelm@24795
   200
  | SOME (Proof (_, (_, gthy))) => pretty_context gthy
wenzelm@24795
   201
  | SOME (SkipProof (_, (gthy, _))) => pretty_context gthy)
wenzelm@23640
   202
  |> Pretty.chunks |> Pretty.writeln;
wenzelm@16815
   203
wenzelm@23640
   204
fun print_state prf_only state =
wenzelm@23701
   205
  (case try node_of state of
wenzelm@23701
   206
    NONE => []
wenzelm@23701
   207
  | SOME (Theory (gthy, _)) => if prf_only then [] else pretty_context gthy
wenzelm@23701
   208
  | SOME (Proof (prf, _)) =>
wenzelm@33390
   209
      Proof.pretty_state (Proof_Node.position prf) (Proof_Node.current prf)
wenzelm@27564
   210
  | SOME (SkipProof (d, _)) => [Pretty.str ("skipped proof: depth " ^ string_of_int d)])
wenzelm@23701
   211
  |> Pretty.markup_chunks Markup.state |> Pretty.writeln;
wenzelm@16815
   212
wenzelm@37858
   213
fun pretty_abstract state = Pretty.str ("<Isar " ^ str_of_state state ^ ">");
wenzelm@37858
   214
wenzelm@16815
   215
wenzelm@15668
   216
wenzelm@5828
   217
(** toplevel transitions **)
wenzelm@5828
   218
wenzelm@32738
   219
val quiet = Unsynchronized.ref false;
wenzelm@39513
   220
val debug = Runtime.debug;
wenzelm@32738
   221
val interact = Unsynchronized.ref false;
wenzelm@42012
   222
val timing = Unsynchronized.ref false;
wenzelm@32738
   223
val profiling = Unsynchronized.ref 0;
wenzelm@32738
   224
val skip_proofs = Unsynchronized.ref false;
wenzelm@16682
   225
wenzelm@33604
   226
fun program body =
wenzelm@33604
   227
 (body
wenzelm@31476
   228
  |> Runtime.controlled_execution
wenzelm@33604
   229
  |> Runtime.toplevel_error (Output.error_msg o ML_Compiler.exn_message)) ();
wenzelm@33604
   230
wenzelm@33604
   231
fun thread interrupts body =
wenzelm@33604
   232
  Thread.fork
wenzelm@39232
   233
    (((fn () => body () handle exn => if Exn.is_interrupt exn then () else reraise exn)
wenzelm@33604
   234
        |> Runtime.debugging
wenzelm@33604
   235
        |> Runtime.toplevel_error
wenzelm@40132
   236
          (fn exn =>
wenzelm@40132
   237
            Output.urgent_message ("## INTERNAL ERROR ##\n" ^ ML_Compiler.exn_message exn))),
wenzelm@37216
   238
      Simple_Thread.attributes interrupts);
wenzelm@20128
   239
wenzelm@5828
   240
wenzelm@27601
   241
(* node transactions -- maintaining stable checkpoints *)
wenzelm@7022
   242
wenzelm@31476
   243
exception FAILURE of state * exn;
wenzelm@31476
   244
wenzelm@6689
   245
local
wenzelm@6689
   246
wenzelm@30366
   247
fun reset_presentation (Theory (gthy, _)) = Theory (gthy, NONE)
wenzelm@30366
   248
  | reset_presentation node = node;
wenzelm@30366
   249
wenzelm@26624
   250
fun is_draft_theory (Theory (gthy, _)) = Context.is_draft (Context.theory_of gthy)
wenzelm@26624
   251
  | is_draft_theory _ = false;
wenzelm@26624
   252
wenzelm@31476
   253
fun is_stale state = Context.is_stale (theory_of state) handle Runtime.UNDEF => false;
wenzelm@27601
   254
wenzelm@26624
   255
fun stale_error NONE = SOME (ERROR "Stale theory encountered after successful execution!")
wenzelm@26624
   256
  | stale_error some = some;
wenzelm@16815
   257
wenzelm@27576
   258
fun map_theory f (Theory (gthy, ctxt)) =
wenzelm@33671
   259
      Theory (Context.mapping f (Local_Theory.raw_theory f) gthy, ctxt)
wenzelm@27576
   260
  | map_theory _ node = node;
wenzelm@6689
   261
wenzelm@6689
   262
in
wenzelm@6689
   263
wenzelm@37953
   264
fun apply_transaction f g node =
wenzelm@20128
   265
  let
wenzelm@27576
   266
    val _ = is_draft_theory node andalso error "Illegal draft theory in toplevel state";
wenzelm@27576
   267
    val cont_node = reset_presentation node;
wenzelm@27576
   268
    val back_node = map_theory (Theory.checkpoint o Theory.copy) cont_node;
wenzelm@37953
   269
    fun state_error e nd = (State (SOME nd, SOME node), e);
wenzelm@6689
   270
wenzelm@20128
   271
    val (result, err) =
wenzelm@20128
   272
      cont_node
wenzelm@31476
   273
      |> Runtime.controlled_execution f
wenzelm@26624
   274
      |> map_theory Theory.checkpoint
wenzelm@26624
   275
      |> state_error NONE
wenzelm@26624
   276
      handle exn => state_error (SOME exn) cont_node;
wenzelm@26624
   277
wenzelm@26624
   278
    val (result', err') =
wenzelm@26624
   279
      if is_stale result then state_error (stale_error err) back_node
wenzelm@26624
   280
      else (result, err);
wenzelm@20128
   281
  in
wenzelm@26624
   282
    (case err' of
wenzelm@30366
   283
      NONE => tap g result'
wenzelm@26624
   284
    | SOME exn => raise FAILURE (result', exn))
wenzelm@20128
   285
  end;
wenzelm@6689
   286
wenzelm@43667
   287
val exit_transaction =
wenzelm@43667
   288
  apply_transaction
wenzelm@43667
   289
    (fn Theory (Context.Theory thy, _) => Theory (Context.Theory (Theory.end_theory thy), NONE)
wenzelm@43667
   290
      | node => node) (K ())
wenzelm@43667
   291
  #> (fn State (node', _) => State (NONE, node'));
wenzelm@43667
   292
wenzelm@6689
   293
end;
wenzelm@6689
   294
wenzelm@6689
   295
wenzelm@6689
   296
(* primitive transitions *)
wenzelm@6689
   297
wenzelm@5828
   298
datatype trans =
wenzelm@44187
   299
  Init of unit -> theory |               (*init theory*)
wenzelm@37953
   300
  Exit |                                 (*formal exit of theory*)
wenzelm@37953
   301
  Keep of bool -> state -> unit |        (*peek at state*)
wenzelm@30366
   302
  Transaction of (bool -> node -> node) * (state -> unit);  (*node transaction and presentation*)
wenzelm@21958
   303
wenzelm@6689
   304
local
wenzelm@5828
   305
wenzelm@44187
   306
fun apply_tr _ (Init f) (State (NONE, _)) =
wenzelm@33727
   307
      State (SOME (Theory (Context.Theory
wenzelm@44186
   308
          (Theory.checkpoint (Runtime.controlled_execution f ())), NONE)), NONE)
wenzelm@43667
   309
  | apply_tr _ Exit (State (SOME (state as Theory (Context.Theory _, _)), _)) =
wenzelm@43667
   310
      exit_transaction state
wenzelm@32792
   311
  | apply_tr int (Keep f) state =
wenzelm@31476
   312
      Runtime.controlled_execution (fn x => tap (f int) x) state
wenzelm@32792
   313
  | apply_tr int (Transaction (f, g)) (State (SOME state, _)) =
wenzelm@32792
   314
      apply_transaction (fn x => f int x) g state
wenzelm@32792
   315
  | apply_tr _ _ _ = raise UNDEF;
wenzelm@5828
   316
wenzelm@32792
   317
fun apply_union _ [] state = raise FAILURE (state, UNDEF)
wenzelm@32792
   318
  | apply_union int (tr :: trs) state =
wenzelm@32792
   319
      apply_union int trs state
wenzelm@32792
   320
        handle Runtime.UNDEF => apply_tr int tr state
wenzelm@32792
   321
          | FAILURE (alt_state, UNDEF) => apply_tr int tr alt_state
wenzelm@6689
   322
          | exn as FAILURE _ => raise exn
wenzelm@6689
   323
          | exn => raise FAILURE (state, exn);
wenzelm@6689
   324
wenzelm@6689
   325
in
wenzelm@6689
   326
wenzelm@32792
   327
fun apply_trans int trs state = (apply_union int trs state, NONE)
skalberg@15531
   328
  handle FAILURE (alt_state, exn) => (alt_state, SOME exn) | exn => (state, SOME exn);
wenzelm@6689
   329
wenzelm@6689
   330
end;
wenzelm@5828
   331
wenzelm@5828
   332
wenzelm@5828
   333
(* datatype transition *)
wenzelm@5828
   334
wenzelm@5828
   335
datatype transition = Transition of
wenzelm@26621
   336
 {name: string,              (*command name*)
wenzelm@26621
   337
  pos: Position.T,           (*source position*)
wenzelm@26621
   338
  int_only: bool,            (*interactive-only*)
wenzelm@26621
   339
  print: bool,               (*print result state*)
wenzelm@26621
   340
  no_timing: bool,           (*suppress timing*)
wenzelm@26621
   341
  trans: trans list};        (*primitive transitions (union)*)
wenzelm@5828
   342
wenzelm@26621
   343
fun make_transition (name, pos, int_only, print, no_timing, trans) =
wenzelm@26621
   344
  Transition {name = name, pos = pos, int_only = int_only, print = print, no_timing = no_timing,
wenzelm@26621
   345
    trans = trans};
wenzelm@5828
   346
wenzelm@26621
   347
fun map_transition f (Transition {name, pos, int_only, print, no_timing, trans}) =
wenzelm@26621
   348
  make_transition (f (name, pos, int_only, print, no_timing, trans));
wenzelm@5828
   349
wenzelm@27441
   350
val empty = make_transition ("", Position.none, false, false, false, []);
wenzelm@5828
   351
wenzelm@5828
   352
wenzelm@5828
   353
(* diagnostics *)
wenzelm@5828
   354
wenzelm@38888
   355
fun print_of (Transition {print, ...}) = print;
wenzelm@27427
   356
fun name_of (Transition {name, ...}) = name;
wenzelm@28105
   357
fun pos_of (Transition {pos, ...}) = pos;
wenzelm@28105
   358
fun str_of tr = quote (name_of tr) ^ Position.str_of (pos_of tr);
wenzelm@5828
   359
wenzelm@27427
   360
fun command_msg msg tr = msg ^ "command " ^ str_of tr;
wenzelm@38875
   361
fun at_command tr = command_msg "At " tr;
wenzelm@5828
   362
wenzelm@5828
   363
fun type_error tr state =
wenzelm@18685
   364
  ERROR (command_msg "Illegal application of " tr ^ " " ^ str_of_state state);
wenzelm@5828
   365
wenzelm@5828
   366
wenzelm@5828
   367
(* modify transitions *)
wenzelm@5828
   368
wenzelm@28451
   369
fun name name = map_transition (fn (_, pos, int_only, print, no_timing, trans) =>
wenzelm@28451
   370
  (name, pos, int_only, print, no_timing, trans));
wenzelm@9010
   371
wenzelm@26621
   372
fun position pos = map_transition (fn (name, _, int_only, print, no_timing, trans) =>
wenzelm@26621
   373
  (name, pos, int_only, print, no_timing, trans));
wenzelm@5828
   374
wenzelm@26621
   375
fun interactive int_only = map_transition (fn (name, pos, _, print, no_timing, trans) =>
wenzelm@26621
   376
  (name, pos, int_only, print, no_timing, trans));
wenzelm@14923
   377
wenzelm@26621
   378
val no_timing = map_transition (fn (name, pos, int_only, print, _, trans) =>
wenzelm@26621
   379
  (name, pos, int_only, print, true, trans));
wenzelm@17363
   380
wenzelm@26621
   381
fun add_trans tr = map_transition (fn (name, pos, int_only, print, no_timing, trans) =>
wenzelm@28451
   382
  (name, pos, int_only, print, no_timing, tr :: trans));
wenzelm@16607
   383
wenzelm@28433
   384
val reset_trans = map_transition (fn (name, pos, int_only, print, no_timing, _) =>
wenzelm@28433
   385
  (name, pos, int_only, print, no_timing, []));
wenzelm@28433
   386
wenzelm@28453
   387
fun set_print print = map_transition (fn (name, pos, int_only, _, no_timing, trans) =>
wenzelm@28453
   388
  (name, pos, int_only, print, no_timing, trans));
wenzelm@28453
   389
wenzelm@28453
   390
val print = set_print true;
wenzelm@5828
   391
wenzelm@5828
   392
wenzelm@21007
   393
(* basic transitions *)
wenzelm@5828
   394
wenzelm@44187
   395
fun init_theory f = add_trans (Init f);
wenzelm@37977
   396
wenzelm@44187
   397
fun is_init (Transition {trans = [Init _], ...}) = true
wenzelm@44187
   398
  | is_init _ = false;
wenzelm@44187
   399
wenzelm@44187
   400
fun modify_init f tr = if is_init tr then init_theory f (reset_trans tr) else tr;
wenzelm@37977
   401
wenzelm@6689
   402
val exit = add_trans Exit;
wenzelm@7612
   403
val keep' = add_trans o Keep;
wenzelm@30366
   404
wenzelm@30366
   405
fun present_transaction f g = add_trans (Transaction (f, g));
wenzelm@30366
   406
fun transaction f = present_transaction f (K ());
wenzelm@5828
   407
wenzelm@7612
   408
fun keep f = add_trans (Keep (fn _ => f));
wenzelm@5828
   409
fun imperative f = keep (fn _ => f ());
wenzelm@5828
   410
wenzelm@27840
   411
fun ignored pos = empty |> name "<ignored>" |> position pos |> imperative I;
wenzelm@27840
   412
fun malformed pos msg =
wenzelm@27840
   413
  empty |> name "<malformed>" |> position pos |> imperative (fn () => error msg);
wenzelm@27840
   414
wenzelm@21007
   415
val unknown_theory = imperative (fn () => warning "Unknown theory context");
wenzelm@21007
   416
val unknown_proof = imperative (fn () => warning "Unknown proof context");
wenzelm@21007
   417
val unknown_context = imperative (fn () => warning "Unknown context");
wenzelm@15668
   418
wenzelm@21007
   419
wenzelm@21007
   420
(* theory transitions *)
wenzelm@15668
   421
wenzelm@44304
   422
val global_theory_group =
wenzelm@44304
   423
  Sign.new_group #>
wenzelm@44304
   424
  Global_Theory.begin_recent_proofs #> Theory.checkpoint;
wenzelm@44304
   425
wenzelm@44304
   426
val local_theory_group =
wenzelm@44304
   427
  Local_Theory.new_group #>
wenzelm@44304
   428
  Local_Theory.raw_theory (Global_Theory.begin_recent_proofs #> Theory.checkpoint);
wenzelm@44304
   429
wenzelm@27601
   430
fun generic_theory f = transaction (fn _ =>
wenzelm@26491
   431
  (fn Theory (gthy, _) => Theory (f gthy, NONE)
wenzelm@26491
   432
    | _ => raise UNDEF));
wenzelm@26491
   433
wenzelm@27601
   434
fun theory' f = transaction (fn int =>
wenzelm@33725
   435
  (fn Theory (Context.Theory thy, _) =>
wenzelm@33725
   436
      let val thy' = thy
wenzelm@44304
   437
        |> global_theory_group
wenzelm@33725
   438
        |> f int
wenzelm@33725
   439
        |> Sign.reset_group;
wenzelm@33725
   440
      in Theory (Context.Theory thy', NONE) end
wenzelm@20963
   441
    | _ => raise UNDEF));
wenzelm@20963
   442
wenzelm@20963
   443
fun theory f = theory' (K f);
wenzelm@20963
   444
wenzelm@27601
   445
fun begin_local_theory begin f = transaction (fn _ =>
wenzelm@20963
   446
  (fn Theory (Context.Theory thy, _) =>
wenzelm@20963
   447
        let
wenzelm@20985
   448
          val lthy = f thy;
wenzelm@21294
   449
          val gthy = if begin then Context.Proof lthy else Context.Theory (loc_exit lthy);
wenzelm@21294
   450
        in Theory (gthy, SOME lthy) end
wenzelm@20963
   451
    | _ => raise UNDEF));
wenzelm@17076
   452
wenzelm@27601
   453
val end_local_theory = transaction (fn _ =>
wenzelm@21294
   454
  (fn Theory (Context.Proof lthy, _) => Theory (Context.Theory (loc_exit lthy), SOME lthy)
wenzelm@21007
   455
    | _ => raise UNDEF));
wenzelm@21007
   456
wenzelm@21007
   457
local
wenzelm@21007
   458
wenzelm@30366
   459
fun local_theory_presentation loc f = present_transaction (fn int =>
wenzelm@21294
   460
  (fn Theory (gthy, _) =>
wenzelm@21294
   461
        let
wenzelm@21294
   462
          val finish = loc_finish loc gthy;
wenzelm@33725
   463
          val lthy' = loc_begin loc gthy
wenzelm@44304
   464
            |> local_theory_group
wenzelm@33725
   465
            |> f int
wenzelm@33725
   466
            |> Local_Theory.reset_group;
wenzelm@21294
   467
        in Theory (finish lthy', SOME lthy') end
wenzelm@30366
   468
    | _ => raise UNDEF));
wenzelm@15668
   469
wenzelm@21007
   470
in
wenzelm@21007
   471
wenzelm@30366
   472
fun local_theory' loc f = local_theory_presentation loc f (K ());
wenzelm@29380
   473
fun local_theory loc f = local_theory' loc (K f);
wenzelm@30366
   474
fun present_local_theory loc = local_theory_presentation loc (K I);
wenzelm@18955
   475
wenzelm@21007
   476
end;
wenzelm@21007
   477
wenzelm@21007
   478
wenzelm@21007
   479
(* proof transitions *)
wenzelm@21007
   480
wenzelm@27601
   481
fun end_proof f = transaction (fn int =>
wenzelm@24795
   482
  (fn Proof (prf, (finish, _)) =>
wenzelm@33390
   483
        let val state = Proof_Node.current prf in
wenzelm@21007
   484
          if can (Proof.assert_bottom true) state then
wenzelm@21007
   485
            let
wenzelm@21007
   486
              val ctxt' = f int state;
wenzelm@21007
   487
              val gthy' = finish ctxt';
wenzelm@21007
   488
            in Theory (gthy', SOME ctxt') end
wenzelm@21007
   489
          else raise UNDEF
wenzelm@21007
   490
        end
wenzelm@27564
   491
    | SkipProof (0, (gthy, _)) => Theory (gthy, NONE)
wenzelm@21007
   492
    | _ => raise UNDEF));
wenzelm@21007
   493
wenzelm@21294
   494
local
wenzelm@21294
   495
wenzelm@27601
   496
fun begin_proof init finish = transaction (fn int =>
wenzelm@21294
   497
  (fn Theory (gthy, _) =>
wenzelm@21294
   498
    let
berghofe@24453
   499
      val prf = init int gthy;
wenzelm@28433
   500
      val skip = ! skip_proofs;
wenzelm@40960
   501
      val (is_goal, no_skip) =
wenzelm@40960
   502
        (true, Proof.schematic_goal prf) handle ERROR _ => (false, true);
wenzelm@21294
   503
    in
wenzelm@40960
   504
      if is_goal andalso skip andalso no_skip then
wenzelm@21294
   505
        warning "Cannot skip proof of schematic goal statement"
wenzelm@21294
   506
      else ();
wenzelm@40960
   507
      if skip andalso not no_skip then
wenzelm@27564
   508
        SkipProof (0, (finish gthy (Proof.global_skip_proof int prf), gthy))
wenzelm@33390
   509
      else Proof (Proof_Node.init prf, (finish gthy, gthy))
wenzelm@21294
   510
    end
wenzelm@21294
   511
  | _ => raise UNDEF));
wenzelm@21294
   512
wenzelm@21294
   513
in
wenzelm@21294
   514
wenzelm@24780
   515
fun local_theory_to_proof' loc f = begin_proof
wenzelm@44304
   516
  (fn int => fn gthy => f int (local_theory_group (loc_begin loc gthy)))
wenzelm@33725
   517
  (fn gthy => loc_finish loc gthy o Local_Theory.reset_group);
wenzelm@24780
   518
berghofe@24453
   519
fun local_theory_to_proof loc f = local_theory_to_proof' loc (K f);
wenzelm@21294
   520
wenzelm@21294
   521
fun theory_to_proof f = begin_proof
wenzelm@44304
   522
  (K (fn Context.Theory thy => f (global_theory_group thy) | _ => raise UNDEF))
wenzelm@42360
   523
  (K (Context.Theory o Sign.reset_group o Proof_Context.theory_of));
wenzelm@21294
   524
wenzelm@21294
   525
end;
wenzelm@21294
   526
wenzelm@27601
   527
val forget_proof = transaction (fn _ =>
wenzelm@21007
   528
  (fn Proof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   529
    | SkipProof (_, (_, orig_gthy)) => Theory (orig_gthy, NONE)
wenzelm@21007
   530
    | _ => raise UNDEF));
wenzelm@21007
   531
wenzelm@30366
   532
val present_proof = present_transaction (fn _ =>
wenzelm@33390
   533
  (fn Proof (prf, x) => Proof (Proof_Node.apply I prf, x)
wenzelm@27564
   534
    | skip as SkipProof _ => skip
wenzelm@30366
   535
    | _ => raise UNDEF));
wenzelm@21177
   536
wenzelm@27601
   537
fun proofs' f = transaction (fn int =>
wenzelm@33390
   538
  (fn Proof (prf, x) => Proof (Proof_Node.applys (f int) prf, x)
wenzelm@27564
   539
    | skip as SkipProof _ => skip
wenzelm@16815
   540
    | _ => raise UNDEF));
wenzelm@15668
   541
wenzelm@17904
   542
fun proof' f = proofs' (Seq.single oo f);
wenzelm@17904
   543
val proofs = proofs' o K;
wenzelm@6689
   544
val proof = proof' o K;
wenzelm@16815
   545
wenzelm@27601
   546
fun actual_proof f = transaction (fn _ =>
wenzelm@21007
   547
  (fn Proof (prf, x) => Proof (f prf, x)
wenzelm@20963
   548
    | _ => raise UNDEF));
wenzelm@16815
   549
wenzelm@27601
   550
fun skip_proof f = transaction (fn _ =>
wenzelm@21007
   551
  (fn SkipProof (h, x) => SkipProof (f h, x)
wenzelm@18563
   552
    | _ => raise UNDEF));
wenzelm@18563
   553
wenzelm@27601
   554
fun skip_proof_to_theory pred = transaction (fn _ =>
wenzelm@27564
   555
  (fn SkipProof (d, (gthy, _)) => if pred d then Theory (gthy, NONE) else raise UNDEF
wenzelm@33725
   556
    | _ => raise UNDEF));
wenzelm@5828
   557
wenzelm@5828
   558
wenzelm@5828
   559
wenzelm@5828
   560
(** toplevel transactions **)
wenzelm@5828
   561
wenzelm@27427
   562
(* identification *)
wenzelm@27427
   563
wenzelm@27427
   564
fun get_id (Transition {pos, ...}) = Position.get_id pos;
wenzelm@27427
   565
fun put_id id (tr as Transition {pos, ...}) = position (Position.put_id id pos) tr;
wenzelm@27427
   566
wenzelm@27427
   567
wenzelm@25960
   568
(* thread position *)
wenzelm@25799
   569
wenzelm@25960
   570
fun setmp_thread_position (Transition {pos, ...}) f x =
wenzelm@25819
   571
  Position.setmp_thread_data pos f x;
wenzelm@25799
   572
wenzelm@27606
   573
fun status tr m =
wenzelm@43665
   574
  setmp_thread_position tr (fn () => Output.status (Markup.markup_only m)) ();
wenzelm@27606
   575
wenzelm@38876
   576
fun error_msg tr msg =
wenzelm@44270
   577
  setmp_thread_position tr (fn () => Output.error_msg' msg) ();
wenzelm@26602
   578
wenzelm@25799
   579
wenzelm@28095
   580
(* post-transition hooks *)
wenzelm@28095
   581
wenzelm@37905
   582
local
wenzelm@37905
   583
  val hooks = Unsynchronized.ref ([]: (transition -> state -> state -> unit) list);
wenzelm@37905
   584
in
wenzelm@28095
   585
wenzelm@32738
   586
fun add_hook f = CRITICAL (fn () => Unsynchronized.change hooks (cons f));
wenzelm@33223
   587
fun get_hooks () = ! hooks;
wenzelm@28095
   588
wenzelm@28095
   589
end;
wenzelm@28095
   590
wenzelm@28095
   591
wenzelm@5828
   592
(* apply transitions *)
wenzelm@5828
   593
wenzelm@6664
   594
local
wenzelm@6664
   595
wenzelm@32792
   596
fun app int (tr as Transition {trans, print, no_timing, ...}) =
wenzelm@25819
   597
  setmp_thread_position tr (fn state =>
wenzelm@25799
   598
    let
wenzelm@25799
   599
      fun do_timing f x = (warning (command_msg "" tr); timeap f x);
wenzelm@25799
   600
      fun do_profiling f x = profile (! profiling) f x;
wenzelm@25799
   601
wenzelm@26256
   602
      val (result, status) =
wenzelm@37905
   603
         state |>
wenzelm@37905
   604
          (apply_trans int trans
wenzelm@37905
   605
            |> (! profiling > 0 andalso not no_timing) ? do_profiling
wenzelm@37905
   606
            |> (! profiling > 0 orelse ! timing andalso not no_timing) ? do_timing);
wenzelm@26256
   607
wenzelm@26621
   608
      val _ = if int andalso not (! quiet) andalso print then print_state false result else ();
wenzelm@26256
   609
    in (result, Option.map (fn UNDEF => type_error tr state | exn => exn) status) end);
wenzelm@6664
   610
wenzelm@6664
   611
in
wenzelm@5828
   612
wenzelm@26602
   613
fun transition int tr st =
wenzelm@28095
   614
  let
wenzelm@28095
   615
    val hooks = get_hooks ();
wenzelm@28103
   616
    fun apply_hooks st' = hooks |> List.app (fn f => (try (fn () => f tr st st') (); ()));
wenzelm@28095
   617
wenzelm@28095
   618
    val ctxt = try context_of st;
wenzelm@28095
   619
    val res =
wenzelm@28095
   620
      (case app int tr st of
wenzelm@38888
   621
        (_, SOME Runtime.TERMINATE) => NONE
wenzelm@38888
   622
      | (st', SOME (Runtime.EXCURSION_FAIL exn_info)) => SOME (st', SOME exn_info)
wenzelm@31476
   623
      | (st', SOME exn) => SOME (st', SOME (Runtime.exn_context ctxt exn, at_command tr))
wenzelm@28103
   624
      | (st', NONE) => SOME (st', NONE));
wenzelm@28103
   625
    val _ = (case res of SOME (st', NONE) => apply_hooks st' | _ => ());
wenzelm@28095
   626
  in res end;
wenzelm@6664
   627
wenzelm@6664
   628
end;
wenzelm@5828
   629
wenzelm@5828
   630
wenzelm@28425
   631
(* nested commands *)
wenzelm@5828
   632
wenzelm@28425
   633
fun command tr st =
wenzelm@27576
   634
  (case transition (! interact) tr st of
wenzelm@28425
   635
    SOME (st', NONE) => st'
wenzelm@39285
   636
  | SOME (_, SOME (exn, info)) =>
wenzelm@39285
   637
      if Exn.is_interrupt exn then reraise exn else raise Runtime.EXCURSION_FAIL (exn, info)
wenzelm@38888
   638
  | NONE => raise Runtime.EXCURSION_FAIL (Runtime.TERMINATE, at_command tr));
wenzelm@27576
   639
wenzelm@29483
   640
fun command_result tr st =
wenzelm@29483
   641
  let val st' = command tr st
wenzelm@29483
   642
  in (st', st') end;
wenzelm@29483
   643
wenzelm@28433
   644
wenzelm@28433
   645
(* excursion of units, consisting of commands with proof *)
wenzelm@28433
   646
wenzelm@33519
   647
structure States = Proof_Data
wenzelm@28974
   648
(
wenzelm@28974
   649
  type T = state list future option;
wenzelm@28974
   650
  fun init _ = NONE;
wenzelm@28974
   651
);
wenzelm@28974
   652
wenzelm@28974
   653
fun proof_result immediate (tr, proof_trs) st =
wenzelm@28433
   654
  let val st' = command tr st in
wenzelm@36315
   655
    if immediate orelse
wenzelm@36315
   656
      null proof_trs orelse
wenzelm@36950
   657
      Keyword.is_schematic_goal (name_of tr) orelse
wenzelm@36950
   658
      exists (Keyword.is_qed_global o name_of) proof_trs orelse
wenzelm@36315
   659
      not (can proof_of st') orelse
wenzelm@36315
   660
      Proof.is_relevant (proof_of st')
wenzelm@28433
   661
    then
wenzelm@28453
   662
      let val (states, st'') = fold_map command_result proof_trs st'
wenzelm@42129
   663
      in (Future.value ((tr, st') :: (proof_trs ~~ states)), st'') end
wenzelm@28453
   664
    else
wenzelm@28433
   665
      let
wenzelm@28433
   666
        val (body_trs, end_tr) = split_last proof_trs;
wenzelm@42360
   667
        val finish = Context.Theory o Proof_Context.theory_of;
wenzelm@28974
   668
wenzelm@29386
   669
        val future_proof = Proof.global_future_proof
wenzelm@28974
   670
          (fn prf =>
wenzelm@41703
   671
            Goal.fork_name "Toplevel.future_proof"
wenzelm@41673
   672
              (fn () =>
wenzelm@41673
   673
                let val (states, result_state) =
wenzelm@41673
   674
                  (case st' of State (SOME (Proof (_, (_, orig_gthy))), prev)
wenzelm@41673
   675
                    => State (SOME (Proof (Proof_Node.init prf, (finish, orig_gthy))), prev))
wenzelm@41673
   676
                  |> fold_map command_result body_trs
wenzelm@41673
   677
                  ||> command (end_tr |> set_print false);
wenzelm@41673
   678
                in (states, presentation_context_of result_state) end))
wenzelm@28974
   679
          #> (fn (states, ctxt) => States.put (SOME states) ctxt);
wenzelm@28974
   680
wenzelm@28974
   681
        val st'' = st' |> command (end_tr |> reset_trans |> end_proof (K future_proof));
wenzelm@28974
   682
wenzelm@28974
   683
        val states =
wenzelm@30398
   684
          (case States.get (presentation_context_of st'') of
wenzelm@37852
   685
            NONE => raise Fail ("No future states for " ^ name_of tr ^ Position.str_of (pos_of tr))
wenzelm@28974
   686
          | SOME states => states);
wenzelm@42129
   687
        val result = states
wenzelm@42129
   688
          |> Future.map (fn sts => (tr, st') :: (body_trs ~~ sts) @ [(end_tr, st'')]);
wenzelm@28974
   689
wenzelm@28974
   690
      in (result, st'') end
wenzelm@28433
   691
  end;
wenzelm@28433
   692
wenzelm@29068
   693
fun excursion input =
wenzelm@28425
   694
  let
wenzelm@28433
   695
    val end_pos = if null input then error "No input" else pos_of (fst (List.last input));
wenzelm@29448
   696
    val immediate = not (Goal.future_enabled ());
wenzelm@29427
   697
    val (results, end_state) = fold_map (proof_result immediate) input toplevel;
wenzelm@37953
   698
    val thy = end_theory end_pos end_state;
wenzelm@42129
   699
  in (results, thy) end;
wenzelm@7062
   700
wenzelm@6664
   701
end;