author  paulson 
Thu, 22 May 1997 15:13:16 +0200  
changeset 3302  404fe31fd8d2 
parent 3272  c93f54759539 
child 3332  3921ebbd9cf0 
permissions  rwrr 
3302  1 
(* Title: TFL/rules 
2 
ID: $Id$ 

3 
Author: Konrad Slind, Cambridge University Computer Laboratory 

4 
Copyright 1997 University of Cambridge 

5 

6 
Emulation of HOL inference rules for TFL 

7 
*) 

8 

2112  9 
structure FastRules : Rules_sig = 
10 
struct 

11 

12 
open Utils; 

13 
open Mask; 

14 
infix 7 >; 

15 

16 
structure USyntax = USyntax; 

17 
structure S = USyntax; 

18 
structure U = Utils; 

19 
structure D = Dcterm; 

20 

21 

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

23 

24 

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

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

27 

28 
fun dest_thm thm = 

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

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

30 
in (map HOLogic.dest_Trueprop hyps, HOLogic.dest_Trueprop prop) 
2112  31 
end; 
32 

33 

34 

35 
(* Inference rules *) 

36 

37 
(* 

38 
* Equality (one step) 

39 
**) 

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

41 
fun SYM thm = thm RS sym; 

42 

43 
fun ALPHA thm ctm1 = 

44 
let val ctm2 = cprop_of thm 

45 
val ctm2_eq = reflexive ctm2 

46 
val ctm1_eq = reflexive ctm1 

47 
in equal_elim (transitive ctm2_eq ctm1_eq) thm 

48 
end; 

49 

50 

51 
(* 

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

52 
* typ instantiation 
2112  53 
**) 
54 
fun INST_TYPE blist thm = 

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

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

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

58 
end 

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

60 

61 

62 
(* 

63 
* Implication and the assumption list 

64 
* 

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

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

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

68 
**) 

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

70 

71 

72 
(* 

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

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

75 
**) 

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

77 

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

79 

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

81 

82 

83 
fun FILTER_DISCH_ALL P thm = 

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

84 
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

85 
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

86 
(chyps thm, thm) 
2112  87 
end; 
88 

89 
(* freezeT expensive! *) 

90 
fun UNDISCH thm = 

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

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

93 
end 

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

95 

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

97 

98 
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

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

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

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

102 
val dummy = by (assume_tac 1) 
2112  103 
val imp_trans = result() 
104 
in 

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

106 
end; 

107 

108 
(* 

109 
* Conjunction 

110 
**) 

111 
fun CONJUNCT1 thm = (thm RS conjunct1) 

112 
fun CONJUNCT2 thm = (thm RS conjunct2); 

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

114 
handle _ => [th]; 

115 

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

117 
 LIST_CONJ [th] = th 

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

119 

120 

121 
(* 

122 
* Disjunction 

123 
**) 

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

125 
val [P,Q] = term_vars prop 

126 
val disj1 = forall_intr (cterm_of sign Q) disjI1 

127 
in 

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

129 
end; 

130 

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

132 
val [P,Q] = term_vars prop 

133 
val disj2 = forall_intr (cterm_of sign P) disjI2 

134 
in 

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

136 
end; 

137 

138 

139 
(* 

140 
* 

141 
* A1  M1, ..., An  Mn 

142 
*  

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

144 
* 

145 
**) 

146 

147 

148 
fun EVEN_ORS thms = 

149 
let fun blue ldisjs [] _ = [] 

150 
 blue ldisjs (th::rst) rdisjs = 

151 
let val tail = tl rdisjs 

152 
val rdisj_tl = D.list_mk_disj tail 

153 
in itlist DISJ2 ldisjs (DISJ1 th rdisj_tl) 

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

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

156 
in 

157 
blue [] thms (map cconcl thms) 

158 
end; 

159 

160 

161 
(* 

162 
* 

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

164 
*  

165 
* A U B U C  R 

166 
* 

167 
**) 

168 
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

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

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

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

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

173 
val dummy = by (assume_tac 1) 
2112  174 
val tfl_exE = result() 
175 
in 

176 
fun DISJ_CASES th1 th2 th3 = 

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

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

179 
val th2' = DISCH disj1 th2 

180 
val th3' = DISCH disj2 th3 

181 
in 

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

183 
end 

184 
end; 

185 

186 

187 
(* 

188 
* 

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

190 
*  

191 
*  M 

192 
* 

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

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

195 
* theorem). 

196 
**) 

197 

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

199 
let fun extract a alist = 

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

201 
mesg = "not a permutation.1"} 

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

203 
in ex ([],alist) 

204 
end 

205 
fun place [] [] = [] 

206 
 place (a::rst) alist = 

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

208 
in item::place rst next 

209 
end 

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

211 
mesg = "not a permutation.2"} 

212 
in place 

