1159
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
1 |
theory Perm
|
1774
|
2 |
imports "../Nominal-General/Nominal2_Atoms"
|
1159
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
3 |
begin
|
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
4 |
|
1910
|
5 |
(* definitions of the permute function for raw nominal datatypes *)
|
1903
|
6 |
|
1683
f78c820f67c3
Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
7 |
|
f78c820f67c3
Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
8 |
ML {*
|
1910
|
9 |
(* returns the type of the nth datatype *)
|
1899
|
10 |
fun nth_dtyp dt_descr sorts i =
|
|
11 |
Datatype_Aux.typ_of_dtyp dt_descr sorts (Datatype_Aux.DtRec i);
|
|
12 |
*}
|
|
13 |
|
|
14 |
ML {*
|
1971
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
15 |
(* generates for every datatype a name str ^ dt_name
|
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
16 |
plus and index for multiple occurences of a string *)
|
1966
b6b3374a402d
factured out common functionality of prefixing the dt-names with a string
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
17 |
fun prefix_dt_names dt_descr sorts str =
|
b6b3374a402d
factured out common functionality of prefixing the dt-names with a string
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
18 |
let
|
b6b3374a402d
factured out common functionality of prefixing the dt-names with a string
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
19 |
fun get_nth_name (i, _) =
|
b6b3374a402d
factured out common functionality of prefixing the dt-names with a string
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
20 |
Datatype_Aux.name_of_typ (nth_dtyp dt_descr sorts i)
|
b6b3374a402d
factured out common functionality of prefixing the dt-names with a string
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
21 |
in
|
b6b3374a402d
factured out common functionality of prefixing the dt-names with a string
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
22 |
Datatype_Prop.indexify_names
|
b6b3374a402d
factured out common functionality of prefixing the dt-names with a string
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
23 |
(map (prefix str o get_nth_name) dt_descr)
|
b6b3374a402d
factured out common functionality of prefixing the dt-names with a string
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
24 |
end
|
b6b3374a402d
factured out common functionality of prefixing the dt-names with a string
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
25 |
*}
|
b6b3374a402d
factured out common functionality of prefixing the dt-names with a string
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
26 |
|
b6b3374a402d
factured out common functionality of prefixing the dt-names with a string
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
27 |
|
b6b3374a402d
factured out common functionality of prefixing the dt-names with a string
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
28 |
ML {*
|
1900
|
29 |
(* permutation function for one argument
|
|
30 |
|
1901
93dfd5a10e92
removed dead code (nominal cannot deal with argument types of constructors that are functions)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
31 |
- in case the argument is recursive it returns
|
93dfd5a10e92
removed dead code (nominal cannot deal with argument types of constructors that are functions)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
32 |
|
93dfd5a10e92
removed dead code (nominal cannot deal with argument types of constructors that are functions)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
33 |
permute_fn p arg
|
93dfd5a10e92
removed dead code (nominal cannot deal with argument types of constructors that are functions)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
34 |
|
2035
3622cae9b10e
to my best knowledge the number of datatypes is equal to the length of the dt_descr; so we can save one argument in define_raw_perm
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
35 |
- in case the argument is non-recursive it will return
|
1900
|
36 |
|
|
37 |
p o arg
|
|
38 |
|
|
39 |
*)
|
2038
|
40 |
fun perm_arg permute_fn_frees p (arg_dty, arg) =
|
1899
|
41 |
if Datatype_Aux.is_rec_type arg_dty
|
2038
|
42 |
then (nth permute_fn_frees (Datatype_Aux.body_index arg_dty)) $ p $ arg
|
2035
3622cae9b10e
to my best knowledge the number of datatypes is equal to the length of the dt_descr; so we can save one argument in define_raw_perm
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
43 |
else mk_perm p arg
|
1899
|
44 |
*}
|
|
45 |
|
|
46 |
ML {*
|
2035
3622cae9b10e
to my best knowledge the number of datatypes is equal to the length of the dt_descr; so we can save one argument in define_raw_perm
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
47 |
(* generates the equation for the permutation function for one constructor;
|
3622cae9b10e
to my best knowledge the number of datatypes is equal to the length of the dt_descr; so we can save one argument in define_raw_perm
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
48 |
i is the index of the corresponding datatype *)
|
2038
|
49 |
fun perm_eq_constr dt_descr sorts permute_fn_frees i (cnstr_name, dts) =
|
1899
|
50 |
let
|
2035
3622cae9b10e
to my best knowledge the number of datatypes is equal to the length of the dt_descr; so we can save one argument in define_raw_perm
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
51 |
val p = Free ("p", @{typ perm})
|
1899
|
52 |
val arg_tys = map (Datatype_Aux.typ_of_dtyp dt_descr sorts) dts
|
|
53 |
val arg_names = Name.variant_list ["p"] (Datatype_Prop.make_tnames arg_tys)
|
|
54 |
val args = map Free (arg_names ~~ arg_tys)
|
|
55 |
val cnstr = Const (cnstr_name, arg_tys ---> (nth_dtyp dt_descr sorts i))
|
2038
|
56 |
val lhs = (nth permute_fn_frees i) $ p $ list_comb (cnstr, args)
|
|
57 |
val rhs = list_comb (cnstr, map (perm_arg permute_fn_frees p) (dts ~~ args))
|
1899
|
58 |
val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
|
|
59 |
in
|
|
60 |
(Attrib.empty_binding, eq)
|
|
61 |
end
|
|
62 |
*}
|
|
63 |
|
|
64 |
ML {*
|
2106
|
65 |
(* proves the two pt-type class properties *)
|
1910
|
66 |
fun prove_permute_zero lthy induct perm_defs perm_fns =
|
|
67 |
let
|
|
68 |
val perm_types = map (body_type o fastype_of) perm_fns
|
|
69 |
val perm_indnames = Datatype_Prop.make_tnames perm_types
|
|
70 |
|
|
71 |
fun single_goal ((perm_fn, T), x) =
|
|
72 |
HOLogic.mk_eq (perm_fn $ @{term "0::perm"} $ Free (x, T), Free (x, T))
|
|
73 |
|
|
74 |
val goals =
|
|
75 |
HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
|
|
76 |
(map single_goal (perm_fns ~~ perm_types ~~ perm_indnames)))
|
|
77 |
|
|
78 |
val simps = HOL_basic_ss addsimps (@{thm permute_zero} :: perm_defs)
|
|
79 |
|
|
80 |
val tac = (Datatype_Aux.indtac induct perm_indnames
|
|
81 |
THEN_ALL_NEW asm_simp_tac simps) 1
|
|
82 |
in
|
|
83 |
Goal.prove lthy perm_indnames [] goals (K tac)
|
|
84 |
|> Datatype_Aux.split_conj_thm
|
|
85 |
end
|
|
86 |
*}
|
|
87 |
|
|
88 |
ML {*
|
|
89 |
fun prove_permute_plus lthy induct perm_defs perm_fns =
|
|
90 |
let
|
2038
|
91 |
val p = Free ("p", @{typ perm})
|
|
92 |
val q = Free ("q", @{typ perm})
|
1910
|
93 |
val perm_types = map (body_type o fastype_of) perm_fns
|
|
94 |
val perm_indnames = Datatype_Prop.make_tnames perm_types
|
|
95 |
|
2038
|
96 |
fun single_goal ((perm_fn, T), x) = HOLogic.mk_eq
|
|
97 |
(perm_fn $ (mk_plus p q) $ Free (x, T), perm_fn $ p $ (perm_fn $ q $ Free (x, T)))
|
1910
|
98 |
|
|
99 |
val goals =
|
|
100 |
HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
|
|
101 |
(map single_goal (perm_fns ~~ perm_types ~~ perm_indnames)))
|
|
102 |
|
|
103 |
val simps = HOL_basic_ss addsimps (@{thm permute_plus} :: perm_defs)
|
|
104 |
|
|
105 |
val tac = (Datatype_Aux.indtac induct perm_indnames
|
|
106 |
THEN_ALL_NEW asm_simp_tac simps) 1
|
|
107 |
in
|
|
108 |
Goal.prove lthy ("p" :: "q" :: perm_indnames) [] goals (K tac)
|
|
109 |
|> Datatype_Aux.split_conj_thm
|
|
110 |
end
|
|
111 |
*}
|
|
112 |
|
|
113 |
ML {*
|
1899
|
114 |
(* defines the permutation functions for raw datatypes and
|
|
115 |
proves that they are instances of pt
|
1910
|
116 |
|
2037
|
117 |
user_dt_nos refers to the number of "un-unfolded" datatypes
|
1910
|
118 |
given by the user
|
1899
|
119 |
*)
|
2047
|
120 |
fun define_raw_perms dt_descr sorts induct_thm user_dt_nos thy =
|
1170
|
121 |
let
|
1899
|
122 |
val all_full_tnames = map (fn (_, (n, _, _)) => n) dt_descr;
|
2037
|
123 |
val user_full_tnames = List.take (all_full_tnames, user_dt_nos);
|
1899
|
124 |
|
1966
b6b3374a402d
factured out common functionality of prefixing the dt-names with a string
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
125 |
val perm_fn_names = prefix_dt_names dt_descr sorts "permute_"
|
2038
|
126 |
val perm_fn_types = map (fn (i, _) => perm_ty (nth_dtyp dt_descr sorts i)) dt_descr
|
|
127 |
val perm_fn_frees = map Free (perm_fn_names ~~ perm_fn_types)
|
1899
|
128 |
|
|
129 |
fun perm_eq (i, (_, _, constrs)) =
|
2038
|
130 |
map (perm_eq_constr dt_descr sorts perm_fn_frees i) constrs;
|
1899
|
131 |
|
|
132 |
val perm_eqs = maps perm_eq dt_descr;
|
|
133 |
|
|
134 |
val lthy =
|
2037
|
135 |
Theory_Target.instantiation (user_full_tnames, [], @{sort pt}) thy;
|
1899
|
136 |
|
2038
|
137 |
val ((perm_funs, perm_eq_thms), lthy') =
|
1899
|
138 |
Primrec.add_primrec
|
|
139 |
(map (fn s => (Binding.name s, NONE, NoSyn)) perm_fn_names) perm_eqs lthy;
|
|
140 |
|
2047
|
141 |
val perm_zero_thms = prove_permute_zero lthy' induct_thm perm_eq_thms perm_funs
|
|
142 |
val perm_plus_thms = prove_permute_plus lthy' induct_thm perm_eq_thms perm_funs
|
2037
|
143 |
val perm_zero_thms' = List.take (perm_zero_thms, user_dt_nos);
|
|
144 |
val perm_plus_thms' = List.take (perm_plus_thms, user_dt_nos)
|
1899
|
145 |
val perms_name = space_implode "_" perm_fn_names
|
|
146 |
val perms_zero_bind = Binding.name (perms_name ^ "_zero")
|
1902
|
147 |
val perms_plus_bind = Binding.name (perms_name ^ "_plus")
|
1903
|
148 |
|
2143
871d8a5e0c67
somewhat simplified the main parsing function; failed to move a Note-statement to define_raw_perms
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
149 |
fun tac _ (_, _, simps) =
|
1910
|
150 |
Class.intro_classes_tac [] THEN ALLGOALS (resolve_tac simps)
|
1903
|
151 |
|
2143
871d8a5e0c67
somewhat simplified the main parsing function; failed to move a Note-statement to define_raw_perms
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
152 |
fun morphism phi (fvs, dfs, simps) =
|
871d8a5e0c67
somewhat simplified the main parsing function; failed to move a Note-statement to define_raw_perms
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
153 |
(map (Morphism.term phi) fvs, map (Morphism.thm phi) dfs, map (Morphism.thm phi) simps);
|
1899
|
154 |
in
|
1910
|
155 |
lthy'
|
|
156 |
|> snd o (Local_Theory.note ((perms_zero_bind, []), perm_zero_thms'))
|
|
157 |
|> snd o (Local_Theory.note ((perms_plus_bind, []), perm_plus_thms'))
|
|
158 |
|> Class_Target.prove_instantiation_exit_result morphism tac
|
2143
871d8a5e0c67
somewhat simplified the main parsing function; failed to move a Note-statement to define_raw_perms
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
159 |
(perm_funs, perm_eq_thms, perm_zero_thms' @ perm_plus_thms')
|
1899
|
160 |
end
|
1159
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
161 |
*}
|
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
162 |
|
2035
3622cae9b10e
to my best knowledge the number of datatypes is equal to the length of the dt_descr; so we can save one argument in define_raw_perm
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
163 |
|
3622cae9b10e
to my best knowledge the number of datatypes is equal to the length of the dt_descr; so we can save one argument in define_raw_perm
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
164 |
|
3622cae9b10e
to my best knowledge the number of datatypes is equal to the length of the dt_descr; so we can save one argument in define_raw_perm
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
165 |
|
3622cae9b10e
to my best knowledge the number of datatypes is equal to the length of the dt_descr; so we can save one argument in define_raw_perm
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
166 |
|
1910
|
167 |
(* permutations for quotient types *)
|
1899
|
168 |
|
2144
|
169 |
ML {* Class_Target.prove_instantiation_exit_result *}
|
|
170 |
|
1253
|
171 |
ML {*
|
1903
|
172 |
fun quotient_lift_consts_export qtys spec ctxt =
|
|
173 |
let
|
|
174 |
val (result, ctxt') = fold_map (Quotient_Def.quotient_lift_const qtys) spec ctxt;
|
|
175 |
val (ts_loc, defs_loc) = split_list result;
|
|
176 |
val morphism = ProofContext.export_morphism ctxt' ctxt;
|
|
177 |
val ts = map (Morphism.term morphism) ts_loc
|
|
178 |
val defs = Morphism.fact morphism defs_loc
|
|
179 |
in
|
|
180 |
(ts, defs, ctxt')
|
|
181 |
end
|
|
182 |
*}
|
|
183 |
|
|
184 |
ML {*
|
1683
f78c820f67c3
Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
185 |
fun define_lifted_perms qtys full_tnames name_term_pairs thms thy =
|
1253
|
186 |
let
|
|
187 |
val lthy =
|
|
188 |
Theory_Target.instantiation (full_tnames, [], @{sort pt}) thy;
|
1683
f78c820f67c3
Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
189 |
val (_, _, lthy') = quotient_lift_consts_export qtys name_term_pairs lthy;
|
f78c820f67c3
Automatically lift theorems and constants only using the new quotient types. Requires new Isabelle.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
190 |
val lifted_thms = map (Quotient_Tacs.lifted qtys lthy') thms;
|
1253
|
191 |
fun tac _ =
|
|
192 |
Class.intro_classes_tac [] THEN
|
|
193 |
(ALLGOALS (resolve_tac lifted_thms))
|
|
194 |
val lthy'' = Class.prove_instantiation_instance tac lthy'
|
|
195 |
in
|
|
196 |
Local_Theory.exit_global lthy''
|
|
197 |
end
|
|
198 |
*}
|
|
199 |
|
1342
|
200 |
ML {*
|
|
201 |
fun neq_to_rel r neq =
|
|
202 |
let
|
|
203 |
val neq = HOLogic.dest_Trueprop (prop_of neq)
|
|
204 |
val eq = HOLogic.dest_not neq
|
|
205 |
val (lhs, rhs) = HOLogic.dest_eq eq
|
|
206 |
val rel = r $ lhs $ rhs
|
|
207 |
val nrel = HOLogic.mk_not rel
|
|
208 |
in
|
|
209 |
HOLogic.mk_Trueprop nrel
|
|
210 |
end
|
|
211 |
*}
|
|
212 |
|
|
213 |
ML {*
|
|
214 |
fun neq_to_rel_tac cases distinct =
|
|
215 |
rtac notI THEN' eresolve_tac cases THEN_ALL_NEW asm_full_simp_tac (HOL_ss addsimps distinct)
|
|
216 |
*}
|
|
217 |
|
|
218 |
ML {*
|
|
219 |
fun distinct_rel ctxt cases (dists, rel) =
|
|
220 |
let
|
|
221 |
val ((_, thms), ctxt') = Variable.import false dists ctxt
|
|
222 |
val terms = map (neq_to_rel rel) thms
|
|
223 |
val nrels = map (fn t => Goal.prove ctxt' [] [] t (fn _ => neq_to_rel_tac cases dists 1)) terms
|
|
224 |
in
|
|
225 |
Variable.export ctxt' ctxt nrels
|
|
226 |
end
|
|
227 |
*}
|
|
228 |
|
|
229 |
|
|
230 |
|
1899
|
231 |
(* Test *)
|
1910
|
232 |
(*
|
|
233 |
atom_decl name
|
1159
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
234 |
|
1896
996d4411e95e
tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
235 |
datatype trm =
|
996d4411e95e
tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
236 |
Var "name"
|
1910
|
237 |
| App "trm" "(trm list) list"
|
1896
996d4411e95e
tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
238 |
| Lam "name" "trm"
|
996d4411e95e
tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
239 |
| Let "bp" "trm" "trm"
|
1170
|
240 |
and bp =
|
|
241 |
BUnit
|
1896
996d4411e95e
tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
242 |
| BVar "name"
|
996d4411e95e
tuned; fleshed out some library functions about permutations; closed Datatype_Aux structure (increases readability)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
243 |
| BPair "bp" "bp"
|
1159
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
244 |
|
1899
|
245 |
setup {* fn thy =>
|
|
246 |
let
|
1910
|
247 |
val info = Datatype.the_info thy "Perm.trm"
|
1899
|
248 |
in
|
1910
|
249 |
define_raw_perms info 2 thy |> snd
|
1899
|
250 |
end
|
|
251 |
*}
|
1170
|
252 |
|
|
253 |
print_theorems
|
|
254 |
*)
|
|
255 |
|
1159
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
256 |
end
|