| author | wenzelm | 
| Sun, 05 Jun 2005 23:07:24 +0200 | |
| changeset 16286 | 550d113ccd8f | 
| parent 16169 | b59202511b8a | 
| child 16450 | 66667013ca6e | 
| permissions | -rw-r--r-- | 
| 5820 | 1 | (* Title: Pure/Isar/proof.ML | 
| 2 | ID: $Id$ | |
| 3 | Author: Markus Wenzel, TU Muenchen | |
| 4 | ||
| 15757 | 5 | The Isar/VM proof language interpreter. | 
| 5820 | 6 | *) | 
| 7 | ||
| 15194 | 8 | |
| 9 | (*Jia Meng: added in atp_hook and modified enter_forward. *) | |
| 10 | ||
| 8152 | 11 | signature BASIC_PROOF = | 
| 12 | sig | |
| 8374 | 13 | val FINDGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq | 
| 14 | val HEADGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq | |
| 8152 | 15 | end; | 
| 16 | ||
| 5820 | 17 | signature PROOF = | 
| 18 | sig | |
| 8152 | 19 | include BASIC_PROOF | 
| 5820 | 20 | type context | 
| 21 | type state | |
| 14129 | 22 | datatype mode = Forward | ForwardChain | Backward | 
| 5820 | 23 | exception STATE of string * state | 
| 6871 | 24 | val check_result: string -> state -> 'a Seq.seq -> 'a Seq.seq | 
| 12045 | 25 | val init_state: theory -> state | 
| 5820 | 26 | val context_of: state -> context | 
| 27 | val theory_of: state -> theory | |
| 28 | val sign_of: state -> Sign.sg | |
| 13377 | 29 | val map_context: (context -> context) -> state -> state | 
| 7924 | 30 | val warn_extra_tfrees: state -> state -> state | 
| 14554 | 31 | val put_thms: string * thm list -> state -> state | 
| 7605 | 32 | val reset_thms: string -> state -> state | 
| 6091 | 33 | val the_facts: state -> thm list | 
| 9469 | 34 | val the_fact: state -> thm | 
| 11079 
a378479996f7
val get_goal: state -> context * (thm list * thm);
 wenzelm parents: 
10938diff
changeset | 35 | val get_goal: state -> context * (thm list * thm) | 
| 6091 | 36 | val goal_facts: (state -> thm list) -> state -> state | 
| 14129 | 37 | val get_mode: state -> mode | 
| 9469 | 38 | val is_chain: state -> bool | 
| 6891 | 39 | val assert_forward: state -> state | 
| 9469 | 40 | val assert_forward_or_chain: state -> state | 
| 5820 | 41 | val assert_backward: state -> state | 
| 8206 | 42 | val assert_no_chain: state -> state | 
| 15452 | 43 | val atp_hook: (ProofContext.context * Thm.thm -> unit) ref | 
| 5820 | 44 | val enter_forward: state -> state | 
| 6529 | 45 | val verbose: bool ref | 
| 13698 | 46 | val show_main_goal: bool ref | 
| 12423 | 47 | val pretty_state: int -> state -> Pretty.T list | 
| 10360 | 48 | val pretty_goals: bool -> state -> Pretty.T list | 
| 6776 | 49 | val level: state -> int | 
| 5820 | 50 | type method | 
| 6848 | 51 | val method: (thm list -> tactic) -> method | 
| 16146 | 52 | val method_cases: | 
| 53 | (thm list -> thm -> (thm * (string * RuleCases.T option) list) Seq.seq) -> method | |
| 5820 | 54 | val refine: (context -> method) -> state -> state Seq.seq | 
| 8234 | 55 | val refine_end: (context -> method) -> state -> state Seq.seq | 
| 5936 | 56 | val match_bind: (string list * string) list -> state -> state | 
| 57 | val match_bind_i: (term list * term) list -> state -> state | |
| 8617 
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8582diff
changeset | 58 | val let_bind: (string list * string) list -> state -> state | 
| 
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8582diff
changeset | 59 | val let_bind_i: (term list * term) list -> state -> state | 
| 14564 | 60 | val simple_note_thms: string -> thm list -> state -> state | 
| 61 | val note_thmss: ((bstring * context attribute list) * | |
| 15456 
956d6acacf89
Specific theorems in a named list of theorems can now be referred to
 berghofe parents: 
15452diff
changeset | 62 | (thmref * context attribute list) list) list -> state -> state | 
| 14564 | 63 | val note_thmss_i: ((bstring * context attribute list) * | 
| 9196 | 64 | (thm list * context attribute list) list) list -> state -> state | 
| 12714 | 65 | val with_thmss: ((bstring * context attribute list) * | 
| 15456 
956d6acacf89
Specific theorems in a named list of theorems can now be referred to
 berghofe parents: 
15452diff
changeset | 66 | (thmref * context attribute list) list) list -> state -> state | 
| 12714 | 67 | val with_thmss_i: ((bstring * context attribute list) * | 
| 9196 | 68 | (thm list * context attribute list) list) list -> state -> state | 
| 15456 
956d6acacf89
Specific theorems in a named list of theorems can now be referred to
 berghofe parents: 
15452diff
changeset | 69 | val using_thmss: ((thmref * context attribute list) list) list -> state -> state | 
| 12930 | 70 | val using_thmss_i: ((thm list * context attribute list) list) list -> state -> state | 
| 7412 | 71 | val fix: (string list * string option) list -> state -> state | 
| 7665 | 72 | val fix_i: (string list * typ option) list -> state -> state | 
| 9469 | 73 | val assm: ProofContext.exporter | 
| 10464 | 74 | -> ((string * context attribute list) * (string * (string list * string list)) list) list | 
| 7271 | 75 | -> state -> state | 
| 9469 | 76 | val assm_i: ProofContext.exporter | 
| 10464 | 77 | -> ((string * context attribute list) * (term * (term list * term list)) list) list | 
| 6932 | 78 | -> state -> state | 
| 10464 | 79 | val assume: | 
| 80 | ((string * context attribute list) * (string * (string list * string list)) list) list | |
| 7271 | 81 | -> state -> state | 
| 10464 | 82 | val assume_i: ((string * context attribute list) * (term * (term list * term list)) list) list | 
| 6932 | 83 | -> state -> state | 
| 10464 | 84 | val presume: | 
| 85 | ((string * context attribute list) * (string * (string list * string list)) list) list | |
| 6932 | 86 | -> state -> state | 
| 10464 | 87 | val presume_i: ((string * context attribute list) * (term * (term list * term list)) list) list | 
| 6932 | 88 | -> state -> state | 
| 11891 | 89 | val def: string -> context attribute list -> string * (string * string list) -> state -> state | 
| 90 | val def_i: string -> context attribute list -> string * (term * term list) -> state -> state | |
| 11793 
5f0ab6f5c280
support impromptu terminology of cases parameters;
 wenzelm parents: 
