src/HOL/BNF/Tools/bnf_def.ML
author blanchet
Wed Sep 26 10:00:59 2012 +0200 (2012-09-26)
changeset 49585 5c4a12550491
parent 49538 c90818b63599
child 49591 91b228e26348
permissions -rw-r--r--
generate high-level "maps", "sets", and "rels" properties
blanchet@49509
     1
(*  Title:      HOL/BNF/Tools/bnf_def.ML
blanchet@48975
     2
    Author:     Dmitriy Traytel, TU Muenchen
blanchet@48975
     3
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@48975
     4
    Copyright   2012
blanchet@48975
     5
blanchet@48975
     6
Definition of bounded natural functors.
blanchet@48975
     7
*)
blanchet@48975
     8
blanchet@48975
     9
signature BNF_DEF =
blanchet@48975
    10
sig
blanchet@48975
    11
  type BNF
blanchet@48975
    12
  type nonemptiness_witness = {I: int list, wit: term, prop: thm list}
blanchet@48975
    13
blanchet@48975
    14
  val bnf_of: Proof.context -> string -> BNF option
traytel@49434
    15
  val register_bnf: string -> (BNF * local_theory) -> (BNF * local_theory)
traytel@49434
    16
blanchet@48975
    17
  val name_of_bnf: BNF -> binding
blanchet@48975
    18
  val T_of_bnf: BNF -> typ
blanchet@48975
    19
  val live_of_bnf: BNF -> int
blanchet@48975
    20
  val lives_of_bnf: BNF -> typ list
blanchet@48975
    21
  val dead_of_bnf: BNF -> int
blanchet@48975
    22
  val deads_of_bnf: BNF -> typ list
blanchet@48975
    23
  val nwits_of_bnf: BNF -> int
blanchet@48975
    24
blanchet@48975
    25
  val mapN: string
blanchet@49507
    26
  val relN: string
blanchet@48975
    27
  val setN: string
blanchet@48975
    28
  val mk_setN: int -> string
blanchet@49506
    29
  val srelN: string
blanchet@48975
    30
blanchet@49214
    31
  val map_of_bnf: BNF -> term
blanchet@49585
    32
  val sets_of_bnf: BNF -> term list
blanchet@49585
    33
  val rel_of_bnf: BNF -> term
blanchet@49214
    34
blanchet@48975
    35
  val mk_T_of_bnf: typ list -> typ list -> BNF -> typ
blanchet@48975
    36
  val mk_bd_of_bnf: typ list -> typ list -> BNF -> term
blanchet@48975
    37
  val mk_map_of_bnf: typ list -> typ list -> typ list -> BNF -> term
blanchet@49507
    38
  val mk_rel_of_bnf: typ list -> typ list -> typ list -> BNF -> term
blanchet@48975
    39
  val mk_sets_of_bnf: typ list list -> typ list list -> BNF -> term list
blanchet@49506
    40
  val mk_srel_of_bnf: typ list -> typ list -> typ list -> BNF -> term
blanchet@48975
    41
  val mk_wits_of_bnf: typ list list -> typ list list -> BNF -> (int list * term) list
blanchet@48975
    42
blanchet@48975
    43
  val bd_Card_order_of_bnf: BNF -> thm
blanchet@48975
    44
  val bd_Cinfinite_of_bnf: BNF -> thm
blanchet@48975
    45
  val bd_Cnotzero_of_bnf: BNF -> thm
blanchet@48975
    46
  val bd_card_order_of_bnf: BNF -> thm
blanchet@48975
    47
  val bd_cinfinite_of_bnf: BNF -> thm
blanchet@48975
    48
  val collect_set_natural_of_bnf: BNF -> thm
blanchet@48975
    49
  val in_bd_of_bnf: BNF -> thm
blanchet@48975
    50
  val in_cong_of_bnf: BNF -> thm
blanchet@48975
    51
  val in_mono_of_bnf: BNF -> thm
blanchet@49506
    52
  val in_srel_of_bnf: BNF -> thm
blanchet@48975
    53
  val map_comp'_of_bnf: BNF -> thm
blanchet@48975
    54
  val map_comp_of_bnf: BNF -> thm
blanchet@48975
    55
  val map_cong_of_bnf: BNF -> thm
blanchet@48975
    56
  val map_def_of_bnf: BNF -> thm
blanchet@48975
    57
  val map_id'_of_bnf: BNF -> thm
blanchet@48975
    58
  val map_id_of_bnf: BNF -> thm
blanchet@48975
    59
  val map_wppull_of_bnf: BNF -> thm
blanchet@48975
    60
  val map_wpull_of_bnf: BNF -> thm
blanchet@49537
    61
  val rel_as_srel_of_bnf: BNF -> thm
blanchet@49507
    62
  val rel_def_of_bnf: BNF -> thm
blanchet@49537
    63
  val rel_flip_of_bnf: BNF -> thm
blanchet@48975
    64
  val set_bd_of_bnf: BNF -> thm list
blanchet@48975
    65
  val set_defs_of_bnf: BNF -> thm list
blanchet@48975
    66
  val set_natural'_of_bnf: BNF -> thm list
blanchet@48975
    67
  val set_natural_of_bnf: BNF -> thm list
blanchet@49506
    68
  val srel_def_of_bnf: BNF -> thm
blanchet@49506
    69
  val srel_Gr_of_bnf: BNF -> thm
blanchet@49506
    70
  val srel_Id_of_bnf: BNF -> thm
blanchet@49506
    71
  val srel_O_of_bnf: BNF -> thm
blanchet@49506
    72
  val srel_O_Gr_of_bnf: BNF -> thm
blanchet@49506
    73
  val srel_cong_of_bnf: BNF -> thm
blanchet@49506
    74
  val srel_converse_of_bnf: BNF -> thm
blanchet@49506
    75
  val srel_mono_of_bnf: BNF -> thm
blanchet@48975
    76
  val wit_thms_of_bnf: BNF -> thm list
blanchet@48975
    77
  val wit_thmss_of_bnf: BNF -> thm list list
blanchet@48975
    78
blanchet@48975
    79
  val mk_witness: int list * term -> thm list -> nonemptiness_witness
traytel@49103
    80
  val minimize_wits: (''a list * 'b) list -> (''a list * 'b) list
blanchet@48975
    81
  val wits_of_bnf: BNF -> nonemptiness_witness list
blanchet@48975
    82
blanchet@49460
    83
  val zip_axioms: 'a -> 'a -> 'a -> 'a list -> 'a -> 'a -> 'a list -> 'a -> 'a -> 'a -> 'a list
blanchet@49456
    84
blanchet@48975
    85
  datatype const_policy = Dont_Inline | Hardly_Inline | Smart_Inline | Do_Inline
blanchet@49538
    86
  datatype fact_policy = Dont_Note | Note_Some | Note_All
blanchet@49538
    87
blanchet@48975
    88
  val bnf_note_all: bool Config.T
traytel@49435
    89
  val user_policy: fact_policy -> Proof.context -> fact_policy
blanchet@48975
    90
blanchet@48975
    91
  val print_bnfs: Proof.context -> unit
blanchet@49018
    92
  val bnf_def: const_policy -> (Proof.context -> fact_policy) -> (binding -> binding) ->
blanchet@48975
    93
    ({prems: thm list, context: Proof.context} -> tactic) list ->
blanchet@48975
    94
    ({prems: thm list, context: Proof.context} -> tactic) -> typ list option ->
blanchet@49459
    95
    ((((binding * term) * term list) * term) * term list) * term option -> local_theory ->
blanchet@48975
    96
    BNF * local_theory
blanchet@48975
    97
end;
blanchet@48975
    98
blanchet@48975
    99
structure BNF_Def : BNF_DEF =
blanchet@48975
   100
struct
blanchet@48975
   101
blanchet@48975
   102
open BNF_Util
blanchet@49463
   103
open BNF_Tactics
blanchet@49284
   104
open BNF_Def_Tactics
blanchet@48975
   105
blanchet@48975
   106
type axioms = {
blanchet@48975
   107
  map_id: thm,
blanchet@48975
   108
  map_comp: thm,
blanchet@48975
   109
  map_cong: thm,
blanchet@48975
   110
  set_natural: thm list,
blanchet@48975
   111
  bd_card_order: thm,
blanchet@48975
   112
  bd_cinfinite: thm,
blanchet@48975
   113
  set_bd: thm list,
blanchet@48975
   114
  in_bd: thm,
blanchet@49453
   115
  map_wpull: thm,
blanchet@49506
   116
  srel_O_Gr: thm
blanchet@48975
   117
};
blanchet@48975
   118
blanchet@49506
   119
fun mk_axioms' (((((((((id, comp), cong), nat), c_o), cinf), set_bd), in_bd), wpull), srel) =
blanchet@48975
   120
  {map_id = id, map_comp = comp, map_cong = cong, set_natural = nat, bd_card_order = c_o,
blanchet@49506
   121
   bd_cinfinite = cinf, set_bd = set_bd, in_bd = in_bd, map_wpull = wpull, srel_O_Gr = srel};
blanchet@48975
   122
blanchet@48975
   123
fun dest_cons [] = raise Empty
blanchet@48975
   124
  | dest_cons (x :: xs) = (x, xs);
blanchet@48975
   125
blanchet@48975
   126
fun mk_axioms n thms = thms
blanchet@48975
   127
  |> map the_single
blanchet@48975
   128
  |> dest_cons
blanchet@48975
   129
  ||>> dest_cons
blanchet@48975
   130
  ||>> dest_cons
blanchet@48975
   131
  ||>> chop n
blanchet@48975
   132
  ||>> dest_cons
blanchet@48975
   133
  ||>> dest_cons
blanchet@48975
   134
  ||>> chop n
blanchet@48975
   135
  ||>> dest_cons
blanchet@49453
   136
  ||>> dest_cons
blanchet@48975
   137
  ||> the_single
blanchet@48975
   138
  |> mk_axioms';
blanchet@48975
   139
blanchet@49506
   140
fun zip_axioms mid mcomp mcong snat bdco bdinf sbd inbd wpull srel =
blanchet@49506
   141
  [mid, mcomp, mcong] @ snat @ [bdco, bdinf] @ sbd @ [inbd, wpull, srel];
blanchet@49460
   142
blanchet@49453
   143
fun dest_axioms {map_id, map_comp, map_cong, set_natural, bd_card_order, bd_cinfinite, set_bd,
blanchet@49506
   144
  in_bd, map_wpull, srel_O_Gr} =
blanchet@49460
   145
  zip_axioms map_id map_comp map_cong set_natural bd_card_order bd_cinfinite set_bd in_bd map_wpull
blanchet@49506
   146
    srel_O_Gr;
blanchet@48975
   147
blanchet@49463
   148
fun map_axioms f {map_id, map_comp, map_cong, set_natural, bd_card_order, bd_cinfinite, set_bd,
blanchet@49506
   149
  in_bd, map_wpull, srel_O_Gr} =
blanchet@48975
   150
  {map_id = f map_id,
blanchet@49463
   151
    map_comp = f map_comp,
blanchet@49463
   152
    map_cong = f map_cong,
blanchet@49463
   153
    set_natural = map f set_natural,
blanchet@49463
   154
    bd_card_order = f bd_card_order,
blanchet@49463
   155
    bd_cinfinite = f bd_cinfinite,
blanchet@49463
   156
    set_bd = map f set_bd,
blanchet@49463
   157
    in_bd = f in_bd,
blanchet@49463
   158
    map_wpull = f map_wpull,
blanchet@49506
   159
    srel_O_Gr = f srel_O_Gr};
blanchet@48975
   160
blanchet@48975
   161
val morph_axioms = map_axioms o Morphism.thm;
blanchet@48975
   162
blanchet@48975
   163
type defs = {
blanchet@48975
   164
  map_def: thm,
blanchet@48975
   165
  set_defs: thm list,
blanchet@49507
   166
  rel_def: thm,
blanchet@49506
   167
  srel_def: thm
blanchet@48975
   168
}
blanchet@48975
   169
blanchet@49507
   170
fun mk_defs map sets rel srel = {map_def = map, set_defs = sets, rel_def = rel, srel_def = srel};
blanchet@48975
   171
blanchet@49507
   172
fun map_defs f {map_def, set_defs, rel_def, srel_def} =
blanchet@49507
   173
  {map_def = f map_def, set_defs = map f set_defs, rel_def = f rel_def, srel_def = f srel_def};
