author | chaieb |
Fri, 30 Jan 2009 13:24:23 +0000 | |
changeset 29696 | 477c7fcc0777 |
parent 29265 | 5b4247055bd7 |
child 30190 | 479806475f3c |
permissions | -rw-r--r-- |
29265
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents:
26939
diff
changeset
|
1 |
(* Title: FOLP/simp.ML |
0 | 2 |
Author: Tobias Nipkow |
3 |
Copyright 1993 University of Cambridge |
|
4 |
||
5 |
FOLP version of... |
|
6 |
||
7 |
Generic simplifier, suitable for most logics. (from Provers) |
|
8 |
||
9 |
This version allows instantiation of Vars in the subgoal, since the proof |
|
10 |
term must change. |
|
11 |
*) |
|
12 |
||
13 |
signature SIMP_DATA = |
|
14 |
sig |
|
15 |
val case_splits : (thm * string) list |
|
16 |
val dest_red : term -> term * term * term |
|
17 |
val mk_rew_rules : thm -> thm list |
|
18 |
val norm_thms : (thm*thm) list (* [(?x>>norm(?x), norm(?x)>>?x), ...] *) |
|
19 |
val red1 : thm (* ?P>>?Q ==> ?P ==> ?Q *) |
|
20 |
val red2 : thm (* ?P>>?Q ==> ?Q ==> ?P *) |
|
21 |
val refl_thms : thm list |
|
22 |
val subst_thms : thm list (* [ ?a>>?b ==> ?P(?a) ==> ?P(?b), ...] *) |
|
23 |
val trans_thms : thm list |
|
24 |
end; |
|
25 |
||
26 |
||
27 |
infix 4 addrews addcongs delrews delcongs setauto; |
|
28 |
||
29 |
signature SIMP = |
|
30 |
sig |
|
31 |
type simpset |
|
32 |
val empty_ss : simpset |
|
33 |
val addcongs : simpset * thm list -> simpset |
|
34 |
val addrews : simpset * thm list -> simpset |
|
35 |
val delcongs : simpset * thm list -> simpset |
|
36 |
val delrews : simpset * thm list -> simpset |
|
37 |
val dest_ss : simpset -> thm list * thm list |
|
38 |
val print_ss : simpset -> unit |
|
39 |
val setauto : simpset * (int -> tactic) -> simpset |
|
40 |
val ASM_SIMP_CASE_TAC : simpset -> int -> tactic |
|
41 |
val ASM_SIMP_TAC : simpset -> int -> tactic |
|
42 |
val CASE_TAC : simpset -> int -> tactic |
|
43 |
val SIMP_CASE2_TAC : simpset -> int -> tactic |
|
44 |
val SIMP_THM : simpset -> thm -> thm |
|
45 |
val SIMP_TAC : simpset -> int -> tactic |
|
46 |
val SIMP_CASE_TAC : simpset -> int -> tactic |
|
47 |
val mk_congs : theory -> string list -> thm list |
|
48 |
val mk_typed_congs : theory -> (string * string) list -> thm list |
|
49 |
(* temporarily disabled: |
|
50 |
val extract_free_congs : unit -> thm list |
|
51 |
*) |
|
52 |
val tracing : bool ref |
|
53 |
end; |
|
54 |
||
55 |
functor SimpFun (Simp_data: SIMP_DATA) : SIMP = |
|
56 |
struct |
|
57 |
||
19805 | 58 |
local open Simp_data in |
0 | 59 |
|
60 |
(*For taking apart reductions into left, right hand sides*) |
|
61 |
val lhs_of = #2 o dest_red; |
|
62 |
val rhs_of = #3 o dest_red; |
|
63 |
||
64 |
(*** Indexing and filtering of theorems ***) |
|
65 |
||
22360
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents:
21287
diff
changeset
|
66 |
fun eq_brl ((b1 : bool, th1), (b2, th2)) = b1 = b2 andalso Thm.eq_thm_prop (th1, th2); |
0 | 67 |
|
68 |
(*insert a thm in a discrimination net by its lhs*) |
|
69 |
fun lhs_insert_thm (th,net) = |
|
16800 | 70 |
Net.insert_term eq_brl (lhs_of (concl_of th), (false,th)) net |
0 | 71 |
handle Net.INSERT => net; |
72 |
||
73 |
(*match subgoal i against possible theorems in the net. |
|
74 |
Similar to match_from_nat_tac, but the net does not contain numbers; |
|
75 |
rewrite rules are not ordered.*) |
|
76 |
fun net_tac net = |
|
77 |
SUBGOAL(fn (prem,i) => |
|
19805 | 78 |
resolve_tac (Net.unify_term net (Logic.strip_assums_concl prem)) i); |
0 | 79 |
|
80 |
(*match subgoal i against possible theorems indexed by lhs in the net*) |
|
81 |
fun lhs_net_tac net = |
|
82 |
SUBGOAL(fn (prem,i) => |
|
1459 | 83 |
biresolve_tac (Net.unify_term net |
19805 | 84 |
(lhs_of (Logic.strip_assums_concl prem))) i); |
0 | 85 |
|
15570 | 86 |
fun nth_subgoal i thm = List.nth(prems_of thm,i-1); |
0 | 87 |
|
19805 | 88 |
fun goal_concl i thm = Logic.strip_assums_concl (nth_subgoal i thm); |
0 | 89 |
|
90 |
fun lhs_of_eq i thm = lhs_of(goal_concl i thm) |
|
91 |
and rhs_of_eq i thm = rhs_of(goal_concl i thm); |
|
92 |
||
93 |
fun var_lhs(thm,i) = |
|
94 |
let fun var(Var _) = true |
|
95 |
| var(Abs(_,_,t)) = var t |
|
96 |
| var(f$_) = var f |
|
97 |
| var _ = false; |
|
98 |
in var(lhs_of_eq i thm) end; |
|
99 |
||
100 |
fun contains_op opns = |
|
101 |
let fun contains(Const(s,_)) = s mem opns | |
|
102 |
contains(s$t) = contains s orelse contains t | |
|
103 |
contains(Abs(_,_,t)) = contains t | |
|
104 |
contains _ = false; |
|
105 |
in contains end; |
|
106 |
||
107 |
fun may_match(match_ops,i) = contains_op match_ops o lhs_of_eq i; |
|
108 |
||
109 |
val (normI_thms,normE_thms) = split_list norm_thms; |
|
110 |
||
111 |
(*Get the norm constants from norm_thms*) |
|
112 |
val norms = |
|
113 |
let fun norm thm = |
|
114 |
case lhs_of(concl_of thm) of |
|
1459 | 115 |
Const(n,_)$_ => n |
26928 | 116 |
| _ => (Display.prths normE_thms; error"No constant in lhs of a norm_thm") |
0 | 117 |
in map norm normE_thms end; |
118 |
||
119 |
fun lhs_is_NORM(thm,i) = case lhs_of_eq i thm of |
|
1459 | 120 |
Const(s,_)$_ => s mem norms | _ => false; |
0 | 121 |
|
122 |
val refl_tac = resolve_tac refl_thms; |
|
123 |
||
124 |
fun find_res thms thm = |
|
26928 | 125 |
let fun find [] = (Display.prths thms; error"Check Simp_Data") |
6969 | 126 |
| find(th::thms) = thm RS th handle THM _ => find thms |
0 | 127 |
in find thms end; |
128 |
||
129 |
val mk_trans = find_res trans_thms; |
|
130 |
||
131 |
fun mk_trans2 thm = |
|
132 |
let fun mk[] = error"Check transitivity" |
|
6969 | 133 |
| mk(t::ts) = (thm RSN (2,t)) handle THM _ => mk ts |
0 | 134 |
in mk trans_thms end; |
135 |
||
136 |
(*Applies tactic and returns the first resulting state, FAILS if none!*) |
|
4271
3a82492e70c5
changed Pure/Sequence interface -- isatool fixseq;
wenzelm
parents:
3537
diff
changeset
|
137 |
fun one_result(tac,thm) = case Seq.pull(tac thm) of |
15531 | 138 |
SOME(thm',_) => thm' |
139 |
| NONE => raise THM("Simplifier: could not continue", 0, [thm]); |
|
0 | 140 |
|
141 |
fun res1(thm,thms,i) = one_result(resolve_tac thms i,thm); |
|
142 |
||
143 |
||
144 |
(**** Adding "NORM" tags ****) |
|
145 |
||
146 |
(*get name of the constant from conclusion of a congruence rule*) |
|
147 |
fun cong_const cong = |
|
148 |
case head_of (lhs_of (concl_of cong)) of |
|
1459 | 149 |
Const(c,_) => c |
150 |
| _ => "" (*a placeholder distinct from const names*); |
|
0 | 151 |
|
152 |
(*true if the term is an atomic proposition (no ==> signs) *) |
|
19805 | 153 |
val atomic = null o Logic.strip_assums_hyp; |
0 | 154 |
|
155 |
(*ccs contains the names of the constants possessing congruence rules*) |
|
156 |
fun add_hidden_vars ccs = |
|
21078 | 157 |
let fun add_hvars tm hvars = case tm of |
29265
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents:
26939
diff
changeset
|
158 |
Abs(_,_,body) => OldTerm.add_term_vars(body,hvars) |
1459 | 159 |
| _$_ => let val (f,args) = strip_comb tm |
160 |
in case f of |
|
161 |
Const(c,T) => |
|
21078 | 162 |
if member (op =) ccs c |
163 |
then fold_rev add_hvars args hvars |
|
29265
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents:
26939
diff
changeset
|
164 |
else OldTerm.add_term_vars (tm, hvars) |
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents:
26939
diff
changeset
|
165 |
| _ => OldTerm.add_term_vars (tm, hvars) |
1459 | 166 |
end |
167 |
| _ => hvars; |
|
0 | 168 |
in add_hvars end; |
169 |
||
170 |
fun add_new_asm_vars new_asms = |
|
21078 | 171 |
let fun itf (tm, at) vars = |
29265
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents:
26939
diff
changeset
|
172 |
if at then vars else OldTerm.add_term_vars(tm,vars) |
1459 | 173 |
fun add_list(tm,al,vars) = let val (_,tml) = strip_comb tm |
174 |
in if length(tml)=length(al) |
|
21078 | 175 |
then fold_rev itf (tml ~~ al) vars |
1459 | 176 |
else vars |
177 |
end |
|
178 |
fun add_vars (tm,vars) = case tm of |
|
179 |
Abs (_,_,body) => add_vars(body,vars) |
|
180 |
| r$s => (case head_of tm of |
|
17325 | 181 |
Const(c,T) => (case AList.lookup (op =) new_asms c of |
15531 | 182 |
NONE => add_vars(r,add_vars(s,vars)) |
183 |
| SOME(al) => add_list(tm,al,vars)) |
|
1459 | 184 |
| _ => add_vars(r,add_vars(s,vars))) |
185 |
| _ => vars |
|
0 | 186 |
in add_vars end; |
187 |
||
188 |
||
189 |
fun add_norms(congs,ccs,new_asms) thm = |
|
190 |
let val thm' = mk_trans2 thm; |
|
191 |
(* thm': [?z -> l; Prems; r -> ?t] ==> ?z -> ?t *) |
|
192 |
val nops = nprems_of thm' |
|
193 |
val lhs = rhs_of_eq 1 thm' |
|
194 |
val rhs = lhs_of_eq nops thm' |
|
195 |
val asms = tl(rev(tl(prems_of thm'))) |
|
21078 | 196 |
val hvars = fold_rev (add_hidden_vars ccs) (lhs::rhs::asms) [] |
0 | 197 |
val hvars = add_new_asm_vars new_asms (rhs,hvars) |
21078 | 198 |
fun it_asms asm hvars = |
1459 | 199 |
if atomic asm then add_new_asm_vars new_asms (asm,hvars) |
29265
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents:
26939
diff
changeset
|
200 |
else OldTerm.add_term_frees(asm,hvars) |
21078 | 201 |
val hvars = fold_rev it_asms asms hvars |
0 | 202 |
val hvs = map (#1 o dest_Var) hvars |
203 |
val refl1_tac = refl_tac 1 |
|
3537 | 204 |
fun norm_step_tac st = st |> |
205 |
(case head_of(rhs_of_eq 1 st) of |
|
206 |
Var(ixn,_) => if ixn mem hvs then refl1_tac |
|
207 |
else resolve_tac normI_thms 1 ORELSE refl1_tac |
|
208 |
| Const _ => resolve_tac normI_thms 1 ORELSE |
|
209 |
resolve_tac congs 1 ORELSE refl1_tac |
|
210 |
| Free _ => resolve_tac congs 1 ORELSE refl1_tac |
|
211 |
| _ => refl1_tac) |
|
212 |
val add_norm_tac = DEPTH_FIRST (has_fewer_prems nops) norm_step_tac |
|
15531 | 213 |
val SOME(thm'',_) = Seq.pull(add_norm_tac thm') |
0 | 214 |
in thm'' end; |
215 |
||
216 |
fun add_norm_tags congs = |
|
217 |
let val ccs = map cong_const congs |
|
15570 | 218 |
val new_asms = List.filter (exists not o #2) |
1459 | 219 |
(ccs ~~ (map (map atomic o prems_of) congs)); |
0 | 220 |
in add_norms(congs,ccs,new_asms) end; |
221 |
||
222 |
fun normed_rews congs = |
|
19925
3f9341831812
eliminated freeze/varify in favour of Variable.import/export/trade;
wenzelm
parents:
19876
diff
changeset
|
223 |
let val add_norms = add_norm_tags congs in |
21287 | 224 |
fn thm => Variable.tradeT |
225 |
(K (map (add_norms o mk_trans) o maps mk_rew_rules)) (Variable.thm_context thm) [thm] |
|
0 | 226 |
end; |
227 |
||
1459 | 228 |
fun NORM norm_lhs_tac = EVERY'[rtac red2 , norm_lhs_tac, refl_tac]; |
0 | 229 |
|
230 |
val trans_norms = map mk_trans normE_thms; |
|
231 |
||
232 |
||
233 |
(* SIMPSET *) |
|
234 |
||
235 |
datatype simpset = |
|
1459 | 236 |
SS of {auto_tac: int -> tactic, |
237 |
congs: thm list, |
|
238 |
cong_net: thm Net.net, |
|
239 |
mk_simps: thm -> thm list, |
|
240 |
simps: (thm * thm list) list, |
|
241 |
simp_net: thm Net.net} |
|
0 | 242 |
|
243 |
val empty_ss = SS{auto_tac= K no_tac, congs=[], cong_net=Net.empty, |
|
1459 | 244 |
mk_simps=normed_rews[], simps=[], simp_net=Net.empty}; |
0 | 245 |
|
246 |
(** Insertion of congruences and rewrites **) |
|
247 |
||
248 |
(*insert a thm in a thm net*) |
|
21078 | 249 |
fun insert_thm_warn th net = |
22360
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents:
21287
diff
changeset
|
250 |
Net.insert_term Thm.eq_thm_prop (concl_of th, th) net |
0 | 251 |
handle Net.INSERT => |
26928 | 252 |
(writeln"\nDuplicate rewrite or congruence rule:"; Display.print_thm th; |
0 | 253 |
net); |
254 |
||
21078 | 255 |
val insert_thms = fold_rev insert_thm_warn; |
0 | 256 |
|
257 |
fun addrew(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net}, thm) = |
|
258 |
let val thms = mk_simps thm |
|
259 |
in SS{auto_tac=auto_tac,congs=congs, cong_net=cong_net, mk_simps=mk_simps, |
|
21078 | 260 |
simps = (thm,thms)::simps, simp_net = insert_thms thms simp_net} |
0 | 261 |
end; |
262 |
||
15570 | 263 |
val op addrews = Library.foldl addrew; |
0 | 264 |
|
265 |
fun op addcongs(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net}, thms) = |
|
266 |
let val congs' = thms @ congs; |
|
267 |
in SS{auto_tac=auto_tac, congs= congs', |
|
21078 | 268 |
cong_net= insert_thms (map mk_trans thms) cong_net, |
0 | 269 |
mk_simps= normed_rews congs', simps=simps, simp_net=simp_net} |
270 |
end; |
|
271 |
||
272 |
(** Deletion of congruences and rewrites **) |
|
273 |
||
274 |
(*delete a thm from a thm net*) |
|
21078 | 275 |
fun delete_thm_warn th net = |
22360
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents:
21287
diff
changeset
|
276 |
Net.delete_term Thm.eq_thm_prop (concl_of th, th) net |
0 | 277 |
handle Net.DELETE => |
26928 | 278 |
(writeln"\nNo such rewrite or congruence rule:"; Display.print_thm th; |
0 | 279 |
net); |
280 |
||
21078 | 281 |
val delete_thms = fold_rev delete_thm_warn; |
0 | 282 |
|
283 |
fun op delcongs(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net}, thms) = |
|
22360
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents:
21287
diff
changeset
|
284 |
let val congs' = fold (remove Thm.eq_thm_prop) thms congs |
0 | 285 |
in SS{auto_tac=auto_tac, congs= congs', |
21078 | 286 |
cong_net= delete_thms (map mk_trans thms) cong_net, |
0 | 287 |
mk_simps= normed_rews congs', simps=simps, simp_net=simp_net} |
288 |
end; |
|
289 |
||
290 |
fun delrew(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net}, thm) = |
|
291 |
let fun find((p as (th,ths))::ps',ps) = |
|
22360
26ead7ed4f4b
moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents:
21287
diff
changeset
|
292 |
if Thm.eq_thm_prop(thm,th) then (ths,ps@ps') else find(ps',p::ps) |
0 | 293 |
| find([],simps') = (writeln"\nNo such rewrite or congruence rule:"; |
26928 | 294 |
Display.print_thm thm; |
1459 | 295 |
([],simps')) |
0 | 296 |
val (thms,simps') = find(simps,[]) |
297 |
in SS{auto_tac=auto_tac, congs=congs, cong_net=cong_net, mk_simps=mk_simps, |
|
21078 | 298 |
simps = simps', simp_net = delete_thms thms simp_net } |
0 | 299 |
end; |
300 |
||
15570 | 301 |
val op delrews = Library.foldl delrew; |
0 | 302 |
|
303 |
||
304 |
fun op setauto(SS{congs,cong_net,mk_simps,simps,simp_net,...}, auto_tac) = |
|
305 |
SS{auto_tac=auto_tac, congs=congs, cong_net=cong_net, mk_simps=mk_simps, |
|
306 |
simps=simps, simp_net=simp_net}; |
|
307 |
||
308 |
||
309 |
(** Inspection of a simpset **) |
|
310 |
||
311 |
fun dest_ss(SS{congs,simps,...}) = (congs, map #1 simps); |
|
312 |
||
313 |
fun print_ss(SS{congs,simps,...}) = |
|
26928 | 314 |
(writeln"Congruences:"; Display.prths congs; |
315 |
writeln"Rewrite Rules:"; Display.prths (map #1 simps); ()); |
|
0 | 316 |
|
317 |
||
318 |
(* Rewriting with conditionals *) |
|
319 |
||
320 |
val (case_thms,case_consts) = split_list case_splits; |
|
321 |
val case_rews = map mk_trans case_thms; |
|
322 |
||
323 |
fun if_rewritable ifc i thm = |
|
324 |
let val tm = goal_concl i thm |
|
1459 | 325 |
fun nobound(Abs(_,_,tm),j,k) = nobound(tm,j,k+1) |
326 |
| nobound(s$t,j,k) = nobound(s,j,k) andalso nobound(t,j,k) |
|
327 |
| nobound(Bound n,j,k) = n < k orelse k+j <= n |
|
328 |
| nobound(_) = true; |
|
329 |
fun check_args(al,j) = forall (fn t => nobound(t,j,0)) al |
|
330 |
fun find_if(Abs(_,_,tm),j) = find_if(tm,j+1) |
|
331 |
| find_if(tm as s$t,j) = let val (f,al) = strip_comb tm in |
|
332 |
case f of Const(c,_) => if c=ifc then check_args(al,j) |
|
333 |
else find_if(s,j) orelse find_if(t,j) |
|
334 |
| _ => find_if(s,j) orelse find_if(t,j) end |
|
335 |
| find_if(_) = false; |
|
0 | 336 |
in find_if(tm,0) end; |
337 |
||
338 |
fun IF1_TAC cong_tac i = |
|
1512 | 339 |
let fun seq_try (ifth::ifths,ifc::ifcs) thm = |
340 |
(COND (if_rewritable ifc i) (DETERM(rtac ifth i)) |
|
341 |
(seq_try(ifths,ifcs))) thm |
|
342 |
| seq_try([],_) thm = no_tac thm |
|
343 |
and try_rew thm = (seq_try(case_rews,case_consts) ORELSE one_subt) thm |
|
1459 | 344 |
and one_subt thm = |
345 |
let val test = has_fewer_prems (nprems_of thm + 1) |
|
1512 | 346 |
fun loop thm = |
347 |
COND test no_tac |
|
348 |
((try_rew THEN DEPTH_FIRST test (refl_tac i)) |
|
349 |
ORELSE (refl_tac i THEN loop)) thm |
|
350 |
in (cong_tac THEN loop) thm end |
|
351 |
in COND (may_match(case_consts,i)) try_rew no_tac end; |
|
0 | 352 |
|
353 |
fun CASE_TAC (SS{cong_net,...}) i = |
|
354 |
let val cong_tac = net_tac cong_net i |
|
355 |
in NORM (IF1_TAC cong_tac) i end; |
|
356 |
||
357 |
(* Rewriting Automaton *) |
|
358 |
||
359 |
datatype cntrl = STOP | MK_EQ | ASMS of int | SIMP_LHS | REW | REFL | TRUE |
|
1459 | 360 |
| PROVE | POP_CS | POP_ARTR | IF; |
22578 | 361 |
|
0 | 362 |
fun simp_refl([],_,ss) = ss |
363 |
| simp_refl(a'::ns,a,ss) = if a'=a then simp_refl(ns,a,SIMP_LHS::REFL::ss) |
|
1459 | 364 |
else simp_refl(ns,a,ASMS(a)::SIMP_LHS::REFL::POP_ARTR::ss); |
0 | 365 |
|
366 |
(** Tracing **) |
|
367 |
||
368 |
val tracing = ref false; |
|
369 |
||
370 |
(*Replace parameters by Free variables in P*) |
|
371 |
fun variants_abs ([],P) = P |
|
372 |
| variants_abs ((a,T)::aTs, P) = |
|
20194 | 373 |
variants_abs (aTs, #2 (Syntax.variant_abs(a,T,P))); |
0 | 374 |
|
375 |
(*Select subgoal i from proof state; substitute parameters, for printing*) |
|
376 |
fun prepare_goal i st = |
|
377 |
let val subgi = nth_subgoal i st |
|
19805 | 378 |
val params = rev (Logic.strip_params subgi) |
379 |
in variants_abs (params, Logic.strip_assums_concl subgi) end; |
|
0 | 380 |
|
381 |
(*print lhs of conclusion of subgoal i*) |
|
382 |
fun pr_goal_lhs i st = |
|
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
383 |
writeln (Syntax.string_of_term_global (Thm.theory_of_thm st) |
1459 | 384 |
(lhs_of (prepare_goal i st))); |
0 | 385 |
|
386 |
(*print conclusion of subgoal i*) |
|
387 |
fun pr_goal_concl i st = |
|
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26928
diff
changeset
|
388 |
writeln (Syntax.string_of_term_global (Thm.theory_of_thm st) (prepare_goal i st)) |
0 | 389 |
|
390 |
(*print subgoals i to j (inclusive)*) |
|
391 |
fun pr_goals (i,j) st = |
|
392 |
if i>j then () |
|
393 |
else (pr_goal_concl i st; pr_goals (i+1,j) st); |
|
394 |
||
395 |
(*Print rewrite for tracing; i=subgoal#, n=number of new subgoals, |
|
396 |
thm=old state, thm'=new state *) |
|
397 |
fun pr_rew (i,n,thm,thm',not_asms) = |
|
398 |
if !tracing |
|
399 |
then (if not_asms then () else writeln"Assumption used in"; |
|
400 |
pr_goal_lhs i thm; writeln"->"; pr_goal_lhs (i+n) thm'; |
|
1459 | 401 |
if n>0 then (writeln"Conditions:"; pr_goals (i, i+n-1) thm') |
0 | 402 |
else (); |
403 |
writeln"" ) |
|
404 |
else (); |
|
405 |
||
406 |
(* Skip the first n hyps of a goal, and return the rest in generalized form *) |
|
407 |
fun strip_varify(Const("==>", _) $ H $ B, n, vs) = |
|
1459 | 408 |
if n=0 then subst_bounds(vs,H)::strip_varify(B,0,vs) |
409 |
else strip_varify(B,n-1,vs) |
|
0 | 410 |
| strip_varify(Const("all",_)$Abs(_,T,t), n, vs) = |
1459 | 411 |
strip_varify(t,n,Var(("?",length vs),T)::vs) |
0 | 412 |
| strip_varify _ = []; |
413 |
||
414 |
fun execute(ss,if_fl,auto_tac,cong_tac,net,i,thm) = let |
|
415 |
||
416 |
fun simp_lhs(thm,ss,anet,ats,cs) = |
|
417 |
if var_lhs(thm,i) then (ss,thm,anet,ats,cs) else |
|
418 |
if lhs_is_NORM(thm,i) then (ss, res1(thm,trans_norms,i), anet,ats,cs) |
|
4271
3a82492e70c5
changed Pure/Sequence interface -- isatool fixseq;
wenzelm
parents:
3537
diff
changeset
|
419 |
else case Seq.pull(cong_tac i thm) of |
15531 | 420 |
SOME(thm',_) => |
1459 | 421 |
let val ps = prems_of thm and ps' = prems_of thm'; |
422 |
val n = length(ps')-length(ps); |
|
19805 | 423 |
val a = length(Logic.strip_assums_hyp(List.nth(ps,i-1))) |
424 |
val l = map (fn p => length(Logic.strip_assums_hyp(p))) |
|
15570 | 425 |
(Library.take(n,Library.drop(i-1,ps'))); |
1459 | 426 |
in (simp_refl(rev(l),a,REW::ss),thm',anet,ats,cs) end |
15531 | 427 |
| NONE => (REW::ss,thm,anet,ats,cs); |
0 | 428 |
|
429 |
(*NB: the "Adding rewrites:" trace will look strange because assumptions |
|
430 |
are represented by rules, generalized over their parameters*) |
|
431 |
fun add_asms(ss,thm,a,anet,ats,cs) = |
|
432 |
let val As = strip_varify(nth_subgoal i thm, a, []); |
|
22596 | 433 |
val thms = map (trivial o cterm_of(Thm.theory_of_thm thm)) As; |
15570 | 434 |
val new_rws = List.concat(map mk_rew_rules thms); |
435 |
val rwrls = map mk_trans (List.concat(map mk_rew_rules thms)); |
|
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
436 |
val anet' = foldr lhs_insert_thm anet rwrls |
0 | 437 |
in if !tracing andalso not(null new_rws) |
26928 | 438 |
then (writeln"Adding rewrites:"; Display.prths new_rws; ()) |
1459 | 439 |
else (); |
440 |
(ss,thm,anet',anet::ats,cs) |
|
0 | 441 |
end; |
442 |
||
4271
3a82492e70c5
changed Pure/Sequence interface -- isatool fixseq;
wenzelm
parents:
3537
diff
changeset
|
443 |
fun rew(seq,thm,ss,anet,ats,cs, more) = case Seq.pull seq of |
15531 | 444 |
SOME(thm',seq') => |
1459 | 445 |
let val n = (nprems_of thm') - (nprems_of thm) |
446 |
in pr_rew(i,n,thm,thm',more); |
|
447 |
if n=0 then (SIMP_LHS::ss, thm', anet, ats, cs) |
|
448 |
else ((replicate n PROVE) @ (POP_CS::SIMP_LHS::ss), |
|
449 |
thm', anet, ats, (ss,thm,anet,ats,seq',more)::cs) |
|
450 |
end |
|
15531 | 451 |
| NONE => if more |
1512 | 452 |
then rew((lhs_net_tac anet i THEN assume_tac i) thm, |
1459 | 453 |
thm,ss,anet,ats,cs,false) |
454 |
else (ss,thm,anet,ats,cs); |
|
0 | 455 |
|
456 |
fun try_true(thm,ss,anet,ats,cs) = |
|
4271
3a82492e70c5
changed Pure/Sequence interface -- isatool fixseq;
wenzelm
parents:
3537
diff
changeset
|
457 |
case Seq.pull(auto_tac i thm) of |
15531 | 458 |
SOME(thm',_) => (ss,thm',anet,ats,cs) |
459 |
| NONE => let val (ss0,thm0,anet0,ats0,seq,more)::cs0 = cs |
|
1459 | 460 |
in if !tracing |
461 |
then (writeln"*** Failed to prove precondition. Normal form:"; |
|
462 |
pr_goal_concl i thm; writeln"") |
|
463 |
else (); |
|
464 |
rew(seq,thm0,ss0,anet0,ats0,cs0,more) |
|
465 |
end; |
|
0 | 466 |
|
467 |
fun if_exp(thm,ss,anet,ats,cs) = |
|
4271
3a82492e70c5
changed Pure/Sequence interface -- isatool fixseq;
wenzelm
parents:
3537
diff
changeset
|
468 |
case Seq.pull (IF1_TAC (cong_tac i) i thm) of |
15531 | 469 |
SOME(thm',_) => (SIMP_LHS::IF::ss,thm',anet,ats,cs) |
470 |
| NONE => (ss,thm,anet,ats,cs); |
|
0 | 471 |
|
472 |
fun step(s::ss, thm, anet, ats, cs) = case s of |
|
1459 | 473 |
MK_EQ => (ss, res1(thm,[red2],i), anet, ats, cs) |
474 |
| ASMS(a) => add_asms(ss,thm,a,anet,ats,cs) |
|
475 |
| SIMP_LHS => simp_lhs(thm,ss,anet,ats,cs) |
|
1512 | 476 |
| REW => rew(net_tac net i thm,thm,ss,anet,ats,cs,true) |
1459 | 477 |
| REFL => (ss, res1(thm,refl_thms,i), anet, ats, cs) |
478 |
| TRUE => try_true(res1(thm,refl_thms,i),ss,anet,ats,cs) |
|
479 |
| PROVE => (if if_fl then MK_EQ::SIMP_LHS::IF::TRUE::ss |
|
480 |
else MK_EQ::SIMP_LHS::TRUE::ss, thm, anet, ats, cs) |
|
481 |
| POP_ARTR => (ss,thm,hd ats,tl ats,cs) |
|
482 |
| POP_CS => (ss,thm,anet,ats,tl cs) |
|
483 |
| IF => if_exp(thm,ss,anet,ats,cs); |
|
0 | 484 |
|
485 |
fun exec(state as (s::ss, thm, _, _, _)) = |
|
1459 | 486 |
if s=STOP then thm else exec(step(state)); |
0 | 487 |
|
488 |
in exec(ss, thm, Net.empty, [], []) end; |
|
489 |
||
490 |
||
491 |
fun EXEC_TAC(ss,fl) (SS{auto_tac,cong_net,simp_net,...}) = |
|
492 |
let val cong_tac = net_tac cong_net |
|
1512 | 493 |
in fn i => |
494 |
(fn thm => |
|
4271
3a82492e70c5
changed Pure/Sequence interface -- isatool fixseq;
wenzelm
parents:
3537
diff
changeset
|
495 |
if i <= 0 orelse nprems_of thm < i then Seq.empty |
3a82492e70c5
changed Pure/Sequence interface -- isatool fixseq;
wenzelm
parents:
3537
diff
changeset
|
496 |
else Seq.single(execute(ss,fl,auto_tac,cong_tac,simp_net,i,thm))) |
1512 | 497 |
THEN TRY(auto_tac i) |
0 | 498 |
end; |
499 |
||
500 |
val SIMP_TAC = EXEC_TAC([MK_EQ,SIMP_LHS,REFL,STOP],false); |
|
501 |
val SIMP_CASE_TAC = EXEC_TAC([MK_EQ,SIMP_LHS,IF,REFL,STOP],false); |
|
502 |
||
503 |
val ASM_SIMP_TAC = EXEC_TAC([ASMS(0),MK_EQ,SIMP_LHS,REFL,STOP],false); |
|
504 |
val ASM_SIMP_CASE_TAC = EXEC_TAC([ASMS(0),MK_EQ,SIMP_LHS,IF,REFL,STOP],false); |
|
505 |
||
506 |
val SIMP_CASE2_TAC = EXEC_TAC([MK_EQ,SIMP_LHS,IF,REFL,STOP],true); |
|
507 |
||
508 |
fun REWRITE (ss,fl) (SS{auto_tac,cong_net,simp_net,...}) = |
|
509 |
let val cong_tac = net_tac cong_net |
|
510 |
in fn thm => let val state = thm RSN (2,red1) |
|
1459 | 511 |
in execute(ss,fl,auto_tac,cong_tac,simp_net,1,state) end |
0 | 512 |
end; |
513 |
||
514 |
val SIMP_THM = REWRITE ([ASMS(0),SIMP_LHS,IF,REFL,STOP],false); |
|
515 |
||
516 |
||
517 |
(* Compute Congruence rules for individual constants using the substition |
|
518 |
rules *) |
|
519 |
||
520 |
val subst_thms = map standard subst_thms; |
|
521 |
||
522 |
||
523 |
fun exp_app(0,t) = t |
|
524 |
| exp_app(i,t) = exp_app(i-1,t $ Bound (i-1)); |
|
525 |
||
526 |
fun exp_abs(Type("fun",[T1,T2]),t,i) = |
|
1459 | 527 |
Abs("x"^string_of_int i,T1,exp_abs(T2,t,i+1)) |
0 | 528 |
| exp_abs(T,t,i) = exp_app(i,t); |
529 |
||
530 |
fun eta_Var(ixn,T) = exp_abs(T,Var(ixn,T),0); |
|
531 |
||
532 |
||
533 |
fun Pinst(f,fT,(eq,eqT),k,i,T,yik,Ts) = |
|
534 |
let fun xn_list(x,n) = |
|
1459 | 535 |
let val ixs = map (fn i => (x^(radixstring(26,"a",i)),0)) (0 upto n); |
15570 | 536 |
in ListPair.map eta_Var (ixs, Library.take(n+1,Ts)) end |
0 | 537 |
val lhs = list_comb(f,xn_list("X",k-1)) |
538 |
val rhs = list_comb(f,xn_list("X",i-1) @ [Bound 0] @ yik) |
|
539 |
in Abs("", T, Const(eq,[fT,fT]--->eqT) $ lhs $ rhs) end; |
|
540 |
||
16931 | 541 |
fun find_subst sg T = |
0 | 542 |
let fun find (thm::thms) = |
1459 | 543 |
let val (Const(_,cT), va, vb) = dest_red(hd(prems_of thm)); |
29265
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents:
26939
diff
changeset
|
544 |
val [P] = OldTerm.add_term_vars(concl_of thm,[]) \\ [va,vb] |
1459 | 545 |
val eqT::_ = binder_types cT |
16931 | 546 |
in if Sign.typ_instance sg (T,eqT) then SOME(thm,va,vb,P) |
1459 | 547 |
else find thms |
548 |
end |
|
15531 | 549 |
| find [] = NONE |
0 | 550 |
in find subst_thms end; |
551 |
||
552 |
fun mk_cong sg (f,aTs,rT) (refl,eq) = |
|
16931 | 553 |
let val k = length aTs; |
0 | 554 |
fun ri((subst,va as Var(_,Ta),vb as Var(_,Tb),P),i,si,T,yik) = |
1459 | 555 |
let val ca = cterm_of sg va |
556 |
and cx = cterm_of sg (eta_Var(("X"^si,0),T)) |
|
557 |
val cb = cterm_of sg vb |
|
558 |
and cy = cterm_of sg (eta_Var(("Y"^si,0),T)) |
|
559 |
val cP = cterm_of sg P |
|
560 |
and cp = cterm_of sg (Pinst(f,rT,eq,k,i,T,yik,aTs)) |
|
561 |
in cterm_instantiate [(ca,cx),(cb,cy),(cP,cp)] subst end; |
|
0 | 562 |
fun mk(c,T::Ts,i,yik) = |
1459 | 563 |
let val si = radixstring(26,"a",i) |
16931 | 564 |
in case find_subst sg T of |
15531 | 565 |
NONE => mk(c,Ts,i-1,eta_Var(("X"^si,0),T)::yik) |
566 |
| SOME s => let val c' = c RSN (2,ri(s,i,si,T,yik)) |
|
1459 | 567 |
in mk(c',Ts,i-1,eta_Var(("Y"^si,0),T)::yik) end |
568 |
end |
|
0 | 569 |
| mk(c,[],_,_) = c; |
570 |
in mk(refl,rev aTs,k-1,[]) end; |
|
571 |
||
572 |
fun mk_cong_type sg (f,T) = |
|
573 |
let val (aTs,rT) = strip_type T; |
|
574 |
fun find_refl(r::rs) = |
|
1459 | 575 |
let val (Const(eq,eqT),_,_) = dest_red(concl_of r) |
16931 | 576 |
in if Sign.typ_instance sg (rT, hd(binder_types eqT)) |
15531 | 577 |
then SOME(r,(eq,body_type eqT)) else find_refl rs |
1459 | 578 |
end |
15531 | 579 |
| find_refl([]) = NONE; |
0 | 580 |
in case find_refl refl_thms of |
15531 | 581 |
NONE => [] | SOME(refl) => [mk_cong sg (f,aTs,rT) refl] |
0 | 582 |
end; |
583 |
||
584 |
fun mk_cong_thy thy f = |
|
22578 | 585 |
let val T = case Sign.const_type thy f of |
15531 | 586 |
NONE => error(f^" not declared") | SOME(T) => T; |
16876 | 587 |
val T' = Logic.incr_tvar 9 T; |
22578 | 588 |
in mk_cong_type thy (Const(f,T'),T') end; |
0 | 589 |
|
15570 | 590 |
fun mk_congs thy = List.concat o map (mk_cong_thy thy); |
0 | 591 |
|
592 |
fun mk_typed_congs thy = |
|
22675
acf10be7dcca
cleaned/simplified Sign.read_typ, Thm.read_cterm etc.;
wenzelm
parents:
22596
diff
changeset
|
593 |
let |
acf10be7dcca
cleaned/simplified Sign.read_typ, Thm.read_cterm etc.;
wenzelm
parents:
22596
diff
changeset
|
594 |
fun readfT(f,s) = |
acf10be7dcca
cleaned/simplified Sign.read_typ, Thm.read_cterm etc.;
wenzelm
parents:
22596
diff
changeset
|
595 |
let |
24707 | 596 |
val T = Logic.incr_tvar 9 (Syntax.read_typ_global thy s); |
22675
acf10be7dcca
cleaned/simplified Sign.read_typ, Thm.read_cterm etc.;
wenzelm
parents:
22596
diff
changeset
|
597 |
val t = case Sign.const_type thy f of |
acf10be7dcca
cleaned/simplified Sign.read_typ, Thm.read_cterm etc.;
wenzelm
parents:
22596
diff
changeset
|
598 |
SOME(_) => Const(f,T) | NONE => Free(f,T) |
acf10be7dcca
cleaned/simplified Sign.read_typ, Thm.read_cterm etc.;
wenzelm
parents:
22596
diff
changeset
|
599 |
in (t,T) end |
22578 | 600 |
in List.concat o map (mk_cong_type thy o readfT) end; |
0 | 601 |
|
22675
acf10be7dcca
cleaned/simplified Sign.read_typ, Thm.read_cterm etc.;
wenzelm
parents:
22596
diff
changeset
|
602 |
end; |
acf10be7dcca
cleaned/simplified Sign.read_typ, Thm.read_cterm etc.;
wenzelm
parents:
22596
diff
changeset
|
603 |
end; |