author | wenzelm |
Tue, 08 Jul 2025 12:10:00 +0200 | |
changeset 82828 | 05d2b8b675da |
parent 82826 | f5fd9b41188a |
child 82829 | 57c331dc167d |
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 |
||
18637 | 40 |
(* context data *) |
12350 | 41 |
|
33370 | 42 |
datatype rules = Rules of |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
43 |
{decls: Bires.decls, |
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
44 |
netpairs: Bires.netpair list, |
51798 | 45 |
wrappers: |
46 |
((Proof.context -> (int -> tactic) -> int -> tactic) * stamp) list * |
|
47 |
((Proof.context -> (int -> tactic) -> int -> tactic) * stamp) list}; |
|
12350 | 48 |
|
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
49 |
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
|
50 |
Rules {decls = decls, netpairs = netpairs, wrappers = wrappers}; |
12350 | 51 |
|
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
52 |
fun add_rule kind opt_weight th (rules as Rules {decls, netpairs, wrappers}) = |
61049 | 53 |
let |
82817
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
54 |
val weight = opt_weight |> \<^if_none>\<open>Bires.subgoals_of (Bires.kind_rule kind th)\<close>; |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
55 |
val decl = {kind = kind, tag = Bires.weight_tag weight, implicit = false}; |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
56 |
in |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
57 |
(case Bires.extend_decls (Thm.trim_context th, decl) decls of |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
58 |
NONE => rules |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
59 |
| SOME (new_rule, decls') => |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
60 |
let val netpairs' = netpairs |> Bires.kind_map kind (Bires.insert_rule new_rule) |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
61 |
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
|
62 |
end; |
12350 | 63 |
|
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
64 |
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
|
65 |
(case Bires.remove_decls th decls of |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
66 |
NONE => rules |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
67 |
| SOME decls' => |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
68 |
let val netpairs' = map (Bires.remove_rule th) netpairs |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
69 |
in make_rules decls' netpairs' wrappers end); |
12350 | 70 |
|
45375
7fe19930dfc9
more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents:
39557
diff
changeset
|
71 |
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
|
72 |
|
82816 | 73 |
structure Data = Generic_Data |
18637 | 74 |
( |
33370 | 75 |
type T = rules; |
82828 | 76 |
val empty = make_rules Bires.empty_decls (Bires.kind_values Bires.empty_netpair) ([], []); |
33519 | 77 |
fun merge |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
78 |
(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
|
79 |
Rules {decls = decls2, netpairs = _, wrappers = (ws2, ws2')}) = |
12350 | 80 |
let |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
81 |
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
|
82 |
val netpairs = |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
83 |
netpairs1 |> map_index (uncurry (fn i => |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
84 |
new_rules |> fold (fn (th, decl) => |
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
85 |
Bires.kind_index (#kind decl) = i ? Bires.insert_rule (th, decl)))); |
18637 | 86 |
val wrappers = |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
87 |
(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
|
88 |
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
|
89 |
in make_rules decls netpairs wrappers end; |
18637 | 90 |
); |
12350 | 91 |
|
22846 | 92 |
fun print_rules ctxt = |
93 |
let |
|
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
94 |
val Rules {decls, ...} = Data.get (Context.Proof ctxt); |
82817
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
95 |
fun prt_kind kind = |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
96 |
Pretty.big_list (Bires.kind_title kind ^ ":") |
82826
f5fd9b41188a
efficient rule declarations in canonical order, for update of netpairs and print operation;
wenzelm
parents:
82818
diff
changeset
|
97 |
(Bires.print_decls kind decls |> map (fn (th, _) => Thm.pretty_thm_item ctxt th)); |
82817
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
98 |
in Pretty.writeln (Pretty.chunks (map prt_kind Bires.kind_domain)) end; |
12350 | 99 |
|
100 |
||
101 |
(* access data *) |
|
102 |
||
82816 | 103 |
fun netpairs ctxt = let val Rules {netpairs, ...} = Data.get (Context.Proof ctxt) in netpairs end; |
12350 | 104 |
val netpair_bang = hd o netpairs; |
105 |
val netpair = hd o tl o netpairs; |
|
106 |
||
107 |
||
12399 | 108 |
(* retrieving rules *) |
109 |
||
61049 | 110 |
local |
111 |
||
82812
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
112 |
fun order weighted = |
ea8d633fd4a8
just one type Bires.netpair, based on Bires.tag with explicit weight;
wenzelm
parents:
82808
diff
changeset
|
113 |
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
|
114 |
|
12399 | 115 |
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
|
116 |
map snd (order weighted (Net.unify_term net t)); |
12399 | 117 |
|
118 |
fun find_erules _ [] = K [] |
|
12805 | 119 |
| find_erules w (fact :: _) = may_unify w (Logic.strip_assums_concl (Thm.prop_of fact)); |
16424 | 120 |
|
12399 | 121 |
fun find_irules w goal = may_unify w (Logic.strip_assums_concl goal); |
12380 | 122 |
|
61049 | 123 |
in |
12380 | 124 |
|
61049 | 125 |
fun find_rules_netpair ctxt weighted facts goal (inet, enet) = |
126 |
find_erules weighted facts enet @ find_irules weighted goal inet |
|
67649 | 127 |
|> map (Thm.transfer' ctxt); |
61049 | 128 |
|
129 |
fun find_rules ctxt weighted facts goal = |
|
130 |
map (find_rules_netpair ctxt weighted facts goal) (netpairs ctxt); |
|
131 |
||
132 |
end; |
|
12350 | 133 |
|
134 |
||
12399 | 135 |
(* wrappers *) |
136 |
||
18667 | 137 |
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
|
138 |
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
|
139 |
make_rules decls netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers))); |
12350 | 140 |
|
141 |
val addSWrapper = gen_add_wrapper Library.apfst; |
|
142 |
val addWrapper = gen_add_wrapper Library.apsnd; |
|
143 |
||
144 |
||
145 |
fun gen_wrap which ctxt = |
|
82816 | 146 |
let val Rules {wrappers, ...} = Data.get (Context.Proof ctxt) |
51798 | 147 |
in fold_rev (fn (w, _) => w ctxt) (which wrappers) end; |
12350 | 148 |
|
149 |
val Swrap = gen_wrap #1; |
|
150 |
val wrap = gen_wrap #2; |
|
151 |
||
152 |
||
153 |
||
154 |
(** attributes **) |
|
155 |
||
156 |
(* add and del rules *) |
|
157 |
||
45375
7fe19930dfc9
more explicit representation of rule_attribute vs. declaration_attribute vs. mixed_attribute;
wenzelm
parents:
39557
diff
changeset
|
158 |
|
82816 | 159 |
val rule_del = Thm.declaration_attribute (Data.map o del_rule); |
12350 | 160 |
|
18637 | 161 |
fun rule_add k view opt_w = |
82816 | 162 |
Thm.declaration_attribute (fn th => Data.map (add_rule k opt_w (view th) o del_rule th)); |
12350 | 163 |
|
82817
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
164 |
val intro_bang = rule_add Bires.intro_bang_kind I; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
165 |
val elim_bang = rule_add Bires.elim_bang_kind I; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
166 |
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
|
167 |
val intro = rule_add Bires.intro_kind I; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
168 |
val elim = rule_add Bires.elim_kind I; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
169 |
val dest = rule_add Bires.elim_kind Tactic.make_elim; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
170 |
val intro_query = rule_add Bires.intro_query_kind I; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
171 |
val elim_query = rule_add Bires.elim_query_kind I; |
be1fb22d9e2a
clarified signature: more explicit type Bires.kind;
wenzelm
parents:
82816
diff
changeset
|
172 |
val dest_query = rule_add Bires.elim_query_kind Tactic.make_elim; |
12350 | 173 |
|
53171 | 174 |
val _ = Theory.setup |
175 |
(snd o Global_Theory.add_thms [((Binding.empty, Drule.equal_intr_rule), [intro_query NONE])]); |
|
18637 | 176 |
|
12350 | 177 |
|
18637 | 178 |
(* concrete syntax *) |
179 |
||
30529 | 180 |
fun add a b c x = |
27809
a1e409db516b
unified Args.T with OuterLex.token, renamed some operations;
wenzelm
parents:
26463
diff
changeset
|
181 |
(Scan.lift ((Args.bang >> K a || Args.query >> K c || Scan.succeed b) -- |
36950 | 182 |
Scan.option Parse.nat) >> (fn (f, n) => f n)) x; |
18637 | 183 |
|
53171 | 184 |
val _ = Theory.setup |
67147 | 185 |
(Attrib.setup \<^binding>\<open>intro\<close> (add intro_bang intro intro_query) |
82818 | 186 |
"declaration of Pure introduction rule" #> |
67147 | 187 |
Attrib.setup \<^binding>\<open>elim\<close> (add elim_bang elim elim_query) |
82818 | 188 |
"declaration of Pure elimination rule" #> |
67147 | 189 |
Attrib.setup \<^binding>\<open>dest\<close> (add dest_bang dest dest_query) |
82818 | 190 |
"declaration of Pure destruction rule" #> |
67147 | 191 |
Attrib.setup \<^binding>\<open>rule\<close> (Scan.lift Args.del >> K rule_del) |
82818 | 192 |
"remove declaration of Pure intro/elim/dest rule"); |
12350 | 193 |
|
194 |
end; |