11742diff
changeset | 91 | val invoke_case: string * string option list * context attribute list -> state -> state | 
| 15703 | 92 | val multi_theorem: string -> (theory -> theory) -> | 
| 15696 | 93 | (cterm list -> context -> context -> thm -> thm) -> | 
| 15703 | 94 | (xstring * (Attrib.src list * Attrib.src list list)) option -> | 
| 95 | bstring * theory attribute list -> Locale.element Locale.elem_expr list | |
| 12146 | 96 | -> ((bstring * theory attribute list) * (string * (string list * string list)) list) list | 
| 97 | -> theory -> state | |
| 15703 | 98 | val multi_theorem_i: string -> (theory -> theory) -> | 
| 15696 | 99 | (cterm list -> context -> context -> thm -> thm) -> | 
| 15703 | 100 | (string * (Attrib.src list * Attrib.src list list)) option | 
| 12959 | 101 | -> bstring * theory attribute list | 
| 15703 | 102 | -> Locale.element_i Locale.elem_expr list | 
| 12146 | 103 | -> ((bstring * theory attribute list) * (term * (term list * term list)) list) list | 
| 104 | -> theory -> state | |
| 5820 | 105 | val chain: state -> state | 
| 6091 | 106 | val from_facts: thm list -> state -> state | 
| 12971 | 107 | val show: (bool -> state -> state) -> (state -> state Seq.seq) -> bool | 
| 12146 | 108 | -> ((string * context attribute list) * (string * (string list * string list)) list) list | 
| 109 | -> bool -> state -> state | |
| 12971 | 110 | val show_i: (bool -> state -> state) -> (state -> state Seq.seq) -> bool | 
| 12146 | 111 | -> ((string * context attribute list) * (term * (term list * term list)) list) list | 
| 112 | -> bool -> state -> state | |
| 12971 | 113 | val have: (state -> state Seq.seq) -> bool | 
| 12146 | 114 | -> ((string * context attribute list) * (string * (string list * string list)) list) list | 
| 115 | -> state -> state | |
| 12971 | 116 | val have_i: (state -> state Seq.seq) -> bool | 
| 12146 | 117 | -> ((string * context attribute list) * (term * (term list * term list)) list) list | 
| 118 | -> state -> state | |
| 6404 | 119 | val at_bottom: state -> bool | 
| 6982 | 120 | val local_qed: (state -> state Seq.seq) | 
| 12146 | 121 | -> (context -> string * (string * thm list) list -> unit) * (context -> thm -> unit) | 
| 122 | -> state -> state Seq.seq | |
| 6950 | 123 | val global_qed: (state -> state Seq.seq) -> state | 
| 12244 | 124 | -> (theory * ((string * string) * (string * thm list) list)) Seq.seq | 
| 6896 | 125 | val begin_block: state -> state | 
| 6932 | 126 | val end_block: state -> state Seq.seq | 
| 6896 | 127 | val next_block: state -> state | 
| 14548 | 128 | val thisN: string | 
| 15194 | 129 | val call_atp: bool ref; | 
| 5820 | 130 | end; | 
| 131 | ||
| 13377 | 132 | structure Proof: PROOF = | 
| 5820 | 133 | struct | 
| 134 | ||
| 15194 | 135 | (*Jia Meng: by default, no ATP is called. *) | 
| 136 | val call_atp = ref false; | |
| 5820 | 137 | |
| 138 | (** proof state **) | |
| 139 | ||
| 140 | type context = ProofContext.context; | |
| 141 | ||
| 142 | ||
| 143 | (* type goal *) | |
| 144 | ||
| 145 | datatype kind = | |
| 12959 | 146 |   Theorem of {kind: string,
 | 
| 147 | theory_spec: (bstring * theory attribute list) * theory attribute list list, | |
| 15703 | 148 | locale_spec: (string * (Attrib.src list * Attrib.src list list)) option, | 
| 149 | view: cterm list * cterm list, (* target view and includes view *) | |
| 150 | export: cterm list -> context -> context -> thm -> thm} | | |
| 15696 | 151 | (* exporter to be used in finish_global *) | 
| 12959 | 152 | Show of context attribute list list | | 
| 15703 | 153 | Have of context attribute list list; | 
| 15099 | 154 | |
| 13415 | 155 | fun kind_name _ (Theorem {kind = s, theory_spec = ((a, _), _),
 | 
| 15596 | 156 | locale_spec = NONE, ...}) = s ^ (if a = "" then "" else " " ^ a) | 
| 13399 
c136276dc847
support locale ``views'' (for cumulative predicates);
 wenzelm parents: 
13377diff
changeset | 157 |   | kind_name sg (Theorem {kind = s, theory_spec = ((a, _), _),
 | 
| 15596 | 158 | locale_spec = SOME (name, _), ...}) = | 
| 16146 | 159 | s ^ " (in " ^ Locale.extern sg name ^ ")" ^ (if a = "" then "" else " " ^ a) | 
| 12015 | 160 | | kind_name _ (Show _) = "show" | 
| 15703 | 161 | | kind_name _ (Have _) = "have"; | 
| 5820 | 162 | |
| 163 | type goal = | |
| 12146 | 164 | (kind * (*result kind*) | 
| 165 | string list * (*result names*) | |
| 166 | term list list) * (*result statements*) | |
| 167 | (thm list * (*use facts*) | |
| 168 | thm); (*goal: subgoals ==> statement*) | |
| 5820 | 169 | |
| 170 | ||
| 171 | (* type mode *) | |
| 172 | ||
| 173 | datatype mode = Forward | ForwardChain | Backward; | |
| 7201 | 174 | val mode_name = (fn Forward => "state" | ForwardChain => "chain" | Backward => "prove"); | 
| 5820 | 175 | |
| 176 | ||
| 177 | (* datatype state *) | |
| 178 | ||
| 7176 | 179 | datatype node = | 
| 180 | Node of | |
| 181 |    {context: context,
 | |
| 182 | facts: thm list option, | |
| 183 | mode: mode, | |
| 15703 | 184 | goal: (goal * (after_qed * bool)) option} | 
| 7176 | 185 | and state = | 
| 5820 | 186 | State of | 
| 187 | node * (*current*) | |
| 15703 | 188 | node list (*parents wrt. block structure*) | 
| 189 | and after_qed = | |
| 190 | AfterQed of (state -> state Seq.seq) * (theory -> theory); | |
| 5820 | 191 | |
| 7176 | 192 | fun make_node (context, facts, mode, goal) = | 
| 193 |   Node {context = context, facts = facts, mode = mode, goal = goal};
 | |
| 194 | ||
| 195 | ||
| 5820 | 196 | exception STATE of string * state; | 
| 197 | ||
| 198 | fun err_malformed name state = | |
| 199 | raise STATE (name ^ ": internal error -- malformed proof state", state); | |
| 200 | ||
| 6871 | 201 | fun check_result msg state sq = | 
| 202 | (case Seq.pull sq of | |
| 15531 | 203 | NONE => raise STATE (msg, state) | 
| 204 | | SOME s_sq => Seq.cons s_sq); | |
| 6871 | 205 | |
| 5820 | 206 | |
| 7176 | 207 | fun map_current f (State (Node {context, facts, mode, goal}, nodes)) =
 | 
| 5820 | 208 | State (make_node (f (context, facts, mode, goal)), nodes); | 
| 209 | ||
| 12045 | 210 | fun init_state thy = | 
| 15531 | 211 | State (make_node (ProofContext.init thy, NONE, Forward, NONE), []); | 
| 12045 | 212 | |
| 5820 | 213 | |
| 214 | ||
| 215 | (** basic proof state operations **) | |
| 216 | ||
| 217 | (* context *) | |
| 218 | ||
| 7176 | 219 | fun context_of (State (Node {context, ...}, _)) = context;
 | 
| 5820 | 220 | val theory_of = ProofContext.theory_of o context_of; | 
| 221 | val sign_of = ProofContext.sign_of o context_of; | |
| 222 | ||
| 223 | fun map_context f = map_current (fn (ctxt, facts, mode, goal) => (f ctxt, facts, mode, goal)); | |
| 224 | ||
| 7176 | 225 | fun map_context_result f (state as State (Node {context, facts, mode, goal}, nodes)) =
 | 
