264 val ((((raw_dt_names, (raw_bn_funs_loc, raw_bn_eqs_loc)), raw_bclauses), raw_bns), lthy1) = |
264 val ((((raw_dt_names, (raw_bn_funs_loc, raw_bn_eqs_loc)), raw_bclauses), raw_bns), lthy1) = |
265 raw_nominal_decls dts bn_funs bn_eqs bclauses lthy |
265 raw_nominal_decls dts bn_funs bn_eqs bclauses lthy |
266 |
266 |
267 val dtinfo = Datatype.the_info (ProofContext.theory_of lthy1) (hd raw_dt_names); |
267 val dtinfo = Datatype.the_info (ProofContext.theory_of lthy1) (hd raw_dt_names); |
268 val {descr, sorts, ...} = dtinfo; |
268 val {descr, sorts, ...} = dtinfo; |
|
269 fun nth_dtyp i = typ_of_dtyp descr sorts (DtRec i); |
|
270 val raw_tys = map (fn (i, _) => nth_dtyp i) descr; |
|
271 val all_typs = map (fn i => typ_of_dtyp descr sorts (DtRec i)) (map fst descr) |
|
272 |
|
273 val all_full_tnames = map (fn (_, (n, _, _)) => n) descr; |
|
274 val dtinfos = map (Datatype.the_info (ProofContext.theory_of lthy1)) all_full_tnames; |
|
275 val inject = flat (map #inject dtinfos); |
|
276 val distincts = flat (map #distinct dtinfos); |
|
277 val rel_dtinfos = List.take (dtinfos, (length dts)); |
|
278 val rel_distinct = map #distinct rel_dtinfos; |
|
279 val induct = #induct dtinfo; |
|
280 val exhausts = map #exhaust dtinfos; |
269 |
281 |
270 val ((raw_perm_def, raw_perm_simps, perms), lthy2) = |
282 val ((raw_perm_def, raw_perm_simps, perms), lthy2) = |
271 Local_Theory.theory_result (define_raw_perms dtinfo (length dts)) lthy1; |
283 Local_Theory.theory_result (define_raw_perms dtinfo (length dts)) lthy1; |
272 |
284 |
273 val morphism_2_0 = ProofContext.export_morphism lthy2 lthy |
285 val morphism_2_0 = ProofContext.export_morphism lthy2 lthy |
274 fun export_fun f (t, l) = (f t, map (map (apsnd (Option.map f))) l); |
286 fun export_fun f (t, l) = (f t, map (map (apsnd (Option.map f))) l); |
275 val raw_bns_exp = map (apsnd (map (export_fun (Morphism.term morphism_2_0)))) raw_bns; |
287 val raw_bns_exp = map (apsnd (map (export_fun (Morphism.term morphism_2_0)))) raw_bns; |
276 val bn_funs_decls = flat (map (fn (ith, l) => map (fn (bn, data) => (bn, ith, data)) l) raw_bns_exp); |
288 val bn_funs_decls = flat (map (fn (ith, l) => map (fn (bn, data) => (bn, ith, data)) l) raw_bns_exp); |
|
289 val raw_bn_eqs = ProofContext.export lthy2 lthy raw_bn_eqs_loc |
277 val thy = Local_Theory.exit_global lthy2; |
290 val thy = Local_Theory.exit_global lthy2; |
|
291 val thy_name = Context.theory_name thy |
278 val lthy3 = Theory_Target.init NONE thy; |
292 val lthy3 = Theory_Target.init NONE thy; |
279 |
293 val raw_bn_funs = map (fn (f, _, _) => f) bn_funs_decls; |
280 val ((fv, fvbn), fvsimps, lthy4) = define_raw_fv dtinfo bn_funs_decls raw_bclauses lthy3; |
294 |
281 val (((alpha_ts, alpha_intros), (alpha_cases, alpha_induct)), lthy5) = |
295 val ((fv, fvbn), fv_def, lthy3a) = define_raw_fv dtinfo bn_funs_decls raw_bclauses lthy3; |
282 define_raw_alpha dtinfo bn_funs_decls raw_bclauses fv lthy4; |
296 val (((alpha_ts, alpha_intros), (alpha_cases, alpha_induct)), lthy4) = |
283 in |
297 define_raw_alpha dtinfo bn_funs_decls raw_bclauses fv lthy3a; |
284 ((raw_dt_names, raw_bn_funs_loc, raw_bn_eqs_loc, raw_bclauses, raw_bns), lthy5) |
298 val (alpha_ts_nobn, alpha_ts_bn) = chop (length fv) alpha_ts |
|
299 val dts_names = map (fn (i, (s, _, _)) => (s, i)) (#descr dtinfo); |
|
300 val bn_tys = map (domain_type o fastype_of) raw_bn_funs; |
|
301 val bn_nos = map (dtyp_no_of_typ dts_names) bn_tys; |
|
302 val bns = raw_bn_funs ~~ bn_nos; |
|
303 val rel_dists = flat (map (distinct_rel lthy4 alpha_cases) |
|
304 (rel_distinct ~~ alpha_ts_nobn)); |
|
305 val rel_dists_bn = flat (map (distinct_rel lthy4 alpha_cases) |
|
306 ((map (fn i => nth rel_distinct i) bn_nos) ~~ alpha_ts_bn)) |
|
307 val alpha_eq_iff = build_rel_inj alpha_intros (inject @ distincts) alpha_cases lthy4 |
|
308 val _ = warning "Proving equivariance"; |
|
309 val (bv_eqvt, lthy5) = prove_eqvt raw_tys induct (raw_bn_eqs @ raw_perm_def) (map fst bns) lthy4 |
|
310 val (fv_eqvt, lthy6) = prove_eqvt raw_tys induct (fv_def @ raw_perm_def) (fv @ fvbn) lthy5 |
|
311 fun alpha_eqvt_tac' _ = Skip_Proof.cheat_tac thy |
|
312 val alpha_eqvt = build_alpha_eqvts alpha_ts alpha_eqvt_tac' lthy6; |
|
313 val _ = warning "Proving equivalence"; |
|
314 val fv_alpha_all = combine_fv_alpha_bns (fv, fvbn) (alpha_ts_nobn, alpha_ts_bn) bn_nos; |
|
315 val reflps = build_alpha_refl fv_alpha_all alpha_ts induct alpha_eq_iff lthy6; |
|
316 val alpha_equivp = |
|
317 build_equivps alpha_ts reflps alpha_induct |
|
318 inject alpha_eq_iff distincts alpha_cases alpha_eqvt lthy6; |
|
319 val qty_binds = map (fn (_, b, _, _) => b) dts; |
|
320 val qty_names = map Name.of_binding qty_binds; |
|
321 val qty_full_names = map (Long_Name.qualify thy_name) qty_names |
|
322 val (qtys, lthy7) = define_quotient_types qty_binds all_typs alpha_ts_nobn alpha_equivp lthy6; |
|
323 val const_names = map Name.of_binding (flat (map (fn (_, _, _, t) => map (fn (b, _, _) => b) t) dts)); |
|
324 val raw_consts = |
|
325 flat (map (fn (i, (_, _, l)) => |
|
326 map (fn (cname, dts) => |
|
327 Const (cname, map (typ_of_dtyp descr sorts) dts ---> |
|
328 typ_of_dtyp descr sorts (DtRec i))) l) descr); |
|
329 val (consts, const_defs, lthy8) = quotient_lift_consts_export qtys (const_names ~~ raw_consts) lthy7; |
|
330 val _ = warning "Proving respects"; |
|
331 val bns_rsp_pre' = build_fvbv_rsps alpha_ts alpha_induct raw_bn_eqs (map fst bns) lthy8; |
|
332 val (bns_rsp_pre, lthy9) = fold_map ( |
|
333 fn (bn_t, _) => prove_const_rsp qtys Binding.empty [bn_t] (fn _ => |
|
334 resolve_tac bns_rsp_pre' 1)) bns lthy8; |
|
335 val bns_rsp = flat (map snd bns_rsp_pre); |
|
336 (*val _ = map tracing (map PolyML.makestring fv_alpha_all);*) |
|
337 fun fv_rsp_tac _ = Skip_Proof.cheat_tac thy |
|
338 val fv_rsps = prove_fv_rsp fv_alpha_all alpha_ts fv_rsp_tac lthy9; |
|
339 val (fv_rsp_pre, lthy10) = fold_map |
|
340 (fn fv => fn ctxt => prove_const_rsp qtys Binding.empty [fv] |
|
341 (fn _ => asm_simp_tac (HOL_ss addsimps fv_rsps) 1) ctxt) (fv @ fvbn) lthy9; |
|
342 val fv_rsp = flat (map snd fv_rsp_pre); |
|
343 val (perms_rsp, lthy11) = prove_const_rsp qtys Binding.empty perms |
|
344 (fn _ => asm_simp_tac (HOL_ss addsimps alpha_eqvt) 1) lthy10; |
|
345 val (alpha_bn_rsps, lthy11a) = fold_map (fn cnst => prove_const_rsp qtys Binding.empty [cnst] |
|
346 (fn _ => Skip_Proof.cheat_tac thy)) alpha_ts_bn lthy11 |
|
347 fun const_rsp_tac _ = |
|
348 let val alpha_alphabn = prove_alpha_alphabn alpha_ts alpha_induct alpha_eq_iff alpha_ts_bn lthy11a |
|
349 in constr_rsp_tac alpha_eq_iff (fv_rsp @ bns_rsp @ reflps @ alpha_alphabn) 1 end |
|
350 val (const_rsps, lthy12) = fold_map (fn cnst => prove_const_rsp qtys Binding.empty [cnst] |
|
351 const_rsp_tac) raw_consts lthy11a |
|
352 val qfv_names = map (unsuffix "_raw" o Long_Name.base_name o fst o dest_Const) (fv @ fvbn) |
|
353 val (qfv_ts, qfv_defs, lthy12a) = quotient_lift_consts_export qtys (qfv_names ~~ (fv @ fvbn)) lthy12; |
|
354 val (qfv_ts_nobn, qfv_ts_bn) = chop (length perms) qfv_ts; |
|
355 val qbn_names = map (fn (b, _ , _) => Name.of_binding b) bn_funs |
|
356 val (qbn_ts, qbn_defs, lthy12b) = quotient_lift_consts_export qtys (qbn_names ~~ raw_bn_funs) lthy12a; |
|
357 val qalpha_bn_names = map (unsuffix "_raw" o Long_Name.base_name o fst o dest_Const) alpha_ts_bn |
|
358 val (qalpha_ts_bn, qalphabn_defs, lthy12c) = quotient_lift_consts_export qtys (qalpha_bn_names ~~ alpha_ts_bn) lthy12b; |
|
359 val _ = warning "Lifting permutations"; |
|
360 val thy = Local_Theory.exit_global lthy12c; |
|
361 val perm_names = map (fn x => "permute_" ^ x) qty_names |
|
362 val thy' = define_lifted_perms qtys qty_full_names (perm_names ~~ perms) raw_perm_simps thy; |
|
363 val lthy13 = Theory_Target.init NONE thy'; |
|
364 val q_name = space_implode "_" qty_names; |
|
365 fun suffix_bind s = Binding.qualify true q_name (Binding.name s); |
|
366 val _ = warning "Lifting induction"; |
|
367 val constr_names = map (Long_Name.base_name o fst o dest_Const) consts; |
|
368 val q_induct = Rule_Cases.name constr_names (lift_thm qtys lthy13 induct); |
|
369 fun note_suffix s th ctxt = |
|
370 snd (Local_Theory.note ((suffix_bind s, []), th) ctxt); |
|
371 fun note_simp_suffix s th ctxt = |
|
372 snd (Local_Theory.note ((suffix_bind s, [Attrib.internal (K Simplifier.simp_add)]), th) ctxt); |
|
373 val (_, lthy14) = Local_Theory.note ((suffix_bind "induct", |
|
374 [Attrib.internal (K (Rule_Cases.case_names constr_names))]), [Rule_Cases.name constr_names q_induct]) lthy13; |
|
375 val q_inducts = Project_Rule.projects lthy13 (1 upto (length fv)) q_induct |
|
376 val (_, lthy14a) = Local_Theory.note ((suffix_bind "inducts", []), q_inducts) lthy14; |
|
377 val q_perm = map (lift_thm qtys lthy14) raw_perm_def; |
|
378 val lthy15 = note_simp_suffix "perm" q_perm lthy14a; |
|
379 val q_fv = map (lift_thm qtys lthy15) fv_def; |
|
380 val lthy16 = note_simp_suffix "fv" q_fv lthy15; |
|
381 val q_bn = map (lift_thm qtys lthy16) raw_bn_eqs; |
|
382 val lthy17 = note_simp_suffix "bn" q_bn lthy16; |
|
383 val _ = warning "Lifting eq-iff"; |
|
384 (*val _ = map tracing (map PolyML.makestring alpha_eq_iff);*) |
|
385 val eq_iff_unfolded0 = map (Local_Defs.unfold lthy17 @{thms alphas3}) alpha_eq_iff |
|
386 val eq_iff_unfolded1 = map (Local_Defs.unfold lthy17 @{thms alphas2}) eq_iff_unfolded0 |
|
387 val eq_iff_unfolded2 = map (Local_Defs.unfold lthy17 @{thms alphas} ) eq_iff_unfolded1 |
|
388 val q_eq_iff_pre0 = map (lift_thm qtys lthy17) eq_iff_unfolded2; |
|
389 val q_eq_iff_pre1 = map (Local_Defs.fold lthy17 @{thms alphas3}) q_eq_iff_pre0 |
|
390 val q_eq_iff_pre2 = map (Local_Defs.fold lthy17 @{thms alphas2}) q_eq_iff_pre1 |
|
391 val q_eq_iff = map (Local_Defs.fold lthy17 @{thms alphas}) q_eq_iff_pre2 |
|
392 val (_, lthy18) = Local_Theory.note ((suffix_bind "eq_iff", []), q_eq_iff) lthy17; |
|
393 val q_dis = map (lift_thm qtys lthy18) rel_dists; |
|
394 val lthy19 = note_simp_suffix "distinct" q_dis lthy18; |
|
395 val q_eqvt = map (lift_thm qtys lthy19) (bv_eqvt @ fv_eqvt); |
|
396 val (_, lthy20) = Local_Theory.note ((Binding.empty, |
|
397 [Attrib.internal (fn _ => Nominal_ThmDecls.eqvt_add)]), q_eqvt) lthy19; |
|
398 val _ = warning "Supports"; |
|
399 val supports = map (prove_supports lthy20 q_perm) consts; |
|
400 val fin_supp = HOLogic.conj_elims (prove_fs lthy20 q_induct supports qtys); |
|
401 val thy3 = Local_Theory.exit_global lthy20; |
|
402 val _ = warning "Instantiating FS"; |
|
403 val lthy21 = Theory_Target.instantiation (qty_full_names, [], @{sort fs}) thy3; |
|
404 fun tac _ = Class.intro_classes_tac [] THEN (ALLGOALS (resolve_tac fin_supp)) |
|
405 val lthy22 = Class.prove_instantiation_instance tac lthy21 |
|
406 val fv_alpha_all = combine_fv_alpha_bns (qfv_ts_nobn, qfv_ts_bn) (alpha_ts_nobn, qalpha_ts_bn) bn_nos; |
|
407 val (names, supp_eq_t) = supp_eq fv_alpha_all; |
|
408 val _ = warning "Support Equations"; |
|
409 val q_supp = HOLogic.conj_elims (Goal.prove lthy22 names [] supp_eq_t (fn _ => supp_eq_tac q_induct q_fv q_perm q_eq_iff lthy22 1)) handle _ => []; |
|
410 val lthy23 = note_suffix "supp" q_supp lthy22; |
|
411 in |
|
412 (0, lthy23) |
285 end |
413 end |
286 *} |
414 *} |
287 |
415 |
288 section {* Preparing and parsing of the specification *} |
416 section {* Preparing and parsing of the specification *} |
289 |
417 |