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 |
TODO:
|
|
10 |
- del rules;
|
|
11 |
- tactics;
|
|
12 |
- add/del ML interface (?);
|
|
13 |
*)
|
|
14 |
|
|
15 |
signature RULE_CONTEXT =
|
|
16 |
sig
|
|
17 |
type T
|
|
18 |
val empty_rules: T
|
|
19 |
val print_global_rules: theory -> unit
|
|
20 |
val print_local_rules: Proof.context -> unit
|
|
21 |
val intro_bang_global: int option -> theory attribute
|
|
22 |
val elim_bang_global: int option -> theory attribute
|
|
23 |
val dest_bang_global: int option -> theory attribute
|
|
24 |
val intro_global: int option -> theory attribute
|
|
25 |
val elim_global: int option -> theory attribute
|
|
26 |
val dest_global: int option -> theory attribute
|
|
27 |
val intro_query_global: int option -> theory attribute
|
|
28 |
val elim_query_global: int option -> theory attribute
|
|
29 |
val dest_query_global: int option -> theory attribute
|
|
30 |
val intro_bang_local: int option -> Proof.context attribute
|
|
31 |
val elim_bang_local: int option -> Proof.context attribute
|
|
32 |
val dest_bang_local: int option -> Proof.context attribute
|
|
33 |
val intro_local: int option -> Proof.context attribute
|
|
34 |
val elim_local: int option -> Proof.context attribute
|
|
35 |
val dest_local: int option -> Proof.context attribute
|
|
36 |
val intro_query_local: int option -> Proof.context attribute
|
|
37 |
val elim_query_local: int option -> Proof.context attribute
|
|
38 |
val dest_query_local: int option -> Proof.context attribute
|
|
39 |
val setup: (theory -> theory) list
|
|
40 |
end;
|
|
41 |
|
|
42 |
structure RuleContext: RULE_CONTEXT =
|
|
43 |
struct
|
|
44 |
|
|
45 |
|
|
46 |
(** rule declaration contexts **)
|
|
47 |
|
|
48 |
(* rule kinds *)
|
|
49 |
|
|
50 |
val intro_bangK = (0, false);
|
|
51 |
val elim_bangK = (0, true);
|
|
52 |
val introK = (1, false);
|
|
53 |
val elimK = (1, true);
|
|
54 |
val intro_queryK = (2, false);
|
|
55 |
val elim_queryK = (2, true);
|
|
56 |
|
|
57 |
val kind_names =
|
|
58 |
[(intro_bangK, "safe introduction rules (intro!)"),
|
|
59 |
(elim_bangK, "safe elimination rules (elim!)"),
|
|
60 |
(introK, "introduction rules (intro)"),
|
|
61 |
(elimK, "elimination rules (elim)"),
|
|
62 |
(intro_queryK, "extra introduction rules (intro?)"),
|
|
63 |
(elim_queryK, "extra elimination rules (elim?)")];
|
|
64 |
|
|
65 |
val rule_kinds = map #1 kind_names;
|
|
66 |
val rule_indexes = distinct (map #1 rule_kinds);
|
|
67 |
|
|
68 |
|
|
69 |
(* netpairs *)
|
|
70 |
|
|
71 |
type net = ((int * int) * (bool * thm)) Net.net;
|
|
72 |
|
|
73 |
val empty_netpairs = replicate (length rule_indexes) (Net.empty: net, Net.empty: net);
|
|
74 |
|
|
75 |
fun make_netpairs rules =
|
|
76 |
let
|
|
77 |
fun add (netpairs, (n, (w, ((i, b), th)))) =
|
|
78 |
map_nth_elem i (curry insert_tagged_brl ((w, n), (b, th))) netpairs;
|
|
79 |
val next = ~ (length rules);
|
|
80 |
in (next - 1, foldl add (empty_netpairs, next upto ~1 ~~ rules)) end;
|
|
81 |
|
|
82 |
|
|
83 |
(* rules *)
|
|
84 |
|
|
85 |
datatype T = Rules of
|
|
86 |
{next: int,
|
|
87 |
rules: (int * ((int * bool) * thm)) list,
|
|
88 |
netpairs: (net * net) list,
|
|
89 |
wrappers: ((bool * ((int -> tactic) -> int -> tactic)) * stamp) list};
|
|
90 |
|
|
91 |
val empty_rules = Rules {next = ~1, rules = [], netpairs = empty_netpairs, wrappers = []};
|
|
92 |
|
|
93 |
fun add_rule (i, b) opt_w th (Rules {next, rules, netpairs, wrappers}) =
|
|
94 |
let val w = (case opt_w of Some w => w | None => Tactic.subgoals_of_brl (b, th)) in
|
|
95 |
Rules {next = next - 1,
|
|
96 |
rules = (w, ((i, b), th)) :: rules,
|
|
97 |
netpairs = map_nth_elem i (curry insert_tagged_brl ((w, next), (b, th))) netpairs,
|
|
98 |
wrappers = wrappers}
|
|
99 |
end;
|
|
100 |
|
|
101 |
fun print_rules prt x (Rules {rules, ...}) =
|
|
102 |
let
|
|
103 |
fun prt_kind (i, b) =
|
|
104 |
Pretty.big_list (the (assoc (kind_names, (i, b))) ^ ":")
|
|
105 |
(mapfilter (fn (_, (k, th)) => if k = (i, b) then Some (prt x th) else None) rules);
|
|
106 |
in Pretty.writeln (Pretty.chunks (map prt_kind rule_kinds)) end;
|
|
107 |
|
|
108 |
|
|
109 |
(* theory and proof data *)
|
|
110 |
|
|
111 |
structure GlobalRulesArgs =
|
|
112 |
struct
|
|
113 |
val name = "Isar/rule_context";
|
|
114 |
type T = T;
|
|
115 |
|
|
116 |
val empty = empty_rules;
|
|
117 |
val copy = I;
|
|
118 |
val finish = I;
|
|
119 |
val prep_ext = I;
|
|
120 |
|
|
121 |
fun merge (Rules {rules = rules1, wrappers = wrappers1, ...},
|
|
122 |
Rules {rules = rules2, wrappers = wrappers2, ...}) =
|
|
123 |
let
|
|
124 |
val wrappers = gen_merge_lists' eq_snd wrappers1 wrappers2;
|
|
125 |
val rules = gen_merge_lists' (fn ((_, (k1, th1)), (_, (k2, th2))) =>
|
|
126 |
k1 = k2 andalso Thm.eq_thm (th1, th2)) rules1 rules2;
|
|
127 |
val next = ~ (length rules);
|
|
128 |
val netpairs = foldl (fn (nps, (n, (w, ((i, b), th)))) =>
|
|
129 |
map_nth_elem i (curry insert_tagged_brl ((w, n), (b, th))) nps)
|
|
130 |
(empty_netpairs, next upto ~1 ~~ rules);
|
|
131 |
in Rules {next = next - 1, rules = rules, netpairs = netpairs, wrappers = wrappers} end;
|
|
132 |
|
|
133 |
val print = print_rules Display.pretty_thm_sg;
|
|
134 |
end;
|
|
135 |
|
|
136 |
structure GlobalRules = TheoryDataFun(GlobalRulesArgs);
|
|
137 |
val print_global_rules = GlobalRules.print;
|
|
138 |
|
|
139 |
structure LocalRulesArgs =
|
|
140 |
struct
|
|
141 |
val name = GlobalRulesArgs.name;
|
|
142 |
type T = GlobalRulesArgs.T;
|
|
143 |
val init = GlobalRules.get;
|
|
144 |
val print = print_rules ProofContext.pretty_thm;
|
|
145 |
end;
|
|
146 |
|
|
147 |
structure LocalRules = ProofDataFun(LocalRulesArgs);
|
|
148 |
val print_local_rules = LocalRules.print;
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
(** attributes **)
|
|
153 |
|
|
154 |
(* add rules *)
|
|
155 |
|
|
156 |
local
|
|
157 |
|
|
158 |
fun mk_att k view map_data opt_w (x, th) = (map_data (add_rule k opt_w th) x, th);
|
|
159 |
|
|
160 |
in
|
|
161 |
|
|
162 |
val intro_bang_global = mk_att intro_bangK I GlobalRules.map;
|
|
163 |
val elim_bang_global = mk_att elim_bangK I GlobalRules.map;
|
|
164 |
val dest_bang_global = mk_att elim_bangK Tactic.make_elim GlobalRules.map;
|
|
165 |
val intro_global = mk_att introK I GlobalRules.map;
|
|
166 |
val elim_global = mk_att elimK I GlobalRules.map;
|
|
167 |
val dest_global = mk_att elimK Tactic.make_elim GlobalRules.map;
|
|
168 |
val intro_query_global = mk_att intro_queryK I GlobalRules.map;
|
|
169 |
val elim_query_global = mk_att elim_queryK I GlobalRules.map;
|
|
170 |
val dest_query_global = mk_att elim_queryK Tactic.make_elim GlobalRules.map;
|
|
171 |
|
|
172 |
val intro_bang_local = mk_att intro_bangK I LocalRules.map;
|
|
173 |
val elim_bang_local = mk_att elim_bangK I LocalRules.map;
|
|
174 |
val dest_bang_local = mk_att elim_bangK Tactic.make_elim LocalRules.map;
|
|
175 |
val intro_local = mk_att introK I LocalRules.map;
|
|
176 |
val elim_local = mk_att elimK I LocalRules.map;
|
|
177 |
val dest_local = mk_att elimK Tactic.make_elim LocalRules.map;
|
|
178 |
val intro_query_local = mk_att intro_queryK I LocalRules.map;
|
|
179 |
val elim_query_local = mk_att elim_queryK I LocalRules.map;
|
|
180 |
val dest_query_local = mk_att elim_queryK Tactic.make_elim LocalRules.map;
|
|
181 |
|
|
182 |
end;
|
|
183 |
|
|
184 |
|
|
185 |
(* concrete syntax *)
|
|
186 |
|
|
187 |
fun add_args a b c x = Attrib.syntax
|
|
188 |
(Scan.lift (Scan.option (Args.bracks Args.nat) --
|
|
189 |
(Args.bang >> K a || Args.query >> K c || Scan.succeed b) >> op |>)) x;
|
|
190 |
|
|
191 |
val rule_atts =
|
|
192 |
[("intro",
|
|
193 |
(add_args intro_bang_global intro_global intro_query_global,
|
|
194 |
add_args intro_bang_local intro_local intro_query_local),
|
|
195 |
"declaration of introduction rule"),
|
|
196 |
("elim",
|
|
197 |
(add_args elim_bang_global elim_global elim_query_global,
|
|
198 |
add_args elim_bang_local elim_local elim_query_local),
|
|
199 |
"declaration of elimination rule"),
|
|
200 |
("dest",
|
|
201 |
(add_args dest_bang_global dest_global dest_query_global,
|
|
202 |
add_args dest_bang_local dest_local dest_query_local),
|
|
203 |
"declaration of destruction rule")];
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
(** theory setup **)
|
|
208 |
|
|
209 |
val setup =
|
|
210 |
[GlobalRules.init, LocalRules.init
|
|
211 |
(*FIXME Attrib.add_attributes rule_atts*)];
|
|
212 |
|
|
213 |
|
|
214 |
end;
|