author | wenzelm |
Tue, 15 Jul 2025 11:26:31 +0200 | |
changeset 82873 | e567fd948ff0 |
parent 82868 | c2a88a1cd07d |
child 82874 | abfb6ed8ec21 |
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 |
||
82839
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
40 |
(* rule declarations *) |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
41 |
|
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
42 |
type decl = thm Bires.decl; |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
43 |
type decls = thm Bires.decls; |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
44 |
|
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
45 |
fun init_decl kind opt_weight th : decl = |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
46 |
let val weight = opt_weight |> \<^if_none>\<open>Bires.subgoals_of (Bires.kind_rule kind th)\<close>; |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
47 |
in {kind = kind, tag = Bires.weight_tag weight, info = th} end; |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
48 |
|
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
49 |
fun insert_decl ({kind, tag, info = th}: decl) = |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
50 |
Bires.insert_tagged_rule (tag, Bires.kind_rule kind th); |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
51 |
|
82848
0a8977dcb21a
more accurate delete operation via authentic index --- minor performance tuning;
wenzelm
parents:
82847
diff
changeset
|
52 |
fun remove_decl ({tag = {index, ...}, info = th, ...}: decl) = |
0a8977dcb21a
more accurate delete operation via authentic index --- minor performance tuning;
wenzelm
parents:
82847
diff
changeset
|
53 |
Bires.delete_tagged_rule (index, th); |
82839
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
54 |
|
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
55 |
|
18637 | 56 |
(* context data *) |
12350 | 57 |
|
33370 | 58 |
datatype rules = Rules of |
82839
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
59 |
{decls: decls, |
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
60 |
netpairs: Bires.netpair list, |
51798 | 61 |
wrappers: |
62 |
((Proof.context -> (int -> tactic) -> int -> tactic) * stamp) list * |
|
63 |
((Proof.context -> (int -> tactic) -> int -> tactic) * stamp) list}; |
|
12350 | 64 |
|
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
65 |
fun make_rules decls netpairs wrappers = |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
66 |
Rules {decls = decls, netpairs = netpairs, wrappers = wrappers}; |
12350 | 67 |
|
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
68 |
fun add_rule kind opt_weight th (rules as Rules {decls, netpairs, wrappers}) = |
61049 | 69 |
let |
82839
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
70 |
val th' = Thm.trim_context th; |
82873 | 71 |
val th'' = Thm.trim_context (Bires.kind_make_elim kind th); |
82868
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
72 |
val decl' = init_decl kind opt_weight th''; |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
73 |
in |
82839
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
74 |
(case Bires.extend_decls (th', decl') decls of |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
75 |
(NONE, _) => rules |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
76 |
| (SOME new_decl, decls') => |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
77 |
let val netpairs' = Bires.kind_map kind (insert_decl new_decl) netpairs |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
78 |
in make_rules decls' netpairs' wrappers end) |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
79 |
end; |
12350 | 80 |
|
82868
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
81 |
fun del_rule th (rules as Rules {decls, netpairs, wrappers}) = |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
82 |
(case Bires.remove_decls th decls of |
82839
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
83 |
([], _) => rules |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
84 |
| (old_decls, decls') => |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
85 |
let |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
86 |
val netpairs' = |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
87 |
fold (fn decl as {kind, ...} => Bires.kind_map kind (remove_decl decl)) |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
88 |
old_decls netpairs; |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
89 |
in make_rules decls' netpairs' wrappers end); |
12350 | 90 |
|
82816 | 91 |
structure Data = Generic_Data |
18637 | 92 |
( |
33370 | 93 |
type T = rules; |
82828 | 94 |
val empty = make_rules Bires.empty_decls (Bires.kind_values Bires.empty_netpair) ([], []); |
82847 | 95 |
fun merge (rules1, rules2) = |
96 |
if pointer_eq (rules1, rules2) then rules1 |
|
97 |
else |
|
12350 | 98 |
let |
82847 | 99 |
val Rules {decls = decls1, netpairs = netpairs1, wrappers = (ws1, ws1')} = rules1; |
100 |
val Rules {decls = decls2, netpairs = _, wrappers = (ws2, ws2')} = rules2; |
|
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
101 |
val (new_rules, decls) = Bires.merge_decls (decls1, decls2); |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
102 |
val netpairs = |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
103 |
netpairs1 |> map_index (uncurry (fn i => |
82839
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
104 |
new_rules |> fold (fn decl => |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
105 |
if Bires.kind_index (#kind decl) = i then insert_decl decl else I))) |
18637 | 106 |
val wrappers = |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
107 |
(Library.merge (eq_snd (op =)) (ws1, ws2), |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
108 |
Library.merge (eq_snd (op =)) (ws1', ws2')); |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
109 |
in make_rules decls netpairs wrappers end; |
18637 | 110 |
); |
12350 | 111 |
|
22846 | 112 |
fun print_rules ctxt = |
82836 | 113 |
let val Rules {decls, ...} = Data.get (Context.Proof ctxt) |
82864 | 114 |
in Pretty.writeln (Pretty.chunks (Bires.pretty_decls ctxt decls)) end; |
12350 | 115 |
|
116 |
||
117 |
(* access data *) |
|
118 |
||
82816 | 119 |
fun netpairs ctxt = let val Rules {netpairs, ...} = Data.get (Context.Proof ctxt) in netpairs end; |
12350 | 120 |
val netpair_bang = hd o netpairs; |
121 |
val netpair = hd o tl o netpairs; |
|
122 |
||
123 |
||
12399 | 124 |
(* retrieving rules *) |
125 |
||
61049 | 126 |
local |
127 |
||
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
128 |
fun order weighted = |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
129 |
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
|
130 |
|
12399 | 131 |
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
|
132 |
map snd (order weighted (Net.unify_term net t)); |
12399 | 133 |
|
134 |
fun find_erules _ [] = K [] |
|
12805 | 135 |
| find_erules w (fact :: _) = may_unify w (Logic.strip_assums_concl (Thm.prop_of fact)); |
16424 | 136 |
|
12399 | 137 |
fun find_irules w goal = may_unify w (Logic.strip_assums_concl goal); |
12380 | 138 |
|
61049 | 139 |
in |
12380 | 140 |
|
61049 | 141 |
fun find_rules_netpair ctxt weighted facts goal (inet, enet) = |
142 |
find_erules weighted facts enet @ find_irules weighted goal inet |
|
67649 | 143 |
|> map (Thm.transfer' ctxt); |
61049 | 144 |
|
145 |
fun find_rules ctxt weighted facts goal = |
|
146 |
map (find_rules_netpair ctxt weighted facts goal) (netpairs ctxt); |
|
147 |
||
148 |
end; |
|
12350 | 149 |
|
150 |
||
12399 | 151 |
(* wrappers *) |
152 |
||
18667 | 153 |
fun gen_add_wrapper upd w = |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
154 |
Context.theory_map (Data.map (fn Rules {decls, netpairs, wrappers} => |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
155 |
make_rules decls netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers))); |
12350 | 156 |
|
157 |
val addSWrapper = gen_add_wrapper Library.apfst; |
|
158 |
val addWrapper = gen_add_wrapper Library.apsnd; |
|
159 |
||
160 |
||
161 |
fun gen_wrap which ctxt = |
|
82816 | 162 |
let val Rules {wrappers, ...} = Data.get (Context.Proof ctxt) |
51798 | 163 |
in fold_rev (fn (w, _) => w ctxt) (which wrappers) end; |
12350 | 164 |
|
165 |
val Swrap = gen_wrap #1; |
|
166 |
val wrap = gen_wrap #2; |
|
167 |
||
168 |
||
169 |
||
170 |
(** attributes **) |
|
171 |
||
172 |
(* add and del rules *) |
|
173 |
||
82816 | 174 |
val rule_del = Thm.declaration_attribute (Data.map o del_rule); |
12350 | 175 |
|
82868
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
176 |
fun rule_add k opt_w = |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
177 |
Thm.declaration_attribute (fn th => Data.map (add_rule k opt_w th o del_rule th)); |
12350 | 178 |
|
82868
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
179 |
val intro_bang = rule_add Bires.intro_bang_kind; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
180 |
val elim_bang = rule_add Bires.elim_bang_kind; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
181 |
val dest_bang = rule_add Bires.dest_bang_kind; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
182 |
val intro = rule_add Bires.intro_kind; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
183 |
val elim = rule_add Bires.elim_kind; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
184 |
val dest = rule_add Bires.dest_kind; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
185 |
val intro_query = rule_add Bires.intro_query_kind; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
186 |
val elim_query = rule_add Bires.elim_query_kind; |
c2a88a1cd07d
explicit "dest" rules: no longer declare [elim_format, elim];
wenzelm
parents:
82864
diff
changeset
|
187 |
val dest_query = rule_add Bires.dest_query_kind; |
12350 | 188 |
|
53171 | 189 |
val _ = Theory.setup |
190 |
(snd o Global_Theory.add_thms [((Binding.empty, Drule.equal_intr_rule), [intro_query NONE])]); |
|
18637 | 191 |
|
12350 | 192 |
|
18637 | 193 |
(* concrete syntax *) |
194 |
||
30529 | 195 |
fun add a b c x = |
27809
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
26463
diff
changeset
|
196 |
(Scan.lift ((Args.bang >> K a || Args.query >> K c || Scan.succeed b) -- |
36950 | 197 |
Scan.option Parse.nat) >> (fn (f, n) => f n)) x; |
18637 | 198 |
|
53171 | 199 |
val _ = Theory.setup |
67147 | 200 |
(Attrib.setup \<^binding>\<open>intro\<close> (add intro_bang intro intro_query) |
82818 | 201 |
"declaration of Pure introduction rule" #> |
67147 | 202 |
Attrib.setup \<^binding>\<open>elim\<close> (add elim_bang elim elim_query) |
82818 | 203 |
"declaration of Pure elimination rule" #> |
67147 | 204 |
Attrib.setup \<^binding>\<open>dest\<close> (add dest_bang dest dest_query) |
82818 | 205 |
"declaration of Pure destruction rule" #> |
67147 | 206 |
Attrib.setup \<^binding>\<open>rule\<close> (Scan.lift Args.del >> K rule_del) |
82818 | 207 |
"remove declaration of Pure intro/elim/dest rule"); |
12350 | 208 |
|
209 |
end; |