blanchet@48975
   174
blanchet@48975
   175
val morph_defs = map_defs o Morphism.thm;
blanchet@48975
   176
blanchet@48975
   177
type facts = {
blanchet@48975
   178
  bd_Card_order: thm,
blanchet@48975
   179
  bd_Cinfinite: thm,
blanchet@48975
   180
  bd_Cnotzero: thm,
blanchet@48975
   181
  collect_set_natural: thm lazy,
blanchet@48975
   182
  in_cong: thm lazy,
blanchet@48975
   183
  in_mono: thm lazy,
blanchet@49506
   184
  in_srel: thm lazy,
blanchet@48975
   185
  map_comp': thm lazy,
blanchet@48975
   186
  map_id': thm lazy,
blanchet@48975
   187
  map_wppull: thm lazy,
blanchet@49537
   188
  rel_as_srel: thm lazy,
blanchet@49537
   189
  rel_flip: thm lazy,
blanchet@49506
   190
  set_natural': thm lazy list,
blanchet@49506
   191
  srel_cong: thm lazy,
blanchet@49506
   192
  srel_mono: thm lazy,
blanchet@49506
   193
  srel_Id: thm lazy,
blanchet@49506
   194
  srel_Gr: thm lazy,
blanchet@49506
   195
  srel_converse: thm lazy,
blanchet@49506
   196
  srel_O: thm lazy
blanchet@48975
   197
};
blanchet@48975
   198
blanchet@49506
   199
fun mk_facts bd_Card_order bd_Cinfinite bd_Cnotzero collect_set_natural in_cong in_mono in_srel
blanchet@49537
   200
    map_comp' map_id' map_wppull rel_as_srel rel_flip set_natural' srel_cong srel_mono srel_Id
blanchet@49537
   201
    srel_Gr srel_converse srel_O = {
blanchet@48975
   202
  bd_Card_order = bd_Card_order,
blanchet@48975
   203
  bd_Cinfinite = bd_Cinfinite,
blanchet@48975
   204
  bd_Cnotzero = bd_Cnotzero,
blanchet@48975
   205
  collect_set_natural = collect_set_natural,
blanchet@48975
   206
  in_cong = in_cong,
blanchet@48975
   207
  in_mono = in_mono,
blanchet@49506
   208
  in_srel = in_srel,
blanchet@48975
   209
  map_comp' = map_comp',
blanchet@48975
   210
  map_id' = map_id',
blanchet@48975
   211
  map_wppull = map_wppull,
blanchet@49537
   212
  rel_as_srel = rel_as_srel,
blanchet@49537
   213
  rel_flip = rel_flip,
blanchet@49506
   214
  set_natural' = set_natural',
blanchet@49506
   215
  srel_cong = srel_cong,
blanchet@49506
   216
  srel_mono = srel_mono,
blanchet@49506
   217
  srel_Id = srel_Id,
blanchet@49506
   218
  srel_Gr = srel_Gr,
blanchet@49506
   219
  srel_converse = srel_converse,
blanchet@49506
   220
  srel_O = srel_O};
blanchet@48975
   221
blanchet@48975
   222
fun map_facts f {
blanchet@48975
   223
  bd_Card_order,
blanchet@48975
   224
  bd_Cinfinite,
blanchet@48975
   225
  bd_Cnotzero,
blanchet@48975
   226
  collect_set_natural,
blanchet@48975
   227
  in_cong,
blanchet@48975
   228
  in_mono,
blanchet@49506
   229
  in_srel,
blanchet@48975
   230
  map_comp',
blanchet@48975
   231
  map_id',
blanchet@48975
   232
  map_wppull,
blanchet@49537
   233
  rel_as_srel,
blanchet@49537
   234
  rel_flip,
blanchet@49506
   235
  set_natural',
blanchet@49506
   236
  srel_cong,
blanchet@49506
   237
  srel_mono,
blanchet@49506
   238
  srel_Id,
blanchet@49506
   239
  srel_Gr,
blanchet@49506
   240
  srel_converse,
blanchet@49506
   241
  srel_O} =
blanchet@48975
   242
  {bd_Card_order = f bd_Card_order,
blanchet@48975
   243
    bd_Cinfinite = f bd_Cinfinite,
blanchet@48975
   244
    bd_Cnotzero = f bd_Cnotzero,
blanchet@48975
   245
    collect_set_natural = Lazy.map f collect_set_natural,
blanchet@48975
   246
    in_cong = Lazy.map f in_cong,
blanchet@48975
   247
    in_mono = Lazy.map f in_mono,
blanchet@49506
   248
    in_srel = Lazy.map f in_srel,
blanchet@48975
   249
    map_comp' = Lazy.map f map_comp',
blanchet@48975
   250
    map_id' = Lazy.map f map_id',
blanchet@48975
   251
    map_wppull = Lazy.map f map_wppull,
blanchet@49537
   252
    rel_as_srel = Lazy.map f rel_as_srel,
blanchet@49537
   253
    rel_flip = Lazy.map f rel_flip,
blanchet@49506
   254
    set_natural' = map (Lazy.map f) set_natural',
blanchet@49506
   255
    srel_cong = Lazy.map f srel_cong,
blanchet@49506
   256
    srel_mono = Lazy.map f srel_mono,
blanchet@49506
   257
    srel_Id = Lazy.map f srel_Id,
blanchet@49506
   258
    srel_Gr = Lazy.map f srel_Gr,
blanchet@49506
   259
    srel_converse = Lazy.map f srel_converse,
blanchet@49506
   260
    srel_O = Lazy.map f srel_O};
blanchet@48975
   261
blanchet@48975
   262
val morph_facts = map_facts o Morphism.thm;
blanchet@48975
   263
blanchet@48975
   264
type nonemptiness_witness = {
blanchet@48975
   265
  I: int list,
blanchet@48975
   266
  wit: term,
blanchet@48975
   267
  prop: thm list
blanchet@48975
   268
};
blanchet@48975
   269
blanchet@48975
   270
fun mk_witness (I, wit) prop = {I = I, wit = wit, prop = prop};
blanchet@48975
   271
fun map_witness f g {I, wit, prop} = {I = I, wit = f wit, prop = map g prop};
blanchet@48975
   272
fun morph_witness phi = map_witness (Morphism.term phi) (Morphism.thm phi);
blanchet@48975
   273
blanchet@48975
   274
datatype BNF = BNF of {
blanchet@48975
   275
  name: binding,
blanchet@48975
   276
  T: typ,
blanchet@48975
   277
  live: int,
blanchet@48975
   278
  lives: typ list, (*source type variables of map, only for composition*)
blanchet@48975
   279
  lives': typ list, (*target type variables of map, only for composition*)
blanchet@48975
   280
  dead: int,
blanchet@48975
   281
  deads: typ list, (*only for composition*)
blanchet@48975
   282
  map: term,
blanchet@48975
   283
  sets: term list,
blanchet@48975
   284
  bd: term,
blanchet@48975
   285
  axioms: axioms,
blanchet@48975
   286
  defs: defs,
blanchet@48975
   287
  facts: facts,
blanchet@48975
   288
  nwits: int,
blanchet@48975
   289
  wits: nonemptiness_witness list,
blanchet@49507
   290
  rel: term,
blanchet@49506
   291
  srel: term
blanchet@48975
   292
};
blanchet@48975
   293
blanchet@48975
   294
(* getters *)
blanchet@48975
   295
blanchet@48975
   296
fun rep_bnf (BNF bnf) = bnf;
blanchet@48975
   297
val name_of_bnf = #name o rep_bnf;
blanchet@48975
   298
val T_of_bnf = #T o rep_bnf;
blanchet@48975
   299
fun mk_T_of_bnf Ds Ts bnf =
blanchet@48975
   300
  let val bnf_rep = rep_bnf bnf
