author  paulson 
Tue, 20 May 1997 11:49:57 +0200  
changeset 3245  241838c01caf 
parent 3191  14bd6e5985f1 
child 3272  c93f54759539 
permissions  rwrr 
2112  1 
structure FastRules : Rules_sig = 
2 
struct 

3 

4 
open Utils; 

5 
open Mask; 

6 
infix 7 >; 

7 

8 
structure USyntax = USyntax; 

9 
structure S = USyntax; 

10 
structure U = Utils; 

11 
structure D = Dcterm; 

12 

13 

14 
fun RULES_ERR{func,mesg} = Utils.ERR{module = "FastRules",func=func,mesg=mesg}; 

15 

16 

17 
fun cconcl thm = D.drop_prop(#prop(crep_thm thm)); 

18 
fun chyps thm = map D.drop_prop(#hyps(crep_thm thm)); 

19 

20 
fun dest_thm thm = 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

21 
let val {prop,hyps,...} = rep_thm thm 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

22 
in (map HOLogic.dest_Trueprop hyps, HOLogic.dest_Trueprop prop) 
2112  23 
end; 
24 

25 

26 

27 
(* Inference rules *) 

28 

29 
(* 

30 
* Equality (one step) 

31 
**) 

32 
fun REFL tm = Thm.reflexive tm RS meta_eq_to_obj_eq; 

33 
fun SYM thm = thm RS sym; 

34 

35 
fun ALPHA thm ctm1 = 

36 
let val ctm2 = cprop_of thm 

37 
val ctm2_eq = reflexive ctm2 

38 
val ctm1_eq = reflexive ctm1 

39 
in equal_elim (transitive ctm2_eq ctm1_eq) thm 

40 
end; 

41 

42 

43 
(* 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

44 
* typ instantiation 
2112  45 
**) 
46 
fun INST_TYPE blist thm = 

47 
let val {sign,...} = rep_thm thm 

48 
val blist' = map (fn (TVar(idx,_) > B) => (idx, ctyp_of sign B)) blist 

49 
in Thm.instantiate (blist',[]) thm 

50 
end 

51 
handle _ => raise RULES_ERR{func = "INST_TYPE", mesg = ""}; 

52 

53 

54 
(* 

55 
* Implication and the assumption list 

56 
* 

57 
* Assumptions get stuck on the metalanguage assumption list. Implications 

58 
* are in the object language, so discharging an assumption "A" from theorem 

59 
* "B" results in something that looks like "A > B". 

60 
**) 

61 
fun ASSUME ctm = Thm.assume (D.mk_prop ctm); 

62 

63 

64 
(* 

65 
* Implication in TFL is >. Metalanguage implication (==>) is only used 

66 
* in the implementation of some of the inference rules below. 

67 
**) 

68 
fun MP th1 th2 = th2 RS (th1 RS mp); 

69 

70 
fun DISCH tm thm = Thm.implies_intr (D.mk_prop tm) thm COMP impI; 

71 

72 
fun DISCH_ALL thm = Utils.itlist DISCH (#hyps (crep_thm thm)) thm; 

73 

74 

75 
fun FILTER_DISCH_ALL P thm = 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

76 
let fun check tm = U.holds P (#t(rep_cterm tm)) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

77 
in foldr (fn (tm,th) => if (check tm) then DISCH tm th else th) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

78 
(chyps thm, thm) 
2112  79 
end; 
80 

81 
(* freezeT expensive! *) 

82 
fun UNDISCH thm = 

83 
let val tm = D.mk_prop(#1(D.dest_imp(cconcl (freezeT thm)))) 

84 
in implies_elim (thm RS mp) (ASSUME tm) 

85 
end 

86 
handle _ => raise RULES_ERR{func = "UNDISCH", mesg = ""}; 

87 

88 
fun PROVE_HYP ath bth = MP (DISCH (cconcl ath) bth) ath; 

89 

90 
local val [p1,p2] = goal HOL.thy "(A>B) ==> (B > C) ==> (A>C)" 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

91 
val dummy = by (rtac impI 1) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

92 
val dummy = by (rtac (p2 RS mp) 1) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

93 
val dummy = by (rtac (p1 RS mp) 1) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

94 
val dummy = by (assume_tac 1) 
2112  95 
val imp_trans = result() 
96 
in 

97 
fun IMP_TRANS th1 th2 = th2 RS (th1 RS imp_trans) 

98 
end; 

99 

100 
(* 

101 
* Conjunction 

102 
**) 

103 
fun CONJUNCT1 thm = (thm RS conjunct1) 

104 
fun CONJUNCT2 thm = (thm RS conjunct2); 

105 
fun CONJUNCTS th = (CONJUNCTS (CONJUNCT1 th) @ CONJUNCTS (CONJUNCT2 th)) 

106 
handle _ => [th]; 

107 

108 
fun LIST_CONJ [] = raise RULES_ERR{func = "LIST_CONJ", mesg = "empty list"} 

109 
 LIST_CONJ [th] = th 

110 
 LIST_CONJ (th::rst) = MP(MP(conjI COMP (impI RS impI)) th) (LIST_CONJ rst); 

111 

112 

113 
(* 

114 
* Disjunction 

115 
**) 

116 
local val {prop,sign,...} = rep_thm disjI1 

117 
val [P,Q] = term_vars prop 

118 
val disj1 = forall_intr (cterm_of sign Q) disjI1 

119 
in 

120 
fun DISJ1 thm tm = thm RS (forall_elim (D.drop_prop tm) disj1) 

121 
end; 

122 

123 
local val {prop,sign,...} = rep_thm disjI2 

124 
val [P,Q] = term_vars prop 

125 
val disj2 = forall_intr (cterm_of sign P) disjI2 

126 
in 

127 
fun DISJ2 tm thm = thm RS (forall_elim (D.drop_prop tm) disj2) 

128 
end; 

129 

130 

131 
(* 

132 
* 

133 
* A1  M1, ..., An  Mn 

134 
*  

135 
* [A1  M1 \/ ... \/ Mn, ..., An  M1 \/ ... \/ Mn] 

136 
* 

137 
**) 

138 

139 

140 
fun EVEN_ORS thms = 

141 
let fun blue ldisjs [] _ = [] 

142 
 blue ldisjs (th::rst) rdisjs = 

143 
let val tail = tl rdisjs 

144 
val rdisj_tl = D.list_mk_disj tail 

145 
in itlist DISJ2 ldisjs (DISJ1 th rdisj_tl) 

146 
:: blue (ldisjs@[cconcl th]) rst tail 

147 
end handle _ => [itlist DISJ2 ldisjs th] 

148 
in 

149 
blue [] thms (map cconcl thms) 

150 
end; 

151 

152 

153 
(* 

154 
* 

155 
* A  P \/ Q B,P  R C,Q  R 

156 
*  

157 
* A U B U C  R 

158 
* 

159 
**) 

160 
local val [p1,p2,p3] = goal HOL.thy "(P  Q) ==> (P > R) ==> (Q > R) ==> R" 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

161 
val dummy = by (rtac (p1 RS disjE) 1) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

162 
val dummy = by (rtac (p2 RS mp) 1) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

163 
val dummy = by (assume_tac 1) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

164 
val dummy = by (rtac (p3 RS mp) 1) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

165 
val dummy = by (assume_tac 1) 
2112  166 
val tfl_exE = result() 
167 
in 

168 
fun DISJ_CASES th1 th2 th3 = 

169 
let val c = D.drop_prop(cconcl th1) 

170 
val (disj1,disj2) = D.dest_disj c 

171 
val th2' = DISCH disj1 th2 

172 
val th3' = DISCH disj2 th3 

173 
in 

174 
th3' RS (th2' RS (th1 RS tfl_exE)) 

175 
end 

176 
end; 

177 

178 

179 
(* 

180 
* 

181 
*  A1 \/ ... \/ An [A1  M, ..., An  M] 

182 
*  

183 
*  M 

184 
* 

185 
* Note. The list of theorems may be all jumbled up, so we have to 

186 
* first organize it to align with the first argument (the disjunctive 

187 
* theorem). 

188 
**) 

189 

190 
fun organize eq = (* a bit slow  analogous to insertion sort *) 

191 
let fun extract a alist = 

192 
let fun ex (_,[]) = raise RULES_ERR{func = "organize", 

193 
mesg = "not a permutation.1"} 

194 
 ex(left,h::t) = if (eq h a) then (h,rev left@t) else ex(h::left,t) 

195 
in ex ([],alist) 

196 
end 

197 
fun place [] [] = [] 

198 
 place (a::rst) alist = 

199 
let val (item,next) = extract a alist 

200 
in item::place rst next 

201 
end 

202 
 place _ _ = raise RULES_ERR{func = "organize", 

203 
mesg = "not a permutation.2"} 

204 
in place 

205 
end; 

206 
(* freezeT expensive! *) 

207 
fun DISJ_CASESL disjth thl = 

208 
let val c = cconcl disjth 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

209 
fun eq th atm = exists (fn t => HOLogic.dest_Trueprop t 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

210 
aconv term_of atm) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

211 
(#hyps(rep_thm th)) 
2112  212 
val tml = D.strip_disj c 
213 
fun DL th [] = raise RULES_ERR{func="DISJ_CASESL",mesg="no cases"} 

214 
 DL th [th1] = PROVE_HYP th th1 

215 
 DL th [th1,th2] = DISJ_CASES th th1 th2 

216 
 DL th (th1::rst) = 

217 
let val tm = #2(D.dest_disj(D.drop_prop(cconcl th))) 

218 
in DISJ_CASES th th1 (DL (ASSUME tm) rst) end 

219 
in DL (freezeT disjth) (organize eq tml thl) 

220 
end; 

221 

222 

223 
(* 

224 
* Universals 

225 
**) 

226 
local (* this is fragile *) 

227 
val {prop,sign,...} = rep_thm spec 

228 
val x = hd (tl (term_vars prop)) 

229 
val (TVar (indx,_)) = type_of x 

230 
val gspec = forall_intr (cterm_of sign x) spec 

231 
in 

232 
fun SPEC tm thm = 

233 
let val {sign,T,...} = rep_cterm tm 

234 
val gspec' = instantiate([(indx,ctyp_of sign T)],[]) gspec 

235 
in thm RS (forall_elim tm gspec') 

236 
end 

237 
end; 

238 

239 
fun SPEC_ALL thm = rev_itlist SPEC (#1(D.strip_forall(cconcl thm))) thm; 

240 

241 
val ISPEC = SPEC 

242 
val ISPECL = rev_itlist ISPEC; 

243 

244 
(* Not optimized! Too complicated. *) 

245 
local val {prop,sign,...} = rep_thm allI 

246 
val [P] = add_term_vars (prop, []) 

247 
fun cty_theta s = map (fn (i,ty) => (i, ctyp_of s ty)) 

248 
fun ctm_theta s = map (fn (i,tm2) => 

249 
let val ctm2 = cterm_of s tm2 

250 
in (cterm_of s (Var(i,#T(rep_cterm ctm2))), ctm2) 

251 
end) 

252 
fun certify s (ty_theta,tm_theta) = (cty_theta s ty_theta, 

253 
ctm_theta s tm_theta) 

254 
in 

255 
fun GEN v th = 

256 
let val gth = forall_intr v th 

257 
val {prop=Const("all",_)$Abs(x,ty,rst),sign,...} = rep_thm gth 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

258 
val P' = Abs(x,ty, HOLogic.dest_Trueprop rst) (* get rid of trueprop *) 
2112  259 
val tsig = #tsig(Sign.rep_sg sign) 
260 
val theta = Pattern.match tsig (P,P') 

261 
val allI2 = instantiate (certify sign theta) allI 

262 
val thm = implies_elim allI2 gth 

263 
val {prop = tp $ (A $ Abs(_,_,M)),sign,...} = rep_thm thm 

264 
val prop' = tp $ (A $ Abs(x,ty,M)) 

265 
in ALPHA thm (cterm_of sign prop') 

266 
end 

267 
end; 

268 

269 
val GENL = itlist GEN; 

270 

271 
fun GEN_ALL thm = 

272 
let val {prop,sign,...} = rep_thm thm 

273 
val tycheck = cterm_of sign 

274 
val vlist = map tycheck (add_term_vars (prop, [])) 

275 
in GENL vlist thm 

276 
end; 

277 

278 

279 
local fun string_of(s,_) = s 

280 
in 

281 
fun freeze th = 

282 
let val fth = freezeT th 

283 
val {prop,sign,...} = rep_thm fth 

284 
fun mk_inst (Var(v,T)) = 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

285 
(cterm_of sign (Var(v,T)), 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

286 
cterm_of sign (Free(string_of v, T))) 
2112  287 
val insts = map mk_inst (term_vars prop) 
288 
in instantiate ([],insts) fth 

289 
end 

290 
end; 

291 

292 
fun MATCH_MP th1 th2 = 

293 
if (D.is_forall (D.drop_prop(cconcl th1))) 

294 
then MATCH_MP (th1 RS spec) th2 

295 
else MP th1 th2; 

296 

297 

298 
(* 

299 
* Existentials 

300 
**) 

301 

302 

303 

304 
(* 

305 
* Existential elimination 

306 
* 

307 
* A1  ?x.t[x] , A2, "t[v]"  t' 

308 
*  (variable v occurs nowhere) 

309 
* A1 u A2  t' 

310 
* 

311 
**) 

312 

313 
local val [p1,p2] = goal HOL.thy "(? x. P x) ==> (!x. P x > Q) ==> Q" 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

314 
val dummy = by (rtac (p1 RS exE) 1) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

315 
val dummy = by (rtac ((p2 RS allE) RS mp) 1) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

316 
val dummy = by (assume_tac 2) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

317 
val dummy = by (assume_tac 1) 
2112  318 
val choose_thm = result() 
319 
in 

320 
fun CHOOSE(fvar,exth) fact = 

321 
let val lam = #2(dest_comb(D.drop_prop(cconcl exth))) 

322 
val redex = capply lam fvar 

323 
val {sign,t,...} = rep_cterm redex 

324 
val residue = cterm_of sign (S.beta_conv t) 

325 
in GEN fvar (DISCH residue fact) RS (exth RS choose_thm) 

326 
end 

327 
end; 

328 

329 

330 
local val {prop,sign,...} = rep_thm exI 

331 
val [P,x] = term_vars prop 

332 
in 

333 
fun EXISTS (template,witness) thm = 

334 
let val {prop,sign,...} = rep_thm thm 

335 
val P' = cterm_of sign P 

336 
val x' = cterm_of sign x 

337 
val abstr = #2(dest_comb template) 

338 
in 

339 
thm RS (cterm_instantiate[(P',abstr), (x',witness)] exI) 

340 
end 

341 
end; 

342 

343 
(* 

344 
* 

345 
* A  M 

346 
*  [v_1,...,v_n] 

347 
* A  ?v1...v_n. M 

348 
* 

349 
**) 

350 

351 
fun EXISTL vlist th = 

352 
U.itlist (fn v => fn thm => EXISTS(D.mk_exists(v,cconcl thm), v) thm) 

353 
vlist th; 

354 

355 

356 
(* 

357 
* 

358 
* A  M[x_1,...,x_n] 

359 
*  [(x > y)_1,...,(x > y)_n] 

360 
* A  ?y_1...y_n. M 

361 
* 

362 
**) 

363 
(* Could be improved, but needs "subst" for certified terms *) 

364 

365 
fun IT_EXISTS blist th = 

366 
let val {sign,...} = rep_thm th 

367 
val tych = cterm_of sign 

368 
val detype = #t o rep_cterm 

369 
val blist' = map (fn (x>y) => (detype x > detype y)) blist 

370 
fun ?v M = cterm_of sign (S.mk_exists{Bvar=v,Body = M}) 

371 

372 
in 

373 
U.itlist (fn (b as (r1 > r2)) => fn thm => 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

374 
EXISTS(?r2(S.subst[b] (HOLogic.dest_Trueprop(#prop(rep_thm thm)))), tych r1) 
2112  375 
thm) 
376 
blist' th 

377 
end; 

378 

379 
(* 

380 
* Faster version, that fails for some as yet unknown reason 

381 
* fun IT_EXISTS blist th = 

382 
* let val {sign,...} = rep_thm th 

383 
* val tych = cterm_of sign 

384 
* fun detype (x > y) = ((#t o rep_cterm) x > (#t o rep_cterm) y) 

385 
* in 

386 
* fold (fn (b as (r1>r2), thm) => 

387 
* EXISTS(D.mk_exists(r2, tych(S.subst[detype b](#t(rep_cterm(cconcl thm))))), 

388 
* r1) thm) blist th 

389 
* end; 

390 
**) 

391 

392 
(* 

393 
* Rewriting 

394 
**) 

395 

396 
fun SUBS thl = 

397 
rewrite_rule (map (fn th => (th RS eq_reflection) handle _ => th) thl); 

398 

399 
val simplify = rewrite_rule; 

400 

401 
local fun rew_conv mss = rewrite_cterm (true,false) mss (K(K None)) 

402 
in 

403 
fun simpl_conv thl ctm = 

404 
rew_conv (Thm.mss_of (#simps(rep_ss HOL_ss)@thl)) ctm 

405 
RS meta_eq_to_obj_eq 

406 
end; 

407 

408 
local fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]) 

409 
in 

410 
val RIGHT_ASSOC = rewrite_rule [prover"((ab)c) = (a(bc))" RS eq_reflection] 

411 
val ASM = refl RS iffD1 

412 
end; 

413 

414 

415 

416 

417 
(* 

418 
* TERMINATION CONDITION EXTRACTION 

419 
**) 

420 

421 

422 
(* Object language quantifier, i.e., "!" *) 

423 
fun Forall v M = S.mk_forall{Bvar=v, Body=M}; 

424 

425 

426 
(* Fragile: it's a cong if it is not "R y x ==> cut f R x y = f y" *) 

427 
fun is_cong thm = 

428 
let val {prop, ...} = rep_thm thm 

429 
in case prop 

430 
of (Const("==>",_)$(Const("Trueprop",_)$ _) $ 

431 
(Const("==",_) $ (Const ("cut",_) $ f $ R $ a $ x) $ _)) => false 

432 
 _ => true 

433 
end; 

434 

435 

436 

437 
fun dest_equal(Const ("==",_) $ 

438 
(Const ("Trueprop",_) $ lhs) 

439 
$ (Const ("Trueprop",_) $ rhs)) = {lhs=lhs, rhs=rhs} 

440 
 dest_equal(Const ("==",_) $ lhs $ rhs) = {lhs=lhs, rhs=rhs} 

441 
 dest_equal tm = S.dest_eq tm; 

442 

443 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

444 
fun get_rhs tm = #rhs(dest_equal (HOLogic.dest_Trueprop tm)); 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

445 
fun get_lhs tm = #lhs(dest_equal (HOLogic.dest_Trueprop tm)); 
2112  446 

447 
fun variants FV vlist = 

448 
rev(#1(U.rev_itlist (fn v => fn (V,W) => 

449 
let val v' = S.variant W v 

450 
in (v'::V, v'::W) end) 

451 
vlist ([],FV))); 

452 

453 

454 
fun dest_all(Const("all",_) $ (a as Abs _)) = S.dest_abs a 

455 
 dest_all _ = raise RULES_ERR{func = "dest_all", mesg = "not a !!"}; 

456 

457 
val is_all = Utils.can dest_all; 

458 

459 
fun strip_all fm = 

460 
if (is_all fm) 

461 
then let val {Bvar,Body} = dest_all fm 

462 
val (bvs,core) = strip_all Body 

463 
in ((Bvar::bvs), core) 

464 
end 

465 
else ([],fm); 

466 

467 
fun break_all(Const("all",_) $ Abs (_,_,body)) = body 

468 
 break_all _ = raise RULES_ERR{func = "break_all", mesg = "not a !!"}; 

469 

470 
fun list_break_all(Const("all",_) $ Abs (s,ty,body)) = 

471 
let val (L,core) = list_break_all body 

472 
in ((s,ty)::L, core) 

473 
end 

474 
 list_break_all tm = ([],tm); 

475 

476 
(* 

477 
* Rename a term of the form 

478 
* 

479 
* !!x1 ...xn. x1=M1 ==> ... ==> xn=Mn 

480 
* ==> ((%v1...vn. Q) x1 ... xn = g x1 ... xn. 

481 
* to one of 

482 
* 

483 
* !!v1 ... vn. v1=M1 ==> ... ==> vn=Mn 

484 
* ==> ((%v1...vn. Q) v1 ... vn = g v1 ... vn. 

485 
* 

486 
* This prevents name problems in extraction, and helps the result to read 

487 
* better. There is a problem with varstructs, since they can introduce more 

488 
* than n variables, and some extra reasoning needs to be done. 

489 
**) 

490 

491 
fun get ([],_,L) = rev L 

492 
 get (ant::rst,n,L) = 

493 
case (list_break_all ant) 

494 
of ([],_) => get (rst, n+1,L) 

495 
 (vlist,body) => 

496 
let val eq = Logic.strip_imp_concl body 

497 
val (f,args) = S.strip_comb (get_lhs eq) 

498 
val (vstrl,_) = S.strip_abs f 

499 
val names = map (#Name o S.dest_var) 

500 
(variants (S.free_vars body) vstrl) 

501 
in get (rst, n+1, (names,n)::L) 

502 
end handle _ => get (rst, n+1, L); 

503 

504 
(* Note: rename_params_rule counts from 1, not 0 *) 

505 
fun rename thm = 

506 
let val {prop,sign,...} = rep_thm thm 

507 
val tych = cterm_of sign 

508 
val ants = Logic.strip_imp_prems prop 

509 
val news = get (ants,1,[]) 

510 
in 

511 
U.rev_itlist rename_params_rule news thm 

512 
end; 

513 

514 

515 
(* 

516 
* Betaconversion to the rhs of an equation (taken from hol90/drule.sml) 

517 
**) 

518 

519 
fun list_beta_conv tm = 

520 
let fun rbeta th = transitive th (beta_conversion(#2(D.dest_eq(cconcl th)))) 

521 
fun iter [] = reflexive tm 

522 
 iter (v::rst) = rbeta (combination(iter rst) (reflexive v)) 

523 
in iter end; 

524 

525 

526 
(* 

527 
* Trace information for the rewriter 

528 
**) 

529 
val term_ref = ref[] : term list ref 

530 
val mss_ref = ref [] : meta_simpset list ref; 

531 
val thm_ref = ref [] : thm list ref; 

532 
val tracing = ref false; 

533 

3191  534 
fun say s = if !tracing then (output(std_out,s); flush_out std_out) else (); 
2112  535 

536 
fun print_thms s L = 

537 
(say s; 

538 
map (fn th => say (string_of_thm th ^"\n")) L; 

539 
say"\n"); 

540 

541 
fun print_cterms s L = 

542 
(say s; 

543 
map (fn th => say (string_of_cterm th ^"\n")) L; 

544 
say"\n"); 

545 

546 
(* 

547 
* General abstraction handlers, should probably go in USyntax. 

548 
**) 

549 
fun mk_aabs(vstr,body) = S.mk_abs{Bvar=vstr,Body=body} 

550 
handle _ => S.mk_pabs{varstruct = vstr, body = body}; 

551 

552 
fun list_mk_aabs (vstrl,tm) = 

553 
U.itlist (fn vstr => fn tm => mk_aabs(vstr,tm)) vstrl tm; 

554 

555 
fun dest_aabs tm = 

556 
let val {Bvar,Body} = S.dest_abs tm 

557 
in (Bvar,Body) 

558 
end handle _ => let val {varstruct,body} = S.dest_pabs tm 

559 
in (varstruct,body) 

560 
end; 

561 

562 
fun strip_aabs tm = 

563 
let val (vstr,body) = dest_aabs tm 

564 
val (bvs, core) = strip_aabs body 

565 
in (vstr::bvs, core) 

566 
end 

567 
handle _ => ([],tm); 

568 

569 
fun dest_combn tm 0 = (tm,[]) 

570 
 dest_combn tm n = 

571 
let val {Rator,Rand} = S.dest_comb tm 

572 
val (f,rands) = dest_combn Rator (n1) 

573 
in (f,Rand::rands) 

574 
end; 

575 

576 

577 

578 

579 
local fun dest_pair M = let val {fst,snd} = S.dest_pair M in (fst,snd) end 

580 
fun mk_fst tm = 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

581 
let val ty as Type("*", [fty,sty]) = type_of tm 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

582 
in Const ("fst", ty > fty) $ tm end 
2112  583 
fun mk_snd tm = 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

584 
let val ty as Type("*", [fty,sty]) = type_of tm 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

585 
in Const ("snd", ty > sty) $ tm end 
2112  586 
in 
587 
fun XFILL tych x vstruct = 

588 
let fun traverse p xocc L = 

589 
if (S.is_var p) 

590 
then tych xocc::L 

591 
else let val (p1,p2) = dest_pair p 

592 
in traverse p1 (mk_fst xocc) (traverse p2 (mk_snd xocc) L) 

593 
end 

594 
in 

595 
traverse vstruct x [] 

596 
end end; 

597 

598 
(* 

599 
* Replace a free tuple (vstr) by a universally quantified variable (a). 

600 
* Note that the notion of "freeness" for a tuple is different than for a 

601 
* variable: if variables in the tuple also occur in any other place than 

602 
* an occurrences of the tuple, they aren't "free" (which is thus probably 

603 
* the wrong word to use). 

604 
**) 

605 

606 
fun VSTRUCT_ELIM tych a vstr th = 

607 
let val L = S.free_vars_lr vstr 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

608 
val bind1 = tych (HOLogic.mk_Trueprop (HOLogic.mk_eq(a,vstr))) 
2112  609 
val thm1 = implies_intr bind1 (SUBS [SYM(assume bind1)] th) 
610 
val thm2 = forall_intr_list (map tych L) thm1 

611 
val thm3 = forall_elim_list (XFILL tych a vstr) thm2 

612 
in refl RS 

613 
rewrite_rule[symmetric (surjective_pairing RS eq_reflection)] thm3 

614 
end; 

615 

616 
fun PGEN tych a vstr th = 

617 
let val a1 = tych a 

618 
val vstr1 = tych vstr 

619 
in 

620 
forall_intr a1 

621 
(if (S.is_var vstr) 

622 
then cterm_instantiate [(vstr1,a1)] th 

623 
else VSTRUCT_ELIM tych a vstr th) 

624 
end; 

625 

626 

627 
(* 

628 
* Takes apart a paired betaredex, looking like "(\(x,y).N) vstr", into 

629 
* 

630 
* (([x,y],N),vstr) 

631 
**) 

632 
fun dest_pbeta_redex M n = 

633 
let val (f,args) = dest_combn M n 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

634 
val dummy = dest_aabs f 
2112  635 
in (strip_aabs f,args) 
636 
end; 

637 

638 
fun pbeta_redex M n = U.can (U.C dest_pbeta_redex n) M; 

639 

640 
fun dest_impl tm = 

641 
let val ants = Logic.strip_imp_prems tm 

642 
val eq = Logic.strip_imp_concl tm 

643 
in (ants,get_lhs eq) 

644 
end; 

645 

646 
val pbeta_reduce = simpl_conv [split RS eq_reflection]; 

647 
val restricted = U.can(S.find_term 

648 
(U.holds(fn c => (#Name(S.dest_const c)="cut")))) 

649 

650 
fun CONTEXT_REWRITE_RULE(func,R){thms=[cut_lemma],congs,th} = 

651 
let val tc_list = ref[]: term list ref 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

652 
val dummy = term_ref := [] 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

653 
val dummy = thm_ref := [] 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

654 
val dummy = mss_ref := [] 
2112  655 
val cut_lemma' = (cut_lemma RS mp) RS eq_reflection 
656 
fun prover mss thm = 

657 
let fun cong_prover mss thm = 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

658 
let val dummy = say "cong_prover:\n" 
2112  659 
val cntxt = prems_of_mss mss 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

660 
val dummy = print_thms "cntxt:\n" cntxt 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

661 
val dummy = say "cong rule:\n" 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

662 
val dummy = say (string_of_thm thm^"\n") 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

663 
val dummy = thm_ref := (thm :: !thm_ref) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

664 
val dummy = mss_ref := (mss :: !mss_ref) 
2112  665 
(* Unquantified eliminate *) 
666 
fun uq_eliminate (thm,imp,sign) = 

667 
let val tych = cterm_of sign 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

668 
val dummy = print_cterms "To eliminate:\n" [tych imp] 
2112  669 
val ants = map tych (Logic.strip_imp_prems imp) 
670 
val eq = Logic.strip_imp_concl imp 

671 
val lhs = tych(get_lhs eq) 

672 
val mss' = add_prems(mss, map ASSUME ants) 

673 
val lhs_eq_lhs1 = rewrite_cterm(false,true)mss' prover lhs 

674 
handle _ => reflexive lhs 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

675 
val dummy = print_thms "proven:\n" [lhs_eq_lhs1] 
2112  676 
val lhs_eq_lhs2 = implies_intr_list ants lhs_eq_lhs1 
677 
val lhs_eeq_lhs2 = lhs_eq_lhs2 RS meta_eq_to_obj_eq 

678 
in 

679 
lhs_eeq_lhs2 COMP thm 

680 
end 

681 
fun pq_eliminate (thm,sign,vlist,imp_body,lhs_eq) = 

682 
let val ((vstrl,_),args) = dest_pbeta_redex lhs_eq(length vlist) 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

683 
val dummy = assert (forall (op aconv) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

684 
(ListPair.zip (vlist, args))) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

685 
"assertion failed in CONTEXT_REWRITE_RULE" 
2112  686 
(* val fbvs1 = variants (S.free_vars imp) fbvs *) 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

687 
val imp_body1 = S.subst (map (op>) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

688 
(ListPair.zip (args, vstrl))) 
2112  689 
imp_body 
690 
val tych = cterm_of sign 

691 
val ants1 = map tych (Logic.strip_imp_prems imp_body1) 

692 
val eq1 = Logic.strip_imp_concl imp_body1 

693 
val Q = get_lhs eq1 

694 
val QeqQ1 = pbeta_reduce (tych Q) 

695 
val Q1 = #2(D.dest_eq(cconcl QeqQ1)) 

696 
val mss' = add_prems(mss, map ASSUME ants1) 

697 
val Q1eeqQ2 = rewrite_cterm (false,true) mss' prover Q1 

698 
handle _ => reflexive Q1 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

699 
val Q2 = get_rhs(HOLogic.dest_Trueprop(#prop(rep_thm Q1eeqQ2))) 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

700 
val Q3 = tych(list_comb(list_mk_aabs(vstrl,Q2),vstrl)) 
2112  701 
val Q2eeqQ3 = symmetric(pbeta_reduce Q3 RS eq_reflection) 
702 
val thA = transitive(QeqQ1 RS eq_reflection) Q1eeqQ2 

703 
val QeeqQ3 = transitive thA Q2eeqQ3 handle _ => 

704 
((Q2eeqQ3 RS meta_eq_to_obj_eq) 

705 
RS ((thA RS meta_eq_to_obj_eq) RS trans)) 

706 
RS eq_reflection 

707 
val impth = implies_intr_list ants1 QeeqQ3 

708 
val impth1 = impth RS meta_eq_to_obj_eq 

709 
(* Need to abstract *) 

710 
val ant_th = U.itlist2 (PGEN tych) args vstrl impth1 

711 
in ant_th COMP thm 

712 
end 

713 
fun q_eliminate (thm,imp,sign) = 

714 
let val (vlist,imp_body) = strip_all imp 

715 
val (ants,Q) = dest_impl imp_body 

716 
in if (pbeta_redex Q) (length vlist) 

717 
then pq_eliminate (thm,sign,vlist,imp_body,Q) 

718 
else 

719 
let val tych = cterm_of sign 

720 
val ants1 = map tych ants 

721 
val mss' = add_prems(mss, map ASSUME ants1) 

722 
val Q_eeq_Q1 = rewrite_cterm(false,true) mss' 

723 
prover (tych Q) 

724 
handle _ => reflexive (tych Q) 

725 
val lhs_eeq_lhs2 = implies_intr_list ants1 Q_eeq_Q1 

726 
val lhs_eq_lhs2 = lhs_eeq_lhs2 RS meta_eq_to_obj_eq 

727 
val ant_th = forall_intr_list(map tych vlist)lhs_eq_lhs2 

728 
in 

729 
ant_th COMP thm 

730 
end end 

731 

732 
fun eliminate thm = 

733 
case (rep_thm thm) 

734 
of {prop = (Const("==>",_) $ imp $ _), sign, ...} => 

735 
eliminate 

736 
(if not(is_all imp) 

737 
then uq_eliminate (thm,imp,sign) 

738 
else q_eliminate (thm,imp,sign)) 

739 
(* Assume that the leading constant is ==, *) 

740 
 _ => thm (* if it is not a ==> *) 

741 
in Some(eliminate (rename thm)) 

742 
end handle _ => None 

743 

744 
fun restrict_prover mss thm = 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

745 
let val dummy = say "restrict_prover:\n" 
2112  746 
val cntxt = rev(prems_of_mss mss) 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

747 
val dummy = print_thms "cntxt:\n" cntxt 
2112  748 
val {prop = Const("==>",_) $ (Const("Trueprop",_) $ A) $ _, 
749 
sign,...} = rep_thm thm 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

750 
fun genl tm = let val vlist = U.set_diff (curry(op aconv)) 
2112  751 
(add_term_frees(tm,[])) [func,R] 
752 
in U.itlist Forall vlist tm 

753 
end 

754 
(* 

755 
* This actually isn't quite right, since it will think that 

756 
* notfully applied occs. of "f" in the context mean that the 

757 
* current call is nested. The real solution is to pass in a 

758 
* term "f v1..vn" which is a pattern that any full application 

759 
* of "f" will match. 

760 
**) 

761 
val func_name = #Name(S.dest_const func handle _ => 

762 
S.dest_var func) 

763 
fun is_func tm = (#Name(S.dest_const tm handle _ => 

764 
S.dest_var tm) = func_name) 

765 
handle _ => false 

766 
val nested = U.can(S.find_term is_func) 

767 
val rcontext = rev cntxt 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

768 
val cncl = HOLogic.dest_Trueprop o #prop o rep_thm 
2112  769 
val antl = case rcontext of [] => [] 
770 
 _ => [S.list_mk_conj(map cncl rcontext)] 

771 
val TC = genl(S.list_mk_imp(antl, A)) 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

772 
val dummy = print_cterms "func:\n" [cterm_of sign func] 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

773 
val dummy = print_cterms "TC:\n" [cterm_of sign (HOLogic.mk_Trueprop TC)] 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

774 
val dummy = tc_list := (TC :: !tc_list) 
2112  775 
val nestedp = nested TC 
3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

776 
val dummy = if nestedp then say "nested\n" else say "not_nested\n" 
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

777 
val dummy = term_ref := ([func,TC]@(!term_ref)) 
2112  778 
val th' = if nestedp then raise RULES_ERR{func = "solver", 
779 
mesg = "nested function"} 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

780 
else let val cTC = cterm_of sign (HOLogic.mk_Trueprop TC) 
2112  781 
in case rcontext of 
782 
[] => SPEC_ALL(ASSUME cTC) 

783 
 _ => MP (SPEC_ALL (ASSUME cTC)) 

784 
(LIST_CONJ rcontext) 

785 
end 

786 
val th'' = th' RS thm 

787 
in Some (th'') 

788 
end handle _ => None 

789 
in 

790 
(if (is_cong thm) then cong_prover else restrict_prover) mss thm 

791 
end 

792 
val ctm = cprop_of th 

793 
val th1 = rewrite_cterm(false,true) (add_congs(mss_of [cut_lemma'], congs)) 

794 
prover ctm 

795 
val th2 = equal_elim th1 th 

796 
in 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

797 
(th2, filter (not o restricted) (!tc_list)) 
2112  798 
end; 
799 

800 

801 

802 
fun prove (tm,tac) = 

803 
let val {t,sign,...} = rep_cterm tm 

3245
241838c01caf
Removal of redundant code (unused or already present in Isabelle.
paulson
parents:
3191
diff
changeset

804 
val ptm = cterm_of sign(HOLogic.mk_Trueprop t) 
2112  805 
in 
806 
freeze(prove_goalw_cterm [] ptm (fn _ => [tac])) 

807 
end; 

808 

809 

810 
end; (* Rules *) 