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