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