src/HOL/BNF/Tools/bnf_def.ML
author blanchet
Mon, 04 Nov 2013 16:53:43 +0100
changeset 55698 8fdb4dc08ed1
parent 55689 7cc6e286fe28
child 55717 3e1d230f1c00
permissions -rw-r--r--
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet@50524
     1
(*  Title:      HOL/BNF/Tools/bnf_def.ML
blanchet@49990
     2
    Author:     Dmitriy Traytel, TU Muenchen
blanchet@49990
     3
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@49990
     4
    Copyright   2012
blanchet@49990
     5
blanchet@49990
     6
Definition of bounded natural functors.
blanchet@49990
     7
*)
blanchet@49990
     8
blanchet@49990
     9
signature BNF_DEF =
blanchet@49990
    10
sig
blanchet@52974
    11
  type bnf
blanchet@49990
    12
  type nonemptiness_witness = {I: int list, wit: term, prop: thm list}
blanchet@49990
    13
blanchet@52974
    14
  val morph_bnf: morphism -> bnf -> bnf
blanchet@52974
    15
  val eq_bnf: bnf * bnf -> bool
blanchet@52974
    16
  val bnf_of: Proof.context -> string -> bnf option
blanchet@52974
    17
  val register_bnf: string -> (bnf * local_theory) -> (bnf * local_theory)
traytel@50449
    18
blanchet@52974
    19
  val name_of_bnf: bnf -> binding
blanchet@52974
    20
  val T_of_bnf: bnf -> typ
blanchet@52974
    21
  val live_of_bnf: bnf -> int
blanchet@52974
    22
  val lives_of_bnf: bnf -> typ list
blanchet@52974
    23
  val dead_of_bnf: bnf -> int
blanchet@52974
    24
  val deads_of_bnf: bnf -> typ list
blanchet@52974
    25
  val nwits_of_bnf: bnf -> int
blanchet@49990
    26
blanchet@49990
    27
  val mapN: string
blanchet@50522
    28
  val relN: string
blanchet@49990
    29
  val setN: string
blanchet@49990
    30
  val mk_setN: int -> string
blanchet@49990
    31
blanchet@52974
    32
  val map_of_bnf: bnf -> term
blanchet@52974
    33
  val sets_of_bnf: bnf -> term list
blanchet@52974
    34
  val rel_of_bnf: bnf -> term
blanchet@50229
    35
blanchet@52974
    36
  val mk_T_of_bnf: typ list -> typ list -> bnf -> typ
blanchet@52974
    37
  val mk_bd_of_bnf: typ list -> typ list -> bnf -> term
blanchet@52974
    38
  val mk_map_of_bnf: typ list -> typ list -> typ list -> bnf -> term
blanchet@52974
    39
  val mk_rel_of_bnf: typ list -> typ list -> typ list -> bnf -> term
blanchet@52974
    40
  val mk_sets_of_bnf: typ list list -> typ list list -> bnf -> term list
blanchet@52974
    41
  val mk_wits_of_bnf: typ list list -> typ list list -> bnf -> (int list * term) list
blanchet@49990
    42
blanchet@52974
    43
  val bd_Card_order_of_bnf: bnf -> thm
blanchet@52974
    44
  val bd_Cinfinite_of_bnf: bnf -> thm
blanchet@52974
    45
  val bd_Cnotzero_of_bnf: bnf -> thm
blanchet@52974
    46
  val bd_card_order_of_bnf: bnf -> thm
blanchet@52974
    47
  val bd_cinfinite_of_bnf: bnf -> thm
blanchet@52974
    48
  val collect_set_map_of_bnf: bnf -> thm
blanchet@52974
    49
  val in_bd_of_bnf: bnf -> thm
blanchet@52974
    50
  val in_cong_of_bnf: bnf -> thm
blanchet@52974
    51
  val in_mono_of_bnf: bnf -> thm
traytel@53030
    52
  val in_rel_of_bnf: bnf -> thm
blanchet@54424
    53
  val map_comp0_of_bnf: bnf -> thm
blanchet@54425
    54
  val map_comp_of_bnf: bnf -> thm
blanchet@52974
    55
  val map_cong0_of_bnf: bnf -> thm
blanchet@52974
    56
  val map_cong_of_bnf: bnf -> thm
blanchet@52974
    57
  val map_def_of_bnf: bnf -> thm
blanchet@54407
    58
  val map_id0_of_bnf: bnf -> thm
blanchet@54422
    59
  val map_id_of_bnf: bnf -> thm
traytel@53856
    60
  val map_transfer_of_bnf: bnf -> thm
blanchet@52974
    61
  val map_wppull_of_bnf: bnf -> thm
blanchet@52974
    62
  val map_wpull_of_bnf: bnf -> thm
blanchet@52974
    63
  val rel_def_of_bnf: bnf -> thm
traytel@53030
    64
  val rel_Grp_of_bnf: bnf -> thm
traytel@53030
    65
  val rel_OO_of_bnf: bnf -> thm
traytel@53030
    66
  val rel_OO_Grp_of_bnf: bnf -> thm
traytel@53030
    67
  val rel_cong_of_bnf: bnf -> thm
traytel@53030
    68
  val rel_conversep_of_bnf: bnf -> thm
traytel@53030
    69
  val rel_mono_of_bnf: bnf -> thm
traytel@53053
    70
  val rel_mono_strong_of_bnf: bnf -> thm
blanchet@52974
    71
  val rel_eq_of_bnf: bnf -> thm
blanchet@52974
    72
  val rel_flip_of_bnf: bnf -> thm
blanchet@52974
    73
  val set_bd_of_bnf: bnf -> thm list
blanchet@52974
    74
  val set_defs_of_bnf: bnf -> thm list
blanchet@54426
    75
  val set_map0_of_bnf: bnf -> thm list
blanchet@54427
    76
  val set_map_of_bnf: bnf -> thm list
blanchet@52974
    77
  val wit_thms_of_bnf: bnf -> thm list
blanchet@52974
    78
  val wit_thmss_of_bnf: bnf -> thm list list
blanchet@49990
    79
blanchet@55688
    80
  val mk_map: int -> typ list -> typ list -> term -> term
blanchet@55688
    81
  val mk_rel: int -> typ list -> typ list -> term -> term
blanchet@55689
    82
  val build_map: Proof.context -> (typ * typ -> term) -> typ * typ -> term
blanchet@55689
    83
  val build_rel: Proof.context -> (typ * typ -> term) -> typ * typ -> term
blanchet@55698
    84
  val flatten_type_args_of_bnf: bnf -> 'a -> 'a list -> 'a list
