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