author  berghofe 
Wed, 31 Oct 2001 19:41:29 +0100  
changeset 11999  43b4385445bf 
parent 11715  592923615f77 
child 12041  27214c16ebe4 
permissions  rwrr 
11519  1 
(* Title: Pure/proofterm.ML 
2 
ID: $Id$ 

11540  3 
Author: Stefan Berghofer, TU Muenchen 
4 
License: GPL (GNU GENERAL PUBLIC LICENSE) 

11519  5 

11540  6 
LF style proof terms. 
11519  7 
*) 
8 

11615  9 
infix 8 % %% %>; 
11519  10 

11 
signature BASIC_PROOFTERM = 

12 
sig 

11543
d61b913431c5
renamed `keep_derivs' to `proofs', and made an integer;
wenzelm
parents:
11540
diff
changeset

13 
val proofs: int ref 
11519  14 

15 
datatype proof = 

16 
PBound of int 

17 
 Abst of string * typ option * proof 

18 
 AbsP of string * term option * proof 

11615  19 
 op % of proof * term option 
20 
 op %% of proof * proof 

11519  21 
 Hyp of term 
22 
 PThm of (string * (string * string list) list) * proof * term * typ list option 

23 
 PAxm of string * term * typ list option 

24 
 Oracle of string * term * typ list option 

25 
 MinProof of proof list; 

26 

11615  27 
val %> : proof * term > proof 
11519  28 
end; 
29 

30 
signature PROOFTERM = 

31 
sig 

32 
include BASIC_PROOFTERM 

33 

34 
val infer_derivs : (proof > proof > proof) > bool * proof > bool * proof > bool * proof 

35 
val infer_derivs' : (proof > proof) > (bool * proof > bool * proof) 

36 

37 
(** primitive operations **) 

38 
val proof_combt : proof * term list > proof 

39 
val proof_combt' : proof * term option list > proof 

40 
val proof_combP : proof * proof list > proof 

41 
val strip_combt : proof > proof * term option list 

42 
val strip_combP : proof > proof * proof list 

43 
val strip_thm : proof > proof 

44 
val map_proof_terms : (term > term) > (typ > typ) > proof > proof 

45 
val fold_proof_terms : (term * 'a > 'a) > (typ * 'a > 'a) > 'a * proof > 'a 

46 
val add_prf_names : string list * proof > string list 

47 
val add_prf_tfree_names : string list * proof > string list 

48 
val add_prf_tvar_ixns : indexname list * proof > indexname list 

49 
val prf_abstract_over : term > proof > proof 

50 
val prf_incr_bv : int > int > int > int > proof > proof 

51 
val incr_pboundvars : int > int > proof > proof 

52 
val prf_loose_bvar1 : proof > int > bool 

53 
val prf_loose_Pbvar1 : proof > int > bool 

54 
val norm_proof : Envir.env > proof > proof 

55 
val norm_proof' : Envir.env > proof > proof 

56 
val prf_subst_bounds : term list > proof > proof 

57 
val prf_subst_pbounds : proof list > proof > proof 

58 
val freeze_thaw_prf : proof > proof * (proof > proof) 

59 

60 
val thms_of_proof : (term * proof) list Symtab.table > proof > 

61 
(term * proof) list Symtab.table 

62 
val axms_of_proof : proof Symtab.table > proof > proof Symtab.table 

63 
val oracles_of_proof : proof list > proof > proof list 

64 

65 
(** proof terms for specific inference rules **) 

66 
val implies_intr_proof : term > proof > proof 

67 
val forall_intr_proof : term > string > proof > proof 

68 
val varify_proof : term > string list > proof > proof 

69 
val freezeT : term > proof > proof 

70 
val rotate_proof : term list > term > int > proof > proof 

71 
val permute_prems_prf : term list > int > int > proof > proof 

72 
val instantiate : (indexname * typ) list > (term * term) list > proof > proof 

73 
val lift_proof : term > int > term > proof > proof 

74 
val assumption_proof : term list > term > int > proof > proof 

75 
val bicompose_proof : term list > term list > term list > term option > 

76 
int > proof > proof > proof 

77 
val equality_axms : (string * term) list 

78 
val reflexive_axm : proof 

79 
val symmetric_axm : proof 

80 
val transitive_axm : proof 

81 
val equal_intr_axm : proof 

82 
val equal_elim_axm : proof 

83 
val abstract_rule_axm : proof 

84 
val combination_axm : proof 

85 
val reflexive : proof 

86 
val symmetric : proof > proof 

87 
val transitive : term > typ > proof > proof > proof 

88 
val abstract_rule : term > string > proof > proof 

89 
val combination : term > term > term > term > typ > proof > proof > proof 

90 
val equal_intr : term > term > proof > proof > proof 

91 
val equal_elim : term > term > proof > proof > proof 

92 
val axm_proof : string > term > proof 

93 
val oracle_proof : string > term > proof 

94 
val thm_proof : Sign.sg > string * (string * string list) list > 

95 
term list > term > proof > proof 

96 
val get_name_tags : term > proof > string * (string * string list) list 

97 

98 
(** rewriting on proof terms **) 

99 
val add_prf_rrules : theory > (proof * proof) list > unit 

100 
val add_prf_rprocs : theory > 

101 
(string * (Term.typ list > proof > proof option)) list > unit 

102 
val rewrite_proof : Type.type_sig > (proof * proof) list * 

103 
(string * (typ list > proof > proof option)) list > proof > proof 

11615  104 
val rewrite_proof_notypes : (proof * proof) list * 
105 
(string * (typ list > proof > proof option)) list > proof > proof 

11519  106 
val init : theory > theory 
107 

108 
end 

109 

110 
structure Proofterm : PROOFTERM = 

111 
struct 

112 

11715
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

113 
open Envir; 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

114 

11519  115 
datatype proof = 
116 
PBound of int 

117 
 Abst of string * typ option * proof 

118 
 AbsP of string * term option * proof 

11615  119 
 op % of proof * term option 
120 
 op %% of proof * proof 

11519  121 
 Hyp of term 
122 
 PThm of (string * (string * string list) list) * proof * term * typ list option 

123 
 PAxm of string * term * typ list option 

124 
 Oracle of string * term * typ list option 

125 
 MinProof of proof list; 

126 

127 
fun oracles_of_proof prfs prf = 

128 
let 

129 
fun oras_of (tabs, Abst (_, _, prf)) = oras_of (tabs, prf) 

130 
 oras_of (tabs, AbsP (_, _, prf)) = oras_of (tabs, prf) 

11615  131 
 oras_of (tabs, prf % _) = oras_of (tabs, prf) 
132 
 oras_of (tabs, prf1 %% prf2) = oras_of (oras_of (tabs, prf1), prf2) 

11519  133 
 oras_of (tabs as (thms, oras), PThm ((name, _), prf, prop, _)) = 
134 
(case Symtab.lookup (thms, name) of 

135 
None => oras_of ((Symtab.update ((name, [prop]), thms), oras), prf) 

136 
 Some ps => if prop mem ps then tabs else 

137 
oras_of ((Symtab.update ((name, prop::ps), thms), oras), prf)) 

138 
 oras_of ((thms, oras), prf as Oracle _) = (thms, prf ins oras) 

139 
 oras_of (tabs, MinProof prfs) = foldl oras_of (tabs, prfs) 

140 
 oras_of (tabs, _) = tabs 

141 
in 

142 
snd (oras_of ((Symtab.empty, prfs), prf)) 

143 
end; 

144 

145 
fun thms_of_proof tab (Abst (_, _, prf)) = thms_of_proof tab prf 

146 
 thms_of_proof tab (AbsP (_, _, prf)) = thms_of_proof tab prf 

11615  147 
 thms_of_proof tab (prf1 %% prf2) = thms_of_proof (thms_of_proof tab prf1) prf2 
