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