src/HOL/Tools/BNF/bnf_fp_def_sugar.ML
changeset 63814 f84d100e4c6d
parent 63813 076129f60a31
child 63815 64f4267e6677
equal deleted inserted replaced
63813:076129f60a31 63814:f84d100e4c6d
   592     @{map 3} (fn b_name => fn Type (T_name, _) => fn thms =>
   592     @{map 3} (fn b_name => fn Type (T_name, _) => fn thms =>
   593         ((Binding.qualify true b_name (Binding.name thmN), attrs T_name), [(thms, [])]))
   593         ((Binding.qualify true b_name (Binding.name thmN), attrs T_name), [(thms, [])]))
   594       b_names Ts thmss)
   594       b_names Ts thmss)
   595   #> filter_out (null o fst o hd o snd);
   595   #> filter_out (null o fst o hd o snd);
   596 
   596 
   597 fun derive_map_set_rel_pred_thms plugins fp live As Bs C E abs_inverses ctr_defs'
   597 fun derive_map_set_rel_pred_thms plugins fp live As Bs C E abs_inverses ctr_defs fp_nesting_set_maps
   598     fp_nesting_set_maps fp_nesting_rel_eq_onps live_nesting_map_id0s live_nesting_set_maps
   598     fp_nesting_rel_eq_onps live_nesting_map_id0s live_nesting_set_maps live_nesting_rel_eqs
   599     live_nesting_rel_eqs live_nesting_rel_eq_onps fp_b_name fp_bnf fp_bnfs fpT ctor ctor_dtor
   599     live_nesting_rel_eq_onps fp_b_name fp_bnf fp_bnfs fpT ctor ctor_dtor dtor_ctor pre_map_def
   600     dtor_ctor pre_map_def pre_set_defs pre_rel_def fp_map_thm fp_set_thms fp_rel_thm ctr_Tss abs
   600     pre_set_defs pre_rel_def fp_map_thm fp_set_thms fp_rel_thm ctr_Tss abs
   601     ({casex, case_thms, discs, selss, sel_defs, ctrs, exhaust, exhaust_discs, disc_thmss, sel_thmss,
   601     ({casex, case_thms, discs, selss, sel_defs, ctrs, exhaust, exhaust_discs, disc_thmss, sel_thmss,
   602       injects, distincts, distinct_discsss, ...} : ctr_sugar)
   602       injects, distincts, distinct_discsss, ...} : ctr_sugar)
   603     lthy =
   603     lthy =
   604   let
   604   let
   605     val n = length ctr_Tss;
   605     val n = length ctr_Tss;
   624       ||>> yield_singleton (mk_Frees "b") fpBT
   624       ||>> yield_singleton (mk_Frees "b") fpBT
   625       ||>> apfst HOLogic.mk_Trueprop o yield_singleton (mk_Frees "thesis") HOLogic.boolT;
   625       ||>> apfst HOLogic.mk_Trueprop o yield_singleton (mk_Frees "thesis") HOLogic.boolT;
   626 
   626 
   627     val ctrAs = map (mk_ctr As) ctrs;
   627     val ctrAs = map (mk_ctr As) ctrs;
   628     val ctrBs = map (mk_ctr Bs) ctrs;
   628     val ctrBs = map (mk_ctr Bs) ctrs;
       
   629 
       
   630     val ctr_defs' = map2 (fn m => fn def => mk_unabs_def m (def RS meta_eq_to_obj_eq)) ms ctr_defs;
   629 
   631 
   630     fun derive_rel_case relAsBs rel_inject_thms rel_distinct_thms =
   632     fun derive_rel_case relAsBs rel_inject_thms rel_distinct_thms =
   631       let
   633       let
   632         val rel_Rs_a_b = list_comb (relAsBs, Rs) $ ta $ tb;
   634         val rel_Rs_a_b = list_comb (relAsBs, Rs) $ ta $ tb;
   633 
   635 
  1703   in
  1705   in
  1704     (postproc_co_induct lthy nn @{thm predicate2D} @{thm predicate2D_conj} rel_coinduct0_thm,
  1706     (postproc_co_induct lthy nn @{thm predicate2D} @{thm predicate2D_conj} rel_coinduct0_thm,
  1705      mk_coinduct_attrs fpA_Ts (map #ctrs ctr_sugars) (map #discs ctr_sugars) mss)
  1707      mk_coinduct_attrs fpA_Ts (map #ctrs ctr_sugars) (map #discs ctr_sugars) mss)
  1706   end;
  1708   end;
  1707 
  1709 
  1708 fun derive_set_induct_thms_for_types lthy nn fpTs ctrss setss dtor_set_inducts exhausts
  1710 fun derive_set_induct_thms_for_types lthy nn fpTs ctrss setss dtor_set_inducts exhausts set_pre_defs
  1709     set_pre_defs ctor_defs dtor_ctors Abs_pre_inverses =
  1711     ctor_defs dtor_ctors Abs_pre_inverses =
  1710   let
  1712   let
  1711     fun mk_prems A Ps ctr_args t ctxt =
  1713     fun mk_prems A Ps ctr_args t ctxt =
  1712       (case fastype_of t of
  1714       (case fastype_of t of
  1713         Type (type_name, innerTs) =>
  1715         Type (type_name, innerTs) =>
  1714         (case bnf_of ctxt type_name of
  1716         (case bnf_of ctxt type_name of
  2246     val dtors = map (mk_dtor As) dtors0;
  2248     val dtors = map (mk_dtor As) dtors0;
  2247 
  2249 
  2248     val fpTs = map (domain_type o fastype_of) dtors;
  2250     val fpTs = map (domain_type o fastype_of) dtors;
  2249     val fpBTs = map B_ify_T fpTs;
  2251     val fpBTs = map B_ify_T fpTs;
  2250 
  2252 
  2251     val code_attrs = if plugins code_plugin then [Code.add_default_eqn_attrib Code.Equation] else [];
  2253     val code_attrs =
       
  2254       if plugins code_plugin then [Code.add_default_eqn_attrib Code.Equation] else [];
  2252 
  2255 
  2253     val ctr_Tsss = map (map (map (Term.typ_subst_atomic (Xs ~~ fpTs)))) ctrXs_Tsss;
  2256     val ctr_Tsss = map (map (map (Term.typ_subst_atomic (Xs ~~ fpTs)))) ctrXs_Tsss;
  2254     val ns = map length ctr_Tsss;
  2257     val ns = map length ctr_Tsss;
  2255     val kss = map (fn n => 1 upto n) ns;
  2258     val kss = map (fn n => 1 upto n) ns;
  2256     val mss = map (map length) ctr_Tsss;
  2259     val mss = map (map length) ctr_Tsss;
  2257 
  2260 
  2258     val (xtor_co_recs, recs_args_types, corecs_args_types) =
  2261     val (xtor_co_recs, recs_args_types, corecs_args_types) =
  2259       mk_co_recs_prelims lthy fp ctr_Tsss fpTs Cs absTs repTs ns mss xtor_co_recs0;
  2262       mk_co_recs_prelims lthy fp ctr_Tsss fpTs Cs absTs repTs ns mss xtor_co_recs0;
  2260     val lthy' = lthy;
  2263     val lthy' = lthy;
  2261 
  2264 
  2262     fun define_ctrs_dtrs_for_type fp_bnf fp_b fpT C E ctor dtor xtor_co_rec ctor_dtor dtor_ctor
  2265     fun define_ctrs_dtrs_for_type fp_b fpT ctor dtor ctor_dtor dtor_ctor n ks abs ctr_bindings
  2263         ctor_inject pre_map_def pre_set_defs pre_rel_def fp_map_thm fp_set_thms fp_rel_thm n ks ms
  2266         ctr_mixfixes ctr_Tss no_defs_lthy =
  2264         abs abs_inject type_definition ctr_bindings ctr_mixfixes ctr_Tss disc_bindings sel_bindingss
       
  2265         raw_sel_default_eqs no_defs_lthy =
       
  2266       let
  2267       let
  2267         val fp_b_name = Binding.name_of fp_b;
  2268         val fp_b_name = Binding.name_of fp_b;
  2268 
  2269 
  2269         val ctr_absT = domain_type (fastype_of ctor);
  2270         val ctr_absT = domain_type (fastype_of ctor);
  2270 
  2271 
  2290 
  2291 
  2291         val ctr_rhss =
  2292         val ctr_rhss =
  2292           map2 (fn k => fn xs => fold_rev Term.lambda xs (ctor $ mk_absumprod ctr_absT abs n k xs))
  2293           map2 (fn k => fn xs => fold_rev Term.lambda xs (ctor $ mk_absumprod ctr_absT abs n k xs))
  2293             ks xss;
  2294             ks xss;
  2294 
  2295 
  2295         val ((raw_ctrs, raw_ctr_defs), (lthy', lthy)) = no_defs_lthy
  2296         val ((raw_ctrs, raw_ctr_defs), (lthy, lthy_old)) = no_defs_lthy
  2296           |> Local_Theory.open_target |> snd
  2297           |> Local_Theory.open_target |> snd
  2297           |> apfst split_list o @{fold_map 3} (fn b => fn mx => fn rhs =>
  2298           |> apfst split_list o @{fold_map 3} (fn b => fn mx => fn rhs =>
  2298               Local_Theory.define
  2299               Local_Theory.define ((b, mx),
  2299                 ((b, mx), ((Thm.make_def_binding (Config.get no_defs_lthy bnf_internals) b, []), rhs))
  2300                 ((Thm.make_def_binding (Config.get no_defs_lthy bnf_internals) b, []), rhs))
  2300                   #>> apsnd snd) ctr_bindings ctr_mixfixes ctr_rhss
  2301               #>> apsnd snd) ctr_bindings ctr_mixfixes ctr_rhss
  2301           ||> `Local_Theory.close_target;
  2302           ||> `Local_Theory.close_target;
  2302 
  2303 
  2303         val phi = Proof_Context.export_morphism lthy lthy';
  2304         val phi = Proof_Context.export_morphism lthy_old lthy;
  2304 
  2305 
  2305         val ctr_defs = map (Morphism.thm phi) raw_ctr_defs;
  2306         val ctr_defs = map (Morphism.thm phi) raw_ctr_defs;
  2306         val ctr_defs' =
       
  2307           map2 (fn m => fn def => mk_unabs_def m (def RS meta_eq_to_obj_eq)) ms ctr_defs;
       
  2308 
       
  2309         val ctrs0 = map (Morphism.term phi) raw_ctrs;
  2307         val ctrs0 = map (Morphism.term phi) raw_ctrs;
       
  2308       in
       
  2309         ((xss, ctrs0, ctor_iff_dtor_thm, ctr_defs), lthy)
       
  2310       end;
       
  2311 
       
  2312     fun define_ctrs_dtrs_for_type_etc fp_bnf fp_b fpT C E ctor dtor xtor_co_rec ctor_dtor dtor_ctor
       
  2313         ctor_inject pre_map_def pre_set_defs pre_rel_def fp_map_thm fp_set_thms fp_rel_thm n ks ms
       
  2314         abs abs_inject type_definition ctr_bindings ctr_mixfixes ctr_Tss disc_bindings sel_bindingss
       
  2315         raw_sel_default_eqs no_defs_lthy =
       
  2316       let
       
  2317         val ((xss, ctrs0, ctor_iff_dtor_thm, ctr_defs), lthy) =
       
  2318           define_ctrs_dtrs_for_type fp_b fpT ctor dtor ctor_dtor dtor_ctor n ks abs ctr_bindings
       
  2319             ctr_mixfixes ctr_Tss no_defs_lthy;
       
  2320 
       
  2321         val fp_b_name = Binding.name_of fp_b;
       
  2322 
  2310         val ctrs = map (mk_ctr As) ctrs0;
  2323         val ctrs = map (mk_ctr As) ctrs0;
  2311 
  2324 
  2312         fun wrap_ctrs lthy =
  2325         fun wrap_ctrs lthy =
  2313           let
  2326           let
  2314             val sumEN_thm' =
  2327             val sumEN_thm' =
  2338             val sel_default_eqs = map (prepare_term sel_default_lthy) raw_sel_default_eqs;
  2351             val sel_default_eqs = map (prepare_term sel_default_lthy) raw_sel_default_eqs;
  2339 
  2352 
  2340             fun ctr_spec_of disc_b ctr0 sel_bs = ((disc_b, ctr0), sel_bs);
  2353             fun ctr_spec_of disc_b ctr0 sel_bs = ((disc_b, ctr0), sel_bs);
  2341             val ctr_specs = @{map 3} ctr_spec_of disc_bindings ctrs0 sel_bindingss;
  2354             val ctr_specs = @{map 3} ctr_spec_of disc_bindings ctrs0 sel_bindingss;
  2342 
  2355 
  2343             val (ctr_sugar as {case_cong, ...}, lthy') =
  2356             val (ctr_sugar as {case_cong, ...}, lthy) =
  2344               free_constructors (ctr_sugar_kind_of_fp_kind fp) tacss
  2357               free_constructors (ctr_sugar_kind_of_fp_kind fp) tacss
  2345                 ((((plugins, discs_sels), standard_binding), ctr_specs), sel_default_eqs) lthy
  2358                 ((((plugins, discs_sels), standard_binding), ctr_specs), sel_default_eqs) lthy
  2346 
  2359 
  2347             val anonymous_notes =
  2360             val anonymous_notes =
  2348               [([case_cong], fundefcong_attrs)]
  2361               [([case_cong], fundefcong_attrs)]
  2349               |> map (fn (thms, attrs) => ((Binding.empty, attrs), [(thms, [])]));
  2362               |> map (fn (thms, attrs) => ((Binding.empty, attrs), [(thms, [])]));
  2350 
  2363 
  2351             val notes =
  2364             val notes =
  2352               if Config.get lthy' bnf_internals then
  2365               if Config.get lthy bnf_internals then
  2353                 [(ctor_iff_dtorN, [ctor_iff_dtor_thm], K [])]
  2366                 [(ctor_iff_dtorN, [ctor_iff_dtor_thm], K [])]
  2354                 |> massage_simple_notes fp_b_name
  2367                 |> massage_simple_notes fp_b_name
  2355               else
  2368               else
  2356                 [];
  2369                 [];
  2357           in
  2370           in
  2358             (ctr_sugar, lthy' |> Local_Theory.notes (anonymous_notes @ notes) |> snd)
  2371             (ctr_sugar, lthy |> Local_Theory.notes (anonymous_notes @ notes) |> snd)
  2359           end;
  2372           end;
  2360 
  2373 
  2361         fun mk_binding pre = Binding.qualify false fp_b_name (Binding.prefix_name (pre ^ "_") fp_b);
  2374         fun mk_binding pre = Binding.qualify false fp_b_name (Binding.prefix_name (pre ^ "_") fp_b);
  2362 
  2375 
  2363         fun massage_res (((ctr_sugar, maps_sets_rels), co_rec_res), lthy) =
  2376         fun massage_res (((ctr_sugar, maps_sets_rels), co_rec_res), lthy) =
  2364           (((maps_sets_rels, (ctrs, xss, ctor_iff_dtor_thm, ctr_defs, ctr_sugar)), co_rec_res),
  2377           (((maps_sets_rels, (ctrs, xss, ctor_iff_dtor_thm, ctr_defs, ctr_sugar)), co_rec_res),
  2365            lthy);
  2378            lthy);
  2366       in
  2379       in
  2367         (wrap_ctrs
  2380         (wrap_ctrs
  2368          #> (fn (ctr_sugar, lthy) =>
  2381          #> (fn (ctr_sugar, lthy) =>
  2369            derive_map_set_rel_pred_thms plugins fp live As Bs C E abs_inverses ctr_defs'
  2382            derive_map_set_rel_pred_thms plugins fp live As Bs C E abs_inverses ctr_defs
  2370              fp_nesting_set_maps fp_nesting_rel_eq_onps live_nesting_map_id0s live_nesting_set_maps
  2383              fp_nesting_set_maps fp_nesting_rel_eq_onps live_nesting_map_id0s live_nesting_set_maps
  2371              live_nesting_rel_eqs live_nesting_rel_eq_onps fp_b_name fp_bnf fp_bnfs fpT ctor
  2384              live_nesting_rel_eqs live_nesting_rel_eq_onps fp_b_name fp_bnf fp_bnfs fpT ctor
  2372              ctor_dtor dtor_ctor pre_map_def pre_set_defs pre_rel_def fp_map_thm fp_set_thms
  2385              ctor_dtor dtor_ctor pre_map_def pre_set_defs pre_rel_def fp_map_thm fp_set_thms
  2373              fp_rel_thm ctr_Tss abs ctr_sugar lthy
  2386              fp_rel_thm ctr_Tss abs ctr_sugar lthy
  2374            |>> pair ctr_sugar)
  2387            |>> pair ctr_sugar)
  2375          ##>>
  2388          ##>>
  2376            (if fp = Least_FP then define_rec (the recs_args_types) mk_binding fpTs Cs reps
  2389            (if fp = Least_FP then define_rec (the recs_args_types) mk_binding fpTs Cs reps
  2377             else define_corec (the corecs_args_types) mk_binding fpTs Cs abss) xtor_co_rec
  2390             else define_corec (the corecs_args_types) mk_binding fpTs Cs abss) xtor_co_rec
  2378          #> massage_res, lthy')
  2391          #> massage_res, lthy)
  2379       end;
  2392       end;
  2380 
  2393 
  2381     fun wrap_ctrs_derive_map_set_rel_pred_thms_define_co_rec_for_types (wrap_one_etc, lthy) =
  2394     fun wrap_ctrs_derive_map_set_rel_pred_thms_define_co_rec_for_types (wrap_one_etc, lthy) =
  2382       fold_map I wrap_one_etc lthy
  2395       fold_map I wrap_one_etc lthy
  2383       |>> apsnd split_list o apfst (apsnd @{split_list 5} o apfst @{split_list 17} o split_list)
  2396       |>> apsnd split_list o apfst (apsnd @{split_list 5} o apfst @{split_list 17} o split_list)
  2736           sel_transferss map_o_corec_thmss
  2749           sel_transferss map_o_corec_thmss
  2737       end;
  2750       end;
  2738 
  2751 
  2739     val lthy'' = lthy'
  2752     val lthy'' = lthy'
  2740       |> live > 0 ? fold2 (fn Type (s, _) => fn bnf => register_bnf_raw s bnf) fpTs fp_bnfs
  2753       |> live > 0 ? fold2 (fn Type (s, _) => fn bnf => register_bnf_raw s bnf) fpTs fp_bnfs
  2741       |> @{fold_map 29} define_ctrs_dtrs_for_type fp_bnfs fp_bs fpTs Cs Es ctors dtors
  2754       |> @{fold_map 29} define_ctrs_dtrs_for_type_etc fp_bnfs fp_bs fpTs Cs Es ctors dtors
  2742         xtor_co_recs ctor_dtors dtor_ctors ctor_injects pre_map_defs pre_set_defss pre_rel_defs
  2755         xtor_co_recs ctor_dtors dtor_ctors ctor_injects pre_map_defs pre_set_defss pre_rel_defs
  2743         xtor_maps xtor_setss xtor_rels ns kss mss abss abs_injects type_definitions ctr_bindingss
  2756         xtor_maps xtor_setss xtor_rels ns kss mss abss abs_injects type_definitions ctr_bindingss
  2744         ctr_mixfixess ctr_Tsss disc_bindingss sel_bindingsss raw_sel_default_eqss
  2757         ctr_mixfixess ctr_Tsss disc_bindingss sel_bindingsss raw_sel_default_eqss
  2745       |> wrap_ctrs_derive_map_set_rel_pred_thms_define_co_rec_for_types
  2758       |> wrap_ctrs_derive_map_set_rel_pred_thms_define_co_rec_for_types
  2746       |> case_fp fp derive_note_induct_recs_thms_for_types
  2759       |> case_fp fp derive_note_induct_recs_thms_for_types