author | wenzelm |
Fri, 11 Jul 2025 11:52:43 +0200 | |
changeset 82839 | 60ec2b2dc55a |
parent 82836 | 68a0219861b7 |
child 82847 | d8ecaff223ff |
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 |
|
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
52 |
fun remove_decl ({info = th, ...}: decl) = |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
53 |
Bires.delete_tagged_rule (false, th) o Bires.delete_tagged_rule (true, th); |
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; |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
71 |
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
|
72 |
in |
82839
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
73 |
(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
|
74 |
(NONE, _) => rules |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
75 |
| (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
|
76 |
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
|
77 |
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
|
78 |
end; |
12350 | 79 |
|
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
80 |
fun del_rule0 th (rules as Rules {decls, netpairs, wrappers}) = |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
81 |
(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
|
82 |
([], _) => rules |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
83 |
| (old_decls, decls') => |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
84 |
let |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
85 |
val netpairs' = |
60ec2b2dc55a
clarified signature: rule declarations work via "info" as internal rule (which coincides with external rule);
wenzelm
parents:
82836
diff
changeset
|
86 |
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
|
87 |
old_decls netpairs; |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
88 |
in make_rules decls' netpairs' wrappers end); |
12350 | 89 |
|
45375
7fe19930dfc9
more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents:
39557
diff
changeset
|
90 |
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
|
91 |
|
82816 | 92 |
structure Data = Generic_Data |
18637 | 93 |
( |
33370 | 94 |
type T = rules; |
82828 | 95 |
val empty = make_rules Bires.empty_decls (Bires.kind_values Bires.empty_netpair) ([], []); |
33519 | 96 |
fun merge |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
97 |
(Rules {decls = decls1, netpairs = netpairs1, wrappers = (ws1, ws1')}, |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
98 |
Rules {decls = decls2, netpairs = _, wrappers = (ws2, ws2')}) = |
12350 | 99 |
let |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
100 |
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
|
101 |
val netpairs = |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
102 |
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
|
103 |
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
|
104 |
if Bires.kind_index (#kind decl) = i then insert_decl decl else I))) |
18637 | 105 |
val wrappers = |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
106 |
(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
|
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 |
in make_rules decls netpairs wrappers end; |
18637 | 109 |
); |
12350 | 110 |
|
22846 | 111 |
fun print_rules ctxt = |
82836 | 112 |
let val Rules {decls, ...} = Data.get (Context.Proof ctxt) |
113 |
in Pretty.writeln (Pretty.chunks (Bires.pretty_decls ctxt Bires.kind_domain decls)) end; |
|
12350 | 114 |
|
115 |
||
116 |
(* access data *) |
|
117 |
||
82816 | 118 |
fun netpairs ctxt = let val Rules {netpairs, ...} = Data.get (Context.Proof ctxt) in netpairs end; |
12350 | 119 |
val netpair_bang = hd o netpairs; |
120 |
val netpair = hd o tl o netpairs; |
|
121 |
||
122 |
||
12399 | 123 |
(* retrieving rules *) |
124 |
||
61049 | 125 |
local |
126 |
||
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
127 |
fun order weighted = |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
128 |
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
|
129 |
|
12399 | 130 |
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
|
131 |
map snd (order weighted (Net.unify_term net t)); |
12399 | 132 |
|
133 |
fun find_erules _ [] = K [] |
|
12805 | 134 |
| find_erules w (fact :: _) = may_unify w (Logic.strip_assums_concl (Thm.prop_of fact)); |
16424 | 135 |
|
12399 | 136 |
fun find_irules w goal = may_unify w (Logic.strip_assums_concl goal); |
12380 | 137 |
|
61049 | 138 |
in |
12380 | 139 |
|
61049 | 140 |
fun find_rules_netpair ctxt weighted facts goal (inet, enet) = |
141 |
find_erules weighted facts enet @ find_irules weighted goal inet |
|
67649 | 142 |
|> map (Thm.transfer' ctxt); |
61049 | 143 |
|
144 |
fun find_rules ctxt weighted facts goal = |
|
145 |
map (find_rules_netpair ctxt weighted facts goal) (netpairs ctxt); |
|
146 |
||
147 |
end; |
|
12350 | 148 |
|
149 |
||
12399 | 150 |
(* wrappers *) |
151 |
||
18667 | 152 |
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
|
153 |
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
|
154 |
make_rules decls netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers))); |
12350 | 155 |
|
156 |
val addSWrapper = gen_add_wrapper Library.apfst; |
|
157 |
val addWrapper = gen_add_wrapper Library.apsnd; |
|
158 |
||
159 |
||
160 |
fun gen_wrap which ctxt = |
|
82816 | 161 |
let val Rules {wrappers, ...} = Data.get (Context.Proof ctxt) |
51798 | 162 |
in fold_rev (fn (w, _) => w ctxt) (which wrappers) end; |
12350 | 163 |
|
164 |
val Swrap = gen_wrap #1; |
|
165 |
val wrap = gen_wrap #2; |
|
166 |
||
167 |
||
168 |
||
169 |
(** attributes **) |
|
170 |
||
171 |
(* add and del rules *) |
|
172 |
||
45375
7fe19930dfc9
more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents:
39557
diff
changeset
|
173 |
|
82816 | 174 |
val rule_del = Thm.declaration_attribute (Data.map o del_rule); |
12350 | 175 |
|
18637 | 176 |
fun rule_add k view opt_w = |
82816 | 177 |
Thm.declaration_attribute (fn th => Data.map (add_rule k opt_w (view th) o del_rule th)); |
12350 | 178 |
|
82817
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
179 |
val intro_bang = rule_add Bires.intro_bang_kind I; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
180 |
val elim_bang = rule_add Bires.elim_bang_kind I; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
181 |
val dest_bang = rule_add Bires.elim_bang_kind Tactic.make_elim; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
182 |
val intro = rule_add Bires.intro_kind I; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
183 |
val elim = rule_add Bires.elim_kind I; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
184 |
val dest = rule_add Bires.elim_kind Tactic.make_elim; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
185 |
val intro_query = rule_add Bires.intro_query_kind I; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
186 |
val elim_query = rule_add Bires.elim_query_kind I; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
187 |
val dest_query = rule_add Bires.elim_query_kind Tactic.make_elim; |
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; |