1941
+ − 1
theory NewParser
1971
8daf6ff5e11a
simpliied and moved the remaining lemmas about the atom-function to Nominal2_Base
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 2
imports "../Nominal-General/Nominal2_Base"
1941
+ − 3
"../Nominal-General/Nominal2_Eqvt"
+ − 4
"../Nominal-General/Nominal2_Supp"
2324
+ − 5
"Perm" "Tacs" "Lift" "Equivp"
1941
+ − 6
begin
+ − 7
2314
+ − 8
(* TODO
+ − 9
+ − 10
we need to also export a cases-rule for nominal datatypes
+ − 11
size function
+ − 12
*)
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>
diff
changeset
+ − 13
1941
+ − 14
section{* Interface for nominal_datatype *}
+ − 15
+ − 16
+ − 17
ML {*
+ − 18
(* nominal datatype parser *)
+ − 19
local
2168
+ − 20
structure P = Parse;
1941
+ − 21
structure S = Scan
+ − 22
+ − 23
fun triple1 ((x, y), z) = (x, y, z)
+ − 24
fun triple2 (x, (y, z)) = (x, y, z)
+ − 25
fun tuple ((x, y, z), u) = (x, y, z, u)
+ − 26
fun tswap (((x, y), z), u) = (x, y, u, z)
+ − 27
in
+ − 28
2168
+ − 29
val _ = Keyword.keyword "bind"
+ − 30
val _ = Keyword.keyword "bind_set"
+ − 31
val _ = Keyword.keyword "bind_res"
1941
+ − 32
1943
+ − 33
val anno_typ = S.option (P.name --| P.$$$ "::") -- P.typ
1941
+ − 34
+ − 35
val bind_mode = P.$$$ "bind" || P.$$$ "bind_set" || P.$$$ "bind_res"
+ − 36
+ − 37
val bind_clauses =
+ − 38
P.enum "," (bind_mode -- S.repeat1 P.term -- (P.$$$ "in" |-- S.repeat1 P.name) >> triple1)
+ − 39
+ − 40
val cnstr_parser =
1943
+ − 41
P.binding -- S.repeat anno_typ -- bind_clauses -- P.opt_mixfix >> tswap
1941
+ − 42
+ − 43
(* datatype parser *)
+ − 44
val dt_parser =
+ − 45
(P.type_args -- P.binding -- P.opt_mixfix >> triple1) --
1943
+ − 46
(P.$$$ "=" |-- P.enum1 "|" cnstr_parser) >> tuple
1941
+ − 47
+ − 48
(* binding function parser *)
+ − 49
val bnfun_parser =
2168
+ − 50
S.optional (P.$$$ "binder" |-- P.fixes -- Parse_Spec.where_alt_specs) ([], [])
1941
+ − 51
+ − 52
(* main parser *)
+ − 53
val main_parser =
1944
+ − 54
P.and_list1 dt_parser -- bnfun_parser >> triple2
1941
+ − 55
+ − 56
end
+ − 57
*}
+ − 58
+ − 59
ML {*
+ − 60
fun get_cnstrs dts =
+ − 61
map (fn (_, _, _, constrs) => constrs) dts
+ − 62
+ − 63
fun get_typed_cnstrs dts =
+ − 64
flat (map (fn (_, bn, _, constrs) =>
+ − 65
(map (fn (bn', _, _) => (Binding.name_of bn, Binding.name_of bn')) constrs)) dts)
+ − 66
+ − 67
fun get_cnstr_strs dts =
+ − 68
map (fn (bn, _, _) => Binding.name_of bn) (flat (get_cnstrs dts))
+ − 69
+ − 70
fun get_bn_fun_strs bn_funs =
+ − 71
map (fn (bn_fun, _, _) => Binding.name_of bn_fun) bn_funs
+ − 72
*}
+ − 73
2106
+ − 74
1941
+ − 75
ML {*
+ − 76
fun add_datatype_wrapper dt_names dts =
+ − 77
let
+ − 78
val conf = Datatype.default_config
+ − 79
in
+ − 80
Local_Theory.theory_result (Datatype.add_datatype conf dt_names dts)
+ − 81
end
+ − 82
*}
+ − 83
1944
+ − 84
+ − 85
text {* Infrastructure for adding "_raw" to types and terms *}
+ − 86
1941
+ − 87
ML {*
+ − 88
fun add_raw s = s ^ "_raw"
+ − 89
fun add_raws ss = map add_raw ss
+ − 90
fun raw_bind bn = Binding.suffix_name "_raw" bn
+ − 91
+ − 92
fun replace_str ss s =
+ − 93
case (AList.lookup (op=) ss s) of
+ − 94
SOME s' => s'
+ − 95
| NONE => s
+ − 96
+ − 97
fun replace_typ ty_ss (Type (a, Ts)) = Type (replace_str ty_ss a, map (replace_typ ty_ss) Ts)
+ − 98
| replace_typ ty_ss T = T
+ − 99
+ − 100
fun raw_dts ty_ss dts =
+ − 101
let
+ − 102
fun raw_dts_aux1 (bind, tys, mx) =
+ − 103
(raw_bind bind, map (replace_typ ty_ss) tys, mx)
+ − 104
+ − 105
fun raw_dts_aux2 (ty_args, bind, mx, constrs) =
+ − 106
(ty_args, raw_bind bind, mx, map raw_dts_aux1 constrs)
+ − 107
in
+ − 108
map raw_dts_aux2 dts
+ − 109
end
+ − 110
+ − 111
fun replace_aterm trm_ss (Const (a, T)) = Const (replace_str trm_ss a, T)
+ − 112
| replace_aterm trm_ss (Free (a, T)) = Free (replace_str trm_ss a, T)
+ − 113
| replace_aterm trm_ss trm = trm
+ − 114
+ − 115
fun replace_term trm_ss ty_ss trm =
+ − 116
trm |> Term.map_aterms (replace_aterm trm_ss) |> map_types (replace_typ ty_ss)
+ − 117
*}
+ − 118
+ − 119
ML {*
+ − 120
fun rawify_dts dt_names dts dts_env =
+ − 121
let
+ − 122
val raw_dts = raw_dts dts_env dts
+ − 123
val raw_dt_names = add_raws dt_names
+ − 124
in
+ − 125
(raw_dt_names, raw_dts)
+ − 126
end
+ − 127
*}
+ − 128
+ − 129
ML {*
+ − 130
fun rawify_bn_funs dts_env cnstrs_env bn_fun_env bn_funs bn_eqs =
+ − 131
let
+ − 132
val bn_funs' = map (fn (bn, ty, mx) =>
2304
+ − 133
(raw_bind bn, SOME (replace_typ dts_env ty), mx)) bn_funs
1941
+ − 134
+ − 135
val bn_eqs' = map (fn (attr, trm) =>
+ − 136
(attr, replace_term (cnstrs_env @ bn_fun_env) dts_env trm)) bn_eqs
+ − 137
in
+ − 138
(bn_funs', bn_eqs')
+ − 139
end
+ − 140
*}
+ − 141
+ − 142
ML {*
+ − 143
fun rawify_bclauses dts_env cnstrs_env bn_fun_env bclauses =
+ − 144
let
+ − 145
fun rawify_bnds bnds =
+ − 146
map (apfst (Option.map (replace_term (cnstrs_env @ bn_fun_env) dts_env))) bnds
+ − 147
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>
diff
changeset
+ − 148
fun rawify_bclause (BC (mode, bnds, bdys)) = BC (mode, rawify_bnds bnds, bdys)
1941
+ − 149
in
+ − 150
map (map (map rawify_bclause)) bclauses
+ − 151
end
+ − 152
*}
+ − 153
2143
871d8a5e0c67
somewhat simplified the main parsing function; failed to move a Note-statement to define_raw_perms
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 154
(* strip_bn_fun takes a rhs of a bn function: this can only contain unions or
871d8a5e0c67
somewhat simplified the main parsing function; failed to move a Note-statement to define_raw_perms
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 155
appends of elements; in case of recursive calls it retruns also the applied
871d8a5e0c67
somewhat simplified the main parsing function; failed to move a Note-statement to define_raw_perms
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 156
bn function *)
1941
+ − 157
ML {*
2294
+ − 158
fun strip_bn_fun lthy args t =
+ − 159
let
+ − 160
fun aux t =
+ − 161
case t of
+ − 162
Const (@{const_name sup}, _) $ l $ r => aux l @ aux r
+ − 163
| Const (@{const_name append}, _) $ l $ r => aux l @ aux r
+ − 164
| Const (@{const_name insert}, _) $ (Const (@{const_name atom}, _) $ (x as Var _)) $ y =>
+ − 165
(find_index (equal x) args, NONE) :: aux y
+ − 166
| Const (@{const_name Cons}, _) $ (Const (@{const_name atom}, _) $ (x as Var _)) $ y =>
+ − 167
(find_index (equal x) args, NONE) :: aux y
+ − 168
| Const (@{const_name bot}, _) => []
+ − 169
| Const (@{const_name Nil}, _) => []
+ − 170
| (f as Const _) $ (x as Var _) => [(find_index (equal x) args, SOME f)]
+ − 171
| _ => error ("Unsupported binding function: " ^ (Syntax.string_of_term lthy t))
+ − 172
in
+ − 173
aux t
+ − 174
end
1941
+ − 175
*}
+ − 176
+ − 177
ML {*
+ − 178
fun find [] _ = error ("cannot find element")
+ − 179
| find ((x, z)::xs) y = if (Long_Name.base_name x) = y then z else find xs y
+ − 180
*}
+ − 181
+ − 182
ML {*
2295
+ − 183
fun prep_bn_info lthy dt_names dts eqs =
1941
+ − 184
let
+ − 185
fun aux eq =
+ − 186
let
+ − 187
val (lhs, rhs) = eq
+ − 188
|> HOLogic.dest_Trueprop
+ − 189
|> HOLogic.dest_eq
+ − 190
val (bn_fun, [cnstr]) = strip_comb lhs
2294
+ − 191
val (_, ty) = dest_Const bn_fun
1941
+ − 192
val (ty_name, _) = dest_Type (domain_type ty)
+ − 193
val dt_index = find_index (fn x => x = ty_name) dt_names
2294
+ − 194
val (cnstr_head, cnstr_args) = strip_comb cnstr
+ − 195
val rhs_elements = strip_bn_fun lthy cnstr_args rhs
1941
+ − 196
in
2308
+ − 197
(dt_index, (bn_fun, (cnstr_head, rhs_elements)))
2122
+ − 198
end
1941
+ − 199
fun order dts i ts =
+ − 200
let
+ − 201
val dt = nth dts i
+ − 202
val cts = map (fn (x, _, _) => Binding.name_of x) ((fn (_, _, _, x) => x) dt)
+ − 203
val ts' = map (fn (x, y) => (fst (dest_Const x), y)) ts
+ − 204
in
+ − 205
map (find ts') cts
+ − 206
end
+ − 207
+ − 208
val unordered = AList.group (op=) (map aux eqs)
+ − 209
val unordered' = map (fn (x, y) => (x, AList.group (op=) y)) unordered
+ − 210
val ordered = map (fn (x, y) => (x, map (fn (v, z) => (v, order dts x z)) y)) unordered'
2118
+ − 211
val ordered' = flat (map (fn (ith, l) => map (fn (bn, data) => (bn, ith, data)) l) ordered)
2122
+ − 212
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>
diff
changeset
+ − 213
(*val _ = tracing ("eqs\n" ^ cat_lines (map (Syntax.string_of_term lthy) eqs))*)
2142
c39d4fe31100
moved the exporting part into the parser (this is still a hack); re-added CoreHaskell again to the examples - there seems to be a problem with the variable name pat
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 214
(*val _ = tracing ("map eqs\n" ^ @{make_string} (map aux2 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>
diff
changeset
+ − 215
(*val _ = tracing ("ordered'\n" ^ @{make_string} ordered')*)
1941
+ − 216
in
2125
60ee289a8c63
made out of STEPS a configuration value so that it can be set individually in each file
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 217
ordered'
1941
+ − 218
end
+ − 219
*}
+ − 220
+ − 221
ML {*
+ − 222
fun raw_nominal_decls dts bn_funs bn_eqs binds lthy =
+ − 223
let
+ − 224
val thy = ProofContext.theory_of lthy
+ − 225
val thy_name = Context.theory_name thy
+ − 226
+ − 227
val dt_names = map (fn (_, s, _, _) => Binding.name_of s) dts
+ − 228
val dt_full_names = map (Long_Name.qualify thy_name) dt_names
+ − 229
val dt_full_names' = add_raws dt_full_names
+ − 230
val dts_env = dt_full_names ~~ dt_full_names'
+ − 231
+ − 232
val cnstrs = get_cnstr_strs dts
+ − 233
val cnstrs_ty = get_typed_cnstrs dts
+ − 234
val cnstrs_full_names = map (Long_Name.qualify thy_name) cnstrs
+ − 235
val cnstrs_full_names' = map (fn (x, y) => Long_Name.qualify thy_name
+ − 236
(Long_Name.qualify (add_raw x) (add_raw y))) cnstrs_ty
+ − 237
val cnstrs_env = cnstrs_full_names ~~ cnstrs_full_names'
+ − 238
+ − 239
val bn_fun_strs = get_bn_fun_strs bn_funs
+ − 240
val bn_fun_strs' = add_raws bn_fun_strs
+ − 241
val bn_fun_env = bn_fun_strs ~~ bn_fun_strs'
+ − 242
val bn_fun_full_env = map (pairself (Long_Name.qualify thy_name))
+ − 243
(bn_fun_strs ~~ bn_fun_strs')
+ − 244
+ − 245
val (raw_dt_names, raw_dts) = rawify_dts dt_names dts dts_env
+ − 246
val (raw_bn_funs, raw_bn_eqs) = rawify_bn_funs dts_env cnstrs_env bn_fun_env bn_funs bn_eqs
+ − 247
val raw_bclauses = rawify_bclauses dts_env cnstrs_env bn_fun_full_env binds
+ − 248
2308
+ − 249
val (raw_dt_full_names, lthy1) =
+ − 250
add_datatype_wrapper raw_dt_names raw_dts lthy
2304
+ − 251
in
2337
+ − 252
(raw_dt_full_names, raw_dts, raw_bclauses, raw_bn_funs, raw_bn_eqs, lthy1)
2304
+ − 253
end
+ − 254
*}
+ − 255
+ − 256
ML {*
+ − 257
fun raw_bn_decls dt_names dts raw_bn_funs raw_bn_eqs constr_thms lthy =
2308
+ − 258
if null raw_bn_funs
+ − 259
then ([], [], [], [], lthy)
+ − 260
else
+ − 261
let
+ − 262
val (_, lthy1) = Function.add_function raw_bn_funs raw_bn_eqs
+ − 263
Function_Common.default_config (pat_completeness_simp constr_thms) lthy
2304
+ − 264
2308
+ − 265
val (info, lthy2) = prove_termination (Local_Theory.restore lthy1)
+ − 266
val {fs, simps, inducts, ...} = info;
+ − 267
+ − 268
val raw_bn_induct = (the inducts)
+ − 269
val raw_bn_eqs = the simps
2142
c39d4fe31100
moved the exporting part into the parser (this is still a hack); re-added CoreHaskell again to the examples - there seems to be a problem with the variable name pat
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 270
2308
+ − 271
val raw_bn_info =
+ − 272
prep_bn_info lthy dt_names dts (map prop_of raw_bn_eqs)
+ − 273
in
+ − 274
(fs, raw_bn_eqs, raw_bn_info, raw_bn_induct, lthy2)
+ − 275
end
1941
+ − 276
*}
+ − 277
2304
+ − 278
2017
6a4049e1d68d
Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 279
2023
+ − 280
ML {* val cheat_fv_rsp = Unsynchronized.ref false *}
+ − 281
ML {* val cheat_alpha_bn_rsp = Unsynchronized.ref false *}
+ − 282
ML {* val cheat_supp_eq = Unsynchronized.ref false *}
2008
1bddffddc03f
attempted to remove dependency on (old) Fv and (old) Parser; lifting still uses Fv.thy; the examples do not work at the moment (with equivp proofs failing)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 283
1bddffddc03f
attempted to remove dependency on (old) Fv and (old) Parser; lifting still uses Fv.thy; the examples do not work at the moment (with equivp proofs failing)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 284
2046
73c50e913db6
tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 285
ML {*
73c50e913db6
tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 286
(* for testing porposes - to exit the procedure early *)
73c50e913db6
tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 287
exception TEST of Proof.context
73c50e913db6
tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 288
2125
60ee289a8c63
made out of STEPS a configuration value so that it can be set individually in each file
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 289
val (STEPS, STEPS_setup) = Attrib.config_int "STEPS" (K 10);
60ee289a8c63
made out of STEPS a configuration value so that it can be set individually in each file
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 290
60ee289a8c63
made out of STEPS a configuration value so that it can be set individually in each file
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 291
fun get_STEPS ctxt = Config.get ctxt STEPS
2046
73c50e913db6
tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 292
*}
2008
1bddffddc03f
attempted to remove dependency on (old) Fv and (old) Parser; lifting still uses Fv.thy; the examples do not work at the moment (with equivp proofs failing)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 293
2125
60ee289a8c63
made out of STEPS a configuration value so that it can be set individually in each file
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 294
setup STEPS_setup
60ee289a8c63
made out of STEPS a configuration value so that it can be set individually in each file
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 295
1941
+ − 296
ML {*
+ − 297
fun nominal_datatype2 dts bn_funs bn_eqs bclauses lthy =
+ − 298
let
2294
+ − 299
(* definition of the raw datatypes *)
2316
+ − 300
val _ = warning "Definition of raw datatypes";
2337
+ − 301
val (raw_dt_names, raw_dts, raw_bclauses, raw_bn_funs, raw_bn_eqs, lthy0) =
2308
+ − 302
if get_STEPS lthy > 0
2295
+ − 303
then raw_nominal_decls dts bn_funs bn_eqs bclauses lthy
2046
73c50e913db6
tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 304
else raise TEST lthy
1941
+ − 305
2304
+ − 306
val dtinfo = Datatype.the_info (ProofContext.theory_of lthy0) (hd raw_dt_names)
2143
871d8a5e0c67
somewhat simplified the main parsing function; failed to move a Note-statement to define_raw_perms
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 307
val {descr, sorts, ...} = dtinfo
2388
+ − 308
val all_raw_tys = all_dtyps descr sorts
+ − 309
val all_raw_ty_names = map (fn (_, (n, _, _)) => n) descr
2337
+ − 310
val all_raw_constrs =
+ − 311
flat (map (map (fn (c, _, _, _) => c)) (all_dtyp_constrs_types descr sorts))
+ − 312
2388
+ − 313
val dtinfos = map (Datatype.the_info (ProofContext.theory_of lthy0)) all_raw_ty_names
2046
73c50e913db6
tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 314
val inject_thms = flat (map #inject dtinfos);
73c50e913db6
tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 315
val distinct_thms = flat (map #distinct dtinfos);
2304
+ − 316
val constr_thms = inject_thms @ distinct_thms
2046
73c50e913db6
tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 317
val rel_dtinfos = List.take (dtinfos, (length dts));
2300
+ − 318
val raw_constrs_distinct = (map #distinct rel_dtinfos);
2388
+ − 319
val raw_induct_thm = #induct dtinfo;
+ − 320
val raw_induct_thms = #inducts dtinfo;
2046
73c50e913db6
tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 321
val exhaust_thms = map #exhaust dtinfos;
2388
+ − 322
val raw_size_trms = map (fn ty => Const (@{const_name size}, ty --> @{typ nat})) all_raw_tys
+ − 323
val raw_size_thms = Size.size_thms (ProofContext.theory_of lthy0) (hd raw_dt_names)
+ − 324
|> `(fn thms => (length thms) div 2)
2392
+ − 325
|> uncurry drop
2388
+ − 326
1999
+ − 327
2008
1bddffddc03f
attempted to remove dependency on (old) Fv and (old) Parser; lifting still uses Fv.thy; the examples do not work at the moment (with equivp proofs failing)
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 328
(* definitions of raw permutations *)
2316
+ − 329
val _ = warning "Definition of raw permutations";
2384
+ − 330
val ((raw_perm_funs, raw_perm_simps, raw_perm_laws), lthy2) =
2308
+ − 331
if get_STEPS lthy0 > 1
2388
+ − 332
then Local_Theory.theory_result (define_raw_perms descr sorts raw_induct_thm (length dts)) lthy0
2308
+ − 333
else raise TEST lthy0
2144
+ − 334
+ − 335
(* noting the raw permutations as eqvt theorems *)
+ − 336
val eqvt_attrib = Attrib.internal (K Nominal_ThmDecls.eqvt_add)
2384
+ − 337
val (_, lthy2a) = Local_Theory.note ((Binding.empty, [eqvt_attrib]), raw_perm_simps) lthy2
2143
871d8a5e0c67
somewhat simplified the main parsing function; failed to move a Note-statement to define_raw_perms
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 338
2105
+ − 339
val thy = Local_Theory.exit_global lthy2a;
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 340
val thy_name = Context.theory_name thy
2011
12ce87b55f97
tried to add some comments in the huge(!) nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 341
2142
c39d4fe31100
moved the exporting part into the parser (this is still a hack); re-added CoreHaskell again to the examples - there seems to be a problem with the variable name pat
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 342
(* definition of raw fv_functions *)
2316
+ − 343
val _ = warning "Definition of raw fv-functions";
2396
+ − 344
val lthy3 = Named_Target.init NONE thy;
1960
+ − 345
2346
+ − 346
val (raw_bns, raw_bn_eqs, raw_bn_info, raw_bn_induct, lthy3a) =
2308
+ − 347
if get_STEPS lthy3 > 2
2388
+ − 348
then raw_bn_decls all_raw_ty_names raw_dts raw_bn_funs raw_bn_eqs constr_thms lthy3
2046
73c50e913db6
tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 349
else raise TEST lthy3
2292
+ − 350
2308
+ − 351
val (raw_fvs, raw_fv_bns, raw_fv_defs, raw_fv_bns_induct, lthy3b) =
+ − 352
if get_STEPS lthy3a > 3
+ − 353
then define_raw_fvs descr sorts raw_bn_info raw_bclauses constr_thms lthy3a
+ − 354
else raise TEST lthy3a
+ − 355
2011
12ce87b55f97
tried to add some comments in the huge(!) nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 356
(* definition of raw alphas *)
2316
+ − 357
val _ = warning "Definition of alphas";
2300
+ − 358
val (alpha_trms, alpha_bn_trms, alpha_intros, alpha_cases, alpha_induct, lthy4) =
2308
+ − 359
if get_STEPS lthy3b > 4
+ − 360
then define_raw_alpha descr sorts raw_bn_info raw_bclauses raw_fvs lthy3b
+ − 361
else raise TEST lthy3b
2336
+ − 362
val alpha_tys = map (domain_type o fastype_of) alpha_trms
+ − 363
2300
+ − 364
(* definition of alpha-distinct lemmas *)
2316
+ − 365
val _ = warning "Distinct theorems";
2300
+ − 366
val (alpha_distincts, alpha_bn_distincts) =
+ − 367
mk_alpha_distincts lthy4 alpha_cases raw_constrs_distinct alpha_trms alpha_bn_trms raw_bn_info
+ − 368
2361
+ − 369
(* definition of alpha_eq_iff lemmas *)
+ − 370
(* they have a funny shape for the simplifier *)
2316
+ − 371
val _ = warning "Eq-iff theorems";
2387
+ − 372
val (alpha_eq_iff_simps, alpha_eq_iff) =
2295
+ − 373
if get_STEPS lthy > 5
2300
+ − 374
then mk_alpha_eq_iff lthy4 alpha_intros distinct_thms inject_thms alpha_cases
2295
+ − 375
else raise TEST lthy4
2022
+ − 376
2388
+ − 377
(* proving equivariance lemmas for bns, fvs, size and alpha *)
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 378
val _ = warning "Proving equivariance";
2305
+ − 379
val bn_eqvt =
2298
+ − 380
if get_STEPS lthy > 6
2384
+ − 381
then raw_prove_eqvt raw_bns raw_bn_induct (raw_bn_eqs @ raw_perm_simps) lthy4
2298
+ − 382
else raise TEST lthy4
+ − 383
2308
+ − 384
(* noting the bn_eqvt lemmas in a temprorary theory *)
2305
+ − 385
val add_eqvt = Attrib.internal (K Nominal_ThmDecls.eqvt_add)
2308
+ − 386
val lthy_tmp = snd (Local_Theory.note ((Binding.empty, [add_eqvt]), bn_eqvt) lthy4)
2305
+ − 387
+ − 388
val fv_eqvt =
2298
+ − 389
if get_STEPS lthy > 7
2384
+ − 390
then raw_prove_eqvt (raw_fvs @ raw_fv_bns) raw_fv_bns_induct (raw_fv_defs @ raw_perm_simps)
2388
+ − 391
(Local_Theory.restore lthy_tmp)
+ − 392
else raise TEST lthy4
+ − 393
2389
+ − 394
val raw_size_eqvt =
2388
+ − 395
if get_STEPS lthy > 8
+ − 396
then raw_prove_eqvt raw_size_trms raw_induct_thms (raw_size_thms @ raw_perm_simps)
+ − 397
(Local_Theory.restore lthy_tmp)
2389
+ − 398
|> map (rewrite_rule @{thms permute_nat_def[THEN eq_reflection]})
+ − 399
|> map (fn thm => thm RS @{thm sym})
2305
+ − 400
else raise TEST lthy4
+ − 401
2336
+ − 402
val lthy5 = snd (Local_Theory.note ((Binding.empty, [add_eqvt]), fv_eqvt) lthy_tmp)
2306
+ − 403
2336
+ − 404
val (alpha_eqvt, lthy6) =
2388
+ − 405
if get_STEPS lthy > 9
2336
+ − 406
then Nominal_Eqvt.equivariance true (alpha_trms @ alpha_bn_trms) alpha_induct alpha_intros lthy5
2311
+ − 407
else raise TEST lthy4
+ − 408
+ − 409
(* proving alpha equivalence *)
+ − 410
val _ = warning "Proving equivalence"
+ − 411
2316
+ − 412
val alpha_refl_thms =
2388
+ − 413
if get_STEPS lthy > 10
+ − 414
then raw_prove_refl alpha_trms alpha_bn_trms alpha_intros raw_induct_thm lthy6
2336
+ − 415
else raise TEST lthy6
2316
+ − 416
2311
+ − 417
val alpha_sym_thms =
2388
+ − 418
if get_STEPS lthy > 11
2336
+ − 419
then raw_prove_sym (alpha_trms @ alpha_bn_trms) alpha_intros alpha_induct lthy6
+ − 420
else raise TEST lthy6
2298
+ − 421
2311
+ − 422
val alpha_trans_thms =
2388
+ − 423
if get_STEPS lthy > 12
2311
+ − 424
then raw_prove_trans (alpha_trms @ alpha_bn_trms) (distinct_thms @ inject_thms)
2336
+ − 425
alpha_intros alpha_induct alpha_cases lthy6
+ − 426
else raise TEST lthy6
2311
+ − 427
2322
+ − 428
val alpha_equivp_thms =
2388
+ − 429
if get_STEPS lthy > 13
2336
+ − 430
then raw_prove_equivp alpha_trms alpha_refl_thms alpha_sym_thms alpha_trans_thms lthy6
+ − 431
else raise TEST lthy6
2322
+ − 432
2320
+ − 433
(* proving alpha implies alpha_bn *)
+ − 434
val _ = warning "Proving alpha implies bn"
+ − 435
+ − 436
val alpha_bn_imp_thms =
2388
+ − 437
if get_STEPS lthy > 14
2336
+ − 438
then raw_prove_bn_imp alpha_trms alpha_bn_trms alpha_intros alpha_induct lthy6
+ − 439
else raise TEST lthy6
2322
+ − 440
2397
+ − 441
(* respectfulness proofs *)
+ − 442
val raw_funs_rsp_aux = raw_fv_bn_rsp_aux alpha_trms alpha_bn_trms raw_fvs
2388
+ − 443
raw_bns raw_fv_bns alpha_induct (raw_bn_eqs @ raw_fv_defs) lthy6
2397
+ − 444
val raw_funs_rsp = map mk_funs_rsp raw_funs_rsp_aux
2388
+ − 445
2392
+ − 446
val raw_size_rsp = raw_size_rsp_aux (alpha_trms @ alpha_bn_trms) alpha_induct
+ − 447
(raw_size_thms @ raw_size_eqvt) lthy6
2397
+ − 448
|> map mk_funs_rsp
2392
+ − 449
2395
+ − 450
val raw_constrs_rsp = raw_constrs_rsp all_raw_constrs alpha_trms alpha_intros
2397
+ − 451
(alpha_bn_imp_thms @ raw_funs_rsp_aux) lthy6
+ − 452
+ − 453
val alpha_permute_rsp = map mk_alpha_permute_rsp alpha_eqvt
2384
+ − 454
2336
+ − 455
(* defining the quotient type *)
+ − 456
val _ = warning "Declaring the quotient types"
+ − 457
val qty_descr = map (fn (vs, bind, mx, _) => (vs, bind, mx)) dts
2339
+ − 458
val qty_binds = map (fn (_, bind, _, _) => bind) dts
+ − 459
val qty_names = map Name.of_binding qty_binds;
2337
+ − 460
val qty_full_names = map (Long_Name.qualify thy_name) qty_names (* not used *)
2336
+ − 461
+ − 462
val (qty_infos, lthy7) =
2388
+ − 463
if get_STEPS lthy > 15
2337
+ − 464
then qtype_defs qty_descr alpha_tys alpha_trms alpha_equivp_thms lthy6
2336
+ − 465
else raise TEST lthy6
+ − 466
+ − 467
val qtys = map #qtyp qty_infos
2339
+ − 468
+ − 469
(* defining of quotient term-constructors, binding functions, free vars functions *)
2378
+ − 470
val _ = warning "Defining the quotient constants"
2346
+ − 471
val qconstrs_descr =
2338
+ − 472
flat (map (fn (_, _, _, cs) => map (fn (b, _, mx) => (Name.of_binding b, mx)) cs) dts)
+ − 473
|> map2 (fn t => fn (b, mx) => (b, t, mx)) all_raw_constrs
+ − 474
2339
+ − 475
val qbns_descr =
2346
+ − 476
map2 (fn (b, _, mx) => fn t => (Name.of_binding b, t, mx)) bn_funs raw_bns
2339
+ − 477
+ − 478
val qfvs_descr =
2346
+ − 479
map2 (fn n => fn t => ("fv_" ^ n, t, NoSyn)) qty_names raw_fvs
2339
+ − 480
2346
+ − 481
val qfv_bns_descr =
+ − 482
map2 (fn (b, _, _) => fn t => ("fv_" ^ Name.of_binding b, t, NoSyn)) bn_funs raw_fv_bns
2339
+ − 483
2384
+ − 484
val qalpha_bns_descr =
+ − 485
map2 (fn (b, _, _) => fn t => ("alpha_" ^ Name.of_binding b, t, NoSyn)) bn_funs alpha_bn_trms
+ − 486
+ − 487
val (((((qconstrs_info, qbns_info), qfvs_info), qfv_bns_info), qalpha_bns_info), lthy8) =
2388
+ − 488
if get_STEPS lthy > 16
2346
+ − 489
then
+ − 490
lthy7
+ − 491
|> qconst_defs qtys qconstrs_descr
+ − 492
||>> qconst_defs qtys qbns_descr
+ − 493
||>> qconst_defs qtys qfvs_descr
+ − 494
||>> qconst_defs qtys qfv_bns_descr
2384
+ − 495
||>> qconst_defs qtys qalpha_bns_descr
2338
+ − 496
else raise TEST lthy7
+ − 497
2346
+ − 498
val qconstrs = map #qconst qconstrs_info
+ − 499
val qbns = map #qconst qbns_info
+ − 500
val qfvs = map #qconst qfvs_info
+ − 501
val qfv_bns = map #qconst qfv_bns_info
2384
+ − 502
val qalpha_bns = map #qconst qalpha_bns_info
2339
+ − 503
2388
+ − 504
(* temporary theorem bindings *)
2361
+ − 505
+ − 506
val (_, lthy8') = lthy8
+ − 507
|> Local_Theory.note ((@{binding "distinct"}, []), alpha_distincts)
2387
+ − 508
||>> Local_Theory.note ((@{binding "eq_iff"}, []), alpha_eq_iff)
+ − 509
||>> Local_Theory.note ((@{binding "eq_iff_simps"}, []), alpha_eq_iff_simps)
2361
+ − 510
||>> Local_Theory.note ((@{binding "fv_defs"}, []), raw_fv_defs)
+ − 511
||>> Local_Theory.note ((@{binding "perm_simps"}, []), raw_perm_simps)
2384
+ − 512
||>> Local_Theory.note ((@{binding "perm_laws"}, []), raw_perm_laws)
+ − 513
||>> Local_Theory.note ((@{binding "alpha_bn_imps"}, []), alpha_bn_imp_thms)
2388
+ − 514
||>> Local_Theory.note ((@{binding "funs_rsp"}, []), raw_funs_rsp)
2392
+ − 515
||>> Local_Theory.note ((@{binding "size_rsp"}, []), raw_size_rsp)
2395
+ − 516
||>> Local_Theory.note ((@{binding "constrs_rsp"}, []), raw_constrs_rsp)
2397
+ − 517
||>> Local_Theory.note ((@{binding "permute_rsp"}, []), alpha_permute_rsp)
+ − 518
2336
+ − 519
val _ =
2388
+ − 520
if get_STEPS lthy > 17
2361
+ − 521
then true else raise TEST lthy8'
2336
+ − 522
+ − 523
(* old stuff *)
+ − 524
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 525
val _ = warning "Proving respects";
2322
+ − 526
+ − 527
val bn_nos = map (fn (_, i, _) => i) raw_bn_info;
2346
+ − 528
val bns = raw_bns ~~ bn_nos;
2322
+ − 529
2300
+ − 530
val bns_rsp_pre' = build_fvbv_rsps alpha_trms alpha_induct raw_bn_eqs (map fst bns) lthy8;
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 531
val (bns_rsp_pre, lthy9) = fold_map (
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 532
fn (bn_t, _) => prove_const_rsp qtys Binding.empty [bn_t] (fn _ =>
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 533
resolve_tac bns_rsp_pre' 1)) bns lthy8;
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 534
val bns_rsp = flat (map snd bns_rsp_pre);
2017
6a4049e1d68d
Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 535
6a4049e1d68d
Add explicit cheats in NewParser and comment out the examples for outside use.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 536
fun fv_rsp_tac _ = if !cheat_fv_rsp then Skip_Proof.cheat_tac thy
2296
+ − 537
else fvbv_rsp_tac alpha_induct raw_fv_defs lthy8 1;
2322
+ − 538
+ − 539
val fv_alpha_all = combine_fv_alpha_bns (raw_fvs, raw_fv_bns) (alpha_trms, alpha_bn_trms) bn_nos
+ − 540
2300
+ − 541
val fv_rsps = prove_fv_rsp fv_alpha_all alpha_trms fv_rsp_tac lthy9;
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 542
val (fv_rsp_pre, lthy10) = fold_map
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 543
(fn fv => fn ctxt => prove_const_rsp qtys Binding.empty [fv]
2296
+ − 544
(fn _ => asm_simp_tac (HOL_ss addsimps fv_rsps) 1) ctxt) (raw_fvs @ raw_fv_bns) lthy9;
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 545
val fv_rsp = flat (map snd fv_rsp_pre);
2292
+ − 546
val (perms_rsp, lthy11) = prove_const_rsp qtys Binding.empty raw_perm_funs
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 547
(fn _ => asm_simp_tac (HOL_ss addsimps alpha_eqvt) 1) lthy10;
2023
+ − 548
fun alpha_bn_rsp_tac _ = if !cheat_alpha_bn_rsp then Skip_Proof.cheat_tac thy
+ − 549
else
2323
+ − 550
let val alpha_bn_rsp_pre = prove_alpha_bn_rsp alpha_trms alpha_induct (alpha_eq_iff @ alpha_distincts @ alpha_bn_distincts) alpha_equivp_thms exhaust_thms alpha_bn_trms lthy11 in asm_simp_tac (HOL_ss addsimps alpha_bn_rsp_pre) 1 end;
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 551
val (alpha_bn_rsps, lthy11a) = fold_map (fn cnst => prove_const_rsp qtys Binding.empty [cnst]
2300
+ − 552
alpha_bn_rsp_tac) alpha_bn_trms lthy11
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 553
fun const_rsp_tac _ =
2300
+ − 554
let val alpha_alphabn = prove_alpha_alphabn alpha_trms alpha_induct alpha_eq_iff alpha_bn_trms lthy11a
2322
+ − 555
in constr_rsp_tac alpha_eq_iff (fv_rsp @ bns_rsp @ alpha_refl_thms @ alpha_alphabn) 1 end
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 556
val (const_rsps, lthy12) = fold_map (fn cnst => prove_const_rsp qtys Binding.empty [cnst]
2346
+ − 557
const_rsp_tac) all_raw_constrs lthy11a
2339
+ − 558
val qfv_names = map (unsuffix "_raw" o Long_Name.base_name o fst o dest_Const) (raw_fvs @ raw_fv_bns)
2337
+ − 559
val dd = map2 (fn x => fn y => (x, y, NoSyn)) qfv_names (raw_fvs @ raw_fv_bns)
2346
+ − 560
val (qfv_info, lthy12a) = qconst_defs qtys dd lthy12;
+ − 561
val qfv_ts = map #qconst qfv_info
+ − 562
val qfv_defs = map #def qfv_info
2292
+ − 563
val (qfv_ts_nobn, qfv_ts_bn) = chop (length raw_perm_funs) qfv_ts;
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 564
val qbn_names = map (fn (b, _ , _) => Name.of_binding b) bn_funs
2346
+ − 565
val dd = map2 (fn x => fn y => (x, y, NoSyn)) qbn_names raw_bns
+ − 566
val (qbn_info, lthy12b) = qconst_defs qtys dd lthy12a;
+ − 567
val qbn_ts = map #qconst qbn_info
+ − 568
val qbn_defs = map #def qbn_info
2300
+ − 569
val qalpha_bn_names = map (unsuffix "_raw" o Long_Name.base_name o fst o dest_Const) alpha_bn_trms
2337
+ − 570
val dd = map2 (fn x => fn y => (x, y, NoSyn)) qalpha_bn_names alpha_bn_trms
2346
+ − 571
val (qalpha_info, lthy12c) = qconst_defs qtys dd lthy12b;
+ − 572
val qalpha_bn_trms = map #qconst qalpha_info
+ − 573
val qalphabn_defs = map #def qalpha_info
+ − 574
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 575
val _ = warning "Lifting permutations";
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 576
val thy = Local_Theory.exit_global lthy12c;
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 577
val perm_names = map (fn x => "permute_" ^ x) qty_names
2337
+ − 578
val dd = map2 (fn x => fn y => (x, y, NoSyn)) perm_names raw_perm_funs
2346
+ − 579
(* use Local_Theory.theory_result *)
2384
+ − 580
val thy' = qperm_defs qtys qty_full_names dd raw_perm_laws thy;
2396
+ − 581
val lthy13 = Named_Target.init NONE thy';
2346
+ − 582
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 583
val q_name = space_implode "_" qty_names;
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 584
fun suffix_bind s = Binding.qualify true q_name (Binding.name s);
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 585
val _ = warning "Lifting induction";
2346
+ − 586
val constr_names = map (Long_Name.base_name o fst o dest_Const) qconstrs;
2388
+ − 587
val q_induct = Rule_Cases.name constr_names (lift_thm qtys lthy13 raw_induct_thm);
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 588
fun note_suffix s th ctxt =
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 589
snd (Local_Theory.note ((suffix_bind s, []), th) ctxt);
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 590
fun note_simp_suffix s th ctxt =
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 591
snd (Local_Theory.note ((suffix_bind s, [Attrib.internal (K Simplifier.simp_add)]), th) ctxt);
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 592
val (_, lthy14) = Local_Theory.note ((suffix_bind "induct",
2011
12ce87b55f97
tried to add some comments in the huge(!) nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 593
[Attrib.internal (K (Rule_Cases.case_names constr_names))]),
12ce87b55f97
tried to add some comments in the huge(!) nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 594
[Rule_Cases.name constr_names q_induct]) lthy13;
2296
+ − 595
val q_inducts = Project_Rule.projects lthy13 (1 upto (length raw_fvs)) q_induct
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 596
val (_, lthy14a) = Local_Theory.note ((suffix_bind "inducts", []), q_inducts) lthy14;
2384
+ − 597
val q_perm = map (lift_thm qtys lthy14) raw_perm_simps;
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 598
val lthy15 = note_simp_suffix "perm" q_perm lthy14a;
2296
+ − 599
val q_fv = map (lift_thm qtys lthy15) raw_fv_defs;
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 600
val lthy16 = note_simp_suffix "fv" q_fv lthy15;
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 601
val q_bn = map (lift_thm qtys lthy16) raw_bn_eqs;
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 602
val lthy17 = note_simp_suffix "bn" q_bn lthy16;
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 603
val _ = warning "Lifting eq-iff";
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 604
(*val _ = map tracing (map PolyML.makestring alpha_eq_iff);*)
2300
+ − 605
val eq_iff_unfolded0 = map (Local_Defs.unfold lthy17 @{thms alphas}) alpha_eq_iff
2089
+ − 606
val eq_iff_unfolded1 = map (Local_Defs.unfold lthy17 @{thms Pair_eqvt}) eq_iff_unfolded0
+ − 607
val q_eq_iff_pre0 = map (lift_thm qtys lthy17) eq_iff_unfolded1;
+ − 608
val q_eq_iff_pre1 = map (Local_Defs.fold lthy17 @{thms Pair_eqvt}) q_eq_iff_pre0
+ − 609
val q_eq_iff_pre2 = map (Local_Defs.fold lthy17 @{thms alphas}) q_eq_iff_pre1
+ − 610
val q_eq_iff = map (Local_Defs.unfold lthy17 (Quotient_Info.id_simps_get lthy17)) q_eq_iff_pre2
2025
+ − 611
val (_, lthy18) = Local_Theory.note ((suffix_bind "eq_iff", []), q_eq_iff) lthy17;
2300
+ − 612
val q_dis = map (lift_thm qtys lthy18) alpha_distincts;
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 613
val lthy19 = note_simp_suffix "distinct" q_dis lthy18;
2305
+ − 614
val q_eqvt = map (lift_thm qtys lthy19) (bn_eqvt @ fv_eqvt);
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 615
val (_, lthy20) = Local_Theory.note ((Binding.empty,
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 616
[Attrib.internal (fn _ => Nominal_ThmDecls.eqvt_add)]), q_eqvt) lthy19;
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 617
val _ = warning "Supports";
2346
+ − 618
val supports = map (prove_supports lthy20 q_perm) qconstrs;
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 619
val fin_supp = HOLogic.conj_elims (prove_fs lthy20 q_induct supports qtys);
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 620
val thy3 = Local_Theory.exit_global lthy20;
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 621
val _ = warning "Instantiating FS";
2396
+ − 622
val lthy21 = Class.instantiation (qty_full_names, [], @{sort fs}) thy3;
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 623
fun tac _ = Class.intro_classes_tac [] THEN (ALLGOALS (resolve_tac fin_supp))
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 624
val lthy22 = Class.prove_instantiation_instance tac lthy21
2300
+ − 625
val fv_alpha_all = combine_fv_alpha_bns (qfv_ts_nobn, qfv_ts_bn) (alpha_trms, qalpha_bn_trms) bn_nos;
2001
7c8242a02f39
NewParser with Parser functionality, but some cheats included since the order of datayupes is wrong.
Cezary Kaliszyk <kaliszyk@in.tum.de>
diff
changeset
+ − 626
val (names, supp_eq_t) = supp_eq fv_alpha_all;
2020
+ − 627
val _ = warning "Support Equations";
2023
+ − 628
fun supp_eq_tac' _ = if !cheat_supp_eq then Skip_Proof.cheat_tac thy else
2025
+ − 629
supp_eq_tac q_induct q_fv q_perm q_eq_iff lthy22 1;
2023
+ − 630
val q_supp = HOLogic.conj_elims (Goal.prove lthy22 names [] supp_eq_t supp_eq_tac') handle e =>
+ − 631
let val _ = warning ("Support eqs failed") in [] end;
2020
+ − 632
val lthy23 = note_suffix "supp" q_supp lthy22;
1941
+ − 633
in
2020
+ − 634
(0, lthy23)
2046
73c50e913db6
tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 635
end handle TEST ctxt => (0, ctxt)
1941
+ − 636
*}
+ − 637
+ − 638
section {* Preparing and parsing of the specification *}
+ − 639
+ − 640
ML {*
+ − 641
(* parsing the datatypes and declaring *)
+ − 642
(* constructors in the local theory *)
+ − 643
fun prepare_dts dt_strs lthy =
+ − 644
let
+ − 645
val thy = ProofContext.theory_of lthy
+ − 646
+ − 647
fun mk_type full_tname tvrs =
+ − 648
Type (full_tname, map (fn a => TVar ((a, 0), [])) tvrs)
+ − 649
+ − 650
fun prep_cnstr full_tname tvs (cname, anno_tys, mx, _) =
+ − 651
let
+ − 652
val tys = map (Syntax.read_typ lthy o snd) anno_tys
+ − 653
val ty = mk_type full_tname tvs
+ − 654
in
+ − 655
((cname, tys ---> ty, mx), (cname, tys, mx))
+ − 656
end
+ − 657
+ − 658
fun prep_dt (tvs, tname, mx, cnstrs) =
+ − 659
let
+ − 660
val full_tname = Sign.full_name thy tname
+ − 661
val (cnstrs', cnstrs'') =
+ − 662
split_list (map (prep_cnstr full_tname tvs) cnstrs)
+ − 663
in
+ − 664
(cnstrs', (tvs, tname, mx, cnstrs''))
+ − 665
end
+ − 666
+ − 667
val (cnstrs, dts) = split_list (map prep_dt dt_strs)
+ − 668
in
+ − 669
lthy
+ − 670
|> Local_Theory.theory (Sign.add_consts_i (flat cnstrs))
+ − 671
|> pair dts
+ − 672
end
+ − 673
*}
+ − 674
+ − 675
ML {*
+ − 676
(* parsing the binding function specification and *)
+ − 677
(* declaring the functions in the local theory *)
+ − 678
fun prepare_bn_funs bn_fun_strs bn_eq_strs lthy =
+ − 679
let
+ − 680
val ((bn_funs, bn_eqs), _) =
+ − 681
Specification.read_spec bn_fun_strs bn_eq_strs lthy
+ − 682
+ − 683
fun prep_bn_fun ((bn, T), mx) = (bn, T, mx)
+ − 684
+ − 685
val bn_funs' = map prep_bn_fun bn_funs
+ − 686
in
+ − 687
lthy
+ − 688
|> Local_Theory.theory (Sign.add_consts_i bn_funs')
+ − 689
|> pair (bn_funs', bn_eqs)
+ − 690
end
+ − 691
*}
+ − 692
+ − 693
text {* associates every SOME with the index in the list; drops NONEs *}
+ − 694
ML {*
+ − 695
fun indexify xs =
+ − 696
let
+ − 697
fun mapp _ [] = []
+ − 698
| mapp i (NONE :: xs) = mapp (i + 1) xs
+ − 699
| mapp i (SOME x :: xs) = (x, i) :: mapp (i + 1) xs
+ − 700
in
+ − 701
mapp 0 xs
+ − 702
end
+ − 703
+ − 704
fun index_lookup xs x =
+ − 705
case AList.lookup (op=) xs x of
+ − 706
SOME x => x
+ − 707
| NONE => error ("Cannot find " ^ x ^ " as argument annotation.");
+ − 708
*}
+ − 709
+ − 710
ML {*
+ − 711
fun prepare_bclauses dt_strs lthy =
+ − 712
let
+ − 713
val annos_bclauses =
+ − 714
get_cnstrs dt_strs
+ − 715
|> map (map (fn (_, antys, _, bns) => (map fst antys, bns)))
+ − 716
+ − 717
fun prep_binder env bn_str =
+ − 718
case (Syntax.read_term lthy bn_str) of
+ − 719
Free (x, _) => (NONE, index_lookup env x)
+ − 720
| Const (a, T) $ Free (x, _) => (SOME (Const (a, T)), index_lookup env x)
+ − 721
| _ => error ("The term " ^ bn_str ^ " is not allowed as binding function.")
+ − 722
+ − 723
fun prep_body env bn_str = index_lookup env bn_str
+ − 724
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>
diff
changeset
+ − 725
fun prep_mode "bind" = Lst
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>
diff
changeset
+ − 726
| prep_mode "bind_set" = Set
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>
diff
changeset
+ − 727
| prep_mode "bind_res" = Res
1941
+ − 728
+ − 729
fun prep_bclause env (mode, binders, bodies) =
+ − 730
let
+ − 731
val binders' = map (prep_binder env) binders
+ − 732
val bodies' = map (prep_body env) bodies
+ − 733
in
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>
diff
changeset
+ − 734
BC (prep_mode mode, binders', bodies')
1941
+ − 735
end
+ − 736
+ − 737
fun prep_bclauses (annos, bclause_strs) =
+ − 738
let
+ − 739
val env = indexify annos (* for every label, associate the index *)
+ − 740
in
+ − 741
map (prep_bclause env) bclause_strs
+ − 742
end
+ − 743
in
+ − 744
map (map prep_bclauses) annos_bclauses
+ − 745
end
+ − 746
*}
+ − 747
1943
+ − 748
text {*
+ − 749
adds an empty binding clause for every argument
+ − 750
that is not already part of a binding clause
+ − 751
*}
+ − 752
1941
+ − 753
ML {*
+ − 754
fun included i bcs =
+ − 755
let
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>
diff
changeset
+ − 756
fun incl (BC (_, bns, bds)) = (member (op =) (map snd bns) i) orelse (member (op =) bds i)
1941
+ − 757
in
+ − 758
exists incl bcs
+ − 759
end
+ − 760
*}
+ − 761
+ − 762
ML {*
+ − 763
fun complete dt_strs bclauses =
+ − 764
let
+ − 765
val args =
+ − 766
get_cnstrs dt_strs
+ − 767
|> map (map (fn (_, antys, _, _) => length antys))
+ − 768
+ − 769
fun complt n bcs =
+ − 770
let
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>
diff
changeset
+ − 771
fun add bcs i = (if included i bcs then [] else [BC (Lst, [], [i])])
1941
+ − 772
in
+ − 773
bcs @ (flat (map_range (add bcs) n))
+ − 774
end
+ − 775
in
+ − 776
map2 (map2 complt) args bclauses
+ − 777
end
+ − 778
*}
+ − 779
+ − 780
ML {*
+ − 781
fun nominal_datatype2_cmd (dt_strs, bn_fun_strs, bn_eq_strs) lthy =
+ − 782
let
+ − 783
fun prep_typ (tvs, tname, mx, _) = (tname, length tvs, mx)
+ − 784
val lthy0 =
+ − 785
Local_Theory.theory (Sign.add_types (map prep_typ dt_strs)) lthy
1944
+ − 786
val (dts, lthy1) = prepare_dts dt_strs lthy0
+ − 787
val ((bn_funs, bn_eqs), lthy2) = prepare_bn_funs bn_fun_strs bn_eq_strs lthy1
1941
+ − 788
val bclauses = prepare_bclauses dt_strs lthy2
+ − 789
val bclauses' = complete dt_strs bclauses
+ − 790
in
+ − 791
nominal_datatype2 dts bn_funs bn_eqs bclauses' lthy |> snd
+ − 792
end
+ − 793
+ − 794
+ − 795
(* Command Keyword *)
+ − 796
2168
+ − 797
val _ = Outer_Syntax.local_theory "nominal_datatype" "test" Keyword.thy_decl
1941
+ − 798
(main_parser >> nominal_datatype2_cmd)
+ − 799
*}
+ − 800
2292
+ − 801
+ − 802
text {*
+ − 803
nominal_datatype2 does the following things in order:
1941
+ − 804
2292
+ − 805
Parser.thy/raw_nominal_decls
+ − 806
1) define the raw datatype
+ − 807
2) define the raw binding functions
2046
73c50e913db6
tuned and added some comments to the code; added also an exception for early exit of the nominal2_cmd function
Christian Urban <urbanc@in.tum.de>
diff
changeset
+ − 808
2292
+ − 809
Perm.thy/define_raw_perms
+ − 810
3) define permutations of the raw datatype and show that the raw type is
+ − 811
in the pt typeclass
+ − 812
+ − 813
Lift.thy/define_fv_alpha_export, Fv.thy/define_fv & define_alpha
+ − 814
4) define fv and fv_bn
+ − 815
5) define alpha and alpha_bn
1941
+ − 816
2292
+ − 817
Perm.thy/distinct_rel
+ − 818
6) prove alpha_distincts (C1 x \<notsimeq> C2 y ...) (Proof by cases; simp)
1941
+ − 819
2292
+ − 820
Tacs.thy/build_rel_inj
+ − 821
6) prove alpha_eq_iff (C1 x = C2 y \<leftrightarrow> P x y ...)
+ − 822
(left-to-right by intro rule, right-to-left by cases; simp)
+ − 823
Equivp.thy/prove_eqvt
+ − 824
7) prove bn_eqvt (common induction on the raw datatype)
+ − 825
8) prove fv_eqvt (common induction on the raw datatype with help of above)
+ − 826
Rsp.thy/build_alpha_eqvts
+ − 827
9) prove alpha_eqvt and alpha_bn_eqvt
+ − 828
(common alpha-induction, unfolding alpha_gen, permute of #* and =)
+ − 829
Equivp.thy/build_alpha_refl & Equivp.thy/build_equivps
+ − 830
10) prove that alpha and alpha_bn are equivalence relations
+ − 831
(common induction and application of 'compose' lemmas)
+ − 832
Lift.thy/define_quotient_types
+ − 833
11) define quotient types
+ − 834
Rsp.thy/build_fvbv_rsps
+ − 835
12) prove bn respects (common induction and simp with alpha_gen)
+ − 836
Rsp.thy/prove_const_rsp
+ − 837
13) prove fv respects (common induction and simp with alpha_gen)
+ − 838
14) prove permute respects (unfolds to alpha_eqvt)
+ − 839
Rsp.thy/prove_alpha_bn_rsp
+ − 840
15) prove alpha_bn respects
+ − 841
(alpha_induct then cases then sym and trans of the relations)
+ − 842
Rsp.thy/prove_alpha_alphabn
+ − 843
16) show that alpha implies alpha_bn (by unduction, needed in following step)
+ − 844
Rsp.thy/prove_const_rsp
+ − 845
17) prove respects for all datatype constructors
+ − 846
(unfold eq_iff and alpha_gen; introduce zero permutations; simp)
+ − 847
Perm.thy/quotient_lift_consts_export
+ − 848
18) define lifted constructors, fv, bn, alpha_bn, permutations
+ − 849
Perm.thy/define_lifted_perms
+ − 850
19) lift permutation zero and add properties to show that quotient type is in the pt typeclass
+ − 851
Lift.thy/lift_thm
+ − 852
20) lift permutation simplifications
+ − 853
21) lift induction
+ − 854
22) lift fv
+ − 855
23) lift bn
+ − 856
24) lift eq_iff
+ − 857
25) lift alpha_distincts
+ − 858
26) lift fv and bn eqvts
+ − 859
Equivp.thy/prove_supports
+ − 860
27) prove that union of arguments supports constructors
+ − 861
Equivp.thy/prove_fs
+ − 862
28) show that the lifted type is in fs typeclass (* by q_induct, supports *)
+ − 863
Equivp.thy/supp_eq
+ − 864
29) prove supp = fv
+ − 865
*}
1964
+ − 866
1941
+ − 867
+ − 868
+ − 869
end
+ − 870
+ − 871
+ − 872