1159
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
1 |
theory Perm
|
1170
|
2 |
imports "Nominal2_Atoms"
|
1159
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
3 |
begin
|
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
4 |
|
1170
|
5 |
ML {*
|
|
6 |
open Datatype_Aux; (* typ_of_dtyp, DtRec, ... *)
|
|
7 |
fun permute ty = Const (@{const_name permute}, @{typ perm} --> ty --> ty);
|
|
8 |
val minus_perm = Const (@{const_name minus}, @{typ perm} --> @{typ perm});
|
|
9 |
*}
|
1159
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
10 |
|
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
11 |
ML {*
|
1248
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
12 |
fun prove_perm_empty lthy induct perm_def perm_frees =
|
1247
a728e199851d
Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
13 |
let
|
1248
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
14 |
val perm_types = map fastype_of perm_frees;
|
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
15 |
val perm_indnames = Datatype_Prop.make_tnames (map body_type perm_types);
|
1256
|
16 |
fun glc ((perm, T), x) =
|
|
17 |
HOLogic.mk_eq (perm $ @{term "0 :: perm"} $ Free (x, T), Free (x, T))
|
1247
a728e199851d
Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
18 |
val gl =
|
a728e199851d
Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
19 |
HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
|
1256
|
20 |
(map glc (perm_frees ~~ map body_type perm_types ~~ perm_indnames)));
|
1247
a728e199851d
Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
21 |
fun tac _ =
|
a728e199851d
Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
22 |
EVERY [
|
a728e199851d
Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
23 |
indtac induct perm_indnames 1,
|
a728e199851d
Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
24 |
ALLGOALS (asm_full_simp_tac (HOL_ss addsimps (@{thm permute_zero} :: perm_def)))
|
a728e199851d
Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
25 |
];
|
a728e199851d
Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
26 |
in
|
a728e199851d
Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
27 |
split_conj_thm (Goal.prove lthy perm_indnames [] gl tac)
|
a728e199851d
Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
28 |
end;
|
a728e199851d
Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
29 |
*}
|
a728e199851d
Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
30 |
|
1248
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
31 |
ML {*
|
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
32 |
fun prove_perm_append lthy induct perm_def perm_frees =
|
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
33 |
let
|
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
34 |
val add_perm = @{term "op + :: (perm \<Rightarrow> perm \<Rightarrow> perm)"}
|
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
35 |
val pi1 = Free ("pi1", @{typ perm});
|
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
36 |
val pi2 = Free ("pi2", @{typ perm});
|
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
37 |
val perm_types = map fastype_of perm_frees
|
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
38 |
val perm_indnames = Datatype_Prop.make_tnames (map body_type perm_types);
|
1256
|
39 |
fun glc ((perm, T), x) =
|
|
40 |
HOLogic.mk_eq (
|
|
41 |
perm $ (add_perm $ pi1 $ pi2) $ Free (x, T),
|
|
42 |
perm $ pi1 $ (perm $ pi2 $ Free (x, T)))
|
1248
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
43 |
val gl =
|
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
44 |
(HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
|
1256
|
45 |
(map glc (perm_frees ~~ map body_type perm_types ~~ perm_indnames))))
|
1248
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
46 |
fun tac _ =
|
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
47 |
EVERY [
|
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
48 |
indtac induct perm_indnames 1,
|
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
49 |
ALLGOALS (asm_full_simp_tac (HOL_ss addsimps (@{thm permute_plus} :: perm_def)))
|
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
50 |
]
|
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
51 |
in
|
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
52 |
split_conj_thm (Goal.prove lthy ("pi1" :: "pi2" :: perm_indnames) [] gl tac)
|
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
53 |
end;
|
705afaaf6fb4
More refactoring and removed references to the global simpset in Perm.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
54 |
*}
|
1247
a728e199851d
Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
55 |
|
a728e199851d
Factor-out 'prove_perm_empty'; I plan to use it in defining permutations on the lifted type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
|
56 |
ML {*
|
1277
|
57 |
fun define_raw_perms (dt_info : info) number thy =
|
1170
|
58 |
let
|
1277
|
59 |
val {descr, induct, sorts, ...} = dt_info;
|
|
60 |
val all_full_tnames = map (fn (_, (n, _, _)) => n) descr;
|
|
61 |
val full_tnames = List.take (all_full_tnames, number);
|
1159
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
62 |
fun nth_dtyp i = typ_of_dtyp descr sorts (DtRec i);
|
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
63 |
val perm_names' = Datatype_Prop.indexify_names (map (fn (i, _) =>
|
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
64 |
"permute_" ^ name_of_typ (nth_dtyp i)) descr);
|
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
65 |
val perm_types = map (fn (i, _) =>
|
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
66 |
let val T = nth_dtyp i
|
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
67 |
in @{typ perm} --> T --> T end) descr;
|
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
68 |
val perm_names_types' = perm_names' ~~ perm_types;
|
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
69 |
val pi = Free ("pi", @{typ perm});
|
1164
|
70 |
fun perm_eq_constr i (cname, dts) =
|
|
71 |
let
|
|
72 |
val Ts = map (typ_of_dtyp descr sorts) dts;
|
|
73 |
val names = Name.variant_list ["pi"] (Datatype_Prop.make_tnames Ts);
|
|
74 |
val args = map Free (names ~~ Ts);
|
|
75 |
val c = Const (cname, Ts ---> (nth_dtyp i));
|
|
76 |
fun perm_arg (dt, x) =
|
|
77 |
let val T = type_of x
|
|
78 |
in
|
|
79 |
if is_rec_type dt then
|
|
80 |
let val (Us, _) = strip_type T
|
|
81 |
in list_abs (map (pair "x") Us,
|
|
82 |
Free (nth perm_names_types' (body_index dt)) $ pi $
|
|
83 |
list_comb (x, map (fn (i, U) =>
|
|
84 |
(permute U) $ (minus_perm $ pi) $ Bound i)
|
|
85 |
((length Us - 1 downto 0) ~~ Us)))
|
|
86 |
end
|
|
87 |
else (permute T) $ pi $ x
|
|
88 |
end;
|
|
89 |
in
|
|
90 |
(Attrib.empty_binding, HOLogic.mk_Trueprop (HOLogic.mk_eq
|
|
91 |
(Free (nth perm_names_types' i) $
|
|
92 |
Free ("pi", @{typ perm}) $ list_comb (c, args),
|
|
93 |
list_comb (c, map perm_arg (dts ~~ args)))))
|
|
94 |
end;
|
1170
|
95 |
fun perm_eq (i, (_, _, constrs)) = map (perm_eq_constr i) constrs;
|
|
96 |
val perm_eqs = maps perm_eq descr;
|
|
97 |
val lthy =
|
|
98 |
Theory_Target.instantiation (full_tnames, [], @{sort pt}) thy;
|
1257
|
99 |
val ((perm_frees, perm_ldef), lthy') =
|
1170
|
100 |
Primrec.add_primrec
|
|
101 |
(map (fn s => (Binding.name s, NONE, NoSyn)) perm_names') perm_eqs lthy;
|
1277
|
102 |
val perm_empty_thms = List.take (prove_perm_empty lthy' induct perm_ldef perm_frees, number);
|
|
103 |
val perm_append_thms = List.take (prove_perm_append lthy' induct perm_ldef perm_frees, number)
|
1249
|
104 |
val perms_name = space_implode "_" perm_names'
|
|
105 |
val perms_zero_bind = Binding.name (perms_name ^ "_zero")
|
|
106 |
val perms_append_bind = Binding.name (perms_name ^ "_append")
|
1257
|
107 |
fun tac _ (_, simps, _) =
|
1253
|
108 |
(Class.intro_classes_tac []) THEN (ALLGOALS (resolve_tac simps));
|
1257
|
109 |
fun morphism phi (dfs, simps, fvs) =
|
|
110 |
(map (Morphism.thm phi) dfs, map (Morphism.thm phi) simps, map (Morphism.term phi) fvs);
|
1170
|
111 |
in
|
1249
|
112 |
lthy'
|
|
113 |
|> snd o (Local_Theory.note ((perms_zero_bind, []), perm_empty_thms))
|
|
114 |
|> snd o (Local_Theory.note ((perms_append_bind, []), perm_append_thms))
|
1257
|
115 |
|> Class_Target.prove_instantiation_exit_result morphism tac (perm_ldef, (perm_empty_thms @ perm_append_thms), perm_frees)
|
1170
|
116 |
end
|
1159
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
117 |
|
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
118 |
*}
|
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
119 |
|
1253
|
120 |
ML {*
|
|
121 |
fun define_lifted_perms full_tnames name_term_pairs thms thy =
|
|
122 |
let
|
|
123 |
val lthy =
|
|
124 |
Theory_Target.instantiation (full_tnames, [], @{sort pt}) thy;
|
|
125 |
val lthy' = fold (snd oo Quotient_Def.quotient_lift_const) name_term_pairs lthy
|
|
126 |
val lifted_thms = map (fn x => snd (Quotient_Tacs.lifted_attrib (Context.Proof lthy', x))) thms
|
|
127 |
fun tac _ =
|
|
128 |
Class.intro_classes_tac [] THEN
|
|
129 |
(ALLGOALS (resolve_tac lifted_thms))
|
|
130 |
val lthy'' = Class.prove_instantiation_instance tac lthy'
|
|
131 |
in
|
|
132 |
Local_Theory.exit_global lthy''
|
|
133 |
end
|
|
134 |
*}
|
|
135 |
|
1342
|
136 |
ML {*
|
|
137 |
fun neq_to_rel r neq =
|
|
138 |
let
|
|
139 |
val neq = HOLogic.dest_Trueprop (prop_of neq)
|
|
140 |
val eq = HOLogic.dest_not neq
|
|
141 |
val (lhs, rhs) = HOLogic.dest_eq eq
|
|
142 |
val rel = r $ lhs $ rhs
|
|
143 |
val nrel = HOLogic.mk_not rel
|
|
144 |
in
|
|
145 |
HOLogic.mk_Trueprop nrel
|
|
146 |
end
|
|
147 |
*}
|
|
148 |
|
|
149 |
ML {*
|
|
150 |
fun neq_to_rel_tac cases distinct =
|
|
151 |
rtac notI THEN' eresolve_tac cases THEN_ALL_NEW asm_full_simp_tac (HOL_ss addsimps distinct)
|
|
152 |
*}
|
|
153 |
|
|
154 |
ML {*
|
|
155 |
fun distinct_rel ctxt cases (dists, rel) =
|
|
156 |
let
|
|
157 |
val ((_, thms), ctxt') = Variable.import false dists ctxt
|
|
158 |
val terms = map (neq_to_rel rel) thms
|
|
159 |
val nrels = map (fn t => Goal.prove ctxt' [] [] t (fn _ => neq_to_rel_tac cases dists 1)) terms
|
|
160 |
in
|
|
161 |
Variable.export ctxt' ctxt nrels
|
|
162 |
end
|
|
163 |
*}
|
|
164 |
|
|
165 |
|
|
166 |
|
1170
|
167 |
(* Test
|
|
168 |
atom_decl name
|
1159
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
169 |
|
1170
|
170 |
datatype rtrm1 =
|
|
171 |
rVr1 "name"
|
|
172 |
| rAp1 "rtrm1" "rtrm1 list"
|
|
173 |
| rLm1 "name" "rtrm1"
|
|
174 |
| rLt1 "bp" "rtrm1" "rtrm1"
|
|
175 |
and bp =
|
|
176 |
BUnit
|
|
177 |
| BVr "name"
|
|
178 |
| BPr "bp" "bp"
|
1159
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
179 |
|
1170
|
180 |
|
|
181 |
setup {* snd o define_raw_perms ["rtrm1", "bp"] ["Perm.rtrm1", "Perm.bp"] *}
|
|
182 |
print_theorems
|
|
183 |
*)
|
|
184 |
|
1159
3c6bee89d826
Ported Stefan's permutation code, still needs some localizing.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
185 |
end
|