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