author | wenzelm |
Thu, 29 Apr 2004 06:05:03 +0200 | |
changeset 14688 | edb7dacde656 |
parent 14643 | 130076a81b84 |
child 14981 | e73f8140af78 |
permissions | -rw-r--r-- |
10413 | 1 |
(* Title: Pure/meta_simplifier.ML |
2 |
ID: $Id$ |
|
11672 | 3 |
Author: Tobias Nipkow and Stefan Berghofer |
12783 | 4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
10413 | 5 |
|
11672 | 6 |
Meta-level Simplification. |
10413 | 7 |
*) |
8 |
||
11672 | 9 |
signature BASIC_META_SIMPLIFIER = |
10 |
sig |
|
11 |
val trace_simp: bool ref |
|
12 |
val debug_simp: bool ref |
|
13828 | 13 |
val simp_depth_limit: int ref |
11672 | 14 |
end; |
15 |
||
10413 | 16 |
signature META_SIMPLIFIER = |
17 |
sig |
|
11672 | 18 |
include BASIC_META_SIMPLIFIER |
10413 | 19 |
exception SIMPLIFIER of string * thm |
13486
54464ea94d6f
exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents:
13458
diff
changeset
|
20 |
exception SIMPROC_FAIL of string * exn |
10413 | 21 |
type meta_simpset |
12603 | 22 |
val dest_mss : meta_simpset -> |
10413 | 23 |
{simps: thm list, congs: thm list, procs: (string * cterm list) list} |
24 |
val empty_mss : meta_simpset |
|
12603 | 25 |
val clear_mss : meta_simpset -> meta_simpset |
26 |
val merge_mss : meta_simpset * meta_simpset -> meta_simpset |
|
10413 | 27 |
val add_simps : meta_simpset * thm list -> meta_simpset |
28 |
val del_simps : meta_simpset * thm list -> meta_simpset |
|
29 |
val mss_of : thm list -> meta_simpset |
|
30 |
val add_congs : meta_simpset * thm list -> meta_simpset |
|
31 |
val del_congs : meta_simpset * thm list -> meta_simpset |
|
12603 | 32 |
val add_simprocs : meta_simpset * |
10413 | 33 |
(string * cterm list * (Sign.sg -> thm list -> term -> thm option) * stamp) list |
34 |
-> meta_simpset |
|
12603 | 35 |
val del_simprocs : meta_simpset * |
10413 | 36 |
(string * cterm list * (Sign.sg -> thm list -> term -> thm option) * stamp) list |
37 |
-> meta_simpset |
|
38 |
val add_prems : meta_simpset * thm list -> meta_simpset |
|
39 |
val prems_of_mss : meta_simpset -> thm list |
|
40 |
val set_mk_rews : meta_simpset * (thm -> thm list) -> meta_simpset |
|
41 |
val set_mk_sym : meta_simpset * (thm -> thm option) -> meta_simpset |
|
42 |
val set_mk_eq_True : meta_simpset * (thm -> thm option) -> meta_simpset |
|
14242
ec70653a02bf
Added access to the mk_rews field (and friends).
skalberg
parents:
14040
diff
changeset
|
43 |
val get_mk_rews : meta_simpset -> thm -> thm list |
ec70653a02bf
Added access to the mk_rews field (and friends).
skalberg
parents:
14040
diff
changeset
|
44 |
val get_mk_sym : meta_simpset -> thm -> thm option |
ec70653a02bf
Added access to the mk_rews field (and friends).
skalberg
parents:
14040
diff
changeset
|
45 |
val get_mk_eq_True : meta_simpset -> thm -> thm option |
10413 | 46 |
val set_termless : meta_simpset * (term * term -> bool) -> meta_simpset |
12779 | 47 |
val beta_eta_conversion: cterm -> thm |
11672 | 48 |
val rewrite_cterm: bool * bool * bool -> |
49 |
(meta_simpset -> thm -> thm option) -> meta_simpset -> cterm -> thm |
|
11736 | 50 |
val goals_conv : (int -> bool) -> (cterm -> thm) -> cterm -> thm |
51 |
val forall_conv : (cterm -> thm) -> cterm -> thm |
|
52 |
val fconv_rule : (cterm -> thm) -> thm -> thm |
|
11767 | 53 |
val rewrite_aux : (meta_simpset -> thm -> thm option) -> bool -> thm list -> cterm -> thm |
54 |
val simplify_aux : (meta_simpset -> thm -> thm option) -> bool -> thm list -> thm -> thm |
|
10413 | 55 |
val rewrite_thm : bool * bool * bool |
56 |
-> (meta_simpset -> thm -> thm option) |
|
57 |
-> meta_simpset -> thm -> thm |
|
58 |
val rewrite_goals_rule_aux: (meta_simpset -> thm -> thm option) -> thm list -> thm -> thm |
|
59 |
val rewrite_goal_rule : bool* bool * bool |
|
60 |
-> (meta_simpset -> thm -> thm option) |
|
61 |
-> meta_simpset -> int -> thm -> thm |
|
13196 | 62 |
val rewrite_term: Sign.sg -> thm list -> (term -> term option) list -> term -> term |
10413 | 63 |
end; |
64 |
||
65 |
structure MetaSimplifier : META_SIMPLIFIER = |
|
66 |
struct |
|
67 |
||
68 |
(** diagnostics **) |
|
69 |
||
70 |
exception SIMPLIFIER of string * thm; |
|
13486
54464ea94d6f
exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents:
13458
diff
changeset
|
71 |
exception SIMPROC_FAIL of string * exn; |
10413 | 72 |
|
11505
a410fa8acfca
Implemented indentation schema for conditional rewrite trace.
nipkow
parents:
11504
diff
changeset
|
73 |
val simp_depth = ref 0; |
13828 | 74 |
val simp_depth_limit = ref 1000; |
11505
a410fa8acfca
Implemented indentation schema for conditional rewrite trace.
nipkow
parents:
11504
diff
changeset
|
75 |
|
12603 | 76 |
local |
77 |
||
78 |
fun println a = |
|
79 |
tracing ((case ! simp_depth of 0 => "" | n => "[" ^ string_of_int n ^ "]") ^ a); |
|
11505
a410fa8acfca
Implemented indentation schema for conditional rewrite trace.
nipkow
parents:
11504
diff
changeset
|
80 |
|
a410fa8acfca
Implemented indentation schema for conditional rewrite trace.
nipkow
parents:
11504
diff
changeset
|
81 |
fun prnt warn a = if warn then warning a else println a; |
12603 | 82 |
fun prtm warn a sign t = prnt warn (a ^ "\n" ^ Sign.string_of_term sign t); |
83 |
fun prctm warn a t = prnt warn (a ^ "\n" ^ Display.string_of_cterm t); |
|
10413 | 84 |
|
12603 | 85 |
in |
10413 | 86 |
|
12603 | 87 |
fun prthm warn a = prctm warn a o Thm.cprop_of; |
10413 | 88 |
|
89 |
val trace_simp = ref false; |
|
90 |
val debug_simp = ref false; |
|
91 |
||
92 |
fun trace warn a = if !trace_simp then prnt warn a else (); |
|
93 |
fun debug warn a = if !debug_simp then prnt warn a else (); |
|
94 |
||
95 |
fun trace_term warn a sign t = if !trace_simp then prtm warn a sign t else (); |
|
96 |
fun trace_cterm warn a t = if !trace_simp then prctm warn a t else (); |
|
97 |
fun debug_term warn a sign t = if !debug_simp then prtm warn a sign t else (); |
|
98 |
||
13569 | 99 |
fun trace_thm a thm = |
10413 | 100 |
let val {sign, prop, ...} = rep_thm thm |
13569 | 101 |
in trace_term false a sign prop end; |
102 |
||
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
103 |
fun trace_named_thm a (thm, name) = |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
104 |
trace_thm (a ^ (if name = "" then "" else " " ^ quote name) ^ ":") thm; |
10413 | 105 |
|
12603 | 106 |
end; |
10413 | 107 |
|
108 |
||
109 |
(** meta simp sets **) |
|
110 |
||
111 |
(* basic components *) |
|
112 |
||
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
113 |
type rrule = {thm: thm, name: string, lhs: term, elhs: cterm, fo: bool, perm: bool}; |
10413 | 114 |
(* thm: the rewrite rule |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
115 |
name: name of theorem from which rewrite rule was extracted |
10413 | 116 |
lhs: the left-hand side |
117 |
elhs: the etac-contracted lhs. |
|
118 |
fo: use first-order matching |
|
119 |
perm: the rewrite rule is permutative |
|
12603 | 120 |
Remarks: |
10413 | 121 |
- elhs is used for matching, |
122 |
lhs only for preservation of bound variable names. |
|
123 |
- fo is set iff |
|
124 |
either elhs is first-order (no Var is applied), |
|
125 |
in which case fo-matching is complete, |
|
126 |
or elhs is not a pattern, |
|
127 |
in which case there is nothing better to do. |
|
128 |
*) |
|
129 |
type cong = {thm: thm, lhs: cterm}; |
|
130 |
type simproc = |
|
131 |
{name: string, proc: Sign.sg -> thm list -> term -> thm option, lhs: cterm, id: stamp}; |
|
132 |
||
133 |
fun eq_rrule ({thm = thm1, ...}: rrule, {thm = thm2, ...}: rrule) = |
|
134 |
#prop (rep_thm thm1) aconv #prop (rep_thm thm2); |
|
135 |
||
12603 | 136 |
fun eq_cong ({thm = thm1, ...}: cong, {thm = thm2, ...}: cong) = |
10413 | 137 |
#prop (rep_thm thm1) aconv #prop (rep_thm thm2); |
138 |
||
139 |
fun eq_prem (thm1, thm2) = |
|
140 |
#prop (rep_thm thm1) aconv #prop (rep_thm thm2); |
|
141 |
||
142 |
fun eq_simproc ({id = s1, ...}:simproc, {id = s2, ...}:simproc) = (s1 = s2); |
|
143 |
||
144 |
fun mk_simproc (name, proc, lhs, id) = |
|
145 |
{name = name, proc = proc, lhs = lhs, id = id}; |
|
146 |
||
147 |
||
148 |
(* datatype mss *) |
|
149 |
||
150 |
(* |
|
151 |
A "mss" contains data needed during conversion: |
|
152 |
rules: discrimination net of rewrite rules; |
|
153 |
congs: association list of congruence rules and |
|
154 |
a list of `weak' congruence constants. |
|
155 |
A congruence is `weak' if it avoids normalization of some argument. |
|
156 |
procs: discrimination net of simplification procedures |
|
157 |
(functions that prove rewrite rules on the fly); |
|
158 |
bounds: names of bound variables already used |
|
159 |
(for generating new names when rewriting under lambda abstractions); |
|
160 |
prems: current premises; |
|
161 |
mk_rews: mk: turns simplification thms into rewrite rules; |
|
162 |
mk_sym: turns == around; (needs Drule!) |
|
163 |
mk_eq_True: turns P into P == True - logic specific; |
|
164 |
termless: relation for ordered rewriting; |
|
11504 | 165 |
depth: depth of conditional rewriting; |
10413 | 166 |
*) |
167 |
||
168 |
datatype meta_simpset = |
|
169 |
Mss of { |
|
170 |
rules: rrule Net.net, |
|
171 |
congs: (string * cong) list * string list, |
|
172 |
procs: simproc Net.net, |
|
173 |
bounds: string list, |
|
174 |
prems: thm list, |
|
175 |
mk_rews: {mk: thm -> thm list, |
|
176 |
mk_sym: thm -> thm option, |
|
177 |
mk_eq_True: thm -> thm option}, |
|
11504 | 178 |
termless: term * term -> bool, |
179 |
depth: int}; |
|
10413 | 180 |
|
11504 | 181 |
fun mk_mss (rules, congs, procs, bounds, prems, mk_rews, termless, depth) = |
10413 | 182 |
Mss {rules = rules, congs = congs, procs = procs, bounds = bounds, |
11504 | 183 |
prems=prems, mk_rews=mk_rews, termless=termless, depth=depth}; |
10413 | 184 |
|
11504 | 185 |
fun upd_rules(Mss{rules,congs,procs,bounds,prems,mk_rews,termless,depth}, rules') = |
186 |
mk_mss(rules',congs,procs,bounds,prems,mk_rews,termless,depth); |
|
10413 | 187 |
|
188 |
val empty_mss = |
|
189 |
let val mk_rews = {mk = K [], mk_sym = K None, mk_eq_True = K None} |
|
11504 | 190 |
in mk_mss (Net.empty, ([], []), Net.empty, [], [], mk_rews, Term.termless, 0) end; |
10413 | 191 |
|
192 |
fun clear_mss (Mss {mk_rews, termless, ...}) = |
|
11504 | 193 |
mk_mss (Net.empty, ([], []), Net.empty, [], [], mk_rews, termless,0); |
10413 | 194 |
|
11504 | 195 |
fun incr_depth(Mss{rules,congs,procs,bounds,prems,mk_rews,termless,depth}) = |
13828 | 196 |
let val depth1 = depth+1 |
197 |
in if depth1 > !simp_depth_limit |
|
198 |
then (warning "simp_depth_limit exceeded - giving up"; None) |
|
14040 | 199 |
else (if depth1 mod 10 = 0 |
13828 | 200 |
then warning("Simplification depth " ^ string_of_int depth1) |
201 |
else (); |
|
202 |
Some(mk_mss(rules,congs,procs,bounds,prems,mk_rews,termless,depth1)) |
|
203 |
) |
|
204 |
end; |
|
10413 | 205 |
|
206 |
||
207 |
(** simpset operations **) |
|
208 |
||
209 |
(* term variables *) |
|
210 |
||
211 |
val add_term_varnames = foldl_aterms (fn (xs, Var (x, _)) => ins_ix (x, xs) | (xs, _) => xs); |
|
212 |
fun term_varnames t = add_term_varnames ([], t); |
|
213 |
||
214 |
||
215 |
(* dest_mss *) |
|
216 |
||
217 |
fun dest_mss (Mss {rules, congs, procs, ...}) = |
|
218 |
{simps = map (fn (_, {thm, ...}) => thm) (Net.dest rules), |
|
219 |
congs = map (fn (_, {thm, ...}) => thm) (fst congs), |
|
220 |
procs = |
|
221 |
map (fn (_, {name, lhs, id, ...}) => ((name, lhs), id)) (Net.dest procs) |
|
222 |
|> partition_eq eq_snd |
|
223 |
|> map (fn ps => (#1 (#1 (hd ps)), map (#2 o #1) ps)) |
|
224 |
|> Library.sort_wrt #1}; |
|
225 |
||
226 |
||
12603 | 227 |
(* merge_mss *) (*NOTE: ignores mk_rews, termless and depth of 2nd mss*) |
10413 | 228 |
|
229 |
fun merge_mss |
|
230 |
(Mss {rules = rules1, congs = (congs1,weak1), procs = procs1, |
|
11504 | 231 |
bounds = bounds1, prems = prems1, mk_rews, termless, depth}, |
10413 | 232 |
Mss {rules = rules2, congs = (congs2,weak2), procs = procs2, |
233 |
bounds = bounds2, prems = prems2, ...}) = |
|
234 |
mk_mss |
|
235 |
(Net.merge (rules1, rules2, eq_rrule), |
|
12285 | 236 |
(gen_merge_lists (eq_cong o pairself snd) congs1 congs2, |
10413 | 237 |
merge_lists weak1 weak2), |
238 |
Net.merge (procs1, procs2, eq_simproc), |
|
239 |
merge_lists bounds1 bounds2, |
|
12285 | 240 |
gen_merge_lists eq_prem prems1 prems2, |
11504 | 241 |
mk_rews, termless, depth); |
10413 | 242 |
|
243 |
||
244 |
(* add_simps *) |
|
245 |
||
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
246 |
fun mk_rrule2{thm, name, lhs, elhs, perm} = |
10413 | 247 |
let val fo = Pattern.first_order (term_of elhs) orelse not(Pattern.pattern (term_of elhs)) |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
248 |
in {thm=thm, name=name, lhs=lhs, elhs=elhs, fo=fo, perm=perm} end |
10413 | 249 |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
250 |
fun insert_rrule quiet (mss as Mss {rules,...}, |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
251 |
rrule as {thm,name,lhs,elhs,perm}) = |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
252 |
(trace_named_thm "Adding rewrite rule" (thm, name); |
10413 | 253 |
let val rrule2 as {elhs,...} = mk_rrule2 rrule |
254 |
val rules' = Net.insert_term ((term_of elhs, rrule2), rules, eq_rrule) |
|
255 |
in upd_rules(mss,rules') end |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
256 |
handle Net.INSERT => if quiet then mss else |
10413 | 257 |
(prthm true "Ignoring duplicate rewrite rule:" thm; mss)); |
258 |
||
259 |
fun vperm (Var _, Var _) = true |
|
260 |
| vperm (Abs (_, _, s), Abs (_, _, t)) = vperm (s, t) |
|
261 |
| vperm (t1 $ t2, u1 $ u2) = vperm (t1, u1) andalso vperm (t2, u2) |
|
262 |
| vperm (t, u) = (t = u); |
|
263 |
||
264 |
fun var_perm (t, u) = |
|
265 |
vperm (t, u) andalso eq_set (term_varnames t, term_varnames u); |
|
266 |
||
267 |
(* FIXME: it seems that the conditions on extra variables are too liberal if |
|
268 |
prems are nonempty: does solving the prems really guarantee instantiation of |
|
269 |
all its Vars? Better: a dynamic check each time a rule is applied. |
|
270 |
*) |
|
271 |
fun rewrite_rule_extra_vars prems elhs erhs = |
|
272 |
not (term_varnames erhs subset foldl add_term_varnames (term_varnames elhs, prems)) |
|
273 |
orelse |
|
274 |
not ((term_tvars erhs) subset |
|
275 |
(term_tvars elhs union List.concat(map term_tvars prems))); |
|
276 |
||
277 |
(*Simple test for looping rewrite rules and stupid orientations*) |
|
278 |
fun reorient sign prems lhs rhs = |
|
279 |
rewrite_rule_extra_vars prems lhs rhs |
|
280 |
orelse |
|
281 |
is_Var (head_of lhs) |
|
282 |
orelse |
|
283 |
(exists (apl (lhs, Logic.occs)) (rhs :: prems)) |
|
284 |
orelse |
|
285 |
(null prems andalso |
|
14643 | 286 |
Pattern.matches (Sign.tsig_of sign) (lhs, rhs)) |
10413 | 287 |
(*the condition "null prems" is necessary because conditional rewrites |
288 |
with extra variables in the conditions may terminate although |
|
289 |
the rhs is an instance of the lhs. Example: ?m < ?n ==> f(?n) == f(?m)*) |
|
290 |
orelse |
|
291 |
(is_Const lhs andalso not(is_Const rhs)) |
|
292 |
||
293 |
fun decomp_simp thm = |
|
294 |
let val {sign, prop, ...} = rep_thm thm; |
|
295 |
val prems = Logic.strip_imp_prems prop; |
|
296 |
val concl = Drule.strip_imp_concl (cprop_of thm); |
|
297 |
val (lhs, rhs) = Drule.dest_equals concl handle TERM _ => |
|
298 |
raise SIMPLIFIER ("Rewrite rule not a meta-equality", thm) |
|
299 |
val elhs = snd (Drule.dest_equals (cprop_of (Thm.eta_conversion lhs))); |
|
300 |
val elhs = if elhs=lhs then lhs else elhs (* try to share *) |
|
301 |
val erhs = Pattern.eta_contract (term_of rhs); |
|
302 |
val perm = var_perm (term_of elhs, erhs) andalso not (term_of elhs aconv erhs) |
|
303 |
andalso not (is_Var (term_of elhs)) |
|
304 |
in (sign, prems, term_of lhs, elhs, term_of rhs, perm) end; |
|
305 |
||
12783 | 306 |
fun decomp_simp' thm = |
12979
4c76bce4ce39
decomp_simp': use lhs instead of elhs (preserves more bound variable names);
wenzelm
parents:
12783
diff
changeset
|
307 |
let val (_, _, lhs, _, rhs, _) = decomp_simp thm in |
12783 | 308 |
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
|
309 |
else (lhs, rhs) |
12783 | 310 |
end; |
311 |
||
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
312 |
fun mk_eq_True (Mss{mk_rews={mk_eq_True,...},...}) (thm, name) = |
10413 | 313 |
case mk_eq_True thm of |
314 |
None => [] |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
315 |
| Some eq_True => |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
316 |
let val (_,_,lhs,elhs,_,_) = decomp_simp eq_True |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
317 |
in [{thm=eq_True, name=name, lhs=lhs, elhs=elhs, perm=false}] end; |
10413 | 318 |
|
319 |
(* create the rewrite rule and possibly also the ==True variant, |
|
320 |
in case there are extra vars on the rhs *) |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
321 |
fun rrule_eq_True(thm,name,lhs,elhs,rhs,mss,thm2) = |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
322 |
let val rrule = {thm=thm, name=name, lhs=lhs, elhs=elhs, perm=false} |
10413 | 323 |
in if (term_varnames rhs) subset (term_varnames lhs) andalso |
324 |
(term_tvars rhs) subset (term_tvars lhs) |
|
325 |
then [rrule] |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
326 |
else mk_eq_True mss (thm2, name) @ [rrule] |
10413 | 327 |
end; |
328 |
||
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
329 |
fun mk_rrule mss (thm, name) = |
10413 | 330 |
let val (_,prems,lhs,elhs,rhs,perm) = decomp_simp thm |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
331 |
in if perm then [{thm=thm, name=name, lhs=lhs, elhs=elhs, perm=true}] else |
10413 | 332 |
(* weak test for loops: *) |
333 |
if rewrite_rule_extra_vars prems lhs rhs orelse |
|
334 |
is_Var (term_of elhs) |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
335 |
then mk_eq_True mss (thm, name) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
336 |
else rrule_eq_True(thm,name,lhs,elhs,rhs,mss,thm) |
10413 | 337 |
end; |
338 |
||
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
339 |
fun orient_rrule mss (thm, name) = |
10413 | 340 |
let val (sign,prems,lhs,elhs,rhs,perm) = decomp_simp thm |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
341 |
in if perm then [{thm=thm, name=name, lhs=lhs, elhs=elhs, perm=true}] |
10413 | 342 |
else if reorient sign prems lhs rhs |
343 |
then if reorient sign prems rhs lhs |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
344 |
then mk_eq_True mss (thm, name) |
10413 | 345 |
else let val Mss{mk_rews={mk_sym,...},...} = mss |
346 |
in case mk_sym thm of |
|
347 |
None => [] |
|
348 |
| Some thm' => |
|
349 |
let val (_,_,lhs',elhs',rhs',_) = decomp_simp thm' |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
350 |
in rrule_eq_True(thm',name,lhs',elhs',rhs',mss,thm) end |
10413 | 351 |
end |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
352 |
else rrule_eq_True(thm,name,lhs,elhs,rhs,mss,thm) |
10413 | 353 |
end; |
354 |
||
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
355 |
fun extract_rews(Mss{mk_rews = {mk,...},...},thms) = |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
356 |
flat (map (fn thm => map (rpair (Thm.name_of_thm thm)) (mk thm)) thms); |
10413 | 357 |
|
358 |
fun orient_comb_simps comb mk_rrule (mss,thms) = |
|
359 |
let val rews = extract_rews(mss,thms) |
|
360 |
val rrules = flat (map mk_rrule rews) |
|
361 |
in foldl comb (mss,rrules) end |
|
362 |
||
363 |
(* Add rewrite rules explicitly; do not reorient! *) |
|
364 |
fun add_simps(mss,thms) = |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
365 |
orient_comb_simps (insert_rrule false) (mk_rrule mss) (mss,thms); |
10413 | 366 |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
367 |
fun mss_of thms = foldl (insert_rrule false) (empty_mss, flat |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
368 |
(map (fn thm => mk_rrule empty_mss (thm, Thm.name_of_thm thm)) thms)); |
10413 | 369 |
|
370 |
fun extract_safe_rrules(mss,thm) = |
|
371 |
flat (map (orient_rrule mss) (extract_rews(mss,[thm]))); |
|
372 |
||
373 |
(* del_simps *) |
|
374 |
||
375 |
fun del_rrule(mss as Mss {rules,...}, |
|
376 |
rrule as {thm, elhs, ...}) = |
|
377 |
(upd_rules(mss, Net.delete_term ((term_of elhs, rrule), rules, eq_rrule)) |
|
378 |
handle Net.DELETE => |
|
379 |
(prthm true "Rewrite rule not in simpset:" thm; mss)); |
|
380 |
||
381 |
fun del_simps(mss,thms) = |
|
382 |
orient_comb_simps del_rrule (map mk_rrule2 o mk_rrule mss) (mss,thms); |
|
383 |
||
384 |
||
385 |
(* add_congs *) |
|
386 |
||
387 |
fun is_full_cong_prems [] varpairs = null varpairs |
|
388 |
| is_full_cong_prems (p::prems) varpairs = |
|
389 |
(case Logic.strip_assums_concl p of |
|
390 |
Const("==",_) $ lhs $ rhs => |
|
391 |
let val (x,xs) = strip_comb lhs and (y,ys) = strip_comb rhs |
|
392 |
in is_Var x andalso forall is_Bound xs andalso |
|
393 |
null(findrep(xs)) andalso xs=ys andalso |
|
394 |
(x,y) mem varpairs andalso |
|
395 |
is_full_cong_prems prems (varpairs\(x,y)) |
|
396 |
end |
|
397 |
| _ => false); |
|
398 |
||
399 |
fun is_full_cong thm = |
|
400 |
let val prems = prems_of thm |
|
401 |
and concl = concl_of thm |
|
402 |
val (lhs,rhs) = Logic.dest_equals concl |
|
403 |
val (f,xs) = strip_comb lhs |
|
404 |
and (g,ys) = strip_comb rhs |
|
405 |
in |
|
406 |
f=g andalso null(findrep(xs@ys)) andalso length xs = length ys andalso |
|
407 |
is_full_cong_prems prems (xs ~~ ys) |
|
408 |
end |
|
409 |
||
13835
12b2ffbe543a
Change to meta simplifier: congruence rules may now have frees as head of term.
ballarin
parents:
13828
diff
changeset
|
410 |
fun cong_name (Const (a, _)) = Some a |
12b2ffbe543a
Change to meta simplifier: congruence rules may now have frees as head of term.
ballarin
parents:
13828
diff
changeset
|
411 |
| cong_name (Free (a, _)) = Some ("Free: " ^ a) |
12b2ffbe543a
Change to meta simplifier: congruence rules may now have frees as head of term.
ballarin
parents:
13828
diff
changeset
|
412 |
| cong_name _ = None; |
12b2ffbe543a
Change to meta simplifier: congruence rules may now have frees as head of term.
ballarin
parents:
13828
diff
changeset
|
413 |
|
11504 | 414 |
fun add_cong (Mss {rules,congs,procs,bounds,prems,mk_rews,termless,depth}, thm) = |
10413 | 415 |
let |
416 |
val (lhs, _) = Drule.dest_equals (Drule.strip_imp_concl (cprop_of thm)) handle TERM _ => |
|
417 |
raise SIMPLIFIER ("Congruence not a meta-equality", thm); |
|
418 |
(* val lhs = Pattern.eta_contract lhs; *) |
|
13835
12b2ffbe543a
Change to meta simplifier: congruence rules may now have frees as head of term.
ballarin
parents:
13828
diff
changeset
|
419 |
val a = (case cong_name (head_of (term_of lhs)) of |
12b2ffbe543a
Change to meta simplifier: congruence rules may now have frees as head of term.
ballarin
parents:
13828
diff
changeset
|
420 |
Some a => a |
12b2ffbe543a
Change to meta simplifier: congruence rules may now have frees as head of term.
ballarin
parents:
13828
diff
changeset
|
421 |
| None => |
12b2ffbe543a
Change to meta simplifier: congruence rules may now have frees as head of term.
ballarin
parents:
13828
diff
changeset
|
422 |
raise SIMPLIFIER ("Congruence must start with a constant or free variable", thm)); |
10413 | 423 |
val (alist,weak) = congs |
424 |
val alist2 = overwrite_warn (alist, (a,{lhs=lhs, thm=thm})) |
|
425 |
("Overwriting congruence rule for " ^ quote a); |
|
426 |
val weak2 = if is_full_cong thm then weak else a::weak |
|
427 |
in |
|
11504 | 428 |
mk_mss (rules,(alist2,weak2),procs,bounds,prems,mk_rews,termless,depth) |
10413 | 429 |
end; |
430 |
||
431 |
val (op add_congs) = foldl add_cong; |
|
432 |
||
433 |
||
434 |
(* del_congs *) |
|
435 |
||
11504 | 436 |
fun del_cong (Mss {rules,congs,procs,bounds,prems,mk_rews,termless,depth}, thm) = |
10413 | 437 |
let |
438 |
val (lhs, _) = Logic.dest_equals (concl_of thm) handle TERM _ => |
|
439 |
raise SIMPLIFIER ("Congruence not a meta-equality", thm); |
|
440 |
(* val lhs = Pattern.eta_contract lhs; *) |
|
13835
12b2ffbe543a
Change to meta simplifier: congruence rules may now have frees as head of term.
ballarin
parents:
13828
diff
changeset
|
441 |
val a = (case cong_name (head_of lhs) of |
12b2ffbe543a
Change to meta simplifier: congruence rules may now have frees as head of term.
ballarin
parents:
13828
diff
changeset
|
442 |
Some a => a |
12b2ffbe543a
Change to meta simplifier: congruence rules may now have frees as head of term.
ballarin
parents:
13828
diff
changeset
|
443 |
| None => |
12b2ffbe543a
Change to meta simplifier: congruence rules may now have frees as head of term.
ballarin
parents:
13828
diff
changeset
|
444 |
raise SIMPLIFIER ("Congruence must start with a constant", thm)); |
10413 | 445 |
val (alist,_) = congs |
446 |
val alist2 = filter (fn (x,_)=> x<>a) alist |
|
447 |
val weak2 = mapfilter (fn(a,{thm,...}) => if is_full_cong thm then None |
|
448 |
else Some a) |
|
449 |
alist2 |
|
450 |
in |
|
11504 | 451 |
mk_mss (rules,(alist2,weak2),procs,bounds,prems,mk_rews,termless,depth) |
10413 | 452 |
end; |
453 |
||
454 |
val (op del_congs) = foldl del_cong; |
|
455 |
||
456 |
||
457 |
(* add_simprocs *) |
|
458 |
||
11504 | 459 |
fun add_proc (mss as Mss {rules,congs,procs,bounds,prems,mk_rews,termless,depth}, |
10413 | 460 |
(name, lhs, proc, id)) = |
461 |
let val {sign, t, ...} = rep_cterm lhs |
|
462 |
in (trace_term false ("Adding simplification procedure " ^ quote name ^ " for") |
|
463 |
sign t; |
|
464 |
mk_mss (rules, congs, |
|
465 |
Net.insert_term ((t, mk_simproc (name, proc, lhs, id)), procs, eq_simproc) |
|
12603 | 466 |
handle Net.INSERT => |
467 |
(warning ("Ignoring duplicate simplification procedure \"" |
|
468 |
^ name ^ "\""); |
|
469 |
procs), |
|
11504 | 470 |
bounds, prems, mk_rews, termless,depth)) |
10413 | 471 |
end; |
472 |
||
473 |
fun add_simproc (mss, (name, lhss, proc, id)) = |
|
474 |
foldl add_proc (mss, map (fn lhs => (name, lhs, proc, id)) lhss); |
|
475 |
||
476 |
val add_simprocs = foldl add_simproc; |
|
477 |
||
478 |
||
479 |
(* del_simprocs *) |
|
480 |
||
11504 | 481 |
fun del_proc (mss as Mss {rules,congs,procs,bounds,prems,mk_rews,termless,depth}, |
10413 | 482 |
(name, lhs, proc, id)) = |
483 |
mk_mss (rules, congs, |
|
484 |
Net.delete_term ((term_of lhs, mk_simproc (name, proc, lhs, id)), procs, eq_simproc) |
|
12603 | 485 |
handle Net.DELETE => |
486 |
(warning ("Simplification procedure \"" ^ name ^ |
|
487 |
"\" not in simpset"); procs), |
|
11504 | 488 |
bounds, prems, mk_rews, termless, depth); |
10413 | 489 |
|
490 |
fun del_simproc (mss, (name, lhss, proc, id)) = |
|
491 |
foldl del_proc (mss, map (fn lhs => (name, lhs, proc, id)) lhss); |
|
492 |
||
493 |
val del_simprocs = foldl del_simproc; |
|
494 |
||
495 |
||
496 |
(* prems *) |
|
497 |
||
11504 | 498 |
fun add_prems (Mss {rules,congs,procs,bounds,prems,mk_rews,termless,depth}, thms) = |
499 |
mk_mss (rules, congs, procs, bounds, thms @ prems, mk_rews, termless, depth); |
|
10413 | 500 |
|
501 |
fun prems_of_mss (Mss {prems, ...}) = prems; |
|
502 |
||
503 |
||
504 |
(* mk_rews *) |
|
505 |
||
506 |
fun set_mk_rews |
|
11504 | 507 |
(Mss {rules, congs, procs, bounds, prems, mk_rews, termless, depth}, mk) = |
10413 | 508 |
mk_mss (rules, congs, procs, bounds, prems, |
509 |
{mk=mk, mk_sym= #mk_sym mk_rews, mk_eq_True= #mk_eq_True mk_rews}, |
|
11504 | 510 |
termless, depth); |
10413 | 511 |
|
512 |
fun set_mk_sym |
|
11504 | 513 |
(Mss {rules,congs,procs,bounds,prems,mk_rews,termless,depth}, mk_sym) = |
10413 | 514 |
mk_mss (rules, congs, procs, bounds, prems, |
515 |
{mk= #mk mk_rews, mk_sym= mk_sym, mk_eq_True= #mk_eq_True mk_rews}, |
|
11504 | 516 |
termless,depth); |
10413 | 517 |
|
518 |
fun set_mk_eq_True |
|
11504 | 519 |
(Mss {rules,congs,procs,bounds,prems,mk_rews,termless,depth}, mk_eq_True) = |
10413 | 520 |
mk_mss (rules, congs, procs, bounds, prems, |
521 |
{mk= #mk mk_rews, mk_sym= #mk_sym mk_rews, mk_eq_True= mk_eq_True}, |
|
11504 | 522 |
termless,depth); |
10413 | 523 |
|
14242
ec70653a02bf
Added access to the mk_rews field (and friends).
skalberg
parents:
14040
diff
changeset
|
524 |
fun get_mk_rews (Mss {mk_rews,...}) = #mk mk_rews |
ec70653a02bf
Added access to the mk_rews field (and friends).
skalberg
parents:
14040
diff
changeset
|
525 |
fun get_mk_sym (Mss {mk_rews,...}) = #mk_sym mk_rews |
ec70653a02bf
Added access to the mk_rews field (and friends).
skalberg
parents:
14040
diff
changeset
|
526 |
fun get_mk_eq_True (Mss {mk_rews,...}) = #mk_eq_True mk_rews |
ec70653a02bf
Added access to the mk_rews field (and friends).
skalberg
parents:
14040
diff
changeset
|
527 |
|
10413 | 528 |
(* termless *) |
529 |
||
530 |
fun set_termless |
|
11504 | 531 |
(Mss {rules, congs, procs, bounds, prems, mk_rews, depth, ...}, termless) = |
532 |
mk_mss (rules, congs, procs, bounds, prems, mk_rews, termless, depth); |
|
10413 | 533 |
|
534 |
||
535 |
||
536 |
(** rewriting **) |
|
537 |
||
538 |
(* |
|
539 |
Uses conversions, see: |
|
540 |
L C Paulson, A higher-order implementation of rewriting, |
|
541 |
Science of Computer Programming 3 (1983), pages 119-149. |
|
542 |
*) |
|
543 |
||
544 |
val dest_eq = Drule.dest_equals o cprop_of; |
|
545 |
val lhs_of = fst o dest_eq; |
|
546 |
val rhs_of = snd o dest_eq; |
|
547 |
||
548 |
fun beta_eta_conversion t = |
|
549 |
let val thm = beta_conversion true t; |
|
550 |
in transitive thm (eta_conversion (rhs_of thm)) end; |
|
551 |
||
552 |
fun check_conv msg thm thm' = |
|
553 |
let |
|
554 |
val thm'' = transitive thm (transitive |
|
555 |
(symmetric (beta_eta_conversion (lhs_of thm'))) thm') |
|
13569 | 556 |
in (if msg then trace_thm "SUCCEEDED" thm' else (); Some thm'') end |
10413 | 557 |
handle THM _ => |
558 |
let val {sign, prop = _ $ _ $ prop0, ...} = rep_thm thm; |
|
559 |
in |
|
13569 | 560 |
(trace_thm "Proved wrong thm (Check subgoaler?)" thm'; |
10413 | 561 |
trace_term false "Should have proved:" sign prop0; |
562 |
None) |
|
563 |
end; |
|
564 |
||
565 |
||
566 |
(* mk_procrule *) |
|
567 |
||
568 |
fun mk_procrule thm = |
|
569 |
let val (_,prems,lhs,elhs,rhs,_) = decomp_simp thm |
|
570 |
in if rewrite_rule_extra_vars prems lhs rhs |
|
571 |
then (prthm true "Extra vars on rhs:" thm; []) |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
572 |
else [mk_rrule2{thm=thm, name="", lhs=lhs, elhs=elhs, perm=false}] |
10413 | 573 |
end; |
574 |
||
575 |
||
576 |
(* conversion to apply the meta simpset to a term *) |
|
577 |
||
578 |
(* Since the rewriting strategy is bottom-up, we avoid re-normalizing already |
|
579 |
normalized terms by carrying around the rhs of the rewrite rule just |
|
580 |
applied. This is called the `skeleton'. It is decomposed in parallel |
|
581 |
with the term. Once a Var is encountered, the corresponding term is |
|
582 |
already in normal form. |
|
583 |
skel0 is a dummy skeleton that is to enforce complete normalization. |
|
584 |
*) |
|
585 |
val skel0 = Bound 0; |
|
586 |
||
587 |
(* Use rhs as skeleton only if the lhs does not contain unnormalized bits. |
|
588 |
The latter may happen iff there are weak congruence rules for constants |
|
589 |
in the lhs. |
|
590 |
*) |
|
591 |
fun uncond_skel((_,weak),(lhs,rhs)) = |
|
592 |
if null weak then rhs (* optimization *) |
|
593 |
else if exists_Const (fn (c,_) => c mem weak) lhs then skel0 |
|
594 |
else rhs; |
|
595 |
||
596 |
(* Behaves like unconditional rule if rhs does not contain vars not in the lhs. |
|
597 |
Otherwise those vars may become instantiated with unnormalized terms |
|
598 |
while the premises are solved. |
|
599 |
*) |
|
600 |
fun cond_skel(args as (congs,(lhs,rhs))) = |
|
601 |
if term_varnames rhs subset term_varnames lhs then uncond_skel(args) |
|
602 |
else skel0; |
|
603 |
||
604 |
(* |
|
605 |
we try in order: |
|
606 |
(1) beta reduction |
|
607 |
(2) unconditional rewrite rules |
|
608 |
(3) conditional rewrite rules |
|
609 |
(4) simplification procedures |
|
610 |
||
611 |
IMPORTANT: rewrite rules must not introduce new Vars or TVars! |
|
612 |
||
613 |
*) |
|
614 |
||
615 |
fun rewritec (prover, signt, maxt) |
|
11504 | 616 |
(mss as Mss{rules, procs, termless, prems, congs, depth,...}) t = |
10413 | 617 |
let |
618 |
val eta_thm = Thm.eta_conversion t; |
|
619 |
val eta_t' = rhs_of eta_thm; |
|
620 |
val eta_t = term_of eta_t'; |
|
621 |
val tsigt = Sign.tsig_of signt; |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
622 |
fun rew {thm, name, lhs, elhs, fo, perm} = |
10413 | 623 |
let |
624 |
val {sign, prop, maxidx, ...} = rep_thm thm; |
|
625 |
val _ = if Sign.subsig (sign, signt) then () |
|
626 |
else (prthm true "Ignoring rewrite rule from different theory:" thm; |
|
627 |
raise Pattern.MATCH); |
|
628 |
val (rthm, elhs') = if maxt = ~1 then (thm, elhs) |
|
629 |
else (Thm.incr_indexes (maxt+1) thm, Thm.cterm_incr_indexes (maxt+1) elhs); |
|
630 |
val insts = if fo then Thm.cterm_first_order_match (elhs', eta_t') |
|
631 |
else Thm.cterm_match (elhs', eta_t'); |
|
632 |
val thm' = Thm.instantiate insts (Thm.rename_boundvars lhs eta_t rthm); |
|
14643 | 633 |
val prop' = Thm.prop_of thm'; |
10413 | 634 |
val unconditional = (Logic.count_prems (prop',0) = 0); |
635 |
val (lhs', rhs') = Logic.dest_equals (Logic.strip_imp_concl prop') |
|
636 |
in |
|
11295 | 637 |
if perm andalso not (termless (rhs', lhs')) |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
638 |
then (trace_named_thm "Cannot apply permutative rewrite rule" (thm, name); |
13569 | 639 |
trace_thm "Term does not become smaller:" thm'; None) |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
640 |
else (trace_named_thm "Applying instance of rewrite rule" (thm, name); |
10413 | 641 |
if unconditional |
642 |
then |
|
13569 | 643 |
(trace_thm "Rewriting:" thm'; |
10413 | 644 |
let val lr = Logic.dest_equals prop; |
645 |
val Some thm'' = check_conv false eta_thm thm' |
|
646 |
in Some (thm'', uncond_skel (congs, lr)) end) |
|
647 |
else |
|
13569 | 648 |
(trace_thm "Trying to rewrite:" thm'; |
13828 | 649 |
case incr_depth mss of |
650 |
None => (trace_thm "FAILED - reached depth limit" thm'; None) |
|
651 |
| Some mss => |
|
652 |
(case prover mss thm' of |
|
13569 | 653 |
None => (trace_thm "FAILED" thm'; None) |
10413 | 654 |
| Some thm2 => |
655 |
(case check_conv true eta_thm thm2 of |
|
656 |
None => None | |
|
657 |
Some thm2' => |
|
658 |
let val concl = Logic.strip_imp_concl prop |
|
659 |
val lr = Logic.dest_equals concl |
|
13828 | 660 |
in Some (thm2', cond_skel (congs, lr)) end)))) |
10413 | 661 |
end |
662 |
||
663 |
fun rews [] = None |
|
664 |
| rews (rrule :: rrules) = |
|
665 |
let val opt = rew rrule handle Pattern.MATCH => None |
|
666 |
in case opt of None => rews rrules | some => some end; |
|
667 |
||
668 |
fun sort_rrules rrs = let |
|
14643 | 669 |
fun is_simple({thm, ...}:rrule) = case Thm.prop_of thm of |
10413 | 670 |
Const("==",_) $ _ $ _ => true |
12603 | 671 |
| _ => false |
10413 | 672 |
fun sort [] (re1,re2) = re1 @ re2 |
12603 | 673 |
| sort (rr::rrs) (re1,re2) = if is_simple rr |
10413 | 674 |
then sort rrs (rr::re1,re2) |
675 |
else sort rrs (re1,rr::re2) |
|
676 |
in sort rrs ([],[]) end |
|
677 |
||
678 |
fun proc_rews ([]:simproc list) = None |
|
679 |
| proc_rews ({name, proc, lhs, ...} :: ps) = |
|
680 |
if Pattern.matches tsigt (term_of lhs, term_of t) then |
|
681 |
(debug_term false ("Trying procedure " ^ quote name ^ " on:") signt eta_t; |
|
13486
54464ea94d6f
exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents:
13458
diff
changeset
|
682 |
case transform_failure (curry SIMPROC_FAIL name) |
54464ea94d6f
exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents:
13458
diff
changeset
|
683 |
(fn () => proc signt prems eta_t) () of |
54464ea94d6f
exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents:
13458
diff
changeset
|
684 |
None => (debug false "FAILED"; proc_rews ps) |
54464ea94d6f
exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents:
13458
diff
changeset
|
685 |
| Some raw_thm => |
13569 | 686 |
(trace_thm ("Procedure " ^ quote name ^ " produced rewrite rule:") raw_thm; |
10413 | 687 |
(case rews (mk_procrule raw_thm) of |
13486
54464ea94d6f
exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents:
13458
diff
changeset
|
688 |
None => (trace_cterm true ("IGNORED result of simproc " ^ quote name ^ |
54464ea94d6f
exception SIMPROC_FAIL: solid error reporting of simprocs;
wenzelm
parents:
13458
diff
changeset
|
689 |
" -- does not match") t; proc_rews ps) |
10413 | 690 |
| some => some))) |
691 |
else proc_rews ps; |
|
692 |
in case eta_t of |
|
693 |
Abs _ $ _ => Some (transitive eta_thm |
|
12155
13c5469b4bb3
congc now returns None if congruence rule has no effect.
berghofe
parents:
11886
diff
changeset
|
694 |
(beta_conversion false eta_t'), skel0) |
10413 | 695 |
| _ => (case rews (sort_rrules (Net.match_term rules eta_t)) of |
696 |
None => proc_rews (Net.match_term procs eta_t) |
|
697 |
| some => some) |
|
698 |
end; |
|
699 |
||
700 |
||
701 |
(* conversion to apply a congruence rule to a term *) |
|
702 |
||
703 |
fun congc (prover,signt,maxt) {thm=cong,lhs=lhs} t = |
|
14643 | 704 |
let val sign = Thm.sign_of_thm cong |
10413 | 705 |
val _ = if Sign.subsig (sign, signt) then () |
706 |
else error("Congruence rule from different theory") |
|
707 |
val rthm = if maxt = ~1 then cong else Thm.incr_indexes (maxt+1) cong; |
|
708 |
val rlhs = fst (Drule.dest_equals (Drule.strip_imp_concl (cprop_of rthm))); |
|
709 |
val insts = Thm.cterm_match (rlhs, t) |
|
710 |
(* Pattern.match can raise Pattern.MATCH; |
|
711 |
is handled when congc is called *) |
|
712 |
val thm' = Thm.instantiate insts (Thm.rename_boundvars (term_of rlhs) (term_of t) rthm); |
|
13569 | 713 |
val unit = trace_thm "Applying congruence rule:" thm'; |
13932
0eb3d91b519a
Simplifier no longer aborts on failed congruence proof.
ballarin
parents:
13835
diff
changeset
|
714 |
fun err (msg, thm) = (trace_thm msg thm; None) |
10413 | 715 |
in case prover thm' of |
13932
0eb3d91b519a
Simplifier no longer aborts on failed congruence proof.
ballarin
parents:
13835
diff
changeset
|
716 |
None => err ("Congruence proof failed. Could not prove", thm') |
10413 | 717 |
| Some thm2 => (case check_conv true (beta_eta_conversion t) thm2 of |
13932
0eb3d91b519a
Simplifier no longer aborts on failed congruence proof.
ballarin
parents:
13835
diff
changeset
|
718 |
None => err ("Congruence proof failed. Should not have proved", thm2) |
12155
13c5469b4bb3
congc now returns None if congruence rule has no effect.
berghofe
parents:
11886
diff
changeset
|
719 |
| Some thm2' => |
13c5469b4bb3
congc now returns None if congruence rule has no effect.
berghofe
parents:
11886
diff
changeset
|
720 |
if op aconv (pairself term_of (dest_equals (cprop_of thm2'))) |
13c5469b4bb3
congc now returns None if congruence rule has no effect.
berghofe
parents:
11886
diff
changeset
|
721 |
then None else Some thm2') |
10413 | 722 |
end; |
723 |
||
724 |
val (cA, (cB, cC)) = |
|
725 |
apsnd dest_equals (dest_implies (hd (cprems_of Drule.imp_cong))); |
|
726 |
||
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
727 |
fun transitive1 None None = None |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
728 |
| transitive1 (Some thm1) None = Some thm1 |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
729 |
| transitive1 None (Some thm2) = Some thm2 |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
730 |
| transitive1 (Some thm1) (Some thm2) = Some (transitive thm1 thm2) |
10413 | 731 |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
732 |
fun transitive2 thm = transitive1 (Some thm); |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
733 |
fun transitive3 thm = transitive1 thm o Some; |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
734 |
|
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
735 |
fun imp_cong' e = combination (combination refl_implies e); |
12155
13c5469b4bb3
congc now returns None if congruence rule has no effect.
berghofe
parents:
11886
diff
changeset
|
736 |
|
10413 | 737 |
fun bottomc ((simprem,useprem,mutsimp), prover, sign, maxidx) = |
738 |
let |
|
739 |
fun botc skel mss t = |
|
740 |
if is_Var skel then None |
|
741 |
else |
|
742 |
(case subc skel mss t of |
|
743 |
some as Some thm1 => |
|
744 |
(case rewritec (prover, sign, maxidx) mss (rhs_of thm1) of |
|
745 |
Some (thm2, skel2) => |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
746 |
transitive2 (transitive thm1 thm2) |
10413 | 747 |
(botc skel2 mss (rhs_of thm2)) |
748 |
| None => some) |
|
749 |
| None => |
|
750 |
(case rewritec (prover, sign, maxidx) mss t of |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
751 |
Some (thm2, skel2) => transitive2 thm2 |
10413 | 752 |
(botc skel2 mss (rhs_of thm2)) |
753 |
| None => None)) |
|
754 |
||
755 |
and try_botc mss t = |
|
756 |
(case botc skel0 mss t of |
|
757 |
Some trec1 => trec1 | None => (reflexive t)) |
|
758 |
||
759 |
and subc skel |
|
11504 | 760 |
(mss as Mss{rules,congs,procs,bounds,prems,mk_rews,termless,depth}) t0 = |
10413 | 761 |
(case term_of t0 of |
762 |
Abs (a, T, t) => |
|
763 |
let val b = variant bounds a |
|
10767
8fa4aafa7314
Thm: dest_comb, dest_abs, capply, cabs no longer global;
wenzelm
parents:
10413
diff
changeset
|
764 |
val (v, t') = Thm.dest_abs (Some ("." ^ b)) t0 |
11504 | 765 |
val mss' = mk_mss (rules, congs, procs, b :: bounds, prems, mk_rews, termless,depth) |
10413 | 766 |
val skel' = case skel of Abs (_, _, sk) => sk | _ => skel0 |
767 |
in case botc skel' mss' t' of |
|
768 |
Some thm => Some (abstract_rule a v thm) |
|
769 |
| None => None |
|
770 |
end |
|
771 |
| t $ _ => (case t of |
|
13614 | 772 |
Const ("==>", _) $ _ => impc t0 mss |
10413 | 773 |
| Abs _ => |
774 |
let val thm = beta_conversion false t0 |
|
775 |
in case subc skel0 mss (rhs_of thm) of |
|
776 |
None => Some thm |
|
777 |
| Some thm' => Some (transitive thm thm') |
|
778 |
end |
|
779 |
| _ => |
|
780 |
let fun appc () = |
|
781 |
let |
|
782 |
val (tskel, uskel) = case skel of |
|
783 |
tskel $ uskel => (tskel, uskel) |
|
784 |
| _ => (skel0, skel0); |
|
10767
8fa4aafa7314
Thm: dest_comb, dest_abs, capply, cabs no longer global;
wenzelm
parents:
10413
diff
changeset
|
785 |
val (ct, cu) = Thm.dest_comb t0 |
10413 | 786 |
in |
787 |
(case botc tskel mss ct of |
|
788 |
Some thm1 => |
|
789 |
(case botc uskel mss cu of |
|
790 |
Some thm2 => Some (combination thm1 thm2) |
|
791 |
| None => Some (combination thm1 (reflexive cu))) |
|
792 |
| None => |
|
793 |
(case botc uskel mss cu of |
|
794 |
Some thm1 => Some (combination (reflexive ct) thm1) |
|
795 |
| None => None)) |
|
796 |
end |
|
797 |
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
|
798 |
in case cong_name h of |
12b2ffbe543a
Change to meta simplifier: congruence rules may now have frees as head of term.
ballarin
parents:
13828
diff
changeset
|
799 |
Some a => |
10413 | 800 |
(case assoc_string (fst congs, a) of |
801 |
None => appc () |
|
802 |
| Some cong => |
|
803 |
(* post processing: some partial applications h t1 ... tj, j <= length ts, |
|
804 |
may be a redex. Example: map (%x.x) = (%xs.xs) wrt map_cong *) |
|
805 |
(let |
|
806 |
val thm = congc (prover mss, sign, maxidx) cong t0; |
|
12155
13c5469b4bb3
congc now returns None if congruence rule has no effect.
berghofe
parents:
11886
diff
changeset
|
807 |
val t = if_none (apsome rhs_of thm) t0; |
10767
8fa4aafa7314
Thm: dest_comb, dest_abs, capply, cabs no longer global;
wenzelm
parents:
10413
diff
changeset
|
808 |
val (cl, cr) = Thm.dest_comb t |
10413 | 809 |
val dVar = Var(("", 0), dummyT) |
810 |
val skel = |
|
811 |
list_comb (h, replicate (length ts) dVar) |
|
812 |
in case botc skel mss cl of |
|
12155
13c5469b4bb3
congc now returns None if congruence rule has no effect.
berghofe
parents:
11886
diff
changeset
|
813 |
None => thm |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
814 |
| Some thm' => transitive3 thm |
12155
13c5469b4bb3
congc now returns None if congruence rule has no effect.
berghofe
parents:
11886
diff
changeset
|
815 |
(combination thm' (reflexive cr)) |
10413 | 816 |
end handle TERM _ => error "congc result" |
817 |
| Pattern.MATCH => appc ())) |
|
818 |
| _ => appc () |
|
819 |
end) |
|
820 |
| _ => None) |
|
821 |
||
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
822 |
and impc ct mss = |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
823 |
if mutsimp then mut_impc0 [] ct [] [] mss else nonmut_impc ct mss |
10413 | 824 |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
825 |
and rules_of_prem mss prem = |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
826 |
if maxidx_of_term (term_of prem) <> ~1 |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
827 |
then (trace_cterm true |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
828 |
"Cannot add premise as rewrite rule because it contains (type) unknowns:" prem; ([], None)) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
829 |
else |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
830 |
let val asm = assume prem |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
831 |
in (extract_safe_rrules (mss, asm), Some asm) end |
10413 | 832 |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
833 |
and add_rrules (rrss, asms) mss = |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
834 |
add_prems (foldl (insert_rrule true) (mss, flat rrss), mapfilter I asms) |
10413 | 835 |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
836 |
and disch r (prem, eq) = |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
837 |
let |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
838 |
val (lhs, rhs) = dest_eq eq; |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
839 |
val eq' = implies_elim (Thm.instantiate |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
840 |
([], [(cA, prem), (cB, lhs), (cC, rhs)]) Drule.imp_cong) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
841 |
(implies_intr prem eq) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
842 |
in if not r then eq' else |
10413 | 843 |
let |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
844 |
val (prem', concl) = dest_implies lhs; |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
845 |
val (prem'', _) = dest_implies rhs |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
846 |
in transitive (transitive |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
847 |
(Thm.instantiate ([], [(cA, prem'), (cB, prem), (cC, concl)]) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
848 |
Drule.swap_prems_eq) eq') |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
849 |
(Thm.instantiate ([], [(cA, prem), (cB, prem''), (cC, concl)]) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
850 |
Drule.swap_prems_eq) |
10413 | 851 |
end |
852 |
end |
|
853 |
||
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
854 |
and rebuild [] _ _ _ _ eq = eq |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
855 |
| rebuild (prem :: prems) concl (rrs :: rrss) (asm :: asms) mss eq = |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
856 |
let |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
857 |
val mss' = add_rrules (rev rrss, rev asms) mss; |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
858 |
val concl' = |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
859 |
Drule.mk_implies (prem, if_none (apsome rhs_of eq) concl); |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
860 |
val dprem = apsome (curry (disch false) prem) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
861 |
in case rewritec (prover, sign, maxidx) mss' concl' of |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
862 |
None => rebuild prems concl' rrss asms mss (dprem eq) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
863 |
| Some (eq', _) => transitive2 (foldl (disch false o swap) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
864 |
(the (transitive3 (dprem eq) eq'), prems)) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
865 |
(mut_impc0 (rev prems) (rhs_of eq') (rev rrss) (rev asms) mss) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
866 |
end |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
867 |
|
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
868 |
and mut_impc0 prems concl rrss asms mss = |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
869 |
let |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
870 |
val prems' = strip_imp_prems concl; |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
871 |
val (rrss', asms') = split_list (map (rules_of_prem mss) prems') |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
872 |
in mut_impc (prems @ prems') (strip_imp_concl concl) (rrss @ rrss') |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
873 |
(asms @ asms') [] [] [] [] mss ~1 ~1 |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
874 |
end |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
875 |
|
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
876 |
and mut_impc [] concl [] [] prems' rrss' asms' eqns mss changed k = |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
877 |
transitive1 (foldl (fn (eq2, (eq1, prem)) => transitive1 eq1 |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
878 |
(apsome (curry (disch false) prem) eq2)) (None, eqns ~~ prems')) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
879 |
(if changed > 0 then |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
880 |
mut_impc (rev prems') concl (rev rrss') (rev asms') |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
881 |
[] [] [] [] mss ~1 changed |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
882 |
else rebuild prems' concl rrss' asms' mss |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
883 |
(botc skel0 (add_rrules (rev rrss', rev asms') mss) concl)) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
884 |
|
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
885 |
| mut_impc (prem :: prems) concl (rrs :: rrss) (asm :: asms) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
886 |
prems' rrss' asms' eqns mss changed k = |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
887 |
case (if k = 0 then None else botc skel0 (add_rrules |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
888 |
(rev rrss' @ rrss, rev asms' @ asms) mss) prem) of |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
889 |
None => mut_impc prems concl rrss asms (prem :: prems') |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
890 |
(rrs :: rrss') (asm :: asms') (None :: eqns) mss changed |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
891 |
(if k = 0 then 0 else k - 1) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
892 |
| Some eqn => |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
893 |
let |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
894 |
val prem' = rhs_of eqn; |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
895 |
val tprems = map term_of prems; |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
896 |
val i = 1 + foldl Int.max (~1, map (fn p => |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
897 |
find_index_eq p tprems) (#hyps (rep_thm eqn))); |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
898 |
val (rrs', asm') = rules_of_prem mss prem' |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
899 |
in mut_impc prems concl rrss asms (prem' :: prems') |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
900 |
(rrs' :: rrss') (asm' :: asms') (Some (foldr (disch true) |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
901 |
(take (i, prems), imp_cong' eqn (reflexive (Drule.list_implies |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
902 |
(drop (i, prems), concl))))) :: eqns) mss (length prems') ~1 |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
903 |
end |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
904 |
|
10413 | 905 |
(* legacy code - only for backwards compatibility *) |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
906 |
and nonmut_impc ct mss = |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
907 |
let val (prem, conc) = dest_implies ct; |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
908 |
val thm1 = if simprem then botc skel0 mss prem else None; |
10413 | 909 |
val prem1 = if_none (apsome rhs_of thm1) prem; |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
910 |
val mss1 = if not useprem then mss else add_rrules |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
911 |
(apsnd single (apfst single (rules_of_prem mss prem1))) mss |
10413 | 912 |
in (case botc skel0 mss1 conc of |
913 |
None => (case thm1 of |
|
914 |
None => None |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
915 |
| Some thm1' => Some (imp_cong' thm1' (reflexive conc))) |
10413 | 916 |
| Some thm2 => |
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
917 |
let val thm2' = disch false (prem1, thm2) |
10413 | 918 |
in (case thm1 of |
919 |
None => Some thm2' |
|
13607
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
920 |
| Some thm1' => |
6908230623a3
Completely reimplemented mutual simplification of premises.
berghofe
parents:
13569
diff
changeset
|
921 |
Some (transitive (imp_cong' thm1' (reflexive conc)) thm2')) |
10413 | 922 |
end) |
923 |
end |
|
924 |
||
925 |
in try_botc end; |
|
926 |
||
927 |
||
928 |
(*** Meta-rewriting: rewrites t to u and returns the theorem t==u ***) |
|
929 |
||
930 |
(* |
|
931 |
Parameters: |
|
932 |
mode = (simplify A, |
|
933 |
use A in simplifying B, |
|
934 |
use prems of B (if B is again a meta-impl.) to simplify A) |
|
935 |
when simplifying A ==> B |
|
936 |
mss: contains equality theorems of the form [|p1,...|] ==> t==u |
|
937 |
prover: how to solve premises in conditional rewrites and congruences |
|
938 |
*) |
|
939 |
||
940 |
fun rewrite_cterm mode prover mss ct = |
|
941 |
let val {sign, t, maxidx, ...} = rep_cterm ct |
|
11505
a410fa8acfca
Implemented indentation schema for conditional rewrite trace.
nipkow
parents:
11504
diff
changeset
|
942 |
val Mss{depth, ...} = mss |
14330 | 943 |
in trace_cterm false "SIMPLIFIER INVOKED ON THE FOLLOWING TERM:" ct; |
944 |
simp_depth := depth; |
|
11505
a410fa8acfca
Implemented indentation schema for conditional rewrite trace.
nipkow
parents:
11504
diff
changeset
|
945 |
bottomc (mode, prover, sign, maxidx) mss ct |
a410fa8acfca
Implemented indentation schema for conditional rewrite trace.
nipkow
parents:
11504
diff
changeset
|
946 |
end |
10413 | 947 |
handle THM (s, _, thms) => |
948 |
error ("Exception THM was raised in simplifier:\n" ^ s ^ "\n" ^ |
|
11886 | 949 |
Pretty.string_of (Display.pretty_thms thms)); |
10413 | 950 |
|
951 |
(*In [A1,...,An]==>B, rewrite the selected A's only -- for rewrite_goals_tac*) |
|
952 |
fun goals_conv pred cv = |
|
953 |
let fun gconv i ct = |
|
954 |
let val (A,B) = Drule.dest_implies ct |
|
13661
ec97dfc2bfe0
No more explicit manipulation of flex-flex constraints in goals_conv.
berghofe
parents:
13614
diff
changeset
|
955 |
in imp_cong' (if pred i then cv A else reflexive A) (gconv (i+1) B) end |
10413 | 956 |
handle TERM _ => reflexive ct |
957 |
in gconv 1 end; |
|
958 |
||
11737 | 959 |
(* Rewrite A in !!x1,...,xn. A *) |
11736 | 960 |
fun forall_conv cv ct = |
961 |
let val p as (ct1, ct2) = Thm.dest_comb ct |
|
962 |
in (case pairself term_of p of |
|
963 |
(Const ("all", _), Abs (s, _, _)) => |
|
964 |
let val (v, ct') = Thm.dest_abs (Some "@") ct2; |
|
965 |
in Thm.combination (Thm.reflexive ct1) |
|
966 |
(Thm.abstract_rule s v (forall_conv cv ct')) |
|
967 |
end |
|
968 |
| _ => cv ct) |
|
969 |
end handle TERM _ => cv ct; |
|
970 |
||
10413 | 971 |
(*Use a conversion to transform a theorem*) |
972 |
fun fconv_rule cv th = equal_elim (cv (cprop_of th)) th; |
|
973 |
||
11760 | 974 |
(*Rewrite a cterm*) |
11767 | 975 |
fun rewrite_aux _ _ [] = (fn ct => Thm.reflexive ct) |
976 |
| rewrite_aux prover full thms = rewrite_cterm (full, false, false) prover (mss_of thms); |
|
11672 | 977 |
|
10413 | 978 |
(*Rewrite a theorem*) |
11767 | 979 |
fun simplify_aux _ _ [] = (fn th => th) |
980 |
| simplify_aux prover full thms = |
|
981 |
fconv_rule (rewrite_cterm (full, false, false) prover (mss_of thms)); |
|
10413 | 982 |
|
983 |
fun rewrite_thm mode prover mss = fconv_rule (rewrite_cterm mode prover mss); |
|
984 |
||
985 |
(*Rewrite the subgoals of a proof state (represented by a theorem) *) |
|
986 |
fun rewrite_goals_rule_aux _ [] th = th |
|
987 |
| rewrite_goals_rule_aux prover thms th = |
|
988 |
fconv_rule (goals_conv (K true) (rewrite_cterm (true, true, false) prover |
|
989 |
(mss_of thms))) th; |
|
990 |
||
991 |
(*Rewrite the subgoal of a proof state (represented by a theorem) *) |
|
992 |
fun rewrite_goal_rule mode prover mss i thm = |
|
993 |
if 0 < i andalso i <= nprems_of thm |
|
994 |
then fconv_rule (goals_conv (fn j => j=i) (rewrite_cterm mode prover mss)) thm |
|
995 |
else raise THM("rewrite_goal_rule",i,[thm]); |
|
996 |
||
12783 | 997 |
|
998 |
(*simple term rewriting -- without proofs*) |
|
13196 | 999 |
fun rewrite_term sg rules procs = |
1000 |
Pattern.rewrite_term (Sign.tsig_of sg) (map decomp_simp' rules) procs; |
|
12783 | 1001 |
|
10413 | 1002 |
end; |
1003 |
||
11672 | 1004 |
structure BasicMetaSimplifier: BASIC_META_SIMPLIFIER = MetaSimplifier; |
1005 |
open BasicMetaSimplifier; |