src/HOL/BNF/Tools/bnf_comp.ML
author traytel
Thu Apr 25 19:18:20 2013 +0200 (2013-04-25)
changeset 51782 84e7225f5ab6
parent 51767 bbcdd8519253
child 51837 087498724486
permissions -rw-r--r--
removed unnecessary assumptions in some theorems about cardinal exponentiation
     1 (*  Title:      HOL/BNF/Tools/bnf_comp.ML
     2     Author:     Dmitriy Traytel, TU Muenchen
     3     Author:     Jasmin Blanchette, TU Muenchen
     4     Copyright   2012
     5 
     6 Composition of bounded natural functors.
     7 *)
     8 
     9 signature BNF_COMP =
    10 sig
    11   val ID_bnf: BNF_Def.BNF
    12   val DEADID_bnf: BNF_Def.BNF
    13 
    14   type unfold_set
    15   val empty_unfolds: unfold_set
    16 
    17   val bnf_of_typ: BNF_Def.const_policy -> (binding -> binding) ->
    18     ((string * sort) list list -> (string * sort) list) -> typ -> unfold_set * Proof.context ->
    19     (BNF_Def.BNF * (typ list * typ list)) * (unfold_set * Proof.context)
    20   val default_comp_sort: (string * sort) list list -> (string * sort) list
    21   val normalize_bnfs: (int -> binding -> binding) -> ''a list list -> ''a list ->
    22     (''a list list -> ''a list) -> BNF_Def.BNF list -> unfold_set -> Proof.context ->
    23     (int list list * ''a list) * (BNF_Def.BNF list * (unfold_set * Proof.context))
    24   val seal_bnf: unfold_set -> binding -> typ list -> BNF_Def.BNF -> Proof.context ->
    25     (BNF_Def.BNF * typ list) * local_theory
    26 end;
    27 
    28 structure BNF_Comp : BNF_COMP =
    29 struct
    30 
    31 open BNF_Def
    32 open BNF_Util
    33 open BNF_Tactics
    34 open BNF_Comp_Tactics
    35 
    36 val ID_bnf = the (bnf_of @{context} "Basic_BNFs.ID");
    37 val DEADID_bnf = the (bnf_of @{context} "Basic_BNFs.DEADID");
    38 
    39 (* TODO: Replace by "BNF_Defs.defs list" *)
    40 type unfold_set = {
    41   map_unfolds: thm list,
    42   set_unfoldss: thm list list,
    43   rel_unfolds: thm list,
    44   srel_unfolds: thm list
    45 };
    46 
    47 val empty_unfolds = {map_unfolds = [], set_unfoldss = [], rel_unfolds = [], srel_unfolds = []};
    48 
    49 fun add_to_thms thms new = thms |> not (Thm.is_reflexive new) ? insert Thm.eq_thm new;
    50 fun adds_to_thms thms news = insert (eq_set Thm.eq_thm) (no_reflexive news) thms;
    51 
    52 fun add_to_unfolds map sets rel srel
    53   {map_unfolds, set_unfoldss, rel_unfolds, srel_unfolds} =
    54   {map_unfolds = add_to_thms map_unfolds map,
    55     set_unfoldss = adds_to_thms set_unfoldss sets,
    56     rel_unfolds = add_to_thms rel_unfolds rel,
    57     srel_unfolds = add_to_thms srel_unfolds srel};
    58 
    59 fun add_bnf_to_unfolds bnf =
    60   add_to_unfolds (map_def_of_bnf bnf) (set_defs_of_bnf bnf) (rel_def_of_bnf bnf)
    61     (srel_def_of_bnf bnf);
    62 
    63 val bdTN = "bdT";
    64 
    65 fun mk_killN n = "_kill" ^ string_of_int n;
    66 fun mk_liftN n = "_lift" ^ string_of_int n;
    67 fun mk_permuteN src dest =
    68   "_permute_" ^ implode (map string_of_int src) ^ "_" ^ implode (map string_of_int dest);
    69 
    70 (*copied from Envir.expand_term_free*)
    71 fun expand_term_const defs =
    72   let
    73     val eqs = map ((fn ((x, U), u) => (x, (U, u))) o apfst dest_Const) defs;
    74     val get = fn Const (x, _) => AList.lookup (op =) eqs x | _ => NONE;
    75   in Envir.expand_term get end;
    76 
    77 fun clean_compose_bnf const_policy qualify b outer inners (unfold_set, lthy) =
    78   let
    79     val olive = live_of_bnf outer;
    80     val onwits = nwits_of_bnf outer;
    81     val odead = dead_of_bnf outer;
    82     val inner = hd inners;
    83     val ilive = live_of_bnf inner;
    84     val ideads = map dead_of_bnf inners;
    85     val inwitss = map nwits_of_bnf inners;
    86 
    87     (* TODO: check olive = length inners > 0,
    88                    forall inner from inners. ilive = live,
    89                    forall inner from inners. idead = dead  *)
    90 
    91     val (oDs, lthy1) = apfst (map TFree)
    92       (Variable.invent_types (replicate odead HOLogic.typeS) lthy);
    93     val (Dss, lthy2) = apfst (map (map TFree))
    94         (fold_map Variable.invent_types (map (fn n => replicate n HOLogic.typeS) ideads) lthy1);
    95     val (Ass, lthy3) = apfst (replicate ilive o map TFree)
    96       (Variable.invent_types (replicate ilive HOLogic.typeS) lthy2);
    97     val As = if ilive > 0 then hd Ass else [];
    98     val Ass_repl = replicate olive As;
    99     val (Bs, _(*lthy4*)) = apfst (map TFree)
   100       (Variable.invent_types (replicate ilive HOLogic.typeS) lthy3);
   101     val Bss_repl = replicate olive Bs;
   102 
   103     val ((((fs', Qs'), Asets), xs), _(*names_lthy*)) = lthy
   104       |> apfst snd o mk_Frees' "f" (map2 (curry (op -->)) As Bs)
   105       ||>> apfst snd o mk_Frees' "Q" (map2 mk_pred2T As Bs)
   106       ||>> mk_Frees "A" (map HOLogic.mk_setT As)
   107       ||>> mk_Frees "x" As;
   108 
   109     val CAs = map3 mk_T_of_bnf Dss Ass_repl inners;
   110     val CCA = mk_T_of_bnf oDs CAs outer;
   111     val CBs = map3 mk_T_of_bnf Dss Bss_repl inners;
   112     val outer_sets = mk_sets_of_bnf (replicate olive oDs) (replicate olive CAs) outer;
   113     val inner_setss = map3 mk_sets_of_bnf (map (replicate ilive) Dss) (replicate olive Ass) inners;
   114     val inner_bds = map3 mk_bd_of_bnf Dss Ass_repl inners;
   115     val outer_bd = mk_bd_of_bnf oDs CAs outer;
   116 
   117     (*%f1 ... fn. outer.map (inner_1.map f1 ... fn) ... (inner_m.map f1 ... fn)*)
   118     val mapx = fold_rev Term.abs fs'
   119       (Term.list_comb (mk_map_of_bnf oDs CAs CBs outer,
   120         map2 (fn Ds => (fn f => Term.list_comb (f, map Bound (ilive - 1 downto 0))) o
   121           mk_map_of_bnf Ds As Bs) Dss inners));
   122     (*%Q1 ... Qn. outer.rel (inner_1.rel Q1 ... Qn) ... (inner_m.rel Q1 ... Qn)*)
   123     val rel = fold_rev Term.abs Qs'
   124       (Term.list_comb (mk_rel_of_bnf oDs CAs CBs outer,
   125         map2 (fn Ds => (fn f => Term.list_comb (f, map Bound (ilive - 1 downto 0))) o
   126           mk_rel_of_bnf Ds As Bs) Dss inners));
   127 
   128     (*Union o collect {outer.set_1 ... outer.set_m} o outer.map inner_1.set_i ... inner_m.set_i*)
   129     (*Union o collect {image inner_1.set_i o outer.set_1 ... image inner_m.set_i o outer.set_m}*)
   130     fun mk_set i =
   131       let
   132         val (setTs, T) = `(replicate olive o HOLogic.mk_setT) (nth As i);
   133         val outer_set = mk_collect
   134           (mk_sets_of_bnf (replicate olive oDs) (replicate olive setTs) outer)
   135           (mk_T_of_bnf oDs setTs outer --> HOLogic.mk_setT T);
   136         val inner_sets = map (fn sets => nth sets i) inner_setss;
   137         val outer_map = mk_map_of_bnf oDs CAs setTs outer;
   138         val map_inner_sets = Term.list_comb (outer_map, inner_sets);
   139         val collect_image = mk_collect
   140           (map2 (fn f => fn set => HOLogic.mk_comp (mk_image f, set)) inner_sets outer_sets)
   141           (CCA --> HOLogic.mk_setT T);
   142       in
   143         (Library.foldl1 HOLogic.mk_comp [mk_Union T, outer_set, map_inner_sets],
   144         HOLogic.mk_comp (mk_Union T, collect_image))
   145       end;
   146 
   147     val (sets, sets_alt) = map_split mk_set (0 upto ilive - 1);
   148 
   149     (*(inner_1.bd +c ... +c inner_m.bd) *c outer.bd*)
   150     val bd = Term.absdummy CCA (mk_cprod (Library.foldr1 (uncurry mk_csum) inner_bds) outer_bd);
   151 
   152     fun map_id_tac _ =
   153       mk_comp_map_id_tac (map_id_of_bnf outer) (map_cong0_of_bnf outer)
   154         (map map_id_of_bnf inners);
   155 
   156     fun map_comp_tac _ =
   157       mk_comp_map_comp_tac (map_comp_of_bnf outer) (map_cong0_of_bnf outer)
   158         (map map_comp_of_bnf inners);
   159 
   160     fun mk_single_set_map_tac i _ =
   161       mk_comp_set_map_tac (map_comp_of_bnf outer) (map_cong0_of_bnf outer)
   162         (collect_set_map_of_bnf outer)
   163         (map ((fn thms => nth thms i) o set_map_of_bnf) inners);
   164 
   165     val set_map_tacs = map mk_single_set_map_tac (0 upto ilive - 1);
   166 
   167     fun bd_card_order_tac _ =
   168       mk_comp_bd_card_order_tac (map bd_card_order_of_bnf inners) (bd_card_order_of_bnf outer);
   169 
   170     fun bd_cinfinite_tac _ =
   171       mk_comp_bd_cinfinite_tac (bd_cinfinite_of_bnf inner) (bd_cinfinite_of_bnf outer);
   172 
   173     val set_alt_thms =
   174       if ! quick_and_dirty then
   175         []
   176       else
   177         map (fn goal =>
   178           Goal.prove_sorry lthy [] [] goal
   179             (fn {context = ctxt, prems = _} =>
   180               mk_comp_set_alt_tac ctxt (collect_set_map_of_bnf outer))
   181           |> Thm.close_derivation)
   182         (map2 (curry (HOLogic.mk_Trueprop o HOLogic.mk_eq)) sets sets_alt);
   183 
   184     fun map_cong0_tac _ =
   185       mk_comp_map_cong0_tac set_alt_thms (map_cong0_of_bnf outer) (map map_cong0_of_bnf inners);
   186 
   187     val set_bd_tacs =
   188       if ! quick_and_dirty then
   189         replicate ilive (K all_tac)
   190       else
   191         let
   192           val outer_set_bds = set_bd_of_bnf outer;
   193           val inner_set_bdss = map set_bd_of_bnf inners;
   194           val inner_bd_Card_orders = map bd_Card_order_of_bnf inners;
   195           fun single_set_bd_thm i j =
   196             @{thm comp_single_set_bd} OF [nth inner_bd_Card_orders j, nth (nth inner_set_bdss j) i,
   197               nth outer_set_bds j]
   198           val single_set_bd_thmss =
   199             map ((fn f => map f (0 upto olive - 1)) o single_set_bd_thm) (0 upto ilive - 1);
   200         in
   201           map2 (fn set_alt => fn single_set_bds => fn {context = ctxt, prems = _} =>
   202             mk_comp_set_bd_tac ctxt set_alt single_set_bds)
   203           set_alt_thms single_set_bd_thmss
   204         end;
   205 
   206     val in_alt_thm =
   207       let
   208         val inx = mk_in Asets sets CCA;
   209         val in_alt = mk_in (map2 (mk_in Asets) inner_setss CAs) outer_sets CCA;
   210         val goal = fold_rev Logic.all Asets (mk_Trueprop_eq (inx, in_alt));
   211       in
   212         Goal.prove_sorry lthy [] [] goal
   213           (fn {context = ctxt, prems = _} => mk_comp_in_alt_tac ctxt set_alt_thms)
   214         |> Thm.close_derivation
   215       end;
   216 
   217     fun in_bd_tac _ =
   218       mk_comp_in_bd_tac in_alt_thm (map in_bd_of_bnf inners) (in_bd_of_bnf outer)
   219         (map bd_Cinfinite_of_bnf inners) (bd_Card_order_of_bnf outer);
   220 
   221     fun map_wpull_tac _ =
   222       mk_map_wpull_tac in_alt_thm (map map_wpull_of_bnf inners) (map_wpull_of_bnf outer);
   223 
   224     fun srel_O_Gr_tac _ =
   225       let
   226         val basic_thms = @{thms mem_Collect_eq fst_conv snd_conv}; (*TODO: tune*)
   227         val outer_srel_Gr = srel_Gr_of_bnf outer RS sym;
   228         val outer_srel_cong = srel_cong_of_bnf outer;
   229         val thm =
   230           (trans OF [in_alt_thm RS @{thm O_Gr_cong},
   231              trans OF [@{thm arg_cong2[of _ _ _ _ relcomp]} OF
   232                [trans OF [outer_srel_Gr RS @{thm arg_cong[of _ _ converse]},
   233                  srel_converse_of_bnf outer RS sym], outer_srel_Gr],
   234                trans OF [srel_O_of_bnf outer RS sym, outer_srel_cong OF
   235                  (map (fn bnf => srel_O_Gr_of_bnf bnf RS sym) inners)]]] RS sym)
   236           |> unfold_thms lthy (basic_thms @ srel_def_of_bnf outer :: map srel_def_of_bnf inners);
   237       in
   238         unfold_thms_tac lthy basic_thms THEN rtac thm 1
   239       end;
   240 
   241     val tacs = zip_axioms map_id_tac map_comp_tac map_cong0_tac set_map_tacs bd_card_order_tac
   242       bd_cinfinite_tac set_bd_tacs in_bd_tac map_wpull_tac srel_O_Gr_tac;
   243 
   244     val outer_wits = mk_wits_of_bnf (replicate onwits oDs) (replicate onwits CAs) outer;
   245 
   246     val inner_witss = map (map (fn (I, wit) => Term.list_comb (wit, map (nth xs) I)))
   247       (map3 (fn Ds => fn n => mk_wits_of_bnf (replicate n Ds) (replicate n As))
   248         Dss inwitss inners);
   249 
   250     val inner_witsss = map (map (nth inner_witss) o fst) outer_wits;
   251 
   252     val wits = (inner_witsss, (map (single o snd) outer_wits))
   253       |-> map2 (fold (map_product (fn iwit => fn owit => owit $ iwit)))
   254       |> flat
   255       |> map (`(fn t => Term.add_frees t []))
   256       |> minimize_wits
   257       |> map (fn (frees, t) => fold absfree frees t);
   258 
   259     fun wit_tac {context = ctxt, prems = _} =
   260       mk_comp_wit_tac ctxt (wit_thms_of_bnf outer) (collect_set_map_of_bnf outer)
   261         (maps wit_thms_of_bnf inners);
   262 
   263     val (bnf', lthy') =
   264       bnf_def const_policy (K Dont_Note) qualify tacs wit_tac (SOME (oDs @ flat Dss)) Binding.empty
   265         Binding.empty [] (((((b, mapx), sets), bd), wits), SOME rel) lthy;
   266   in
   267     (bnf', (add_bnf_to_unfolds bnf' unfold_set, lthy'))
   268   end;
   269 
   270 (* Killing live variables *)
   271 
   272 fun kill_bnf qualify n bnf (unfold_set, lthy) = if n = 0 then (bnf, (unfold_set, lthy)) else
   273   let
   274     val b = Binding.suffix_name (mk_killN n) (name_of_bnf bnf);
   275     val live = live_of_bnf bnf;
   276     val dead = dead_of_bnf bnf;
   277     val nwits = nwits_of_bnf bnf;
   278 
   279     (* TODO: check 0 < n <= live *)
   280 
   281     val (Ds, lthy1) = apfst (map TFree)
   282       (Variable.invent_types (replicate dead HOLogic.typeS) lthy);
   283     val ((killedAs, As), lthy2) = apfst (`(take n) o map TFree)
   284       (Variable.invent_types (replicate live HOLogic.typeS) lthy1);
   285     val (Bs, _(*lthy3*)) = apfst (append killedAs o map TFree)
   286       (Variable.invent_types (replicate (live - n) HOLogic.typeS) lthy2);
   287 
   288     val ((Asets, lives), _(*names_lthy*)) = lthy
   289       |> mk_Frees "A" (map HOLogic.mk_setT (drop n As))
   290       ||>> mk_Frees "x" (drop n As);
   291     val xs = map (fn T => HOLogic.choice_const T $ absdummy T @{term True}) killedAs @ lives;
   292 
   293     val T = mk_T_of_bnf Ds As bnf;
   294 
   295     (*bnf.map id ... id*)
   296     val mapx = Term.list_comb (mk_map_of_bnf Ds As Bs bnf, map HOLogic.id_const killedAs);
   297     (*bnf.rel (op =) ... (op =)*)
   298     val rel = Term.list_comb (mk_rel_of_bnf Ds As Bs bnf, map HOLogic.eq_const killedAs);
   299 
   300     val bnf_sets = mk_sets_of_bnf (replicate live Ds) (replicate live As) bnf;
   301     val sets = drop n bnf_sets;
   302 
   303     (*(|UNIV :: A1 set| +c ... +c |UNIV :: An set|) *c bnf.bd*)
   304     val bnf_bd = mk_bd_of_bnf Ds As bnf;
   305     val bd = mk_cprod
   306       (Library.foldr1 (uncurry mk_csum) (map (mk_card_of o HOLogic.mk_UNIV) killedAs)) bnf_bd;
   307 
   308     fun map_id_tac _ = rtac (map_id_of_bnf bnf) 1;
   309     fun map_comp_tac {context = ctxt, prems = _} =
   310       unfold_thms_tac ctxt ((map_comp_of_bnf bnf RS sym) :: @{thms o_assoc id_o o_id}) THEN
   311       rtac refl 1;
   312     fun map_cong0_tac {context = ctxt, prems = _} =
   313       mk_kill_map_cong0_tac ctxt n (live - n) (map_cong0_of_bnf bnf);
   314     val set_map_tacs = map (fn thm => fn _ => rtac thm 1) (drop n (set_map_of_bnf bnf));
   315     fun bd_card_order_tac _ = mk_kill_bd_card_order_tac n (bd_card_order_of_bnf bnf);
   316     fun bd_cinfinite_tac _ = mk_kill_bd_cinfinite_tac (bd_Cinfinite_of_bnf bnf);
   317     val set_bd_tacs =
   318       map (fn thm => fn _ => mk_kill_set_bd_tac (bd_Card_order_of_bnf bnf) thm)
   319         (drop n (set_bd_of_bnf bnf));
   320 
   321     val in_alt_thm =
   322       let
   323         val inx = mk_in Asets sets T;
   324         val in_alt = mk_in (map HOLogic.mk_UNIV killedAs @ Asets) bnf_sets T;
   325         val goal = fold_rev Logic.all Asets (mk_Trueprop_eq (inx, in_alt));
   326       in
   327         Goal.prove_sorry lthy [] [] goal (K kill_in_alt_tac) |> Thm.close_derivation
   328       end;
   329 
   330     fun in_bd_tac _ =
   331       mk_kill_in_bd_tac n (live > n) in_alt_thm (in_bd_of_bnf bnf) (bd_Card_order_of_bnf bnf)
   332         (bd_Cinfinite_of_bnf bnf) (bd_Cnotzero_of_bnf bnf);
   333     fun map_wpull_tac _ = mk_map_wpull_tac in_alt_thm [] (map_wpull_of_bnf bnf);
   334 
   335     fun srel_O_Gr_tac _ =
   336       let
   337         val srel_Gr = srel_Gr_of_bnf bnf RS sym
   338         val thm =
   339           (trans OF [in_alt_thm RS @{thm O_Gr_cong},
   340             trans OF [@{thm arg_cong2[of _ _ _ _ relcomp]} OF
   341               [trans OF [srel_Gr RS @{thm arg_cong[of _ _ converse]},
   342                 srel_converse_of_bnf bnf RS sym], srel_Gr],
   343               trans OF [srel_O_of_bnf bnf RS sym, srel_cong_of_bnf bnf OF
   344                 (replicate n @{thm trans[OF Gr_UNIV_id[OF refl] Id_alt[symmetric]]} @
   345                  replicate (live - n) @{thm Gr_fst_snd})]]] RS sym)
   346           |> unfold_thms lthy (srel_def_of_bnf bnf :: @{thms Id_def' mem_Collect_eq split_conv});
   347       in
   348         rtac thm 1
   349       end;
   350 
   351     val tacs = zip_axioms map_id_tac map_comp_tac map_cong0_tac set_map_tacs bd_card_order_tac
   352       bd_cinfinite_tac set_bd_tacs in_bd_tac map_wpull_tac srel_O_Gr_tac;
   353 
   354     val bnf_wits = mk_wits_of_bnf (replicate nwits Ds) (replicate nwits As) bnf;
   355 
   356     val wits = map (fn t => fold absfree (Term.add_frees t []) t)
   357       (map (fn (I, wit) => Term.list_comb (wit, map (nth xs) I)) bnf_wits);
   358 
   359     fun wit_tac _ = mk_simple_wit_tac (wit_thms_of_bnf bnf);
   360 
   361     val (bnf', lthy') =
   362       bnf_def Smart_Inline (K Dont_Note) qualify tacs wit_tac (SOME (killedAs @ Ds)) Binding.empty
   363         Binding.empty [] (((((b, mapx), sets), Term.absdummy T bd), wits), SOME rel) lthy;
   364   in
   365     (bnf', (add_bnf_to_unfolds bnf' unfold_set, lthy'))
   366   end;
   367 
   368 (* Adding dummy live variables *)
   369 
   370 fun lift_bnf qualify n bnf (unfold_set, lthy) = if n = 0 then (bnf, (unfold_set, lthy)) else
   371   let
   372     val b = Binding.suffix_name (mk_liftN n) (name_of_bnf bnf);
   373     val live = live_of_bnf bnf;
   374     val dead = dead_of_bnf bnf;
   375     val nwits = nwits_of_bnf bnf;
   376 
   377     (* TODO: check 0 < n *)
   378 
   379     val (Ds, lthy1) = apfst (map TFree)
   380       (Variable.invent_types (replicate dead HOLogic.typeS) lthy);
   381     val ((newAs, As), lthy2) = apfst (chop n o map TFree)
   382       (Variable.invent_types (replicate (n + live) HOLogic.typeS) lthy1);
   383     val ((newBs, Bs), _(*lthy3*)) = apfst (chop n o map TFree)
   384       (Variable.invent_types (replicate (n + live) HOLogic.typeS) lthy2);
   385 
   386     val (Asets, _(*names_lthy*)) = lthy
   387       |> mk_Frees "A" (map HOLogic.mk_setT (newAs @ As));
   388 
   389     val T = mk_T_of_bnf Ds As bnf;
   390 
   391     (*%f1 ... fn. bnf.map*)
   392     val mapx =
   393       fold_rev Term.absdummy (map2 (curry (op -->)) newAs newBs) (mk_map_of_bnf Ds As Bs bnf);
   394     (*%Q1 ... Qn. bnf.rel*)
   395     val rel = fold_rev Term.absdummy (map2 mk_pred2T newAs newBs) (mk_rel_of_bnf Ds As Bs bnf);
   396 
   397     val bnf_sets = mk_sets_of_bnf (replicate live Ds) (replicate live As) bnf;
   398     val sets = map (fn A => absdummy T (HOLogic.mk_set A [])) newAs @ bnf_sets;
   399 
   400     val bd = mk_bd_of_bnf Ds As bnf;
   401 
   402     fun map_id_tac _ = rtac (map_id_of_bnf bnf) 1;
   403     fun map_comp_tac {context = ctxt, prems = _} =
   404       unfold_thms_tac ctxt ((map_comp_of_bnf bnf RS sym) :: @{thms o_assoc id_o o_id}) THEN
   405       rtac refl 1;
   406     fun map_cong0_tac {context = ctxt, prems = _} =
   407       rtac (map_cong0_of_bnf bnf) 1 THEN REPEAT_DETERM_N live (Goal.assume_rule_tac ctxt 1);
   408     val set_map_tacs =
   409       if ! quick_and_dirty then
   410         replicate (n + live) (K all_tac)
   411       else
   412         replicate n (K empty_natural_tac) @
   413         map (fn thm => fn _ => rtac thm 1) (set_map_of_bnf bnf);
   414     fun bd_card_order_tac _ = rtac (bd_card_order_of_bnf bnf) 1;
   415     fun bd_cinfinite_tac _ = rtac (bd_cinfinite_of_bnf bnf) 1;
   416     val set_bd_tacs =
   417       if ! quick_and_dirty then
   418         replicate (n + live) (K all_tac)
   419       else
   420         replicate n (K (mk_lift_set_bd_tac (bd_Card_order_of_bnf bnf))) @
   421         (map (fn thm => fn _ => rtac thm 1) (set_bd_of_bnf bnf));
   422 
   423     val in_alt_thm =
   424       let
   425         val inx = mk_in Asets sets T;
   426         val in_alt = mk_in (drop n Asets) bnf_sets T;
   427         val goal = fold_rev Logic.all Asets (mk_Trueprop_eq (inx, in_alt));
   428       in
   429         Goal.prove_sorry lthy [] [] goal (K lift_in_alt_tac) |> Thm.close_derivation
   430       end;
   431 
   432     fun in_bd_tac _ = mk_lift_in_bd_tac n in_alt_thm (in_bd_of_bnf bnf) (bd_Card_order_of_bnf bnf);
   433     fun map_wpull_tac _ = mk_map_wpull_tac in_alt_thm [] (map_wpull_of_bnf bnf);
   434 
   435     fun srel_O_Gr_tac _ =
   436       mk_simple_srel_O_Gr_tac lthy (srel_def_of_bnf bnf) (srel_O_Gr_of_bnf bnf) in_alt_thm;
   437 
   438     val tacs = zip_axioms map_id_tac map_comp_tac map_cong0_tac set_map_tacs bd_card_order_tac
   439       bd_cinfinite_tac set_bd_tacs in_bd_tac map_wpull_tac srel_O_Gr_tac;
   440 
   441     val wits = map snd (mk_wits_of_bnf (replicate nwits Ds) (replicate nwits As) bnf);
   442 
   443     fun wit_tac _ = mk_simple_wit_tac (wit_thms_of_bnf bnf);
   444 
   445     val (bnf', lthy') =
   446       bnf_def Smart_Inline (K Dont_Note) qualify tacs wit_tac (SOME Ds) Binding.empty Binding.empty
   447         [] (((((b, mapx), sets), Term.absdummy T bd), wits), SOME rel) lthy;
   448 
   449   in
   450     (bnf', (add_bnf_to_unfolds bnf' unfold_set, lthy'))
   451   end;
   452 
   453 (* Changing the order of live variables *)
   454 
   455 fun permute_bnf qualify src dest bnf (unfold_set, lthy) =
   456   if src = dest then (bnf, (unfold_set, lthy)) else
   457   let
   458     val b = Binding.suffix_name (mk_permuteN src dest) (name_of_bnf bnf);
   459     val live = live_of_bnf bnf;
   460     val dead = dead_of_bnf bnf;
   461     val nwits = nwits_of_bnf bnf;
   462     fun permute xs = mk_permute src dest xs;
   463     fun permute_rev xs = mk_permute dest src xs;
   464 
   465     val (Ds, lthy1) = apfst (map TFree)
   466       (Variable.invent_types (replicate dead HOLogic.typeS) lthy);
   467     val (As, lthy2) = apfst (map TFree)
   468       (Variable.invent_types (replicate live HOLogic.typeS) lthy1);
   469     val (Bs, _(*lthy3*)) = apfst (map TFree)
   470       (Variable.invent_types (replicate live HOLogic.typeS) lthy2);
   471 
   472     val (Asets, _(*names_lthy*)) = lthy
   473       |> mk_Frees "A" (map HOLogic.mk_setT (permute As));
   474 
   475     val T = mk_T_of_bnf Ds As bnf;
   476 
   477     (*%f(1) ... f(n). bnf.map f\<sigma>(1) ... f\<sigma>(n)*)
   478     val mapx = fold_rev Term.absdummy (permute (map2 (curry op -->) As Bs))
   479       (Term.list_comb (mk_map_of_bnf Ds As Bs bnf, permute_rev (map Bound (live - 1 downto 0))));
   480     (*%Q(1) ... Q(n). bnf.rel Q\<sigma>(1) ... Q\<sigma>(n)*)
   481     val rel = fold_rev Term.absdummy (permute (map2 mk_pred2T As Bs))
   482       (Term.list_comb (mk_rel_of_bnf Ds As Bs bnf, permute_rev (map Bound (live - 1 downto 0))));
   483 
   484     val bnf_sets = mk_sets_of_bnf (replicate live Ds) (replicate live As) bnf;
   485     val sets = permute bnf_sets;
   486 
   487     val bd = mk_bd_of_bnf Ds As bnf;
   488 
   489     fun map_id_tac _ = rtac (map_id_of_bnf bnf) 1;
   490     fun map_comp_tac _ = rtac (map_comp_of_bnf bnf) 1;
   491     fun map_cong0_tac {context = ctxt, prems = _} =
   492       rtac (map_cong0_of_bnf bnf) 1 THEN REPEAT_DETERM_N live (Goal.assume_rule_tac ctxt 1);
   493     val set_map_tacs = permute (map (fn thm => fn _ => rtac thm 1) (set_map_of_bnf bnf));
   494     fun bd_card_order_tac _ = rtac (bd_card_order_of_bnf bnf) 1;
   495     fun bd_cinfinite_tac _ = rtac (bd_cinfinite_of_bnf bnf) 1;
   496     val set_bd_tacs = permute (map (fn thm => fn _ => rtac thm 1) (set_bd_of_bnf bnf));
   497 
   498     val in_alt_thm =
   499       let
   500         val inx = mk_in Asets sets T;
   501         val in_alt = mk_in (permute_rev Asets) bnf_sets T;
   502         val goal = fold_rev Logic.all Asets (mk_Trueprop_eq (inx, in_alt));
   503       in
   504         Goal.prove_sorry lthy [] [] goal (K (mk_permute_in_alt_tac src dest))
   505         |> Thm.close_derivation
   506       end;
   507 
   508     fun in_bd_tac _ =
   509       mk_permute_in_bd_tac src dest in_alt_thm (in_bd_of_bnf bnf) (bd_Card_order_of_bnf bnf);
   510     fun map_wpull_tac _ = mk_map_wpull_tac in_alt_thm [] (map_wpull_of_bnf bnf);
   511 
   512     fun srel_O_Gr_tac _ =
   513       mk_simple_srel_O_Gr_tac lthy (srel_def_of_bnf bnf) (srel_O_Gr_of_bnf bnf) in_alt_thm;
   514 
   515     val tacs = zip_axioms map_id_tac map_comp_tac map_cong0_tac set_map_tacs bd_card_order_tac
   516       bd_cinfinite_tac set_bd_tacs in_bd_tac map_wpull_tac srel_O_Gr_tac;
   517 
   518     val wits = map snd (mk_wits_of_bnf (replicate nwits Ds) (replicate nwits As) bnf);
   519 
   520     fun wit_tac _ = mk_simple_wit_tac (wit_thms_of_bnf bnf);
   521 
   522     val (bnf', lthy') =
   523       bnf_def Smart_Inline (K Dont_Note) qualify tacs wit_tac (SOME Ds) Binding.empty Binding.empty
   524         [] (((((b, mapx), sets), Term.absdummy T bd), wits), SOME rel) lthy;
   525   in
   526     (bnf', (add_bnf_to_unfolds bnf' unfold_set, lthy'))
   527   end;
   528 
   529 (* Composition pipeline *)
   530 
   531 fun permute_and_kill qualify n src dest bnf =
   532   bnf
   533   |> permute_bnf qualify src dest
   534   #> uncurry (kill_bnf qualify n);
   535 
   536 fun lift_and_permute qualify n src dest bnf =
   537   bnf
   538   |> lift_bnf qualify n
   539   #> uncurry (permute_bnf qualify src dest);
   540 
   541 fun normalize_bnfs qualify Ass Ds sort bnfs unfold_set lthy =
   542   let
   543     val before_kill_src = map (fn As => 0 upto (length As - 1)) Ass;
   544     val kill_poss = map (find_indices Ds) Ass;
   545     val live_poss = map2 (subtract (op =)) kill_poss before_kill_src;
   546     val before_kill_dest = map2 append kill_poss live_poss;
   547     val kill_ns = map length kill_poss;
   548     val (inners', (unfold_set', lthy')) =
   549       fold_map5 (fn i => permute_and_kill (qualify i))
   550         (if length bnfs = 1 then [0] else (1 upto length bnfs))
   551         kill_ns before_kill_src before_kill_dest bnfs (unfold_set, lthy);
   552 
   553     val Ass' = map2 (map o nth) Ass live_poss;
   554     val As = sort Ass';
   555     val after_lift_dest = replicate (length Ass') (0 upto (length As - 1));
   556     val old_poss = map (map (fn x => find_index (fn y => x = y) As)) Ass';
   557     val new_poss = map2 (subtract (op =)) old_poss after_lift_dest;
   558     val after_lift_src = map2 append new_poss old_poss;
   559     val lift_ns = map (fn xs => length As - length xs) Ass';
   560   in
   561     ((kill_poss, As), fold_map5 (fn i => lift_and_permute (qualify i))
   562       (if length bnfs = 1 then [0] else (1 upto length bnfs))
   563       lift_ns after_lift_src after_lift_dest inners' (unfold_set', lthy'))
   564   end;
   565 
   566 fun default_comp_sort Ass =
   567   Library.sort (Term_Ord.typ_ord o pairself TFree) (fold (fold (insert (op =))) Ass []);
   568 
   569 fun compose_bnf const_policy qualify sort outer inners oDs Dss tfreess (unfold_set, lthy) =
   570   let
   571     val b = name_of_bnf outer;
   572 
   573     val Ass = map (map Term.dest_TFree) tfreess;
   574     val Ds = fold (fold Term.add_tfreesT) (oDs :: Dss) [];
   575 
   576     val ((kill_poss, As), (inners', (unfold_set', lthy'))) =
   577       normalize_bnfs qualify Ass Ds sort inners unfold_set lthy;
   578 
   579     val Ds = oDs @ flat (map3 (append oo map o nth) tfreess kill_poss Dss);
   580     val As = map TFree As;
   581   in
   582     apfst (rpair (Ds, As))
   583       (clean_compose_bnf const_policy (qualify 0) b outer inners' (unfold_set', lthy'))
   584   end;
   585 
   586 (* Hide the type of the bound (optimization) and unfold the definitions (nicer to the user) *)
   587 
   588 fun seal_bnf (unfold_set : unfold_set) b Ds bnf lthy =
   589   let
   590     val live = live_of_bnf bnf;
   591     val nwits = nwits_of_bnf bnf;
   592 
   593     val (As, lthy1) = apfst (map TFree)
   594       (Variable.invent_types (replicate live HOLogic.typeS) (fold Variable.declare_typ Ds lthy));
   595     val (Bs, _) = apfst (map TFree)
   596       (Variable.invent_types (replicate live HOLogic.typeS) lthy1);
   597 
   598     val map_unfolds = #map_unfolds unfold_set;
   599     val set_unfoldss = #set_unfoldss unfold_set;
   600     val rel_unfolds = #rel_unfolds unfold_set;
   601     val srel_unfolds = #srel_unfolds unfold_set;
   602 
   603     val expand_maps =
   604       fold expand_term_const (map (single o Logic.dest_equals o Thm.prop_of) map_unfolds);
   605     val expand_sets =
   606       fold expand_term_const (map (map (Logic.dest_equals o Thm.prop_of)) set_unfoldss);
   607     val expand_rels =
   608       fold expand_term_const (map (single o Logic.dest_equals o Thm.prop_of) rel_unfolds);
   609     val unfold_maps = fold (unfold_thms lthy o single) map_unfolds;
   610     val unfold_sets = fold (unfold_thms lthy) set_unfoldss;
   611     val unfold_rels = unfold_thms lthy rel_unfolds;
   612     val unfold_srels = unfold_thms lthy srel_unfolds;
   613     val unfold_all = unfold_sets o unfold_maps o unfold_rels o unfold_srels;
   614     val bnf_map = expand_maps (mk_map_of_bnf Ds As Bs bnf);
   615     val bnf_sets = map (expand_maps o expand_sets)
   616       (mk_sets_of_bnf (replicate live Ds) (replicate live As) bnf);
   617     val bnf_bd = mk_bd_of_bnf Ds As bnf;
   618     val bnf_rel = expand_rels (mk_rel_of_bnf Ds As Bs bnf);
   619     val T = mk_T_of_bnf Ds As bnf;
   620 
   621     (*bd should only depend on dead type variables!*)
   622     val bd_repT = fst (dest_relT (fastype_of bnf_bd));
   623     val bdT_bind = Binding.suffix_name ("_" ^ bdTN) b;
   624     val params = fold Term.add_tfreesT Ds [];
   625     val deads = map TFree params;
   626 
   627     val ((bdT_name, (bdT_glob_info, bdT_loc_info)), lthy) =
   628       typedef (bdT_bind, params, NoSyn)
   629         (HOLogic.mk_UNIV bd_repT) NONE (EVERY' [rtac exI, rtac UNIV_I] 1) lthy;
   630 
   631     val bnf_bd' = mk_dir_image bnf_bd
   632       (Const (#Abs_name bdT_glob_info, bd_repT --> Type (bdT_name, deads)))
   633 
   634     val Abs_bdT_inj = mk_Abs_inj_thm (#Abs_inject bdT_loc_info);
   635     val Abs_bdT_bij = mk_Abs_bij_thm lthy Abs_bdT_inj (#Abs_cases bdT_loc_info);
   636 
   637     val bd_ordIso = @{thm dir_image} OF [Abs_bdT_inj, bd_Card_order_of_bnf bnf];
   638     val bd_card_order =
   639       @{thm card_order_dir_image} OF [Abs_bdT_bij, bd_card_order_of_bnf bnf];
   640     val bd_cinfinite =
   641       (@{thm Cinfinite_cong} OF [bd_ordIso, bd_Cinfinite_of_bnf bnf]) RS conjunct1;
   642 
   643     val set_bds =
   644       map (fn thm => @{thm ordLeq_ordIso_trans} OF [thm, bd_ordIso]) (set_bd_of_bnf bnf);
   645     val in_bd =
   646       @{thm ordLeq_ordIso_trans} OF [in_bd_of_bnf bnf,
   647         @{thm cexp_cong2} OF [bd_ordIso, if live = 0 then
   648           @{thm Card_order_ctwo} else @{thm Card_order_csum},
   649             bd_Card_order_of_bnf bnf]];
   650 
   651     fun mk_tac thm {context = ctxt, prems = _} =
   652       (rtac (unfold_all thm) THEN'
   653       SOLVE o REPEAT_DETERM o (atac ORELSE' Goal.assume_rule_tac ctxt)) 1;
   654 
   655     val tacs = zip_axioms (mk_tac (map_id_of_bnf bnf)) (mk_tac (map_comp_of_bnf bnf))
   656       (mk_tac (map_cong0_of_bnf bnf)) (map mk_tac (set_map_of_bnf bnf))
   657       (K (rtac bd_card_order 1)) (K (rtac bd_cinfinite 1)) (map mk_tac set_bds) (mk_tac in_bd)
   658       (mk_tac (map_wpull_of_bnf bnf))
   659       (mk_tac (unfold_thms lthy [srel_def_of_bnf bnf] (srel_O_Gr_of_bnf bnf)));
   660 
   661     val bnf_wits = map snd (mk_wits_of_bnf (replicate nwits Ds) (replicate nwits As) bnf);
   662 
   663     fun wit_tac _ = mk_simple_wit_tac (map unfold_all (wit_thms_of_bnf bnf));
   664 
   665     val (bnf', lthy') = bnf_def Hardly_Inline (user_policy Dont_Note) I tacs wit_tac (SOME deads)
   666       Binding.empty Binding.empty []
   667       (((((b, bnf_map), bnf_sets), Term.absdummy T bnf_bd'), bnf_wits), SOME bnf_rel) lthy;
   668   in
   669     ((bnf', deads), lthy')
   670   end;
   671 
   672 fun bnf_of_typ _ _ _ (T as TFree _) accum = ((ID_bnf, ([], [T])), accum)
   673   | bnf_of_typ _ _ _ (TVar _) _ = error "Unexpected schematic variable"
   674   | bnf_of_typ const_policy qualify' sort (T as Type (C, Ts)) (unfold_set, lthy) =
   675     let
   676       val tfrees = Term.add_tfreesT T [];
   677       val bnf_opt = if null tfrees then NONE else bnf_of lthy C;
   678     in
   679       (case bnf_opt of
   680         NONE => ((DEADID_bnf, ([T], [])), (unfold_set, lthy))
   681       | SOME bnf =>
   682         if forall (can Term.dest_TFree) Ts andalso length Ts = length tfrees then
   683           let
   684             val T' = T_of_bnf bnf;
   685             val deads = deads_of_bnf bnf;
   686             val lives = lives_of_bnf bnf;
   687             val tvars' = Term.add_tvarsT T' [];
   688             val deads_lives =
   689               pairself (map (Term.typ_subst_TVars (map fst tvars' ~~ map TFree tfrees)))
   690                 (deads, lives);
   691           in ((bnf, deads_lives), (unfold_set, lthy)) end
   692         else
   693           let
   694             val name = Long_Name.base_name C;
   695             fun qualify i =
   696               let val namei = name ^ nonzero_string_of_int i;
   697               in qualify' o Binding.qualify true namei end;
   698             val odead = dead_of_bnf bnf;
   699             val olive = live_of_bnf bnf;
   700             val oDs_pos = find_indices [TFree ("dead", [])] (snd (Term.dest_Type
   701               (mk_T_of_bnf (replicate odead (TFree ("dead", []))) (replicate olive dummyT) bnf)));
   702             val oDs = map (nth Ts) oDs_pos;
   703             val Ts' = map (nth Ts) (subtract (op =) oDs_pos (0 upto length Ts - 1));
   704             val ((inners, (Dss, Ass)), (unfold_set', lthy')) =
   705               apfst (apsnd split_list o split_list)
   706                 (fold_map2 (fn i => bnf_of_typ Smart_Inline (qualify i) sort)
   707                 (if length Ts' = 1 then [0] else (1 upto length Ts')) Ts' (unfold_set, lthy));
   708           in
   709             compose_bnf const_policy qualify sort bnf inners oDs Dss Ass (unfold_set', lthy')
   710           end)
   711     end;
   712 
   713 end;