author | wenzelm |
Sun, 15 Jan 2006 19:58:55 +0100 | |
changeset 18692 | 2270e25e9128 |
parent 18667 | 85d04c28224a |
child 18708 | 4b3dadb4fe33 |
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 |
||
5 |
Declarations of intro/elim/dest rules in Pure; see |
|
6 |
Provers/classical.ML for a more specialized version of the same idea. |
|
7 |
*) |
|
8 |
||
9 |
signature CONTEXT_RULES = |
|
10 |
sig |
|
11 |
type netpair |
|
12 |
type T |
|
13372
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
13 |
val netpair_bang: ProofContext.context -> netpair |
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
14 |
val netpair: ProofContext.context -> netpair |
12350 | 15 |
val orderlist: ((int * int) * 'a) list -> 'a list |
12399 | 16 |
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
|
17 |
val find_rules: bool -> thm list -> term -> ProofContext.context -> thm list list |
18637 | 18 |
val print_rules: Context.generic -> unit |
12350 | 19 |
val addSWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory |
20 |
val addWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory |
|
13372
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
21 |
val Swrap: ProofContext.context -> (int -> tactic) -> int -> tactic |
18790d503fe0
module now right after ProofContext (for locales);
wenzelm
parents:
13105
diff
changeset
|
22 |
val wrap: ProofContext.context -> (int -> tactic) -> int -> tactic |
18637 | 23 |
val intro_bang: int option -> Context.generic attribute |
24 |
val elim_bang: int option -> Context.generic attribute |
|
25 |
val dest_bang: int option -> Context.generic attribute |
|
26 |
val intro: int option -> Context.generic attribute |
|
27 |
val elim: int option -> Context.generic attribute |
|
28 |
val dest: int option -> Context.generic attribute |
|
29 |
val intro_query: int option -> Context.generic attribute |
|
30 |
val elim_query: int option -> Context.generic attribute |
|
31 |
val dest_query: int option -> Context.generic attribute |
|
32 |
val rule_del: Context.generic attribute |
|
18692 | 33 |
val add_args: (int option -> 'a attribute) -> (int option -> 'a attribute) -> |
34 |
(int option -> 'a attribute) -> Attrib.src -> 'a attribute |
|
12350 | 35 |
end; |
36 |
||
37 |
structure ContextRules: CONTEXT_RULES = |
|
38 |
struct |
|
39 |
||
40 |
||
41 |
(** rule declaration contexts **) |
|
42 |
||
43 |
(* rule kinds *) |
|
44 |
||
45 |
val intro_bangK = (0, false); |
|
46 |
val elim_bangK = (0, true); |
|
47 |
val introK = (1, false); |
|
48 |
val elimK = (1, true); |
|
49 |
val intro_queryK = (2, false); |
|
50 |
val elim_queryK = (2, true); |
|
51 |
||
52 |
val kind_names = |
|
53 |
[(intro_bangK, "safe introduction rules (intro!)"), |
|
54 |
(elim_bangK, "safe elimination rules (elim!)"), |
|
55 |
(introK, "introduction rules (intro)"), |
|
56 |
(elimK, "elimination rules (elim)"), |
|
57 |
(intro_queryK, "extra introduction rules (intro?)"), |
|
58 |
(elim_queryK, "extra elimination rules (elim?)")]; |
|
59 |
||
60 |
val rule_kinds = map #1 kind_names; |
|
61 |
val rule_indexes = distinct (map #1 rule_kinds); |
|
62 |
||
63 |
||
18637 | 64 |
(* context data *) |
12350 | 65 |
|
66 |
type netpair = ((int * int) * (bool * thm)) Net.net * ((int * int) * (bool * thm)) Net.net; |
|
67 |
val empty_netpairs: netpair list = replicate (length rule_indexes) (Net.empty, Net.empty); |
|
68 |
||
69 |
datatype T = Rules of |
|
70 |
{next: int, |
|
71 |
rules: (int * ((int * bool) * thm)) list, |
|
72 |
netpairs: netpair list, |
|
73 |
wrappers: (((int -> tactic) -> int -> tactic) * stamp) list * |
|
74 |
(((int -> tactic) -> int -> tactic) * stamp) list}; |
|
75 |
||
76 |
fun make_rules next rules netpairs wrappers = |
|
77 |
Rules {next = next, rules = rules, netpairs = netpairs, wrappers = wrappers}; |
|
78 |
||
79 |
fun add_rule (i, b) opt_w th (Rules {next, rules, netpairs, wrappers}) = |
|
15531 | 80 |
let val w = (case opt_w of SOME w => w | NONE => Tactic.subgoals_of_brl (b, th)) in |
12350 | 81 |
make_rules (next - 1) ((w, ((i, b), th)) :: rules) |
18011
685d95c793ff
cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents:
17511
diff
changeset
|
82 |
(nth_map i (curry insert_tagged_brl ((w, next), (b, th))) netpairs) wrappers |
12350 | 83 |
end; |
84 |
||
85 |
fun del_rule th (rs as Rules {next, rules, netpairs, wrappers}) = |
|
86 |
let |
|
13105
3d1e7a199bdc
use eq_thm_prop instead of slightly inadequate eq_thm;
wenzelm
parents:
12805
diff
changeset
|
87 |
fun eq_th (_, (_, th')) = Drule.eq_thm_prop (th, th'); |
12350 | 88 |
fun del b netpair = delete_tagged_brl ((b, th), netpair) handle Net.DELETE => netpair; |
89 |
in |
|
90 |
if not (exists eq_th rules) then rs |
|
91 |
else make_rules next (filter_out eq_th rules) (map (del false o del true) netpairs) wrappers |
|
92 |
end; |
|
93 |
||
18637 | 94 |
structure Rules = GenericDataFun |
95 |
( |
|
17511 | 96 |
val name = "Isar/rules"; |
12350 | 97 |
type T = T; |
98 |
||
99 |
val empty = make_rules ~1 [] empty_netpairs ([], []); |
|
16424 | 100 |
val extend = I; |
12350 | 101 |
|
16424 | 102 |
fun merge _ (Rules {rules = rules1, wrappers = (ws1, ws1'), ...}, |
12350 | 103 |
Rules {rules = rules2, wrappers = (ws2, ws2'), ...}) = |
104 |
let |
|
18637 | 105 |
val wrappers = |
106 |
(gen_merge_lists' (eq_snd (op =)) ws1 ws2, gen_merge_lists' (eq_snd (op =)) ws1' ws2'); |
|
12350 | 107 |
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
|
108 |
k1 = k2 andalso Drule.eq_thm_prop (th1, th2)) rules1 rules2; |
12350 | 109 |
val next = ~ (length rules); |
15570 | 110 |
val netpairs = Library.foldl (fn (nps, (n, (w, ((i, b), th)))) => |
18011
685d95c793ff
cleaned up nth, nth_update, nth_map and nth_string functions
haftmann
parents:
17511
diff
changeset
|
111 |
nth_map i (curry insert_tagged_brl ((w, n), (b, th))) nps) |
12350 | 112 |
(empty_netpairs, next upto ~1 ~~ rules); |
18637 | 113 |
in make_rules (next - 1) rules netpairs wrappers end |
12350 | 114 |
|
18637 | 115 |
fun print generic (Rules {rules, ...}) = |
116 |
let |
|
117 |
val ctxt = Context.proof_of generic; |
|
118 |
fun prt_kind (i, b) = |
|
119 |
Pretty.big_list ((the o AList.lookup (op =) kind_names) (i, b) ^ ":") |
|
120 |
(List.mapPartial (fn (_, (k, th)) => |
|
121 |
if k = (i, b) then SOME (ProofContext.pretty_thm ctxt th) else NONE) |
|
122 |
(sort (int_ord o pairself fst) rules)); |
|
123 |
in Pretty.writeln (Pretty.chunks (map prt_kind rule_kinds)) end; |
|
124 |
); |
|
12350 | 125 |
|
18637 | 126 |
val _ = Context.add_setup [Rules.init]; |
127 |
val print_rules = Rules.print; |
|
12350 | 128 |
|
129 |
||
130 |
(* access data *) |
|
131 |
||
18637 | 132 |
fun netpairs ctxt = let val Rules {netpairs, ...} = Rules.get (Context.Proof ctxt) in netpairs end; |
12350 | 133 |
val netpair_bang = hd o netpairs; |
134 |
val netpair = hd o tl o netpairs; |
|
135 |
||
136 |
||
12399 | 137 |
(* retrieving rules *) |
138 |
||
12350 | 139 |
fun untaglist [] = [] |
140 |
| untaglist [(k : int * int, x)] = [x] |
|
141 |
| untaglist ((k, x) :: (rest as (k', x') :: _)) = |
|
142 |
if k = k' then untaglist rest |
|
143 |
else x :: untaglist rest; |
|
144 |
||
16424 | 145 |
fun orderlist brls = |
16512 | 146 |
untaglist (sort (prod_ord int_ord int_ord o pairself fst) brls); |
16424 | 147 |
|
148 |
fun orderlist_no_weight brls = |
|
16512 | 149 |
untaglist (sort (int_ord o pairself (snd o fst)) brls); |
12399 | 150 |
|
151 |
fun may_unify weighted t net = |
|
152 |
map snd ((if weighted then orderlist else orderlist_no_weight) (Net.unify_term net t)); |
|
153 |
||
154 |
fun find_erules _ [] = K [] |
|
12805 | 155 |
| find_erules w (fact :: _) = may_unify w (Logic.strip_assums_concl (Thm.prop_of fact)); |
16424 | 156 |
|
12399 | 157 |
fun find_irules w goal = may_unify w (Logic.strip_assums_concl goal); |
12380 | 158 |
|
12399 | 159 |
fun find_rules_netpair weighted facts goal (inet, enet) = |
160 |
find_erules weighted facts enet @ find_irules weighted goal inet; |
|
12380 | 161 |
|
16424 | 162 |
fun find_rules weighted facts goals = |
163 |
map (find_rules_netpair weighted facts goals) o netpairs; |
|
12350 | 164 |
|
165 |
||
12399 | 166 |
(* wrappers *) |
167 |
||
18667 | 168 |
fun gen_add_wrapper upd w = |
169 |
Context.map_theory (Rules.map (fn (rs as Rules {next, rules, netpairs, wrappers}) => |
|
170 |
make_rules next rules netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers))); |
|
12350 | 171 |
|
172 |
val addSWrapper = gen_add_wrapper Library.apfst; |
|
173 |
val addWrapper = gen_add_wrapper Library.apsnd; |
|
174 |
||
175 |
||
176 |
fun gen_wrap which ctxt = |
|
18637 | 177 |
let val Rules {wrappers, ...} = Rules.get (Context.Proof ctxt) |
17351 | 178 |
in fold_rev fst (which wrappers) end; |
12350 | 179 |
|
180 |
val Swrap = gen_wrap #1; |
|
181 |
val wrap = gen_wrap #2; |
|
182 |
||
183 |
||
184 |
||
185 |
(** attributes **) |
|
186 |
||
187 |
(* add and del rules *) |
|
188 |
||
18637 | 189 |
fun rule_del (x, th) = |
190 |
(Rules.map (del_rule th o del_rule (Tactic.make_elim th)) x, th); |
|
12350 | 191 |
|
18637 | 192 |
fun rule_add k view opt_w = |
193 |
(fn (x, th) => (Rules.map (add_rule k opt_w (view th)) x, th)) o rule_del; |
|
12350 | 194 |
|
18637 | 195 |
val intro_bang = rule_add intro_bangK I; |
196 |
val elim_bang = rule_add elim_bangK I; |
|
197 |
val dest_bang = rule_add elim_bangK Tactic.make_elim; |
|
198 |
val intro = rule_add introK I; |
|
199 |
val elim = rule_add elimK I; |
|
200 |
val dest = rule_add elimK Tactic.make_elim; |
|
201 |
val intro_query = rule_add intro_queryK I; |
|
202 |
val elim_query = rule_add elim_queryK I; |
|
203 |
val dest_query = rule_add elim_queryK Tactic.make_elim; |
|
12350 | 204 |
|
18637 | 205 |
val _ = Context.add_setup |
206 |
[snd o PureThy.add_thms [(("", Drule.equal_intr_rule), [Attrib.theory (intro_query NONE)])]]; |
|
207 |
||
12350 | 208 |
|
18637 | 209 |
(* concrete syntax *) |
210 |
||
211 |
fun add_args a b c x = Attrib.syntax |
|
18692 | 212 |
(Scan.lift ((Args.bang >> K a || Args.query >> K c || Scan.succeed b) -- Scan.option Args.nat) |
18637 | 213 |
>> (fn (f, n) => f n)) x; |
214 |
||
215 |
val rule_atts = |
|
216 |
[("intro", Attrib.common (add_args intro_bang intro intro_query), |
|
217 |
"declaration of introduction rule"), |
|
218 |
("elim", Attrib.common (add_args elim_bang elim elim_query), |
|
219 |
"declaration of elimination rule"), |
|
220 |
("dest", Attrib.common (add_args dest_bang dest dest_query), |
|
221 |
"declaration of destruction rule"), |
|
18692 | 222 |
("rule", Attrib.common (Attrib.syntax (Scan.lift Args.del >> K rule_del)), |
18637 | 223 |
"remove declaration of intro/elim/dest rule")]; |
224 |
||
225 |
val _ = Context.add_setup [Attrib.add_attributes rule_atts]; |
|
12350 | 226 |
|
227 |
end; |