author | wenzelm |
Sun, 10 Dec 2006 15:30:40 +0100 | |
changeset 21742 | a330e58226d0 |
parent 21708 | 45e7491bea47 |
child 21962 | 279b129498b6 |
permissions | -rw-r--r-- |
10413 | 1 |
(* Title: Pure/meta_simplifier.ML |
2 |
ID: $Id$ |
|
11672 | 3 |
Author: Tobias Nipkow and Stefan Berghofer |
10413 | 4 |
|
11672 | 5 |
Meta-level Simplification. |
10413 | 6 |
*) |
7 |
||
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
8 |
infix 4 |
15023 | 9 |
addsimps delsimps addeqcongs deleqcongs addcongs delcongs addsimprocs delsimprocs |
15199 | 10 |
setmksimps setmkcong setmksym setmkeqTrue settermless setsubgoaler |
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
11 |
setloop' setloop addloop addloop' delloop setSSolver addSSolver setSolver addSolver; |
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
12 |
|
11672 | 13 |
signature BASIC_META_SIMPLIFIER = |
14 |
sig |
|
15023 | 15 |
val debug_simp: bool ref |
11672 | 16 |
val trace_simp: bool ref |
13828 | 17 |
val simp_depth_limit: int ref |
16042 | 18 |
val trace_simp_depth_limit: int ref |
15023 | 19 |
type rrule |
16807 | 20 |
val eq_rrule: rrule * rrule -> bool |
15023 | 21 |
type cong |
22 |
type simpset |
|
23 |
type proc |
|
17614 | 24 |
type solver |
25 |
val mk_solver': string -> (simpset -> int -> tactic) -> solver |
|
26 |
val mk_solver: string -> (thm list -> int -> tactic) -> solver |
|
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
27 |
val rep_ss: simpset -> |
15023 | 28 |
{rules: rrule Net.net, |
29 |
prems: thm list, |
|
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
30 |
bounds: int * ((string * typ) * string) list, |
20289 | 31 |
context: Proof.context option} * |
15023 | 32 |
{congs: (string * cong) list * string list, |
33 |
procs: proc Net.net, |
|
34 |
mk_rews: |
|
35 |
{mk: thm -> thm list, |
|
36 |
mk_cong: thm -> thm, |
|
37 |
mk_sym: thm -> thm option, |
|
18208 | 38 |
mk_eq_True: thm -> thm option, |
39 |
reorient: theory -> term list -> term -> term -> bool}, |
|
15023 | 40 |
termless: term * term -> bool, |
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
41 |
subgoal_tac: simpset -> int -> tactic, |
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
42 |
loop_tacs: (string * (simpset -> int -> tactic)) list, |
15023 | 43 |
solvers: solver list * solver list} |
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
44 |
val print_ss: simpset -> unit |
15023 | 45 |
val empty_ss: simpset |
46 |
val merge_ss: simpset * simpset -> simpset |
|
47 |
type simproc |
|
48 |
val mk_simproc: string -> cterm list -> |
|
16458 | 49 |
(theory -> simpset -> term -> thm option) -> simproc |
15023 | 50 |
val add_prems: thm list -> simpset -> simpset |
51 |
val prems_of_ss: simpset -> thm list |
|
52 |
val addsimps: simpset * thm list -> simpset |
|
53 |
val delsimps: simpset * thm list -> simpset |
|
54 |
val addeqcongs: simpset * thm list -> simpset |
|
55 |
val deleqcongs: simpset * thm list -> simpset |
|
56 |
val addcongs: simpset * thm list -> simpset |
|
57 |
val delcongs: simpset * thm list -> simpset |
|
58 |
val addsimprocs: simpset * simproc list -> simpset |
|
59 |
val delsimprocs: simpset * simproc list -> simpset |
|
60 |
val setmksimps: simpset * (thm -> thm list) -> simpset |
|
61 |
val setmkcong: simpset * (thm -> thm) -> simpset |
|
62 |
val setmksym: simpset * (thm -> thm option) -> simpset |
|
63 |
val setmkeqTrue: simpset * (thm -> thm option) -> simpset |
|
64 |
val settermless: simpset * (term * term -> bool) -> simpset |
|
65 |
val setsubgoaler: simpset * (simpset -> int -> tactic) -> simpset |
|
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
66 |
val setloop': simpset * (simpset -> int -> tactic) -> simpset |
15023 | 67 |
val setloop: simpset * (int -> tactic) -> simpset |
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
68 |
val addloop': simpset * (string * (simpset -> int -> tactic)) -> simpset |
15023 | 69 |
val addloop: simpset * (string * (int -> tactic)) -> simpset |
70 |
val delloop: simpset * string -> simpset |
|
71 |
val setSSolver: simpset * solver -> simpset |
|
72 |
val addSSolver: simpset * solver -> simpset |
|
73 |
val setSolver: simpset * solver -> simpset |
|
74 |
val addSolver: simpset * solver -> simpset |
|
21708 | 75 |
|
76 |
val rewrite_rule: thm list -> thm -> thm |
|
77 |
val rewrite_goals_rule: thm list -> thm -> thm |
|
78 |
val rewrite_goals_tac: thm list -> tactic |
|
79 |
val rewrite_tac: thm list -> tactic |
|
80 |
val rewtac: thm -> tactic |
|
81 |
val prune_params_tac: tactic |
|
82 |
val fold_rule: thm list -> thm -> thm |
|
83 |
val fold_tac: thm list -> tactic |
|
84 |
val fold_goals_tac: thm list -> tactic |
|
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
85 |
end; |
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
86 |
|
10413 | 87 |
signature META_SIMPLIFIER = |
88 |
sig |
|
11672 | 89 |
include BASIC_META_SIMPLIFIER |
10413 | 90 |
exception SIMPLIFIER of string * thm |
17966
34e420fa03ad
moved various simplification tactics and rules to simplifier.ML;
wenzelm
parents:
17897
diff
changeset
|
91 |
val solver: simpset -> solver -> int -> tactic |
15023 | 92 |
val clear_ss: simpset -> simpset |
93 |
exception SIMPROC_FAIL of string * exn |
|
16458 | 94 |
val simproc_i: theory -> string -> term list |
95 |
-> (theory -> simpset -> term -> thm option) -> simproc |
|
96 |
val simproc: theory -> string -> string list |
|
97 |
-> (theory -> simpset -> term -> thm option) -> simproc |
|
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
98 |
val inherit_context: simpset -> simpset -> simpset |
20289 | 99 |
val the_context: simpset -> Proof.context |
100 |
val context: Proof.context -> simpset -> simpset |
|
17897 | 101 |
val theory_context: theory -> simpset -> simpset |
17723 | 102 |
val debug_bounds: bool ref |
18208 | 103 |
val set_reorient: (theory -> term list -> term -> term -> bool) -> simpset -> simpset |
17966
34e420fa03ad
moved various simplification tactics and rules to simplifier.ML;
wenzelm
parents:
17897
diff
changeset
|
104 |
val set_solvers: solver list -> simpset -> simpset |
11672 | 105 |
val rewrite_cterm: bool * bool * bool -> |
15023 | 106 |
(simpset -> thm -> thm option) -> simpset -> cterm -> thm |
16458 | 107 |
val rewrite_term: theory -> thm list -> (term -> term option) list -> term -> term |
15023 | 108 |
val rewrite_thm: bool * bool * bool -> |
109 |
(simpset -> thm -> thm option) -> simpset -> thm -> thm |
|
110 |
val rewrite_goal_rule: bool * bool * bool -> |
|
111 |
(simpset -> thm -> thm option) -> simpset -> int -> thm -> thm |
|
21605 | 112 |
val norm_hhf: thm -> thm |
113 |
val norm_hhf_protect: thm -> thm |
|
21708 | 114 |
val rewrite: bool -> thm list -> cterm -> thm |
115 |
val simplify: bool -> thm list -> thm -> thm |
|
10413 | 116 |
end; |
117 |
||
15023 | 118 |
structure MetaSimplifier: META_SIMPLIFIER = |
10413 | 119 |
struct |
120 |
||
15023 | 121 |
|
122 |
(** datatype simpset **) |
|
123 |
||
124 |
(* rewrite rules *) |
|
10413 | 125 |
|
20546
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
126 |
type rrule = |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
127 |
{thm: thm, (*the rewrite rule*) |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
128 |
name: string, (*name of theorem from which rewrite rule was extracted*) |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
129 |
lhs: term, (*the left-hand side*) |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
130 |
elhs: cterm, (*the etac-contracted lhs*) |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
131 |
extra: bool, (*extra variables outside of elhs*) |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
132 |
fo: bool, (*use first-order matching*) |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
133 |
perm: bool}; (*the rewrite rule is permutative*) |
15023 | 134 |
|
20546
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
135 |
(* |
12603 | 136 |
Remarks: |
10413 | 137 |
- elhs is used for matching, |
15023 | 138 |
lhs only for preservation of bound variable names; |
10413 | 139 |
- fo is set iff |
140 |
either elhs is first-order (no Var is applied), |
|
15023 | 141 |
in which case fo-matching is complete, |
10413 | 142 |
or elhs is not a pattern, |
20546
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
143 |
in which case there is nothing better to do; |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
144 |
*) |
10413 | 145 |
|
146 |
fun eq_rrule ({thm = thm1, ...}: rrule, {thm = thm2, ...}: rrule) = |
|
15023 | 147 |
Drule.eq_thm_prop (thm1, thm2); |
148 |
||
149 |
||
150 |
(* congruences *) |
|
151 |
||
152 |
type cong = {thm: thm, lhs: cterm}; |
|
10413 | 153 |
|
12603 | 154 |
fun eq_cong ({thm = thm1, ...}: cong, {thm = thm2, ...}: cong) = |
15023 | 155 |
Drule.eq_thm_prop (thm1, thm2); |
10413 | 156 |
|
157 |
||
17614 | 158 |
(* simplification sets, procedures, and solvers *) |
15023 | 159 |
|
160 |
(*A simpset contains data required during conversion: |
|
10413 | 161 |
rules: discrimination net of rewrite rules; |
15023 | 162 |
prems: current premises; |
15249
0da6b3075bfa
Replaced list of bound variables in simpset by maximal index of bound
berghofe
parents:
15199
diff
changeset
|
163 |
bounds: maximal index of bound variables already used |
15023 | 164 |
(for generating new names when rewriting under lambda abstractions); |
10413 | 165 |
congs: association list of congruence rules and |
166 |
a list of `weak' congruence constants. |
|
167 |
A congruence is `weak' if it avoids normalization of some argument. |
|
168 |
procs: discrimination net of simplification procedures |
|
169 |
(functions that prove rewrite rules on the fly); |
|
15023 | 170 |
mk_rews: |
171 |
mk: turn simplification thms into rewrite rules; |
|
172 |
mk_cong: prepare congruence rules; |
|
173 |
mk_sym: turn == around; |
|
174 |
mk_eq_True: turn P into P == True; |
|
175 |
termless: relation for ordered rewriting;*) |
|
15011 | 176 |
|
15023 | 177 |
type mk_rews = |
178 |
{mk: thm -> thm list, |
|
179 |
mk_cong: thm -> thm, |
|
180 |
mk_sym: thm -> thm option, |
|
18208 | 181 |
mk_eq_True: thm -> thm option, |
182 |
reorient: theory -> term list -> term -> term -> bool}; |
|
15023 | 183 |
|
184 |
datatype simpset = |
|
185 |
Simpset of |
|
186 |
{rules: rrule Net.net, |
|
10413 | 187 |
prems: thm list, |
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
188 |
bounds: int * ((string * typ) * string) list, |
20289 | 189 |
context: Proof.context option} * |
15023 | 190 |
{congs: (string * cong) list * string list, |
191 |
procs: proc Net.net, |
|
192 |
mk_rews: mk_rews, |
|
11504 | 193 |
termless: term * term -> bool, |
15011 | 194 |
subgoal_tac: simpset -> int -> tactic, |
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
195 |
loop_tacs: (string * (simpset -> int -> tactic)) list, |
15023 | 196 |
solvers: solver list * solver list} |
197 |
and proc = |
|
198 |
Proc of |
|
199 |
{name: string, |
|
200 |
lhs: cterm, |
|
16458 | 201 |
proc: theory -> simpset -> term -> thm option, |
17614 | 202 |
id: stamp} |
203 |
and solver = |
|
204 |
Solver of |
|
205 |
{name: string, |
|
206 |
solver: simpset -> int -> tactic, |
|
15023 | 207 |
id: stamp}; |
208 |
||
209 |
||
210 |
fun rep_ss (Simpset args) = args; |
|
10413 | 211 |
|
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
212 |
fun make_ss1 (rules, prems, bounds, context) = |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
213 |
{rules = rules, prems = prems, bounds = bounds, context = context}; |
15023 | 214 |
|
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
215 |
fun map_ss1 f {rules, prems, bounds, context} = |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
216 |
make_ss1 (f (rules, prems, bounds, context)); |
10413 | 217 |
|
15023 | 218 |
fun make_ss2 (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) = |
219 |
{congs = congs, procs = procs, mk_rews = mk_rews, termless = termless, |
|
220 |
subgoal_tac = subgoal_tac, loop_tacs = loop_tacs, solvers = solvers}; |
|
221 |
||
222 |
fun map_ss2 f {congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers} = |
|
223 |
make_ss2 (f (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers)); |
|
224 |
||
225 |
fun make_simpset (args1, args2) = Simpset (make_ss1 args1, make_ss2 args2); |
|
10413 | 226 |
|
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
227 |
fun map_simpset f (Simpset ({rules, prems, bounds, context}, |
15023 | 228 |
{congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers})) = |
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
229 |
make_simpset (f ((rules, prems, bounds, context), |
15023 | 230 |
(congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers))); |
10413 | 231 |
|
15023 | 232 |
fun map_simpset1 f (Simpset (r1, r2)) = Simpset (map_ss1 f r1, r2); |
233 |
fun map_simpset2 f (Simpset (r1, r2)) = Simpset (r1, map_ss2 f r2); |
|
234 |
||
17614 | 235 |
fun prems_of_ss (Simpset ({prems, ...}, _)) = prems; |
236 |
||
237 |
||
238 |
fun eq_proc (Proc {id = id1, ...}, Proc {id = id2, ...}) = (id1 = id2); |
|
239 |
||
240 |
fun mk_solver' name solver = Solver {name = name, solver = solver, id = stamp ()}; |
|
241 |
fun mk_solver name solver = mk_solver' name (solver o prems_of_ss); |
|
242 |
||
243 |
fun solver_name (Solver {name, ...}) = name; |
|
17966
34e420fa03ad
moved various simplification tactics and rules to simplifier.ML;
wenzelm
parents:
17897
diff
changeset
|
244 |
fun solver ss (Solver {solver = tac, ...}) = tac ss; |
17614 | 245 |
fun eq_solver (Solver {id = id1, ...}, Solver {id = id2, ...}) = (id1 = id2); |
246 |
val merge_solvers = gen_merge_lists eq_solver; |
|
247 |
||
15023 | 248 |
|
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
249 |
(* diagnostics *) |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
250 |
|
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
251 |
exception SIMPLIFIER of string * thm; |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
252 |
|
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
253 |
val debug_simp = ref false; |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
254 |
val trace_simp = ref false; |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
255 |
val simp_depth = ref 0; |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
256 |
val simp_depth_limit = ref 100; |
18573 | 257 |
val trace_simp_depth_limit = ref 1; |
258 |
val trace_simp_depth_limit_exceeded = ref false; |
|
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
259 |
local |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
260 |
|
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
261 |
fun println a = |
18573 | 262 |
if ! simp_depth > ! trace_simp_depth_limit |
263 |
then if !trace_simp_depth_limit_exceeded then () |
|
264 |
else (tracing "trace_simp_depth_limit exceeded!"; |
|
265 |
trace_simp_depth_limit_exceeded := true) |
|
266 |
else (tracing (enclose "[" "]" (string_of_int (! simp_depth)) ^ a); |
|
267 |
trace_simp_depth_limit_exceeded := false); |
|
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
268 |
|
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
269 |
fun prnt warn a = if warn then warning a else println a; |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
270 |
|
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
271 |
fun show_bounds (Simpset ({bounds = (_, bs), ...}, _)) t = |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
272 |
let |
20146 | 273 |
val names = Term.declare_term_names t Name.context; |
274 |
val xs = rev (#1 (Name.variants (rev (map #2 bs)) names)); |
|
17614 | 275 |
fun subst (((b, T), _), x') = (Free (b, T), Syntax.mark_boundT (x', T)); |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
276 |
in Term.subst_atomic (ListPair.map subst (bs, xs)) t end; |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
277 |
|
17705 | 278 |
in |
279 |
||
280 |
fun print_term warn a ss thy t = prnt warn (a ^ "\n" ^ |
|
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
281 |
Sign.string_of_term thy (if ! debug_simp then t else show_bounds ss t)); |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
282 |
|
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
283 |
fun debug warn a = if ! debug_simp then prnt warn a else (); |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
284 |
fun trace warn a = if ! trace_simp then prnt warn a else (); |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
285 |
|
17705 | 286 |
fun debug_term warn a ss thy t = if ! debug_simp then print_term warn a ss thy t else (); |
287 |
fun trace_term warn a ss thy t = if ! trace_simp then print_term warn a ss thy t else (); |
|
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
288 |
|
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
289 |
fun trace_cterm warn a ss ct = |
17705 | 290 |
if ! trace_simp then print_term warn a ss (Thm.theory_of_cterm ct) (Thm.term_of ct) else (); |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
291 |
|
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
292 |
fun trace_thm a ss th = |
17705 | 293 |
if ! trace_simp then print_term false a ss (Thm.theory_of_thm th) (Thm.full_prop_of th) else (); |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
294 |
|
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
295 |
fun trace_named_thm a ss (th, name) = |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
296 |
if ! trace_simp then |
17705 | 297 |
print_term false (if name = "" then a else a ^ " " ^ quote name ^ ":") ss |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
298 |
(Thm.theory_of_thm th) (Thm.full_prop_of th) |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
299 |
else (); |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
300 |
|
17705 | 301 |
fun warn_thm a ss th = print_term true a ss (Thm.theory_of_thm th) (Thm.full_prop_of th); |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
302 |
|
20028
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
303 |
fun cond_warn_thm a (ss as Simpset ({context, ...}, _)) th = |
20546
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
304 |
if is_some context then () else warn_thm a ss th; |
20028
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
305 |
|
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
306 |
end; |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
307 |
|
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
308 |
|
15023 | 309 |
(* print simpsets *) |
10413 | 310 |
|
15023 | 311 |
fun print_ss ss = |
312 |
let |
|
15034 | 313 |
val pretty_thms = map Display.pretty_thm; |
15023 | 314 |
|
15034 | 315 |
fun pretty_cong (name, th) = |
316 |
Pretty.block [Pretty.str (name ^ ":"), Pretty.brk 1, Display.pretty_thm th]; |
|
15023 | 317 |
fun pretty_proc (name, lhss) = |
318 |
Pretty.big_list (name ^ ":") (map Display.pretty_cterm lhss); |
|
15034 | 319 |
|
320 |
val Simpset ({rules, ...}, {congs, procs, loop_tacs, solvers, ...}) = ss; |
|
16807 | 321 |
val smps = map #thm (Net.entries rules); |
15034 | 322 |
val cngs = map (fn (name, {thm, ...}) => (name, thm)) (#1 congs); |
16807 | 323 |
val prcs = Net.entries procs |> |
324 |
map (fn Proc {name, lhs, id, ...} => ((name, lhs), id)) |
|
17496 | 325 |
|> partition_eq (eq_snd (op =)) |
17756 | 326 |
|> map (fn ps => (fst (fst (hd ps)), map (snd o fst) ps)) |
327 |
|> Library.sort_wrt fst; |
|
15023 | 328 |
in |
15034 | 329 |
[Pretty.big_list "simplification rules:" (pretty_thms smps), |
330 |
Pretty.big_list "simplification procedures:" (map pretty_proc prcs), |
|
331 |
Pretty.big_list "congruences:" (map pretty_cong cngs), |
|
21286
b5e7b80caa6a
introduces canonical AList functions for loop_tacs
haftmann
parents:
20972
diff
changeset
|
332 |
Pretty.strs ("loopers:" :: map (quote o fst) loop_tacs), |
15088 | 333 |
Pretty.strs ("unsafe solvers:" :: map (quote o solver_name) (#1 solvers)), |
334 |
Pretty.strs ("safe solvers:" :: map (quote o solver_name) (#2 solvers))] |
|
15023 | 335 |
|> Pretty.chunks |> Pretty.writeln |
13828 | 336 |
end; |
10413 | 337 |
|
15023 | 338 |
|
339 |
(* simprocs *) |
|
340 |
||
341 |
exception SIMPROC_FAIL of string * exn; |
|
342 |
||
343 |
datatype simproc = Simproc of proc list; |
|
344 |
||
345 |
fun mk_simproc name lhss proc = |
|
346 |
let val id = stamp () in |
|
347 |
Simproc (lhss |> map (fn lhs => |
|
348 |
Proc {name = name, lhs = lhs, proc = proc, id = id})) |
|
349 |
end; |
|
350 |
||
19798 | 351 |
(* FIXME avoid global thy and Logic.varify *) |
16458 | 352 |
fun simproc_i thy name = mk_simproc name o map (Thm.cterm_of thy o Logic.varify); |
16807 | 353 |
fun simproc thy name = simproc_i thy name o map (Sign.read_term thy); |
15023 | 354 |
|
15011 | 355 |
|
10413 | 356 |
|
357 |
(** simpset operations **) |
|
358 |
||
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
359 |
(* context *) |
10413 | 360 |
|
17614 | 361 |
fun eq_bound (x: string, (y, _)) = x = y; |
362 |
||
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
363 |
fun add_bound bound = map_simpset1 (fn (rules, prems, (count, bounds), context) => |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
364 |
(rules, prems, (count + 1, bound :: bounds), context)); |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
365 |
|
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
366 |
fun add_prems ths = map_simpset1 (fn (rules, prems, bounds, context) => |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
367 |
(rules, ths @ prems, bounds, context)); |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
368 |
|
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
369 |
fun inherit_context (Simpset ({bounds, context, ...}, _)) = |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
370 |
map_simpset1 (fn (rules, prems, _, _) => (rules, prems, bounds, context)); |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
371 |
|
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
372 |
fun the_context (Simpset ({context = SOME ctxt, ...}, _)) = ctxt |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
373 |
| the_context _ = raise Fail "Simplifier: no proof context in simpset"; |
10413 | 374 |
|
17897 | 375 |
fun context ctxt = |
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
376 |
map_simpset1 (fn (rules, prems, bounds, _) => (rules, prems, bounds, SOME ctxt)); |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
377 |
|
21516 | 378 |
val theory_context = context o ProofContext.init; |
17897 | 379 |
|
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
380 |
fun fallback_context _ (ss as Simpset ({context = SOME _, ...}, _)) = ss |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
381 |
| fallback_context thy ss = |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
382 |
(warning "Simplifier: no proof context in simpset -- fallback to theory context!"; |
17897 | 383 |
theory_context thy ss); |
384 |
||
385 |
||
20028
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
386 |
(* maintain simp rules *) |
10413 | 387 |
|
20546
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
388 |
(* FIXME: it seems that the conditions on extra variables are too liberal if |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
389 |
prems are nonempty: does solving the prems really guarantee instantiation of |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
390 |
all its Vars? Better: a dynamic check each time a rule is applied. |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
391 |
*) |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
392 |
fun rewrite_rule_extra_vars prems elhs erhs = |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
393 |
let |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
394 |
val elhss = elhs :: prems; |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
395 |
val tvars = fold Term.add_tvars elhss []; |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
396 |
val vars = fold Term.add_vars elhss []; |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
397 |
in |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
398 |
erhs |> Term.exists_type (Term.exists_subtype |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
399 |
(fn TVar v => not (member (op =) tvars v) | _ => false)) orelse |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
400 |
erhs |> Term.exists_subterm |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
401 |
(fn Var v => not (member (op =) vars v) | _ => false) |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
402 |
end; |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
403 |
|
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
404 |
fun rrule_extra_vars elhs thm = |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
405 |
rewrite_rule_extra_vars [] (term_of elhs) (Thm.full_prop_of thm); |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
406 |
|
15023 | 407 |
fun mk_rrule2 {thm, name, lhs, elhs, perm} = |
408 |
let |
|
20546
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
409 |
val t = term_of elhs; |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
410 |
val fo = Pattern.first_order t orelse not (Pattern.pattern t); |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
411 |
val extra = rrule_extra_vars elhs thm; |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
412 |
in {thm = thm, name = name, lhs = lhs, elhs = elhs, extra = extra, fo = fo, perm = perm} end; |
10413 | 413 |
|
20028
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
414 |
fun del_rrule (rrule as {thm, elhs, ...}) ss = |
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
415 |
ss |> map_simpset1 (fn (rules, prems, bounds, context) => |
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
416 |
(Net.delete_term eq_rrule (term_of elhs, rrule) rules, prems, bounds, context)) |
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
417 |
handle Net.DELETE => (cond_warn_thm "Rewrite rule not in simpset:" ss thm; ss); |
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
418 |
|
20546
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
419 |
fun insert_rrule (rrule as {thm, name, elhs, ...}) ss = |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
420 |
(trace_named_thm "Adding rewrite rule" ss (thm, name); |
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
421 |
ss |> map_simpset1 (fn (rules, prems, bounds, context) => |
15023 | 422 |
let |
423 |
val rrule2 as {elhs, ...} = mk_rrule2 rrule; |
|
16807 | 424 |
val rules' = Net.insert_term eq_rrule (term_of elhs, rrule2) rules; |
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
425 |
in (rules', prems, bounds, context) end) |
20028
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
426 |
handle Net.INSERT => (cond_warn_thm "Ignoring duplicate rewrite rule:" ss thm; ss)); |
10413 | 427 |
|
428 |
fun vperm (Var _, Var _) = true |
|
429 |
| vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t) |
|
430 |
| vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2) |
|
431 |
| vperm (t, u) = (t = u); |
|
432 |
||
433 |
fun var_perm (t, u) = |
|
20197
ffc64d90fc1f
use Term.add_vars instead of obsolete term_varnames;
wenzelm
parents:
20146
diff
changeset
|
434 |
vperm (t, u) andalso gen_eq_set (op =) (Term.add_vars t [], Term.add_vars u []); |
10413 | 435 |
|
15023 | 436 |
(*simple test for looping rewrite rules and stupid orientations*) |
18208 | 437 |
fun default_reorient thy prems lhs rhs = |
15023 | 438 |
rewrite_rule_extra_vars prems lhs rhs |
439 |
orelse |
|
440 |
is_Var (head_of lhs) |
|
441 |
orelse |
|
16305 | 442 |
(* turns t = x around, which causes a headache if x is a local variable - |
443 |
usually it is very useful :-( |
|
444 |
is_Free rhs andalso not(is_Free lhs) andalso not(Logic.occs(rhs,lhs)) |
|
445 |
andalso not(exists_subterm is_Var lhs) |
|
446 |
orelse |
|
447 |
*) |
|
16842 | 448 |
exists (fn t => Logic.occs (lhs, t)) (rhs :: prems) |
15023 | 449 |
orelse |
17203 | 450 |
null prems andalso Pattern.matches thy (lhs, rhs) |
10413 | 451 |
(*the condition "null prems" is necessary because conditional rewrites |
452 |
with extra variables in the conditions may terminate although |
|
15023 | 453 |
the rhs is an instance of the lhs; example: ?m < ?n ==> f(?n) == f(?m)*) |
454 |
orelse |
|
455 |
is_Const lhs andalso not (is_Const rhs); |
|
10413 | 456 |
|
457 |
fun decomp_simp thm = |
|
15023 | 458 |
let |
16458 | 459 |
val {thy, prop, ...} = Thm.rep_thm thm; |
15023 | 460 |
val prems = Logic.strip_imp_prems prop; |
461 |
val concl = Drule.strip_imp_concl (Thm.cprop_of thm); |
|
462 |
val (lhs, rhs) = Drule.dest_equals concl handle TERM _ => |
|
463 |
raise SIMPLIFIER ("Rewrite rule not a meta-equality", thm); |
|
20579 | 464 |
val elhs = Thm.dest_arg (Thm.cprop_of (Thm.eta_conversion lhs)); |
16665 | 465 |
val elhs = if term_of elhs aconv term_of lhs then lhs else elhs; (*share identical copies*) |
18929 | 466 |
val erhs = Envir.eta_contract (term_of rhs); |
15023 | 467 |
val perm = |
468 |
var_perm (term_of elhs, erhs) andalso |
|
469 |
not (term_of elhs aconv erhs) andalso |
|
470 |
not (is_Var (term_of elhs)); |
|
16458 | 471 |
in (thy, prems, term_of lhs, elhs, term_of rhs, perm) end; |
10413 | 472 |
|
12783 | 473 |
fun decomp_simp' thm = |
12979
4c76bce4ce39
decomp_simp': use lhs instead of elhs (preserves more bound variable names);
wenzelm
parents:
12783
diff
changeset
|
474 |
let val (_, _, lhs, _, rhs, _) = decomp_simp thm in |
12783 | 475 |
if Thm.nprems_of thm > 0 then raise SIMPLIFIER ("Bad conditional rewrite rule", thm) |
12979
4c76bce4ce39
decomp_simp': use lhs instead of elhs (preserves more bound variable names);
wenzelm
parents:
12783
diff
changeset
|
476 |
else (lhs, rhs) |
12783 | 477 |
end; |
478 |
||
15023 | 479 |
fun mk_eq_True (Simpset (_, {mk_rews = {mk_eq_True, ...}, ...})) (thm, name) = |
480 |
(case mk_eq_True thm of |
|
15531 | 481 |
NONE => [] |
482 |
| SOME eq_True => |
|
20546
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
483 |
let |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
484 |
val (_, _, lhs, elhs, _, _) = decomp_simp eq_True; |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
485 |
val extra = rrule_extra_vars elhs eq_True; |
15023 | 486 |
in [{thm = eq_True, name = name, lhs = lhs, elhs = elhs, perm = false}] end); |
10413 | 487 |
|
15023 | 488 |
(*create the rewrite rule and possibly also the eq_True variant, |
489 |
in case there are extra vars on the rhs*) |
|
490 |
fun rrule_eq_True (thm, name, lhs, elhs, rhs, ss, thm2) = |
|
491 |
let val rrule = {thm = thm, name = name, lhs = lhs, elhs = elhs, perm = false} in |
|
20546
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
492 |
if rewrite_rule_extra_vars [] lhs rhs then |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
493 |
mk_eq_True ss (thm2, name) @ [rrule] |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
494 |
else [rrule] |
10413 | 495 |
end; |
496 |
||
15023 | 497 |
fun mk_rrule ss (thm, name) = |
498 |
let val (_, prems, lhs, elhs, rhs, perm) = decomp_simp thm in |
|
499 |
if perm then [{thm = thm, name = name, lhs = lhs, elhs = elhs, perm = true}] |
|
500 |
else |
|
501 |
(*weak test for loops*) |
|
502 |
if rewrite_rule_extra_vars prems lhs rhs orelse is_Var (term_of elhs) |
|
503 |
then mk_eq_True ss (thm, name) |
|
504 |
else rrule_eq_True (thm, name, lhs, elhs, rhs, ss, thm) |
|
10413 | 505 |
end; |
506 |
||
15023 | 507 |
fun orient_rrule ss (thm, name) = |
18208 | 508 |
let |
509 |
val (thy, prems, lhs, elhs, rhs, perm) = decomp_simp thm; |
|
510 |
val Simpset (_, {mk_rews = {reorient, mk_sym, ...}, ...}) = ss; |
|
511 |
in |
|
15023 | 512 |
if perm then [{thm = thm, name = name, lhs = lhs, elhs = elhs, perm = true}] |
16458 | 513 |
else if reorient thy prems lhs rhs then |
514 |
if reorient thy prems rhs lhs |
|
15023 | 515 |
then mk_eq_True ss (thm, name) |
516 |
else |
|
18208 | 517 |
(case mk_sym thm of |
518 |
NONE => [] |
|
519 |
| SOME thm' => |
|
520 |
let val (_, _, lhs', elhs', rhs', _) = decomp_simp thm' |
|
521 |
in rrule_eq_True (thm', name, lhs', elhs', rhs', ss, thm) end) |
|
15023 | 522 |
else rrule_eq_True (thm, name, lhs, elhs, rhs, ss, thm) |
10413 | 523 |
end; |
524 |
||
15199 | 525 |
fun extract_rews (Simpset (_, {mk_rews = {mk, ...}, ...}), thms) = |
21646
c07b5b0e8492
thm/prf: separate official name vs. additional tags;
wenzelm
parents:
21605
diff
changeset
|
526 |
maps (fn thm => map (rpair (PureThy.get_name_hint thm)) (mk thm)) thms; |
10413 | 527 |
|
15023 | 528 |
fun extract_safe_rrules (ss, thm) = |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19303
diff
changeset
|
529 |
maps (orient_rrule ss) (extract_rews (ss, [thm])); |
10413 | 530 |
|
531 |
||
20028
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
532 |
(* add/del rules explicitly *) |
10413 | 533 |
|
20028
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
534 |
fun comb_simps comb mk_rrule (ss, thms) = |
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
535 |
let |
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
536 |
val rews = extract_rews (ss, thms); |
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
537 |
in fold (fold comb o mk_rrule) rews ss end; |
10413 | 538 |
|
20028
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
539 |
fun ss addsimps thms = |
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
540 |
comb_simps insert_rrule (mk_rrule ss) (ss, thms); |
10413 | 541 |
|
15023 | 542 |
fun ss delsimps thms = |
20028
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
543 |
comb_simps del_rrule (map mk_rrule2 o mk_rrule ss) (ss, thms); |
15023 | 544 |
|
545 |
||
546 |
(* congs *) |
|
10413 | 547 |
|
15531 | 548 |
fun cong_name (Const (a, _)) = SOME a |
549 |
| cong_name (Free (a, _)) = SOME ("Free: " ^ a) |
|
550 |
| cong_name _ = NONE; |
|
13835
12b2ffbe543a
Change to meta simplifier: congruence rules may now have frees as head of term.
ballarin
parents:
13828
diff
changeset
|
551 |
|
15023 | 552 |
local |
553 |
||
554 |
fun is_full_cong_prems [] [] = true |
|
555 |
| is_full_cong_prems [] _ = false |
|
556 |
| is_full_cong_prems (p :: prems) varpairs = |
|
557 |
(case Logic.strip_assums_concl p of |
|
558 |
Const ("==", _) $ lhs $ rhs => |
|
559 |
let val (x, xs) = strip_comb lhs and (y, ys) = strip_comb rhs in |
|
560 |
is_Var x andalso forall is_Bound xs andalso |
|
20972 | 561 |
not (has_duplicates (op =) xs) andalso xs = ys andalso |
20671 | 562 |
member (op =) varpairs (x, y) andalso |
19303 | 563 |
is_full_cong_prems prems (remove (op =) (x, y) varpairs) |
15023 | 564 |
end |
565 |
| _ => false); |
|
566 |
||
567 |
fun is_full_cong thm = |
|
10413 | 568 |
let |
15023 | 569 |
val prems = prems_of thm and concl = concl_of thm; |
570 |
val (lhs, rhs) = Logic.dest_equals concl; |
|
571 |
val (f, xs) = strip_comb lhs and (g, ys) = strip_comb rhs; |
|
10413 | 572 |
in |
20972 | 573 |
f = g andalso not (has_duplicates (op =) (xs @ ys)) andalso length xs = length ys andalso |
15023 | 574 |
is_full_cong_prems prems (xs ~~ ys) |
10413 | 575 |
end; |
576 |
||
15023 | 577 |
fun add_cong (ss, thm) = ss |> |
578 |
map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) => |
|
579 |
let |
|
580 |
val (lhs, _) = Drule.dest_equals (Drule.strip_imp_concl (Thm.cprop_of thm)) |
|
581 |
handle TERM _ => raise SIMPLIFIER ("Congruence not a meta-equality", thm); |
|
18929 | 582 |
(*val lhs = Envir.eta_contract lhs;*) |
20057 | 583 |
val a = the (cong_name (head_of (term_of lhs))) handle Option.Option => |
15023 | 584 |
raise SIMPLIFIER ("Congruence must start with a constant or free variable", thm); |
585 |
val (alist, weak) = congs; |
|
586 |
val alist2 = overwrite_warn (alist, (a, {lhs = lhs, thm = thm})) |
|
587 |
("Overwriting congruence rule for " ^ quote a); |
|
588 |
val weak2 = if is_full_cong thm then weak else a :: weak; |
|
589 |
in ((alist2, weak2), procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) end); |
|
10413 | 590 |
|
15023 | 591 |
fun del_cong (ss, thm) = ss |> |
592 |
map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) => |
|
593 |
let |
|
594 |
val (lhs, _) = Logic.dest_equals (Thm.concl_of thm) handle TERM _ => |
|
595 |
raise SIMPLIFIER ("Congruence not a meta-equality", thm); |
|
18929 | 596 |
(*val lhs = Envir.eta_contract lhs;*) |
20057 | 597 |
val a = the (cong_name (head_of lhs)) handle Option.Option => |
15023 | 598 |
raise SIMPLIFIER ("Congruence must start with a constant", thm); |
599 |
val (alist, _) = congs; |
|
15570 | 600 |
val alist2 = List.filter (fn (x, _) => x <> a) alist; |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19303
diff
changeset
|
601 |
val weak2 = alist2 |> map_filter (fn (a, {thm, ...}: cong) => |
15531 | 602 |
if is_full_cong thm then NONE else SOME a); |
15023 | 603 |
in ((alist2, weak2), procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) end); |
10413 | 604 |
|
15023 | 605 |
fun mk_cong (Simpset (_, {mk_rews = {mk_cong = f, ...}, ...})) = f; |
606 |
||
607 |
in |
|
608 |
||
15570 | 609 |
val (op addeqcongs) = Library.foldl add_cong; |
610 |
val (op deleqcongs) = Library.foldl del_cong; |
|
15023 | 611 |
|
612 |
fun ss addcongs congs = ss addeqcongs map (mk_cong ss) congs; |
|
613 |
fun ss delcongs congs = ss deleqcongs map (mk_cong ss) congs; |
|
614 |
||
615 |
end; |
|
10413 | 616 |
|
617 |
||
15023 | 618 |
(* simprocs *) |
619 |
||
620 |
local |
|
10413 | 621 |
|
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
622 |
fun add_proc (proc as Proc {name, lhs, ...}) ss = |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
623 |
(trace_cterm false ("Adding simplification procedure " ^ quote name ^ " for") ss lhs; |
15023 | 624 |
map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) => |
16807 | 625 |
(congs, Net.insert_term eq_proc (term_of lhs, proc) procs, |
15023 | 626 |
mk_rews, termless, subgoal_tac, loop_tacs, solvers)) ss |
627 |
handle Net.INSERT => |
|
628 |
(warning ("Ignoring duplicate simplification procedure " ^ quote name); ss)); |
|
10413 | 629 |
|
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
630 |
fun del_proc (proc as Proc {name, lhs, ...}) ss = |
15023 | 631 |
map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) => |
16807 | 632 |
(congs, Net.delete_term eq_proc (term_of lhs, proc) procs, |
15023 | 633 |
mk_rews, termless, subgoal_tac, loop_tacs, solvers)) ss |
634 |
handle Net.DELETE => |
|
635 |
(warning ("Simplification procedure " ^ quote name ^ " not in simpset"); ss); |
|
10413 | 636 |
|
15023 | 637 |
in |
10413 | 638 |
|
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
639 |
fun ss addsimprocs ps = fold (fn Simproc procs => fold add_proc procs) ps ss; |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
640 |
fun ss delsimprocs ps = fold (fn Simproc procs => fold del_proc procs) ps ss; |
10413 | 641 |
|
15023 | 642 |
end; |
10413 | 643 |
|
644 |
||
645 |
(* mk_rews *) |
|
646 |
||
15023 | 647 |
local |
648 |
||
18208 | 649 |
fun map_mk_rews f = map_simpset2 (fn (congs, procs, {mk, mk_cong, mk_sym, mk_eq_True, reorient}, |
15023 | 650 |
termless, subgoal_tac, loop_tacs, solvers) => |
18208 | 651 |
let |
652 |
val (mk', mk_cong', mk_sym', mk_eq_True', reorient') = |
|
653 |
f (mk, mk_cong, mk_sym, mk_eq_True, reorient); |
|
654 |
val mk_rews' = {mk = mk', mk_cong = mk_cong', mk_sym = mk_sym', mk_eq_True = mk_eq_True', |
|
655 |
reorient = reorient'}; |
|
656 |
in (congs, procs, mk_rews', termless, subgoal_tac, loop_tacs, solvers) end); |
|
15023 | 657 |
|
658 |
in |
|
10413 | 659 |
|
18208 | 660 |
fun ss setmksimps mk = ss |> map_mk_rews (fn (_, mk_cong, mk_sym, mk_eq_True, reorient) => |
661 |
(mk, mk_cong, mk_sym, mk_eq_True, reorient)); |
|
15023 | 662 |
|
18208 | 663 |
fun ss setmkcong mk_cong = ss |> map_mk_rews (fn (mk, _, mk_sym, mk_eq_True, reorient) => |
664 |
(mk, mk_cong, mk_sym, mk_eq_True, reorient)); |
|
10413 | 665 |
|
18208 | 666 |
fun ss setmksym mk_sym = ss |> map_mk_rews (fn (mk, mk_cong, _, mk_eq_True, reorient) => |
667 |
(mk, mk_cong, mk_sym, mk_eq_True, reorient)); |
|
10413 | 668 |
|
18208 | 669 |
fun ss setmkeqTrue mk_eq_True = ss |> map_mk_rews (fn (mk, mk_cong, mk_sym, _, reorient) => |
670 |
(mk, mk_cong, mk_sym, mk_eq_True, reorient)); |
|
671 |
||
672 |
fun set_reorient reorient = map_mk_rews (fn (mk, mk_cong, mk_sym, mk_eq_True, _) => |
|
673 |
(mk, mk_cong, mk_sym, mk_eq_True, reorient)); |
|
15023 | 674 |
|
675 |
end; |
|
676 |
||
14242
ec70653a02bf
Added access to the mk_rews field (and friends).
skalberg
parents:
14040
diff
changeset
|
677 |
|
10413 | 678 |
(* termless *) |
679 |
||
15023 | 680 |
fun ss settermless termless = ss |> |
681 |
map_simpset2 (fn (congs, procs, mk_rews, _, subgoal_tac, loop_tacs, solvers) => |
|
682 |
(congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers)); |
|
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
683 |
|
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
684 |
|
15023 | 685 |
(* tactics *) |
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
686 |
|
15023 | 687 |
fun ss setsubgoaler subgoal_tac = ss |> |
688 |
map_simpset2 (fn (congs, procs, mk_rews, termless, _, loop_tacs, solvers) => |
|
689 |
(congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers)); |
|
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
690 |
|
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
691 |
fun ss setloop' tac = ss |> |
15023 | 692 |
map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, _, solvers) => |
693 |
(congs, procs, mk_rews, termless, subgoal_tac, [("", tac)], solvers)); |
|
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
694 |
|
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
695 |
fun ss setloop tac = ss setloop' (K tac); |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
696 |
|
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
697 |
fun ss addloop' (name, tac) = ss |> |
15023 | 698 |
map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) => |
699 |
(congs, procs, mk_rews, termless, subgoal_tac, |
|
21286
b5e7b80caa6a
introduces canonical AList functions for loop_tacs
haftmann
parents:
20972
diff
changeset
|
700 |
(if AList.defined (op =) loop_tacs name |
b5e7b80caa6a
introduces canonical AList functions for loop_tacs
haftmann
parents:
20972
diff
changeset
|
701 |
then warning ("Overwriting looper " ^ quote name) |
b5e7b80caa6a
introduces canonical AList functions for loop_tacs
haftmann
parents:
20972
diff
changeset
|
702 |
else (); AList.update (op =) (name, tac) loop_tacs), |
15023 | 703 |
solvers)); |
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
704 |
|
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
705 |
fun ss addloop (name, tac) = ss addloop' (name, K tac); |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
706 |
|
15023 | 707 |
fun ss delloop name = ss |> |
708 |
map_simpset2 (fn (congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, solvers) => |
|
21286
b5e7b80caa6a
introduces canonical AList functions for loop_tacs
haftmann
parents:
20972
diff
changeset
|
709 |
(congs, procs, mk_rews, termless, subgoal_tac, |
b5e7b80caa6a
introduces canonical AList functions for loop_tacs
haftmann
parents:
20972
diff
changeset
|
710 |
(if AList.defined (op =) loop_tacs name |
b5e7b80caa6a
introduces canonical AList functions for loop_tacs
haftmann
parents:
20972
diff
changeset
|
711 |
then () |
b5e7b80caa6a
introduces canonical AList functions for loop_tacs
haftmann
parents:
20972
diff
changeset
|
712 |
else warning ("No such looper in simpset: " ^ quote name); |
b5e7b80caa6a
introduces canonical AList functions for loop_tacs
haftmann
parents:
20972
diff
changeset
|
713 |
AList.delete (op =) name loop_tacs), solvers)); |
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
714 |
|
15023 | 715 |
fun ss setSSolver solver = ss |> map_simpset2 (fn (congs, procs, mk_rews, termless, |
716 |
subgoal_tac, loop_tacs, (unsafe_solvers, _)) => |
|
717 |
(congs, procs, mk_rews, termless, subgoal_tac, loop_tacs, (unsafe_solvers, [solver]))); |
|
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
718 |
|
15023 | 719 |
fun ss addSSolver solver = ss |> map_simpset2 (fn (congs, procs, mk_rews, termless, |
720 |
subgoal_tac, loop_tacs, (unsafe_solvers, solvers)) => (congs, procs, mk_rews, termless, |
|
721 |
subgoal_tac, loop_tacs, (unsafe_solvers, merge_solvers solvers [solver]))); |
|
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
722 |
|
15023 | 723 |
fun ss setSolver solver = ss |> map_simpset2 (fn (congs, procs, mk_rews, termless, |
724 |
subgoal_tac, loop_tacs, (_, solvers)) => (congs, procs, mk_rews, termless, |
|
725 |
subgoal_tac, loop_tacs, ([solver], solvers))); |
|
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
726 |
|
15023 | 727 |
fun ss addSolver solver = ss |> map_simpset2 (fn (congs, procs, mk_rews, termless, |
728 |
subgoal_tac, loop_tacs, (unsafe_solvers, solvers)) => (congs, procs, mk_rews, termless, |
|
729 |
subgoal_tac, loop_tacs, (merge_solvers unsafe_solvers [solver], solvers))); |
|
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
730 |
|
15023 | 731 |
fun set_solvers solvers = map_simpset2 (fn (congs, procs, mk_rews, termless, |
732 |
subgoal_tac, loop_tacs, _) => (congs, procs, mk_rews, termless, |
|
733 |
subgoal_tac, loop_tacs, (solvers, solvers))); |
|
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
734 |
|
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
735 |
|
18208 | 736 |
(* empty *) |
737 |
||
738 |
fun init_ss mk_rews termless subgoal_tac solvers = |
|
739 |
make_simpset ((Net.empty, [], (0, []), NONE), |
|
740 |
(([], []), Net.empty, mk_rews, termless, subgoal_tac, [], solvers)); |
|
741 |
||
742 |
fun clear_ss (ss as Simpset (_, {mk_rews, termless, subgoal_tac, solvers, ...})) = |
|
743 |
init_ss mk_rews termless subgoal_tac solvers |
|
744 |
|> inherit_context ss; |
|
745 |
||
746 |
val basic_mk_rews: mk_rews = |
|
747 |
{mk = fn th => if can Logic.dest_equals (Thm.concl_of th) then [th] else [], |
|
748 |
mk_cong = I, |
|
749 |
mk_sym = SOME o Drule.symmetric_fun, |
|
750 |
mk_eq_True = K NONE, |
|
751 |
reorient = default_reorient}; |
|
752 |
||
753 |
val empty_ss = init_ss basic_mk_rews Term.termless (K (K no_tac)) ([], []); |
|
754 |
||
755 |
||
756 |
(* merge *) (*NOTE: ignores some fields of 2nd simpset*) |
|
757 |
||
758 |
fun merge_ss (ss1, ss2) = |
|
759 |
let |
|
760 |
val Simpset ({rules = rules1, prems = prems1, bounds = bounds1, context = _}, |
|
761 |
{congs = (congs1, weak1), procs = procs1, mk_rews, termless, subgoal_tac, |
|
762 |
loop_tacs = loop_tacs1, solvers = (unsafe_solvers1, solvers1)}) = ss1; |
|
763 |
val Simpset ({rules = rules2, prems = prems2, bounds = bounds2, context = _}, |
|
764 |
{congs = (congs2, weak2), procs = procs2, mk_rews = _, termless = _, subgoal_tac = _, |
|
765 |
loop_tacs = loop_tacs2, solvers = (unsafe_solvers2, solvers2)}) = ss2; |
|
766 |
||
767 |
val rules' = Net.merge eq_rrule (rules1, rules2); |
|
768 |
val prems' = gen_merge_lists Drule.eq_thm_prop prems1 prems2; |
|
769 |
val bounds' = if #1 bounds1 < #1 bounds2 then bounds2 else bounds1; |
|
770 |
val congs' = gen_merge_lists (eq_cong o pairself #2) congs1 congs2; |
|
771 |
val weak' = merge_lists weak1 weak2; |
|
772 |
val procs' = Net.merge eq_proc (procs1, procs2); |
|
21286
b5e7b80caa6a
introduces canonical AList functions for loop_tacs
haftmann
parents:
20972
diff
changeset
|
773 |
val loop_tacs' = AList.merge (op =) (K true) (loop_tacs1, loop_tacs2); |
18208 | 774 |
val unsafe_solvers' = merge_solvers unsafe_solvers1 unsafe_solvers2; |
775 |
val solvers' = merge_solvers solvers1 solvers2; |
|
776 |
in |
|
777 |
make_simpset ((rules', prems', bounds', NONE), ((congs', weak'), procs', |
|
778 |
mk_rews, termless, subgoal_tac, loop_tacs', (unsafe_solvers', solvers'))) |
|
779 |
end; |
|
780 |
||
781 |
||
15006
107e4dfd3b96
Merging the meta-simplifier with the Provers-simplifier. Next step:
skalberg
parents:
15001
diff
changeset
|
782 |
|
10413 | 783 |
(** rewriting **) |
784 |
||
785 |
(* |
|
786 |
Uses conversions, see: |
|
787 |
L C Paulson, A higher-order implementation of rewriting, |
|
788 |
Science of Computer Programming 3 (1983), pages 119-149. |
|
789 |
*) |
|
790 |
||
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
791 |
fun check_conv msg ss thm thm' = |
10413 | 792 |
let |
793 |
val thm'' = transitive thm (transitive |
|
20905 | 794 |
(symmetric (Drule.beta_eta_conversion (Drule.lhs_of thm'))) thm') |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
795 |
in if msg then trace_thm "SUCCEEDED" ss thm' else (); SOME thm'' end |
10413 | 796 |
handle THM _ => |
16458 | 797 |
let val {thy, prop = _ $ _ $ prop0, ...} = Thm.rep_thm thm in |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
798 |
trace_thm "Proved wrong thm (Check subgoaler?)" ss thm'; |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
799 |
trace_term false "Should have proved:" ss thy prop0; |
15531 | 800 |
NONE |
10413 | 801 |
end; |
802 |
||
803 |
||
804 |
(* mk_procrule *) |
|
805 |
||
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
806 |
fun mk_procrule ss thm = |
15023 | 807 |
let val (_, prems, lhs, elhs, rhs, _) = decomp_simp thm in |
808 |
if rewrite_rule_extra_vars prems lhs rhs |
|
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
809 |
then (warn_thm "Extra vars on rhs:" ss thm; []) |
15023 | 810 |
else [mk_rrule2 {thm = thm, name = "", lhs = lhs, elhs = elhs, perm = false}] |
10413 | 811 |
end; |
812 |
||
813 |
||
15023 | 814 |
(* rewritec: conversion to apply the meta simpset to a term *) |
10413 | 815 |
|
15023 | 816 |
(*Since the rewriting strategy is bottom-up, we avoid re-normalizing already |
817 |
normalized terms by carrying around the rhs of the rewrite rule just |
|
818 |
applied. This is called the `skeleton'. It is decomposed in parallel |
|
819 |
with the term. Once a Var is encountered, the corresponding term is |
|
820 |
already in normal form. |
|
821 |
skel0 is a dummy skeleton that is to enforce complete normalization.*) |
|
822 |
||
10413 | 823 |
val skel0 = Bound 0; |
824 |
||
15023 | 825 |
(*Use rhs as skeleton only if the lhs does not contain unnormalized bits. |
826 |
The latter may happen iff there are weak congruence rules for constants |
|
827 |
in the lhs.*) |
|
10413 | 828 |
|
15023 | 829 |
fun uncond_skel ((_, weak), (lhs, rhs)) = |
830 |
if null weak then rhs (*optimization*) |
|
20671 | 831 |
else if exists_Const (member (op =) weak o #1) lhs then skel0 |
15023 | 832 |
else rhs; |
833 |
||
834 |
(*Behaves like unconditional rule if rhs does not contain vars not in the lhs. |
|
835 |
Otherwise those vars may become instantiated with unnormalized terms |
|
836 |
while the premises are solved.*) |
|
837 |
||
838 |
fun cond_skel (args as (congs, (lhs, rhs))) = |
|
20197
ffc64d90fc1f
use Term.add_vars instead of obsolete term_varnames;
wenzelm
parents:
20146
diff
changeset
|
839 |
if Term.add_vars rhs [] subset Term.add_vars lhs [] then uncond_skel args |
10413 | 840 |
else skel0; |
841 |
||
842 |
(* |
|
15023 | 843 |
Rewriting -- we try in order: |
10413 | 844 |
(1) beta reduction |
845 |
(2) unconditional rewrite rules |
|
846 |
(3) conditional rewrite rules |
|
847 |
(4) simplification procedures |
|
848 |
||
849 |
IMPORTANT: rewrite rules must not introduce new Vars or TVars! |
|
850 |
*) |
|
851 |
||
16458 | 852 |
fun rewritec (prover, thyt, maxt) ss t = |
10413 | 853 |
let |
15023 | 854 |
val Simpset ({rules, ...}, {congs, procs, termless, ...}) = ss; |
10413 | 855 |
val eta_thm = Thm.eta_conversion t; |
20905 | 856 |
val eta_t' = Drule.rhs_of eta_thm; |
10413 | 857 |
val eta_t = term_of eta_t'; |
20546
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
858 |
fun rew {thm, name, lhs, elhs, extra, fo, perm} = |
10413 | 859 |
let |
16458 | 860 |
val {thy, prop, maxidx, ...} = rep_thm thm; |
20546
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
861 |
val (rthm, elhs') = |
8923deb735ad
rrule: maintain 'extra' field for rule that contain extra vars outside elhs;
wenzelm
parents:
20330
diff
changeset
|
862 |
if maxt = ~1 orelse not extra then (thm, elhs) |
10413 | 863 |
else (Thm.incr_indexes (maxt+1) thm, Thm.cterm_incr_indexes (maxt+1) elhs); |
864 |
val insts = if fo then Thm.cterm_first_order_match (elhs', eta_t') |
|
865 |
else Thm.cterm_match (elhs', eta_t'); |
|
866 |
val thm' = Thm.instantiate insts (Thm.rename_boundvars lhs eta_t rthm); |
|
14643 | 867 |
val prop' = Thm.prop_of thm'; |
21576 | 868 |
val unconditional = (Logic.count_prems prop' = 0); |
10413 | 869 |
val (lhs', rhs') = Logic.dest_equals (Logic.strip_imp_concl prop') |
870 |
in |
|
11295 | 871 |
if perm andalso not (termless (rhs', lhs')) |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
872 |
then (trace_named_thm "Cannot apply permutative rewrite rule" ss (thm, name); |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
873 |
trace_thm "Term does not become smaller:" ss thm'; NONE) |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
874 |
else (trace_named_thm "Applying instance of rewrite rule" ss (thm, name); |
10413 | 875 |
if unconditional |
876 |
then |
|
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
877 |
(trace_thm "Rewriting:" ss thm'; |
10413 | 878 |
let val lr = Logic.dest_equals prop; |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
879 |
val SOME thm'' = check_conv false ss eta_thm thm' |
15531 | 880 |
in SOME (thm'', uncond_skel (congs, lr)) end) |
10413 | 881 |
else |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
882 |
(trace_thm "Trying to rewrite:" ss thm'; |
16042 | 883 |
if !simp_depth > !simp_depth_limit |
884 |
then let val s = "simp_depth_limit exceeded - giving up" |
|
885 |
in trace false s; warning s; NONE end |
|
886 |
else |
|
887 |
case prover ss thm' of |
|
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
888 |
NONE => (trace_thm "FAILED" ss thm'; NONE) |
15531 | 889 |
| SOME thm2 => |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
890 |
(case check_conv true ss eta_thm thm2 of |
15531 | 891 |
NONE => NONE | |
892 |
SOME thm2' => |
|
10413 | 893 |
let val concl = Logic.strip_imp_concl prop |
894 |
val lr = Logic.dest_equals concl |
|
16042 | 895 |
in SOME (thm2', cond_skel (congs, lr)) end))) |
10413 | 896 |
end |
897 |
||
15531 | 898 |
fun rews [] = NONE |
10413 | 899 |
| rews (rrule :: rrules) = |
15531 | 900 |
let val opt = rew rrule handle Pattern.MATCH => NONE |
901 |
in case opt of NONE => rews rrules | some => some end; |
|
10413 | 902 |
|
903 |
fun sort_rrules rrs = let |
|
14643 | 904 |
fun is_simple({thm, ...}:rrule) = case Thm.prop_of thm of |
10413 | 905 |
Const("==",_) $ _ $ _ => true |
12603 | 906 |
| _ => false |
10413 | 907 |
fun sort [] (re1,re2) = re1 @ re2 |
12603 | 908 |
| sort (rr::rrs) (re1,re2) = if is_simple rr |
10413 | 909 |
then sort rrs (rr::re1,re2) |
910 |
else sort rrs (re1,rr::re2) |
|
911 |
in sort rrs ([],[]) end |
|
912 |
||
15531 | 913 |
fun proc_rews [] = NONE |
15023 | 914 |
| proc_rews (Proc {name, proc, lhs, ...} :: ps) = |
17203 | 915 |
if Pattern.matches thyt (Thm.term_of lhs, Thm.term_of t) then |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
916 |
(debug_term false ("Trying procedure " ^ quote name ^ " on:") ss thyt eta_t; |
13486
54464ea94d6f
exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents:
13458
diff
changeset
|
917 |
case transform_failure (curry SIMPROC_FAIL name) |
16458 | 918 |
(fn () => proc thyt ss eta_t) () of |
15531 | 919 |
NONE => (debug false "FAILED"; proc_rews ps) |
920 |
| SOME raw_thm => |
|
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
921 |
(trace_thm ("Procedure " ^ quote name ^ " produced rewrite rule:") ss raw_thm; |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
922 |
(case rews (mk_procrule ss raw_thm) of |
15531 | 923 |
NONE => (trace_cterm true ("IGNORED result of simproc " ^ quote name ^ |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
924 |
" -- does not match") ss t; proc_rews ps) |
10413 | 925 |
| some => some))) |
926 |
else proc_rews ps; |
|
927 |
in case eta_t of |
|
15531 | 928 |
Abs _ $ _ => SOME (transitive eta_thm |
12155
13c5469b4bb3
congc now returns None if congruence rule has no effect.
berghofe
parents:
11886
diff
changeset
|
929 |
(beta_conversion false eta_t'), skel0) |
10413 | 930 |
| _ => (case rews (sort_rrules (Net.match_term rules eta_t)) of |
15531 | 931 |
NONE => proc_rews (Net.match_term procs eta_t) |
10413 | 932 |
| some => some) |
933 |
end; |
|
934 |
||
935 |
||
936 |
(* conversion to apply a congruence rule to a term *) |
|
937 |
||
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
938 |
fun congc prover ss maxt {thm=cong,lhs=lhs} t = |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
939 |
let val rthm = Thm.incr_indexes (maxt+1) cong; |
10413 | 940 |
val rlhs = fst (Drule.dest_equals (Drule.strip_imp_concl (cprop_of rthm))); |
941 |
val insts = Thm.cterm_match (rlhs, t) |
|
942 |
(* Pattern.match can raise Pattern.MATCH; |
|
943 |
is handled when congc is called *) |
|
944 |
val thm' = Thm.instantiate insts (Thm.rename_boundvars (term_of rlhs) (term_of t) rthm); |
|
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
945 |
val unit = trace_thm "Applying congruence rule:" ss thm'; |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
946 |
fun err (msg, thm) = (trace_thm msg ss thm; NONE) |
10413 | 947 |
in case prover thm' of |
15531 | 948 |
NONE => err ("Congruence proof failed. Could not prove", thm') |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
949 |
| SOME thm2 => (case check_conv true ss (Drule.beta_eta_conversion t) thm2 of |
15531 | 950 |
NONE => err ("Congruence proof failed. Should not have proved", thm2) |
951 |
| SOME thm2' => |
|
12155
13c5469b4bb3
congc now returns None if congruence rule has no effect.
berghofe
parents:
11886
diff
changeset
|
952 |
if op aconv (pairself term_of (dest_equals (cprop_of thm2'))) |
15531 | 953 |
then NONE else SOME thm2') |
10413 | 954 |
end; |
955 |
||
956 |
val (cA, (cB, cC)) = |
|
957 |
apsnd dest_equals (dest_implies (hd (cprems_of Drule.imp_cong))); |
|
958 |
||
15531 | 959 |
fun transitive1 NONE NONE = NONE |
960 |
| transitive1 (SOME thm1) NONE = SOME thm1 |
|
961 |
| transitive1 NONE (SOME thm2) = SOME thm2 |
|
962 |
| transitive1 (SOME thm1) (SOME thm2) = SOME (transitive thm1 thm2) |
|
10413 | 963 |
|
15531 | 964 |
fun transitive2 thm = transitive1 (SOME thm); |
965 |
fun transitive3 thm = transitive1 thm o SOME; |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
966 |
|
16458 | 967 |
fun bottomc ((simprem, useprem, mutsimp), prover, thy, maxidx) = |
10413 | 968 |
let |
15023 | 969 |
fun botc skel ss t = |
15531 | 970 |
if is_Var skel then NONE |
10413 | 971 |
else |
15023 | 972 |
(case subc skel ss t of |
15531 | 973 |
some as SOME thm1 => |
20905 | 974 |
(case rewritec (prover, thy, maxidx) ss (Drule.rhs_of thm1) of |
15531 | 975 |
SOME (thm2, skel2) => |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
976 |
transitive2 (transitive thm1 thm2) |
20905 | 977 |
(botc skel2 ss (Drule.rhs_of thm2)) |
15531 | 978 |
| NONE => some) |
979 |
| NONE => |
|
16458 | 980 |
(case rewritec (prover, thy, maxidx) ss t of |
15531 | 981 |
SOME (thm2, skel2) => transitive2 thm2 |
20905 | 982 |
(botc skel2 ss (Drule.rhs_of thm2)) |
15531 | 983 |
| NONE => NONE)) |
10413 | 984 |
|
15023 | 985 |
and try_botc ss t = |
986 |
(case botc skel0 ss t of |
|
15531 | 987 |
SOME trec1 => trec1 | NONE => (reflexive t)) |
10413 | 988 |
|
15023 | 989 |
and subc skel (ss as Simpset ({bounds, ...}, {congs, ...})) t0 = |
10413 | 990 |
(case term_of t0 of |
991 |
Abs (a, T, t) => |
|
15023 | 992 |
let |
20079
ec5c8584487c
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
20057
diff
changeset
|
993 |
val b = Name.bound (#1 bounds); |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
994 |
val (v, t') = Thm.dest_abs (SOME b) t0; |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
995 |
val b' = #1 (Term.dest_Free (Thm.term_of v)); |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
996 |
val _ = conditional (b <> b') (fn () => |
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
997 |
warning ("Simplifier: renamed bound variable " ^ quote b ^ " to " ^ quote b')); |
17614 | 998 |
val ss' = add_bound ((b', T), a) ss; |
15023 | 999 |
val skel' = case skel of Abs (_, _, sk) => sk | _ => skel0; |
1000 |
in case botc skel' ss' t' of |
|
15531 | 1001 |
SOME thm => SOME (abstract_rule a v thm) |
1002 |
| NONE => NONE |
|
10413 | 1003 |
end |
1004 |
| t $ _ => (case t of |
|
15023 | 1005 |
Const ("==>", _) $ _ => impc t0 ss |
10413 | 1006 |
| Abs _ => |
1007 |
let val thm = beta_conversion false t0 |
|
20905 | 1008 |
in case subc skel0 ss (Drule.rhs_of thm) of |
15531 | 1009 |
NONE => SOME thm |
1010 |
| SOME thm' => SOME (transitive thm thm') |
|
10413 | 1011 |
end |
1012 |
| _ => |
|
1013 |
let fun appc () = |
|
1014 |
let |
|
1015 |
val (tskel, uskel) = case skel of |
|
1016 |
tskel $ uskel => (tskel, uskel) |
|
1017 |
| _ => (skel0, skel0); |
|
10767
8fa4aafa7314
Thm: dest_comb, dest_abs, capply, cabs no longer global;
wenzelm
parents:
10413
diff
changeset
|
1018 |
val (ct, cu) = Thm.dest_comb t0 |
10413 | 1019 |
in |
15023 | 1020 |
(case botc tskel ss ct of |
15531 | 1021 |
SOME thm1 => |
15023 | 1022 |
(case botc uskel ss cu of |
15531 | 1023 |
SOME thm2 => SOME (combination thm1 thm2) |
1024 |
| NONE => SOME (combination thm1 (reflexive cu))) |
|
1025 |
| NONE => |
|
15023 | 1026 |
(case botc uskel ss cu of |
15531 | 1027 |
SOME thm1 => SOME (combination (reflexive ct) thm1) |
1028 |
| NONE => NONE)) |
|
10413 | 1029 |
end |
1030 |
val (h, ts) = strip_comb t |
|
13835
12b2ffbe543a
Change to meta simplifier: congruence rules may now have frees as head of term.
ballarin
parents:
13828
diff
changeset
|
1031 |
in case cong_name h of |
15531 | 1032 |
SOME a => |
17232 | 1033 |
(case AList.lookup (op =) (fst congs) a of |
15531 | 1034 |
NONE => appc () |
1035 |
| SOME cong => |
|
15023 | 1036 |
(*post processing: some partial applications h t1 ... tj, j <= length ts, |
1037 |
may be a redex. Example: map (%x. x) = (%xs. xs) wrt map_cong*) |
|
10413 | 1038 |
(let |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
1039 |
val thm = congc (prover ss) ss maxidx cong t0; |
20905 | 1040 |
val t = the_default t0 (Option.map Drule.rhs_of thm); |
10767
8fa4aafa7314
Thm: dest_comb, dest_abs, capply, cabs no longer global;
wenzelm
parents:
10413
diff
changeset
|
1041 |
val (cl, cr) = Thm.dest_comb t |
10413 | 1042 |
val dVar = Var(("", 0), dummyT) |
1043 |
val skel = |
|
1044 |
list_comb (h, replicate (length ts) dVar) |
|
15023 | 1045 |
in case botc skel ss cl of |
15531 | 1046 |
NONE => thm |
1047 |
| SOME thm' => transitive3 thm |
|
12155
13c5469b4bb3
congc now returns None if congruence rule has no effect.
berghofe
parents:
11886
diff
changeset
|
1048 |
(combination thm' (reflexive cr)) |
20057 | 1049 |
end handle Pattern.MATCH => appc ())) |
10413 | 1050 |
| _ => appc () |
1051 |
end) |
|
15531 | 1052 |
| _ => NONE) |
10413 | 1053 |
|
15023 | 1054 |
and impc ct ss = |
1055 |
if mutsimp then mut_impc0 [] ct [] [] ss else nonmut_impc ct ss |
|
10413 | 1056 |
|
15023 | 1057 |
and rules_of_prem ss prem = |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1058 |
if maxidx_of_term (term_of prem) <> ~1 |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1059 |
then (trace_cterm true |
16985
7df8abe926c3
improved bounds: nameless Term.bound, recover names for output;
wenzelm
parents:
16938
diff
changeset
|
1060 |
"Cannot add premise as rewrite rule because it contains (type) unknowns:" ss prem; ([], NONE)) |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1061 |
else |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1062 |
let val asm = assume prem |
15531 | 1063 |
in (extract_safe_rrules (ss, asm), SOME asm) end |
10413 | 1064 |
|
15023 | 1065 |
and add_rrules (rrss, asms) ss = |
20028
b9752164ad92
add/del_simps: warning for inactive simpset (no context);
wenzelm
parents:
19798
diff
changeset
|
1066 |
(fold o fold) insert_rrule rrss ss |> add_prems (map_filter I asms) |
10413 | 1067 |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1068 |
and disch r (prem, eq) = |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1069 |
let |
20905 | 1070 |
val (lhs, rhs) = Drule.dest_equals (Thm.cprop_of eq); |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1071 |
val eq' = implies_elim (Thm.instantiate |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1072 |
([], [(cA, prem), (cB, lhs), (cC, rhs)]) Drule.imp_cong) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1073 |
(implies_intr prem eq) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1074 |
in if not r then eq' else |
10413 | 1075 |
let |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1076 |
val (prem', concl) = dest_implies lhs; |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1077 |
val (prem'', _) = dest_implies rhs |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1078 |
in transitive (transitive |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1079 |
(Thm.instantiate ([], [(cA, prem'), (cB, prem), (cC, concl)]) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1080 |
Drule.swap_prems_eq) eq') |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1081 |
(Thm.instantiate ([], [(cA, prem), (cB, prem''), (cC, concl)]) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1082 |
Drule.swap_prems_eq) |
10413 | 1083 |
end |
1084 |
end |
|
1085 |
||
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1086 |
and rebuild [] _ _ _ _ eq = eq |
15023 | 1087 |
| rebuild (prem :: prems) concl (rrs :: rrss) (asm :: asms) ss eq = |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1088 |
let |
15023 | 1089 |
val ss' = add_rrules (rev rrss, rev asms) ss; |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1090 |
val concl' = |
20905 | 1091 |
Drule.mk_implies (prem, the_default concl (Option.map Drule.rhs_of eq)); |
15570 | 1092 |
val dprem = Option.map (curry (disch false) prem) |
16458 | 1093 |
in case rewritec (prover, thy, maxidx) ss' concl' of |
15531 | 1094 |
NONE => rebuild prems concl' rrss asms ss (dprem eq) |
15570 | 1095 |
| SOME (eq', _) => transitive2 (Library.foldl (disch false o swap) |
19502 | 1096 |
(the (transitive3 (dprem eq) eq'), prems)) |
20905 | 1097 |
(mut_impc0 (rev prems) (Drule.rhs_of eq') (rev rrss) (rev asms) ss) |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1098 |
end |
15023 | 1099 |
|
1100 |
and mut_impc0 prems concl rrss asms ss = |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1101 |
let |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1102 |
val prems' = strip_imp_prems concl; |
15023 | 1103 |
val (rrss', asms') = split_list (map (rules_of_prem ss) prems') |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1104 |
in mut_impc (prems @ prems') (strip_imp_concl concl) (rrss @ rrss') |
15023 | 1105 |
(asms @ asms') [] [] [] [] ss ~1 ~1 |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1106 |
end |
15023 | 1107 |
|
1108 |
and mut_impc [] concl [] [] prems' rrss' asms' eqns ss changed k = |
|
15570 | 1109 |
transitive1 (Library.foldl (fn (eq2, (eq1, prem)) => transitive1 eq1 |
1110 |
(Option.map (curry (disch false) prem) eq2)) (NONE, eqns ~~ prems')) |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1111 |
(if changed > 0 then |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1112 |
mut_impc (rev prems') concl (rev rrss') (rev asms') |
15023 | 1113 |
[] [] [] [] ss ~1 changed |
1114 |
else rebuild prems' concl rrss' asms' ss |
|
1115 |
(botc skel0 (add_rrules (rev rrss', rev asms') ss) concl)) |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1116 |
|
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1117 |
| mut_impc (prem :: prems) concl (rrs :: rrss) (asm :: asms) |
15023 | 1118 |
prems' rrss' asms' eqns ss changed k = |
15531 | 1119 |
case (if k = 0 then NONE else botc skel0 (add_rrules |
15023 | 1120 |
(rev rrss' @ rrss, rev asms' @ asms) ss) prem) of |
15531 | 1121 |
NONE => mut_impc prems concl rrss asms (prem :: prems') |
1122 |
(rrs :: rrss') (asm :: asms') (NONE :: eqns) ss changed |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1123 |
(if k = 0 then 0 else k - 1) |
15531 | 1124 |
| SOME eqn => |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1125 |
let |
20905 | 1126 |
val prem' = Drule.rhs_of eqn; |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1127 |
val tprems = map term_of prems; |
15570 | 1128 |
val i = 1 + Library.foldl Int.max (~1, map (fn p => |
19618 | 1129 |
find_index (fn q => q aconv p) tprems) (#hyps (rep_thm eqn))); |
15023 | 1130 |
val (rrs', asm') = rules_of_prem ss prem' |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1131 |
in mut_impc prems concl rrss asms (prem' :: prems') |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
1132 |
(rrs' :: rrss') (asm' :: asms') (SOME (foldr (disch true) |
18470 | 1133 |
(Drule.imp_cong_rule eqn (reflexive (Drule.list_implies |
20671 | 1134 |
(Library.drop (i, prems), concl)))) (Library.take (i, prems))) :: eqns) |
1135 |
ss (length prems') ~1 |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1136 |
end |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1137 |
|
15023 | 1138 |
(*legacy code - only for backwards compatibility*) |
1139 |
and nonmut_impc ct ss = |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1140 |
let val (prem, conc) = dest_implies ct; |
15531 | 1141 |
val thm1 = if simprem then botc skel0 ss prem else NONE; |
20905 | 1142 |
val prem1 = the_default prem (Option.map Drule.rhs_of thm1); |
15023 | 1143 |
val ss1 = if not useprem then ss else add_rrules |
1144 |
(apsnd single (apfst single (rules_of_prem ss prem1))) ss |
|
1145 |
in (case botc skel0 ss1 conc of |
|
15531 | 1146 |
NONE => (case thm1 of |
1147 |
NONE => NONE |
|
18470 | 1148 |
| SOME thm1' => SOME (Drule.imp_cong_rule thm1' (reflexive conc))) |
15531 | 1149 |
| SOME thm2 => |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
1150 |
let val thm2' = disch false (prem1, thm2) |
10413 | 1151 |
in (case thm1 of |
15531 | 1152 |
NONE => SOME thm2' |
1153 |
| SOME thm1' => |
|
18470 | 1154 |
SOME (transitive (Drule.imp_cong_rule thm1' (reflexive conc)) thm2')) |
10413 | 1155 |
end) |
1156 |
end |
|
1157 |
||
15023 | 1158 |
in try_botc end; |
10413 | 1159 |
|
1160 |
||
15023 | 1161 |
(* Meta-rewriting: rewrites t to u and returns the theorem t==u *) |
10413 | 1162 |
|
1163 |
(* |
|
1164 |
Parameters: |
|
1165 |
mode = (simplify A, |
|
1166 |
use A in simplifying B, |
|
1167 |
use prems of B (if B is again a meta-impl.) to simplify A) |
|
1168 |
when simplifying A ==> B |
|
1169 |
prover: how to solve premises in conditional rewrites and congruences |
|
1170 |
*) |
|
1171 |
||
17705 | 1172 |
val debug_bounds = ref false; |
1173 |
||
1174 |
fun check_bounds ss ct = conditional (! debug_bounds) (fn () => |
|
17614 | 1175 |
let |
1176 |
val Simpset ({bounds = (_, bounds), ...}, _) = ss; |
|
1177 |
val bs = fold_aterms (fn Free (x, _) => |
|
20079
ec5c8584487c
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
20057
diff
changeset
|
1178 |
if Name.is_bound x andalso not (AList.defined eq_bound bounds x) |
17614 | 1179 |
then insert (op =) x else I |
1180 |
| _ => I) (term_of ct) []; |
|
17705 | 1181 |
in |
1182 |
if null bs then () |
|
17723 | 1183 |
else print_term true ("Simplifier: term contains loose bounds: " ^ commas_quote bs) ss |
17705 | 1184 |
(Thm.theory_of_cterm ct) (Thm.term_of ct) |
1185 |
end); |
|
17614 | 1186 |
|
19052
113dbd65319e
rewrite_cterm: Thm.adjust_maxidx prevents unnecessary increments on rules;
wenzelm
parents:
18929
diff
changeset
|
1187 |
fun rewrite_cterm mode prover raw_ss raw_ct = |
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
1188 |
let |
20260 | 1189 |
val ct = Thm.adjust_maxidx_cterm ~1 raw_ct; |
17882
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
1190 |
val {thy, t, maxidx, ...} = Thm.rep_cterm ct; |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
1191 |
val ss = fallback_context thy raw_ss; |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
1192 |
val _ = inc simp_depth; |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
1193 |
val _ = conditional (!simp_depth mod 20 = 0) (fn () => |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
1194 |
warning ("Simplification depth " ^ string_of_int (! simp_depth))); |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
1195 |
val _ = trace_cterm false "SIMPLIFIER INVOKED ON THE FOLLOWING TERM:" ss ct; |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
1196 |
val _ = check_bounds ss ct; |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
1197 |
val res = bottomc (mode, Option.map Drule.flexflex_unique oo prover, thy, maxidx) ss ct |
b6e44fc46cf0
added set/addloop' for simpset dependent loopers;
wenzelm
parents:
17756
diff
changeset
|
1198 |
in dec simp_depth; res end |
20057 | 1199 |
handle exn => (dec simp_depth; raise exn); (* FIXME avoid handling of generic exceptions *) |
10413 | 1200 |
|
21708 | 1201 |
val simple_prover = |
1202 |
SINGLE o (fn ss => ALLGOALS (resolve_tac (prems_of_ss ss))); |
|
1203 |
||
11760 | 1204 |
(*Rewrite a cterm*) |
21708 | 1205 |
fun rewrite _ [] ct = Thm.reflexive ct |
1206 |
| rewrite full thms ct = |
|
1207 |
rewrite_cterm (full, false, false) simple_prover |
|
1208 |
(theory_context (Thm.theory_of_cterm ct) empty_ss addsimps thms) ct; |
|
11672 | 1209 |
|
10413 | 1210 |
(*Rewrite a theorem*) |
21708 | 1211 |
fun simplify _ [] th = th |
1212 |
| simplify full thms th = |
|
1213 |
Drule.fconv_rule (rewrite_cterm (full, false, false) simple_prover |
|
17897 | 1214 |
(theory_context (Thm.theory_of_thm th) empty_ss addsimps thms)) th; |
10413 | 1215 |
|
21708 | 1216 |
val rewrite_rule = simplify true; |
1217 |
||
15023 | 1218 |
(*simple term rewriting -- no proof*) |
16458 | 1219 |
fun rewrite_term thy rules procs = |
17203 | 1220 |
Pattern.rewrite_term thy (map decomp_simp' rules) procs; |
15023 | 1221 |
|
1222 |
fun rewrite_thm mode prover ss = Drule.fconv_rule (rewrite_cterm mode prover ss); |
|
10413 | 1223 |
|
1224 |
(*Rewrite the subgoals of a proof state (represented by a theorem) *) |
|
21708 | 1225 |
fun rewrite_goals_rule thms th = |
1226 |
Drule.fconv_rule (Drule.goals_conv (K true) (rewrite_cterm (true, true, true) simple_prover |
|
19142
99a72b8c9974
rewrite_goals_rule_aux: actually use prems if present;
wenzelm
parents:
19052
diff
changeset
|
1227 |
(theory_context (Thm.theory_of_thm th) empty_ss addsimps thms))) th; |
10413 | 1228 |
|
15023 | 1229 |
(*Rewrite the subgoal of a proof state (represented by a theorem)*) |
15011 | 1230 |
fun rewrite_goal_rule mode prover ss i thm = |
10413 | 1231 |
if 0 < i andalso i <= nprems_of thm |
15011 | 1232 |
then Drule.fconv_rule (Drule.goals_conv (fn j => j=i) (rewrite_cterm mode prover ss)) thm |
10413 | 1233 |
else raise THM("rewrite_goal_rule",i,[thm]); |
1234 |
||
20228
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
wenzelm
parents:
20197
diff
changeset
|
1235 |
|
21708 | 1236 |
(** meta-rewriting tactics **) |
1237 |
||
1238 |
(*Rewrite throughout proof state. *) |
|
1239 |
fun rewrite_tac defs = PRIMITIVE(rewrite_rule defs); |
|
1240 |
||
1241 |
(*Rewrite subgoals only, not main goal. *) |
|
1242 |
fun rewrite_goals_tac defs = PRIMITIVE (rewrite_goals_rule defs); |
|
1243 |
fun rewtac def = rewrite_goals_tac [def]; |
|
1244 |
||
1245 |
(*Prunes all redundant parameters from the proof state by rewriting. |
|
1246 |
DOES NOT rewrite main goal, where quantification over an unused bound |
|
1247 |
variable is sometimes done to avoid the need for cut_facts_tac.*) |
|
1248 |
val prune_params_tac = rewrite_goals_tac [triv_forall_equality]; |
|
1249 |
||
1250 |
||
1251 |
(* for folding definitions, handling critical pairs *) |
|
1252 |
||
1253 |
(*The depth of nesting in a term*) |
|
1254 |
fun term_depth (Abs(a,T,t)) = 1 + term_depth t |
|
1255 |
| term_depth (f$t) = 1 + Int.max(term_depth f, term_depth t) |
|
1256 |
| term_depth _ = 0; |
|
1257 |
||
1258 |
val lhs_of_thm = #1 o Logic.dest_equals o prop_of; |
|
1259 |
||
1260 |
(*folding should handle critical pairs! E.g. K == Inl(0), S == Inr(Inl(0)) |
|
1261 |
Returns longest lhs first to avoid folding its subexpressions.*) |
|
1262 |
fun sort_lhs_depths defs = |
|
1263 |
let val keylist = AList.make (term_depth o lhs_of_thm) defs |
|
1264 |
val keys = sort_distinct (rev_order o int_ord) (map #2 keylist) |
|
1265 |
in map (AList.find (op =) keylist) keys end; |
|
1266 |
||
1267 |
val rev_defs = sort_lhs_depths o map symmetric; |
|
1268 |
||
1269 |
fun fold_rule defs = fold rewrite_rule (rev_defs defs); |
|
1270 |
fun fold_tac defs = EVERY (map rewrite_tac (rev_defs defs)); |
|
1271 |
fun fold_goals_tac defs = EVERY (map rewrite_goals_tac (rev_defs defs)); |
|
1272 |
||
1273 |
||
20228
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
wenzelm
parents:
20197
diff
changeset
|
1274 |
(* HHF normal form: !! before ==>, outermost !! generalized *) |
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
wenzelm
parents:
20197
diff
changeset
|
1275 |
|
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
wenzelm
parents:
20197
diff
changeset
|
1276 |
local |
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
wenzelm
parents:
20197
diff
changeset
|
1277 |
|
21565 | 1278 |
fun gen_norm_hhf ss th = |
1279 |
(if Drule.is_norm_hhf (Thm.prop_of th) then th |
|
1280 |
else Drule.fconv_rule (rewrite_cterm (true, false, false) (K (K NONE)) ss) th) |
|
1281 |
|> Thm.adjust_maxidx_thm ~1 |
|
1282 |
|> Drule.gen_all; |
|
20228
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
wenzelm
parents:
20197
diff
changeset
|
1283 |
|
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
wenzelm
parents:
20197
diff
changeset
|
1284 |
val ss = theory_context ProtoPure.thy empty_ss addsimps [Drule.norm_hhf_eq]; |
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
wenzelm
parents:
20197
diff
changeset
|
1285 |
|
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
wenzelm
parents:
20197
diff
changeset
|
1286 |
in |
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
wenzelm
parents:
20197
diff
changeset
|
1287 |
|
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
wenzelm
parents:
20197
diff
changeset
|
1288 |
val norm_hhf = gen_norm_hhf ss; |
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
wenzelm
parents:
20197
diff
changeset
|
1289 |
val norm_hhf_protect = gen_norm_hhf (ss addeqcongs [Drule.protect_cong]); |
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
wenzelm
parents:
20197
diff
changeset
|
1290 |
|
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
wenzelm
parents:
20197
diff
changeset
|
1291 |
end; |
e0f9e8a6556b
moved Goal.norm_hhf(_protect) to meta_simplifier.ML (pervasive);
wenzelm
parents:
20197
diff
changeset
|
1292 |
|
10413 | 1293 |
end; |
1294 |
||
11672 | 1295 |
structure BasicMetaSimplifier: BASIC_META_SIMPLIFIER = MetaSimplifier; |
1296 |
open BasicMetaSimplifier; |