author | urbanc |
Wed, 02 Nov 2005 15:31:12 +0100 | |
changeset 18067 | 8b9848d150ba |
parent 18066 | d1e47ee13070 |
child 18068 | e8c3d371594e |
permissions | -rw-r--r-- |
17870 | 1 |
(* $Id$ *) |
2 |
||
3 |
signature NOMINAL_PACKAGE = |
|
4 |
sig |
|
5 |
val create_nom_typedecls : string list -> theory -> theory |
|
6 |
val add_nominal_datatype : bool -> string list -> (string list * bstring * mixfix * |
|
7 |
(bstring * string list * mixfix) list) list -> theory -> theory * |
|
8 |
{distinct : thm list list, |
|
9 |
inject : thm list list, |
|
10 |
exhaustion : thm list, |
|
11 |
rec_thms : thm list, |
|
12 |
case_thms : thm list list, |
|
13 |
split_thms : (thm * thm) list, |
|
14 |
induction : thm, |
|
15 |
size : thm list, |
|
16 |
simps : thm list} |
|
17 |
val setup : (theory -> theory) list |
|
18 |
end |
|
19 |
||
20 |
structure NominalPackage (*: NOMINAL_PACKAGE *) = |
|
21 |
struct |
|
22 |
||
23 |
open DatatypeAux; |
|
24 |
||
25 |
(* data kind 'HOL/nominal' *) |
|
26 |
||
27 |
structure NominalArgs = |
|
28 |
struct |
|
29 |
val name = "HOL/nominal"; |
|
30 |
type T = unit Symtab.table; |
|
31 |
||
32 |
val empty = Symtab.empty; |
|
33 |
val copy = I; |
|
34 |
val extend = I; |
|
35 |
fun merge _ x = Symtab.merge (K true) x; |
|
36 |
||
37 |
fun print sg tab = (); |
|
38 |
end; |
|
39 |
||
40 |
structure NominalData = TheoryDataFun(NominalArgs); |
|
41 |
||
42 |
fun atoms_of thy = map fst (Symtab.dest (NominalData.get thy)); |
|
43 |
||
44 |
(* FIXME: add to hologic.ML ? *) |
|
45 |
fun mk_listT T = Type ("List.list", [T]); |
|
46 |
fun mk_permT T = mk_listT (HOLogic.mk_prodT (T, T)); |
|
47 |
||
48 |
fun mk_Cons x xs = |
|
49 |
let val T = fastype_of x |
|
50 |
in Const ("List.list.Cons", T --> mk_listT T --> mk_listT T) $ x $ xs end; |
|
51 |
||
18067 | 52 |
(* FIXME: should be a library function *) |
53 |
fun cprod ([], ys) = [] |
|
54 |
| cprod (x :: xs, ys) = map (pair x) ys @ cprod (xs, ys); |
|
17870 | 55 |
|
56 |
(* this function sets up all matters related to atom- *) |
|
57 |
(* kinds; the user specifies a list of atom-kind names *) |
|
58 |
(* atom_decl <ak1> ... <akn> *) |
|
59 |
fun create_nom_typedecls ak_names thy = |
|
60 |
let |
|
61 |
(* declares a type-decl for every atom-kind: *) |
|
62 |
(* that is typedecl <ak> *) |
|
63 |
val thy1 = TypedefPackage.add_typedecls (map (fn x => (x,[],NoSyn)) ak_names) thy; |
|
64 |
||
65 |
(* produces a list consisting of pairs: *) |
|
66 |
(* fst component is the atom-kind name *) |
|
67 |
(* snd component is its type *) |
|
68 |
val full_ak_names = map (Sign.intern_type (sign_of thy1)) ak_names; |
|
69 |
val ak_names_types = ak_names ~~ map (Type o rpair []) full_ak_names; |
|
70 |
||
71 |
(* adds for every atom-kind an axiom *) |
|
72 |
(* <ak>_infinite: infinite (UNIV::<ak_type> set) *) |
|
73 |
val (thy2,inf_axs) = PureThy.add_axioms_i (map (fn (ak_name, T) => |
|
74 |
let |
|
75 |
val name = ak_name ^ "_infinite" |
|
76 |
val axiom = HOLogic.mk_Trueprop (HOLogic.mk_not |
|
77 |
(HOLogic.mk_mem (HOLogic.mk_UNIV T, |
|
78 |
Const ("Finite_Set.Finites", HOLogic.mk_setT (HOLogic.mk_setT T))))) |
|
79 |
in |
|
80 |
((name, axiom), []) |
|
81 |
end) ak_names_types) thy1; |
|
82 |
||
83 |
(* declares a swapping function for every atom-kind, it is *) |
|
84 |
(* const swap_<ak> :: <akT> * <akT> => <akT> => <akT> *) |
|
85 |
(* swap_<ak> (a,b) c = (if a=c then b (else if b=c then a else c)) *) |
|
86 |
(* overloades then the general swap-function *) |
|
87 |
val (thy3, swap_eqs) = foldl_map (fn (thy, (ak_name, T)) => |
|
88 |
let |
|
89 |
val swapT = HOLogic.mk_prodT (T, T) --> T --> T; |
|
90 |
val swap_name = Sign.full_name (sign_of thy) ("swap_" ^ ak_name); |
|
91 |
val a = Free ("a", T); |
|
92 |
val b = Free ("b", T); |
|
93 |
val c = Free ("c", T); |
|
94 |
val ab = Free ("ab", HOLogic.mk_prodT (T, T)) |
|
95 |
val cif = Const ("HOL.If", HOLogic.boolT --> T --> T --> T); |
|
96 |
val cswap_akname = Const (swap_name, swapT); |
|
97 |
val cswap = Const ("nominal.swap", swapT) |
|
98 |
||
99 |
val name = "swap_"^ak_name^"_def"; |
|
100 |
val def1 = HOLogic.mk_Trueprop (HOLogic.mk_eq |
|
101 |
(cswap_akname $ HOLogic.mk_prod (a,b) $ c, |
|
102 |
cif $ HOLogic.mk_eq (a,c) $ b $ (cif $ HOLogic.mk_eq (b,c) $ a $ c))) |
|
103 |
val def2 = Logic.mk_equals (cswap $ ab $ c, cswap_akname $ ab $ c) |
|
104 |
in |
|
105 |
thy |> Theory.add_consts_i [("swap_" ^ ak_name, swapT, NoSyn)] |
|
106 |
|> (#1 o PureThy.add_defs_i true [((name, def2),[])]) |
|
107 |
|> PrimrecPackage.add_primrec_i "" [(("", def1),[])] |
|
108 |
end) (thy2, ak_names_types); |
|
109 |
||
110 |
(* declares a permutation function for every atom-kind acting *) |
|
111 |
(* on such atoms *) |
|
112 |
(* const <ak>_prm_<ak> :: (<akT> * <akT>)list => akT => akT *) |
|
113 |
(* <ak>_prm_<ak> [] a = a *) |
|
114 |
(* <ak>_prm_<ak> (x#xs) a = swap_<ak> x (perm xs a) *) |
|
115 |
val (thy4, prm_eqs) = foldl_map (fn (thy, (ak_name, T)) => |
|
116 |
let |
|
117 |
val swapT = HOLogic.mk_prodT (T, T) --> T --> T; |
|
118 |
val swap_name = Sign.full_name (sign_of thy) ("swap_" ^ ak_name) |
|
119 |
val prmT = mk_permT T --> T --> T; |
|
120 |
val prm_name = ak_name ^ "_prm_" ^ ak_name; |
|
121 |
val qu_prm_name = Sign.full_name (sign_of thy) prm_name; |
|
122 |
val x = Free ("x", HOLogic.mk_prodT (T, T)); |
|
123 |
val xs = Free ("xs", mk_permT T); |
|
124 |
val a = Free ("a", T) ; |
|
125 |
||
126 |
val cnil = Const ("List.list.Nil", mk_permT T); |
|
127 |
||
128 |
val def1 = HOLogic.mk_Trueprop (HOLogic.mk_eq (Const (qu_prm_name, prmT) $ cnil $ a, a)); |
|
129 |
||
130 |
val def2 = HOLogic.mk_Trueprop (HOLogic.mk_eq |
|
131 |
(Const (qu_prm_name, prmT) $ mk_Cons x xs $ a, |
|
132 |
Const (swap_name, swapT) $ x $ (Const (qu_prm_name, prmT) $ xs $ a))); |
|
133 |
in |
|
134 |
thy |> Theory.add_consts_i [(prm_name, mk_permT T --> T --> T, NoSyn)] |
|
135 |
|> PrimrecPackage.add_primrec_i "" [(("", def1), []),(("", def2), [])] |
|
136 |
end) (thy3, ak_names_types); |
|
137 |
||
138 |
(* defines permutation functions for all combinations of atom-kinds; *) |
|
139 |
(* there are a trivial cases and non-trivial cases *) |
|
140 |
(* non-trivial case: *) |
|
141 |
(* <ak>_prm_<ak>_def: perm pi a == <ak>_prm_<ak> pi a *) |
|
142 |
(* trivial case with <ak> != <ak'> *) |
|
143 |
(* <ak>_prm<ak'>_def[simp]: perm pi a == a *) |
|
144 |
(* *) |
|
145 |
(* the trivial cases are added to the simplifier, while the non- *) |
|
146 |
(* have their own rules proved below *) |
|
147 |
val (thy5, perm_defs) = foldl_map (fn (thy, (ak_name, T)) => |
|
148 |
foldl_map (fn (thy', (ak_name', T')) => |
|
149 |
let |
|
150 |
val perm_def_name = ak_name ^ "_prm_" ^ ak_name'; |
|
151 |
val pi = Free ("pi", mk_permT T); |
|
152 |
val a = Free ("a", T'); |
|
153 |
val cperm = Const ("nominal.perm", mk_permT T --> T' --> T'); |
|
154 |
val cperm_def = Const (Sign.full_name (sign_of thy') perm_def_name, mk_permT T --> T' --> T'); |
|
155 |
||
156 |
val name = ak_name ^ "_prm_" ^ ak_name' ^ "_def"; |
|
157 |
val def = Logic.mk_equals |
|
158 |
(cperm $ pi $ a, if ak_name = ak_name' then cperm_def $ pi $ a else a) |
|
159 |
in |
|
160 |
thy' |> PureThy.add_defs_i true [((name, def),[])] |
|
161 |
end) (thy, ak_names_types)) (thy4, ak_names_types); |
|
162 |
||
163 |
(* proves that every atom-kind is an instance of at *) |
|
164 |
(* lemma at_<ak>_inst: *) |
|
165 |
(* at TYPE(<ak>) *) |
|
166 |
val (thy6, prm_cons_thms) = |
|
167 |
thy5 |> PureThy.add_thms (map (fn (ak_name, T) => |
|
168 |
let |
|
169 |
val ak_name_qu = Sign.full_name (sign_of thy5) (ak_name); |
|
170 |
val i_type = Type(ak_name_qu,[]); |
|
171 |
val cat = Const ("nominal.at",(Term.itselfT i_type) --> HOLogic.boolT); |
|
172 |
val at_type = Logic.mk_type i_type; |
|
173 |
val simp_s = HOL_basic_ss addsimps PureThy.get_thmss thy5 |
|
174 |
[Name "at_def", |
|
175 |
Name (ak_name ^ "_prm_" ^ ak_name ^ "_def"), |
|
176 |
Name (ak_name ^ "_prm_" ^ ak_name ^ ".simps"), |
|
177 |
Name ("swap_" ^ ak_name ^ "_def"), |
|
178 |
Name ("swap_" ^ ak_name ^ ".simps"), |
|
179 |
Name (ak_name ^ "_infinite")] |
|
180 |
||
181 |
val name = "at_"^ak_name^ "_inst"; |
|
182 |
val statement = HOLogic.mk_Trueprop (cat $ at_type); |
|
183 |
||
18010 | 184 |
val proof = fn _ => auto_tac (claset(),simp_s); |
17870 | 185 |
|
186 |
in |
|
18010 | 187 |
((name, standard (Goal.prove thy5 [] [] statement proof)), []) |
17870 | 188 |
end) ak_names_types); |
189 |
||
190 |
(* declares a perm-axclass for every atom-kind *) |
|
191 |
(* axclass pt_<ak> *) |
|
192 |
(* pt_<ak>1[simp]: perm [] x = x *) |
|
193 |
(* pt_<ak>2: perm (pi1@pi2) x = perm pi1 (perm pi2 x) *) |
|
194 |
(* pt_<ak>3: pi1 ~ pi2 ==> perm pi1 x = perm pi2 x *) |
|
195 |
val (thy7, pt_ax_classes) = foldl_map (fn (thy, (ak_name, T)) => |
|
196 |
let |
|
197 |
val cl_name = "pt_"^ak_name; |
|
198 |
val ty = TFree("'a",["HOL.type"]); |
|
199 |
val x = Free ("x", ty); |
|
200 |
val pi1 = Free ("pi1", mk_permT T); |
|
201 |
val pi2 = Free ("pi2", mk_permT T); |
|
202 |
val cperm = Const ("nominal.perm", mk_permT T --> ty --> ty); |
|
203 |
val cnil = Const ("List.list.Nil", mk_permT T); |
|
204 |
val cappend = Const ("List.op @",mk_permT T --> mk_permT T --> mk_permT T); |
|
205 |
val cprm_eq = Const ("nominal.prm_eq",mk_permT T --> mk_permT T --> HOLogic.boolT); |
|
206 |
(* nil axiom *) |
|
207 |
val axiom1 = HOLogic.mk_Trueprop (HOLogic.mk_eq |
|
208 |
(cperm $ cnil $ x, x)); |
|
209 |
(* append axiom *) |
|
210 |
val axiom2 = HOLogic.mk_Trueprop (HOLogic.mk_eq |
|
211 |
(cperm $ (cappend $ pi1 $ pi2) $ x, cperm $ pi1 $ (cperm $ pi2 $ x))); |
|
212 |
(* perm-eq axiom *) |
|
213 |
val axiom3 = Logic.mk_implies |
|
214 |
(HOLogic.mk_Trueprop (cprm_eq $ pi1 $ pi2), |
|
215 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (cperm $ pi1 $ x, cperm $ pi2 $ x))); |
|
216 |
in |
|
217 |
thy |> AxClass.add_axclass_i (cl_name, ["HOL.type"]) |
|
218 |
[((cl_name^"1", axiom1),[Simplifier.simp_add_global]), |
|
219 |
((cl_name^"2", axiom2),[]), |
|
220 |
((cl_name^"3", axiom3),[])] |
|
221 |
end) (thy6,ak_names_types); |
|
222 |
||
223 |
(* proves that every pt_<ak>-type together with <ak>-type *) |
|
224 |
(* instance of pt *) |
|
225 |
(* lemma pt_<ak>_inst: *) |
|
226 |
(* pt TYPE('x::pt_<ak>) TYPE(<ak>) *) |
|
227 |
val (thy8, prm_inst_thms) = |
|
228 |
thy7 |> PureThy.add_thms (map (fn (ak_name, T) => |
|
229 |
let |
|
230 |
val ak_name_qu = Sign.full_name (sign_of thy7) (ak_name); |
|
231 |
val pt_name_qu = Sign.full_name (sign_of thy7) ("pt_"^ak_name); |
|
232 |
val i_type1 = TFree("'x",[pt_name_qu]); |
|
233 |
val i_type2 = Type(ak_name_qu,[]); |
|
234 |
val cpt = Const ("nominal.pt",(Term.itselfT i_type1)-->(Term.itselfT i_type2)-->HOLogic.boolT); |
|
235 |
val pt_type = Logic.mk_type i_type1; |
|
236 |
val at_type = Logic.mk_type i_type2; |
|
237 |
val simp_s = HOL_basic_ss addsimps PureThy.get_thmss thy7 |
|
238 |
[Name "pt_def", |
|
239 |
Name ("pt_" ^ ak_name ^ "1"), |
|
240 |
Name ("pt_" ^ ak_name ^ "2"), |
|
241 |
Name ("pt_" ^ ak_name ^ "3")]; |
|
242 |
||
243 |
val name = "pt_"^ak_name^ "_inst"; |
|
244 |
val statement = HOLogic.mk_Trueprop (cpt $ pt_type $ at_type); |
|
245 |
||
18010 | 246 |
val proof = fn _ => auto_tac (claset(),simp_s); |
17870 | 247 |
in |
18010 | 248 |
((name, standard (Goal.prove thy7 [] [] statement proof)), []) |
17870 | 249 |
end) ak_names_types); |
250 |
||
251 |
(* declares an fs-axclass for every atom-kind *) |
|
252 |
(* axclass fs_<ak> *) |
|
253 |
(* fs_<ak>1: finite ((supp x)::<ak> set) *) |
|
254 |
val (thy11, fs_ax_classes) = foldl_map (fn (thy, (ak_name, T)) => |
|
255 |
let |
|
256 |
val cl_name = "fs_"^ak_name; |
|
257 |
val pt_name = Sign.full_name (sign_of thy) ("pt_"^ak_name); |
|
258 |
val ty = TFree("'a",["HOL.type"]); |
|
259 |
val x = Free ("x", ty); |
|
260 |
val csupp = Const ("nominal.supp", ty --> HOLogic.mk_setT T); |
|
261 |
val cfinites = Const ("Finite_Set.Finites", HOLogic.mk_setT (HOLogic.mk_setT T)) |
|
262 |
||
263 |
val axiom1 = HOLogic.mk_Trueprop (HOLogic.mk_mem (csupp $ x, cfinites)); |
|
264 |
||
265 |
in |
|
266 |
thy |> AxClass.add_axclass_i (cl_name, [pt_name]) [((cl_name^"1", axiom1),[])] |
|
267 |
end) (thy8,ak_names_types); |
|
268 |
||
269 |
(* proves that every fs_<ak>-type together with <ak>-type *) |
|
270 |
(* instance of fs-type *) |
|
271 |
(* lemma abst_<ak>_inst: *) |
|
272 |
(* fs TYPE('x::pt_<ak>) TYPE (<ak>) *) |
|
273 |
val (thy12, fs_inst_thms) = |
|
274 |
thy11 |> PureThy.add_thms (map (fn (ak_name, T) => |
|
275 |
let |
|
276 |
val ak_name_qu = Sign.full_name (sign_of thy11) (ak_name); |
|
277 |
val fs_name_qu = Sign.full_name (sign_of thy11) ("fs_"^ak_name); |
|
278 |
val i_type1 = TFree("'x",[fs_name_qu]); |
|
279 |
val i_type2 = Type(ak_name_qu,[]); |
|
280 |
val cfs = Const ("nominal.fs", |
|
281 |
(Term.itselfT i_type1)-->(Term.itselfT i_type2)-->HOLogic.boolT); |
|
282 |
val fs_type = Logic.mk_type i_type1; |
|
283 |
val at_type = Logic.mk_type i_type2; |
|
284 |
val simp_s = HOL_basic_ss addsimps PureThy.get_thmss thy11 |
|
285 |
[Name "fs_def", |
|
286 |
Name ("fs_" ^ ak_name ^ "1")]; |
|
287 |
||
288 |
val name = "fs_"^ak_name^ "_inst"; |
|
289 |
val statement = HOLogic.mk_Trueprop (cfs $ fs_type $ at_type); |
|
290 |
||
18010 | 291 |
val proof = fn _ => auto_tac (claset(),simp_s); |
17870 | 292 |
in |
18010 | 293 |
((name, standard (Goal.prove thy11 [] [] statement proof)), []) |
17870 | 294 |
end) ak_names_types); |
295 |
||
296 |
(* declares for every atom-kind combination an axclass *) |
|
297 |
(* cp_<ak1>_<ak2> giving a composition property *) |
|
298 |
(* cp_<ak1>_<ak2>1: pi1 o pi2 o x = (pi1 o pi2) o (pi1 o x) *) |
|
299 |
val (thy12b,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
300 |
foldl_map (fn (thy', (ak_name', T')) => |
|
301 |
let |
|
302 |
val cl_name = "cp_"^ak_name^"_"^ak_name'; |
|
303 |
val ty = TFree("'a",["HOL.type"]); |
|
304 |
val x = Free ("x", ty); |
|
305 |
val pi1 = Free ("pi1", mk_permT T); |
|
306 |
val pi2 = Free ("pi2", mk_permT T'); |
|
307 |
val cperm1 = Const ("nominal.perm", mk_permT T --> ty --> ty); |
|
308 |
val cperm2 = Const ("nominal.perm", mk_permT T' --> ty --> ty); |
|
309 |
val cperm3 = Const ("nominal.perm", mk_permT T --> mk_permT T' --> mk_permT T'); |
|
310 |
||
311 |
val ax1 = HOLogic.mk_Trueprop |
|
312 |
(HOLogic.mk_eq (cperm1 $ pi1 $ (cperm2 $ pi2 $ x), |
|
313 |
cperm2 $ (cperm3 $ pi1 $ pi2) $ (cperm1 $ pi1 $ x))); |
|
314 |
in |
|
315 |
(fst (AxClass.add_axclass_i (cl_name, ["HOL.type"]) [((cl_name^"1", ax1),[])] thy'),()) |
|
316 |
end) |
|
317 |
(thy, ak_names_types)) (thy12, ak_names_types) |
|
318 |
||
319 |
(* proves for every <ak>-combination a cp_<ak1>_<ak2>_inst theorem; *) |
|
320 |
(* lemma cp_<ak1>_<ak2>_inst: *) |
|
321 |
(* cp TYPE('a::cp_<ak1>_<ak2>) TYPE(<ak1>) TYPE(<ak2>) *) |
|
322 |
val (thy12c, cp_thms) = foldl_map (fn (thy, (ak_name, T)) => |
|
323 |
foldl_map (fn (thy', (ak_name', T')) => |
|
324 |
let |
|
325 |
val ak_name_qu = Sign.full_name (sign_of thy') (ak_name); |
|
326 |
val ak_name_qu' = Sign.full_name (sign_of thy') (ak_name'); |
|
327 |
val cp_name_qu = Sign.full_name (sign_of thy') ("cp_"^ak_name^"_"^ak_name'); |
|
328 |
val i_type0 = TFree("'a",[cp_name_qu]); |
|
329 |
val i_type1 = Type(ak_name_qu,[]); |
|
330 |
val i_type2 = Type(ak_name_qu',[]); |
|
331 |
val ccp = Const ("nominal.cp", |
|
332 |
(Term.itselfT i_type0)-->(Term.itselfT i_type1)--> |
|
333 |
(Term.itselfT i_type2)-->HOLogic.boolT); |
|
334 |
val at_type = Logic.mk_type i_type1; |
|
335 |
val at_type' = Logic.mk_type i_type2; |
|
336 |
val cp_type = Logic.mk_type i_type0; |
|
337 |
val simp_s = HOL_basic_ss addsimps PureThy.get_thmss thy' [(Name "cp_def")]; |
|
338 |
val cp1 = PureThy.get_thm thy' (Name ("cp_"^ak_name^"_"^ak_name'^"1")); |
|
339 |
||
340 |
val name = "cp_"^ak_name^ "_"^ak_name'^"_inst"; |
|
341 |
val statement = HOLogic.mk_Trueprop (ccp $ cp_type $ at_type $ at_type'); |
|
342 |
||
18010 | 343 |
val proof = fn _ => EVERY [auto_tac (claset(),simp_s), rtac cp1 1]; |
17870 | 344 |
in |
345 |
thy' |> PureThy.add_thms |
|
18010 | 346 |
[((name, standard (Goal.prove thy' [] [] statement proof)), [])] |
17870 | 347 |
end) |
348 |
(thy, ak_names_types)) (thy12b, ak_names_types); |
|
349 |
||
350 |
(* proves for every non-trivial <ak>-combination a disjointness *) |
|
351 |
(* theorem; i.e. <ak1> != <ak2> *) |
|
352 |
(* lemma ds_<ak1>_<ak2>: *) |
|
353 |
(* dj TYPE(<ak1>) TYPE(<ak2>) *) |
|
354 |
val (thy12d, dj_thms) = foldl_map (fn (thy, (ak_name, T)) => |
|
355 |
foldl_map (fn (thy', (ak_name', T')) => |
|
356 |
(if not (ak_name = ak_name') |
|
357 |
then |
|
358 |
let |
|
359 |
val ak_name_qu = Sign.full_name (sign_of thy') (ak_name); |
|
360 |
val ak_name_qu' = Sign.full_name (sign_of thy') (ak_name'); |
|
361 |
val i_type1 = Type(ak_name_qu,[]); |
|
362 |
val i_type2 = Type(ak_name_qu',[]); |
|
363 |
val cdj = Const ("nominal.disjoint", |
|
364 |
(Term.itselfT i_type1)-->(Term.itselfT i_type2)-->HOLogic.boolT); |
|
365 |
val at_type = Logic.mk_type i_type1; |
|
366 |
val at_type' = Logic.mk_type i_type2; |
|
367 |
val simp_s = HOL_basic_ss addsimps PureThy.get_thmss thy' |
|
368 |
[Name "disjoint_def", |
|
369 |
Name (ak_name^"_prm_"^ak_name'^"_def"), |
|
370 |
Name (ak_name'^"_prm_"^ak_name^"_def")]; |
|
371 |
||
372 |
val name = "dj_"^ak_name^"_"^ak_name'; |
|
373 |
val statement = HOLogic.mk_Trueprop (cdj $ at_type $ at_type'); |
|
374 |
||
18010 | 375 |
val proof = fn _ => auto_tac (claset(),simp_s); |
17870 | 376 |
in |
377 |
thy' |> PureThy.add_thms |
|
18010 | 378 |
[((name, standard (Goal.prove thy' [] [] statement proof)), []) ] |
17870 | 379 |
end |
380 |
else |
|
381 |
(thy',[]))) (* do nothing branch, if ak_name = ak_name' *) |
|
382 |
(thy, ak_names_types)) (thy12c, ak_names_types); |
|
383 |
||
384 |
(*<<<<<<< pt_<ak> class instances >>>>>>>*) |
|
385 |
(*=========================================*) |
|
386 |
||
387 |
(* some frequently used theorems *) |
|
388 |
val pt1 = PureThy.get_thm thy12c (Name "pt1"); |
|
389 |
val pt2 = PureThy.get_thm thy12c (Name "pt2"); |
|
390 |
val pt3 = PureThy.get_thm thy12c (Name "pt3"); |
|
391 |
val at_pt_inst = PureThy.get_thm thy12c (Name "at_pt_inst"); |
|
392 |
val pt_bool_inst = PureThy.get_thm thy12c (Name "pt_bool_inst"); |
|
393 |
val pt_set_inst = PureThy.get_thm thy12c (Name "pt_set_inst"); |
|
394 |
val pt_unit_inst = PureThy.get_thm thy12c (Name "pt_unit_inst"); |
|
395 |
val pt_prod_inst = PureThy.get_thm thy12c (Name "pt_prod_inst"); |
|
396 |
val pt_list_inst = PureThy.get_thm thy12c (Name "pt_list_inst"); |
|
397 |
val pt_optn_inst = PureThy.get_thm thy12c (Name "pt_option_inst"); |
|
398 |
val pt_noptn_inst = PureThy.get_thm thy12c (Name "pt_noption_inst"); |
|
399 |
val pt_fun_inst = PureThy.get_thm thy12c (Name "pt_fun_inst"); |
|
400 |
||
401 |
(* for all atom-kind combination shows that *) |
|
402 |
(* every <ak> is an instance of pt_<ai> *) |
|
403 |
val (thy13,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
404 |
foldl_map (fn (thy', (ak_name', T')) => |
|
405 |
(if ak_name = ak_name' |
|
406 |
then |
|
407 |
let |
|
408 |
val qu_name = Sign.full_name (sign_of thy') ak_name; |
|
409 |
val qu_class = Sign.full_name (sign_of thy') ("pt_"^ak_name); |
|
410 |
val at_inst = PureThy.get_thm thy' (Name ("at_"^ak_name ^"_inst")); |
|
411 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
412 |
rtac ((at_inst RS at_pt_inst) RS pt1) 1, |
|
413 |
rtac ((at_inst RS at_pt_inst) RS pt2) 1, |
|
414 |
rtac ((at_inst RS at_pt_inst) RS pt3) 1, |
|
415 |
atac 1]; |
|
416 |
in |
|
417 |
(AxClass.add_inst_arity_i (qu_name,[],[qu_class]) proof thy',()) |
|
418 |
end |
|
419 |
else |
|
420 |
let |
|
421 |
val qu_name' = Sign.full_name (sign_of thy') ak_name'; |
|
422 |
val qu_class = Sign.full_name (sign_of thy') ("pt_"^ak_name); |
|
423 |
val simp_s = HOL_basic_ss addsimps |
|
424 |
PureThy.get_thmss thy' [Name (ak_name^"_prm_"^ak_name'^"_def")]; |
|
425 |
val proof = EVERY [AxClass.intro_classes_tac [], auto_tac (claset(),simp_s)]; |
|
426 |
in |
|
427 |
(AxClass.add_inst_arity_i (qu_name',[],[qu_class]) proof thy',()) |
|
428 |
end)) |
|
429 |
(thy, ak_names_types)) (thy12c, ak_names_types); |
|
430 |
||
431 |
(* shows that bool is an instance of pt_<ak> *) |
|
432 |
(* uses the theorem pt_bool_inst *) |
|
433 |
val (thy14,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
434 |
let |
|
435 |
val qu_class = Sign.full_name (sign_of thy) ("pt_"^ak_name); |
|
436 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
437 |
rtac (pt_bool_inst RS pt1) 1, |
|
438 |
rtac (pt_bool_inst RS pt2) 1, |
|
439 |
rtac (pt_bool_inst RS pt3) 1, |
|
440 |
atac 1]; |
|
441 |
in |
|
442 |
(AxClass.add_inst_arity_i ("bool",[],[qu_class]) proof thy,()) |
|
443 |
end) (thy13,ak_names_types); |
|
444 |
||
445 |
(* shows that set(pt_<ak>) is an instance of pt_<ak> *) |
|
446 |
(* unfolds the permutation definition and applies pt_<ak>i *) |
|
447 |
val (thy15,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
448 |
let |
|
449 |
val qu_class = Sign.full_name (sign_of thy) ("pt_"^ak_name); |
|
450 |
val pt_inst = PureThy.get_thm thy (Name ("pt_"^ak_name^"_inst")); |
|
451 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
452 |
rtac ((pt_inst RS pt_set_inst) RS pt1) 1, |
|
453 |
rtac ((pt_inst RS pt_set_inst) RS pt2) 1, |
|
454 |
rtac ((pt_inst RS pt_set_inst) RS pt3) 1, |
|
455 |
atac 1]; |
|
456 |
in |
|
457 |
(AxClass.add_inst_arity_i ("set",[[qu_class]],[qu_class]) proof thy,()) |
|
458 |
end) (thy14,ak_names_types); |
|
459 |
||
460 |
(* shows that unit is an instance of pt_<ak> *) |
|
461 |
val (thy16,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
462 |
let |
|
463 |
val qu_class = Sign.full_name (sign_of thy) ("pt_"^ak_name); |
|
464 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
465 |
rtac (pt_unit_inst RS pt1) 1, |
|
466 |
rtac (pt_unit_inst RS pt2) 1, |
|
467 |
rtac (pt_unit_inst RS pt3) 1, |
|
468 |
atac 1]; |
|
469 |
in |
|
470 |
(AxClass.add_inst_arity_i ("Product_Type.unit",[],[qu_class]) proof thy,()) |
|
471 |
end) (thy15,ak_names_types); |
|
472 |
||
473 |
(* shows that *(pt_<ak>,pt_<ak>) is an instance of pt_<ak> *) |
|
474 |
(* uses the theorem pt_prod_inst and pt_<ak>_inst *) |
|
475 |
val (thy17,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
476 |
let |
|
477 |
val qu_class = Sign.full_name (sign_of thy) ("pt_"^ak_name); |
|
478 |
val pt_inst = PureThy.get_thm thy (Name ("pt_"^ak_name^"_inst")); |
|
479 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
480 |
rtac ((pt_inst RS (pt_inst RS pt_prod_inst)) RS pt1) 1, |
|
481 |
rtac ((pt_inst RS (pt_inst RS pt_prod_inst)) RS pt2) 1, |
|
482 |
rtac ((pt_inst RS (pt_inst RS pt_prod_inst)) RS pt3) 1, |
|
483 |
atac 1]; |
|
484 |
in |
|
485 |
(AxClass.add_inst_arity_i ("*",[[qu_class],[qu_class]],[qu_class]) proof thy,()) |
|
486 |
end) (thy16,ak_names_types); |
|
487 |
||
488 |
(* shows that list(pt_<ak>) is an instance of pt_<ak> *) |
|
489 |
(* uses the theorem pt_list_inst and pt_<ak>_inst *) |
|
490 |
val (thy18,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
491 |
let |
|
492 |
val qu_class = Sign.full_name (sign_of thy) ("pt_"^ak_name); |
|
493 |
val pt_inst = PureThy.get_thm thy (Name ("pt_"^ak_name^"_inst")); |
|
494 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
495 |
rtac ((pt_inst RS pt_list_inst) RS pt1) 1, |
|
496 |
rtac ((pt_inst RS pt_list_inst) RS pt2) 1, |
|
497 |
rtac ((pt_inst RS pt_list_inst) RS pt3) 1, |
|
498 |
atac 1]; |
|
499 |
in |
|
500 |
(AxClass.add_inst_arity_i ("List.list",[[qu_class]],[qu_class]) proof thy,()) |
|
501 |
end) (thy17,ak_names_types); |
|
502 |
||
503 |
(* shows that option(pt_<ak>) is an instance of pt_<ak> *) |
|
504 |
(* uses the theorem pt_option_inst and pt_<ak>_inst *) |
|
505 |
val (thy18a,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
506 |
let |
|
507 |
val qu_class = Sign.full_name (sign_of thy) ("pt_"^ak_name); |
|
508 |
val pt_inst = PureThy.get_thm thy (Name ("pt_"^ak_name^"_inst")); |
|
509 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
510 |
rtac ((pt_inst RS pt_optn_inst) RS pt1) 1, |
|
511 |
rtac ((pt_inst RS pt_optn_inst) RS pt2) 1, |
|
512 |
rtac ((pt_inst RS pt_optn_inst) RS pt3) 1, |
|
513 |
atac 1]; |
|
514 |
in |
|
515 |
(AxClass.add_inst_arity_i ("Datatype.option",[[qu_class]],[qu_class]) proof thy,()) |
|
516 |
end) (thy18,ak_names_types); |
|
517 |
||
518 |
(* shows that nOption(pt_<ak>) is an instance of pt_<ak> *) |
|
519 |
(* uses the theorem pt_option_inst and pt_<ak>_inst *) |
|
520 |
val (thy18b,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
521 |
let |
|
522 |
val qu_class = Sign.full_name (sign_of thy) ("pt_"^ak_name); |
|
523 |
val pt_inst = PureThy.get_thm thy (Name ("pt_"^ak_name^"_inst")); |
|
524 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
525 |
rtac ((pt_inst RS pt_noptn_inst) RS pt1) 1, |
|
526 |
rtac ((pt_inst RS pt_noptn_inst) RS pt2) 1, |
|
527 |
rtac ((pt_inst RS pt_noptn_inst) RS pt3) 1, |
|
528 |
atac 1]; |
|
529 |
in |
|
530 |
(AxClass.add_inst_arity_i ("nominal.nOption",[[qu_class]],[qu_class]) proof thy,()) |
|
531 |
end) (thy18a,ak_names_types); |
|
532 |
||
533 |
||
534 |
(* shows that fun(pt_<ak>,pt_<ak>) is an instance of pt_<ak> *) |
|
535 |
(* uses the theorem pt_list_inst and pt_<ak>_inst *) |
|
536 |
val (thy19,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
537 |
let |
|
538 |
val qu_class = Sign.full_name (sign_of thy) ("pt_"^ak_name); |
|
539 |
val at_thm = PureThy.get_thm thy (Name ("at_"^ak_name^"_inst")); |
|
540 |
val pt_inst = PureThy.get_thm thy (Name ("pt_"^ak_name^"_inst")); |
|
541 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
542 |
rtac ((at_thm RS (pt_inst RS (pt_inst RS pt_fun_inst))) RS pt1) 1, |
|
543 |
rtac ((at_thm RS (pt_inst RS (pt_inst RS pt_fun_inst))) RS pt2) 1, |
|
544 |
rtac ((at_thm RS (pt_inst RS (pt_inst RS pt_fun_inst))) RS pt3) 1, |
|
545 |
atac 1]; |
|
546 |
in |
|
547 |
(AxClass.add_inst_arity_i ("fun",[[qu_class],[qu_class]],[qu_class]) proof thy,()) |
|
548 |
end) (thy18b,ak_names_types); |
|
549 |
||
550 |
(*<<<<<<< fs_<ak> class instances >>>>>>>*) |
|
551 |
(*=========================================*) |
|
552 |
val fs1 = PureThy.get_thm thy19 (Name "fs1"); |
|
553 |
val fs_at_inst = PureThy.get_thm thy19 (Name "fs_at_inst"); |
|
554 |
val fs_unit_inst = PureThy.get_thm thy19 (Name "fs_unit_inst"); |
|
555 |
val fs_bool_inst = PureThy.get_thm thy19 (Name "fs_bool_inst"); |
|
556 |
val fs_prod_inst = PureThy.get_thm thy19 (Name "fs_prod_inst"); |
|
557 |
val fs_list_inst = PureThy.get_thm thy19 (Name "fs_list_inst"); |
|
558 |
||
559 |
(* shows that <ak> is an instance of fs_<ak> *) |
|
560 |
(* uses the theorem at_<ak>_inst *) |
|
561 |
val (thy20,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
562 |
let |
|
563 |
val qu_name = Sign.full_name (sign_of thy) ak_name; |
|
564 |
val qu_class = Sign.full_name (sign_of thy) ("fs_"^ak_name); |
|
565 |
val at_thm = PureThy.get_thm thy (Name ("at_"^ak_name^"_inst")); |
|
566 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
567 |
rtac ((at_thm RS fs_at_inst) RS fs1) 1]; |
|
568 |
in |
|
569 |
(AxClass.add_inst_arity_i (qu_name,[],[qu_class]) proof thy,()) |
|
570 |
end) (thy19,ak_names_types); |
|
571 |
||
572 |
(* shows that unit is an instance of fs_<ak> *) |
|
573 |
(* uses the theorem fs_unit_inst *) |
|
574 |
val (thy21,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
575 |
let |
|
576 |
val qu_class = Sign.full_name (sign_of thy) ("fs_"^ak_name); |
|
577 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
578 |
rtac (fs_unit_inst RS fs1) 1]; |
|
579 |
in |
|
580 |
(AxClass.add_inst_arity_i ("Product_Type.unit",[],[qu_class]) proof thy,()) |
|
581 |
end) (thy20,ak_names_types); |
|
582 |
||
583 |
(* shows that bool is an instance of fs_<ak> *) |
|
584 |
(* uses the theorem fs_bool_inst *) |
|
585 |
val (thy22,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
586 |
let |
|
587 |
val qu_class = Sign.full_name (sign_of thy) ("fs_"^ak_name); |
|
588 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
589 |
rtac (fs_bool_inst RS fs1) 1]; |
|
590 |
in |
|
591 |
(AxClass.add_inst_arity_i ("bool",[],[qu_class]) proof thy,()) |
|
592 |
end) (thy21,ak_names_types); |
|
593 |
||
594 |
(* shows that *(fs_<ak>,fs_<ak>) is an instance of fs_<ak> *) |
|
595 |
(* uses the theorem fs_prod_inst *) |
|
596 |
val (thy23,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
597 |
let |
|
598 |
val qu_class = Sign.full_name (sign_of thy) ("fs_"^ak_name); |
|
599 |
val fs_inst = PureThy.get_thm thy (Name ("fs_"^ak_name^"_inst")); |
|
600 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
601 |
rtac ((fs_inst RS (fs_inst RS fs_prod_inst)) RS fs1) 1]; |
|
602 |
in |
|
603 |
(AxClass.add_inst_arity_i ("*",[[qu_class],[qu_class]],[qu_class]) proof thy,()) |
|
604 |
end) (thy22,ak_names_types); |
|
605 |
||
606 |
(* shows that list(fs_<ak>) is an instance of fs_<ak> *) |
|
607 |
(* uses the theorem fs_list_inst *) |
|
608 |
val (thy24,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
609 |
let |
|
610 |
val qu_class = Sign.full_name (sign_of thy) ("fs_"^ak_name); |
|
611 |
val fs_inst = PureThy.get_thm thy (Name ("fs_"^ak_name^"_inst")); |
|
612 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
613 |
rtac ((fs_inst RS fs_list_inst) RS fs1) 1]; |
|
614 |
in |
|
615 |
(AxClass.add_inst_arity_i ("List.list",[[qu_class]],[qu_class]) proof thy,()) |
|
616 |
end) (thy23,ak_names_types); |
|
617 |
||
618 |
(*<<<<<<< cp_<ak>_<ai> class instances >>>>>>>*) |
|
619 |
(*==============================================*) |
|
620 |
val cp1 = PureThy.get_thm thy24 (Name "cp1"); |
|
621 |
val cp_unit_inst = PureThy.get_thm thy24 (Name "cp_unit_inst"); |
|
622 |
val cp_bool_inst = PureThy.get_thm thy24 (Name "cp_bool_inst"); |
|
623 |
val cp_prod_inst = PureThy.get_thm thy24 (Name "cp_prod_inst"); |
|
624 |
val cp_list_inst = PureThy.get_thm thy24 (Name "cp_list_inst"); |
|
625 |
val cp_fun_inst = PureThy.get_thm thy24 (Name "cp_fun_inst"); |
|
626 |
val cp_option_inst = PureThy.get_thm thy24 (Name "cp_option_inst"); |
|
627 |
val cp_noption_inst = PureThy.get_thm thy24 (Name "cp_noption_inst"); |
|
628 |
val pt_perm_compose = PureThy.get_thm thy24 (Name "pt_perm_compose"); |
|
629 |
val dj_pp_forget = PureThy.get_thm thy24 (Name "dj_perm_perm_forget"); |
|
630 |
||
631 |
(* shows that <aj> is an instance of cp_<ak>_<ai> *) |
|
632 |
(* that needs a three-nested loop *) |
|
633 |
val (thy25,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
634 |
foldl_map (fn (thy', (ak_name', T')) => |
|
635 |
foldl_map (fn (thy'', (ak_name'', T'')) => |
|
636 |
let |
|
637 |
val qu_name = Sign.full_name (sign_of thy'') ak_name; |
|
638 |
val qu_class = Sign.full_name (sign_of thy'') ("cp_"^ak_name'^"_"^ak_name''); |
|
639 |
val proof = |
|
640 |
(if (ak_name'=ak_name'') then |
|
641 |
(let |
|
642 |
val pt_inst = PureThy.get_thm thy'' (Name ("pt_"^ak_name''^"_inst")); |
|
643 |
val at_inst = PureThy.get_thm thy'' (Name ("at_"^ak_name''^"_inst")); |
|
644 |
in |
|
645 |
EVERY [AxClass.intro_classes_tac [], |
|
646 |
rtac (at_inst RS (pt_inst RS pt_perm_compose)) 1] |
|
647 |
end) |
|
648 |
else |
|
649 |
(let |
|
650 |
val dj_inst = PureThy.get_thm thy'' (Name ("dj_"^ak_name''^"_"^ak_name')); |
|
651 |
val simp_s = HOL_basic_ss addsimps |
|
652 |
((dj_inst RS dj_pp_forget):: |
|
653 |
(PureThy.get_thmss thy'' |
|
654 |
[Name (ak_name' ^"_prm_"^ak_name^"_def"), |
|
655 |
Name (ak_name''^"_prm_"^ak_name^"_def")])); |
|
656 |
in |
|
657 |
EVERY [AxClass.intro_classes_tac [], simp_tac simp_s 1] |
|
658 |
end)) |
|
659 |
in |
|
660 |
(AxClass.add_inst_arity_i (qu_name,[],[qu_class]) proof thy'',()) |
|
661 |
end) |
|
662 |
(thy', ak_names_types)) (thy, ak_names_types)) (thy24, ak_names_types); |
|
663 |
||
664 |
(* shows that unit is an instance of cp_<ak>_<ai> *) |
|
665 |
(* for every <ak>-combination *) |
|
666 |
val (thy26,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
667 |
foldl_map (fn (thy', (ak_name', T')) => |
|
668 |
let |
|
669 |
val qu_class = Sign.full_name (sign_of thy') ("cp_"^ak_name^"_"^ak_name'); |
|
670 |
val proof = EVERY [AxClass.intro_classes_tac [],rtac (cp_unit_inst RS cp1) 1]; |
|
671 |
in |
|
672 |
(AxClass.add_inst_arity_i ("Product_Type.unit",[],[qu_class]) proof thy',()) |
|
673 |
end) |
|
674 |
(thy, ak_names_types)) (thy25, ak_names_types); |
|
675 |
||
676 |
(* shows that bool is an instance of cp_<ak>_<ai> *) |
|
677 |
(* for every <ak>-combination *) |
|
678 |
val (thy27,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
679 |
foldl_map (fn (thy', (ak_name', T')) => |
|
680 |
let |
|
681 |
val qu_class = Sign.full_name (sign_of thy') ("cp_"^ak_name^"_"^ak_name'); |
|
682 |
val proof = EVERY [AxClass.intro_classes_tac [], rtac (cp_bool_inst RS cp1) 1]; |
|
683 |
in |
|
684 |
(AxClass.add_inst_arity_i ("bool",[],[qu_class]) proof thy',()) |
|
685 |
end) |
|
686 |
(thy, ak_names_types)) (thy26, ak_names_types); |
|
687 |
||
688 |
(* shows that prod is an instance of cp_<ak>_<ai> *) |
|
689 |
(* for every <ak>-combination *) |
|
690 |
val (thy28,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
691 |
foldl_map (fn (thy', (ak_name', T')) => |
|
692 |
let |
|
693 |
val qu_class = Sign.full_name (sign_of thy') ("cp_"^ak_name^"_"^ak_name'); |
|
694 |
val cp_inst = PureThy.get_thm thy' (Name ("cp_"^ak_name^"_"^ak_name'^"_inst")); |
|
695 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
696 |
rtac ((cp_inst RS (cp_inst RS cp_prod_inst)) RS cp1) 1]; |
|
697 |
in |
|
698 |
(AxClass.add_inst_arity_i ("*",[[qu_class],[qu_class]],[qu_class]) proof thy',()) |
|
699 |
end) |
|
700 |
(thy, ak_names_types)) (thy27, ak_names_types); |
|
701 |
||
702 |
(* shows that list is an instance of cp_<ak>_<ai> *) |
|
703 |
(* for every <ak>-combination *) |
|
704 |
val (thy29,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
705 |
foldl_map (fn (thy', (ak_name', T')) => |
|
706 |
let |
|
707 |
val qu_class = Sign.full_name (sign_of thy') ("cp_"^ak_name^"_"^ak_name'); |
|
708 |
val cp_inst = PureThy.get_thm thy' (Name ("cp_"^ak_name^"_"^ak_name'^"_inst")); |
|
709 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
710 |
rtac ((cp_inst RS cp_list_inst) RS cp1) 1]; |
|
711 |
in |
|
712 |
(AxClass.add_inst_arity_i ("List.list",[[qu_class]],[qu_class]) proof thy',()) |
|
713 |
end) |
|
714 |
(thy, ak_names_types)) (thy28, ak_names_types); |
|
715 |
||
716 |
(* shows that function is an instance of cp_<ak>_<ai> *) |
|
717 |
(* for every <ak>-combination *) |
|
718 |
val (thy30,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
719 |
foldl_map (fn (thy', (ak_name', T')) => |
|
720 |
let |
|
721 |
val qu_class = Sign.full_name (sign_of thy') ("cp_"^ak_name^"_"^ak_name'); |
|
722 |
val cp_inst = PureThy.get_thm thy' (Name ("cp_"^ak_name^"_"^ak_name'^"_inst")); |
|
723 |
val pt_inst = PureThy.get_thm thy' (Name ("pt_"^ak_name^"_inst")); |
|
724 |
val at_inst = PureThy.get_thm thy' (Name ("at_"^ak_name^"_inst")); |
|
725 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
726 |
rtac ((at_inst RS (pt_inst RS (cp_inst RS (cp_inst RS cp_fun_inst)))) RS cp1) 1]; |
|
727 |
in |
|
728 |
(AxClass.add_inst_arity_i ("fun",[[qu_class],[qu_class]],[qu_class]) proof thy',()) |
|
729 |
end) |
|
730 |
(thy, ak_names_types)) (thy29, ak_names_types); |
|
731 |
||
732 |
(* shows that option is an instance of cp_<ak>_<ai> *) |
|
733 |
(* for every <ak>-combination *) |
|
734 |
val (thy31,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
735 |
foldl_map (fn (thy', (ak_name', T')) => |
|
736 |
let |
|
737 |
val qu_class = Sign.full_name (sign_of thy') ("cp_"^ak_name^"_"^ak_name'); |
|
738 |
val cp_inst = PureThy.get_thm thy' (Name ("cp_"^ak_name^"_"^ak_name'^"_inst")); |
|
739 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
740 |
rtac ((cp_inst RS cp_option_inst) RS cp1) 1]; |
|
741 |
in |
|
742 |
(AxClass.add_inst_arity_i ("Datatype.option",[[qu_class]],[qu_class]) proof thy',()) |
|
743 |
end) |
|
744 |
(thy, ak_names_types)) (thy30, ak_names_types); |
|
745 |
||
746 |
(* shows that nOption is an instance of cp_<ak>_<ai> *) |
|
747 |
(* for every <ak>-combination *) |
|
748 |
val (thy32,_) = foldl_map (fn (thy, (ak_name, T)) => |
|
749 |
foldl_map (fn (thy', (ak_name', T')) => |
|
750 |
let |
|
751 |
val qu_class = Sign.full_name (sign_of thy') ("cp_"^ak_name^"_"^ak_name'); |
|
752 |
val cp_inst = PureThy.get_thm thy' (Name ("cp_"^ak_name^"_"^ak_name'^"_inst")); |
|
753 |
val proof = EVERY [AxClass.intro_classes_tac [], |
|
754 |
rtac ((cp_inst RS cp_noption_inst) RS cp1) 1]; |
|
755 |
in |
|
756 |
(AxClass.add_inst_arity_i ("nominal.nOption",[[qu_class]],[qu_class]) proof thy',()) |
|
757 |
end) |
|
758 |
(thy, ak_names_types)) (thy31, ak_names_types); |
|
759 |
||
760 |
(* abbreviations for some collection of rules *) |
|
761 |
(*============================================*) |
|
762 |
val abs_fun_pi = PureThy.get_thm thy32 (Name ("nominal.abs_fun_pi")); |
|
763 |
val abs_fun_pi_ineq = PureThy.get_thm thy32 (Name ("nominal.abs_fun_pi_ineq")); |
|
764 |
val abs_fun_eq = PureThy.get_thm thy32 (Name ("nominal.abs_fun_eq")); |
|
765 |
val dj_perm_forget = PureThy.get_thm thy32 (Name ("nominal.dj_perm_forget")); |
|
766 |
val dj_pp_forget = PureThy.get_thm thy32 (Name ("nominal.dj_perm_perm_forget")); |
|
767 |
val fresh_iff = PureThy.get_thm thy32 (Name ("nominal.fresh_abs_fun_iff")); |
|
768 |
val fresh_iff_ineq = PureThy.get_thm thy32 (Name ("nominal.fresh_abs_fun_iff_ineq")); |
|
769 |
val abs_fun_supp = PureThy.get_thm thy32 (Name ("nominal.abs_fun_supp")); |
|
770 |
val abs_fun_supp_ineq = PureThy.get_thm thy32 (Name ("nominal.abs_fun_supp_ineq")); |
|
771 |
val pt_swap_bij = PureThy.get_thm thy32 (Name ("nominal.pt_swap_bij")); |
|
772 |
val pt_fresh_fresh = PureThy.get_thm thy32 (Name ("nominal.pt_fresh_fresh")); |
|
773 |
val pt_bij = PureThy.get_thm thy32 (Name ("nominal.pt_bij")); |
|
774 |
val pt_perm_compose = PureThy.get_thm thy32 (Name ("nominal.pt_perm_compose")); |
|
775 |
val perm_eq_app = PureThy.get_thm thy32 (Name ("nominal.perm_eq_app")); |
|
18054 | 776 |
val at_fresh = PureThy.get_thm thy32 (Name ("nominal.at_fresh")); |
18067 | 777 |
val at_calc = PureThy.get_thms thy32 (Name ("nominal.at_calc")); |
18054 | 778 |
val at_supp = PureThy.get_thm thy32 (Name ("nominal.at_supp")); |
18067 | 779 |
val dj_supp = PureThy.get_thm thy32 (Name ("nominal.dj_supp")); |
17870 | 780 |
|
781 |
(* abs_perm collects all lemmas for simplifying a permutation *) |
|
782 |
(* in front of an abs_fun *) |
|
783 |
val (thy33,_) = |
|
784 |
let |
|
785 |
val name = "abs_perm" |
|
786 |
val thm_list = Library.flat (map (fn (ak_name, T) => |
|
787 |
let |
|
788 |
val at_inst = PureThy.get_thm thy32 (Name ("at_"^ak_name^"_inst")); |
|
789 |
val pt_inst = PureThy.get_thm thy32 (Name ("pt_"^ak_name^"_inst")); |
|
790 |
val thm = [pt_inst, at_inst] MRS abs_fun_pi |
|
791 |
val thm_list = map (fn (ak_name', T') => |
|
792 |
let |
|
793 |
val cp_inst = PureThy.get_thm thy32 (Name ("cp_"^ak_name^"_"^ak_name'^"_inst")); |
|
794 |
in |
|
795 |
[pt_inst, pt_inst, at_inst, cp_inst] MRS abs_fun_pi_ineq |
|
796 |
end) ak_names_types; |
|
797 |
in thm::thm_list end) (ak_names_types)) |
|
798 |
in |
|
799 |
(PureThy.add_thmss [((name, thm_list),[])] thy32) |
|
800 |
end; |
|
801 |
||
18054 | 802 |
val (thy34,_) = |
18067 | 803 |
let |
804 |
(* takes a theorem and a list of theorems *) |
|
805 |
(* produces a list of theorems of the form *) |
|
806 |
(* [t1 RS thm,..,tn RS thm] where t1..tn in thms *) |
|
807 |
fun instantiate thm thms = map (fn ti => ti RS thm) thms; |
|
808 |
||
809 |
(* takes two theorem lists (hopefully of the same length) *) |
|
810 |
(* produces a list of theorems of the form *) |
|
811 |
(* [t1 RS m1,..,tn RS mn] where t1..tn in thms1 and m1..mn in thms2 *) |
|
812 |
fun instantiate_zip thms1 thms2 = |
|
813 |
map (fn (t1,t2) => t1 RS t2) (thms1 ~~ thms2); |
|
814 |
||
815 |
(* list of all at_inst-theorems *) |
|
816 |
val ats = map (fn ak => PureThy.get_thm thy33 (Name ("at_"^ak^"_inst"))) ak_names |
|
817 |
(* list of all pt_inst-theorems *) |
|
818 |
val pts = map (fn ak => PureThy.get_thm thy33 (Name ("pt_"^ak^"_inst"))) ak_names |
|
819 |
(* list of all cp_inst-theorems *) |
|
820 |
val cps = |
|
821 |
let fun cps_fun (ak1,ak2) = PureThy.get_thm thy33 (Name ("cp_"^ak1^"_"^ak2^"_inst")) |
|
822 |
in map cps_fun (cprod (ak_names,ak_names)) end; |
|
823 |
(* list of all dj_inst-theorems *) |
|
824 |
val djs = |
|
825 |
let fun djs_fun (ak1,ak2) = |
|
826 |
if ak1=ak2 |
|
827 |
then NONE |
|
828 |
else SOME(PureThy.get_thm thy33 (Name ("dj_"^ak1^"_"^ak2))) |
|
829 |
in List.mapPartial I (map djs_fun (cprod (ak_names,ak_names))) end; |
|
830 |
||
831 |
fun inst_pt thms = Library.flat (map (fn ti => instantiate ti pts) thms); |
|
832 |
fun inst_at thms = Library.flat (map (fn ti => instantiate ti ats) thms); |
|
833 |
fun inst_pt_at thms = instantiate_zip ats (inst_pt thms); |
|
834 |
fun inst_dj thms = Library.flat (map (fn ti => instantiate ti djs) thms); |
|
17870 | 835 |
|
836 |
in |
|
837 |
thy33 |
|
18067 | 838 |
|> PureThy.add_thmss [(("alpha", inst_pt_at [abs_fun_eq]),[])] |
839 |
|>>> PureThy.add_thmss [(("perm_swap", inst_pt_at [pt_swap_bij]),[])] |
|
840 |
|>>> PureThy.add_thmss [(("perm_fresh_fresh", inst_pt_at [pt_fresh_fresh]),[])] |
|
841 |
|>>> PureThy.add_thmss [(("perm_bij", inst_pt_at [pt_bij]),[])] |
|
842 |
|>>> PureThy.add_thmss [(("perm_compose", inst_pt_at [pt_perm_compose]),[])] |
|
843 |
|>>> PureThy.add_thmss [(("perm_app_eq", inst_pt_at [perm_eq_app]),[])] |
|
844 |
|>>> PureThy.add_thmss [(("supp_atm", (inst_at [at_supp]) @ (inst_dj [dj_supp])),[])] |
|
845 |
|>>> PureThy.add_thmss [(("fresh_atm", inst_at [at_fresh]),[])] |
|
846 |
|>>> PureThy.add_thmss [(("calc_atm", inst_at at_calc),[])] |
|
847 |
||
17870 | 848 |
end; |
849 |
||
850 |
(* perm_dj collects all lemmas that forget an permutation *) |
|
851 |
(* when it acts on an atom of different type *) |
|
852 |
val (thy35,_) = |
|
853 |
let |
|
854 |
val name = "perm_dj" |
|
855 |
val thm_list = Library.flat (map (fn (ak_name, T) => |
|
856 |
Library.flat (map (fn (ak_name', T') => |
|
857 |
if not (ak_name = ak_name') |
|
858 |
then |
|
859 |
let |
|
860 |
val dj_inst = PureThy.get_thm thy34 (Name ("dj_"^ak_name^"_"^ak_name')); |
|
861 |
in |
|
862 |
[dj_inst RS dj_perm_forget, dj_inst RS dj_pp_forget] |
|
863 |
end |
|
864 |
else []) ak_names_types)) ak_names_types) |
|
865 |
in |
|
866 |
(PureThy.add_thmss [((name, thm_list),[])] thy34) |
|
867 |
end; |
|
868 |
||
869 |
(* abs_fresh collects all lemmas for simplifying a freshness *) |
|
870 |
(* proposition involving an abs_fun *) |
|
871 |
||
872 |
val (thy36,_) = |
|
873 |
let |
|
874 |
val name = "abs_fresh" |
|
875 |
val thm_list = Library.flat (map (fn (ak_name, T) => |
|
876 |
let |
|
877 |
val at_inst = PureThy.get_thm thy35 (Name ("at_"^ak_name^"_inst")); |
|
878 |
val pt_inst = PureThy.get_thm thy35 (Name ("pt_"^ak_name^"_inst")); |
|
879 |
val fs_inst = PureThy.get_thm thy35 (Name ("fs_"^ak_name^"_inst")); |
|
880 |
val thm = [pt_inst, at_inst, (fs_inst RS fs1)] MRS fresh_iff |
|
881 |
val thm_list = Library.flat (map (fn (ak_name', T') => |
|
882 |
(if (not (ak_name = ak_name')) |
|
883 |
then |
|
884 |
let |
|
885 |
val cp_inst = PureThy.get_thm thy35 (Name ("cp_"^ak_name^"_"^ak_name'^"_inst")); |
|
886 |
val dj_inst = PureThy.get_thm thy35 (Name ("dj_"^ak_name'^"_"^ak_name)); |
|
887 |
in |
|
888 |
[[pt_inst, pt_inst, at_inst, cp_inst, dj_inst] MRS fresh_iff_ineq] |
|
889 |
end |
|
890 |
else [])) ak_names_types); |
|
891 |
in thm::thm_list end) (ak_names_types)) |
|
892 |
in |
|
893 |
(PureThy.add_thmss [((name, thm_list),[])] thy35) |
|
894 |
end; |
|
895 |
||
896 |
(* abs_supp collects all lemmas for simplifying *) |
|
897 |
(* support proposition involving an abs_fun *) |
|
898 |
||
899 |
val (thy37,_) = |
|
900 |
let |
|
901 |
val name = "abs_supp" |
|
902 |
val thm_list = Library.flat (map (fn (ak_name, T) => |
|
903 |
let |
|
904 |
val at_inst = PureThy.get_thm thy36 (Name ("at_"^ak_name^"_inst")); |
|
905 |
val pt_inst = PureThy.get_thm thy36 (Name ("pt_"^ak_name^"_inst")); |
|
906 |
val fs_inst = PureThy.get_thm thy36 (Name ("fs_"^ak_name^"_inst")); |
|
907 |
val thm1 = [pt_inst, at_inst, (fs_inst RS fs1)] MRS abs_fun_supp |
|
908 |
val thm2 = [pt_inst, at_inst] MRS abs_fun_supp |
|
909 |
val thm_list = Library.flat (map (fn (ak_name', T') => |
|
910 |
(if (not (ak_name = ak_name')) |
|
911 |
then |
|
912 |
let |
|
913 |
val cp_inst = PureThy.get_thm thy36 (Name ("cp_"^ak_name^"_"^ak_name'^"_inst")); |
|
914 |
val dj_inst = PureThy.get_thm thy36 (Name ("dj_"^ak_name'^"_"^ak_name)); |
|
915 |
in |
|
916 |
[[pt_inst, pt_inst, at_inst, cp_inst, dj_inst] MRS abs_fun_supp_ineq] |
|
917 |
end |
|
918 |
else [])) ak_names_types); |
|
919 |
in thm1::thm2::thm_list end) (ak_names_types)) |
|
920 |
in |
|
921 |
(PureThy.add_thmss [((name, thm_list),[])] thy36) |
|
922 |
end; |
|
923 |
||
924 |
in NominalData.put (fold Symtab.update (map (rpair ()) full_ak_names) |
|
925 |
(NominalData.get thy11)) thy37 |
|
926 |
end; |
|
927 |
||
928 |
||
929 |
(* syntax und parsing *) |
|
930 |
structure P = OuterParse and K = OuterKeyword; |
|
931 |
||
932 |
val atom_declP = |
|
933 |
OuterSyntax.command "atom_decl" "Declare new kinds of atoms" K.thy_decl |
|
934 |
(Scan.repeat1 P.name >> (Toplevel.theory o create_nom_typedecls)); |
|
935 |
||
936 |
val _ = OuterSyntax.add_parsers [atom_declP]; |
|
937 |
||
938 |
val setup = [NominalData.init]; |
|
939 |
||
940 |
(*=======================================================================*) |
|
941 |
||
18016 | 942 |
(** FIXME: DatatypePackage should export this function **) |
943 |
||
944 |
local |
|
945 |
||
946 |
fun dt_recs (DtTFree _) = [] |
|
947 |
| dt_recs (DtType (_, dts)) = List.concat (map dt_recs dts) |
|
948 |
| dt_recs (DtRec i) = [i]; |
|
949 |
||
950 |
fun dt_cases (descr: descr) (_, args, constrs) = |
|
951 |
let |
|
952 |
fun the_bname i = Sign.base_name (#1 (valOf (AList.lookup (op =) descr i))); |
|
953 |
val bnames = map the_bname (distinct (List.concat (map dt_recs args))); |
|
954 |
in map (fn (c, _) => space_implode "_" (Sign.base_name c :: bnames)) constrs end; |
|
955 |
||
956 |
||
957 |
fun induct_cases descr = |
|
958 |
DatatypeProp.indexify_names (List.concat (map (dt_cases descr) (map #2 descr))); |
|
959 |
||
960 |
fun exhaust_cases descr i = dt_cases descr (valOf (AList.lookup (op =) descr i)); |
|
961 |
||
962 |
in |
|
963 |
||
964 |
fun mk_case_names_induct descr = RuleCases.case_names (induct_cases descr); |
|
965 |
||
966 |
fun mk_case_names_exhausts descr new = |
|
967 |
map (RuleCases.case_names o exhaust_cases descr o #1) |
|
968 |
(List.filter (fn ((_, (name, _, _))) => name mem_string new) descr); |
|
969 |
||
970 |
end; |
|
971 |
||
972 |
(*******************************) |
|
973 |
||
17870 | 974 |
val (_ $ (_ $ (_ $ (distinct_f $ _) $ _))) = hd (prems_of distinct_lemma); |
975 |
||
976 |
fun read_typ sign ((Ts, sorts), str) = |
|
977 |
let |
|
978 |
val T = Type.no_tvars (Sign.read_typ (sign, (AList.lookup op =) |
|
979 |
(map (apfst (rpair ~1)) sorts)) str) handle TYPE (msg, _, _) => error msg |
|
980 |
in (Ts @ [T], add_typ_tfrees (T, sorts)) end; |
|
981 |
||
982 |
(** taken from HOL/Tools/datatype_aux.ML **) |
|
983 |
||
984 |
fun indtac indrule indnames i st = |
|
985 |
let |
|
986 |
val ts = HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of indrule)); |
|
987 |
val ts' = HOLogic.dest_conj (HOLogic.dest_Trueprop |
|
988 |
(Logic.strip_imp_concl (List.nth (prems_of st, i - 1)))); |
|
989 |
val getP = if can HOLogic.dest_imp (hd ts) then |
|
990 |
(apfst SOME) o HOLogic.dest_imp else pair NONE; |
|
991 |
fun abstr (t1, t2) = (case t1 of |
|
992 |
NONE => (case filter (fn Free (s, _) => s mem indnames | _ => false) |
|
993 |
(term_frees t2) of |
|
994 |
[Free (s, T)] => absfree (s, T, t2) |
|
995 |
| _ => sys_error "indtac") |
|
996 |
| SOME (_ $ t' $ _) => Abs ("x", fastype_of t', abstract_over (t', t2))) |
|
997 |
val cert = cterm_of (Thm.sign_of_thm st); |
|
998 |
val Ps = map (cert o head_of o snd o getP) ts; |
|
999 |
val indrule' = cterm_instantiate (Ps ~~ |
|
1000 |
(map (cert o abstr o getP) ts')) indrule |
|
1001 |
in |
|
1002 |
rtac indrule' i st |
|
1003 |
end; |
|
1004 |
||
1005 |
fun gen_add_nominal_datatype prep_typ err flat_names new_type_names dts thy = |
|
1006 |
let |
|
1007 |
(* this theory is used just for parsing *) |
|
1008 |
||
1009 |
val tmp_thy = thy |> |
|
1010 |
Theory.copy |> |
|
1011 |
Theory.add_types (map (fn (tvs, tname, mx, _) => |
|
1012 |
(tname, length tvs, mx)) dts); |
|
1013 |
||
1014 |
val sign = Theory.sign_of tmp_thy; |
|
1015 |
||
1016 |
val atoms = atoms_of thy; |
|
1017 |
val classes = map (NameSpace.map_base (fn s => "pt_" ^ s)) atoms; |
|
1018 |
val cp_classes = List.concat (map (fn atom1 => map (fn atom2 => |
|
1019 |
Sign.intern_class thy ("cp_" ^ Sign.base_name atom1 ^ "_" ^ |
|
1020 |
Sign.base_name atom2)) atoms) atoms); |
|
1021 |
fun augment_sort S = S union classes; |
|
1022 |
val augment_sort_typ = map_type_tfree (fn (s, S) => TFree (s, augment_sort S)); |
|
1023 |
||
1024 |
fun prep_constr ((constrs, sorts), (cname, cargs, mx)) = |
|
1025 |
let val (cargs', sorts') = Library.foldl (prep_typ sign) (([], sorts), cargs) |
|
1026 |
in (constrs @ [(cname, cargs', mx)], sorts') end |
|
1027 |
||
1028 |
fun prep_dt_spec ((dts, sorts), (tvs, tname, mx, constrs)) = |
|
1029 |
let val (constrs', sorts') = Library.foldl prep_constr (([], sorts), constrs) |
|
1030 |
in (dts @ [(tvs, tname, mx, constrs')], sorts') end |
|
1031 |
||
1032 |
val (dts', sorts) = Library.foldl prep_dt_spec (([], []), dts); |
|
1033 |
val sorts' = map (apsnd augment_sort) sorts; |
|
1034 |
val tyvars = map #1 dts'; |
|
1035 |
||
1036 |
val types_syntax = map (fn (tvs, tname, mx, constrs) => (tname, mx)) dts'; |
|
1037 |
val constr_syntax = map (fn (tvs, tname, mx, constrs) => |
|
1038 |
map (fn (cname, cargs, mx) => (cname, mx)) constrs) dts'; |
|
1039 |
||
1040 |
val ps = map (fn (_, n, _, _) => |
|
1041 |
(Sign.full_name sign n, Sign.full_name sign (n ^ "_Rep"))) dts; |
|
1042 |
val rps = map Library.swap ps; |
|
1043 |
||
1044 |
fun replace_types (Type ("nominal.ABS", [T, U])) = |
|
1045 |
Type ("fun", [T, Type ("nominal.nOption", [replace_types U])]) |
|
1046 |
| replace_types (Type (s, Ts)) = |
|
1047 |
Type (getOpt (AList.lookup op = ps s, s), map replace_types Ts) |
|
1048 |
| replace_types T = T; |
|
1049 |
||
1050 |
fun replace_types' (Type (s, Ts)) = |
|
1051 |
Type (getOpt (AList.lookup op = rps s, s), map replace_types' Ts) |
|
1052 |
| replace_types' T = T; |
|
1053 |
||
1054 |
val dts'' = map (fn (tvs, tname, mx, constrs) => (tvs, tname ^ "_Rep", NoSyn, |
|
1055 |
map (fn (cname, cargs, mx) => (cname, |
|
1056 |
map (augment_sort_typ o replace_types) cargs, NoSyn)) constrs)) dts'; |
|
1057 |
||
1058 |
val new_type_names' = map (fn n => n ^ "_Rep") new_type_names; |
|
1059 |
val full_new_type_names' = map (Sign.full_name (sign_of thy)) new_type_names'; |
|
1060 |
||
18045 | 1061 |
val ({induction, ...},thy1) = |
17870 | 1062 |
DatatypePackage.add_datatype_i err flat_names new_type_names' dts'' thy; |
1063 |
||
1064 |
val SOME {descr, ...} = Symtab.lookup |
|
1065 |
(DatatypePackage.get_datatypes thy1) (hd full_new_type_names'); |
|
1066 |
val typ_of_dtyp = typ_of_dtyp descr sorts'; |
|
1067 |
fun nth_dtyp i = typ_of_dtyp (DtRec i); |
|
1068 |
||
1069 |
(**** define permutation functions ****) |
|
1070 |
||
1071 |
val permT = mk_permT (TFree ("'x", HOLogic.typeS)); |
|
1072 |
val pi = Free ("pi", permT); |
|
1073 |
val perm_types = map (fn (i, _) => |
|
1074 |
let val T = nth_dtyp i |
|
1075 |
in permT --> T --> T end) descr; |
|
1076 |
val perm_names = replicate (length new_type_names) "nominal.perm" @ |
|
1077 |
DatatypeProp.indexify_names (map (fn i => Sign.full_name (sign_of thy1) |
|
1078 |
("perm_" ^ name_of_typ (nth_dtyp i))) |
|
1079 |
(length new_type_names upto length descr - 1)); |
|
1080 |
val perm_names_types = perm_names ~~ perm_types; |
|
1081 |
||
1082 |
val perm_eqs = List.concat (map (fn (i, (_, _, constrs)) => |
|
1083 |
let val T = nth_dtyp i |
|
1084 |
in map (fn (cname, dts) => |
|
1085 |
let |
|
1086 |
val Ts = map typ_of_dtyp dts; |
|
1087 |
val names = DatatypeProp.make_tnames Ts; |
|
1088 |
val args = map Free (names ~~ Ts); |
|
1089 |
val c = Const (cname, Ts ---> T); |
|
1090 |
fun perm_arg (dt, x) = |
|
1091 |
let val T = type_of x |
|
1092 |
in if is_rec_type dt then |
|
1093 |
let val (Us, _) = strip_type T |
|
1094 |
in list_abs (map (pair "x") Us, |
|
1095 |
Const (List.nth (perm_names_types, body_index dt)) $ pi $ |
|
1096 |
list_comb (x, map (fn (i, U) => |
|
1097 |
Const ("nominal.perm", permT --> U --> U) $ |
|
1098 |
(Const ("List.rev", permT --> permT) $ pi) $ |
|
1099 |
Bound i) ((length Us - 1 downto 0) ~~ Us))) |
|
1100 |
end |
|
1101 |
else Const ("nominal.perm", permT --> T --> T) $ pi $ x |
|
1102 |
end; |
|
1103 |
in |
|
1104 |
(("", HOLogic.mk_Trueprop (HOLogic.mk_eq |
|
1105 |
(Const (List.nth (perm_names_types, i)) $ |
|
1106 |
Free ("pi", mk_permT (TFree ("'x", HOLogic.typeS))) $ |
|
1107 |
list_comb (c, args), |
|
1108 |
list_comb (c, map perm_arg (dts ~~ args))))), []) |
|
1109 |
end) constrs |
|
1110 |
end) descr); |
|
1111 |
||
1112 |
val (thy2, perm_simps) = thy1 |> |
|
1113 |
Theory.add_consts_i (map (fn (s, T) => (Sign.base_name s, T, NoSyn)) |
|
1114 |
(List.drop (perm_names_types, length new_type_names))) |> |
|
1115 |
PrimrecPackage.add_primrec_i "" perm_eqs; |
|
1116 |
||
1117 |
(**** prove that permutation functions introduced by unfolding are ****) |
|
1118 |
(**** equivalent to already existing permutation functions ****) |
|
1119 |
||
1120 |
val _ = warning ("length descr: " ^ string_of_int (length descr)); |
|
1121 |
val _ = warning ("length new_type_names: " ^ string_of_int (length new_type_names)); |
|
1122 |
||
1123 |
val perm_indnames = DatatypeProp.make_tnames (map body_type perm_types); |
|
1124 |
val perm_fun_def = PureThy.get_thm thy2 (Name "perm_fun_def"); |
|
1125 |
||
1126 |
val unfolded_perm_eq_thms = |
|
1127 |
if length descr = length new_type_names then [] |
|
1128 |
else map standard (List.drop (split_conj_thm |
|
18010 | 1129 |
(Goal.prove thy2 [] [] |
17870 | 1130 |
(HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj |
1131 |
(map (fn (c as (s, T), x) => |
|
1132 |
let val [T1, T2] = binder_types T |
|
1133 |
in HOLogic.mk_eq (Const c $ pi $ Free (x, T2), |
|
1134 |
Const ("nominal.perm", T) $ pi $ Free (x, T2)) |
|
1135 |
end) |
|
18010 | 1136 |
(perm_names_types ~~ perm_indnames)))) |
1137 |
(fn _ => EVERY [indtac induction perm_indnames 1, |
|
17870 | 1138 |
ALLGOALS (asm_full_simp_tac |
1139 |
(simpset_of thy2 addsimps [perm_fun_def]))])), |
|
1140 |
length new_type_names)); |
|
1141 |
||
1142 |
(**** prove [] \<bullet> t = t ****) |
|
1143 |
||
1144 |
val _ = warning "perm_empty_thms"; |
|
1145 |
||
1146 |
val perm_empty_thms = List.concat (map (fn a => |
|
1147 |
let val permT = mk_permT (Type (a, [])) |
|
1148 |
in map standard (List.take (split_conj_thm |
|
18010 | 1149 |
(Goal.prove thy2 [] [] |
17870 | 1150 |
(HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj |
1151 |
(map (fn ((s, T), x) => HOLogic.mk_eq |
|
1152 |
(Const (s, permT --> T --> T) $ |
|
1153 |
Const ("List.list.Nil", permT) $ Free (x, T), |
|
1154 |
Free (x, T))) |
|
1155 |
(perm_names ~~ |
|
18010 | 1156 |
map body_type perm_types ~~ perm_indnames)))) |
1157 |
(fn _ => EVERY [indtac induction perm_indnames 1, |
|
17870 | 1158 |
ALLGOALS (asm_full_simp_tac (simpset_of thy2))])), |
1159 |
length new_type_names)) |
|
1160 |
end) |
|
1161 |
atoms); |
|
1162 |
||
1163 |
(**** prove (pi1 @ pi2) \<bullet> t = pi1 \<bullet> (pi2 \<bullet> t) ****) |
|
1164 |
||
1165 |
val _ = warning "perm_append_thms"; |
|
1166 |
||
1167 |
(*FIXME: these should be looked up statically*) |
|
1168 |
val at_pt_inst = PureThy.get_thm thy2 (Name "at_pt_inst"); |
|
1169 |
val pt2 = PureThy.get_thm thy2 (Name "pt2"); |
|
1170 |
||
1171 |
val perm_append_thms = List.concat (map (fn a => |
|
1172 |
let |
|
1173 |
val permT = mk_permT (Type (a, [])); |
|
1174 |
val pi1 = Free ("pi1", permT); |
|
1175 |
val pi2 = Free ("pi2", permT); |
|
1176 |
val pt_inst = PureThy.get_thm thy2 (Name ("pt_" ^ Sign.base_name a ^ "_inst")); |
|
1177 |
val pt2' = pt_inst RS pt2; |
|
1178 |
val pt2_ax = PureThy.get_thm thy2 |
|
1179 |
(Name (NameSpace.map_base (fn s => "pt_" ^ s ^ "2") a)); |
|
1180 |
in List.take (map standard (split_conj_thm |
|
18010 | 1181 |
(Goal.prove thy2 [] [] |
17870 | 1182 |
(HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj |
1183 |
(map (fn ((s, T), x) => |
|
1184 |
let val perm = Const (s, permT --> T --> T) |
|
1185 |
in HOLogic.mk_eq |
|
1186 |
(perm $ (Const ("List.op @", permT --> permT --> permT) $ |
|
1187 |
pi1 $ pi2) $ Free (x, T), |
|
1188 |
perm $ pi1 $ (perm $ pi2 $ Free (x, T))) |
|
1189 |
end) |
|
1190 |
(perm_names ~~ |
|
18010 | 1191 |
map body_type perm_types ~~ perm_indnames)))) |
1192 |
(fn _ => EVERY [indtac induction perm_indnames 1, |
|
17870 | 1193 |
ALLGOALS (asm_full_simp_tac (simpset_of thy2 addsimps [pt2', pt2_ax]))]))), |
1194 |
length new_type_names) |
|
1195 |
end) atoms); |
|
1196 |
||
1197 |
(**** prove pi1 ~ pi2 ==> pi1 \<bullet> t = pi2 \<bullet> t ****) |
|
1198 |
||
1199 |
val _ = warning "perm_eq_thms"; |
|
1200 |
||
1201 |
val pt3 = PureThy.get_thm thy2 (Name "pt3"); |
|
1202 |
val pt3_rev = PureThy.get_thm thy2 (Name "pt3_rev"); |
|
1203 |
||
1204 |
val perm_eq_thms = List.concat (map (fn a => |
|
1205 |
let |
|
1206 |
val permT = mk_permT (Type (a, [])); |
|
1207 |
val pi1 = Free ("pi1", permT); |
|
1208 |
val pi2 = Free ("pi2", permT); |
|
1209 |
(*FIXME: not robust - better access these theorems using NominalData?*) |
|
1210 |
val at_inst = PureThy.get_thm thy2 (Name ("at_" ^ Sign.base_name a ^ "_inst")); |
|
1211 |
val pt_inst = PureThy.get_thm thy2 (Name ("pt_" ^ Sign.base_name a ^ "_inst")); |
|
1212 |
val pt3' = pt_inst RS pt3; |
|
1213 |
val pt3_rev' = at_inst RS (pt_inst RS pt3_rev); |
|
1214 |
val pt3_ax = PureThy.get_thm thy2 |
|
1215 |
(Name (NameSpace.map_base (fn s => "pt_" ^ s ^ "3") a)); |
|
1216 |
in List.take (map standard (split_conj_thm |
|
18010 | 1217 |
(Goal.prove thy2 [] [] (Logic.mk_implies |
17870 | 1218 |
(HOLogic.mk_Trueprop (Const ("nominal.prm_eq", |
1219 |
permT --> permT --> HOLogic.boolT) $ pi1 $ pi2), |
|
1220 |
HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj |
|
1221 |
(map (fn ((s, T), x) => |
|
1222 |
let val perm = Const (s, permT --> T --> T) |
|
1223 |
in HOLogic.mk_eq |
|
1224 |
(perm $ pi1 $ Free (x, T), |
|
1225 |
perm $ pi2 $ Free (x, T)) |
|
1226 |
end) |
|
1227 |
(perm_names ~~ |
|
18010 | 1228 |
map body_type perm_types ~~ perm_indnames))))) |
1229 |
(fn _ => EVERY [indtac induction perm_indnames 1, |
|
17870 | 1230 |
ALLGOALS (asm_full_simp_tac (simpset_of thy2 addsimps [pt3', pt3_rev', pt3_ax]))]))), |
1231 |
length new_type_names) |
|
1232 |
end) atoms); |
|
1233 |
||
1234 |
(**** prove pi1 \<bullet> (pi2 \<bullet> t) = (pi1 \<bullet> pi2) \<bullet> (pi1 \<bullet> t) ****) |
|
1235 |
||
1236 |
val cp1 = PureThy.get_thm thy2 (Name "cp1"); |
|
1237 |
val dj_cp = PureThy.get_thm thy2 (Name "dj_cp"); |
|
1238 |
val pt_perm_compose = PureThy.get_thm thy2 (Name "pt_perm_compose"); |
|
1239 |
val pt_perm_compose_rev = PureThy.get_thm thy2 (Name "pt_perm_compose_rev"); |
|
1240 |
val dj_perm_perm_forget = PureThy.get_thm thy2 (Name "dj_perm_perm_forget"); |
|
1241 |
||
1242 |
fun composition_instance name1 name2 thy = |
|
1243 |
let |
|
1244 |
val name1' = Sign.base_name name1; |
|
1245 |
val name2' = Sign.base_name name2; |
|
1246 |
val cp_class = Sign.intern_class thy ("cp_" ^ name1' ^ "_" ^ name2'); |
|
1247 |
val permT1 = mk_permT (Type (name1, [])); |
|
1248 |
val permT2 = mk_permT (Type (name2, [])); |
|
1249 |
val augment = map_type_tfree |
|
1250 |
(fn (x, S) => TFree (x, cp_class :: S)); |
|
1251 |
val Ts = map (augment o body_type) perm_types; |
|
1252 |
val cp_inst = PureThy.get_thm thy |
|
1253 |
(Name ("cp_" ^ name1' ^ "_" ^ name2' ^ "_inst")); |
|
1254 |
val simps = simpset_of thy addsimps (perm_fun_def :: |
|
1255 |
(if name1 <> name2 then |
|
1256 |
let val dj = PureThy.get_thm thy (Name ("dj_" ^ name2' ^ "_" ^ name1')) |
|
1257 |
in [dj RS (cp_inst RS dj_cp), dj RS dj_perm_perm_forget] end |
|
1258 |
else |
|
1259 |
let |
|
1260 |
val at_inst = PureThy.get_thm thy (Name ("at_" ^ name1' ^ "_inst")); |
|
1261 |
val pt_inst = PureThy.get_thm thy (Name ("pt_" ^ name1' ^ "_inst")) |
|
1262 |
in |
|
1263 |
[cp_inst RS cp1 RS sym, |
|
1264 |
at_inst RS (pt_inst RS pt_perm_compose) RS sym, |
|
1265 |
at_inst RS (pt_inst RS pt_perm_compose_rev) RS sym] |
|
1266 |
end)) |
|
18010 | 1267 |
val thms = split_conj_thm (standard (Goal.prove thy [] [] |
17870 | 1268 |
(HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj |
1269 |
(map (fn ((s, T), x) => |
|
1270 |
let |
|
1271 |
val pi1 = Free ("pi1", permT1); |
|
1272 |
val pi2 = Free ("pi2", permT2); |
|
1273 |
val perm1 = Const (s, permT1 --> T --> T); |
|
1274 |
val perm2 = Const (s, permT2 --> T --> T); |
|
1275 |
val perm3 = Const ("nominal.perm", permT1 --> permT2 --> permT2) |
|
1276 |
in HOLogic.mk_eq |
|
1277 |
(perm1 $ pi1 $ (perm2 $ pi2 $ Free (x, T)), |
|
1278 |
perm2 $ (perm3 $ pi1 $ pi2) $ (perm1 $ pi1 $ Free (x, T))) |
|
1279 |
end) |
|
18010 | 1280 |
(perm_names ~~ Ts ~~ perm_indnames)))) |
1281 |
(fn _ => EVERY [indtac induction perm_indnames 1, |
|
1282 |
ALLGOALS (asm_full_simp_tac simps)]))) |
|
17870 | 1283 |
in |
1284 |
foldl (fn ((s, tvs), thy) => AxClass.add_inst_arity_i |
|
1285 |
(s, replicate (length tvs) (cp_class :: classes), [cp_class]) |
|
1286 |
(AxClass.intro_classes_tac [] THEN ALLGOALS (resolve_tac thms)) thy) |
|
1287 |
thy (full_new_type_names' ~~ tyvars) |
|
1288 |
end; |
|
1289 |
||
1290 |
val (thy3, perm_thmss) = thy2 |> |
|
1291 |
fold (fn name1 => fold (composition_instance name1) atoms) atoms |> |
|
1292 |
curry (Library.foldr (fn ((i, (tyname, args, _)), thy) => |
|
1293 |
AxClass.add_inst_arity_i (tyname, replicate (length args) classes, classes) |
|
1294 |
(AxClass.intro_classes_tac [] THEN REPEAT (EVERY |
|
1295 |
[resolve_tac perm_empty_thms 1, |
|
1296 |
resolve_tac perm_append_thms 1, |
|
1297 |
resolve_tac perm_eq_thms 1, assume_tac 1])) thy)) |
|
1298 |
(List.take (descr, length new_type_names)) |> |
|
1299 |
PureThy.add_thmss |
|
1300 |
[((space_implode "_" new_type_names ^ "_unfolded_perm_eq", |
|
1301 |
unfolded_perm_eq_thms), [Simplifier.simp_add_global]), |
|
1302 |
((space_implode "_" new_type_names ^ "_perm_empty", |
|
1303 |
perm_empty_thms), [Simplifier.simp_add_global]), |
|
1304 |
((space_implode "_" new_type_names ^ "_perm_append", |
|
1305 |
perm_append_thms), [Simplifier.simp_add_global]), |
|
1306 |
((space_implode "_" new_type_names ^ "_perm_eq", |
|
1307 |
perm_eq_thms), [Simplifier.simp_add_global])]; |
|
1308 |
||
1309 |
(**** Define representing sets ****) |
|
1310 |
||
1311 |
val _ = warning "representing sets"; |
|
1312 |
||
1313 |
val rep_set_names = map (Sign.full_name thy3) (DatatypeProp.indexify_names |
|
1314 |
(map (fn (i, _) => name_of_typ (nth_dtyp i) ^ "_set") descr)); |
|
1315 |
val big_rep_name = |
|
1316 |
space_implode "_" (DatatypeProp.indexify_names (List.mapPartial |
|
1317 |
(fn (i, ("nominal.nOption", _, _)) => NONE |
|
1318 |
| (i, _) => SOME (name_of_typ (nth_dtyp i))) descr)) ^ "_set"; |
|
1319 |
val _ = warning ("big_rep_name: " ^ big_rep_name); |
|
1320 |
||
1321 |
fun strip_option (dtf as DtType ("fun", [dt, DtRec i])) = |
|
1322 |
(case AList.lookup op = descr i of |
|
1323 |
SOME ("nominal.nOption", _, [(_, [dt']), _]) => |
|
1324 |
apfst (cons dt) (strip_option dt') |
|
1325 |
| _ => ([], dtf)) |
|
1326 |
| strip_option dt = ([], dt); |
|
1327 |
||
1328 |
fun make_intr s T (cname, cargs) = |
|
1329 |
let |
|
1330 |
fun mk_prem (dt, (j, j', prems, ts)) = |
|
1331 |
let |
|
1332 |
val (dts, dt') = strip_option dt; |
|
1333 |
val (dts', dt'') = strip_dtyp dt'; |
|
1334 |
val Ts = map typ_of_dtyp dts; |
|
1335 |
val Us = map typ_of_dtyp dts'; |
|
1336 |
val T = typ_of_dtyp dt''; |
|
1337 |
val free = mk_Free "x" (Us ---> T) j; |
|
1338 |
val free' = app_bnds free (length Us); |
|
1339 |
fun mk_abs_fun (T, (i, t)) = |
|
1340 |
let val U = fastype_of t |
|
1341 |
in (i + 1, Const ("nominal.abs_fun", [T, U, T] ---> |
|
1342 |
Type ("nominal.nOption", [U])) $ mk_Free "y" T i $ t) |
|
1343 |
end |
|
1344 |
in (j + 1, j' + length Ts, |
|
1345 |
case dt'' of |
|
1346 |
DtRec k => list_all (map (pair "x") Us, |
|
1347 |
HOLogic.mk_Trueprop (HOLogic.mk_mem (free', |
|
1348 |
Const (List.nth (rep_set_names, k), |
|
1349 |
HOLogic.mk_setT T)))) :: prems |
|
1350 |
| _ => prems, |
|
1351 |
snd (foldr mk_abs_fun (j', free) Ts) :: ts) |
|
1352 |
end; |
|
1353 |
||
1354 |
val (_, _, prems, ts) = foldr mk_prem (1, 1, [], []) cargs; |
|
1355 |
val concl = HOLogic.mk_Trueprop (HOLogic.mk_mem |
|
1356 |
(list_comb (Const (cname, map fastype_of ts ---> T), ts), |
|
1357 |
Const (s, HOLogic.mk_setT T))) |
|
1358 |
in Logic.list_implies (prems, concl) |
|
1359 |
end; |
|
1360 |
||
1361 |
val (intr_ts, ind_consts) = |
|
1362 |
apfst List.concat (ListPair.unzip (List.mapPartial |
|
1363 |
(fn ((_, ("nominal.nOption", _, _)), _) => NONE |
|
1364 |
| ((i, (_, _, constrs)), rep_set_name) => |
|
1365 |
let val T = nth_dtyp i |
|
1366 |
in SOME (map (make_intr rep_set_name T) constrs, |
|
1367 |
Const (rep_set_name, HOLogic.mk_setT T)) |
|
1368 |
end) |
|
1369 |
(descr ~~ rep_set_names))); |
|
1370 |
||
1371 |
val (thy4, {raw_induct = rep_induct, intrs = rep_intrs, ...}) = |
|
1372 |
setmp InductivePackage.quiet_mode false |
|
1373 |
(InductivePackage.add_inductive_i false true big_rep_name false true false |
|
1374 |
ind_consts (map (fn x => (("", x), [])) intr_ts) []) thy3; |
|
1375 |
||
1376 |
(**** Prove that representing set is closed under permutation ****) |
|
1377 |
||
1378 |
val _ = warning "proving closure under permutation..."; |
|
1379 |
||
1380 |
val perm_indnames' = List.mapPartial |
|
1381 |
(fn (x, (_, ("nominal.nOption", _, _))) => NONE | (x, _) => SOME x) |
|
1382 |
(perm_indnames ~~ descr); |
|
1383 |
||
1384 |
fun mk_perm_closed name = map (fn th => standard (th RS mp)) |
|
18010 | 1385 |
(List.take (split_conj_thm (Goal.prove thy4 [] [] |
17870 | 1386 |
(HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj (map |
1387 |
(fn (S, x) => |
|
1388 |
let |
|
1389 |
val S = map_term_types (map_type_tfree |
|
1390 |
(fn (s, cs) => TFree (s, cs union cp_classes))) S; |
|
1391 |
val T = HOLogic.dest_setT (fastype_of S); |
|
1392 |
val permT = mk_permT (Type (name, [])) |
|
1393 |
in HOLogic.mk_imp (HOLogic.mk_mem (Free (x, T), S), |
|
1394 |
HOLogic.mk_mem (Const ("nominal.perm", permT --> T --> T) $ |
|
1395 |
Free ("pi", permT) $ Free (x, T), S)) |
|
18010 | 1396 |
end) (ind_consts ~~ perm_indnames')))) |
1397 |
(fn _ => EVERY (* CU: added perm_fun_def in the final tactic in order to deal with funs *) |
|
17870 | 1398 |
[indtac rep_induct [] 1, |
1399 |
ALLGOALS (simp_tac (simpset_of thy4 addsimps |
|
1400 |
(symmetric perm_fun_def :: PureThy.get_thms thy4 (Name ("abs_perm"))))), |
|
1401 |
ALLGOALS (resolve_tac rep_intrs |
|
1402 |
THEN_ALL_NEW (asm_full_simp_tac (simpset_of thy4 addsimps [perm_fun_def])))])), |
|
1403 |
length new_type_names)); |
|
1404 |
||
1405 |
(* FIXME: theorems are stored in database for testing only *) |
|
1406 |
val perm_closed_thmss = map mk_perm_closed atoms; |
|
1407 |
val (thy5, _) = PureThy.add_thmss [(("perm_closed", |
|
1408 |
List.concat perm_closed_thmss), [])] thy4; |
|
1409 |
||
1410 |
(**** typedef ****) |
|
1411 |
||
1412 |
val _ = warning "defining type..."; |
|
1413 |
||
1414 |
val (thy6, typedefs) = |
|
1415 |
foldl_map (fn (thy, ((((name, mx), tvs), c), name')) => |
|
1416 |
setmp TypedefPackage.quiet_mode true |
|
1417 |
(TypedefPackage.add_typedef_i false (SOME name') (name, tvs, mx) c NONE |
|
1418 |
(rtac exI 1 THEN |
|
1419 |
QUIET_BREADTH_FIRST (has_fewer_prems 1) |
|
1420 |
(resolve_tac rep_intrs 1))) thy |> (fn (thy, r) => |
|
1421 |
let |
|
1422 |
val permT = mk_permT (TFree (variant tvs "'a", HOLogic.typeS)); |
|
1423 |
val pi = Free ("pi", permT); |
|
1424 |
val tvs' = map (fn s => TFree (s, the (AList.lookup op = sorts' s))) tvs; |
|
1425 |
val T = Type (Sign.intern_type thy name, tvs'); |
|
1426 |
val Const (_, Type (_, [U])) = c |
|
1427 |
in apsnd (pair r o hd) |
|
1428 |
(PureThy.add_defs_i true [(("prm_" ^ name ^ "_def", Logic.mk_equals |
|
1429 |
(Const ("nominal.perm", permT --> T --> T) $ pi $ Free ("x", T), |
|
1430 |
Const (Sign.intern_const thy ("Abs_" ^ name), U --> T) $ |
|
1431 |
(Const ("nominal.perm", permT --> U --> U) $ pi $ |
|
1432 |
(Const (Sign.intern_const thy ("Rep_" ^ name), T --> U) $ |
|
1433 |
Free ("x", T))))), [])] thy) |
|
1434 |
end)) |
|
1435 |
(thy5, types_syntax ~~ tyvars ~~ |
|
1436 |
(List.take (ind_consts, length new_type_names)) ~~ new_type_names); |
|
1437 |
||
1438 |
val perm_defs = map snd typedefs; |
|
1439 |
val Abs_inverse_thms = map (#Abs_inverse o fst) typedefs; |
|
18016 | 1440 |
val Rep_inverse_thms = map (#Rep_inverse o fst) typedefs; |
17870 | 1441 |
val Rep_thms = map (#Rep o fst) typedefs; |
1442 |
||
18016 | 1443 |
val big_name = space_implode "_" new_type_names; |
1444 |
||
1445 |
||
17870 | 1446 |
(** prove that new types are in class pt_<name> **) |
1447 |
||
1448 |
val _ = warning "prove that new types are in class pt_<name> ..."; |
|
1449 |
||
1450 |
fun pt_instance ((class, atom), perm_closed_thms) = |
|
1451 |
fold (fn (((({Abs_inverse, Rep_inverse, Rep, ...}, |
|
1452 |
perm_def), name), tvs), perm_closed) => fn thy => |
|
1453 |
AxClass.add_inst_arity_i |
|
1454 |
(Sign.intern_type thy name, |
|
1455 |
replicate (length tvs) (classes @ cp_classes), [class]) |
|
1456 |
(EVERY [AxClass.intro_classes_tac [], |
|
1457 |
rewrite_goals_tac [perm_def], |
|
1458 |
asm_full_simp_tac (simpset_of thy addsimps [Rep_inverse]) 1, |
|
1459 |
asm_full_simp_tac (simpset_of thy addsimps |
|
1460 |
[Rep RS perm_closed RS Abs_inverse]) 1, |
|
1461 |
asm_full_simp_tac (HOL_basic_ss addsimps [PureThy.get_thm thy |
|
1462 |
(Name ("pt_" ^ Sign.base_name atom ^ "3"))]) 1]) thy) |
|
1463 |
(typedefs ~~ new_type_names ~~ tyvars ~~ perm_closed_thms); |
|
1464 |
||
1465 |
||
1466 |
(** prove that new types are in class cp_<name1>_<name2> **) |
|
1467 |
||
1468 |
val _ = warning "prove that new types are in class cp_<name1>_<name2> ..."; |
|
1469 |
||
1470 |
fun cp_instance (atom1, perm_closed_thms1) (atom2, perm_closed_thms2) thy = |
|
1471 |
let |
|
1472 |
val name = "cp_" ^ Sign.base_name atom1 ^ "_" ^ Sign.base_name atom2; |
|
1473 |
val class = Sign.intern_class thy name; |
|
1474 |
val cp1' = PureThy.get_thm thy (Name (name ^ "_inst")) RS cp1 |
|
1475 |
in fold (fn ((((({Abs_inverse, Rep_inverse, Rep, ...}, |
|
1476 |
perm_def), name), tvs), perm_closed1), perm_closed2) => fn thy => |
|
1477 |
AxClass.add_inst_arity_i |
|
1478 |
(Sign.intern_type thy name, |
|
1479 |
replicate (length tvs) (classes @ cp_classes), [class]) |
|
1480 |
(EVERY [AxClass.intro_classes_tac [], |
|
1481 |
rewrite_goals_tac [perm_def], |
|
1482 |
asm_full_simp_tac (simpset_of thy addsimps |
|
1483 |
((Rep RS perm_closed1 RS Abs_inverse) :: |
|
1484 |
(if atom1 = atom2 then [] |
|
1485 |
else [Rep RS perm_closed2 RS Abs_inverse]))) 1, |
|
18016 | 1486 |
cong_tac 1, |
17870 | 1487 |
rtac refl 1, |
1488 |
rtac cp1' 1]) thy) |
|
1489 |
(typedefs ~~ new_type_names ~~ tyvars ~~ perm_closed_thms1 ~~ |
|
1490 |
perm_closed_thms2) thy |
|
1491 |
end; |
|
1492 |
||
1493 |
val thy7 = fold (fn x => fn thy => thy |> |
|
1494 |
pt_instance x |> |
|
1495 |
fold (cp_instance (apfst snd x)) (atoms ~~ perm_closed_thmss)) |
|
1496 |
(classes ~~ atoms ~~ perm_closed_thmss) thy6; |
|
1497 |
||
1498 |
(**** constructors ****) |
|
1499 |
||
1500 |
fun mk_abs_fun (x, t) = |
|
1501 |
let |
|
1502 |
val T = fastype_of x; |
|
1503 |
val U = fastype_of t |
|
1504 |
in |
|
1505 |
Const ("nominal.abs_fun", T --> U --> T --> |
|
1506 |
Type ("nominal.nOption", [U])) $ x $ t |
|
1507 |
end; |
|
1508 |
||
18016 | 1509 |
val (ty_idxs, _) = foldl |
1510 |
(fn ((i, ("nominal.nOption", _, _)), p) => p |
|
1511 |
| ((i, _), (ty_idxs, j)) => (ty_idxs @ [(i, j)], j + 1)) ([], 0) descr; |
|
1512 |
||
1513 |
fun reindex (DtType (s, dts)) = DtType (s, map reindex dts) |
|
1514 |
| reindex (DtRec i) = DtRec (the (AList.lookup op = ty_idxs i)) |
|
1515 |
| reindex dt = dt; |
|
1516 |
||
1517 |
fun strip_suffix i s = implode (List.take (explode s, size s - i)); |
|
1518 |
||
1519 |
(** strips the "_Rep" in type names *) |
|
18045 | 1520 |
fun strip_nth_name i s = |
1521 |
let val xs = NameSpace.unpack s; |
|
1522 |
in NameSpace.pack (Library.nth_map (length xs - i) (strip_suffix 4) xs) end; |
|
18016 | 1523 |
|
1524 |
val descr'' = List.mapPartial |
|
1525 |
(fn (i, ("nominal.nOption", _, _)) => NONE |
|
1526 |
| (i, (s, dts, constrs)) => SOME (the (AList.lookup op = ty_idxs i), |
|
1527 |
(strip_nth_name 1 s, map reindex dts, |
|
1528 |
map (fn (cname, cargs) => |
|
1529 |
(strip_nth_name 2 cname, |
|
1530 |
foldl (fn (dt, dts) => |
|
1531 |
let val (dts', dt') = strip_option dt |
|
1532 |
in (dts @ dts' @ [reindex dt']) end) [] cargs)) constrs))) descr; |
|
18045 | 1533 |
|
18016 | 1534 |
val (descr1, descr2) = splitAt (length new_type_names, descr''); |
1535 |
val descr' = [descr1, descr2]; |
|
1536 |
||
17870 | 1537 |
val typ_of_dtyp' = replace_types' o typ_of_dtyp; |
1538 |
||
1539 |
val rep_names = map (fn s => |
|
1540 |
Sign.intern_const thy7 ("Rep_" ^ s)) new_type_names; |
|
1541 |
val abs_names = map (fn s => |
|
1542 |
Sign.intern_const thy7 ("Abs_" ^ s)) new_type_names; |
|
1543 |
||
18016 | 1544 |
val recTs' = List.mapPartial |
1545 |
(fn ((_, ("nominal.nOption", _, _)), T) => NONE |
|
1546 |
| (_, T) => SOME T) (descr ~~ get_rec_types descr sorts'); |
|
1547 |
val recTs = get_rec_types (List.concat descr') sorts'; |
|
1548 |
val newTs' = Library.take (length new_type_names, recTs'); |
|
1549 |
val newTs = Library.take (length new_type_names, recTs); |
|
17870 | 1550 |
|
1551 |
val full_new_type_names = map (Sign.full_name (sign_of thy)) new_type_names; |
|
1552 |
||
1553 |
fun make_constr_def tname T T' ((thy, defs, eqns), ((cname, cargs), (cname', mx))) = |
|
1554 |
let |
|
1555 |
fun constr_arg (dt, (j, l_args, r_args)) = |
|
1556 |
let |
|
1557 |
val x' = mk_Free "x" (typ_of_dtyp' dt) j; |
|
1558 |
val (dts, dt') = strip_option dt; |
|
1559 |
val xs = map (fn (dt, i) => mk_Free "x" (typ_of_dtyp' dt) i) |
|
1560 |
(dts ~~ (j upto j + length dts - 1)) |
|
1561 |
val x = mk_Free "x" (typ_of_dtyp' dt') (j + length dts) |
|
1562 |
val (dts', dt'') = strip_dtyp dt' |
|
1563 |
in case dt'' of |
|
1564 |
DtRec k => if k < length new_type_names then |
|
1565 |
(j + length dts + 1, |
|
1566 |
xs @ x :: l_args, |
|
1567 |
foldr mk_abs_fun |
|
1568 |
(list_abs (map (pair "z" o typ_of_dtyp') dts', |
|
1569 |
Const (List.nth (rep_names, k), typ_of_dtyp' dt'' --> |
|
1570 |
typ_of_dtyp dt'') $ app_bnds x (length dts'))) |
|
1571 |
xs :: r_args) |
|
1572 |
else error "nested recursion not (yet) supported" |
|
1573 |
| _ => (j + 1, x' :: l_args, x' :: r_args) |
|
1574 |
end |
|
1575 |
||
1576 |
val (_, l_args, r_args) = foldr constr_arg (1, [], []) cargs; |
|
1577 |
val abs_name = Sign.intern_const (Theory.sign_of thy) ("Abs_" ^ tname); |
|
1578 |
val rep_name = Sign.intern_const (Theory.sign_of thy) ("Rep_" ^ tname); |
|
1579 |
val constrT = map fastype_of l_args ---> T; |
|
1580 |
val lhs = list_comb (Const (Sign.full_name thy (Sign.base_name cname), |
|
1581 |
constrT), l_args); |
|
1582 |
val rhs = list_comb (Const (cname, map fastype_of r_args ---> T'), r_args); |
|
1583 |
val def = Logic.mk_equals (lhs, Const (abs_name, T' --> T) $ rhs); |
|
1584 |
val eqn = HOLogic.mk_Trueprop (HOLogic.mk_eq |
|
1585 |
(Const (rep_name, T --> T') $ lhs, rhs)); |
|
1586 |
val def_name = (Sign.base_name cname) ^ "_def"; |
|
1587 |
val (thy', [def_thm]) = thy |> |
|
1588 |
Theory.add_consts_i [(cname', constrT, mx)] |> |
|
1589 |
(PureThy.add_defs_i false o map Thm.no_attributes) [(def_name, def)] |
|
1590 |
in (thy', defs @ [def_thm], eqns @ [eqn]) end; |
|
1591 |
||
1592 |
fun dt_constr_defs ((thy, defs, eqns, dist_lemmas), |
|
1593 |
(((((_, (_, _, constrs)), tname), T), T'), constr_syntax)) = |
|
1594 |
let |
|
1595 |
val rep_const = cterm_of thy |
|
1596 |
(Const (Sign.intern_const thy ("Rep_" ^ tname), T --> T')); |
|
1597 |
val dist = standard (cterm_instantiate [(cterm_of thy distinct_f, rep_const)] distinct_lemma); |
|
1598 |
val (thy', defs', eqns') = Library.foldl (make_constr_def tname T T') |
|
1599 |
((Theory.add_path tname thy, defs, []), constrs ~~ constr_syntax) |
|
1600 |
in |
|
1601 |
(parent_path flat_names thy', defs', eqns @ [eqns'], dist_lemmas @ [dist]) |
|
1602 |
end; |
|
1603 |
||
1604 |
val (thy8, constr_defs, constr_rep_eqns, dist_lemmas) = Library.foldl dt_constr_defs |
|
1605 |
((thy7, [], [], []), List.take (descr, length new_type_names) ~~ |
|
1606 |
new_type_names ~~ newTs ~~ newTs' ~~ constr_syntax); |
|
1607 |
||
1608 |
val abs_inject_thms = map (fn tname => |
|
1609 |
PureThy.get_thm thy8 (Name ("Abs_" ^ tname ^ "_inject"))) new_type_names; |
|
1610 |
||
1611 |
val rep_inject_thms = map (fn tname => |
|
1612 |
PureThy.get_thm thy8 (Name ("Rep_" ^ tname ^ "_inject"))) new_type_names; |
|
1613 |
||
1614 |
val rep_thms = map (fn tname => |
|
1615 |
PureThy.get_thm thy8 (Name ("Rep_" ^ tname))) new_type_names; |
|
1616 |
||
1617 |
val rep_inverse_thms = map (fn tname => |
|
1618 |
PureThy.get_thm thy8 (Name ("Rep_" ^ tname ^ "_inverse"))) new_type_names; |
|
1619 |
||
1620 |
(* prove theorem Rep_i (Constr_j ...) = Constr'_j ... *) |
|
1621 |
||
1622 |
fun prove_constr_rep_thm eqn = |
|
1623 |
let |
|
1624 |
val inj_thms = map (fn r => r RS iffD1) abs_inject_thms; |
|
1625 |
val rewrites = constr_defs @ map mk_meta_eq rep_inverse_thms |
|
18010 | 1626 |
in standard (Goal.prove thy8 [] [] eqn (fn _ => EVERY |
17870 | 1627 |
[resolve_tac inj_thms 1, |
1628 |
rewrite_goals_tac rewrites, |
|
1629 |
rtac refl 3, |
|
1630 |
resolve_tac rep_intrs 2, |
|
18010 | 1631 |
REPEAT (resolve_tac rep_thms 1)])) |
17870 | 1632 |
end; |
1633 |
||
1634 |
val constr_rep_thmss = map (map prove_constr_rep_thm) constr_rep_eqns; |
|
1635 |
||
1636 |
(* prove theorem pi \<bullet> Rep_i x = Rep_i (pi \<bullet> x) *) |
|
1637 |
||
1638 |
fun prove_perm_rep_perm (atom, perm_closed_thms) = map (fn th => |
|
1639 |
let |
|
1640 |
val _ $ (_ $ (Rep $ x) $ _) = Logic.unvarify (prop_of th); |
|
1641 |
val Type ("fun", [T, U]) = fastype_of Rep; |
|
1642 |
val permT = mk_permT (Type (atom, [])); |
|
1643 |
val pi = Free ("pi", permT); |
|
1644 |
in |
|
18010 | 1645 |
standard (Goal.prove thy8 [] [] (HOLogic.mk_Trueprop (HOLogic.mk_eq |
17870 | 1646 |
(Const ("nominal.perm", permT --> U --> U) $ pi $ (Rep $ x), |
18010 | 1647 |
Rep $ (Const ("nominal.perm", permT --> T --> T) $ pi $ x)))) |
1648 |
(fn _ => simp_tac (HOL_basic_ss addsimps (perm_defs @ Abs_inverse_thms @ |
|
1649 |
perm_closed_thms @ Rep_thms)) 1)) |
|
17870 | 1650 |
end) Rep_thms; |
1651 |
||
1652 |
val perm_rep_perm_thms = List.concat (map prove_perm_rep_perm |
|
1653 |
(atoms ~~ perm_closed_thmss)); |
|
1654 |
||
1655 |
(* prove distinctness theorems *) |
|
1656 |
||
18016 | 1657 |
val distinct_props = setmp DatatypeProp.dtK 1000 |
1658 |
(DatatypeProp.make_distincts new_type_names descr' sorts') thy8; |
|
17870 | 1659 |
|
1660 |
val dist_rewrites = map (fn (rep_thms, dist_lemma) => |
|
1661 |
dist_lemma::(rep_thms @ [In0_eq, In1_eq, In0_not_In1, In1_not_In0])) |
|
1662 |
(constr_rep_thmss ~~ dist_lemmas); |
|
1663 |
||
1664 |
fun prove_distinct_thms (_, []) = [] |
|
1665 |
| prove_distinct_thms (p as (rep_thms, dist_lemma), t::ts) = |
|
1666 |
let |
|
18010 | 1667 |
val dist_thm = standard (Goal.prove thy8 [] [] t (fn _ => |
1668 |
simp_tac (simpset_of thy8 addsimps (dist_lemma :: rep_thms)) 1)) |
|
17870 | 1669 |
in dist_thm::(standard (dist_thm RS not_sym)):: |
1670 |
(prove_distinct_thms (p, ts)) |
|
1671 |
end; |
|
1672 |
||
1673 |
val distinct_thms = map prove_distinct_thms |
|
1674 |
(constr_rep_thmss ~~ dist_lemmas ~~ distinct_props); |
|
1675 |
||
1676 |
(** prove equations for permutation functions **) |
|
1677 |
||
1678 |
val abs_perm = PureThy.get_thms thy8 (Name "abs_perm"); (* FIXME *) |
|
1679 |
||
1680 |
val perm_simps' = map (fn (((i, (_, _, constrs)), tname), constr_rep_thms) => |
|
1681 |
let val T = replace_types' (nth_dtyp i) |
|
1682 |
in List.concat (map (fn (atom, perm_closed_thms) => |
|
1683 |
map (fn ((cname, dts), constr_rep_thm) => |
|
1684 |
let |
|
1685 |
val cname = Sign.intern_const thy8 |
|
1686 |
(NameSpace.append tname (Sign.base_name cname)); |
|
1687 |
val permT = mk_permT (Type (atom, [])); |
|
1688 |
val pi = Free ("pi", permT); |
|
1689 |
||
1690 |
fun perm t = |
|
1691 |
let val T = fastype_of t |
|
1692 |
in Const ("nominal.perm", permT --> T --> T) $ pi $ t end; |
|
1693 |
||
1694 |
fun constr_arg (dt, (j, l_args, r_args)) = |
|
1695 |
let |
|
1696 |
val x' = mk_Free "x" (typ_of_dtyp' dt) j; |
|
1697 |
val (dts, dt') = strip_option dt; |
|
1698 |
val Ts = map typ_of_dtyp' dts; |
|
1699 |
val xs = map (fn (T, i) => mk_Free "x" T i) |
|
1700 |
(Ts ~~ (j upto j + length dts - 1)) |
|
1701 |
val x = mk_Free "x" (typ_of_dtyp' dt') (j + length dts); |
|
1702 |
val (dts', dt'') = strip_dtyp dt'; |
|
1703 |
in case dt'' of |
|
1704 |
DtRec k => if k < length new_type_names then |
|
1705 |
(j + length dts + 1, |
|
1706 |
xs @ x :: l_args, |
|
1707 |
map perm (xs @ [x]) @ r_args) |
|
1708 |
else error "nested recursion not (yet) supported" |
|
1709 |
| _ => (j + 1, x' :: l_args, perm x' :: r_args) |
|
1710 |
end |
|
1711 |
||
1712 |
val (_, l_args, r_args) = foldr constr_arg (1, [], []) dts; |
|
1713 |
val c = Const (cname, map fastype_of l_args ---> T) |
|
1714 |
in |
|
18010 | 1715 |
standard (Goal.prove thy8 [] [] |
17870 | 1716 |
(HOLogic.mk_Trueprop (HOLogic.mk_eq |
18010 | 1717 |
(perm (list_comb (c, l_args)), list_comb (c, r_args)))) |
1718 |
(fn _ => EVERY |
|
17870 | 1719 |
[simp_tac (simpset_of thy8 addsimps (constr_rep_thm :: perm_defs)) 1, |
1720 |
simp_tac (HOL_basic_ss addsimps (Rep_thms @ Abs_inverse_thms @ |
|
1721 |
constr_defs @ perm_closed_thms)) 1, |
|
1722 |
TRY (simp_tac (HOL_basic_ss addsimps |
|
1723 |
(symmetric perm_fun_def :: abs_perm)) 1), |
|
1724 |
TRY (simp_tac (HOL_basic_ss addsimps |
|
1725 |
(perm_fun_def :: perm_defs @ Rep_thms @ Abs_inverse_thms @ |
|
18010 | 1726 |
perm_closed_thms)) 1)])) |
17870 | 1727 |
end) (constrs ~~ constr_rep_thms)) (atoms ~~ perm_closed_thmss)) |
1728 |
end) (List.take (descr, length new_type_names) ~~ new_type_names ~~ constr_rep_thmss); |
|
1729 |
||
1730 |
(** prove injectivity of constructors **) |
|
1731 |
||
1732 |
val rep_inject_thms' = map (fn th => th RS sym) rep_inject_thms; |
|
1733 |
val alpha = PureThy.get_thms thy8 (Name "alpha"); |
|
1734 |
val abs_fresh = PureThy.get_thms thy8 (Name "abs_fresh"); |
|
1735 |
val fresh_def = PureThy.get_thm thy8 (Name "fresh_def"); |
|
1736 |
val supp_def = PureThy.get_thm thy8 (Name "supp_def"); |
|
1737 |
||
1738 |
val inject_thms = map (fn (((i, (_, _, constrs)), tname), constr_rep_thms) => |
|
1739 |
let val T = replace_types' (nth_dtyp i) |
|
1740 |
in List.mapPartial (fn ((cname, dts), constr_rep_thm) => |
|
1741 |
if null dts then NONE else SOME |
|
1742 |
let |
|
1743 |
val cname = Sign.intern_const thy8 |
|
1744 |
(NameSpace.append tname (Sign.base_name cname)); |
|
1745 |
||
1746 |
fun make_inj (dt, (j, args1, args2, eqs)) = |
|
1747 |
let |
|
1748 |
val x' = mk_Free "x" (typ_of_dtyp' dt) j; |
|
1749 |
val y' = mk_Free "y" (typ_of_dtyp' dt) j; |
|
1750 |
val (dts, dt') = strip_option dt; |
|
1751 |
val Ts_idx = map typ_of_dtyp' dts ~~ (j upto j + length dts - 1); |
|
1752 |
val xs = map (fn (T, i) => mk_Free "x" T i) Ts_idx; |
|
1753 |
val ys = map (fn (T, i) => mk_Free "y" T i) Ts_idx; |
|
1754 |
val x = mk_Free "x" (typ_of_dtyp' dt') (j + length dts); |
|
1755 |
val y = mk_Free "y" (typ_of_dtyp' dt') (j + length dts); |
|
1756 |
val (dts', dt'') = strip_dtyp dt'; |
|
1757 |
in case dt'' of |
|
1758 |
DtRec k => if k < length new_type_names then |
|
1759 |
(j + length dts + 1, |
|
1760 |
xs @ (x :: args1), ys @ (y :: args2), |
|
1761 |
HOLogic.mk_eq |
|
1762 |
(foldr mk_abs_fun x xs, foldr mk_abs_fun y ys) :: eqs) |
|
1763 |
else error "nested recursion not (yet) supported" |
|
1764 |
| _ => (j + 1, x' :: args1, y' :: args2, HOLogic.mk_eq (x', y') :: eqs) |
|
1765 |
end; |
|
1766 |
||
1767 |
val (_, args1, args2, eqs) = foldr make_inj (1, [], [], []) dts; |
|
1768 |
val Ts = map fastype_of args1; |
|
1769 |
val c = Const (cname, Ts ---> T) |
|
1770 |
in |
|
18010 | 1771 |
standard (Goal.prove thy8 [] [] (HOLogic.mk_Trueprop (HOLogic.mk_eq |
17870 | 1772 |
(HOLogic.mk_eq (list_comb (c, args1), list_comb (c, args2)), |
18010 | 1773 |
foldr1 HOLogic.mk_conj eqs))) |
1774 |
(fn _ => EVERY |
|
17870 | 1775 |
[asm_full_simp_tac (simpset_of thy8 addsimps (constr_rep_thm :: |
1776 |
rep_inject_thms')) 1, |
|
1777 |
TRY (asm_full_simp_tac (HOL_basic_ss addsimps (fresh_def :: supp_def :: |
|
1778 |
alpha @ abs_perm @ abs_fresh @ rep_inject_thms @ |
|
17874
8be65cf94d2e
Improved proof of injectivity theorems to make it work on
berghofe
parents:
17873
diff
changeset
|
1779 |
perm_rep_perm_thms)) 1), |
8be65cf94d2e
Improved proof of injectivity theorems to make it work on
berghofe
parents:
17873
diff
changeset
|
1780 |
TRY (asm_full_simp_tac (HOL_basic_ss addsimps (perm_fun_def :: |
18010 | 1781 |
expand_fun_eq :: rep_inject_thms @ perm_rep_perm_thms)) 1)])) |
17870 | 1782 |
end) (constrs ~~ constr_rep_thms) |
1783 |
end) (List.take (descr, length new_type_names) ~~ new_type_names ~~ constr_rep_thmss); |
|
1784 |
||
17872
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1785 |
(** equations for support and freshness **) |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1786 |
|
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1787 |
val Un_assoc = PureThy.get_thm thy8 (Name "Un_assoc"); |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1788 |
val de_Morgan_conj = PureThy.get_thm thy8 (Name "de_Morgan_conj"); |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1789 |
val Collect_disj_eq = PureThy.get_thm thy8 (Name "Collect_disj_eq"); |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1790 |
val finite_Un = PureThy.get_thm thy8 (Name "finite_Un"); |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1791 |
|
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1792 |
val (supp_thms, fresh_thms) = ListPair.unzip (map ListPair.unzip |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1793 |
(map (fn ((((i, (_, _, constrs)), tname), inject_thms'), perm_thms') => |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1794 |
let val T = replace_types' (nth_dtyp i) |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1795 |
in List.concat (map (fn (cname, dts) => map (fn atom => |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1796 |
let |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1797 |
val cname = Sign.intern_const thy8 |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1798 |
(NameSpace.append tname (Sign.base_name cname)); |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1799 |
val atomT = Type (atom, []); |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1800 |
|
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1801 |
fun process_constr (dt, (j, args1, args2)) = |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1802 |
let |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1803 |
val x' = mk_Free "x" (typ_of_dtyp' dt) j; |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1804 |
val (dts, dt') = strip_option dt; |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1805 |
val Ts_idx = map typ_of_dtyp' dts ~~ (j upto j + length dts - 1); |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1806 |
val xs = map (fn (T, i) => mk_Free "x" T i) Ts_idx; |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1807 |
val x = mk_Free "x" (typ_of_dtyp' dt') (j + length dts); |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1808 |
val (dts', dt'') = strip_dtyp dt'; |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1809 |
in case dt'' of |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1810 |
DtRec k => if k < length new_type_names then |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1811 |
(j + length dts + 1, |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1812 |
xs @ (x :: args1), foldr mk_abs_fun x xs :: args2) |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1813 |
else error "nested recursion not (yet) supported" |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1814 |
| _ => (j + 1, x' :: args1, x' :: args2) |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1815 |
end; |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1816 |
|
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1817 |
val (_, args1, args2) = foldr process_constr (1, [], []) dts; |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1818 |
val Ts = map fastype_of args1; |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1819 |
val c = list_comb (Const (cname, Ts ---> T), args1); |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1820 |
fun supp t = |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1821 |
Const ("nominal.supp", fastype_of t --> HOLogic.mk_setT atomT) $ t; |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1822 |
fun fresh t = |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1823 |
Const ("nominal.fresh", atomT --> fastype_of t --> HOLogic.boolT) $ |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1824 |
Free ("a", atomT) $ t; |
18010 | 1825 |
val supp_thm = standard (Goal.prove thy8 [] [] |
17872
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1826 |
(HOLogic.mk_Trueprop (HOLogic.mk_eq |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1827 |
(supp c, |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1828 |
if null dts then Const ("{}", HOLogic.mk_setT atomT) |
18010 | 1829 |
else foldr1 (HOLogic.mk_binop "op Un") (map supp args2)))) |
17872
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1830 |
(fn _ => |
18010 | 1831 |
simp_tac (HOL_basic_ss addsimps (supp_def :: |
17872
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1832 |
Un_assoc :: de_Morgan_conj :: Collect_disj_eq :: finite_Un :: |
17874
8be65cf94d2e
Improved proof of injectivity theorems to make it work on
berghofe
parents:
17873
diff
changeset
|
1833 |
symmetric empty_def :: Finites.emptyI :: simp_thms @ |
18010 | 1834 |
abs_perm @ abs_fresh @ inject_thms' @ perm_thms')) 1)) |
17872
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1835 |
in |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1836 |
(supp_thm, |
18010 | 1837 |
standard (Goal.prove thy8 [] [] (HOLogic.mk_Trueprop (HOLogic.mk_eq |
17872
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1838 |
(fresh c, |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1839 |
if null dts then HOLogic.true_const |
18010 | 1840 |
else foldr1 HOLogic.mk_conj (map fresh args2)))) |
17872
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1841 |
(fn _ => |
18010 | 1842 |
simp_tac (simpset_of thy8 addsimps [fresh_def, supp_thm]) 1))) |
17872
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1843 |
end) atoms) constrs) |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1844 |
end) (List.take (descr, length new_type_names) ~~ new_type_names ~~ inject_thms ~~ perm_simps'))); |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1845 |
|
18016 | 1846 |
(**** Induction theorem ****) |
1847 |
||
1848 |
val arities = get_arities (List.concat descr'); |
|
1849 |
||
1850 |
fun mk_funs_inv thm = |
|
1851 |
let |
|
1852 |
val {sign, prop, ...} = rep_thm thm; |
|
1853 |
val _ $ (_ $ (Const (_, Type (_, [U, _])) $ _ $ S)) $ |
|
1854 |
(_ $ (_ $ (r $ (a $ _)) $ _)) = Type.freeze prop; |
|
1855 |
val used = add_term_tfree_names (a, []); |
|
1856 |
||
1857 |
fun mk_thm i = |
|
1858 |
let |
|
1859 |
val Ts = map (TFree o rpair HOLogic.typeS) |
|
1860 |
(variantlist (replicate i "'t", used)); |
|
1861 |
val f = Free ("f", Ts ---> U) |
|
1862 |
in standard (Goal.prove sign [] [] (Logic.mk_implies |
|
1863 |
(HOLogic.mk_Trueprop (HOLogic.list_all |
|
1864 |
(map (pair "x") Ts, HOLogic.mk_mem (app_bnds f i, S))), |
|
1865 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (list_abs (map (pair "x") Ts, |
|
1866 |
r $ (a $ app_bnds f i)), f)))) |
|
1867 |
(fn _ => EVERY [REPEAT (rtac ext 1), REPEAT (etac allE 1), rtac thm 1, atac 1])) |
|
1868 |
end |
|
1869 |
in map (fn r => r RS subst) (thm :: map mk_thm arities) end; |
|
1870 |
||
1871 |
fun mk_indrule_lemma ((prems, concls), (((i, _), T), U)) = |
|
1872 |
let |
|
1873 |
val Rep_t = Const (List.nth (rep_names, i), T --> U) $ |
|
1874 |
mk_Free "x" T i; |
|
1875 |
||
1876 |
val Abs_t = Const (List.nth (abs_names, i), U --> T) |
|
1877 |
||
1878 |
in (prems @ [HOLogic.imp $ HOLogic.mk_mem (Rep_t, |
|
1879 |
Const (List.nth (rep_set_names, i), HOLogic.mk_setT U)) $ |
|
1880 |
(mk_Free "P" (T --> HOLogic.boolT) (i + 1) $ (Abs_t $ Rep_t))], |
|
1881 |
concls @ [mk_Free "P" (T --> HOLogic.boolT) (i + 1) $ mk_Free "x" T i]) |
|
1882 |
end; |
|
1883 |
||
1884 |
val (indrule_lemma_prems, indrule_lemma_concls) = |
|
1885 |
Library.foldl mk_indrule_lemma (([], []), (List.concat descr' ~~ recTs ~~ recTs')); |
|
1886 |
||
1887 |
val indrule_lemma = standard (Goal.prove thy8 [] [] |
|
1888 |
(Logic.mk_implies |
|
1889 |
(HOLogic.mk_Trueprop (mk_conj indrule_lemma_prems), |
|
1890 |
HOLogic.mk_Trueprop (mk_conj indrule_lemma_concls))) (fn _ => EVERY |
|
1891 |
[REPEAT (etac conjE 1), |
|
1892 |
REPEAT (EVERY |
|
1893 |
[TRY (rtac conjI 1), full_simp_tac (HOL_basic_ss addsimps Rep_inverse_thms) 1, |
|
1894 |
etac mp 1, resolve_tac Rep_thms 1])])); |
|
1895 |
||
1896 |
val Ps = map head_of (HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of indrule_lemma))); |
|
1897 |
val frees = if length Ps = 1 then [Free ("P", snd (dest_Var (hd Ps)))] else |
|
1898 |
map (Free o apfst fst o dest_Var) Ps; |
|
1899 |
val indrule_lemma' = cterm_instantiate |
|
1900 |
(map (cterm_of thy8) Ps ~~ map (cterm_of thy8) frees) indrule_lemma; |
|
1901 |
||
1902 |
val Abs_inverse_thms' = List.concat (map mk_funs_inv Abs_inverse_thms); |
|
1903 |
||
1904 |
val dt_induct_prop = DatatypeProp.make_ind descr' sorts'; |
|
1905 |
val dt_induct = standard (Goal.prove thy8 [] |
|
1906 |
(Logic.strip_imp_prems dt_induct_prop) (Logic.strip_imp_concl dt_induct_prop) |
|
1907 |
(fn prems => EVERY |
|
1908 |
[rtac indrule_lemma' 1, |
|
1909 |
(DatatypeAux.indtac rep_induct THEN_ALL_NEW ObjectLogic.atomize_tac) 1, |
|
1910 |
EVERY (map (fn (prem, r) => (EVERY |
|
1911 |
[REPEAT (eresolve_tac Abs_inverse_thms' 1), |
|
1912 |
simp_tac (HOL_basic_ss addsimps [symmetric r]) 1, |
|
1913 |
DEPTH_SOLVE_1 (ares_tac [prem] 1 ORELSE etac allE 1)])) |
|
1914 |
(prems ~~ constr_defs))])); |
|
1915 |
||
18017
f6abeac6dcb5
fixed case names in the weak induction principle and
urbanc
parents:
18016
diff
changeset
|
1916 |
val case_names_induct = mk_case_names_induct (List.concat descr'); |
18016 | 1917 |
|
18066
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1918 |
(**** prove that new datatypes have finite support ****) |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1919 |
|
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1920 |
val indnames = DatatypeProp.make_tnames recTs; |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1921 |
|
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1922 |
val abs_supp = PureThy.get_thms thy8 (Name "abs_supp"); |
18067 | 1923 |
val supp_atm = PureThy.get_thms thy8 (Name "supp_atm"); |
18066
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1924 |
|
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1925 |
val finite_supp_thms = map (fn atom => |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1926 |
let val atomT = Type (atom, []) |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1927 |
in map standard (List.take |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1928 |
(split_conj_thm (Goal.prove thy8 [] [] (HOLogic.mk_Trueprop |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1929 |
(foldr1 HOLogic.mk_conj (map (fn (s, T) => HOLogic.mk_mem |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1930 |
(Const ("nominal.supp", T --> HOLogic.mk_setT atomT) $ Free (s, T), |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1931 |
Const ("Finite_Set.Finites", HOLogic.mk_setT (HOLogic.mk_setT atomT)))) |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1932 |
(indnames ~~ recTs)))) |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1933 |
(fn _ => indtac dt_induct indnames 1 THEN |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1934 |
ALLGOALS (asm_full_simp_tac (simpset_of thy8 addsimps |
18067 | 1935 |
(abs_supp @ supp_atm @ |
18066
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1936 |
PureThy.get_thms thy8 (Name ("fs_" ^ Sign.base_name atom ^ "1")) @ |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1937 |
List.concat supp_thms))))), |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1938 |
length new_type_names)) |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1939 |
end) atoms; |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1940 |
|
17870 | 1941 |
val (thy9, _) = thy8 |> |
18016 | 1942 |
Theory.add_path big_name |> |
18017
f6abeac6dcb5
fixed case names in the weak induction principle and
urbanc
parents:
18016
diff
changeset
|
1943 |
PureThy.add_thms [(("induct_weak", dt_induct), [case_names_induct])] |>> |
18016 | 1944 |
Theory.parent_path |>>> |
17870 | 1945 |
DatatypeAux.store_thmss "distinct" new_type_names distinct_thms |>>> |
1946 |
DatatypeAux.store_thmss "constr_rep" new_type_names constr_rep_thmss |>>> |
|
1947 |
DatatypeAux.store_thmss "perm" new_type_names perm_simps' |>>> |
|
17872
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1948 |
DatatypeAux.store_thmss "inject" new_type_names inject_thms |>>> |
f08fc98a164a
Implemented proofs for support and freshness theorems.
berghofe
parents:
17870
diff
changeset
|
1949 |
DatatypeAux.store_thmss "supp" new_type_names supp_thms |>>> |
18066
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1950 |
DatatypeAux.store_thmss "fresh" new_type_names fresh_thms |>> |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1951 |
fold (fn (atom, ths) => fn thy => |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1952 |
let val class = Sign.intern_class thy ("fs_" ^ Sign.base_name atom) |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1953 |
in fold (fn T => AxClass.add_inst_arity_i |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1954 |
(fst (dest_Type T), |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1955 |
replicate (length sorts) [class], [class]) |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1956 |
(AxClass.intro_classes_tac [] THEN resolve_tac ths 1)) newTs thy |
d1e47ee13070
Added code for proving that new datatype has finite support.
berghofe
parents:
18054
diff
changeset
|
1957 |
end) (atoms ~~ finite_supp_thms); |
17870 | 1958 |
|
1959 |
in |
|
1960 |
(thy9, perm_eq_thms) |
|
1961 |
end; |
|
1962 |
||
1963 |
val add_nominal_datatype = gen_add_nominal_datatype read_typ true; |
|
1964 |
||
1965 |
||
1966 |
(* FIXME: The following stuff should be exported by DatatypePackage *) |
|
1967 |
||
1968 |
local structure P = OuterParse and K = OuterKeyword in |
|
1969 |
||
1970 |
val datatype_decl = |
|
1971 |
Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") -- P.type_args -- P.name -- P.opt_infix -- |
|
1972 |
(P.$$$ "=" |-- P.enum1 "|" (P.name -- Scan.repeat P.typ -- P.opt_mixfix)); |
|
1973 |
||
1974 |
fun mk_datatype args = |
|
1975 |
let |
|
1976 |
val names = map (fn ((((NONE, _), t), _), _) => t | ((((SOME t, _), _), _), _) => t) args; |
|
1977 |
val specs = map (fn ((((_, vs), t), mx), cons) => |
|
1978 |
(vs, t, mx, map (fn ((x, y), z) => (x, y, z)) cons)) args; |
|
1979 |
in #1 o add_nominal_datatype false names specs end; |
|
1980 |
||
1981 |
val nominal_datatypeP = |
|
1982 |
OuterSyntax.command "nominal_datatype" "define inductive datatypes" K.thy_decl |
|
1983 |
(P.and_list1 datatype_decl >> (Toplevel.theory o mk_datatype)); |
|
1984 |
||
1985 |
val _ = OuterSyntax.add_parsers [nominal_datatypeP]; |
|
1986 |
||
1987 |
end; |
|
1988 |
||
1989 |
end |