148 
 thms_of_proof tab (prf % _) = thms_of_proof tab prf 

11519  149 
 thms_of_proof tab (prf' as PThm ((s, _), prf, prop, _)) = 
150 
(case Symtab.lookup (tab, s) of 

151 
None => thms_of_proof (Symtab.update ((s, [(prop, prf')]), tab)) prf 

152 
 Some ps => if exists (equal prop o fst) ps then tab else 

153 
thms_of_proof (Symtab.update ((s, (prop, prf')::ps), tab)) prf) 

154 
 thms_of_proof tab _ = tab; 

155 

156 
fun axms_of_proof tab (Abst (_, _, prf)) = axms_of_proof tab prf 

157 
 axms_of_proof tab (AbsP (_, _, prf)) = axms_of_proof tab prf 

11615  158 
 axms_of_proof tab (prf1 %% prf2) = axms_of_proof (axms_of_proof tab prf1) prf2 
159 
 axms_of_proof tab (prf % _) = axms_of_proof tab prf 

11519  160 
 axms_of_proof tab (prf as PAxm (s, _, _)) = Symtab.update ((s, prf), tab) 
161 
 axms_of_proof tab _ = tab; 

162 

163 
(** collect all theorems, axioms and oracles **) 

164 

165 
fun mk_min_proof (prfs, Abst (_, _, prf)) = mk_min_proof (prfs, prf) 

166 
 mk_min_proof (prfs, AbsP (_, _, prf)) = mk_min_proof (prfs, prf) 

11615  167 
 mk_min_proof (prfs, prf % _) = mk_min_proof (prfs, prf) 
168 
 mk_min_proof (prfs, prf1 %% prf2) = mk_min_proof (mk_min_proof (prfs, prf1), prf2) 

11519  169 
 mk_min_proof (prfs, prf as PThm _) = prf ins prfs 
170 
 mk_min_proof (prfs, prf as PAxm _) = prf ins prfs 

171 
 mk_min_proof (prfs, prf as Oracle _) = prf ins prfs 

172 
 mk_min_proof (prfs, MinProof prfs') = prfs union prfs' 

173 
 mk_min_proof (prfs, _) = prfs; 

174 

175 
(** proof objects with different levels of detail **) 

176 

11543
d61b913431c5
renamed `keep_derivs' to `proofs', and made an integer;
wenzelm
parents:
11540
diff
changeset

177 
val proofs = ref 2; 
11519  178 

11543
d61b913431c5
renamed `keep_derivs' to `proofs', and made an integer;
wenzelm
parents:
11540
diff
changeset

179 
fun err_illegal_level i = 
d61b913431c5
renamed `keep_derivs' to `proofs', and made an integer;
wenzelm
parents:
11540
diff
changeset

180 
error ("Illegal level of detail for proof objects: " ^ string_of_int i); 
11519  181 

182 
fun if_ora b = if b then oracles_of_proof else K; 

183 

184 
fun infer_derivs f (ora1, prf1) (ora2, prf2) = 

185 
(ora1 orelse ora2, 

11543
d61b913431c5
renamed `keep_derivs' to `proofs', and made an integer;
wenzelm
parents:
11540
diff
changeset

186 
case !proofs of 
d61b913431c5
renamed `keep_derivs' to `proofs', and made an integer;
wenzelm
parents:
11540
diff
changeset

187 
2 => f prf1 prf2 
d61b913431c5
renamed `keep_derivs' to `proofs', and made an integer;
wenzelm
parents:
11540
diff
changeset

188 
 1 => MinProof (mk_min_proof (mk_min_proof ([], prf1), prf2)) 
d61b913431c5
renamed `keep_derivs' to `proofs', and made an integer;
wenzelm
parents:
11540
diff
changeset

189 
 0 => MinProof (if_ora ora2 (if_ora ora1 [] prf1) prf2) 
d61b913431c5
renamed `keep_derivs' to `proofs', and made an integer;
wenzelm
parents:
11540
diff
changeset

190 
 i => err_illegal_level i); 
11519  191 

192 
fun infer_derivs' f (ora, prf) = 

193 
(ora, 

11543
d61b913431c5
renamed `keep_derivs' to `proofs', and made an integer;
wenzelm
parents:
11540
diff
changeset

194 
case !proofs of 
d61b913431c5
renamed `keep_derivs' to `proofs', and made an integer;
wenzelm
parents:
11540
diff
changeset

195 
2 => f prf 
d61b913431c5
renamed `keep_derivs' to `proofs', and made an integer;
wenzelm
parents:
11540
diff
changeset

196 
 1 => MinProof (mk_min_proof ([], prf)) 
d61b913431c5
renamed `keep_derivs' to `proofs', and made an integer;
wenzelm
parents:
11540
diff
changeset

197 
 0 => MinProof (if_ora ora [] prf) 
d61b913431c5
renamed `keep_derivs' to `proofs', and made an integer;
wenzelm
parents:
11540
diff
changeset

198 
 i => err_illegal_level i); 
11519  199 

11615  200 
fun (prf %> t) = prf % Some t; 
11519  201 

11615  202 
val proof_combt = foldl (op %>); 
203 
val proof_combt' = foldl (op %); 

204 
val proof_combP = foldl (op %%); 

11519  205 

206 
fun strip_combt prf = 

11615  207 
let fun stripc (prf % t, ts) = stripc (prf, t::ts) 
11519  208 
 stripc x = x 
209 
in stripc (prf, []) end; 

210 

211 
fun strip_combP prf = 

11615  212 
let fun stripc (prf %% prf', prfs) = stripc (prf, prf'::prfs) 
11519  213 
 stripc x = x 
214 
in stripc (prf, []) end; 

215 

216 
fun strip_thm prf = (case strip_combt (fst (strip_combP prf)) of 

217 
(PThm (_, prf', _, _), _) => prf' 

218 
 _ => prf); 

219 

220 
val mk_Abst = foldr (fn ((s, T:typ), prf) => Abst (s, None, prf)); 

221 
fun mk_AbsP (i, prf) = funpow i (fn prf => AbsP ("H", None, prf)) prf; 

222 

11715
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

223 
fun apsome' f None = raise SAME 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

224 
 apsome' f (Some x) = Some (f x); 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

225 

592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

226 
fun same f x = 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

227 
let val x' = f x 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

228 
in if x = x' then raise SAME else x' end; 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

229 

592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

230 
fun map_proof_terms f g = 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

231 
let 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

232 
fun mapp (Abst (s, T, prf)) = (Abst (s, apsome' (same g) T, mapph prf) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

233 
handle SAME => Abst (s, T, mapp prf)) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

234 
 mapp (AbsP (s, t, prf)) = (AbsP (s, apsome' (same f) t, mapph prf) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

235 
handle SAME => AbsP (s, t, mapp prf)) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

236 
 mapp (prf % t) = (mapp prf % apsome f t 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

237 
handle SAME => prf % apsome' (same f) t) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

238 
 mapp (prf1 %% prf2) = (mapp prf1 %% mapph prf2 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

239 
handle SAME => prf1 %% mapp prf2) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

240 
 mapp (PThm (a, prf, prop, Some Ts)) = 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

241 
PThm (a, prf, prop, Some (same (map g) Ts)) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

242 
 mapp (PAxm (a, prop, Some Ts)) = 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

243 
PAxm (a, prop, Some (same (map g) Ts)) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

244 
 mapp _ = raise SAME 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

245 
and mapph prf = (mapp prf handle SAME => prf) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

246 

592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

247 
in mapph end; 
11519  248 

249 
fun fold_proof_terms f g (a, Abst (_, Some T, prf)) = fold_proof_terms f g (g (T, a), prf) 

250 
 fold_proof_terms f g (a, Abst (_, None, prf)) = fold_proof_terms f g (a, prf) 

251 
 fold_proof_terms f g (a, AbsP (_, Some t, prf)) = fold_proof_terms f g (f (t, a), prf) 

252 
 fold_proof_terms f g (a, AbsP (_, None, prf)) = fold_proof_terms f g (a, prf) 

11615  253 
 fold_proof_terms f g (a, prf % Some t) = f (t, fold_proof_terms f g (a, prf)) 
254 
 fold_proof_terms f g (a, prf % None) = fold_proof_terms f g (a, prf) 

255 
 fold_proof_terms f g (a, prf1 %% prf2) = fold_proof_terms f g 

11519  256 
(fold_proof_terms f g (a, prf1), prf2) 
257 
 fold_proof_terms _ g (a, PThm (_, _, _, Some Ts)) = foldr g (Ts, a) 

258 
 fold_proof_terms _ g (a, PAxm (_, prop, Some Ts)) = foldr g (Ts, a) 

259 
 fold_proof_terms _ _ (a, _) = a; 

260 

261 
val add_prf_names = fold_proof_terms add_term_names ((uncurry K) o swap); 

262 
val add_prf_tfree_names = fold_proof_terms add_term_tfree_names add_typ_tfree_names; 

263 
val add_prf_tvar_ixns = fold_proof_terms add_term_tvar_ixns (add_typ_ixns o swap); 

264 

265 

266 
(***** utilities *****) 

267 

268 
fun strip_abs (_::Ts) (Abs (_, _, t)) = strip_abs Ts t 

269 
 strip_abs _ t = t; 

270 

271 
fun mk_abs Ts t = foldl (fn (t', T) => Abs ("", T, t')) (t, Ts); 

272 

273 

274 
(*Abstraction of a proof term over its occurrences of v, 

275 
which must contain no loose bound variables. 

276 
The resulting proof term is ready to become the body of an Abst.*) 

277 

278 
fun prf_abstract_over v = 

279 
let 

11715
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

280 
fun abst' lev u = if v aconv u then Bound lev else 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

281 
(case u of 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

282 
Abs (a, T, t) => Abs (a, T, abst' (lev + 1) t) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

283 
 f $ t => (abst' lev f $ absth' lev t handle SAME => f $ abst' lev t) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

284 
 _ => raise SAME) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

285 
and absth' lev t = (abst' lev t handle SAME => t); 
11519  286 

11715
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

287 
fun abst lev (AbsP (a, t, prf)) = 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

288 
(AbsP (a, apsome' (abst' lev) t, absth lev prf) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

289 
handle SAME => AbsP (a, t, abst lev prf)) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

290 
 abst lev (Abst (a, T, prf)) = Abst (a, T, abst (lev + 1) prf) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

291 
 abst lev (prf1 %% prf2) = (abst lev prf1 %% absth lev prf2 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

292 
handle SAME => prf1 %% abst lev prf2) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

293 
 abst lev (prf % t) = (abst lev prf % apsome (absth' lev) t 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

294 
handle SAME => prf % apsome' (abst' lev) t) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

295 
 abst _ _ = raise SAME 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

296 
and absth lev prf = (abst lev prf handle SAME => prf) 
11519  297 

11715
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

298 
in absth 0 end; 
11519  299 

300 

301 
(*increments a proof term's nonlocal bound variables 

302 
required when moving a proof term within abstractions 

303 
inc is increment for bound variables 

304 
lev is level at which a bound variable is considered 'loose'*) 

305 

306 
fun incr_bv' inct tlev t = incr_bv (inct, tlev, t); 

307 

11715
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

308 
fun prf_incr_bv' incP inct Plev tlev (PBound i) = 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

309 
if i >= Plev then PBound (i+incP) else raise SAME 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

310 
 prf_incr_bv' incP inct Plev tlev (AbsP (a, t, body)) = 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

311 
(AbsP (a, apsome' (same (incr_bv' inct tlev)) t, 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

312 
prf_incr_bv incP inct (Plev+1) tlev body) handle SAME => 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

313 
AbsP (a, t, prf_incr_bv' incP inct (Plev+1) tlev body)) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

314 
 prf_incr_bv' incP inct Plev tlev (Abst (a, T, body)) = 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

315 
Abst (a, T, prf_incr_bv' incP inct Plev (tlev+1) body) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

316 
 prf_incr_bv' incP inct Plev tlev (prf %% prf') = 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

317 
(prf_incr_bv' incP inct Plev tlev prf %% prf_incr_bv incP inct Plev tlev prf' 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

318 
handle SAME => prf %% prf_incr_bv' incP inct Plev tlev prf') 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

319 
 prf_incr_bv' incP inct Plev tlev (prf % t) = 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

320 
(prf_incr_bv' incP inct Plev tlev prf % apsome (incr_bv' inct tlev) t 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

321 
handle SAME => prf % apsome' (same (incr_bv' inct tlev)) t) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

322 
 prf_incr_bv' _ _ _ _ _ = raise SAME 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

323 
and prf_incr_bv incP inct Plev tlev prf = 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

324 
(prf_incr_bv' incP inct Plev tlev prf handle SAME => prf); 
11519  325 

326 
fun incr_pboundvars 0 0 prf = prf 

327 
 incr_pboundvars incP inct prf = prf_incr_bv incP inct 0 0 prf; 

328 

329 

11615  330 
fun prf_loose_bvar1 (prf1 %% prf2) k = prf_loose_bvar1 prf1 k orelse prf_loose_bvar1 prf2 k 
331 
 prf_loose_bvar1 (prf % Some t) k = prf_loose_bvar1 prf k orelse loose_bvar1 (t, k) 

332 
 prf_loose_bvar1 (_ % None) _ = true 

11519  333 
 prf_loose_bvar1 (AbsP (_, Some t, prf)) k = loose_bvar1 (t, k) orelse prf_loose_bvar1 prf k 
334 
 prf_loose_bvar1 (AbsP (_, None, _)) k = true 

335 
 prf_loose_bvar1 (Abst (_, _, prf)) k = prf_loose_bvar1 prf (k+1) 

336 
 prf_loose_bvar1 _ _ = false; 

337 

338 
fun prf_loose_Pbvar1 (PBound i) k = i = k 

11615  339 
 prf_loose_Pbvar1 (prf1 %% prf2) k = prf_loose_Pbvar1 prf1 k orelse prf_loose_Pbvar1 prf2 k 
340 
 prf_loose_Pbvar1 (prf % _) k = prf_loose_Pbvar1 prf k 

11519  341 
 prf_loose_Pbvar1 (AbsP (_, _, prf)) k = prf_loose_Pbvar1 prf (k+1) 
342 
 prf_loose_Pbvar1 (Abst (_, _, prf)) k = prf_loose_Pbvar1 prf k 

343 
 prf_loose_Pbvar1 _ _ = false; 

344 

345 

346 
(**** substitutions ****) 

347 

348 
fun norm_proof env = 

349 
let 

350 
fun norm (Abst (s, T, prf)) = (Abst (s, apsome' (norm_type_same env) T, normh prf) 

351 
handle SAME => Abst (s, T, norm prf)) 

352 
 norm (AbsP (s, t, prf)) = (AbsP (s, apsome' (norm_term_same env) t, normh prf) 

353 
handle SAME => AbsP (s, t, norm prf)) 

11615  354 
 norm (prf % t) = (norm prf % apsome (norm_term env) t 
355 
handle SAME => prf % apsome' (norm_term_same env) t) 

356 
 norm (prf1 %% prf2) = (norm prf1 %% normh prf2 

357 
handle SAME => prf1 %% norm prf2) 

11519  358 
 norm (PThm (s, prf, t, Ts)) = PThm (s, prf, t, apsome' (norm_types_same env) Ts) 
359 
 norm (PAxm (s, prop, Ts)) = PAxm (s, prop, apsome' (norm_types_same env) Ts) 

360 
 norm _ = raise SAME 

361 
and normh prf = (norm prf handle SAME => prf); 

362 
in normh end; 

363 

364 
(***** Remove some types in proof term (to save space) *****) 

365 

366 
fun remove_types (Abs (s, _, t)) = Abs (s, dummyT, remove_types t) 

367 
 remove_types (t $ u) = remove_types t $ remove_types u 

368 
 remove_types (Const (s, _)) = Const (s, dummyT) 

369 
 remove_types t = t; 

370 

371 
fun remove_types_env (Envir.Envir {iTs, asol, maxidx}) = 

372 
Envir.Envir {iTs = iTs, asol = Vartab.map remove_types asol, maxidx = maxidx}; 

373 

374 
fun norm_proof' env prf = norm_proof (remove_types_env env) prf; 

375 

376 
(**** substitution of bound variables ****) 

377 

378 
fun prf_subst_bounds args prf = 

379 
let 

380 
val n = length args; 

381 
fun subst' lev (Bound i) = 

382 
(if i<lev then raise SAME (*var is locally bound*) 

383 
else incr_boundvars lev (List.nth (args, ilev)) 

384 
handle Subscript => Bound (in) (*loose: change it*)) 

385 
 subst' lev (Abs (a, T, body)) = Abs (a, T, subst' (lev+1) body) 

386 
 subst' lev (f $ t) = (subst' lev f $ substh' lev t 

387 
handle SAME => f $ subst' lev t) 

388 
 subst' _ _ = raise SAME 

389 
and substh' lev t = (subst' lev t handle SAME => t); 

390 

391 
fun subst lev (AbsP (a, t, body)) = (AbsP (a, apsome' (subst' lev) t, substh lev body) 

392 
handle SAME => AbsP (a, t, subst lev body)) 

393 
 subst lev (Abst (a, T, body)) = Abst (a, T, subst (lev+1) body) 

11615  394 
 subst lev (prf %% prf') = (subst lev prf %% substh lev prf' 
395 
handle SAME => prf %% subst lev prf') 

396 
 subst lev (prf % t) = (subst lev prf % apsome (substh' lev) t 

397 
handle SAME => prf % apsome' (subst' lev) t) 

11519  398 
 subst _ _ = raise SAME 
399 
and substh lev prf = (subst lev prf handle SAME => prf) 

400 
in case args of [] => prf  _ => substh 0 prf end; 

401 

402 
fun prf_subst_pbounds args prf = 

403 
let 

404 
val n = length args; 

405 
fun subst (PBound i) Plev tlev = 

406 
(if i < Plev then raise SAME (*var is locally bound*) 

407 
else incr_pboundvars Plev tlev (List.nth (args, iPlev)) 

408 
handle Subscript => PBound (in) (*loose: change it*)) 

409 
 subst (AbsP (a, t, body)) Plev tlev = AbsP (a, t, subst body (Plev+1) tlev) 

410 
 subst (Abst (a, T, body)) Plev tlev = Abst (a, T, subst body Plev (tlev+1)) 

11615  411 
 subst (prf %% prf') Plev tlev = (subst prf Plev tlev %% substh prf' Plev tlev 
412 
handle SAME => prf %% subst prf' Plev tlev) 

413 
 subst (prf % t) Plev tlev = subst prf Plev tlev % t 

11519  414 
 subst prf _ _ = raise SAME 
415 
and substh prf Plev tlev = (subst prf Plev tlev handle SAME => prf) 

416 
in case args of [] => prf  _ => substh prf 0 0 end; 

417 

418 

419 
(**** Freezing and thawing of variables in proof terms ****) 

420 

421 
fun frzT names = 

422 
map_type_tvar (fn (ixn, xs) => TFree (the (assoc (names, ixn)), xs)); 

423 

424 
fun thawT names = 

425 
map_type_tfree (fn (s, xs) => case assoc (names, s) of 

426 
None => TFree (s, xs) 

427 
 Some ixn => TVar (ixn, xs)); 

428 

429 
fun freeze names names' (t $ u) = 

430 
freeze names names' t $ freeze names names' u 

431 
 freeze names names' (Abs (s, T, t)) = 

432 
Abs (s, frzT names' T, freeze names names' t) 

433 
 freeze names names' (Const (s, T)) = Const (s, frzT names' T) 

434 
 freeze names names' (Free (s, T)) = Free (s, frzT names' T) 

435 
 freeze names names' (Var (ixn, T)) = 

436 
Free (the (assoc (names, ixn)), frzT names' T) 

437 
 freeze names names' t = t; 

438 

439 
fun thaw names names' (t $ u) = 

440 
thaw names names' t $ thaw names names' u 

441 
 thaw names names' (Abs (s, T, t)) = 

442 
Abs (s, thawT names' T, thaw names names' t) 

443 
 thaw names names' (Const (s, T)) = Const (s, thawT names' T) 

444 
 thaw names names' (Free (s, T)) = 

445 
let val T' = thawT names' T 

446 
in case assoc (names, s) of 

447 
None => Free (s, T') 

448 
 Some ixn => Var (ixn, T') 

449 
end 

450 
 thaw names names' (Var (ixn, T)) = Var (ixn, thawT names' T) 

451 
 thaw names names' t = t; 

452 

453 
fun freeze_thaw_prf prf = 

454 
let 

455 
val (fs, Tfs, vs, Tvs) = fold_proof_terms 

456 
(fn (t, (fs, Tfs, vs, Tvs)) => 

457 
(add_term_frees (t, fs), add_term_tfree_names (t, Tfs), 

458 
add_term_vars (t, vs), add_term_tvar_ixns (t, Tvs))) 

459 
(fn (T, (fs, Tfs, vs, Tvs)) => 

460 
(fs, add_typ_tfree_names (T, Tfs), 

461 
vs, add_typ_ixns (Tvs, T))) 

462 
(([], [], [], []), prf); 

463 
val fs' = map (fst o dest_Free) fs; 

464 
val vs' = map (fst o dest_Var) vs; 

465 
val names = vs' ~~ variantlist (map fst vs', fs'); 

466 
val names' = Tvs ~~ variantlist (map fst Tvs, Tfs); 

467 
val rnames = map swap names; 

468 
val rnames' = map swap names'; 

469 
in 

470 
(map_proof_terms (freeze names names') (frzT names') prf, 

471 
map_proof_terms (thaw rnames rnames') (thawT rnames')) 

472 
end; 

473 

474 

475 
(***** implication introduction *****) 

476 

477 
fun implies_intr_proof h prf = 

478 
let 

11715
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

479 
fun abshyp i (Hyp t) = if h aconv t then PBound i else raise SAME 
11519  480 
 abshyp i (Abst (s, T, prf)) = Abst (s, T, abshyp i prf) 
481 
 abshyp i (AbsP (s, t, prf)) = AbsP (s, t, abshyp (i+1) prf) 

11615  482 
 abshyp i (prf % t) = abshyp i prf % t 
11715
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

483 
 abshyp i (prf1 %% prf2) = (abshyp i prf1 %% abshyph i prf2 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

484 
handle SAME => prf1 %% abshyp i prf2) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

485 
 abshyp _ _ = raise SAME 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

486 
and abshyph i prf = (abshyp i prf handle SAME => prf) 
11519  487 
in 
11715
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

488 
AbsP ("H", None (*h*), abshyph 0 prf) 
11519  489 
end; 
490 

491 

492 
(***** forall introduction *****) 

493 

494 
fun forall_intr_proof x a prf = Abst (a, None, prf_abstract_over x prf); 

495 

496 

497 
(***** varify *****) 

498 

499 
fun varify_proof t fixed prf = 

500 
let 

501 
val fs = add_term_tfree_names (t, []) \\ fixed; 

502 
val ixns = add_term_tvar_ixns (t, []); 

503 
val fmap = fs ~~ variantlist (fs, map #1 ixns) 

504 
fun thaw (f as (a, S)) = 

505 
(case assoc (fmap, a) of 

506 
None => TFree f 

507 
 Some b => TVar ((b, 0), S)); 

508 
in map_proof_terms (map_term_types (map_type_tfree thaw)) (map_type_tfree thaw) prf 

509 
end; 

510 

511 

512 
local 

513 

514 
fun new_name (ix, (pairs,used)) = 

515 
let val v = variant used (string_of_indexname ix) 

516 
in ((ix, v) :: pairs, v :: used) end; 

517 

518 
fun freeze_one alist (ix, sort) = (case assoc (alist, ix) of 

519 
None => TVar (ix, sort) 

520 
 Some name => TFree (name, sort)); 

521 

522 
in 

523 

524 
fun freezeT t prf = 

525 
let 

526 
val used = it_term_types add_typ_tfree_names (t, []) 

527 
and tvars = map #1 (it_term_types add_typ_tvars (t, [])); 

528 
val (alist, _) = foldr new_name (tvars, ([], used)); 

529 
in 

530 
(case alist of 

531 
[] => prf (*nothing to do!*) 

532 
 _ => 

533 
let val frzT = map_type_tvar (freeze_one alist) 

534 
in map_proof_terms (map_term_types frzT) frzT prf end) 

535 
end; 

536 

537 
end; 

538 

539 

540 
(***** rotate assumptions *****) 

541 

542 
fun rotate_proof Bs Bi m prf = 

543 
let 

544 
val params = Term.strip_all_vars Bi; 

545 
val asms = Logic.strip_imp_prems (Term.strip_all_body Bi); 

546 
val i = length asms; 

547 
val j = length Bs; 

548 
in 

549 
mk_AbsP (j+1, proof_combP (prf, map PBound 

550 
(j downto 1) @ [mk_Abst (params, mk_AbsP (i, 

551 
proof_combP (proof_combt (PBound i, map Bound ((length params  1) downto 0)), 

552 
map PBound (((im1) downto 0) @ ((i1) downto (im))))))])) 

553 
end; 

554 

555 

556 
(***** permute premises *****) 

557 

558 
fun permute_prems_prf prems j k prf = 

559 
let val n = length prems 

560 
in mk_AbsP (n, proof_combP (prf, 

561 
map PBound ((n1 downto nj) @ (k1 downto 0) @ (nj1 downto k)))) 

562 
end; 

563 

564 

565 
(***** instantiation *****) 

566 

567 
fun instantiate vTs tpairs = 

568 
map_proof_terms (subst_atomic (map (apsnd remove_types) tpairs) o 

569 
subst_TVars vTs) (typ_subst_TVars vTs); 

570 

571 

572 
(***** lifting *****) 

573 

574 
fun lift_proof Bi inc prop prf = 

575 
let 

576 
val (_, lift_all) = Logic.lift_fns (Bi, inc); 

577 

578 
fun lift'' Us Ts t = strip_abs Ts (Logic.incr_indexes (Us, inc) (mk_abs Ts t)); 

579 

11715
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

580 
fun lift' Us Ts (Abst (s, T, prf)) = 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

581 
(Abst (s, apsome' (same (incr_tvar inc)) T, lifth' Us (dummyT::Ts) prf) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

582 
handle SAME => Abst (s, T, lift' Us (dummyT::Ts) prf)) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

583 
 lift' Us Ts (AbsP (s, t, prf)) = 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

584 
(AbsP (s, apsome' (same (lift'' Us Ts)) t, lifth' Us Ts prf) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

585 
handle SAME => AbsP (s, t, lift' Us Ts prf)) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

586 
 lift' Us Ts (prf % t) = (lift' Us Ts prf % apsome (lift'' Us Ts) t 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

587 
handle SAME => prf % apsome' (same (lift'' Us Ts)) t) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

588 
 lift' Us Ts (prf1 %% prf2) = (lift' Us Ts prf1 %% lifth' Us Ts prf2 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

589 
handle SAME => prf1 %% lift' Us Ts prf2) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

590 
 lift' _ _ (PThm (s, prf, prop, Ts)) = 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

591 
PThm (s, prf, prop, apsome' (same (map (incr_tvar inc))) Ts) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

592 
 lift' _ _ (PAxm (s, prop, Ts)) = 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

593 
PAxm (s, prop, apsome' (same (map (incr_tvar inc))) Ts) 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

594 
 lift' _ _ _ = raise SAME 
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

595 
and lifth' Us Ts prf = (lift' Us Ts prf handle SAME => prf); 
11519  596 

597 
val ps = map lift_all (Logic.strip_imp_prems (snd (Logic.strip_flexpairs prop))); 

598 
val k = length ps; 

599 

600 
fun mk_app (b, (i, j, prf)) = 

11615  601 
if b then (i1, j, prf %% PBound i) else (i, j1, prf %> Bound j); 
11519  602 

603 
fun lift Us bs i j (Const ("==>", _) $ A $ B) = 

604 
AbsP ("H", None (*A*), lift Us (true::bs) (i+1) j B) 

605 
 lift Us bs i j (Const ("all", _) $ Abs (a, T, t)) = 

606 
Abst (a, None (*T*), lift (T::Us) (false::bs) i (j+1) t) 

11715
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

607 
 lift Us bs i j _ = proof_combP (lifth' (rev Us) [] prf, 
11519  608 
map (fn k => (#3 (foldr mk_app (bs, (i1, j1, PBound k))))) 
609 
(i + k  1 downto i)); 

610 
in 

611 
mk_AbsP (k, lift [] [] 0 0 Bi) 

612 
end; 

613 

614 

615 
(***** proof by assumption *****) 

616 

617 
fun mk_asm_prf (Const ("==>", _) $ A $ B) i = AbsP ("H", None (*A*), mk_asm_prf B (i+1)) 

618 
 mk_asm_prf (Const ("all", _) $ Abs (a, T, t)) i = Abst (a, None (*T*), mk_asm_prf t i) 

619 
 mk_asm_prf _ i = PBound i; 

620 

621 
fun assumption_proof Bs Bi n prf = 

622 
mk_AbsP (length Bs, proof_combP (prf, 

623 
map PBound (length Bs  1 downto 0) @ [mk_asm_prf Bi (~n)])); 

624 

625 

626 
(***** Composition of object rule with proof state *****) 

627 

628 
fun flatten_params_proof i j n (Const ("==>", _) $ A $ B, k) = 

629 
AbsP ("H", None (*A*), flatten_params_proof (i+1) j n (B, k)) 

630 
 flatten_params_proof i j n (Const ("all", _) $ Abs (a, T, t), k) = 

631 
Abst (a, None (*T*), flatten_params_proof i (j+1) n (t, k)) 

632 
 flatten_params_proof i j n (_, k) = proof_combP (proof_combt (PBound (k+i), 

633 
map Bound (j1 downto 0)), map PBound (i1 downto 0 \ in)); 

634 

635 
fun bicompose_proof Bs oldAs newAs A n rprf sprf = 

636 
let 

637 
val la = length newAs; 

638 
val lb = length Bs; 

639 
in 

640 
mk_AbsP (lb+la, proof_combP (sprf, 

11615  641 
map PBound (lb + la  1 downto la)) %% 
11519  642 
proof_combP (rprf, (if n>0 then [mk_asm_prf (the A) (~n)] else []) @ 
643 
map (flatten_params_proof 0 0 n) (oldAs ~~ (la  1 downto 0)))) 

644 
end; 

645 

646 

647 
(***** axioms for equality *****) 

648 

649 
val aT = TFree ("'a", ["logic"]); 

650 
val bT = TFree ("'b", ["logic"]); 

651 
val x = Free ("x", aT); 

652 
val y = Free ("y", aT); 

653 
val z = Free ("z", aT); 

654 
val A = Free ("A", propT); 

655 
val B = Free ("B", propT); 

656 
val f = Free ("f", aT > bT); 

657 
val g = Free ("g", aT > bT); 

658 

659 
local open Logic in 

660 

661 
val equality_axms = 

662 
[("reflexive", mk_equals (x, x)), 

663 
("symmetric", mk_implies (mk_equals (x, y), mk_equals (y, x))), 

664 
("transitive", list_implies ([mk_equals (x, y), mk_equals (y, z)], mk_equals (x, z))), 

665 
("equal_intr", list_implies ([mk_implies (A, B), mk_implies (B, A)], mk_equals (A, B))), 

666 
("equal_elim", list_implies ([mk_equals (A, B), A], B)), 

667 
("abstract_rule", Logic.mk_implies 

668 
(all aT $ Abs ("x", aT, equals bT $ (f $ Bound 0) $ (g $ Bound 0)), 

669 
equals (aT > bT) $ 

670 
Abs ("x", aT, f $ Bound 0) $ Abs ("x", aT, g $ Bound 0))), 

671 
("combination", Logic.list_implies 

672 
([Logic.mk_equals (f, g), Logic.mk_equals (x, y)], 

673 
Logic.mk_equals (f $ x, g $ y)))]; 

674 

675 
val [reflexive_axm, symmetric_axm, transitive_axm, equal_intr_axm, 

676 
equal_elim_axm, abstract_rule_axm, combination_axm] = 

677 
map (fn (s, t) => PAxm ("ProtoPure." ^ s, varify t, None)) equality_axms; 

678 

679 
end; 

680 

11615  681 
val reflexive = reflexive_axm % None; 
11519  682 

11615  683 
fun symmetric (prf as PAxm ("ProtoPure.reflexive", _, _) % _) = prf 
684 
 symmetric prf = symmetric_axm % None % None %% prf; 

11519  685 

11615  686 
fun transitive _ _ (PAxm ("ProtoPure.reflexive", _, _) % _) prf2 = prf2 
687 
 transitive _ _ prf1 (PAxm ("ProtoPure.reflexive", _, _) % _) = prf1 

11519  688 
 transitive u (Type ("prop", [])) prf1 prf2 = 
11615  689 
transitive_axm % None % Some (remove_types u) % None %% prf1 %% prf2 
11519  690 
 transitive u T prf1 prf2 = 
11615  691 
transitive_axm % None % None % None %% prf1 %% prf2; 
11519  692 

693 
fun abstract_rule x a prf = 

11615  694 
abstract_rule_axm % None % None %% forall_intr_proof x a prf; 
11519  695 

11615  696 
fun check_comb (PAxm ("ProtoPure.combination", _, _) % f % g % _ % _ %% prf %% _) = 
11519  697 
is_some f orelse check_comb prf 
11615  698 
 check_comb (PAxm ("ProtoPure.transitive", _, _) % _ % _ % _ %% prf1 %% prf2) = 
11519  699 
check_comb prf1 andalso check_comb prf2 
11615  700 
 check_comb (PAxm ("ProtoPure.symmetric", _, _) % _ % _ %% prf) = check_comb prf 
11519  701 
 check_comb _ = false; 
702 

703 
fun combination f g t u (Type (_, [T, U])) prf1 prf2 = 

704 
let 

705 
val f = Envir.beta_norm f; 

706 
val g = Envir.beta_norm g; 

707 
val prf = if check_comb prf1 then 

11615  708 
combination_axm % None % None 
11519  709 
else (case prf1 of 
11615  710 
PAxm ("ProtoPure.reflexive", _, _) % _ => 
711 
combination_axm %> remove_types f % None 

712 
 _ => combination_axm %> remove_types f %> remove_types g) 

11519  713 
in 
714 
(case T of 

11615  715 
Type ("fun", _) => prf % 
11519  716 
(case head_of f of 
717 
Abs _ => Some (remove_types t) 

718 
 Var _ => Some (remove_types t) 

11615  719 
 _ => None) % 
11519  720 
(case head_of g of 
721 
Abs _ => Some (remove_types u) 

722 
 Var _ => Some (remove_types u) 

11615  723 
 _ => None) %% prf1 %% prf2 
724 
 _ => prf % None % None %% prf1 %% prf2) 

11519  725 
end; 
726 

727 
fun equal_intr A B prf1 prf2 = 

11615  728 
equal_intr_axm %> remove_types A %> remove_types B %% prf1 %% prf2; 
11519  729 

730 
fun equal_elim A B prf1 prf2 = 

11615  731 
equal_elim_axm %> remove_types A %> remove_types B %% prf1 %% prf2; 
11519  732 

733 

734 
(***** axioms and theorems *****) 

735 

736 
fun vars_of t = rev (foldl_aterms 

737 
(fn (vs, v as Var _) => v ins vs  (vs, _) => vs) ([], t)); 

738 

739 
fun test_args _ [] = true 

740 
 test_args is (Bound i :: ts) = 

741 
not (i mem is) andalso test_args (i :: is) ts 

742 
 test_args _ _ = false; 

743 

744 
fun is_fun (Type ("fun", _)) = true 

745 
 is_fun (TVar _) = true 

746 
 is_fun _ = false; 

747 

748 
fun add_funvars Ts (vs, t) = 

749 
if is_fun (fastype_of1 (Ts, t)) then 

750 
vs union mapfilter (fn Var (ixn, T) => 

751 
if is_fun T then Some ixn else None  _ => None) (vars_of t) 

752 
else vs; 

753 

754 
fun add_npvars q p Ts (vs, Const ("==>", _) $ t $ u) = 

755 
add_npvars q p Ts (add_npvars q (not p) Ts (vs, t), u) 

756 
 add_npvars q p Ts (vs, Const ("all", Type (_, [Type (_, [T, _]), _])) $ t) = 

757 
add_npvars q p Ts (vs, if p andalso q then betapply (t, Var (("",0), T)) else t) 

758 
 add_npvars q p Ts (vs, t) = (case strip_comb t of 

759 
(Var (ixn, _), ts) => if test_args [] ts then vs 

760 
else foldl (add_npvars q p Ts) (overwrite (vs, 

761 
(ixn, foldl (add_funvars Ts) (if_none (assoc (vs, ixn)) [], ts))), ts) 

762 
 (Abs (_, T, u), ts) => foldl (add_npvars q p (T::Ts)) (vs, u :: ts) 

763 
 (_, ts) => foldl (add_npvars q p Ts) (vs, ts)); 

764 

765 
fun prop_vars (Const ("==>", _) $ P $ Q) = prop_vars P union prop_vars Q 

766 
 prop_vars (Const ("all", _) $ Abs (_, _, t)) = prop_vars t 

767 
 prop_vars t = (case strip_comb t of 

768 
(Var (ixn, _), _) => [ixn]  _ => []); 

769 

770 
fun is_proj t = 

771 
let 

772 
fun is_p i t = (case strip_comb t of 

773 
(Bound j, []) => false 

774 
 (Bound j, ts) => j >= i orelse exists (is_p i) ts 

775 
 (Abs (_, _, u), _) => is_p (i+1) u 

776 
 (_, ts) => exists (is_p i) ts) 

777 
in (case strip_abs_body t of 

778 
Bound _ => true 

779 
 t' => is_p 0 t') 

780 
end; 

781 

782 
fun needed_vars prop = 

783 
foldl op union ([], map op ins (add_npvars true true [] ([], prop))) union 

784 
prop_vars prop; 

785 

786 
fun gen_axm_proof c name prop = 

787 
let 

788 
val nvs = needed_vars prop; 

789 
val args = map (fn (v as Var (ixn, _)) => 

790 
if ixn mem nvs then Some v else None) (vars_of prop) @ 

791 
map Some (sort (make_ord atless) (term_frees prop)); 

792 
in 

793 
proof_combt' (c (name, prop, None), args) 

794 
end; 

795 

796 
val axm_proof = gen_axm_proof PAxm; 

797 
val oracle_proof = gen_axm_proof Oracle; 

798 

799 
fun shrink ls lev (prf as Abst (a, T, body)) = 

800 
let val (b, is, ch, body') = shrink ls (lev+1) body 

801 
in (b, is, ch, if ch then Abst (a, T, body') else prf) end 

802 
 shrink ls lev (prf as AbsP (a, t, body)) = 

803 
let val (b, is, ch, body') = shrink (lev::ls) lev body 

804 
in (b orelse 0 mem is, mapfilter (fn 0 => None  i => Some (i1)) is, 

805 
ch, if ch then AbsP (a, t, body') else prf) 

806 
end 

807 
 shrink ls lev prf = 

808 
let val (is, ch, _, prf') = shrink' ls lev [] [] prf 

809 
in (false, is, ch, prf') end 

11615  810 
and shrink' ls lev ts prfs (prf as prf1 %% prf2) = 
11519  811 
let 
812 
val p as (_, is', ch', prf') = shrink ls lev prf2; 

813 
val (is, ch, ts', prf'') = shrink' ls lev ts (p::prfs) prf1 

814 
in (is union is', ch orelse ch', ts', 

11615  815 
if ch orelse ch' then prf'' %% prf' else prf) 
11519  816 
end 
11615  817 
 shrink' ls lev ts prfs (prf as prf1 % t) = 
11519  818 
let val (is, ch, (ch', t')::ts', prf') = shrink' ls lev (t::ts) prfs prf1 
11615  819 
in (is, ch orelse ch', ts', if ch orelse ch' then prf' % t' else prf) end 
11519  820 
 shrink' ls lev ts prfs (prf as PBound i) = 
821 
(if exists (fn Some (Bound j) => levj <= nth_elem (i, ls)  _ => true) ts 

822 
orelse exists #1 prfs then [i] else [], false, map (pair false) ts, prf) 

823 
 shrink' ls lev ts prfs (prf as Hyp _) = ([], false, map (pair false) ts, prf) 

11615  824 
 shrink' ls lev ts prfs (prf as MinProof _) = 
825 
([], false, map (pair false) ts, prf) 

11519  826 
 shrink' ls lev ts prfs prf = 
827 
let 

828 
val prop = (case prf of PThm (_, _, prop, _) => prop  PAxm (_, prop, _) => prop 

829 
 Oracle (_, prop, _) => prop  _ => error "shrink: proof not in normal form"); 

830 
val vs = vars_of prop; 

831 
val ts' = take (length vs, ts) 

832 
val ts'' = drop (length vs, ts) 

833 
val insts = take (length ts', map (fst o dest_Var) vs) ~~ ts'; 

834 
val nvs = foldl (fn (ixns', (ixn, ixns)) => 

835 
ixn ins (case assoc (insts, ixn) of 

836 
Some (Some t) => if is_proj t then ixns union ixns' else ixns' 

837 
 _ => ixns union ixns')) 

838 
(needed prop ts'' prfs, add_npvars false true [] ([], prop)); 

839 
val insts' = map 

840 
(fn (ixn, x as Some _) => if ixn mem nvs then (false, x) else (true, None) 

841 
 (_, x) => (false, x)) insts 

842 
in ([], false, insts' @ map (pair false) ts'', prf) end 

843 
and needed (Const ("==>", _) $ t $ u) ts ((b, _, _, _)::prfs) = 

844 
(if b then map (fst o dest_Var) (vars_of t) else []) union needed u ts prfs 

845 
 needed (Var (ixn, _)) (_::_) _ = [ixn] 

846 
 needed _ _ _ = []; 

847 

848 

849 
(**** Simple first order matching functions for terms and proofs ****) 

850 

851 
exception PMatch; 

852 

853 
(** see pattern.ML **) 

854 

855 
fun fomatch Ts tmatch = 

856 
let 

857 
fun mtch (instsp as (tyinsts, insts)) = fn 

858 
(Var (ixn, T), t) => 

859 
(tmatch (tyinsts, fn () => (T, fastype_of1 (Ts, t))), (ixn, t)::insts) 

860 
 (Free (a, T), Free (b, U)) => 

861 
if a=b then (tmatch (tyinsts, K (T, U)), insts) else raise PMatch 

862 
 (Const (a, T), Const (b, U)) => 

863 
if a=b then (tmatch (tyinsts, K (T, U)), insts) else raise PMatch 

864 
 (f $ t, g $ u) => mtch (mtch instsp (f, g)) (t, u) 

865 
 _ => raise PMatch 

866 
in mtch end; 

867 

868 
fun match_proof Ts tmatch = 

869 
let 

870 
fun mtch (inst as (pinst, tinst as (tyinsts, insts))) = fn 

871 
(Hyp (Var (ixn, _)), prf) => ((ixn, prf)::pinst, tinst) 

11615  872 
 (prf1 % opt1, prf2 % opt2) => 
11519  873 
let val inst' as (pinst, tinst) = mtch inst (prf1, prf2) 
874 
in (case (opt1, opt2) of 

875 
(None, _) => inst' 

876 
 (Some _, None) => raise PMatch 

877 
 (Some t, Some u) => (pinst, fomatch Ts tmatch tinst (t, Envir.beta_norm u))) 

878 
end 

11615  879 
 (prf1 %% prf2, prf1' %% prf2') => 
11519  880 
mtch (mtch inst (prf1, prf1')) (prf2, prf2') 
881 
 (PThm ((name1, _), _, prop1, None), PThm ((name2, _), _, prop2, _)) => 

882 
if name1=name2 andalso prop1=prop2 then inst else raise PMatch 

883 
 (PThm ((name1, _), _, prop1, Some Ts), PThm ((name2, _), _, prop2, Some Us)) => 

884 
if name1=name2 andalso prop1=prop2 then 

885 
(pinst, (foldl (tmatch o apsnd K) (tyinsts, Ts ~~ Us), insts)) 

886 
else raise PMatch 

887 
 (PAxm (s1, _, None), PAxm (s2, _, _)) => 

888 
if s1=s2 then inst else raise PMatch 

889 
 (PAxm (s1, _, Some Ts), PAxm (s2, _, Some Us)) => 

890 
if s1=s2 then 

891 
(pinst, (foldl (tmatch o apsnd K) (tyinsts, Ts ~~ Us), insts)) 

892 
else raise PMatch 

893 
 _ => raise PMatch 

894 
in mtch end; 

895 

896 
fun prf_subst (pinst, (tyinsts, insts)) = 

897 
let 

898 
val substT = typ_subst_TVars_Vartab tyinsts; 

899 

900 
fun subst' lev (t as Var (ixn, _)) = (case assoc (insts, ixn) of 

901 
None => t 

902 
 Some u => incr_boundvars lev u) 

903 
 subst' lev (Const (s, T)) = Const (s, substT T) 

904 
 subst' lev (Free (s, T)) = Free (s, substT T) 

905 
 subst' lev (Abs (a, T, body)) = Abs (a, substT T, subst' (lev+1) body) 

906 
 subst' lev (f $ t) = subst' lev f $ subst' lev t 

907 
 subst' _ t = t; 

908 

909 
fun subst plev tlev (AbsP (a, t, body)) = 

910 
AbsP (a, apsome (subst' tlev) t, subst (plev+1) tlev body) 

911 
 subst plev tlev (Abst (a, T, body)) = 

912 
Abst (a, apsome substT T, subst plev (tlev+1) body) 

11615  913 
 subst plev tlev (prf %% prf') = subst plev tlev prf %% subst plev tlev prf' 
914 
 subst plev tlev (prf % t) = subst plev tlev prf % apsome (subst' tlev) t 

11519  915 
 subst plev tlev (prf as Hyp (Var (ixn, _))) = (case assoc (pinst, ixn) of 
916 
None => prf 

917 
 Some prf' => incr_pboundvars plev tlev prf') 

918 
 subst _ _ (PThm (id, prf, prop, Ts)) = 

919 
PThm (id, prf, prop, apsome (map substT) Ts) 

920 
 subst _ _ (PAxm (id, prop, Ts)) = 

921 
PAxm (id, prop, apsome (map substT) Ts) 

922 
 subst _ _ t = t 

923 
in subst 0 0 end; 

924 

925 
(**** rewriting on proof terms ****) 

926 

927 
fun rewrite_prf tmatch (rules, procs) prf = 

928 
let 

11615  929 
fun rew _ (Abst (_, _, body) % Some t) = Some (prf_subst_bounds [t] body) 
930 
 rew _ (AbsP (_, _, body) %% prf) = Some (prf_subst_pbounds [prf] body) 

11519  931 
 rew Ts prf = (case get_first (fn (_, r) => r Ts prf) procs of 
932 
Some prf' => Some prf' 

933 
 None => get_first (fn (prf1, prf2) => Some (prf_subst 

934 
(match_proof Ts tmatch ([], (Vartab.empty, [])) (prf1, prf)) prf2) 

935 
handle PMatch => None) rules); 

936 

11615  937 
fun rew0 Ts (prf as AbsP (_, _, prf' %% PBound 0)) = 
11519  938 
if prf_loose_Pbvar1 prf' 0 then rew Ts prf 
939 
else 

940 
let val prf'' = incr_pboundvars (~1) 0 prf' 

941 
in Some (if_none (rew Ts prf'') prf'') end 

11615  942 
 rew0 Ts (prf as Abst (_, _, prf' % Some (Bound 0))) = 
11519  943 
if prf_loose_bvar1 prf' 0 then rew Ts prf 
944 
else 

945 
let val prf'' = incr_pboundvars 0 (~1) prf' 

946 
in Some (if_none (rew Ts prf'') prf'') end 

947 
 rew0 Ts prf = rew Ts prf; 

948 

949 
fun rew1 Ts prf = (case rew2 Ts prf of 

950 
Some prf1 => (case rew0 Ts prf1 of 

951 
Some prf2 => Some (if_none (rew1 Ts prf2) prf2) 

952 
 None => Some prf1) 

953 
 None => (case rew0 Ts prf of 

954 
Some prf1 => Some (if_none (rew1 Ts prf1) prf1) 

955 
 None => None)) 

956 

11615  957 
and rew2 Ts (prf % Some t) = (case prf of 
11519  958 
Abst (_, _, body) => 
959 
let val prf' = prf_subst_bounds [t] body 

960 
in Some (if_none (rew2 Ts prf') prf') end 

961 
 _ => (case rew1 Ts prf of 

11615  962 
Some prf' => Some (prf' % Some t) 
11519  963 
 None => None)) 
11615  964 
 rew2 Ts (prf % None) = apsome (fn prf' => prf' % None) (rew1 Ts prf) 
965 
 rew2 Ts (prf1 %% prf2) = (case prf1 of 

11519  966 
AbsP (_, _, body) => 
967 
let val prf' = prf_subst_pbounds [prf2] body 

968 
in Some (if_none (rew2 Ts prf') prf') end 

969 
 _ => (case rew1 Ts prf1 of 

970 
Some prf1' => (case rew1 Ts prf2 of 

11615  971 
Some prf2' => Some (prf1' %% prf2') 
972 
 None => Some (prf1' %% prf2)) 

11519  973 
 None => (case rew1 Ts prf2 of 
11615  974 
Some prf2' => Some (prf1 %% prf2') 
11519  975 
 None => None))) 
976 
 rew2 Ts (Abst (s, T, prf)) = (case rew1 (if_none T dummyT :: Ts) prf of 

977 
Some prf' => Some (Abst (s, T, prf')) 

978 
 None => None) 

979 
 rew2 Ts (AbsP (s, t, prf)) = (case rew1 Ts prf of 

980 
Some prf' => Some (AbsP (s, t, prf')) 

981 
 None => None) 

982 
 rew2 _ _ = None 

983 

984 
in if_none (rew1 [] prf) prf end; 

985 

986 
fun rewrite_proof tsig = rewrite_prf (fn (tab, f) => 

987 
Type.typ_match tsig (tab, f ()) handle Type.TYPE_MATCH => raise PMatch); 

988 

11715
592923615f77
Tuned several functions to improve sharing of unchanged subproofs.
berghofe
parents:
11652
diff
changeset

989 
fun rewrite_proof_notypes rews = rewrite_prf fst rews; 
11615  990 

11519  991 
(**** theory data ****) 
992 

993 
(* data kind 'Pure/proof' *) 

994 

995 
structure ProofArgs = 

996 
struct 

997 
val name = "Pure/proof"; 

998 
type T = ((proof * proof) list * 

999 
(string * (typ list > proof > proof option)) list) ref; 

1000 

1001 
val empty = (ref ([], [])): T; 

1002 
fun copy (ref rews) = (ref rews): T; (*create new reference!*) 

1003 
val prep_ext = copy; 

1004 
fun merge (ref (rules1, procs1), ref (rules2, procs2)) = ref 

1005 
(merge_lists rules1 rules2, 

1006 
generic_merge (uncurry equal o pairself fst) I I procs1 procs2); 

1007 
fun print _ _ = (); 

1008 
end; 

1009 

1010 
structure ProofData = TheoryDataFun(ProofArgs); 

1011 

1012 
val init = ProofData.init; 

1013 

1014 
fun add_prf_rrules thy rs = 

1015 
let val r = ProofData.get thy 

1016 
in r := (rs @ fst (!r), snd (!r)) end; 

1017 

1018 
fun add_prf_rprocs thy ps = 

1019 
let val r = ProofData.get thy 

1020 
in r := (fst (!r), ps @ snd (!r)) end; 

1021 

1022 
fun thm_proof sign (name, tags) hyps prop prf = 

1023 
let 

1024 
val hyps' = gen_distinct op aconv hyps; 

1025 
val prop = Logic.list_implies (hyps', prop); 

1026 
val nvs = needed_vars prop; 

1027 
val args = map (fn (v as Var (ixn, _)) => 

1028 
if ixn mem nvs then Some v else None) (vars_of prop) @ 

1029 
map Some (sort (make_ord atless) (term_frees prop)); 

11543
d61b913431c5
renamed `keep_derivs' to `proofs', and made an integer;
wenzelm
parents:
11540
diff
changeset

1030 
val opt_prf = if ! proofs = 2 then 
11519  1031 
#4 (shrink [] 0 (rewrite_prf fst (!(ProofData.get_sg sign)) 
1032 
(foldr (uncurry implies_intr_proof) (hyps', prf)))) 

1033 
else MinProof (mk_min_proof ([], prf)); 

11999  1034 
val head = (case strip_combt (fst (strip_combP opt_prf)) of 
11519  1035 
(PThm ((old_name, _), prf', prop', None), args') => 
1036 
if (old_name="" orelse old_name=name) andalso 

1037 
prop = prop' andalso args = args' then 

1038 
PThm ((name, tags), prf', prop, None) 

1039 
else 

1040 
PThm ((name, tags), opt_prf, prop, None) 

1041 
 _ => PThm ((name, tags), opt_prf, prop, None)) 

1042 
in 

1043 
proof_combP (proof_combt' (head, args), map Hyp hyps') 

1044 
end; 

1045 

1046 
fun get_name_tags prop prf = (case strip_combt (fst (strip_combP prf)) of 

1047 
(PThm ((name, tags), _, prop', _), _) => 

1048 
if prop=prop' then (name, tags) else ("", []) 

1049 
 (PAxm (name, prop', _), _) => 

1050 
if prop=prop' then (name, []) else ("", []) 

1051 
 _ => ("", [])); 

1052 

1053 
end; 

1054 

1055 
structure BasicProofterm : BASIC_PROOFTERM = Proofterm; 

1056 
open BasicProofterm; 