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