213 
end; 

214 
(* freezeT expensive! *) 

215 
fun DISJ_CASESL disjth thl = 

216 
let val c = cconcl disjth 

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

217 
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

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

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

222 
 DL th [th1] = PROVE_HYP th th1 

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

224 
 DL th (th1::rst) = 

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

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

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

228 
end; 

229 

230 

231 
(* 

232 
* Universals 

233 
**) 

234 
local (* this is fragile *) 

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

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

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

238 
val gspec = forall_intr (cterm_of sign x) spec 

239 
in 

240 
fun SPEC tm thm = 

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

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

243 
in thm RS (forall_elim tm gspec') 

244 
end 

245 
end; 

246 

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

248 

249 
val ISPEC = SPEC 

250 
val ISPECL = rev_itlist ISPEC; 

251 

252 
(* Not optimized! Too complicated. *) 

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

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

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

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

257 
let val ctm2 = cterm_of s tm2 

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

259 
end) 

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

261 
ctm_theta s tm_theta) 

262 
in 

263 
fun GEN v th = 

264 
let val gth = forall_intr v th 

265 
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

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

269 
val allI2 = instantiate (certify sign theta) allI 

270 
val thm = implies_elim allI2 gth 

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

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

273 
in ALPHA thm (cterm_of sign prop') 

274 
end 

275 
end; 

276 

277 
val GENL = itlist GEN; 

278 

279 
fun GEN_ALL thm = 

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

281 
val tycheck = cterm_of sign 

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

283 
in GENL vlist thm 

284 
end; 

285 

286 

287 
local fun string_of(s,_) = s 

288 
in 

289 
fun freeze th = 

290 
let val fth = freezeT th 

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

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

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

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

294 
cterm_of sign (Free(string_of v, T))) 
2112  295 
val insts = map mk_inst (term_vars prop) 
296 
in instantiate ([],insts) fth 

297 
end 

298 
end; 

299 

300 
fun MATCH_MP th1 th2 = 

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

302 
then MATCH_MP (th1 RS spec) th2 

303 
else MP th1 th2; 

304 

305 

306 
(* 

307 
* Existentials 

308 
**) 

309 

310 

311 

312 
(* 

313 
* Existential elimination 

314 
* 

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

316 
*  (variable v occurs nowhere) 

317 
* A1 u A2  t' 

318 
* 

319 
**) 

320 

321 
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

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

323 
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

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

325 
val dummy = by (assume_tac 1) 
2112  326 
val choose_thm = result() 
327 
in 

328 
fun CHOOSE(fvar,exth) fact = 

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

330 
val redex = capply lam fvar 

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

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

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

334 
end 

335 
end; 

336 

337 

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

339 
val [P,x] = term_vars prop 

340 
in 

341 
fun EXISTS (template,witness) thm = 

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

343 
val P' = cterm_of sign P 

344 
val x' = cterm_of sign x 

345 
val abstr = #2(dest_comb template) 

346 
in 

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

348 
end 

349 
end; 

350 

351 
(* 

352 
* 

353 
* A  M 

354 
*  [v_1,...,v_n] 

355 
* A  ?v1...v_n. M 

356 
* 

357 
**) 

358 

359 
fun EXISTL vlist th = 

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

361 
vlist th; 

362 

363 

364 
(* 

365 
* 

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

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

368 
* A  ?y_1...y_n. M 

369 
* 

370 
**) 

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

372 

373 
fun IT_EXISTS blist th = 

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

375 
val tych = cterm_of sign 

376 
val detype = #t o rep_cterm 

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

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

379 

380 
in 

381 
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

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

385 
end; 

386 

387 
(* 

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

389 
* fun IT_EXISTS blist th = 

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

391 
* val tych = cterm_of sign 

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

393 
* in 

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

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

396 
* r1) thm) blist th 

397 
* end; 

398 
**) 

399 

400 
(* 

401 
* Rewriting 

402 
**) 

403 

404 
fun SUBS thl = 

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

406 

407 
val simplify = rewrite_rule; 

408 

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

410 
in 

411 
fun simpl_conv thl ctm = 

3272
c93f54759539
Basis library input/output primitives; \!simpset instead of HOL_ss
paulson
parents:
3245
diff
changeset

412 
rew_conv (Thm.mss_of (#simps(rep_ss (!simpset))@thl)) ctm 
2112  413 
RS meta_eq_to_obj_eq 
414 
end; 

415 

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

417 
in 

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

419 
val ASM = refl RS iffD1 

420 
end; 

421 

422 

423 

424 

425 
(* 

426 
* TERMINATION CONDITION EXTRACTION 

427 
**) 

428 

429 

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

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

432 

433 

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

435 
fun is_cong thm = 

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

437 
in case prop 

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

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

440 
 _ => true 

441 
end; 

442 

443 

444 

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

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

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

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

449 
 dest_equal tm = S.dest_eq tm; 

450 

451 

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

452 
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

453 
fun get_lhs tm = #lhs(dest_equal (HOLogic.dest_Trueprop tm)); 
2112  454 

455 
fun variants FV vlist = 

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

457 
let val v' = S.variant W v 

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

459 
vlist ([],FV))); 

