0
|
1 |
theory QuotMain
|
6
|
2 |
imports QuotScript QuotList Prove
|
0
|
3 |
begin
|
|
4 |
|
|
5 |
locale QUOT_TYPE =
|
|
6 |
fixes R :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
|
|
7 |
and Abs :: "('a \<Rightarrow> bool) \<Rightarrow> 'b"
|
|
8 |
and Rep :: "'b \<Rightarrow> ('a \<Rightarrow> bool)"
|
|
9 |
assumes equiv: "EQUIV R"
|
|
10 |
and rep_prop: "\<And>y. \<exists>x. Rep y = R x"
|
|
11 |
and rep_inverse: "\<And>x. Abs (Rep x) = x"
|
|
12 |
and abs_inverse: "\<And>x. (Rep (Abs (R x))) = (R x)"
|
|
13 |
and rep_inject: "\<And>x y. (Rep x = Rep y) = (x = y)"
|
15
|
14 |
begin
|
0
|
15 |
|
|
16 |
definition
|
|
17 |
"ABS x \<equiv> Abs (R x)"
|
|
18 |
|
|
19 |
definition
|
|
20 |
"REP a = Eps (Rep a)"
|
|
21 |
|
15
|
22 |
lemma lem9:
|
0
|
23 |
shows "R (Eps (R x)) = R x"
|
|
24 |
proof -
|
|
25 |
have a: "R x x" using equiv by (simp add: EQUIV_REFL_SYM_TRANS REFL_def)
|
|
26 |
then have "R x (Eps (R x))" by (rule someI)
|
15
|
27 |
then show "R (Eps (R x)) = R x"
|
0
|
28 |
using equiv unfolding EQUIV_def by simp
|
|
29 |
qed
|
|
30 |
|
|
31 |
theorem thm10:
|
24
|
32 |
shows "ABS (REP a) \<equiv> a"
|
|
33 |
apply (rule eq_reflection)
|
|
34 |
unfolding ABS_def REP_def
|
0
|
35 |
proof -
|
15
|
36 |
from rep_prop
|
0
|
37 |
obtain x where eq: "Rep a = R x" by auto
|
|
38 |
have "Abs (R (Eps (Rep a))) = Abs (R (Eps (R x)))" using eq by simp
|
|
39 |
also have "\<dots> = Abs (R x)" using lem9 by simp
|
|
40 |
also have "\<dots> = Abs (Rep a)" using eq by simp
|
|
41 |
also have "\<dots> = a" using rep_inverse by simp
|
|
42 |
finally
|
|
43 |
show "Abs (R (Eps (Rep a))) = a" by simp
|
|
44 |
qed
|
|
45 |
|
15
|
46 |
lemma REP_refl:
|
0
|
47 |
shows "R (REP a) (REP a)"
|
|
48 |
unfolding REP_def
|
|
49 |
by (simp add: equiv[simplified EQUIV_def])
|
|
50 |
|
|
51 |
lemma lem7:
|
22
|
52 |
shows "(R x = R y) = (Abs (R x) = Abs (R y))"
|
0
|
53 |
apply(rule iffI)
|
|
54 |
apply(simp)
|
|
55 |
apply(drule rep_inject[THEN iffD2])
|
|
56 |
apply(simp add: abs_inverse)
|
|
57 |
done
|
15
|
58 |
|
0
|
59 |
theorem thm11:
|
|
60 |
shows "R r r' = (ABS r = ABS r')"
|
|
61 |
unfolding ABS_def
|
|
62 |
by (simp only: equiv[simplified EQUIV_def] lem7)
|
|
63 |
|
4
|
64 |
|
2
|
65 |
lemma REP_ABS_rsp:
|
4
|
66 |
shows "R f (REP (ABS g)) = R f g"
|
|
67 |
and "R (REP (ABS g)) f = R g f"
|
23
|
68 |
by (simp_all add: thm10 thm11)
|
4
|
69 |
|
0
|
70 |
lemma QUOTIENT:
|
|
71 |
"QUOTIENT R ABS REP"
|
|
72 |
apply(unfold QUOTIENT_def)
|
|
73 |
apply(simp add: thm10)
|
|
74 |
apply(simp add: REP_refl)
|
|
75 |
apply(subst thm11[symmetric])
|
|
76 |
apply(simp add: equiv[simplified EQUIV_def])
|
|
77 |
done
|
|
78 |
|
21
|
79 |
lemma R_trans:
|
36
|
80 |
assumes ab: "R a b"
|
|
81 |
and bc: "R b c"
|
22
|
82 |
shows "R a c"
|
21
|
83 |
proof -
|
|
84 |
have tr: "TRANS R" using equiv EQUIV_REFL_SYM_TRANS[of R] by simp
|
|
85 |
moreover have ab: "R a b" by fact
|
|
86 |
moreover have bc: "R b c" by fact
|
22
|
87 |
ultimately show "R a c" unfolding TRANS_def by blast
|
21
|
88 |
qed
|
|
89 |
|
|
90 |
lemma R_sym:
|
36
|
91 |
assumes ab: "R a b"
|
22
|
92 |
shows "R b a"
|
21
|
93 |
proof -
|
|
94 |
have re: "SYM R" using equiv EQUIV_REFL_SYM_TRANS[of R] by simp
|
22
|
95 |
then show "R b a" using ab unfolding SYM_def by blast
|
21
|
96 |
qed
|
|
97 |
|
36
|
98 |
lemma R_trans2:
|
|
99 |
assumes ac: "R a c"
|
22
|
100 |
and bd: "R b d"
|
21
|
101 |
shows "R a b = R c d"
|
|
102 |
proof
|
23
|
103 |
assume "R a b"
|
21
|
104 |
then have "R b a" using R_sym by blast
|
|
105 |
then have "R b c" using ac R_trans by blast
|
|
106 |
then have "R c b" using R_sym by blast
|
|
107 |
then show "R c d" using bd R_trans by blast
|
|
108 |
next
|
23
|
109 |
assume "R c d"
|
21
|
110 |
then have "R a d" using ac R_trans by blast
|
|
111 |
then have "R d a" using R_sym by blast
|
|
112 |
then have "R b a" using bd R_trans by blast
|
|
113 |
then show "R a b" using R_sym by blast
|
|
114 |
qed
|
|
115 |
|
|
116 |
lemma REPS_same:
|
25
|
117 |
shows "R (REP a) (REP b) \<equiv> (a = b)"
|
38
|
118 |
proof -
|
|
119 |
have "R (REP a) (REP b) = (a = b)"
|
|
120 |
proof
|
|
121 |
assume as: "R (REP a) (REP b)"
|
|
122 |
from rep_prop
|
|
123 |
obtain x y
|
|
124 |
where eqs: "Rep a = R x" "Rep b = R y" by blast
|
|
125 |
from eqs have "R (Eps (R x)) (Eps (R y))" using as unfolding REP_def by simp
|
|
126 |
then have "R x (Eps (R y))" using lem9 by simp
|
|
127 |
then have "R (Eps (R y)) x" using R_sym by blast
|
|
128 |
then have "R y x" using lem9 by simp
|
|
129 |
then have "R x y" using R_sym by blast
|
|
130 |
then have "ABS x = ABS y" using thm11 by simp
|
|
131 |
then have "Abs (Rep a) = Abs (Rep b)" using eqs unfolding ABS_def by simp
|
|
132 |
then show "a = b" using rep_inverse by simp
|
|
133 |
next
|
|
134 |
assume ab: "a = b"
|
|
135 |
have "REFL R" using equiv EQUIV_REFL_SYM_TRANS[of R] by simp
|
|
136 |
then show "R (REP a) (REP b)" unfolding REFL_def using ab by auto
|
|
137 |
qed
|
|
138 |
then show "R (REP a) (REP b) \<equiv> (a = b)" by simp
|
21
|
139 |
qed
|
|
140 |
|
0
|
141 |
end
|
|
142 |
|
|
143 |
section {* type definition for the quotient type *}
|
|
144 |
|
|
145 |
ML {*
|
1
|
146 |
(* constructs the term \<lambda>(c::ty \<Rightarrow> bool). \<exists>x. c = rel x *)
|
0
|
147 |
fun typedef_term rel ty lthy =
|
15
|
148 |
let
|
|
149 |
val [x, c] = [("x", ty), ("c", ty --> @{typ bool})]
|
0
|
150 |
|> Variable.variant_frees lthy [rel]
|
|
151 |
|> map Free
|
|
152 |
in
|
15
|
153 |
lambda c
|
39
980d45c4a726
fixed a bug in my code: function typedef_term constructs now now the correct term when the relation is called x
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
154 |
(HOLogic.exists_const ty $
|
980d45c4a726
fixed a bug in my code: function typedef_term constructs now now the correct term when the relation is called x
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
155 |
lambda x (HOLogic.mk_eq (c, (rel $ x))))
|
0
|
156 |
end
|
|
157 |
|
41
|
158 |
|
26
|
159 |
(* makes the new type definitions and proves non-emptyness*)
|
|
160 |
fun typedef_make (qty_name, rel, ty) lthy =
|
38
|
161 |
let
|
26
|
162 |
val typedef_tac =
|
|
163 |
EVERY1 [rewrite_goal_tac @{thms mem_def},
|
38
|
164 |
rtac @{thm exI},
|
|
165 |
rtac @{thm exI},
|
26
|
166 |
rtac @{thm refl}]
|
|
167 |
in
|
|
168 |
LocalTheory.theory_result
|
|
169 |
(Typedef.add_typedef false NONE
|
|
170 |
(qty_name, map fst (Term.add_tfreesT ty []), NoSyn)
|
|
171 |
(typedef_term rel ty lthy)
|
|
172 |
NONE typedef_tac) lthy
|
|
173 |
end
|
0
|
174 |
|
41
|
175 |
|
40
|
176 |
(* tactic to prove the QUOT_TYPE theorem for the new type *)
|
15
|
177 |
fun typedef_quot_type_tac equiv_thm (typedef_info: Typedef.info) =
|
0
|
178 |
let
|
|
179 |
val rep_thm = #Rep typedef_info
|
|
180 |
val rep_inv = #Rep_inverse typedef_info
|
|
181 |
val abs_inv = #Abs_inverse typedef_info
|
|
182 |
val rep_inj = #Rep_inject typedef_info
|
|
183 |
|
|
184 |
val ss = HOL_basic_ss addsimps @{thms mem_def}
|
|
185 |
val rep_thm_simpd = Simplifier.asm_full_simplify ss rep_thm
|
|
186 |
val abs_inv_simpd = Simplifier.asm_full_simplify ss abs_inv
|
|
187 |
in
|
|
188 |
EVERY1 [rtac @{thm QUOT_TYPE.intro},
|
15
|
189 |
rtac equiv_thm,
|
|
190 |
rtac rep_thm_simpd,
|
|
191 |
rtac rep_inv,
|
0
|
192 |
rtac abs_inv_simpd, rtac @{thm exI}, rtac @{thm refl},
|
|
193 |
rtac rep_inj]
|
|
194 |
end
|
|
195 |
|
41
|
196 |
|
0
|
197 |
fun typedef_quot_type_thm (rel, abs, rep, equiv_thm, typedef_info) lthy =
|
|
198 |
let
|
|
199 |
val quot_type_const = Const (@{const_name "QUOT_TYPE"}, dummyT)
|
|
200 |
val goal = HOLogic.mk_Trueprop (quot_type_const $ rel $ abs $ rep)
|
|
201 |
|> Syntax.check_term lthy
|
|
202 |
in
|
|
203 |
Goal.prove lthy [] [] goal
|
26
|
204 |
(K (typedef_quot_type_tac equiv_thm typedef_info))
|
0
|
205 |
end
|
|
206 |
|
41
|
207 |
|
40
|
208 |
(* proves the quotient theorem *)
|
0
|
209 |
fun typedef_quotient_thm (rel, abs, rep, abs_def, rep_def, quot_type_thm) lthy =
|
|
210 |
let
|
|
211 |
val quotient_const = Const (@{const_name "QUOTIENT"}, dummyT)
|
|
212 |
val goal = HOLogic.mk_Trueprop (quotient_const $ rel $ abs $ rep)
|
|
213 |
|> Syntax.check_term lthy
|
26
|
214 |
|
|
215 |
val typedef_quotient_thm_tac =
|
|
216 |
EVERY1 [K (rewrite_goals_tac [abs_def, rep_def]),
|
|
217 |
rtac @{thm QUOT_TYPE.QUOTIENT},
|
|
218 |
rtac quot_type_thm]
|
0
|
219 |
in
|
38
|
220 |
Goal.prove lthy [] [] goal
|
26
|
221 |
(K typedef_quotient_thm_tac)
|
0
|
222 |
end
|
|
223 |
*}
|
|
224 |
|
|
225 |
text {* two wrappers for define and note *}
|
15
|
226 |
ML {*
|
17
|
227 |
fun make_def (name, mx, rhs) lthy =
|
15
|
228 |
let
|
17
|
229 |
val ((rhs, (_ , thm)), lthy') =
|
|
230 |
LocalTheory.define Thm.internalK ((name, mx), (Attrib.empty_binding, rhs)) lthy
|
0
|
231 |
in
|
17
|
232 |
((rhs, thm), lthy')
|
0
|
233 |
end
|
|
234 |
*}
|
|
235 |
|
|
236 |
ML {*
|
26
|
237 |
fun note_thm (name, thm) lthy =
|
0
|
238 |
let
|
15
|
239 |
val ((_,[thm']), lthy') = LocalTheory.note Thm.theoremK ((name, []), [thm]) lthy
|
0
|
240 |
in
|
16
|
241 |
(thm', lthy')
|
0
|
242 |
end
|
|
243 |
*}
|
|
244 |
|
14
|
245 |
ML {*
|
|
246 |
val no_vars = Thm.rule_attribute (fn context => fn th =>
|
|
247 |
let
|
|
248 |
val ctxt = Variable.set_body false (Context.proof_of context);
|
|
249 |
val ((_, [th']), _) = Variable.import true [th] ctxt;
|
|
250 |
in th' end);
|
|
251 |
*}
|
|
252 |
|
|
253 |
ML {*
|
0
|
254 |
fun typedef_main (qty_name, rel, ty, equiv_thm) lthy =
|
15
|
255 |
let
|
0
|
256 |
(* generates typedef *)
|
26
|
257 |
val ((_, typedef_info), lthy1) = typedef_make (qty_name, rel, ty) lthy
|
0
|
258 |
|
|
259 |
(* abs and rep functions *)
|
|
260 |
val abs_ty = #abs_type typedef_info
|
|
261 |
val rep_ty = #rep_type typedef_info
|
|
262 |
val abs_name = #Abs_name typedef_info
|
|
263 |
val rep_name = #Rep_name typedef_info
|
|
264 |
val abs = Const (abs_name, rep_ty --> abs_ty)
|
|
265 |
val rep = Const (rep_name, abs_ty --> rep_ty)
|
|
266 |
|
|
267 |
(* ABS and REP definitions *)
|
|
268 |
val ABS_const = Const (@{const_name "QUOT_TYPE.ABS"}, dummyT )
|
|
269 |
val REP_const = Const (@{const_name "QUOT_TYPE.REP"}, dummyT )
|
26
|
270 |
val ABS_trm = Syntax.check_term lthy1 (ABS_const $ rel $ abs)
|
|
271 |
val REP_trm = Syntax.check_term lthy1 (REP_const $ rep)
|
0
|
272 |
val ABS_name = Binding.prefix_name "ABS_" qty_name
|
15
|
273 |
val REP_name = Binding.prefix_name "REP_" qty_name
|
26
|
274 |
val (((ABS, ABS_def), (REP, REP_def)), lthy2) =
|
|
275 |
lthy1 |> make_def (ABS_name, NoSyn, ABS_trm)
|
0
|
276 |
||>> make_def (REP_name, NoSyn, REP_trm)
|
|
277 |
|
|
278 |
(* quot_type theorem *)
|
26
|
279 |
val quot_thm = typedef_quot_type_thm (rel, abs, rep, equiv_thm, typedef_info) lthy2
|
0
|
280 |
val quot_thm_name = Binding.prefix_name "QUOT_TYPE_" qty_name
|
|
281 |
|
|
282 |
(* quotient theorem *)
|
26
|
283 |
val quotient_thm = typedef_quotient_thm (rel, ABS, REP, ABS_def, REP_def, quot_thm) lthy2
|
0
|
284 |
val quotient_thm_name = Binding.prefix_name "QUOTIENT_" qty_name
|
|
285 |
|
14
|
286 |
(* interpretation *)
|
|
287 |
val bindd = ((Binding.make ("", Position.none)), ([]: Attrib.src list))
|
26
|
288 |
val ((_, [eqn1pre]), lthy3) = Variable.import true [ABS_def] lthy2;
|
14
|
289 |
val eqn1i = Thm.prop_of (symmetric eqn1pre)
|
26
|
290 |
val ((_, [eqn2pre]), lthy4) = Variable.import true [REP_def] lthy3;
|
14
|
291 |
val eqn2i = Thm.prop_of (symmetric eqn2pre)
|
|
292 |
|
26
|
293 |
val exp_morphism = ProofContext.export_morphism lthy4 (ProofContext.init (ProofContext.theory_of lthy4));
|
14
|
294 |
val exp_term = Morphism.term exp_morphism;
|
|
295 |
val exp = Morphism.thm exp_morphism;
|
|
296 |
|
|
297 |
val mthd = Method.SIMPLE_METHOD ((rtac quot_thm 1) THEN
|
16
|
298 |
ALLGOALS (simp_tac (HOL_basic_ss addsimps [(symmetric (exp ABS_def)), (symmetric (exp REP_def))])))
|
14
|
299 |
val mthdt = Method.Basic (fn _ => mthd)
|
|
300 |
val bymt = Proof.global_terminal_proof (mthdt, NONE)
|
16
|
301 |
val exp_i = [(@{const_name QUOT_TYPE}, ((("QUOT_TYPE_I_" ^ (Binding.name_of qty_name)), true),
|
14
|
302 |
Expression.Named [
|
|
303 |
("R", rel),
|
|
304 |
("Abs", abs),
|
|
305 |
("Rep", rep)
|
|
306 |
]))]
|
0
|
307 |
in
|
26
|
308 |
lthy4
|
|
309 |
|> note_thm (quot_thm_name, quot_thm)
|
|
310 |
||>> note_thm (quotient_thm_name, quotient_thm)
|
14
|
311 |
||> LocalTheory.theory (fn thy =>
|
|
312 |
let
|
|
313 |
val global_eqns = map exp_term [eqn2i, eqn1i];
|
16
|
314 |
(* Not sure if the following context should not be used *)
|
26
|
315 |
val (global_eqns2, lthy5) = Variable.import_terms true global_eqns lthy4;
|
14
|
316 |
val global_eqns3 = map (fn t => (bindd, t)) global_eqns2;
|
|
317 |
in ProofContext.theory_of (bymt (Expression.interpretation (exp_i, []) global_eqns3 thy)) end)
|
0
|
318 |
end
|
|
319 |
*}
|
|
320 |
|
|
321 |
section {* various tests for quotient types*}
|
|
322 |
datatype trm =
|
15
|
323 |
var "nat"
|
0
|
324 |
| app "trm" "trm"
|
|
325 |
| lam "nat" "trm"
|
13
|
326 |
|
38
|
327 |
axiomatization
|
|
328 |
RR :: "trm \<Rightarrow> trm \<Rightarrow> bool"
|
26
|
329 |
where
|
14
|
330 |
r_eq: "EQUIV RR"
|
0
|
331 |
|
|
332 |
local_setup {*
|
16
|
333 |
typedef_main (@{binding "qtrm"}, @{term "RR"}, @{typ trm}, @{thm r_eq}) #> snd
|
0
|
334 |
*}
|
|
335 |
|
|
336 |
term Rep_qtrm
|
2
|
337 |
term REP_qtrm
|
0
|
338 |
term Abs_qtrm
|
|
339 |
term ABS_qtrm
|
|
340 |
thm QUOT_TYPE_qtrm
|
|
341 |
thm QUOTIENT_qtrm
|
2
|
342 |
|
16
|
343 |
(* Test interpretation *)
|
|
344 |
thm QUOT_TYPE_I_qtrm.thm11
|
26
|
345 |
thm QUOT_TYPE.thm11
|
16
|
346 |
|
21
|
347 |
print_theorems
|
|
348 |
|
0
|
349 |
thm Rep_qtrm
|
|
350 |
|
|
351 |
text {* another test *}
|
|
352 |
datatype 'a trm' =
|
15
|
353 |
var' "'a"
|
0
|
354 |
| app' "'a trm'" "'a trm'"
|
|
355 |
| lam' "'a" "'a trm'"
|
15
|
356 |
|
0
|
357 |
consts R' :: "'a trm' \<Rightarrow> 'a trm' \<Rightarrow> bool"
|
|
358 |
axioms r_eq': "EQUIV R'"
|
|
359 |
|
14
|
360 |
|
0
|
361 |
local_setup {*
|
16
|
362 |
typedef_main (@{binding "qtrm'"}, @{term "R'"}, @{typ "'a trm'"}, @{thm r_eq'}) #> snd
|
0
|
363 |
*}
|
|
364 |
|
14
|
365 |
print_theorems
|
|
366 |
|
0
|
367 |
term ABS_qtrm'
|
|
368 |
term REP_qtrm'
|
|
369 |
thm QUOT_TYPE_qtrm'
|
|
370 |
thm QUOTIENT_qtrm'
|
|
371 |
thm Rep_qtrm'
|
|
372 |
|
14
|
373 |
|
0
|
374 |
text {* a test with lists of terms *}
|
|
375 |
datatype t =
|
|
376 |
vr "string"
|
|
377 |
| ap "t list"
|
|
378 |
| lm "string" "t"
|
|
379 |
|
|
380 |
consts Rt :: "t \<Rightarrow> t \<Rightarrow> bool"
|
|
381 |
axioms t_eq: "EQUIV Rt"
|
|
382 |
|
|
383 |
local_setup {*
|
|
384 |
typedef_main (@{binding "qt"}, @{term "Rt"}, @{typ "t"}, @{thm t_eq}) #> snd
|
|
385 |
*}
|
|
386 |
|
|
387 |
section {* lifting of constants *}
|
|
388 |
|
|
389 |
text {* information about map-functions for type-constructor *}
|
|
390 |
ML {*
|
|
391 |
type typ_info = {mapfun: string}
|
|
392 |
|
|
393 |
local
|
|
394 |
structure Data = GenericDataFun
|
|
395 |
(type T = typ_info Symtab.table
|
|
396 |
val empty = Symtab.empty
|
|
397 |
val extend = I
|
|
398 |
fun merge _ = Symtab.merge (K true))
|
|
399 |
in
|
|
400 |
val lookup = Symtab.lookup o Data.get
|
15
|
401 |
fun update k v = Data.map (Symtab.update (k, v))
|
0
|
402 |
end
|
|
403 |
*}
|
|
404 |
|
|
405 |
(* mapfuns for some standard types *)
|
|
406 |
setup {*
|
|
407 |
Context.theory_map (update @{type_name "list"} {mapfun = @{const_name "map"}})
|
28
|
408 |
#> Context.theory_map (update @{type_name "*"} {mapfun = @{const_name "prod_fun"}})
|
0
|
409 |
#> Context.theory_map (update @{type_name "fun"} {mapfun = @{const_name "fun_map"}})
|
|
410 |
*}
|
|
411 |
|
|
412 |
ML {* lookup (Context.Proof @{context}) @{type_name list} *}
|
|
413 |
|
|
414 |
ML {*
|
|
415 |
datatype abs_or_rep = abs | rep
|
|
416 |
|
15
|
417 |
fun get_fun abs_or_rep rty qty lthy ty =
|
0
|
418 |
let
|
|
419 |
val qty_name = Long_Name.base_name (fst (dest_Type qty))
|
15
|
420 |
|
0
|
421 |
fun get_fun_aux s fs_tys =
|
|
422 |
let
|
|
423 |
val (fs, tys) = split_list fs_tys
|
15
|
424 |
val (otys, ntys) = split_list tys
|
0
|
425 |
val oty = Type (s, otys)
|
|
426 |
val nty = Type (s, ntys)
|
|
427 |
val ftys = map (op -->) tys
|
|
428 |
in
|
15
|
429 |
(case (lookup (Context.Proof lthy) s) of
|
0
|
430 |
SOME info => (list_comb (Const (#mapfun info, ftys ---> oty --> nty), fs), (oty, nty))
|
41
|
431 |
| NONE => raise ERROR ("no map association for type " ^ s))
|
0
|
432 |
end
|
|
433 |
|
|
434 |
fun get_const abs = (Const ("QuotMain.ABS_" ^ qty_name, rty --> qty), (rty, qty))
|
|
435 |
| get_const rep = (Const ("QuotMain.REP_" ^ qty_name, qty --> rty), (qty, rty))
|
41
|
436 |
|
|
437 |
fun mk_identity ty = Abs ("x", ty, Bound 0)
|
|
438 |
|
0
|
439 |
in
|
|
440 |
if ty = qty
|
|
441 |
then (get_const abs_or_rep)
|
|
442 |
else (case ty of
|
41
|
443 |
TFree _ => (mk_identity ty, (ty, ty))
|
|
444 |
| Type (_, []) => (mk_identity ty, (ty, ty))
|
15
|
445 |
| Type (s, tys) => get_fun_aux s (map (get_fun abs_or_rep rty qty lthy) tys)
|
41
|
446 |
| _ => raise ERROR ("no type variables")
|
0
|
447 |
)
|
|
448 |
end
|
|
449 |
*}
|
|
450 |
|
|
451 |
ML {*
|
2
|
452 |
get_fun rep @{typ t} @{typ qt} @{context} @{typ "t * nat"}
|
|
453 |
|> fst
|
|
454 |
|> Syntax.string_of_term @{context}
|
|
455 |
|> writeln
|
|
456 |
*}
|
|
457 |
|
|
458 |
|
41
|
459 |
text {* produces the definition for a lifted constant *}
|
2
|
460 |
ML {*
|
0
|
461 |
fun get_const_def nconst oconst rty qty lthy =
|
|
462 |
let
|
|
463 |
val ty = fastype_of nconst
|
|
464 |
val (arg_tys, res_ty) = strip_type ty
|
14
|
465 |
|
0
|
466 |
val fresh_args = arg_tys |> map (pair "x")
|
15
|
467 |
|> Variable.variant_frees lthy [nconst, oconst]
|
0
|
468 |
|> map Free
|
|
469 |
|
|
470 |
val rep_fns = map (fst o get_fun rep rty qty lthy) arg_tys
|
|
471 |
val abs_fn = (fst o get_fun abs rty qty lthy) res_ty
|
|
472 |
|
|
473 |
in
|
|
474 |
map (op $) (rep_fns ~~ fresh_args)
|
|
475 |
|> curry list_comb oconst
|
|
476 |
|> curry (op $) abs_fn
|
|
477 |
|> fold_rev lambda fresh_args
|
|
478 |
end
|
|
479 |
*}
|
|
480 |
|
|
481 |
ML {*
|
15
|
482 |
fun exchange_ty rty qty ty =
|
41
|
483 |
if ty = rty
|
|
484 |
then qty
|
15
|
485 |
else
|
0
|
486 |
(case ty of
|
|
487 |
Type (s, tys) => Type (s, map (exchange_ty rty qty) tys)
|
41
|
488 |
| _ => ty
|
|
489 |
)
|
0
|
490 |
*}
|
|
491 |
|
|
492 |
ML {*
|
17
|
493 |
fun make_const_def nconst_bname oconst mx rty qty lthy =
|
0
|
494 |
let
|
|
495 |
val oconst_ty = fastype_of oconst
|
|
496 |
val nconst_ty = exchange_ty rty qty oconst_ty
|
17
|
497 |
val nconst = Const (Binding.name_of nconst_bname, nconst_ty)
|
0
|
498 |
val def_trm = get_const_def nconst oconst rty qty lthy
|
|
499 |
in
|
17
|
500 |
make_def (nconst_bname, mx, def_trm) lthy
|
15
|
501 |
end
|
0
|
502 |
*}
|
|
503 |
|
2
|
504 |
local_setup {*
|
41
|
505 |
make_const_def @{binding VR} @{term "vr"} NoSyn @{typ "t"} @{typ "qt"} #> snd #>
|
|
506 |
make_const_def @{binding AP} @{term "ap"} NoSyn @{typ "t"} @{typ "qt"} #> snd #>
|
17
|
507 |
make_const_def @{binding LM} @{term "lm"} NoSyn @{typ "t"} @{typ "qt"} #> snd
|
2
|
508 |
*}
|
|
509 |
|
41
|
510 |
term vr
|
|
511 |
term ap
|
|
512 |
term lm
|
|
513 |
thm VR_def AP_def LM_def
|
14
|
514 |
term LM
|
2
|
515 |
term VR
|
|
516 |
term AP
|
|
517 |
|
|
518 |
|
0
|
519 |
text {* a test with functions *}
|
|
520 |
datatype 'a t' =
|
|
521 |
vr' "string"
|
|
522 |
| ap' "('a t') * ('a t')"
|
|
523 |
| lm' "'a" "string \<Rightarrow> ('a t')"
|
|
524 |
|
|
525 |
consts Rt' :: "('a t') \<Rightarrow> ('a t') \<Rightarrow> bool"
|
|
526 |
axioms t_eq': "EQUIV Rt'"
|
|
527 |
|
14
|
528 |
|
0
|
529 |
local_setup {*
|
|
530 |
typedef_main (@{binding "qt'"}, @{term "Rt'"}, @{typ "'a t'"}, @{thm t_eq'}) #> snd
|
|
531 |
*}
|
|
532 |
|
14
|
533 |
print_theorems
|
2
|
534 |
|
0
|
535 |
local_setup {*
|
41
|
536 |
make_const_def @{binding VR'} @{term "vr'"} NoSyn @{typ "'a t'"} @{typ "'a qt'"} #> snd #>
|
|
537 |
make_const_def @{binding AP'} @{term "ap'"} NoSyn @{typ "'a t'"} @{typ "'a qt'"} #> snd #>
|
17
|
538 |
make_const_def @{binding LM'} @{term "lm'"} NoSyn @{typ "'a t'"} @{typ "'a qt'"} #> snd
|
0
|
539 |
*}
|
|
540 |
|
41
|
541 |
term vr'
|
|
542 |
term ap'
|
|
543 |
term ap'
|
|
544 |
thm VR'_def AP'_def LM'_def
|
14
|
545 |
term LM'
|
0
|
546 |
term VR'
|
|
547 |
term AP'
|
|
548 |
|
14
|
549 |
|
0
|
550 |
text {* finite set example *}
|
13
|
551 |
print_syntax
|
14
|
552 |
inductive
|
13
|
553 |
list_eq (infix "\<approx>" 50)
|
0
|
554 |
where
|
|
555 |
"a#b#xs \<approx> b#a#xs"
|
|
556 |
| "[] \<approx> []"
|
|
557 |
| "xs \<approx> ys \<Longrightarrow> ys \<approx> xs"
|
|
558 |
| "a#a#xs \<approx> a#xs"
|
|
559 |
| "xs \<approx> ys \<Longrightarrow> a#xs \<approx> a#ys"
|
|
560 |
| "\<lbrakk>xs1 \<approx> xs2; xs2 \<approx> xs3\<rbrakk> \<Longrightarrow> xs1 \<approx> xs3"
|
|
561 |
|
43
|
562 |
lemma list_eq_refl:
|
0
|
563 |
shows "xs \<approx> xs"
|
13
|
564 |
apply (induct xs)
|
|
565 |
apply (auto intro: list_eq.intros)
|
|
566 |
done
|
0
|
567 |
|
|
568 |
lemma equiv_list_eq:
|
|
569 |
shows "EQUIV list_eq"
|
13
|
570 |
unfolding EQUIV_REFL_SYM_TRANS REFL_def SYM_def TRANS_def
|
43
|
571 |
apply(auto intro: list_eq.intros list_eq_refl)
|
13
|
572 |
done
|
0
|
573 |
|
|
574 |
local_setup {*
|
|
575 |
typedef_main (@{binding "fset"}, @{term "list_eq"}, @{typ "'a list"}, @{thm "equiv_list_eq"}) #> snd
|
|
576 |
*}
|
|
577 |
|
14
|
578 |
print_theorems
|
|
579 |
|
0
|
580 |
typ "'a fset"
|
|
581 |
thm "Rep_fset"
|
|
582 |
|
|
583 |
local_setup {*
|
17
|
584 |
make_const_def @{binding EMPTY} @{term "[]"} NoSyn @{typ "'a list"} @{typ "'a fset"} #> snd
|
0
|
585 |
*}
|
|
586 |
|
14
|
587 |
term Nil
|
0
|
588 |
term EMPTY
|
2
|
589 |
thm EMPTY_def
|
|
590 |
|
0
|
591 |
|
|
592 |
local_setup {*
|
17
|
593 |
make_const_def @{binding INSERT} @{term "op #"} NoSyn @{typ "'a list"} @{typ "'a fset"} #> snd
|
0
|
594 |
*}
|
|
595 |
|
14
|
596 |
term Cons
|
0
|
597 |
term INSERT
|
2
|
598 |
thm INSERT_def
|
0
|
599 |
|
|
600 |
local_setup {*
|
17
|
601 |
make_const_def @{binding UNION} @{term "op @"} NoSyn @{typ "'a list"} @{typ "'a fset"} #> snd
|
0
|
602 |
*}
|
|
603 |
|
15
|
604 |
term append
|
0
|
605 |
term UNION
|
2
|
606 |
thm UNION_def
|
|
607 |
|
7
|
608 |
|
0
|
609 |
thm QUOTIENT_fset
|
|
610 |
|
14
|
611 |
thm QUOT_TYPE_I_fset.thm11
|
9
|
612 |
|
|
613 |
|
13
|
614 |
fun
|
|
615 |
membship :: "'a \<Rightarrow> 'a list \<Rightarrow> bool" (infix "memb" 100)
|
0
|
616 |
where
|
|
617 |
m1: "(x memb []) = False"
|
|
618 |
| m2: "(x memb (y#xs)) = ((x=y) \<or> (x memb xs))"
|
|
619 |
|
2
|
620 |
lemma mem_respects:
|
9
|
621 |
fixes z
|
2
|
622 |
assumes a: "list_eq x y"
|
9
|
623 |
shows "(z memb x) = (z memb y)"
|
13
|
624 |
using a by induct auto
|
|
625 |
|
2
|
626 |
|
29
|
627 |
fun
|
|
628 |
card1 :: "'a list \<Rightarrow> nat"
|
|
629 |
where
|
|
630 |
card1_nil: "(card1 []) = 0"
|
|
631 |
| card1_cons: "(card1 (x # xs)) = (if (x memb xs) then (card1 xs) else (Suc (card1 xs)))"
|
|
632 |
|
|
633 |
local_setup {*
|
|
634 |
make_const_def @{binding card} @{term "card1"} NoSyn @{typ "'a list"} @{typ "'a fset"} #> snd
|
|
635 |
*}
|
|
636 |
|
|
637 |
term card1
|
|
638 |
term card
|
|
639 |
thm card_def
|
|
640 |
|
|
641 |
(* text {*
|
|
642 |
Maybe make_const_def should require a theorem that says that the particular lifted function
|
|
643 |
respects the relation. With it such a definition would be impossible:
|
|
644 |
make_const_def @{binding CARD} @{term "length"} NoSyn @{typ "'a list"} @{typ "'a fset"} #> snd
|
41
|
645 |
*}*)
|
29
|
646 |
|
|
647 |
lemma card1_rsp:
|
|
648 |
fixes a b :: "'a list"
|
|
649 |
assumes e: "a \<approx> b"
|
|
650 |
shows "card1 a = card1 b"
|
|
651 |
using e apply induct
|
|
652 |
apply (simp_all add:mem_respects)
|
|
653 |
done
|
|
654 |
|
|
655 |
lemma card1_0:
|
|
656 |
fixes a :: "'a list"
|
|
657 |
shows "(card1 a = 0) = (a = [])"
|
|
658 |
apply (induct a)
|
|
659 |
apply (simp)
|
|
660 |
apply (simp_all)
|
|
661 |
apply meson
|
|
662 |
apply (simp_all)
|
|
663 |
done
|
|
664 |
|
|
665 |
lemma not_mem_card1:
|
|
666 |
fixes x :: "'a"
|
|
667 |
fixes xs :: "'a list"
|
|
668 |
shows "~(x memb xs) \<Longrightarrow> card1 (x # xs) = Suc (card1 xs)"
|
|
669 |
by simp
|
|
670 |
|
|
671 |
|
|
672 |
lemma mem_cons:
|
|
673 |
fixes x :: "'a"
|
|
674 |
fixes xs :: "'a list"
|
|
675 |
assumes a : "x memb xs"
|
|
676 |
shows "x # xs \<approx> xs"
|
38
|
677 |
using a
|
|
678 |
apply (induct xs)
|
|
679 |
apply (auto intro: list_eq.intros)
|
|
680 |
done
|
29
|
681 |
|
|
682 |
lemma card1_suc:
|
|
683 |
fixes xs :: "'a list"
|
|
684 |
fixes n :: "nat"
|
|
685 |
assumes c: "card1 xs = Suc n"
|
|
686 |
shows "\<exists>a ys. ~(a memb ys) \<and> xs \<approx> (a # ys)"
|
38
|
687 |
using c
|
|
688 |
apply(induct xs)
|
|
689 |
apply (metis Suc_neq_Zero card1_0)
|
|
690 |
apply (metis QUOT_TYPE_I_fset.R_trans QuotMain.card1_cons list_eq_sym mem_cons)
|
|
691 |
done
|
29
|
692 |
|
12
|
693 |
lemma cons_preserves:
|
|
694 |
fixes z
|
|
695 |
assumes a: "xs \<approx> ys"
|
|
696 |
shows "(z # xs) \<approx> (z # ys)"
|
13
|
697 |
using a by (rule QuotMain.list_eq.intros(5))
|
12
|
698 |
|
29
|
699 |
|
|
700 |
text {*
|
40
|
701 |
Unlam_def converts a definition given as
|
|
702 |
|
|
703 |
c \<equiv> %x. %y. f x y
|
|
704 |
|
|
705 |
to a theorem of the form
|
|
706 |
|
|
707 |
c x y \<equiv> f x y
|
|
708 |
|
|
709 |
This function is needed to rewrite the right-hand
|
|
710 |
side to the left-hand side.
|
29
|
711 |
*}
|
|
712 |
|
11
|
713 |
ML {*
|
29
|
714 |
fun unlam_def_aux orig_ctxt ctxt t =
|
13
|
715 |
let val rhs = Thm.rhs_of t in
|
|
716 |
(case try (Thm.dest_abs NONE) rhs of
|
|
717 |
SOME (v, vt) =>
|
|
718 |
let
|
|
719 |
val (vname, vt) = Term.dest_Free (Thm.term_of v)
|
|
720 |
val ([vnname], ctxt) = Variable.variant_fixes [vname] ctxt
|
|
721 |
val nv = Free(vnname, vt)
|
|
722 |
val t2 = Drule.fun_cong_rule t (Thm.cterm_of (ProofContext.theory_of ctxt) nv)
|
|
723 |
val tnorm = equal_elim (Drule.beta_eta_conversion (Thm.cprop_of t2)) t2
|
29
|
724 |
in unlam_def_aux orig_ctxt ctxt tnorm end
|
13
|
725 |
| NONE => singleton (ProofContext.export ctxt orig_ctxt) t)
|
29
|
726 |
end;
|
|
727 |
|
|
728 |
fun unlam_def ctxt t = unlam_def_aux ctxt ctxt t
|
10
|
729 |
*}
|
9
|
730 |
|
10
|
731 |
local_setup {*
|
17
|
732 |
make_const_def @{binding IN} @{term "membship"} NoSyn @{typ "'a list"} @{typ "'a fset"} #> snd
|
10
|
733 |
*}
|
|
734 |
|
0
|
735 |
term membship
|
|
736 |
term IN
|
2
|
737 |
thm IN_def
|
29
|
738 |
ML {* unlam_def @{context} @{thm IN_def} *}
|
0
|
739 |
|
2
|
740 |
lemmas a = QUOT_TYPE.ABS_def[OF QUOT_TYPE_fset]
|
|
741 |
thm QUOT_TYPE.thm11[OF QUOT_TYPE_fset, THEN iffD1, simplified a]
|
0
|
742 |
|
2
|
743 |
lemma yy:
|
|
744 |
shows "(False = x memb []) = (False = IN (x::nat) EMPTY)"
|
|
745 |
unfolding IN_def EMPTY_def
|
|
746 |
apply(rule_tac f="(op =) False" in arg_cong)
|
|
747 |
apply(rule mem_respects)
|
14
|
748 |
apply(simp only: QUOT_TYPE_I_fset.REP_ABS_rsp)
|
2
|
749 |
apply(rule list_eq.intros)
|
0
|
750 |
done
|
|
751 |
|
2
|
752 |
lemma
|
|
753 |
shows "IN (x::nat) EMPTY = False"
|
|
754 |
using m1
|
|
755 |
apply -
|
|
756 |
apply(rule yy[THEN iffD1, symmetric])
|
|
757 |
apply(simp)
|
0
|
758 |
done
|
|
759 |
|
2
|
760 |
lemma
|
|
761 |
shows "((x=y) \<or> (IN x xs) = (IN (x::nat) (INSERT y xs))) =
|
23
|
762 |
((x=y) \<or> x memb REP_fset xs = x memb (y # REP_fset xs))"
|
2
|
763 |
unfolding IN_def INSERT_def
|
|
764 |
apply(rule_tac f="(op \<or>) (x=y)" in arg_cong)
|
|
765 |
apply(rule_tac f="(op =) (x memb REP_fset xs)" in arg_cong)
|
|
766 |
apply(rule mem_respects)
|
|
767 |
apply(rule list_eq.intros(3))
|
|
768 |
apply(unfold REP_fset_def ABS_fset_def)
|
5
|
769 |
apply(simp only: QUOT_TYPE.REP_ABS_rsp[OF QUOT_TYPE_fset])
|
43
|
770 |
apply(rule list_eq_refl)
|
2
|
771 |
done
|
0
|
772 |
|
7
|
773 |
lemma append_respects_fst:
|
14
|
774 |
assumes a : "list_eq l1 l2"
|
4
|
775 |
shows "list_eq (l1 @ s) (l2 @ s)"
|
3
|
776 |
using a
|
|
777 |
apply(induct)
|
4
|
778 |
apply(auto intro: list_eq.intros)
|
43
|
779 |
apply(simp add: list_eq_refl)
|
3
|
780 |
done
|
|
781 |
|
18
|
782 |
lemma yyy:
|
3
|
783 |
shows "
|
|
784 |
(
|
|
785 |
(UNION EMPTY s = s) &
|
|
786 |
((UNION (INSERT e s1) s2) = (INSERT e (UNION s1 s2)))
|
|
787 |
) = (
|
|
788 |
((ABS_fset ([] @ REP_fset s)) = s) &
|
|
789 |
((ABS_fset ((e # (REP_fset s1)) @ REP_fset s2)) = ABS_fset (e # (REP_fset s1 @ REP_fset s2)))
|
|
790 |
)"
|
|
791 |
unfolding UNION_def EMPTY_def INSERT_def
|
|
792 |
apply(rule_tac f="(op &)" in arg_cong2)
|
|
793 |
apply(rule_tac f="(op =)" in arg_cong2)
|
14
|
794 |
apply(simp only: QUOT_TYPE_I_fset.thm11[symmetric])
|
7
|
795 |
apply(rule append_respects_fst)
|
18
|
796 |
apply(simp only: QUOT_TYPE_I_fset.REP_ABS_rsp)
|
43
|
797 |
apply(rule list_eq_refl)
|
3
|
798 |
apply(simp)
|
|
799 |
apply(rule_tac f="(op =)" in arg_cong2)
|
14
|
800 |
apply(simp only: QUOT_TYPE_I_fset.thm11[symmetric])
|
7
|
801 |
apply(rule append_respects_fst)
|
14
|
802 |
apply(simp only: QUOT_TYPE_I_fset.REP_ABS_rsp)
|
43
|
803 |
apply(rule list_eq_refl)
|
14
|
804 |
apply(simp only: QUOT_TYPE_I_fset.thm11[symmetric])
|
3
|
805 |
apply(rule list_eq.intros(5))
|
14
|
806 |
apply(simp only: QUOT_TYPE_I_fset.REP_ABS_rsp)
|
43
|
807 |
apply(rule list_eq_refl)
|
3
|
808 |
done
|
|
809 |
|
|
810 |
lemma
|
|
811 |
shows "
|
|
812 |
(UNION EMPTY s = s) &
|
7
|
813 |
((UNION (INSERT e s1) s2) = (INSERT e (UNION s1 s2)))"
|
|
814 |
apply (simp add: yyy)
|
24
|
815 |
apply (simp add: QUOT_TYPE_I_fset.thm10)
|
7
|
816 |
done
|
3
|
817 |
|
8
|
818 |
ML {*
|
|
819 |
fun mk_rep_abs x = @{term REP_fset} $ (@{term ABS_fset} $ x)
|
41
|
820 |
val consts = [@{const_name "Nil"}, @{const_name "append"},
|
|
821 |
@{const_name "Cons"}, @{const_name "membship"},
|
|
822 |
@{const_name "card1"}]
|
8
|
823 |
*}
|
|
824 |
|
|
825 |
ML {*
|
43
|
826 |
fun build_goal ctxt thm constructors lifted_type mk_rep_abs =
|
8
|
827 |
let
|
43
|
828 |
fun is_constructor (Const (x, _)) = member (op =) constructors x
|
|
829 |
| is_constructor _ = false;
|
|
830 |
|
|
831 |
fun maybe_mk_rep_abs t =
|
|
832 |
let
|
|
833 |
val _ = writeln ("Maybe: " ^ Syntax.string_of_term ctxt t)
|
|
834 |
in
|
|
835 |
if type_of t = lifted_type then mk_rep_abs t else t
|
|
836 |
end;
|
|
837 |
|
|
838 |
fun build_aux ctxt1 tm =
|
|
839 |
let
|
|
840 |
val (head, args) = Term.strip_comb tm;
|
|
841 |
val args' = map (build_aux ctxt1) args;
|
|
842 |
in
|
|
843 |
(case head of
|
|
844 |
Abs (x, T, t) =>
|
|
845 |
let
|
|
846 |
val ([x'], ctxt2) = Variable.variant_fixes [x] ctxt1;
|
|
847 |
val v = Free (x', T);
|
|
848 |
val t' = subst_bound (v, t);
|
|
849 |
val rec_term = build_aux ctxt2 t';
|
|
850 |
in Term.lambda_name (x, v) rec_term end
|
|
851 |
| _ =>
|
|
852 |
if is_constructor head then
|
|
853 |
maybe_mk_rep_abs (list_comb (head, map maybe_mk_rep_abs args'))
|
|
854 |
else list_comb (head, args'))
|
|
855 |
end;
|
|
856 |
|
|
857 |
val concl2 = Thm.prop_of thm;
|
|
858 |
in
|
|
859 |
Logic.mk_equals (build_aux ctxt concl2, concl2)
|
|
860 |
end
|
|
861 |
*}
|
|
862 |
|
|
863 |
ML {*
|
|
864 |
fun build_goal' ctxt thm constructors lifted_type mk_rep_abs =
|
|
865 |
let
|
|
866 |
fun is_const (Const (x, t)) = member (op =) constructors x
|
8
|
867 |
| is_const _ = false
|
41
|
868 |
|
12
|
869 |
fun maybe_mk_rep_abs t =
|
|
870 |
let
|
43
|
871 |
val _ = writeln ("Maybe: " ^ Syntax.string_of_term ctxt t)
|
12
|
872 |
in
|
|
873 |
if type_of t = lifted_type then mk_rep_abs t else t
|
|
874 |
end
|
35
|
875 |
(* handle TYPE _ => t*)
|
43
|
876 |
fun build_aux ctxt1 (Abs (x, T, t)) =
|
|
877 |
let
|
|
878 |
val ([x'], ctxt2) = Variable.variant_fixes [x] ctxt1;
|
|
879 |
val v = Free (x', T);
|
|
880 |
val t' = subst_bound (v, t);
|
|
881 |
val rec_term = build_aux ctxt2 t';
|
|
882 |
in Term.lambda_name (x, v) rec_term end
|
|
883 |
| build_aux ctxt1 (f $ a) =
|
11
|
884 |
let
|
16
|
885 |
val (f, args) = strip_comb (f $ a)
|
41
|
886 |
val _ = writeln (Syntax.string_of_term ctxt f)
|
11
|
887 |
in
|
43
|
888 |
if is_const f then
|
|
889 |
maybe_mk_rep_abs
|
|
890 |
(list_comb (f, map maybe_mk_rep_abs (map (build_aux ctxt1) args)))
|
|
891 |
else list_comb (build_aux ctxt1 f, map (build_aux ctxt1) args)
|
11
|
892 |
end
|
43
|
893 |
| build_aux _ x =
|
12
|
894 |
if is_const x then maybe_mk_rep_abs x else x
|
41
|
895 |
|
31
|
896 |
val concl2 = term_of (#prop (crep_thm thm))
|
8
|
897 |
in
|
43
|
898 |
Logic.mk_equals (build_aux ctxt concl2, concl2)
|
8
|
899 |
end *}
|
|
900 |
|
29
|
901 |
ML {* val fset_defs = @{thms EMPTY_def IN_def UNION_def card_def INSERT_def} *}
|
34
|
902 |
ML {* val fset_defs_sym = map (fn t => symmetric (unlam_def @{context} t)) fset_defs *}
|
18
|
903 |
|
|
904 |
ML {*
|
|
905 |
fun dest_cbinop t =
|
|
906 |
let
|
|
907 |
val (t2, rhs) = Thm.dest_comb t;
|
|
908 |
val (bop, lhs) = Thm.dest_comb t2;
|
|
909 |
in
|
|
910 |
(bop, (lhs, rhs))
|
|
911 |
end
|
|
912 |
*}
|
38
|
913 |
|
18
|
914 |
ML {*
|
|
915 |
fun dest_ceq t =
|
|
916 |
let
|
|
917 |
val (bop, pair) = dest_cbinop t;
|
|
918 |
val (bop_s, _) = Term.dest_Const (Thm.term_of bop);
|
|
919 |
in
|
|
920 |
if bop_s = "op =" then pair else (raise CTERM ("Not an equality", [t]))
|
|
921 |
end
|
|
922 |
*}
|
38
|
923 |
|
19
|
924 |
ML Thm.instantiate
|
|
925 |
ML {*@{thm arg_cong2}*}
|
|
926 |
ML {*@{thm arg_cong2[of _ _ _ _ "op ="]} *}
|
|
927 |
ML {* val cT = @{cpat "op ="} |> Thm.ctyp_of_term |> Thm.dest_ctyp |> hd *}
|
25
|
928 |
ML {*
|
19
|
929 |
Toplevel.program (fn () =>
|
38
|
930 |
Drule.instantiate' [SOME cT,SOME cT,SOME @{ctyp bool}] [NONE,NONE,NONE,NONE,SOME (@{cpat "op ="})] @{thm arg_cong2}
|
19
|
931 |
)
|
|
932 |
*}
|
|
933 |
|
18
|
934 |
ML {*
|
35
|
935 |
fun split_binop_conv t =
|
18
|
936 |
let
|
|
937 |
val (lhs, rhs) = dest_ceq t;
|
|
938 |
val (bop, _) = dest_cbinop lhs;
|
19
|
939 |
val [clT, cr2] = bop |> Thm.ctyp_of_term |> Thm.dest_ctyp;
|
|
940 |
val [cmT, crT] = Thm.dest_ctyp cr2;
|
18
|
941 |
in
|
36
|
942 |
Drule.instantiate' [SOME clT, SOME cmT, SOME crT] [NONE, NONE, NONE, NONE, SOME bop] @{thm arg_cong2}
|
18
|
943 |
end
|
|
944 |
*}
|
19
|
945 |
|
18
|
946 |
ML {*
|
35
|
947 |
fun split_arg_conv t =
|
|
948 |
let
|
|
949 |
val (lhs, rhs) = dest_ceq t;
|
|
950 |
val (lop, larg) = Thm.dest_comb lhs;
|
|
951 |
val [caT, crT] = lop |> Thm.ctyp_of_term |> Thm.dest_ctyp;
|
|
952 |
in
|
36
|
953 |
Drule.instantiate' [SOME caT, SOME crT] [NONE, NONE, SOME lop] @{thm arg_cong}
|
35
|
954 |
end
|
|
955 |
*}
|
|
956 |
|
|
957 |
ML {*
|
|
958 |
fun split_binop_tac n thm =
|
18
|
959 |
let
|
19
|
960 |
val concl = Thm.cprem_of thm n;
|
|
961 |
val (_, cconcl) = Thm.dest_comb concl;
|
35
|
962 |
val rewr = split_binop_conv cconcl;
|
|
963 |
in
|
|
964 |
rtac rewr n thm
|
|
965 |
end
|
|
966 |
handle CTERM _ => Seq.empty
|
|
967 |
*}
|
|
968 |
|
|
969 |
ML {*
|
|
970 |
fun split_arg_tac n thm =
|
|
971 |
let
|
|
972 |
val concl = Thm.cprem_of thm n;
|
|
973 |
val (_, cconcl) = Thm.dest_comb concl;
|
|
974 |
val rewr = split_arg_conv cconcl;
|
18
|
975 |
in
|
19
|
976 |
rtac rewr n thm
|
18
|
977 |
end
|
19
|
978 |
handle CTERM _ => Seq.empty
|
18
|
979 |
*}
|
19
|
980 |
|
20
|
981 |
(* Has all the theorems about fset plugged in. These should be parameters to the tactic *)
|
|
982 |
|
32
|
983 |
lemma trueprop_cong:
|
|
984 |
shows "(a \<equiv> b) \<Longrightarrow> (Trueprop a \<equiv> Trueprop b)"
|
|
985 |
by auto
|
|
986 |
|
43
|
987 |
|
|
988 |
thm QUOT_TYPE_I_fset.R_trans2
|
19
|
989 |
ML {*
|
25
|
990 |
fun foo_tac' ctxt =
|
|
991 |
REPEAT_ALL_NEW (FIRST' [
|
43
|
992 |
(* rtac @{thm trueprop_cong},*)
|
|
993 |
rtac @{thm list_eq_refl},
|
19
|
994 |
rtac @{thm cons_preserves},
|
|
995 |
rtac @{thm mem_respects},
|
34
|
996 |
rtac @{thm card1_rsp},
|
25
|
997 |
rtac @{thm QUOT_TYPE_I_fset.R_trans2},
|
|
998 |
CHANGED o (simp_tac ((Simplifier.context ctxt HOL_ss) addsimps @{thms QUOT_TYPE_I_fset.REP_ABS_rsp})),
|
35
|
999 |
DatatypeAux.cong_tac,
|
|
1000 |
rtac @{thm ext},
|
43
|
1001 |
rtac @{thm eq_reflection},
|
32
|
1002 |
CHANGED o (ObjectLogic.full_atomize_tac)
|
25
|
1003 |
])
|
19
|
1004 |
*}
|
|
1005 |
|
20
|
1006 |
ML {*
|
44
|
1007 |
<<<<<<< variant A
|
36
|
1008 |
val m1_novars = snd(no_vars ((Context.Theory @{theory}), @{thm m1}))
|
43
|
1009 |
val goal = build_goal @{context} m1_novars consts @{typ "'a list"} mk_rep_abs
|
44
|
1010 |
>>>>>>> variant B
|
38
|
1011 |
val m1_novars = snd(no_vars ((Context.Theory @{theory}),@{thm m1}))
|
41
|
1012 |
val goal = build_goal @{context} m1_novars consts @{typ "'a list"} mk_rep_abs
|
44
|
1013 |
####### Ancestor
|
|
1014 |
val m1_novars = snd(no_vars ((Context.Theory @{theory}), @{thm m1}))
|
|
1015 |
val goal = build_goal m1_novars consts @{typ "'a list"} mk_rep_abs
|
|
1016 |
======= end
|
20
|
1017 |
val cgoal = cterm_of @{theory} goal
|
21
|
1018 |
val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite false fset_defs_sym cgoal)
|
20
|
1019 |
*}
|
|
1020 |
|
30
e35198635d64
Using "atomize" the versions with arbitrary Trueprops can be proven.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1021 |
(*notation ( output) "prop" ("#_" [1000] 1000) *)
|
e35198635d64
Using "atomize" the versions with arbitrary Trueprops can be proven.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1022 |
notation ( output) "Trueprop" ("#_" [1000] 1000)
|
29
|
1023 |
|
43
|
1024 |
lemma atomize_eqv [atomize]: "(Trueprop A \<equiv> Trueprop B) \<equiv> (A \<equiv> B)"
|
|
1025 |
(is "?rhs \<equiv> ?lhs")
|
|
1026 |
proof
|
|
1027 |
assume "PROP ?lhs" then show "PROP ?rhs" by unfold
|
|
1028 |
next
|
|
1029 |
assume *: "PROP ?rhs"
|
|
1030 |
have "A = B"
|
|
1031 |
proof (cases A)
|
|
1032 |
case True
|
|
1033 |
with * have B by unfold
|
|
1034 |
with `A` show ?thesis by simp
|
|
1035 |
next
|
|
1036 |
case False
|
|
1037 |
with * have "~ B" by auto
|
|
1038 |
with `~ A` show ?thesis by simp
|
|
1039 |
qed
|
|
1040 |
then show "PROP ?lhs" by (rule eq_reflection)
|
|
1041 |
qed
|
|
1042 |
|
|
1043 |
|
30
e35198635d64
Using "atomize" the versions with arbitrary Trueprops can be proven.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1044 |
prove {* (Thm.term_of cgoal2) *}
|
21
|
1045 |
apply (tactic {* LocalDefs.unfold_tac @{context} fset_defs *} )
|
43
|
1046 |
apply (atomize (full))
|
|
1047 |
|
|
1048 |
apply (rule trueprop_cong)
|
25
|
1049 |
apply (tactic {* foo_tac' @{context} 1 *})
|
43
|
1050 |
thm eq_reflection
|
30
e35198635d64
Using "atomize" the versions with arbitrary Trueprops can be proven.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1051 |
done
|
18
|
1052 |
|
20
|
1053 |
thm length_append (* Not true but worth checking that the goal is correct *)
|
|
1054 |
ML {*
|
36
|
1055 |
val m1_novars = snd(no_vars ((Context.Theory @{theory}), @{thm length_append}))
|
20
|
1056 |
val goal = build_goal m1_novars consts @{typ "'a list"} mk_rep_abs
|
|
1057 |
val cgoal = cterm_of @{theory} goal
|
21
|
1058 |
val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite false fset_defs_sym cgoal)
|
20
|
1059 |
*}
|
30
e35198635d64
Using "atomize" the versions with arbitrary Trueprops can be proven.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1060 |
prove {* Thm.term_of cgoal2 *}
|
21
|
1061 |
apply (tactic {* LocalDefs.unfold_tac @{context} fset_defs *} )
|
30
e35198635d64
Using "atomize" the versions with arbitrary Trueprops can be proven.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1062 |
apply (tactic {* foo_tac' @{context} 1 *})
|
25
|
1063 |
sorry
|
19
|
1064 |
|
20
|
1065 |
thm m2
|
|
1066 |
ML {*
|
36
|
1067 |
val m1_novars = snd(no_vars ((Context.Theory @{theory}), @{thm m2}))
|
20
|
1068 |
val goal = build_goal m1_novars consts @{typ "'a list"} mk_rep_abs
|
|
1069 |
val cgoal = cterm_of @{theory} goal
|
21
|
1070 |
val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite false fset_defs_sym cgoal)
|
20
|
1071 |
*}
|
30
e35198635d64
Using "atomize" the versions with arbitrary Trueprops can be proven.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1072 |
prove {* Thm.term_of cgoal2 *}
|
21
|
1073 |
apply (tactic {* LocalDefs.unfold_tac @{context} fset_defs *} )
|
30
e35198635d64
Using "atomize" the versions with arbitrary Trueprops can be proven.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1074 |
apply (tactic {* foo_tac' @{context} 1 *})
|
e35198635d64
Using "atomize" the versions with arbitrary Trueprops can be proven.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1075 |
done
|
21
|
1076 |
|
|
1077 |
thm list_eq.intros(4)
|
|
1078 |
ML {*
|
36
|
1079 |
val m1_novars = snd(no_vars ((Context.Theory @{theory}), @{thm list_eq.intros(4)}))
|
21
|
1080 |
val goal = build_goal m1_novars consts @{typ "'a list"} mk_rep_abs
|
|
1081 |
val cgoal = cterm_of @{theory} goal
|
|
1082 |
val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite false fset_defs_sym cgoal)
|
24
|
1083 |
val cgoal3 = Thm.rhs_of (MetaSimplifier.rewrite true @{thms QUOT_TYPE_I_fset.thm10} cgoal2)
|
21
|
1084 |
*}
|
|
1085 |
|
24
|
1086 |
(* It is the same, but we need a name for it. *)
|
30
e35198635d64
Using "atomize" the versions with arbitrary Trueprops can be proven.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1087 |
prove {* Thm.term_of cgoal3 *}
|
29
|
1088 |
apply (tactic {* LocalDefs.unfold_tac @{context} fset_defs *} )
|
|
1089 |
apply (tactic {* foo_tac' @{context} 1 *})
|
30
e35198635d64
Using "atomize" the versions with arbitrary Trueprops can be proven.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1090 |
done
|
24
|
1091 |
lemma zzz :
|
30
e35198635d64
Using "atomize" the versions with arbitrary Trueprops can be proven.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1092 |
"Trueprop (REP_fset (INSERT a (INSERT a (ABS_fset xs))) \<approx> REP_fset (INSERT a (ABS_fset xs)))
|
e35198635d64
Using "atomize" the versions with arbitrary Trueprops can be proven.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1093 |
\<equiv> Trueprop (a # a # xs \<approx> a # xs)"
|
21
|
1094 |
apply (tactic {* LocalDefs.unfold_tac @{context} fset_defs *} )
|
25
|
1095 |
apply (tactic {* foo_tac' @{context} 1 *})
|
21
|
1096 |
done
|
|
1097 |
|
24
|
1098 |
lemma zzz' :
|
|
1099 |
"(REP_fset (INSERT a (INSERT a (ABS_fset xs))) \<approx> REP_fset (INSERT a (ABS_fset xs)))"
|
|
1100 |
using list_eq.intros(4) by (simp only: zzz)
|
|
1101 |
|
|
1102 |
thm QUOT_TYPE_I_fset.REPS_same
|
25
|
1103 |
ML {* val zzz'' = MetaSimplifier.rewrite_rule @{thms QUOT_TYPE_I_fset.REPS_same} @{thm zzz'} *}
|
|
1104 |
|
21
|
1105 |
thm list_eq.intros(5)
|
|
1106 |
ML {*
|
36
|
1107 |
val m1_novars = snd(no_vars ((Context.Theory @{theory}), @{thm list_eq.intros(5)}))
|
21
|
1108 |
val goal = build_goal m1_novars consts @{typ "'a list"} mk_rep_abs
|
38
|
1109 |
*}
|
|
1110 |
ML {*
|
36
|
1111 |
val cgoal =
|
29
|
1112 |
Toplevel.program (fn () =>
|
|
1113 |
cterm_of @{theory} goal
|
|
1114 |
)
|
|
1115 |
*}
|
|
1116 |
ML {*
|
34
|
1117 |
val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite true fset_defs_sym cgoal)
|
21
|
1118 |
*}
|
30
e35198635d64
Using "atomize" the versions with arbitrary Trueprops can be proven.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1119 |
prove {* Thm.term_of cgoal2 *}
|
21
|
1120 |
apply (tactic {* LocalDefs.unfold_tac @{context} fset_defs *} )
|
25
|
1121 |
apply (tactic {* foo_tac' @{context} 1 *})
|
21
|
1122 |
done
|
18
|
1123 |
|
29
|
1124 |
thm list.induct
|
|
1125 |
|
|
1126 |
ML {*
|
38
|
1127 |
val m1_novars = snd(no_vars ((Context.Theory @{theory}),@{thm list.induct}))
|
29
|
1128 |
*}
|
|
1129 |
ML {*
|
|
1130 |
val goal =
|
|
1131 |
Toplevel.program (fn () =>
|
41
|
1132 |
build_goal @{context} m1_novars consts @{typ "'a list"} mk_rep_abs
|
29
|
1133 |
)
|
|
1134 |
*}
|
|
1135 |
ML {*
|
|
1136 |
val cgoal = cterm_of @{theory} goal
|
35
|
1137 |
*}
|
|
1138 |
ML {*
|
34
|
1139 |
val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite true fset_defs_sym cgoal)
|
29
|
1140 |
*}
|
35
|
1141 |
|
33
e8f1fa50329a
Found the source for the exception and added a handle for it.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1142 |
prove {* (Thm.term_of cgoal2) *}
|
e8f1fa50329a
Found the source for the exception and added a handle for it.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1143 |
apply (tactic {* LocalDefs.unfold_tac @{context} fset_defs *} )
|
34
|
1144 |
apply (tactic {* foo_tac' @{context} 1 *})
|
33
e8f1fa50329a
Found the source for the exception and added a handle for it.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1145 |
sorry
|
29
|
1146 |
|
37
|
1147 |
ML {*
|
|
1148 |
fun lift_theorem_fset_aux thm lthy =
|
|
1149 |
let
|
|
1150 |
val ((_, [novars]), lthy2) = Variable.import true [thm] lthy;
|
|
1151 |
val goal = build_goal novars consts @{typ "'a list"} mk_rep_abs;
|
|
1152 |
val cgoal = cterm_of @{theory} goal;
|
|
1153 |
val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite true fset_defs_sym cgoal);
|
|
1154 |
val tac = (LocalDefs.unfold_tac @{context} fset_defs) THEN (foo_tac' @{context}) 1;
|
|
1155 |
val cthm = Goal.prove_internal [] cgoal2 (fn _ => tac);
|
|
1156 |
val nthm = MetaSimplifier.rewrite_rule [symmetric cthm] (snd (no_vars (Context.Theory @{theory}, thm)))
|
|
1157 |
val nthm2 = MetaSimplifier.rewrite_rule @{thms QUOT_TYPE_I_fset.REPS_same QUOT_TYPE_I_fset.thm10} nthm;
|
|
1158 |
val [nthm3] = ProofContext.export lthy2 lthy [nthm2]
|
|
1159 |
in
|
|
1160 |
nthm3
|
|
1161 |
end
|
|
1162 |
*}
|
|
1163 |
|
|
1164 |
ML {* lift_theorem_fset_aux @{thm m1} @{context} *}
|
|
1165 |
|
|
1166 |
ML {*
|
|
1167 |
fun lift_theorem_fset name thm lthy =
|
|
1168 |
let
|
|
1169 |
val lifted_thm = lift_theorem_fset_aux thm lthy;
|
|
1170 |
val (_, lthy2) = note_thm (name, lifted_thm) lthy;
|
|
1171 |
in
|
|
1172 |
lthy2
|
|
1173 |
end;
|
|
1174 |
*}
|
|
1175 |
|
|
1176 |
local_setup {* lift_theorem_fset @{binding "m1_lift"} @{thm m1} *}
|
|
1177 |
local_setup {* lift_theorem_fset @{binding "leqi4_lift"} @{thm list_eq.intros(4)} *}
|
|
1178 |
local_setup {* lift_theorem_fset @{binding "leqi5_lift"} @{thm list_eq.intros(5)} *}
|
|
1179 |
local_setup {* lift_theorem_fset @{binding "m2_lift"} @{thm m2} *}
|
|
1180 |
|
|
1181 |
thm m1_lift
|
|
1182 |
thm leqi4_lift
|
|
1183 |
thm leqi5_lift
|
|
1184 |
thm m2_lift
|
|
1185 |
|
|
1186 |
ML Drule.instantiate'
|
|
1187 |
text {*
|
|
1188 |
We lost the schematic variable again :(.
|
|
1189 |
Another variable export will still be necessary...
|
|
1190 |
*}
|
|
1191 |
ML {*
|
|
1192 |
Toplevel.program (fn () =>
|
|
1193 |
MetaSimplifier.rewrite_rule @{thms QUOT_TYPE_I_fset.thm10} (
|
|
1194 |
Drule.instantiate' [] [NONE, NONE, SOME (@{cpat "REP_fset x"})] @{thm m2_lift}
|
|
1195 |
)
|
|
1196 |
)
|
|
1197 |
*}
|
|
1198 |
|
42
|
1199 |
thm leqi4_lift
|
|
1200 |
ML {*
|
|
1201 |
val (nam, typ) = (hd (Term.add_vars (term_of (#prop (crep_thm @{thm leqi4_lift}))) []))
|
|
1202 |
val (_, l) = dest_Type typ
|
|
1203 |
val t = Type ("QuotMain.fset", l)
|
|
1204 |
val v = Var (nam, t)
|
|
1205 |
val cv = cterm_of @{theory} ((term_of @{cpat "REP_fset"}) $ v)
|
|
1206 |
*}
|
|
1207 |
|
|
1208 |
ML {*
|
|
1209 |
term_of (#prop (crep_thm @{thm sym}))
|
|
1210 |
*}
|
|
1211 |
|
|
1212 |
ML {*
|
|
1213 |
Toplevel.program (fn () =>
|
|
1214 |
MetaSimplifier.rewrite_rule @{thms QUOT_TYPE_I_fset.thm10} (
|
|
1215 |
Drule.instantiate' [] [NONE, SOME (cv)] @{thm leqi4_lift}
|
|
1216 |
)
|
|
1217 |
)
|
|
1218 |
*}
|
|
1219 |
|
|
1220 |
|
37
|
1221 |
|
|
1222 |
|
|
1223 |
|
|
1224 |
ML {* MRS *}
|
29
|
1225 |
thm card1_suc
|
|
1226 |
|
|
1227 |
ML {*
|
38
|
1228 |
val m1_novars = snd(no_vars ((Context.Theory @{theory}),@{thm card1_suc}))
|
29
|
1229 |
*}
|
|
1230 |
ML {*
|
41
|
1231 |
val goal = build_goal @{context} m1_novars consts @{typ "'a list"} mk_rep_abs
|
29
|
1232 |
*}
|
|
1233 |
ML {*
|
|
1234 |
val cgoal = cterm_of @{theory} goal
|
34
|
1235 |
val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite true fset_defs_sym cgoal)
|
29
|
1236 |
*}
|
35
|
1237 |
ML {* @{term "\<exists>x. P x"} *}
|
42
|
1238 |
ML {* Thm.bicompose *}
|
33
e8f1fa50329a
Found the source for the exception and added a handle for it.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
1239 |
prove {* (Thm.term_of cgoal2) *}
|
42
|
1240 |
apply (tactic {* LocalDefs.unfold_tac @{context} fset_defs *} )
|
34
|
1241 |
apply (tactic {* foo_tac' @{context} 1 *})
|
29
|
1242 |
|
|
1243 |
|
18
|
1244 |
(*
|
|
1245 |
datatype obj1 =
|
|
1246 |
OVAR1 "string"
|
|
1247 |
| OBJ1 "(string * (string \<Rightarrow> obj1)) list"
|
|
1248 |
| INVOKE1 "obj1 \<Rightarrow> string"
|
|
1249 |
| UPDATE1 "obj1 \<Rightarrow> string \<Rightarrow> (string \<Rightarrow> obj1)"
|
|
1250 |
*)
|
21
|
1251 |
|
|
1252 |
|