2297
+ − 1
(* Title: nominal_dt_rawfuns.ML
2288
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 2
Author: Cezary Kaliszyk
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 3
Author: Christian Urban
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 4
2598
+ − 5
Definitions of the raw fv, fv_bn and permute functions.
2288
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 6
*)
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 7
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 8
signature NOMINAL_DT_RAWFUNS =
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 9
sig
2608
+ − 10
val get_all_binders: bclause list -> (term option * int) list
2616
dd7490fdd998
all examples for strong exhausts work; recursive binders need to be treated differently; still unclean version with lots of diagnostic code
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 11
val is_recursive_binder: bclause -> bool
2608
+ − 12
2957
+ − 13
val define_raw_bns: raw_dt_info -> (binding * typ option * mixfix) list ->
3245
+ − 14
Specification.multi_specs -> local_theory ->
2560
+ − 15
(term list * thm list * bn_info list * thm list * local_theory)
2601
+ − 16
2957
+ − 17
val define_raw_fvs: raw_dt_info -> bn_info list -> bclause list list list ->
+ − 18
Proof.context -> term list * term list * thm list * thm list * local_theory
2560
+ − 19
2957
+ − 20
val define_raw_bn_perms: raw_dt_info -> bn_info list -> local_theory ->
+ − 21
(term list * thm list * local_theory)
+ − 22
+ − 23
val define_raw_perms: raw_dt_info -> local_theory -> (term list * thm list * thm list) * local_theory
2305
+ − 24
+ − 25
val raw_prove_eqvt: term list -> thm list -> thm list -> Proof.context -> thm list
2598
+ − 26
2288
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 27
end
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 28
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 29
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 30
structure Nominal_Dt_RawFuns: NOMINAL_DT_RAWFUNS =
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 31
struct
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 32
2957
+ − 33
open Nominal_Permeq
2288
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 34
2608
+ − 35
fun get_all_binders bclauses =
+ − 36
bclauses
2611
3d101f2f817c
simple cases for strong inducts done; infrastructure for the difficult ones is there
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 37
|> map (fn (BC (_, binders, _)) => binders)
2608
+ − 38
|> flat
+ − 39
|> remove_dups (op =)
+ − 40
2616
dd7490fdd998
all examples for strong exhausts work; recursive binders need to be treated differently; still unclean version with lots of diagnostic code
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 41
fun is_recursive_binder (BC (_, binders, bodies)) =
dd7490fdd998
all examples for strong exhausts work; recursive binders need to be treated differently; still unclean version with lots of diagnostic code
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 42
case (inter (op =) (map snd binders) bodies) of
dd7490fdd998
all examples for strong exhausts work; recursive binders need to be treated differently; still unclean version with lots of diagnostic code
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 43
nil => false
dd7490fdd998
all examples for strong exhausts work; recursive binders need to be treated differently; still unclean version with lots of diagnostic code
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 44
| _ => true
dd7490fdd998
all examples for strong exhausts work; recursive binders need to be treated differently; still unclean version with lots of diagnostic code
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 45
dd7490fdd998
all examples for strong exhausts work; recursive binders need to be treated differently; still unclean version with lots of diagnostic code
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 46
2438
abafea9b39bb
corrected bug with fv-function generation (that was the problem with recursive binders)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 47
fun lookup xs x = the (AList.lookup (op=) xs x)
abafea9b39bb
corrected bug with fv-function generation (that was the problem with recursive binders)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 48
abafea9b39bb
corrected bug with fv-function generation (that was the problem with recursive binders)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 49
2601
+ − 50
(** functions that define the raw binding functions **)
+ − 51
+ − 52
(* strip_bn_fun takes a rhs of a bn function: this can only contain unions or
+ − 53
appends of elements; in case of recursive calls it returns also the applied
+ − 54
bn function *)
3218
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 55
fun strip_bn_fun ctxt args t =
2601
+ − 56
let
+ − 57
fun aux t =
+ − 58
case t of
+ − 59
Const (@{const_name sup}, _) $ l $ r => aux l @ aux r
+ − 60
| Const (@{const_name append}, _) $ l $ r => aux l @ aux r
+ − 61
| Const (@{const_name insert}, _) $ (Const (@{const_name atom}, _) $ (x as Var _)) $ y =>
+ − 62
(find_index (equal x) args, NONE) :: aux y
+ − 63
| Const (@{const_name Cons}, _) $ (Const (@{const_name atom}, _) $ (x as Var _)) $ y =>
+ − 64
(find_index (equal x) args, NONE) :: aux y
+ − 65
| Const (@{const_name bot}, _) => []
+ − 66
| Const (@{const_name Nil}, _) => []
+ − 67
| (f as Const _) $ (x as Var _) => [(find_index (equal x) args, SOME f)]
3218
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 68
| _ => error ("Unsupported binding function: " ^ (Syntax.string_of_term ctxt t))
2601
+ − 69
in
+ − 70
aux t
+ − 71
end
+ − 72
+ − 73
(** definition of the raw binding functions **)
+ − 74
3218
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 75
fun prep_bn_info ctxt dt_names dts bn_funs eqs =
2601
+ − 76
let
+ − 77
fun process_eq eq =
+ − 78
let
+ − 79
val (lhs, rhs) = eq
+ − 80
|> HOLogic.dest_Trueprop
+ − 81
|> HOLogic.dest_eq
+ − 82
val (bn_fun, [cnstr]) = strip_comb lhs
+ − 83
val (_, ty) = dest_Const bn_fun
+ − 84
val (ty_name, _) = dest_Type (domain_type ty)
+ − 85
val dt_index = find_index (fn x => x = ty_name) dt_names
+ − 86
val (cnstr_head, cnstr_args) = strip_comb cnstr
+ − 87
val cnstr_name = Long_Name.base_name (fst (dest_Const cnstr_head))
3218
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 88
val rhs_elements = strip_bn_fun ctxt cnstr_args rhs
2601
+ − 89
in
+ − 90
((bn_fun, dt_index), (cnstr_name, rhs_elements))
+ − 91
end
+ − 92
+ − 93
(* order according to constructor names *)
+ − 94
fun cntrs_order ((bn, dt_index), data) =
+ − 95
let
+ − 96
val dt = nth dts dt_index
3065
+ − 97
val cts = snd dt
2601
+ − 98
val ct_names = map (Binding.name_of o (fn (x, _, _) => x)) cts
+ − 99
in
+ − 100
(bn, (bn, dt_index, order (op=) ct_names data))
+ − 101
end
+ − 102
in
+ − 103
eqs
+ − 104
|> map process_eq
+ − 105
|> AList.group (op=) (* eqs grouped according to bn_functions *)
+ − 106
|> map cntrs_order (* inner data ordered according to constructors *)
+ − 107
|> order (op=) bn_funs (* ordered according to bn_functions *)
+ − 108
end
+ − 109
2957
+ − 110
fun define_raw_bns raw_dt_info raw_bn_funs raw_bn_eqs lthy =
2601
+ − 111
if null raw_bn_funs
+ − 112
then ([], [], [], [], lthy)
+ − 113
else
+ − 114
let
2957
+ − 115
val RawDtInfo
+ − 116
{raw_dt_names, raw_dts, raw_inject_thms, raw_distinct_thms, raw_size_thms, ...} = raw_dt_info
+ − 117
2601
+ − 118
val (_, lthy1) = Function.add_function raw_bn_funs raw_bn_eqs
2957
+ − 119
Function_Common.default_config (pat_completeness_simp (raw_inject_thms @ raw_distinct_thms)) lthy
2601
+ − 120
3245
+ − 121
val (info, lthy2) = prove_termination_fun raw_size_thms (Local_Theory.reset lthy1) (* FIXME disrupts context *)
2601
+ − 122
val {fs, simps, inducts, ...} = info
+ − 123
+ − 124
val raw_bn_induct = (the inducts)
+ − 125
val raw_bn_eqs = the simps
+ − 126
+ − 127
val raw_bn_info =
3239
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 128
prep_bn_info lthy raw_dt_names raw_dts fs (map Thm.prop_of raw_bn_eqs)
2601
+ − 129
in
+ − 130
(fs, raw_bn_eqs, raw_bn_info, raw_bn_induct, lthy2)
+ − 131
end
+ − 132
+ − 133
+ − 134
2295
+ − 135
(** functions that construct the equations for fv and fv_bn **)
2292
+ − 136
2464
f4eba60cbd69
made the fv-definition aggree more with alpha (needed in the support proofs)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 137
fun mk_fv_rhs lthy fv_map fv_bn_map args (BC (bmode, binders, bodies)) =
2295
+ − 138
let
2476
+ − 139
fun mk_fv_body fv_map args i =
+ − 140
let
+ − 141
val arg = nth args i
+ − 142
val ty = fastype_of arg
+ − 143
in
+ − 144
case AList.lookup (op=) fv_map ty of
+ − 145
NONE => mk_supp arg
+ − 146
| SOME fv => fv $ arg
+ − 147
end
2295
+ − 148
2464
f4eba60cbd69
made the fv-definition aggree more with alpha (needed in the support proofs)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 149
fun mk_fv_binder lthy fv_bn_map args binders =
2476
+ − 150
let
+ − 151
fun bind_set lthy args (NONE, i) = (setify lthy (nth args i), @{term "{}::atom set"})
+ − 152
| bind_set _ args (SOME bn, i) = (bn $ (nth args i),
+ − 153
if member (op=) bodies i then @{term "{}::atom set"}
+ − 154
else lookup fv_bn_map bn $ (nth args i))
+ − 155
fun bind_lst lthy args (NONE, i) = (listify lthy (nth args i), @{term "[]::atom list"})
+ − 156
| bind_lst _ args (SOME bn, i) = (bn $ (nth args i),
+ − 157
if member (op=) bodies i then @{term "[]::atom list"}
+ − 158
else lookup fv_bn_map bn $ (nth args i))
2464
f4eba60cbd69
made the fv-definition aggree more with alpha (needed in the support proofs)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 159
2476
+ − 160
val (combine_fn, bind_fn) =
+ − 161
case bmode of
+ − 162
Lst => (fold_append, bind_lst)
+ − 163
| Set => (fold_union, bind_set)
+ − 164
| Res => (fold_union, bind_set)
+ − 165
in
+ − 166
binders
+ − 167
|> map (bind_fn lthy args)
+ − 168
|> split_list
3239
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 169
|> apply2 combine_fn
2476
+ − 170
end
2295
+ − 171
2476
+ − 172
val t1 = map (mk_fv_body fv_map args) bodies
+ − 173
val (t2, t3) = mk_fv_binder lthy fv_bn_map args binders
+ − 174
in
+ − 175
mk_union (mk_diff (fold_union t1, to_set t2), to_set t3)
+ − 176
end
2292
+ − 177
2293
+ − 178
(* in case of fv_bn we have to treat the case special, where an
+ − 179
"empty" binding clause is given *)
2292
+ − 180
fun mk_fv_bn_rhs lthy fv_map fv_bn_map bn_args args bclause =
2295
+ − 181
let
2476
+ − 182
fun mk_fv_bn_body i =
+ − 183
let
+ − 184
val arg = nth args i
+ − 185
val ty = fastype_of arg
+ − 186
in
+ − 187
case AList.lookup (op=) bn_args i of
+ − 188
NONE => (case (AList.lookup (op=) fv_map ty) of
+ − 189
NONE => mk_supp arg
+ − 190
| SOME fv => fv $ arg)
+ − 191
| SOME (NONE) => @{term "{}::atom set"}
+ − 192
| SOME (SOME bn) => lookup fv_bn_map bn $ arg
+ − 193
end
2295
+ − 194
in
2476
+ − 195
case bclause of
+ − 196
BC (_, [], bodies) => fold_union (map mk_fv_bn_body bodies)
+ − 197
| _ => mk_fv_rhs lthy fv_map fv_bn_map args bclause
+ − 198
end
2288
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 199
2296
+ − 200
fun mk_fv_eq lthy fv_map fv_bn_map (constr, ty, arg_tys, _) bclauses =
2476
+ − 201
let
3239
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 202
val arg_names = Old_Datatype_Prop.make_tnames arg_tys
2476
+ − 203
val args = map Free (arg_names ~~ arg_tys)
+ − 204
val fv = lookup fv_map ty
+ − 205
val lhs = fv $ list_comb (constr, args)
+ − 206
val rhs_trms = map (mk_fv_rhs lthy fv_map fv_bn_map args) bclauses
+ − 207
val rhs = fold_union rhs_trms
+ − 208
in
+ − 209
HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
+ − 210
end
2292
+ − 211
2296
+ − 212
fun mk_fv_bn_eq lthy bn_trm fv_map fv_bn_map (bn_args, (constr, _, arg_tys, _)) bclauses =
2476
+ − 213
let
3239
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 214
val arg_names = Old_Datatype_Prop.make_tnames arg_tys
2476
+ − 215
val args = map Free (arg_names ~~ arg_tys)
+ − 216
val fv_bn = lookup fv_bn_map bn_trm
+ − 217
val lhs = fv_bn $ list_comb (constr, args)
+ − 218
val rhs_trms = map (mk_fv_bn_rhs lthy fv_map fv_bn_map bn_args args) bclauses
+ − 219
val rhs = fold_union rhs_trms
+ − 220
in
+ − 221
HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
+ − 222
end
2288
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 223
2292
+ − 224
fun mk_fv_bn_eqs lthy fv_map fv_bn_map constrs_info bclausesss (bn_trm, bn_n, bn_argss) =
2476
+ − 225
let
+ − 226
val nth_constrs_info = nth constrs_info bn_n
+ − 227
val nth_bclausess = nth bclausesss bn_n
+ − 228
in
+ − 229
map2 (mk_fv_bn_eq lthy bn_trm fv_map fv_bn_map) (bn_argss ~~ nth_constrs_info) nth_bclausess
+ − 230
end
2288
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 231
2957
+ − 232
fun define_raw_fvs raw_dt_info bn_info bclausesss lthy =
2476
+ − 233
let
2957
+ − 234
val RawDtInfo
+ − 235
{raw_dt_names, raw_tys, raw_cns_info, raw_inject_thms, raw_distinct_thms, raw_size_thms, ...} =
+ − 236
raw_dt_info
+ − 237
+ − 238
val fv_names = map (prefix "fv_" o Long_Name.base_name) raw_dt_names
2476
+ − 239
val fv_tys = map (fn ty => ty --> @{typ "atom set"}) raw_tys
+ − 240
val fv_frees = map Free (fv_names ~~ fv_tys);
+ − 241
val fv_map = raw_tys ~~ fv_frees
2288
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 242
2476
+ − 243
val (bns, bn_tys) = split_list (map (fn (bn, i, _) => (bn, i)) bn_info)
+ − 244
val bn_names = map (fn bn => Long_Name.base_name (fst (dest_Const bn))) bns
+ − 245
val fv_bn_names = map (prefix "fv_") bn_names
+ − 246
val fv_bn_arg_tys = map (nth raw_tys) bn_tys
+ − 247
val fv_bn_tys = map (fn ty => ty --> @{typ "atom set"}) fv_bn_arg_tys
+ − 248
val fv_bn_frees = map Free (fv_bn_names ~~ fv_bn_tys)
+ − 249
val fv_bn_map = bns ~~ fv_bn_frees
2292
+ − 250
2957
+ − 251
val fv_eqs = map2 (map2 (mk_fv_eq lthy fv_map fv_bn_map)) raw_cns_info bclausesss
+ − 252
val fv_bn_eqs = map (mk_fv_bn_eqs lthy fv_map fv_bn_map raw_cns_info bclausesss) bn_info
2288
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 253
2476
+ − 254
val all_fun_names = map (fn s => (Binding.name s, NONE, NoSyn)) (fv_names @ fv_bn_names)
3245
+ − 255
val all_fun_eqs = map (fn t => ((Binding.empty_atts, t), [], [])) (flat fv_eqs @ flat fv_bn_eqs)
2288
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 256
2476
+ − 257
val (_, lthy') = Function.add_function all_fun_names all_fun_eqs
2957
+ − 258
Function_Common.default_config (pat_completeness_simp (raw_inject_thms @ raw_distinct_thms)) lthy
2569
94750b31a97d
fixed bug in fv function where a shallow binder binds lists of names
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 259
3245
+ − 260
val (info, lthy'') = prove_termination_fun raw_size_thms (Local_Theory.reset lthy') (* FIXME disrupts context *)
2410
2bbdb9c427b5
improved runtime slightly, by constructing an explicit size measure for the function definitions
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 261
2888
eda5aeb056a6
fixed nasty bug with type variables in nominal_datatypes; this included to be careful with the output of the inductive and function package
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 262
val {fs, simps, inducts, ...} = info;
2288
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 263
3244
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 264
val morphism =
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 265
Proof_Context.export_morphism lthy''
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 266
(Proof_Context.transfer (Proof_Context.theory_of lthy'') lthy)
2476
+ − 267
val simps_exp = map (Morphism.thm morphism) (the simps)
+ − 268
val inducts_exp = map (Morphism.thm morphism) (the inducts)
2888
eda5aeb056a6
fixed nasty bug with type variables in nominal_datatypes; this included to be careful with the output of the inductive and function package
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 269
2476
+ − 270
val (fvs', fv_bns') = chop (length fv_frees) fs
2888
eda5aeb056a6
fixed nasty bug with type variables in nominal_datatypes; this included to be careful with the output of the inductive and function package
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 271
eda5aeb056a6
fixed nasty bug with type variables in nominal_datatypes; this included to be careful with the output of the inductive and function package
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 272
(* grafting the types so that they coincide with the input into the function package *)
eda5aeb056a6
fixed nasty bug with type variables in nominal_datatypes; this included to be careful with the output of the inductive and function package
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 273
val fvs'' = map2 (fn t => fn ty => Const (fst (dest_Const t), ty) ) fvs' fv_tys
eda5aeb056a6
fixed nasty bug with type variables in nominal_datatypes; this included to be careful with the output of the inductive and function package
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 274
val fv_bns'' = map2 (fn t => fn ty => Const (fst (dest_Const t), ty) ) fv_bns' fv_bn_tys
2476
+ − 275
in
2888
eda5aeb056a6
fixed nasty bug with type variables in nominal_datatypes; this included to be careful with the output of the inductive and function package
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 276
(fvs'', fv_bns'', simps_exp, inducts_exp, lthy'')
2476
+ − 277
end
2305
+ − 278
+ − 279
2560
+ − 280
(** definition of raw permute_bn functions **)
+ − 281
+ − 282
fun mk_perm_bn_eq_rhs p perm_bn_map bn_args (i, arg) =
+ − 283
case AList.lookup (op=) bn_args i of
+ − 284
NONE => arg
+ − 285
| SOME (NONE) => mk_perm p arg
+ − 286
| SOME (SOME bn) => (lookup perm_bn_map bn) $ p $ arg
+ − 287
+ − 288
+ − 289
fun mk_perm_bn_eq lthy bn_trm perm_bn_map bn_args (constr, _, arg_tys, _) =
+ − 290
let
+ − 291
val p = Free ("p", @{typ perm})
3239
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 292
val arg_names = Old_Datatype_Prop.make_tnames arg_tys
2560
+ − 293
val args = map Free (arg_names ~~ arg_tys)
+ − 294
val perm_bn = lookup perm_bn_map bn_trm
+ − 295
val lhs = perm_bn $ p $ list_comb (constr, args)
+ − 296
val rhs = list_comb (constr, map_index (mk_perm_bn_eq_rhs p perm_bn_map bn_args) args)
+ − 297
in
+ − 298
HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
+ − 299
end
+ − 300
+ − 301
fun mk_perm_bn_eqs lthy perm_bn_map cns_info (bn_trm, bn_n, bn_argss) =
+ − 302
let
+ − 303
val nth_cns_info = nth cns_info bn_n
+ − 304
in
+ − 305
map2 (mk_perm_bn_eq lthy bn_trm perm_bn_map) bn_argss nth_cns_info
+ − 306
end
+ − 307
2957
+ − 308
fun define_raw_bn_perms raw_dt_info bn_info lthy =
2560
+ − 309
if null bn_info
+ − 310
then ([], [], lthy)
+ − 311
else
+ − 312
let
2957
+ − 313
val RawDtInfo
+ − 314
{raw_tys, raw_cns_info, raw_inject_thms, raw_distinct_thms, raw_size_thms, ...} = raw_dt_info
+ − 315
2560
+ − 316
val (bns, bn_tys) = split_list (map (fn (bn, i, _) => (bn, i)) bn_info)
+ − 317
val bn_names = map (fn bn => Long_Name.base_name (fst (dest_Const bn))) bns
+ − 318
val perm_bn_names = map (prefix "permute_") bn_names
+ − 319
val perm_bn_arg_tys = map (nth raw_tys) bn_tys
+ − 320
val perm_bn_tys = map (fn ty => @{typ "perm"} --> ty --> ty) perm_bn_arg_tys
+ − 321
val perm_bn_frees = map Free (perm_bn_names ~~ perm_bn_tys)
+ − 322
val perm_bn_map = bns ~~ perm_bn_frees
+ − 323
2957
+ − 324
val perm_bn_eqs = map (mk_perm_bn_eqs lthy perm_bn_map raw_cns_info) bn_info
2560
+ − 325
+ − 326
val all_fun_names = map (fn s => (Binding.name s, NONE, NoSyn)) perm_bn_names
3245
+ − 327
val all_fun_eqs = map (fn t => ((Binding.empty_atts, t), [], [])) (flat perm_bn_eqs)
2560
+ − 328
+ − 329
val prod_simps = @{thms prod.inject HOL.simp_thms}
+ − 330
+ − 331
val (_, lthy') = Function.add_function all_fun_names all_fun_eqs
2957
+ − 332
Function_Common.default_config
+ − 333
(pat_completeness_simp (prod_simps @ raw_inject_thms @ raw_distinct_thms)) lthy
2560
+ − 334
3245
+ − 335
val (info, lthy'') = prove_termination_fun raw_size_thms (Local_Theory.reset lthy') (* FIXME disrupts context *)
2560
+ − 336
+ − 337
val {fs, simps, ...} = info;
+ − 338
3244
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 339
val morphism =
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 340
Proof_Context.export_morphism lthy''
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 341
(Proof_Context.transfer (Proof_Context.theory_of lthy'') lthy)
2560
+ − 342
val simps_exp = map (Morphism.thm morphism) (the simps)
+ − 343
in
+ − 344
(fs, simps_exp, lthy'')
+ − 345
end
+ − 346
2598
+ − 347
(*** raw permutation functions ***)
+ − 348
+ − 349
(** proves the two pt-type class properties **)
+ − 350
3218
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 351
fun prove_permute_zero induct perm_defs perm_fns ctxt =
2598
+ − 352
let
+ − 353
val perm_types = map (body_type o fastype_of) perm_fns
3239
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 354
val perm_indnames = Old_Datatype_Prop.make_tnames perm_types
2598
+ − 355
+ − 356
fun single_goal ((perm_fn, T), x) =
+ − 357
HOLogic.mk_eq (perm_fn $ @{term "0::perm"} $ Free (x, T), Free (x, T))
+ − 358
+ − 359
val goals =
+ − 360
HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
+ − 361
(map single_goal (perm_fns ~~ perm_types ~~ perm_indnames)))
+ − 362
in
3244
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 363
Goal.prove ctxt perm_indnames [] goals
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 364
(fn {context = ctxt', ...} =>
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 365
(Old_Datatype_Aux.ind_tac ctxt' induct perm_indnames THEN_ALL_NEW
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 366
asm_simp_tac
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 367
(put_simpset HOL_basic_ss ctxt' addsimps (@{thm permute_zero} :: perm_defs))) 1)
3239
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 368
|> Old_Datatype_Aux.split_conj_thm
2598
+ − 369
end
+ − 370
+ − 371
3218
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 372
fun prove_permute_plus induct perm_defs perm_fns ctxt =
2598
+ − 373
let
+ − 374
val p = Free ("p", @{typ perm})
+ − 375
val q = Free ("q", @{typ perm})
+ − 376
val perm_types = map (body_type o fastype_of) perm_fns
3239
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 377
val perm_indnames = Old_Datatype_Prop.make_tnames perm_types
2598
+ − 378
+ − 379
fun single_goal ((perm_fn, T), x) = HOLogic.mk_eq
+ − 380
(perm_fn $ (mk_plus p q) $ Free (x, T), perm_fn $ p $ (perm_fn $ q $ Free (x, T)))
+ − 381
+ − 382
val goals =
+ − 383
HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj
+ − 384
(map single_goal (perm_fns ~~ perm_types ~~ perm_indnames)))
+ − 385
in
3244
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 386
Goal.prove ctxt ("p" :: "q" :: perm_indnames) [] goals
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 387
(fn {context = ctxt', ...} =>
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 388
(Old_Datatype_Aux.ind_tac ctxt' induct perm_indnames THEN_ALL_NEW
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 389
asm_simp_tac
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 390
(put_simpset HOL_basic_ss ctxt' addsimps (@{thm permute_plus} :: perm_defs))) 1)
3239
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 391
|> Old_Datatype_Aux.split_conj_thm
2598
+ − 392
end
+ − 393
+ − 394
+ − 395
fun mk_perm_eq ty_perm_assoc cnstr =
+ − 396
let
+ − 397
fun lookup_perm p (ty, arg) =
+ − 398
case (AList.lookup (op=) ty_perm_assoc ty) of
+ − 399
SOME perm => perm $ p $ arg
+ − 400
| NONE => Const (@{const_name permute}, perm_ty ty) $ p $ arg
+ − 401
+ − 402
val p = Free ("p", @{typ perm})
+ − 403
val (arg_tys, ty) =
+ − 404
fastype_of cnstr
+ − 405
|> strip_type
+ − 406
3239
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 407
val arg_names = Name.variant_list ["p"] (Old_Datatype_Prop.make_tnames arg_tys)
2598
+ − 408
val args = map Free (arg_names ~~ arg_tys)
+ − 409
+ − 410
val lhs = lookup_perm p (ty, list_comb (cnstr, args))
+ − 411
val rhs = list_comb (cnstr, map (lookup_perm p) (arg_tys ~~ args))
+ − 412
+ − 413
val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
+ − 414
in
3245
+ − 415
((Binding.empty_atts, eq), [], [])
2598
+ − 416
end
+ − 417
+ − 418
2957
+ − 419
fun define_raw_perms raw_dt_info lthy =
2598
+ − 420
let
2957
+ − 421
val RawDtInfo
+ − 422
{raw_dt_names, raw_tys, raw_ty_args, raw_all_cns, raw_induct_thm, ...} = raw_dt_info
+ − 423
+ − 424
val perm_fn_names = raw_dt_names
2598
+ − 425
|> map Long_Name.base_name
+ − 426
|> map (prefix "permute_")
+ − 427
2957
+ − 428
val perm_fn_types = map perm_ty raw_tys
2598
+ − 429
val perm_fn_frees = map Free (perm_fn_names ~~ perm_fn_types)
+ − 430
val perm_fn_binds = map (fn s => (Binding.name s, NONE, NoSyn)) perm_fn_names
+ − 431
2957
+ − 432
val perm_eqs = map (mk_perm_eq (raw_tys ~~ perm_fn_frees)) (flat raw_all_cns)
2598
+ − 433
3239
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 434
fun tac ctxt (_, _, simps) =
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 435
Class.intro_classes_tac ctxt [] THEN ALLGOALS (resolve_tac ctxt simps)
2598
+ − 436
+ − 437
fun morphism phi (fvs, dfs, simps) =
+ − 438
(map (Morphism.term phi) fvs,
+ − 439
map (Morphism.thm phi) dfs,
+ − 440
map (Morphism.thm phi) simps);
+ − 441
+ − 442
val ((perm_funs, perm_eq_thms), lthy') =
+ − 443
lthy
+ − 444
|> Local_Theory.exit_global
2957
+ − 445
|> Class.instantiation (raw_dt_names, raw_ty_args, @{sort pt})
3239
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 446
|> BNF_LFP_Compat.primrec perm_fn_binds perm_eqs
2598
+ − 447
2957
+ − 448
val perm_zero_thms = prove_permute_zero raw_induct_thm perm_eq_thms perm_funs lthy'
+ − 449
val perm_plus_thms = prove_permute_plus raw_induct_thm perm_eq_thms perm_funs lthy'
2598
+ − 450
in
+ − 451
lthy'
+ − 452
|> Class.prove_instantiation_exit_result morphism tac
+ − 453
(perm_funs, perm_eq_thms, perm_zero_thms @ perm_plus_thms)
+ − 454
||> Named_Target.theory_init
+ − 455
end
+ − 456
+ − 457
2957
+ − 458
(** equivarance proofs **)
+ − 459
+ − 460
val eqvt_apply_sym = @{thm eqvt_apply[symmetric]}
+ − 461
+ − 462
fun subproof_tac const_names simps =
3218
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 463
SUBPROOF (fn {prems, context = ctxt, ...} =>
2957
+ − 464
HEADGOAL
3218
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 465
(simp_tac (put_simpset HOL_basic_ss ctxt addsimps simps)
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 466
THEN' eqvt_tac ctxt (eqvt_relaxed_config addexcls const_names)
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 467
THEN' simp_tac (put_simpset HOL_basic_ss ctxt addsimps (prems @ [eqvt_apply_sym]))))
2957
+ − 468
+ − 469
fun prove_eqvt_tac insts ind_thms const_names simps ctxt =
+ − 470
HEADGOAL
3226
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 471
(Object_Logic.full_atomize_tac ctxt
3239
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 472
THEN' (DETERM o (Induct_Tacs.induct_tac ctxt insts (SOME ind_thms)))
2957
+ − 473
THEN_ALL_NEW subproof_tac const_names simps ctxt)
+ − 474
+ − 475
fun mk_eqvt_goal pi const arg =
+ − 476
let
+ − 477
val lhs = mk_perm pi (const $ arg)
+ − 478
val rhs = const $ (mk_perm pi arg)
+ − 479
in
+ − 480
HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
+ − 481
end
+ − 482
+ − 483
+ − 484
fun raw_prove_eqvt consts ind_thms simps ctxt =
+ − 485
if null consts then []
+ − 486
else
+ − 487
let
+ − 488
val ([p], ctxt') = Variable.variant_fixes ["p"] ctxt
+ − 489
val p = Free (p, @{typ perm})
+ − 490
val arg_tys =
+ − 491
consts
+ − 492
|> map fastype_of
+ − 493
|> map domain_type
+ − 494
val (arg_names, ctxt'') =
3239
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 495
Variable.variant_fixes (Old_Datatype_Prop.make_tnames arg_tys) ctxt'
2957
+ − 496
val args = map Free (arg_names ~~ arg_tys)
+ − 497
val goals = map2 (mk_eqvt_goal p) consts args
+ − 498
val insts = map (single o SOME) arg_names
+ − 499
val const_names = map (fst o dest_Const) consts
+ − 500
in
3239
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
+ − 501
Goal.prove_common ctxt'' NONE [] [] goals (fn {context, ...} =>
2957
+ − 502
prove_eqvt_tac insts ind_thms const_names simps context)
3045
d0ad264f8c4f
updated to Isabelle 3 Nov; it includes a hack to work around a bug in the localised version of the quotient package
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 503
|> Proof_Context.export ctxt'' ctxt
2957
+ − 504
end
+ − 505
+ − 506
2288
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 507
end (* structure *)
3b83960f9544
new fv/fv_bn function (supp breaks now); exported raw perms and raw funs into separate ML-files
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
+ − 508