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