| author | wenzelm | 
| Fri, 16 Apr 2004 04:09:53 +0200 | |
| changeset 14579 | e79f1923fa0a | 
| parent 14564 | 3667b4616e9a | 
| child 14981 | e73f8140af78 | 
| permissions | -rw-r--r-- | 
| 6778 | 1 | (* Title: Pure/Isar/calculation.ML | 
| 2 | ID: $Id$ | |
| 3 | Author: Markus Wenzel, TU Muenchen | |
| 8807 | 4 | License: GPL (GNU GENERAL PUBLIC LICENSE) | 
| 6778 | 5 | |
| 6 | Support for calculational proofs. | |
| 7 | *) | |
| 8 | ||
| 9 | signature CALCULATION = | |
| 10 | sig | |
| 11 | val print_global_rules: theory -> unit | |
| 12 | val print_local_rules: Proof.context -> unit | |
| 13 | val trans_add_global: theory attribute | |
| 14 | val trans_del_global: theory attribute | |
| 15 | val trans_add_local: Proof.context attribute | |
| 16 | val trans_del_local: Proof.context attribute | |
| 12379 | 17 | val sym_add_global: theory attribute | 
| 18 | val sym_del_global: theory attribute | |
| 19 | val sym_add_local: Proof.context attribute | |
| 20 | val sym_del_local: Proof.context attribute | |
| 21 | val symmetric_global: theory attribute | |
| 22 | val symmetric_local: Proof.context attribute | |
| 12055 | 23 | val also: thm list option -> (Proof.context -> thm list -> unit) | 
| 24 | -> Proof.state -> Proof.state Seq.seq | |
| 25 | val finally: thm list option -> (Proof.context -> thm list -> unit) | |
| 26 | -> Proof.state -> Proof.state Seq.seq | |
| 27 | val moreover: (Proof.context -> thm list -> unit) -> Proof.state -> Proof.state | |
| 28 | val ultimately: (Proof.context -> thm list -> unit) -> Proof.state -> Proof.state | |
| 6778 | 29 | val setup: (theory -> theory) list | 
| 30 | end; | |
| 31 | ||
| 32 | structure Calculation: CALCULATION = | |
| 33 | struct | |
| 34 | ||
| 35 | (** global and local calculation data **) | |
| 36 | ||
| 8300 | 37 | (* theory data kind 'Isar/calculation' *) | 
| 6778 | 38 | |
| 12379 | 39 | fun print_rules prt x (trans, sym) = | 
| 40 | [Pretty.big_list "transitivity rules:" (map (prt x) (NetRules.rules trans)), | |
| 41 | Pretty.big_list "symmetry rules:" (map (prt x) sym)] | |
| 42 | |> Pretty.chunks |> Pretty.writeln; | |
| 6778 | 43 | |
| 44 | structure GlobalCalculationArgs = | |
| 45 | struct | |
| 46 | val name = "Isar/calculation"; | |
| 12379 | 47 | type T = thm NetRules.T * thm list | 
| 6778 | 48 | |
| 12379 | 49 | val empty = (NetRules.elim, []); | 
| 6778 | 50 | val copy = I; | 
| 51 | val prep_ext = I; | |
| 12379 | 52 | fun merge ((trans1, sym1), (trans2, sym2)) = | 
| 53 | (NetRules.merge (trans1, trans2), Drule.merge_rules (sym1, sym2)); | |
| 12055 | 54 | val print = print_rules Display.pretty_thm_sg; | 
| 6778 | 55 | end; | 
| 56 | ||
| 57 | structure GlobalCalculation = TheoryDataFun(GlobalCalculationArgs); | |
| 58 | val print_global_rules = GlobalCalculation.print; | |
| 59 | ||
| 60 | ||
| 61 | (* proof data kind 'Isar/calculation' *) | |
| 62 | ||
| 63 | structure LocalCalculationArgs = | |
| 64 | struct | |
| 65 | val name = "Isar/calculation"; | |
| 12379 | 66 | type T = (thm NetRules.T * thm list) * (thm list * int) option; | 
| 6778 | 67 | |
| 68 | fun init thy = (GlobalCalculation.get thy, None); | |
| 12055 | 69 | fun print ctxt (rs, _) = print_rules ProofContext.pretty_thm ctxt rs; | 
| 6778 | 70 | end; | 
| 71 | ||
| 72 | structure LocalCalculation = ProofDataFun(LocalCalculationArgs); | |
| 13371 | 73 | val get_local_rules = #1 o LocalCalculation.get o Proof.context_of; | 
| 6778 | 74 | val print_local_rules = LocalCalculation.print; | 
| 75 | ||
| 76 | ||
| 77 | (* access calculation *) | |
| 78 | ||
| 79 | fun get_calculation state = | |
| 13371 | 80 | (case #2 (LocalCalculation.get (Proof.context_of state)) of | 
| 6778 | 81 | None => None | 
| 7414 | 82 | | Some (thms, lev) => if lev = Proof.level state then Some thms else None); | 
| 6778 | 83 | |
| 7414 | 84 | fun put_calculation thms state = | 
| 13371 | 85 | Proof.map_context | 
| 86 | (LocalCalculation.put (get_local_rules state, Some (thms, Proof.level state))) state; | |
| 6778 | 87 | |
| 6787 | 88 | fun reset_calculation state = | 
| 13371 | 89 | Proof.map_context (LocalCalculation.put (get_local_rules state, None)) state; | 
| 6787 | 90 | |
| 6778 | 91 | |
| 92 | ||
| 93 | (** attributes **) | |
| 94 | ||
| 12379 | 95 | (* add/del rules *) | 
| 96 | ||
| 97 | fun global_att f (x, thm) = (GlobalCalculation.map (f thm) x, thm); | |
| 98 | fun local_att f (x, thm) = (LocalCalculation.map (apfst (f thm)) x, thm); | |
| 6778 | 99 | |
| 12379 | 100 | val trans_add_global = global_att (apfst o NetRules.insert); | 
| 101 | val trans_del_global = global_att (apfst o NetRules.delete); | |
| 102 | val trans_add_local = local_att (apfst o NetRules.insert); | |
| 103 | val trans_del_local = local_att (apfst o NetRules.delete); | |
| 6778 | 104 | |
| 12379 | 105 | val sym_add_global = global_att (apsnd o Drule.add_rule) o ContextRules.elim_query_global None; | 
| 12402 | 106 | val sym_del_global = global_att (apsnd o Drule.del_rule) o ContextRules.rule_del_global; | 
| 12379 | 107 | val sym_add_local = local_att (apsnd o Drule.add_rule) o ContextRules.elim_query_local None; | 
| 12402 | 108 | val sym_del_local = local_att (apsnd o Drule.del_rule) o ContextRules.rule_del_local; | 
| 12379 | 109 | |
| 110 | ||
| 111 | (* symmetry *) | |
| 112 | ||
| 113 | fun gen_symmetric get_sym = Drule.rule_attribute (fn x => fn th => | |
| 114 | (case Seq.chop (2, Method.multi_resolves [th] (get_sym x)) of | |
| 115 | ([th'], _) => th' | |
| 116 |   | ([], _) => raise THM ("symmetric: no unifiers", 1, [th])
 | |
| 117 |   | _ => raise THM ("symmetric: multiple unifiers", 1, [th])));
 | |
| 118 | ||
| 119 | val symmetric_global = gen_symmetric (#2 o GlobalCalculation.get); | |
| 120 | val symmetric_local = gen_symmetric (#2 o #1 o LocalCalculation.get); | |
| 6778 | 121 | |
| 122 | ||
| 123 | (* concrete syntax *) | |
| 124 | ||
| 125 | val trans_attr = | |
| 8634 | 126 | (Attrib.add_del_args trans_add_global trans_del_global, | 
| 127 | Attrib.add_del_args trans_add_local trans_del_local); | |
| 6778 | 128 | |
| 12379 | 129 | val sym_attr = | 
| 130 | (Attrib.add_del_args sym_add_global sym_del_global, | |
| 131 | Attrib.add_del_args sym_add_local sym_del_local); | |
| 132 | ||
| 6778 | 133 | |
| 6787 | 134 | |
| 6778 | 135 | (** proof commands **) | 
| 136 | ||
| 14555 
341908d6c792
'also'/'moreover': do not interfere with current facts, allow in chain mode;
 wenzelm parents: 
14549diff
changeset | 137 | fun assert_sane final = | 
| 
341908d6c792
'also'/'moreover': do not interfere with current facts, allow in chain mode;
 wenzelm parents: 
14549diff
changeset | 138 | if final then Proof.assert_forward else Proof.assert_forward_or_chain; | 
| 
341908d6c792
'also'/'moreover': do not interfere with current facts, allow in chain mode;
 wenzelm parents: 
14549diff
changeset | 139 | |
| 
341908d6c792
'also'/'moreover': do not interfere with current facts, allow in chain mode;
 wenzelm parents: 
14549diff
changeset | 140 | |
| 8588 | 141 | (* maintain calculation register *) | 
| 8562 | 142 | |
| 6778 | 143 | val calculationN = "calculation"; | 
| 144 | ||
| 8588 | 145 | fun maintain_calculation false calc state = | 
| 146 | state | |
| 147 | |> put_calculation calc | |
| 14555 
341908d6c792
'also'/'moreover': do not interfere with current facts, allow in chain mode;
 wenzelm parents: 
14549diff
changeset | 148 | |> Proof.put_thms (calculationN, calc) | 
| 8588 | 149 | | maintain_calculation true calc state = | 
| 150 | state | |
| 151 | |> reset_calculation | |
| 152 | |> Proof.reset_thms calculationN | |
| 14564 | 153 | |> Proof.simple_note_thms "" calc | 
| 8588 | 154 | |> Proof.chain; | 
| 8562 | 155 | |
| 156 | ||
| 157 | (* 'also' and 'finally' *) | |
| 158 | ||
| 8588 | 159 | fun err_if state b msg = if b then raise Proof.STATE (msg, state) else (); | 
| 160 | ||
| 6877 | 161 | fun calculate final opt_rules print state = | 
| 6778 | 162 | let | 
| 12805 | 163 | val strip_assums_concl = Logic.strip_assums_concl o Thm.prop_of; | 
| 9322 | 164 | val eq_prop = op aconv o pairself (Pattern.eta_contract o strip_assums_concl); | 
| 11097 | 165 | fun projection ths th = Library.exists (Library.curry eq_prop th) ths; | 
| 8300 | 166 | |
| 11097 | 167 | fun combine ths = | 
| 168 | (case opt_rules of Some rules => rules | |
| 169 | | None => | |
| 12379 | 170 | (case ths of [] => NetRules.rules (#1 (get_local_rules state)) | 
| 171 | | th :: _ => NetRules.retrieve (#1 (get_local_rules state)) (strip_assums_concl th))) | |
| 11097 | 172 | |> Seq.of_list |> Seq.map (Method.multi_resolve ths) |> Seq.flat | 
| 173 | |> Seq.filter (not o projection ths); | |
| 7414 | 174 | |
| 14555 
341908d6c792
'also'/'moreover': do not interfere with current facts, allow in chain mode;
 wenzelm parents: 
14549diff
changeset | 175 | val facts = Proof.the_facts (assert_sane final state); | 
| 6903 | 176 | val (initial, calculations) = | 
| 6778 | 177 | (case get_calculation state of | 
| 7414 | 178 | None => (true, Seq.single facts) | 
| 11097 | 179 | | Some calc => (false, Seq.map single (combine (calc @ facts)))); | 
| 6778 | 180 | in | 
| 8588 | 181 | err_if state (initial andalso final) "No calculation yet"; | 
| 182 | err_if state (initial andalso is_some opt_rules) "Initial calculation -- no rules to be given"; | |
| 12055 | 183 | calculations |> Seq.map (fn calc => (print (Proof.context_of state) calc; | 
| 184 | state |> maintain_calculation final calc)) | |
| 6778 | 185 | end; | 
| 186 | ||
| 6782 | 187 | fun also print = calculate false print; | 
| 188 | fun finally print = calculate true print; | |
| 6778 | 189 | |
| 190 | ||
| 8588 | 191 | (* 'moreover' and 'ultimately' *) | 
| 8562 | 192 | |
| 8588 | 193 | fun collect final print state = | 
| 194 | let | |
| 14555 
341908d6c792
'also'/'moreover': do not interfere with current facts, allow in chain mode;
 wenzelm parents: 
14549diff
changeset | 195 | val facts = Proof.the_facts (assert_sane final state); | 
| 8588 | 196 | val (initial, thms) = | 
| 197 | (case get_calculation state of | |
| 198 | None => (true, []) | |
| 199 | | Some thms => (false, thms)); | |
| 200 | val calc = thms @ facts; | |
| 201 | in | |
| 202 | err_if state (initial andalso final) "No calculation yet"; | |
| 12055 | 203 | print (Proof.context_of state) calc; | 
| 8588 | 204 | state |> maintain_calculation final calc | 
| 205 | end; | |
| 206 | ||
| 207 | fun moreover print = collect false print; | |
| 208 | fun ultimately print = collect true print; | |
| 8562 | 209 | |
| 210 | ||
| 6778 | 211 | |
| 212 | (** theory setup **) | |
| 213 | ||
| 12379 | 214 | val setup = | 
| 215 | [GlobalCalculation.init, LocalCalculation.init, | |
| 216 | Attrib.add_attributes | |
| 217 |    [("trans", trans_attr, "declaration of transitivity rule"),
 | |
| 218 |     ("sym", sym_attr, "declaration of symmetry rule"),
 | |
| 219 |     ("symmetric", (Attrib.no_args symmetric_global, Attrib.no_args symmetric_local),
 | |
| 220 | "resolution with symmetry rule")], | |
| 221 | #1 o PureThy.add_thms | |
| 222 |    [(("", transitive_thm), [trans_add_global]),
 | |
| 223 |     (("", symmetric_thm), [sym_add_global])]];
 | |
| 6778 | 224 | |
| 225 | end; |