0
|
1 |
theory QuotMain
|
6
|
2 |
imports QuotScript QuotList Prove
|
0
|
3 |
begin
|
|
4 |
|
6
|
5 |
(*
|
|
6 |
prove {* @{prop "True"} *}
|
|
7 |
apply(rule TrueI)
|
|
8 |
done
|
|
9 |
*)
|
|
10 |
|
0
|
11 |
locale QUOT_TYPE =
|
|
12 |
fixes R :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
|
|
13 |
and Abs :: "('a \<Rightarrow> bool) \<Rightarrow> 'b"
|
|
14 |
and Rep :: "'b \<Rightarrow> ('a \<Rightarrow> bool)"
|
|
15 |
assumes equiv: "EQUIV R"
|
|
16 |
and rep_prop: "\<And>y. \<exists>x. Rep y = R x"
|
|
17 |
and rep_inverse: "\<And>x. Abs (Rep x) = x"
|
|
18 |
and abs_inverse: "\<And>x. (Rep (Abs (R x))) = (R x)"
|
|
19 |
and rep_inject: "\<And>x y. (Rep x = Rep y) = (x = y)"
|
|
20 |
begin
|
|
21 |
|
|
22 |
definition
|
|
23 |
"ABS x \<equiv> Abs (R x)"
|
|
24 |
|
|
25 |
definition
|
|
26 |
"REP a = Eps (Rep a)"
|
|
27 |
|
|
28 |
lemma lem9:
|
|
29 |
shows "R (Eps (R x)) = R x"
|
|
30 |
proof -
|
|
31 |
have a: "R x x" using equiv by (simp add: EQUIV_REFL_SYM_TRANS REFL_def)
|
|
32 |
then have "R x (Eps (R x))" by (rule someI)
|
|
33 |
then show "R (Eps (R x)) = R x"
|
|
34 |
using equiv unfolding EQUIV_def by simp
|
|
35 |
qed
|
|
36 |
|
|
37 |
theorem thm10:
|
|
38 |
shows "ABS (REP a) = a"
|
|
39 |
unfolding ABS_def REP_def
|
|
40 |
proof -
|
|
41 |
from rep_prop
|
|
42 |
obtain x where eq: "Rep a = R x" by auto
|
|
43 |
have "Abs (R (Eps (Rep a))) = Abs (R (Eps (R x)))" using eq by simp
|
|
44 |
also have "\<dots> = Abs (R x)" using lem9 by simp
|
|
45 |
also have "\<dots> = Abs (Rep a)" using eq by simp
|
|
46 |
also have "\<dots> = a" using rep_inverse by simp
|
|
47 |
finally
|
|
48 |
show "Abs (R (Eps (Rep a))) = a" by simp
|
|
49 |
qed
|
|
50 |
|
|
51 |
lemma REP_refl:
|
|
52 |
shows "R (REP a) (REP a)"
|
|
53 |
unfolding REP_def
|
|
54 |
by (simp add: equiv[simplified EQUIV_def])
|
|
55 |
|
|
56 |
lemma lem7:
|
|
57 |
"(R x = R y) = (Abs (R x) = Abs (R y))"
|
|
58 |
apply(rule iffI)
|
|
59 |
apply(simp)
|
|
60 |
apply(drule rep_inject[THEN iffD2])
|
|
61 |
apply(simp add: abs_inverse)
|
|
62 |
done
|
|
63 |
|
|
64 |
theorem thm11:
|
|
65 |
shows "R r r' = (ABS r = ABS r')"
|
|
66 |
unfolding ABS_def
|
|
67 |
by (simp only: equiv[simplified EQUIV_def] lem7)
|
|
68 |
|
4
|
69 |
|
2
|
70 |
lemma REP_ABS_rsp:
|
4
|
71 |
shows "R f (REP (ABS g)) = R f g"
|
|
72 |
and "R (REP (ABS g)) f = R g f"
|
|
73 |
apply(subst thm11)
|
|
74 |
apply(simp add: thm10 thm11)
|
|
75 |
apply(subst thm11)
|
|
76 |
apply(simp add: thm10 thm11)
|
|
77 |
done
|
|
78 |
|
0
|
79 |
lemma QUOTIENT:
|
|
80 |
"QUOTIENT R ABS REP"
|
|
81 |
apply(unfold QUOTIENT_def)
|
|
82 |
apply(simp add: thm10)
|
|
83 |
apply(simp add: REP_refl)
|
|
84 |
apply(subst thm11[symmetric])
|
|
85 |
apply(simp add: equiv[simplified EQUIV_def])
|
|
86 |
done
|
|
87 |
|
|
88 |
end
|
|
89 |
|
|
90 |
section {* type definition for the quotient type *}
|
|
91 |
|
|
92 |
ML {*
|
2
|
93 |
Variable.variant_frees
|
|
94 |
*}
|
|
95 |
|
|
96 |
|
|
97 |
ML {*
|
1
|
98 |
(* constructs the term \<lambda>(c::ty \<Rightarrow> bool). \<exists>x. c = rel x *)
|
0
|
99 |
fun typedef_term rel ty lthy =
|
|
100 |
let
|
|
101 |
val [x, c] = [("x", ty), ("c", ty --> @{typ bool})]
|
|
102 |
|> Variable.variant_frees lthy [rel]
|
|
103 |
|> map Free
|
|
104 |
in
|
1
|
105 |
lambda c
|
|
106 |
(HOLogic.mk_exists
|
|
107 |
("x", ty, HOLogic.mk_eq (c, (rel $ x))))
|
0
|
108 |
end
|
|
109 |
*}
|
|
110 |
|
14
|
111 |
(*
|
0
|
112 |
ML {*
|
14
|
113 |
typedef_term @{term R} @{typ "nat"} @{context}
|
2
|
114 |
|> Syntax.string_of_term @{context}
|
|
115 |
|> writeln
|
14
|
116 |
*}*)
|
2
|
117 |
|
|
118 |
ML {*
|
0
|
119 |
val typedef_tac =
|
|
120 |
EVERY1 [rewrite_goal_tac @{thms mem_def},
|
|
121 |
rtac @{thm exI}, rtac @{thm exI}, rtac @{thm refl}]
|
|
122 |
*}
|
|
123 |
|
|
124 |
ML {*
|
|
125 |
(* makes the new type definitions *)
|
|
126 |
fun typedef_make (qty_name, rel, ty) lthy =
|
|
127 |
LocalTheory.theory_result
|
1
|
128 |
(Typedef.add_typedef false NONE
|
0
|
129 |
(qty_name, map fst (Term.add_tfreesT ty []), NoSyn)
|
|
130 |
(typedef_term rel ty lthy)
|
|
131 |
NONE typedef_tac) lthy
|
|
132 |
*}
|
|
133 |
|
2
|
134 |
text {* proves the QUOT_TYPE theorem for the new type *}
|
0
|
135 |
ML {*
|
1
|
136 |
fun typedef_quot_type_tac equiv_thm (typedef_info: Typedef.info) =
|
0
|
137 |
let
|
|
138 |
val rep_thm = #Rep typedef_info
|
|
139 |
val rep_inv = #Rep_inverse typedef_info
|
|
140 |
val abs_inv = #Abs_inverse typedef_info
|
|
141 |
val rep_inj = #Rep_inject typedef_info
|
|
142 |
|
|
143 |
val ss = HOL_basic_ss addsimps @{thms mem_def}
|
|
144 |
val rep_thm_simpd = Simplifier.asm_full_simplify ss rep_thm
|
|
145 |
val abs_inv_simpd = Simplifier.asm_full_simplify ss abs_inv
|
|
146 |
in
|
|
147 |
EVERY1 [rtac @{thm QUOT_TYPE.intro},
|
|
148 |
rtac equiv_thm,
|
|
149 |
rtac rep_thm_simpd,
|
|
150 |
rtac rep_inv,
|
|
151 |
rtac abs_inv_simpd, rtac @{thm exI}, rtac @{thm refl},
|
|
152 |
rtac rep_inj]
|
|
153 |
end
|
|
154 |
*}
|
|
155 |
|
2
|
156 |
term QUOT_TYPE
|
|
157 |
ML {* HOLogic.mk_Trueprop *}
|
|
158 |
ML {* Goal.prove *}
|
|
159 |
ML {* Syntax.check_term *}
|
|
160 |
|
0
|
161 |
ML {*
|
|
162 |
fun typedef_quot_type_thm (rel, abs, rep, equiv_thm, typedef_info) lthy =
|
|
163 |
let
|
|
164 |
val quot_type_const = Const (@{const_name "QUOT_TYPE"}, dummyT)
|
|
165 |
val goal = HOLogic.mk_Trueprop (quot_type_const $ rel $ abs $ rep)
|
|
166 |
|> Syntax.check_term lthy
|
|
167 |
in
|
|
168 |
Goal.prove lthy [] [] goal
|
|
169 |
(fn _ => typedef_quot_type_tac equiv_thm typedef_info)
|
|
170 |
end
|
|
171 |
*}
|
|
172 |
|
|
173 |
ML {*
|
|
174 |
fun typedef_quotient_thm_tac defs quot_type_thm =
|
|
175 |
EVERY1 [K (rewrite_goals_tac defs),
|
|
176 |
rtac @{thm QUOT_TYPE.QUOTIENT},
|
|
177 |
rtac quot_type_thm]
|
|
178 |
*}
|
|
179 |
|
|
180 |
ML {*
|
|
181 |
fun typedef_quotient_thm (rel, abs, rep, abs_def, rep_def, quot_type_thm) lthy =
|
|
182 |
let
|
|
183 |
val quotient_const = Const (@{const_name "QUOTIENT"}, dummyT)
|
|
184 |
val goal = HOLogic.mk_Trueprop (quotient_const $ rel $ abs $ rep)
|
|
185 |
|> Syntax.check_term lthy
|
|
186 |
in
|
|
187 |
Goal.prove lthy [] [] goal
|
|
188 |
(fn _ => typedef_quotient_thm_tac [abs_def, rep_def] quot_type_thm)
|
|
189 |
end
|
|
190 |
*}
|
|
191 |
|
|
192 |
text {* two wrappers for define and note *}
|
|
193 |
ML {*
|
|
194 |
fun make_def (name, mx, trm) lthy =
|
|
195 |
let
|
|
196 |
val ((trm, (_ , thm)), lthy') =
|
|
197 |
LocalTheory.define Thm.internalK ((name, mx), (Attrib.empty_binding, trm)) lthy
|
|
198 |
in
|
|
199 |
((trm, thm), lthy')
|
|
200 |
end
|
|
201 |
*}
|
|
202 |
|
14
|
203 |
(*
|
|
204 |
locale foo = fixes x :: nat
|
|
205 |
begin
|
|
206 |
|
|
207 |
ML {* make_def (@{binding foo}, NoSyn, @{term "x + x"}) @{context} *}
|
|
208 |
*)
|
|
209 |
|
0
|
210 |
ML {*
|
|
211 |
fun reg_thm (name, thm) lthy =
|
|
212 |
let
|
|
213 |
val ((_,[thm']), lthy') = LocalTheory.note Thm.theoremK ((name, []), [thm]) lthy
|
|
214 |
in
|
|
215 |
(thm',lthy')
|
|
216 |
end
|
|
217 |
*}
|
|
218 |
|
14
|
219 |
ML {*
|
|
220 |
val no_vars = Thm.rule_attribute (fn context => fn th =>
|
|
221 |
let
|
|
222 |
val ctxt = Variable.set_body false (Context.proof_of context);
|
|
223 |
val ((_, [th']), _) = Variable.import true [th] ctxt;
|
|
224 |
in th' end);
|
|
225 |
*}
|
|
226 |
|
|
227 |
ML ProofContext.theory_of
|
|
228 |
|
|
229 |
ML Expression.interpretation
|
|
230 |
|
|
231 |
ML {*
|
|
232 |
fun my_print_tac ctxt thm =
|
|
233 |
let
|
|
234 |
val _ = tracing (Display.string_of_thm ctxt thm)
|
|
235 |
in
|
|
236 |
Seq.single thm
|
|
237 |
end *}
|
|
238 |
|
|
239 |
ML LocalTheory.theory_result
|
|
240 |
|
|
241 |
|
|
242 |
ML {* Binding.str_of @{binding foo} *}
|
|
243 |
|
|
244 |
ML {*
|
0
|
245 |
fun typedef_main (qty_name, rel, ty, equiv_thm) lthy =
|
|
246 |
let
|
|
247 |
(* generates typedef *)
|
|
248 |
val ((_,typedef_info), lthy') = typedef_make (qty_name, rel, ty) lthy
|
|
249 |
|
|
250 |
(* abs and rep functions *)
|
|
251 |
val abs_ty = #abs_type typedef_info
|
|
252 |
val rep_ty = #rep_type typedef_info
|
|
253 |
val abs_name = #Abs_name typedef_info
|
|
254 |
val rep_name = #Rep_name typedef_info
|
|
255 |
val abs = Const (abs_name, rep_ty --> abs_ty)
|
|
256 |
val rep = Const (rep_name, abs_ty --> rep_ty)
|
|
257 |
|
|
258 |
(* ABS and REP definitions *)
|
|
259 |
val ABS_const = Const (@{const_name "QUOT_TYPE.ABS"}, dummyT )
|
|
260 |
val REP_const = Const (@{const_name "QUOT_TYPE.REP"}, dummyT )
|
|
261 |
val ABS_trm = Syntax.check_term lthy' (ABS_const $ rel $ abs)
|
|
262 |
val REP_trm = Syntax.check_term lthy' (REP_const $ rep)
|
|
263 |
val ABS_name = Binding.prefix_name "ABS_" qty_name
|
|
264 |
val REP_name = Binding.prefix_name "REP_" qty_name
|
|
265 |
val (((ABS, ABS_def), (REP, REP_def)), lthy'') =
|
|
266 |
lthy' |> make_def (ABS_name, NoSyn, ABS_trm)
|
|
267 |
||>> make_def (REP_name, NoSyn, REP_trm)
|
|
268 |
|
14
|
269 |
(* REMOVE VARIFY *)
|
|
270 |
val _ = tracing (Syntax.string_of_typ lthy' (type_of ABS_trm))
|
|
271 |
val _ = tracing (Syntax.string_of_typ lthy' (type_of REP_trm))
|
|
272 |
val _ = tracing (Syntax.string_of_typ lthy' (type_of rel))
|
|
273 |
|
0
|
274 |
(* quot_type theorem *)
|
|
275 |
val quot_thm = typedef_quot_type_thm (rel, abs, rep, equiv_thm, typedef_info) lthy''
|
|
276 |
val quot_thm_name = Binding.prefix_name "QUOT_TYPE_" qty_name
|
|
277 |
|
|
278 |
(* quotient theorem *)
|
|
279 |
val quotient_thm = typedef_quotient_thm (rel, ABS, REP, ABS_def, REP_def, quot_thm) lthy''
|
|
280 |
val quotient_thm_name = Binding.prefix_name "QUOTIENT_" qty_name
|
|
281 |
|
14
|
282 |
(* interpretation *)
|
|
283 |
val bindd = ((Binding.make ("", Position.none)), ([]: Attrib.src list))
|
|
284 |
val ((_, [eqn1pre]), lthy''') = Variable.import true [ABS_def] lthy'';
|
|
285 |
val eqn1i = Thm.prop_of (symmetric eqn1pre)
|
|
286 |
val ((_, [eqn2pre]), lthy'''') = Variable.import true [REP_def] lthy''';
|
|
287 |
val eqn2i = Thm.prop_of (symmetric eqn2pre)
|
|
288 |
|
|
289 |
val exp_morphism = ProofContext.export_morphism lthy'''' (ProofContext.init (ProofContext.theory_of lthy''''));
|
|
290 |
val exp_term = Morphism.term exp_morphism;
|
|
291 |
val exp = Morphism.thm exp_morphism;
|
|
292 |
|
|
293 |
val mthd = Method.SIMPLE_METHOD ((rtac quot_thm 1) THEN
|
|
294 |
ALLGOALS (simp_tac (HOL_basic_ss addsimps [(symmetric (exp ABS_def)), (symmetric (exp REP_def))]))
|
|
295 |
(*THEN print_tac "Hello"*))
|
|
296 |
val mthdt = Method.Basic (fn _ => mthd)
|
|
297 |
val bymt = Proof.global_terminal_proof (mthdt, NONE)
|
|
298 |
val exp_i = [(@{const_name QUOT_TYPE},((("QUOT_TYPE_I_" ^ (Binding.name_of qty_name)), true),
|
|
299 |
Expression.Named [
|
|
300 |
("R", rel),
|
|
301 |
("Abs", abs),
|
|
302 |
("Rep", rep)
|
|
303 |
]))]
|
0
|
304 |
in
|
14
|
305 |
lthy''''
|
|
306 |
|> reg_thm (quot_thm_name, quot_thm)
|
0
|
307 |
||>> reg_thm (quotient_thm_name, quotient_thm)
|
14
|
308 |
||> LocalTheory.theory (fn thy =>
|
|
309 |
let
|
|
310 |
val global_eqns = map exp_term [eqn2i, eqn1i];
|
|
311 |
val (global_eqns2, lthy''''') = Variable.import_terms true global_eqns lthy'''';
|
|
312 |
val global_eqns3 = map (fn t => (bindd, t)) global_eqns2;
|
|
313 |
(* val _ = tracing (Syntax.string_of_typ lthy'''' (type_of (fst (Logic.dest_equals (exp_term eqn1i)))));*)
|
|
314 |
in ProofContext.theory_of (bymt (Expression.interpretation (exp_i, []) global_eqns3 thy)) end)
|
0
|
315 |
end
|
|
316 |
*}
|
|
317 |
|
14
|
318 |
|
0
|
319 |
section {* various tests for quotient types*}
|
|
320 |
datatype trm =
|
|
321 |
var "nat"
|
|
322 |
| app "trm" "trm"
|
|
323 |
| lam "nat" "trm"
|
13
|
324 |
|
14
|
325 |
axiomatization RR :: "trm \<Rightarrow> trm \<Rightarrow> bool" where
|
|
326 |
r_eq: "EQUIV RR"
|
0
|
327 |
|
2
|
328 |
ML {*
|
|
329 |
typedef_main
|
|
330 |
*}
|
|
331 |
|
14
|
332 |
(*ML {*
|
|
333 |
val lthy = @{context};
|
|
334 |
val qty_name = @{binding "qtrm"}
|
|
335 |
val rel = @{term "RR"}
|
|
336 |
val ty = @{typ trm}
|
|
337 |
val equiv_thm = @{thm r_eq}
|
|
338 |
*}*)
|
|
339 |
|
0
|
340 |
local_setup {*
|
14
|
341 |
fn lthy =>
|
|
342 |
Toplevel.program (fn () =>
|
|
343 |
exception_trace (
|
|
344 |
fn () => snd (typedef_main (@{binding "qtrm"}, @{term "RR"}, @{typ trm}, @{thm r_eq}) lthy)
|
|
345 |
)
|
|
346 |
)
|
0
|
347 |
*}
|
14
|
348 |
print_theorems
|
|
349 |
|
|
350 |
(*thm QUOT_TYPE_I_qtrm.thm11*)
|
|
351 |
|
0
|
352 |
|
|
353 |
term Rep_qtrm
|
2
|
354 |
term REP_qtrm
|
0
|
355 |
term Abs_qtrm
|
|
356 |
term ABS_qtrm
|
|
357 |
thm QUOT_TYPE_qtrm
|
|
358 |
thm QUOTIENT_qtrm
|
2
|
359 |
|
0
|
360 |
thm Rep_qtrm
|
|
361 |
|
|
362 |
text {* another test *}
|
13
|
363 |
datatype 'a my = Foo
|
0
|
364 |
consts Rmy :: "'a my \<Rightarrow> 'a my \<Rightarrow> bool"
|
|
365 |
axioms rmy_eq: "EQUIV Rmy"
|
|
366 |
|
|
367 |
term "\<lambda>(c::'a my\<Rightarrow>bool). \<exists>x. c = Rmy x"
|
|
368 |
|
|
369 |
datatype 'a trm' =
|
|
370 |
var' "'a"
|
|
371 |
| app' "'a trm'" "'a trm'"
|
|
372 |
| lam' "'a" "'a trm'"
|
|
373 |
|
|
374 |
consts R' :: "'a trm' \<Rightarrow> 'a trm' \<Rightarrow> bool"
|
|
375 |
axioms r_eq': "EQUIV R'"
|
|
376 |
|
14
|
377 |
|
0
|
378 |
local_setup {*
|
14
|
379 |
fn lthy =>
|
|
380 |
exception_trace
|
|
381 |
(fn () => (typedef_main (@{binding "qtrm'"}, @{term "R'"}, @{typ "'a trm'"}, @{thm r_eq'}) #> snd) lthy)
|
0
|
382 |
*}
|
|
383 |
|
14
|
384 |
print_theorems
|
|
385 |
|
0
|
386 |
term ABS_qtrm'
|
|
387 |
term REP_qtrm'
|
|
388 |
thm QUOT_TYPE_qtrm'
|
|
389 |
thm QUOTIENT_qtrm'
|
|
390 |
thm Rep_qtrm'
|
|
391 |
|
14
|
392 |
|
0
|
393 |
text {* a test with lists of terms *}
|
|
394 |
datatype t =
|
|
395 |
vr "string"
|
|
396 |
| ap "t list"
|
|
397 |
| lm "string" "t"
|
|
398 |
|
|
399 |
consts Rt :: "t \<Rightarrow> t \<Rightarrow> bool"
|
|
400 |
axioms t_eq: "EQUIV Rt"
|
|
401 |
|
|
402 |
local_setup {*
|
|
403 |
typedef_main (@{binding "qt"}, @{term "Rt"}, @{typ "t"}, @{thm t_eq}) #> snd
|
|
404 |
*}
|
|
405 |
|
|
406 |
section {* lifting of constants *}
|
|
407 |
|
|
408 |
text {* information about map-functions for type-constructor *}
|
|
409 |
ML {*
|
|
410 |
type typ_info = {mapfun: string}
|
|
411 |
|
|
412 |
local
|
|
413 |
structure Data = GenericDataFun
|
|
414 |
(type T = typ_info Symtab.table
|
|
415 |
val empty = Symtab.empty
|
|
416 |
val extend = I
|
|
417 |
fun merge _ = Symtab.merge (K true))
|
|
418 |
in
|
|
419 |
val lookup = Symtab.lookup o Data.get
|
|
420 |
fun update k v = Data.map (Symtab.update (k, v))
|
|
421 |
end
|
|
422 |
*}
|
|
423 |
|
|
424 |
(* mapfuns for some standard types *)
|
|
425 |
setup {*
|
|
426 |
Context.theory_map (update @{type_name "list"} {mapfun = @{const_name "map"}})
|
|
427 |
#> Context.theory_map (update @{type_name "*"} {mapfun = @{const_name "prod_fun"}})
|
|
428 |
#> Context.theory_map (update @{type_name "fun"} {mapfun = @{const_name "fun_map"}})
|
|
429 |
*}
|
|
430 |
|
2
|
431 |
|
0
|
432 |
ML {* lookup (Context.Proof @{context}) @{type_name list} *}
|
|
433 |
|
|
434 |
ML {*
|
|
435 |
datatype abs_or_rep = abs | rep
|
|
436 |
|
|
437 |
fun get_fun abs_or_rep rty qty lthy ty =
|
|
438 |
let
|
|
439 |
val qty_name = Long_Name.base_name (fst (dest_Type qty))
|
|
440 |
|
|
441 |
fun get_fun_aux s fs_tys =
|
|
442 |
let
|
|
443 |
val (fs, tys) = split_list fs_tys
|
|
444 |
val (otys, ntys) = split_list tys
|
|
445 |
val oty = Type (s, otys)
|
|
446 |
val nty = Type (s, ntys)
|
|
447 |
val ftys = map (op -->) tys
|
|
448 |
in
|
|
449 |
(case (lookup (Context.Proof lthy) s) of
|
|
450 |
SOME info => (list_comb (Const (#mapfun info, ftys ---> oty --> nty), fs), (oty, nty))
|
|
451 |
| NONE => raise ERROR ("no map association for type " ^ s))
|
|
452 |
end
|
|
453 |
|
|
454 |
fun get_const abs = (Const ("QuotMain.ABS_" ^ qty_name, rty --> qty), (rty, qty))
|
|
455 |
| get_const rep = (Const ("QuotMain.REP_" ^ qty_name, qty --> rty), (qty, rty))
|
|
456 |
in
|
|
457 |
if ty = qty
|
|
458 |
then (get_const abs_or_rep)
|
|
459 |
else (case ty of
|
|
460 |
TFree _ => (Abs ("x", ty, Bound 0), (ty, ty))
|
|
461 |
| Type (_, []) => (Abs ("x", ty, Bound 0), (ty, ty))
|
|
462 |
| Type (s, tys) => get_fun_aux s (map (get_fun abs_or_rep rty qty lthy) tys)
|
|
463 |
| _ => raise ERROR ("no variables")
|
|
464 |
)
|
|
465 |
end
|
|
466 |
*}
|
|
467 |
|
|
468 |
ML {*
|
2
|
469 |
get_fun rep @{typ t} @{typ qt} @{context} @{typ "t * nat"}
|
|
470 |
|> fst
|
|
471 |
|> Syntax.string_of_term @{context}
|
|
472 |
|> writeln
|
|
473 |
*}
|
|
474 |
|
|
475 |
|
|
476 |
ML {*
|
0
|
477 |
fun get_const_def nconst oconst rty qty lthy =
|
|
478 |
let
|
|
479 |
val ty = fastype_of nconst
|
|
480 |
val (arg_tys, res_ty) = strip_type ty
|
14
|
481 |
|
0
|
482 |
val fresh_args = arg_tys |> map (pair "x")
|
|
483 |
|> Variable.variant_frees lthy [nconst, oconst]
|
|
484 |
|> map Free
|
|
485 |
|
|
486 |
val rep_fns = map (fst o get_fun rep rty qty lthy) arg_tys
|
|
487 |
val abs_fn = (fst o get_fun abs rty qty lthy) res_ty
|
|
488 |
|
|
489 |
in
|
|
490 |
map (op $) (rep_fns ~~ fresh_args)
|
|
491 |
|> curry list_comb oconst
|
|
492 |
|> curry (op $) abs_fn
|
|
493 |
|> fold_rev lambda fresh_args
|
|
494 |
end
|
|
495 |
*}
|
|
496 |
|
|
497 |
ML {*
|
|
498 |
fun exchange_ty rty qty ty =
|
|
499 |
if ty = rty then qty
|
|
500 |
else
|
|
501 |
(case ty of
|
|
502 |
Type (s, tys) => Type (s, map (exchange_ty rty qty) tys)
|
|
503 |
| _ => ty)
|
|
504 |
*}
|
|
505 |
|
|
506 |
ML {*
|
|
507 |
fun make_const_def nconst_name oconst mx rty qty lthy =
|
|
508 |
let
|
|
509 |
val oconst_ty = fastype_of oconst
|
|
510 |
val nconst_ty = exchange_ty rty qty oconst_ty
|
|
511 |
val nconst = Const (nconst_name, nconst_ty)
|
|
512 |
val def_trm = get_const_def nconst oconst rty qty lthy
|
|
513 |
in
|
|
514 |
make_def (Binding.name nconst_name, mx, def_trm) lthy
|
|
515 |
end
|
|
516 |
*}
|
|
517 |
|
2
|
518 |
local_setup {*
|
|
519 |
make_const_def "VR" @{term "vr"} NoSyn @{typ "t"} @{typ "qt"} #> snd
|
|
520 |
*}
|
|
521 |
|
|
522 |
local_setup {*
|
|
523 |
make_const_def "AP" @{term "ap"} NoSyn @{typ "t"} @{typ "qt"} #> snd
|
|
524 |
*}
|
|
525 |
|
|
526 |
local_setup {*
|
|
527 |
make_const_def "LM" @{term "lm"} NoSyn @{typ "t"} @{typ "qt"} #> snd
|
|
528 |
*}
|
|
529 |
|
|
530 |
thm VR_def
|
|
531 |
thm AP_def
|
|
532 |
thm LM_def
|
14
|
533 |
term LM
|
2
|
534 |
term VR
|
|
535 |
term AP
|
|
536 |
|
|
537 |
|
0
|
538 |
text {* a test with functions *}
|
|
539 |
datatype 'a t' =
|
|
540 |
vr' "string"
|
|
541 |
| ap' "('a t') * ('a t')"
|
|
542 |
| lm' "'a" "string \<Rightarrow> ('a t')"
|
|
543 |
|
|
544 |
consts Rt' :: "('a t') \<Rightarrow> ('a t') \<Rightarrow> bool"
|
|
545 |
axioms t_eq': "EQUIV Rt'"
|
|
546 |
|
14
|
547 |
|
0
|
548 |
local_setup {*
|
|
549 |
typedef_main (@{binding "qt'"}, @{term "Rt'"}, @{typ "'a t'"}, @{thm t_eq'}) #> snd
|
|
550 |
*}
|
|
551 |
|
14
|
552 |
print_theorems
|
2
|
553 |
|
0
|
554 |
local_setup {*
|
|
555 |
make_const_def "VR'" @{term "vr'"} NoSyn @{typ "'a t'"} @{typ "'a qt'"} #> snd
|
|
556 |
*}
|
|
557 |
|
|
558 |
local_setup {*
|
|
559 |
make_const_def "AP'" @{term "ap'"} NoSyn @{typ "'a t'"} @{typ "'a qt'"} #> snd
|
|
560 |
*}
|
|
561 |
|
|
562 |
local_setup {*
|
|
563 |
make_const_def "LM'" @{term "lm'"} NoSyn @{typ "'a t'"} @{typ "'a qt'"} #> snd
|
|
564 |
*}
|
|
565 |
|
|
566 |
thm VR'_def
|
|
567 |
thm AP'_def
|
|
568 |
thm LM'_def
|
14
|
569 |
term LM'
|
0
|
570 |
term VR'
|
|
571 |
term AP'
|
|
572 |
|
14
|
573 |
|
0
|
574 |
text {* finite set example *}
|
13
|
575 |
print_syntax
|
14
|
576 |
inductive
|
13
|
577 |
list_eq (infix "\<approx>" 50)
|
0
|
578 |
where
|
|
579 |
"a#b#xs \<approx> b#a#xs"
|
|
580 |
| "[] \<approx> []"
|
|
581 |
| "xs \<approx> ys \<Longrightarrow> ys \<approx> xs"
|
|
582 |
| "a#a#xs \<approx> a#xs"
|
|
583 |
| "xs \<approx> ys \<Longrightarrow> a#xs \<approx> a#ys"
|
|
584 |
| "\<lbrakk>xs1 \<approx> xs2; xs2 \<approx> xs3\<rbrakk> \<Longrightarrow> xs1 \<approx> xs3"
|
|
585 |
|
|
586 |
lemma list_eq_sym:
|
|
587 |
shows "xs \<approx> xs"
|
13
|
588 |
apply (induct xs)
|
|
589 |
apply (auto intro: list_eq.intros)
|
|
590 |
done
|
0
|
591 |
|
|
592 |
lemma equiv_list_eq:
|
|
593 |
shows "EQUIV list_eq"
|
13
|
594 |
unfolding EQUIV_REFL_SYM_TRANS REFL_def SYM_def TRANS_def
|
|
595 |
apply(auto intro: list_eq.intros list_eq_sym)
|
|
596 |
done
|
0
|
597 |
|
|
598 |
local_setup {*
|
|
599 |
typedef_main (@{binding "fset"}, @{term "list_eq"}, @{typ "'a list"}, @{thm "equiv_list_eq"}) #> snd
|
|
600 |
*}
|
|
601 |
|
14
|
602 |
print_theorems
|
|
603 |
|
0
|
604 |
typ "'a fset"
|
|
605 |
thm "Rep_fset"
|
|
606 |
|
|
607 |
local_setup {*
|
|
608 |
make_const_def "EMPTY" @{term "[]"} NoSyn @{typ "'a list"} @{typ "'a fset"} #> snd
|
|
609 |
*}
|
|
610 |
|
14
|
611 |
term Nil
|
0
|
612 |
term EMPTY
|
2
|
613 |
thm EMPTY_def
|
|
614 |
|
0
|
615 |
|
|
616 |
local_setup {*
|
|
617 |
make_const_def "INSERT" @{term "op #"} NoSyn @{typ "'a list"} @{typ "'a fset"} #> snd
|
|
618 |
*}
|
|
619 |
|
14
|
620 |
term Cons
|
0
|
621 |
term INSERT
|
2
|
622 |
thm INSERT_def
|
0
|
623 |
|
|
624 |
local_setup {*
|
|
625 |
make_const_def "UNION" @{term "op @"} NoSyn @{typ "'a list"} @{typ "'a fset"} #> snd
|
|
626 |
*}
|
|
627 |
|
|
628 |
term append
|
|
629 |
term UNION
|
2
|
630 |
thm UNION_def
|
|
631 |
|
7
|
632 |
local_setup {*
|
|
633 |
make_const_def "CARD" @{term "length"} NoSyn @{typ "'a list"} @{typ "'a fset"} #> snd
|
|
634 |
*}
|
|
635 |
|
|
636 |
term length
|
|
637 |
term CARD
|
|
638 |
thm CARD_def
|
|
639 |
|
0
|
640 |
thm QUOTIENT_fset
|
|
641 |
|
14
|
642 |
thm QUOT_TYPE_I_fset.thm11
|
9
|
643 |
|
|
644 |
|
13
|
645 |
fun
|
|
646 |
membship :: "'a \<Rightarrow> 'a list \<Rightarrow> bool" (infix "memb" 100)
|
0
|
647 |
where
|
|
648 |
m1: "(x memb []) = False"
|
|
649 |
| m2: "(x memb (y#xs)) = ((x=y) \<or> (x memb xs))"
|
|
650 |
|
2
|
651 |
lemma mem_respects:
|
9
|
652 |
fixes z
|
2
|
653 |
assumes a: "list_eq x y"
|
9
|
654 |
shows "(z memb x) = (z memb y)"
|
13
|
655 |
using a by induct auto
|
|
656 |
|
2
|
657 |
|
12
|
658 |
lemma cons_preserves:
|
|
659 |
fixes z
|
|
660 |
assumes a: "xs \<approx> ys"
|
|
661 |
shows "(z # xs) \<approx> (z # ys)"
|
13
|
662 |
using a by (rule QuotMain.list_eq.intros(5))
|
12
|
663 |
|
11
|
664 |
ML {*
|
|
665 |
fun unlam_def orig_ctxt ctxt t =
|
13
|
666 |
let val rhs = Thm.rhs_of t in
|
|
667 |
(case try (Thm.dest_abs NONE) rhs of
|
|
668 |
SOME (v, vt) =>
|
|
669 |
let
|
|
670 |
val (vname, vt) = Term.dest_Free (Thm.term_of v)
|
|
671 |
val ([vnname], ctxt) = Variable.variant_fixes [vname] ctxt
|
|
672 |
val nv = Free(vnname, vt)
|
|
673 |
val t2 = Drule.fun_cong_rule t (Thm.cterm_of (ProofContext.theory_of ctxt) nv)
|
|
674 |
val tnorm = equal_elim (Drule.beta_eta_conversion (Thm.cprop_of t2)) t2
|
|
675 |
in unlam_def orig_ctxt ctxt tnorm end
|
|
676 |
| NONE => singleton (ProofContext.export ctxt orig_ctxt) t)
|
|
677 |
end
|
10
|
678 |
*}
|
9
|
679 |
|
10
|
680 |
local_setup {*
|
0
|
681 |
make_const_def "IN" @{term "membship"} NoSyn @{typ "'a list"} @{typ "'a fset"} #> snd
|
10
|
682 |
*}
|
|
683 |
|
0
|
684 |
term membship
|
|
685 |
term IN
|
2
|
686 |
thm IN_def
|
0
|
687 |
|
2
|
688 |
lemmas a = QUOT_TYPE.ABS_def[OF QUOT_TYPE_fset]
|
|
689 |
thm QUOT_TYPE.thm11[OF QUOT_TYPE_fset, THEN iffD1, simplified a]
|
0
|
690 |
|
2
|
691 |
lemma yy:
|
|
692 |
shows "(False = x memb []) = (False = IN (x::nat) EMPTY)"
|
|
693 |
unfolding IN_def EMPTY_def
|
|
694 |
apply(rule_tac f="(op =) False" in arg_cong)
|
|
695 |
apply(rule mem_respects)
|
14
|
696 |
apply(simp only: QUOT_TYPE_I_fset.REP_ABS_rsp)
|
2
|
697 |
apply(rule list_eq.intros)
|
0
|
698 |
done
|
|
699 |
|
2
|
700 |
lemma
|
|
701 |
shows "IN (x::nat) EMPTY = False"
|
|
702 |
using m1
|
|
703 |
apply -
|
|
704 |
apply(rule yy[THEN iffD1, symmetric])
|
|
705 |
apply(simp)
|
0
|
706 |
done
|
|
707 |
|
2
|
708 |
lemma
|
|
709 |
shows "((x=y) \<or> (IN x xs) = (IN (x::nat) (INSERT y xs))) =
|
|
710 |
((x = y) \<or> x memb REP_fset xs = x memb (y # REP_fset xs))"
|
|
711 |
unfolding IN_def INSERT_def
|
|
712 |
apply(rule_tac f="(op \<or>) (x=y)" in arg_cong)
|
|
713 |
apply(rule_tac f="(op =) (x memb REP_fset xs)" in arg_cong)
|
|
714 |
apply(rule mem_respects)
|
|
715 |
apply(rule list_eq.intros(3))
|
|
716 |
apply(unfold REP_fset_def ABS_fset_def)
|
5
|
717 |
apply(simp only: QUOT_TYPE.REP_ABS_rsp[OF QUOT_TYPE_fset])
|
2
|
718 |
apply(rule list_eq_sym)
|
|
719 |
done
|
0
|
720 |
|
7
|
721 |
lemma append_respects_fst:
|
14
|
722 |
assumes a : "list_eq l1 l2"
|
4
|
723 |
shows "list_eq (l1 @ s) (l2 @ s)"
|
3
|
724 |
using a
|
|
725 |
apply(induct)
|
4
|
726 |
apply(auto intro: list_eq.intros)
|
|
727 |
apply(simp add: list_eq_sym)
|
3
|
728 |
done
|
|
729 |
|
|
730 |
lemma yyy :
|
|
731 |
shows "
|
|
732 |
(
|
|
733 |
(UNION EMPTY s = s) &
|
|
734 |
((UNION (INSERT e s1) s2) = (INSERT e (UNION s1 s2)))
|
|
735 |
) = (
|
|
736 |
((ABS_fset ([] @ REP_fset s)) = s) &
|
|
737 |
((ABS_fset ((e # (REP_fset s1)) @ REP_fset s2)) = ABS_fset (e # (REP_fset s1 @ REP_fset s2)))
|
|
738 |
)"
|
|
739 |
unfolding UNION_def EMPTY_def INSERT_def
|
|
740 |
apply(rule_tac f="(op &)" in arg_cong2)
|
|
741 |
apply(rule_tac f="(op =)" in arg_cong2)
|
14
|
742 |
apply(simp only: QUOT_TYPE_I_fset.thm11[symmetric])
|
7
|
743 |
apply(rule append_respects_fst)
|
14
|
744 |
apply(simp only:QUOT_TYPE_I_fset.REP_ABS_rsp)
|
3
|
745 |
apply(rule list_eq_sym)
|
|
746 |
apply(simp)
|
|
747 |
apply(rule_tac f="(op =)" in arg_cong2)
|
14
|
748 |
apply(simp only: QUOT_TYPE_I_fset.thm11[symmetric])
|
7
|
749 |
apply(rule append_respects_fst)
|
14
|
750 |
apply(simp only: QUOT_TYPE_I_fset.REP_ABS_rsp)
|
3
|
751 |
apply(rule list_eq_sym)
|
14
|
752 |
apply(simp only: QUOT_TYPE_I_fset.thm11[symmetric])
|
3
|
753 |
apply(rule list_eq.intros(5))
|
14
|
754 |
apply(simp only: QUOT_TYPE_I_fset.REP_ABS_rsp)
|
3
|
755 |
apply(rule list_eq_sym)
|
|
756 |
done
|
|
757 |
|
|
758 |
lemma
|
|
759 |
shows "
|
|
760 |
(UNION EMPTY s = s) &
|
7
|
761 |
((UNION (INSERT e s1) s2) = (INSERT e (UNION s1 s2)))"
|
|
762 |
apply (simp add: yyy)
|
14
|
763 |
apply (rule QUOT_TYPE_I_fset.thm10)
|
7
|
764 |
done
|
3
|
765 |
|
8
|
766 |
ML {*
|
|
767 |
fun mk_rep_abs x = @{term REP_fset} $ (@{term ABS_fset} $ x)
|
12
|
768 |
val consts = [@{const_name "Nil"}, @{const_name "append"}, @{const_name "Cons"}, @{const_name "membship"}]
|
8
|
769 |
*}
|
|
770 |
|
|
771 |
ML {*
|
12
|
772 |
fun build_goal thm constructors lifted_type mk_rep_abs =
|
8
|
773 |
let
|
|
774 |
fun is_const (Const (x, t)) = x mem constructors
|
|
775 |
| is_const _ = false
|
12
|
776 |
fun maybe_mk_rep_abs t =
|
|
777 |
let
|
|
778 |
val _ = writeln ("Maybe: " ^ Syntax.string_of_term @{context} t)
|
|
779 |
in
|
|
780 |
if type_of t = lifted_type then mk_rep_abs t else t
|
|
781 |
end
|
8
|
782 |
fun build_aux (Abs (s, t, tr)) = (Abs (s, t, build_aux tr))
|
|
783 |
| build_aux (f $ a) =
|
11
|
784 |
let
|
|
785 |
val (f,args) = strip_comb (f $ a)
|
12
|
786 |
val _ = writeln (Syntax.string_of_term @{context} f)
|
11
|
787 |
in
|
12
|
788 |
(if is_const f then maybe_mk_rep_abs (list_comb (f, (map maybe_mk_rep_abs (map build_aux args))))
|
11
|
789 |
else list_comb ((build_aux f), (map build_aux args)))
|
|
790 |
end
|
8
|
791 |
| build_aux x =
|
12
|
792 |
if is_const x then maybe_mk_rep_abs x else x
|
9
|
793 |
val concl = HOLogic.dest_Trueprop (Thm.concl_of thm)
|
8
|
794 |
in
|
|
795 |
HOLogic.mk_eq ((build_aux concl), concl)
|
|
796 |
end *}
|
|
797 |
|
|
798 |
|
|
799 |
ML {*
|
|
800 |
val m1_novars = snd(no_vars ((Context.Theory @{theory}),@{thm m1}))
|
12
|
801 |
val goal = build_goal m1_novars consts @{typ "'a list"} mk_rep_abs
|
8
|
802 |
val cgoal = cterm_of @{theory} goal
|
|
803 |
*}
|
|
804 |
|
|
805 |
ML {* val emptyt = symmetric @{thm EMPTY_def} *}
|
9
|
806 |
ML {* val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite false [emptyt] cgoal) *}
|
|
807 |
|
11
|
808 |
ML {* val in_t = (symmetric (unlam_def @{context} @{context} @{thm IN_def})) *}
|
9
|
809 |
ML {* val cgoal3 = Thm.rhs_of (MetaSimplifier.rewrite false [in_t] (cgoal2)) *}
|
|
810 |
|
|
811 |
(*theorem "(IN x EMPTY = False) = (x memb [] = False)"*)
|
|
812 |
|
|
813 |
prove {* HOLogic.mk_Trueprop (Thm.term_of cgoal3) *}
|
|
814 |
apply(rule_tac f="(op =)" in arg_cong2)
|
|
815 |
unfolding IN_def EMPTY_def
|
|
816 |
apply (rule_tac mem_respects)
|
14
|
817 |
apply (simp only: QUOT_TYPE_I_fset.REP_ABS_rsp)
|
10
|
818 |
apply (simp_all)
|
|
819 |
apply (rule list_eq_sym)
|
11
|
820 |
done
|
|
821 |
|
|
822 |
thm length_append (* Not true but worth checking that the goal is correct *)
|
|
823 |
ML {*
|
|
824 |
val m1_novars = snd(no_vars ((Context.Theory @{theory}),@{thm length_append}))
|
12
|
825 |
val goal = build_goal m1_novars consts @{typ "'a list"} mk_rep_abs
|
11
|
826 |
val cgoal = cterm_of @{theory} goal
|
|
827 |
*}
|
|
828 |
ML {* val uniont = symmetric (unlam_def @{context} @{context} @{thm UNION_def}) *}
|
|
829 |
ML {* val cardt = symmetric (unlam_def @{context} @{context} @{thm CARD_def}) *}
|
12
|
830 |
ML {* val insertt = symmetric (unlam_def @{context} @{context} @{thm INSERT_def}) *}
|
11
|
831 |
ML {* val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite true [cardt] cgoal) *}
|
|
832 |
ML {* val cgoal3 = Thm.rhs_of (MetaSimplifier.rewrite true [uniont] cgoal2) *}
|
12
|
833 |
|
|
834 |
thm m2
|
|
835 |
ML {*
|
|
836 |
val m1_novars = snd(no_vars ((Context.Theory @{theory}),@{thm m2}))
|
|
837 |
val goal = build_goal m1_novars consts @{typ "'a list"} mk_rep_abs
|
|
838 |
val cgoal = cterm_of @{theory} goal
|
|
839 |
*}
|
|
840 |
ML {* val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite true [in_t] cgoal) *}
|
|
841 |
ML {* val cgoal3 = Thm.rhs_of (MetaSimplifier.rewrite true [insertt] cgoal2) *}
|
|
842 |
prove {* HOLogic.mk_Trueprop (Thm.term_of cgoal3) *}
|
|
843 |
apply(rule_tac f="(op =)" in arg_cong2)
|
|
844 |
unfolding IN_def
|
|
845 |
apply (rule_tac mem_respects)
|
|
846 |
unfolding INSERT_def
|
14
|
847 |
apply (simp only: QUOT_TYPE_I_fset.REP_ABS_rsp)
|
12
|
848 |
apply (rule cons_preserves)
|
14
|
849 |
apply (simp only: QUOT_TYPE_I_fset.REP_ABS_rsp)
|
12
|
850 |
apply (rule list_eq_sym)
|
|
851 |
apply(rule_tac f="(op \<or>)" in arg_cong2)
|
|
852 |
apply (simp)
|
|
853 |
apply (rule_tac mem_respects)
|
14
|
854 |
apply (simp only: QUOT_TYPE_I_fset.REP_ABS_rsp)
|
12
|
855 |
apply (rule list_eq_sym)
|
|
856 |
done
|