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