src/HOL/BNF/Tools/bnf_fp_def_sugar.ML
changeset 51903 126f8d11f873
parent 51902 1ab4214a08f9
child 51904 ba735ac9044a
equal deleted inserted replaced
51902:1ab4214a08f9 51903:126f8d11f873
    25   val fp_sugar_of: Proof.context -> string -> fp_sugar option
    25   val fp_sugar_of: Proof.context -> string -> fp_sugar option
    26 
    26 
    27   val exists_subtype_in: typ list -> typ -> bool
    27   val exists_subtype_in: typ list -> typ -> bool
    28   val indexify_fst: ''a list -> (int -> ''a * 'b -> 'c) -> ''a * 'b -> 'c
    28   val indexify_fst: ''a list -> (int -> ''a * 'b -> 'c) -> ''a * 'b -> 'c
    29   val mk_fp_iter: bool -> typ list -> typ list -> term list -> term list * typ list
    29   val mk_fp_iter: bool -> typ list -> typ list -> term list -> term list * typ list
       
    30   val mk_un_fold_co_rec_prelims: bool -> typ list -> typ list -> typ list -> int list ->
       
    31     int list list -> term list -> term list -> Proof.context ->
       
    32     (term list * term list * ((term list list * typ list list * term list list list)
       
    33        * (term list list * typ list list * term list list list)) option
       
    34      * (term list * term list list
       
    35         * ((term list list * term list list list list * term list list list list)
       
    36            * (typ list * typ list list list * typ list list))
       
    37         * ((term list list * term list list list list * term list list list list)
       
    38            * (typ list * typ list list list * typ list list))) option)
       
    39     * Proof.context
    30   val build_map: local_theory -> (typ * typ -> term) -> typ * typ -> term
    40   val build_map: local_theory -> (typ * typ -> term) -> typ * typ -> term
    31 
    41 
    32   val mk_iter_fun_arg_types_pairsss: typ list -> int list -> int list list -> term ->
    42   val mk_iter_fun_arg_types_pairsss: typ list -> int list -> int list list -> term ->
    33     (typ list * typ list) list list list
    43     (typ list * typ list) list list list
    34   val mk_folds_recs: Proof.context -> typ list -> typ list -> typ list -> int list ->
    44   val mk_folds_recs: Proof.context -> typ list -> typ list -> typ list -> int list ->
   154 val mp_conj = @{thm mp_conj};
   164 val mp_conj = @{thm mp_conj};
   155 
   165 
   156 val simp_attrs = @{attributes [simp]};
   166 val simp_attrs = @{attributes [simp]};
   157 val code_simp_attrs = Code.add_default_eqn_attrib :: simp_attrs;
   167 val code_simp_attrs = Code.add_default_eqn_attrib :: simp_attrs;
   158 
   168 
   159 fun split_list4 [] = ([], [], [], [])
       
   160   | split_list4 ((x1, x2, x3, x4) :: xs) =
       
   161     let val (xs1, xs2, xs3, xs4) = split_list4 xs;
       
   162     in (x1 :: xs1, x2 :: xs2, x3 :: xs3, x4 :: xs4) end;
       
   163 
       
   164 val exists_subtype_in = Term.exists_subtype o member (op =);
   169 val exists_subtype_in = Term.exists_subtype o member (op =);
   165 
   170 
   166 fun resort_tfree S (TFree (s, _)) = TFree (s, S);
   171 fun resort_tfree S (TFree (s, _)) = TFree (s, S);
   167 
   172 
   168 fun typ_subst_nonatomic inst (T as Type (s, Ts)) =
   173 fun typ_subst_nonatomic inst (T as Type (s, Ts)) =
   330 
   335 
   331     val unfold_args = mk_args rssss gssss;
   336     val unfold_args = mk_args rssss gssss;
   332     val corec_args = mk_args sssss hssss;
   337     val corec_args = mk_args sssss hssss;
   333   in
   338   in
   334     ((cs, cpss, (unfold_args, unfold_types), (corec_args, corec_types)), lthy)
   339     ((cs, cpss, (unfold_args, unfold_types), (corec_args, corec_types)), lthy)
       
   340   end;
       
   341 
       
   342 fun mk_un_fold_co_rec_prelims lfp fpTs As Cs ns mss fp_folds0 fp_recs0 lthy =
       
   343   let
       
   344     val (fp_folds, fp_fold_fun_Ts) = mk_fp_iter lfp As Cs fp_folds0;
       
   345     val (fp_recs, fp_rec_fun_Ts) = mk_fp_iter lfp As Cs fp_recs0;
       
   346 
       
   347     val ((fold_rec_args_types, unfold_corec_args_types), lthy') =
       
   348       if lfp then
       
   349         mk_fold_rec_args_types fpTs Cs ns mss fp_fold_fun_Ts fp_rec_fun_Ts lthy
       
   350         |>> (rpair NONE o SOME)
       
   351       else
       
   352         mk_unfold_corec_args_types fpTs Cs ns mss fp_fold_fun_Ts fp_rec_fun_Ts lthy
       
   353         |>> (pair NONE o SOME)
       
   354   in
       
   355     ((fp_folds, fp_recs, fold_rec_args_types, unfold_corec_args_types), lthy')
   335   end;
   356   end;
   336 
   357 
   337 fun mk_map live Ts Us t =
   358 fun mk_map live Ts Us t =
   338   let val (Type (_, Ts0), Type (_, Us0)) = strip_typeN (live + 1) (fastype_of t) |>> List.last in
   359   let val (Type (_, Ts0), Type (_, Us0)) = strip_typeN (live + 1) (fastype_of t) |>> List.last in
   339     Term.subst_atomic_types (Ts0 @ Us0 ~~ Ts @ Us) t
   360     Term.subst_atomic_types (Ts0 @ Us0 ~~ Ts @ Us) t
  1128     val ctr_Tsss = map (map (map (Term.typ_subst_atomic (Xs ~~ fpTs)))) ctr_TsssXs;
  1149     val ctr_Tsss = map (map (map (Term.typ_subst_atomic (Xs ~~ fpTs)))) ctr_TsssXs;
  1129     val ns = map length ctr_Tsss;
  1150     val ns = map length ctr_Tsss;
  1130     val kss = map (fn n => 1 upto n) ns;
  1151     val kss = map (fn n => 1 upto n) ns;
  1131     val mss = map (map length) ctr_Tsss;
  1152     val mss = map (map length) ctr_Tsss;
  1132 
  1153 
  1133     val (fp_folds, fp_fold_fun_Ts) = mk_fp_iter lfp As Cs fp_folds0;
  1154     val ((fp_folds, fp_recs, fold_rec_args_types, unfold_corec_args_types), lthy) =
  1134     val (fp_recs, fp_rec_fun_Ts) = mk_fp_iter lfp As Cs fp_recs0;
  1155       mk_un_fold_co_rec_prelims lfp fpTs As Cs ns mss fp_folds0 fp_recs0 lthy;
  1135 
       
  1136     val ((fold_rec_arg_types, unfold_corec_args_types), _) =
       
  1137       if lfp then
       
  1138         mk_fold_rec_args_types fpTs Cs ns mss fp_fold_fun_Ts fp_rec_fun_Ts lthy
       
  1139         |>> rpair ([], [], (([], [], []), ([], [], [])), (([], [], []), ([], [], [])))
       
  1140       else
       
  1141         mk_unfold_corec_args_types fpTs Cs ns mss fp_fold_fun_Ts fp_rec_fun_Ts lthy
       
  1142         |>> pair (([], [], []), ([], [], []));
       
  1143 
  1156 
  1144     fun define_ctrs_case_for_type (((((((((((((((((((((((((fp_bnf, fp_b), fpT), C), ctor), dtor),
  1157     fun define_ctrs_case_for_type (((((((((((((((((((((((((fp_bnf, fp_b), fpT), C), ctor), dtor),
  1145             fp_fold), fp_rec), ctor_dtor), dtor_ctor), ctor_inject), pre_map_def), pre_set_defs),
  1158             fp_fold), fp_rec), ctor_dtor), dtor_ctor), ctor_inject), pre_map_def), pre_set_defs),
  1146           pre_rel_def), fp_map_thm), fp_set_thms), fp_rel_thm), n), ks), ms), ctr_bindings),
  1159           pre_rel_def), fp_map_thm), fp_set_thms), fp_rel_thm), n), ks), ms), ctr_bindings),
  1147         ctr_mixfixes), ctr_Tss), disc_bindings), sel_bindingss), raw_sel_defaultss) no_defs_lthy =
  1160         ctr_mixfixes), ctr_Tss), disc_bindings), sel_bindingss), raw_sel_defaultss) no_defs_lthy =
  1329         fun massage_res (((maps_sets_rels, ctr_sugar), co_iter_res), lthy) =
  1342         fun massage_res (((maps_sets_rels, ctr_sugar), co_iter_res), lthy) =
  1330           (((maps_sets_rels, (ctrs, xss, ctr_defs, ctr_sugar)), co_iter_res), lthy);
  1343           (((maps_sets_rels, (ctrs, xss, ctr_defs, ctr_sugar)), co_iter_res), lthy);
  1331       in
  1344       in
  1332         (wrap_ctrs
  1345         (wrap_ctrs
  1333          #> derive_maps_sets_rels
  1346          #> derive_maps_sets_rels
  1334          ##>> (if lfp then define_fold_rec fold_rec_arg_types
  1347          ##>> (if lfp then define_fold_rec (the fold_rec_args_types)
  1335            else define_unfold_corec unfold_corec_args_types)
  1348            else define_unfold_corec (the unfold_corec_args_types))
  1336              mk_binding fpTs As Cs fp_fold fp_rec
  1349              mk_binding fpTs As Cs fp_fold fp_rec
  1337          #> massage_res, lthy')
  1350          #> massage_res, lthy')
  1338       end;
  1351       end;
  1339 
  1352 
  1340     fun wrap_types_etc (wrap_types_etcs, lthy) =
  1353     fun wrap_types_etc (wrap_types_etcs, lthy) =