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