author | paulson |
Fri, 19 Mar 2004 10:42:38 +0100 | |
changeset 14472 | cba7c0a3ffb3 |
parent 13372 | 18790d503fe0 |
child 14981 | e73f8140af78 |
permissions | -rw-r--r-- |
12350 | 1 |
(* Title: Pure/Isar/context_rules.ML |
2 |
ID: $Id$ |
|
3 |
Author: Stefan Berghofer and Markus Wenzel, TU Muenchen |
|
4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
|
5 |
||
6 |
Declarations of intro/elim/dest rules in Pure; see |
|
7 |
Provers/classical.ML for a more specialized version of the same idea. |
|
8 |
*) |
|
9 |
||
10 |
signature CONTEXT_RULES = |
|
11 |
sig |
|
12 |
type netpair |
|
13 |
type T |
|
13372
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
14 |
val netpair_bang: ProofContext.context -> netpair |
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
15 |
val netpair: ProofContext.context -> netpair |
12350 | 16 |
val orderlist: ((int * int) * 'a) list -> 'a list |
12399 | 17 |
val find_rules_netpair: bool -> thm list -> term -> netpair -> thm list |
13372
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
18 |
val find_rules: bool -> thm list -> term -> ProofContext.context -> thm list list |
12350 | 19 |
val print_global_rules: theory -> unit |
13372
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
20 |
val print_local_rules: ProofContext.context -> unit |
12350 | 21 |
val addSWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory |
22 |
val addWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory |
|
13372
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
23 |
val Swrap: ProofContext.context -> (int -> tactic) -> int -> tactic |
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
24 |
val wrap: ProofContext.context -> (int -> tactic) -> int -> tactic |
12350 | 25 |
val intro_bang_global: int option -> theory attribute |
26 |
val elim_bang_global: int option -> theory attribute |
|
27 |
val dest_bang_global: int option -> theory attribute |
|
28 |
val intro_global: int option -> theory attribute |
|
29 |
val elim_global: int option -> theory attribute |
|
30 |
val dest_global: int option -> theory attribute |
|
31 |
val intro_query_global: int option -> theory attribute |
|
32 |
val elim_query_global: int option -> theory attribute |
|
33 |
val dest_query_global: int option -> theory attribute |
|
34 |
val rule_del_global: theory attribute |
|
13372
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
35 |
val intro_bang_local: int option -> ProofContext.context attribute |
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
36 |
val elim_bang_local: int option -> ProofContext.context attribute |
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
37 |
val dest_bang_local: int option -> ProofContext.context attribute |
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
38 |
val intro_local: int option -> ProofContext.context attribute |
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
39 |
val elim_local: int option -> ProofContext.context attribute |
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
40 |
val dest_local: int option -> ProofContext.context attribute |
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
41 |
val intro_query_local: int option -> ProofContext.context attribute |
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
42 |
val elim_query_local: int option -> ProofContext.context attribute |
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
43 |
val dest_query_local: int option -> ProofContext.context attribute |
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
44 |
val rule_del_local: ProofContext.context attribute |
12380 | 45 |
val addXIs_global: theory * thm list -> theory |
46 |
val addXEs_global: theory * thm list -> theory |
|
47 |
val addXDs_global: theory * thm list -> theory |
|
48 |
val delrules_global: theory * thm list -> theory |
|
13372
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
49 |
val addXIs_local: ProofContext.context * thm list -> ProofContext.context |
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
50 |
val addXEs_local: ProofContext.context * thm list -> ProofContext.context |
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
51 |
val addXDs_local: ProofContext.context * thm list -> ProofContext.context |
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
52 |
val delrules_local: ProofContext.context * thm list -> ProofContext.context |
12350 | 53 |
val setup: (theory -> theory) list |
54 |
end; |
|
55 |
||
56 |
structure ContextRules: CONTEXT_RULES = |
|
57 |
struct |
|
58 |
||
59 |
||
60 |
(** rule declaration contexts **) |
|
61 |
||
62 |
(* rule kinds *) |
|
63 |
||
64 |
val intro_bangK = (0, false); |
|
65 |
val elim_bangK = (0, true); |
|
66 |
val introK = (1, false); |
|
67 |
val elimK = (1, true); |
|
68 |
val intro_queryK = (2, false); |
|
69 |
val elim_queryK = (2, true); |
|
70 |
||
71 |
val kind_names = |
|
72 |
[(intro_bangK, "safe introduction rules (intro!)"), |
|
73 |
(elim_bangK, "safe elimination rules (elim!)"), |
|
74 |
(introK, "introduction rules (intro)"), |
|
75 |
(elimK, "elimination rules (elim)"), |
|
76 |
(intro_queryK, "extra introduction rules (intro?)"), |
|
77 |
(elim_queryK, "extra elimination rules (elim?)")]; |
|
78 |
||
79 |
val rule_kinds = map #1 kind_names; |
|
80 |
val rule_indexes = distinct (map #1 rule_kinds); |
|
81 |
||
82 |
||
83 |
(* raw data *) |
|
84 |
||
85 |
type netpair = ((int * int) * (bool * thm)) Net.net * ((int * int) * (bool * thm)) Net.net; |
|
86 |
val empty_netpairs: netpair list = replicate (length rule_indexes) (Net.empty, Net.empty); |
|
87 |
||
88 |
datatype T = Rules of |
|
89 |
{next: int, |
|
90 |
rules: (int * ((int * bool) * thm)) list, |
|
91 |
netpairs: netpair list, |
|
92 |
wrappers: (((int -> tactic) -> int -> tactic) * stamp) list * |
|
93 |
(((int -> tactic) -> int -> tactic) * stamp) list}; |
|
94 |
||
95 |
fun make_rules next rules netpairs wrappers = |
|
96 |
Rules {next = next, rules = rules, netpairs = netpairs, wrappers = wrappers}; |
|
97 |
||
98 |
fun add_rule (i, b) opt_w th (Rules {next, rules, netpairs, wrappers}) = |
|
99 |
let val w = (case opt_w of Some w => w | None => Tactic.subgoals_of_brl (b, th)) in |
|
100 |
make_rules (next - 1) ((w, ((i, b), th)) :: rules) |
|
101 |
(map_nth_elem i (curry insert_tagged_brl ((w, next), (b, th))) netpairs) wrappers |
|
102 |
end; |
|
103 |
||
104 |
fun del_rule th (rs as Rules {next, rules, netpairs, wrappers}) = |
|
105 |
let |
|
13105
3d1e7a199bdc
use eq_thm_prop instead of slightly inadequate eq_thm;
wenzelm
parents:
12805
diff
changeset
|
106 |
fun eq_th (_, (_, th')) = Drule.eq_thm_prop (th, th'); |
12350 | 107 |
fun del b netpair = delete_tagged_brl ((b, th), netpair) handle Net.DELETE => netpair; |
108 |
in |
|
109 |
if not (exists eq_th rules) then rs |
|
110 |
else make_rules next (filter_out eq_th rules) (map (del false o del true) netpairs) wrappers |
|
111 |
end; |
|
112 |
||
113 |
fun print_rules prt x (Rules {rules, ...}) = |
|
114 |
let |
|
115 |
fun prt_kind (i, b) = |
|
116 |
Pretty.big_list (the (assoc (kind_names, (i, b))) ^ ":") |
|
117 |
(mapfilter (fn (_, (k, th)) => if k = (i, b) then Some (prt x th) else None) |
|
14472
cba7c0a3ffb3
Removing the datatype declaration of "order" allows the standard General.order
paulson
parents:
13372
diff
changeset
|
118 |
(sort (Int.compare o pairself fst) rules)); |
12350 | 119 |
in Pretty.writeln (Pretty.chunks (map prt_kind rule_kinds)) end; |
120 |
||
121 |
||
122 |
(* theory and proof data *) |
|
123 |
||
124 |
structure GlobalRulesArgs = |
|
125 |
struct |
|
126 |
val name = "Isar/rule_context"; |
|
127 |
type T = T; |
|
128 |
||
129 |
val empty = make_rules ~1 [] empty_netpairs ([], []); |
|
130 |
val copy = I; |
|
131 |
val prep_ext = I; |
|
132 |
||
133 |
fun merge (Rules {rules = rules1, wrappers = (ws1, ws1'), ...}, |
|
134 |
Rules {rules = rules2, wrappers = (ws2, ws2'), ...}) = |
|
135 |
let |
|
136 |
val wrappers = (gen_merge_lists' eq_snd ws1 ws2, gen_merge_lists' eq_snd ws1' ws2'); |
|
137 |
val rules = gen_merge_lists' (fn ((_, (k1, th1)), (_, (k2, th2))) => |
|
13105
3d1e7a199bdc
use eq_thm_prop instead of slightly inadequate eq_thm;
wenzelm
parents:
12805
diff
changeset
|
138 |
k1 = k2 andalso Drule.eq_thm_prop (th1, th2)) rules1 rules2; |
12350 | 139 |
val next = ~ (length rules); |
140 |
val netpairs = foldl (fn (nps, (n, (w, ((i, b), th)))) => |
|
141 |
map_nth_elem i (curry insert_tagged_brl ((w, n), (b, th))) nps) |
|
142 |
(empty_netpairs, next upto ~1 ~~ rules); |
|
143 |
in make_rules (next - 1) rules netpairs wrappers end; |
|
144 |
||
145 |
val print = print_rules Display.pretty_thm_sg; |
|
146 |
end; |
|
147 |
||
148 |
structure GlobalRules = TheoryDataFun(GlobalRulesArgs); |
|
149 |
val print_global_rules = GlobalRules.print; |
|
150 |
||
151 |
structure LocalRulesArgs = |
|
152 |
struct |
|
153 |
val name = GlobalRulesArgs.name; |
|
154 |
type T = GlobalRulesArgs.T; |
|
155 |
val init = GlobalRules.get; |
|
156 |
val print = print_rules ProofContext.pretty_thm; |
|
157 |
end; |
|
158 |
||
159 |
structure LocalRules = ProofDataFun(LocalRulesArgs); |
|
160 |
val print_local_rules = LocalRules.print; |
|
161 |
||
162 |
||
163 |
(* access data *) |
|
164 |
||
165 |
fun netpairs ctxt = let val Rules {netpairs, ...} = LocalRules.get ctxt in netpairs end; |
|
166 |
val netpair_bang = hd o netpairs; |
|
167 |
val netpair = hd o tl o netpairs; |
|
168 |
||
169 |
||
12399 | 170 |
(* retrieving rules *) |
171 |
||
12350 | 172 |
fun untaglist [] = [] |
173 |
| untaglist [(k : int * int, x)] = [x] |
|
174 |
| untaglist ((k, x) :: (rest as (k', x') :: _)) = |
|
175 |
if k = k' then untaglist rest |
|
176 |
else x :: untaglist rest; |
|
177 |
||
14472
cba7c0a3ffb3
Removing the datatype declaration of "order" allows the standard General.order
paulson
parents:
13372
diff
changeset
|
178 |
fun orderlist brls = |
cba7c0a3ffb3
Removing the datatype declaration of "order" allows the standard General.order
paulson
parents:
13372
diff
changeset
|
179 |
untaglist (sort (prod_ord Int.compare Int.compare o pairself fst) brls); |
cba7c0a3ffb3
Removing the datatype declaration of "order" allows the standard General.order
paulson
parents:
13372
diff
changeset
|
180 |
fun orderlist_no_weight brls = |
cba7c0a3ffb3
Removing the datatype declaration of "order" allows the standard General.order
paulson
parents:
13372
diff
changeset
|
181 |
untaglist (sort (Int.compare o pairself (snd o fst)) brls); |
12399 | 182 |
|
183 |
fun may_unify weighted t net = |
|
184 |
map snd ((if weighted then orderlist else orderlist_no_weight) (Net.unify_term net t)); |
|
185 |
||
186 |
fun find_erules _ [] = K [] |
|
12805 | 187 |
| find_erules w (fact :: _) = may_unify w (Logic.strip_assums_concl (Thm.prop_of fact)); |
12399 | 188 |
fun find_irules w goal = may_unify w (Logic.strip_assums_concl goal); |
12380 | 189 |
|
12399 | 190 |
fun find_rules_netpair weighted facts goal (inet, enet) = |
191 |
find_erules weighted facts enet @ find_irules weighted goal inet; |
|
12380 | 192 |
|
12399 | 193 |
fun find_rules weighted facts goals = map (find_rules_netpair weighted facts goals) o netpairs; |
12350 | 194 |
|
195 |
||
12399 | 196 |
(* wrappers *) |
197 |
||
12350 | 198 |
fun gen_add_wrapper upd w = GlobalRules.map (fn (rs as Rules {next, rules, netpairs, wrappers}) => |
199 |
make_rules next rules netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers)); |
|
200 |
||
201 |
val addSWrapper = gen_add_wrapper Library.apfst; |
|
202 |
val addWrapper = gen_add_wrapper Library.apsnd; |
|
203 |
||
204 |
||
205 |
fun gen_wrap which ctxt = |
|
206 |
let val Rules {wrappers, ...} = LocalRules.get ctxt |
|
207 |
in fn tac => foldr (fn ((w, _), t) => w t) (which wrappers, tac) end; |
|
208 |
||
209 |
val Swrap = gen_wrap #1; |
|
210 |
val wrap = gen_wrap #2; |
|
211 |
||
212 |
||
213 |
||
214 |
(** attributes **) |
|
215 |
||
216 |
(* add and del rules *) |
|
217 |
||
218 |
local |
|
219 |
||
220 |
fun del map_data (x, th) = |
|
221 |
(map_data (del_rule th o del_rule (Tactic.make_elim th)) x, th); |
|
222 |
||
223 |
fun add k view map_data opt_w = |
|
12412 | 224 |
(fn (x, th) => (map_data (add_rule k opt_w (view th)) x, th)) o del map_data; |
12350 | 225 |
|
226 |
in |
|
227 |
||
228 |
val intro_bang_global = add intro_bangK I GlobalRules.map; |
|
229 |
val elim_bang_global = add elim_bangK I GlobalRules.map; |
|
230 |
val dest_bang_global = add elim_bangK Tactic.make_elim GlobalRules.map; |
|
231 |
val intro_global = add introK I GlobalRules.map; |
|
232 |
val elim_global = add elimK I GlobalRules.map; |
|
233 |
val dest_global = add elimK Tactic.make_elim GlobalRules.map; |
|
234 |
val intro_query_global = add intro_queryK I GlobalRules.map; |
|
235 |
val elim_query_global = add elim_queryK I GlobalRules.map; |
|
236 |
val dest_query_global = add elim_queryK Tactic.make_elim GlobalRules.map; |
|
237 |
val rule_del_global = del GlobalRules.map; |
|
238 |
||
239 |
val intro_bang_local = add intro_bangK I LocalRules.map; |
|
240 |
val elim_bang_local = add elim_bangK I LocalRules.map; |
|
241 |
val dest_bang_local = add elim_bangK Tactic.make_elim LocalRules.map; |
|
242 |
val intro_local = add introK I LocalRules.map; |
|
243 |
val elim_local = add elimK I LocalRules.map; |
|
244 |
val dest_local = add elimK Tactic.make_elim LocalRules.map; |
|
245 |
val intro_query_local = add intro_queryK I LocalRules.map; |
|
246 |
val elim_query_local = add elim_queryK I LocalRules.map; |
|
247 |
val dest_query_local = add elim_queryK Tactic.make_elim LocalRules.map; |
|
248 |
val rule_del_local = del LocalRules.map; |
|
249 |
||
250 |
end; |
|
251 |
||
252 |
||
12380 | 253 |
(* low-level modifiers *) |
254 |
||
255 |
fun modifier att (x, ths) = |
|
256 |
#1 (Thm.applys_attributes ((x, rev ths), [att])); |
|
257 |
||
258 |
val addXIs_global = modifier (intro_query_global None); |
|
259 |
val addXEs_global = modifier (elim_query_global None); |
|
260 |
val addXDs_global = modifier (dest_query_global None); |
|
261 |
val delrules_global = modifier rule_del_global; |
|
262 |
||
263 |
val addXIs_local = modifier (intro_query_local None); |
|
264 |
val addXEs_local = modifier (elim_query_local None); |
|
265 |
val addXDs_local = modifier (dest_query_local None); |
|
266 |
val delrules_local = modifier rule_del_local; |
|
267 |
||
268 |
||
12350 | 269 |
|
270 |
(** theory setup **) |
|
271 |
||
272 |
val setup = |
|
13372
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
273 |
[GlobalRules.init, LocalRules.init]; |
12350 | 274 |
|
275 |
||
276 |
end; |