src/HOL/Tools/BNF/bnf_fp_n2m_sugar.ML
changeset 58335 a5a3b576fcfb
parent 58315 6d8458bc6e27
child 58340 5f6f48e87de6
equal deleted inserted replaced
58334:7553a1bcecb7 58335:a5a3b576fcfb
     9 sig
     9 sig
    10   val unfold_lets_splits: term -> term
    10   val unfold_lets_splits: term -> term
    11   val unfold_splits_lets: term -> term
    11   val unfold_splits_lets: term -> term
    12   val dest_map: Proof.context -> string -> term -> term * term list
    12   val dest_map: Proof.context -> string -> term -> term * term list
    13 
    13 
    14   val mutualize_fp_sugars: BNF_Util.fp_kind -> int list -> binding list -> typ list -> term list ->
    14   val mutualize_fp_sugars: (string -> bool) -> BNF_Util.fp_kind -> int list -> binding list ->
    15     term list list list list -> BNF_FP_Def_Sugar.fp_sugar list -> local_theory ->
    15     typ list -> term list -> term list list list list -> BNF_FP_Def_Sugar.fp_sugar list ->
       
    16     local_theory ->
    16     (BNF_FP_Def_Sugar.fp_sugar list
    17     (BNF_FP_Def_Sugar.fp_sugar list
    17      * (BNF_FP_Def_Sugar.lfp_sugar_thms option * BNF_FP_Def_Sugar.gfp_sugar_thms option))
    18      * (BNF_FP_Def_Sugar.lfp_sugar_thms option * BNF_FP_Def_Sugar.gfp_sugar_thms option))
    18     * local_theory
    19     * local_theory
    19   val nested_to_mutual_fps: BNF_Util.fp_kind -> binding list -> typ list -> term list ->
    20   val nested_to_mutual_fps: (string -> bool) -> BNF_Util.fp_kind -> binding list -> typ list ->
    20     (term * term list list) list list -> local_theory ->
    21     term list -> (term * term list list) list list -> local_theory ->
    21     (typ list * int list * BNF_FP_Def_Sugar.fp_sugar list
    22     (typ list * int list * BNF_FP_Def_Sugar.fp_sugar list
    22      * (BNF_FP_Def_Sugar.lfp_sugar_thms option * BNF_FP_Def_Sugar.gfp_sugar_thms option))
    23      * (BNF_FP_Def_Sugar.lfp_sugar_thms option * BNF_FP_Def_Sugar.gfp_sugar_thms option))
    23     * local_theory
    24     * local_theory
    24 end;
    25 end;
    25 
    26 
   114 fun get_indices callers t =
   115 fun get_indices callers t =
   115   callers
   116   callers
   116   |> map_index (fn (i, v) => if exists_subterm (equal v) t then SOME i else NONE)
   117   |> map_index (fn (i, v) => if exists_subterm (equal v) t then SOME i else NONE)
   117   |> map_filter I;
   118   |> map_filter I;
   118 
   119 
   119 fun mutualize_fp_sugars fp cliques bs fpTs callers callssss fp_sugars0 no_defs_lthy =
   120 fun mutualize_fp_sugars plugins fp cliques bs fpTs callers callssss fp_sugars0 no_defs_lthy =
   120   let
   121   let
   121     val thy = Proof_Context.theory_of no_defs_lthy;
   122     val thy = Proof_Context.theory_of no_defs_lthy;
   122 
   123 
   123     val qsotm = quote o Syntax.string_of_term no_defs_lthy;
   124     val qsotm = quote o Syntax.string_of_term no_defs_lthy;
   124 
   125 
   267           |>> split_list;
   268           |>> split_list;
   268 
   269 
   269         val ((common_co_inducts, co_inductss', co_rec_thmss, co_rec_disc_thmss, co_rec_sel_thmsss),
   270         val ((common_co_inducts, co_inductss', co_rec_thmss, co_rec_disc_thmss, co_rec_sel_thmsss),
   270              fp_sugar_thms) =
   271              fp_sugar_thms) =
   271           if fp = Least_FP then
   272           if fp = Least_FP then
   272             derive_induct_recs_thms_for_types pre_bnfs recs_args_types xtor_co_induct
   273             derive_induct_recs_thms_for_types plugins pre_bnfs recs_args_types xtor_co_induct
   273               xtor_co_rec_thms live_nesting_bnfs fp_nesting_bnfs fpTs Cs Xs ctrXs_Tsss
   274               xtor_co_rec_thms live_nesting_bnfs fp_nesting_bnfs fpTs Cs Xs ctrXs_Tsss
   274               fp_abs_inverses fp_type_definitions abs_inverses ctrss ctr_defss co_recs co_rec_defs
   275               fp_abs_inverses fp_type_definitions abs_inverses ctrss ctr_defss co_recs co_rec_defs
   275               lthy
   276               lthy
   276             |> `(fn ((inducts, induct, _), (rec_thmss, _)) =>
   277             |> `(fn ((inducts, induct, _), (rec_thmss, _)) =>
   277               ([induct], [inducts], rec_thmss, replicate nn [], replicate nn []))
   278               ([induct], [inducts], rec_thmss, replicate nn [], replicate nn []))
   330     f (T, U) #> (if s = s' then fold (fold_subtype_pairs f) (Ts ~~ Us) else I)
   331     f (T, U) #> (if s = s' then fold (fold_subtype_pairs f) (Ts ~~ Us) else I)
   331   | fold_subtype_pairs f TU = f TU;
   332   | fold_subtype_pairs f TU = f TU;
   332 
   333 
   333 val impossible_caller = Bound ~1;
   334 val impossible_caller = Bound ~1;
   334 
   335 
   335 fun nested_to_mutual_fps fp actual_bs actual_Ts actual_callers actual_callssss0 lthy =
   336 fun nested_to_mutual_fps plugins fp actual_bs actual_Ts actual_callers actual_callssss0 lthy =
   336   let
   337   let
   337     val qsoty = quote o Syntax.string_of_typ lthy;
   338     val qsoty = quote o Syntax.string_of_typ lthy;
   338     val qsotys = space_implode " or " o map qsoty;
   339     val qsotys = space_implode " or " o map qsoty;
   339 
   340 
   340     fun not_co_datatype0 T = error (qsoty T ^ " is not a " ^ co_prefix fp ^ "datatype");
   341     fun not_co_datatype0 T = error (qsoty T ^ " is not a " ^ co_prefix fp ^ "datatype");
   454 
   455 
   455     val ((perm_fp_sugars, fp_sugar_thms), lthy) =
   456     val ((perm_fp_sugars, fp_sugar_thms), lthy) =
   456       if length perm_Tss = 1 then
   457       if length perm_Tss = 1 then
   457         ((perm_fp_sugars0, (NONE, NONE)), lthy)
   458         ((perm_fp_sugars0, (NONE, NONE)), lthy)
   458       else
   459       else
   459         mutualize_fp_sugars fp perm_cliques perm_bs perm_frozen_gen_Ts perm_callers perm_callssss
   460         mutualize_fp_sugars plugins fp perm_cliques perm_bs perm_frozen_gen_Ts perm_callers
   460           perm_fp_sugars0 lthy;
   461           perm_callssss perm_fp_sugars0 lthy;
   461 
   462 
   462     val fp_sugars = unpermute perm_fp_sugars;
   463     val fp_sugars = unpermute perm_fp_sugars;
   463   in
   464   in
   464     ((missing_Ts, perm_kks, fp_sugars, fp_sugar_thms), lthy)
   465     ((missing_Ts, perm_kks, fp_sugars, fp_sugar_thms), lthy)
   465   end;
   466   end;