1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
+ − 1
theory Fv
1534
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 2
imports "Nominal2_Atoms" "Abs" "Perm" "Rsp" "Nominal2_FSet"
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
+ − 3
begin
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
+ − 4
1505
+ − 5
(* The bindings data structure:
+ − 6
+ − 7
Bindings are a list of lists of lists of triples.
1172
+ − 8
1358
+ − 9
The first list represents the datatypes defined.
+ − 10
The second list represents the constructors.
+ − 11
The internal list is a list of all the bndings that
+ − 12
concern the constructor.
1172
+ − 13
1358
+ − 14
Every triple consists of a function, the binding and
+ − 15
the body.
1169
+ − 16
+ − 17
Eg:
+ − 18
nominal_datatype
+ − 19
+ − 20
C1
+ − 21
| C2 x y z bind x in z
1172
+ − 22
| C3 x y z bind f x in z bind g y in z
1169
+ − 23
+ − 24
yields:
1172
+ − 25
[
+ − 26
[],
1358
+ − 27
[(NONE, 0, 2)],
+ − 28
[(SOME (Const f), 0, 2), (Some (Const g), 1, 2)]]
1185
+ − 29
1358
+ − 30
A SOME binding has to have a function which takes an appropriate
+ − 31
argument and returns an atom set. A NONE binding has to be on an
+ − 32
argument that is an atom or an atom set.
1505
+ − 33
*)
1185
+ − 34
1505
+ − 35
(*
1510
+ − 36
An overview of the generation of free variables:
1505
+ − 37
+ − 38
1) fv_bn functions are generated only for the non-recursive binds.
+ − 39
+ − 40
An fv_bn for a constructor is a union of values for the arguments:
1191
+ − 41
1505
+ − 42
For an argument x that is in the bn function
+ − 43
- if it is a recursive argument bn' we return: fv_bn' x
+ − 44
- otherwise empty
+ − 45
+ − 46
For an argument x that is not in the bn function
+ − 47
- for atom we return: {atom x}
+ − 48
- for atom set we return: atom ` x
+ − 49
- for a recursive call to type ty' we return: fv_ty' x
+ − 50
with fv of the appropriate type
+ − 51
- otherwise empty
+ − 52
1514
+ − 53
2) fv_ty functions generated for all types being defined:
1191
+ − 54
1514
+ − 55
fv_ty for a constructor is a union of values for the arguments.
1505
+ − 56
1508
+ − 57
For an argument that is bound in a shallow binding we return empty.
+ − 58
+ − 59
For an argument x that bound in a non-recursive deep binding
1505
+ − 60
we return: fv_bn x.
+ − 61
+ − 62
Otherwise we return the free variables of the argument minus the
+ − 63
bound variables of the argument.
1358
+ − 64
1505
+ − 65
The free variables for an argument x are:
+ − 66
- for an atom: {atom x}
+ − 67
- for atom set: atom ` x
+ − 68
- for recursive call to type ty' return: fv_ty' x
+ − 69
- for nominal datatype ty' return: fv_ty' x
+ − 70
+ − 71
The bound variables are a union of results of all bindings that
+ − 72
involve the given argument. For a paricular binding:
+ − 73
+ − 74
- for a binding function bn: bn x
+ − 75
- for a recursive argument of type ty': fv_fy' x
+ − 76
- for nominal datatype ty' return: fv_ty' x
1169
+ − 77
*)
+ − 78
1510
+ − 79
(*
+ − 80
An overview of the generation of alpha-equivalence:
1513
+ − 81
+ − 82
1) alpha_bn relations are generated for binding functions.
+ − 83
+ − 84
An alpha_bn for a constructor is true if a conjunction of
+ − 85
propositions for each argument holds.
+ − 86
+ − 87
For an argument a proposition is build as follows from
+ − 88
th:
+ − 89
+ − 90
- for a recursive argument in the bn function, we return: alpha_bn argl argr
+ − 91
- for a recursive argument for type ty not in bn, we return: alpha_ty argl argr
+ − 92
- for other arguments in the bn function we return: True
+ − 93
- for other arguments not in the bn function we return: argl = argr
+ − 94
1514
+ − 95
2) alpha_ty relations are generated for all the types being defined:
+ − 96
1516
+ − 97
For each constructor we gather all the arguments that are bound,
+ − 98
and for each of those we add a permutation. We associate those
+ − 99
permutations with the bindings. Note that two bindings can have
+ − 100
the same permutation if the arguments being bound are the same.
1514
+ − 101
1516
+ − 102
An alpha_ty for a constructor is true if there exist permutations
+ − 103
as above such that a conjunction of propositions for all arguments holds.
1514
+ − 104
+ − 105
For an argument we allow bindings where only one of the following
+ − 106
holds:
+ − 107
+ − 108
- Argument is bound in some shallow bindings: We return true
1516
+ − 109
- Argument of type ty is bound recursively in some other
+ − 110
arguments [i1, .. in] with one binding function bn.
+ − 111
We return:
+ − 112
+ − 113
(bn argl, (argl, argl_i1, ..., argl_in)) \<approx>gen
+ − 114
\<lambda>(argl,argl1,..,argln) (argr,argr1,..,argrn).
+ − 115
(alpha_ty argl argr) \<and> (alpha_i1 argl1 argr1) \<and> .. \<and> (alpha_in argln argrn)
+ − 116
\<lambda>(arg,arg1,..,argn). (fv_ty arg) \<union> (fv_i1 arg1) \<union> .. \<union> (fv_in argn)
+ − 117
pi
+ − 118
(bn argr, (argr, argr_i1, ..., argr_in))
+ − 119
1514
+ − 120
- Argument is bound in some deep non-recursive bindings.
+ − 121
We return: alpha_bn argl argr
1516
+ − 122
- Argument of type ty has some shallow bindings [b1..bn] and/or
+ − 123
non-recursive bindings [f1 a1, .., fm am], where the bindings
+ − 124
have the permutations p1..pl. We return:
+ − 125
+ − 126
(b1l \<union>..\<union> bnl \<union> f1 a1l \<union>..\<union> fn anl, argl) \<approx>gen
+ − 127
alpha_ty fv_ty (p1 +..+ pl)
+ − 128
(b1r \<union>..\<union> bnr \<union> f1 a1r \<union>..\<union> fn anr, argr)
+ − 129
1514
+ − 130
- Argument has some recursive bindings. The bindings were
+ − 131
already treated in 2nd case so we return: True
+ − 132
- Argument has no bindings and is not bound.
+ − 133
If it is recursive for type ty, we return: alpha_ty argl argr
+ − 134
Otherwise we return: argl = argr
+ − 135
1510
+ − 136
*)
+ − 137
1362
+ − 138
ML {*
+ − 139
fun is_atom thy typ =
+ − 140
Sign.of_sort thy (typ, @{sort at})
1366
+ − 141
+ − 142
fun is_atom_set thy (Type ("fun", [t, @{typ bool}])) = is_atom thy t
+ − 143
| is_atom_set thy _ = false;
1534
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 144
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 145
fun is_atom_fset thy (Type ("FSet.fset", [t])) = is_atom thy t
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 146
| is_atom_fset thy _ = false;
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 147
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 148
val fset_to_set = @{term "fset_to_set :: atom fset \<Rightarrow> atom set"}
1362
+ − 149
*}
+ − 150
+ − 151
1366
+ − 152
1534
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 153
1358
+ − 154
(* Like map2, only if the second list is empty passes empty lists insted of error *)
1302
+ − 155
ML {*
+ − 156
fun map2i _ [] [] = []
+ − 157
| map2i f (x :: xs) (y :: ys) = f x y :: map2i f xs ys
+ − 158
| map2i f (x :: xs) [] = f x [] :: map2i f xs []
+ − 159
| map2i _ _ _ = raise UnequalLengths;
+ − 160
*}
+ − 161
1358
+ − 162
(* Finds bindings with the same function and binding, and gathers all
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 163
bodys for such pairs
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 164
*)
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
+ − 165
ML {*
1357
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 166
fun gather_binds binds =
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 167
let
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 168
fun gather_binds_cons binds =
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 169
let
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 170
val common = map (fn (f, bi, _) => (f, bi)) binds
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 171
val nodups = distinct (op =) common
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 172
fun find_bodys (sf, sbi) =
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 173
filter (fn (f, bi, _) => f = sf andalso bi = sbi) binds
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 174
val bodys = map ((map (fn (_, _, bo) => bo)) o find_bodys) nodups
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 175
in
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 176
nodups ~~ bodys
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 177
end
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 178
in
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 179
map (map gather_binds_cons) binds
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 180
end
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 181
*}
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 182
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 183
ML {*
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 184
fun un_gather_binds_cons binds =
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 185
flat (map (fn (((f, bi), bos), pi) => map (fn bo => ((f, bi, bo), pi)) bos) binds)
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 186
*}
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 187
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 188
ML {*
1175
+ − 189
open Datatype_Aux; (* typ_of_dtyp, DtRec, ... *);
1178
+ − 190
(* TODO: It is the same as one in 'nominal_atoms' *)
1175
+ − 191
fun mk_atom ty = Const (@{const_name atom}, ty --> @{typ atom});
+ − 192
val noatoms = @{term "{} :: atom set"};
+ − 193
fun mk_single_atom x = HOLogic.mk_set @{typ atom} [mk_atom (type_of x) $ x];
+ − 194
fun mk_union sets =
+ − 195
fold (fn a => fn b =>
+ − 196
if a = noatoms then b else
+ − 197
if b = noatoms then a else
1323
+ − 198
if a = b then a else
1325
+ − 199
HOLogic.mk_binop @{const_name sup} (a, b)) (rev sets) noatoms;
+ − 200
val mk_inter = foldr1 (HOLogic.mk_binop @{const_name inf})
1288
+ − 201
fun mk_conjl props =
+ − 202
fold (fn a => fn b =>
+ − 203
if a = @{term True} then b else
+ − 204
if b = @{term True} then a else
1428
+ − 205
HOLogic.mk_conj (a, b)) (rev props) @{term True};
1175
+ − 206
fun mk_diff a b =
+ − 207
if b = noatoms then a else
+ − 208
if b = a then noatoms else
+ − 209
HOLogic.mk_binop @{const_name minus} (a, b);
1534
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 210
fun mk_atom_set t =
1185
+ − 211
let
+ − 212
val ty = fastype_of t;
+ − 213
val atom_ty = HOLogic.dest_setT ty --> @{typ atom};
+ − 214
val img_ty = atom_ty --> ty --> @{typ "atom set"};
+ − 215
in
+ − 216
(Const (@{const_name image}, img_ty) $ Const (@{const_name atom}, atom_ty) $ t)
+ − 217
end;
1534
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 218
fun mk_atom_fset t =
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 219
let
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 220
val ty = fastype_of t;
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 221
val atom_ty = dest_fsetT ty --> @{typ atom};
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 222
val fmap_ty = atom_ty --> ty --> @{typ "atom fset"};
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 223
in
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 224
fset_to_set $ ((Const (@{const_name fmap}, fmap_ty) $ Const (@{const_name atom}, atom_ty) $ t))
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 225
end;
1193
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 226
(* Similar to one in USyntax *)
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 227
fun mk_pair (fst, snd) =
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 228
let val ty1 = fastype_of fst
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 229
val ty2 = fastype_of snd
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 230
val c = HOLogic.pair_const ty1 ty2
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 231
in c $ fst $ snd
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 232
end;
1468
+ − 233
*}
1193
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 234
1468
+ − 235
(* Given [fv1, fv2, fv3] creates %(x, y, z). fv1 x u fv2 y u fv3 z *)
+ − 236
ML {*
+ − 237
fun mk_compound_fv fvs =
+ − 238
let
+ − 239
val nos = (length fvs - 1) downto 0;
+ − 240
val fvs_applied = map (fn (fv, no) => fv $ Bound no) (fvs ~~ nos);
+ − 241
val fvs_union = mk_union fvs_applied;
+ − 242
val (tyh :: tys) = rev (map (domain_type o fastype_of) fvs);
+ − 243
fun fold_fun ty t = HOLogic.mk_split (Abs ("", ty, t))
+ − 244
in
+ − 245
fold fold_fun tys (Abs ("", tyh, fvs_union))
+ − 246
end;
1175
+ − 247
*}
+ − 248
1468
+ − 249
ML {* @{term "\<lambda>(x, y, z). \<lambda>(x', y', z'). R x x' \<and> R2 y y' \<and> R3 z z'"} *}
+ − 250
+ − 251
(* Given [R1, R2, R3] creates %(x,x'). %(y,y'). %(z,z'). R x x' \<and> R y y' \<and> R z z' *)
+ − 252
ML {*
+ − 253
fun mk_compound_alpha Rs =
+ − 254
let
+ − 255
val nos = (length Rs - 1) downto 0;
+ − 256
val nos2 = (2 * length Rs - 1) downto length Rs;
+ − 257
val Rs_applied = map (fn (R, (no2, no)) => R $ Bound no2 $ Bound no) (Rs ~~ (nos2 ~~ nos));
+ − 258
val Rs_conj = mk_conjl Rs_applied;
+ − 259
val (tyh :: tys) = rev (map (domain_type o fastype_of) Rs);
+ − 260
fun fold_fun ty t = HOLogic.mk_split (Abs ("", ty, t))
+ − 261
val abs_rhs = fold fold_fun tys (Abs ("", tyh, Rs_conj))
+ − 262
in
+ − 263
fold fold_fun tys (Abs ("", tyh, abs_rhs))
+ − 264
end;
+ − 265
*}
+ − 266
+ − 267
ML {* cterm_of @{theory} (mk_compound_alpha [@{term "R :: 'a \<Rightarrow> 'a \<Rightarrow> bool"}, @{term "R2 :: 'b \<Rightarrow> 'b \<Rightarrow> bool"}, @{term "R3 :: 'b \<Rightarrow> 'b \<Rightarrow> bool"}]) *}
+ − 268
1288
+ − 269
ML {* fun add_perm (p1, p2) = Const(@{const_name plus}, @{typ "perm \<Rightarrow> perm \<Rightarrow> perm"}) $ p1 $ p2 *}
+ − 270
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 271
ML {*
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 272
fun non_rec_binds l =
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 273
let
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 274
fun is_non_rec (SOME (f, false), _, _) = SOME f
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 275
| is_non_rec _ = NONE
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 276
in
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 277
distinct (op =) (map_filter is_non_rec (flat (flat l)))
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 278
end
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 279
*}
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 280
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 281
(* We assume no bindings in the type on which bn is defined *)
1505
+ − 282
(* TODO: currently works only with current fv_bn function *)
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 283
ML {*
1379
+ − 284
fun fv_bn thy (dt_info : Datatype_Aux.info) fv_frees (bn, ith_dtyp, args_in_bns) =
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 285
let
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 286
val {descr, sorts, ...} = dt_info;
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 287
fun nth_dtyp i = typ_of_dtyp descr sorts (DtRec i);
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 288
val fvbn_name = "fv_" ^ (Long_Name.base_name (fst (dest_Const bn)));
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 289
val fvbn = Free (fvbn_name, fastype_of (nth fv_frees ith_dtyp));
1379
+ − 290
fun fv_bn_constr (cname, dts) args_in_bn =
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 291
let
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 292
val Ts = map (typ_of_dtyp descr sorts) dts;
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 293
val names = Datatype_Prop.make_tnames Ts;
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 294
val args = map Free (names ~~ Ts);
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 295
val c = Const (cname, Ts ---> (nth_dtyp ith_dtyp));
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 296
fun fv_arg ((dt, x), arg_no) =
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 297
let
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 298
val ty = fastype_of x
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 299
in
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 300
if arg_no mem args_in_bn then
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 301
(if is_rec_type dt then
1415
+ − 302
(if body_index dt = ith_dtyp then fvbn $ x else error "fv_bn: recursive argument, but wrong datatype.")
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 303
else @{term "{} :: atom set"}) else
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 304
if is_atom thy ty then mk_single_atom x else
1534
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 305
if is_atom_set thy ty then mk_atom_set x else
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 306
if is_atom_fset thy ty then mk_atom_fset x else
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 307
if is_rec_type dt then nth fv_frees (body_index dt) $ x else
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 308
@{term "{} :: atom set"}
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 309
end;
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 310
val arg_nos = 0 upto (length dts - 1)
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 311
in
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 312
HOLogic.mk_Trueprop (HOLogic.mk_eq
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 313
(fvbn $ list_comb (c, args), mk_union (map fv_arg (dts ~~ args ~~ arg_nos))))
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 314
end;
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 315
val (_, (_, _, constrs)) = nth descr ith_dtyp;
1379
+ − 316
val eqs = map2i fv_bn_constr constrs args_in_bns
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 317
in
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 318
((bn, fvbn), (fvbn_name, eqs))
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 319
end
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 320
*}
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 321
1385
+ − 322
ML {*
+ − 323
fun alpha_bn thy (dt_info : Datatype_Aux.info) alpha_frees ((bn, ith_dtyp, args_in_bns), is_rec) =
+ − 324
let
+ − 325
val {descr, sorts, ...} = dt_info;
+ − 326
fun nth_dtyp i = typ_of_dtyp descr sorts (DtRec i);
+ − 327
val alpha_bn_name = "alpha_" ^ (Long_Name.base_name (fst (dest_Const bn)));
1457
91fe914e1bef
alpha_bn doesn't need the permutation in non-recursive case.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 328
val alpha_bn_type =
1462
+ − 329
(*if is_rec then @{typ perm} --> nth_dtyp ith_dtyp --> nth_dtyp ith_dtyp --> @{typ bool} else*)
+ − 330
nth_dtyp ith_dtyp --> nth_dtyp ith_dtyp --> @{typ bool};
1385
+ − 331
val alpha_bn_free = Free(alpha_bn_name, alpha_bn_type);
1457
91fe914e1bef
alpha_bn doesn't need the permutation in non-recursive case.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 332
val pi = Free("pi", @{typ perm})
1386
+ − 333
fun alpha_bn_constr (cname, dts) args_in_bn =
+ − 334
let
+ − 335
val Ts = map (typ_of_dtyp descr sorts) dts;
+ − 336
val names = Name.variant_list ["pi"] (Datatype_Prop.make_tnames Ts);
+ − 337
val names2 = Name.variant_list ("pi" :: names) (Datatype_Prop.make_tnames Ts);
+ − 338
val args = map Free (names ~~ Ts);
+ − 339
val args2 = map Free (names2 ~~ Ts);
+ − 340
val c = Const (cname, Ts ---> (nth_dtyp ith_dtyp));
1387
+ − 341
val rhs = HOLogic.mk_Trueprop
1462
+ − 342
(alpha_bn_free $ (list_comb (c, args)) $ (list_comb (c, args2)));
1387
+ − 343
fun lhs_arg ((dt, arg_no), (arg, arg2)) =
1388
+ − 344
let
+ − 345
val argty = fastype_of arg;
+ − 346
val permute = Const (@{const_name permute}, @{typ perm} --> argty --> argty);
+ − 347
in
+ − 348
if is_rec_type dt then
1462
+ − 349
if arg_no mem args_in_bn then alpha_bn_free $ arg $ arg2
+ − 350
else (nth alpha_frees (body_index dt)) $ arg $ arg2
1388
+ − 351
else
+ − 352
if arg_no mem args_in_bn then @{term True}
1462
+ − 353
else HOLogic.mk_eq (arg, arg2)
1388
+ − 354
end
1387
+ − 355
val arg_nos = 0 upto (length dts - 1)
+ − 356
val lhss = mk_conjl (map lhs_arg (dts ~~ arg_nos ~~ (args ~~ args2)))
+ − 357
val eq = Logic.mk_implies (HOLogic.mk_Trueprop lhss, rhs)
1386
+ − 358
in
1387
+ − 359
eq
1386
+ − 360
end
1385
+ − 361
val (_, (_, _, constrs)) = nth descr ith_dtyp;
1386
+ − 362
val eqs = map2i alpha_bn_constr constrs args_in_bns
1385
+ − 363
in
1389
+ − 364
((bn, alpha_bn_free), (alpha_bn_name, eqs))
1385
+ − 365
end
+ − 366
*}
+ − 367
1397
+ − 368
(* Checks that a list of bindings contains only compatible ones *)
+ − 369
ML {*
+ − 370
fun bns_same l =
+ − 371
length (distinct (op =) (map (fn ((b, _, _), _) => b) l)) = 1
+ − 372
*}
+ − 373
1206
+ − 374
(* TODO: Notice datatypes without bindings and replace alpha with equality *)
1175
+ − 375
ML {*
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 376
fun define_fv_alpha (dt_info : Datatype_Aux.info) bindsall bns lthy =
1178
+ − 377
let
1366
+ − 378
val thy = ProofContext.theory_of lthy;
1277
+ − 379
val {descr, sorts, ...} = dt_info;
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
+ − 380
fun nth_dtyp i = typ_of_dtyp descr sorts (DtRec i);
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
+ − 381
val fv_names = Datatype_Prop.indexify_names (map (fn (i, _) =>
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
+ − 382
"fv_" ^ name_of_typ (nth_dtyp i)) descr);
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
+ − 383
val fv_types = map (fn (i, _) => nth_dtyp i --> @{typ "atom set"}) descr;
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
+ − 384
val fv_frees = map Free (fv_names ~~ fv_types);
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 385
val nr_bns = non_rec_binds bindsall;
1464
1850361efb8f
Revert 7c8cd6eae8e2, now all proofs in Term5 go through, both recursive and not.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 386
val rel_bns = filter (fn (bn, _, _) => bn mem nr_bns) bns;
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 387
val (bn_fv_bns, fv_bn_names_eqs) = split_list (map (fv_bn thy dt_info fv_frees) rel_bns);
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 388
val (fv_bn_names, fv_bn_eqs) = split_list fv_bn_names_eqs;
1193
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 389
val alpha_names = Datatype_Prop.indexify_names (map (fn (i, _) =>
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 390
"alpha_" ^ name_of_typ (nth_dtyp i)) descr);
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 391
val alpha_types = map (fn (i, _) => nth_dtyp i --> nth_dtyp i --> @{typ bool}) descr;
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 392
val alpha_frees = map Free (alpha_names ~~ alpha_types);
1385
+ − 393
(* We assume that a bn is either recursive or not *)
+ − 394
val bns_rec = map (fn (bn, _, _) => not (bn mem nr_bns)) bns;
1389
+ − 395
val (bn_alpha_bns, alpha_bn_names_eqs) = split_list (map (alpha_bn thy dt_info alpha_frees) (bns ~~ bns_rec))
+ − 396
val (alpha_bn_names, alpha_bn_eqs) = split_list alpha_bn_names_eqs;
+ − 397
val alpha_bn_frees = map snd bn_alpha_bns;
+ − 398
val alpha_bn_types = map fastype_of alpha_bn_frees;
1288
+ − 399
fun fv_alpha_constr ith_dtyp (cname, dts) bindcs =
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
+ − 400
let
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
+ − 401
val Ts = map (typ_of_dtyp descr sorts) dts;
1288
+ − 402
val bindslen = length bindcs
1323
+ − 403
val pi_strs_same = replicate bindslen "pi"
+ − 404
val pi_strs = Name.variant_list [] pi_strs_same;
1288
+ − 405
val pis = map (fn ps => Free (ps, @{typ perm})) pi_strs;
1357
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 406
val bind_pis_gath = bindcs ~~ pis;
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 407
val bind_pis = un_gather_binds_cons bind_pis_gath;
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 408
val bindcs = map fst bind_pis;
1288
+ − 409
val names = Name.variant_list pi_strs (Datatype_Prop.make_tnames Ts);
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
+ − 410
val args = map Free (names ~~ Ts);
1288
+ − 411
val names2 = Name.variant_list (pi_strs @ names) (Datatype_Prop.make_tnames Ts);
1193
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 412
val args2 = map Free (names2 ~~ Ts);
1288
+ − 413
val c = Const (cname, Ts ---> (nth_dtyp ith_dtyp));
+ − 414
val fv_c = nth fv_frees ith_dtyp;
+ − 415
val alpha = nth alpha_frees ith_dtyp;
+ − 416
val arg_nos = 0 upto (length dts - 1)
+ − 417
fun fv_bind args (NONE, i, _) =
1177
+ − 418
if is_rec_type (nth dts i) then (nth fv_frees (body_index (nth dts i))) $ (nth args i) else
1366
+ − 419
if ((is_atom thy) o fastype_of) (nth args i) then mk_single_atom (nth args i) else
1534
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 420
if ((is_atom_set thy) o fastype_of) (nth args i) then mk_atom_set (nth args i) else
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 421
if ((is_atom_fset thy) o fastype_of) (nth args i) then mk_atom_fset (nth args i) else
1366
+ − 422
(* TODO we do not know what to do with non-atomizable things *)
+ − 423
@{term "{} :: atom set"}
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 424
| fv_bind args (SOME (f, _), i, _) = f $ (nth args i);
1288
+ − 425
fun fv_binds args relevant = mk_union (map (fv_bind args) relevant)
1464
1850361efb8f
Revert 7c8cd6eae8e2, now all proofs in Term5 go through, both recursive and not.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 426
fun find_nonrec_binder j (SOME (f, false), i, _) = if i = j then SOME f else NONE
1850361efb8f
Revert 7c8cd6eae8e2, now all proofs in Term5 go through, both recursive and not.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 427
| find_nonrec_binder _ _ = NONE
1288
+ − 428
fun fv_arg ((dt, x), arg_no) =
1464
1850361efb8f
Revert 7c8cd6eae8e2, now all proofs in Term5 go through, both recursive and not.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 429
case get_first (find_nonrec_binder arg_no) bindcs of
1850361efb8f
Revert 7c8cd6eae8e2, now all proofs in Term5 go through, both recursive and not.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 430
SOME f =>
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 431
(case get_first (fn (x, y) => if x = f then SOME y else NONE) bn_fv_bns of
1464
1850361efb8f
Revert 7c8cd6eae8e2, now all proofs in Term5 go through, both recursive and not.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 432
SOME fv_bn => fv_bn $ x
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 433
| NONE => error "bn specified in a non-rec binding but not in bn list")
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 434
| NONE =>
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 435
let
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 436
val arg =
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 437
if is_rec_type dt then nth fv_frees (body_index dt) $ x else
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 438
if ((is_atom thy) o fastype_of) x then mk_single_atom x else
1534
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 439
if ((is_atom_set thy) o fastype_of) x then mk_atom_set x else
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 440
if ((is_atom_fset thy) o fastype_of) x then mk_atom_fset x else
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 441
(* TODO we do not know what to do with non-atomizable things *)
1454
7c8cd6eae8e2
FV_bn generated for recursive functions as well, and used in main fv for bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 442
@{term "{} :: atom set"};
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 443
(* If i = j then we generate it only once *)
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 444
val relevant = filter (fn (_, i, j) => ((i = arg_no) orelse (j = arg_no))) bindcs;
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 445
val sub = fv_binds args relevant
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 446
in
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 447
mk_diff arg sub
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 448
end;
1193
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 449
val fv_eq = HOLogic.mk_Trueprop (HOLogic.mk_eq
1288
+ − 450
(fv_c $ list_comb (c, args), mk_union (map fv_arg (dts ~~ args ~~ arg_nos))))
1193
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 451
val alpha_rhs =
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 452
HOLogic.mk_Trueprop (alpha $ (list_comb (c, args)) $ (list_comb (c, args2)));
1288
+ − 453
fun alpha_arg ((dt, arg_no), (arg, arg2)) =
+ − 454
let
1383
+ − 455
val rel_in_simp_binds = filter (fn ((NONE, i, _), _) => i = arg_no | _ => false) bind_pis;
+ − 456
val rel_in_comp_binds = filter (fn ((SOME _, i, _), _) => i = arg_no | _ => false) bind_pis;
1468
+ − 457
val rel_has_binds = filter (fn ((NONE, _, j), _) => j = arg_no
+ − 458
| ((SOME (_, false), _, j), _) => j = arg_no
+ − 459
| _ => false) bind_pis;
1472
+ − 460
val rel_has_rec_binds = filter
+ − 461
(fn ((SOME (_, true), _, j), _) => j = arg_no | _ => false) bind_pis;
1288
+ − 462
in
1472
+ − 463
case (rel_in_simp_binds, rel_in_comp_binds, rel_has_binds, rel_has_rec_binds) of
+ − 464
([], [], [], []) =>
1383
+ − 465
if is_rec_type dt then (nth alpha_frees (body_index dt) $ arg $ arg2)
+ − 466
else (HOLogic.mk_eq (arg, arg2))
1472
+ − 467
| (_, [], [], []) => @{term True}
+ − 468
| ([], [], [], _) => @{term True}
+ − 469
| ([], ((((SOME (bn, is_rec)), _, _), pi) :: _), [], []) =>
1462
+ − 470
if not (bns_same rel_in_comp_binds) then error "incompatible bindings for an argument" else
+ − 471
if is_rec then
+ − 472
let
+ − 473
val (rbinds, rpis) = split_list rel_in_comp_binds
1468
+ − 474
val bound_in_nos = map (fn (_, _, i) => i) rbinds
+ − 475
val bound_in_ty_nos = map (fn i => body_index (nth dts i)) bound_in_nos;
+ − 476
val bound_args = arg :: map (nth args) bound_in_nos;
+ − 477
val bound_args2 = arg2 :: map (nth args2) bound_in_nos;
+ − 478
fun bound_in args (_, _, i) = nth args i;
1462
+ − 479
val lhs_binds = fv_binds args rbinds
1468
+ − 480
val lhs_arg = foldr1 HOLogic.mk_prod bound_args
+ − 481
val lhs = mk_pair (lhs_binds, lhs_arg);
1462
+ − 482
val rhs_binds = fv_binds args2 rbinds;
1468
+ − 483
val rhs_arg = foldr1 HOLogic.mk_prod bound_args2;
+ − 484
val rhs = mk_pair (rhs_binds, rhs_arg);
+ − 485
val fvs = map (nth fv_frees) ((body_index dt) :: bound_in_ty_nos);
+ − 486
val fv = mk_compound_fv fvs;
+ − 487
val alphas = map (nth alpha_frees) ((body_index dt) :: bound_in_ty_nos);
+ − 488
val alpha = mk_compound_alpha alphas;
1462
+ − 489
val pi = foldr1 add_perm (distinct (op =) rpis);
+ − 490
val alpha_gen_pre = Const (@{const_name alpha_gen}, dummyT) $ lhs $ alpha $ fv $ pi $ rhs;
+ − 491
val alpha_gen = Syntax.check_term lthy alpha_gen_pre
+ − 492
in
+ − 493
alpha_gen
+ − 494
end
+ − 495
else
+ − 496
let
+ − 497
val alpha_bn_const =
+ − 498
nth alpha_bn_frees (find_index (fn (b, _, _) => b = bn) bns)
+ − 499
in
+ − 500
alpha_bn_const $ arg $ arg2
+ − 501
end
1472
+ − 502
| ([], [], relevant, []) =>
1383
+ − 503
let
1288
+ − 504
val (rbinds, rpis) = split_list relevant
+ − 505
val lhs_binds = fv_binds args rbinds
+ − 506
val lhs = mk_pair (lhs_binds, arg);
+ − 507
val rhs_binds = fv_binds args2 rbinds;
+ − 508
val rhs = mk_pair (rhs_binds, arg2);
+ − 509
val alpha = nth alpha_frees (body_index dt);
+ − 510
val fv = nth fv_frees (body_index dt);
1359
+ − 511
val pi = foldr1 add_perm (distinct (op =) rpis);
1288
+ − 512
val alpha_gen_pre = Const (@{const_name alpha_gen}, dummyT) $ lhs $ alpha $ fv $ pi $ rhs;
1325
+ − 513
val alpha_gen = Syntax.check_term lthy alpha_gen_pre
1288
+ − 514
in
1357
42b7abf779ec
Gather bindings with same binder, and generate only one permutation for them.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 515
alpha_gen
1383
+ − 516
end
1385
+ − 517
| _ => error "Fv.alpha: not supported binding structure"
1288
+ − 518
end
+ − 519
val alphas = map alpha_arg (dts ~~ arg_nos ~~ (args ~~ args2))
+ − 520
val alpha_lhss = mk_conjl alphas
+ − 521
val alpha_lhss_ex =
+ − 522
fold (fn pi_str => fn t => HOLogic.mk_exists (pi_str, @{typ perm}, t)) pi_strs alpha_lhss
+ − 523
val alpha_eq = Logic.mk_implies (HOLogic.mk_Trueprop alpha_lhss_ex, alpha_rhs)
1173
+ − 524
in
1193
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 525
(fv_eq, alpha_eq)
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
+ − 526
end;
1302
+ − 527
fun fv_alpha_eq (i, (_, _, constrs)) binds = map2i (fv_alpha_constr i) constrs binds;
1376
+ − 528
val fveqs_alphaeqs = map2i fv_alpha_eq descr (gather_binds bindsall)
+ − 529
val (fv_eqs_perfv, alpha_eqs) = apsnd flat (split_list (map split_list fveqs_alphaeqs))
+ − 530
val rel_bns_nos = map (fn (_, i, _) => i) rel_bns;
1385
+ − 531
fun filter_fun (_, b) = b mem rel_bns_nos;
1376
+ − 532
val all_fvs = (fv_names ~~ fv_eqs_perfv) ~~ (0 upto (length fv_names - 1))
+ − 533
val (fv_names_fst, fv_eqs_fst) = apsnd flat (split_list (map fst (filter_out filter_fun all_fvs)))
+ − 534
val (fv_names_snd, fv_eqs_snd) = apsnd flat (split_list (map fst (filter filter_fun all_fvs)))
+ − 535
val fv_eqs_all = fv_eqs_fst @ (flat fv_bn_eqs);
+ − 536
val fv_names_all = fv_names_fst @ fv_bn_names;
1193
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 537
val add_binds = map (fn x => (Attrib.empty_binding, x))
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 538
(* Function_Fun.add_fun Function_Common.default_config ... true *)
1193
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 539
val (fvs, lthy') = (Primrec.add_primrec
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 540
(map (fn s => (Binding.name s, NONE, NoSyn)) fv_names_all) (add_binds fv_eqs_all) lthy)
1376
+ − 541
val (fvs2, lthy'') =
+ − 542
if fv_eqs_snd = [] then (([], []), lthy') else
+ − 543
(Primrec.add_primrec
+ − 544
(map (fn s => (Binding.name s, NONE, NoSyn)) fv_names_snd) (add_binds fv_eqs_snd) lthy')
+ − 545
val (alphas, lthy''') = (Inductive.add_inductive_i
1325
+ − 546
{quiet_mode = true, verbose = false, alt_name = Binding.empty,
1193
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 547
coind = false, no_elim = false, no_ind = false, skip_mono = true, fork_mono = false}
1389
+ − 548
(map2 (fn x => fn y => ((Binding.name x, y), NoSyn)) (alpha_names @ alpha_bn_names)
+ − 549
(alpha_types @ alpha_bn_types)) []
+ − 550
(add_binds (alpha_eqs @ flat alpha_bn_eqs)) [] lthy'')
1385
+ − 551
val all_fvs = (fst fvs @ fst fvs2, snd fvs @ snd fvs2)
1178
+ − 552
in
1385
+ − 553
((all_fvs, alphas), lthy''')
1178
+ − 554
end
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
+ − 555
*}
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
+ − 556
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 557
(*
1178
+ − 558
atom_decl name
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 559
datatype lam =
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 560
VAR "name"
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 561
| APP "lam" "lam"
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 562
| LET "bp" "lam"
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 563
and bp =
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 564
BP "name" "lam"
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 565
primrec
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 566
bi::"bp \<Rightarrow> atom set"
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 567
where
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 568
"bi (BP x t) = {atom x}"
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 569
setup {* snd o define_raw_perms (Datatype.the_info @{theory} "Fv.lam") 2 *}
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 570
local_setup {*
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 571
snd o define_fv_alpha (Datatype.the_info @{theory} "Fv.lam")
1389
+ − 572
[[[], [], [(SOME (@{term bi}, true), 0, 1)]], [[]]] [(@{term bi}, 1, [[0]])] *}
1375
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 573
print_theorems
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 574
*)
aa787c9b6955
A version of Fv that takes into account recursive and non-recursive bindings.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 575
1389
+ − 576
(*atom_decl name
1178
+ − 577
datatype rtrm1 =
+ − 578
rVr1 "name"
+ − 579
| rAp1 "rtrm1" "rtrm1"
+ − 580
| rLm1 "name" "rtrm1" --"name is bound in trm1"
+ − 581
| rLt1 "bp" "rtrm1" "rtrm1" --"all variables in bp are bound in the 2nd trm1"
+ − 582
and bp =
+ − 583
BUnit
+ − 584
| BVr "name"
+ − 585
| BPr "bp" "bp"
1358
+ − 586
primrec
1178
+ − 587
bv1
+ − 588
where
+ − 589
"bv1 (BUnit) = {}"
+ − 590
| "bv1 (BVr x) = {atom x}"
+ − 591
| "bv1 (BPr bp1 bp2) = (bv1 bp1) \<union> (bv1 bp1)"
1389
+ − 592
setup {* snd o define_raw_perms (Datatype.the_info @{theory} "Fv.rtrm1") 2 *}
+ − 593
local_setup {*
+ − 594
snd o define_fv_alpha (Datatype.the_info @{theory} "Fv.rtrm1")
+ − 595
[[[], [], [(NONE, 0, 1)], [(SOME (@{term bv1}, false), 0, 2)]],
+ − 596
[[], [], []]] [(@{term bv1}, 1, [[], [0], [0, 1]])] *}
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
+ − 597
print_theorems
1193
a228acf2907e
Full alpha equivalence + testing in terms. Some differ but it seems the generated version is more correct.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 598
*)
1180
+ − 599
1389
+ − 600
(*
+ − 601
atom_decl name
+ − 602
datatype rtrm5 =
+ − 603
rVr5 "name"
+ − 604
| rLt5 "rlts" "rtrm5" --"bind (bv5 lts) in (rtrm5)"
+ − 605
and rlts =
+ − 606
rLnil
+ − 607
| rLcons "name" "rtrm5" "rlts"
+ − 608
primrec
+ − 609
rbv5
+ − 610
where
+ − 611
"rbv5 rLnil = {}"
+ − 612
| "rbv5 (rLcons n t ltl) = {atom n} \<union> (rbv5 ltl)"
+ − 613
setup {* snd o define_raw_perms (Datatype.the_info @{theory} "Fv.rtrm5") 2 *}
+ − 614
local_setup {* snd o define_fv_alpha (Datatype.the_info @{theory} "Fv.rtrm5")
+ − 615
[[[], [(SOME (@{term rbv5}, false), 0, 1)]], [[], []]] [(@{term rbv5}, 1, [[], [0, 2]])] *}
+ − 616
print_theorems
+ − 617
*)
1199
+ − 618
1196
+ − 619
ML {*
1199
+ − 620
fun alpha_inj_tac dist_inj intrs elims =
+ − 621
SOLVED' (asm_full_simp_tac (HOL_ss addsimps intrs)) ORELSE'
1216
+ − 622
(rtac @{thm iffI} THEN' RANGE [
1199
+ − 623
(eresolve_tac elims THEN_ALL_NEW
+ − 624
asm_full_simp_tac (HOL_ss addsimps dist_inj)
+ − 625
),
1216
+ − 626
asm_full_simp_tac (HOL_ss addsimps intrs)])
1199
+ − 627
*}
+ − 628
+ − 629
ML {*
+ − 630
fun build_alpha_inj_gl thm =
+ − 631
let
+ − 632
val prop = prop_of thm;
+ − 633
val concl = HOLogic.dest_Trueprop (Logic.strip_imp_concl prop);
+ − 634
val hyps = map HOLogic.dest_Trueprop (Logic.strip_imp_prems prop);
+ − 635
fun list_conj l = foldr1 HOLogic.mk_conj l;
+ − 636
in
+ − 637
if hyps = [] then concl
+ − 638
else HOLogic.mk_eq (concl, list_conj hyps)
+ − 639
end;
+ − 640
*}
+ − 641
+ − 642
ML {*
+ − 643
fun build_alpha_inj intrs dist_inj elims ctxt =
1196
+ − 644
let
1199
+ − 645
val ((_, thms_imp), ctxt') = Variable.import false intrs ctxt;
+ − 646
val gls = map (HOLogic.mk_Trueprop o build_alpha_inj_gl) thms_imp;
+ − 647
fun tac _ = alpha_inj_tac dist_inj intrs elims 1;
+ − 648
val thms = map (fn gl => Goal.prove ctxt' [] [] gl tac) gls;
1196
+ − 649
in
1199
+ − 650
Variable.export ctxt' ctxt thms
1168
5c1e16806901
Code for generating the fv function, no bindings yet.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
+ − 651
end
1196
+ − 652
*}
+ − 653
1207
+ − 654
ML {*
1214
+ − 655
fun build_alpha_refl_gl alphas (x, y, z) =
1207
+ − 656
let
+ − 657
fun build_alpha alpha =
+ − 658
let
+ − 659
val ty = domain_type (fastype_of alpha);
1214
+ − 660
val var = Free(x, ty);
+ − 661
val var2 = Free(y, ty);
+ − 662
val var3 = Free(z, ty);
1209
+ − 663
val symp = HOLogic.mk_imp (alpha $ var $ var2, alpha $ var2 $ var);
+ − 664
val transp = HOLogic.mk_imp (alpha $ var $ var2,
1214
+ − 665
HOLogic.mk_all (z, ty,
1209
+ − 666
HOLogic.mk_imp (alpha $ var2 $ var3, alpha $ var $ var3)))
1207
+ − 667
in
1209
+ − 668
((alpha $ var $ var), (symp, transp))
1208
+ − 669
end;
1209
+ − 670
val (refl_eqs, eqs) = split_list (map build_alpha alphas)
+ − 671
val (sym_eqs, trans_eqs) = split_list eqs
+ − 672
fun conj l = @{term Trueprop} $ foldr1 HOLogic.mk_conj l
1207
+ − 673
in
1209
+ − 674
(conj refl_eqs, (conj sym_eqs, conj trans_eqs))
1196
+ − 675
end
1207
+ − 676
*}
+ − 677
1213
+ − 678
ML {*
1333
+ − 679
fun reflp_tac induct inj ctxt =
1213
+ − 680
rtac induct THEN_ALL_NEW
1333
+ − 681
simp_tac ((mk_minimal_ss ctxt) addsimps inj) THEN_ALL_NEW
+ − 682
split_conjs THEN_ALL_NEW REPEAT o rtac @{thm exI[of _ "0 :: perm"]}
+ − 683
THEN_ALL_NEW split_conjs THEN_ALL_NEW asm_full_simp_tac (HOL_ss addsimps
+ − 684
@{thms alpha_gen fresh_star_def fresh_zero_perm permute_zero ball_triv
1482
+ − 685
add_0_left supp_zero_perm Int_empty_left split_conv})
1213
+ − 686
*}
+ − 687
1333
+ − 688
1301
+ − 689
lemma exi_neg: "\<exists>(pi :: perm). P pi \<Longrightarrow> (\<And>(p :: perm). P p \<Longrightarrow> Q (- p)) \<Longrightarrow> \<exists>pi. Q pi"
+ − 690
apply (erule exE)
+ − 691
apply (rule_tac x="-pi" in exI)
+ − 692
by auto
+ − 693
1213
+ − 694
ML {*
1334
+ − 695
fun symp_tac induct inj eqvt ctxt =
+ − 696
ind_tac induct THEN_ALL_NEW
+ − 697
simp_tac ((mk_minimal_ss ctxt) addsimps inj) THEN_ALL_NEW split_conjs
+ − 698
THEN_ALL_NEW
+ − 699
REPEAT o etac @{thm exi_neg}
+ − 700
THEN_ALL_NEW
+ − 701
split_conjs THEN_ALL_NEW
+ − 702
asm_full_simp_tac (HOL_ss addsimps @{thms supp_minus_perm minus_add[symmetric]}) THEN_ALL_NEW
1487
+ − 703
TRY o (rtac @{thm alpha_gen_compose_sym2} ORELSE' rtac @{thm alpha_gen_compose_sym}) THEN_ALL_NEW
+ − 704
(asm_full_simp_tac (HOL_ss addsimps (eqvt @ all_eqvts ctxt)))
1213
+ − 705
*}
+ − 706
+ − 707
ML {*
1217
+ − 708
fun imp_elim_tac case_rules =
+ − 709
Subgoal.FOCUS (fn {concl, context, ...} =>
+ − 710
case term_of concl of
+ − 711
_ $ (_ $ asm $ _) =>
+ − 712
let
+ − 713
fun filter_fn case_rule = (
+ − 714
case Logic.strip_assums_hyp (prop_of case_rule) of
+ − 715
((_ $ asmc) :: _) =>
+ − 716
let
+ − 717
val thy = ProofContext.theory_of context
+ − 718
in
+ − 719
Pattern.matches thy (asmc, asm)
+ − 720
end
+ − 721
| _ => false)
+ − 722
val matching_rules = filter filter_fn case_rules
+ − 723
in
+ − 724
(rtac impI THEN' rotate_tac (~1) THEN' eresolve_tac matching_rules) 1
+ − 725
end
+ − 726
| _ => no_tac
+ − 727
)
+ − 728
*}
+ − 729
1301
+ − 730
+ − 731
lemma exi_sum: "\<exists>(pi :: perm). P pi \<Longrightarrow> \<exists>(pi :: perm). Q pi \<Longrightarrow> (\<And>(p :: perm) (pi :: perm). P p \<Longrightarrow> Q pi \<Longrightarrow> R (pi + p)) \<Longrightarrow> \<exists>pi. R pi"
+ − 732
apply (erule exE)+
+ − 733
apply (rule_tac x="pia + pi" in exI)
+ − 734
by auto
+ − 735
1217
+ − 736
ML {*
1339
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 737
fun is_ex (Const ("Ex", _) $ Abs _) = true
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 738
| is_ex _ = false;
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 739
*}
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 740
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 741
ML {*
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 742
fun eetac rule = Subgoal.FOCUS_PARAMS
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 743
(fn (focus) =>
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 744
let
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 745
val concl = #concl focus
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 746
val prems = Logic.strip_imp_prems (term_of concl)
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 747
val exs = filter (fn x => is_ex (HOLogic.dest_Trueprop x)) prems
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 748
val cexs = map (SOME o (cterm_of (ProofContext.theory_of (#context focus)))) exs
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 749
val thins = map (fn cex => Drule.instantiate' [] [cex] Drule.thin_rl) cexs
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 750
in
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 751
(etac rule THEN' RANGE[
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 752
atac,
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 753
eresolve_tac thins
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 754
]) 1
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 755
end
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 756
)
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 757
*}
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 758
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 759
ML {*
1217
+ − 760
fun transp_tac ctxt induct alpha_inj term_inj distinct cases eqvt =
1339
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 761
ind_tac induct THEN_ALL_NEW
1217
+ − 762
(TRY o rtac allI THEN' imp_elim_tac cases ctxt) THEN_ALL_NEW
1339
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 763
asm_full_simp_tac ((mk_minimal_ss ctxt) addsimps alpha_inj) THEN_ALL_NEW
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 764
split_conjs THEN_ALL_NEW REPEAT o (eetac @{thm exi_sum} ctxt) THEN_ALL_NEW split_conjs
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 765
THEN_ALL_NEW (asm_full_simp_tac (HOL_ss addsimps (term_inj @ distinct)))
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 766
THEN_ALL_NEW split_conjs THEN_ALL_NEW
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 767
TRY o (etac @{thm alpha_gen_compose_trans} THEN' RANGE[atac]) THEN_ALL_NEW
5256f256edd8
Comment out Weird and Phd until we have an idea how to handle multiple permutations. Transp that works for multiple existentials.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 768
(asm_full_simp_tac (HOL_ss addsimps (all_eqvts ctxt @ eqvt @ term_inj @ distinct)))
1213
+ − 769
*}
+ − 770
1215
+ − 771
lemma transp_aux:
+ − 772
"(\<And>xa ya. R xa ya \<longrightarrow> (\<forall>z. R ya z \<longrightarrow> R xa z)) \<Longrightarrow> transp R"
+ − 773
unfolding transp_def
+ − 774
by blast
+ − 775
+ − 776
ML {*
+ − 777
fun equivp_tac reflps symps transps =
+ − 778
simp_tac (HOL_ss addsimps @{thms equivp_reflp_symp_transp reflp_def symp_def})
1221
+ − 779
THEN' rtac conjI THEN' rtac allI THEN'
1215
+ − 780
resolve_tac reflps THEN'
1221
+ − 781
rtac conjI THEN' rtac allI THEN' rtac allI THEN'
1215
+ − 782
resolve_tac symps THEN'
+ − 783
rtac @{thm transp_aux} THEN' resolve_tac transps
+ − 784
*}
+ − 785
1213
+ − 786
ML {*
1214
+ − 787
fun build_equivps alphas term_induct alpha_induct term_inj alpha_inj distinct cases eqvt ctxt =
1213
+ − 788
let
1214
+ − 789
val ([x, y, z], ctxt') = Variable.variant_fixes ["x","y","z"] ctxt;
+ − 790
val (reflg, (symg, transg)) = build_alpha_refl_gl alphas (x, y, z)
1333
+ − 791
fun reflp_tac' _ = reflp_tac term_induct alpha_inj ctxt 1;
1334
+ − 792
fun symp_tac' _ = symp_tac alpha_induct alpha_inj eqvt ctxt 1;
1217
+ − 793
fun transp_tac' _ = transp_tac ctxt alpha_induct alpha_inj term_inj distinct cases eqvt 1;
1214
+ − 794
val reflt = Goal.prove ctxt' [] [] reflg reflp_tac';
+ − 795
val symt = Goal.prove ctxt' [] [] symg symp_tac';
+ − 796
val transt = Goal.prove ctxt' [] [] transg transp_tac';
+ − 797
val [refltg, symtg, transtg] = Variable.export ctxt' ctxt [reflt, symt, transt]
1215
+ − 798
val reflts = HOLogic.conj_elims refltg
+ − 799
val symts = HOLogic.conj_elims symtg
+ − 800
val transts = HOLogic.conj_elims transtg
1214
+ − 801
fun equivp alpha =
+ − 802
let
1215
+ − 803
val equivp = Const (@{const_name equivp}, fastype_of alpha --> @{typ bool})
+ − 804
val goal = @{term Trueprop} $ (equivp $ alpha)
+ − 805
fun tac _ = equivp_tac reflts symts transts 1
+ − 806
in
+ − 807
Goal.prove ctxt [] [] goal tac
+ − 808
end
1213
+ − 809
in
1215
+ − 810
map equivp alphas
1213
+ − 811
end
+ − 812
*}
1207
+ − 813
1217
+ − 814
(*
+ − 815
Tests:
+ − 816
prove alpha1_reflp_aux: {* fst (build_alpha_refl_gl [@{term alpha_rtrm1}, @{term alpha_bp}] ("x","y","z")) *}
+ − 817
by (tactic {* reflp_tac @{thm rtrm1_bp.induct} @{thms alpha1_inj} 1 *})
+ − 818
+ − 819
prove alpha1_symp_aux: {* (fst o snd) (build_alpha_refl_gl [@{term alpha_rtrm1}, @{term alpha_bp}] ("x","y","z")) *}
+ − 820
by (tactic {* symp_tac @{thm alpha_rtrm1_alpha_bp.induct} @{thms alpha1_inj} @{thms alpha1_eqvt} 1 *})
+ − 821
+ − 822
prove alpha1_transp_aux: {* (snd o snd) (build_alpha_refl_gl [@{term alpha_rtrm1}, @{term alpha_bp}] ("x","y","z")) *}
+ − 823
by (tactic {* transp_tac @{context} @{thm alpha_rtrm1_alpha_bp.induct} @{thms alpha1_inj} @{thms rtrm1.inject bp.inject} @{thms rtrm1.distinct bp.distinct} @{thms alpha_rtrm1.cases alpha_bp.cases} @{thms alpha1_eqvt} 1 *})
+ − 824
+ − 825
lemma alpha1_equivp:
+ − 826
"equivp alpha_rtrm1"
+ − 827
"equivp alpha_bp"
+ − 828
apply (tactic {*
+ − 829
(simp_tac (HOL_ss addsimps @{thms equivp_reflp_symp_transp reflp_def symp_def})
+ − 830
THEN' rtac @{thm conjI} THEN' rtac @{thm allI} THEN'
+ − 831
resolve_tac (HOLogic.conj_elims @{thm alpha1_reflp_aux})
+ − 832
THEN' rtac @{thm conjI} THEN' rtac @{thm allI} THEN' rtac @{thm allI} THEN'
+ − 833
resolve_tac (HOLogic.conj_elims @{thm alpha1_symp_aux}) THEN' rtac @{thm transp_aux}
+ − 834
THEN' resolve_tac (HOLogic.conj_elims @{thm alpha1_transp_aux})
+ − 835
)
+ − 836
1 *})
+ − 837
done*)
+ − 838
1308
+ − 839
ML {*
+ − 840
fun dtyp_no_of_typ _ (TFree (n, _)) = error "dtyp_no_of_typ: Illegal free"
+ − 841
| dtyp_no_of_typ _ (TVar _) = error "dtyp_no_of_typ: Illegal schematic"
+ − 842
| dtyp_no_of_typ dts (Type (tname, Ts)) =
+ − 843
case try (find_index (curry op = tname o fst)) dts of
+ − 844
NONE => error "dtyp_no_of_typ: Illegal recursion"
+ − 845
| SOME i => i
+ − 846
*}
+ − 847
1427
+ − 848
lemma not_in_union: "c \<notin> a \<union> b \<equiv> (c \<notin> a \<and> c \<notin> b)"
+ − 849
by auto
+ − 850
+ − 851
ML {*
+ − 852
fun supports_tac perm =
+ − 853
simp_tac (HOL_ss addsimps @{thms supports_def not_in_union} @ perm) THEN_ALL_NEW (
+ − 854
REPEAT o rtac allI THEN' REPEAT o rtac impI THEN' split_conjs THEN'
+ − 855
asm_full_simp_tac (HOL_ss addsimps @{thms fresh_def[symmetric]
1534
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 856
swap_fresh_fresh fresh_atom swap_at_base_simps(3) swap_atom_image_fresh
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 857
supp_fset_to_set supp_fmap_atom}))
1427
+ − 858
*}
+ − 859
+ − 860
ML {*
1428
+ − 861
fun mk_supp ty x =
+ − 862
Const (@{const_name supp}, ty --> @{typ "atom set"}) $ x
+ − 863
*}
+ − 864
+ − 865
ML {*
+ − 866
fun mk_supports_eq thy cnstr =
1427
+ − 867
let
+ − 868
val (tys, ty) = (strip_type o fastype_of) cnstr
+ − 869
val names = Datatype_Prop.make_tnames tys
+ − 870
val frees = map Free (names ~~ tys)
+ − 871
val rhs = list_comb (cnstr, frees)
1428
+ − 872
1427
+ − 873
fun mk_supp_arg (x, ty) =
1428
+ − 874
if is_atom thy ty then mk_supp @{typ atom} (mk_atom ty $ x) else
1534
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 875
if is_atom_set thy ty then mk_supp @{typ "atom set"} (mk_atom_set x) else
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 876
if is_atom_fset thy ty then mk_supp @{typ "atom set"} (mk_atom_fset x)
1427
+ − 877
else mk_supp ty x
+ − 878
val lhss = map mk_supp_arg (frees ~~ tys)
+ − 879
val supports = Const(@{const_name "supports"}, @{typ "atom set"} --> ty --> @{typ bool})
+ − 880
val eq = HOLogic.mk_Trueprop (supports $ mk_union lhss $ rhs)
+ − 881
in
+ − 882
(names, eq)
1422
+ − 883
end
1427
+ − 884
*}
+ − 885
1428
+ − 886
ML {*
+ − 887
fun prove_supports ctxt perms cnst =
+ − 888
let
+ − 889
val (names, eq) = mk_supports_eq (ProofContext.theory_of ctxt) cnst
+ − 890
in
+ − 891
Goal.prove ctxt names [] eq (fn _ => supports_tac perms 1)
1427
+ − 892
end
1428
+ − 893
*}
+ − 894
+ − 895
ML {*
+ − 896
fun mk_fs tys =
+ − 897
let
+ − 898
val names = Datatype_Prop.make_tnames tys
+ − 899
val frees = map Free (names ~~ tys)
+ − 900
val supps = map2 mk_supp tys frees
+ − 901
val fin_supps = map (fn x => @{term "finite :: atom set \<Rightarrow> bool"} $ x) supps
+ − 902
in
+ − 903
(names, HOLogic.mk_Trueprop (mk_conjl fin_supps))
+ − 904
end
+ − 905
*}
+ − 906
+ − 907
ML {*
+ − 908
fun fs_tac induct supports = ind_tac induct THEN_ALL_NEW (
+ − 909
rtac @{thm supports_finite} THEN' resolve_tac supports) THEN_ALL_NEW
1534
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 910
asm_full_simp_tac (HOL_ss addsimps @{thms supp_atom supp_atom_image supp_fset_to_set
984ea1299cd7
The nominal infrastructure for fset. 'fs' missing, but not needed so far.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 911
supp_fmap_atom finite_insert finite.emptyI finite_Un})
1428
+ − 912
*}
+ − 913
+ − 914
ML {*
+ − 915
fun prove_fs ctxt induct supports tys =
+ − 916
let
+ − 917
val (names, eq) = mk_fs tys
+ − 918
in
+ − 919
Goal.prove ctxt names [] eq (fn _ => fs_tac induct supports 1)
+ − 920
end
+ − 921
*}
+ − 922
+ − 923
end