src/HOL/Tools/BNF/bnf_gfp_grec_sugar.ML
changeset 70494 41108e3e9ca5
parent 69992 bd3c10813cc4
child 71179 592e2afdd50c
equal deleted inserted replaced
70493:a9053fa30909 70494:41108e3e9ca5
   245 
   245 
   246     val dtor_ctor = nth dtor_ctors fp_res_index;
   246     val dtor_ctor = nth dtor_ctors fp_res_index;
   247   in
   247   in
   248     Goal.prove_sorry ctxt vars [] goal (fn {context = ctxt, prems = _} =>
   248     Goal.prove_sorry ctxt vars [] goal (fn {context = ctxt, prems = _} =>
   249       mk_case_dtor_tac ctxt u abs_inverse dtor_ctor ctr_defs exhaust case_thms)
   249       mk_case_dtor_tac ctxt u abs_inverse dtor_ctor ctr_defs exhaust case_thms)
   250     |> Thm.close_derivation
   250     |> Thm.close_derivation \<^here>
   251   end;
   251   end;
   252 
   252 
   253 fun derive_case_trivial ctxt fpT_name =
   253 fun derive_case_trivial ctxt fpT_name =
   254   let
   254   let
   255     val SOME {casex, exhaust, case_thms, ...} = ctr_sugar_of ctxt fpT_name;
   255     val SOME {casex, exhaust, case_thms, ...} = ctr_sugar_of ctxt fpT_name;
   267     val exhaust' = infer_instantiate' ctxt [SOME (Thm.cterm_of ctxt var)] exhaust;
   267     val exhaust' = infer_instantiate' ctxt [SOME (Thm.cterm_of ctxt var)] exhaust;
   268   in
   268   in
   269     Goal.prove_sorry ctxt [var_name] [] goal (fn {context = ctxt, prems = _} =>
   269     Goal.prove_sorry ctxt [var_name] [] goal (fn {context = ctxt, prems = _} =>
   270       HEADGOAL (rtac ctxt exhaust') THEN ALLGOALS (hyp_subst_tac ctxt) THEN
   270       HEADGOAL (rtac ctxt exhaust') THEN ALLGOALS (hyp_subst_tac ctxt) THEN
   271       unfold_thms_tac ctxt case_thms THEN ALLGOALS (rtac ctxt refl))
   271       unfold_thms_tac ctxt case_thms THEN ALLGOALS (rtac ctxt refl))
   272     |> Thm.close_derivation
   272     |> Thm.close_derivation \<^here>
   273   end;
   273   end;
   274 
   274 
   275 fun mk_abs_rep_transfers ctxt fpT_name =
   275 fun mk_abs_rep_transfers ctxt fpT_name =
   276   [mk_abs_transfer ctxt fpT_name, mk_rep_transfer ctxt fpT_name]
   276   [mk_abs_transfer ctxt fpT_name, mk_rep_transfer ctxt fpT_name]
   277   handle Fail _ => [];
   277   handle Fail _ => [];
   349       mk_code_tac ctxt num_args fpsig_nesting_maps ssig_map eval pre_map_def abs_inverse
   349       mk_code_tac ctxt num_args fpsig_nesting_maps ssig_map eval pre_map_def abs_inverse
   350         fpsig_nesting_map_ident0s fpsig_nesting_map_comps fpsig_nesting_map_thms
   350         fpsig_nesting_map_ident0s fpsig_nesting_map_comps fpsig_nesting_map_thms
   351         live_nesting_map_ident0s fp_map_ident case_trivial ctr_defs case_eq_ifs corecUU_thm
   351         live_nesting_map_ident0s fp_map_ident case_trivial ctr_defs case_eq_ifs corecUU_thm
   352         all_sig_map_thms ssig_map_thms all_algLam_alg_pointfuls (all_algrho_eqs_of ctxt) eval_simps
   352         all_sig_map_thms ssig_map_thms all_algLam_alg_pointfuls (all_algrho_eqs_of ctxt) eval_simps
   353         inner_fp_simps fun_def))
   353         inner_fp_simps fun_def))
   354     |> Thm.close_derivation
   354     |> Thm.close_derivation \<^here>
   355   end;
   355   end;
   356 
   356 
   357 fun derive_unique ctxt phi code_goal
   357 fun derive_unique ctxt phi code_goal
   358     {sig_fp_sugars, ssig_fp_sugar, eval, eval_simps, all_algLam_algs, corecUU_unique, ...} fpT_name
   358     {sig_fp_sugars, ssig_fp_sugar, eval, eval_simps, all_algLam_algs, corecUU_unique, ...} fpT_name
   359     eq_corecUU =
   359     eq_corecUU =
   408       mk_unique_tac ctxt num_args fpsig_nesting_maps ssig_map eval pre_map_def abs_inverse
   408       mk_unique_tac ctxt num_args fpsig_nesting_maps ssig_map eval pre_map_def abs_inverse
   409         fpsig_nesting_map_ident0s fpsig_nesting_map_comps fpsig_nesting_map_thms
   409         fpsig_nesting_map_ident0s fpsig_nesting_map_comps fpsig_nesting_map_thms
   410         live_nesting_map_ident0s fp_map_ident case_trivial ctr_defs case_eq_ifs all_sig_map_thms
   410         live_nesting_map_ident0s fp_map_ident case_trivial ctr_defs case_eq_ifs all_sig_map_thms
   411         ssig_map_thms all_algLam_alg_pointfuls (all_algrho_eqs_of ctxt) eval_simps corecUU_unique
   411         ssig_map_thms all_algLam_alg_pointfuls (all_algrho_eqs_of ctxt) eval_simps corecUU_unique
   412         eq_corecUU)
   412         eq_corecUU)
   413     |> Thm.close_derivation
   413     |> Thm.close_derivation \<^here>
   414   end;
   414   end;
   415 
   415 
   416 fun derive_last_disc ctxt fcT_name =
   416 fun derive_last_disc ctxt fcT_name =
   417   let
   417   let
   418     val SOME {T = fcT, discs, exhaust, disc_thmss, ...} = ctr_sugar_of ctxt fcT_name;
   418     val SOME {T = fcT, discs, exhaust, disc_thmss, ...} = ctr_sugar_of ctxt fcT_name;
   426 
   426 
   427     val goal = mk_Trueprop_eq (last_udisc, foldr1 HOLogic.mk_conj not_udiscs);
   427     val goal = mk_Trueprop_eq (last_udisc, foldr1 HOLogic.mk_conj not_udiscs);
   428   in
   428   in
   429     Goal.prove_sorry ctxt [fst (dest_Free u)] [] goal (fn {context = ctxt, prems = _} =>
   429     Goal.prove_sorry ctxt [fst (dest_Free u)] [] goal (fn {context = ctxt, prems = _} =>
   430       mk_last_disc_tac ctxt u exhaust (flat disc_thmss))
   430       mk_last_disc_tac ctxt u exhaust (flat disc_thmss))
   431     |> Thm.close_derivation
   431     |> Thm.close_derivation \<^here>
   432   end;
   432   end;
   433 
   433 
   434 fun derive_eq_algrho ctxt {sig_fp_sugars, ssig_fp_sugar, eval, eval_simps, all_algLam_algs,
   434 fun derive_eq_algrho ctxt {sig_fp_sugars, ssig_fp_sugar, eval, eval_simps, all_algLam_algs,
   435       corecUU_unique, ...}
   435       corecUU_unique, ...}
   436     ({algrho = algrho0, dtor_algrho, ...} : friend_info) fun_t k_T code_goal const_transfers rho_def
   436     ({algrho = algrho0, dtor_algrho, ...} : friend_info) fun_t k_T code_goal const_transfers rho_def
   520           fpsig_nesting_map_ident0s fpsig_nesting_map_comps fpsig_nesting_map_thms
   520           fpsig_nesting_map_ident0s fpsig_nesting_map_comps fpsig_nesting_map_thms
   521           live_nesting_map_ident0s fp_map_ident dtor_ctor ctor_iff_dtor ctr_defs nullary_disc_defs
   521           live_nesting_map_ident0s fp_map_ident dtor_ctor ctor_iff_dtor ctr_defs nullary_disc_defs
   522           disc_sel_eq_cases case_dtor case_eq_ifs const_pointful_naturals
   522           disc_sel_eq_cases case_dtor case_eq_ifs const_pointful_naturals
   523           fp_nesting_k_map_disc_sels' rho_def dtor_algrho corecUU_unique eq_corecUU all_sig_map_thms
   523           fp_nesting_k_map_disc_sels' rho_def dtor_algrho corecUU_unique eq_corecUU all_sig_map_thms
   524           ssig_map_thms all_algLam_alg_pointfuls (all_algrho_eqs_of ctxt) eval_simps)
   524           ssig_map_thms all_algLam_alg_pointfuls (all_algrho_eqs_of ctxt) eval_simps)
   525       |> Thm.close_derivation
   525       |> Thm.close_derivation \<^here>
   526       handle e as ERROR _ =>
   526       handle e as ERROR _ =>
   527         (case filter (is_none o snd) (const_transfers ~~ const_pointful_natural_opts) of
   527         (case filter (is_none o snd) (const_transfers ~~ const_pointful_natural_opts) of
   528           [] => Exn.reraise e
   528           [] => Exn.reraise e
   529         | thm_nones =>
   529         | thm_nones =>
   530           error ("Failed to state naturality property for " ^
   530           error ("Failed to state naturality property for " ^
   547 
   547 
   548     fun derive_unprimed rho_transfer' =
   548     fun derive_unprimed rho_transfer' =
   549       Variable.add_free_names ctxt goal []
   549       Variable.add_free_names ctxt goal []
   550       |> (fn vars => Goal.prove_sorry ctxt vars [] goal (fn {context = ctxt, prems = _} =>
   550       |> (fn vars => Goal.prove_sorry ctxt vars [] goal (fn {context = ctxt, prems = _} =>
   551         unfold_thms_tac ctxt simps THEN HEADGOAL (rtac ctxt rho_transfer')))
   551         unfold_thms_tac ctxt simps THEN HEADGOAL (rtac ctxt rho_transfer')))
   552       |> Thm.close_derivation;
   552       |> Thm.close_derivation \<^here>;
   553 
   553 
   554     val goal' = Raw_Simplifier.rewrite_term thy simps [] goal;
   554     val goal' = Raw_Simplifier.rewrite_term thy simps [] goal;
   555   in
   555   in
   556     if null abs_rep_transfers then (goal', derive_unprimed #> fold_rho)
   556     if null abs_rep_transfers then (goal', derive_unprimed #> fold_rho)
   557     else (goal, fold_rho)
   557     else (goal, fold_rho)
   565     Variable.add_free_names ctxt goal []
   565     Variable.add_free_names ctxt goal []
   566     |> (fn vars => Goal.prove (*no sorry*) ctxt vars [] goal (fn {context = ctxt, prems = _} =>
   566     |> (fn vars => Goal.prove (*no sorry*) ctxt vars [] goal (fn {context = ctxt, prems = _} =>
   567       mk_rho_transfer_tac ctxt (null abs_rep_transfers) (rel_def_of_bnf pre_bnf)
   567       mk_rho_transfer_tac ctxt (null abs_rep_transfers) (rel_def_of_bnf pre_bnf)
   568       const_transfers))
   568       const_transfers))
   569     |> unfold_thms ctxt [rho_def RS @{thm symmetric}]
   569     |> unfold_thms ctxt [rho_def RS @{thm symmetric}]
   570     |> Thm.close_derivation
   570     |> Thm.close_derivation \<^here>
   571   end;
   571   end;
   572 
   572 
   573 fun mk_cong_intro_ctr_or_friend_goal ctxt fpT Rcong alg =
   573 fun mk_cong_intro_ctr_or_friend_goal ctxt fpT Rcong alg =
   574   let
   574   let
   575     val xy_Ts = binder_types (fastype_of alg);
   575     val xy_Ts = binder_types (fastype_of alg);
   604 
   604 
   605     fun prove ctr_def goal =
   605     fun prove ctr_def goal =
   606       Variable.add_free_names ctxt goal []
   606       Variable.add_free_names ctxt goal []
   607       |> (fn vars => Goal.prove_sorry ctxt vars [] goal (fn {context = ctxt, prems = _} =>
   607       |> (fn vars => Goal.prove_sorry ctxt vars [] goal (fn {context = ctxt, prems = _} =>
   608         mk_cong_intro_ctr_or_friend_tac ctxt ctr_def [pre_rel_def, abs_inverse] cong_ctor_intro))
   608         mk_cong_intro_ctr_or_friend_tac ctxt ctr_def [pre_rel_def, abs_inverse] cong_ctor_intro))
   609       |> Thm.close_derivation;
   609       |> Thm.close_derivation \<^here>;
   610 
   610 
   611     val goals = map (mk_cong_intro_ctr_or_friend_goal ctxt fpT Rcong) ctrs;
   611     val goals = map (mk_cong_intro_ctr_or_friend_goal ctxt fpT Rcong) ctrs;
   612   in
   612   in
   613     map2 prove ctr_defs goals
   613     map2 prove ctr_defs goals
   614   end;
   614   end;
   633     val goal = mk_cong_intro_ctr_or_friend_goal ctxt fpT Rcong friend;
   633     val goal = mk_cong_intro_ctr_or_friend_goal ctxt fpT Rcong friend;
   634   in
   634   in
   635     Variable.add_free_names ctxt goal []
   635     Variable.add_free_names ctxt goal []
   636     |> (fn vars => Goal.prove_sorry ctxt vars [] goal (fn {context = ctxt, prems = _} =>
   636     |> (fn vars => Goal.prove_sorry ctxt vars [] goal (fn {context = ctxt, prems = _} =>
   637       mk_cong_intro_ctr_or_friend_tac ctxt eq_algrho [] cong_algrho_intro))
   637       mk_cong_intro_ctr_or_friend_tac ctxt eq_algrho [] cong_algrho_intro))
   638     |> Thm.close_derivation
   638     |> Thm.close_derivation \<^here>
   639   end;
   639   end;
   640 
   640 
   641 fun derive_cong_intros lthy ctr_names friend_names
   641 fun derive_cong_intros lthy ctr_names friend_names
   642     ({cong_base, cong_refl, cong_sym, cong_trans, cong_alg_intros, ...} : dtor_coinduct_info) =
   642     ({cong_base, cong_refl, cong_sym, cong_trans, cong_alg_intros, ...} : dtor_coinduct_info) =
   643   let
   643   let
  1910       mk_eq_corecUU_tac ctxt num_args fpsig_nesting_maps ssig_map eval pre_map_def abs_inverse
  1910       mk_eq_corecUU_tac ctxt num_args fpsig_nesting_maps ssig_map eval pre_map_def abs_inverse
  1911         fpsig_nesting_map_ident0s fpsig_nesting_map_comps fpsig_nesting_map_thms
  1911         fpsig_nesting_map_ident0s fpsig_nesting_map_comps fpsig_nesting_map_thms
  1912         live_nesting_map_ident0s fp_map_ident case_trivial ctr_defs case_eq_ifs all_sig_map_thms
  1912         live_nesting_map_ident0s fp_map_ident case_trivial ctr_defs case_eq_ifs all_sig_map_thms
  1913         ssig_map_thms all_algLam_alg_pointfuls (all_algrho_eqs_of ctxt) eval_simps corecUU_unique
  1913         ssig_map_thms all_algLam_alg_pointfuls (all_algrho_eqs_of ctxt) eval_simps corecUU_unique
  1914         fun_code)
  1914         fun_code)
  1915     |> Thm.close_derivation
  1915     |> Thm.close_derivation \<^here>
  1916   end;
  1916   end;
  1917 
  1917 
  1918 fun derive_coinduct_cong_intros
  1918 fun derive_coinduct_cong_intros
  1919     ({fpT = fpT0 as Type (fpT_name, _), friend_names = friend_names0,
  1919     ({fpT = fpT0 as Type (fpT_name, _), friend_names = friend_names0,
  1920       corecUU = Const (corecUU_name, _), dtor_coinduct_info as {dtor_coinduct, ...}, ...})
  1920       corecUU = Const (corecUU_name, _), dtor_coinduct_info as {dtor_coinduct, ...}, ...})
  2165 
  2165 
  2166     fun prove_transfer_goal ctxt goal =
  2166     fun prove_transfer_goal ctxt goal =
  2167       Variable.add_free_names ctxt goal []
  2167       Variable.add_free_names ctxt goal []
  2168       |> (fn vars => Goal.prove (*no sorry*) ctxt vars [] goal (fn {context = ctxt, prems = _} =>
  2168       |> (fn vars => Goal.prove (*no sorry*) ctxt vars [] goal (fn {context = ctxt, prems = _} =>
  2169         HEADGOAL (Transfer.transfer_prover_tac ctxt)))
  2169         HEADGOAL (Transfer.transfer_prover_tac ctxt)))
  2170       |> Thm.close_derivation;
  2170       |> Thm.close_derivation \<^here>;
  2171 
  2171 
  2172     fun maybe_prove_transfer_goal ctxt goal =
  2172     fun maybe_prove_transfer_goal ctxt goal =
  2173       (case try (prove_transfer_goal ctxt) goal of
  2173       (case try (prove_transfer_goal ctxt) goal of
  2174         SOME thm => apfst (cons thm)
  2174         SOME thm => apfst (cons thm)
  2175       | NONE => apsnd (cons goal));
  2175       | NONE => apsnd (cons goal));