author | Christian Urban <urbanc@in.tum.de> |
Tue, 04 May 2010 14:38:07 +0100 | |
changeset 2048 | 20be95dce643 |
parent 2010 | 19fe16dd36c2 |
child 2073 | 2bfd5be8578a |
permissions | -rw-r--r-- |
1992 | 1 |
theory NewAlpha |
2 |
imports "NewFv" |
|
3 |
begin |
|
4 |
||
5 |
(* Given [fv1, fv2, fv3] |
|
6 |
produces %(x, y, z). fv1 x u fv2 y u fv3 z *) |
|
7 |
ML {* |
|
8 |
fun mk_compound_fv fvs = |
|
9 |
let |
|
10 |
val nos = (length fvs - 1) downto 0; |
|
11 |
val fvs_applied = map (fn (fv, no) => fv $ Bound no) (fvs ~~ nos); |
|
12 |
val fvs_union = mk_union fvs_applied; |
|
13 |
val (tyh :: tys) = rev (map (domain_type o fastype_of) fvs); |
|
14 |
fun fold_fun ty t = HOLogic.mk_split (Abs ("", ty, t)) |
|
15 |
in |
|
16 |
fold fold_fun tys (Abs ("", tyh, fvs_union)) |
|
17 |
end; |
|
18 |
*} |
|
19 |
||
20 |
(* Given [R1, R2, R3] |
|
21 |
produces %(x,x'). %(y,y'). %(z,z'). R x x' \<and> R y y' \<and> R z z' *) |
|
22 |
ML {* |
|
23 |
fun mk_compound_alpha Rs = |
|
24 |
let |
|
25 |
val nos = (length Rs - 1) downto 0; |
|
26 |
val nos2 = (2 * length Rs - 1) downto length Rs; |
|
27 |
val Rs_applied = map (fn (R, (no2, no)) => R $ Bound no2 $ Bound no) |
|
28 |
(Rs ~~ (nos2 ~~ nos)); |
|
29 |
val Rs_conj = foldr1 HOLogic.mk_conj Rs_applied; |
|
30 |
val (tyh :: tys) = rev (map (domain_type o fastype_of) Rs); |
|
31 |
fun fold_fun ty t = HOLogic.mk_split (Abs ("", ty, t)) |
|
32 |
val abs_rhs = fold fold_fun tys (Abs ("", tyh, Rs_conj)) |
|
33 |
in |
|
34 |
fold fold_fun tys (Abs ("", tyh, abs_rhs)) |
|
35 |
end; |
|
36 |
*} |
|
37 |
||
38 |
||
39 |
ML {* |
|
40 |
fun alpha_bm_lsts lthy dt_descr sorts dts args args2 alpha_frees fv_frees |
|
41 |
bn_alphabn alpha_const binds bodys = |
|
42 |
let |
|
43 |
val thy = ProofContext.theory_of lthy; |
|
44 |
fun bind_set args (NONE, no) = setify thy (nth args no) |
|
45 |
| bind_set args (SOME f, no) = f $ (nth args no) |
|
46 |
fun bind_lst args (NONE, no) = listify thy (nth args no) |
|
47 |
| bind_lst args (SOME f, no) = f $ (nth args no) |
|
48 |
fun append (t1, t2) = |
|
49 |
Const(@{const_name append}, @{typ "atom list \<Rightarrow> atom list \<Rightarrow> atom list"}) $ t1 $ t2; |
|
50 |
fun binds_fn args nos = |
|
51 |
if alpha_const = @{const_name alpha_lst} |
|
52 |
then foldr1 append (map (bind_lst args) nos) |
|
53 |
else mk_union (map (bind_set args) nos); |
|
54 |
val lhs_binds = binds_fn args binds; |
|
55 |
val rhs_binds = binds_fn args2 binds; |
|
56 |
val lhs_bodys = foldr1 HOLogic.mk_prod (map (nth args) bodys); |
|
57 |
val rhs_bodys = foldr1 HOLogic.mk_prod (map (nth args2) bodys); |
|
2010
19fe16dd36c2
replaced make_pair with library function HOLogic.mk_prod
Christian Urban <urbanc@in.tum.de>
parents:
1996
diff
changeset
|
58 |
val lhs = HOLogic.mk_prod (lhs_binds, lhs_bodys); |
19fe16dd36c2
replaced make_pair with library function HOLogic.mk_prod
Christian Urban <urbanc@in.tum.de>
parents:
1996
diff
changeset
|
59 |
val rhs = HOLogic.mk_prod (rhs_binds, rhs_bodys); |
1992 | 60 |
val body_dts = map (nth dts) bodys; |
61 |
fun fv_for_dt dt = |
|
62 |
if Datatype_Aux.is_rec_type dt |
|
63 |
then nth fv_frees (Datatype_Aux.body_index dt) |
|
64 |
else Const (@{const_name supp}, |
|
65 |
Datatype_Aux.typ_of_dtyp dt_descr sorts dt --> @{typ "atom set"}) |
|
66 |
val fvs = map fv_for_dt body_dts; |
|
67 |
val fv = mk_compound_fv fvs; |
|
68 |
fun alpha_for_dt dt = |
|
69 |
if Datatype_Aux.is_rec_type dt |
|
70 |
then nth alpha_frees (Datatype_Aux.body_index dt) |
|
71 |
else Const (@{const_name "op ="}, |
|
72 |
Datatype_Aux.typ_of_dtyp dt_descr sorts dt --> |
|
73 |
Datatype_Aux.typ_of_dtyp dt_descr sorts dt --> @{typ bool}) |
|
74 |
val alphas = map alpha_for_dt body_dts; |
|
75 |
val alpha = mk_compound_alpha alphas; |
|
76 |
val alpha_gen_pre = Const (alpha_const, dummyT) $ lhs $ alpha $ fv $ (Bound 0) $ rhs |
|
77 |
val alpha_gen_ex = HOLogic.exists_const @{typ perm} $ Abs ("p", @{typ perm}, alpha_gen_pre) |
|
78 |
val t = Syntax.check_term lthy alpha_gen_ex |
|
79 |
in |
|
80 |
case binds of |
|
81 |
[(SOME bn, i)] => if i mem bodys then [t] |
|
82 |
else [t, ((the (AList.lookup (op=) bn_alphabn bn)) $ nth args i $ nth args2 i)] |
|
83 |
| _ => [t] |
|
84 |
end |
|
85 |
*} |
|
86 |
||
87 |
ML {* |
|
88 |
fun alpha_bn_bm lthy dt_descr sorts dts args args2 alpha_frees fv_frees bn_alphabn args_in_bn bm = |
|
89 |
case bm of |
|
90 |
BEmy i => |
|
91 |
let |
|
92 |
val arg = nth args i; |
|
93 |
val arg2 = nth args2 i; |
|
94 |
val dt = nth dts i; |
|
95 |
in |
|
96 |
case AList.lookup (op=) args_in_bn i of |
|
97 |
NONE => if Datatype_Aux.is_rec_type dt |
|
98 |
then [(nth alpha_frees (Datatype_Aux.body_index dt)) $ arg $ arg2] |
|
99 |
else [HOLogic.mk_eq (arg, arg2)] |
|
100 |
| SOME (SOME (f : term)) => [(the (AList.lookup (op=) bn_alphabn f)) $ arg $ arg2] |
|
101 |
| SOME NONE => [] |
|
102 |
end |
|
103 |
| BLst (x, y) => alpha_bm_lsts lthy dt_descr sorts dts args args2 alpha_frees |
|
104 |
fv_frees bn_alphabn @{const_name alpha_lst} x y |
|
105 |
| BSet (x, y) => alpha_bm_lsts lthy dt_descr sorts dts args args2 alpha_frees |
|
106 |
fv_frees bn_alphabn @{const_name alpha_gen} x y |
|
107 |
| BRes (x, y) => alpha_bm_lsts lthy dt_descr sorts dts args args2 alpha_frees |
|
108 |
fv_frees bn_alphabn @{const_name alpha_res} x y |
|
109 |
*} |
|
110 |
||
111 |
||
112 |
ML {* |
|
113 |
fun alpha_bn lthy dt_descr sorts alpha_frees fv_frees bn_alphabn bclausess |
|
114 |
(alphabn, (_, ith_dtyp, args_in_bns)) = |
|
115 |
let |
|
116 |
fun alpha_bn_constr (cname, dts) (args_in_bn, bclauses) = |
|
117 |
let |
|
118 |
val Ts = map (Datatype_Aux.typ_of_dtyp dt_descr sorts) dts; |
|
119 |
val names = Datatype_Prop.make_tnames Ts; |
|
120 |
val names2 = Name.variant_list names (Datatype_Prop.make_tnames Ts); |
|
121 |
val args = map Free (names ~~ Ts); |
|
122 |
val args2 = map Free (names2 ~~ Ts); |
|
123 |
val c = Const (cname, Ts ---> (nth_dtyp dt_descr sorts ith_dtyp)); |
|
124 |
val alpha_bn_bm = alpha_bn_bm lthy dt_descr sorts dts args args2 alpha_frees |
|
125 |
fv_frees bn_alphabn args_in_bn; |
|
126 |
val rhs = HOLogic.mk_Trueprop |
|
127 |
(alphabn $ (list_comb (c, args)) $ (list_comb (c, args2))); |
|
128 |
val lhss = map HOLogic.mk_Trueprop (flat (map alpha_bn_bm bclauses)) |
|
129 |
in |
|
130 |
Library.foldr Logic.mk_implies (lhss, rhs) |
|
131 |
end; |
|
132 |
val (_, (_, _, constrs)) = nth dt_descr ith_dtyp; |
|
133 |
in |
|
134 |
map2 alpha_bn_constr constrs (args_in_bns ~~ bclausess) |
|
135 |
end |
|
136 |
*} |
|
137 |
||
138 |
ML {* |
|
139 |
fun alpha_bns lthy dt_descr sorts alpha_frees fv_frees bn_funs bclausesss = |
|
140 |
let |
|
141 |
fun mk_alphabn_free (bn, ith, _) = |
|
142 |
let |
|
143 |
val alphabn_name = "alpha_" ^ (Long_Name.base_name (fst (dest_Const bn))); |
|
144 |
val ty = nth_dtyp dt_descr sorts ith; |
|
145 |
val alphabn_type = ty --> ty --> @{typ bool}; |
|
146 |
val alphabn_free = Free(alphabn_name, alphabn_type); |
|
147 |
in |
|
148 |
(alphabn_name, alphabn_free) |
|
149 |
end; |
|
150 |
val (alphabn_names, alphabn_frees) = split_list (map mk_alphabn_free bn_funs); |
|
151 |
val bn_alphabn = (map (fn (bn, _, _) => bn) bn_funs) ~~ alphabn_frees |
|
152 |
val bclausessl = map (fn (_, i, _) => nth bclausesss i) bn_funs; |
|
153 |
val eqs = map2 (alpha_bn lthy dt_descr sorts alpha_frees fv_frees bn_alphabn) bclausessl |
|
154 |
(alphabn_frees ~~ bn_funs); |
|
155 |
in |
|
156 |
(bn_alphabn, alphabn_names, eqs) |
|
157 |
end |
|
158 |
*} |
|
159 |
||
160 |
ML {* |
|
161 |
fun alpha_bm lthy dt_descr sorts dts args args2 alpha_frees fv_frees bn_alphabn bm = |
|
162 |
case bm of |
|
163 |
BEmy i => |
|
164 |
let |
|
165 |
val arg = nth args i; |
|
166 |
val arg2 = nth args2 i; |
|
167 |
val dt = nth dts i; |
|
168 |
in |
|
169 |
if Datatype_Aux.is_rec_type dt |
|
170 |
then [(nth alpha_frees (Datatype_Aux.body_index dt)) $ arg $ arg2] |
|
171 |
else [HOLogic.mk_eq (arg, arg2)] |
|
172 |
end |
|
173 |
| BLst (x, y) => alpha_bm_lsts lthy dt_descr sorts dts args args2 alpha_frees |
|
174 |
fv_frees bn_alphabn @{const_name alpha_lst} x y |
|
175 |
| BSet (x, y) => alpha_bm_lsts lthy dt_descr sorts dts args args2 alpha_frees |
|
176 |
fv_frees bn_alphabn @{const_name alpha_gen} x y |
|
177 |
| BRes (x, y) => alpha_bm_lsts lthy dt_descr sorts dts args args2 alpha_frees |
|
178 |
fv_frees bn_alphabn @{const_name alpha_res} x y |
|
179 |
*} |
|
180 |
||
181 |
ML {* |
|
182 |
fun alpha lthy dt_descr sorts alpha_frees fv_frees bn_alphabn bclausess (alpha_free, ith_dtyp) = |
|
183 |
let |
|
184 |
fun alpha_constr (cname, dts) bclauses = |
|
185 |
let |
|
186 |
val Ts = map (Datatype_Aux.typ_of_dtyp dt_descr sorts) dts; |
|
187 |
val names = Datatype_Prop.make_tnames Ts; |
|
188 |
val names2 = Name.variant_list names (Datatype_Prop.make_tnames Ts); |
|
189 |
val args = map Free (names ~~ Ts); |
|
190 |
val args2 = map Free (names2 ~~ Ts); |
|
191 |
val c = Const (cname, Ts ---> (nth_dtyp dt_descr sorts ith_dtyp)); |
|
192 |
val alpha_bm = alpha_bm lthy dt_descr sorts dts args args2 alpha_frees fv_frees bn_alphabn |
|
193 |
val rhs = HOLogic.mk_Trueprop |
|
194 |
(alpha_free $ (list_comb (c, args)) $ (list_comb (c, args2))); |
|
195 |
val lhss = map HOLogic.mk_Trueprop (flat (map alpha_bm bclauses)) |
|
196 |
in |
|
197 |
Library.foldr Logic.mk_implies (lhss, rhs) |
|
198 |
end; |
|
199 |
val (_, (_, _, constrs)) = nth dt_descr ith_dtyp; |
|
200 |
in |
|
201 |
map2 alpha_constr constrs bclausess |
|
202 |
end |
|
203 |
*} |
|
204 |
||
205 |
ML {* |
|
206 |
fun define_raw_alpha (dt_info : Datatype_Aux.info) bn_funs bclausesss fv_frees lthy = |
|
207 |
let |
|
208 |
val {descr as dt_descr, sorts, ...} = dt_info; |
|
209 |
||
210 |
val alpha_names = prefix_dt_names dt_descr sorts "alpha_"; |
|
211 |
val alpha_types = map (fn (i, _) => |
|
212 |
nth_dtyp dt_descr sorts i --> nth_dtyp dt_descr sorts i --> @{typ bool}) dt_descr; |
|
213 |
val alpha_frees = map Free (alpha_names ~~ alpha_types); |
|
214 |
||
215 |
val (bn_alphabn, alpha_bn_names, alpha_bn_eqs) = |
|
216 |
alpha_bns lthy dt_descr sorts alpha_frees fv_frees bn_funs bclausesss |
|
217 |
||
218 |
val alpha_bns = map snd bn_alphabn; |
|
219 |
val alpha_bn_types = map fastype_of alpha_bns; |
|
220 |
||
221 |
val alpha_nums = 0 upto (length alpha_frees - 1) |
|
222 |
||
223 |
val alpha_eqs = map2 (alpha lthy dt_descr sorts alpha_frees fv_frees bn_alphabn) bclausesss |
|
224 |
(alpha_frees ~~ alpha_nums); |
|
225 |
||
226 |
val all_alpha_names = map2 (fn s => fn ty => ((Binding.name s, ty), NoSyn)) |
|
227 |
(alpha_names @ alpha_bn_names) (alpha_types @ alpha_bn_types) |
|
228 |
val all_alpha_eqs = map (pair Attrib.empty_binding) (flat alpha_eqs @ flat alpha_bn_eqs) |
|
229 |
||
230 |
val (alphas, lthy') = Inductive.add_inductive_i |
|
231 |
{quiet_mode = true, verbose = false, alt_name = Binding.empty, |
|
232 |
coind = false, no_elim = false, no_ind = false, skip_mono = true, fork_mono = false} |
|
233 |
all_alpha_names [] all_alpha_eqs [] lthy |
|
1996
953f74f40727
Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1992
diff
changeset
|
234 |
|
953f74f40727
Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1992
diff
changeset
|
235 |
val alpha_ts_loc = #preds alphas; |
953f74f40727
Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1992
diff
changeset
|
236 |
val alpha_induct_loc = #induct alphas; |
953f74f40727
Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1992
diff
changeset
|
237 |
val alpha_intros_loc = #intrs alphas; |
953f74f40727
Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1992
diff
changeset
|
238 |
val alpha_cases_loc = #elims alphas; |
953f74f40727
Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1992
diff
changeset
|
239 |
val morphism = ProofContext.export_morphism lthy' lthy; |
953f74f40727
Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1992
diff
changeset
|
240 |
|
953f74f40727
Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1992
diff
changeset
|
241 |
val alpha_ts = map (Morphism.term morphism) alpha_ts_loc; |
953f74f40727
Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1992
diff
changeset
|
242 |
val alpha_induct = Morphism.thm morphism alpha_induct_loc; |
953f74f40727
Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1992
diff
changeset
|
243 |
val alpha_intros = Morphism.fact morphism alpha_intros_loc |
953f74f40727
Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1992
diff
changeset
|
244 |
val alpha_cases = Morphism.fact morphism alpha_cases_loc |
1992 | 245 |
in |
1996
953f74f40727
Change signature of fv and alpha generation.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
1992
diff
changeset
|
246 |
(((alpha_ts, alpha_intros), (alpha_cases, alpha_induct)), lthy') |
1992 | 247 |
end |
248 |
*} |
|
249 |
||
250 |
end |