src/HOL/Tools/BNF/bnf_gfp_grec_sugar.ML
changeset 62746 4384baae8753
parent 62743 f9a65b48f5e2
child 62958 b41c1cb5e251
equal deleted inserted replaced
62745:257a022f7e7b 62746:4384baae8753
    18   val explore_corec_equation: Proof.context -> bool -> bool -> string -> term ->
    18   val explore_corec_equation: Proof.context -> bool -> bool -> string -> term ->
    19     BNF_GFP_Grec_Sugar_Util.s_parse_info -> typ -> term list * term -> term list * term
    19     BNF_GFP_Grec_Sugar_Util.s_parse_info -> typ -> term list * term -> term list * term
    20   val build_corecUU_arg_and_goals: bool -> term -> term list * term -> local_theory ->
    20   val build_corecUU_arg_and_goals: bool -> term -> term list * term -> local_theory ->
    21     (((thm list * thm list * thm list) * term list) * term) * local_theory
    21     (((thm list * thm list * thm list) * term list) * term) * local_theory
    22   val derive_eq_corecUU: Proof.context -> BNF_GFP_Grec.corec_info -> term -> term -> thm -> thm
    22   val derive_eq_corecUU: Proof.context -> BNF_GFP_Grec.corec_info -> term -> term -> thm -> thm
    23   val derive_unique: Proof.context -> morphism -> term -> BNF_GFP_Grec.corec_info -> typ -> thm ->
    23   val derive_unique: Proof.context -> morphism -> term -> BNF_GFP_Grec.corec_info -> string ->
    24     thm
    24     thm -> thm
    25 
    25 
    26   val corec_cmd: corec_option list -> (binding * string option * mixfix) list * string ->
    26   val corec_cmd: corec_option list -> (binding * string option * mixfix) list * string ->
    27     local_theory -> local_theory
    27     local_theory -> local_theory
    28   val corecursive_cmd: corec_option list -> (binding * string option * mixfix) list * string ->
    28   val corecursive_cmd: corec_option list -> (binding * string option * mixfix) list * string ->
    29     local_theory -> Proof.state
    29     local_theory -> Proof.state
   260 
   260 
   261 fun derive_case_trivial ctxt fpT_name =
   261 fun derive_case_trivial ctxt fpT_name =
   262   let
   262   let
   263     val SOME {casex, exhaust, case_thms, ...} = ctr_sugar_of ctxt fpT_name;
   263     val SOME {casex, exhaust, case_thms, ...} = ctr_sugar_of ctxt fpT_name;
   264 
   264 
   265     val fpT0 as Type (_, As0) = domain_type (body_fun_type (fastype_of casex));
   265     val Type (_, As0) = domain_type (body_fun_type (fastype_of casex));
   266 
   266 
   267     val (As, _) = ctxt
   267     val (As, _) = ctxt
   268       |> mk_TFrees' (map Type.sort_of_atyp As0);
   268       |> mk_TFrees' (map Type.sort_of_atyp As0);
   269     val fpT = Type (fpT_name, As);
   269     val fpT = Type (fpT_name, As);
   270 
   270 
   319 
   319 
   320 fun all_algrho_eqs_of ctxt =
   320 fun all_algrho_eqs_of ctxt =
   321   maps (#algrho_eqs o snd) (all_friend_extras_of ctxt);
   321   maps (#algrho_eqs o snd) (all_friend_extras_of ctxt);
   322 
   322 
   323 fun derive_code ctxt inner_fp_simps goal
   323 fun derive_code ctxt inner_fp_simps goal
   324     {sig_fp_sugars, ssig_fp_sugar, eval, eval_simps, all_algLam_algs, corecUU_thm, ...} res_T fun_t
   324     {sig_fp_sugars, ssig_fp_sugar, eval, eval_simps, all_algLam_algs, corecUU_thm, ...} fun_t
   325     fun_def =
   325     fun_def =
   326   let
   326   let
   327     val fun_T = fastype_of fun_t;
   327     val fun_T = fastype_of fun_t;
   328     val (arg_Ts, Type (fpT_name, _)) = strip_type fun_T;
   328     val (arg_Ts, Type (fpT_name, _)) = strip_type fun_T;
   329     val num_args = length arg_Ts;
   329     val num_args = length arg_Ts;
   364         inner_fp_simps fun_def))
   364         inner_fp_simps fun_def))
   365     |> Thm.close_derivation
   365     |> Thm.close_derivation
   366   end;
   366   end;
   367 
   367 
   368 fun derive_unique ctxt phi code_goal
   368 fun derive_unique ctxt phi code_goal
   369     {sig_fp_sugars, ssig_fp_sugar, eval, eval_simps, all_algLam_algs, corecUU_unique, ...}
   369     {sig_fp_sugars, ssig_fp_sugar, eval, eval_simps, all_algLam_algs, corecUU_unique, ...} fpT_name
   370     (res_T as Type (fpT_name, _)) eq_corecUU =
   370     eq_corecUU =
   371   let
   371   let
   372     val SOME {pre_bnf, fp_bnf, absT_info, fp_nesting_bnfs, live_nesting_bnfs, fp_ctr_sugar, ...} =
   372     val SOME {pre_bnf, fp_bnf, absT_info, fp_nesting_bnfs, live_nesting_bnfs, fp_ctr_sugar, ...} =
   373       fp_sugar_of ctxt fpT_name;
   373       fp_sugar_of ctxt fpT_name;
   374     val SOME {case_trivial, ...} = codatatype_extra_of ctxt fpT_name;
   374     val SOME {case_trivial, ...} = codatatype_extra_of ctxt fpT_name;
   375 
   375 
   588     val ((xs, ys), _) = ctxt
   588     val ((xs, ys), _) = ctxt
   589       |> mk_Frees "x" xy_Ts
   589       |> mk_Frees "x" xy_Ts
   590       ||>> mk_Frees "y" xy_Ts;
   590       ||>> mk_Frees "y" xy_Ts;
   591 
   591 
   592     fun mk_prem xy_T x y =
   592     fun mk_prem xy_T x y =
   593       BNF_Def.build_rel [] ctxt [fpT]
   593       build_rel [] ctxt [fpT] (fn (T, _) => if T = fpT then Rcong else HOLogic.eq_const T)
   594         (fn (T, _) => if T = fpT then Rcong else HOLogic.eq_const T) (xy_T, xy_T) $ x $ y;
   594         (xy_T, xy_T) $ x $ y;
   595 
   595 
   596     val prems = @{map 3} mk_prem xy_Ts xs ys;
   596     val prems = @{map 3} mk_prem xy_Ts xs ys;
   597     val concl = Rcong $ list_comb (alg, xs) $ list_comb (alg, ys);
   597     val concl = Rcong $ list_comb (alg, xs) $ list_comb (alg, ys);
   598   in
   598   in
   599     Logic.list_implies (map HOLogic.mk_Trueprop prems, HOLogic.mk_Trueprop concl)
   599     Logic.list_implies (map HOLogic.mk_Trueprop prems, HOLogic.mk_Trueprop concl)
  1053           build_map lthy [] (mk_undefined o op -->) (apply2 tupleT_of_funT (param1_T, param2_T))
  1053           build_map lthy [] (mk_undefined o op -->) (apply2 tupleT_of_funT (param1_T, param2_T))
  1054           |> singleton (Type_Infer_Context.infer_types lthy)
  1054           |> singleton (Type_Infer_Context.infer_types lthy)
  1055           |> singleton (Type_Infer.fixate lthy)
  1055           |> singleton (Type_Infer.fixate lthy)
  1056           |> type_of
  1056           |> type_of
  1057           |> dest_funT
  1057           |> dest_funT
  1058           |-> BNF_GFP_Grec_Sugar_Util.generalize_types 1
  1058           |-> generalize_types 1
  1059           |> funT_of_tupleT m;
  1059           |> funT_of_tupleT m;
  1060 
  1060 
  1061         val j = maxidx_of_typ deadfixed_T + 1;
  1061         val j = maxidx_of_typ deadfixed_T + 1;
  1062 
  1062 
  1063         fun varifyT (Type (s, Ts)) = Type (s, map varifyT Ts)
  1063         fun varifyT (Type (s, Ts)) = Type (s, map varifyT Ts)
  2108             |> Local_Theory.notes notes |> snd
  2108             |> Local_Theory.notes notes |> snd
  2109           end;
  2109           end;
  2110 
  2110 
  2111         val lthy = lthy |> friend ? derive_and_note_friend_extra_theorems;
  2111         val lthy = lthy |> friend ? derive_and_note_friend_extra_theorems;
  2112 
  2112 
  2113         val code_thm = derive_code lthy inner_fp_simps code_goal corec_info res_T fun_t fun_def;
  2113         val code_thm = derive_code lthy inner_fp_simps code_goal corec_info fun_t fun_def;
  2114 (* TODO:
  2114 (* TODO:
  2115         val ctr_thmss = map mk_thm (#2 views);
  2115         val ctr_thmss = map mk_thm (#2 views);
  2116         val disc_thmss = map mk_thm (#3 views);
  2116         val disc_thmss = map mk_thm (#3 views);
  2117         val disc_iff_thmss = map mk_thm (#4 views);
  2117         val disc_iff_thmss = map mk_thm (#4 views);
  2118         val sel_thmss = map mk_thm (#5 views);
  2118         val sel_thmss = map mk_thm (#5 views);
  2119 *)
  2119 *)
  2120 
  2120 
  2121         val uniques =
  2121         val uniques =
  2122           if null inner_fp_simps then [derive_unique lthy phi (#1 views0) corec_info res_T fun_def]
  2122           if null inner_fp_simps then
  2123           else [];
  2123             [derive_unique lthy phi (#1 views0) corec_info fpT_name fun_def]
       
  2124           else
       
  2125             [];
  2124 
  2126 
  2125 (* TODO:
  2127 (* TODO:
  2126         val disc_iff_or_disc_thmss =
  2128         val disc_iff_or_disc_thmss =
  2127           map2 (fn [] => I | disc_iffs => K disc_iffs) disc_iff_thmss disc_thmss;
  2129           map2 (fn [] => I | disc_iffs => K disc_iffs) disc_iff_thmss disc_thmss;
  2128         val simp_thmss = map2 append disc_iff_or_disc_thmss sel_thmss;
  2130         val simp_thmss = map2 append disc_iff_or_disc_thmss sel_thmss;
  2297           |> register_friend_extra fun_name eq_algrho algrho_eq
  2299           |> register_friend_extra fun_name eq_algrho algrho_eq
  2298           |> register_coinduct_dynamic_friend fpT_name fun_name
  2300           |> register_coinduct_dynamic_friend fpT_name fun_name
  2299           |> derive_and_update_coinduct_cong_intross [corec_info];
  2301           |> derive_and_update_coinduct_cong_intross [corec_info];
  2300         val cong_intros_pairs = AList.group (op =) cong_intro_pairs;
  2302         val cong_intros_pairs = AList.group (op =) cong_intro_pairs;
  2301 
  2303 
  2302         val unique = derive_unique lthy Morphism.identity code_goal corec_info res_T eq_corecUU;
  2304         val unique = derive_unique lthy Morphism.identity code_goal corec_info fpT_name eq_corecUU;
  2303 
  2305 
  2304         val notes =
  2306         val notes =
  2305           [(codeN, [code_thm], []),
  2307           [(codeN, [code_thm], []),
  2306            (coinductN, [coinduct], coinduct_attrs),
  2308            (coinductN, [coinduct], coinduct_attrs),
  2307            (cong_introsN, maps snd cong_intros_pairs, []),
  2309            (cong_introsN, maps snd cong_intros_pairs, []),