1 (* Title: Pure/Isar/calculation.ML
3 Author: Markus Wenzel, TU Muenchen
5 Generic calculational proofs.
8 signature CALCULATION =
10 val print_rules: Proof.context -> unit
11 val get_calculation: Proof.state -> thm list option
12 val trans_add: attribute
13 val trans_del: attribute
14 val sym_add: attribute
15 val sym_del: attribute
16 val symmetric: attribute
17 val also: (Facts.ref * Attrib.src list) list option -> bool -> Proof.state -> Proof.state Seq.seq
18 val also_i: thm list option -> bool -> Proof.state -> Proof.state Seq.seq
19 val finally_: (Facts.ref * Attrib.src list) list option -> bool ->
20 Proof.state -> Proof.state Seq.seq
21 val finally_i: thm list option -> bool -> Proof.state -> Proof.state Seq.seq
22 val moreover: bool -> Proof.state -> Proof.state
23 val ultimately: bool -> Proof.state -> Proof.state
26 structure Calculation: CALCULATION =
29 (** calculation data **)
31 structure CalculationData = GenericDataFun
33 type T = (thm NetRules.T * thm list) * (thm list * int) option;
34 val empty = ((NetRules.elim, []), NONE);
37 fun merge _ (((trans1, sym1), _), ((trans2, sym2), _)) =
38 ((NetRules.merge (trans1, trans2), Thm.merge_thms (sym1, sym2)), NONE);
41 fun print_rules ctxt =
42 let val ((trans, sym), _) = CalculationData.get (Context.Proof ctxt) in
43 [Pretty.big_list "transitivity rules:"
44 (map (ProofContext.pretty_thm ctxt) (NetRules.rules trans)),
45 Pretty.big_list "symmetry rules:" (map (ProofContext.pretty_thm ctxt) sym)]
46 |> Pretty.chunks |> Pretty.writeln
50 (* access calculation *)
52 fun get_calculation state =
53 (case #2 (CalculationData.get (Context.Proof (Proof.context_of state))) of
55 | SOME (thms, lev) => if lev = Proof.level state then SOME thms else NONE);
57 val calculationN = "calculation";
59 fun put_calculation calc =
60 `Proof.level #-> (fn lev => Proof.map_context (Context.proof_map
61 (CalculationData.map (apsnd (K (Option.map (rpair lev) calc))))))
62 #> Proof.put_thms false (calculationN, calc);
70 val trans_add = Thm.declaration_attribute (CalculationData.map o apfst o apfst o NetRules.insert);
71 val trans_del = Thm.declaration_attribute (CalculationData.map o apfst o apfst o NetRules.delete);
74 Thm.declaration_attribute (CalculationData.map o apfst o apsnd o Thm.add_thm)
75 #> ContextRules.elim_query NONE;
77 Thm.declaration_attribute (CalculationData.map o apfst o apsnd o Thm.del_thm)
78 #> ContextRules.rule_del;
83 val symmetric = Thm.rule_attribute (fn x => fn th =>
84 (case Seq.chop 2 (Drule.multi_resolves [th] (#2 (#1 (CalculationData.get x)))) of
85 ([th'], _) => Drule.zero_var_indexes th'
86 | ([], _) => raise THM ("symmetric: no unifiers", 1, [th])
87 | _ => raise THM ("symmetric: multiple unifiers", 1, [th])));
92 val trans_att = Attrib.add_del_args trans_add trans_del;
93 val sym_att = Attrib.add_del_args sym_add sym_del;
95 val _ = Context.>> (Context.map_theory
96 (Attrib.add_attributes
97 [("trans", trans_att, "declaration of transitivity rule"),
98 ("sym", sym_att, "declaration of symmetry rule"),
99 ("symmetric", Attrib.no_args symmetric, "resolution with symmetry rule")] #>
101 [(("", transitive_thm), [trans_add]),
102 (("", symmetric_thm), [sym_add])] #> snd));
106 (** proof commands **)
108 fun err_if b msg = if b then error msg else ();
110 fun assert_sane final =
111 if final then Proof.assert_forward else Proof.assert_forward_or_chain;
113 fun maintain_calculation false calc = put_calculation (SOME calc)
114 | maintain_calculation true calc = put_calculation NONE #> Proof.chain_facts calc;
116 fun print_calculation false _ _ = ()
117 | print_calculation true ctxt calc =
118 Pretty.writeln (ProofContext.pretty_fact ctxt (calculationN, calc));
121 (* also and finally *)
123 val get_rules = #1 o CalculationData.get o Context.Proof o Proof.context_of;
125 fun calculate prep_rules final raw_rules int state =
127 val strip_assums_concl = Logic.strip_assums_concl o Thm.prop_of;
128 val eq_prop = op aconv o pairself (Envir.beta_eta_contract o strip_assums_concl);
129 fun projection ths th = Library.exists (Library.curry eq_prop th) ths;
131 val opt_rules = Option.map (prep_rules state) raw_rules;
133 (case opt_rules of SOME rules => rules
135 (case ths of [] => NetRules.rules (#1 (get_rules state))
136 | th :: _ => NetRules.retrieve (#1 (get_rules state)) (strip_assums_concl th)))
137 |> Seq.of_list |> Seq.maps (Drule.multi_resolve ths)
138 |> Seq.filter (not o projection ths);
140 val facts = Proof.the_facts (assert_sane final state);
141 val (initial, calculations) =
142 (case get_calculation state of
143 NONE => (true, Seq.single facts)
144 | SOME calc => (false, Seq.map single (combine (calc @ facts))));
146 err_if (initial andalso final) "No calculation yet";
147 err_if (initial andalso is_some opt_rules) "Initial calculation -- no rules to be given";
148 calculations |> Seq.map (fn calc => (print_calculation int (Proof.context_of state) calc;
149 state |> maintain_calculation final calc))
152 val also = calculate Proof.get_thmss false;
153 val also_i = calculate (K I) false;
154 val finally_ = calculate Proof.get_thmss true;
155 val finally_i = calculate (K I) true;
158 (* moreover and ultimately *)
160 fun collect final int state =
162 val facts = Proof.the_facts (assert_sane final state);
163 val (initial, thms) =
164 (case get_calculation state of
166 | SOME thms => (false, thms));
167 val calc = thms @ facts;
169 err_if (initial andalso final) "No calculation yet";
170 print_calculation int (Proof.context_of state) calc;
171 state |> maintain_calculation final calc
174 val moreover = collect false;
175 val ultimately = collect true;