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