author | wenzelm |
Sat, 21 Nov 2009 17:01:44 +0100 | |
changeset 33834 | 7c06e19f717c |
parent 33519 | e31a85f92ce9 |
child 36323 | 655e2d74de3a |
permissions | -rw-r--r-- |
6778 | 1 |
(* Title: Pure/Isar/calculation.ML |
2 |
Author: Markus Wenzel, TU Muenchen |
|
3 |
||
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
4 |
Generic calculational proofs. |
6778 | 5 |
*) |
6 |
||
7 |
signature CALCULATION = |
|
8 |
sig |
|
21506 | 9 |
val print_rules: Proof.context -> unit |
16571 | 10 |
val get_calculation: Proof.state -> thm list option |
18728 | 11 |
val trans_add: attribute |
12 |
val trans_del: attribute |
|
13 |
val sym_add: attribute |
|
14 |
val sym_del: attribute |
|
15 |
val symmetric: attribute |
|
26336
a0e2b706ce73
renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents:
26252
diff
changeset
|
16 |
val also: (Facts.ref * Attrib.src list) list option -> bool -> Proof.state -> Proof.state Seq.seq |
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
17 |
val also_i: thm list option -> bool -> Proof.state -> Proof.state Seq.seq |
30189
3633f560f4c3
discontinued experimental support for Alice -- too hard to maintain its many language incompatibilities, never really worked anyway;
wenzelm
parents:
29606
diff
changeset
|
18 |
val finally: (Facts.ref * Attrib.src list) list option -> bool -> |
22573 | 19 |
Proof.state -> Proof.state Seq.seq |
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
20 |
val finally_i: thm list option -> bool -> Proof.state -> Proof.state Seq.seq |
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
21 |
val moreover: bool -> Proof.state -> Proof.state |
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
22 |
val ultimately: bool -> Proof.state -> Proof.state |
6778 | 23 |
end; |
24 |
||
25 |
structure Calculation: CALCULATION = |
|
26 |
struct |
|
27 |
||
18637 | 28 |
(** calculation data **) |
6778 | 29 |
|
33519 | 30 |
structure CalculationData = Generic_Data |
18637 | 31 |
( |
30560 | 32 |
type T = (thm Item_Net.T * thm list) * (thm list * int) option; |
33 |
val empty = ((Thm.elim_rules, []), NONE); |
|
18637 | 34 |
val extend = I; |
33519 | 35 |
fun merge (((trans1, sym1), _), ((trans2, sym2), _)) = |
30560 | 36 |
((Item_Net.merge (trans1, trans2), Thm.merge_thms (sym1, sym2)), NONE); |
18637 | 37 |
); |
38 |
||
22846 | 39 |
fun print_rules ctxt = |
40 |
let val ((trans, sym), _) = CalculationData.get (Context.Proof ctxt) in |
|
41 |
[Pretty.big_list "transitivity rules:" |
|
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:
30560
diff
changeset
|
42 |
(map (Display.pretty_thm ctxt) (Item_Net.content trans)), |
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents:
30560
diff
changeset
|
43 |
Pretty.big_list "symmetry rules:" (map (Display.pretty_thm ctxt) sym)] |
22846 | 44 |
|> Pretty.chunks |> Pretty.writeln |
45 |
end; |
|
6778 | 46 |
|
47 |
||
48 |
(* access calculation *) |
|
49 |
||
50 |
fun get_calculation state = |
|
18637 | 51 |
(case #2 (CalculationData.get (Context.Proof (Proof.context_of state))) of |
15531 | 52 |
NONE => NONE |
53 |
| SOME (thms, lev) => if lev = Proof.level state then SOME thms else NONE); |
|
6778 | 54 |
|
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
55 |
val calculationN = "calculation"; |
6778 | 56 |
|
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
57 |
fun put_calculation calc = |
18728 | 58 |
`Proof.level #-> (fn lev => Proof.map_context (Context.proof_map |
59 |
(CalculationData.map (apsnd (K (Option.map (rpair lev) calc)))))) |
|
26252 | 60 |
#> Proof.put_thms false (calculationN, calc); |
6778 | 61 |
|
62 |
||
18637 | 63 |
|
6778 | 64 |
(** attributes **) |
65 |
||
12379 | 66 |
(* add/del rules *) |
67 |
||
33373 | 68 |
val trans_add = Thm.declaration_attribute (CalculationData.map o apfst o apfst o Item_Net.update); |
69 |
val trans_del = Thm.declaration_attribute (CalculationData.map o apfst o apfst o Item_Net.remove); |
|
6778 | 70 |
|
18637 | 71 |
val sym_add = |
24039
273698405054
renamed Drule.add/del/merge_rules to Thm.add/del/merge_thms;
wenzelm
parents:
22846
diff
changeset
|
72 |
Thm.declaration_attribute (CalculationData.map o apfst o apsnd o Thm.add_thm) |
33369 | 73 |
#> Context_Rules.elim_query NONE; |
74 |
||
18637 | 75 |
val sym_del = |
24039
273698405054
renamed Drule.add/del/merge_rules to Thm.add/del/merge_thms;
wenzelm
parents:
22846
diff
changeset
|
76 |
Thm.declaration_attribute (CalculationData.map o apfst o apsnd o Thm.del_thm) |
33369 | 77 |
#> Context_Rules.rule_del; |
12379 | 78 |
|
79 |
||
18637 | 80 |
(* symmetric *) |
12379 | 81 |
|
18728 | 82 |
val symmetric = Thm.rule_attribute (fn x => fn th => |
19861 | 83 |
(case Seq.chop 2 (Drule.multi_resolves [th] (#2 (#1 (CalculationData.get x)))) of |
20898 | 84 |
([th'], _) => Drule.zero_var_indexes th' |
12379 | 85 |
| ([], _) => raise THM ("symmetric: no unifiers", 1, [th]) |
86 |
| _ => raise THM ("symmetric: multiple unifiers", 1, [th]))); |
|
87 |
||
6778 | 88 |
|
89 |
(* concrete syntax *) |
|
90 |
||
26463 | 91 |
val _ = Context.>> (Context.map_theory |
30528 | 92 |
(Attrib.setup (Binding.name "trans") (Attrib.add_del trans_add trans_del) |
93 |
"declaration of transitivity rule" #> |
|
94 |
Attrib.setup (Binding.name "sym") (Attrib.add_del sym_add sym_del) |
|
95 |
"declaration of symmetry rule" #> |
|
96 |
Attrib.setup (Binding.name "symmetric") (Scan.succeed symmetric) |
|
97 |
"resolution with symmetry rule" #> |
|
18708 | 98 |
PureThy.add_thms |
29579 | 99 |
[((Binding.empty, transitive_thm), [trans_add]), |
100 |
((Binding.empty, symmetric_thm), [sym_add])] #> snd)); |
|
15801 | 101 |
|
6778 | 102 |
|
6787 | 103 |
|
6778 | 104 |
(** proof commands **) |
105 |
||
18678 | 106 |
fun err_if b msg = if b then error msg else (); |
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
107 |
|
14555
341908d6c792
'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents:
14549
diff
changeset
|
108 |
fun assert_sane final = |
341908d6c792
'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents:
14549
diff
changeset
|
109 |
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
|
110 |
|
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
111 |
fun maintain_calculation false calc = put_calculation (SOME calc) |
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
112 |
| maintain_calculation true calc = put_calculation NONE #> Proof.chain_facts calc; |
6778 | 113 |
|
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
114 |
fun print_calculation false _ _ = () |
28210 | 115 |
| print_calculation true ctxt calc = Pretty.writeln |
30279 | 116 |
(ProofContext.pretty_fact ctxt (ProofContext.full_name ctxt (Binding.name calculationN), calc)); |
8562 | 117 |
|
118 |
||
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
119 |
(* also and finally *) |
8562 | 120 |
|
18637 | 121 |
val get_rules = #1 o CalculationData.get o Context.Proof o Proof.context_of; |
122 |
||
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
123 |
fun calculate prep_rules final raw_rules int state = |
6778 | 124 |
let |
12805 | 125 |
val strip_assums_concl = Logic.strip_assums_concl o Thm.prop_of; |
18947 | 126 |
val eq_prop = op aconv o pairself (Envir.beta_eta_contract o strip_assums_concl); |
11097 | 127 |
fun projection ths th = Library.exists (Library.curry eq_prop th) ths; |
8300 | 128 |
|
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
129 |
val opt_rules = Option.map (prep_rules state) raw_rules; |
11097 | 130 |
fun combine ths = |
15531 | 131 |
(case opt_rules of SOME rules => rules |
132 |
| NONE => |
|
33373 | 133 |
(case ths of |
134 |
[] => Item_Net.content (#1 (get_rules state)) |
|
30560 | 135 |
| th :: _ => Item_Net.retrieve (#1 (get_rules state)) (strip_assums_concl th))) |
18223 | 136 |
|> Seq.of_list |> Seq.maps (Drule.multi_resolve ths) |
11097 | 137 |
|> Seq.filter (not o projection ths); |
7414 | 138 |
|
14555
341908d6c792
'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents:
14549
diff
changeset
|
139 |
val facts = Proof.the_facts (assert_sane final state); |
6903 | 140 |
val (initial, calculations) = |
6778 | 141 |
(case get_calculation state of |
15531 | 142 |
NONE => (true, Seq.single facts) |
143 |
| SOME calc => (false, Seq.map single (combine (calc @ facts)))); |
|
6778 | 144 |
in |
18678 | 145 |
err_if (initial andalso final) "No calculation yet"; |
146 |
err_if (initial andalso is_some opt_rules) "Initial calculation -- no rules to be given"; |
|
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
147 |
calculations |> Seq.map (fn calc => (print_calculation int (Proof.context_of state) calc; |
12055 | 148 |
state |> maintain_calculation final calc)) |
6778 | 149 |
end; |
150 |
||
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
151 |
val also = calculate Proof.get_thmss false; |
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
152 |
val also_i = calculate (K I) false; |
30189
3633f560f4c3
discontinued experimental support for Alice -- too hard to maintain its many language incompatibilities, never really worked anyway;
wenzelm
parents:
29606
diff
changeset
|
153 |
val finally = calculate Proof.get_thmss true; |
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
154 |
val finally_i = calculate (K I) true; |
6778 | 155 |
|
156 |
||
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
157 |
(* moreover and ultimately *) |
8562 | 158 |
|
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
159 |
fun collect final int state = |
8588 | 160 |
let |
14555
341908d6c792
'also'/'moreover': do not interfere with current facts, allow in chain mode;
wenzelm
parents:
14549
diff
changeset
|
161 |
val facts = Proof.the_facts (assert_sane final state); |
8588 | 162 |
val (initial, thms) = |
163 |
(case get_calculation state of |
|
15531 | 164 |
NONE => (true, []) |
165 |
| SOME thms => (false, thms)); |
|
8588 | 166 |
val calc = thms @ facts; |
167 |
in |
|
18678 | 168 |
err_if (initial andalso final) "No calculation yet"; |
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
169 |
print_calculation int (Proof.context_of state) calc; |
8588 | 170 |
state |> maintain_calculation final calc |
171 |
end; |
|
172 |
||
17350
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
173 |
val moreover = collect false; |
26cd3756377a
more self-contained proof elements (material from isar_thy.ML);
wenzelm
parents:
16571
diff
changeset
|
174 |
val ultimately = collect true; |
8562 | 175 |
|
6778 | 176 |
end; |