460 

461 

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

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

464 

465 
val is_all = Utils.can dest_all; 

466 

467 
fun strip_all fm = 

468 
if (is_all fm) 

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

470 
val (bvs,core) = strip_all Body 

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

472 
end 

473 
else ([],fm); 

474 

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

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

477 

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

479 
let val (L,core) = list_break_all body 

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

481 
end 

482 
 list_break_all tm = ([],tm); 

483 

484 
(* 

485 
* Rename a term of the form 

486 
* 

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

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

489 
* to one of 

490 
* 

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

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

493 
* 

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

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

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

497 
**) 

498 

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

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

501 
case (list_break_all ant) 

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

503 
 (vlist,body) => 

504 
let val eq = Logic.strip_imp_concl body 

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

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

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

508 
(variants (S.free_vars body) vstrl) 

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

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

511 

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

513 
fun rename thm = 

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

515 
val tych = cterm_of sign 

516 
val ants = Logic.strip_imp_prems prop 

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

518 
in 

519 
U.rev_itlist rename_params_rule news thm 

520 
end; 

521 

522 

523 
(* 

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

525 
**) 

526 

527 
fun list_beta_conv tm = 

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

529 
fun iter [] = reflexive tm 

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

531 
in iter end; 

532 

533 

534 
(* 

535 
* Trace information for the rewriter 

536 
**) 

537 
val term_ref = ref[] : term list ref 

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

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

540 
val tracing = ref false; 

541 

3272
c93f54759539
Basis library input/output primitives; \!simpset instead of HOL_ss
paulson
parents:
3245
diff
changeset

542 
fun say s = if !tracing then TextIO.output (TextIO.stdOut, s) else (); 
2112  543 

544 
fun print_thms s L = 

545 
(say s; 

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

547 
say"\n"); 

548 

549 
fun print_cterms s L = 

550 
(say s; 

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

552 
say"\n"); 

553 

554 
(* 

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

556 
**) 

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

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

559 

560 
fun list_mk_aabs (vstrl,tm) = 

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

562 

563 
fun dest_aabs tm = 

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

565 
in (Bvar,Body) 

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

567 
in (varstruct,body) 

568 
end; 

569 

570 
fun strip_aabs tm = 

571 
let val (vstr,body) = dest_aabs tm 

572 
val (bvs, core) = strip_aabs body 

573 
in (vstr::bvs, core) 

574 
end 

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

576 

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

578 
 dest_combn tm n = 

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

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

581 
in (f,Rand::rands) 

582 
end; 

583 

584 

585 

586 

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

588 
fun mk_fst tm = 

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

589 
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

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

592 
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

593 
in Const ("snd", ty > sty) $ tm end 
2112  594 
in 
595 
fun XFILL tych x vstruct = 

596 
let fun traverse p xocc L = 

597 
if (S.is_var p) 

598 
then tych xocc::L 

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

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

601 
end 

602 
in 

603 
traverse vstruct x [] 

604 
end end; 

605 

606 
(* 

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

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

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

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

611 
* the wrong word to use). 

612 
**) 

613 

614 
fun VSTRUCT_ELIM tych a vstr th = 

615 
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

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

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

620 
in refl RS 

621 
rewrite_rule[symmetric (surjective_pairing RS eq_reflection)] thm3 

622 
end; 

623 

624 
fun PGEN tych a vstr th = 

625 
let val a1 = tych a 

626 
val vstr1 = tych vstr 

627 
in 

628 
forall_intr a1 

629 
(if (S.is_var vstr) 

630 
then cterm_instantiate [(vstr1,a1)] th 

631 
else VSTRUCT_ELIM tych a vstr th) 

632 
end; 

633 

634 

635 
(* 

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

637 
* 

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

639 
**) 

640 
fun dest_pbeta_redex M n = 

641 
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

642 
val dummy = dest_aabs f 
2112  643 
in (strip_aabs f,args) 
644 
end; 

645 

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

647 

648 
fun dest_impl tm = 

649 
let val ants = Logic.strip_imp_prems tm 

650 
val eq = Logic.strip_imp_concl tm 

651 
in (ants,get_lhs eq) 

652 
end; 

653 

654 
val pbeta_reduce = simpl_conv [split RS eq_reflection]; 

