12301
|
1 |
(* Title: Pure/Isar/rule_context.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Stefan Berghofer and Markus Wenzel, TU Muenchen
|
|
4 |
License: GPL (GNU GENERAL PUBLIC LICENSE)
|
|
5 |
|
|
6 |
Declarations of intro/elim/dest rules in Pure; see
|
|
7 |
Provers/classical.ML for a more specialized version.
|
|
8 |
*)
|
|
9 |
|
|
10 |
signature RULE_CONTEXT =
|
|
11 |
sig
|
12326
|
12 |
type netpair
|
12301
|
13 |
type T
|
12348
|
14 |
val Snetpair: Proof.context -> netpair
|
|
15 |
val netpair: Proof.context -> netpair
|
|
16 |
val netpairs: Proof.context -> netpair list
|
|
17 |
val orderlist: ((int * int) * 'a) list -> 'a list
|
|
18 |
val orderlist_no_weight: ((int * int) * 'a) list -> 'a list
|
12301
|
19 |
val print_global_rules: theory -> unit
|
|
20 |
val print_local_rules: Proof.context -> unit
|
12348
|
21 |
val addSWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory
|
|
22 |
val addWrapper: ((int -> tactic) -> int -> tactic) -> theory -> theory
|
|
23 |
val Swrap: Proof.context -> (int -> tactic) -> int -> tactic
|
|
24 |
val wrap: Proof.context -> (int -> tactic) -> int -> tactic
|
12301
|
25 |
val intro_bang_global: int option -> theory attribute
|
|
26 |
val elim_bang_global: int option -> theory attribute
|
|
27 |
val dest_bang_global: int option -> theory attribute
|
|
28 |
val intro_global: int option -> theory attribute
|
|
29 |
val elim_global: int option -> theory attribute
|
|
30 |
val dest_global: int option -> theory attribute
|
|
31 |
val intro_query_global: int option -> theory attribute
|
|
32 |
val elim_query_global: int option -> theory attribute
|
|
33 |
val dest_query_global: int option -> theory attribute
|
12321
|
34 |
val rule_del_global: theory attribute
|
12301
|
35 |
val intro_bang_local: int option -> Proof.context attribute
|
|
36 |
val elim_bang_local: int option -> Proof.context attribute
|
|
37 |
val dest_bang_local: int option -> Proof.context attribute
|
|
38 |
val intro_local: int option -> Proof.context attribute
|
|
39 |
val elim_local: int option -> Proof.context attribute
|
|
40 |
val dest_local: int option -> Proof.context attribute
|
|
41 |
val intro_query_local: int option -> Proof.context attribute
|
|
42 |
val elim_query_local: int option -> Proof.context attribute
|
|
43 |
val dest_query_local: int option -> Proof.context attribute
|
12321
|
44 |
val rule_del_local: Proof.context attribute
|
12301
|
45 |
val setup: (theory -> theory) list
|
|
46 |
end;
|
|
47 |
|
|
48 |
structure RuleContext: RULE_CONTEXT =
|
|
49 |
struct
|
|
50 |
|
|
51 |
|
|
52 |
(** rule declaration contexts **)
|
|
53 |
|
|
54 |
(* rule kinds *)
|
|
55 |
|
|
56 |
val intro_bangK = (0, false);
|
|
57 |
val elim_bangK = (0, true);
|
|
58 |
val introK = (1, false);
|
|
59 |
val elimK = (1, true);
|
|
60 |
val intro_queryK = (2, false);
|
|
61 |
val elim_queryK = (2, true);
|
|
62 |
|
|
63 |
val kind_names =
|
|
64 |
[(intro_bangK, "safe introduction rules (intro!)"),
|
|
65 |
(elim_bangK, "safe elimination rules (elim!)"),
|
|
66 |
(introK, "introduction rules (intro)"),
|
|
67 |
(elimK, "elimination rules (elim)"),
|
|
68 |
(intro_queryK, "extra introduction rules (intro?)"),
|
|
69 |
(elim_queryK, "extra elimination rules (elim?)")];
|
|
70 |
|
|
71 |
val rule_kinds = map #1 kind_names;
|
|
72 |
val rule_indexes = distinct (map #1 rule_kinds);
|
|
73 |
|
|
74 |
|
12348
|
75 |
(* raw data *)
|
12301
|
76 |
|
12326
|
77 |
type netpair = ((int * int) * (bool * thm)) Net.net * ((int * int) * (bool * thm)) Net.net;
|
|
78 |
val empty_netpairs: netpair list = replicate (length rule_indexes) (Net.empty, Net.empty);
|
12301
|
79 |
|
|
80 |
datatype T = Rules of
|
|
81 |
{next: int,
|
|
82 |
rules: (int * ((int * bool) * thm)) list,
|
12326
|
83 |
netpairs: netpair list,
|
12348
|
84 |
wrappers: (((int -> tactic) -> int -> tactic) * stamp) list *
|
|
85 |
(((int -> tactic) -> int -> tactic) * stamp) list};
|
12301
|
86 |
|
12321
|
87 |
fun make_rules next rules netpairs wrappers =
|
|
88 |
Rules {next = next, rules = rules, netpairs = netpairs, wrappers = wrappers};
|
12301
|
89 |
|
|
90 |
fun add_rule (i, b) opt_w th (Rules {next, rules, netpairs, wrappers}) =
|
|
91 |
let val w = (case opt_w of Some w => w | None => Tactic.subgoals_of_brl (b, th)) in
|
12321
|
92 |
make_rules (next - 1) ((w, ((i, b), th)) :: rules)
|
|
93 |
(map_nth_elem i (curry insert_tagged_brl ((w, next), (b, th))) netpairs) wrappers
|
|
94 |
end;
|
|
95 |
|
|
96 |
fun del_rule th (rs as Rules {next, rules, netpairs, wrappers}) =
|
|
97 |
let
|
|
98 |
fun eq_th (_, (_, th')) = Thm.eq_thm (th, th');
|
12348
|
99 |
fun del b netpair = delete_tagged_brl ((b, th), netpair) handle Net.DELETE => netpair;
|
12321
|
100 |
in
|
|
101 |
if not (exists eq_th rules) then rs
|
|
102 |
else make_rules next (filter_out eq_th rules) (map (del false o del true) netpairs) wrappers
|
12301
|
103 |
end;
|
|
104 |
|
|
105 |
fun print_rules prt x (Rules {rules, ...}) =
|
|
106 |
let
|
|
107 |
fun prt_kind (i, b) =
|
|
108 |
Pretty.big_list (the (assoc (kind_names, (i, b))) ^ ":")
|
12348
|
109 |
(mapfilter (fn (_, (k, th)) => if k = (i, b) then Some (prt x th) else None)
|
|
110 |
(sort (int_ord o pairself fst) rules));
|
12301
|
111 |
in Pretty.writeln (Pretty.chunks (map prt_kind rule_kinds)) end;
|
|
112 |
|
|
113 |
|
|
114 |
(* theory and proof data *)
|
|
115 |
|
|
116 |
structure GlobalRulesArgs =
|
|
117 |
struct
|
|
118 |
val name = "Isar/rule_context";
|
|
119 |
type T = T;
|
|
120 |
|
12348
|
121 |
val empty = make_rules ~1 [] empty_netpairs ([], []);
|
12301
|
122 |
val copy = I;
|
|
123 |
val prep_ext = I;
|
|
124 |
|
12348
|
125 |
fun merge (Rules {rules = rules1, wrappers = (ws1, ws1'), ...},
|
|
126 |
Rules {rules = rules2, wrappers = (ws2, ws2'), ...}) =
|
12301
|
127 |
let
|
12348
|
128 |
val wrappers = (gen_merge_lists' eq_snd ws1 ws2, gen_merge_lists' eq_snd ws1' ws2');
|
12301
|
129 |
val rules = gen_merge_lists' (fn ((_, (k1, th1)), (_, (k2, th2))) =>
|
|
130 |
k1 = k2 andalso Thm.eq_thm (th1, th2)) rules1 rules2;
|
|
131 |
val next = ~ (length rules);
|
|
132 |
val netpairs = foldl (fn (nps, (n, (w, ((i, b), th)))) =>
|
|
133 |
map_nth_elem i (curry insert_tagged_brl ((w, n), (b, th))) nps)
|
|
134 |
(empty_netpairs, next upto ~1 ~~ rules);
|
12321
|
135 |
in make_rules (next - 1) rules netpairs wrappers end;
|
12301
|
136 |
|
|
137 |
val print = print_rules Display.pretty_thm_sg;
|
|
138 |
end;
|
|
139 |
|
|
140 |
structure GlobalRules = TheoryDataFun(GlobalRulesArgs);
|
|
141 |
val print_global_rules = GlobalRules.print;
|
|
142 |
|
|
143 |
structure LocalRulesArgs =
|
|
144 |
struct
|
|
145 |
val name = GlobalRulesArgs.name;
|
|
146 |
type T = GlobalRulesArgs.T;
|
|
147 |
val init = GlobalRules.get;
|
|
148 |
val print = print_rules ProofContext.pretty_thm;
|
|
149 |
end;
|
|
150 |
|
|
151 |
structure LocalRules = ProofDataFun(LocalRulesArgs);
|
|
152 |
val print_local_rules = LocalRules.print;
|
|
153 |
|
12348
|
154 |
fun netpairs ctxt = let val Rules {netpairs, ...} = LocalRules.get ctxt in netpairs end;
|
|
155 |
val Snetpair = hd o netpairs;
|
|
156 |
val netpair = hd o tl o netpairs;
|
|
157 |
|
|
158 |
|
|
159 |
fun untaglist [] = []
|
|
160 |
| untaglist [(k : int * int, x)] = [x]
|
|
161 |
| untaglist ((k, x) :: (rest as (k', x') :: _)) =
|
|
162 |
if k = k' then untaglist rest
|
|
163 |
else x :: untaglist rest;
|
|
164 |
|
|
165 |
fun orderlist brls = untaglist (sort (prod_ord int_ord int_ord o pairself fst) brls);
|
|
166 |
fun orderlist_no_weight brls = untaglist (sort (int_ord o pairself (snd o fst)) brls);
|
|
167 |
|
|
168 |
|
|
169 |
(* wrappers *)
|
|
170 |
|
|
171 |
fun gen_add_wrapper upd w = GlobalRules.map (fn (rs as Rules {next, rules, netpairs, wrappers}) =>
|
|
172 |
make_rules next rules netpairs (upd (fn ws => (w, stamp ()) :: ws) wrappers));
|
|
173 |
|
|
174 |
val addSWrapper = gen_add_wrapper Library.apfst;
|
|
175 |
val addWrapper = gen_add_wrapper Library.apsnd;
|
|
176 |
|
|
177 |
|
|
178 |
fun gen_wrap which ctxt =
|
|
179 |
let val Rules {wrappers, ...} = LocalRules.get ctxt
|
|
180 |
in fn tac => foldr (fn ((w, _), t) => w t) (which wrappers, tac) end;
|
|
181 |
|
|
182 |
val Swrap = gen_wrap #1;
|
|
183 |
val wrap = gen_wrap #2;
|
|
184 |
|
12301
|
185 |
|
|
186 |
|
|
187 |
(** attributes **)
|
|
188 |
|
12321
|
189 |
(* add and del rules *)
|
12301
|
190 |
|
|
191 |
local
|
|
192 |
|
12321
|
193 |
fun del map_data (x, th) =
|
|
194 |
(map_data (del_rule th o del_rule (Tactic.make_elim th)) x, th);
|
|
195 |
|
|
196 |
fun add k view map_data opt_w =
|
|
197 |
(fn (x, th) => (map_data (add_rule k opt_w th) x, th)) o del map_data;
|
12301
|
198 |
|
|
199 |
in
|
|
200 |
|
12321
|
201 |
val intro_bang_global = add intro_bangK I GlobalRules.map;
|
|
202 |
val elim_bang_global = add elim_bangK I GlobalRules.map;
|
|
203 |
val dest_bang_global = add elim_bangK Tactic.make_elim GlobalRules.map;
|
|
204 |
val intro_global = add introK I GlobalRules.map;
|
|
205 |
val elim_global = add elimK I GlobalRules.map;
|
|
206 |
val dest_global = add elimK Tactic.make_elim GlobalRules.map;
|
|
207 |
val intro_query_global = add intro_queryK I GlobalRules.map;
|
|
208 |
val elim_query_global = add elim_queryK I GlobalRules.map;
|
|
209 |
val dest_query_global = add elim_queryK Tactic.make_elim GlobalRules.map;
|
|
210 |
val rule_del_global = del GlobalRules.map;
|
12301
|
211 |
|
12321
|
212 |
val intro_bang_local = add intro_bangK I LocalRules.map;
|
|
213 |
val elim_bang_local = add elim_bangK I LocalRules.map;
|
|
214 |
val dest_bang_local = add elim_bangK Tactic.make_elim LocalRules.map;
|
|
215 |
val intro_local = add introK I LocalRules.map;
|
|
216 |
val elim_local = add elimK I LocalRules.map;
|
|
217 |
val dest_local = add elimK Tactic.make_elim LocalRules.map;
|
|
218 |
val intro_query_local = add intro_queryK I LocalRules.map;
|
|
219 |
val elim_query_local = add elim_queryK I LocalRules.map;
|
|
220 |
val dest_query_local = add elim_queryK Tactic.make_elim LocalRules.map;
|
|
221 |
val rule_del_local = del LocalRules.map;
|
12301
|
222 |
|
|
223 |
end;
|
|
224 |
|
|
225 |
|
|
226 |
(* concrete syntax *)
|
|
227 |
|
|
228 |
fun add_args a b c x = Attrib.syntax
|
12348
|
229 |
(Scan.lift (Scan.option Args.nat --
|
12301
|
230 |
(Args.bang >> K a || Args.query >> K c || Scan.succeed b) >> op |>)) x;
|
|
231 |
|
12321
|
232 |
fun del_args att = Attrib.syntax (Scan.lift Args.del >> K att);
|
|
233 |
|
|
234 |
|
12301
|
235 |
val rule_atts =
|
|
236 |
[("intro",
|
|
237 |
(add_args intro_bang_global intro_global intro_query_global,
|
|
238 |
add_args intro_bang_local intro_local intro_query_local),
|
|
239 |
"declaration of introduction rule"),
|
|
240 |
("elim",
|
|
241 |
(add_args elim_bang_global elim_global elim_query_global,
|
|
242 |
add_args elim_bang_local elim_local elim_query_local),
|
|
243 |
"declaration of elimination rule"),
|
|
244 |
("dest",
|
|
245 |
(add_args dest_bang_global dest_global dest_query_global,
|
|
246 |
add_args dest_bang_local dest_local dest_query_local),
|
12321
|
247 |
"declaration of destruction rule"),
|
|
248 |
("rule", (del_args rule_del_global, del_args rule_del_local),
|
|
249 |
"remove declaration of intro/elim/dest rule")];
|
12301
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
(** theory setup **)
|
|
254 |
|
|
255 |
val setup =
|
12326
|
256 |
[GlobalRules.init, LocalRules.init, Attrib.add_attributes rule_atts];
|
12301
|
257 |
|
|
258 |
|
|
259 |
end;
|