blanchet@48975
   301
  in Term.typ_subst_atomic ((#deads bnf_rep ~~ Ds) @ (#lives bnf_rep ~~ Ts)) (#T bnf_rep) end;
blanchet@48975
   302
val live_of_bnf = #live o rep_bnf;
blanchet@48975
   303
val lives_of_bnf = #lives o rep_bnf;
blanchet@48975
   304
val dead_of_bnf = #dead o rep_bnf;
blanchet@48975
   305
val deads_of_bnf = #deads o rep_bnf;
blanchet@48975
   306
val axioms_of_bnf = #axioms o rep_bnf;
blanchet@48975
   307
val facts_of_bnf = #facts o rep_bnf;
blanchet@48975
   308
val nwits_of_bnf = #nwits o rep_bnf;
blanchet@48975
   309
val wits_of_bnf = #wits o rep_bnf;
blanchet@48975
   310
blanchet@48975
   311
(*terms*)
blanchet@48975
   312
val map_of_bnf = #map o rep_bnf;
blanchet@48975
   313
val sets_of_bnf = #sets o rep_bnf;
blanchet@48975
   314
fun mk_map_of_bnf Ds Ts Us bnf =
blanchet@48975
   315
  let val bnf_rep = rep_bnf bnf;
blanchet@48975
   316
  in
blanchet@48975
   317
    Term.subst_atomic_types
blanchet@48975
   318
      ((#deads bnf_rep ~~ Ds) @ (#lives bnf_rep ~~ Ts) @ (#lives' bnf_rep ~~ Us)) (#map bnf_rep)
blanchet@48975
   319
  end;
blanchet@48975
   320
fun mk_sets_of_bnf Dss Tss bnf =
blanchet@48975
   321
  let val bnf_rep = rep_bnf bnf;
blanchet@48975
   322
  in
blanchet@48975
   323
    map2 (fn (Ds, Ts) => Term.subst_atomic_types
blanchet@48975
   324
      ((#deads bnf_rep ~~ Ds) @ (#lives bnf_rep ~~ Ts))) (Dss ~~ Tss) (#sets bnf_rep)
blanchet@48975
   325
  end;
blanchet@48975
   326
val bd_of_bnf = #bd o rep_bnf;
blanchet@48975
   327
fun mk_bd_of_bnf Ds Ts bnf =
blanchet@48975
   328
  let val bnf_rep = rep_bnf bnf;
blanchet@48975
   329
  in Term.subst_atomic_types ((#deads bnf_rep ~~ Ds) @ (#lives bnf_rep ~~ Ts)) (#bd bnf_rep) end;
blanchet@48975
   330
fun mk_wits_of_bnf Dss Tss bnf =
blanchet@48975
   331
  let
blanchet@48975
   332
    val bnf_rep = rep_bnf bnf;
blanchet@48975
   333
    val wits = map (fn x => (#I x, #wit x)) (#wits bnf_rep);
blanchet@48975
   334
  in
blanchet@48975
   335
    map2 (fn (Ds, Ts) => apsnd (Term.subst_atomic_types
blanchet@48975
   336
      ((#deads bnf_rep ~~ Ds) @ (#lives bnf_rep ~~ Ts)))) (Dss ~~ Tss) wits
blanchet@48975
   337
  end;
blanchet@49507
   338
val rel_of_bnf = #rel o rep_bnf;
blanchet@49507
   339
fun mk_rel_of_bnf Ds Ts Us bnf =
blanchet@49462
   340
  let val bnf_rep = rep_bnf bnf;
blanchet@49462
   341
  in
blanchet@49462
   342
    Term.subst_atomic_types
blanchet@49507
   343
      ((#deads bnf_rep ~~ Ds) @ (#lives bnf_rep ~~ Ts) @ (#lives' bnf_rep ~~ Us)) (#rel bnf_rep)
blanchet@49462
   344
  end;
blanchet@49506
   345
val srel_of_bnf = #srel o rep_bnf;
blanchet@49506
   346
fun mk_srel_of_bnf Ds Ts Us bnf =
blanchet@48975
   347
  let val bnf_rep = rep_bnf bnf;
blanchet@48975
   348
  in
blanchet@48975
   349
    Term.subst_atomic_types
blanchet@49506
   350
      ((#deads bnf_rep ~~ Ds) @ (#lives bnf_rep ~~ Ts) @ (#lives' bnf_rep ~~ Us)) (#srel bnf_rep)
blanchet@48975
   351
  end;
blanchet@48975
   352
blanchet@48975
   353
(*thms*)
blanchet@48975
   354
val bd_card_order_of_bnf = #bd_card_order o #axioms o rep_bnf;
blanchet@48975
   355
val bd_cinfinite_of_bnf = #bd_cinfinite o #axioms o rep_bnf;
blanchet@48975
   356
val bd_Card_order_of_bnf = #bd_Card_order o #facts o rep_bnf;
blanchet@48975
   357
val bd_Cinfinite_of_bnf = #bd_Cinfinite o #facts o rep_bnf;
blanchet@48975
   358
val bd_Cnotzero_of_bnf = #bd_Cnotzero o #facts o rep_bnf;
blanchet@48975
   359
val collect_set_natural_of_bnf = Lazy.force o #collect_set_natural o #facts o rep_bnf;
blanchet@48975
   360
val in_bd_of_bnf = #in_bd o #axioms o rep_bnf;
blanchet@48975
   361
val in_cong_of_bnf = Lazy.force o #in_cong o #facts o rep_bnf;
blanchet@48975
   362
val in_mono_of_bnf = Lazy.force o #in_mono o #facts o rep_bnf;
blanchet@49506
   363
val in_srel_of_bnf = Lazy.force o #in_srel o #facts o rep_bnf;
blanchet@48975
   364
val map_def_of_bnf = #map_def o #defs o rep_bnf;
blanchet@48975
   365
val map_id_of_bnf = #map_id o #axioms o rep_bnf;
blanchet@48975
   366
val map_id'_of_bnf = Lazy.force o #map_id' o #facts o rep_bnf;
blanchet@48975
   367
val map_comp_of_bnf = #map_comp o #axioms o rep_bnf;
blanchet@48975
   368
val map_comp'_of_bnf = Lazy.force o #map_comp' o #facts o rep_bnf;
blanchet@48975
   369
val map_cong_of_bnf = #map_cong o #axioms o rep_bnf;
blanchet@48975
   370
val map_wppull_of_bnf = Lazy.force o #map_wppull o #facts o rep_bnf;
blanchet@48975
   371
val map_wpull_of_bnf = #map_wpull o #axioms o rep_bnf;
blanchet@49537
   372
val rel_as_srel_of_bnf = Lazy.force o #rel_as_srel o #facts o rep_bnf;
blanchet@49507
   373
val rel_def_of_bnf = #rel_def o #defs o rep_bnf;
blanchet@49537
   374
val rel_flip_of_bnf = Lazy.force o #rel_flip o #facts o rep_bnf;
blanchet@48975
   375
val set_bd_of_bnf = #set_bd o #axioms o rep_bnf;
blanchet@48975
   376
val set_defs_of_bnf = #set_defs o #defs o rep_bnf;
blanchet@48975
   377
val set_natural_of_bnf = #set_natural o #axioms o rep_bnf;
blanchet@48975
   378
val set_natural'_of_bnf = map Lazy.force o #set_natural' o #facts o rep_bnf;
blanchet@49506
   379
val srel_cong_of_bnf = Lazy.force o #srel_cong o #facts o rep_bnf;
blanchet@49506
   380
val srel_mono_of_bnf = Lazy.force o #srel_mono o #facts o rep_bnf;
blanchet@49506
   381
val srel_def_of_bnf = #srel_def o #defs o rep_bnf;
blanchet@49506
   382
val srel_Id_of_bnf = Lazy.force o #srel_Id o #facts o rep_bnf;
blanchet@49506
   383
val srel_Gr_of_bnf = Lazy.force o #srel_Gr o #facts o rep_bnf;
blanchet@49506
   384
val srel_converse_of_bnf = Lazy.force o #srel_converse o #facts o rep_bnf;
blanchet@49506
   385
val srel_O_of_bnf = Lazy.force o #srel_O o #facts o rep_bnf;
blanchet@49506
   386
val srel_O_Gr_of_bnf = #srel_O_Gr o #axioms o rep_bnf;
blanchet@48975
   387
val wit_thms_of_bnf = maps #prop o wits_of_bnf;
blanchet@48975
   388
val wit_thmss_of_bnf = map #prop o wits_of_bnf;
blanchet@48975
   389
blanchet@49507
   390
fun mk_bnf name T live lives lives' dead deads map sets bd axioms defs facts wits rel srel =
blanchet@48975
   391
  BNF {name = name, T = T,
blanchet@48975
   392
       live = live, lives = lives, lives' = lives', dead = dead, deads = deads,
blanchet@48975
   393
       map = map, sets = sets, bd = bd,
blanchet@48975
   394
       axioms = axioms, defs = defs, facts = facts,
blanchet@49507
   395
       nwits = length wits, wits = wits, rel = rel, srel = srel};
blanchet@48975
   396
blanchet@48975
   397
fun morph_bnf phi (BNF {name = name, T = T, live = live, lives = lives, lives' = lives',
blanchet@48975
   398
  dead = dead, deads = deads, map = map, sets = sets, bd = bd,
blanchet@48975
   399
  axioms = axioms, defs = defs, facts = facts,
blanchet@49507
   400
  nwits = nwits, wits = wits, rel = rel, srel = srel}) =
blanchet@48975
   401
  BNF {name = Morphism.binding phi name, T = Morphism.typ phi T,
blanchet@48975
   402
    live = live, lives = List.map (Morphism.typ phi) lives,
blanchet@48975
   403
    lives' = List.map (Morphism.typ phi) lives',
blanchet@48975
   404
    dead = dead, deads = List.map (Morphism.typ phi) deads,
blanchet@48975
   405
    map = Morphism.term phi map, sets = List.map (Morphism.term phi) sets,
blanchet@48975
   406
    bd = Morphism.term phi bd,
blanchet@48975
   407
    axioms = morph_axioms phi axioms,
blanchet@48975
   408
    defs = morph_defs phi defs,
blanchet@48975
   409
    facts = morph_facts phi facts,
blanchet@48975
   410
    nwits = nwits,
blanchet@48975
   411
    wits = List.map (morph_witness phi) wits,
blanchet@49507
   412
    rel = Morphism.term phi rel, srel = Morphism.term phi srel};
blanchet@48975
   413
blanchet@48975
   414
fun eq_bnf (BNF {T = T1, live = live1, dead = dead1, ...},
blanchet@48975
   415
  BNF {T = T2, live = live2, dead = dead2, ...}) =
blanchet@48975
   416
  Type.could_unify (T1, T2) andalso live1 = live2 andalso dead1 = dead2;
blanchet@48975
   417
blanchet@48975
   418
structure Data = Generic_Data
blanchet@48975
   419
(
blanchet@48975
   420
  type T = BNF Symtab.table;
blanchet@48975
   421
  val empty = Symtab.empty;
blanchet@48975
   422
  val extend = I;
blanchet@49462
   423
  val merge = Symtab.merge eq_bnf;
blanchet@48975
   424
);
blanchet@48975
   425
blanchet@48975
   426
val bnf_of = Symtab.lookup o Data.get o Context.Proof;
blanchet@48975
   427
blanchet@48975
   428
blanchet@48975
   429
blanchet@48975
   430
(* Utilities *)
blanchet@48975
   431
blanchet@48975
   432
fun normalize_set insts instA set =
blanchet@48975
   433
  let
blanchet@48975
   434
    val (T, T') = dest_funT (fastype_of set);
blanchet@48975
   435
    val A = fst (Term.dest_TVar (HOLogic.dest_setT T'));
blanchet@48975
   436
    val params = Term.add_tvar_namesT T [];
blanchet@48975
   437
  in Term.subst_TVars ((A :: params) ~~ (instA :: insts)) set end;
blanchet@48975
   438
blanchet@49507
   439
fun normalize_rel ctxt instTs instA instB rel =
blanchet@49462
   440
  let
blanchet@49462
   441
    val thy = Proof_Context.theory_of ctxt;
blanchet@49462
   442
    val tyenv =
blanchet@49507
   443
      Sign.typ_match thy (fastype_of rel, Library.foldr (op -->) (instTs, mk_pred2T instA instB))
blanchet@49463
   444
        Vartab.empty;
blanchet@49507
   445
  in Envir.subst_term (tyenv, Vartab.empty) rel end
blanchet@49462
   446
  handle Type.TYPE_MATCH => error "Bad predicator";
blanchet@49462
   447
blanchet@49506
   448
fun normalize_srel ctxt instTs instA instB srel =
blanchet@48975
   449
  let
blanchet@48975
   450
    val thy = Proof_Context.theory_of ctxt;
blanchet@48975
   451
    val tyenv =
blanchet@49506
   452
      Sign.typ_match thy (fastype_of srel, Library.foldr (op -->) (instTs, mk_relT (instA, instB)))
blanchet@48975
   453
        Vartab.empty;
blanchet@49506
   454
  in Envir.subst_term (tyenv, Vartab.empty) srel end
blanchet@49453
   455
  handle Type.TYPE_MATCH => error "Bad relator";
blanchet@48975
   456
blanchet@48975
   457
fun normalize_wit insts CA As wit =
blanchet@48975
   458
  let
blanchet@48975
   459
    fun strip_param (Ts, T as Type (@{type_name fun}, [T1, T2])) =
blanchet@48975
   460
        if Type.raw_instance (CA, T) then (Ts, T) else strip_param (T1 :: Ts, T2)
blanchet@48975
   461
      | strip_param x = x;
blanchet@48975
   462
    val (Ts, T) = strip_param ([], fastype_of wit);
blanchet@48975
   463
    val subst = Term.add_tvar_namesT T [] ~~ insts;
blanchet@48975
   464
    fun find y = find_index (fn x => x = y) As;
blanchet@48975
   465
  in
blanchet@48975
   466
    (map (find o Term.typ_subst_TVars subst) (rev Ts), Term.subst_TVars subst wit)
blanchet@48975
   467
  end;
blanchet@48975
   468
blanchet@48975
   469
fun minimize_wits wits =
blanchet@48975
   470
 let
blanchet@48975
   471
   fun minimize done [] = done
traytel@49103
   472
     | minimize done ((I, wit) :: todo) =
blanchet@48975
   473
       if exists (fn (J, _) => subset (op =) (J, I)) (done @ todo)
blanchet@48975
   474
       then minimize done todo
blanchet@48975
   475
       else minimize ((I, wit) :: done) todo;
blanchet@48975
   476
 in minimize [] wits end;
blanchet@48975
   477
blanchet@48975
   478
blanchet@48975
   479
blanchet@48975
   480
(* Names *)
blanchet@48975
   481
blanchet@48975
   482
val mapN = "map";
blanchet@48975
   483
val setN = "set";
blanchet@48975
   484
fun mk_setN i = setN ^ nonzero_string_of_int i;
blanchet@48975
   485
val bdN = "bd";
blanchet@48975
   486
val witN = "wit";
blanchet@48975
   487
fun mk_witN i = witN ^ nonzero_string_of_int i;
blanchet@49507
   488
val relN = "rel";
blanchet@49506
   489
val srelN = "srel";
blanchet@48975
   490
blanchet@48975
   491
val bd_card_orderN = "bd_card_order";
blanchet@48975
   492
val bd_cinfiniteN = "bd_cinfinite";
blanchet@48975
   493
val bd_Card_orderN = "bd_Card_order";
blanchet@48975
   494
val bd_CinfiniteN = "bd_Cinfinite";
blanchet@48975
   495
val bd_CnotzeroN = "bd_Cnotzero";
blanchet@48975
   496
val collect_set_naturalN = "collect_set_natural";
blanchet@48975
   497
val in_bdN = "in_bd";
blanchet@48975
   498
val in_monoN = "in_mono";
blanchet@49506
   499
val in_srelN = "in_srel";
blanchet@48975
   500
val map_idN = "map_id";
blanchet@48975
   501
val map_id'N = "map_id'";
blanchet@48975
   502
val map_compN = "map_comp";
blanchet@48975
   503
val map_comp'N = "map_comp'";
blanchet@48975
   504
val map_congN = "map_cong";
blanchet@48975
   505
val map_wpullN = "map_wpull";
blanchet@49537
   506
val rel_as_srelN = "rel_as_srel";
blanchet@49537
   507
val rel_flipN = "rel_flip";
blanchet@49537
   508
val set_naturalN = "set_natural";
blanchet@49537
   509
val set_natural'N = "set_natural'";
blanchet@49537
   510
val set_bdN = "set_bd";
blanchet@49506
   511
val srel_IdN = "srel_Id";
blanchet@49506
   512
val srel_GrN = "srel_Gr";
blanchet@49506
   513
val srel_converseN = "srel_converse";
blanchet@49506
   514
val srel_monoN = "srel_mono"
blanchet@49506
   515
val srel_ON = "srel_comp";
blanchet@49506
   516
val srel_O_GrN = "srel_comp_Gr";
blanchet@48975
   517
blanchet@48975
   518
datatype const_policy = Dont_Inline | Hardly_Inline | Smart_Inline | Do_Inline;
blanchet@48975
   519
blanchet@49538
   520
datatype fact_policy = Dont_Note | Note_Some | Note_All;
blanchet@48975
   521
blanchet@48975
   522
val bnf_note_all = Attrib.setup_config_bool @{binding bnf_note_all} (K false);
blanchet@48975
   523
blanchet@49538
   524
fun user_policy policy ctxt = if Config.get ctxt bnf_note_all then Note_All else policy;
blanchet@48975
   525
blanchet@48975
   526
val smart_max_inline_size = 25; (*FUDGE*)
blanchet@48975
   527
blanchet@48975
   528
blanchet@48975
   529
(* Define new BNFs *)
blanchet@48975
   530
blanchet@49019
   531
fun prepare_def const_policy mk_fact_policy qualify prep_term Ds_opt
blanchet@49507
   532
  (((((raw_b, raw_map), raw_sets), raw_bd_Abs), raw_wits), raw_rel_opt) no_defs_lthy =
blanchet@48975
   533
  let
blanchet@48975
   534
    val fact_policy = mk_fact_policy no_defs_lthy;
blanchet@48975
   535
    val b = qualify raw_b;
blanchet@48975
   536
    val live = length raw_sets;
blanchet@48975
   537
    val nwits = length raw_wits;
blanchet@48975
   538
blanchet@48975
   539
    val map_rhs = prep_term no_defs_lthy raw_map;
blanchet@48975
   540
    val set_rhss = map (prep_term no_defs_lthy) raw_sets;
blanchet@48975
   541
    val (bd_rhsT, bd_rhs) = (case prep_term no_defs_lthy raw_bd_Abs of
blanchet@48975
   542
      Abs (_, T, t) => (T, t)
blanchet@48975
   543
    | _ => error "Bad bound constant");
blanchet@48975
   544
    val wit_rhss = map (prep_term no_defs_lthy) raw_wits;
blanchet@48975
   545
traytel@49434
   546
    fun err T =
traytel@49434
   547
      error ("Trying to register the type " ^ quote (Syntax.string_of_typ no_defs_lthy T) ^
traytel@49434
   548
        " as unnamed BNF");
traytel@49434
   549
traytel@49434
   550
    val (b, key) =
traytel@49434
   551
      if Binding.eq_name (b, Binding.empty) then
traytel@49434
   552
        (case bd_rhsT of
traytel@49434
   553
          Type (C, Ts) => if forall (is_some o try dest_TFree) Ts
traytel@49434
   554
            then (Binding.qualified_name C, C) else err bd_rhsT
traytel@49434
   555
        | T => err T)
traytel@49434
   556
      else (b, Local_Theory.full_name no_defs_lthy b);
traytel@49434
   557
blanchet@49463
   558
    fun maybe_define user_specified (b, rhs) lthy =
blanchet@48975
   559
      let
blanchet@48975
   560
        val inline =
blanchet@49538
   561
          (user_specified orelse fact_policy = Dont_Note) andalso
blanchet@48975
   562
          (case const_policy of
blanchet@48975
   563
            Dont_Inline => false
blanchet@48975
   564
          | Hardly_Inline => Term.is_Free rhs orelse Term.is_Const rhs
blanchet@48975
   565
          | Smart_Inline => Term.size_of_term rhs <= smart_max_inline_size
blanchet@48975
   566
          | Do_Inline => true)
blanchet@48975
   567
      in
blanchet@48975
   568
        if inline then
blanchet@49460
   569
          ((rhs, Drule.reflexive_thm), lthy)
blanchet@48975
   570
        else
blanchet@48975
   571
          let val b = b () in
blanchet@48975
   572
            apfst (apsnd snd) (Local_Theory.define ((b, NoSyn), ((Thm.def_binding b, []), rhs))
blanchet@48975
   573
              lthy)
blanchet@48975
   574
          end
blanchet@48975
   575
      end;
blanchet@49459
   576
blanchet@49459
   577
    fun maybe_restore lthy_old lthy =
blanchet@49459
   578
      lthy |> not (pointer_eq (lthy_old, lthy)) ? Local_Theory.restore;
blanchet@48975
   579
blanchet@49459
   580
    val map_bind_def = (fn () => Binding.suffix_name ("_" ^ mapN) b, map_rhs);
blanchet@49459
   581
    val set_binds_defs =
blanchet@49459
   582
      let
blanchet@49459
   583
        val bs = if live = 1 then [fn () => Binding.suffix_name ("_" ^ setN) b]
blanchet@49459
   584
          else map (fn i => fn () => Binding.suffix_name ("_" ^ mk_setN i) b) (1 upto live)
blanchet@49459
   585
      in map2 pair bs set_rhss end;
blanchet@49459
   586
    val bd_bind_def = (fn () => Binding.suffix_name ("_" ^ bdN) b, bd_rhs);
blanchet@49459
   587
    val wit_binds_defs =
blanchet@49459
   588
      let
blanchet@49459
   589
        val bs = if nwits = 1 then [fn () => Binding.suffix_name ("_" ^ witN) b]
blanchet@49459
   590
          else map (fn i => fn () => Binding.suffix_name ("_" ^ mk_witN i) b) (1 upto nwits);
blanchet@49459
   591
      in map2 pair bs wit_rhss end;
blanchet@49459
   592
blanchet@49459
   593
    val (((((bnf_map_term, raw_map_def),
blanchet@48975
   594
      (bnf_set_terms, raw_set_defs)),
blanchet@48975
   595
      (bnf_bd_term, raw_bd_def)),
blanchet@49459
   596
      (bnf_wit_terms, raw_wit_defs)), (lthy, lthy_old)) =
blanchet@48975
   597
        no_defs_lthy
blanchet@49463
   598
        |> maybe_define true map_bind_def
blanchet@49463
   599
        ||>> apfst split_list o fold_map (maybe_define true) set_binds_defs
blanchet@49463
   600
        ||>> maybe_define true bd_bind_def
blanchet@49463
   601
        ||>> apfst split_list o fold_map (maybe_define true) wit_binds_defs
blanchet@48975
   602
        ||> `(maybe_restore no_defs_lthy);
blanchet@48975
   603
blanchet@49459
   604
    val phi = Proof_Context.export_morphism lthy_old lthy;
blanchet@48975
   605
blanchet@48975
   606
    val bnf_map_def = Morphism.thm phi raw_map_def;
blanchet@48975
   607
    val bnf_set_defs = map (Morphism.thm phi) raw_set_defs;
blanchet@48975
   608
    val bnf_bd_def = Morphism.thm phi raw_bd_def;
blanchet@48975
   609
    val bnf_wit_defs = map (Morphism.thm phi) raw_wit_defs;
blanchet@48975
   610
blanchet@48975
   611
    val bnf_map = Morphism.term phi bnf_map_term;
blanchet@48975
   612
blanchet@48975
   613
    (*TODO: handle errors*)
blanchet@48975
   614
    (*simple shape analysis of a map function*)
traytel@49395
   615
    val ((alphas, betas), (CA, _)) =
traytel@49395
   616
      fastype_of bnf_map
traytel@49395
   617
      |> strip_typeN live
traytel@49395
   618
      |>> map_split dest_funT
traytel@49395
   619
      ||> dest_funT
traytel@49395
   620
      handle TYPE _ => error "Bad map function";
blanchet@48975
   621
blanchet@48975
   622
    val CA_params = map TVar (Term.add_tvarsT CA []);
blanchet@48975
   623
blanchet@48975
   624
    val bnf_sets = map2 (normalize_set CA_params) alphas (map (Morphism.term phi) bnf_set_terms);
blanchet@48975
   625
    val bdT = Morphism.typ phi bd_rhsT;
blanchet@48975
   626
    val bnf_bd =
blanchet@48975
   627
      Term.subst_TVars (Term.add_tvar_namesT bdT [] ~~ CA_params) (Morphism.term phi bnf_bd_term);
blanchet@48975
   628
    val bnf_wits = map (normalize_wit CA_params CA alphas o Morphism.term phi) bnf_wit_terms;
blanchet@48975
   629
blanchet@48975
   630
    (*TODO: assert Ds = (TVars of bnf_map) \ (alphas @ betas) as sets*)
blanchet@48975
   631
    val deads = (case Ds_opt of
blanchet@48975
   632
      NONE => subtract (op =) (alphas @ betas) (map TVar (Term.add_tvars bnf_map []))
blanchet@48975
   633
    | SOME Ds => map (Morphism.typ phi) Ds);
blanchet@48975
   634
    val dead = length deads;
blanchet@48975
   635
blanchet@48975
   636
    (*TODO: further checks of type of bnf_map*)
blanchet@48975
   637
    (*TODO: check types of bnf_sets*)
blanchet@48975
   638
    (*TODO: check type of bnf_bd*)
blanchet@49507
   639
    (*TODO: check type of bnf_rel*)
blanchet@48975
   640
blanchet@48975
   641
    val ((((((((((As', Bs'), Cs), Ds), B1Ts), B2Ts), domTs), ranTs), ranTs'), ranTs''),
blanchet@49459
   642
      (Ts, T)) = lthy
blanchet@48975
   643
      |> mk_TFrees live
blanchet@48975
   644
      ||>> mk_TFrees live
blanchet@48975
   645
      ||>> mk_TFrees live
blanchet@48975
   646
      ||>> mk_TFrees dead
blanchet@48975
   647
      ||>> mk_TFrees live
blanchet@48975
   648
      ||>> mk_TFrees live
blanchet@48975
   649
      ||>> mk_TFrees live
blanchet@48975
   650
      ||>> mk_TFrees live
blanchet@48975
   651
      ||>> mk_TFrees live
blanchet@48975
   652
      ||>> mk_TFrees live
blanchet@48975
   653
      ||> fst o mk_TFrees 1
blanchet@48975
   654
      ||> the_single
blanchet@48975
   655
      ||> `(replicate live);
blanchet@48975
   656
blanchet@48975
   657
    fun mk_bnf_map As' Bs' =
blanchet@48975
   658
      Term.subst_atomic_types ((deads ~~ Ds) @ (alphas ~~ As') @ (betas ~~ Bs')) bnf_map;
blanchet@49453
   659
    fun mk_bnf_t As' = Term.subst_atomic_types ((deads ~~ Ds) @ (alphas ~~ As'));
blanchet@49453
   660
    fun mk_bnf_T As' = Term.typ_subst_atomic ((deads ~~ Ds) @ (alphas ~~ As'));
blanchet@49453
   661
blanchet@48975
   662
    val (setRTs, RTs) = map_split (`HOLogic.mk_setT o HOLogic.mk_prodT) (As' ~~ Bs');
blanchet@48975
   663
    val setRTsAsCs = map (HOLogic.mk_setT o HOLogic.mk_prodT) (As' ~~ Cs);
blanchet@48975
   664
    val setRTsBsCs = map (HOLogic.mk_setT o HOLogic.mk_prodT) (Bs' ~~ Cs);
blanchet@48975
   665
    val setRT's = map (HOLogic.mk_setT o HOLogic.mk_prodT) (Bs' ~~ As');
blanchet@48975
   666
    val self_setRTs = map (HOLogic.mk_setT o HOLogic.mk_prodT) (As' ~~ As');
blanchet@49463
   667
    val QTs = map2 mk_pred2T As' Bs';
blanchet@48975
   668
blanchet@49453
   669
    val CA' = mk_bnf_T As' CA;
blanchet@49453
   670
    val CB' = mk_bnf_T Bs' CA;
blanchet@49453
   671
    val CC' = mk_bnf_T Cs CA;
blanchet@49453
   672
    val CRs' = mk_bnf_T RTs CA;
blanchet@49463
   673
    val CA'CB' = HOLogic.mk_prodT (CA', CB');
blanchet@49453
   674
blanchet@48975
   675
    val bnf_map_AsAs = mk_bnf_map As' As';
blanchet@48975
   676
    val bnf_map_AsBs = mk_bnf_map As' Bs';
blanchet@48975
   677
    val bnf_map_AsCs = mk_bnf_map As' Cs;
blanchet@48975
   678
    val bnf_map_BsCs = mk_bnf_map Bs' Cs;
blanchet@48975
   679
    val bnf_sets_As = map (mk_bnf_t As') bnf_sets;
blanchet@48975
   680
    val bnf_sets_Bs = map (mk_bnf_t Bs') bnf_sets;
blanchet@48975
   681
    val bnf_bd_As = mk_bnf_t As' bnf_bd;
blanchet@48975
   682
    val bnf_wit_As = map (apsnd (mk_bnf_t As')) bnf_wits;
blanchet@48975
   683
blanchet@49463
   684
    val (((((((((((((((((((((((((fs, fs_copy), gs), hs), p), (x, x')), (y, y')), (z, z')), zs), As),
blanchet@49459
   685
      As_copy), Xs), B1s), B2s), f1s), f2s), e1s), e2s), p1s), p2s), bs), (Rs, Rs')), Rs_copy), Ss),
blanchet@49459
   686
      (Qs, Qs')), _) = lthy
blanchet@48975
   687
      |> mk_Frees "f" (map2 (curry (op -->)) As' Bs')
blanchet@48975
   688
      ||>> mk_Frees "f" (map2 (curry (op -->)) As' Bs')
blanchet@48975
   689
      ||>> mk_Frees "g" (map2 (curry (op -->)) Bs' Cs)
blanchet@48975
   690
      ||>> mk_Frees "h" (map2 (curry (op -->)) As' Ts)
blanchet@49463
   691
      ||>> yield_singleton (mk_Frees "p") CA'CB'
blanchet@48975
   692
      ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "x") CA'
blanchet@48975
   693
      ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "y") CB'
blanchet@48975
   694
      ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "z") CRs'
blanchet@48975
   695
      ||>> mk_Frees "z" As'
blanchet@48975
   696
      ||>> mk_Frees "A" (map HOLogic.mk_setT As')
blanchet@48975
   697
      ||>> mk_Frees "A" (map HOLogic.mk_setT As')
blanchet@48975
   698
      ||>> mk_Frees "A" (map HOLogic.mk_setT domTs)
blanchet@48975
   699
      ||>> mk_Frees "B1" (map HOLogic.mk_setT B1Ts)
blanchet@48975
   700
      ||>> mk_Frees "B2" (map HOLogic.mk_setT B2Ts)
blanchet@48975
   701
      ||>> mk_Frees "f1" (map2 (curry (op -->)) B1Ts ranTs)
blanchet@48975
   702
      ||>> mk_Frees "f2" (map2 (curry (op -->)) B2Ts ranTs)
blanchet@48975
   703
      ||>> mk_Frees "e1" (map2 (curry (op -->)) B1Ts ranTs')
blanchet@48975
   704
      ||>> mk_Frees "e2" (map2 (curry (op -->)) B2Ts ranTs'')
blanchet@48975
   705
      ||>> mk_Frees "p1" (map2 (curry (op -->)) domTs B1Ts)
blanchet@48975
   706
      ||>> mk_Frees "p2" (map2 (curry (op -->)) domTs B2Ts)
blanchet@48975
   707
      ||>> mk_Frees "b" As'
blanchet@49585
   708
      ||>> mk_Frees' "r" setRTs
blanchet@49585
   709
      ||>> mk_Frees "r" setRTs
blanchet@49585
   710
      ||>> mk_Frees "s" setRTsBsCs
blanchet@49537
   711
      ||>> mk_Frees' "R" QTs;
blanchet@48975
   712
blanchet@49459
   713
    (*Gr (in R1 .. Rn) (map fst .. fst)^-1 O Gr (in R1 .. Rn) (map snd .. snd)*)
blanchet@49463
   714
    val O_Gr =
blanchet@49459
   715
      let
blanchet@49459
   716
        val map1 = Term.list_comb (mk_bnf_map RTs As', map fst_const RTs);
blanchet@49459
   717
        val map2 = Term.list_comb (mk_bnf_map RTs Bs', map snd_const RTs);
blanchet@49459
   718
        val bnf_in = mk_in (map Free Rs') (map (mk_bnf_t RTs) bnf_sets) CRs';
blanchet@49459
   719
      in
blanchet@49459
   720
        mk_rel_comp (mk_converse (mk_Gr bnf_in map1), mk_Gr bnf_in map2)
blanchet@49459
   721
      end;
blanchet@49459
   722
blanchet@49463
   723
    fun mk_predicate_of_set x_name y_name t =
blanchet@49463
   724
      let
blanchet@49463
   725
        val (T, U) = HOLogic.dest_prodT (HOLogic.dest_setT (fastype_of t));
blanchet@49463
   726
        val x = Free (x_name, T);
blanchet@49463
   727
        val y = Free (y_name, U);
blanchet@49463
   728
      in fold_rev Term.lambda [x, y] (HOLogic.mk_mem (HOLogic.mk_prod (x, y), t)) end;
blanchet@49463
   729
blanchet@49537
   730
    val sQs =
blanchet@49537
   731
      map3 (fn Q => fn T => fn U =>
blanchet@49537
   732
          HOLogic.Collect_const (HOLogic.mk_prodT (T, U)) $ HOLogic.mk_split Q) Qs As' Bs';
blanchet@49537
   733
blanchet@49507
   734
    val rel_rhs = (case raw_rel_opt of
blanchet@49463
   735
        NONE =>
blanchet@49463
   736
        fold_rev absfree Qs' (mk_predicate_of_set (fst x') (fst y')
blanchet@49537
   737
          (Term.list_comb (fold_rev Term.absfree Rs' O_Gr, sQs)))
blanchet@49507
   738
      | SOME raw_rel => prep_term no_defs_lthy raw_rel);
blanchet@49463
   739
blanchet@49507
   740
    val rel_bind_def = (fn () => Binding.suffix_name ("_" ^ relN) b, rel_rhs);
blanchet@49463
   741
blanchet@49507
   742
    val ((bnf_rel_term, raw_rel_def), (lthy, lthy_old)) =
blanchet@49463
   743
      lthy
blanchet@49507
   744
      |> maybe_define (is_some raw_rel_opt) rel_bind_def
blanchet@49463
   745
      ||> `(maybe_restore lthy);
blanchet@49463
   746
blanchet@49463
   747
    val phi = Proof_Context.export_morphism lthy_old lthy;
blanchet@49507
   748
    val bnf_rel_def = Morphism.thm phi raw_rel_def;
blanchet@49507
   749
    val bnf_rel = Morphism.term phi bnf_rel_term;
blanchet@49463
   750
blanchet@49507
   751
    fun mk_bnf_rel QTs CA' CB' = normalize_rel lthy QTs CA' CB' bnf_rel;
blanchet@49463
   752
blanchet@49507
   753
    val rel = mk_bnf_rel QTs CA' CB';
blanchet@49463
   754
blanchet@49506
   755
    val srel_rhs =
blanchet@49463
   756
      fold_rev Term.absfree Rs' (HOLogic.Collect_const CA'CB' $
blanchet@49507
   757
        Term.lambda p (Term.list_comb (rel, map (mk_predicate_of_set (fst x') (fst y')) Rs) $
blanchet@49463
   758
        HOLogic.mk_fst p $ HOLogic.mk_snd p));
blanchet@49459
   759
blanchet@49506
   760
    val srel_bind_def = (fn () => Binding.suffix_name ("_" ^ srelN) b, srel_rhs);
blanchet@49459
   761
blanchet@49506
   762
    val ((bnf_srel_term, raw_srel_def), (lthy, lthy_old)) =
blanchet@49459
   763
      lthy
blanchet@49506
   764
      |> maybe_define false srel_bind_def
blanchet@49459
   765
      ||> `(maybe_restore lthy);
blanchet@49459
   766
blanchet@49459
   767
    val phi = Proof_Context.export_morphism lthy_old lthy;
blanchet@49506
   768
    val bnf_srel_def = Morphism.thm phi raw_srel_def;
blanchet@49506
   769
    val bnf_srel = Morphism.term phi bnf_srel_term;
blanchet@49459
   770
blanchet@49506
   771
    fun mk_bnf_srel setRTs CA' CB' = normalize_srel lthy setRTs CA' CB' bnf_srel;
blanchet@49459
   772
blanchet@49506
   773
    val srel = mk_bnf_srel setRTs CA' CB';
blanchet@49459
   774
blanchet@49460
   775
    val _ = case no_reflexive (raw_map_def :: raw_set_defs @ [raw_bd_def] @
blanchet@49507
   776
        raw_wit_defs @ [raw_rel_def, raw_srel_def]) of
blanchet@49459
   777
        [] => ()
blanchet@49459
   778
      | defs => Proof_Display.print_consts true lthy_old (K false)
blanchet@49459
   779
          (map (dest_Free o fst o Logic.dest_equals o prop_of) defs);
blanchet@49459
   780
blanchet@49458
   781
    val map_id_goal =
blanchet@48975
   782
      let
blanchet@49018
   783
        val bnf_map_app_id = Term.list_comb (bnf_map_AsAs, map HOLogic.id_const As');
blanchet@48975
   784
      in
blanchet@48975
   785
        HOLogic.mk_Trueprop
blanchet@48975
   786
          (HOLogic.mk_eq (bnf_map_app_id, HOLogic.id_const CA'))
blanchet@48975
   787
      end;
blanchet@48975
   788
blanchet@49458
   789
    val map_comp_goal =
blanchet@48975
   790
      let
blanchet@49018
   791
        val bnf_map_app_comp = Term.list_comb (bnf_map_AsCs, map2 (curry HOLogic.mk_comp) gs fs);
blanchet@48975
   792
        val comp_bnf_map_app = HOLogic.mk_comp
blanchet@48975
   793
          (Term.list_comb (bnf_map_BsCs, gs),
blanchet@48975
   794
           Term.list_comb (bnf_map_AsBs, fs));
blanchet@48975
   795
      in
blanchet@49123
   796
        fold_rev Logic.all (fs @ gs) (mk_Trueprop_eq (bnf_map_app_comp, comp_bnf_map_app))
blanchet@48975
   797
      end;
blanchet@48975
   798
blanchet@49458
   799
    val map_cong_goal =
blanchet@48975
   800
      let
blanchet@48975
   801
        fun mk_prem z set f f_copy =
blanchet@48975
   802
          Logic.all z (Logic.mk_implies
blanchet@48975
   803
            (HOLogic.mk_Trueprop (HOLogic.mk_mem (z, set $ x)),
blanchet@49123
   804
            mk_Trueprop_eq (f $ z, f_copy $ z)));
blanchet@48975
   805
        val prems = map4 mk_prem zs bnf_sets_As fs fs_copy;
blanchet@48975
   806
        val eq = HOLogic.mk_eq (Term.list_comb (bnf_map_AsBs, fs) $ x,
blanchet@48975
   807
          Term.list_comb (bnf_map_AsBs, fs_copy) $ x);
blanchet@48975
   808
      in
blanchet@48975
   809
        fold_rev Logic.all (x :: fs @ fs_copy)
blanchet@48975
   810
          (Logic.list_implies (prems, HOLogic.mk_Trueprop eq))
blanchet@48975
   811
      end;
blanchet@48975
   812
blanchet@49458
   813
    val set_naturals_goal =
blanchet@48975
   814
      let
blanchet@48975
   815
        fun mk_goal setA setB f =
blanchet@48975
   816
          let
blanchet@48975
   817
            val set_comp_map =
blanchet@48975
   818
              HOLogic.mk_comp (setB, Term.list_comb (bnf_map_AsBs, fs));
blanchet@48975
   819
            val image_comp_set = HOLogic.mk_comp (mk_image f, setA);
blanchet@48975
   820
          in
blanchet@49123
   821
            fold_rev Logic.all fs (mk_Trueprop_eq (set_comp_map, image_comp_set))
blanchet@48975
   822
          end;
blanchet@48975
   823
      in
blanchet@48975
   824
        map3 mk_goal bnf_sets_As bnf_sets_Bs fs
blanchet@48975
   825
      end;
blanchet@48975
   826
blanchet@49458
   827
    val card_order_bd_goal = HOLogic.mk_Trueprop (mk_card_order bnf_bd_As);
blanchet@48975
   828
blanchet@49458
   829
    val cinfinite_bd_goal = HOLogic.mk_Trueprop (mk_cinfinite bnf_bd_As);
blanchet@48975
   830
blanchet@49458
   831
    val set_bds_goal =
blanchet@48975
   832
      let
blanchet@48975
   833
        fun mk_goal set =
blanchet@48975
   834
          Logic.all x (HOLogic.mk_Trueprop (mk_ordLeq (mk_card_of (set $ x)) bnf_bd_As));
blanchet@48975
   835
      in
blanchet@48975
   836
        map mk_goal bnf_sets_As
blanchet@48975
   837
      end;
blanchet@48975
   838
blanchet@49458
   839
    val in_bd_goal =
blanchet@48975
   840
      let
blanchet@48975
   841
        val bd = mk_cexp
blanchet@48975
   842
          (if live = 0 then ctwo
blanchet@48975
   843
            else mk_csum (Library.foldr1 (uncurry mk_csum) (map mk_card_of As)) ctwo)
blanchet@48975
   844
          bnf_bd_As;
blanchet@48975
   845
      in
blanchet@48975
   846
        fold_rev Logic.all As
blanchet@48975
   847
          (HOLogic.mk_Trueprop (mk_ordLeq (mk_card_of (mk_in As bnf_sets_As CA')) bd))
blanchet@48975
   848
      end;
blanchet@48975
   849
blanchet@49458
   850
    val map_wpull_goal =
blanchet@48975
   851
      let
blanchet@48975
   852
        val prems = map HOLogic.mk_Trueprop
blanchet@48975
   853
          (map8 mk_wpull Xs B1s B2s f1s f2s (replicate live NONE) p1s p2s);
blanchet@48975
   854
        val CX = mk_bnf_T domTs CA;
blanchet@48975
   855
        val CB1 = mk_bnf_T B1Ts CA;
blanchet@48975
   856
        val CB2 = mk_bnf_T B2Ts CA;
blanchet@48975
   857
        val bnf_sets_CX = map2 (normalize_set (map (mk_bnf_T domTs) CA_params)) domTs bnf_sets;
blanchet@48975
   858
        val bnf_sets_CB1 = map2 (normalize_set (map (mk_bnf_T B1Ts) CA_params)) B1Ts bnf_sets;
blanchet@48975
   859
        val bnf_sets_CB2 = map2 (normalize_set (map (mk_bnf_T B2Ts) CA_params)) B2Ts bnf_sets;
blanchet@48975
   860
        val bnf_map_app_f1 = Term.list_comb (mk_bnf_map B1Ts ranTs, f1s);
blanchet@48975
   861
        val bnf_map_app_f2 = Term.list_comb (mk_bnf_map B2Ts ranTs, f2s);
blanchet@48975
   862
        val bnf_map_app_p1 = Term.list_comb (mk_bnf_map domTs B1Ts, p1s);
blanchet@48975
   863
        val bnf_map_app_p2 = Term.list_comb (mk_bnf_map domTs B2Ts, p2s);
blanchet@48975
   864
blanchet@48975
   865
        val map_wpull = mk_wpull (mk_in Xs bnf_sets_CX CX)
blanchet@48975
   866
          (mk_in B1s bnf_sets_CB1 CB1) (mk_in B2s bnf_sets_CB2 CB2)
blanchet@48975
   867
          bnf_map_app_f1 bnf_map_app_f2 NONE bnf_map_app_p1 bnf_map_app_p2;
blanchet@48975
   868
      in
blanchet@48975
   869
        fold_rev Logic.all (Xs @ B1s @ B2s @ f1s @ f2s @ p1s @ p2s)
blanchet@48975
   870
          (Logic.list_implies (prems, HOLogic.mk_Trueprop map_wpull))
blanchet@48975
   871
      end;
blanchet@48975
   872
blanchet@49506
   873
    val srel_O_Gr_goal = fold_rev Logic.all Rs (mk_Trueprop_eq (Term.list_comb (srel, Rs), O_Gr));
blanchet@49453
   874
blanchet@49460
   875
    val goals = zip_axioms map_id_goal map_comp_goal map_cong_goal set_naturals_goal
blanchet@49506
   876
      card_order_bd_goal cinfinite_bd_goal set_bds_goal in_bd_goal map_wpull_goal srel_O_Gr_goal;
blanchet@48975
   877
blanchet@48975
   878
    fun mk_wit_goals (I, wit) =
blanchet@48975
   879
      let
blanchet@48975
   880
        val xs = map (nth bs) I;
blanchet@48975
   881
        fun wit_goal i =
blanchet@48975
   882
          let
blanchet@48975
   883
            val z = nth zs i;
blanchet@48975
   884
            val set_wit = nth bnf_sets_As i $ Term.list_comb (wit, xs);
blanchet@48975
   885
            val concl = HOLogic.mk_Trueprop
blanchet@48975
   886
              (if member (op =) I i then HOLogic.mk_eq (z, nth bs i)
blanchet@48975
   887
              else @{term False});
blanchet@48975
   888
          in
blanchet@48975
   889
            fold_rev Logic.all (z :: xs)
blanchet@48975
   890
              (Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_mem (z, set_wit)), concl))
blanchet@48975
   891
          end;
blanchet@48975
   892
      in
blanchet@48975
   893
        map wit_goal (0 upto live - 1)
blanchet@48975
   894
      end;
blanchet@48975
   895
blanchet@48975
   896
    val wit_goalss = map mk_wit_goals bnf_wit_As;
blanchet@48975
   897
blanchet@48975
   898
    fun after_qed thms lthy =
blanchet@48975
   899
      let
blanchet@48975
   900
        val (axioms, wit_thms) = apfst (mk_axioms live) (chop (length goals) thms);
blanchet@48975
   901
traytel@49109
   902
        val bd_Card_order = #bd_card_order axioms RS @{thm conjunct2[OF card_order_on_Card_order]};
blanchet@48975
   903
        val bd_Cinfinite = @{thm conjI} OF [#bd_cinfinite axioms, bd_Card_order];
blanchet@48975
   904
        val bd_Cnotzero = bd_Cinfinite RS @{thm Cinfinite_Cnotzero};
blanchet@48975
   905
blanchet@48975
   906
        fun mk_collect_set_natural () =
blanchet@48975
   907
          let
blanchet@48975
   908
            val defT = mk_bnf_T Ts CA --> HOLogic.mk_setT T;
blanchet@48975
   909
            val collect_map = HOLogic.mk_comp
blanchet@48975
   910
              (mk_collect (map (mk_bnf_t Ts) bnf_sets) defT,
blanchet@48975
   911
              Term.list_comb (mk_bnf_map As' Ts, hs));
blanchet@48975
   912
            val image_collect = mk_collect
blanchet@48975
   913
              (map2 (fn h => fn set => HOLogic.mk_comp (mk_image h, set)) hs bnf_sets_As)
blanchet@48975
   914
              defT;
blanchet@48975
   915
            (*collect {set1 ... setm} o map f1 ... fm = collect {f1` o set1 ... fm` o setm}*)
blanchet@49123
   916
            val goal = fold_rev Logic.all hs (mk_Trueprop_eq (collect_map, image_collect));
blanchet@48975
   917
          in
blanchet@48975
   918
            Skip_Proof.prove lthy [] [] goal
blanchet@48975
   919
              (fn {context = ctxt, ...} => mk_collect_set_natural_tac ctxt (#set_natural axioms))
traytel@49109
   920
            |> Thm.close_derivation
blanchet@48975
   921
          end;
blanchet@48975
   922
blanchet@49538
   923
        val collect_set_natural = Lazy.lazy mk_collect_set_natural;
blanchet@48975
   924
blanchet@48975
   925
        fun mk_in_mono () =
blanchet@48975
   926
          let
blanchet@48975
   927
            val prems_mono = map2 (HOLogic.mk_Trueprop oo mk_subset) As As_copy;
blanchet@49458
   928
            val in_mono_goal =
blanchet@48975
   929
              fold_rev Logic.all (As @ As_copy)
blanchet@48975
   930
                (Logic.list_implies (prems_mono, HOLogic.mk_Trueprop
blanchet@48975
   931
                  (mk_subset (mk_in As bnf_sets_As CA') (mk_in As_copy bnf_sets_As CA'))));
blanchet@48975
   932
          in
blanchet@49458
   933
            Skip_Proof.prove lthy [] [] in_mono_goal (K (mk_in_mono_tac live))
traytel@49109
   934
            |> Thm.close_derivation
blanchet@48975
   935
          end;
blanchet@48975
   936
blanchet@49538
   937
        val in_mono = Lazy.lazy mk_in_mono;
blanchet@48975
   938
blanchet@48975
   939
        fun mk_in_cong () =
blanchet@48975
   940
          let
blanchet@48975
   941
            val prems_cong = map2 (HOLogic.mk_Trueprop oo curry HOLogic.mk_eq) As As_copy;
blanchet@49458
   942
            val in_cong_goal =
blanchet@48975
   943
              fold_rev Logic.all (As @ As_copy)
blanchet@48975
   944
                (Logic.list_implies (prems_cong, HOLogic.mk_Trueprop
blanchet@48975
   945
                  (HOLogic.mk_eq (mk_in As bnf_sets_As CA', mk_in As_copy bnf_sets_As CA'))));
blanchet@48975
   946
          in
blanchet@49458
   947
            Skip_Proof.prove lthy [] [] in_cong_goal (K ((TRY o hyp_subst_tac THEN' rtac refl) 1))
traytel@49109
   948
            |> Thm.close_derivation
blanchet@48975
   949
          end;
blanchet@48975
   950
blanchet@49538
   951
        val in_cong = Lazy.lazy mk_in_cong;
blanchet@48975
   952
blanchet@49538
   953
        val map_id' = Lazy.lazy (fn () => mk_id' (#map_id axioms));
blanchet@49538
   954
        val map_comp' = Lazy.lazy (fn () => mk_comp' (#map_comp axioms));
blanchet@48975
   955
blanchet@48975
   956
        val set_natural' =
blanchet@49538
   957
          map (fn thm => Lazy.lazy (fn () => mk_set_natural' thm)) (#set_natural axioms);
blanchet@48975
   958
blanchet@48975
   959
        fun mk_map_wppull () =
blanchet@48975
   960
          let
blanchet@48975
   961
            val prems = if live = 0 then [] else
blanchet@48975
   962
              [HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
blanchet@48975
   963
                (map8 mk_wpull Xs B1s B2s f1s f2s (map SOME (e1s ~~ e2s)) p1s p2s))];
blanchet@48975
   964
            val CX = mk_bnf_T domTs CA;
blanchet@48975
   965
            val CB1 = mk_bnf_T B1Ts CA;
blanchet@48975
   966
            val CB2 = mk_bnf_T B2Ts CA;
blanchet@48975
   967
            val bnf_sets_CX =
blanchet@48975
   968
              map2 (normalize_set (map (mk_bnf_T domTs) CA_params)) domTs bnf_sets;
blanchet@48975
   969
            val bnf_sets_CB1 =
blanchet@48975
   970
              map2 (normalize_set (map (mk_bnf_T B1Ts) CA_params)) B1Ts bnf_sets;
blanchet@48975
   971
            val bnf_sets_CB2 =
blanchet@48975
   972
              map2 (normalize_set (map (mk_bnf_T B2Ts) CA_params)) B2Ts bnf_sets;
blanchet@48975
   973
            val bnf_map_app_f1 = Term.list_comb (mk_bnf_map B1Ts ranTs, f1s);
blanchet@48975
   974
            val bnf_map_app_f2 = Term.list_comb (mk_bnf_map B2Ts ranTs, f2s);
blanchet@48975
   975
            val bnf_map_app_e1 = Term.list_comb (mk_bnf_map B1Ts ranTs', e1s);
blanchet@48975
   976
            val bnf_map_app_e2 = Term.list_comb (mk_bnf_map B2Ts ranTs'', e2s);
blanchet@48975
   977
            val bnf_map_app_p1 = Term.list_comb (mk_bnf_map domTs B1Ts, p1s);
blanchet@48975
   978
            val bnf_map_app_p2 = Term.list_comb (mk_bnf_map domTs B2Ts, p2s);
blanchet@48975
   979
blanchet@48975
   980
            val concl = mk_wpull (mk_in Xs bnf_sets_CX CX)
blanchet@48975
   981
              (mk_in B1s bnf_sets_CB1 CB1) (mk_in B2s bnf_sets_CB2 CB2)
blanchet@48975
   982
              bnf_map_app_f1 bnf_map_app_f2 (SOME (bnf_map_app_e1, bnf_map_app_e2))
blanchet@48975
   983
              bnf_map_app_p1 bnf_map_app_p2;
blanchet@48975
   984
blanchet@48975
   985
            val goal =
blanchet@48975
   986
              fold_rev Logic.all (Xs @ B1s @ B2s @ f1s @ f2s @ e1s @ e2s @ p1s @ p2s)
blanchet@48975
   987
                (Logic.list_implies (prems, HOLogic.mk_Trueprop concl))
blanchet@48975
   988
          in
blanchet@48975
   989
            Skip_Proof.prove lthy [] [] goal
blanchet@48975
   990
              (fn _ => mk_map_wppull_tac (#map_id axioms) (#map_cong axioms)
blanchet@48975
   991
                (#map_wpull axioms) (Lazy.force map_comp') (map Lazy.force set_natural'))
traytel@49109
   992
            |> Thm.close_derivation
blanchet@48975
   993
          end;
blanchet@48975
   994
blanchet@49506
   995
        val srel_O_Grs = no_refl [#srel_O_Gr axioms];
blanchet@49453
   996
blanchet@49538
   997
        val map_wppull = Lazy.lazy mk_map_wppull;
blanchet@48975
   998
blanchet@49506
   999
        fun mk_srel_Gr () =
blanchet@48975
  1000
          let
blanchet@49506
  1001
            val lhs = Term.list_comb (srel, map2 mk_Gr As fs);
blanchet@48975
  1002
            val rhs = mk_Gr (mk_in As bnf_sets_As CA') (Term.list_comb (bnf_map_AsBs, fs));
blanchet@49123
  1003
            val goal = fold_rev Logic.all (As @ fs) (mk_Trueprop_eq (lhs, rhs));
blanchet@48975
  1004
          in
blanchet@48975
  1005
            Skip_Proof.prove lthy [] [] goal
blanchet@49506
  1006
              (mk_srel_Gr_tac srel_O_Grs (#map_id axioms) (#map_cong axioms) (Lazy.force map_id')
blanchet@48975
  1007
                (Lazy.force map_comp') (map Lazy.force set_natural'))
traytel@49109
  1008
            |> Thm.close_derivation
blanchet@48975
  1009
          end;
blanchet@48975
  1010
blanchet@49538
  1011
        val srel_Gr = Lazy.lazy mk_srel_Gr;
blanchet@48975
  1012
blanchet@49506
  1013
        fun mk_srel_prems f = map2 (HOLogic.mk_Trueprop oo f) Rs Rs_copy
blanchet@49506
  1014
        fun mk_srel_concl f = HOLogic.mk_Trueprop
blanchet@49506
  1015
          (f (Term.list_comb (srel, Rs), Term.list_comb (srel, Rs_copy)));
blanchet@48975
  1016
blanchet@49506
  1017
        fun mk_srel_mono () =
blanchet@48975
  1018
          let
blanchet@49506
  1019
            val mono_prems = mk_srel_prems mk_subset;
blanchet@49506
  1020
            val mono_concl = mk_srel_concl (uncurry mk_subset);
blanchet@48975
  1021
          in
blanchet@48975
  1022
            Skip_Proof.prove lthy [] []
blanchet@48975
  1023
              (fold_rev Logic.all (Rs @ Rs_copy) (Logic.list_implies (mono_prems, mono_concl)))
blanchet@49506
  1024
              (mk_srel_mono_tac srel_O_Grs (Lazy.force in_mono))
traytel@49109
  1025
            |> Thm.close_derivation
blanchet@48975
  1026
          end;
blanchet@48975
  1027
blanchet@49506
  1028
        fun mk_srel_cong () =
blanchet@48975
  1029
          let
blanchet@49506
  1030
            val cong_prems = mk_srel_prems (curry HOLogic.mk_eq);
blanchet@49506
  1031
            val cong_concl = mk_srel_concl HOLogic.mk_eq;
blanchet@48975
  1032
          in
blanchet@48975
  1033
            Skip_Proof.prove lthy [] []
blanchet@48975
  1034
              (fold_rev Logic.all (Rs @ Rs_copy) (Logic.list_implies (cong_prems, cong_concl)))
blanchet@48975
  1035
              (fn _ => (TRY o hyp_subst_tac THEN' rtac refl) 1)
traytel@49109
  1036
            |> Thm.close_derivation
blanchet@48975
  1037
          end;
blanchet@48975
  1038
blanchet@49538
  1039
        val srel_mono = Lazy.lazy mk_srel_mono;
blanchet@49538
  1040
        val srel_cong = Lazy.lazy mk_srel_cong;
blanchet@48975
  1041
blanchet@49506
  1042
        fun mk_srel_Id () =
blanchet@49515
  1043
          let val srelAsAs = mk_bnf_srel self_setRTs CA' CA' in
blanchet@48975
  1044
            Skip_Proof.prove lthy [] []
blanchet@48975
  1045
              (HOLogic.mk_Trueprop
blanchet@49515
  1046
                (HOLogic.mk_eq (Term.list_comb (srelAsAs, map Id_const As'), Id_const CA')))
blanchet@49506
  1047
              (mk_srel_Id_tac live (Lazy.force srel_Gr) (#map_id axioms))
traytel@49109
  1048
            |> Thm.close_derivation
blanchet@48975
  1049
          end;
blanchet@48975
  1050
blanchet@49538
  1051
        val srel_Id = Lazy.lazy mk_srel_Id;
blanchet@48975
  1052
blanchet@49506
  1053
        fun mk_srel_converse () =
blanchet@48975
  1054
          let
blanchet@49515
  1055
            val srelBsAs = mk_bnf_srel setRT's CB' CA';
blanchet@49515
  1056
            val lhs = Term.list_comb (srelBsAs, map mk_converse Rs);
blanchet@49506
  1057
            val rhs = mk_converse (Term.list_comb (srel, Rs));
blanchet@48975
  1058
            val le_goal = fold_rev Logic.all Rs (HOLogic.mk_Trueprop (mk_subset lhs rhs));
blanchet@48975
  1059
            val le_thm = Skip_Proof.prove lthy [] [] le_goal
blanchet@49506
  1060
              (mk_srel_converse_le_tac srel_O_Grs (Lazy.force srel_Id) (#map_cong axioms)
blanchet@48975
  1061
                (Lazy.force map_comp') (map Lazy.force set_natural'))
traytel@49109
  1062
              |> Thm.close_derivation
blanchet@49123
  1063
            val goal = fold_rev Logic.all Rs (mk_Trueprop_eq (lhs, rhs));
blanchet@48975
  1064
          in
blanchet@49506
  1065
            Skip_Proof.prove lthy [] [] goal (fn _ => mk_srel_converse_tac le_thm)
traytel@49109
  1066
            |> Thm.close_derivation
blanchet@48975
  1067
          end;
blanchet@48975
  1068
blanchet@49538
  1069
        val srel_converse = Lazy.lazy mk_srel_converse;
blanchet@48975
  1070
blanchet@49506
  1071
        fun mk_srel_O () =
blanchet@48975
  1072
          let
blanchet@49515
  1073
            val srelAsCs = mk_bnf_srel setRTsAsCs CA' CC';
blanchet@49515
  1074
            val srelBsCs = mk_bnf_srel setRTsBsCs CB' CC';
blanchet@49515
  1075
            val lhs = Term.list_comb (srelAsCs, map2 (curry mk_rel_comp) Rs Ss);
blanchet@49515
  1076
            val rhs = mk_rel_comp (Term.list_comb (srel, Rs), Term.list_comb (srelBsCs, Ss));
blanchet@49123
  1077
            val goal = fold_rev Logic.all (Rs @ Ss) (mk_Trueprop_eq (lhs, rhs));
blanchet@48975
  1078
          in
blanchet@48975
  1079
            Skip_Proof.prove lthy [] [] goal
blanchet@49506
  1080
              (mk_srel_O_tac srel_O_Grs (Lazy.force srel_Id) (#map_cong axioms)
blanchet@48975
  1081
                (Lazy.force map_wppull) (Lazy.force map_comp') (map Lazy.force set_natural'))
traytel@49109
  1082
            |> Thm.close_derivation
blanchet@48975
  1083
          end;
blanchet@48975
  1084
blanchet@49538
  1085
        val srel_O = Lazy.lazy mk_srel_O;
blanchet@48975
  1086
blanchet@49506
  1087
        fun mk_in_srel () =
blanchet@48975
  1088
          let
blanchet@48975
  1089
            val bnf_in = mk_in Rs (map (mk_bnf_t RTs) bnf_sets) CRs';
blanchet@48975
  1090
            val map1 = Term.list_comb (mk_bnf_map RTs As', map fst_const RTs);
blanchet@48975
  1091
            val map2 = Term.list_comb (mk_bnf_map RTs Bs', map snd_const RTs);
blanchet@48975
  1092
            val map_fst_eq = HOLogic.mk_eq (map1 $ z, x);
blanchet@48975
  1093
            val map_snd_eq = HOLogic.mk_eq (map2 $ z, y);
blanchet@49506
  1094
            val lhs = HOLogic.mk_mem (HOLogic.mk_prod (x, y), Term.list_comb (srel, Rs));
blanchet@48975
  1095
            val rhs =
blanchet@48975
  1096
              HOLogic.mk_exists (fst z', snd z', HOLogic.mk_conj (HOLogic.mk_mem (z, bnf_in),
blanchet@48975
  1097
                HOLogic.mk_conj (map_fst_eq, map_snd_eq)));
blanchet@48975
  1098
            val goal =
blanchet@49123
  1099
              fold_rev Logic.all (x :: y :: Rs) (mk_Trueprop_eq (lhs, rhs));
blanchet@48975
  1100
          in
blanchet@49506
  1101
            Skip_Proof.prove lthy [] [] goal (mk_in_srel_tac srel_O_Grs (length bnf_sets))
traytel@49109
  1102
            |> Thm.close_derivation
blanchet@48975
  1103
          end;
blanchet@48975
  1104
blanchet@49538
  1105
        val in_srel = Lazy.lazy mk_in_srel;
blanchet@48975
  1106
blanchet@49537
  1107
        val eqset_imp_iff_pair = @{thm eqset_imp_iff_pair};
blanchet@49537
  1108
        val mem_Collect_etc = @{thms mem_Collect_eq fst_conv snd_conv prod.cases};
blanchet@49537
  1109
blanchet@49537
  1110
        fun mk_rel_as_srel () =
blanchet@49537
  1111
          unfold_thms lthy mem_Collect_etc
blanchet@49537
  1112
            (funpow live (fn thm => thm RS @{thm fun_cong_pair}) (bnf_srel_def RS meta_eq_to_obj_eq)
blanchet@49537
  1113
               RS eqset_imp_iff_pair RS sym)
blanchet@49537
  1114
          |> Drule.zero_var_indexes;
blanchet@49537
  1115
blanchet@49538
  1116
        val rel_as_srel = Lazy.lazy mk_rel_as_srel;
blanchet@49537
  1117
blanchet@49537
  1118
        fun mk_rel_flip () =
blanchet@49537
  1119
          let
blanchet@49537
  1120
            val srel_converse_thm = Lazy.force srel_converse;
blanchet@49585
  1121
            val cts = map (SOME o certify lthy) sQs;
blanchet@49585
  1122
            val srel_converse_thm' = cterm_instantiate_pos cts srel_converse_thm;
blanchet@49537
  1123
          in
blanchet@49537
  1124
            unfold_thms lthy (bnf_srel_def :: @{thm converse_iff} :: mem_Collect_etc)
blanchet@49537
  1125
              (srel_converse_thm' RS eqset_imp_iff_pair)
blanchet@49585
  1126
            |> Drule.zero_var_indexes
blanchet@49585
  1127
            |> Thm.generalize (map (fst o dest_TFree) (As' @ Bs'), map fst Qs') 1
blanchet@49537
  1128
          end;
blanchet@49537
  1129
blanchet@49538
  1130
        val rel_flip = Lazy.lazy mk_rel_flip;
blanchet@49537
  1131
blanchet@49507
  1132
        val defs = mk_defs bnf_map_def bnf_set_defs bnf_rel_def bnf_srel_def;
blanchet@48975
  1133
blanchet@49506
  1134
        val facts = mk_facts bd_Card_order bd_Cinfinite bd_Cnotzero collect_set_natural in_cong
blanchet@49537
  1135
          in_mono in_srel map_comp' map_id' map_wppull rel_as_srel rel_flip set_natural' srel_cong
blanchet@49537
  1136
          srel_mono srel_Id srel_Gr srel_converse srel_O;
blanchet@48975
  1137
blanchet@48975
  1138
        val wits = map2 mk_witness bnf_wits wit_thms;
blanchet@48975
  1139
blanchet@49507
  1140
        val bnf_rel =
blanchet@49507
  1141
          Term.subst_atomic_types ((Ds ~~ deads) @ (As' ~~ alphas) @ (Bs' ~~ betas)) rel;
blanchet@49506
  1142
        val bnf_srel =
blanchet@49506
  1143
          Term.subst_atomic_types ((Ds ~~ deads) @ (As' ~~ alphas) @ (Bs' ~~ betas)) srel;
blanchet@48975
  1144
blanchet@48975
  1145
        val bnf = mk_bnf b CA live alphas betas dead deads bnf_map bnf_sets bnf_bd axioms defs facts
blanchet@49507
  1146
          wits bnf_rel bnf_srel;
blanchet@48975
  1147
      in
blanchet@48975
  1148
        (bnf, lthy
blanchet@49538
  1149
          |> (if fact_policy = Note_All then
blanchet@48975
  1150
                let
blanchet@48975
  1151
                  val witNs = if length wits = 1 then [witN] else map mk_witN (1 upto length wits);
traytel@49109
  1152
                  val notes =
traytel@49109
  1153
                    [(bd_card_orderN, [#bd_card_order axioms]),
traytel@49109
  1154
                    (bd_cinfiniteN, [#bd_cinfinite axioms]),
traytel@49109
  1155
                    (bd_Card_orderN, [#bd_Card_order facts]),
traytel@49109
  1156
                    (bd_CinfiniteN, [#bd_Cinfinite facts]),
traytel@49109
  1157
                    (bd_CnotzeroN, [#bd_Cnotzero facts]),
traytel@49109
  1158
                    (collect_set_naturalN, [Lazy.force (#collect_set_natural facts)]),
traytel@49109
  1159
                    (in_bdN, [#in_bd axioms]),
traytel@49109
  1160
                    (in_monoN, [Lazy.force (#in_mono facts)]),
blanchet@49506
  1161
                    (in_srelN, [Lazy.force (#in_srel facts)]),
traytel@49109
  1162
                    (map_compN, [#map_comp axioms]),
traytel@49109
  1163
                    (map_idN, [#map_id axioms]),
traytel@49109
  1164
                    (map_wpullN, [#map_wpull axioms]),
traytel@49109
  1165
                    (set_naturalN, #set_natural axioms),
traytel@49109
  1166
                    (set_bdN, #set_bd axioms)] @
traytel@49109
  1167
                    map2 pair witNs wit_thms
traytel@49109
  1168
                    |> map (fn (thmN, thms) =>
traytel@49109
  1169
                      ((qualify (Binding.qualify true (Binding.name_of b) (Binding.name thmN)), []),
traytel@49109
  1170
                      [(thms, [])]));
blanchet@48975
  1171
                in
traytel@49109
  1172
                  Local_Theory.notes notes #> snd
blanchet@48975
  1173
                end
blanchet@48975
  1174
              else
blanchet@48975
  1175
                I)
blanchet@49538
  1176
          |> (if fact_policy <> Dont_Note then
traytel@49109
  1177
                let
traytel@49109
  1178
                  val notes =
blanchet@49506
  1179
                    [(map_comp'N, [Lazy.force (#map_comp' facts)]),
blanchet@49506
  1180
                    (map_congN, [#map_cong axioms]),
traytel@49109
  1181
                    (map_id'N, [Lazy.force (#map_id' facts)]),
blanchet@49537
  1182
                    (rel_as_srelN, [Lazy.force (#rel_as_srel facts)]),
blanchet@49537
  1183
                    (rel_flipN, [Lazy.force (#rel_flip facts)]),
blanchet@49506
  1184
                    (set_natural'N, map Lazy.force (#set_natural' facts)),
blanchet@49506
  1185
                    (srel_O_GrN, srel_O_Grs),
blanchet@49506
  1186
                    (srel_IdN, [Lazy.force (#srel_Id facts)]),
blanchet@49506
  1187
                    (srel_GrN, [Lazy.force (#srel_Gr facts)]),
blanchet@49506
  1188
                    (srel_converseN, [Lazy.force (#srel_converse facts)]),
blanchet@49506
  1189
                    (srel_monoN, [Lazy.force (#srel_mono facts)]),
blanchet@49506
  1190
                    (srel_ON, [Lazy.force (#srel_O facts)])]
blanchet@49460
  1191
                    |> filter_out (null o #2)
traytel@49109
  1192
                    |> map (fn (thmN, thms) =>
traytel@49109
  1193
                      ((qualify (Binding.qualify true (Binding.name_of b) (Binding.name thmN)), []),
traytel@49109
  1194
                      [(thms, [])]));
traytel@49109
  1195
                in
traytel@49109
  1196
                  Local_Theory.notes notes #> snd
traytel@49109
  1197
                end
blanchet@48975
  1198
              else
blanchet@48975
  1199
                I))
blanchet@48975
  1200
      end;
blanchet@49459
  1201
blanchet@49459
  1202
    val one_step_defs =
blanchet@49507
  1203
      no_reflexive (bnf_map_def :: bnf_bd_def :: bnf_set_defs @ bnf_wit_defs @ [bnf_rel_def,
blanchet@49506
  1204
        bnf_srel_def]);
blanchet@48975
  1205
  in
blanchet@49459
  1206
    (key, goals, wit_goalss, after_qed, lthy, one_step_defs)
blanchet@48975
  1207
  end;
blanchet@48975
  1208
traytel@49434
  1209
fun register_bnf key (bnf, lthy) =
traytel@49434
  1210
  (bnf, Local_Theory.declaration {syntax = false, pervasive = true}
traytel@49434
  1211
    (fn phi => Data.map (Symtab.update_new (key, morph_bnf phi bnf))) lthy);
traytel@49434
  1212
blanchet@49456
  1213
(* TODO: Once the invariant "nwits > 0" holds, remove "mk_conjunction_balanced'" and "rtac TrueI"
blanchet@49456
  1214
   below *)
blanchet@49456
  1215
fun mk_conjunction_balanced' [] = @{prop True}
blanchet@49456
  1216
  | mk_conjunction_balanced' ts = Logic.mk_conjunction_balanced ts;
blanchet@49456
  1217
blanchet@49018
  1218
fun bnf_def const_policy fact_policy qualify tacs wit_tac Ds =
blanchet@49463
  1219
  (fn (_, goals, wit_goalss, after_qed, lthy, one_step_defs) =>
blanchet@48975
  1220
  let
blanchet@49456
  1221
    val wits_tac =
blanchet@49456
  1222
      K (TRYALL Goal.conjunction_tac) THEN' K (TRYALL (rtac TrueI)) THEN'
blanchet@49504
  1223
      mk_unfold_thms_then_tac lthy one_step_defs wit_tac;
blanchet@49456
  1224
    val wit_goals = map mk_conjunction_balanced' wit_goalss;
blanchet@48975
  1225
    val wit_thms =
blanchet@49456
  1226
      Skip_Proof.prove lthy [] [] (mk_conjunction_balanced' wit_goals) wits_tac
blanchet@48975
  1227
      |> Conjunction.elim_balanced (length wit_goals)
blanchet@48975
  1228
      |> map2 (Conjunction.elim_balanced o length) wit_goalss
blanchet@49456
  1229
      |> map (map (Thm.close_derivation o Thm.forall_elim_vars 0));
blanchet@48975
  1230
  in
blanchet@49111
  1231
    map2 (Thm.close_derivation oo Skip_Proof.prove lthy [] [])
blanchet@49504
  1232
      goals (map (mk_unfold_thms_then_tac lthy one_step_defs) tacs)
blanchet@48975
  1233
    |> (fn thms => after_qed (map single thms @ wit_thms) lthy)
blanchet@49279
  1234
  end) oo prepare_def const_policy fact_policy qualify (K I) Ds;
blanchet@48975
  1235
traytel@49434
  1236
val bnf_def_cmd = (fn (key, goals, wit_goals, after_qed, lthy, defs) =>
blanchet@48975
  1237
  Proof.unfolding ([[(defs, [])]])
traytel@49434
  1238
    (Proof.theorem NONE (snd o register_bnf key oo after_qed)
blanchet@48975
  1239
      (map (single o rpair []) goals @ map (map (rpair [])) wit_goals) lthy)) oo
blanchet@49538
  1240
  prepare_def Do_Inline (user_policy Note_Some) I Syntax.read_term NONE;
blanchet@48975
  1241
blanchet@48975
  1242
fun print_bnfs ctxt =
blanchet@48975
  1243
  let
blanchet@48975
  1244
    fun pretty_set sets i = Pretty.block
blanchet@48975
  1245
      [Pretty.str (mk_setN (i + 1) ^ ":"), Pretty.brk 1,
blanchet@48975
  1246
          Pretty.quote (Syntax.pretty_term ctxt (nth sets i))];
blanchet@48975
  1247
blanchet@48975
  1248
    fun pretty_bnf (key, BNF {T = T, map = map, sets = sets, bd = bd,
blanchet@48975
  1249
      live = live, lives = lives, dead = dead, deads = deads, ...}) =
blanchet@48975
  1250
      Pretty.big_list
blanchet@48975
  1251
        (Pretty.string_of (Pretty.block [Pretty.str key, Pretty.str ":", Pretty.brk 1,
blanchet@48975
  1252
          Pretty.quote (Syntax.pretty_typ ctxt T)]))
blanchet@48975
  1253
        ([Pretty.block [Pretty.str "live:", Pretty.brk 1, Pretty.str (string_of_int live),
blanchet@48975
  1254
            Pretty.brk 3, Pretty.list "[" "]" (List.map (Syntax.pretty_typ ctxt) lives)],
blanchet@48975
  1255
          Pretty.block [Pretty.str "dead:", Pretty.brk 1, Pretty.str (string_of_int dead),
blanchet@48975
  1256
            Pretty.brk 3, Pretty.list "[" "]" (List.map (Syntax.pretty_typ ctxt) deads)],
blanchet@48975
  1257
          Pretty.block [Pretty.str (mapN ^ ":"), Pretty.brk 1,
blanchet@48975
  1258
            Pretty.quote (Syntax.pretty_term ctxt map)]] @
blanchet@48975
  1259
          List.map (pretty_set sets) (0 upto length sets - 1) @
blanchet@48975
  1260
          [Pretty.block [Pretty.str (bdN ^ ":"), Pretty.brk 1,
blanchet@48975
  1261
            Pretty.quote (Syntax.pretty_term ctxt bd)]]);
blanchet@48975
  1262
  in
blanchet@48975
  1263
    Pretty.big_list "BNFs:" (map pretty_bnf (Symtab.dest (Data.get (Context.Proof ctxt))))
blanchet@48975
  1264
    |> Pretty.writeln
blanchet@48975
  1265
  end;
blanchet@48975
  1266
blanchet@48975
  1267
val _ =
blanchet@48975
  1268
  Outer_Syntax.improper_command @{command_spec "print_bnfs"} "print all BNFs"
blanchet@48975
  1269
    (Scan.succeed (Toplevel.keep (print_bnfs o Toplevel.context_of)));
blanchet@48975
  1270
blanchet@48975
  1271
val _ =
blanchet@48975
  1272
  Outer_Syntax.local_theory_to_proof @{command_spec "bnf_def"} "define a BNF for an existing type"
traytel@49434
  1273
    ((parse_opt_binding_colon -- Parse.term --
blanchet@49277
  1274
       (@{keyword "["} |-- Parse.list Parse.term --| @{keyword "]"}) -- Parse.term --
blanchet@49459
  1275
       (@{keyword "["} |-- Parse.list Parse.term --| @{keyword "]"}) -- Scan.option Parse.term)
blanchet@49459
  1276
       >> bnf_def_cmd);
blanchet@48975
  1277
blanchet@48975
  1278
end;