blanchet@55698
    85
  val map_flattened_map_args: Proof.context -> string -> (term list -> 'a list) -> term list ->
blanchet@55698
    86
    'a list
blanchet@55688
    87
blanchet@49990
    88
  val mk_witness: int list * term -> thm list -> nonemptiness_witness
traytel@50118
    89
  val minimize_wits: (''a list * 'b) list -> (''a list * 'b) list
blanchet@52974
    90
  val wits_of_bnf: bnf -> nonemptiness_witness list
blanchet@49990
    91
traytel@53772
    92
  val zip_axioms: 'a -> 'a -> 'a -> 'a list -> 'a -> 'a -> 'a list -> 'a -> 'a -> 'a list
blanchet@50471
    93
blanchet@49990
    94
  datatype const_policy = Dont_Inline | Hardly_Inline | Smart_Inline | Do_Inline
blanchet@50553
    95
  datatype fact_policy = Dont_Note | Note_Some | Note_All
blanchet@50553
    96
blanchet@49990
    97
  val bnf_note_all: bool Config.T
traytel@54280
    98
  val bnf_timing: bool Config.T
traytel@50450
    99
  val user_policy: fact_policy -> Proof.context -> fact_policy
traytel@53857
   100
  val note_bnf_thms: fact_policy -> (binding -> binding) -> binding -> bnf -> Proof.context ->
traytel@53857
   101
    Proof.context
blanchet@49990
   102
blanchet@49990
   103
  val print_bnfs: Proof.context -> unit
blanchet@50033
   104
  val bnf_def: const_policy -> (Proof.context -> fact_policy) -> (binding -> binding) ->
blanchet@49990
   105
    ({prems: thm list, context: Proof.context} -> tactic) list ->
blanchet@52895
   106
    ({prems: thm list, context: Proof.context} -> tactic) -> typ list option -> binding ->
blanchet@52904
   107
    binding -> binding list ->
blanchet@52904
   108
    ((((binding * term) * term list) * term) * term list) * term option ->
blanchet@52974
   109
    local_theory -> bnf * local_theory
blanchet@49990
   110
end;
blanchet@49990
   111
blanchet@49990
   112
structure BNF_Def : BNF_DEF =
blanchet@49990
   113
struct
blanchet@49990
   114
blanchet@49990
   115
open BNF_Util
blanchet@50478
   116
open BNF_Tactics
blanchet@50299
   117
open BNF_Def_Tactics
blanchet@49990
   118
blanchet@52902
   119
val fundef_cong_attrs = @{attributes [fundef_cong]};
blanchet@52902
   120
blanchet@49990
   121
type axioms = {
blanchet@54407
   122
  map_id0: thm,
blanchet@54424
   123
  map_comp0: thm,
blanchet@52898
   124
  map_cong0: thm,
blanchet@54426
   125
  set_map0: thm list,
blanchet@49990
   126
  bd_card_order: thm,
blanchet@49990
   127
  bd_cinfinite: thm,
blanchet@49990
   128
  set_bd: thm list,
blanchet@50468
   129
  map_wpull: thm,
traytel@53030
   130
  rel_OO_Grp: thm
blanchet@49990
   131
};
blanchet@49990
   132
blanchet@54426
   133
fun mk_axioms' ((((((((id, comp), cong), map), c_o), cinf), set_bd), wpull), rel) =
blanchet@54426
   134
  {map_id0 = id, map_comp0 = comp, map_cong0 = cong, set_map0 = map, bd_card_order = c_o,
traytel@53772
   135
   bd_cinfinite = cinf, set_bd = set_bd, map_wpull = wpull, rel_OO_Grp = rel};
blanchet@49990
   136
wenzelm@53067
   137
fun dest_cons [] = raise List.Empty
blanchet@49990
   138
  | dest_cons (x :: xs) = (x, xs);
blanchet@49990
   139
blanchet@49990
   140
fun mk_axioms n thms = thms
blanchet@49990
   141
  |> map the_single
blanchet@49990
   142
  |> dest_cons
blanchet@49990
   143
  ||>> dest_cons
blanchet@49990
   144
  ||>> dest_cons
blanchet@49990
   145
  ||>> chop n
blanchet@49990
   146
  ||>> dest_cons
blanchet@49990
   147
  ||>> dest_cons
blanchet@49990
   148
  ||>> chop n
blanchet@49990
   149
  ||>> dest_cons
blanchet@49990
   150
  ||> the_single
blanchet@49990
   151
  |> mk_axioms';
blanchet@49990
   152
blanchet@54426
   153
fun zip_axioms mid mcomp mcong smap bdco bdinf sbd wpull rel =
blanchet@54426
   154
  [mid, mcomp, mcong] @ smap @ [bdco, bdinf] @ sbd @ [wpull, rel];
blanchet@50475
   155
blanchet@54426
   156
fun dest_axioms {map_id0, map_comp0, map_cong0, set_map0, bd_card_order, bd_cinfinite, set_bd,
traytel@53030
   157
  map_wpull, rel_OO_Grp} =
blanchet@54426
   158
  zip_axioms map_id0 map_comp0 map_cong0 set_map0 bd_card_order bd_cinfinite set_bd map_wpull
traytel@53030
   159
    rel_OO_Grp;
blanchet@49990
   160
blanchet@54426
   161
fun map_axioms f {map_id0, map_comp0, map_cong0, set_map0, bd_card_order, bd_cinfinite, set_bd,
traytel@53772
   162
  map_wpull, rel_OO_Grp} =
blanchet@54407
   163
  {map_id0 = f map_id0,
blanchet@54424
   164
    map_comp0 = f map_comp0,
blanchet@52898
   165
    map_cong0 = f map_cong0,
blanchet@54426
   166
    set_map0 = map f set_map0,
blanchet@50478
   167
    bd_card_order = f bd_card_order,
blanchet@50478
   168
    bd_cinfinite = f bd_cinfinite,
blanchet@50478
   169
    set_bd = map f set_bd,
blanchet@50478
   170
    map_wpull = f map_wpull,
traytel@53030
   171
    rel_OO_Grp = f rel_OO_Grp};
blanchet@49990
   172
blanchet@49990
   173
val morph_axioms = map_axioms o Morphism.thm;
blanchet@49990
   174
blanchet@49990
   175
type defs = {
blanchet@49990
   176
  map_def: thm,
blanchet@49990
   177
  set_defs: thm list,
traytel@53030
   178
  rel_def: thm
blanchet@49990
   179
}
blanchet@49990
   180
traytel@53030
   181
fun mk_defs map sets rel = {map_def = map, set_defs = sets, rel_def = rel};
blanchet@49990
   182
traytel@53030
   183
fun map_defs f {map_def, set_defs, rel_def} =
traytel@53030
   184
  {map_def = f map_def, set_defs = map f set_defs, rel_def = f rel_def};
blanchet@49990
   185
blanchet@49990
   186
val morph_defs = map_defs o Morphism.thm;
blanchet@49990
   187
blanchet@49990
   188
type facts = {
blanchet@49990
   189
  bd_Card_order: thm,
blanchet@49990
   190
  bd_Cinfinite: thm,
blanchet@49990
   191
  bd_Cnotzero: thm,
blanchet@52903
   192
  collect_set_map: thm lazy,
traytel@53772
   193
  in_bd: thm lazy,
blanchet@49990
   194
  in_cong: thm lazy,
blanchet@49990
   195
  in_mono: thm lazy,
traytel@53030
   196
  in_rel: thm lazy,
blanchet@54425
   197
  map_comp: thm lazy,
blanchet@52899
   198
  map_cong: thm lazy,
blanchet@54422
   199
  map_id: thm lazy,
traytel@53856
   200
  map_transfer: thm lazy,
blanchet@49990
   201
  map_wppull: thm lazy,
blanchet@50606
   202
  rel_eq: thm lazy,
blanchet@50552
   203
  rel_flip: thm lazy,
blanchet@54427
   204
  set_map: thm lazy list,
traytel@53030
   205
  rel_cong: thm lazy,
traytel@53030
   206
  rel_mono: thm lazy,
traytel@53053
   207
  rel_mono_strong: thm lazy,
traytel@53030
   208
  rel_Grp: thm lazy,
traytel@53030
   209
  rel_conversep: thm lazy,
traytel@53030
   210
  rel_OO: thm lazy
blanchet@49990
   211
};
blanchet@49990
   212
traytel@53772
   213
fun mk_facts bd_Card_order bd_Cinfinite bd_Cnotzero collect_set_map in_bd in_cong in_mono in_rel
blanchet@54427
   214
    map_comp map_cong map_id map_transfer map_wppull rel_eq rel_flip set_map rel_cong rel_mono
traytel@53856
   215
    rel_mono_strong rel_Grp rel_conversep rel_OO = {
blanchet@49990
   216
  bd_Card_order = bd_Card_order,
blanchet@49990
   217
  bd_Cinfinite = bd_Cinfinite,
blanchet@49990
   218
  bd_Cnotzero = bd_Cnotzero,
blanchet@52903
   219
  collect_set_map = collect_set_map,
traytel@53772
   220
  in_bd = in_bd,
blanchet@49990
   221
  in_cong = in_cong,
blanchet@49990
   222
  in_mono = in_mono,
traytel@53030
   223
  in_rel = in_rel,
blanchet@54425
   224
  map_comp = map_comp,
blanchet@52899
   225
  map_cong = map_cong,
blanchet@54422
   226
  map_id = map_id,
traytel@53856
   227
  map_transfer = map_transfer,
blanchet@49990
   228
  map_wppull = map_wppull,
blanchet@50606
   229
  rel_eq = rel_eq,
blanchet@50552
   230
  rel_flip = rel_flip,
blanchet@54427
   231
  set_map = set_map,
traytel@53030
   232
  rel_cong = rel_cong,
traytel@53030
   233
  rel_mono = rel_mono,
traytel@53053
   234
  rel_mono_strong = rel_mono_strong,
traytel@53030
   235
  rel_Grp = rel_Grp,
traytel@53030
   236
  rel_conversep = rel_conversep,
traytel@53030
   237
  rel_OO = rel_OO};
blanchet@49990
   238
blanchet@49990
   239
fun map_facts f {
blanchet@49990
   240
  bd_Card_order,
blanchet@49990
   241
  bd_Cinfinite,
blanchet@49990
   242
  bd_Cnotzero,
blanchet@52903
   243
  collect_set_map,
traytel@53772
   244
  in_bd,
blanchet@49990
   245
  in_cong,
blanchet@49990
   246
  in_mono,
traytel@53030
   247
  in_rel,
blanchet@54425
   248
  map_comp,
blanchet@52899
   249
  map_cong,
blanchet@54422
   250
  map_id,
traytel@53856
   251
  map_transfer,
blanchet@49990
   252
  map_wppull,
blanchet@50606
   253
  rel_eq,
blanchet@50552
   254
  rel_flip,
blanchet@54427
   255
  set_map,
traytel@53030
   256
  rel_cong,
traytel@53030
   257
  rel_mono,
traytel@53053
   258
  rel_mono_strong,
traytel@53030
   259
  rel_Grp,
traytel@53030
   260
  rel_conversep,
traytel@53030
   261
  rel_OO} =
blanchet@49990
   262
  {bd_Card_order = f bd_Card_order,
blanchet@49990
   263
    bd_Cinfinite = f bd_Cinfinite,
blanchet@49990
   264
    bd_Cnotzero = f bd_Cnotzero,
blanchet@52903
   265
    collect_set_map = Lazy.map f collect_set_map,
traytel@53772
   266
    in_bd = Lazy.map f in_bd,
blanchet@49990
   267
    in_cong = Lazy.map f in_cong,
blanchet@49990
   268
    in_mono = Lazy.map f in_mono,
traytel@53030
   269
    in_rel = Lazy.map f in_rel,
blanchet@54425
   270
    map_comp = Lazy.map f map_comp,
blanchet@52899
   271
    map_cong = Lazy.map f map_cong,
blanchet@54422
   272
    map_id = Lazy.map f map_id,
traytel@53856
   273
    map_transfer = Lazy.map f map_transfer,
blanchet@49990
   274
    map_wppull = Lazy.map f map_wppull,
blanchet@50606
   275
    rel_eq = Lazy.map f rel_eq,
blanchet@50552
   276
    rel_flip = Lazy.map f rel_flip,
blanchet@54427
   277
    set_map = map (Lazy.map f) set_map,
traytel@53030
   278
    rel_cong = Lazy.map f rel_cong,
traytel@53030
   279
    rel_mono = Lazy.map f rel_mono,
traytel@53053
   280
    rel_mono_strong = Lazy.map f rel_mono_strong,
traytel@53030
   281
    rel_Grp = Lazy.map f rel_Grp,
traytel@53030
   282
    rel_conversep = Lazy.map f rel_conversep,
traytel@53030
   283
    rel_OO = Lazy.map f rel_OO};
blanchet@49990
   284
blanchet@49990
   285
val morph_facts = map_facts o Morphism.thm;
blanchet@49990
   286
blanchet@49990
   287
type nonemptiness_witness = {
blanchet@49990
   288
  I: int list,
blanchet@49990
   289
  wit: term,
blanchet@49990
   290
  prop: thm list
blanchet@49990
   291
};
blanchet@49990
   292
blanchet@49990
   293
fun mk_witness (I, wit) prop = {I = I, wit = wit, prop = prop};
blanchet@49990
   294
fun map_witness f g {I, wit, prop} = {I = I, wit = f wit, prop = map g prop};
blanchet@49990
   295
fun morph_witness phi = map_witness (Morphism.term phi) (Morphism.thm phi);
blanchet@49990
   296
blanchet@52974
   297
datatype bnf = BNF of {
blanchet@49990
   298
  name: binding,
blanchet@49990
   299
  T: typ,
blanchet@49990
   300
  live: int,
panny@54398
   301
  lives: typ list, (*source type variables of map*)
panny@54398
   302
  lives': typ list, (*target type variables of map*)
blanchet@49990
   303
  dead: int,
panny@54398
   304
  deads: typ list,
blanchet@49990
   305
  map: term,
blanchet@49990
   306
  sets: term list,
blanchet@49990
   307
  bd: term,
blanchet@49990
   308
  axioms: axioms,
blanchet@49990
   309
  defs: defs,
blanchet@49990
   310
  facts: facts,
blanchet@49990
   311
  nwits: int,
blanchet@49990
   312
  wits: nonemptiness_witness list,
traytel@53030
   313
  rel: term
blanchet@49990
   314
};
blanchet@49990
   315
blanchet@49990
   316
(* getters *)
blanchet@49990
   317
blanchet@49990
   318
fun rep_bnf (BNF bnf) = bnf;
blanchet@49990
   319
val name_of_bnf = #name o rep_bnf;
blanchet@49990
   320
val T_of_bnf = #T o rep_bnf;
blanchet@49990
   321
fun mk_T_of_bnf Ds Ts bnf =
blanchet@49990
   322
  let val bnf_rep = rep_bnf bnf
blanchet@49990
   323
  in Term.typ_subst_atomic ((#deads bnf_rep ~~ Ds) @ (#lives bnf_rep ~~ Ts)) (#T bnf_rep) end;
blanchet@49990
   324
val live_of_bnf = #live o rep_bnf;
blanchet@49990
   325
val lives_of_bnf = #lives o rep_bnf;
blanchet@49990
   326
val dead_of_bnf = #dead o rep_bnf;
blanchet@49990
   327
val deads_of_bnf = #deads o rep_bnf;
blanchet@49990
   328
val axioms_of_bnf = #axioms o rep_bnf;
blanchet@49990
   329
val facts_of_bnf = #facts o rep_bnf;
blanchet@49990
   330
val nwits_of_bnf = #nwits o rep_bnf;
blanchet@49990
   331
val wits_of_bnf = #wits o rep_bnf;
blanchet@49990
   332
blanchet@54168
   333
fun flatten_type_args_of_bnf bnf dead_x xs =
blanchet@54168
   334
  let
blanchet@54168
   335
    val Type (_, Ts) = T_of_bnf bnf;
blanchet@54168
   336
    val lives = lives_of_bnf bnf;
blanchet@54168
   337
    val deads = deads_of_bnf bnf;
blanchet@54168
   338
  in
blanchet@54177
   339
    permute_like (op =) (deads @ lives) Ts (replicate (length deads) dead_x @ xs)
blanchet@54168
   340
  end;
blanchet@54168
   341
blanchet@49990
   342
(*terms*)
blanchet@49990
   343
val map_of_bnf = #map o rep_bnf;
blanchet@49990
   344
val sets_of_bnf = #sets o rep_bnf;
blanchet@49990
   345
fun mk_map_of_bnf Ds Ts Us bnf =
blanchet@49990
   346
  let val bnf_rep = rep_bnf bnf;
blanchet@49990
   347
  in
blanchet@49990
   348
    Term.subst_atomic_types
blanchet@49990
   349
      ((#deads bnf_rep ~~ Ds) @ (#lives bnf_rep ~~ Ts) @ (#lives' bnf_rep ~~ Us)) (#map bnf_rep)
blanchet@49990
   350
  end;
blanchet@49990
   351
fun mk_sets_of_bnf Dss Tss bnf =
blanchet@49990
   352
  let val bnf_rep = rep_bnf bnf;
blanchet@49990
   353
  in
blanchet@49990
   354
    map2 (fn (Ds, Ts) => Term.subst_atomic_types
blanchet@49990
   355
      ((#deads bnf_rep ~~ Ds) @ (#lives bnf_rep ~~ Ts))) (Dss ~~ Tss) (#sets bnf_rep)
blanchet@49990
   356
  end;
blanchet@49990
   357
val bd_of_bnf = #bd o rep_bnf;
blanchet@49990
   358
fun mk_bd_of_bnf Ds Ts bnf =
blanchet@49990
   359
  let val bnf_rep = rep_bnf bnf;
blanchet@49990
   360
  in Term.subst_atomic_types ((#deads bnf_rep ~~ Ds) @ (#lives bnf_rep ~~ Ts)) (#bd bnf_rep) end;
blanchet@49990
   361
fun mk_wits_of_bnf Dss Tss bnf =
blanchet@49990
   362
  let
blanchet@49990
   363
    val bnf_rep = rep_bnf bnf;
blanchet@49990
   364
    val wits = map (fn x => (#I x, #wit x)) (#wits bnf_rep);
blanchet@49990
   365
  in
blanchet@49990
   366
    map2 (fn (Ds, Ts) => apsnd (Term.subst_atomic_types
blanchet@49990
   367
      ((#deads bnf_rep ~~ Ds) @ (#lives bnf_rep ~~ Ts)))) (Dss ~~ Tss) wits
blanchet@49990
   368
  end;
blanchet@50522
   369
val rel_of_bnf = #rel o rep_bnf;
blanchet@50522
   370
fun mk_rel_of_bnf Ds Ts Us bnf =
blanchet@50477
   371
  let val bnf_rep = rep_bnf bnf;
blanchet@50477
   372
  in
blanchet@50477
   373
    Term.subst_atomic_types
blanchet@50522
   374
      ((#deads bnf_rep ~~ Ds) @ (#lives bnf_rep ~~ Ts) @ (#lives' bnf_rep ~~ Us)) (#rel bnf_rep)
blanchet@50477
   375
  end;
blanchet@49990
   376
blanchet@49990
   377
(*thms*)
blanchet@49990
   378
val bd_card_order_of_bnf = #bd_card_order o #axioms o rep_bnf;
blanchet@49990
   379
val bd_cinfinite_of_bnf = #bd_cinfinite o #axioms o rep_bnf;
blanchet@49990
   380
val bd_Card_order_of_bnf = #bd_Card_order o #facts o rep_bnf;
blanchet@49990
   381
val bd_Cinfinite_of_bnf = #bd_Cinfinite o #facts o rep_bnf;
blanchet@49990
   382
val bd_Cnotzero_of_bnf = #bd_Cnotzero o #facts o rep_bnf;
blanchet@52903
   383
val collect_set_map_of_bnf = Lazy.force o #collect_set_map o #facts o rep_bnf;
traytel@53772
   384
val in_bd_of_bnf = Lazy.force o #in_bd o #facts o rep_bnf;
blanchet@49990
   385
val in_cong_of_bnf = Lazy.force o #in_cong o #facts o rep_bnf;
blanchet@49990
   386
val in_mono_of_bnf = Lazy.force o #in_mono o #facts o rep_bnf;
traytel@53030
   387
val in_rel_of_bnf = Lazy.force o #in_rel o #facts o rep_bnf;
blanchet@49990
   388
val map_def_of_bnf = #map_def o #defs o rep_bnf;
blanchet@54407
   389
val map_id0_of_bnf = #map_id0 o #axioms o rep_bnf;
blanchet@54422
   390
val map_id_of_bnf = Lazy.force o #map_id o #facts o rep_bnf;
blanchet@54424
   391
val map_comp0_of_bnf = #map_comp0 o #axioms o rep_bnf;
blanchet@54425
   392
val map_comp_of_bnf = Lazy.force o #map_comp o #facts o rep_bnf;
blanchet@52898
   393
val map_cong0_of_bnf = #map_cong0 o #axioms o rep_bnf;
blanchet@52899
   394
val map_cong_of_bnf = Lazy.force o #map_cong o #facts o rep_bnf;
traytel@53868
   395
val map_transfer_of_bnf = Lazy.force o #map_transfer o #facts o rep_bnf;
blanchet@49990
   396
val map_wppull_of_bnf = Lazy.force o #map_wppull o #facts o rep_bnf;
blanchet@49990
   397
val map_wpull_of_bnf = #map_wpull o #axioms o rep_bnf;
blanchet@50522
   398
val rel_def_of_bnf = #rel_def o #defs o rep_bnf;
blanchet@50606
   399
val rel_eq_of_bnf = Lazy.force o #rel_eq o #facts o rep_bnf;
blanchet@50552
   400
val rel_flip_of_bnf = Lazy.force o #rel_flip o #facts o rep_bnf;
blanchet@49990
   401
val set_bd_of_bnf = #set_bd o #axioms o rep_bnf;
blanchet@49990
   402
val set_defs_of_bnf = #set_defs o #defs o rep_bnf;
blanchet@54426
   403
val set_map0_of_bnf = #set_map0 o #axioms o rep_bnf;
blanchet@54427
   404
val set_map_of_bnf = map Lazy.force o #set_map o #facts o rep_bnf;
traytel@53030
   405
val rel_cong_of_bnf = Lazy.force o #rel_cong o #facts o rep_bnf;
traytel@53030
   406
val rel_mono_of_bnf = Lazy.force o #rel_mono o #facts o rep_bnf;
traytel@53053
   407
val rel_mono_strong_of_bnf = Lazy.force o #rel_mono_strong o #facts o rep_bnf;
traytel@53030
   408
val rel_Grp_of_bnf = Lazy.force o #rel_Grp o #facts o rep_bnf;
traytel@53030
   409
val rel_conversep_of_bnf = Lazy.force o #rel_conversep o #facts o rep_bnf;
traytel@53030
   410
val rel_OO_of_bnf = Lazy.force o #rel_OO o #facts o rep_bnf;
traytel@53030
   411
val rel_OO_Grp_of_bnf = #rel_OO_Grp o #axioms o rep_bnf;
blanchet@49990
   412
val wit_thms_of_bnf = maps #prop o wits_of_bnf;
blanchet@49990
   413
val wit_thmss_of_bnf = map #prop o wits_of_bnf;
blanchet@49990
   414
traytel@53030
   415
fun mk_bnf name T live lives lives' dead deads map sets bd axioms defs facts wits rel =
blanchet@49990
   416
  BNF {name = name, T = T,
blanchet@49990
   417
       live = live, lives = lives, lives' = lives', dead = dead, deads = deads,
blanchet@49990
   418
       map = map, sets = sets, bd = bd,
blanchet@49990
   419
       axioms = axioms, defs = defs, facts = facts,
traytel@53030
   420
       nwits = length wits, wits = wits, rel = rel};
blanchet@49990
   421
blanchet@49990
   422
fun morph_bnf phi (BNF {name = name, T = T, live = live, lives = lives, lives' = lives',
blanchet@49990
   423
  dead = dead, deads = deads, map = map, sets = sets, bd = bd,
blanchet@49990
   424
  axioms = axioms, defs = defs, facts = facts,
traytel@53030
   425
  nwits = nwits, wits = wits, rel = rel}) =
blanchet@49990
   426
  BNF {name = Morphism.binding phi name, T = Morphism.typ phi T,
blanchet@49990
   427
    live = live, lives = List.map (Morphism.typ phi) lives,
blanchet@49990
   428
    lives' = List.map (Morphism.typ phi) lives',
blanchet@49990
   429
    dead = dead, deads = List.map (Morphism.typ phi) deads,
blanchet@49990
   430
    map = Morphism.term phi map, sets = List.map (Morphism.term phi) sets,
blanchet@49990
   431
    bd = Morphism.term phi bd,
blanchet@49990
   432
    axioms = morph_axioms phi axioms,
blanchet@49990
   433
    defs = morph_defs phi defs,
blanchet@49990
   434
    facts = morph_facts phi facts,
blanchet@49990
   435
    nwits = nwits,
blanchet@49990
   436
    wits = List.map (morph_witness phi) wits,
traytel@53030
   437
    rel = Morphism.term phi rel};
blanchet@49990
   438
blanchet@49990
   439
fun eq_bnf (BNF {T = T1, live = live1, dead = dead1, ...},
blanchet@49990
   440
  BNF {T = T2, live = live2, dead = dead2, ...}) =
blanchet@49990
   441
  Type.could_unify (T1, T2) andalso live1 = live2 andalso dead1 = dead2;
blanchet@49990
   442
blanchet@49990
   443
structure Data = Generic_Data
blanchet@49990
   444
(
blanchet@52974
   445
  type T = bnf Symtab.table;
blanchet@49990
   446
  val empty = Symtab.empty;
blanchet@49990
   447
  val extend = I;
blanchet@50477
   448
  val merge = Symtab.merge eq_bnf;
blanchet@49990
   449
);
blanchet@49990
   450
traytel@54263
   451
fun bnf_of ctxt =
traytel@54263
   452
  Symtab.lookup (Data.get (Context.Proof ctxt))
traytel@54263
   453
  #> Option.map (morph_bnf (Morphism.thm_morphism (Thm.transfer (Proof_Context.theory_of ctxt))));
blanchet@49990
   454
blanchet@49990
   455
blanchet@49990
   456
(* Utilities *)
blanchet@49990
   457
blanchet@49990
   458
fun normalize_set insts instA set =
blanchet@49990
   459
  let
blanchet@49990
   460
    val (T, T') = dest_funT (fastype_of set);
blanchet@49990
   461
    val A = fst (Term.dest_TVar (HOLogic.dest_setT T'));
blanchet@49990
   462
    val params = Term.add_tvar_namesT T [];
blanchet@49990
   463
  in Term.subst_TVars ((A :: params) ~~ (instA :: insts)) set end;
blanchet@49990
   464
blanchet@50522
   465
fun normalize_rel ctxt instTs instA instB rel =
blanchet@50477
   466
  let
blanchet@50477
   467
    val thy = Proof_Context.theory_of ctxt;
blanchet@50477
   468
    val tyenv =
blanchet@50522
   469
      Sign.typ_match thy (fastype_of rel, Library.foldr (op -->) (instTs, mk_pred2T instA instB))
blanchet@50478
   470
        Vartab.empty;
blanchet@50522
   471
  in Envir.subst_term (tyenv, Vartab.empty) rel end
blanchet@50468
   472
  handle Type.TYPE_MATCH => error "Bad relator";
blanchet@49990
   473
blanchet@49990
   474
fun normalize_wit insts CA As wit =
blanchet@49990
   475
  let
blanchet@49990
   476
    fun strip_param (Ts, T as Type (@{type_name fun}, [T1, T2])) =
blanchet@49990
   477
        if Type.raw_instance (CA, T) then (Ts, T) else strip_param (T1 :: Ts, T2)
blanchet@49990
   478
      | strip_param x = x;
blanchet@49990
   479
    val (Ts, T) = strip_param ([], fastype_of wit);
blanchet@49990
   480
    val subst = Term.add_tvar_namesT T [] ~~ insts;
blanchet@49990
   481
    fun find y = find_index (fn x => x = y) As;
blanchet@49990
   482
  in
blanchet@49990
   483
    (map (find o Term.typ_subst_TVars subst) (rev Ts), Term.subst_TVars subst wit)
blanchet@49990
   484
  end;
blanchet@49990
   485
blanchet@49990
   486
fun minimize_wits wits =
blanchet@49990
   487
 let
blanchet@49990
   488
   fun minimize done [] = done
traytel@50118
   489
     | minimize done ((I, wit) :: todo) =
blanchet@49990
   490
       if exists (fn (J, _) => subset (op =) (J, I)) (done @ todo)
blanchet@49990
   491
       then minimize done todo
blanchet@49990
   492
       else minimize ((I, wit) :: done) todo;
blanchet@49990
   493
 in minimize [] wits end;
blanchet@49990
   494
blanchet@55688
   495
fun mk_map live Ts Us t =
blanchet@55688
   496
  let val (Type (_, Ts0), Type (_, Us0)) = strip_typeN (live + 1) (fastype_of t) |>> List.last in
blanchet@55688
   497
    Term.subst_atomic_types (Ts0 @ Us0 ~~ Ts @ Us) t
blanchet@55688
   498
  end;
blanchet@55688
   499
blanchet@55688
   500
fun mk_rel live Ts Us t =
blanchet@55688
   501
  let val [Type (_, Ts0), Type (_, Us0)] = binder_types (snd (strip_typeN live (fastype_of t))) in
blanchet@55688
   502
    Term.subst_atomic_types (Ts0 @ Us0 ~~ Ts @ Us) t
blanchet@55688
   503
  end;
blanchet@55688
   504
blanchet@55689
   505
fun build_map_or_rel mk const of_bnf dest ctxt build_simple =
blanchet@55688
   506
  let
blanchet@55688
   507
    fun build (TU as (T, U)) =
blanchet@55688
   508
      if T = U then
blanchet@55688
   509
        const T
blanchet@55688
   510
      else
blanchet@55688
   511
        (case TU of
blanchet@55688
   512
          (Type (s, Ts), Type (s', Us)) =>
blanchet@55688
   513
          if s = s' then
blanchet@55688
   514
            let
blanchet@55689
   515
              val bnf = the (bnf_of ctxt s);
blanchet@55688
   516
              val live = live_of_bnf bnf;
blanchet@55688
   517
              val mapx = mk live Ts Us (of_bnf bnf);
blanchet@55688
   518
              val TUs' = map dest (fst (strip_typeN live (fastype_of mapx)));
blanchet@55688
   519
            in Term.list_comb (mapx, map build TUs') end
blanchet@55688
   520
          else
blanchet@55688
   521
            build_simple TU
blanchet@55688
   522
        | _ => build_simple TU);
blanchet@55688
   523
  in build end;
blanchet@55688
   524
blanchet@55688
   525
val build_map = build_map_or_rel mk_map HOLogic.id_const map_of_bnf dest_funT;
blanchet@55688
   526
val build_rel = build_map_or_rel mk_rel HOLogic.eq_const rel_of_bnf dest_pred2T;
blanchet@49990
   527
blanchet@55698
   528
fun map_flattened_map_args ctxt s map_args fs =
blanchet@55698
   529
  let
blanchet@55698
   530
    val flat_fs = flatten_type_args_of_bnf (the (bnf_of ctxt s)) Term.dummy fs;
blanchet@55698
   531
    val flat_fs' = map_args flat_fs;
blanchet@55698
   532
  in
blanchet@55698
   533
    permute_like (op aconv) flat_fs fs flat_fs'
blanchet@55698
   534
  end;
blanchet@55698
   535
blanchet@49990
   536
blanchet@49990
   537
(* Names *)
blanchet@49990
   538
blanchet@49990
   539
val mapN = "map";
blanchet@49990
   540
val setN = "set";
blanchet@49990
   541
fun mk_setN i = setN ^ nonzero_string_of_int i;
blanchet@49990
   542
val bdN = "bd";
blanchet@49990
   543
val witN = "wit";
blanchet@49990
   544
fun mk_witN i = witN ^ nonzero_string_of_int i;
blanchet@50522
   545
val relN = "rel";
blanchet@49990
   546
blanchet@49990
   547
val bd_card_orderN = "bd_card_order";
blanchet@49990
   548
val bd_cinfiniteN = "bd_cinfinite";
blanchet@49990
   549
val bd_Card_orderN = "bd_Card_order";
blanchet@49990
   550
val bd_CinfiniteN = "bd_Cinfinite";
blanchet@49990
   551
val bd_CnotzeroN = "bd_Cnotzero";
blanchet@52903
   552
val collect_set_mapN = "collect_set_map";
blanchet@49990
   553
val in_bdN = "in_bd";
blanchet@49990
   554
val in_monoN = "in_mono";
traytel@53030
   555
val in_relN = "in_rel";
blanchet@54407
   556
val map_id0N = "map_id0";
blanchet@54422
   557
val map_idN = "map_id";
blanchet@54424
   558
val map_comp0N = "map_comp0";
blanchet@54425
   559
val map_compN = "map_comp";
blanchet@52898
   560
val map_cong0N = "map_cong0";
blanchet@52899
   561
val map_congN = "map_cong";
traytel@53856
   562
val map_transferN = "map_transfer";
blanchet@49990
   563
val map_wpullN = "map_wpull";
blanchet@50606
   564
val rel_eqN = "rel_eq";
blanchet@50552
   565
val rel_flipN = "rel_flip";
blanchet@54426
   566
val set_map0N = "set_map0";
blanchet@54427
   567
val set_mapN = "set_map";
blanchet@50552
   568
val set_bdN = "set_bd";
traytel@53030
   569
val rel_GrpN = "rel_Grp";
traytel@53030
   570
val rel_conversepN = "rel_conversep";
traytel@53030
   571
val rel_monoN = "rel_mono"
traytel@53053
   572
val rel_mono_strongN = "rel_mono_strong"
traytel@53030
   573
val rel_OON = "rel_compp";
traytel@53030
   574
val rel_OO_GrpN = "rel_compp_Grp";
blanchet@49990
   575
blanchet@49990
   576
datatype const_policy = Dont_Inline | Hardly_Inline | Smart_Inline | Do_Inline;
blanchet@49990
   577
blanchet@50553
   578
datatype fact_policy = Dont_Note | Note_Some | Note_All;
blanchet@49990
   579
blanchet@49990
   580
val bnf_note_all = Attrib.setup_config_bool @{binding bnf_note_all} (K false);
traytel@54280
   581
val bnf_timing = Attrib.setup_config_bool @{binding bnf_timing} (K false);
blanchet@49990
   582
blanchet@50553
   583
fun user_policy policy ctxt = if Config.get ctxt bnf_note_all then Note_All else policy;
blanchet@49990
   584
blanchet@49990
   585
val smart_max_inline_size = 25; (*FUDGE*)
blanchet@49990
   586
traytel@55182
   587
fun note_bnf_thms fact_policy qualify' bnf_b bnf =
traytel@53857
   588
  let
traytel@53857
   589
    val axioms = axioms_of_bnf bnf;
traytel@53857
   590
    val facts = facts_of_bnf bnf;
traytel@53857
   591
    val wits = wits_of_bnf bnf;
traytel@55182
   592
    val qualify =
traytel@55182
   593
      let val (_, qs, _) = Binding.dest bnf_b;
traytel@55182
   594
      in fold_rev (fn (s, mand) => Binding.qualify mand s) qs #> qualify' end;
traytel@53857
   595
  in
traytel@53857
   596
    (if fact_policy = Note_All then
traytel@53857
   597
      let
traytel@53857
   598
        val witNs = if length wits = 1 then [witN] else map mk_witN (1 upto length wits);
traytel@53857
   599
        val notes =
traytel@53857
   600
          [(bd_card_orderN, [#bd_card_order axioms]),
traytel@53857
   601
            (bd_cinfiniteN, [#bd_cinfinite axioms]),
traytel@53857
   602
            (bd_Card_orderN, [#bd_Card_order facts]),
traytel@53857
   603
            (bd_CinfiniteN, [#bd_Cinfinite facts]),
traytel@53857
   604
            (bd_CnotzeroN, [#bd_Cnotzero facts]),
traytel@53857
   605
            (collect_set_mapN, [Lazy.force (#collect_set_map facts)]),
traytel@53857
   606
            (in_bdN, [Lazy.force (#in_bd facts)]),
traytel@53857
   607
            (in_monoN, [Lazy.force (#in_mono facts)]),
traytel@53857
   608
            (in_relN, [Lazy.force (#in_rel facts)]),
blanchet@54424
   609
            (map_comp0N, [#map_comp0 axioms]),
blanchet@54407
   610
            (map_id0N, [#map_id0 axioms]),
traytel@53857
   611
            (map_transferN, [Lazy.force (#map_transfer facts)]),
traytel@53857
   612
            (map_wpullN, [#map_wpull axioms]),
traytel@53857
   613
            (rel_mono_strongN, [Lazy.force (#rel_mono_strong facts)]),
blanchet@54426
   614
            (set_map0N, #set_map0 axioms),
traytel@53857
   615
            (set_bdN, #set_bd axioms)] @
traytel@53857
   616
            (witNs ~~ wit_thmss_of_bnf bnf)
traytel@53857
   617
            |> map (fn (thmN, thms) =>
blanchet@54402
   618
              ((qualify (Binding.qualify true (Binding.name_of bnf_b) (Binding.name thmN)), []),
traytel@53857
   619
              [(thms, [])]));
traytel@53857
   620
        in
traytel@53857
   621
          Local_Theory.notes notes #> snd
traytel@53857
   622
        end
traytel@53857
   623
      else
traytel@53857
   624
        I)
traytel@53857
   625
    #> (if fact_policy <> Dont_Note then
traytel@53857
   626
        let
traytel@53857
   627
          val notes =
blanchet@54425
   628
            [(map_compN, [Lazy.force (#map_comp facts)], []),
traytel@53857
   629
            (map_cong0N, [#map_cong0 axioms], []),
traytel@53857
   630
            (map_congN, [Lazy.force (#map_cong facts)], fundef_cong_attrs),
blanchet@54422
   631
            (map_idN, [Lazy.force (#map_id facts)], []),
traytel@53857
   632
            (rel_eqN, [Lazy.force (#rel_eq facts)], []),
traytel@53857
   633
            (rel_flipN, [Lazy.force (#rel_flip facts)], []),
blanchet@54427
   634
            (set_mapN, map Lazy.force (#set_map facts), []),
traytel@53857
   635
            (rel_OO_GrpN, no_refl [#rel_OO_Grp axioms], []),
traytel@53857
   636
            (rel_GrpN, [Lazy.force (#rel_Grp facts)], []),
traytel@53857
   637
            (rel_conversepN, [Lazy.force (#rel_conversep facts)], []),
traytel@53857
   638
            (rel_monoN, [Lazy.force (#rel_mono facts)], []),
traytel@53857
   639
            (rel_OON, [Lazy.force (#rel_OO facts)], [])]
traytel@53857
   640
            |> filter_out (null o #2)
traytel@53857
   641
            |> map (fn (thmN, thms, attrs) =>
blanchet@54402
   642
              ((qualify (Binding.qualify true (Binding.name_of bnf_b) (Binding.name thmN)),
traytel@53857
   643
                attrs), [(thms, [])]));
traytel@53857
   644
        in
traytel@53857
   645
          Local_Theory.notes notes #> snd
traytel@53857
   646
        end
traytel@53857
   647
      else
traytel@53857
   648
        I)
traytel@53857
   649
  end;
traytel@53857
   650
blanchet@49990
   651
blanchet@49990
   652
(* Define new BNFs *)
blanchet@49990
   653
blanchet@52904
   654
fun prepare_def const_policy mk_fact_policy qualify prep_term Ds_opt map_b rel_b set_bs
blanchet@54402
   655
  (((((raw_bnf_b, raw_map), raw_sets), raw_bd_Abs), raw_wits), raw_rel_opt) no_defs_lthy =
blanchet@49990
   656
  let
blanchet@49990
   657
    val fact_policy = mk_fact_policy no_defs_lthy;
blanchet@54402
   658
    val bnf_b = qualify raw_bnf_b;
blanchet@49990
   659
    val live = length raw_sets;
blanchet@49990
   660
blanchet@49990
   661
    val map_rhs = prep_term no_defs_lthy raw_map;
blanchet@49990
   662
    val set_rhss = map (prep_term no_defs_lthy) raw_sets;
blanchet@49990
   663
    val (bd_rhsT, bd_rhs) = (case prep_term no_defs_lthy raw_bd_Abs of
blanchet@49990
   664
      Abs (_, T, t) => (T, t)
blanchet@49990
   665
    | _ => error "Bad bound constant");
blanchet@49990
   666
traytel@50449
   667
    fun err T =
traytel@50449
   668
      error ("Trying to register the type " ^ quote (Syntax.string_of_typ no_defs_lthy T) ^
traytel@50449
   669
        " as unnamed BNF");
traytel@50449
   670
blanchet@54402
   671
    val (bnf_b, key) =
blanchet@54402
   672
      if Binding.eq_name (bnf_b, Binding.empty) then
traytel@50449
   673
        (case bd_rhsT of
blanchet@54402
   674
          Type (C, Ts) => if forall (can dest_TFree) Ts
traytel@50449
   675
            then (Binding.qualified_name C, C) else err bd_rhsT
traytel@50449
   676
        | T => err T)
blanchet@54402
   677
      else (bnf_b, Local_Theory.full_name no_defs_lthy bnf_b);
blanchet@54402
   678
blanchet@55608
   679
    val def_qualify = Binding.conceal o Binding.qualify false (Binding.name_of bnf_b);
blanchet@54402
   680
blanchet@54402
   681
    fun mk_suffix_binding suf = Binding.suffix_name ("_" ^ suf) bnf_b;
traytel@50449
   682
blanchet@50478
   683
    fun maybe_define user_specified (b, rhs) lthy =
blanchet@49990
   684
      let
blanchet@49990
   685
        val inline =
blanchet@50553
   686
          (user_specified orelse fact_policy = Dont_Note) andalso
blanchet@49990
   687
          (case const_policy of
blanchet@49990
   688
            Dont_Inline => false
blanchet@49990
   689
          | Hardly_Inline => Term.is_Free rhs orelse Term.is_Const rhs
blanchet@49990
   690
          | Smart_Inline => Term.size_of_term rhs <= smart_max_inline_size
blanchet@49990
   691
          | Do_Inline => true)
blanchet@49990
   692
      in
blanchet@49990
   693
        if inline then
blanchet@50475
   694
          ((rhs, Drule.reflexive_thm), lthy)
blanchet@49990
   695
        else
blanchet@49990
   696
          let val b = b () in
blanchet@49990
   697
            apfst (apsnd snd) (Local_Theory.define ((b, NoSyn), ((Thm.def_binding b, []), rhs))
blanchet@49990
   698
              lthy)
blanchet@49990
   699
          end
blanchet@49990
   700
      end;
blanchet@49990
   701
blanchet@50474
   702
    fun maybe_restore lthy_old lthy =
blanchet@50474
   703
      lthy |> not (pointer_eq (lthy_old, lthy)) ? Local_Theory.restore;
blanchet@50474
   704
blanchet@52895
   705
    val map_bind_def =
blanchet@54402
   706
      (fn () => def_qualify (if Binding.is_empty map_b then mk_suffix_binding mapN else map_b),
blanchet@54402
   707
         map_rhs);
blanchet@50474
   708
    val set_binds_defs =
blanchet@50474
   709
      let
blanchet@52894
   710
        fun set_name i get_b =
blanchet@52894
   711
          (case try (nth set_bs) (i - 1) of
blanchet@52894
   712
            SOME b => if Binding.is_empty b then get_b else K b
blanchet@54402
   713
          | NONE => get_b) #> def_qualify;
blanchet@54402
   714
        val bs = if live = 1 then [set_name 1 (fn () => mk_suffix_binding setN)]
blanchet@54402
   715
          else map (fn i => set_name i (fn () => mk_suffix_binding (mk_setN i))) (1 upto live);
blanchet@52894
   716
      in bs ~~ set_rhss end;
blanchet@54402
   717
    val bd_bind_def = (fn () => def_qualify (mk_suffix_binding bdN), bd_rhs);
blanchet@50474
   718
traytel@55641
   719
    val ((((bnf_map_term, raw_map_def),
blanchet@49990
   720
      (bnf_set_terms, raw_set_defs)),
traytel@55641
   721
      (bnf_bd_term, raw_bd_def)), (lthy, lthy_old)) =
blanchet@49990
   722
        no_defs_lthy
blanchet@50478
   723
        |> maybe_define true map_bind_def
blanchet@50478
   724
        ||>> apfst split_list o fold_map (maybe_define true) set_binds_defs
blanchet@50478
   725
        ||>> maybe_define true bd_bind_def
blanchet@49990
   726
        ||> `(maybe_restore no_defs_lthy);
blanchet@49990
   727
blanchet@50474
   728
    val phi = Proof_Context.export_morphism lthy_old lthy;
blanchet@49990
   729
blanchet@49990
   730
    val bnf_map_def = Morphism.thm phi raw_map_def;
blanchet@49990
   731
    val bnf_set_defs = map (Morphism.thm phi) raw_set_defs;
blanchet@49990
   732
    val bnf_bd_def = Morphism.thm phi raw_bd_def;
blanchet@49990
   733
blanchet@49990
   734
    val bnf_map = Morphism.term phi bnf_map_term;
blanchet@49990
   735
blanchet@49990
   736
    (*TODO: handle errors*)
blanchet@49990
   737
    (*simple shape analysis of a map function*)
traytel@50410
   738
    val ((alphas, betas), (CA, _)) =
traytel@50410
   739
      fastype_of bnf_map
traytel@50410
   740
      |> strip_typeN live
traytel@50410
   741
      |>> map_split dest_funT
traytel@50410
   742
      ||> dest_funT
traytel@50410
   743
      handle TYPE _ => error "Bad map function";
blanchet@49990
   744
blanchet@49990
   745
    val CA_params = map TVar (Term.add_tvarsT CA []);
blanchet@49990
   746
blanchet@49990
   747
    val bnf_sets = map2 (normalize_set CA_params) alphas (map (Morphism.term phi) bnf_set_terms);
blanchet@49990
   748
    val bdT = Morphism.typ phi bd_rhsT;
blanchet@49990
   749
    val bnf_bd =
blanchet@49990
   750
      Term.subst_TVars (Term.add_tvar_namesT bdT [] ~~ CA_params) (Morphism.term phi bnf_bd_term);
blanchet@49990
   751
blanchet@49990
   752
    (*TODO: assert Ds = (TVars of bnf_map) \ (alphas @ betas) as sets*)
blanchet@49990
   753
    val deads = (case Ds_opt of
blanchet@49990
   754
      NONE => subtract (op =) (alphas @ betas) (map TVar (Term.add_tvars bnf_map []))
blanchet@49990
   755
    | SOME Ds => map (Morphism.typ phi) Ds);
blanchet@49990
   756
    val dead = length deads;
blanchet@49990
   757
blanchet@49990
   758
    (*TODO: further checks of type of bnf_map*)
blanchet@49990
   759
    (*TODO: check types of bnf_sets*)
blanchet@49990
   760
    (*TODO: check type of bnf_bd*)
blanchet@50522
   761
    (*TODO: check type of bnf_rel*)
blanchet@49990
   762
blanchet@49990
   763
    val ((((((((((As', Bs'), Cs), Ds), B1Ts), B2Ts), domTs), ranTs), ranTs'), ranTs''),
blanchet@50474
   764
      (Ts, T)) = lthy
blanchet@49990
   765
      |> mk_TFrees live
blanchet@49990
   766
      ||>> mk_TFrees live
blanchet@49990
   767
      ||>> mk_TFrees live
blanchet@49990
   768
      ||>> mk_TFrees dead
blanchet@49990
   769
      ||>> mk_TFrees live
blanchet@49990
   770
      ||>> mk_TFrees live
blanchet@49990
   771
      ||>> mk_TFrees live
blanchet@49990
   772
      ||>> mk_TFrees live
blanchet@49990
   773
      ||>> mk_TFrees live
blanchet@49990
   774
      ||>> mk_TFrees live
blanchet@49990
   775
      ||> fst o mk_TFrees 1
blanchet@49990
   776
      ||> the_single
blanchet@49990
   777
      ||> `(replicate live);
blanchet@49990
   778
blanchet@49990
   779
    fun mk_bnf_map As' Bs' =
blanchet@49990
   780
      Term.subst_atomic_types ((deads ~~ Ds) @ (alphas ~~ As') @ (betas ~~ Bs')) bnf_map;
blanchet@50468
   781
    fun mk_bnf_t As' = Term.subst_atomic_types ((deads ~~ Ds) @ (alphas ~~ As'));
blanchet@50468
   782
    fun mk_bnf_T As' = Term.typ_subst_atomic ((deads ~~ Ds) @ (alphas ~~ As'));
blanchet@50468
   783
traytel@53030
   784
    val RTs = map HOLogic.mk_prodT (As' ~~ Bs');
traytel@53030
   785
    val pred2RTs = map2 mk_pred2T As' Bs';
traytel@53030
   786
    val pred2RTsAsCs = map2 mk_pred2T As' Cs;
traytel@53030
   787
    val pred2RTsBsCs = map2 mk_pred2T Bs' Cs;
traytel@53030
   788
    val pred2RT's = map2 mk_pred2T Bs' As';
traytel@53030
   789
    val self_pred2RTs = map2 mk_pred2T As' As';
traytel@53856
   790
    val transfer_domRTs = map2 mk_pred2T As' B1Ts;
traytel@53856
   791
    val transfer_ranRTs = map2 mk_pred2T Bs' B2Ts;
blanchet@49990
   792
blanchet@50468
   793
    val CA' = mk_bnf_T As' CA;
blanchet@50468
   794
    val CB' = mk_bnf_T Bs' CA;
blanchet@50468
   795
    val CC' = mk_bnf_T Cs CA;
blanchet@50468
   796
    val CRs' = mk_bnf_T RTs CA;
traytel@53856
   797
    val CB1 = mk_bnf_T B1Ts CA;
traytel@53856
   798
    val CB2 = mk_bnf_T B2Ts CA;
blanchet@50468
   799
blanchet@49990
   800
    val bnf_map_AsAs = mk_bnf_map As' As';
blanchet@49990
   801
    val bnf_map_AsBs = mk_bnf_map As' Bs';
blanchet@49990
   802
    val bnf_map_AsCs = mk_bnf_map As' Cs;
blanchet@49990
   803
    val bnf_map_BsCs = mk_bnf_map Bs' Cs;
blanchet@49990
   804
    val bnf_sets_As = map (mk_bnf_t As') bnf_sets;
blanchet@49990
   805
    val bnf_sets_Bs = map (mk_bnf_t Bs') bnf_sets;
blanchet@49990
   806
    val bnf_bd_As = mk_bnf_t As' bnf_bd;
blanchet@49990
   807
blanchet@50610
   808
    val pre_names_lthy = lthy;
traytel@54698
   809
    val ((((((((((((((((((((((((fs, gs), hs), x), y), zs), ys), As),
blanchet@50474
   810
      As_copy), Xs), B1s), B2s), f1s), f2s), e1s), e2s), p1s), p2s), bs), (Rs, Rs')), Rs_copy), Ss),
traytel@53856
   811
      transfer_domRs), transfer_ranRs), names_lthy) = pre_names_lthy
traytel@54060
   812
      |> mk_Frees "f" (map2 (curry op -->) As' Bs')
traytel@54060
   813
      ||>> mk_Frees "g" (map2 (curry op -->) Bs' Cs)
traytel@54060
   814
      ||>> mk_Frees "h" (map2 (curry op -->) As' Ts)
traytel@53031
   815
      ||>> yield_singleton (mk_Frees "x") CA'
traytel@53031
   816
      ||>> yield_singleton (mk_Frees "y") CB'
blanchet@49990
   817
      ||>> mk_Frees "z" As'
traytel@53053
   818
      ||>> mk_Frees "y" Bs'
blanchet@49990
   819
      ||>> mk_Frees "A" (map HOLogic.mk_setT As')
blanchet@49990
   820
      ||>> mk_Frees "A" (map HOLogic.mk_setT As')
blanchet@49990
   821
      ||>> mk_Frees "A" (map HOLogic.mk_setT domTs)
blanchet@49990
   822
      ||>> mk_Frees "B1" (map HOLogic.mk_setT B1Ts)
blanchet@49990
   823
      ||>> mk_Frees "B2" (map HOLogic.mk_setT B2Ts)
traytel@54060
   824
      ||>> mk_Frees "f1" (map2 (curry op -->) B1Ts ranTs)
traytel@54060
   825
      ||>> mk_Frees "f2" (map2 (curry op -->) B2Ts ranTs)
traytel@54060
   826
      ||>> mk_Frees "e1" (map2 (curry op -->) B1Ts ranTs')
traytel@54060
   827
      ||>> mk_Frees "e2" (map2 (curry op -->) B2Ts ranTs'')
traytel@54060
   828
      ||>> mk_Frees "p1" (map2 (curry op -->) domTs B1Ts)
traytel@54060
   829
      ||>> mk_Frees "p2" (map2 (curry op -->) domTs B2Ts)
blanchet@49990
   830
      ||>> mk_Frees "b" As'
traytel@53030
   831
      ||>> mk_Frees' "R" pred2RTs
traytel@53030
   832
      ||>> mk_Frees "R" pred2RTs
traytel@53856
   833
      ||>> mk_Frees "S" pred2RTsBsCs
traytel@53856
   834
      ||>> mk_Frees "R" transfer_domRTs
traytel@53856
   835
      ||>> mk_Frees "S" transfer_ranRTs;
blanchet@49990
   836
blanchet@52899
   837
    val fs_copy = map2 (retype_free o fastype_of) fs gs;
blanchet@52899
   838
    val x_copy = retype_free CA' y;
blanchet@52899
   839
traytel@53030
   840
    val setRs =
traytel@53030
   841
      map3 (fn R => fn T => fn U =>
traytel@53030
   842
          HOLogic.Collect_const (HOLogic.mk_prodT (T, U)) $ HOLogic.mk_split R) Rs As' Bs';
traytel@53030
   843
traytel@53030
   844
    (*Grp (in (Collect (split R1) .. Collect (split Rn))) (map fst .. fst)^--1 OO
traytel@53030
   845
      Grp (in (Collect (split R1) .. Collect (split Rn))) (map snd .. snd)*)
traytel@53030
   846
    val OO_Grp =
blanchet@50474
   847
      let
blanchet@50474
   848
        val map1 = Term.list_comb (mk_bnf_map RTs As', map fst_const RTs);
blanchet@50474
   849
        val map2 = Term.list_comb (mk_bnf_map RTs Bs', map snd_const RTs);
traytel@53030
   850
        val bnf_in = mk_in setRs (map (mk_bnf_t RTs) bnf_sets) CRs';
blanchet@50474
   851
      in
traytel@53030
   852
        mk_rel_compp (mk_conversep (mk_Grp bnf_in map1), mk_Grp bnf_in map2)
blanchet@50474
   853
      end;
blanchet@50474
   854
blanchet@50522
   855
    val rel_rhs = (case raw_rel_opt of
traytel@53030
   856
        NONE => fold_rev Term.absfree Rs' OO_Grp
blanchet@50522
   857
      | SOME raw_rel => prep_term no_defs_lthy raw_rel);
blanchet@50478
   858
blanchet@52904
   859
    val rel_bind_def =
blanchet@54402
   860
      (fn () => def_qualify (if Binding.is_empty rel_b then mk_suffix_binding relN else rel_b),
blanchet@54402
   861
         rel_rhs);
blanchet@50478
   862
traytel@55641
   863
    val wit_rhss =
traytel@55641
   864
      if null raw_wits then
traytel@55641
   865
        [fold_rev Term.absdummy As' (Term.list_comb (bnf_map_AsAs,
traytel@55641
   866
          map2 (fn T => fn i => Term.absdummy T (Bound i)) As' (live downto 1)) $
traytel@55641
   867
          Const (@{const_name undefined}, CA'))]
traytel@55641
   868
      else map (prep_term no_defs_lthy) raw_wits;
traytel@55641
   869
    val nwits = length wit_rhss;
traytel@55641
   870
    val wit_binds_defs =
traytel@55641
   871
      let
traytel@55641
   872
        val bs = if nwits = 1 then [fn () => def_qualify (mk_suffix_binding witN)]
traytel@55641
   873
          else map (fn i => fn () => def_qualify (mk_suffix_binding (mk_witN i))) (1 upto nwits);
traytel@55641
   874
      in bs ~~ wit_rhss end;
traytel@55641
   875
traytel@55641
   876
    val (((bnf_rel_term, raw_rel_def), (bnf_wit_terms, raw_wit_defs)), (lthy, lthy_old)) =
blanchet@50478
   877
      lthy
blanchet@50522
   878
      |> maybe_define (is_some raw_rel_opt) rel_bind_def
traytel@55641
   879
      ||>> apfst split_list o fold_map (maybe_define (not (null raw_wits))) wit_binds_defs
blanchet@50478
   880
      ||> `(maybe_restore lthy);
blanchet@50478
   881
blanchet@50478
   882
    val phi = Proof_Context.export_morphism lthy_old lthy;
blanchet@50522
   883
    val bnf_rel_def = Morphism.thm phi raw_rel_def;
blanchet@50522
   884
    val bnf_rel = Morphism.term phi bnf_rel_term;
blanchet@50478
   885
traytel@53030
   886
    fun mk_bnf_rel RTs CA' CB' = normalize_rel lthy RTs CA' CB' bnf_rel;
blanchet@50478
   887
traytel@53030
   888
    val rel = mk_bnf_rel pred2RTs CA' CB';
traytel@53856
   889
    val relAsAs = mk_bnf_rel self_pred2RTs CA' CA';
blanchet@50474
   890
traytel@55641
   891
    val bnf_wit_defs = map (Morphism.thm phi) raw_wit_defs;
traytel@55641
   892
    val bnf_wits = map (normalize_wit CA_params CA alphas o Morphism.term phi) bnf_wit_terms;
traytel@55641
   893
    val bnf_wit_As = map (apsnd (mk_bnf_t As')) bnf_wits;
traytel@55641
   894
blanchet@54407
   895
    val map_id0_goal =
blanchet@52899
   896
      let val bnf_map_app_id = Term.list_comb (bnf_map_AsAs, map HOLogic.id_const As') in
blanchet@52899
   897
        mk_Trueprop_eq (bnf_map_app_id, HOLogic.id_const CA')
blanchet@49990
   898
      end;
blanchet@49990
   899
blanchet@54424
   900
    val map_comp0_goal =
blanchet@49990
   901
      let
blanchet@50033
   902
        val bnf_map_app_comp = Term.list_comb (bnf_map_AsCs, map2 (curry HOLogic.mk_comp) gs fs);
blanchet@49990
   903
        val comp_bnf_map_app = HOLogic.mk_comp
blanchet@52899
   904
          (Term.list_comb (bnf_map_BsCs, gs), Term.list_comb (bnf_map_AsBs, fs));
blanchet@49990
   905
      in
blanchet@50138
   906
        fold_rev Logic.all (fs @ gs) (mk_Trueprop_eq (bnf_map_app_comp, comp_bnf_map_app))
blanchet@49990
   907
      end;
blanchet@49990
   908
blanchet@52899
   909
    fun mk_map_cong_prem x z set f f_copy =
blanchet@52899
   910
      Logic.all z (Logic.mk_implies
blanchet@52899
   911
        (HOLogic.mk_Trueprop (HOLogic.mk_mem (z, set $ x)),
blanchet@52899
   912
        mk_Trueprop_eq (f $ z, f_copy $ z)));
blanchet@52899
   913
blanchet@52898
   914
    val map_cong0_goal =
blanchet@49990
   915
      let
blanchet@52899
   916
        val prems = map4 (mk_map_cong_prem x) zs bnf_sets_As fs fs_copy;
blanchet@52899
   917
        val eq = mk_Trueprop_eq (Term.list_comb (bnf_map_AsBs, fs) $ x,
blanchet@49990
   918
          Term.list_comb (bnf_map_AsBs, fs_copy) $ x);
blanchet@49990
   919
      in
blanchet@52899
   920
        fold_rev Logic.all (x :: fs @ fs_copy) (Logic.list_implies (prems, eq))
blanchet@49990
   921
      end;
blanchet@49990
   922
blanchet@54426
   923
    val set_map0s_goal =
blanchet@49990
   924
      let
blanchet@49990
   925
        fun mk_goal setA setB f =
blanchet@49990
   926
          let
blanchet@49990
   927
            val set_comp_map =
blanchet@49990
   928
              HOLogic.mk_comp (setB, Term.list_comb (bnf_map_AsBs, fs));
blanchet@49990
   929
            val image_comp_set = HOLogic.mk_comp (mk_image f, setA);
blanchet@49990
   930
          in
blanchet@50138
   931
            fold_rev Logic.all fs (mk_Trueprop_eq (set_comp_map, image_comp_set))
blanchet@49990
   932
          end;
blanchet@49990
   933
      in
blanchet@49990
   934
        map3 mk_goal bnf_sets_As bnf_sets_Bs fs
blanchet@49990
   935
      end;
blanchet@49990
   936
blanchet@50473
   937
    val card_order_bd_goal = HOLogic.mk_Trueprop (mk_card_order bnf_bd_As);
blanchet@49990
   938
blanchet@50473
   939
    val cinfinite_bd_goal = HOLogic.mk_Trueprop (mk_cinfinite bnf_bd_As);
blanchet@49990
   940
blanchet@50473
   941
    val set_bds_goal =
blanchet@49990
   942
      let
blanchet@49990
   943
        fun mk_goal set =
blanchet@49990
   944
          Logic.all x (HOLogic.mk_Trueprop (mk_ordLeq (mk_card_of (set $ x)) bnf_bd_As));
blanchet@49990
   945
      in
blanchet@49990
   946
        map mk_goal bnf_sets_As
blanchet@49990
   947
      end;
blanchet@49990
   948
blanchet@50473
   949
    val map_wpull_goal =
blanchet@49990
   950
      let
blanchet@49990
   951
        val prems = map HOLogic.mk_Trueprop
blanchet@49990
   952
          (map8 mk_wpull Xs B1s B2s f1s f2s (replicate live NONE) p1s p2s);
blanchet@49990
   953
        val CX = mk_bnf_T domTs CA;
blanchet@49990
   954
        val bnf_sets_CX = map2 (normalize_set (map (mk_bnf_T domTs) CA_params)) domTs bnf_sets;
blanchet@49990
   955
        val bnf_sets_CB1 = map2 (normalize_set (map (mk_bnf_T B1Ts) CA_params)) B1Ts bnf_sets;
blanchet@49990
   956
        val bnf_sets_CB2 = map2 (normalize_set (map (mk_bnf_T B2Ts) CA_params)) B2Ts bnf_sets;
blanchet@49990
   957
        val bnf_map_app_f1 = Term.list_comb (mk_bnf_map B1Ts ranTs, f1s);
blanchet@49990
   958
        val bnf_map_app_f2 = Term.list_comb (mk_bnf_map B2Ts ranTs, f2s);
blanchet@49990
   959
        val bnf_map_app_p1 = Term.list_comb (mk_bnf_map domTs B1Ts, p1s);
blanchet@49990
   960
        val bnf_map_app_p2 = Term.list_comb (mk_bnf_map domTs B2Ts, p2s);
blanchet@49990
   961
blanchet@49990
   962
        val map_wpull = mk_wpull (mk_in Xs bnf_sets_CX CX)
blanchet@49990
   963
          (mk_in B1s bnf_sets_CB1 CB1) (mk_in B2s bnf_sets_CB2 CB2)
blanchet@49990
   964
          bnf_map_app_f1 bnf_map_app_f2 NONE bnf_map_app_p1 bnf_map_app_p2;
blanchet@49990
   965
      in
blanchet@49990
   966
        fold_rev Logic.all (Xs @ B1s @ B2s @ f1s @ f2s @ p1s @ p2s)
blanchet@49990
   967
          (Logic.list_implies (prems, HOLogic.mk_Trueprop map_wpull))
blanchet@49990
   968
      end;
blanchet@49990
   969
traytel@53030
   970
    val rel_OO_Grp_goal = fold_rev Logic.all Rs (mk_Trueprop_eq (Term.list_comb (rel, Rs), OO_Grp));
blanchet@50468
   971
blanchet@54426
   972
    val goals = zip_axioms map_id0_goal map_comp0_goal map_cong0_goal set_map0s_goal
blanchet@54407
   973
      card_order_bd_goal cinfinite_bd_goal set_bds_goal map_wpull_goal rel_OO_Grp_goal;
blanchet@49990
   974
blanchet@49990
   975
    fun mk_wit_goals (I, wit) =
blanchet@49990
   976
      let
blanchet@49990
   977
        val xs = map (nth bs) I;
blanchet@49990
   978
        fun wit_goal i =
blanchet@49990
   979
          let
blanchet@49990
   980
            val z = nth zs i;
blanchet@49990
   981
            val set_wit = nth bnf_sets_As i $ Term.list_comb (wit, xs);
blanchet@49990
   982
            val concl = HOLogic.mk_Trueprop
blanchet@49990
   983
              (if member (op =) I i then HOLogic.mk_eq (z, nth bs i)
blanchet@49990
   984
              else @{term False});
blanchet@49990
   985
          in
blanchet@49990
   986
            fold_rev Logic.all (z :: xs)
blanchet@49990
   987
              (Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_mem (z, set_wit)), concl))
blanchet@49990
   988
          end;
blanchet@49990
   989
      in
blanchet@49990
   990
        map wit_goal (0 upto live - 1)
blanchet@49990
   991
      end;
blanchet@49990
   992
traytel@55641
   993
    val trivial_wit_tac = mk_trivial_wit_tac bnf_wit_defs;
blanchet@49990
   994
traytel@55641
   995
    val wit_goalss =
traytel@55641
   996
      (if null raw_wits then SOME trivial_wit_tac else NONE, map mk_wit_goals bnf_wit_As);
traytel@55641
   997
traytel@55641
   998
    fun after_qed mk_wit_thms thms lthy =
blanchet@49990
   999
      let
traytel@55641
  1000
        val (axioms, nontriv_wit_thms) = apfst (mk_axioms live) (chop (length goals) thms);
blanchet@49990
  1001
traytel@50124
  1002
        val bd_Card_order = #bd_card_order axioms RS @{thm conjunct2[OF card_order_on_Card_order]};
blanchet@49990
  1003
        val bd_Cinfinite = @{thm conjI} OF [#bd_cinfinite axioms, bd_Card_order];
blanchet@49990
  1004
        val bd_Cnotzero = bd_Cinfinite RS @{thm Cinfinite_Cnotzero};
blanchet@49990
  1005
blanchet@52903
  1006
        fun mk_collect_set_map () =
blanchet@49990
  1007
          let
blanchet@49990
  1008
            val defT = mk_bnf_T Ts CA --> HOLogic.mk_setT T;
blanchet@49990
  1009
            val collect_map = HOLogic.mk_comp
blanchet@49990
  1010
              (mk_collect (map (mk_bnf_t Ts) bnf_sets) defT,
blanchet@49990
  1011
              Term.list_comb (mk_bnf_map As' Ts, hs));
blanchet@49990
  1012
            val image_collect = mk_collect
blanchet@49990
  1013
              (map2 (fn h => fn set => HOLogic.mk_comp (mk_image h, set)) hs bnf_sets_As)
blanchet@49990
  1014
              defT;
blanchet@49990
  1015
            (*collect {set1 ... setm} o map f1 ... fm = collect {f1` o set1 ... fm` o setm}*)
blanchet@50138
  1016
            val goal = fold_rev Logic.all hs (mk_Trueprop_eq (collect_map, image_collect));
blanchet@49990
  1017
          in
blanchet@54426
  1018
            Goal.prove_sorry lthy [] [] goal (K (mk_collect_set_map_tac (#set_map0 axioms)))
traytel@50124
  1019
            |> Thm.close_derivation
blanchet@49990
  1020
          end;
blanchet@49990
  1021
blanchet@52903
  1022
        val collect_set_map = Lazy.lazy mk_collect_set_map;
blanchet@49990
  1023
blanchet@49990
  1024
        fun mk_in_mono () =
blanchet@49990
  1025
          let
traytel@53030
  1026
            val prems_mono = map2 (HOLogic.mk_Trueprop oo mk_leq) As As_copy;
blanchet@50473
  1027
            val in_mono_goal =
blanchet@49990
  1028
              fold_rev Logic.all (As @ As_copy)
blanchet@49990
  1029
                (Logic.list_implies (prems_mono, HOLogic.mk_Trueprop
traytel@53030
  1030
                  (mk_leq (mk_in As bnf_sets_As CA') (mk_in As_copy bnf_sets_As CA'))));
blanchet@49990
  1031
          in
wenzelm@52688
  1032
            Goal.prove_sorry lthy [] [] in_mono_goal (K (mk_in_mono_tac live))
traytel@50124
  1033
            |> Thm.close_derivation
blanchet@49990
  1034
          end;
blanchet@49990
  1035
blanchet@50553
  1036
        val in_mono = Lazy.lazy mk_in_mono;
blanchet@49990
  1037
blanchet@49990
  1038
        fun mk_in_cong () =
blanchet@49990
  1039
          let
blanchet@52899
  1040
            val prems_cong = map2 (curry mk_Trueprop_eq) As As_copy;
blanchet@50473
  1041
            val in_cong_goal =
blanchet@49990
  1042
              fold_rev Logic.all (As @ As_copy)
blanchet@52899
  1043
                (Logic.list_implies (prems_cong,
blanchet@52899
  1044
                  mk_Trueprop_eq (mk_in As bnf_sets_As CA', mk_in As_copy bnf_sets_As CA')));
blanchet@49990
  1045
          in
wenzelm@52935
  1046
            Goal.prove_sorry lthy [] [] in_cong_goal
wenzelm@52935
  1047
              (K ((TRY o hyp_subst_tac lthy THEN' rtac refl) 1))
traytel@50124
  1048
            |> Thm.close_derivation
blanchet@49990
  1049
          end;
blanchet@49990
  1050
blanchet@50553
  1051
        val in_cong = Lazy.lazy mk_in_cong;
blanchet@49990
  1052
blanchet@54422
  1053
        val map_id = Lazy.lazy (fn () => mk_map_id (#map_id0 axioms));
blanchet@54425
  1054
        val map_comp = Lazy.lazy (fn () => mk_map_comp (#map_comp0 axioms));
blanchet@52899
  1055
blanchet@52899
  1056
        fun mk_map_cong () =
blanchet@52899
  1057
          let
blanchet@52899
  1058
            val prem0 = mk_Trueprop_eq (x, x_copy);
blanchet@52899
  1059
            val prems = map4 (mk_map_cong_prem x_copy) zs bnf_sets_As fs fs_copy;
blanchet@52899
  1060
            val eq = mk_Trueprop_eq (Term.list_comb (bnf_map_AsBs, fs) $ x,
blanchet@52899
  1061
              Term.list_comb (bnf_map_AsBs, fs_copy) $ x_copy);
blanchet@52899
  1062
            val goal = fold_rev Logic.all (x :: x_copy :: fs @ fs_copy)
blanchet@52899
  1063
              (Logic.list_implies (prem0 :: prems, eq));
blanchet@52899
  1064
          in
wenzelm@52935
  1065
            Goal.prove_sorry lthy [] [] goal (fn _ => mk_map_cong_tac lthy (#map_cong0 axioms))
blanchet@52899
  1066
            |> Thm.close_derivation
blanchet@52899
  1067
          end;
blanchet@52899
  1068
blanchet@52899
  1069
        val map_cong = Lazy.lazy mk_map_cong;
blanchet@49990
  1070
blanchet@54427
  1071
        val set_map = map (fn thm => Lazy.lazy (fn () => mk_set_map thm)) (#set_map0 axioms);
blanchet@49990
  1072
traytel@55641
  1073
        val wit_thms =
traytel@55641
  1074
          if null nontriv_wit_thms then mk_wit_thms (map Lazy.force set_map) else nontriv_wit_thms;
traytel@55641
  1075
traytel@53772
  1076
        fun mk_in_bd () =
traytel@53772
  1077
          let
traytel@53950
  1078
            val bdT = fst (dest_relT (fastype_of bnf_bd_As));
traytel@53950
  1079
            val bdTs = replicate live bdT;
traytel@53950
  1080
            val bd_bnfT = mk_bnf_T bdTs CA;
traytel@53950
  1081
            val surj_imp_ordLeq_inst = (if live = 0 then TrueI else
traytel@53950
  1082
              let
traytel@53950
  1083
                val ranTs = map (fn AT => mk_sumT (AT, HOLogic.unitT)) As';
traytel@53950
  1084
                val funTs = map (fn T => bdT --> T) ranTs;
traytel@53950
  1085
                val ran_bnfT = mk_bnf_T ranTs CA;
traytel@53950
  1086
                val (revTs, Ts) = `rev (bd_bnfT :: funTs);
traytel@53950
  1087
                val cTs = map (SOME o certifyT lthy) [ran_bnfT, Library.foldr1 HOLogic.mk_prodT Ts];
traytel@53950
  1088
                val tinst = fold (fn T => fn t => HOLogic.mk_split (Term.absdummy T t)) (tl revTs)
traytel@53950
  1089
                  (Term.absdummy (hd revTs) (Term.list_comb (mk_bnf_map bdTs ranTs,
traytel@53950
  1090
                    map Bound (live - 1 downto 0)) $ Bound live));
traytel@53950
  1091
                val cts = [NONE, SOME (certify lthy tinst)];
traytel@53950
  1092
              in
traytel@53950
  1093
                Drule.instantiate' cTs cts @{thm surj_imp_ordLeq}
traytel@53950
  1094
              end);
traytel@53772
  1095
            val bd = mk_cexp
traytel@53772
  1096
              (if live = 0 then ctwo
traytel@53772
  1097
                else mk_csum (Library.foldr1 (uncurry mk_csum) (map mk_card_of As)) ctwo)
traytel@53950
  1098
              (mk_csum bnf_bd_As (mk_card_of (HOLogic.mk_UNIV bd_bnfT)));
traytel@53772
  1099
            val in_bd_goal =
traytel@53772
  1100
              fold_rev Logic.all As
traytel@53772
  1101
                (HOLogic.mk_Trueprop (mk_ordLeq (mk_card_of (mk_in As bnf_sets_As CA')) bd));
traytel@53772
  1102
          in
traytel@53772
  1103
            Goal.prove_sorry lthy [] [] in_bd_goal
traytel@53950
  1104
              (mk_in_bd_tac live surj_imp_ordLeq_inst
blanchet@54425
  1105
                (Lazy.force map_comp) (Lazy.force map_id) (#map_cong0 axioms)
blanchet@54427
  1106
                (map Lazy.force set_map) (#set_bd axioms) (#bd_card_order axioms)
traytel@53772
  1107
                bd_Card_order bd_Cinfinite bd_Cnotzero)
traytel@53772
  1108
            |> Thm.close_derivation
traytel@53772
  1109
          end;
traytel@53772
  1110
traytel@53772
  1111
        val in_bd = Lazy.lazy mk_in_bd;
traytel@53772
  1112
blanchet@49990
  1113
        fun mk_map_wppull () =
blanchet@49990
  1114
          let
blanchet@49990
  1115
            val prems = if live = 0 then [] else
blanchet@49990
  1116
              [HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
blanchet@49990
  1117
                (map8 mk_wpull Xs B1s B2s f1s f2s (map SOME (e1s ~~ e2s)) p1s p2s))];
blanchet@49990
  1118
            val CX = mk_bnf_T domTs CA;
blanchet@49990
  1119
            val bnf_sets_CX =
blanchet@49990
  1120
              map2 (normalize_set (map (mk_bnf_T domTs) CA_params)) domTs bnf_sets;
blanchet@49990
  1121
            val bnf_sets_CB1 =
blanchet@49990
  1122
              map2 (normalize_set (map (mk_bnf_T B1Ts) CA_params)) B1Ts bnf_sets;
blanchet@49990
  1123
            val bnf_sets_CB2 =
blanchet@49990
  1124
              map2 (normalize_set (map (mk_bnf_T B2Ts) CA_params)) B2Ts bnf_sets;
blanchet@49990
  1125
            val bnf_map_app_f1 = Term.list_comb (mk_bnf_map B1Ts ranTs, f1s);
blanchet@49990
  1126
            val bnf_map_app_f2 = Term.list_comb (mk_bnf_map B2Ts ranTs, f2s);
blanchet@49990
  1127
            val bnf_map_app_e1 = Term.list_comb (mk_bnf_map B1Ts ranTs', e1s);
blanchet@49990
  1128
            val bnf_map_app_e2 = Term.list_comb (mk_bnf_map B2Ts ranTs'', e2s);
blanchet@49990
  1129
            val bnf_map_app_p1 = Term.list_comb (mk_bnf_map domTs B1Ts, p1s);
blanchet@49990
  1130
            val bnf_map_app_p2 = Term.list_comb (mk_bnf_map domTs B2Ts, p2s);
blanchet@49990
  1131
blanchet@49990
  1132
            val concl = mk_wpull (mk_in Xs bnf_sets_CX CX)
blanchet@49990
  1133
              (mk_in B1s bnf_sets_CB1 CB1) (mk_in B2s bnf_sets_CB2 CB2)
blanchet@49990
  1134
              bnf_map_app_f1 bnf_map_app_f2 (SOME (bnf_map_app_e1, bnf_map_app_e2))
blanchet@49990
  1135
              bnf_map_app_p1 bnf_map_app_p2;
blanchet@49990
  1136
blanchet@49990
  1137
            val goal =
blanchet@49990
  1138
              fold_rev Logic.all (Xs @ B1s @ B2s @ f1s @ f2s @ e1s @ e2s @ p1s @ p2s)
blanchet@49990
  1139
                (Logic.list_implies (prems, HOLogic.mk_Trueprop concl))
blanchet@49990
  1140
          in
wenzelm@52688
  1141
            Goal.prove_sorry lthy [] [] goal
blanchet@54407
  1142
              (fn _ => mk_map_wppull_tac (#map_id0 axioms) (#map_cong0 axioms)
blanchet@54427
  1143
                (#map_wpull axioms) (Lazy.force map_comp) (map Lazy.force set_map))
traytel@50124
  1144
            |> Thm.close_derivation
blanchet@49990
  1145
          end;
blanchet@49990
  1146
blanchet@52899
  1147
        val map_wppull = Lazy.lazy mk_map_wppull;
blanchet@52899
  1148
traytel@54698
  1149
        val rel_OO_Grp = #rel_OO_Grp axioms;
traytel@54698
  1150
        val rel_OO_Grps = no_refl [rel_OO_Grp];
blanchet@50468
  1151
traytel@53030
  1152
        fun mk_rel_Grp () =
blanchet@49990
  1153
          let
traytel@53030
  1154
            val lhs = Term.list_comb (rel, map2 mk_Grp As fs);
traytel@53030
  1155
            val rhs = mk_Grp (mk_in As bnf_sets_As CA') (Term.list_comb (bnf_map_AsBs, fs));
blanchet@50138
  1156
            val goal = fold_rev Logic.all (As @ fs) (mk_Trueprop_eq (lhs, rhs));
blanchet@49990
  1157
          in
wenzelm@52688
  1158
            Goal.prove_sorry lthy [] [] goal
blanchet@54422
  1159
              (mk_rel_Grp_tac rel_OO_Grps (#map_id0 axioms) (#map_cong0 axioms) (Lazy.force map_id)
blanchet@54427
  1160
                (Lazy.force map_comp) (map Lazy.force set_map))
traytel@50124
  1161
            |> Thm.close_derivation
blanchet@49990
  1162
          end;
blanchet@49990
  1163
traytel@53030
  1164
        val rel_Grp = Lazy.lazy mk_rel_Grp;
blanchet@49990
  1165
traytel@53030
  1166
        fun mk_rel_prems f = map2 (HOLogic.mk_Trueprop oo f) Rs Rs_copy
traytel@53030
  1167
        fun mk_rel_concl f = HOLogic.mk_Trueprop
traytel@53030
  1168
          (f (Term.list_comb (rel, Rs), Term.list_comb (rel, Rs_copy)));
blanchet@49990
  1169
traytel@53030
  1170
        fun mk_rel_mono () =
blanchet@49990
  1171
          let
traytel@53030
  1172
            val mono_prems = mk_rel_prems mk_leq;
traytel@53030
  1173
            val mono_concl = mk_rel_concl (uncurry mk_leq);
blanchet@49990
  1174
          in
wenzelm@52688
  1175
            Goal.prove_sorry lthy [] []
blanchet@49990
  1176
              (fold_rev Logic.all (Rs @ Rs_copy) (Logic.list_implies (mono_prems, mono_concl)))
traytel@53981
  1177
              (K (mk_rel_mono_tac rel_OO_Grps (Lazy.force in_mono)))
traytel@50124
  1178
            |> Thm.close_derivation
blanchet@49990
  1179
          end;
blanchet@49990
  1180
traytel@53030
  1181
        fun mk_rel_cong () =
blanchet@49990
  1182
          let
traytel@53030
  1183
            val cong_prems = mk_rel_prems (curry HOLogic.mk_eq);
traytel@53030
  1184
            val cong_concl = mk_rel_concl HOLogic.mk_eq;
blanchet@49990
  1185
          in
wenzelm@52688
  1186
            Goal.prove_sorry lthy [] []
blanchet@49990
  1187
              (fold_rev Logic.all (Rs @ Rs_copy) (Logic.list_implies (cong_prems, cong_concl)))
wenzelm@52935
  1188
              (fn _ => (TRY o hyp_subst_tac lthy THEN' rtac refl) 1)
traytel@50124
  1189
            |> Thm.close_derivation
blanchet@49990
  1190
          end;
blanchet@49990
  1191
traytel@53030
  1192
        val rel_mono = Lazy.lazy mk_rel_mono;
traytel@53030
  1193
        val rel_cong = Lazy.lazy mk_rel_cong;
blanchet@49990
  1194
traytel@53030
  1195
        fun mk_rel_eq () =
traytel@53856
  1196
          Goal.prove_sorry lthy [] []
traytel@53856
  1197
            (mk_Trueprop_eq (Term.list_comb (relAsAs, map HOLogic.eq_const As'),
traytel@53856
  1198
              HOLogic.eq_const CA'))
blanchet@54407
  1199
            (K (mk_rel_eq_tac live (Lazy.force rel_Grp) (Lazy.force rel_cong) (#map_id0 axioms)))
traytel@53856
  1200
          |> Thm.close_derivation;
blanchet@49990
  1201
traytel@53030
  1202
        val rel_eq = Lazy.lazy mk_rel_eq;
blanchet@49990
  1203
traytel@53030
  1204
        fun mk_rel_conversep () =
blanchet@49990
  1205
          let
traytel@53030
  1206
            val relBsAs = mk_bnf_rel pred2RT's CB' CA';
traytel@53030
  1207
            val lhs = Term.list_comb (relBsAs, map mk_conversep Rs);
traytel@53030
  1208
            val rhs = mk_conversep (Term.list_comb (rel, Rs));
traytel@53030
  1209
            val le_goal = fold_rev Logic.all Rs (HOLogic.mk_Trueprop (mk_leq lhs rhs));
wenzelm@52688
  1210
            val le_thm = Goal.prove_sorry lthy [] [] le_goal
traytel@53030
  1211
              (mk_rel_conversep_le_tac rel_OO_Grps (Lazy.force rel_eq) (#map_cong0 axioms)
blanchet@54427
  1212
                (Lazy.force map_comp) (map Lazy.force set_map))
traytel@50124
  1213
              |> Thm.close_derivation
blanchet@50138
  1214
            val goal = fold_rev Logic.all Rs (mk_Trueprop_eq (lhs, rhs));
blanchet@49990
  1215
          in
traytel@53030
  1216
            Goal.prove_sorry lthy [] [] goal
traytel@53030
  1217
              (K (mk_rel_conversep_tac le_thm (Lazy.force rel_mono)))
traytel@50124
  1218
            |> Thm.close_derivation
blanchet@49990
  1219
          end;
blanchet@49990
  1220
traytel@53030
  1221
        val rel_conversep = Lazy.lazy mk_rel_conversep;
blanchet@49990
  1222
traytel@53030
  1223
        fun mk_rel_OO () =
blanchet@49990
  1224
          let
traytel@53030
  1225
            val relAsCs = mk_bnf_rel pred2RTsAsCs CA' CC';
traytel@53030
  1226
            val relBsCs = mk_bnf_rel pred2RTsBsCs CB' CC';
traytel@53030
  1227
            val lhs = Term.list_comb (relAsCs, map2 (curry mk_rel_compp) Rs Ss);
traytel@53030
  1228
            val rhs = mk_rel_compp (Term.list_comb (rel, Rs), Term.list_comb (relBsCs, Ss));
blanchet@50138
  1229
            val goal = fold_rev Logic.all (Rs @ Ss) (mk_Trueprop_eq (lhs, rhs));
blanchet@49990
  1230
          in
wenzelm@52688
  1231
            Goal.prove_sorry lthy [] [] goal
traytel@53030
  1232
              (mk_rel_OO_tac rel_OO_Grps (Lazy.force rel_eq) (#map_cong0 axioms)
blanchet@54427
  1233
                (Lazy.force map_wppull) (Lazy.force map_comp) (map Lazy.force set_map))
traytel@50124
  1234
            |> Thm.close_derivation
blanchet@49990
  1235
          end;
blanchet@49990
  1236
traytel@53030
  1237
        val rel_OO = Lazy.lazy mk_rel_OO;
blanchet@49990
  1238
traytel@54698
  1239
        fun mk_in_rel () = trans OF [rel_OO_Grp, @{thm OO_Grp_alt}] RS @{thm predicate2_eqD};
blanchet@49990
  1240
traytel@53030
  1241
        val in_rel = Lazy.lazy mk_in_rel;
blanchet@49990
  1242
blanchet@50552
  1243
        fun mk_rel_flip () =
blanchet@50552
  1244
          let
traytel@53030
  1245
            val rel_conversep_thm = Lazy.force rel_conversep;
traytel@53030
  1246
            val cts = map (SOME o certify lthy) Rs;
traytel@53030
  1247
            val rel_conversep_thm' = cterm_instantiate_pos cts rel_conversep_thm;
blanchet@50552
  1248
          in
traytel@53054
  1249
            unfold_thms lthy @{thms conversep_iff} (rel_conversep_thm' RS @{thm predicate2_eqD})
blanchet@50610
  1250
            |> singleton (Proof_Context.export names_lthy pre_names_lthy)
blanchet@50552
  1251
          end;
blanchet@50552
  1252
blanchet@50553
  1253
        val rel_flip = Lazy.lazy mk_rel_flip;
blanchet@50552
  1254
traytel@53053
  1255
        fun mk_rel_mono_strong () =
traytel@53053
  1256
          let
traytel@53053
  1257
            fun mk_prem setA setB R S a b =
traytel@53053
  1258
              HOLogic.mk_Trueprop
traytel@53053
  1259
                (mk_Ball (setA $ x) (Term.absfree (dest_Free a)
traytel@53053
  1260
                  (mk_Ball (setB $ y) (Term.absfree (dest_Free b)
traytel@53053
  1261
                    (HOLogic.mk_imp (R $ a $ b, S $ a $ b))))));
traytel@53053
  1262
            val prems = HOLogic.mk_Trueprop (Term.list_comb (rel, Rs) $ x $ y) :: 
traytel@53053
  1263
              map6 mk_prem bnf_sets_As bnf_sets_Bs Rs Rs_copy zs ys;
traytel@53053
  1264
            val concl = HOLogic.mk_Trueprop (Term.list_comb (rel, Rs_copy) $ x $ y);
traytel@53053
  1265
          in
traytel@53053
  1266
            Goal.prove_sorry lthy [] []
traytel@53053
  1267
              (fold_rev Logic.all (x :: y :: Rs @ Rs_copy) (Logic.list_implies (prems, concl)))
blanchet@54427
  1268
              (mk_rel_mono_strong_tac (Lazy.force in_rel) (map Lazy.force set_map))
traytel@53053
  1269
            |> Thm.close_derivation
traytel@53053
  1270
          end;
traytel@53053
  1271
traytel@53053
  1272
        val rel_mono_strong = Lazy.lazy mk_rel_mono_strong;
traytel@53053
  1273
traytel@53856
  1274
        fun mk_map_transfer () =
traytel@53856
  1275
          let
traytel@53862
  1276
            val rels = map2 mk_fun_rel transfer_domRs transfer_ranRs;
traytel@53862
  1277
            val rel = mk_fun_rel
traytel@53862
  1278
              (Term.list_comb (mk_bnf_rel transfer_domRTs CA' CB1, transfer_domRs))
traytel@53862
  1279
              (Term.list_comb (mk_bnf_rel transfer_ranRTs CB' CB2, transfer_ranRs));
traytel@53862
  1280
            val concl = HOLogic.mk_Trueprop
traytel@53862
  1281
              (fold_rev mk_fun_rel rels rel $ bnf_map_AsBs $ mk_bnf_map B1Ts B2Ts);
traytel@53856
  1282
          in
traytel@53856
  1283
            Goal.prove_sorry lthy [] []
traytel@53862
  1284
              (fold_rev Logic.all (transfer_domRs @ transfer_ranRs) concl)
traytel@53856
  1285
              (mk_map_transfer_tac (Lazy.force rel_mono) (Lazy.force in_rel)
blanchet@54427
  1286
                (map Lazy.force set_map) (#map_cong0 axioms) (Lazy.force map_comp))
traytel@53856
  1287
            |> Thm.close_derivation
traytel@53856
  1288
          end;
traytel@53856
  1289
traytel@53856
  1290
        val map_transfer = Lazy.lazy mk_map_transfer;
traytel@53856
  1291
traytel@53030
  1292
        val defs = mk_defs bnf_map_def bnf_set_defs bnf_rel_def;
blanchet@49990
  1293
traytel@53772
  1294
        val facts = mk_facts bd_Card_order bd_Cinfinite bd_Cnotzero collect_set_map in_bd in_cong
blanchet@54427
  1295
          in_mono in_rel map_comp map_cong map_id map_transfer map_wppull rel_eq rel_flip set_map
traytel@53053
  1296
          rel_cong rel_mono rel_mono_strong rel_Grp rel_conversep rel_OO;
blanchet@49990
  1297
blanchet@49990
  1298
        val wits = map2 mk_witness bnf_wits wit_thms;
blanchet@49990
  1299
blanchet@50522
  1300
        val bnf_rel =
blanchet@50522
  1301
          Term.subst_atomic_types ((Ds ~~ deads) @ (As' ~~ alphas) @ (Bs' ~~ betas)) rel;
blanchet@49990
  1302
blanchet@54402
  1303
        val bnf = mk_bnf bnf_b CA live alphas betas dead deads bnf_map bnf_sets bnf_bd axioms defs
blanchet@54402
  1304
          facts wits bnf_rel;
blanchet@49990
  1305
      in
blanchet@54402
  1306
        (bnf, lthy |> note_bnf_thms fact_policy qualify bnf_b bnf)
blanchet@49990
  1307
      end;
blanchet@50474
  1308
blanchet@50474
  1309
    val one_step_defs =
traytel@53030
  1310
      no_reflexive (bnf_map_def :: bnf_bd_def :: bnf_set_defs @ bnf_wit_defs @ [bnf_rel_def]);
blanchet@49990
  1311
  in
blanchet@50474
  1312
    (key, goals, wit_goalss, after_qed, lthy, one_step_defs)
blanchet@49990
  1313
  end;
blanchet@49990
  1314
traytel@50449
  1315
fun register_bnf key (bnf, lthy) =
traytel@50449
  1316
  (bnf, Local_Theory.declaration {syntax = false, pervasive = true}
blanchet@54094
  1317
    (fn phi => Data.map (Symtab.default (key, morph_bnf phi bnf))) lthy);
traytel@50449
  1318
blanchet@52904
  1319
fun bnf_def const_policy fact_policy qualify tacs wit_tac Ds map_b rel_b set_bs =
traytel@55641
  1320
  (fn (_, goals, (triv_tac_opt, wit_goalss), after_qed, lthy, one_step_defs) =>
blanchet@49990
  1321
  let
traytel@55641
  1322
    fun mk_wits_tac set_maps =
traytel@55641
  1323
      K (TRYALL Goal.conjunction_tac) THEN'
traytel@55641
  1324
      (case triv_tac_opt of
traytel@55641
  1325
        SOME tac => tac set_maps
traytel@55641
  1326
      | NONE => mk_unfold_thms_then_tac lthy one_step_defs wit_tac);
traytel@55641
  1327
    val wit_goals = map Logic.mk_conjunction_balanced wit_goalss;
traytel@55641
  1328
    fun mk_wit_thms set_maps =
traytel@55641
  1329
      Goal.prove_sorry lthy [] [] (Logic.mk_conjunction_balanced wit_goals) (mk_wits_tac set_maps)
traytel@55641
  1330
        |> Conjunction.elim_balanced (length wit_goals)
traytel@55641
  1331
        |> map2 (Conjunction.elim_balanced o length) wit_goalss
traytel@55641
  1332
        |> map (map (Thm.close_derivation o Thm.forall_elim_vars 0));
blanchet@49990
  1333
  in
wenzelm@52688
  1334
    map2 (Thm.close_derivation oo Goal.prove_sorry lthy [] [])
blanchet@50519
  1335
      goals (map (mk_unfold_thms_then_tac lthy one_step_defs) tacs)
traytel@55641
  1336
    |> (fn thms => after_qed mk_wit_thms (map single thms) lthy)
blanchet@52904
  1337
  end) oo prepare_def const_policy fact_policy qualify (K I) Ds map_b rel_b set_bs;
blanchet@49990
  1338
traytel@55641
  1339
val bnf_cmd = (fn (key, goals, (triv_tac_opt, wit_goalss), after_qed, lthy, defs) =>
traytel@55641
  1340
  let
traytel@55641
  1341
    val wit_goals = map Logic.mk_conjunction_balanced wit_goalss;
traytel@55641
  1342
    fun mk_triv_wit_thms tac set_maps =
traytel@55641
  1343
      Goal.prove_sorry lthy [] [] (Logic.mk_conjunction_balanced wit_goals)
traytel@55641
  1344
        (K (TRYALL Goal.conjunction_tac) THEN' tac set_maps)
traytel@55641
  1345
        |> Conjunction.elim_balanced (length wit_goals)
traytel@55641
  1346
        |> map2 (Conjunction.elim_balanced o length) wit_goalss
traytel@55641
  1347
        |> map (map (Thm.close_derivation o Thm.forall_elim_vars 0));
traytel@55641
  1348
    val (mk_wit_thms, nontriv_wit_goals) = 
traytel@55641
  1349
      (case triv_tac_opt of
traytel@55641
  1350
        NONE => (fn _ => [], map (map (rpair [])) wit_goalss)
traytel@55641
  1351
      | SOME tac => (mk_triv_wit_thms tac, []));
traytel@55641
  1352
  in
traytel@55641
  1353
    Proof.unfolding ([[(defs, [])]])
traytel@55641
  1354
      (Proof.theorem NONE (snd o register_bnf key oo after_qed mk_wit_thms)
traytel@55641
  1355
        (map (single o rpair []) goals @ nontriv_wit_goals) lthy)
traytel@55641
  1356
  end) oo prepare_def Do_Inline (user_policy Note_Some) I Syntax.read_term NONE
traytel@55641
  1357
    Binding.empty Binding.empty [];
blanchet@49990
  1358
blanchet@49990
  1359
fun print_bnfs ctxt =
blanchet@49990
  1360
  let
blanchet@49990
  1361
    fun pretty_set sets i = Pretty.block
blanchet@49990
  1362
      [Pretty.str (mk_setN (i + 1) ^ ":"), Pretty.brk 1,
blanchet@49990
  1363
          Pretty.quote (Syntax.pretty_term ctxt (nth sets i))];
blanchet@49990
  1364
blanchet@49990
  1365
    fun pretty_bnf (key, BNF {T = T, map = map, sets = sets, bd = bd,
blanchet@49990
  1366
      live = live, lives = lives, dead = dead, deads = deads, ...}) =
blanchet@49990
  1367
      Pretty.big_list
blanchet@49990
  1368
        (Pretty.string_of (Pretty.block [Pretty.str key, Pretty.str ":", Pretty.brk 1,
blanchet@49990
  1369
          Pretty.quote (Syntax.pretty_typ ctxt T)]))
blanchet@49990
  1370
        ([Pretty.block [Pretty.str "live:", Pretty.brk 1, Pretty.str (string_of_int live),
blanchet@49990
  1371
            Pretty.brk 3, Pretty.list "[" "]" (List.map (Syntax.pretty_typ ctxt) lives)],
blanchet@49990
  1372
          Pretty.block [Pretty.str "dead:", Pretty.brk 1, Pretty.str (string_of_int dead),
blanchet@49990
  1373
            Pretty.brk 3, Pretty.list "[" "]" (List.map (Syntax.pretty_typ ctxt) deads)],
blanchet@49990
  1374
          Pretty.block [Pretty.str (mapN ^ ":"), Pretty.brk 1,
blanchet@49990
  1375
            Pretty.quote (Syntax.pretty_term ctxt map)]] @
blanchet@49990
  1376
          List.map (pretty_set sets) (0 upto length sets - 1) @
blanchet@49990
  1377
          [Pretty.block [Pretty.str (bdN ^ ":"), Pretty.brk 1,
blanchet@49990
  1378
            Pretty.quote (Syntax.pretty_term ctxt bd)]]);
blanchet@49990
  1379
  in
blanchet@49990
  1380
    Pretty.big_list "BNFs:" (map pretty_bnf (Symtab.dest (Data.get (Context.Proof ctxt))))
blanchet@49990
  1381
    |> Pretty.writeln
blanchet@49990
  1382
  end;
blanchet@49990
  1383
blanchet@49990
  1384
val _ =
blanchet@52973
  1385
  Outer_Syntax.improper_command @{command_spec "print_bnfs"}
blanchet@54426
  1386
    "print all bounded natural functors"
blanchet@49990
  1387
    (Scan.succeed (Toplevel.keep (print_bnfs o Toplevel.context_of)));
blanchet@49990
  1388
blanchet@49990
  1389
val _ =
blanchet@52973
  1390
  Outer_Syntax.local_theory_to_proof @{command_spec "bnf"}
blanchet@54426
  1391
    "register a type as a bounded natural functor"
blanchet@52927
  1392
    ((parse_opt_binding_colon -- Parse.term --
blanchet@50292
  1393
       (@{keyword "["} |-- Parse.list Parse.term --| @{keyword "]"}) -- Parse.term --
traytel@55641
  1394
       (Scan.option ((@{keyword "["} |-- Parse.list Parse.term --| @{keyword "]"}))
traytel@55641
  1395
         >> the_default []) --
traytel@55641
  1396
       Scan.option Parse.term)
blanchet@52973
  1397
       >> bnf_cmd);
blanchet@49990
  1398
blanchet@49990
  1399
end;