(* Title: HOL/Tools/BNF/bnf_comp.ML Author: Dmitriy Traytel, TU Muenchen Author: Jasmin Blanchette, TU Muenchen Copyright 2012
Composition of bounded natural functors.
*)
signature BNF_COMP = sig val typedef_threshold: int Config.T val with_typedef_threshold: int -> (Proof.context -> Proof.context) -> Proof.context ->
Proof.context val with_typedef_threshold_yield: int -> (Proof.context -> 'a * Proof.context) -> Proof.context -> 'a * Proof.context
val ID_bnf: BNF_Def.bnf val DEADID_bnf: BNF_Def.bnf
type comp_cache type unfold_set =
{map_unfolds: thm list,
set_unfoldss: thm listlist,
rel_unfolds: thm list,
pred_unfolds: thm list}
val empty_comp_cache: comp_cache val empty_unfolds: unfold_set
exception BAD_DEAD of typ * typ
val bnf_of_typ: bool -> BNF_Def.inline_policy -> (binding -> binding) ->
((string * sort) listlist -> (string * sort) list) -> (string * sort) list ->
(string * sort) list -> typ -> (comp_cache * unfold_set) * local_theory ->
(BNF_Def.bnf * (typ list * typ list)) * ((comp_cache * unfold_set) * local_theory) val default_comp_sort: (string * sort) listlist -> (string * sort) list val clean_compose_bnf: BNF_Def.inline_policy -> (binding -> binding) -> binding -> BNF_Def.bnf ->
BNF_Def.bnf list -> unfold_set * local_theory -> BNF_Def.bnf * (unfold_set * local_theory) val kill_bnf: (binding -> binding) -> int -> BNF_Def.bnf ->
(comp_cache * unfold_set) * local_theory ->
BNF_Def.bnf * ((comp_cache * unfold_set) * local_theory) val lift_bnf: (binding -> binding) -> int -> BNF_Def.bnf ->
(comp_cache * unfold_set) * local_theory ->
BNF_Def.bnf * ((comp_cache * unfold_set) * local_theory) val permute_bnf: (binding -> binding) -> int list -> int list -> BNF_Def.bnf ->
(comp_cache * unfold_set) * local_theory ->
BNF_Def.bnf * ((comp_cache * unfold_set) * local_theory) val permute_and_kill_bnf: (binding -> binding) -> int -> int list -> int list -> BNF_Def.bnf ->
(comp_cache * unfold_set) * local_theory ->
BNF_Def.bnf * ((comp_cache * unfold_set) * local_theory) val lift_and_permute_bnf: (binding -> binding) -> int -> int list -> int list -> BNF_Def.bnf ->
(comp_cache * unfold_set) * local_theory ->
BNF_Def.bnf * ((comp_cache * unfold_set) * local_theory) val normalize_bnfs: (int -> binding -> binding) -> ''a listlist -> ''a list ->
(''a listlist -> ''a list) -> BNF_Def.bnf list -> (comp_cache * unfold_set) * local_theory ->
(int listlist * ''a list) * (BNF_Def.bnf list * ((comp_cache * unfold_set) * local_theory)) val compose_bnf: BNF_Def.inline_policy -> (int -> binding -> binding) ->
((string * sort) listlist -> (string * sort) list) -> BNF_Def.bnf -> BNF_Def.bnf list ->
typ list -> typ listlist -> typ listlist -> (comp_cache * unfold_set) * local_theory ->
(BNF_Def.bnf * (typ list * typ list)) * ((comp_cache * unfold_set) * local_theory) type absT_info =
{absT: typ,
repT: typ,
abs: term,
rep: term,
abs_inject: thm,
abs_inverse: thm,
type_definition: thm}
val morph_absT_info: morphism -> absT_info -> absT_info val mk_absT: theory -> typ -> typ -> typ -> typ val mk_repT: typ -> typ -> typ -> typ val mk_abs: typ -> term -> term val mk_rep: typ -> term -> term val seal_bnf: (binding -> binding) -> unfold_set -> binding -> bool -> typ list -> typ list ->
BNF_Def.bnf -> local_theory -> (BNF_Def.bnf * (typ list * absT_info)) * local_theory end;
structure BNF_Comp : BNF_COMP = struct
open BNF_Def open BNF_Util open BNF_Tactics open BNF_Comp_Tactics
val typedef_threshold = Attrib.setup_config_int \<^binding>\<open>bnf_typedef_threshold\<close> (K 6);
fun with_typedef_threshold threshold f lthy =
lthy
|> Config.put typedef_threshold threshold
|> f
|> Config.put typedef_threshold (Config.get lthy typedef_threshold);
fun with_typedef_threshold_yield threshold f lthy =
lthy
|> Config.put typedef_threshold threshold
|> f
||> Config.put typedef_threshold (Config.get lthy typedef_threshold);
val ID_bnf = the (bnf_of \<^context> "BNF_Composition.ID"); val DEADID_bnf = the (bnf_of \<^context> "BNF_Composition.DEADID");
type comp_cache = (bnf * (typ list * typ list)) Typtab.table;
fun key_of_types s Ts = Type (s, Ts); fun key_of_typess s = key_of_types s o map (key_of_types ""); fun typ_of_int n = Type (string_of_int n, []); fun typ_of_bnf bnf =
key_of_typess "" [[T_of_bnf bnf], lives_of_bnf bnf, sort Term_Ord.typ_ord (deads_of_bnf bnf)];
fun key_of_kill n bnf = key_of_types "k" [typ_of_int n, typ_of_bnf bnf]; fun key_of_lift n bnf = key_of_types "l" [typ_of_int n, typ_of_bnf bnf]; fun key_of_permute src dest bnf =
key_of_types "p" (map typ_of_int src @ map typ_of_int dest @ [typ_of_bnf bnf]); fun key_of_compose oDs Dss Ass outer inners =
key_of_types "c" (map (key_of_typess "") [[oDs], Dss, Ass, [map typ_of_bnf (outer :: inners)]]);
fun mk_killN n = "_kill" ^ string_of_int n; fun mk_liftN n = "_lift" ^ string_of_int n; fun mk_permuteN src dest = "_permute_" ^ implode (map string_of_int src) ^ "_" ^ implode (map string_of_int dest);
val id_bnf_def = @{thm id_bnf_def}; val expand_id_bnf_def =
Envir.expand_term_defs dest_Const
[Thm.prop_of id_bnf_def |> Logic.dest_equals |> apfst dest_Const];
fun is_sum_prod_natLeq (Const (\<^const_name>\<open>csum\<close>, _) $ t $ u) = forall is_sum_prod_natLeq [t, u]
| is_sum_prod_natLeq (Const (\<^const_name>\<open>cprod\<close>, _) $ t $ u) = forall is_sum_prod_natLeq [t, u]
| is_sum_prod_natLeq t = t aconv \<^term>\<open>natLeq\<close>;
fun clean_compose_bnf const_policy qualify b outer inners (unfold_set, lthy) = let val olive = live_of_bnf outer; val onwits = nwits_of_bnf outer; val odeads = deads_of_bnf outer; val inner = hd inners; val ilive = live_of_bnf inner; val ideadss = map deads_of_bnf inners; val inwitss = map nwits_of_bnf inners;
(* TODO: check olive = length inners > 0, forall inner from inners. ilive = live,
forall inner from inners. idead = dead *)
val (oDs, lthy1) = apfst (map TFree)
(Variable.invent_types (mapType.sort_of_atyp odeads) lthy); val (Dss, lthy2) = apfst (map (map TFree))
(fold_map Variable.invent_types (map (mapType.sort_of_atyp) ideadss) lthy1); val (Ass, lthy3) = apfst (replicate ilive o map TFree)
(Variable.invent_types (replicate ilive \<^sort>\<open>type\<close>) lthy2); val As = if ilive > 0 then hd Ass else []; val Ass_repl = replicate olive As; val (Bs, names_lthy) = apfst (map TFree)
(Variable.invent_types (replicate ilive \<^sort>\<open>type\<close>) lthy3); val Bss_repl = replicate olive Bs;
val (((((fs', Qs'), Ps'), Asets), xs), _) = names_lthy
|> apfst snd o mk_Frees' "f" (map2 (curry op -->) As Bs)
||>> apfst snd o mk_Frees' "Q" (map2 mk_pred2T As Bs)
||>> apfst snd o mk_Frees' "P" (map mk_pred1T As)
||>> mk_Frees "A" (map HOLogic.mk_setT As)
||>> mk_Frees "x" As;
val CAs = @{map 3} mk_T_of_bnf Dss Ass_repl inners; val CCA = mk_T_of_bnf oDs CAs outer; val CBs = @{map 3} mk_T_of_bnf Dss Bss_repl inners; val outer_sets = mk_sets_of_bnf (replicate olive oDs) (replicate olive CAs) outer; val inner_setss =
@{map 3} mk_sets_of_bnf (map (replicate ilive) Dss) (replicate olive Ass) inners; val inner_bds = @{map 3} mk_bd_of_bnf Dss Ass_repl inners; val outer_bd = mk_bd_of_bnf oDs CAs outer;
(*%f1 ... fn. outer.map (inner_1.map f1 ... fn) ... (inner_m.map f1 ... fn)*) val mapx = fold_rev Term.abs fs'
(Term.list_comb (mk_map_of_bnf oDs CAs CBs outer,
map2 (fn Ds => (fn f => Term.list_comb (f, map Bound (ilive - 1 downto 0))) o
mk_map_of_bnf Ds As Bs) Dss inners)); (*%Q1 ... Qn. outer.rel (inner_1.rel Q1 ... Qn) ... (inner_m.rel Q1 ... Qn)*) val rel = fold_rev Term.abs Qs'
(Term.list_comb (mk_rel_of_bnf oDs CAs CBs outer,
map2 (fn Ds => (fn f => Term.list_comb (f, map Bound (ilive - 1 downto 0))) o
mk_rel_of_bnf Ds As Bs) Dss inners)); (*%P1 ... Pn. outer.pred (inner_1.pred P1 ... Pn) ... (inner_m.pred P1 ... Pn)*) val pred = fold_rev Term.abs Ps'
(Term.list_comb (mk_pred_of_bnf oDs CAs outer,
map2 (fn Ds => (fn f => Term.list_comb (f, map Bound (ilive - 1 downto 0))) o
mk_pred_of_bnf Ds As) Dss inners));
(*Union o collect {outer.set_1 ... outer.set_m} o outer.map inner_1.set_i ... inner_m.set_i*) (*Union o collect {image inner_1.set_i o outer.set_1 ... image inner_m.set_i o outer.set_m}*) fun mk_set i = let val (setTs, T) = `(replicate olive o HOLogic.mk_setT) (nth As i); val outer_set = mk_collect
(mk_sets_of_bnf (replicate olive oDs) (replicate olive setTs) outer)
(mk_T_of_bnf oDs setTs outer --> HOLogic.mk_setT T); val inner_sets = map (fn sets => nth sets i) inner_setss; val outer_map = mk_map_of_bnf oDs CAs setTs outer; val map_inner_sets = Term.list_comb (outer_map, inner_sets); val collect_image = mk_collect
(map2 (fn f => fn set => HOLogic.mk_comp (mk_image f, set)) inner_sets outer_sets)
(CCA --> HOLogic.mk_setT T); in
(Library.foldl1 HOLogic.mk_comp [mk_Union T, outer_set, map_inner_sets],
HOLogic.mk_comp (mk_Union T, collect_image)) end;
val (bd', bd_ordIso_natLeq_thm_opt) = if is_sum_prod_natLeq bd then let val bd' = \<^term>\natLeq\; val bd_bd' = HOLogic.mk_prod (bd, bd'); val ordIso = Const (\<^const_name>\<open>ordIso\<close>, HOLogic.mk_setT (fastype_of bd_bd')); val goal = mk_Trueprop_mem (bd_bd', ordIso); in
(bd', SOME (Goal.prove_sorry lthy [] [] goal (bd_ordIso_natLeq_tac o #context)
|> Thm.close_derivation \<^here>)) end else
(bd, NONE);
fun rel_OO_Grp_tac ctxt = let val outer_rel_Grp = rel_Grp_of_bnf outer RS sym; val thm =
trans OF [in_alt_thm RS @{thm OO_Grp_cong},
trans OF [@{thm arg_cong2[of _ _ _ _ relcompp]} OF
[trans OF [outer_rel_Grp RS @{thm arg_cong[of _ _ conversep]},
rel_conversep_of_bnf outer RS sym], outer_rel_Grp],
trans OF [rel_OO_of_bnf outer RS sym, rel_cong0_of_bnf outer OF
(map (fn bnf => rel_OO_Grp_of_bnf bnf RS sym) inners)]]] RS sym; in
unfold_thms_tac ctxt set'_eq_sets THEN rtac ctxt thm 1 end;
fun pred_set_tac ctxt = let val pred_alt = unfold_thms ctxt @{thms Ball_Collect}
(trans OF [pred_cong0_of_bnf outer OFmap pred_set_of_bnf inners, pred_set_of_bnf outer]); val in_alt = in_alt_thm RS @{thm Collect_inj} RS sym; in
unfold_thms_tac ctxt (@{thm Ball_Collect} :: set'_eq_sets) THEN
HEADGOAL (rtac ctxt (trans OF [pred_alt, in_alt])) end
val bnf'' = morph_bnf phi bnf'; in
(bnf'', (add_bnf_to_unfolds bnf'' unfold_set, lthy')) end;
(* Killing live variables *)
fun raw_kill_bnf qualify n bnf (accum as (unfold_set, lthy)) = if n = 0 then (bnf, accum) else let val b = Binding.suffix_name (mk_killN n) (name_of_bnf bnf); val live = live_of_bnf bnf; val deads = deads_of_bnf bnf; val nwits = nwits_of_bnf bnf;
(* TODO: check 0 < n <= live *)
val (Ds, lthy1) = apfst (map TFree)
(Variable.invent_types (mapType.sort_of_atyp deads) lthy); val ((killedAs, As), lthy2) = apfst (`(take n) o map TFree)
(Variable.invent_types (replicate live \<^sort>\<open>type\<close>) lthy1); val (Bs, _(*lthy3*)) = apfst (append killedAs o map TFree)
(Variable.invent_types (replicate (live - n) \<^sort>\<open>type\<close>) lthy2);
val ((Asets, lives), _(*names_lthy*)) = lthy
|> mk_Frees "A" (map HOLogic.mk_setT (drop n As))
||>> mk_Frees "x" (drop n As); val xs = map (fn T => Const (\<^const_name>\<open>undefined\<close>, T)) killedAs @ lives;
val T = mk_T_of_bnf Ds As bnf;
(*bnf.map id ... id*) val mapx = Term.list_comb (mk_map_of_bnf Ds As Bs bnf, map HOLogic.id_const killedAs); (*bnf.rel (op =) ... (op =)*) val rel = Term.list_comb (mk_rel_of_bnf Ds As Bs bnf, map HOLogic.eq_const killedAs); (*bnf.pred (%_. True) ... (%_ True)*) val pred = Term.list_comb (mk_pred_of_bnf Ds As bnf, map (fn T => Term.absdummy T \<^term>\<open>True\<close>) killedAs);
val bnf_sets = mk_sets_of_bnf (replicate live Ds) (replicate live As) bnf; val sets = drop n bnf_sets;
val bd = mk_bd_of_bnf Ds As bnf;
fun map_id0_tac ctxt = rtac ctxt (map_id0_of_bnf bnf) 1; fun map_comp0_tac ctxt =
unfold_thms_tac ctxt ((map_comp0_of_bnf bnf RS sym) ::
@{thms comp_assoc id_comp comp_id}) THEN rtac ctxt refl 1; fun map_cong0_tac ctxt =
mk_kill_map_cong0_tac ctxt n (live - n) (map_cong0_of_bnf bnf); val set_map0_tacs = map (fn thm => fn ctxt => rtac ctxt thm 1) (drop n (set_map0_of_bnf bnf)); fun bd_card_order_tac ctxt = rtac ctxt (bd_card_order_of_bnf bnf) 1; fun bd_cinfinite_tac ctxt = rtac ctxt (bd_cinfinite_of_bnf bnf) 1; fun bd_regularCard_tac ctxt = rtac ctxt (bd_regularCard_of_bnf bnf) 1; val set_bd_tacs = map (fn thm => fn ctxt => rtac ctxt thm 1) (drop n (set_bd_of_bnf bnf));
val in_alt_thm = let val inx = mk_in Asets sets T; val in_alt = mk_in (map HOLogic.mk_UNIV killedAs @ Asets) bnf_sets T; val goal = fold_rev Logic.all Asets (mk_Trueprop_eq (inx, in_alt)); in
Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, prems = _} =>
kill_in_alt_tac ctxt) |> Thm.close_derivation \<^here> end;
fun le_rel_OO_tac ctxt =
EVERY' [rtac ctxt @{thm ord_le_eq_trans}, rtac ctxt (le_rel_OO_of_bnf bnf)] 1 THEN
unfold_thms_tac ctxt @{thms eq_OO} THEN rtac ctxt refl 1;
fun rel_OO_Grp_tac ctxt = let val rel_Grp = rel_Grp_of_bnf bnf RS sym val thm =
(trans OF [in_alt_thm RS @{thm OO_Grp_cong},
trans OF [@{thm arg_cong2[of _ _ _ _ relcompp]} OF
[trans OF [rel_Grp RS @{thm arg_cong[of _ _ conversep]},
rel_conversep_of_bnf bnf RS sym], rel_Grp],
trans OF [rel_OO_of_bnf bnf RS sym, rel_cong0_of_bnf bnf OF
(replicate n @{thm trans[OF Grp_UNIV_id[OF refl] eq_alt[symmetric]]} @
replicate (live - n) @{thm Grp_fst_snd})]]] RS sym); in
rtac ctxt thm 1 end;
fun pred_set_tac ctxt = mk_simple_pred_set_tac ctxt (pred_set_of_bnf bnf) in_alt_thm;
val wits = map (fn t => fold absfree (Term.add_frees t []) t)
(map (fn (I, wit) => Term.list_comb (wit, map (nth xs) I)) bnf_wits);
fun wit_tac ctxt = mk_simple_wit_tac ctxt (wit_thms_of_bnf bnf);
val (bnf', lthy') =
bnf_def Smart_Inline (K Dont_Note) true qualify tacs wit_tac (SOME (Ds @ killedAs))
Binding.empty Binding.empty Binding.empty []
(((((((b, T), mapx), sets), bd), wits), SOME rel), SOME pred) lthy; in
(bnf', (add_bnf_to_unfolds bnf' unfold_set, lthy')) end;
fun kill_bnf qualify n bnf (accum as ((cache, unfold_set), lthy)) = letval key = key_of_kill n bnf in
(case Typtab.lookup cache key of
SOME (bnf, _) => (bnf, accum)
| NONE => cache_comp_simple key cache (raw_kill_bnf qualify n bnf (unfold_set, lthy))) end;
(* Adding dummy live variables *)
fun raw_lift_bnf qualify n bnf (accum as (unfold_set, lthy)) = if n = 0 then (bnf, accum) else let val b = Binding.suffix_name (mk_liftN n) (name_of_bnf bnf); val live = live_of_bnf bnf; val deads = deads_of_bnf bnf; val nwits = nwits_of_bnf bnf;
(* TODO: check 0 < n *)
val (Ds, lthy1) = apfst (map TFree)
(Variable.invent_types (mapType.sort_of_atyp deads) lthy); val ((newAs, As), lthy2) = apfst (chop n o map TFree)
(Variable.invent_types (replicate (n + live) \<^sort>\<open>type\<close>) lthy1); val ((newBs, Bs), _(*lthy3*)) = apfst (chop n o map TFree)
(Variable.invent_types (replicate (n + live) \<^sort>\<open>type\<close>) lthy2);
val (Asets, _(*names_lthy*)) = lthy
|> mk_Frees "A" (map HOLogic.mk_setT (newAs @ As));
val T = mk_T_of_bnf Ds As bnf;
(*%f1 ... fn. bnf.map*) val mapx =
fold_rev Term.absdummy (map2 (curry op -->) newAs newBs) (mk_map_of_bnf Ds As Bs bnf); (*%Q1 ... Qn. bnf.rel*) val rel = fold_rev Term.absdummy (map2 mk_pred2T newAs newBs) (mk_rel_of_bnf Ds As Bs bnf); (*%P1 ... Pn. bnf.pred*) val pred = fold_rev Term.absdummy (map mk_pred1T newAs) (mk_pred_of_bnf Ds As bnf);
val bnf_sets = mk_sets_of_bnf (replicate live Ds) (replicate live As) bnf; val sets = map (fn A => absdummy T (HOLogic.mk_set A [])) newAs @ bnf_sets;
val bd = mk_bd_of_bnf Ds As bnf;
fun map_id0_tac ctxt = rtac ctxt (map_id0_of_bnf bnf) 1; fun map_comp0_tac ctxt =
unfold_thms_tac ctxt ((map_comp0_of_bnf bnf RS sym) ::
@{thms comp_assoc id_comp comp_id}) THEN rtac ctxt refl 1; fun map_cong0_tac ctxt =
rtac ctxt (map_cong0_of_bnf bnf) 1 THEN REPEAT_DETERM_N live (Goal.assume_rule_tac ctxt 1); val set_map0_tacs = if Config.get lthy quick_and_dirty then
replicate (n + live) (K all_tac) else
replicate n empty_natural_tac @ map (fn thm => fn ctxt => rtac ctxt thm 1) (set_map0_of_bnf bnf); fun bd_card_order_tac ctxt = rtac ctxt (bd_card_order_of_bnf bnf) 1; fun bd_cinfinite_tac ctxt = rtac ctxt (bd_cinfinite_of_bnf bnf) 1; fun bd_regularCard_tac ctxt = rtac ctxt (bd_regularCard_of_bnf bnf) 1; val set_bd_tacs = if Config.get lthy quick_and_dirty then
replicate (n + live) (K all_tac) else
replicate n (fn ctxt => mk_lift_set_bd_tac ctxt (bd_Cinfinite_of_bnf bnf)) @
(map (fn thm => fn ctxt => rtac ctxt thm 1) (set_bd_of_bnf bnf));
val in_alt_thm = let val inx = mk_in Asets sets T; val in_alt = mk_in (drop n Asets) bnf_sets T; val goal = fold_rev Logic.all Asets (mk_Trueprop_eq (inx, in_alt)); in
Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, prems = _} => lift_in_alt_tac ctxt)
|> Thm.close_derivation \<^here> end;
fun le_rel_OO_tac ctxt = rtac ctxt (le_rel_OO_of_bnf bnf) 1;
fun rel_OO_Grp_tac ctxt = mk_simple_rel_OO_Grp_tac ctxt (rel_OO_Grp_of_bnf bnf) in_alt_thm;
fun pred_set_tac ctxt = mk_simple_pred_set_tac ctxt (pred_set_of_bnf bnf) in_alt_thm;
fun wit_tac ctxt = mk_simple_wit_tac ctxt (wit_thms_of_bnf bnf);
val (bnf', lthy') =
bnf_def Smart_Inline (K Dont_Note) true qualify tacs wit_tac (SOME Ds) Binding.empty
Binding.empty Binding.empty []
(((((((b, T), mapx), sets), bd), wits), SOME rel), SOME pred) lthy; in
(bnf', (add_bnf_to_unfolds bnf' unfold_set, lthy')) end;
fun lift_bnf qualify n bnf (accum as ((cache, unfold_set), lthy)) = letval key = key_of_lift n bnf in
(case Typtab.lookup cache key of
SOME (bnf, _) => (bnf, accum)
| NONE => cache_comp_simple key cache (raw_lift_bnf qualify n bnf (unfold_set, lthy))) end;
(* Changing the order of live variables *)
fun raw_permute_bnf qualify src dest bnf (accum as (unfold_set, lthy)) = if src = dest then (bnf, accum) else let val b = Binding.suffix_name (mk_permuteN src dest) (name_of_bnf bnf) |> Binding.reset_pos; val live = live_of_bnf bnf; val deads = deads_of_bnf bnf; val nwits = nwits_of_bnf bnf;
fun permute xs = permute_like_unique (op =) src dest xs; fun unpermute xs = permute_like_unique (op =) dest src xs;
val (Ds, lthy1) = apfst (map TFree)
(Variable.invent_types (mapType.sort_of_atyp deads) lthy); val (As, lthy2) = apfst (map TFree)
(Variable.invent_types (replicate live \<^sort>\<open>type\<close>) lthy1); val (Bs, _(*lthy3*)) = apfst (map TFree)
(Variable.invent_types (replicate live \<^sort>\<open>type\<close>) lthy2);
val (Asets, _(*names_lthy*)) = lthy
|> mk_Frees "A" (map HOLogic.mk_setT (permute As));
val T = mk_T_of_bnf Ds As bnf;
(*%f(1) ... f(n). bnf.map f\<sigma>(1) ... f\<sigma>(n)*) val mapx = fold_rev Term.absdummy (permute (map2 (curry op -->) As Bs))
(Term.list_comb (mk_map_of_bnf Ds As Bs bnf, unpermute (map Bound (live - 1 downto 0)))); (*%Q(1) ... Q(n). bnf.rel Q\<sigma>(1) ... Q\<sigma>(n)*) val rel = fold_rev Term.absdummy (permute (map2 mk_pred2T As Bs))
(Term.list_comb (mk_rel_of_bnf Ds As Bs bnf, unpermute (map Bound (live - 1 downto 0)))); (*%P(1) ... P(n). bnf.pred P\<sigma>(1) ... P\<sigma>(n)*) val pred = fold_rev Term.absdummy (permute (map mk_pred1T As))
(Term.list_comb (mk_pred_of_bnf Ds As bnf, unpermute (map Bound (live - 1 downto 0))));
val bnf_sets = mk_sets_of_bnf (replicate live Ds) (replicate live As) bnf; val sets = permute bnf_sets;
val bd = mk_bd_of_bnf Ds As bnf;
fun map_id0_tac ctxt = rtac ctxt (map_id0_of_bnf bnf) 1; fun map_comp0_tac ctxt = rtac ctxt (map_comp0_of_bnf bnf) 1; fun map_cong0_tac ctxt =
rtac ctxt (map_cong0_of_bnf bnf) 1 THEN REPEAT_DETERM_N live (Goal.assume_rule_tac ctxt 1); val set_map0_tacs = permute (map (fn thm => fn ctxt => rtac ctxt thm 1) (set_map0_of_bnf bnf)); fun bd_card_order_tac ctxt = rtac ctxt (bd_card_order_of_bnf bnf) 1; fun bd_cinfinite_tac ctxt = rtac ctxt (bd_cinfinite_of_bnf bnf) 1; fun bd_regularCard_tac ctxt = rtac ctxt (bd_regularCard_of_bnf bnf) 1; val set_bd_tacs = permute (map (fn thm => fn ctxt => rtac ctxt thm 1) (set_bd_of_bnf bnf));
val in_alt_thm = let val inx = mk_in Asets sets T; val in_alt = mk_in (unpermute Asets) bnf_sets T; val goal = fold_rev Logic.all Asets (mk_Trueprop_eq (inx, in_alt)); in
Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, prems = _} =>
mk_permute_in_alt_tac ctxt src dest)
|> Thm.close_derivation \<^here> end;
fun le_rel_OO_tac ctxt = rtac ctxt (le_rel_OO_of_bnf bnf) 1;
fun rel_OO_Grp_tac ctxt = mk_simple_rel_OO_Grp_tac ctxt (rel_OO_Grp_of_bnf bnf) in_alt_thm;
fun pred_set_tac ctxt = mk_simple_pred_set_tac ctxt (pred_set_of_bnf bnf) in_alt_thm;
val bnf_map = fold_rev Term.absfree fs' (HOLogic.mk_comp (HOLogic.mk_comp (AbsB,
Term.list_comb (mk_map_of_bnf Ds As Bs bnf, fs)), RepA)); val bnf_sets = map ((fn t => HOLogic.mk_comp (t, RepA)))
(mk_sets_of_bnf (replicate live Ds) (replicate live As) bnf); val bnf_bd = mk_bd_of_bnf Ds As bnf; val bnf_rel = fold_rev Term.absfree Rs' (mk_vimage2p RepA RepB $
(Term.list_comb (mk_rel_of_bnf Ds As Bs bnf, Rs))); val bnf_pred = fold_rev Term.absfree Ps' (HOLogic.mk_comp
(Term.list_comb (mk_pred_of_bnf Ds As bnf, Ps), RepA));
(*bd may depend only on dead type variables*) val bd_repT = fst (dest_relT (fastype_of bnf_bd)); val bdT_bind = qualify (Binding.suffix_name ("_" ^ bdTN) b); val params = Term.add_tfreesT bd_repT []; val all_deads = map TFree (fold_rev Term.add_tfreesT all_Ds []);
val map_def = map_def_of_bnf bnf''; val set_defs = set_defs_of_bnf bnf''; val rel_def = rel_def_of_bnf bnf'';
val bnf_b = qualify b; val def_qualify =
Thm.def_binding o Binding.concealed o Binding.qualify false (Binding.name_of bnf_b); fun mk_prefix_binding pre = Binding.prefix_name (pre ^ "_") bnf_b; val map_b = def_qualify (mk_prefix_binding mapN); val rel_b = def_qualify (mk_prefix_binding relN); val set_bs = if live = 1 then [def_qualify (mk_prefix_binding setN)] elsemap (def_qualify o mk_prefix_binding o mk_setN) (1 upto live);
val (noted, lthy'') = lthy'
|> Local_Theory.notes notes
||> (if repTA = TA then I else register_bnf_raw (dest_Type_name TA) bnf'') in
((morph_bnf (substitute_noted_thm noted) bnf'', (all_deads, absT_info)), lthy'') end;
exception BAD_DEAD of typ * typ;
fun bnf_of_typ _ _ _ _ _ Ds0 (T as TFree T') accum =
(if member (op =) Ds0 T' then (DEADID_bnf, ([T], [])) else (ID_bnf, ([], [T])), accum)
| bnf_of_typ _ _ _ _ _ _ (TVar _) _ = error "Unexpected schematic variable"
| bnf_of_typ optim const_policy qualify' flatten_tyargs Xs Ds0 (T as Type (C, Ts))
(accum as (_, lthy)) = let fun check_bad_dead ((_, (deads, _)), _) = letval Ds = fold Term.add_tfreesT deads [] in
(case Library.inter (op =) Ds Xs of [] => ()
| X :: _ => raise BAD_DEAD (TFree X, T)) end;
val tfrees = subtract (op =) Ds0 (Term.add_tfreesT T []); val bnf_opt = if null tfrees then NONE else bnf_of lthy C; in
(case bnf_opt of
NONE => ((DEADID_bnf, ([T], [])), accum)
| SOME bnf => if optim andalso forall (can Term.dest_TFree) Ts andalso length Ts = length tfrees then let val T' = T_of_bnf bnf; val deads = deads_of_bnf bnf; val lives = lives_of_bnf bnf; val tvars' = Term.add_tvarsT T' []; val Ds_As =
apply2 (map (Term.typ_subst_TVars (map fst tvars' ~~ map TFree tfrees)))
(deads, lives); in ((bnf, Ds_As), accum) end else let val name = Long_Name.base_name C; fun qualify i = letval namei = name ^ nonzero_string_of_int i; in qualify' o Binding.qualify true namei end; val odead = dead_of_bnf bnf; val olive = live_of_bnf bnf; val Ds = map (fn i => TFree (string_of_int i, [])) (1 upto odead); val Us = Term.dest_Type_args (mk_T_of_bnf Ds (replicate olive dummyT) bnf); val oDs_pos = map (fn x => find_index (fn y => x = y) Us) Ds
|> filter (fn x => x >= 0); val oDs = map (nth Ts) oDs_pos; val Ts' = map (nth Ts) (subtract (op =) oDs_pos (0 upto length Ts - 1)); val ((inners, (Dss, Ass)), (accum', lthy')) =
apfst (apsnd split_list o split_list) (@{fold_map 2}
(fn i => bnf_of_typ optim Smart_Inline (qualify i) flatten_tyargs Xs Ds0)
(if length Ts' = 1 then [0] else 1 upto length Ts') Ts' accum); in
compose_bnf const_policy qualify flatten_tyargs bnf inners oDs Dss Ass (accum', lthy') end)
|> tap check_bad_dead end;
end;
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.30Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.