author | Cezary Kaliszyk <kaliszyk@in.tum.de> |
Fri, 23 Oct 2009 16:34:20 +0200 | |
changeset 163 | 3da18bf6886c |
parent 162 | 20f0b148cfe2 |
child 165 | 2c83d04262f9 |
permissions | -rw-r--r-- |
0 | 1 |
theory QuotMain |
6 | 2 |
imports QuotScript QuotList Prove |
71
35be65791f1d
exported parts of QuotMain into a separate ML-file
Christian Urban <urbanc@in.tum.de>
parents:
70
diff
changeset
|
3 |
uses ("quotient.ML") |
0 | 4 |
begin |
5 |
||
148
8e24e65f1e9b
tuned and attempted to store data about the quotients (does not work yet)
Christian Urban <urbanc@in.tum.de>
parents:
147
diff
changeset
|
6 |
ML {* Pretty.writeln *} |
8e24e65f1e9b
tuned and attempted to store data about the quotients (does not work yet)
Christian Urban <urbanc@in.tum.de>
parents:
147
diff
changeset
|
7 |
ML {* LocalTheory.theory_result *} |
127
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
8 |
|
0 | 9 |
locale QUOT_TYPE = |
10 |
fixes R :: "'a \<Rightarrow> 'a \<Rightarrow> bool" |
|
11 |
and Abs :: "('a \<Rightarrow> bool) \<Rightarrow> 'b" |
|
12 |
and Rep :: "'b \<Rightarrow> ('a \<Rightarrow> bool)" |
|
13 |
assumes equiv: "EQUIV R" |
|
14 |
and rep_prop: "\<And>y. \<exists>x. Rep y = R x" |
|
15 |
and rep_inverse: "\<And>x. Abs (Rep x) = x" |
|
16 |
and abs_inverse: "\<And>x. (Rep (Abs (R x))) = (R x)" |
|
17 |
and rep_inject: "\<And>x y. (Rep x = Rep y) = (x = y)" |
|
15 | 18 |
begin |
0 | 19 |
|
20 |
definition |
|
21 |
"ABS x \<equiv> Abs (R x)" |
|
22 |
||
23 |
definition |
|
24 |
"REP a = Eps (Rep a)" |
|
25 |
||
15 | 26 |
lemma lem9: |
0 | 27 |
shows "R (Eps (R x)) = R x" |
28 |
proof - |
|
29 |
have a: "R x x" using equiv by (simp add: EQUIV_REFL_SYM_TRANS REFL_def) |
|
30 |
then have "R x (Eps (R x))" by (rule someI) |
|
15 | 31 |
then show "R (Eps (R x)) = R x" |
0 | 32 |
using equiv unfolding EQUIV_def by simp |
33 |
qed |
|
34 |
||
35 |
theorem thm10: |
|
24
6885fa184e89
Merged with my changes from the morning:
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
23
diff
changeset
|
36 |
shows "ABS (REP a) \<equiv> a" |
6885fa184e89
Merged with my changes from the morning:
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
23
diff
changeset
|
37 |
apply (rule eq_reflection) |
6885fa184e89
Merged with my changes from the morning:
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
23
diff
changeset
|
38 |
unfolding ABS_def REP_def |
0 | 39 |
proof - |
15 | 40 |
from rep_prop |
0 | 41 |
obtain x where eq: "Rep a = R x" by auto |
42 |
have "Abs (R (Eps (Rep a))) = Abs (R (Eps (R x)))" using eq by simp |
|
43 |
also have "\<dots> = Abs (R x)" using lem9 by simp |
|
44 |
also have "\<dots> = Abs (Rep a)" using eq by simp |
|
45 |
also have "\<dots> = a" using rep_inverse by simp |
|
46 |
finally |
|
47 |
show "Abs (R (Eps (Rep a))) = a" by simp |
|
48 |
qed |
|
49 |
||
15 | 50 |
lemma REP_refl: |
0 | 51 |
shows "R (REP a) (REP a)" |
52 |
unfolding REP_def |
|
53 |
by (simp add: equiv[simplified EQUIV_def]) |
|
54 |
||
55 |
lemma lem7: |
|
22 | 56 |
shows "(R x = R y) = (Abs (R x) = Abs (R y))" |
0 | 57 |
apply(rule iffI) |
58 |
apply(simp) |
|
59 |
apply(drule rep_inject[THEN iffD2]) |
|
60 |
apply(simp add: abs_inverse) |
|
61 |
done |
|
15 | 62 |
|
0 | 63 |
theorem thm11: |
64 |
shows "R r r' = (ABS r = ABS r')" |
|
65 |
unfolding ABS_def |
|
66 |
by (simp only: equiv[simplified EQUIV_def] lem7) |
|
67 |
||
4 | 68 |
|
2 | 69 |
lemma REP_ABS_rsp: |
4 | 70 |
shows "R f (REP (ABS g)) = R f g" |
71 |
and "R (REP (ABS g)) f = R g f" |
|
23 | 72 |
by (simp_all add: thm10 thm11) |
4 | 73 |
|
0 | 74 |
lemma QUOTIENT: |
75 |
"QUOTIENT R ABS REP" |
|
76 |
apply(unfold QUOTIENT_def) |
|
77 |
apply(simp add: thm10) |
|
78 |
apply(simp add: REP_refl) |
|
79 |
apply(subst thm11[symmetric]) |
|
80 |
apply(simp add: equiv[simplified EQUIV_def]) |
|
81 |
done |
|
82 |
||
21
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
83 |
lemma R_trans: |
49 | 84 |
assumes ab: "R a b" |
85 |
and bc: "R b c" |
|
22 | 86 |
shows "R a c" |
21
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
87 |
proof - |
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
88 |
have tr: "TRANS R" using equiv EQUIV_REFL_SYM_TRANS[of R] by simp |
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
89 |
moreover have ab: "R a b" by fact |
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
90 |
moreover have bc: "R b c" by fact |
22 | 91 |
ultimately show "R a c" unfolding TRANS_def by blast |
21
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
92 |
qed |
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
93 |
|
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
94 |
lemma R_sym: |
49 | 95 |
assumes ab: "R a b" |
22 | 96 |
shows "R b a" |
21
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
97 |
proof - |
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
98 |
have re: "SYM R" using equiv EQUIV_REFL_SYM_TRANS[of R] by simp |
22 | 99 |
then show "R b a" using ab unfolding SYM_def by blast |
21
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
100 |
qed |
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
101 |
|
49 | 102 |
lemma R_trans2: |
103 |
assumes ac: "R a c" |
|
22 | 104 |
and bd: "R b d" |
21
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
105 |
shows "R a b = R c d" |
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
106 |
proof |
23 | 107 |
assume "R a b" |
21
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
108 |
then have "R b a" using R_sym by blast |
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
109 |
then have "R b c" using ac R_trans by blast |
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
110 |
then have "R c b" using R_sym by blast |
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
111 |
then show "R c d" using bd R_trans by blast |
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
112 |
next |
23 | 113 |
assume "R c d" |
21
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
114 |
then have "R a d" using ac R_trans by blast |
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
115 |
then have "R d a" using R_sym by blast |
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
116 |
then have "R b a" using bd R_trans by blast |
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
117 |
then show "R a b" using R_sym by blast |
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
118 |
qed |
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
119 |
|
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
120 |
lemma REPS_same: |
25
9020ee23a020
The tactic with REPEAT, CHANGED and a proper simpset.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
24
diff
changeset
|
121 |
shows "R (REP a) (REP b) \<equiv> (a = b)" |
38 | 122 |
proof - |
123 |
have "R (REP a) (REP b) = (a = b)" |
|
124 |
proof |
|
125 |
assume as: "R (REP a) (REP b)" |
|
126 |
from rep_prop |
|
127 |
obtain x y |
|
128 |
where eqs: "Rep a = R x" "Rep b = R y" by blast |
|
129 |
from eqs have "R (Eps (R x)) (Eps (R y))" using as unfolding REP_def by simp |
|
130 |
then have "R x (Eps (R y))" using lem9 by simp |
|
131 |
then have "R (Eps (R y)) x" using R_sym by blast |
|
132 |
then have "R y x" using lem9 by simp |
|
133 |
then have "R x y" using R_sym by blast |
|
134 |
then have "ABS x = ABS y" using thm11 by simp |
|
135 |
then have "Abs (Rep a) = Abs (Rep b)" using eqs unfolding ABS_def by simp |
|
136 |
then show "a = b" using rep_inverse by simp |
|
137 |
next |
|
138 |
assume ab: "a = b" |
|
139 |
have "REFL R" using equiv EQUIV_REFL_SYM_TRANS[of R] by simp |
|
140 |
then show "R (REP a) (REP b)" unfolding REFL_def using ab by auto |
|
141 |
qed |
|
142 |
then show "R (REP a) (REP b) \<equiv> (a = b)" by simp |
|
21
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
143 |
qed |
d15121412caa
Added more useful quotient facts.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
20
diff
changeset
|
144 |
|
0 | 145 |
end |
146 |
||
127
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
parents:
126
diff
changeset
|
147 |
|
0 | 148 |
section {* type definition for the quotient type *} |
149 |
||
71
35be65791f1d
exported parts of QuotMain into a separate ML-file
Christian Urban <urbanc@in.tum.de>
parents:
70
diff
changeset
|
150 |
use "quotient.ML" |
0 | 151 |
|
130
8e8ba210f0f7
moved the map-info and fun-info section to quotient.ML
Christian Urban <urbanc@in.tum.de>
parents:
129
diff
changeset
|
152 |
(* mapfuns for some standard types *) |
8e8ba210f0f7
moved the map-info and fun-info section to quotient.ML
Christian Urban <urbanc@in.tum.de>
parents:
129
diff
changeset
|
153 |
setup {* |
8e8ba210f0f7
moved the map-info and fun-info section to quotient.ML
Christian Urban <urbanc@in.tum.de>
parents:
129
diff
changeset
|
154 |
maps_update @{type_name "list"} {mapfun = @{const_name "map"}, relfun = @{const_name "LIST_REL"}} #> |
8e8ba210f0f7
moved the map-info and fun-info section to quotient.ML
Christian Urban <urbanc@in.tum.de>
parents:
129
diff
changeset
|
155 |
maps_update @{type_name "*"} {mapfun = @{const_name "prod_fun"}, relfun = @{const_name "prod_rel"}} #> |
8e8ba210f0f7
moved the map-info and fun-info section to quotient.ML
Christian Urban <urbanc@in.tum.de>
parents:
129
diff
changeset
|
156 |
maps_update @{type_name "fun"} {mapfun = @{const_name "fun_map"}, relfun = @{const_name "FUN_REL"}} |
8e8ba210f0f7
moved the map-info and fun-info section to quotient.ML
Christian Urban <urbanc@in.tum.de>
parents:
129
diff
changeset
|
157 |
*} |
8e8ba210f0f7
moved the map-info and fun-info section to quotient.ML
Christian Urban <urbanc@in.tum.de>
parents:
129
diff
changeset
|
158 |
|
8e8ba210f0f7
moved the map-info and fun-info section to quotient.ML
Christian Urban <urbanc@in.tum.de>
parents:
129
diff
changeset
|
159 |
|
8e8ba210f0f7
moved the map-info and fun-info section to quotient.ML
Christian Urban <urbanc@in.tum.de>
parents:
129
diff
changeset
|
160 |
ML {* maps_lookup @{theory} @{type_name list} *} |
79
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
parents:
77
diff
changeset
|
161 |
|
14
5f6ee943c697
Generalized interpretation, works for all examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
13
diff
changeset
|
162 |
ML {* |
5f6ee943c697
Generalized interpretation, works for all examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
13
diff
changeset
|
163 |
val no_vars = Thm.rule_attribute (fn context => fn th => |
5f6ee943c697
Generalized interpretation, works for all examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
13
diff
changeset
|
164 |
let |
5f6ee943c697
Generalized interpretation, works for all examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
13
diff
changeset
|
165 |
val ctxt = Variable.set_body false (Context.proof_of context); |
5f6ee943c697
Generalized interpretation, works for all examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
13
diff
changeset
|
166 |
val ((_, [th']), _) = Variable.import true [th] ctxt; |
5f6ee943c697
Generalized interpretation, works for all examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
13
diff
changeset
|
167 |
in th' end); |
5f6ee943c697
Generalized interpretation, works for all examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
13
diff
changeset
|
168 |
*} |
5f6ee943c697
Generalized interpretation, works for all examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
13
diff
changeset
|
169 |
|
0 | 170 |
section {* lifting of constants *} |
171 |
||
172 |
ML {* |
|
118
1c30d48bfc15
slightly simplified get_fun function
Christian Urban <urbanc@in.tum.de>
parents:
117
diff
changeset
|
173 |
(* calculates the aggregate abs and rep functions for a given type; |
1c30d48bfc15
slightly simplified get_fun function
Christian Urban <urbanc@in.tum.de>
parents:
117
diff
changeset
|
174 |
repF is for constants' arguments; absF is for constants; |
1c30d48bfc15
slightly simplified get_fun function
Christian Urban <urbanc@in.tum.de>
parents:
117
diff
changeset
|
175 |
function types need to be treated specially, since repF and absF |
163
3da18bf6886c
Split Finite Set example into separate file
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
162
diff
changeset
|
176 |
change |
118
1c30d48bfc15
slightly simplified get_fun function
Christian Urban <urbanc@in.tum.de>
parents:
117
diff
changeset
|
177 |
*) |
46 | 178 |
datatype flag = absF | repF |
0 | 179 |
|
109
386671ef36bd
fixed the problem with function types; but only type_of works; cterm_of does not work
Christian Urban <urbanc@in.tum.de>
parents:
108
diff
changeset
|
180 |
fun negF absF = repF |
386671ef36bd
fixed the problem with function types; but only type_of works; cterm_of does not work
Christian Urban <urbanc@in.tum.de>
parents:
108
diff
changeset
|
181 |
| negF repF = absF |
386671ef36bd
fixed the problem with function types; but only type_of works; cterm_of does not work
Christian Urban <urbanc@in.tum.de>
parents:
108
diff
changeset
|
182 |
|
46 | 183 |
fun get_fun flag rty qty lthy ty = |
0 | 184 |
let |
185 |
val qty_name = Long_Name.base_name (fst (dest_Type qty)) |
|
15 | 186 |
|
0 | 187 |
fun get_fun_aux s fs_tys = |
188 |
let |
|
189 |
val (fs, tys) = split_list fs_tys |
|
15 | 190 |
val (otys, ntys) = split_list tys |
0 | 191 |
val oty = Type (s, otys) |
192 |
val nty = Type (s, ntys) |
|
193 |
val ftys = map (op -->) tys |
|
194 |
in |
|
130
8e8ba210f0f7
moved the map-info and fun-info section to quotient.ML
Christian Urban <urbanc@in.tum.de>
parents:
129
diff
changeset
|
195 |
(case (maps_lookup (ProofContext.theory_of lthy) s) of |
110
f5f641c05794
A fix for one fun_map; doesn't work for more.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
109
diff
changeset
|
196 |
SOME info => (list_comb (Const (#mapfun info, ftys ---> (oty --> nty)), fs), (oty, nty)) |
f5f641c05794
A fix for one fun_map; doesn't work for more.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
109
diff
changeset
|
197 |
| NONE => raise ERROR ("no map association for type " ^ s)) |
f5f641c05794
A fix for one fun_map; doesn't work for more.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
109
diff
changeset
|
198 |
end |
f5f641c05794
A fix for one fun_map; doesn't work for more.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
109
diff
changeset
|
199 |
|
118
1c30d48bfc15
slightly simplified get_fun function
Christian Urban <urbanc@in.tum.de>
parents:
117
diff
changeset
|
200 |
fun get_fun_fun fs_tys = |
110
f5f641c05794
A fix for one fun_map; doesn't work for more.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
109
diff
changeset
|
201 |
let |
f5f641c05794
A fix for one fun_map; doesn't work for more.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
109
diff
changeset
|
202 |
val (fs, tys) = split_list fs_tys |
f5f641c05794
A fix for one fun_map; doesn't work for more.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
109
diff
changeset
|
203 |
val ([oty1, oty2], [nty1, nty2]) = split_list tys |
128 | 204 |
val oty = nty1 --> oty2 |
205 |
val nty = oty1 --> nty2 |
|
110
f5f641c05794
A fix for one fun_map; doesn't work for more.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
109
diff
changeset
|
206 |
val ftys = map (op -->) tys |
f5f641c05794
A fix for one fun_map; doesn't work for more.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
109
diff
changeset
|
207 |
in |
118
1c30d48bfc15
slightly simplified get_fun function
Christian Urban <urbanc@in.tum.de>
parents:
117
diff
changeset
|
208 |
(list_comb (Const (@{const_name "fun_map"}, ftys ---> oty --> nty), fs), (oty, nty)) |
0 | 209 |
end |
210 |
||
163
3da18bf6886c
Split Finite Set example into separate file
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
162
diff
changeset
|
211 |
fun get_const absF = (Const ("FSet.ABS_" ^ qty_name, rty --> qty), (rty, qty)) |
3da18bf6886c
Split Finite Set example into separate file
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
162
diff
changeset
|
212 |
| get_const repF = (Const ("FSet.REP_" ^ qty_name, qty --> rty), (qty, rty)) |
41
72d63aa8af68
added ctxt as explicit argument to build_goal; tuned
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
213 |
|
70
f3cbda066c3a
consistent usage of rty (for the raw, unquotient type); tuned a bit the Isar
Christian Urban <urbanc@in.tum.de>
parents:
69
diff
changeset
|
214 |
fun mk_identity ty = Abs ("", ty, Bound 0) |
41
72d63aa8af68
added ctxt as explicit argument to build_goal; tuned
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
215 |
|
0 | 216 |
in |
217 |
if ty = qty |
|
46 | 218 |
then (get_const flag) |
0 | 219 |
else (case ty of |
41
72d63aa8af68
added ctxt as explicit argument to build_goal; tuned
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
220 |
TFree _ => (mk_identity ty, (ty, ty)) |
118
1c30d48bfc15
slightly simplified get_fun function
Christian Urban <urbanc@in.tum.de>
parents:
117
diff
changeset
|
221 |
| Type (_, []) => (mk_identity ty, (ty, ty)) |
109
386671ef36bd
fixed the problem with function types; but only type_of works; cterm_of does not work
Christian Urban <urbanc@in.tum.de>
parents:
108
diff
changeset
|
222 |
| Type ("fun" , [ty1, ty2]) => |
118
1c30d48bfc15
slightly simplified get_fun function
Christian Urban <urbanc@in.tum.de>
parents:
117
diff
changeset
|
223 |
get_fun_fun [get_fun (negF flag) rty qty lthy ty1, get_fun flag rty qty lthy ty2] |
46 | 224 |
| Type (s, tys) => get_fun_aux s (map (get_fun flag rty qty lthy) tys) |
41
72d63aa8af68
added ctxt as explicit argument to build_goal; tuned
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
225 |
| _ => raise ERROR ("no type variables") |
0 | 226 |
) |
227 |
end |
|
228 |
*} |
|
229 |
||
41
72d63aa8af68
added ctxt as explicit argument to build_goal; tuned
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
230 |
text {* produces the definition for a lifted constant *} |
86 | 231 |
|
2 | 232 |
ML {* |
0 | 233 |
fun get_const_def nconst oconst rty qty lthy = |
234 |
let |
|
235 |
val ty = fastype_of nconst |
|
236 |
val (arg_tys, res_ty) = strip_type ty |
|
14
5f6ee943c697
Generalized interpretation, works for all examples.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
13
diff
changeset
|
237 |
|
0 | 238 |
val fresh_args = arg_tys |> map (pair "x") |
15 | 239 |
|> Variable.variant_frees lthy [nconst, oconst] |
0 | 240 |
|> map Free |
241 |
||
46 | 242 |
val rep_fns = map (fst o get_fun repF rty qty lthy) arg_tys |
243 |
val abs_fn = (fst o get_fun absF rty qty lthy) res_ty |
|
0 | 244 |
|
245 |
in |
|
246 |
map (op $) (rep_fns ~~ fresh_args) |
|
247 |
|> curry list_comb oconst |
|
248 |
|> curry (op $) abs_fn |
|
249 |
|> fold_rev lambda fresh_args |
|
250 |
end |
|
251 |
*} |
|
252 |
||
253 |
ML {* |
|
15 | 254 |
fun exchange_ty rty qty ty = |
49 | 255 |
if ty = rty |
41
72d63aa8af68
added ctxt as explicit argument to build_goal; tuned
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
256 |
then qty |
15 | 257 |
else |
0 | 258 |
(case ty of |
259 |
Type (s, tys) => Type (s, map (exchange_ty rty qty) tys) |
|
41
72d63aa8af68
added ctxt as explicit argument to build_goal; tuned
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
260 |
| _ => ty |
72d63aa8af68
added ctxt as explicit argument to build_goal; tuned
Christian Urban <urbanc@in.tum.de>
parents:
40
diff
changeset
|
261 |
) |
0 | 262 |
*} |
263 |
||
264 |
ML {* |
|
17
55b646c6c4cd
More naming/binding suggestions from Makarius
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
16
diff
changeset
|
265 |
fun make_const_def nconst_bname oconst mx rty qty lthy = |
0 | 266 |
let |
267 |
val oconst_ty = fastype_of oconst |
|
268 |
val nconst_ty = exchange_ty rty qty oconst_ty |
|
17
55b646c6c4cd
More naming/binding suggestions from Makarius
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
16
diff
changeset
|
269 |
val nconst = Const (Binding.name_of nconst_bname, nconst_ty) |
0 | 270 |
val def_trm = get_const_def nconst oconst rty qty lthy |
271 |
in |
|
79
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
parents:
77
diff
changeset
|
272 |
define (nconst_bname, mx, def_trm) lthy |
15 | 273 |
end |
0 | 274 |
*} |
275 |
||
139 | 276 |
section {* ATOMIZE *} |
277 |
||
278 |
text {* |
|
279 |
Unabs_def converts a definition given as |
|
280 |
||
281 |
c \<equiv> %x. %y. f x y |
|
282 |
||
283 |
to a theorem of the form |
|
284 |
||
285 |
c x y \<equiv> f x y |
|
286 |
||
287 |
This function is needed to rewrite the right-hand |
|
288 |
side to the left-hand side. |
|
289 |
*} |
|
290 |
||
291 |
ML {* |
|
292 |
fun unabs_def ctxt def = |
|
293 |
let |
|
294 |
val (lhs, rhs) = Thm.dest_equals (cprop_of def) |
|
295 |
val xs = strip_abs_vars (term_of rhs) |
|
296 |
val (_, ctxt') = Variable.add_fixes (map fst xs) ctxt |
|
297 |
||
298 |
val thy = ProofContext.theory_of ctxt' |
|
299 |
val cxs = map (cterm_of thy o Free) xs |
|
300 |
val new_lhs = Drule.list_comb (lhs, cxs) |
|
301 |
||
302 |
fun get_conv [] = Conv.rewr_conv def |
|
303 |
| get_conv (x::xs) = Conv.fun_conv (get_conv xs) |
|
304 |
in |
|
305 |
get_conv xs new_lhs |> |
|
306 |
singleton (ProofContext.export ctxt' ctxt) |
|
307 |
end |
|
308 |
*} |
|
309 |
||
310 |
lemma atomize_eqv[atomize]: |
|
311 |
shows "(Trueprop A \<equiv> Trueprop B) \<equiv> (A \<equiv> B)" |
|
312 |
proof |
|
313 |
assume "A \<equiv> B" |
|
314 |
then show "Trueprop A \<equiv> Trueprop B" by unfold |
|
315 |
next |
|
316 |
assume *: "Trueprop A \<equiv> Trueprop B" |
|
317 |
have "A = B" |
|
318 |
proof (cases A) |
|
319 |
case True |
|
320 |
have "A" by fact |
|
321 |
then show "A = B" using * by simp |
|
322 |
next |
|
323 |
case False |
|
324 |
have "\<not>A" by fact |
|
325 |
then show "A = B" using * by auto |
|
326 |
qed |
|
327 |
then show "A \<equiv> B" by (rule eq_reflection) |
|
328 |
qed |
|
329 |
||
330 |
ML {* |
|
331 |
fun atomize_thm thm = |
|
332 |
let |
|
333 |
val thm' = forall_intr_vars thm |
|
334 |
val thm'' = ObjectLogic.atomize (cprop_of thm') |
|
335 |
in |
|
140
00d141f2daa7
Further reorganizing the file
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
139
diff
changeset
|
336 |
Thm.freezeT (Simplifier.rewrite_rule [thm''] thm') |
139 | 337 |
end |
338 |
*} |
|
339 |
||
140
00d141f2daa7
Further reorganizing the file
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
139
diff
changeset
|
340 |
ML {* atomize_thm @{thm list.induct} *} |
139 | 341 |
|
342 |
section {* REGULARIZE *} |
|
343 |
||
344 |
text {* tyRel takes a type and builds a relation that a quantifier over this |
|
345 |
type needs to respect. *} |
|
346 |
ML {* |
|
347 |
fun tyRel ty rty rel lthy = |
|
348 |
if ty = rty |
|
349 |
then rel |
|
350 |
else (case ty of |
|
351 |
Type (s, tys) => |
|
352 |
let |
|
353 |
val tys_rel = map (fn ty => ty --> ty --> @{typ bool}) tys; |
|
354 |
val ty_out = ty --> ty --> @{typ bool}; |
|
355 |
val tys_out = tys_rel ---> ty_out; |
|
356 |
in |
|
357 |
(case (maps_lookup (ProofContext.theory_of lthy) s) of |
|
358 |
SOME (info) => list_comb (Const (#relfun info, tys_out), map (fn ty => tyRel ty rty rel lthy) tys) |
|
359 |
| NONE => HOLogic.eq_const ty |
|
360 |
) |
|
361 |
end |
|
362 |
| _ => HOLogic.eq_const ty) |
|
363 |
*} |
|
364 |
||
365 |
definition |
|
366 |
Babs :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b" |
|
367 |
where |
|
368 |
"(x \<in> p) \<Longrightarrow> (Babs p m x = m x)" |
|
369 |
(* TODO: Consider defining it with an "if"; sth like: |
|
370 |
Babs p m = \<lambda>x. if x \<in> p then m x else undefined |
|
371 |
*) |
|
372 |
||
373 |
ML {* |
|
374 |
fun needs_lift (rty as Type (rty_s, _)) ty = |
|
375 |
case ty of |
|
376 |
Type (s, tys) => |
|
377 |
(s = rty_s) orelse (exists (needs_lift rty) tys) |
|
378 |
| _ => false |
|
379 |
||
380 |
*} |
|
381 |
||
382 |
ML {* |
|
383 |
(* trm \<Rightarrow> new_trm *) |
|
384 |
fun regularise trm rty rel lthy = |
|
385 |
case trm of |
|
386 |
Abs (x, T, t) => |
|
387 |
if (needs_lift rty T) then let |
|
388 |
val ([x'], lthy2) = Variable.variant_fixes [x] lthy; |
|
389 |
val v = Free (x', T); |
|
390 |
val t' = subst_bound (v, t); |
|
391 |
val rec_term = regularise t' rty rel lthy2; |
|
392 |
val lam_term = Term.lambda_name (x, v) rec_term; |
|
393 |
val sub_res_term = tyRel T rty rel lthy; |
|
394 |
val respects = Const (@{const_name Respects}, (fastype_of sub_res_term) --> T --> @{typ bool}); |
|
395 |
val res_term = respects $ sub_res_term; |
|
396 |
val ty = fastype_of trm; |
|
397 |
val rabs = Const (@{const_name Babs}, (fastype_of res_term) --> ty --> ty); |
|
398 |
val rabs_term = (rabs $ res_term) $ lam_term; |
|
399 |
in |
|
400 |
rabs_term |
|
401 |
end else let |
|
402 |
val ([x'], lthy2) = Variable.variant_fixes [x] lthy; |
|
403 |
val v = Free (x', T); |
|
404 |
val t' = subst_bound (v, t); |
|
405 |
val rec_term = regularise t' rty rel lthy2; |
|
406 |
in |
|
407 |
Term.lambda_name (x, v) rec_term |
|
408 |
end |
|
409 |
| ((Const (@{const_name "All"}, at)) $ (Abs (x, T, t))) => |
|
410 |
if (needs_lift rty T) then let |
|
411 |
val ([x'], lthy2) = Variable.variant_fixes [x] lthy; |
|
412 |
val v = Free (x', T); |
|
413 |
val t' = subst_bound (v, t); |
|
414 |
val rec_term = regularise t' rty rel lthy2; |
|
415 |
val lam_term = Term.lambda_name (x, v) rec_term; |
|
416 |
val sub_res_term = tyRel T rty rel lthy; |
|
417 |
val respects = Const (@{const_name Respects}, (fastype_of sub_res_term) --> T --> @{typ bool}); |
|
418 |
val res_term = respects $ sub_res_term; |
|
419 |
val ty = fastype_of lam_term; |
|
420 |
val rall = Const (@{const_name Ball}, (fastype_of res_term) --> ty --> @{typ bool}); |
|
421 |
val rall_term = (rall $ res_term) $ lam_term; |
|
422 |
in |
|
423 |
rall_term |
|
424 |
end else let |
|
425 |
val ([x'], lthy2) = Variable.variant_fixes [x] lthy; |
|
426 |
val v = Free (x', T); |
|
427 |
val t' = subst_bound (v, t); |
|
428 |
val rec_term = regularise t' rty rel lthy2; |
|
429 |
val lam_term = Term.lambda_name (x, v) rec_term |
|
430 |
in |
|
431 |
Const(@{const_name "All"}, at) $ lam_term |
|
432 |
end |
|
433 |
| ((Const (@{const_name "All"}, at)) $ P) => |
|
434 |
let |
|
435 |
val (_, [al, _]) = dest_Type (fastype_of P); |
|
436 |
val ([x], lthy2) = Variable.variant_fixes [""] lthy; |
|
437 |
val v = (Free (x, al)); |
|
438 |
val abs = Term.lambda_name (x, v) (P $ v); |
|
439 |
in regularise ((Const (@{const_name "All"}, at)) $ abs) rty rel lthy2 end |
|
440 |
| ((Const (@{const_name "Ex"}, at)) $ (Abs (x, T, t))) => |
|
441 |
if (needs_lift rty T) then let |
|
442 |
val ([x'], lthy2) = Variable.variant_fixes [x] lthy; |
|
443 |
val v = Free (x', T); |
|
444 |
val t' = subst_bound (v, t); |
|
445 |
val rec_term = regularise t' rty rel lthy2; |
|
446 |
val lam_term = Term.lambda_name (x, v) rec_term; |
|
447 |
val sub_res_term = tyRel T rty rel lthy; |
|
448 |
val respects = Const (@{const_name Respects}, (fastype_of sub_res_term) --> T --> @{typ bool}); |
|
449 |
val res_term = respects $ sub_res_term; |
|
450 |
val ty = fastype_of lam_term; |
|
451 |
val rall = Const (@{const_name Bex}, (fastype_of res_term) --> ty --> @{typ bool}); |
|
452 |
val rall_term = (rall $ res_term) $ lam_term; |
|
453 |
in |
|
454 |
rall_term |
|
455 |
end else let |
|
456 |
val ([x'], lthy2) = Variable.variant_fixes [x] lthy; |
|
457 |
val v = Free (x', T); |
|
458 |
val t' = subst_bound (v, t); |
|
459 |
val rec_term = regularise t' rty rel lthy2; |
|
460 |
val lam_term = Term.lambda_name (x, v) rec_term |
|
461 |
in |
|
462 |
Const(@{const_name "Ex"}, at) $ lam_term |
|
463 |
end |
|
464 |
| ((Const (@{const_name "Ex"}, at)) $ P) => |
|
465 |
let |
|
466 |
val (_, [al, _]) = dest_Type (fastype_of P); |
|
467 |
val ([x], lthy2) = Variable.variant_fixes [""] lthy; |
|
468 |
val v = (Free (x, al)); |
|
469 |
val abs = Term.lambda_name (x, v) (P $ v); |
|
470 |
in regularise ((Const (@{const_name "Ex"}, at)) $ abs) rty rel lthy2 end |
|
471 |
| a $ b => (regularise a rty rel lthy) $ (regularise b rty rel lthy) |
|
472 |
| _ => trm |
|
473 |
||
474 |
*} |
|
475 |
||
476 |
(* my version of regularise *) |
|
477 |
(****************************) |
|
478 |
||
479 |
(* some helper functions *) |
|
480 |
||
481 |
||
482 |
ML {* |
|
483 |
fun mk_babs ty ty' = Const (@{const_name "Babs"}, [ty' --> @{typ bool}, ty] ---> ty) |
|
484 |
fun mk_ball ty = Const (@{const_name "Ball"}, [ty, ty] ---> @{typ bool}) |
|
485 |
fun mk_bex ty = Const (@{const_name "Bex"}, [ty, ty] ---> @{typ bool}) |
|
486 |
fun mk_resp ty = Const (@{const_name Respects}, [[ty, ty] ---> @{typ bool}, ty] ---> @{typ bool}) |
|
487 |
*} |
|
488 |
||
489 |
(* applies f to the subterm of an abstractions, otherwise to the given term *) |
|
490 |
ML {* |
|
491 |
fun apply_subt f trm = |
|
492 |
case trm of |
|
145 | 493 |
Abs (x, T, t) => |
494 |
let |
|
495 |
val (x', t') = Term.dest_abs (x, T, t) |
|
496 |
in |
|
497 |
Term.absfree (x', T, f t') |
|
498 |
end |
|
139 | 499 |
| _ => f trm |
500 |
*} |
|
501 |
||
502 |
||
503 |
(* FIXME: assumes always the typ is qty! *) |
|
504 |
(* FIXME: if there are more than one quotient, then you have to look up the relation *) |
|
505 |
ML {* |
|
506 |
fun my_reg rel trm = |
|
507 |
case trm of |
|
508 |
Abs (x, T, t) => |
|
509 |
let |
|
510 |
val ty1 = fastype_of trm |
|
511 |
in |
|
146 | 512 |
(mk_babs ty1 T) $ (mk_resp T $ rel) $ (apply_subt (my_reg rel) trm) |
139 | 513 |
end |
514 |
| Const (@{const_name "All"}, ty) $ t => |
|
515 |
let |
|
516 |
val ty1 = domain_type ty |
|
517 |
val ty2 = domain_type ty1 |
|
518 |
in |
|
519 |
(mk_ball ty1) $ (mk_resp ty2 $ rel) $ (apply_subt (my_reg rel) t) |
|
520 |
end |
|
521 |
| Const (@{const_name "Ex"}, ty) $ t => |
|
522 |
let |
|
523 |
val ty1 = domain_type ty |
|
524 |
val ty2 = domain_type ty1 |
|
525 |
in |
|
526 |
(mk_bex ty1) $ (mk_resp ty2 $ rel) $ (apply_subt (my_reg rel) t) |
|
527 |
end |
|
528 |
| t1 $ t2 => (my_reg rel t1) $ (my_reg rel t2) |
|
529 |
| _ => trm |
|
530 |
*} |
|
531 |
||
532 |
||
533 |
(*fun prove_reg trm \<Rightarrow> thm (we might need some facts to do this) |
|
534 |
trm == new_trm |
|
535 |
*) |
|
536 |
||
141
0ffc37761e53
Further reorganization
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
140
diff
changeset
|
537 |
text {* Assumes that the given theorem is atomized *} |
140
00d141f2daa7
Further reorganizing the file
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
139
diff
changeset
|
538 |
ML {* |
00d141f2daa7
Further reorganizing the file
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
139
diff
changeset
|
539 |
fun build_regularize_goal thm rty rel lthy = |
00d141f2daa7
Further reorganizing the file
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
139
diff
changeset
|
540 |
Logic.mk_implies |
00d141f2daa7
Further reorganizing the file
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
139
diff
changeset
|
541 |
((prop_of thm), |
00d141f2daa7
Further reorganizing the file
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
139
diff
changeset
|
542 |
(regularise (prop_of thm) rty rel lthy)) |
00d141f2daa7
Further reorganizing the file
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
139
diff
changeset
|
543 |
*} |
00d141f2daa7
Further reorganizing the file
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
139
diff
changeset
|
544 |
|
00d141f2daa7
Further reorganizing the file
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
139
diff
changeset
|
545 |
section {* RepAbs injection *} |
139 | 546 |
|
161
2ee03759a22f
Trying to get a simpler lemma with the whole infrastructure
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
160
diff
changeset
|
547 |
(* Needed to have a meta-equality *) |
2ee03759a22f
Trying to get a simpler lemma with the whole infrastructure
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
160
diff
changeset
|
548 |
lemma id_def_sym: "(\<lambda>x. x) \<equiv> id" |
2ee03759a22f
Trying to get a simpler lemma with the whole infrastructure
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
160
diff
changeset
|
549 |
by (simp add: id_def) |
2ee03759a22f
Trying to get a simpler lemma with the whole infrastructure
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
160
diff
changeset
|
550 |
|
139 | 551 |
ML {* |
140
00d141f2daa7
Further reorganizing the file
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
139
diff
changeset
|
552 |
fun build_repabs_term lthy thm constructors rty qty = |
139 | 553 |
let |
554 |
fun mk_rep tm = |
|
555 |
let |
|
556 |
val ty = exchange_ty rty qty (fastype_of tm) |
|
557 |
in fst (get_fun repF rty qty lthy ty) $ tm end |
|
558 |
||
559 |
fun mk_abs tm = |
|
560 |
let |
|
561 |
val ty = exchange_ty rty qty (fastype_of tm) in |
|
562 |
fst (get_fun absF rty qty lthy ty) $ tm end |
|
563 |
||
564 |
fun is_constructor (Const (x, _)) = member (op =) constructors x |
|
565 |
| is_constructor _ = false; |
|
566 |
||
567 |
fun build_aux lthy tm = |
|
568 |
case tm of |
|
569 |
Abs (a as (_, vty, _)) => |
|
570 |
let |
|
571 |
val (vs, t) = Term.dest_abs a; |
|
572 |
val v = Free(vs, vty); |
|
573 |
val t' = lambda v (build_aux lthy t) |
|
574 |
in |
|
575 |
if (not (needs_lift rty (fastype_of tm))) then t' |
|
576 |
else mk_rep (mk_abs ( |
|
577 |
if not (needs_lift rty vty) then t' |
|
578 |
else |
|
579 |
let |
|
580 |
val v' = mk_rep (mk_abs v); |
|
581 |
val t1 = Envir.beta_norm (t' $ v') |
|
582 |
in |
|
583 |
lambda v t1 |
|
584 |
end |
|
585 |
)) |
|
586 |
end |
|
587 |
| x => |
|
588 |
let |
|
589 |
val (opp, tms0) = Term.strip_comb tm |
|
590 |
val tms = map (build_aux lthy) tms0 |
|
591 |
val ty = fastype_of tm |
|
592 |
in |
|
593 |
if (((fst (Term.dest_Const opp)) = @{const_name Respects}) handle _ => false) |
|
594 |
then (list_comb (opp, (hd tms0) :: (tl tms))) |
|
595 |
else if (is_constructor opp andalso needs_lift rty ty) then |
|
596 |
mk_rep (mk_abs (list_comb (opp,tms))) |
|
597 |
else if ((Term.is_Free opp) andalso (length tms > 0) andalso (needs_lift rty ty)) then |
|
149
7cf1d7adfc5f
Removed some debugging messages
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
148
diff
changeset
|
598 |
mk_rep(mk_abs(list_comb(opp,tms))) |
139 | 599 |
else if tms = [] then opp |
600 |
else list_comb(opp, tms) |
|
601 |
end |
|
602 |
in |
|
161
2ee03759a22f
Trying to get a simpler lemma with the whole infrastructure
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
160
diff
changeset
|
603 |
MetaSimplifier.rewrite_term @{theory} @{thms id_def_sym} [] |
2ee03759a22f
Trying to get a simpler lemma with the whole infrastructure
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
160
diff
changeset
|
604 |
(build_aux lthy (Thm.prop_of thm)) |
139 | 605 |
end |
606 |
*} |
|
607 |
||
141
0ffc37761e53
Further reorganization
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
140
diff
changeset
|
608 |
text {* Assumes that it is given a regularized theorem *} |
139 | 609 |
ML {* |
140
00d141f2daa7
Further reorganizing the file
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
139
diff
changeset
|
610 |
fun build_repabs_goal ctxt thm cons rty qty = |
00d141f2daa7
Further reorganizing the file
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
139
diff
changeset
|
611 |
Logic.mk_equals ((Thm.prop_of thm), (build_repabs_term ctxt thm cons rty qty)) |
139 | 612 |
*} |
613 |
||
152
53277fbb2dba
Simplified the proof with some tactic... Still hangs sometimes.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
151
diff
changeset
|
614 |
end |