381
|
1 |
ML {*
|
|
2 |
fun repeat_eqsubst_thm ctxt thms thm =
|
|
3 |
repeat_eqsubst_thm ctxt thms (eqsubst_thm ctxt thms thm)
|
|
4 |
handle _ => thm
|
|
5 |
*}
|
|
6 |
|
|
7 |
|
|
8 |
ML {*
|
|
9 |
fun eqsubst_prop ctxt thms t =
|
|
10 |
let
|
|
11 |
val goalstate = Goal.init (cterm_of (ProofContext.theory_of ctxt) t)
|
|
12 |
val a' = case (SINGLE (EqSubst.eqsubst_tac ctxt [0] thms 1) goalstate) of
|
|
13 |
NONE => error "eqsubst_prop"
|
|
14 |
| SOME th => cprem_of th 1
|
|
15 |
in term_of a' end
|
|
16 |
*}
|
|
17 |
|
|
18 |
ML {*
|
|
19 |
fun repeat_eqsubst_prop ctxt thms t =
|
|
20 |
repeat_eqsubst_prop ctxt thms (eqsubst_prop ctxt thms t)
|
|
21 |
handle _ => t
|
|
22 |
*}
|
|
23 |
|
379
|
24 |
|
|
25 |
text {* tyRel takes a type and builds a relation that a quantifier over this
|
|
26 |
type needs to respect. *}
|
|
27 |
ML {*
|
|
28 |
fun tyRel ty rty rel lthy =
|
|
29 |
if Sign.typ_instance (ProofContext.theory_of lthy) (ty, rty)
|
|
30 |
then rel
|
|
31 |
else (case ty of
|
|
32 |
Type (s, tys) =>
|
|
33 |
let
|
|
34 |
val tys_rel = map (fn ty => ty --> ty --> @{typ bool}) tys;
|
|
35 |
val ty_out = ty --> ty --> @{typ bool};
|
|
36 |
val tys_out = tys_rel ---> ty_out;
|
|
37 |
in
|
|
38 |
(case (maps_lookup (ProofContext.theory_of lthy) s) of
|
|
39 |
SOME (info) => list_comb (Const (#relfun info, tys_out),
|
|
40 |
map (fn ty => tyRel ty rty rel lthy) tys)
|
|
41 |
| NONE => HOLogic.eq_const ty
|
|
42 |
)
|
|
43 |
end
|
|
44 |
| _ => HOLogic.eq_const ty)
|
|
45 |
*}
|
|
46 |
|
|
47 |
(*
|
|
48 |
ML {* cterm_of @{theory}
|
|
49 |
(tyRel @{typ "'a \<Rightarrow> 'a list \<Rightarrow> 't \<Rightarrow> 't"} (Logic.varifyT @{typ "'a list"})
|
|
50 |
@{term "f::('a list \<Rightarrow> 'a list \<Rightarrow> bool)"} @{context})
|
|
51 |
*}
|
|
52 |
*)
|
|
53 |
|
|
54 |
|
|
55 |
ML {*
|
|
56 |
fun mk_babs ty ty' = Const (@{const_name "Babs"}, [ty' --> @{typ bool}, ty] ---> ty)
|
|
57 |
fun mk_ball ty = Const (@{const_name "Ball"}, [ty, ty] ---> @{typ bool})
|
|
58 |
fun mk_bex ty = Const (@{const_name "Bex"}, [ty, ty] ---> @{typ bool})
|
|
59 |
fun mk_resp ty = Const (@{const_name Respects}, [[ty, ty] ---> @{typ bool}, ty] ---> @{typ bool})
|
|
60 |
*}
|
|
61 |
|
|
62 |
(* applies f to the subterm of an abstractions, otherwise to the given term *)
|
|
63 |
ML {*
|
|
64 |
fun apply_subt f trm =
|
|
65 |
case trm of
|
|
66 |
Abs (x, T, t) =>
|
|
67 |
let
|
|
68 |
val (x', t') = Term.dest_abs (x, T, t)
|
|
69 |
in
|
|
70 |
Term.absfree (x', T, f t')
|
|
71 |
end
|
|
72 |
| _ => f trm
|
|
73 |
*}
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
(* FIXME: if there are more than one quotient, then you have to look up the relation *)
|
|
78 |
ML {*
|
|
79 |
fun my_reg lthy rel rty trm =
|
|
80 |
case trm of
|
|
81 |
Abs (x, T, t) =>
|
|
82 |
if (needs_lift rty T) then
|
|
83 |
let
|
|
84 |
val rrel = tyRel T rty rel lthy
|
|
85 |
in
|
|
86 |
(mk_babs (fastype_of trm) T) $ (mk_resp T $ rrel) $ (apply_subt (my_reg lthy rel rty) trm)
|
|
87 |
end
|
|
88 |
else
|
|
89 |
Abs(x, T, (apply_subt (my_reg lthy rel rty) t))
|
|
90 |
| Const (@{const_name "All"}, ty) $ (t as Abs (x, T, _)) =>
|
|
91 |
let
|
|
92 |
val ty1 = domain_type ty
|
|
93 |
val ty2 = domain_type ty1
|
|
94 |
val rrel = tyRel T rty rel lthy
|
|
95 |
in
|
|
96 |
if (needs_lift rty T) then
|
|
97 |
(mk_ball ty1) $ (mk_resp ty2 $ rrel) $ (apply_subt (my_reg lthy rel rty) t)
|
|
98 |
else
|
|
99 |
Const (@{const_name "All"}, ty) $ apply_subt (my_reg lthy rel rty) t
|
|
100 |
end
|
|
101 |
| Const (@{const_name "Ex"}, ty) $ (t as Abs (x, T, _)) =>
|
|
102 |
let
|
|
103 |
val ty1 = domain_type ty
|
|
104 |
val ty2 = domain_type ty1
|
|
105 |
val rrel = tyRel T rty rel lthy
|
|
106 |
in
|
|
107 |
if (needs_lift rty T) then
|
|
108 |
(mk_bex ty1) $ (mk_resp ty2 $ rrel) $ (apply_subt (my_reg lthy rel rty) t)
|
|
109 |
else
|
|
110 |
Const (@{const_name "Ex"}, ty) $ apply_subt (my_reg lthy rel rty) t
|
|
111 |
end
|
|
112 |
| Const (@{const_name "op ="}, ty) $ t =>
|
|
113 |
if needs_lift rty (fastype_of t) then
|
|
114 |
(tyRel (fastype_of t) rty rel lthy) $ t (* FIXME: t should be regularised too *)
|
|
115 |
else Const (@{const_name "op ="}, ty) $ (my_reg lthy rel rty t)
|
|
116 |
| t1 $ t2 => (my_reg lthy rel rty t1) $ (my_reg lthy rel rty t2)
|
|
117 |
| _ => trm
|
|
118 |
*}
|
|
119 |
|
|
120 |
(* For polymorphic types we need to find the type of the Relation term. *)
|
|
121 |
(* TODO: we assume that the relation is a Constant. Is this always true? *)
|
|
122 |
ML {*
|
|
123 |
fun my_reg_inst lthy rel rty trm =
|
|
124 |
case rel of
|
|
125 |
Const (n, _) => Syntax.check_term lthy
|
|
126 |
(my_reg lthy (Const (n, dummyT)) (Logic.varifyT rty) trm)
|
|
127 |
*}
|
|
128 |
|
|
129 |
(*
|
|
130 |
ML {*
|
|
131 |
val r = Free ("R", dummyT);
|
|
132 |
val t = (my_reg_inst @{context} r @{typ "'a list"} @{term "\<forall>(x::'b list). P x"});
|
|
133 |
val t2 = Syntax.check_term @{context} t;
|
|
134 |
cterm_of @{theory} t2
|
|
135 |
*}
|
|
136 |
*)
|
|
137 |
|
|
138 |
text {* Assumes that the given theorem is atomized *}
|
|
139 |
ML {*
|
|
140 |
fun build_regularize_goal thm rty rel lthy =
|
|
141 |
Logic.mk_implies
|
|
142 |
((prop_of thm),
|
|
143 |
(my_reg_inst lthy rel rty (prop_of thm)))
|
|
144 |
*}
|
|
145 |
|
|
146 |
ML {*
|
|
147 |
fun regularize thm rty rel rel_eqv rel_refl lthy =
|
|
148 |
let
|
|
149 |
val goal = build_regularize_goal thm rty rel lthy;
|
|
150 |
fun tac ctxt =
|
|
151 |
(ObjectLogic.full_atomize_tac) THEN'
|
|
152 |
REPEAT_ALL_NEW (FIRST' [
|
|
153 |
rtac rel_refl,
|
|
154 |
atac,
|
|
155 |
rtac @{thm universal_twice},
|
|
156 |
(rtac @{thm impI} THEN' atac),
|
|
157 |
rtac @{thm implication_twice},
|
|
158 |
EqSubst.eqsubst_tac ctxt [0]
|
|
159 |
[(@{thm equiv_res_forall} OF [rel_eqv]),
|
|
160 |
(@{thm equiv_res_exists} OF [rel_eqv])],
|
|
161 |
(* For a = b \<longrightarrow> a \<approx> b *)
|
|
162 |
(rtac @{thm impI} THEN' (asm_full_simp_tac HOL_ss) THEN' rtac rel_refl),
|
|
163 |
(rtac @{thm RIGHT_RES_FORALL_REGULAR})
|
|
164 |
]);
|
|
165 |
val cthm = Goal.prove lthy [] [] goal
|
|
166 |
(fn {context, ...} => tac context 1);
|
|
167 |
in
|
|
168 |
cthm OF [thm]
|
|
169 |
end
|
|
170 |
*}
|
|
171 |
|
|
172 |
(*consts Rl :: "'a list \<Rightarrow> 'a list \<Rightarrow> bool"
|
|
173 |
axioms Rl_eq: "EQUIV Rl"
|
|
174 |
|
|
175 |
quotient ql = "'a list" / "Rl"
|
|
176 |
by (rule Rl_eq)
|
|
177 |
ML {*
|
|
178 |
ctyp_of @{theory} (exchange_ty @{context} (Logic.varifyT @{typ "'a list"}) (Logic.varifyT @{typ "'a ql"}) @{typ "nat list \<Rightarrow> (nat \<times> nat) list"});
|
|
179 |
ctyp_of @{theory} (exchange_ty @{context} (Logic.varifyT @{typ "nat \<times> nat"}) (Logic.varifyT @{typ "int"}) @{typ "nat list \<Rightarrow> (nat \<times> nat) list"})
|
|
180 |
*}
|
|
181 |
*)
|
|
182 |
|
|
183 |
ML {*
|
|
184 |
(* returns all subterms where two types differ *)
|
|
185 |
fun diff (T, S) Ds =
|
|
186 |
case (T, S) of
|
|
187 |
(TVar v, TVar u) => if v = u then Ds else (T, S)::Ds
|
|
188 |
| (TFree x, TFree y) => if x = y then Ds else (T, S)::Ds
|
|
189 |
| (Type (a, Ts), Type (b, Us)) =>
|
|
190 |
if a = b then diffs (Ts, Us) Ds else (T, S)::Ds
|
|
191 |
| _ => (T, S)::Ds
|
|
192 |
and diffs (T::Ts, U::Us) Ds = diffs (Ts, Us) (diff (T, U) Ds)
|
|
193 |
| diffs ([], []) Ds = Ds
|
|
194 |
| diffs _ _ = error "Unequal length of type arguments"
|
|
195 |
|
|
196 |
*}
|
|
197 |
|
|
198 |
ML {*
|
|
199 |
fun build_repabs_term lthy thm consts rty qty =
|
|
200 |
let
|
|
201 |
(* TODO: The rty and qty stored in the quotient_info should
|
|
202 |
be varified, so this will soon not be needed *)
|
|
203 |
val rty = Logic.varifyT rty;
|
|
204 |
val qty = Logic.varifyT qty;
|
|
205 |
|
|
206 |
fun mk_abs tm =
|
|
207 |
let
|
|
208 |
val ty = fastype_of tm
|
|
209 |
in Syntax.check_term lthy ((get_fun_OLD absF (rty, qty) lthy ty) $ tm) end
|
|
210 |
fun mk_repabs tm =
|
|
211 |
let
|
|
212 |
val ty = fastype_of tm
|
|
213 |
in Syntax.check_term lthy ((get_fun_OLD repF (rty, qty) lthy ty) $ (mk_abs tm)) end
|
|
214 |
|
|
215 |
fun is_lifted_const (Const (x, _)) = member (op =) consts x
|
|
216 |
| is_lifted_const _ = false;
|
|
217 |
|
|
218 |
fun build_aux lthy tm =
|
|
219 |
case tm of
|
|
220 |
Abs (a as (_, vty, _)) =>
|
|
221 |
let
|
|
222 |
val (vs, t) = Term.dest_abs a;
|
|
223 |
val v = Free(vs, vty);
|
|
224 |
val t' = lambda v (build_aux lthy t)
|
|
225 |
in
|
|
226 |
if (not (needs_lift rty (fastype_of tm))) then t'
|
|
227 |
else mk_repabs (
|
|
228 |
if not (needs_lift rty vty) then t'
|
|
229 |
else
|
|
230 |
let
|
|
231 |
val v' = mk_repabs v;
|
|
232 |
(* TODO: I believe 'beta' is not needed any more *)
|
|
233 |
val t1 = (* Envir.beta_norm *) (t' $ v')
|
|
234 |
in
|
|
235 |
lambda v t1
|
|
236 |
end)
|
|
237 |
end
|
|
238 |
| x =>
|
|
239 |
case Term.strip_comb tm of
|
|
240 |
(Const(@{const_name Respects}, _), _) => tm
|
|
241 |
| (opp, tms0) =>
|
|
242 |
let
|
|
243 |
val tms = map (build_aux lthy) tms0
|
|
244 |
val ty = fastype_of tm
|
|
245 |
in
|
|
246 |
if (is_lifted_const opp andalso needs_lift rty ty) then
|
|
247 |
mk_repabs (list_comb (opp, tms))
|
|
248 |
else if ((Term.is_Free opp) andalso (length tms > 0) andalso (needs_lift rty ty)) then
|
|
249 |
mk_repabs (list_comb (opp, tms))
|
|
250 |
else if tms = [] then opp
|
|
251 |
else list_comb(opp, tms)
|
|
252 |
end
|
|
253 |
in
|
|
254 |
repeat_eqsubst_prop lthy @{thms id_def_sym}
|
|
255 |
(build_aux lthy (Thm.prop_of thm))
|
|
256 |
end
|
|
257 |
*}
|
|
258 |
|
|
259 |
text {* Builds provable goals for regularized theorems *}
|
|
260 |
ML {*
|
|
261 |
fun build_repabs_goal ctxt thm cons rty qty =
|
|
262 |
Logic.mk_equals ((Thm.prop_of thm), (build_repabs_term ctxt thm cons rty qty))
|
|
263 |
*}
|
|
264 |
|
|
265 |
ML {*
|
|
266 |
fun repabs lthy thm consts rty qty quot_thm reflex_thm trans_thm rsp_thms =
|
|
267 |
let
|
|
268 |
val rt = build_repabs_term lthy thm consts rty qty;
|
|
269 |
val rg = Logic.mk_equals ((Thm.prop_of thm), rt);
|
|
270 |
fun tac ctxt = (ObjectLogic.full_atomize_tac) THEN'
|
|
271 |
(REPEAT_ALL_NEW (r_mk_comb_tac ctxt rty quot_thm reflex_thm trans_thm rsp_thms));
|
|
272 |
val cthm = Goal.prove lthy [] [] rg (fn x => tac (#context x) 1);
|
|
273 |
in
|
|
274 |
@{thm Pure.equal_elim_rule1} OF [cthm, thm]
|
|
275 |
end
|
|
276 |
*}
|
|
277 |
|
|
278 |
|
|
279 |
(* TODO: Check if it behaves properly with varifyed rty *)
|
|
280 |
ML {*
|
|
281 |
fun findabs_all rty tm =
|
|
282 |
case tm of
|
|
283 |
Abs(_, T, b) =>
|
|
284 |
let
|
|
285 |
val b' = subst_bound ((Free ("x", T)), b);
|
|
286 |
val tys = findabs_all rty b'
|
|
287 |
val ty = fastype_of tm
|
|
288 |
in if needs_lift rty ty then (ty :: tys) else tys
|
|
289 |
end
|
|
290 |
| f $ a => (findabs_all rty f) @ (findabs_all rty a)
|
|
291 |
| _ => [];
|
|
292 |
fun findabs rty tm = distinct (op =) (findabs_all rty tm)
|
|
293 |
*}
|
|
294 |
|
|
295 |
|
|
296 |
(* Currently useful only for LAMBDA_PRS *)
|
|
297 |
ML {*
|
|
298 |
fun make_simp_prs_thm lthy quot_thm thm typ =
|
|
299 |
let
|
|
300 |
val (_, [lty, rty]) = dest_Type typ;
|
|
301 |
val thy = ProofContext.theory_of lthy;
|
|
302 |
val (lcty, rcty) = (ctyp_of thy lty, ctyp_of thy rty)
|
|
303 |
val inst = [SOME lcty, NONE, SOME rcty];
|
|
304 |
val lpi = Drule.instantiate' inst [] thm;
|
|
305 |
val tac =
|
|
306 |
(compose_tac (false, lpi, 2)) THEN_ALL_NEW
|
|
307 |
(quotient_tac quot_thm);
|
|
308 |
val gc = Drule.strip_imp_concl (cprop_of lpi);
|
|
309 |
val t = Goal.prove_internal [] gc (fn _ => tac 1)
|
|
310 |
in
|
|
311 |
MetaSimplifier.rewrite_rule [@{thm eq_reflection} OF @{thms id_apply}] t
|
|
312 |
end
|
|
313 |
*}
|
|
314 |
|
|
315 |
ML {*
|
|
316 |
fun findallex_all rty qty tm =
|
|
317 |
case tm of
|
|
318 |
Const (@{const_name All}, T) $ (s as (Abs(_, _, b))) =>
|
|
319 |
let
|
|
320 |
val (tya, tye) = findallex_all rty qty s
|
|
321 |
in if needs_lift rty T then
|
|
322 |
((T :: tya), tye)
|
|
323 |
else (tya, tye) end
|
|
324 |
| Const (@{const_name Ex}, T) $ (s as (Abs(_, _, b))) =>
|
|
325 |
let
|
|
326 |
val (tya, tye) = findallex_all rty qty s
|
|
327 |
in if needs_lift rty T then
|
|
328 |
(tya, (T :: tye))
|
|
329 |
else (tya, tye) end
|
|
330 |
| Abs(_, T, b) =>
|
|
331 |
findallex_all rty qty (subst_bound ((Free ("x", T)), b))
|
|
332 |
| f $ a =>
|
|
333 |
let
|
|
334 |
val (a1, e1) = findallex_all rty qty f;
|
|
335 |
val (a2, e2) = findallex_all rty qty a;
|
|
336 |
in (a1 @ a2, e1 @ e2) end
|
|
337 |
| _ => ([], []);
|
|
338 |
*}
|
|
339 |
|
|
340 |
ML {*
|
|
341 |
fun findallex lthy rty qty tm =
|
|
342 |
let
|
|
343 |
val (a, e) = findallex_all rty qty tm;
|
|
344 |
val (ad, ed) = (map domain_type a, map domain_type e);
|
|
345 |
val (au, eu) = (distinct (op =) ad, distinct (op =) ed);
|
|
346 |
val (rty, qty) = (Logic.varifyT rty, Logic.varifyT qty)
|
|
347 |
in
|
|
348 |
(map (exchange_ty lthy rty qty) au, map (exchange_ty lthy rty qty) eu)
|
|
349 |
end
|
|
350 |
*}
|
|
351 |
|
|
352 |
ML {*
|
|
353 |
fun make_allex_prs_thm lthy quot_thm thm typ =
|
|
354 |
let
|
|
355 |
val (_, [lty, rty]) = dest_Type typ;
|
|
356 |
val thy = ProofContext.theory_of lthy;
|
|
357 |
val (lcty, rcty) = (ctyp_of thy lty, ctyp_of thy rty)
|
|
358 |
val inst = [NONE, SOME lcty];
|
|
359 |
val lpi = Drule.instantiate' inst [] thm;
|
|
360 |
val tac =
|
|
361 |
(compose_tac (false, lpi, 1)) THEN_ALL_NEW
|
|
362 |
(quotient_tac quot_thm);
|
|
363 |
val gc = Drule.strip_imp_concl (cprop_of lpi);
|
|
364 |
val t = Goal.prove_internal [] gc (fn _ => tac 1)
|
|
365 |
val t_noid = MetaSimplifier.rewrite_rule
|
|
366 |
[@{thm eq_reflection} OF @{thms id_apply}] t;
|
|
367 |
val t_sym = @{thm "HOL.sym"} OF [t_noid];
|
|
368 |
val t_eq = @{thm "eq_reflection"} OF [t_sym]
|
|
369 |
in
|
|
370 |
t_eq
|
|
371 |
end
|
|
372 |
*}
|
|
373 |
|
|
374 |
ML {*
|
|
375 |
fun lift_thm lthy qty qty_name rsp_thms defs rthm =
|
|
376 |
let
|
|
377 |
val _ = tracing ("raw theorem:\n" ^ Syntax.string_of_term lthy (prop_of rthm))
|
|
378 |
|
|
379 |
val (rty, rel, rel_refl, rel_eqv) = lookup_quot_data lthy qty;
|
|
380 |
val (trans2, reps_same, absrep, quot) = lookup_quot_thms lthy qty_name;
|
|
381 |
val consts = lookup_quot_consts defs;
|
|
382 |
val t_a = atomize_thm rthm;
|
|
383 |
|
|
384 |
val _ = tracing ("raw atomized theorem:\n" ^ Syntax.string_of_term lthy (prop_of t_a))
|
|
385 |
|
|
386 |
val t_r = regularize t_a rty rel rel_eqv rel_refl lthy;
|
|
387 |
|
|
388 |
val _ = tracing ("regularised theorem:\n" ^ Syntax.string_of_term lthy (prop_of t_r))
|
|
389 |
|
|
390 |
val t_t = repabs lthy t_r consts rty qty quot rel_refl trans2 rsp_thms;
|
|
391 |
|
|
392 |
val _ = tracing ("rep/abs injected theorem:\n" ^ Syntax.string_of_term lthy (prop_of t_t))
|
|
393 |
|
|
394 |
val (alls, exs) = findallex lthy rty qty (prop_of t_a);
|
|
395 |
val allthms = map (make_allex_prs_thm lthy quot @{thm FORALL_PRS}) alls
|
|
396 |
val exthms = map (make_allex_prs_thm lthy quot @{thm EXISTS_PRS}) exs
|
|
397 |
val t_a = MetaSimplifier.rewrite_rule (allthms @ exthms) t_t
|
|
398 |
|
|
399 |
val _ = tracing ("??:\n" ^ Syntax.string_of_term lthy (prop_of t_a))
|
|
400 |
|
|
401 |
val abs = findabs rty (prop_of t_a);
|
|
402 |
val aps = findaps rty (prop_of t_a);
|
|
403 |
val app_prs_thms = map (applic_prs lthy rty qty absrep) aps;
|
|
404 |
val lam_prs_thms = map (make_simp_prs_thm lthy quot @{thm LAMBDA_PRS}) abs;
|
|
405 |
val t_l = repeat_eqsubst_thm lthy (lam_prs_thms @ app_prs_thms) t_a;
|
|
406 |
|
|
407 |
val _ = tracing ("??:\n" ^ Syntax.string_of_term lthy (prop_of t_l))
|
|
408 |
|
|
409 |
val defs_sym = flat (map (add_lower_defs lthy) defs);
|
|
410 |
val defs_sym_eq = map (fn x => eq_reflection OF [x]) defs_sym;
|
|
411 |
val t_id = simp_ids lthy t_l;
|
|
412 |
|
|
413 |
val _ = tracing ("??:\n" ^ Syntax.string_of_term lthy (prop_of t_id))
|
|
414 |
|
|
415 |
val t_d0 = MetaSimplifier.rewrite_rule defs_sym_eq t_id;
|
|
416 |
|
|
417 |
val _ = tracing ("??:\n" ^ Syntax.string_of_term lthy (prop_of t_d0))
|
|
418 |
|
|
419 |
val t_d = repeat_eqsubst_thm lthy defs_sym t_d0;
|
|
420 |
|
|
421 |
val _ = tracing ("??:\n" ^ Syntax.string_of_term lthy (prop_of t_d))
|
|
422 |
|
|
423 |
val t_r = MetaSimplifier.rewrite_rule [reps_same] t_d;
|
|
424 |
|
|
425 |
val _ = tracing ("??:\n" ^ Syntax.string_of_term lthy (prop_of t_r))
|
|
426 |
|
|
427 |
val t_rv = ObjectLogic.rulify t_r
|
|
428 |
|
|
429 |
val _ = tracing ("lifted theorem:\n" ^ Syntax.string_of_term lthy (prop_of t_rv))
|
|
430 |
in
|
|
431 |
Thm.varifyT t_rv
|
|
432 |
end
|
|
433 |
*}
|
|
434 |
|
|
435 |
ML {*
|
|
436 |
fun lift_thm_note qty qty_name rsp_thms defs thm name lthy =
|
|
437 |
let
|
|
438 |
val lifted_thm = lift_thm lthy qty qty_name rsp_thms defs thm;
|
|
439 |
val (_, lthy2) = note (name, lifted_thm) lthy;
|
|
440 |
in
|
|
441 |
lthy2
|
|
442 |
end
|
|
443 |
*}
|
|
444 |
|
|
445 |
|
|
446 |
ML {*
|
|
447 |
fun regularize_goal lthy thm rel_eqv rel_refl qtrm =
|
|
448 |
let
|
|
449 |
val reg_trm = mk_REGULARIZE_goal lthy (prop_of thm) qtrm;
|
|
450 |
fun tac lthy = regularize_tac lthy rel_eqv rel_refl;
|
|
451 |
val cthm = Goal.prove lthy [] [] reg_trm
|
|
452 |
(fn {context, ...} => tac context 1);
|
|
453 |
in
|
|
454 |
cthm OF [thm]
|
|
455 |
end
|
|
456 |
*}
|
|
457 |
|
|
458 |
ML {*
|
|
459 |
fun repabs_goal lthy thm rty quot_thm reflex_thm trans_thm rsp_thms qtrm =
|
|
460 |
let
|
|
461 |
val rg = Syntax.check_term lthy (mk_inj_REPABS_goal lthy ((prop_of thm), qtrm));
|
|
462 |
fun tac ctxt = (ObjectLogic.full_atomize_tac) THEN'
|
|
463 |
(REPEAT_ALL_NEW (r_mk_comb_tac ctxt rty quot_thm reflex_thm trans_thm rsp_thms));
|
|
464 |
val cthm = Goal.prove lthy [] [] rg (fn x => tac (#context x) 1);
|
|
465 |
in
|
|
466 |
@{thm Pure.equal_elim_rule1} OF [cthm, thm]
|
|
467 |
end
|
|
468 |
*}
|
|
469 |
|
381
|
470 |
|
|
471 |
ML {*
|
|
472 |
fun atomize_goal thy gl =
|
|
473 |
let
|
|
474 |
val vars = map Free (Term.add_frees gl []);
|
|
475 |
val all = if fastype_of gl = @{typ bool} then HOLogic.all_const else Term.all;
|
|
476 |
fun lambda_all (var as Free(_, T)) trm = (all T) $ lambda var trm;
|
|
477 |
val glv = fold lambda_all vars gl
|
|
478 |
val gla = (term_of o snd o Thm.dest_equals o cprop_of) (ObjectLogic.atomize (cterm_of thy glv))
|
|
479 |
val glf = Type.legacy_freeze gla
|
|
480 |
in
|
|
481 |
if fastype_of gl = @{typ bool} then @{term Trueprop} $ glf else glf
|
|
482 |
end
|
|
483 |
*}
|
|
484 |
|
|
485 |
|
|
486 |
ML {* atomize_goal @{theory} @{term "x memb [] = False"} *}
|
399
|
487 |
ML {* atomize_goal @{theory} @{term "x = xa ? a # x = a # xa"} *}
|
381
|
488 |
|
|
489 |
|
|
490 |
|
379
|
491 |
ML {*
|
|
492 |
fun lift_thm_goal lthy qty qty_name rsp_thms defs rthm goal =
|
|
493 |
let
|
|
494 |
val (rty, rel, rel_refl, rel_eqv) = lookup_quot_data lthy qty;
|
|
495 |
val (trans2, reps_same, absrep, quot) = lookup_quot_thms lthy qty_name;
|
|
496 |
val t_a = atomize_thm rthm;
|
|
497 |
val goal_a = atomize_goal (ProofContext.theory_of lthy) goal;
|
|
498 |
val t_r = regularize_goal lthy t_a rel_eqv rel_refl goal_a;
|
|
499 |
val t_t = repabs_goal lthy t_r rty quot rel_refl trans2 rsp_thms goal_a;
|
|
500 |
val (alls, exs) = findallex lthy rty qty (prop_of t_a);
|
|
501 |
val allthms = map (make_allex_prs_thm lthy quot @{thm FORALL_PRS}) alls
|
|
502 |
val exthms = map (make_allex_prs_thm lthy quot @{thm EXISTS_PRS}) exs
|
|
503 |
val t_a = MetaSimplifier.rewrite_rule (allthms @ exthms) t_t
|
|
504 |
val abs = findabs rty (prop_of t_a);
|
|
505 |
val aps = findaps rty (prop_of t_a);
|
|
506 |
val app_prs_thms = map (applic_prs lthy rty qty absrep) aps;
|
|
507 |
val lam_prs_thms = map (make_simp_prs_thm lthy quot @{thm LAMBDA_PRS}) abs;
|
|
508 |
val t_l = repeat_eqsubst_thm lthy (lam_prs_thms @ app_prs_thms) t_a;
|
|
509 |
val defs_sym = flat (map (add_lower_defs lthy) defs);
|
|
510 |
val defs_sym_eq = map (fn x => eq_reflection OF [x]) defs_sym;
|
|
511 |
val t_id = simp_ids lthy t_l;
|
|
512 |
val t_d0 = MetaSimplifier.rewrite_rule defs_sym_eq t_id;
|
|
513 |
val t_d = repeat_eqsubst_thm lthy defs_sym t_d0;
|
|
514 |
val t_r = MetaSimplifier.rewrite_rule [reps_same] t_d;
|
|
515 |
val t_rv = ObjectLogic.rulify t_r
|
|
516 |
in
|
|
517 |
Thm.varifyT t_rv
|
|
518 |
end
|
|
519 |
*}
|
|
520 |
|
|
521 |
ML {*
|
|
522 |
fun lift_thm_goal_note lthy qty qty_name rsp_thms defs thm name goal =
|
|
523 |
let
|
|
524 |
val lifted_thm = lift_thm_goal lthy qty qty_name rsp_thms defs thm goal;
|
|
525 |
val (_, lthy2) = note (name, lifted_thm) lthy;
|
|
526 |
in
|
|
527 |
lthy2
|
|
528 |
end
|
|
529 |
*}
|
|
530 |
|
381
|
531 |
ML {*
|
|
532 |
fun simp_ids_trm trm =
|
|
533 |
trm |>
|
|
534 |
MetaSimplifier.rewrite false @{thms eq_reflection[OF FUN_MAP_I] eq_reflection[OF id_apply] id_def_sym prod_fun_id map_id}
|
|
535 |
|> cprop_of |> Thm.dest_equals |> snd
|
|
536 |
|
|
537 |
*}
|