| 5820 | 226 | let val (context', result) = f context | 
| 227 | in (State (make_node (context', facts, mode, goal), nodes), result) end; | |
| 228 | ||
| 229 | ||
| 7924 | 230 | val warn_extra_tfrees = map_context o ProofContext.warn_extra_tfrees o context_of; | 
| 10809 | 231 | val add_binds_i = map_context o ProofContext.add_binds_i; | 
| 6790 | 232 | val auto_bind_goal = map_context o ProofContext.auto_bind_goal; | 
| 12146 | 233 | val auto_bind_facts = map_context o ProofContext.auto_bind_facts; | 
| 6091 | 234 | val put_thms = map_context o ProofContext.put_thms; | 
| 235 | val put_thmss = map_context o ProofContext.put_thmss; | |
| 7605 | 236 | val reset_thms = map_context o ProofContext.reset_thms; | 
| 8374 | 237 | val get_case = ProofContext.get_case o context_of; | 
| 5820 | 238 | |
| 239 | ||
| 240 | (* facts *) | |
| 241 | ||
| 15531 | 242 | fun the_facts (State (Node {facts = SOME facts, ...}, _)) = facts
 | 
| 5820 | 243 |   | the_facts state = raise STATE ("No current facts available", state);
 | 
| 244 | ||
| 9469 | 245 | fun the_fact state = | 
| 246 | (case the_facts state of | |
| 247 | [thm] => thm | |
| 248 |   | _ => raise STATE ("Single fact expected", state));
 | |
| 249 | ||
| 6848 | 250 | fun assert_facts state = (the_facts state; state); | 
| 7176 | 251 | fun get_facts (State (Node {facts, ...}, _)) = facts;
 | 
| 6848 | 252 | |
| 7605 | 253 | |
| 254 | val thisN = "this"; | |
| 255 | ||
| 5820 | 256 | fun put_facts facts state = | 
| 257 | state | |
| 258 | |> map_current (fn (ctxt, _, mode, goal) => (ctxt, facts, mode, goal)) | |
| 15531 | 259 | |> (case facts of NONE => reset_thms thisN | SOME ths => put_thms (thisN, ths)); | 
| 5820 | 260 | |
| 15531 | 261 | val reset_facts = put_facts NONE; | 
| 5820 | 262 | |
| 9196 | 263 | fun these_factss (state, named_factss) = | 
| 15570 | 264 | state |> put_facts (SOME (List.concat (map snd named_factss))); | 
| 5820 | 265 | |
| 266 | ||
| 267 | (* goal *) | |
| 268 | ||
| 10553 | 269 | local | 
| 15531 | 270 |   fun find i (state as State (Node {goal = SOME goal, ...}, _)) = (context_of state, (i, goal))
 | 
| 271 |     | find i (State (Node {goal = NONE, ...}, node :: nodes)) = find (i + 1) (State (node, nodes))
 | |
| 10553 | 272 | | find _ (state as State (_, [])) = err_malformed "find_goal" state; | 
| 273 | in val find_goal = find 0 end; | |
| 7176 | 274 | |
| 11079 
a378479996f7
val get_goal: state -> context * (thm list * thm);
 wenzelm parents: 
10938diff
changeset | 275 | fun get_goal state = | 
| 
a378479996f7
val get_goal: state -> context * (thm list * thm);
 wenzelm parents: 
10938diff
changeset | 276 | let val (ctxt, (_, ((_, x), _))) = find_goal state | 
| 
a378479996f7
val get_goal: state -> context * (thm list * thm);
 wenzelm parents: 
10938diff
changeset | 277 | in (ctxt, x) end; | 
| 
a378479996f7
val get_goal: state -> context * (thm list * thm);
 wenzelm parents: 
10938diff
changeset | 278 | |
| 15452 | 279 | |
| 280 | (* | |
| 281 | (* Jia: added here: get all goals from the state 10/01/05 *) | |
| 16146 | 282 | fun find_all i (state as State (Node {goal = SOME goal, ...}, node::nodes)) =
 | 
| 15452 | 283 | let val others = find_all (i+1) (State (node, nodes)) | 
| 284 | in | |
| 16146 | 285 | (context_of state, (i, goal)) :: others | 
| 15452 | 286 | end | 
| 15531 | 287 |   | find_all i (State (Node {goal = NONE, ...}, node :: nodes)) = find_all (i + 1) (State (node, nodes))
 | 
| 15452 | 288 | | find_all _ (state as State (_, [])) = []; | 
| 289 | ||
| 290 | val find_all_goals = find_all 0; | |
| 291 | ||
| 292 | fun find_all_goals_ctxts state = | |
| 293 | let val all_goals = find_all_goals state | |
| 16146 | 294 | fun find_ctxt_g [] = [] | 
| 295 | | find_ctxt_g ((ctxt, (_, ((_, (_, thm)), _)))::others) = (ctxt,thm) :: (find_ctxt_g others) | |
| 15452 | 296 | in | 
| 16146 | 297 | find_ctxt_g all_goals | 
| 15452 | 298 | end; | 
| 299 | *) | |
| 300 | ||
| 5820 | 301 | fun put_goal goal = map_current (fn (ctxt, facts, mode, _) => (ctxt, facts, mode, goal)); | 
| 302 | ||
| 15531 | 303 | fun map_goal f g (State (Node {context, facts, mode, goal = SOME goal}, nodes)) =
 | 
| 304 | State (make_node (f context, facts, mode, SOME (g goal)), nodes) | |
| 8374 | 305 | | map_goal f g (State (nd, node :: nodes)) = | 
| 306 | let val State (node', nodes') = map_goal f g (State (node, nodes)) | |
| 307 | in map_context f (State (nd, node' :: nodes')) end | |
| 308 | | map_goal _ _ state = state; | |
| 5820 | 309 | |
| 310 | fun goal_facts get state = | |
| 311 | state | |
| 8374 | 312 | |> map_goal I (fn ((result, (_, thm)), f) => ((result, (get state, thm)), f)); | 
| 5820 | 313 | |
| 314 | fun use_facts state = | |
| 315 | state | |
| 316 | |> goal_facts the_facts | |
| 317 | |> reset_facts; | |
| 318 | ||
| 319 | ||
| 320 | (* mode *) | |
| 321 | ||
| 7176 | 322 | fun get_mode (State (Node {mode, ...}, _)) = mode;
 | 
| 5820 | 323 | fun put_mode mode = map_current (fn (ctxt, facts, _, goal) => (ctxt, facts, mode, goal)); | 
| 324 | ||
| 15194 | 325 | val enter_forward_aux = put_mode Forward; | 
| 5820 | 326 | val enter_forward_chain = put_mode ForwardChain; | 
| 327 | val enter_backward = put_mode Backward; | |
| 328 | ||
| 329 | fun assert_mode pred state = | |
| 330 | let val mode = get_mode state in | |
| 331 | if pred mode then state | |
| 12010 | 332 |     else raise STATE ("Illegal application of proof command in "
 | 
| 333 | ^ quote (mode_name mode) ^ " mode", state) | |
| 5820 | 334 | end; | 
| 335 | ||
| 336 | fun is_chain state = get_mode state = ForwardChain; | |
| 337 | val assert_forward = assert_mode (equal Forward); | |
| 338 | val assert_forward_or_chain = assert_mode (equal Forward orf equal ForwardChain); | |
| 339 | val assert_backward = assert_mode (equal Backward); | |
| 8206 | 340 | val assert_no_chain = assert_mode (not_equal ForwardChain); | 
| 5820 | 341 | |
| 342 | ||
| 15194 | 343 | (*Jia Meng: a hook to be used for calling ATPs. *) | 
| 15452 | 344 | (* send the entire proof context *) | 
| 345 | val atp_hook = ref (fn ctxt_state: ProofContext.context * Thm.thm => ()); | |
| 15194 | 346 | |
| 347 | (*Jia Meng: the modified version of enter_forward. *) | |
| 348 | (*Jia Meng: atp: Proof.state -> unit *) | |
| 349 | local | |
| 16146 | 350 | fun atp state = | 
| 15452 | 351 | let val (ctxt, (_, ((_, (_, thm)), _))) = find_goal state | 
| 352 | in | |
| 16146 | 353 | (!atp_hook) (ctxt,thm) | 
| 15452 | 354 | end; | 
| 16146 | 355 | |
| 15194 | 356 | in | 
| 357 | ||
| 16146 | 358 | fun enter_forward state = | 
| 359 | if (!call_atp) then | |
| 15194 | 360 | ((atp state; enter_forward_aux state) | 
| 361 | handle (STATE _) => enter_forward_aux state) | |
| 362 | else (enter_forward_aux state); | |
| 363 | ||
| 364 | end; | |
| 365 | ||
| 366 | ||
| 5820 | 367 | (* blocks *) | 
| 368 | ||
| 6776 | 369 | fun level (State (_, nodes)) = length nodes; | 
| 370 | ||
| 5820 | 371 | fun open_block (State (node, nodes)) = State (node, node :: nodes); | 
| 372 | ||
| 373 | fun new_block state = | |
| 374 | state | |
| 375 | |> open_block | |
| 15531 | 376 | |> put_goal NONE; | 
| 5820 | 377 | |
| 7487 
c0f9b956a3e7
close_block: removed ProofContext.transfer_used_names;
 wenzelm parents: 
7478diff
changeset | 378 | fun close_block (state as State (_, node :: nodes)) = State (node, nodes) | 
| 5820 | 379 |   | close_block state = raise STATE ("Unbalanced block parentheses", state);
 | 
| 380 | ||
| 381 | ||
| 382 | ||
| 12423 | 383 | (** pretty_state **) | 
| 5820 | 384 | |
| 13869 | 385 | val show_main_goal = ref false; | 
| 13698 | 386 | |
| 6529 | 387 | val verbose = ProofContext.verbose; | 
| 388 | ||
| 14876 | 389 | val pretty_goals_local = Display.pretty_goals_aux o ProofContext.pp; | 
| 12085 | 390 | |
| 15531 | 391 | fun pretty_facts _ _ NONE = [] | 
| 392 | | pretty_facts s ctxt (SOME ths) = | |
| 12055 | 393 | [Pretty.big_list (s ^ "this:") (map (ProofContext.pretty_thm ctxt) ths), Pretty.str ""]; | 
| 6756 | 394 | |
| 12423 | 395 | fun pretty_state nr (state as State (Node {context = ctxt, facts, mode, goal = _}, nodes)) =
 | 
| 5820 | 396 | let | 
| 11891 | 397 | val ref (_, _, begin_goal) = Display.current_goals_markers; | 
| 5820 | 398 | |
| 399 | fun levels_up 0 = "" | |
| 7575 | 400 | | levels_up 1 = ", 1 level up" | 
| 401 | | levels_up i = ", " ^ string_of_int i ^ " levels up"; | |
| 5820 | 402 | |
| 11556 | 403 | fun subgoals 0 = "" | 
| 404 | | subgoals 1 = ", 1 subgoal" | |
| 405 | | subgoals n = ", " ^ string_of_int n ^ " subgoals"; | |
| 12146 | 406 | |
| 407 | fun prt_names names = | |
| 12242 
282a92bc5655
multi_theorem: common statement header (covers *all* results);
 wenzelm parents: 
12223diff
changeset | 408 | (case filter_out (equal "") names of [] => "" | 
| 15570 | 409 |       | nms => " " ^ enclose "(" ")" (space_implode " and " (Library.take (7, nms) @
 | 
| 12308 | 410 | (if length nms > 7 then ["..."] else [])))); | 
| 12146 | 411 | |
| 412 | fun prt_goal (_, (i, (((kind, names, _), (goal_facts, thm)), _))) = | |
| 12085 | 413 | pretty_facts "using " ctxt | 
| 15531 | 414 | (if mode <> Backward orelse null goal_facts then NONE else SOME goal_facts) @ | 
| 12146 | 415 |       [Pretty.str ("goal (" ^ kind_name (sign_of state) kind ^ prt_names names ^
 | 
| 416 | levels_up (i div 2) ^ subgoals (Thm.nprems_of thm) ^ "):")] @ | |
| 16146 | 417 | pretty_goals_local ctxt begin_goal (true, ! show_main_goal) (! Display.goals_limit) thm; | 
| 6848 | 418 | |
| 8462 | 419 | val ctxt_prts = | 
| 12085 | 420 | if ! verbose orelse mode = Forward then ProofContext.pretty_context ctxt | 
| 421 | else if mode = Backward then ProofContext.pretty_asms ctxt | |
| 7575 | 422 | else []; | 
| 8462 | 423 | |
| 424 | val prts = | |
| 8582 | 425 |      [Pretty.str ("proof (" ^ mode_name mode ^ "): step " ^ string_of_int nr ^
 | 
| 12010 | 426 | (if ! verbose then ", depth " ^ string_of_int (length nodes div 2 - 1) | 
| 8561 | 427 | else "")), Pretty.str ""] @ | 
| 8462 | 428 | (if null ctxt_prts then [] else ctxt_prts @ [Pretty.str ""]) @ | 
| 429 | (if ! verbose orelse mode = Forward then | |
| 12146 | 430 | (pretty_facts "" ctxt facts @ prt_goal (find_goal state)) | 
| 12085 | 431 | else if mode = ForwardChain then pretty_facts "picking " ctxt facts | 
| 12146 | 432 | else prt_goal (find_goal state)) | 
| 12423 | 433 | in prts end; | 
| 5820 | 434 | |
| 12085 | 435 | fun pretty_goals main state = | 
| 436 | let val (ctxt, (_, ((_, (_, thm)), _))) = find_goal state | |
| 437 | in pretty_goals_local ctxt "" (false, main) (! Display.goals_limit) thm end; | |
| 10320 | 438 | |
| 5820 | 439 | |
| 440 | ||
| 441 | (** proof steps **) | |
| 442 | ||
| 443 | (* datatype method *) | |
| 444 | ||
| 8374 | 445 | datatype method = | 
| 16146 | 446 | Method of thm list -> thm -> (thm * (string * RuleCases.T option) list) Seq.seq; | 
| 8374 | 447 | |
| 448 | fun method tac = Method (fn facts => fn st => Seq.map (rpair []) (tac facts st)); | |
| 449 | val method_cases = Method; | |
| 5820 | 450 | |
| 451 | ||
| 452 | (* refine goal *) | |
| 453 | ||
| 8234 | 454 | local | 
| 455 | ||
| 16146 | 456 | fun goalN i = "goal" ^ string_of_int i; | 
| 457 | fun goals st = map goalN (1 upto Thm.nprems_of st); | |
| 458 | ||
| 459 | fun no_goal_cases st = map (rpair NONE) (goals st); | |
| 460 | ||
| 461 | fun goal_cases st = | |
| 462 | RuleCases.make true NONE (Thm.sign_of_thm st, Thm.prop_of st) (goals st); | |
| 463 | ||
| 5820 | 464 | fun check_sign sg state = | 
| 465 | if Sign.subsig (sg, sign_of state) then state | |
| 466 |   else raise STATE ("Bad signature of result: " ^ Sign.str_of_sg sg, state);
 | |
| 467 | ||
| 8234 | 468 | fun gen_refine current_context meth_fun state = | 
| 6848 | 469 | let | 
| 16146 | 470 | val (goal_ctxt, (_, ((result, (facts, st)), x))) = find_goal state; | 
| 8234 | 471 | val Method meth = meth_fun (if current_context then context_of state else goal_ctxt); | 
| 16146 | 472 | in | 
| 473 | meth facts st |> Seq.map (fn (st', meth_cases) => | |
| 6848 | 474 | state | 
| 16146 | 475 | |> check_sign (Thm.sign_of_thm st') | 
| 476 | |> map_goal | |
| 477 | (ProofContext.add_cases (no_goal_cases st @ goal_cases st' @ meth_cases)) | |
| 478 | (K ((result, (facts, st')), x))) | |
| 479 | end; | |
| 5820 | 480 | |
| 8234 | 481 | in | 
| 482 | ||
| 483 | val refine = gen_refine true; | |
| 484 | val refine_end = gen_refine false; | |
| 485 | ||
| 486 | end; | |
| 487 | ||
| 5820 | 488 | |
| 11917 | 489 | (* goal addressing *) | 
| 6932 | 490 | |
| 8152 | 491 | fun FINDGOAL tac st = | 
| 8374 | 492 | let fun find (i, n) = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1, n)) | 
| 493 | in find (1, Thm.nprems_of st) st end; | |
| 8152 | 494 | |
| 8166 | 495 | fun HEADGOAL tac = tac 1; | 
| 496 | ||
| 9469 | 497 | |
| 498 | (* export results *) | |
| 499 | ||
| 11816 | 500 | fun refine_tac rule = | 
| 12703 
af5fec8a418f
refine_tac: Tactic.norm_hhf_tac before trying rule;
 wenzelm parents: 
12698diff
changeset | 501 | Tactic.norm_hhf_tac THEN' Tactic.rtac rule THEN_ALL_NEW (SUBGOAL (fn (goal, i) => | 
| 11816 | 502 | if can Logic.dest_goal (Logic.strip_assums_concl goal) then | 
| 503 | Tactic.etac Drule.triv_goal i | |
| 504 | else all_tac)); | |
| 505 | ||
| 12146 | 506 | fun export_goal inner print_rule raw_rule state = | 
| 6932 | 507 | let | 
| 12971 | 508 | val (outer, (_, ((result, (facts, thm)), x))) = find_goal state; | 
| 12010 | 509 | val exp_tac = ProofContext.export true inner outer; | 
| 9469 | 510 | fun exp_rule rule = | 
| 511 | let | |
| 12055 | 512 | val _ = print_rule outer rule; | 
| 11816 | 513 | val thmq = FINDGOAL (refine_tac rule) thm; | 
| 12971 | 514 | in Seq.map (fn th => map_goal I (K ((result, (facts, th)), x)) state) thmq end; | 
| 9469 | 515 | in raw_rule |> exp_tac |> (Seq.flat o Seq.map exp_rule) end; | 
| 6932 | 516 | |
| 12146 | 517 | fun export_goals inner print_rule raw_rules = | 
| 518 | Seq.EVERY (map (export_goal inner print_rule) raw_rules); | |
| 519 | ||
| 6932 | 520 | fun transfer_facts inner_state state = | 
| 521 | (case get_facts inner_state of | |
| 15531 | 522 | NONE => Seq.single (reset_facts state) | 
| 523 | | SOME thms => | |
| 8617 
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8582diff
changeset | 524 | let | 
| 12010 | 525 | val exp_tac = ProofContext.export false (context_of inner_state) (context_of state); | 
| 11816 | 526 | val thmqs = map exp_tac thms; | 
| 15531 | 527 | in Seq.map (fn ths => put_facts (SOME ths) state) (Seq.commute thmqs) end); | 
| 6932 | 528 | |
| 529 | ||
| 530 | (* prepare result *) | |
| 531 | ||
| 12146 | 532 | fun prep_result state ts raw_th = | 
| 5820 | 533 | let | 
| 534 | val ctxt = context_of state; | |
| 535 | fun err msg = raise STATE (msg, state); | |
| 536 | ||
| 12146 | 537 | val ngoals = Thm.nprems_of raw_th; | 
| 5820 | 538 | val _ = | 
| 539 | if ngoals = 0 then () | |
| 16146 | 540 | else (err (Pretty.string_of (Pretty.chunks | 
| 541 | (pretty_goals_local ctxt "" (true, !show_main_goal) (! Display.goals_limit) raw_th @ | |
| 12085 | 542 | [Pretty.str (string_of_int ngoals ^ " unsolved goal(s)!")])))); | 
| 5820 | 543 | |
| 12146 | 544 |     val {hyps, sign, ...} = Thm.rep_thm raw_th;
 | 
| 5820 | 545 | val tsig = Sign.tsig_of sign; | 
| 15570 | 546 | val casms = List.concat (map #1 (ProofContext.assumptions_of (context_of state))); | 
| 11816 | 547 | val bad_hyps = Library.gen_rems Term.aconv (hyps, map Thm.term_of casms); | 
| 12146 | 548 | |
| 549 | val th = raw_th RS Drule.rev_triv_goal; | |
| 550 | val ths = Drule.conj_elim_precise (length ts) th | |
| 551 | handle THM _ => err "Main goal structure corrupted"; | |
| 5820 | 552 | in | 
| 12146 | 553 |     conditional (not (null bad_hyps)) (fn () => err ("Additional hypotheses:\n" ^
 | 
| 554 | cat_lines (map (ProofContext.string_of_term ctxt) bad_hyps))); | |
| 12808 
a529b4b91888
RuleCases.make interface based on term instead of thm;
 wenzelm parents: 
12760diff
changeset | 555 | conditional (exists (not o Pattern.matches tsig) (ts ~~ map Thm.prop_of ths)) (fn () => | 
| 12146 | 556 |       err ("Proved a different theorem: " ^ Pretty.string_of (ProofContext.pretty_thm ctxt th)));
 | 
| 557 | ths | |
| 5820 | 558 | end; | 
| 559 | ||
| 560 | ||
| 561 | ||
| 562 | (*** structured proof commands ***) | |
| 563 | ||
| 564 | (** context **) | |
| 565 | ||
| 566 | (* bind *) | |
| 567 | ||
| 568 | fun gen_bind f x state = | |
| 569 | state | |
| 570 | |> assert_forward | |
| 571 | |> map_context (f x) | |
| 572 | |> reset_facts; | |
| 573 | ||
| 8617 
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8582diff
changeset | 574 | val match_bind = gen_bind (ProofContext.match_bind false); | 
| 
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8582diff
changeset | 575 | val match_bind_i = gen_bind (ProofContext.match_bind_i false); | 
| 
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8582diff
changeset | 576 | val let_bind = gen_bind (ProofContext.match_bind true); | 
| 
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8582diff
changeset | 577 | val let_bind_i = gen_bind (ProofContext.match_bind_i true); | 
| 5820 | 578 | |
| 579 | ||
| 14564 | 580 | (* note_thmss *) | 
| 5820 | 581 | |
| 12714 | 582 | local | 
| 583 | ||
| 14564 | 584 | fun gen_note_thmss f args state = | 
| 5820 | 585 | state | 
| 586 | |> assert_forward | |
| 12714 | 587 | |> map_context_result (f args) | 
| 9196 | 588 | |> these_factss; | 
| 5820 | 589 | |
| 12714 | 590 | in | 
| 591 | ||
| 14564 | 592 | val note_thmss = gen_note_thmss ProofContext.note_thmss; | 
| 593 | val note_thmss_i = gen_note_thmss ProofContext.note_thmss_i; | |
| 12714 | 594 | |
| 14564 | 595 | fun simple_note_thms name thms = note_thmss_i [((name, []), [(thms, [])])]; | 
| 12714 | 596 | |
| 597 | end; | |
| 598 | ||
| 599 | ||
| 600 | (* with_thmss *) | |
| 9196 | 601 | |
| 12714 | 602 | local | 
| 603 | ||
| 604 | fun gen_with_thmss f args state = | |
| 605 | let val state' = state |> f args | |
| 14564 | 606 | in state' |> simple_note_thms "" (the_facts state' @ the_facts state) end; | 
| 6876 | 607 | |
| 12714 | 608 | in | 
| 609 | ||
| 14564 | 610 | val with_thmss = gen_with_thmss note_thmss; | 
| 611 | val with_thmss_i = gen_with_thmss note_thmss_i; | |
| 12714 | 612 | |
| 613 | end; | |
| 614 | ||
| 5820 | 615 | |
| 12930 | 616 | (* using_thmss *) | 
| 617 | ||
| 618 | local | |
| 619 | ||
| 620 | fun gen_using_thmss f args state = | |
| 621 | state | |
| 622 | |> assert_backward | |
| 623 |   |> map_context_result (f (map (pair ("", [])) args))
 | |
| 624 | |> (fn (st, named_facts) => | |
| 12971 | 625 | let val (_, (_, ((result, (facts, thm)), x))) = find_goal st; | 
| 15570 | 626 | in st |> map_goal I (K ((result, (facts @ List.concat (map snd named_facts), thm)), x)) end); | 
| 12930 | 627 | |
| 628 | in | |
| 629 | ||
| 14564 | 630 | val using_thmss = gen_using_thmss ProofContext.note_thmss; | 
| 631 | val using_thmss_i = gen_using_thmss ProofContext.note_thmss_i; | |
| 12930 | 632 | |
| 633 | end; | |
| 634 | ||
| 635 | ||
| 5820 | 636 | (* fix *) | 
| 637 | ||
| 638 | fun gen_fix f xs state = | |
| 639 | state | |
| 640 | |> assert_forward | |
| 641 | |> map_context (f xs) | |
| 642 | |> reset_facts; | |
| 643 | ||
| 644 | val fix = gen_fix ProofContext.fix; | |
| 645 | val fix_i = gen_fix ProofContext.fix_i; | |
| 646 | ||
| 647 | ||
| 11891 | 648 | (* assume and presume *) | 
| 5820 | 649 | |
| 9469 | 650 | fun gen_assume f exp args state = | 
| 5820 | 651 | state | 
| 652 | |> assert_forward | |
| 9469 | 653 | |> map_context_result (f exp args) | 
| 11924 | 654 | |> these_factss; | 
| 6932 | 655 | |
| 7271 | 656 | val assm = gen_assume ProofContext.assume; | 
| 657 | val assm_i = gen_assume ProofContext.assume_i; | |
| 11917 | 658 | val assume = assm ProofContext.export_assume; | 
| 659 | val assume_i = assm_i ProofContext.export_assume; | |
| 660 | val presume = assm ProofContext.export_presume; | |
| 661 | val presume_i = assm_i ProofContext.export_presume; | |
| 5820 | 662 | |
| 7271 | 663 | |
| 5820 | 664 | |
| 11891 | 665 | (** local definitions **) | 
| 666 | ||
| 667 | fun gen_def fix prep_term prep_pats raw_name atts (x, (raw_rhs, raw_pats)) state = | |
| 668 | let | |
| 669 | val _ = assert_forward state; | |
| 670 | val ctxt = context_of state; | |
| 671 | ||
| 672 | (*rhs*) | |
| 673 | val name = if raw_name = "" then Thm.def_name x else raw_name; | |
| 674 | val rhs = prep_term ctxt raw_rhs; | |
| 675 | val T = Term.fastype_of rhs; | |
| 676 | val pats = prep_pats T (ProofContext.declare_term rhs ctxt) raw_pats; | |
| 677 | ||
| 678 | (*lhs*) | |
| 679 | val lhs = ProofContext.bind_skolem ctxt [x] (Free (x, T)); | |
| 680 | in | |
| 681 | state | |
| 15531 | 682 | |> fix [([x], NONE)] | 
| 11891 | 683 | |> match_bind_i [(pats, rhs)] | 
| 11917 | 684 | |> assm_i ProofContext.export_def [((name, atts), [(Logic.mk_equals (lhs, rhs), ([], []))])] | 
| 11891 | 685 | end; | 
| 686 | ||
| 687 | val def = gen_def fix ProofContext.read_term ProofContext.read_term_pats; | |
| 688 | val def_i = gen_def fix_i ProofContext.cert_term ProofContext.cert_term_pats; | |
| 689 | ||
| 690 | ||
| 8374 | 691 | (* invoke_case *) | 
| 692 | ||
| 11793 
5f0ab6f5c280
support impromptu terminology of cases parameters;
 wenzelm parents: 
11742diff
changeset | 693 | fun invoke_case (name, xs, atts) state = | 
| 9292 | 694 | let | 
| 10830 | 695 | val (state', (lets, asms)) = | 
| 11793 
5f0ab6f5c280
support impromptu terminology of cases parameters;
 wenzelm parents: 
11742diff
changeset | 696 | map_context_result (ProofContext.apply_case (get_case state name xs)) state; | 
| 13425 
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
 wenzelm parents: 
13415diff
changeset | 697 | val assumptions = asms |> map (fn (a, ts) => | 
| 16146 | 698 | ((NameSpace.qualified name a, atts), map (rpair ([], [])) ts)); | 
| 9292 | 699 | in | 
| 10830 | 700 | state' | 
| 10809 | 701 | |> add_binds_i lets | 
| 16146 | 702 | |> map_context (ProofContext.no_base_names o ProofContext.qualified_names) | 
| 13425 
119ae829ad9b
support for split assumptions in cases (hyps vs. prems);
 wenzelm parents: 
13415diff
changeset | 703 | |> assume_i assumptions | 
| 16146 | 704 | |> map_context (ProofContext.restore_naming (context_of state)) | 
| 14564 | 705 | |> (fn st => simple_note_thms name (the_facts st) st) | 
| 8374 | 706 | end; | 
| 707 | ||
| 708 | ||
| 5820 | 709 | |
| 710 | (** goals **) | |
| 711 | ||
| 712 | (* forward chaining *) | |
| 713 | ||
| 714 | fun chain state = | |
| 715 | state | |
| 716 | |> assert_forward | |
| 6848 | 717 | |> assert_facts | 
| 5820 | 718 | |> enter_forward_chain; | 
| 719 | ||
| 720 | fun from_facts facts state = | |
| 721 | state | |
| 15531 | 722 | |> put_facts (SOME facts) | 
| 5820 | 723 | |> chain; | 
| 724 | ||
| 725 | ||
| 726 | (* setup goals *) | |
| 727 | ||
| 11549 | 728 | val rule_contextN = "rule_context"; | 
| 8991 | 729 | |
| 12971 | 730 | fun setup_goal opt_block prepp autofix kind after_qed pr names raw_propp state = | 
| 5820 | 731 | let | 
| 12146 | 732 | val (state', (propss, gen_binds)) = | 
| 5936 | 733 | state | 
| 734 | |> assert_forward_or_chain | |
| 735 | |> enter_forward | |
| 8617 
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8582diff
changeset | 736 | |> opt_block | 
| 12534 | 737 | |> map_context_result (fn ctxt => prepp (ctxt, raw_propp)); | 
| 12808 
a529b4b91888
RuleCases.make interface based on term instead of thm;
 wenzelm parents: 
12760diff
changeset | 738 | val sign' = sign_of state'; | 
| 12146 | 739 | |
| 15703 | 740 | val AfterQed (after_local, after_global) = after_qed; | 
| 741 | val after_qed' = AfterQed (after_local o map_context gen_binds, after_global); | |
| 742 | ||
| 15570 | 743 | val props = List.concat propss; | 
| 12808 
a529b4b91888
RuleCases.make interface based on term instead of thm;
 wenzelm parents: 
12760diff
changeset | 744 | val cprop = Thm.cterm_of sign' (Logic.mk_conjunction_list props); | 
| 7556 | 745 | val goal = Drule.mk_triv_goal cprop; | 
| 10553 | 746 | |
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 747 | val tvars = foldr Term.add_term_tvars [] props; | 
| 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 748 | val vars = foldr Term.add_term_vars [] props; | 
| 5820 | 749 | in | 
| 10553 | 750 | if null tvars then () | 
| 751 |     else raise STATE ("Goal statement contains illegal schematic type variable(s): " ^
 | |
| 752 | commas (map (Syntax.string_of_vname o #1) tvars), state); | |
| 753 | if null vars then () | |
| 754 |     else warning ("Goal statement contains unbound schematic variable(s): " ^
 | |
| 12055 | 755 | commas (map (ProofContext.string_of_term (context_of state')) vars)); | 
| 5936 | 756 | state' | 
| 12146 | 757 | |> map_context (autofix props) | 
| 15703 | 758 | |> put_goal (SOME (((kind, names, propss), ([], goal)), (after_qed', pr))) | 
| 12167 | 759 | |> map_context (ProofContext.add_cases | 
| 12808 
a529b4b91888
RuleCases.make interface based on term instead of thm;
 wenzelm parents: 
12760diff
changeset | 760 | (if length props = 1 then | 
| 15531 | 761 | RuleCases.make true NONE (Thm.sign_of_thm goal, Thm.prop_of goal) [rule_contextN] | 
| 16146 | 762 | else [(rule_contextN, NONE)])) | 
| 12146 | 763 | |> auto_bind_goal props | 
| 5820 | 764 | |> (if is_chain state then use_facts else reset_facts) | 
| 765 | |> new_block | |
| 766 | |> enter_backward | |
| 767 | end; | |
| 768 | ||
| 769 | (*global goals*) | |
| 15703 | 770 | fun global_goal prep kind after_global export raw_locale a elems concl thy = | 
| 12503 | 771 | let | 
| 12549 | 772 | val init = init_state thy; | 
| 13399 
c136276dc847
support locale ``views'' (for cumulative predicates);
 wenzelm parents: 
13377diff
changeset | 773 | val (opt_name, view, locale_ctxt, elems_ctxt, propp) = | 
| 15570 | 774 | prep (Option.map fst raw_locale) elems (map snd concl) (context_of init); | 
| 12503 | 775 | in | 
| 12549 | 776 | init | 
| 777 | |> open_block | |
| 12511 | 778 | |> map_context (K locale_ctxt) | 
| 12065 | 779 | |> open_block | 
| 12511 | 780 | |> map_context (K elems_ctxt) | 
| 12959 | 781 | |> setup_goal I ProofContext.bind_propp_schematic_i ProofContext.fix_frees | 
| 13415 | 782 |       (Theorem {kind = kind,
 | 
| 783 | theory_spec = (a, map (snd o fst) concl), | |
| 15973 | 784 | locale_spec = (case raw_locale of NONE => NONE | SOME (_, x) => SOME (the opt_name, x)), | 
| 15596 | 785 | view = view, | 
| 15696 | 786 | export = export}) | 
| 15703 | 787 | (AfterQed (Seq.single, after_global)) | 
| 788 | true (map (fst o fst) concl) propp | |
| 12065 | 789 | end; | 
| 5820 | 790 | |
| 12534 | 791 | val multi_theorem = global_goal Locale.read_context_statement; | 
| 792 | val multi_theorem_i = global_goal Locale.cert_context_statement; | |
| 12146 | 793 | |
| 5820 | 794 | |
| 795 | (*local goals*) | |
| 15703 | 796 | fun local_goal' prepp kind (check: bool -> state -> state) after_local pr args int state = | 
| 7928 | 797 | state | 
| 12146 | 798 | |> setup_goal open_block prepp (K I) (kind (map (snd o fst) args)) | 
| 15703 | 799 | (AfterQed (after_local, I)) pr (map (fst o fst) args) (map snd args) | 
| 10936 | 800 | |> warn_extra_tfrees state | 
| 801 | |> check int; | |
| 5820 | 802 | |
| 12971 | 803 | fun local_goal prepp kind f pr args = local_goal' prepp kind (K I) f pr args false; | 
| 10936 | 804 | |
| 805 | val show = local_goal' ProofContext.bind_propp Show; | |
| 806 | val show_i = local_goal' ProofContext.bind_propp_i Show; | |
| 807 | val have = local_goal ProofContext.bind_propp Have; | |
| 10380 | 808 | val have_i = local_goal ProofContext.bind_propp_i Have; | 
| 15703 | 809 | |
| 5820 | 810 | |
| 811 | ||
| 812 | (** conclusions **) | |
| 813 | ||
| 814 | (* current goal *) | |
| 815 | ||
| 15531 | 816 | fun current_goal (State (Node {context, goal = SOME goal, ...}, _)) = (context, goal)
 | 
| 5820 | 817 |   | current_goal state = raise STATE ("No current goal!", state);
 | 
| 818 | ||
| 15531 | 819 | fun assert_current_goal true (state as State (Node {goal = NONE, ...}, _)) =
 | 
| 6404 | 820 |       raise STATE ("No goal in this block!", state)
 | 
| 15531 | 821 |   | assert_current_goal false (state as State (Node {goal = SOME _, ...}, _)) =
 | 
| 6404 | 822 |       raise STATE ("Goal present in this block!", state)
 | 
| 823 | | assert_current_goal _ state = state; | |
| 5820 | 824 | |
| 12010 | 825 | fun assert_bottom b (state as State (_, nodes)) = | 
| 826 | let val bot = (length nodes <= 2) in | |
| 827 |     if b andalso not bot then raise STATE ("Not at bottom of proof!", state)
 | |
| 828 |     else if not b andalso bot then raise STATE ("Already at bottom of proof!", state)
 | |
| 829 | else state | |
| 830 | end; | |
| 5820 | 831 | |
| 6404 | 832 | val at_bottom = can (assert_bottom true o close_block); | 
| 833 | ||
| 6932 | 834 | fun end_proof bot state = | 
| 5820 | 835 | state | 
| 836 | |> assert_forward | |
| 837 | |> close_block | |
| 838 | |> assert_bottom bot | |
| 7011 | 839 | |> assert_current_goal true | 
| 840 | |> goal_facts (K []); | |
| 5820 | 841 | |
| 842 | ||
| 6404 | 843 | (* local_qed *) | 
| 5820 | 844 | |
| 6982 | 845 | fun finish_local (print_result, print_rule) state = | 
| 5820 | 846 | let | 
| 15703 | 847 | val (goal_ctxt, (((kind, names, tss), (_, raw_thm)), (AfterQed (after_local, _), pr))) = | 
| 848 | current_goal state; | |
| 8617 
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8582diff
changeset | 849 | val outer_state = state |> close_block; | 
| 
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8582diff
changeset | 850 | val outer_ctxt = context_of outer_state; | 
| 
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8582diff
changeset | 851 | |
| 15570 | 852 | val ts = List.concat tss; | 
| 12146 | 853 | val results = prep_result state ts raw_thm; | 
| 854 | val resultq = | |
| 855 | results |> map (ProofContext.export false goal_ctxt outer_ctxt) | |
| 856 | |> Seq.commute |> Seq.map (Library.unflat tss); | |
| 9469 | 857 | |
| 15703 | 858 | val (attss, opt_solve) = | 
| 5820 | 859 | (case kind of | 
| 12971 | 860 | Show attss => (attss, | 
| 15703 | 861 | export_goals goal_ctxt (if pr then print_rule else (K (K ()))) results) | 
| 862 | | Have attss => (attss, Seq.single) | |
| 6932 | 863 | | _ => err_malformed "finish_local" state); | 
| 5820 | 864 | in | 
| 12971 | 865 | conditional pr (fn () => print_result goal_ctxt | 
| 866 | (kind_name (sign_of state) kind, names ~~ Library.unflat tss results)); | |
| 8617 
33e2bd53aec3
support Hindley-Milner polymorphisms in binds and facts;
 wenzelm parents: 
8582diff
changeset | 867 | outer_state | 
| 12549 | 868 | |> auto_bind_facts (ProofContext.generalize goal_ctxt outer_ctxt ts) | 
| 9469 | 869 | |> (fn st => Seq.map (fn res => | 
| 15703 | 870 | note_thmss_i ((names ~~ attss) ~~ map (single o Thm.no_attributes) res) st) resultq) | 
| 9469 | 871 | |> (Seq.flat o Seq.map opt_solve) | 
| 15703 | 872 | |> (Seq.flat o Seq.map after_local) | 
| 5820 | 873 | end; | 
| 874 | ||
| 6982 | 875 | fun local_qed finalize print state = | 
| 6404 | 876 | state | 
| 6932 | 877 | |> end_proof false | 
| 6871 | 878 | |> finalize | 
| 6982 | 879 | |> (Seq.flat o Seq.map (finish_local print)); | 
| 5820 | 880 | |
| 881 | ||
| 12703 
af5fec8a418f
refine_tac: Tactic.norm_hhf_tac before trying rule;
 wenzelm parents: 
12698diff
changeset | 882 | (* global_qed *) | 
| 12065 | 883 | |
| 6950 | 884 | fun finish_global state = | 
| 5820 | 885 | let | 
| 15703 | 886 | val (goal_ctxt, (((kind, names, tss), (_, raw_thm)), (AfterQed (_, after_global), _))) = | 
| 887 | current_goal state; | |
| 12065 | 888 | val locale_ctxt = context_of (state |> close_block); | 
| 889 | val theory_ctxt = context_of (state |> close_block |> close_block); | |
| 15703 | 890 |     val {kind = k, theory_spec = ((name, atts), attss),
 | 
| 891 | locale_spec, view = (target_view, elems_view), export} = | |
| 13399 
c136276dc847
support locale ``views'' (for cumulative predicates);
 wenzelm parents: 
13377diff
changeset | 892 | (case kind of Theorem x => x | _ => err_malformed "finish_global" state); | 
| 15703 | 893 | val locale_name = Option.map fst locale_spec; | 
| 5820 | 894 | |
| 15570 | 895 | val ts = List.concat tss; | 
| 15696 | 896 | val locale_results = map (export elems_view goal_ctxt locale_ctxt) | 
| 13377 | 897 | (prep_result state ts raw_thm); | 
| 14215 
ebf291f3b449
Improvements to Isar/Locales:  premises generated by "includes" elements
 ballarin parents: 
14129diff
changeset | 898 | |
| 13377 | 899 | val results = map (Drule.strip_shyps_warning o | 
| 15696 | 900 | export target_view locale_ctxt theory_ctxt) locale_results; | 
| 12703 
af5fec8a418f
refine_tac: Tactic.norm_hhf_tac before trying rule;
 wenzelm parents: 
12698diff
changeset | 901 | |
| 12959 | 902 | val (theory', results') = | 
| 903 | theory_of state | |
| 15696 | 904 | |> (case locale_spec of NONE => I | 
| 905 | | SOME (loc, (loc_atts, loc_attss)) => fn thy => | |
| 12959 | 906 | if length names <> length loc_attss then | 
| 907 |           raise THEORY ("Bad number of locale attributes", [thy])
 | |
| 13335 | 908 | else (thy, locale_ctxt) | 
| 15696 | 909 | |> Locale.add_thmss k loc ((names ~~ Library.unflat tss locale_results) ~~ loc_attss) | 
| 13335 | 910 | |> (fn ((thy', ctxt'), res) => | 
| 12959 | 911 | if name = "" andalso null loc_atts then thy' | 
| 13335 | 912 | else (thy', ctxt') | 
| 15696 | 913 | |> (#1 o #1 o Locale.add_thmss k loc [((name, List.concat (map #2 res)), loc_atts)]))) | 
| 15703 | 914 | |> Locale.smart_note_thmss k locale_name | 
| 12703 
af5fec8a418f
refine_tac: Tactic.norm_hhf_tac before trying rule;
 wenzelm parents: 
12698diff
changeset | 915 | ((names ~~ attss) ~~ map (single o Thm.no_attributes) (Library.unflat tss results)) | 
| 12959 | 916 | |> (fn (thy, res) => (thy, res) | 
| 15703 | 917 | |>> (#1 o Locale.smart_note_thmss k locale_name | 
| 15570 | 918 | [((name, atts), [(List.concat (map #2 res), [])])])); | 
| 15703 | 919 | in (after_global theory', ((k, name), results')) end; | 
| 12959 | 920 | |
| 5820 | 921 | |
| 10553 | 922 | (*note: clients should inspect first result only, as backtracking may destroy theory*) | 
| 6950 | 923 | fun global_qed finalize state = | 
| 5820 | 924 | state | 
| 6932 | 925 | |> end_proof true | 
| 6871 | 926 | |> finalize | 
| 12065 | 927 | |> Seq.map finish_global; | 
| 5820 | 928 | |
| 929 | ||
| 6896 | 930 | |
| 931 | (** blocks **) | |
| 932 | ||
| 933 | (* begin_block *) | |
| 934 | ||
| 935 | fun begin_block state = | |
| 936 | state | |
| 937 | |> assert_forward | |
| 938 | |> new_block | |
| 939 | |> open_block; | |
| 940 | ||
| 941 | ||
| 942 | (* end_block *) | |
| 943 | ||
| 944 | fun end_block state = | |
| 945 | state | |
| 946 | |> assert_forward | |
| 947 | |> close_block | |
| 948 | |> assert_current_goal false | |
| 949 | |> close_block | |
| 6932 | 950 | |> transfer_facts state; | 
| 6896 | 951 | |
| 952 | ||
| 953 | (* next_block *) | |
| 954 | ||
| 955 | fun next_block state = | |
| 956 | state | |
| 957 | |> assert_forward | |
| 958 | |> close_block | |
| 8718 | 959 | |> new_block | 
| 960 | |> reset_facts; | |
| 6896 | 961 | |
| 962 | ||
| 5820 | 963 | end; | 
| 8152 | 964 | |
| 965 | structure BasicProof: BASIC_PROOF = Proof; | |
| 966 | open BasicProof; |