author | wenzelm |
Sat, 23 Apr 2005 19:51:04 +0200 | |
changeset 15831 | aa58e4ec3a1f |
parent 15801 | d2f5ca3c048d |
child 15973 | 5fd94d84470f |
permissions | -rw-r--r-- |
6778 | 1 |
(* Title: Pure/Isar/calculation.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
5 |
Support for calculational proofs. |
|
6 |
*) |
|
7 |
||
8 |
signature CALCULATION = |
|
9 |
sig |
|
10 |
val print_global_rules: theory -> unit |
|
11 |
val print_local_rules: Proof.context -> unit |
|
12 |
val trans_add_global: theory attribute |
|
13 |
val trans_del_global: theory attribute |
|
14 |
val trans_add_local: Proof.context attribute |
|
15 |
val trans_del_local: Proof.context attribute |
|
12379 | 16 |
val sym_add_global: theory attribute |
17 |
val sym_del_global: theory attribute |
|
18 |
val sym_add_local: Proof.context attribute |
|
19 |
val sym_del_local: Proof.context attribute |
|
20 |
val symmetric_global: theory attribute |
|
21 |
val symmetric_local: Proof.context attribute |
|
12055 | 22 |
val also: thm list option -> (Proof.context -> thm list -> unit) |
23 |
-> Proof.state -> Proof.state Seq.seq |
|
24 |
val finally: thm list option -> (Proof.context -> thm list -> unit) |
|
25 |
-> Proof.state -> Proof.state Seq.seq |
|
26 |
val moreover: (Proof.context -> thm list -> unit) -> Proof.state -> Proof.state |
|
27 |
val ultimately: (Proof.context -> thm list -> unit) -> Proof.state -> Proof.state |
|
6778 | 28 |
end; |
29 |
||
30 |
structure Calculation: CALCULATION = |
|
31 |
struct |
|
32 |
||
33 |
(** global and local calculation data **) |
|
34 |
||
8300 | 35 |
(* theory data kind 'Isar/calculation' *) |
6778 | 36 |
|
12379 | 37 |
fun print_rules prt x (trans, sym) = |
38 |
[Pretty.big_list "transitivity rules:" (map (prt x) (NetRules.rules trans)), |
|
39 |
Pretty.big_list "symmetry rules:" (map (prt x) sym)] |
|
40 |
|> Pretty.chunks |> Pretty.writeln; |
|
6778 | 41 |
|
42 |
structure GlobalCalculationArgs = |
|
43 |
struct |
|
44 |
val name = "Isar/calculation"; |
|
12379 | 45 |
type T = thm NetRules.T * thm list |
6778 | 46 |
|
12379 | 47 |
val empty = (NetRules.elim, []); |
6778 | 48 |
val copy = I; |
49 |
val prep_ext = I; |
|
12379 | 50 |
fun merge ((trans1, sym1), (trans2, sym2)) = |
51 |
(NetRules.merge (trans1, trans2), Drule.merge_rules (sym1, sym2)); |
|
12055 | 52 |
val print = print_rules Display.pretty_thm_sg; |
6778 | 53 |
end; |
54 |
||
55 |
structure GlobalCalculation = TheoryDataFun(GlobalCalculationArgs); |
|
15801 | 56 |
val _ = Context.add_setup [GlobalCalculation.init]; |
6778 | 57 |
val print_global_rules = GlobalCalculation.print; |
58 |
||
59 |
||
60 |
(* proof data kind 'Isar/calculation' *) |
|
61 |
||
62 |
structure LocalCalculationArgs = |
|
63 |
struct |
|
64 |
val name = "Isar/calculation"; |
|
12379 | 65 |
type T = (thm NetRules.T * thm list) * (thm list * int) option; |
6778 | 66 |
|
15531 | 67 |
fun init thy = (GlobalCalculation.get thy, NONE); |
12055 | 68 |
fun print ctxt (rs, _) = print_rules ProofContext.pretty_thm ctxt rs; |
6778 | 69 |
end; |
70 |
||
71 |
structure LocalCalculation = ProofDataFun(LocalCalculationArgs); |
|
15801 | 72 |
val _ = Context.add_setup [LocalCalculation.init]; |
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 |
15531 | 81 |
NONE => NONE |
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 |
15531 | 86 |
(LocalCalculation.put (get_local_rules state, SOME (thms, Proof.level state))) state; |
6778 | 87 |
|
6787 | 88 |
fun reset_calculation state = |
15531 | 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 |
|
15531 | 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; |
15531 | 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 |
||
15801 | 133 |
val _ = Context.add_setup |
134 |
[Attrib.add_attributes |
|
135 |
[("trans", trans_attr, "declaration of transitivity rule"), |
|
136 |
("sym", sym_attr, "declaration of symmetry rule"), |
|
137 |
("symmetric", (Attrib.no_args symmetric_global, Attrib.no_args symmetric_local), |
|
138 |
"resolution with symmetry rule")], |
|
139 |
#1 o PureThy.add_thms |
|
140 |
[(("", transitive_thm), [trans_add_global]), |
|
141 |
(("", symmetric_thm), [sym_add_global])]]; |
|
142 |
||
6778 | 143 |
|
6787 | 144 |
|
6778 | 145 |
(** proof commands **) |
146 |
||
14555
341908d6c792
'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents:
14549
diff
changeset
|
147 |
fun assert_sane final = |
341908d6c792
'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents:
14549
diff
changeset
|
148 |
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:
14549
diff
changeset
|
149 |
|
341908d6c792
'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents:
14549
diff
changeset
|
150 |
|
8588 | 151 |
(* maintain calculation register *) |
8562 | 152 |
|
6778 | 153 |
val calculationN = "calculation"; |
154 |
||
8588 | 155 |
fun maintain_calculation false calc state = |
156 |
state |
|
157 |
|> put_calculation calc |
|
14555
341908d6c792
'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents:
14549
diff
changeset
|
158 |
|> Proof.put_thms (calculationN, calc) |
8588 | 159 |
| maintain_calculation true calc state = |
160 |
state |
|
161 |
|> reset_calculation |
|
162 |
|> Proof.reset_thms calculationN |
|
14564 | 163 |
|> Proof.simple_note_thms "" calc |
8588 | 164 |
|> Proof.chain; |
8562 | 165 |
|
166 |
||
167 |
(* 'also' and 'finally' *) |
|
168 |
||
8588 | 169 |
fun err_if state b msg = if b then raise Proof.STATE (msg, state) else (); |
170 |
||
6877 | 171 |
fun calculate final opt_rules print state = |
6778 | 172 |
let |
12805 | 173 |
val strip_assums_concl = Logic.strip_assums_concl o Thm.prop_of; |
9322 | 174 |
val eq_prop = op aconv o pairself (Pattern.eta_contract o strip_assums_concl); |
11097 | 175 |
fun projection ths th = Library.exists (Library.curry eq_prop th) ths; |
8300 | 176 |
|
11097 | 177 |
fun combine ths = |
15531 | 178 |
(case opt_rules of SOME rules => rules |
179 |
| NONE => |
|
12379 | 180 |
(case ths of [] => NetRules.rules (#1 (get_local_rules state)) |
181 |
| th :: _ => NetRules.retrieve (#1 (get_local_rules state)) (strip_assums_concl th))) |
|
11097 | 182 |
|> Seq.of_list |> Seq.map (Method.multi_resolve ths) |> Seq.flat |
183 |
|> Seq.filter (not o projection ths); |
|
7414 | 184 |
|
14555
341908d6c792
'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents:
14549
diff
changeset
|
185 |
val facts = Proof.the_facts (assert_sane final state); |
6903 | 186 |
val (initial, calculations) = |
6778 | 187 |
(case get_calculation state of |
15531 | 188 |
NONE => (true, Seq.single facts) |
189 |
| SOME calc => (false, Seq.map single (combine (calc @ facts)))); |
|
6778 | 190 |
in |
8588 | 191 |
err_if state (initial andalso final) "No calculation yet"; |
15570 | 192 |
err_if state (initial andalso isSome opt_rules) "Initial calculation -- no rules to be given"; |
12055 | 193 |
calculations |> Seq.map (fn calc => (print (Proof.context_of state) calc; |
194 |
state |> maintain_calculation final calc)) |
|
6778 | 195 |
end; |
196 |
||
6782 | 197 |
fun also print = calculate false print; |
198 |
fun finally print = calculate true print; |
|
6778 | 199 |
|
200 |
||
8588 | 201 |
(* 'moreover' and 'ultimately' *) |
8562 | 202 |
|
8588 | 203 |
fun collect final print state = |
204 |
let |
|
14555
341908d6c792
'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents:
14549
diff
changeset
|
205 |
val facts = Proof.the_facts (assert_sane final state); |
8588 | 206 |
val (initial, thms) = |
207 |
(case get_calculation state of |
|
15531 | 208 |
NONE => (true, []) |
209 |
| SOME thms => (false, thms)); |
|
8588 | 210 |
val calc = thms @ facts; |
211 |
in |
|
212 |
err_if state (initial andalso final) "No calculation yet"; |
|
12055 | 213 |
print (Proof.context_of state) calc; |
8588 | 214 |
state |> maintain_calculation final calc |
215 |
end; |
|
216 |
||
217 |
fun moreover print = collect false print; |
|
218 |
fun ultimately print = collect true print; |
|
8562 | 219 |
|
220 |
||
6778 | 221 |
end; |