src/Pure/Isar/proof.ML
author wenzelm
Sat Mar 30 13:40:19 2013 +0100 (2013-03-30)
changeset 51584 98029ceda8ce
parent 51553 63327f679cff
child 51605 eca8acb42e4a
permissions -rw-r--r--
more item markup;
tuned signature;
wenzelm@5820
     1
(*  Title:      Pure/Isar/proof.ML
wenzelm@5820
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5820
     3
wenzelm@19000
     4
The Isar/VM proof language interpreter: maintains a structured flow of
wenzelm@19000
     5
context elements, goals, refinements, and facts.
wenzelm@5820
     6
*)
wenzelm@5820
     7
wenzelm@5820
     8
signature PROOF =
wenzelm@5820
     9
sig
wenzelm@33031
    10
  type context = Proof.context
wenzelm@23639
    11
  type method = Method.method
wenzelm@5820
    12
  type state
wenzelm@17359
    13
  val init: context -> state
wenzelm@17359
    14
  val level: state -> int
wenzelm@17359
    15
  val assert_bottom: bool -> state -> state
wenzelm@5820
    16
  val context_of: state -> context
wenzelm@5820
    17
  val theory_of: state -> theory
wenzelm@13377
    18
  val map_context: (context -> context) -> state -> state
bulwahn@40642
    19
  val map_context_result : (context -> 'a * context) -> state -> 'a * state
wenzelm@28278
    20
  val map_contexts: (context -> context) -> state -> state
wenzelm@37949
    21
  val propagate_ml_env: state -> state
wenzelm@30757
    22
  val bind_terms: (indexname * term option) list -> state -> state
wenzelm@26251
    23
  val put_thms: bool -> string * thm list option -> state -> state
wenzelm@6091
    24
  val the_facts: state -> thm list
wenzelm@9469
    25
  val the_fact: state -> thm
wenzelm@47068
    26
  val set_facts: thm list -> state -> state
wenzelm@47068
    27
  val reset_facts: state -> state
wenzelm@6891
    28
  val assert_forward: state -> state
wenzelm@17359
    29
  val assert_chain: state -> state
wenzelm@9469
    30
  val assert_forward_or_chain: state -> state
wenzelm@5820
    31
  val assert_backward: state -> state
wenzelm@8206
    32
  val assert_no_chain: state -> state
wenzelm@5820
    33
  val enter_forward: state -> state
wenzelm@24543
    34
  val goal_message: (unit -> Pretty.T) -> state -> state
wenzelm@12423
    35
  val pretty_state: int -> state -> Pretty.T list
wenzelm@17112
    36
  val refine: Method.text -> state -> state Seq.seq
wenzelm@17112
    37
  val refine_end: Method.text -> state -> state Seq.seq
wenzelm@18908
    38
  val refine_insert: thm list -> state -> state
wenzelm@17359
    39
  val refine_goals: (context -> thm -> unit) -> context -> thm list -> state -> state Seq.seq
wenzelm@33288
    40
  val raw_goal: state -> {context: context, facts: thm list, goal: thm}
wenzelm@33288
    41
  val goal: state -> {context: context, facts: thm list, goal: thm}
wenzelm@33288
    42
  val simple_goal: state -> {context: context, goal: thm}
wenzelm@38721
    43
  val status_markup: state -> Markup.T
wenzelm@36323
    44
  val let_bind: (term list * term) list -> state -> state
wenzelm@36323
    45
  val let_bind_cmd: (string list * string) list -> state -> state
wenzelm@36505
    46
  val write: Syntax.mode -> (term * mixfix) list -> state -> state
wenzelm@36505
    47
  val write_cmd: Syntax.mode -> (string * mixfix) list -> state -> state
wenzelm@36323
    48
  val fix: (binding * typ option * mixfix) list -> state -> state
wenzelm@36323
    49
  val fix_cmd: (binding * string option * mixfix) list -> state -> state
wenzelm@20224
    50
  val assm: Assumption.export ->
wenzelm@36323
    51
    (Thm.binding * (term * term list) list) list -> state -> state
wenzelm@36323
    52
  val assm_cmd: Assumption.export ->
wenzelm@28084
    53
    (Attrib.binding * (string * string list) list) list -> state -> state
wenzelm@36323
    54
  val assume: (Thm.binding * (term * term list) list) list -> state -> state
wenzelm@36323
    55
  val assume_cmd: (Attrib.binding * (string * string list) list) list -> state -> state
wenzelm@36323
    56
  val presume: (Thm.binding * (term * term list) list) list -> state -> state
wenzelm@36323
    57
  val presume_cmd: (Attrib.binding * (string * string list) list) list -> state -> state
wenzelm@36323
    58
  val def: (Thm.binding * ((binding * mixfix) * (term * term list))) list -> state -> state
wenzelm@36323
    59
  val def_cmd: (Attrib.binding * ((binding * mixfix) * (string * string list))) list -> state -> state
wenzelm@17359
    60
  val chain: state -> state
wenzelm@17359
    61
  val chain_facts: thm list -> state -> state
wenzelm@36323
    62
  val note_thmss: (Thm.binding * (thm list * attribute list) list) list -> state -> state
wenzelm@36323
    63
  val note_thmss_cmd: (Attrib.binding * (Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@36323
    64
  val from_thmss: ((thm list * attribute list) list) list -> state -> state
wenzelm@36323
    65
  val from_thmss_cmd: ((Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@36323
    66
  val with_thmss: ((thm list * attribute list) list) list -> state -> state
wenzelm@36323
    67
  val with_thmss_cmd: ((Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@36323
    68
  val using: ((thm list * attribute list) list) list -> state -> state
wenzelm@36323
    69
  val using_cmd: ((Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@36323
    70
  val unfolding: ((thm list * attribute list) list) list -> state -> state
wenzelm@36323
    71
  val unfolding_cmd: ((Facts.ref * Attrib.src list) list) list -> state -> state
wenzelm@42496
    72
  val invoke_case: string * binding option list * attribute list -> state -> state
wenzelm@42496
    73
  val invoke_case_cmd: string * binding option list * Attrib.src list -> state -> state
wenzelm@17359
    74
  val begin_block: state -> state
wenzelm@17359
    75
  val next_block: state -> state
wenzelm@20309
    76
  val end_block: state -> state
wenzelm@49042
    77
  val begin_notepad: context -> state
wenzelm@49042
    78
  val end_notepad: state -> context
wenzelm@17112
    79
  val proof: Method.text option -> state -> state Seq.seq
wenzelm@49889
    80
  val proof_results: Method.text_range option -> state -> state Seq.result Seq.seq
wenzelm@49865
    81
  val defer: int -> state -> state
wenzelm@49865
    82
  val prefer: int -> state -> state
wenzelm@17112
    83
  val apply: Method.text -> state -> state Seq.seq
wenzelm@17112
    84
  val apply_end: Method.text -> state -> state Seq.seq
wenzelm@49889
    85
  val apply_results: Method.text_range -> state -> state Seq.result Seq.seq
wenzelm@49889
    86
  val apply_end_results: Method.text_range -> state -> state Seq.result Seq.seq
wenzelm@17359
    87
  val local_goal: (context -> ((string * string) * (string * thm list) list) -> unit) ->
wenzelm@49042
    88
    (context -> 'a -> attribute) ->
wenzelm@45327
    89
    ('b list -> context -> (term list list * (context -> context)) * context) ->
wenzelm@29383
    90
    string -> Method.text option -> (thm list list -> state -> state) ->
haftmann@29581
    91
    ((binding * 'a list) * 'b) list -> state -> state
wenzelm@49889
    92
  val local_qed: Method.text_range option * bool -> state -> state
wenzelm@21442
    93
  val theorem: Method.text option -> (thm list list -> context -> context) ->
wenzelm@36323
    94
    (term * term list) list list -> context -> state
wenzelm@36323
    95
  val theorem_cmd: Method.text option -> (thm list list -> context -> context) ->
wenzelm@21362
    96
    (string * string list) list list -> context -> state
wenzelm@49889
    97
  val global_qed: Method.text_range option * bool -> state -> context
wenzelm@49889
    98
  val local_terminal_proof: Method.text_range * Method.text_range option -> state -> state
wenzelm@29383
    99
  val local_default_proof: state -> state
wenzelm@29383
   100
  val local_immediate_proof: state -> state
wenzelm@29383
   101
  val local_skip_proof: bool -> state -> state
wenzelm@29383
   102
  val local_done_proof: state -> state
wenzelm@49889
   103
  val global_terminal_proof: Method.text_range * Method.text_range option -> state -> context
wenzelm@20363
   104
  val global_default_proof: state -> context
wenzelm@20363
   105
  val global_immediate_proof: state -> context
wenzelm@29383
   106
  val global_skip_proof: bool -> state -> context
wenzelm@20363
   107
  val global_done_proof: state -> context
wenzelm@29383
   108
  val have: Method.text option -> (thm list list -> state -> state) ->
wenzelm@36323
   109
    (Thm.binding * (term * term list) list) list -> bool -> state -> state
wenzelm@36323
   110
  val have_cmd: Method.text option -> (thm list list -> state -> state) ->
wenzelm@28084
   111
    (Attrib.binding * (string * string list) list) list -> bool -> state -> state
wenzelm@29383
   112
  val show: Method.text option -> (thm list list -> state -> state) ->
wenzelm@36323
   113
    (Thm.binding * (term * term list) list) list -> bool -> state -> state
wenzelm@36323
   114
  val show_cmd: Method.text option -> (thm list list -> state -> state) ->
wenzelm@28084
   115
    (Attrib.binding * (string * string list) list) list -> bool -> state -> state
wenzelm@29385
   116
  val schematic_goal: state -> bool
wenzelm@51226
   117
  val is_relevant: state -> bool
wenzelm@51318
   118
  val future_proof: (state -> ('a * context) future) -> state -> 'a future * state
wenzelm@49889
   119
  val local_future_terminal_proof: Method.text_range * Method.text_range option -> bool ->
wenzelm@49866
   120
    state -> state
wenzelm@49889
   121
  val global_future_terminal_proof: Method.text_range * Method.text_range option -> bool ->
wenzelm@49866
   122
    state -> context
wenzelm@5820
   123
end;
wenzelm@5820
   124
wenzelm@13377
   125
structure Proof: PROOF =
wenzelm@5820
   126
struct
wenzelm@5820
   127
wenzelm@33031
   128
type context = Proof.context;
wenzelm@17112
   129
type method = Method.method;
wenzelm@16813
   130
wenzelm@5820
   131
wenzelm@5820
   132
(** proof state **)
wenzelm@5820
   133
wenzelm@17359
   134
(* datatype state *)
wenzelm@5820
   135
wenzelm@17112
   136
datatype mode = Forward | Chain | Backward;
wenzelm@5820
   137
wenzelm@17359
   138
datatype state =
wenzelm@17359
   139
  State of node Stack.T
wenzelm@17359
   140
and node =
wenzelm@7176
   141
  Node of
wenzelm@7176
   142
   {context: context,
wenzelm@7176
   143
    facts: thm list option,
wenzelm@7176
   144
    mode: mode,
wenzelm@17359
   145
    goal: goal option}
wenzelm@17359
   146
and goal =
wenzelm@17359
   147
  Goal of
wenzelm@29346
   148
   {statement: (string * Position.T) * term list list * term,
wenzelm@28410
   149
      (*goal kind and statement (starting with vars), initial proposition*)
wenzelm@25958
   150
    messages: (unit -> Pretty.T) list,    (*persistent messages (hints etc.)*)
wenzelm@25958
   151
    using: thm list,                      (*goal facts*)
wenzelm@25958
   152
    goal: thm,                            (*subgoals ==> statement*)
wenzelm@17859
   153
    before_qed: Method.text option,
wenzelm@18124
   154
    after_qed:
wenzelm@29383
   155
      (thm list list -> state -> state) *
wenzelm@20363
   156
      (thm list list -> context -> context)};
wenzelm@17359
   157
wenzelm@24543
   158
fun make_goal (statement, messages, using, goal, before_qed, after_qed) =
wenzelm@24543
   159
  Goal {statement = statement, messages = messages, using = using, goal = goal,
wenzelm@17859
   160
    before_qed = before_qed, after_qed = after_qed};
wenzelm@5820
   161
wenzelm@7176
   162
fun make_node (context, facts, mode, goal) =
wenzelm@7176
   163
  Node {context = context, facts = facts, mode = mode, goal = goal};
wenzelm@7176
   164
wenzelm@17359
   165
fun map_node f (Node {context, facts, mode, goal}) =
wenzelm@17359
   166
  make_node (f (context, facts, mode, goal));
wenzelm@5820
   167
wenzelm@21727
   168
val init_context =
wenzelm@42360
   169
  Proof_Context.set_stmt true #>
wenzelm@47005
   170
  Proof_Context.map_naming (K Name_Space.local_naming);
wenzelm@21727
   171
wenzelm@21466
   172
fun init ctxt =
wenzelm@21727
   173
  State (Stack.init (make_node (init_context ctxt, NONE, Forward, NONE)));
wenzelm@5820
   174
wenzelm@49011
   175
fun top (State st) = Stack.top st |> (fn Node node => node);
wenzelm@49011
   176
fun map_top f (State st) = State (Stack.map_top (map_node f) st);
wenzelm@28278
   177
fun map_all f (State st) = State (Stack.map_all (map_node f) st);
wenzelm@12045
   178
wenzelm@5820
   179
wenzelm@5820
   180
wenzelm@5820
   181
(** basic proof state operations **)
wenzelm@5820
   182
wenzelm@17359
   183
(* block structure *)
wenzelm@17359
   184
wenzelm@17359
   185
fun open_block (State st) = State (Stack.push st);
wenzelm@17359
   186
wenzelm@18678
   187
fun close_block (State st) = State (Stack.pop st)
wenzelm@47060
   188
  handle List.Empty => error "Unbalanced block parentheses";
wenzelm@17359
   189
wenzelm@17359
   190
fun level (State st) = Stack.level st;
wenzelm@17359
   191
wenzelm@17359
   192
fun assert_bottom b state =
wenzelm@47065
   193
  let val b' = level state <= 2 in
wenzelm@47065
   194
    if b andalso not b' then error "Not at bottom of proof"
wenzelm@47065
   195
    else if not b andalso b' then error "Already at bottom of proof"
wenzelm@17359
   196
    else state
wenzelm@17359
   197
  end;
wenzelm@17359
   198
wenzelm@17359
   199
wenzelm@5820
   200
(* context *)
wenzelm@5820
   201
wenzelm@49011
   202
val context_of = #context o top;
wenzelm@42360
   203
val theory_of = Proof_Context.theory_of o context_of;
wenzelm@5820
   204
wenzelm@47431
   205
fun map_node_context f =
wenzelm@47431
   206
  map_node (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@47431
   207
wenzelm@17359
   208
fun map_context f =
wenzelm@49011
   209
  map_top (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@5820
   210
wenzelm@17359
   211
fun map_context_result f state =
wenzelm@17859
   212
  f (context_of state) ||> (fn ctxt => map_context (K ctxt) state);
wenzelm@5820
   213
wenzelm@28278
   214
fun map_contexts f = map_all (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal));
wenzelm@28278
   215
wenzelm@37949
   216
fun propagate_ml_env state = map_contexts
wenzelm@37949
   217
  (Context.proof_map (ML_Env.inherit (Context.Proof (context_of state)))) state;
wenzelm@37949
   218
wenzelm@42360
   219
val bind_terms = map_context o Proof_Context.bind_terms;
wenzelm@42360
   220
val put_thms = map_context oo Proof_Context.put_thms;
wenzelm@5820
   221
wenzelm@5820
   222
wenzelm@5820
   223
(* facts *)
wenzelm@5820
   224
wenzelm@49011
   225
val get_facts = #facts o top;
wenzelm@17359
   226
wenzelm@17359
   227
fun the_facts state =
wenzelm@17359
   228
  (case get_facts state of SOME facts => facts
wenzelm@18678
   229
  | NONE => error "No current facts available");
wenzelm@5820
   230
wenzelm@9469
   231
fun the_fact state =
wenzelm@17359
   232
  (case the_facts state of [thm] => thm
wenzelm@18678
   233
  | _ => error "Single theorem expected");
wenzelm@7605
   234
wenzelm@17359
   235
fun put_facts facts =
wenzelm@49011
   236
  map_top (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal)) #>
wenzelm@33386
   237
  put_thms true (Auto_Bind.thisN, facts);
wenzelm@5820
   238
wenzelm@47068
   239
val set_facts = put_facts o SOME;
wenzelm@47068
   240
val reset_facts = put_facts NONE;
wenzelm@47068
   241
wenzelm@17359
   242
fun these_factss more_facts (named_factss, state) =
wenzelm@47068
   243
  (named_factss, state |> set_facts (maps snd named_factss @ more_facts));
wenzelm@5820
   244
wenzelm@17359
   245
fun export_facts inner outer =
wenzelm@17359
   246
  (case get_facts inner of
wenzelm@47068
   247
    NONE => reset_facts outer
wenzelm@17359
   248
  | SOME thms =>
wenzelm@17359
   249
      thms
wenzelm@42360
   250
      |> Proof_Context.export (context_of inner) (context_of outer)
wenzelm@47068
   251
      |> (fn ths => set_facts ths outer));
wenzelm@5820
   252
wenzelm@5820
   253
wenzelm@5820
   254
(* mode *)
wenzelm@5820
   255
wenzelm@49011
   256
val get_mode = #mode o top;
wenzelm@49011
   257
fun put_mode mode = map_top (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal));
wenzelm@5820
   258
wenzelm@17359
   259
val mode_name = (fn Forward => "state" | Chain => "chain" | Backward => "prove");
wenzelm@5820
   260
wenzelm@5820
   261
fun assert_mode pred state =
wenzelm@5820
   262
  let val mode = get_mode state in
wenzelm@5820
   263
    if pred mode then state
wenzelm@18678
   264
    else error ("Illegal application of proof command in " ^ quote (mode_name mode) ^ " mode")
wenzelm@5820
   265
  end;
wenzelm@5820
   266
wenzelm@19308
   267
val assert_forward = assert_mode (fn mode => mode = Forward);
wenzelm@19308
   268
val assert_chain = assert_mode (fn mode => mode = Chain);
wenzelm@19308
   269
val assert_forward_or_chain = assert_mode (fn mode => mode = Forward orelse mode = Chain);
wenzelm@19308
   270
val assert_backward = assert_mode (fn mode => mode = Backward);
wenzelm@19308
   271
val assert_no_chain = assert_mode (fn mode => mode <> Chain);
wenzelm@5820
   272
wenzelm@17359
   273
val enter_forward = put_mode Forward;
wenzelm@17359
   274
val enter_chain = put_mode Chain;
wenzelm@17359
   275
val enter_backward = put_mode Backward;
wenzelm@5820
   276
wenzelm@17359
   277
wenzelm@17359
   278
(* current goal *)
wenzelm@17359
   279
wenzelm@17359
   280
fun current_goal state =
wenzelm@49011
   281
  (case top state of
wenzelm@17359
   282
    {context, goal = SOME (Goal goal), ...} => (context, goal)
wenzelm@47065
   283
  | _ => error "No current goal");
wenzelm@5820
   284
wenzelm@17359
   285
fun assert_current_goal g state =
wenzelm@17359
   286
  let val g' = can current_goal state in
wenzelm@47065
   287
    if g andalso not g' then error "No goal in this block"
wenzelm@47065
   288
    else if not g andalso g' then error "Goal present in this block"
wenzelm@17359
   289
    else state
wenzelm@17359
   290
  end;
wenzelm@6776
   291
wenzelm@49011
   292
fun put_goal goal = map_top (fn (ctxt, using, mode, _) => (ctxt, using, mode, goal));
wenzelm@17359
   293
wenzelm@47068
   294
val set_goal = put_goal o SOME;
wenzelm@47068
   295
val reset_goal = put_goal NONE;
wenzelm@47068
   296
wenzelm@17859
   297
val before_qed = #before_qed o #2 o current_goal;
wenzelm@17859
   298
wenzelm@17359
   299
wenzelm@17359
   300
(* nested goal *)
wenzelm@5820
   301
wenzelm@47431
   302
fun map_goal f g h (State (Node {context, facts, mode, goal = SOME goal}, node :: nodes)) =
wenzelm@17359
   303
      let
wenzelm@24543
   304
        val Goal {statement, messages, using, goal, before_qed, after_qed} = goal;
wenzelm@24543
   305
        val goal' = make_goal (g (statement, messages, using, goal, before_qed, after_qed));
wenzelm@47431
   306
        val node' = map_node_context h node;
wenzelm@47431
   307
      in State (make_node (f context, facts, mode, SOME goal'), node' :: nodes) end
wenzelm@47431
   308
  | map_goal f g h (State (nd, node :: nodes)) =
wenzelm@47431
   309
      let
wenzelm@47431
   310
        val nd' = map_node_context f nd;
wenzelm@47431
   311
        val State (node', nodes') = map_goal f g h (State (node, nodes));
wenzelm@47431
   312
      in State (nd', node' :: nodes') end
wenzelm@47431
   313
  | map_goal _ _ _ state = state;
wenzelm@5820
   314
wenzelm@47068
   315
fun provide_goal goal = map_goal I (fn (statement, _, using, _, before_qed, after_qed) =>
wenzelm@47431
   316
  (statement, [], using, goal, before_qed, after_qed)) I;
wenzelm@19188
   317
wenzelm@24543
   318
fun goal_message msg = map_goal I (fn (statement, messages, using, goal, before_qed, after_qed) =>
wenzelm@47431
   319
  (statement, msg :: messages, using, goal, before_qed, after_qed)) I;
wenzelm@24543
   320
wenzelm@24556
   321
fun using_facts using = map_goal I (fn (statement, _, _, goal, before_qed, after_qed) =>
wenzelm@47431
   322
  (statement, [], using, goal, before_qed, after_qed)) I;
wenzelm@17359
   323
wenzelm@17359
   324
local
wenzelm@17359
   325
  fun find i state =
wenzelm@17359
   326
    (case try current_goal state of
wenzelm@17359
   327
      SOME (ctxt, goal) => (ctxt, (i, goal))
wenzelm@18678
   328
    | NONE => find (i + 1) (close_block state handle ERROR _ => error "No goal present"));
wenzelm@17359
   329
in val find_goal = find 0 end;
wenzelm@17359
   330
wenzelm@17359
   331
fun get_goal state =
wenzelm@17359
   332
  let val (ctxt, (_, {using, goal, ...})) = find_goal state
wenzelm@17359
   333
  in (ctxt, (using, goal)) end;
wenzelm@5820
   334
wenzelm@5820
   335
wenzelm@5820
   336
wenzelm@12423
   337
(** pretty_state **)
wenzelm@5820
   338
skalberg@15531
   339
fun pretty_facts _ _ NONE = []
wenzelm@51584
   340
  | pretty_facts ctxt s (SOME ths) = [Proof_Display.pretty_goal_facts ctxt s ths, Pretty.str ""];
wenzelm@6756
   341
wenzelm@17359
   342
fun pretty_state nr state =
wenzelm@5820
   343
  let
wenzelm@49011
   344
    val {context = ctxt, facts, mode, goal = _} = top state;
wenzelm@42717
   345
    val verbose = Config.get ctxt Proof_Context.verbose;
wenzelm@5820
   346
wenzelm@49860
   347
    fun prt_goal (SOME (_, (_,
wenzelm@29416
   348
      {statement = ((_, pos), _, _), messages, using, goal, before_qed = _, after_qed = _}))) =
wenzelm@51584
   349
          pretty_facts ctxt "using"
wenzelm@17359
   350
            (if mode <> Backward orelse null using then NONE else SOME using) @
wenzelm@49860
   351
          [Proof_Display.pretty_goal_header goal] @ Goal_Display.pretty_goals ctxt goal @
wenzelm@25958
   352
          (map (fn msg => Position.setmp_thread_data pos msg ()) (rev messages))
wenzelm@17359
   353
      | prt_goal NONE = [];
wenzelm@6848
   354
wenzelm@17359
   355
    val prt_ctxt =
wenzelm@42717
   356
      if verbose orelse mode = Forward then Proof_Context.pretty_context ctxt
wenzelm@42360
   357
      else if mode = Backward then Proof_Context.pretty_ctxt ctxt
wenzelm@7575
   358
      else [];
wenzelm@17359
   359
  in
wenzelm@17359
   360
    [Pretty.str ("proof (" ^ mode_name mode ^ "): step " ^ string_of_int nr ^
wenzelm@42717
   361
      (if verbose then ", depth " ^ string_of_int (level state div 2 - 1) else "")),
wenzelm@17359
   362
      Pretty.str ""] @
wenzelm@17359
   363
    (if null prt_ctxt then [] else prt_ctxt @ [Pretty.str ""]) @
wenzelm@42717
   364
    (if verbose orelse mode = Forward then
wenzelm@51584
   365
       pretty_facts ctxt "" facts @ prt_goal (try find_goal state)
wenzelm@51584
   366
     else if mode = Chain then pretty_facts ctxt "picking" facts
wenzelm@17359
   367
     else prt_goal (try find_goal state))
wenzelm@17359
   368
  end;
wenzelm@5820
   369
wenzelm@5820
   370
wenzelm@5820
   371
wenzelm@5820
   372
(** proof steps **)
wenzelm@5820
   373
wenzelm@17359
   374
(* refine via method *)
wenzelm@5820
   375
wenzelm@8234
   376
local
wenzelm@8234
   377
wenzelm@16146
   378
fun goalN i = "goal" ^ string_of_int i;
wenzelm@16146
   379
fun goals st = map goalN (1 upto Thm.nprems_of st);
wenzelm@16146
   380
wenzelm@16146
   381
fun no_goal_cases st = map (rpair NONE) (goals st);
wenzelm@16146
   382
wenzelm@16146
   383
fun goal_cases st =
wenzelm@47815
   384
  Rule_Cases.make_common
wenzelm@47815
   385
    (Thm.theory_of_thm st, Thm.prop_of st) (map (rpair [] o rpair []) (goals st));
wenzelm@16146
   386
wenzelm@32193
   387
fun apply_method current_context meth state =
wenzelm@6848
   388
  let
wenzelm@24556
   389
    val (goal_ctxt, (_, {statement, messages = _, using, goal, before_qed, after_qed})) =
wenzelm@24543
   390
      find_goal state;
wenzelm@25958
   391
    val ctxt = if current_context then context_of state else goal_ctxt;
wenzelm@16146
   392
  in
wenzelm@32193
   393
    Method.apply meth ctxt using goal |> Seq.map (fn (meth_cases, goal') =>
wenzelm@6848
   394
      state
wenzelm@16146
   395
      |> map_goal
wenzelm@42360
   396
          (Proof_Context.add_cases false (no_goal_cases goal @ goal_cases goal') #>
wenzelm@42360
   397
           Proof_Context.add_cases true meth_cases)
wenzelm@47431
   398
          (K (statement, [], using, goal', before_qed, after_qed)) I)
wenzelm@16146
   399
  end;
wenzelm@5820
   400
wenzelm@19188
   401
fun select_goals n meth state =
wenzelm@51383
   402
  ALLGOALS Goal.conjunction_tac (#2 (#2 (get_goal state)))
wenzelm@19224
   403
  |> Seq.maps (fn goal =>
wenzelm@19188
   404
    state
wenzelm@47068
   405
    |> Seq.lift provide_goal (Goal.extract 1 n goal |> Seq.maps (Goal.conjunction_tac 1))
wenzelm@19188
   406
    |> Seq.maps meth
wenzelm@19188
   407
    |> Seq.maps (fn state' => state'
wenzelm@47068
   408
      |> Seq.lift provide_goal (Goal.retrofit 1 n (#2 (#2 (get_goal state'))) goal))
wenzelm@32193
   409
    |> Seq.maps (apply_method true (K Method.succeed)));
wenzelm@19188
   410
wenzelm@17112
   411
fun apply_text cc text state =
wenzelm@17112
   412
  let
wenzelm@17112
   413
    val thy = theory_of state;
wenzelm@17112
   414
wenzelm@32193
   415
    fun eval (Method.Basic m) = apply_method cc m
wenzelm@32193
   416
      | eval (Method.Source src) = apply_method cc (Method.method thy src)
wenzelm@32193
   417
      | eval (Method.Source_i src) = apply_method cc (Method.method_i thy src)
wenzelm@17112
   418
      | eval (Method.Then txts) = Seq.EVERY (map eval txts)
wenzelm@17112
   419
      | eval (Method.Orelse txts) = Seq.FIRST (map eval txts)
wenzelm@17112
   420
      | eval (Method.Try txt) = Seq.TRY (eval txt)
wenzelm@19188
   421
      | eval (Method.Repeat1 txt) = Seq.REPEAT1 (eval txt)
wenzelm@51383
   422
      | eval (Method.Select_Goals (n, txt)) = select_goals n (eval txt);
wenzelm@17112
   423
  in eval text state end;
wenzelm@17112
   424
wenzelm@8234
   425
in
wenzelm@8234
   426
wenzelm@17112
   427
val refine = apply_text true;
wenzelm@17112
   428
val refine_end = apply_text false;
wenzelm@32856
   429
fun refine_insert ths = Seq.hd o refine (Method.Basic (K (Method.insert ths)));
wenzelm@18908
   430
wenzelm@8234
   431
end;
wenzelm@8234
   432
wenzelm@5820
   433
wenzelm@17359
   434
(* refine via sub-proof *)
wenzelm@17359
   435
wenzelm@46466
   436
local
wenzelm@46466
   437
wenzelm@30557
   438
fun finish_tac 0 = K all_tac
wenzelm@30557
   439
  | finish_tac n =
wenzelm@30557
   440
      Goal.norm_hhf_tac THEN'
wenzelm@30557
   441
      SUBGOAL (fn (goal, i) =>
wenzelm@30557
   442
        if can Logic.unprotect (Logic.strip_assums_concl goal) then
wenzelm@30557
   443
          Tactic.etac Drule.protectI i THEN finish_tac (n - 1) i
wenzelm@30557
   444
        else finish_tac (n - 1) (i + 1));
wenzelm@30557
   445
wenzelm@20208
   446
fun goal_tac rule =
wenzelm@30557
   447
  Goal.norm_hhf_tac THEN'
wenzelm@30557
   448
  Tactic.rtac rule THEN'
wenzelm@30557
   449
  finish_tac (Thm.nprems_of rule);
wenzelm@11816
   450
wenzelm@46466
   451
fun FINDGOAL tac st =
wenzelm@46466
   452
  let fun find i n = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1) n)
wenzelm@46466
   453
  in find 1 (Thm.nprems_of st) st end;
wenzelm@46466
   454
wenzelm@46466
   455
in
wenzelm@46466
   456
wenzelm@19915
   457
fun refine_goals print_rule inner raw_rules state =
wenzelm@19915
   458
  let
wenzelm@19915
   459
    val (outer, (_, goal)) = get_goal state;
wenzelm@20208
   460
    fun refine rule st = (print_rule outer rule; FINDGOAL (goal_tac rule) st);
wenzelm@19915
   461
  in
wenzelm@19915
   462
    raw_rules
wenzelm@42360
   463
    |> Proof_Context.goal_export inner outer
wenzelm@47068
   464
    |> (fn rules => Seq.lift provide_goal (EVERY (map refine rules) goal) state)
wenzelm@19915
   465
  end;
wenzelm@17359
   466
wenzelm@46466
   467
end;
wenzelm@46466
   468
wenzelm@6932
   469
wenzelm@49846
   470
(* conclude goal *)
wenzelm@49846
   471
wenzelm@28627
   472
fun conclude_goal ctxt goal propss =
wenzelm@5820
   473
  let
wenzelm@42360
   474
    val thy = Proof_Context.theory_of ctxt;
wenzelm@24920
   475
    val string_of_term = Syntax.string_of_term ctxt;
wenzelm@32091
   476
    val string_of_thm = Display.string_of_thm ctxt;
wenzelm@5820
   477
wenzelm@49860
   478
    val _ = Thm.no_prems goal orelse error (Proof_Display.string_of_goal ctxt goal);
wenzelm@20224
   479
wenzelm@20224
   480
    val extra_hyps = Assumption.extra_hyps ctxt goal;
wenzelm@20224
   481
    val _ = null extra_hyps orelse
wenzelm@20224
   482
      error ("Additional hypotheses:\n" ^ cat_lines (map string_of_term extra_hyps));
wenzelm@5820
   483
wenzelm@23418
   484
    fun lost_structure () = error ("Lost goal structure:\n" ^ string_of_thm goal);
wenzelm@23418
   485
berghofe@23782
   486
    val th = Goal.conclude
wenzelm@23806
   487
      (if length (flat propss) > 1 then Thm.norm_proof goal else goal)
berghofe@23782
   488
      handle THM _ => lost_structure ();
wenzelm@23418
   489
    val goal_propss = filter_out null propss;
wenzelm@23418
   490
    val results =
wenzelm@23418
   491
      Conjunction.elim_balanced (length goal_propss) th
wenzelm@23418
   492
      |> map2 Conjunction.elim_balanced (map length goal_propss)
wenzelm@23418
   493
      handle THM _ => lost_structure ();
wenzelm@23418
   494
    val _ = Unify.matches_list thy (flat goal_propss) (map Thm.prop_of (flat results)) orelse
wenzelm@19774
   495
      error ("Proved a different theorem:\n" ^ string_of_thm th);
wenzelm@28627
   496
    val _ = Thm.check_shyps (Variable.sorts_of ctxt) th;
wenzelm@23418
   497
wenzelm@23418
   498
    fun recover_result ([] :: pss) thss = [] :: recover_result pss thss
wenzelm@23418
   499
      | recover_result (_ :: pss) (ths :: thss) = ths :: recover_result pss thss
wenzelm@23418
   500
      | recover_result [] [] = []
wenzelm@23418
   501
      | recover_result _ _ = lost_structure ();
wenzelm@23418
   502
  in recover_result propss results end;
wenzelm@5820
   503
wenzelm@49859
   504
val finished_goal_error = "Failed to finish proof";
wenzelm@49859
   505
wenzelm@49889
   506
fun finished_goal pos state =
wenzelm@49859
   507
  let val (ctxt, (_, goal)) = get_goal state in
wenzelm@49859
   508
    if Thm.no_prems goal then Seq.Result state
wenzelm@49889
   509
    else
wenzelm@49889
   510
      Seq.Error (fn () =>
wenzelm@49889
   511
        finished_goal_error ^ Position.here pos ^ ":\n" ^
wenzelm@49889
   512
          Proof_Display.string_of_goal ctxt goal)
wenzelm@49859
   513
  end;
wenzelm@49846
   514
wenzelm@5820
   515
wenzelm@33288
   516
(* goal views -- corresponding to methods *)
wenzelm@33288
   517
wenzelm@33288
   518
fun raw_goal state =
wenzelm@33288
   519
  let val (ctxt, (facts, goal)) = get_goal state
wenzelm@33288
   520
  in {context = ctxt, facts = facts, goal = goal} end;
wenzelm@33288
   521
wenzelm@33288
   522
val goal = raw_goal o refine_insert [];
wenzelm@33288
   523
wenzelm@33288
   524
fun simple_goal state =
wenzelm@33288
   525
  let
wenzelm@33288
   526
    val (_, (facts, _)) = get_goal state;
wenzelm@33288
   527
    val (ctxt, (_, goal)) = get_goal (refine_insert facts state);
wenzelm@33288
   528
  in {context = ctxt, goal = goal} end;
wenzelm@33288
   529
wenzelm@38721
   530
fun status_markup state =
wenzelm@38721
   531
  (case try goal state of
wenzelm@50201
   532
    SOME {goal, ...} => Markup.proof_state (Thm.nprems_of goal)
wenzelm@38721
   533
  | NONE => Markup.empty);
wenzelm@38721
   534
wenzelm@49866
   535
fun method_error kind pos state =
wenzelm@49866
   536
  Seq.single (Proof_Display.method_error kind pos (raw_goal state));
wenzelm@49861
   537
wenzelm@33288
   538
wenzelm@5820
   539
wenzelm@5820
   540
(*** structured proof commands ***)
wenzelm@5820
   541
wenzelm@17112
   542
(** context elements **)
wenzelm@5820
   543
wenzelm@36324
   544
(* let bindings *)
wenzelm@5820
   545
wenzelm@16813
   546
local
wenzelm@16813
   547
wenzelm@17359
   548
fun gen_bind bind args state =
wenzelm@5820
   549
  state
wenzelm@5820
   550
  |> assert_forward
wenzelm@36324
   551
  |> map_context (bind true args #> snd)
wenzelm@47068
   552
  |> reset_facts;
wenzelm@5820
   553
wenzelm@16813
   554
in
wenzelm@16813
   555
wenzelm@42360
   556
val let_bind = gen_bind Proof_Context.match_bind_i;
wenzelm@42360
   557
val let_bind_cmd = gen_bind Proof_Context.match_bind;
wenzelm@5820
   558
wenzelm@16813
   559
end;
wenzelm@16813
   560
wenzelm@5820
   561
wenzelm@36505
   562
(* concrete syntax *)
wenzelm@36505
   563
wenzelm@36505
   564
local
wenzelm@36505
   565
wenzelm@36507
   566
fun gen_write prep_arg mode args =
wenzelm@36507
   567
  assert_forward
wenzelm@42360
   568
  #> map_context (fn ctxt => ctxt |> Proof_Context.notation true mode (map (prep_arg ctxt) args))
wenzelm@47068
   569
  #> reset_facts;
wenzelm@36505
   570
wenzelm@36505
   571
in
wenzelm@36505
   572
wenzelm@36505
   573
val write = gen_write (K I);
wenzelm@36505
   574
wenzelm@36505
   575
val write_cmd =
wenzelm@36505
   576
  gen_write (fn ctxt => fn (c, mx) =>
wenzelm@42360
   577
    (Proof_Context.read_const ctxt false (Mixfix.mixfixT mx) c, mx));
wenzelm@36505
   578
wenzelm@36505
   579
end;
wenzelm@36505
   580
wenzelm@36505
   581
wenzelm@17359
   582
(* fix *)
wenzelm@9196
   583
wenzelm@12714
   584
local
wenzelm@12714
   585
wenzelm@30763
   586
fun gen_fix prep_vars args =
wenzelm@17359
   587
  assert_forward
wenzelm@45597
   588
  #> map_context (fn ctxt => snd (Proof_Context.add_fixes (fst (prep_vars args ctxt)) ctxt))
wenzelm@47068
   589
  #> reset_facts;
wenzelm@5820
   590
wenzelm@16813
   591
in
wenzelm@16813
   592
wenzelm@45597
   593
val fix = gen_fix Proof_Context.cert_vars;
wenzelm@45597
   594
val fix_cmd = gen_fix Proof_Context.read_vars;
wenzelm@5820
   595
wenzelm@16813
   596
end;
wenzelm@16813
   597
wenzelm@5820
   598
wenzelm@17359
   599
(* assume etc. *)
wenzelm@5820
   600
wenzelm@16813
   601
local
wenzelm@16813
   602
wenzelm@17112
   603
fun gen_assume asm prep_att exp args state =
wenzelm@5820
   604
  state
wenzelm@5820
   605
  |> assert_forward
wenzelm@47815
   606
  |> map_context_result (asm exp (Attrib.map_specs (map (prep_att (context_of state))) args))
wenzelm@17359
   607
  |> these_factss [] |> #2;
wenzelm@6932
   608
wenzelm@16813
   609
in
wenzelm@16813
   610
wenzelm@42360
   611
val assm = gen_assume Proof_Context.add_assms_i (K I);
wenzelm@47815
   612
val assm_cmd = gen_assume Proof_Context.add_assms Attrib.attribute_cmd;
wenzelm@36323
   613
val assume = assm Assumption.assume_export;
wenzelm@36323
   614
val assume_cmd = assm_cmd Assumption.assume_export;
wenzelm@36323
   615
val presume = assm Assumption.presume_export;
wenzelm@36323
   616
val presume_cmd = assm_cmd Assumption.presume_export;
wenzelm@5820
   617
wenzelm@16813
   618
end;
wenzelm@16813
   619
wenzelm@7271
   620
wenzelm@17359
   621
(* def *)
wenzelm@11891
   622
wenzelm@16813
   623
local
wenzelm@16813
   624
wenzelm@20913
   625
fun gen_def prep_att prep_vars prep_binds args state =
wenzelm@11891
   626
  let
wenzelm@11891
   627
    val _ = assert_forward state;
wenzelm@20913
   628
    val (raw_name_atts, (raw_vars, raw_rhss)) = args |> split_list ||> split_list;
wenzelm@47815
   629
    val name_atts = map (apsnd (map (prep_att (context_of state)))) raw_name_atts;
wenzelm@11891
   630
  in
wenzelm@20913
   631
    state
wenzelm@20913
   632
    |> map_context_result (prep_vars (map (fn (x, mx) => (x, NONE, mx)) raw_vars))
wenzelm@20913
   633
    |>> map (fn (x, _, mx) => (x, mx))
wenzelm@20913
   634
    |-> (fn vars =>
wenzelm@20913
   635
      map_context_result (prep_binds false (map swap raw_rhss))
wenzelm@49748
   636
      #-> (fn rhss =>
wenzelm@49748
   637
        let
wenzelm@49748
   638
          val defs = (vars ~~ (name_atts ~~ rhss)) |> map (fn ((x, mx), ((a, atts), rhs)) =>
wenzelm@49748
   639
            ((x, mx), ((Thm.def_binding_optional x a, atts), rhs)));
wenzelm@49748
   640
        in map_context_result (Local_Defs.add_defs defs) end))
wenzelm@47068
   641
    |-> (set_facts o map (#2 o #2))
wenzelm@11891
   642
  end;
wenzelm@11891
   643
wenzelm@16813
   644
in
wenzelm@16813
   645
wenzelm@42360
   646
val def = gen_def (K I) Proof_Context.cert_vars Proof_Context.match_bind_i;
wenzelm@47815
   647
val def_cmd = gen_def Attrib.attribute_cmd Proof_Context.read_vars Proof_Context.match_bind;
wenzelm@11891
   648
wenzelm@16813
   649
end;
wenzelm@16813
   650
wenzelm@11891
   651
wenzelm@8374
   652
wenzelm@17112
   653
(** facts **)
wenzelm@5820
   654
wenzelm@17359
   655
(* chain *)
wenzelm@5820
   656
wenzelm@24011
   657
fun clean_facts ctxt =
wenzelm@47068
   658
  set_facts (filter_out Thm.is_dummy (the_facts ctxt)) ctxt;
wenzelm@24011
   659
wenzelm@17359
   660
val chain =
wenzelm@17359
   661
  assert_forward
wenzelm@24011
   662
  #> clean_facts
wenzelm@17359
   663
  #> enter_chain;
wenzelm@5820
   664
wenzelm@17359
   665
fun chain_facts facts =
wenzelm@47068
   666
  set_facts facts
wenzelm@17359
   667
  #> chain;
wenzelm@5820
   668
wenzelm@5820
   669
wenzelm@17359
   670
(* note etc. *)
wenzelm@17112
   671
haftmann@28965
   672
fun no_binding args = map (pair (Binding.empty, [])) args;
wenzelm@17112
   673
wenzelm@17112
   674
local
wenzelm@17112
   675
wenzelm@30760
   676
fun gen_thmss more_facts opt_chain opt_result prep_atts prep_fact args state =
wenzelm@17112
   677
  state
wenzelm@17112
   678
  |> assert_forward
wenzelm@47815
   679
  |> map_context_result (fn ctxt => ctxt |> Proof_Context.note_thmss ""
wenzelm@47815
   680
    (Attrib.map_facts_refs (map (prep_atts ctxt)) (prep_fact ctxt) args))
wenzelm@17112
   681
  |> these_factss (more_facts state)
wenzelm@17359
   682
  ||> opt_chain
wenzelm@17359
   683
  |> opt_result;
wenzelm@17112
   684
wenzelm@17112
   685
in
wenzelm@17112
   686
wenzelm@36323
   687
val note_thmss = gen_thmss (K []) I #2 (K I) (K I);
wenzelm@47815
   688
val note_thmss_cmd = gen_thmss (K []) I #2 Attrib.attribute_cmd Proof_Context.get_fact;
wenzelm@17112
   689
wenzelm@36323
   690
val from_thmss = gen_thmss (K []) chain #2 (K I) (K I) o no_binding;
wenzelm@47815
   691
val from_thmss_cmd =
wenzelm@47815
   692
  gen_thmss (K []) chain #2 Attrib.attribute_cmd Proof_Context.get_fact o no_binding;
wenzelm@17359
   693
wenzelm@36323
   694
val with_thmss = gen_thmss the_facts chain #2 (K I) (K I) o no_binding;
wenzelm@47815
   695
val with_thmss_cmd =
wenzelm@47815
   696
  gen_thmss the_facts chain #2 Attrib.attribute_cmd Proof_Context.get_fact o no_binding;
wenzelm@30760
   697
wenzelm@30760
   698
val local_results = gen_thmss (K []) I I (K I) (K I) o map (apsnd Thm.simple_fact);
wenzelm@17359
   699
wenzelm@17112
   700
end;
wenzelm@17112
   701
wenzelm@17112
   702
wenzelm@18548
   703
(* using/unfolding *)
wenzelm@17112
   704
wenzelm@17112
   705
local
wenzelm@17112
   706
wenzelm@45390
   707
fun gen_using f g prep_att prep_fact args state =
wenzelm@17112
   708
  state
wenzelm@17112
   709
  |> assert_backward
wenzelm@21442
   710
  |> map_context_result
wenzelm@47815
   711
    (fn ctxt => ctxt |> Proof_Context.note_thmss ""
wenzelm@47815
   712
      (Attrib.map_facts_refs (map (prep_att ctxt)) (prep_fact ctxt) (no_binding args)))
wenzelm@18843
   713
  |> (fn (named_facts, state') =>
wenzelm@24556
   714
    state' |> map_goal I (fn (statement, _, using, goal, before_qed, after_qed) =>
wenzelm@18843
   715
      let
wenzelm@18843
   716
        val ctxt = context_of state';
wenzelm@19482
   717
        val ths = maps snd named_facts;
wenzelm@47431
   718
      in (statement, [], f ctxt ths using, g ctxt ths goal, before_qed, after_qed) end) I);
wenzelm@18548
   719
wenzelm@24050
   720
fun append_using _ ths using = using @ filter_out Thm.is_dummy ths;
wenzelm@35624
   721
fun unfold_using ctxt ths = map (Local_Defs.unfold ctxt ths);
wenzelm@35624
   722
val unfold_goals = Local_Defs.unfold_goals;
wenzelm@17112
   723
wenzelm@17112
   724
in
wenzelm@17112
   725
wenzelm@36323
   726
val using = gen_using append_using (K (K I)) (K I) (K I);
wenzelm@47815
   727
val using_cmd = gen_using append_using (K (K I)) Attrib.attribute_cmd Proof_Context.get_fact;
wenzelm@36323
   728
val unfolding = gen_using unfold_using unfold_goals (K I) (K I);
wenzelm@47815
   729
val unfolding_cmd = gen_using unfold_using unfold_goals Attrib.attribute_cmd Proof_Context.get_fact;
wenzelm@17112
   730
wenzelm@17112
   731
end;
wenzelm@17112
   732
wenzelm@17112
   733
wenzelm@17112
   734
(* case *)
wenzelm@17112
   735
wenzelm@17112
   736
local
wenzelm@17112
   737
wenzelm@30419
   738
fun qualified_binding a =
wenzelm@30419
   739
  Binding.qualify true (Long_Name.qualifier a) (Binding.name (Long_Name.base_name a));
wenzelm@30419
   740
wenzelm@17112
   741
fun gen_invoke_case prep_att (name, xs, raw_atts) state =
wenzelm@17112
   742
  let
wenzelm@47815
   743
    val atts = map (prep_att (context_of state)) raw_atts;
wenzelm@19078
   744
    val (asms, state') = state |> map_context_result (fn ctxt =>
wenzelm@42360
   745
      ctxt |> Proof_Context.apply_case (Proof_Context.get_case ctxt name xs));
wenzelm@30419
   746
    val assumptions = asms |> map (fn (a, ts) => ((qualified_binding a, atts), map (rpair []) ts));
wenzelm@17112
   747
  in
wenzelm@17112
   748
    state'
wenzelm@36323
   749
    |> assume assumptions
wenzelm@33386
   750
    |> bind_terms Auto_Bind.no_facts
wenzelm@36323
   751
    |> `the_facts |-> (fn thms => note_thmss [((Binding.name name, []), [(thms, [])])])
wenzelm@17112
   752
  end;
wenzelm@17112
   753
wenzelm@17112
   754
in
wenzelm@17112
   755
wenzelm@36323
   756
val invoke_case = gen_invoke_case (K I);
wenzelm@47815
   757
val invoke_case_cmd = gen_invoke_case Attrib.attribute_cmd;
wenzelm@17112
   758
wenzelm@17112
   759
end;
wenzelm@17112
   760
wenzelm@17112
   761
wenzelm@17112
   762
wenzelm@17359
   763
(** proof structure **)
wenzelm@17359
   764
wenzelm@17359
   765
(* blocks *)
wenzelm@17359
   766
wenzelm@17359
   767
val begin_block =
wenzelm@17359
   768
  assert_forward
wenzelm@17359
   769
  #> open_block
wenzelm@47068
   770
  #> reset_goal
wenzelm@17359
   771
  #> open_block;
wenzelm@17359
   772
wenzelm@17359
   773
val next_block =
wenzelm@17359
   774
  assert_forward
wenzelm@17359
   775
  #> close_block
wenzelm@17359
   776
  #> open_block
wenzelm@47068
   777
  #> reset_goal
wenzelm@47068
   778
  #> reset_facts;
wenzelm@17359
   779
wenzelm@17359
   780
fun end_block state =
wenzelm@17359
   781
  state
wenzelm@17359
   782
  |> assert_forward
wenzelm@40960
   783
  |> assert_bottom false
wenzelm@17359
   784
  |> close_block
wenzelm@17359
   785
  |> assert_current_goal false
wenzelm@17359
   786
  |> close_block
wenzelm@17359
   787
  |> export_facts state;
wenzelm@17359
   788
wenzelm@17359
   789
wenzelm@40960
   790
(* global notepad *)
wenzelm@40960
   791
wenzelm@40960
   792
val begin_notepad =
wenzelm@40960
   793
  init
wenzelm@40960
   794
  #> open_block
wenzelm@40960
   795
  #> map_context (Variable.set_body true)
wenzelm@40960
   796
  #> open_block;
wenzelm@40960
   797
wenzelm@40960
   798
val end_notepad =
wenzelm@40960
   799
  assert_forward
wenzelm@40960
   800
  #> assert_bottom true
wenzelm@40960
   801
  #> close_block
wenzelm@40960
   802
  #> assert_current_goal false
wenzelm@40960
   803
  #> close_block
wenzelm@40960
   804
  #> context_of;
wenzelm@40960
   805
wenzelm@40960
   806
wenzelm@17359
   807
(* sub-proofs *)
wenzelm@17359
   808
wenzelm@17359
   809
fun proof opt_text =
wenzelm@17359
   810
  assert_backward
wenzelm@17859
   811
  #> refine (the_default Method.default_text opt_text)
wenzelm@17359
   812
  #> Seq.map (using_facts [] #> enter_forward);
wenzelm@17359
   813
wenzelm@49866
   814
fun proof_results arg =
wenzelm@49866
   815
  Seq.APPEND (proof (Method.text arg) #> Seq.make_results,
wenzelm@49866
   816
    method_error "initial" (Method.position arg));
wenzelm@49863
   817
wenzelm@49889
   818
fun end_proof bot (prev_pos, (opt_text, immed)) =
wenzelm@49889
   819
  let
wenzelm@49889
   820
    val (finish_text, terminal_pos, finished_pos) =
wenzelm@49889
   821
      (case opt_text of
wenzelm@49889
   822
        NONE => (Method.finish_text (NONE, immed), Position.none, prev_pos)
wenzelm@50912
   823
      | SOME (text, (pos, end_pos)) =>
wenzelm@50912
   824
          (Method.finish_text (SOME text, immed), Position.set_range (pos, end_pos), end_pos));
wenzelm@49889
   825
  in
wenzelm@49889
   826
    Seq.APPEND (fn state =>
wenzelm@49889
   827
      state
wenzelm@49889
   828
      |> assert_forward
wenzelm@49889
   829
      |> assert_bottom bot
wenzelm@49889
   830
      |> close_block
wenzelm@49889
   831
      |> assert_current_goal true
wenzelm@49889
   832
      |> using_facts []
wenzelm@49889
   833
      |> `before_qed |-> (refine o the_default Method.succeed_text)
wenzelm@49889
   834
      |> Seq.maps (refine finish_text)
wenzelm@49889
   835
      |> Seq.make_results, method_error "terminal" terminal_pos)
wenzelm@49889
   836
    #> Seq.maps_results (Seq.single o finished_goal finished_pos)
wenzelm@49889
   837
  end;
wenzelm@17359
   838
wenzelm@29383
   839
fun check_result msg sq =
wenzelm@29383
   840
  (case Seq.pull sq of
wenzelm@29383
   841
    NONE => error msg
wenzelm@29383
   842
  | SOME (s, _) => s);
wenzelm@29383
   843
wenzelm@17359
   844
wenzelm@17359
   845
(* unstructured refinement *)
wenzelm@17359
   846
wenzelm@49865
   847
fun defer i =
wenzelm@49865
   848
  assert_no_chain #>
wenzelm@49865
   849
  refine (Method.Basic (fn _ => METHOD (fn _ => ASSERT_SUBGOAL defer_tac i))) #> Seq.hd;
wenzelm@49865
   850
wenzelm@49865
   851
fun prefer i =
wenzelm@49865
   852
  assert_no_chain #>
wenzelm@49865
   853
  refine (Method.Basic (fn _ => METHOD (fn _ => ASSERT_SUBGOAL prefer_tac i))) #> Seq.hd;
wenzelm@17359
   854
wenzelm@17359
   855
fun apply text = assert_backward #> refine text #> Seq.map (using_facts []);
wenzelm@49866
   856
wenzelm@17359
   857
fun apply_end text = assert_forward #> refine_end text;
wenzelm@17359
   858
wenzelm@49889
   859
fun apply_results (text, range) =
wenzelm@49889
   860
  Seq.APPEND (apply text #> Seq.make_results, method_error "" (Position.set_range range));
wenzelm@49866
   861
wenzelm@49889
   862
fun apply_end_results (text, range) =
wenzelm@49889
   863
  Seq.APPEND (apply_end text #> Seq.make_results, method_error "" (Position.set_range range));
wenzelm@49863
   864
wenzelm@17359
   865
wenzelm@17359
   866
wenzelm@17112
   867
(** goals **)
wenzelm@17112
   868
wenzelm@17359
   869
(* generic goals *)
wenzelm@17359
   870
wenzelm@19774
   871
local
wenzelm@19774
   872
wenzelm@36322
   873
val is_var =
wenzelm@36322
   874
  can (dest_TVar o Logic.dest_type o Logic.dest_term) orf
wenzelm@36322
   875
  can (dest_Var o Logic.dest_term);
wenzelm@36322
   876
wenzelm@36322
   877
fun implicit_vars props =
wenzelm@19846
   878
  let
wenzelm@36354
   879
    val (var_props, _) = take_prefix is_var props;
wenzelm@36322
   880
    val explicit_vars = fold Term.add_vars var_props [];
wenzelm@36322
   881
    val vars = filter_out (member (op =) explicit_vars) (fold Term.add_vars props []);
wenzelm@36322
   882
  in map (Logic.mk_term o Var) vars end;
wenzelm@19774
   883
wenzelm@19774
   884
fun refine_terms n =
wenzelm@30510
   885
  refine (Method.Basic (K (RAW_METHOD
wenzelm@19774
   886
    (K (HEADGOAL (PRECISE_CONJUNCTS n
wenzelm@32193
   887
      (HEADGOAL (CONJUNCTS (ALLGOALS (rtac Drule.termI))))))))))
wenzelm@19774
   888
  #> Seq.hd;
wenzelm@19774
   889
wenzelm@19774
   890
in
wenzelm@17359
   891
wenzelm@17859
   892
fun generic_goal prepp kind before_qed after_qed raw_propp state =
wenzelm@5820
   893
  let
wenzelm@17359
   894
    val thy = theory_of state;
wenzelm@23418
   895
    val cert = Thm.cterm_of thy;
wenzelm@17359
   896
    val chaining = can assert_chain state;
wenzelm@25958
   897
    val pos = Position.thread_data ();
wenzelm@17359
   898
wenzelm@17359
   899
    val ((propss, after_ctxt), goal_state) =
wenzelm@5936
   900
      state
wenzelm@5936
   901
      |> assert_forward_or_chain
wenzelm@5936
   902
      |> enter_forward
wenzelm@17359
   903
      |> open_block
wenzelm@45327
   904
      |> map_context_result (prepp raw_propp);
wenzelm@19482
   905
    val props = flat propss;
wenzelm@15703
   906
wenzelm@36322
   907
    val vars = implicit_vars props;
wenzelm@36322
   908
    val propss' = vars :: propss;
wenzelm@23418
   909
    val goal_propss = filter_out null propss';
wenzelm@29346
   910
    val goal =
wenzelm@29346
   911
      cert (Logic.mk_conjunction_balanced (map Logic.mk_conjunction_balanced goal_propss))
wenzelm@28627
   912
      |> Thm.weaken_sorts (Variable.sorts_of (context_of goal_state));
wenzelm@29346
   913
    val statement = ((kind, pos), propss', Thm.term_of goal);
wenzelm@18124
   914
    val after_qed' = after_qed |>> (fn after_local =>
wenzelm@18124
   915
      fn results => map_context after_ctxt #> after_local results);
wenzelm@5820
   916
  in
wenzelm@17359
   917
    goal_state
wenzelm@21727
   918
    |> map_context (init_context #> Variable.set_body true)
wenzelm@47068
   919
    |> set_goal (make_goal (statement, [], [], Goal.init goal, before_qed, after_qed'))
wenzelm@42360
   920
    |> map_context (Proof_Context.auto_bind_goal props)
wenzelm@21565
   921
    |> chaining ? (`the_facts #-> using_facts)
wenzelm@47068
   922
    |> reset_facts
wenzelm@17359
   923
    |> open_block
wenzelm@47068
   924
    |> reset_goal
wenzelm@5820
   925
    |> enter_backward
wenzelm@23418
   926
    |> not (null vars) ? refine_terms (length goal_propss)
wenzelm@32193
   927
    |> null props ? (refine (Method.Basic Method.assumption) #> Seq.hd)
wenzelm@5820
   928
  end;
wenzelm@5820
   929
wenzelm@29090
   930
fun generic_qed after_ctxt state =
wenzelm@12503
   931
  let
wenzelm@36354
   932
    val (goal_ctxt, {statement = (_, stmt, _), goal, after_qed, ...}) = current_goal state;
wenzelm@8617
   933
    val outer_state = state |> close_block;
wenzelm@8617
   934
    val outer_ctxt = context_of outer_state;
wenzelm@8617
   935
wenzelm@18503
   936
    val props =
wenzelm@19774
   937
      flat (tl stmt)
wenzelm@19915
   938
      |> Variable.exportT_terms goal_ctxt outer_ctxt;
wenzelm@17359
   939
    val results =
wenzelm@28627
   940
      tl (conclude_goal goal_ctxt goal stmt)
wenzelm@42360
   941
      |> burrow (Proof_Context.export goal_ctxt outer_ctxt);
wenzelm@17359
   942
  in
wenzelm@17359
   943
    outer_state
wenzelm@29090
   944
    |> map_context (after_ctxt props)
wenzelm@34239
   945
    |> pair (after_qed, results)
wenzelm@17359
   946
  end;
wenzelm@17359
   947
wenzelm@19774
   948
end;
wenzelm@19774
   949
wenzelm@9469
   950
wenzelm@17359
   951
(* local goals *)
wenzelm@17359
   952
wenzelm@17859
   953
fun local_goal print_results prep_att prepp kind before_qed after_qed stmt state =
wenzelm@17359
   954
  let
wenzelm@21362
   955
    val ((names, attss), propp) =
wenzelm@47815
   956
      Attrib.map_specs (map (prep_att (context_of state))) stmt |> split_list |>> split_list;
wenzelm@17359
   957
wenzelm@18124
   958
    fun after_qed' results =
wenzelm@18808
   959
      local_results ((names ~~ attss) ~~ results)
wenzelm@17359
   960
      #-> (fn res => tap (fn st => print_results (context_of st) ((kind, ""), res) : unit))
wenzelm@18124
   961
      #> after_qed results;
wenzelm@5820
   962
  in
wenzelm@17359
   963
    state
wenzelm@21362
   964
    |> generic_goal prepp kind before_qed (after_qed', K I) propp
wenzelm@19900
   965
    |> tap (Variable.warn_extra_tfrees (context_of state) o context_of)
wenzelm@5820
   966
  end;
wenzelm@5820
   967
wenzelm@49866
   968
fun local_qeds arg =
wenzelm@49866
   969
  end_proof false arg
wenzelm@49859
   970
  #> Seq.map_result (generic_qed Proof_Context.auto_bind_facts #->
wenzelm@49859
   971
    (fn ((after_qed, _), results) => after_qed results));
wenzelm@5820
   972
wenzelm@49889
   973
fun local_qed arg =
wenzelm@49889
   974
  local_qeds (Position.none, arg) #> Seq.the_result finished_goal_error;
wenzelm@29383
   975
wenzelm@5820
   976
wenzelm@17359
   977
(* global goals *)
wenzelm@17359
   978
wenzelm@45327
   979
fun prepp_auto_fixes prepp args =
wenzelm@45327
   980
  prepp args #>
wenzelm@45327
   981
  (fn ((propss, a), ctxt) => ((propss, a), (fold o fold) Variable.auto_fixes propss ctxt));
wenzelm@45327
   982
wenzelm@45327
   983
fun global_goal prepp before_qed after_qed propp =
wenzelm@45327
   984
  init #>
wenzelm@45327
   985
  generic_goal (prepp_auto_fixes prepp) "" before_qed (K I, after_qed) propp;
wenzelm@17359
   986
wenzelm@42360
   987
val theorem = global_goal Proof_Context.bind_propp_schematic_i;
wenzelm@42360
   988
val theorem_cmd = global_goal Proof_Context.bind_propp_schematic;
wenzelm@12065
   989
wenzelm@49866
   990
fun global_qeds arg =
wenzelm@49866
   991
  end_proof true arg
wenzelm@49859
   992
  #> Seq.map_result (generic_qed (K I) #> (fn (((_, after_qed), results), state) =>
wenzelm@49859
   993
    after_qed results (context_of state)));
wenzelm@17112
   994
wenzelm@49889
   995
fun global_qed arg =
wenzelm@49889
   996
  global_qeds (Position.none, arg) #> Seq.the_result finished_goal_error;
wenzelm@12959
   997
wenzelm@5820
   998
wenzelm@49907
   999
(* terminal proof steps *)
wenzelm@17112
  1000
wenzelm@49890
  1001
local
wenzelm@49890
  1002
wenzelm@49846
  1003
fun terminal_proof qeds initial terminal =
wenzelm@49889
  1004
  proof_results (SOME initial) #> Seq.maps_results (qeds (#2 (#2 initial), terminal))
wenzelm@49863
  1005
  #> Seq.the_result "";
wenzelm@49848
  1006
wenzelm@49890
  1007
in
wenzelm@49890
  1008
wenzelm@29383
  1009
fun local_terminal_proof (text, opt_text) = terminal_proof local_qeds text (opt_text, true);
wenzelm@49889
  1010
val local_default_proof = local_terminal_proof ((Method.default_text, Position.no_range), NONE);
wenzelm@49889
  1011
val local_immediate_proof = local_terminal_proof ((Method.this_text, Position.no_range), NONE);
wenzelm@49889
  1012
val local_done_proof = terminal_proof local_qeds (Method.done_text, Position.no_range) (NONE, false);
wenzelm@17112
  1013
wenzelm@29383
  1014
fun global_terminal_proof (text, opt_text) = terminal_proof global_qeds text (opt_text, true);
wenzelm@49889
  1015
val global_default_proof = global_terminal_proof ((Method.default_text, Position.no_range), NONE);
wenzelm@49889
  1016
val global_immediate_proof = global_terminal_proof ((Method.this_text, Position.no_range), NONE);
wenzelm@49889
  1017
val global_done_proof = terminal_proof global_qeds (Method.done_text, Position.no_range) (NONE, false);
wenzelm@5820
  1018
wenzelm@49890
  1019
end;
wenzelm@49890
  1020
wenzelm@6896
  1021
wenzelm@49907
  1022
(* skip proofs *)
wenzelm@49907
  1023
wenzelm@49907
  1024
fun local_skip_proof int state =
wenzelm@49907
  1025
  local_terminal_proof ((Method.sorry_text int, Position.no_range), NONE) state before
wenzelm@51551
  1026
  Skip_Proof.report (context_of state);
wenzelm@49907
  1027
wenzelm@49907
  1028
fun global_skip_proof int state =
wenzelm@49907
  1029
  global_terminal_proof ((Method.sorry_text int, Position.no_range), NONE) state before
wenzelm@51551
  1030
  Skip_Proof.report (context_of state);
wenzelm@49907
  1031
wenzelm@49907
  1032
wenzelm@17359
  1033
(* common goal statements *)
wenzelm@17359
  1034
wenzelm@17359
  1035
local
wenzelm@17359
  1036
wenzelm@17859
  1037
fun gen_have prep_att prepp before_qed after_qed stmt int =
wenzelm@50201
  1038
  local_goal (Proof_Display.print_results Markup.state int)
wenzelm@46728
  1039
    prep_att prepp "have" before_qed after_qed stmt;
wenzelm@6896
  1040
wenzelm@17859
  1041
fun gen_show prep_att prepp before_qed after_qed stmt int state =
wenzelm@17359
  1042
  let
wenzelm@32738
  1043
    val testing = Unsynchronized.ref false;
wenzelm@32738
  1044
    val rule = Unsynchronized.ref (NONE: thm option);
wenzelm@17359
  1045
    fun fail_msg ctxt =
wenzelm@50315
  1046
      "Local statement fails to refine any pending goal" ::
wenzelm@33389
  1047
      (case ! rule of NONE => [] | SOME th => [Proof_Display.string_of_rule ctxt "Failed" th])
wenzelm@17359
  1048
      |> cat_lines;
wenzelm@6896
  1049
wenzelm@17359
  1050
    fun print_results ctxt res =
wenzelm@46728
  1051
      if ! testing then ()
wenzelm@50201
  1052
      else Proof_Display.print_results Markup.state int ctxt res;
wenzelm@17359
  1053
    fun print_rule ctxt th =
wenzelm@17359
  1054
      if ! testing then rule := SOME th
wenzelm@41181
  1055
      else if int then
wenzelm@50201
  1056
        writeln (Markup.markup Markup.state (Proof_Display.string_of_rule ctxt "Successful" th))
wenzelm@17359
  1057
      else ();
wenzelm@17359
  1058
    val test_proof =
wenzelm@50315
  1059
      local_skip_proof true
wenzelm@39616
  1060
      |> Unsynchronized.setmp testing true
wenzelm@42042
  1061
      |> Exn.interruptible_capture;
wenzelm@6896
  1062
wenzelm@18124
  1063
    fun after_qed' results =
wenzelm@19482
  1064
      refine_goals print_rule (context_of state) (flat results)
wenzelm@29383
  1065
      #> check_result "Failed to refine any pending goal"
wenzelm@29383
  1066
      #> after_qed results;
wenzelm@17359
  1067
  in
wenzelm@17359
  1068
    state
wenzelm@17859
  1069
    |> local_goal print_results prep_att prepp "show" before_qed after_qed' stmt
wenzelm@21565
  1070
    |> int ? (fn goal_state =>
wenzelm@49907
  1071
      (case test_proof (map_context (Context_Position.set_visible false) goal_state) of
wenzelm@50315
  1072
        Exn.Res _ => goal_state
wenzelm@42042
  1073
      | Exn.Exn exn => raise Exn.EXCEPTIONS ([exn, ERROR (fail_msg (context_of goal_state))])))
wenzelm@17359
  1074
  end;
wenzelm@17359
  1075
wenzelm@17359
  1076
in
wenzelm@17359
  1077
wenzelm@42360
  1078
val have = gen_have (K I) Proof_Context.bind_propp_i;
wenzelm@47815
  1079
val have_cmd = gen_have Attrib.attribute_cmd Proof_Context.bind_propp;
wenzelm@42360
  1080
val show = gen_show (K I) Proof_Context.bind_propp_i;
wenzelm@47815
  1081
val show_cmd = gen_show Attrib.attribute_cmd Proof_Context.bind_propp;
wenzelm@6896
  1082
wenzelm@5820
  1083
end;
wenzelm@17359
  1084
wenzelm@28410
  1085
wenzelm@29385
  1086
wenzelm@29385
  1087
(** future proofs **)
wenzelm@29385
  1088
wenzelm@29385
  1089
(* relevant proof states *)
wenzelm@29385
  1090
wenzelm@29385
  1091
fun schematic_goal state =
wenzelm@29385
  1092
  let val (_, (_, {statement = (_, _, prop), ...})) = find_goal state
wenzelm@51553
  1093
  in Goal.is_schematic prop end;
wenzelm@29385
  1094
wenzelm@29385
  1095
fun is_relevant state =
wenzelm@29385
  1096
  (case try find_goal state of
wenzelm@29385
  1097
    NONE => true
wenzelm@29385
  1098
  | SOME (_, (_, {statement = (_, _, prop), goal, ...})) =>
wenzelm@51553
  1099
      Goal.is_schematic prop orelse not (Logic.protect prop aconv Thm.concl_of goal));
wenzelm@29385
  1100
wenzelm@29385
  1101
wenzelm@29385
  1102
(* full proofs *)
wenzelm@28410
  1103
wenzelm@29346
  1104
local
wenzelm@29346
  1105
wenzelm@49912
  1106
structure Result = Proof_Data
wenzelm@49912
  1107
(
wenzelm@49912
  1108
  type T = thm option;
wenzelm@49912
  1109
  val empty = NONE;
wenzelm@49912
  1110
  fun init _ = empty;
wenzelm@49912
  1111
);
wenzelm@49912
  1112
wenzelm@49912
  1113
fun the_result ctxt =
wenzelm@49912
  1114
  (case Result.get ctxt of
wenzelm@49912
  1115
    NONE => error "No result of forked proof"
wenzelm@49912
  1116
  | SOME th => th);
wenzelm@49912
  1117
wenzelm@49912
  1118
val set_result = Result.put o SOME;
wenzelm@49912
  1119
val reset_result = Result.put NONE;
wenzelm@49912
  1120
wenzelm@51318
  1121
in
wenzelm@51318
  1122
wenzelm@51318
  1123
fun future_proof fork_proof state =
wenzelm@28410
  1124
  let
wenzelm@29381
  1125
    val _ = assert_backward state;
wenzelm@28410
  1126
    val (goal_ctxt, (_, goal)) = find_goal state;
wenzelm@32786
  1127
    val {statement as (kind, _, prop), messages, using, goal, before_qed, after_qed} = goal;
wenzelm@47431
  1128
    val goal_tfrees =
wenzelm@47431
  1129
      fold Term.add_tfrees
wenzelm@47431
  1130
        (prop :: map Thm.term_of (Assumption.all_assms_of goal_ctxt)) [];
wenzelm@28410
  1131
wenzelm@29383
  1132
    val _ = is_relevant state andalso error "Cannot fork relevant proof";
wenzelm@29383
  1133
wenzelm@29346
  1134
    val prop' = Logic.protect prop;
wenzelm@29346
  1135
    val statement' = (kind, [[], [prop']], prop');
wenzelm@29346
  1136
    val goal' = Thm.adjust_maxidx_thm (Thm.maxidx_of goal)
wenzelm@29346
  1137
      (Drule.comp_no_flatten (goal, Thm.nprems_of goal) 1 Drule.protectI);
wenzelm@49912
  1138
    val after_qed' = (fn [[th]] => map_context (set_result th), fn [[th]] => set_result th);
wenzelm@28410
  1139
wenzelm@28973
  1140
    val result_ctxt =
wenzelm@28973
  1141
      state
wenzelm@49912
  1142
      |> map_context reset_result
wenzelm@29385
  1143
      |> map_goal I (K (statement', messages, using, goal', before_qed, after_qed'))
wenzelm@47431
  1144
        (fold (Variable.declare_typ o TFree) goal_tfrees)
wenzelm@29346
  1145
      |> fork_proof;
wenzelm@28973
  1146
wenzelm@51318
  1147
    val future_thm = Future.map (the_result o snd) result_ctxt;
wenzelm@32062
  1148
    val finished_goal = Goal.future_result goal_ctxt future_thm prop';
wenzelm@28973
  1149
    val state' =
wenzelm@28973
  1150
      state
wenzelm@51318
  1151
      |> map_goal I (K (statement, messages, using, finished_goal, NONE, after_qed)) I;
wenzelm@51318
  1152
  in (Future.map fst result_ctxt, state') end;
wenzelm@29385
  1153
wenzelm@17359
  1154
end;
wenzelm@29346
  1155
wenzelm@29385
  1156
wenzelm@29385
  1157
(* terminal proofs *)
wenzelm@29385
  1158
wenzelm@29385
  1159
local
wenzelm@29385
  1160
wenzelm@51318
  1161
fun future_terminal_proof n proof1 proof2 done int state =
wenzelm@49012
  1162
  if (Goal.future_enabled_level 4 orelse Goal.future_enabled_nested n andalso not int)
wenzelm@49012
  1163
    andalso not (is_relevant state)
wenzelm@49012
  1164
  then
wenzelm@51318
  1165
    state |> future_proof (fn state' =>
wenzelm@51318
  1166
      Goal.fork_name "Proof.future_terminal_proof" ~1
wenzelm@51318
  1167
        (fn () => ((), proof2 state'))) |> snd |> done
wenzelm@51318
  1168
  else proof1 state;
wenzelm@29385
  1169
wenzelm@29385
  1170
in
wenzelm@29385
  1171
wenzelm@51318
  1172
fun local_future_terminal_proof meths =
wenzelm@51332
  1173
  future_terminal_proof 3
wenzelm@51318
  1174
    (local_terminal_proof meths)
wenzelm@51318
  1175
    (local_terminal_proof meths #> context_of) local_done_proof;
wenzelm@29385
  1176
wenzelm@51318
  1177
fun global_future_terminal_proof meths =
wenzelm@51318
  1178
  future_terminal_proof 3
wenzelm@51318
  1179
    (global_terminal_proof meths)
wenzelm@51318
  1180
    (global_terminal_proof meths) global_done_proof;
wenzelm@29350
  1181
wenzelm@29346
  1182
end;
wenzelm@29346
  1183
wenzelm@29385
  1184
end;
wenzelm@29385
  1185