src/HOL/Tools/BNF/bnf_fp_def_sugar.ML
changeset 57260 8747af0d1012
parent 57206 d9be905d6283
child 57279 88263522c31e
equal deleted inserted replaced
57259:3a448982a74a 57260:8747af0d1012
    24 
    24 
    25   val morph_lfp_sugar_thms: morphism -> lfp_sugar_thms -> lfp_sugar_thms
    25   val morph_lfp_sugar_thms: morphism -> lfp_sugar_thms -> lfp_sugar_thms
    26   val transfer_lfp_sugar_thms: Proof.context -> lfp_sugar_thms -> lfp_sugar_thms
    26   val transfer_lfp_sugar_thms: Proof.context -> lfp_sugar_thms -> lfp_sugar_thms
    27 
    27 
    28   type gfp_sugar_thms =
    28   type gfp_sugar_thms =
    29     ((thm list * thm) list * Args.src list)
    29     ((thm list * thm) list * (Args.src list * Args.src list))
    30     * (thm list list * Args.src list)
    30     * (thm list list * Args.src list)
    31     * (thm list list * Args.src list)
    31     * (thm list list * Args.src list)
    32     * (thm list list * Args.src list)
    32     * (thm list list * Args.src list)
    33     * (thm list list list * Args.src list)
    33     * (thm list list list * Args.src list)
    34 
    34 
   278 
   278 
   279 val transfer_lfp_sugar_thms =
   279 val transfer_lfp_sugar_thms =
   280   morph_lfp_sugar_thms o Morphism.transfer_morphism o Proof_Context.theory_of;
   280   morph_lfp_sugar_thms o Morphism.transfer_morphism o Proof_Context.theory_of;
   281 
   281 
   282 type gfp_sugar_thms =
   282 type gfp_sugar_thms =
   283   ((thm list * thm) list * Args.src list)
   283   ((thm list * thm) list * (Args.src list * Args.src list))
   284   * (thm list list * Args.src list)
   284   * (thm list list * Args.src list)
   285   * (thm list list * Args.src list)
   285   * (thm list list * Args.src list)
   286   * (thm list list * Args.src list)
   286   * (thm list list * Args.src list)
   287   * (thm list list list * Args.src list);
   287   * (thm list list list * Args.src list);
   288 
   288 
   289 fun morph_gfp_sugar_thms phi ((coinducts_pairs, coinduct_attrs),
   289 fun morph_gfp_sugar_thms phi ((coinducts_pairs, coinduct_attrs_pair),
   290     (corecss, corec_attrs), (disc_corecss, disc_corec_attrs),
   290     (corecss, corec_attrs), (disc_corecss, disc_corec_attrs),
   291     (disc_corec_iffss, disc_corec_iff_attrs), (sel_corecsss, sel_corec_attrs)) =
   291     (disc_corec_iffss, disc_corec_iff_attrs), (sel_corecsss, sel_corec_attrs)) =
   292   ((map (apfst (map (Morphism.thm phi)) o apsnd (Morphism.thm phi)) coinducts_pairs,
   292   ((map (apfst (map (Morphism.thm phi)) o apsnd (Morphism.thm phi)) coinducts_pairs,
   293     coinduct_attrs),
   293     coinduct_attrs_pair),
   294    (map (map (Morphism.thm phi)) corecss, corec_attrs),
   294    (map (map (Morphism.thm phi)) corecss, corec_attrs),
   295    (map (map (Morphism.thm phi)) disc_corecss, disc_corec_attrs),
   295    (map (map (Morphism.thm phi)) disc_corecss, disc_corec_attrs),
   296    (map (map (Morphism.thm phi)) disc_corec_iffss, disc_corec_iff_attrs),
   296    (map (map (Morphism.thm phi)) disc_corec_iffss, disc_corec_iff_attrs),
   297    (map (map (map (Morphism.thm phi))) sel_corecsss, sel_corec_attrs)) : gfp_sugar_thms;
   297    (map (map (map (Morphism.thm phi))) sel_corecsss, sel_corec_attrs)) : gfp_sugar_thms;
   298 
   298 
   686             mk_coinduct_tac ctxt nesting_rel_eqs nn ns dtor_coinduct' pre_rel_defs fp_abs_inverses
   686             mk_coinduct_tac ctxt nesting_rel_eqs nn ns dtor_coinduct' pre_rel_defs fp_abs_inverses
   687               abs_inverses dtor_ctors exhausts ctr_defss disc_thmsss sel_thmsss)
   687               abs_inverses dtor_ctors exhausts ctr_defss disc_thmsss sel_thmsss)
   688           |> singleton (Proof_Context.export names_lthy lthy)
   688           |> singleton (Proof_Context.export names_lthy lthy)
   689           |> Thm.close_derivation;
   689           |> Thm.close_derivation;
   690 
   690 
   691         fun postproc nn thm =
   691         val postproc =
   692           Thm.permute_prems 0 nn
   692           Drule.zero_var_indexes
   693             (if nn = 1 then thm RS mp else funpow nn (fn thm => reassoc_conjs (thm RS mp_conj)) thm)
   693           #> `(conj_dests nn)
   694           |> Drule.zero_var_indexes
   694           #>> map (fn thm => Thm.permute_prems 0 nn (thm RS mp))
   695           |> `(conj_dests nn);
   695           ##> (fn thm => Thm.permute_prems 0 nn
       
   696             (if nn = 1 then thm RS mp
       
   697              else funpow nn (fn thm => reassoc_conjs (thm RS mp_conj)) thm));
   696 
   698 
   697         val rel_eqs = map rel_eq_of_bnf pre_bnfs;
   699         val rel_eqs = map rel_eq_of_bnf pre_bnfs;
   698         val rel_monos = map rel_mono_of_bnf pre_bnfs;
   700         val rel_monos = map rel_mono_of_bnf pre_bnfs;
   699         val dtor_coinducts =
   701         val dtor_coinducts =
   700           [dtor_coinduct, mk_strong_coinduct_thm dtor_coinduct rel_eqs rel_monos mk_vimage2p lthy]
   702           [dtor_coinduct, mk_strong_coinduct_thm dtor_coinduct rel_eqs rel_monos mk_vimage2p lthy]
   701       in
   703       in
   702         map2 (postproc nn oo prove) dtor_coinducts goals
   704         map2 (postproc oo prove) dtor_coinducts goals
   703       end;
   705       end;
   704 
   706 
   705     fun mk_coinduct_concls ms discs ctrs =
   707     fun mk_coinduct_concls ms discs ctrs =
   706       let
   708       let
   707         fun mk_disc_concl disc = [name_of_disc disc];
   709         fun mk_disc_concl disc = [name_of_disc disc];
   807     fun mk_sel_corec_thms corec_thmss =
   809     fun mk_sel_corec_thms corec_thmss =
   808       map3 (map3 (map2 o mk_sel_corec_thm)) corec_thmss selsss sel_thmsss;
   810       map3 (map3 (map2 o mk_sel_corec_thm)) corec_thmss selsss sel_thmsss;
   809 
   811 
   810     val sel_corec_thmsss = mk_sel_corec_thms corec_thmss;
   812     val sel_corec_thmsss = mk_sel_corec_thms corec_thmss;
   811 
   813 
   812     val coinduct_consumes_attr = Attrib.internal (K (Rule_Cases.consumes nn));
   814     val common_coinduct_consumes_attr = Attrib.internal (K (Rule_Cases.consumes nn));
       
   815     val coinduct_consumes_attr = Attrib.internal (K (Rule_Cases.consumes 1));
       
   816 
   813     val coinduct_case_names_attr = Attrib.internal (K (Rule_Cases.case_names coinduct_cases));
   817     val coinduct_case_names_attr = Attrib.internal (K (Rule_Cases.case_names coinduct_cases));
   814     val coinduct_case_concl_attrs =
   818     val coinduct_case_concl_attrs =
   815       map2 (fn casex => fn concls =>
   819       map2 (fn casex => fn concls =>
   816           Attrib.internal (K (Rule_Cases.case_conclusion (casex, concls))))
   820           Attrib.internal (K (Rule_Cases.case_conclusion (casex, concls))))
   817         coinduct_cases coinduct_conclss;
   821         coinduct_cases coinduct_conclss;
   818     val coinduct_case_attrs =
   822 
       
   823     val common_coinduct_attrs =
       
   824       common_coinduct_consumes_attr :: coinduct_case_names_attr :: coinduct_case_concl_attrs;
       
   825     val coinduct_attrs =
   819       coinduct_consumes_attr :: coinduct_case_names_attr :: coinduct_case_concl_attrs;
   826       coinduct_consumes_attr :: coinduct_case_names_attr :: coinduct_case_concl_attrs;
   820   in
   827   in
   821     ((coinduct_thms_pairs, coinduct_case_attrs),
   828     ((coinduct_thms_pairs, (coinduct_attrs, common_coinduct_attrs)),
   822      (corec_thmss, code_nitpicksimp_attrs),
   829      (corec_thmss, code_nitpicksimp_attrs),
   823      (disc_corec_thmss, []),
   830      (disc_corec_thmss, []),
   824      (disc_corec_iff_thmss, simp_attrs),
   831      (disc_corec_iff_thmss, simp_attrs),
   825      (sel_corec_thmsss, simp_attrs))
   832      (sel_corec_thmsss, simp_attrs))
   826   end;
   833   end;
  1174                   val (((Ds, As), Bs), names_lthy) = lthy
  1181                   val (((Ds, As), Bs), names_lthy) = lthy
  1175                     |> mk_TFrees (dead_of_bnf fp_bnf)
  1182                     |> mk_TFrees (dead_of_bnf fp_bnf)
  1176                     ||>> mk_TFrees (live_of_bnf fp_bnf)
  1183                     ||>> mk_TFrees (live_of_bnf fp_bnf)
  1177                     ||>> mk_TFrees (live_of_bnf fp_bnf);
  1184                     ||>> mk_TFrees (live_of_bnf fp_bnf);
  1178                   val TA as Type (_, ADs) = mk_T_of_bnf Ds As fp_bnf;
  1185                   val TA as Type (_, ADs) = mk_T_of_bnf Ds As fp_bnf;
  1179                   val TB as Type (_, BDs) = mk_T_of_bnf Ds Bs fp_bnf;
  1186                   val Type (_, BDs) = mk_T_of_bnf Ds Bs fp_bnf;
  1180                   val fTs = map2 (curry op -->) As Bs;
  1187                   val fTs = map2 (curry op -->) As Bs;
  1181                   val ((fs, ta), names_lthy) = names_lthy
  1188                   val ((fs, ta), names_lthy) = names_lthy
  1182                     |> mk_Frees "f" fTs
  1189                     |> mk_Frees "f" fTs
  1183                     ||>> yield_singleton (mk_Frees "a") TA;
  1190                     ||>> yield_singleton (mk_Frees "a") TA;
  1184                   val map_term = mk_map_of_bnf Ds As Bs fp_bnf;
  1191                   val map_term = mk_map_of_bnf Ds As Bs fp_bnf;
  1464     fun derive_note_coinduct_corecs_thms_for_types
  1471     fun derive_note_coinduct_corecs_thms_for_types
  1465         ((((mapss, rel_injectss, rel_distinctss, setss), (_, _, ctr_defss, ctr_sugars)),
  1472         ((((mapss, rel_injectss, rel_distinctss, setss), (_, _, ctr_defss, ctr_sugars)),
  1466           (corecs, corec_defs)), lthy) =
  1473           (corecs, corec_defs)), lthy) =
  1467       let
  1474       let
  1468         val (([(coinduct_thms, coinduct_thm), (strong_coinduct_thms, strong_coinduct_thm)],
  1475         val (([(coinduct_thms, coinduct_thm), (strong_coinduct_thms, strong_coinduct_thm)],
  1469               coinduct_attrs),
  1476               (coinduct_attrs, common_coinduct_attrs)),
  1470              (corec_thmss, corec_attrs), (disc_corec_thmss, disc_corec_attrs),
  1477              (corec_thmss, corec_attrs), (disc_corec_thmss, disc_corec_attrs),
  1471              (disc_corec_iff_thmss, disc_corec_iff_attrs), (sel_corec_thmsss, sel_corec_attrs)) =
  1478              (disc_corec_iff_thmss, disc_corec_iff_attrs), (sel_corec_thmsss, sel_corec_attrs)) =
  1472           derive_coinduct_corecs_thms_for_types pre_bnfs (the corecs_args_types) xtor_co_induct
  1479           derive_coinduct_corecs_thms_for_types pre_bnfs (the corecs_args_types) xtor_co_induct
  1473             dtor_injects dtor_ctors xtor_co_rec_thms nesting_bnfs fpTs Cs Xs ctrXs_Tsss kss mss ns
  1480             dtor_injects dtor_ctors xtor_co_rec_thms nesting_bnfs fpTs Cs Xs ctrXs_Tsss kss mss ns
  1474             abs_inverses abs_inverses I ctr_defss ctr_sugars corecs corec_defs
  1481             abs_inverses abs_inverses I ctr_defss ctr_sugars corecs corec_defs
  1485             (map3 flat_corec_thms disc_corec_thmss disc_corec_iff_thmss sel_corec_thmss)
  1492             (map3 flat_corec_thms disc_corec_thmss disc_corec_iff_thmss sel_corec_thmss)
  1486             mapss rel_injectss rel_distinctss setss;
  1493             mapss rel_injectss rel_distinctss setss;
  1487 
  1494 
  1488         val common_notes =
  1495         val common_notes =
  1489           (if nn > 1 then
  1496           (if nn > 1 then
  1490              [(coinductN, [coinduct_thm], coinduct_attrs),
  1497              [(coinductN, [coinduct_thm], common_coinduct_attrs),
  1491               (strong_coinductN, [strong_coinduct_thm], coinduct_attrs)]
  1498               (strong_coinductN, [strong_coinduct_thm], common_coinduct_attrs)]
  1492            else
  1499            else
  1493              [])
  1500              [])
  1494           |> massage_simple_notes fp_common_name;
  1501           |> massage_simple_notes fp_common_name;
  1495 
  1502 
  1496         val notes =
  1503         val notes =