655 
val restricted = U.can(S.find_term 

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

657 

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

659 
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

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

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

662 
val dummy = mss_ref := [] 
2112  663 
val cut_lemma' = (cut_lemma RS mp) RS eq_reflection 
664 
fun prover mss thm = 

665 
let fun cong_prover mss thm = 

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

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

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

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

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

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

672 
val dummy = mss_ref := (mss :: !mss_ref) 
2112  673 
(* Unquantified eliminate *) 
674 
fun uq_eliminate (thm,imp,sign) = 

675 
let val tych = cterm_of sign 

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

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

679 
val lhs = tych(get_lhs eq) 

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

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

682 
handle _ => reflexive lhs 

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

683 
val dummy = print_thms "proven:\n" [lhs_eq_lhs1] 
2112  684 
val lhs_eq_lhs2 = implies_intr_list ants lhs_eq_lhs1 
685 
val lhs_eeq_lhs2 = lhs_eq_lhs2 RS meta_eq_to_obj_eq 

686 
in 

687 
lhs_eeq_lhs2 COMP thm 

688 
end 

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

690 
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

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

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

693 
"assertion failed in CONTEXT_REWRITE_RULE" 
2112  694 
(* 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

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

696 
(ListPair.zip (args, vstrl))) 
2112  697 
imp_body 
698 
val tych = cterm_of sign 

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

700 
val eq1 = Logic.strip_imp_concl imp_body1 

701 
val Q = get_lhs eq1 

702 
val QeqQ1 = pbeta_reduce (tych Q) 

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

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

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

706 
handle _ => reflexive Q1 

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

707 
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

708 
val Q3 = tych(list_comb(list_mk_aabs(vstrl,Q2),vstrl)) 
2112  709 
val Q2eeqQ3 = symmetric(pbeta_reduce Q3 RS eq_reflection) 
710 
val thA = transitive(QeqQ1 RS eq_reflection) Q1eeqQ2 

711 
val QeeqQ3 = transitive thA Q2eeqQ3 handle _ => 

712 
((Q2eeqQ3 RS meta_eq_to_obj_eq) 

713 
RS ((thA RS meta_eq_to_obj_eq) RS trans)) 

714 
RS eq_reflection 

715 
val impth = implies_intr_list ants1 QeeqQ3 

716 
val impth1 = impth RS meta_eq_to_obj_eq 

717 
(* Need to abstract *) 

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

719 
in ant_th COMP thm 

720 
end 

721 
fun q_eliminate (thm,imp,sign) = 

722 
let val (vlist,imp_body) = strip_all imp 

723 
val (ants,Q) = dest_impl imp_body 

724 
in if (pbeta_redex Q) (length vlist) 

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

726 
else 

727 
let val tych = cterm_of sign 

728 
val ants1 = map tych ants 

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

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

731 
prover (tych Q) 

732 
handle _ => reflexive (tych Q) 

733 
val lhs_eeq_lhs2 = implies_intr_list ants1 Q_eeq_Q1 

734 
val lhs_eq_lhs2 = lhs_eeq_lhs2 RS meta_eq_to_obj_eq 

735 
val ant_th = forall_intr_list(map tych vlist)lhs_eq_lhs2 

736 
in 

737 
ant_th COMP thm 

738 
end end 

739 

740 
fun eliminate thm = 

741 
case (rep_thm thm) 

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

743 
eliminate 

744 
(if not(is_all imp) 

745 
then uq_eliminate (thm,imp,sign) 

746 
else q_eliminate (thm,imp,sign)) 

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

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

749 
in Some(eliminate (rename thm)) 

750 
end handle _ => None 

751 

752 
fun restrict_prover mss thm = 

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

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

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

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

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

761 
end 

762 
(* 

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

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

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

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

767 
* of "f" will match. 

768 
**) 

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

770 
S.dest_var func) 

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

772 
S.dest_var tm) = func_name) 

773 
handle _ => false 

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

775 
val rcontext = rev cntxt 

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

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

779 
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

780 
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

781 
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

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

784 
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

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

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

788 
else let val cTC = cterm_of sign (HOLogic.mk_Trueprop TC) 
2112  789 
in case rcontext of 
790 
[] => SPEC_ALL(ASSUME cTC) 

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

792 
(LIST_CONJ rcontext) 

793 
end 

794 
val th'' = th' RS thm 

795 
in Some (th'') 

796 
end handle _ => None 

797 
in 

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

799 
end 

800 
val ctm = cprop_of th 

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

802 
prover ctm 

803 
val th2 = equal_elim th1 th 

804 
in 

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

805 
(th2, filter (not o restricted) (!tc_list)) 
2112  806 
end; 
807 

808 

809 

810 
fun prove (tm,tac) = 

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

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

812 
val ptm = cterm_of sign(HOLogic.mk_Trueprop t) 
2112  813 
in 
814 
freeze(prove_goalw_cterm [] ptm (fn _ => [tac])) 

815 
end; 

816 

817 

818 
end; (* Rules *) 