src/HOL/BNF/Tools/bnf_fp_def_sugar.ML
changeset 52326 a9f75d64b3f4
parent 52321 30d516bbf19f
child 52327 9f14280aa080
equal deleted inserted replaced
52325:a74e0a4741df 52326:a9f75d64b3f4
    36     (term list list
    36     (term list list
    37      * (typ list list * typ list list list list * term list list
    37      * (typ list list * typ list list list list * term list list
    38         * term list list list list) list option
    38         * term list list list list) list option
    39      * (term list * term list list
    39      * (term list * term list list
    40         * ((term list list * term list list list list * term list list list list)
    40         * ((term list list * term list list list list * term list list list list)
    41            * (typ list * typ list list list * typ list list))
    41            * (typ list * typ list list list * typ list list)) list) option)
    42         * ((term list list * term list list list list * term list list list list)
       
    43            * (typ list * typ list list list * typ list list))) option)
       
    44     * Proof.context
    42     * Proof.context
    45   val mk_map: int -> typ list -> typ list -> term -> term
    43   val mk_map: int -> typ list -> typ list -> term -> term
    46   val build_map: local_theory -> (typ * typ -> term) -> typ * typ -> term
    44   val build_map: local_theory -> (typ * typ -> term) -> typ * typ -> term
    47 
    45 
    48   val mk_iter_fun_arg_typessss: typ list -> int list -> int list list -> term ->
    46   val mk_iter_fun_arg_typessss: typ list -> int list -> int list list -> term ->
    49     typ list list list list
    47     typ list list list list
    50   val define_iters: string list ->
    48   val define_iters: string list ->
    51     (typ list list * typ list list list list * term list list * term list list list list) list ->
    49     (typ list list * typ list list list list * term list list * term list list list list) list ->
    52     (string -> binding) -> typ list -> typ list -> term list -> Proof.context ->
    50     (string -> binding) -> typ list -> typ list -> term list -> Proof.context ->
    53     (term list * thm list) * Proof.context
    51     (term list * thm list) * Proof.context
    54   val define_coiters: term list * term list list
    52   val define_coiters: string list -> term list * term list list
    55     * ((term list list * term list list list list * term list list list list)
    53     * ((term list list * term list list list list * term list list list list)
    56        * (typ list * typ list list list * typ list list))
    54        * (typ list * typ list list list * typ list list)) list ->
    57     * ((term list list * term list list list list * term list list list list)
       
    58        * (typ list * typ list list list * typ list list)) ->
       
    59     (string -> binding) -> typ list -> typ list -> term list -> Proof.context ->
    55     (string -> binding) -> typ list -> typ list -> term list -> Proof.context ->
    60     (term list * thm list) * Proof.context
    56     (term list * thm list) * Proof.context
    61   val derive_induct_fold_rec_thms_for_types: BNF_Def.bnf list -> term list list ->
    57   val derive_induct_fold_rec_thms_for_types: BNF_Def.bnf list -> term list list ->
    62     (typ list list * typ list list list list * term list list * term list list list list) list ->
    58     (typ list list * typ list list list list * term list list * term list list list list) list ->
    63     thm -> thm list list -> BNF_Def.bnf list -> BNF_Def.bnf list -> typ list -> typ list ->
    59     thm -> thm list list -> BNF_Def.bnf list -> BNF_Def.bnf list -> typ list -> typ list ->
   354       in (pfss, cqssss, cfssss) end;
   350       in (pfss, cqssss, cfssss) end;
   355 
   351 
   356     val unfold_args = mk_args rssss gssss;
   352     val unfold_args = mk_args rssss gssss;
   357     val corec_args = mk_args sssss hssss;
   353     val corec_args = mk_args sssss hssss;
   358   in
   354   in
   359     ((cs, cpss, (unfold_args, unfold_types), (corec_args, corec_types)), lthy)
   355     ((cs, cpss, [(unfold_args, unfold_types), (corec_args, corec_types)]), lthy)
   360   end;
   356   end;
   361 
   357 
   362 fun mk_un_fold_co_rec_prelims fp fpTs Cs ns mss xtor_co_iterss0' lthy =
   358 fun mk_un_fold_co_rec_prelims fp fpTs Cs ns mss xtor_co_iterss0' lthy =
   363   let
   359   let
   364     val thy = Proof_Context.theory_of lthy;
   360     val thy = Proof_Context.theory_of lthy;
   470     val cqfsss = map3 (map3 (map3 build_dtor_coiter_arg)) f_Tsss cqssss cfssss;
   466     val cqfsss = map3 (map3 (map3 build_dtor_coiter_arg)) f_Tsss cqssss cfssss;
   471   in
   467   in
   472     Term.list_comb (dtor_coiter, map4 mk_preds_getterss_join cs cpss f_sum_prod_Ts cqfsss)
   468     Term.list_comb (dtor_coiter, map4 mk_preds_getterss_join cs cpss f_sum_prod_Ts cqfsss)
   473   end;
   469   end;
   474 
   470 
   475 fun define_iters iterNs iter_args_typess mk_binding fpTs Cs ctor_iters lthy0 =
   471 fun define_iters iterNs iter_args_typess' mk_binding fpTs Cs ctor_iters lthy0 =
   476   let
   472   let
   477     val thy = Proof_Context.theory_of lthy0;
   473     val thy = Proof_Context.theory_of lthy0;
   478 
   474 
   479     val nn = length fpTs;
   475     val nn = length fpTs;
   480 
   476 
   487         val spec =
   483         val spec =
   488           mk_Trueprop_eq (lists_bmoc fss (Free (Binding.name_of b, res_T)),
   484           mk_Trueprop_eq (lists_bmoc fss (Free (Binding.name_of b, res_T)),
   489             mk_iter_body ctor_iter fss xssss);
   485             mk_iter_body ctor_iter fss xssss);
   490       in (b, spec) end;
   486       in (b, spec) end;
   491 
   487 
   492     val binding_specs = map3 generate_iter iterNs iter_args_typess ctor_iters;
   488     val binding_specs = map3 generate_iter iterNs iter_args_typess' ctor_iters;
   493 
   489 
   494     val ((csts, defs), (lthy', lthy)) = lthy0
   490     val ((csts, defs), (lthy', lthy)) = lthy0
   495       |> apfst split_list o fold_map (fn (b, spec) =>
   491       |> apfst split_list o fold_map (fn (b, spec) =>
   496         Specification.definition (SOME (b, NONE, NoSyn), ((Thm.def_binding b, []), spec))
   492         Specification.definition (SOME (b, NONE, NoSyn), ((Thm.def_binding b, []), spec))
   497         #>> apsnd snd) binding_specs
   493         #>> apsnd snd) binding_specs
   505   in
   501   in
   506     ((iters, iter_defs), lthy')
   502     ((iters, iter_defs), lthy')
   507   end;
   503   end;
   508 
   504 
   509 (* TODO: merge with above function? *)
   505 (* TODO: merge with above function? *)
   510 fun define_coiters (cs, cpss, unfold_args_types, corec_args_types) mk_binding fpTs Cs
   506 fun define_coiters coiterNs (cs, cpss, coiter_args_typess') mk_binding fpTs Cs dtor_coiters lthy0 =
   511     [dtor_unfold, dtor_corec] lthy0 =
       
   512   let
   507   let
   513     val thy = Proof_Context.theory_of lthy0;
   508     val thy = Proof_Context.theory_of lthy0;
   514 
   509 
   515     val nn = length fpTs;
   510     val nn = length fpTs;
   516 
   511 
   517     val C_to_fpT as Type (_, [_, fpT]) = snd (strip_typeN nn (fastype_of dtor_unfold));
   512     val C_to_fpT as Type (_, [_, fpT]) = snd (strip_typeN nn (fastype_of (hd dtor_coiters)));
   518 
   513 
   519     fun generate_coiter (suf, dtor_coiter, ((pfss, cqssss, cfssss),
   514     fun generate_coiter suf ((pfss, cqssss, cfssss), (f_sum_prod_Ts, f_Tsss, pf_Tss)) dtor_coiter =
   520         (f_sum_prod_Ts, f_Tsss, pf_Tss))) =
       
   521       let
   515       let
   522         val res_T = fold_rev (curry (op --->)) pf_Tss C_to_fpT;
   516         val res_T = fold_rev (curry (op --->)) pf_Tss C_to_fpT;
   523         val b = mk_binding suf;
   517         val b = mk_binding suf;
   524         val spec =
   518         val spec =
   525           mk_Trueprop_eq (lists_bmoc pfss (Free (Binding.name_of b, res_T)),
   519           mk_Trueprop_eq (lists_bmoc pfss (Free (Binding.name_of b, res_T)),
   526             mk_coiter_body lthy0 cs cpss f_sum_prod_Ts f_Tsss cqssss cfssss dtor_coiter);
   520             mk_coiter_body lthy0 cs cpss f_sum_prod_Ts f_Tsss cqssss cfssss dtor_coiter);
   527       in (b, spec) end;
   521       in (b, spec) end;
   528 
   522 
   529     val binding_specs =
   523     val binding_specs = map3 generate_coiter coiterNs coiter_args_typess' dtor_coiters;
   530       map generate_coiter [(unfoldN, dtor_unfold, unfold_args_types),
       
   531         (corecN, dtor_corec, corec_args_types)];
       
   532 
   524 
   533     val ((csts, defs), (lthy', lthy)) = lthy0
   525     val ((csts, defs), (lthy', lthy)) = lthy0
   534       |> apfst split_list o fold_map (fn (b, spec) =>
   526       |> apfst split_list o fold_map (fn (b, spec) =>
   535         Specification.definition (SOME (b, NONE, NoSyn), ((Thm.def_binding b, []), spec))
   527         Specification.definition (SOME (b, NONE, NoSyn), ((Thm.def_binding b, []), spec))
   536         #>> apsnd snd) binding_specs
   528         #>> apsnd snd) binding_specs
   724     val exhausts = map #exhaust ctr_sugars;
   716     val exhausts = map #exhaust ctr_sugars;
   725     val disc_thmsss = map #disc_thmss ctr_sugars;
   717     val disc_thmsss = map #disc_thmss ctr_sugars;
   726     val discIss = map #discIs ctr_sugars;
   718     val discIss = map #discIs ctr_sugars;
   727     val sel_thmsss = map #sel_thmss ctr_sugars;
   719     val sel_thmsss = map #sel_thmss ctr_sugars;
   728 
   720 
   729     val ((cs, cpss, ((pgss, crssss, cgssss), _), ((phss, csssss, chssss), _)), names_lthy0) =
   721     val ((cs, cpss, [((pgss, crssss, cgssss), _), ((phss, csssss, chssss), _)]), names_lthy0) =
   730       mk_unfold_corec_args_types Cs ns mss [dtor_unfold_fun_Ts, dtor_corec_fun_Ts] lthy;
   722       mk_unfold_corec_args_types Cs ns mss [dtor_unfold_fun_Ts, dtor_corec_fun_Ts] lthy;
   731 
   723 
   732     val (((rs, us'), vs'), names_lthy) =
   724     val (((rs, us'), vs'), names_lthy) =
   733       names_lthy0
   725       names_lthy0
   734       |> mk_Frees "R" (map (fn T => mk_pred2T T T) fpTs)
   726       |> mk_Frees "R" (map (fn T => mk_pred2T T T) fpTs)
  1314       in
  1306       in
  1315         (wrap_ctrs
  1307         (wrap_ctrs
  1316          #> derive_maps_sets_rels
  1308          #> derive_maps_sets_rels
  1317          ##>>
  1309          ##>>
  1318            (if fp = Least_FP then define_iters [foldN, recN] (the fold_rec_args_types)
  1310            (if fp = Least_FP then define_iters [foldN, recN] (the fold_rec_args_types)
  1319             else define_coiters (the unfold_corec_args_types))
  1311             else define_coiters [unfoldN, corecN] (the unfold_corec_args_types))
  1320              mk_binding fpTs Cs [xtor_un_fold, xtor_co_rec]
  1312              mk_binding fpTs Cs [xtor_un_fold, xtor_co_rec]
  1321          #> massage_res, lthy')
  1313          #> massage_res, lthy')
  1322       end;
  1314       end;
  1323 
  1315 
  1324     fun wrap_types_etc (wrap_types_etcs, lthy) =
  1316     fun wrap_types_etc (wrap_types_etcs, lthy) =