author | wenzelm |
Sun, 06 Jul 2025 11:33:23 +0200 | |
changeset 82812 | ea8d633fd4a8 |
parent 82808 | cb93bd70c561 |
child 82816 | ad5a3159b95d |
permissions | -rw-r--r-- |
12350 | 1 |
(* Title: Pure/Isar/context_rules.ML |
2 |
Author: Stefan Berghofer and Markus Wenzel, TU Muenchen |
|
3 |
||
18728 | 4 |
Declarations of intro/elim/dest rules in Pure (see also |
5 |
Provers/classical.ML for a more specialized version of the same idea). |
|
12350 | 6 |
*) |
7 |
||
8 |
signature CONTEXT_RULES = |
|
9 |
sig |
|
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
10 |
val netpair_bang: Proof.context -> Bires.netpair |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
11 |
val netpair: Proof.context -> Bires.netpair |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
12 |
val find_rules_netpair: Proof.context -> bool -> thm list -> term -> Bires.netpair -> thm list |
61049 | 13 |
val find_rules: Proof.context -> bool -> thm list -> term -> thm list list |
21506 | 14 |
val print_rules: Proof.context -> unit |
51798 | 15 |
val addSWrapper: (Proof.context -> (int -> tactic) -> int -> tactic) -> theory -> theory |
16 |
val addWrapper: (Proof.context -> (int -> tactic) -> int -> tactic) -> theory -> theory |
|
20289 | 17 |
val Swrap: Proof.context -> (int -> tactic) -> int -> tactic |
18 |
val wrap: Proof.context -> (int -> tactic) -> int -> tactic |
|
18728 | 19 |
val intro_bang: int option -> attribute |
20 |
val elim_bang: int option -> attribute |
|
21 |
val dest_bang: int option -> attribute |
|
22 |
val intro: int option -> attribute |
|
23 |
val elim: int option -> attribute |
|
24 |
val dest: int option -> attribute |
|
25 |
val intro_query: int option -> attribute |
|
26 |
val elim_query: int option -> attribute |
|
27 |
val dest_query: int option -> attribute |
|
28 |
val rule_del: attribute |
|
30529 | 29 |
val add: (int option -> attribute) -> (int option -> attribute) -> (int option -> attribute) -> |
30 |
attribute context_parser |
|
12350 | 31 |
end; |
32 |
||
33369 | 33 |
structure Context_Rules: CONTEXT_RULES = |
12350 | 34 |
struct |
35 |
||
36 |
||
37 |
(** rule declaration contexts **) |
|
38 |
||
39 |
(* rule kinds *) |
|
40 |
||
41 |
val intro_bangK = (0, false); |
|
42 |
val elim_bangK = (0, true); |
|
43 |
val introK = (1, false); |
|
44 |
val elimK = (1, true); |
|
45 |
val intro_queryK = (2, false); |
|
46 |
val elim_queryK = (2, true); |
|
47 |
||
48 |
val kind_names = |
|
49 |
[(intro_bangK, "safe introduction rules (intro!)"), |
|
50 |
(elim_bangK, "safe elimination rules (elim!)"), |
|
51 |
(introK, "introduction rules (intro)"), |
|
52 |
(elimK, "elimination rules (elim)"), |
|
53 |
(intro_queryK, "extra introduction rules (intro?)"), |
|
54 |
(elim_queryK, "extra elimination rules (elim?)")]; |
|
55 |
||
56 |
val rule_kinds = map #1 kind_names; |
|
19046
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
wenzelm
parents:
18977
diff
changeset
|
57 |
val rule_indexes = distinct (op =) (map #1 rule_kinds); |
12350 | 58 |
|
59 |
||
18637 | 60 |
(* context data *) |
12350 | 61 |
|
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
62 |
val empty_netpairs: Bires.netpair list = |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
63 |
replicate (length rule_indexes) Bires.empty_netpair; |
12350 | 64 |
|
33370 | 65 |
datatype rules = Rules of |
12350 | 66 |
{next: int, |
67 |
rules: (int * ((int * bool) * thm)) list, |
|
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
68 |
netpairs: Bires.netpair list, |
51798 | 69 |
wrappers: |
70 |
((Proof.context -> (int -> tactic) -> int -> tactic) * stamp) list * |
|
71 |
((Proof.context -> (int -> tactic) -> int -> tactic) * stamp) list}; |
|
12350 | 72 |
|
73 |
fun make_rules next rules netpairs wrappers = |
|
74 |
Rules {next = next, rules = rules, netpairs = netpairs, wrappers = wrappers}; |
|
75 |
||
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
76 |
fun add_rule (i, b) opt_weight th (Rules {next, rules, netpairs, wrappers}) = |
61049 | 77 |
let |
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
78 |
val weight = opt_weight |> \<^if_none>\<open>Bires.subgoals_of (b, th)\<close>; |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
79 |
val tag = {weight = weight, index = next}; |
61049 | 80 |
val th' = Thm.trim_context th; |
81 |
in |
|
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
82 |
make_rules (next - 1) ((weight, ((i, b), th')) :: rules) |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
83 |
(nth_map i (Bires.insert_tagged_rule (tag, (b, th'))) netpairs) wrappers |
12350 | 84 |
end; |
85 |
||
45375
7fe19930dfc9
more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents:
39557
diff
changeset
|
86 |
fun del_rule0 th (rs as Rules {next, rules, netpairs, wrappers}) = |
12350 | 87 |
let |
22360
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents:
21506
diff
changeset
|
88 |
fun eq_th (_, (_, th')) = Thm.eq_thm_prop (th, th'); |
82807 | 89 |
fun del b netpair = Bires.delete_tagged_rule (b, th) netpair handle Net.DELETE => netpair; |
12350 | 90 |
in |
91 |
if not (exists eq_th rules) then rs |
|
92 |
else make_rules next (filter_out eq_th rules) (map (del false o del true) netpairs) wrappers |
|
93 |
end; |
|
94 |
||
45375
7fe19930dfc9
more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents:
39557
diff
changeset
|
95 |
fun del_rule th = del_rule0 th o del_rule0 (Tactic.make_elim th); |
7fe19930dfc9
more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents:
39557
diff
changeset
|
96 |
|
33519 | 97 |
structure Rules = Generic_Data |
18637 | 98 |
( |
33370 | 99 |
type T = rules; |
12350 | 100 |
val empty = make_rules ~1 [] empty_netpairs ([], []); |
33519 | 101 |
fun merge |
102 |
(Rules {rules = rules1, wrappers = (ws1, ws1'), ...}, |
|
12350 | 103 |
Rules {rules = rules2, wrappers = (ws2, ws2'), ...}) = |
104 |
let |
|
18637 | 105 |
val wrappers = |
18921 | 106 |
(Library.merge (eq_snd (op =)) (ws1, ws2), Library.merge (eq_snd (op =)) (ws1', ws2')); |
107 |
val rules = Library.merge (fn ((_, (k1, th1)), (_, (k2, th2))) => |
|
22360
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents:
21506
diff
changeset
|
108 |
k1 = k2 andalso Thm.eq_thm_prop (th1, th2)) (rules1, rules2); |
12350 | 109 |
val next = ~ (length rules); |
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
110 |
val netpairs = fold (fn (index, (weight, ((i, b), th))) => |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
111 |
nth_map i (Bires.insert_tagged_rule ({weight = weight, index = index}, (b, th)))) |
19473 | 112 |
(next upto ~1 ~~ rules) empty_netpairs; |
23227 | 113 |
in make_rules (next - 1) rules netpairs wrappers end; |
18637 | 114 |
); |
12350 | 115 |
|
22846 | 116 |
fun print_rules ctxt = |
117 |
let |
|
118 |
val Rules {rules, ...} = Rules.get (Context.Proof ctxt); |
|
119 |
fun prt_kind (i, b) = |
|
120 |
Pretty.big_list ((the o AList.lookup (op =) kind_names) (i, b) ^ ":") |
|
121 |
(map_filter (fn (_, (k, th)) => |
|
61268 | 122 |
if k = (i, b) then SOME (Thm.pretty_thm_item ctxt th) else NONE) |
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
56334
diff
changeset
|
123 |
(sort (int_ord o apply2 fst) rules)); |
82587
7415414bd9d8
more scalable: discontinue odd shortcuts from 6b3739fee456, which produce bulky strings internally;
wenzelm
parents:
77908
diff
changeset
|
124 |
in Pretty.writeln (Pretty.chunks (map prt_kind rule_kinds)) end; |
12350 | 125 |
|
126 |
||
127 |
(* access data *) |
|
128 |
||
18637 | 129 |
fun netpairs ctxt = let val Rules {netpairs, ...} = Rules.get (Context.Proof ctxt) in netpairs end; |
12350 | 130 |
val netpair_bang = hd o netpairs; |
131 |
val netpair = hd o tl o netpairs; |
|
132 |
||
133 |
||
12399 | 134 |
(* retrieving rules *) |
135 |
||
61049 | 136 |
local |
137 |
||
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
138 |
fun order weighted = |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
139 |
make_order_list (Bires.weighted_tag_ord weighted) NONE; |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
140 |
|
12399 | 141 |
fun may_unify weighted t net = |
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
142 |
map snd (order weighted (Net.unify_term net t)); |
12399 | 143 |
|
144 |
fun find_erules _ [] = K [] |
|
12805 | 145 |
| find_erules w (fact :: _) = may_unify w (Logic.strip_assums_concl (Thm.prop_of fact)); |
16424 | 146 |
|
12399 | 147 |
fun find_irules w goal = may_unify w (Logic.strip_assums_concl goal); |
12380 | 148 |
|
61049 | 149 |
in |
12380 | 150 |
|
61049 | 151 |
fun find_rules_netpair ctxt weighted facts goal (inet, enet) = |
152 |
find_erules weighted facts enet @ find_irules weighted goal inet |
|
67649 | 153 |
|> map (Thm.transfer' ctxt); |
61049 | 154 |
|
155 |
fun find_rules ctxt weighted facts goal = |
|
156 |
map (find_rules_netpair ctxt weighted facts goal) (netpairs ctxt); |
|
157 |
||
158 |
end; |
|
12350 | 159 |
|
160 |
||
12399 | 161 |
(* wrappers *) |
162 |
||
18667 | 163 |
fun gen_add_wrapper upd w = |
32784 | 164 |
Context.theory_map (Rules.map (fn Rules {next, rules, netpairs, wrappers} => |
18667 | 165 |
make_rules next rules netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers))); |
12350 | 166 |
|
167 |
val addSWrapper = gen_add_wrapper Library.apfst; |
|
168 |
val addWrapper = gen_add_wrapper Library.apsnd; |
|
169 |
||
170 |
||
171 |
fun gen_wrap which ctxt = |
|
18637 | 172 |
let val Rules {wrappers, ...} = Rules.get (Context.Proof ctxt) |
51798 | 173 |
in fold_rev (fn (w, _) => w ctxt) (which wrappers) end; |
12350 | 174 |
|
175 |
val Swrap = gen_wrap #1; |
|
176 |
val wrap = gen_wrap #2; |
|
177 |
||
178 |
||
179 |
||
180 |
(** attributes **) |
|
181 |
||
182 |
(* add and del rules *) |
|
183 |
||
45375
7fe19930dfc9
more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents:
39557
diff
changeset
|
184 |
|
67626 | 185 |
val rule_del = Thm.declaration_attribute (Rules.map o del_rule); |
12350 | 186 |
|
18637 | 187 |
fun rule_add k view opt_w = |
45375
7fe19930dfc9
more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents:
39557
diff
changeset
|
188 |
Thm.declaration_attribute (fn th => Rules.map (add_rule k opt_w (view th) o del_rule th)); |
12350 | 189 |
|
18637 | 190 |
val intro_bang = rule_add intro_bangK I; |
191 |
val elim_bang = rule_add elim_bangK I; |
|
192 |
val dest_bang = rule_add elim_bangK Tactic.make_elim; |
|
193 |
val intro = rule_add introK I; |
|
194 |
val elim = rule_add elimK I; |
|
195 |
val dest = rule_add elimK Tactic.make_elim; |
|
196 |
val intro_query = rule_add intro_queryK I; |
|
197 |
val elim_query = rule_add elim_queryK I; |
|
198 |
val dest_query = rule_add elim_queryK Tactic.make_elim; |
|
12350 | 199 |
|
53171 | 200 |
val _ = Theory.setup |
201 |
(snd o Global_Theory.add_thms [((Binding.empty, Drule.equal_intr_rule), [intro_query NONE])]); |
|
18637 | 202 |
|
12350 | 203 |
|
18637 | 204 |
(* concrete syntax *) |
205 |
||
30529 | 206 |
fun add a b c x = |
27809
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
26463
diff
changeset
|
207 |
(Scan.lift ((Args.bang >> K a || Args.query >> K c || Scan.succeed b) -- |
36950 | 208 |
Scan.option Parse.nat) >> (fn (f, n) => f n)) x; |
18637 | 209 |
|
53171 | 210 |
val _ = Theory.setup |
67147 | 211 |
(Attrib.setup \<^binding>\<open>intro\<close> (add intro_bang intro intro_query) |
30529 | 212 |
"declaration of introduction rule" #> |
67147 | 213 |
Attrib.setup \<^binding>\<open>elim\<close> (add elim_bang elim elim_query) |
30529 | 214 |
"declaration of elimination rule" #> |
67147 | 215 |
Attrib.setup \<^binding>\<open>dest\<close> (add dest_bang dest dest_query) |
30529 | 216 |
"declaration of destruction rule" #> |
67147 | 217 |
Attrib.setup \<^binding>\<open>rule\<close> (Scan.lift Args.del >> K rule_del) |
53171 | 218 |
"remove declaration of intro/elim/dest rule"); |
12350 | 219 |
|
220 |
end; |