src/HOL/Tools/BNF/bnf_fp_util.ML
author traytel
Thu Apr 07 17:56:22 2016 +0200 (2016-04-07)
changeset 62905 52c5a25e0c96
parent 62863 e0b894bba6ff
child 62907 9ad0bac25a84
permissions -rw-r--r--
derive (co)rec uniformly from (un)fold
blanchet@55061
     1
(*  Title:      HOL/Tools/BNF/bnf_fp_util.ML
blanchet@48975
     2
    Author:     Dmitriy Traytel, TU Muenchen
blanchet@51823
     3
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@57668
     4
    Author:     Martin Desharnais, TU Muenchen
blanchet@58256
     5
    Author:     Stefan Berghofer, TU Muenchen
blanchet@57668
     6
    Copyright   2012, 2013, 2014
blanchet@48975
     7
blanchet@49389
     8
Shared library for the datatype and codatatype constructions.
blanchet@48975
     9
*)
blanchet@48975
    10
blanchet@51850
    11
signature BNF_FP_UTIL =
blanchet@48975
    12
sig
blanchet@49591
    13
  type fp_result =
blanchet@51859
    14
    {Ts: typ list,
blanchet@51859
    15
     bnfs: BNF_Def.bnf list,
traytel@62684
    16
     pre_bnfs: BNF_Def.bnf list,
traytel@62684
    17
     absT_infos: BNF_Comp.absT_info list,
blanchet@51839
    18
     ctors: term list,
blanchet@51819
    19
     dtors: term list,
blanchet@62721
    20
     xtor_un_folds_legacy: term list,
blanchet@55868
    21
     xtor_co_recs: term list,
traytel@53106
    22
     xtor_co_induct: thm,
blanchet@51819
    23
     dtor_ctors: thm list,
blanchet@51819
    24
     ctor_dtors: thm list,
blanchet@51819
    25
     ctor_injects: thm list,
traytel@53203
    26
     dtor_injects: thm list,
desharna@58583
    27
     xtor_maps: thm list,
traytel@62863
    28
     xtor_map_unique: thm,
desharna@58584
    29
     xtor_setss: thm list list,
desharna@58585
    30
     xtor_rels: thm list,
blanchet@62721
    31
     xtor_un_fold_thms_legacy: thm list,
blanchet@55868
    32
     xtor_co_rec_thms: thm list,
traytel@62863
    33
     xtor_un_fold_unique_legacy: thm,
traytel@62863
    34
     xtor_co_rec_unique: thm,
blanchet@62721
    35
     xtor_un_fold_o_maps_legacy: thm list,
desharna@58578
    36
     xtor_co_rec_o_maps: thm list,
blanchet@62721
    37
     xtor_un_fold_transfers_legacy: thm list,
blanchet@59856
    38
     xtor_co_rec_transfers: thm list,
desharna@58579
    39
     xtor_rel_co_induct: thm,
blanchet@59856
    40
     dtor_set_inducts: thm list}
blanchet@49591
    41
blanchet@51823
    42
  val morph_fp_result: morphism -> fp_result -> fp_result
blanchet@51823
    43
traytel@53143
    44
  val time: Proof.context -> Timer.real_timer -> string -> Timer.real_timer
blanchet@48975
    45
blanchet@55851
    46
  val fixpoint: ('a * 'a -> bool) -> ('a list -> 'a list) -> 'a list -> 'a list
blanchet@55851
    47
blanchet@48975
    48
  val IITN: string
blanchet@48975
    49
  val LevN: string
blanchet@48975
    50
  val algN: string
blanchet@48975
    51
  val behN: string
blanchet@48975
    52
  val bisN: string
blanchet@48975
    53
  val carTN: string
blanchet@49338
    54
  val caseN: string
blanchet@48975
    55
  val coN: string
blanchet@48975
    56
  val coinductN: string
blanchet@57983
    57
  val coinduct_strongN: string
blanchet@48975
    58
  val corecN: string
blanchet@57983
    59
  val corec_discN: string
blanchet@57983
    60
  val corec_disc_iffN: string
blanchet@49501
    61
  val ctorN: string
blanchet@49501
    62
  val ctor_dtorN: string
blanchet@49501
    63
  val ctor_exhaustN: string
blanchet@49501
    64
  val ctor_induct2N: string
blanchet@49501
    65
  val ctor_inductN: string
blanchet@49501
    66
  val ctor_injectN: string
blanchet@49504
    67
  val ctor_foldN: string
traytel@52913
    68
  val ctor_fold_o_mapN: string
traytel@52913
    69
  val ctor_fold_transferN: string
blanchet@49504
    70
  val ctor_fold_uniqueN: string
blanchet@49541
    71
  val ctor_mapN: string
blanchet@49543
    72
  val ctor_map_uniqueN: string
blanchet@49501
    73
  val ctor_recN: string
traytel@52913
    74
  val ctor_rec_o_mapN: string
desharna@58444
    75
  val ctor_rec_transferN: string
traytel@51739
    76
  val ctor_rec_uniqueN: string
blanchet@49518
    77
  val ctor_relN: string
blanchet@55901
    78
  val ctor_rel_inductN: string
blanchet@49544
    79
  val ctor_set_inclN: string
blanchet@49544
    80
  val ctor_set_set_inclN: string
blanchet@49501
    81
  val dtorN: string
blanchet@49582
    82
  val dtor_coinductN: string
blanchet@49501
    83
  val dtor_corecN: string
traytel@52913
    84
  val dtor_corec_o_mapN: string
desharna@58445
    85
  val dtor_corec_transferN: string
traytel@51739
    86
  val dtor_corec_uniqueN: string
blanchet@49518
    87
  val dtor_ctorN: string
blanchet@49501
    88
  val dtor_exhaustN: string
blanchet@49501
    89
  val dtor_injectN: string
blanchet@49545
    90
  val dtor_mapN: string
blanchet@49581
    91
  val dtor_map_coinductN: string
blanchet@57983
    92
  val dtor_map_coinduct_strongN: string
blanchet@49543
    93
  val dtor_map_uniqueN: string
blanchet@49545
    94
  val dtor_relN: string
blanchet@55901
    95
  val dtor_rel_coinductN: string
blanchet@49544
    96
  val dtor_set_inclN: string
blanchet@49544
    97
  val dtor_set_set_inclN: string
blanchet@57983
    98
  val dtor_coinduct_strongN: string
blanchet@49516
    99
  val dtor_unfoldN: string
traytel@52913
   100
  val dtor_unfold_o_mapN: string
traytel@52913
   101
  val dtor_unfold_transferN: string
blanchet@49516
   102
  val dtor_unfold_uniqueN: string
blanchet@49020
   103
  val exhaustN: string
traytel@56113
   104
  val colN: string
blanchet@48975
   105
  val inductN: string
blanchet@49019
   106
  val injectN: string
blanchet@48975
   107
  val isNodeN: string
blanchet@48975
   108
  val lsbisN: string
blanchet@49594
   109
  val mapN: string
blanchet@48975
   110
  val map_uniqueN: string
blanchet@48975
   111
  val min_algN: string
blanchet@48975
   112
  val morN: string
blanchet@49020
   113
  val nchotomyN: string
blanchet@48975
   114
  val recN: string
desharna@57525
   115
  val rel_casesN: string
traytel@51918
   116
  val rel_coinductN: string
traytel@51918
   117
  val rel_inductN: string
blanchet@49592
   118
  val rel_injectN: string
desharna@57493
   119
  val rel_introsN: string
blanchet@49592
   120
  val rel_distinctN: string
desharna@57563
   121
  val rel_selN: string
blanchet@48975
   122
  val rvN: string
blanchet@57983
   123
  val corec_selN: string
blanchet@48975
   124
  val set_inclN: string
blanchet@48975
   125
  val set_set_inclN: string
blanchet@53694
   126
  val setN: string
blanchet@49438
   127
  val simpsN: string
blanchet@48975
   128
  val strTN: string
blanchet@48975
   129
  val str_initN: string
blanchet@48975
   130
  val sum_bdN: string
blanchet@48975
   131
  val sum_bdTN: string
blanchet@48975
   132
  val uniqueN: string
blanchet@48975
   133
blanchet@49585
   134
  (* TODO: Don't index set facts. Isabelle packages traditionally generate uniform names. *)
blanchet@49584
   135
  val mk_ctor_setN: int -> string
blanchet@49584
   136
  val mk_dtor_setN: int -> string
blanchet@49542
   137
  val mk_dtor_set_inductN: int -> string
blanchet@48975
   138
  val mk_set_inductN: int -> string
blanchet@48975
   139
blanchet@55575
   140
  val co_prefix: BNF_Util.fp_kind -> string
blanchet@51863
   141
blanchet@48975
   142
  val split_conj_thm: thm -> thm list
blanchet@48975
   143
  val split_conj_prems: int -> thm -> thm
blanchet@48975
   144
blanchet@49255
   145
  val mk_sumTN: typ list -> typ
blanchet@58159
   146
  val mk_sumTN_balanced: typ list -> typ
blanchet@55969
   147
  val mk_tupleT_balanced: typ list -> typ
blanchet@55966
   148
  val mk_sumprodT_balanced: typ list list -> typ
blanchet@49255
   149
blanchet@54923
   150
  val mk_proj: typ -> int -> int -> term
blanchet@54923
   151
traytel@53032
   152
  val mk_convol: term * term -> term
traytel@58443
   153
  val mk_rel_prod: term -> term -> term
traytel@58443
   154
  val mk_rel_sum: term -> term -> term
blanchet@49368
   155
blanchet@49121
   156
  val Inl_const: typ -> typ -> term
blanchet@49121
   157
  val Inr_const: typ -> typ -> term
blanchet@55969
   158
  val mk_tuple_balanced: term list -> term
blanchet@55969
   159
  val mk_tuple1_balanced: typ list -> term list -> term
blanchet@59873
   160
  val abs_curried_balanced: typ list -> term -> term
blanchet@62124
   161
  val mk_tupled_fun: term -> term -> term list -> term
blanchet@49121
   162
blanchet@55414
   163
  val mk_case_sum: term * term -> term
blanchet@55414
   164
  val mk_case_sumN: term list -> term
blanchet@55968
   165
  val mk_case_absumprod: typ -> term -> term list -> term list list -> term list list -> term
traytel@55803
   166
blanchet@49255
   167
  val mk_Inl: typ -> term -> term
blanchet@49255
   168
  val mk_Inr: typ -> term -> term
blanchet@59673
   169
  val mk_sumprod_balanced: typ -> int -> int -> term list -> term
traytel@55803
   170
  val mk_absumprod: typ -> term -> int -> int -> term list -> term
blanchet@49121
   171
blanchet@49255
   172
  val dest_sumT: typ -> typ * typ
blanchet@58159
   173
  val dest_sumTN_balanced: int -> typ -> typ list
blanchet@58159
   174
  val dest_tupleT_balanced: int -> typ -> typ list
traytel@55803
   175
  val dest_absumprodT: typ -> typ -> int -> int list -> typ -> typ list list
blanchet@49176
   176
blanchet@53202
   177
  val If_const: typ -> term
blanchet@53202
   178
blanchet@48975
   179
  val mk_Field: term -> term
blanchet@49275
   180
  val mk_If: term -> term -> term -> term
blanchet@48975
   181
  val mk_union: term * term -> term
blanchet@48975
   182
traytel@55803
   183
  val mk_absumprodE: thm -> int list -> thm
traytel@55803
   184
blanchet@55414
   185
  val mk_sum_caseN: int -> int -> thm
blanchet@55414
   186
  val mk_sum_caseN_balanced: int -> int -> thm
blanchet@49125
   187
blanchet@55851
   188
  val mk_sum_Cinfinite: thm list -> thm
blanchet@55851
   189
  val mk_sum_card_order: thm list -> thm
blanchet@48975
   190
traytel@62905
   191
  val force_typ: Proof.context -> typ -> term -> term
traytel@62905
   192
desharna@58579
   193
  val mk_xtor_rel_co_induct_thm: BNF_Util.fp_kind -> term list -> term list -> term list ->
blanchet@55575
   194
    term list -> term list -> term list -> term list -> term list ->
traytel@52505
   195
    ({prems: thm list, context: Proof.context} -> tactic) -> Proof.context -> thm
traytel@62827
   196
  val mk_xtor_co_iter_transfer_thms: BNF_Util.fp_kind -> term list -> term list -> term list ->
traytel@58443
   197
    term list -> term list -> term list -> term list ->
traytel@58443
   198
    ({prems: thm list, context: Proof.context} -> tactic) -> Proof.context -> thm list
traytel@62827
   199
  val mk_xtor_co_iter_o_map_thms: BNF_Util.fp_kind -> bool -> int -> thm -> thm list -> thm list ->
traytel@52913
   200
    thm list -> thm list -> thm list
traytel@62905
   201
  val derive_xtor_co_recs: BNF_Util.fp_kind -> binding list -> (typ list -> typ list) ->
traytel@62905
   202
    (typ list list * typ list) -> BNF_Def.bnf list -> term list -> term list ->
traytel@62905
   203
    thm -> thm list -> thm list -> thm list -> thm list -> local_theory ->
traytel@62905
   204
    (term list * (thm list * thm * thm list * thm list)) * local_theory
traytel@52505
   205
blanchet@62722
   206
  val fixpoint_bnf: (binding -> binding) ->
blanchet@62722
   207
      (binding list -> (string * sort) list -> typ list * typ list list -> BNF_Def.bnf list ->
blanchet@62722
   208
      BNF_Comp.absT_info list -> local_theory -> 'a) ->
blanchet@55701
   209
    binding list -> (string * sort) list -> (string * sort) list -> ((string * sort) * typ) list ->
blanchet@62720
   210
    BNF_Comp.comp_cache -> local_theory ->
blanchet@62720
   211
    ((BNF_Def.bnf list * BNF_Comp.absT_info list) * BNF_Comp.comp_cache) * 'a
blanchet@58256
   212
  val fp_antiquote_setup: binding -> theory -> theory
blanchet@48975
   213
end;
blanchet@48975
   214
blanchet@51850
   215
structure BNF_FP_Util : BNF_FP_UTIL =
blanchet@48975
   216
struct
blanchet@48975
   217
blanchet@56650
   218
open Ctr_Sugar
blanchet@48975
   219
open BNF_Comp
blanchet@48975
   220
open BNF_Def
blanchet@48975
   221
open BNF_Util
traytel@62905
   222
open BNF_FP_Util_Tactics
blanchet@48975
   223
blanchet@49591
   224
type fp_result =
blanchet@51859
   225
  {Ts: typ list,
blanchet@59856
   226
   bnfs: bnf list,
traytel@62684
   227
   pre_bnfs: BNF_Def.bnf list,
traytel@62684
   228
   absT_infos: BNF_Comp.absT_info list,
blanchet@51839
   229
   ctors: term list,
blanchet@51819
   230
   dtors: term list,
blanchet@62721
   231
   xtor_un_folds_legacy: term list,
blanchet@55868
   232
   xtor_co_recs: term list,
traytel@53106
   233
   xtor_co_induct: thm,
blanchet@51819
   234
   dtor_ctors: thm list,
blanchet@51819
   235
   ctor_dtors: thm list,
blanchet@51819
   236
   ctor_injects: thm list,
traytel@53203
   237
   dtor_injects: thm list,
desharna@58583
   238
   xtor_maps: thm list,
traytel@62863
   239
   xtor_map_unique: thm,
desharna@58584
   240
   xtor_setss: thm list list,
desharna@58585
   241
   xtor_rels: thm list,
blanchet@62721
   242
   xtor_un_fold_thms_legacy: thm list,
blanchet@55868
   243
   xtor_co_rec_thms: thm list,
traytel@62863
   244
   xtor_un_fold_unique_legacy: thm,
traytel@62863
   245
   xtor_co_rec_unique: thm,
blanchet@62721
   246
   xtor_un_fold_o_maps_legacy: thm list,
desharna@58578
   247
   xtor_co_rec_o_maps: thm list,
blanchet@62721
   248
   xtor_un_fold_transfers_legacy: thm list,
blanchet@59856
   249
   xtor_co_rec_transfers: thm list,
desharna@58579
   250
   xtor_rel_co_induct: thm,
blanchet@59856
   251
   dtor_set_inducts: thm list};
blanchet@49591
   252
blanchet@62721
   253
fun morph_fp_result phi {Ts, bnfs, pre_bnfs, absT_infos, ctors, dtors, xtor_un_folds_legacy,
blanchet@62721
   254
    xtor_co_recs, xtor_co_induct, dtor_ctors, ctor_dtors, ctor_injects, dtor_injects, xtor_maps,
traytel@62863
   255
    xtor_map_unique, xtor_setss, xtor_rels, xtor_un_fold_thms_legacy, xtor_co_rec_thms,
traytel@62863
   256
    xtor_un_fold_unique_legacy, xtor_co_rec_unique, xtor_un_fold_o_maps_legacy,
blanchet@62721
   257
    xtor_co_rec_o_maps, xtor_un_fold_transfers_legacy, xtor_co_rec_transfers, xtor_rel_co_induct,
blanchet@62721
   258
    dtor_set_inducts} =
blanchet@51859
   259
  {Ts = map (Morphism.typ phi) Ts,
blanchet@51859
   260
   bnfs = map (morph_bnf phi) bnfs,
traytel@62684
   261
   pre_bnfs = map (morph_bnf phi) pre_bnfs,
traytel@62684
   262
   absT_infos = map (morph_absT_info phi) absT_infos,
blanchet@51839
   263
   ctors = map (Morphism.term phi) ctors,
blanchet@51823
   264
   dtors = map (Morphism.term phi) dtors,
blanchet@62721
   265
   xtor_un_folds_legacy = map (Morphism.term phi) xtor_un_folds_legacy,
blanchet@55868
   266
   xtor_co_recs = map (Morphism.term phi) xtor_co_recs,
traytel@53106
   267
   xtor_co_induct = Morphism.thm phi xtor_co_induct,
blanchet@51823
   268
   dtor_ctors = map (Morphism.thm phi) dtor_ctors,
blanchet@51823
   269
   ctor_dtors = map (Morphism.thm phi) ctor_dtors,
blanchet@51823
   270
   ctor_injects = map (Morphism.thm phi) ctor_injects,
traytel@53203
   271
   dtor_injects = map (Morphism.thm phi) dtor_injects,
desharna@58583
   272
   xtor_maps = map (Morphism.thm phi) xtor_maps,
traytel@62863
   273
   xtor_map_unique = Morphism.thm phi xtor_map_unique,
desharna@58584
   274
   xtor_setss = map (map (Morphism.thm phi)) xtor_setss,
desharna@58585
   275
   xtor_rels = map (Morphism.thm phi) xtor_rels,
blanchet@62721
   276
   xtor_un_fold_thms_legacy = map (Morphism.thm phi) xtor_un_fold_thms_legacy,
blanchet@55868
   277
   xtor_co_rec_thms = map (Morphism.thm phi) xtor_co_rec_thms,
traytel@62863
   278
   xtor_un_fold_unique_legacy = Morphism.thm phi xtor_un_fold_unique_legacy,
traytel@62863
   279
   xtor_co_rec_unique = Morphism.thm phi xtor_co_rec_unique,
blanchet@62721
   280
   xtor_un_fold_o_maps_legacy = map (Morphism.thm phi) xtor_un_fold_o_maps_legacy,
desharna@58578
   281
   xtor_co_rec_o_maps = map (Morphism.thm phi) xtor_co_rec_o_maps,
blanchet@62721
   282
   xtor_un_fold_transfers_legacy = map (Morphism.thm phi) xtor_un_fold_transfers_legacy,
blanchet@59856
   283
   xtor_co_rec_transfers = map (Morphism.thm phi) xtor_co_rec_transfers,
desharna@58579
   284
   xtor_rel_co_induct = Morphism.thm phi xtor_rel_co_induct,
blanchet@59856
   285
   dtor_set_inducts = map (Morphism.thm phi) dtor_set_inducts};
blanchet@51823
   286
traytel@53143
   287
fun time ctxt timer msg = (if Config.get ctxt bnf_timing
traytel@55811
   288
  then warning (msg ^ ": " ^ string_of_int (Time.toMilliseconds (Timer.checkRealTimer timer)) ^
blanchet@62685
   289
    " ms")
blanchet@48975
   290
  else (); Timer.startRealTimer ());
blanchet@48975
   291
blanchet@49223
   292
val preN = "pre_"
blanchet@49223
   293
val rawN = "raw_"
blanchet@49218
   294
blanchet@48975
   295
val coN = "co"
blanchet@49504
   296
val unN = "un"
blanchet@48975
   297
val algN = "alg"
blanchet@48975
   298
val IITN = "IITN"
blanchet@49504
   299
val foldN = "fold"
blanchet@49504
   300
val unfoldN = unN ^ foldN
blanchet@62125
   301
val uniqueN = "unique"
blanchet@62125
   302
val transferN = "transfer"
blanchet@49438
   303
val simpsN = "simps"
blanchet@49501
   304
val ctorN = "ctor"
blanchet@49501
   305
val dtorN = "dtor"
blanchet@49504
   306
val ctor_foldN = ctorN ^ "_" ^ foldN
blanchet@49504
   307
val dtor_unfoldN = dtorN ^ "_" ^ unfoldN
blanchet@62125
   308
val ctor_fold_uniqueN = ctor_foldN ^ "_" ^ uniqueN
traytel@52913
   309
val ctor_fold_o_mapN = ctor_foldN ^ "_o_" ^ mapN
blanchet@62125
   310
val dtor_unfold_uniqueN = dtor_unfoldN ^ "_" ^ uniqueN
traytel@52913
   311
val dtor_unfold_o_mapN = dtor_unfoldN ^ "_o_" ^ mapN
blanchet@62125
   312
val ctor_fold_transferN = ctor_foldN ^ "_" ^ transferN
blanchet@62125
   313
val dtor_unfold_transferN = dtor_unfoldN ^ "_" ^ transferN
blanchet@49541
   314
val ctor_mapN = ctorN ^ "_" ^ mapN
blanchet@49541
   315
val dtor_mapN = dtorN ^ "_" ^ mapN
blanchet@62125
   316
val map_uniqueN = mapN ^ "_" ^ uniqueN
blanchet@49543
   317
val ctor_map_uniqueN = ctorN ^ "_" ^ map_uniqueN
blanchet@49543
   318
val dtor_map_uniqueN = dtorN ^ "_" ^ map_uniqueN
blanchet@48975
   319
val min_algN = "min_alg"
blanchet@48975
   320
val morN = "mor"
blanchet@48975
   321
val bisN = "bis"
blanchet@48975
   322
val lsbisN = "lsbis"
blanchet@48975
   323
val sum_bdTN = "sbdT"
blanchet@48975
   324
val sum_bdN = "sbd"
blanchet@48975
   325
val carTN = "carT"
blanchet@48975
   326
val strTN = "strT"
blanchet@48975
   327
val isNodeN = "isNode"
blanchet@48975
   328
val LevN = "Lev"
blanchet@48975
   329
val rvN = "recover"
blanchet@48975
   330
val behN = "beh"
blanchet@53694
   331
val setN = "set"
blanchet@49584
   332
val mk_ctor_setN = prefix (ctorN ^ "_") o mk_setN
blanchet@49584
   333
val mk_dtor_setN = prefix (dtorN ^ "_") o mk_setN
blanchet@48975
   334
fun mk_set_inductN i = mk_setN i ^ "_induct"
blanchet@49542
   335
val mk_dtor_set_inductN = prefix (dtorN ^ "_") o mk_set_inductN
blanchet@48975
   336
blanchet@48975
   337
val str_initN = "str_init"
blanchet@48975
   338
val recN = "rec"
blanchet@48975
   339
val corecN = coN ^ recN
blanchet@49501
   340
val ctor_recN = ctorN ^ "_" ^ recN
traytel@52913
   341
val ctor_rec_o_mapN = ctor_recN ^ "_o_" ^ mapN
blanchet@62125
   342
val ctor_rec_transferN = ctor_recN ^ "_" ^ transferN
blanchet@62125
   343
val ctor_rec_uniqueN = ctor_recN ^ "_" ^ uniqueN
blanchet@49501
   344
val dtor_corecN = dtorN ^ "_" ^ corecN
traytel@52913
   345
val dtor_corec_o_mapN = dtor_corecN ^ "_o_" ^ mapN
blanchet@62125
   346
val dtor_corec_transferN = dtor_corecN ^ "_" ^ transferN
blanchet@62125
   347
val dtor_corec_uniqueN = dtor_corecN ^ "_" ^ uniqueN
blanchet@48975
   348
blanchet@49501
   349
val ctor_dtorN = ctorN ^ "_" ^ dtorN
blanchet@49501
   350
val dtor_ctorN = dtorN ^ "_" ^ ctorN
blanchet@49020
   351
val nchotomyN = "nchotomy"
blanchet@49019
   352
val injectN = "inject"
blanchet@49020
   353
val exhaustN = "exhaust"
blanchet@49585
   354
val ctor_injectN = ctorN ^ "_" ^ injectN
blanchet@49585
   355
val ctor_exhaustN = ctorN ^ "_" ^ exhaustN
blanchet@49585
   356
val dtor_injectN = dtorN ^ "_" ^ injectN
blanchet@49585
   357
val dtor_exhaustN = dtorN ^ "_" ^ exhaustN
blanchet@49545
   358
val ctor_relN = ctorN ^ "_" ^ relN
blanchet@49545
   359
val dtor_relN = dtorN ^ "_" ^ relN
blanchet@48975
   360
val inductN = "induct"
blanchet@48975
   361
val coinductN = coN ^ inductN
blanchet@49501
   362
val ctor_inductN = ctorN ^ "_" ^ inductN
blanchet@49501
   363
val ctor_induct2N = ctor_inductN ^ "2"
blanchet@49581
   364
val dtor_map_coinductN = dtor_mapN ^ "_" ^ coinductN
blanchet@49582
   365
val dtor_coinductN = dtorN ^ "_" ^ coinductN
blanchet@57983
   366
val coinduct_strongN = coinductN ^ "_strong"
blanchet@57983
   367
val dtor_map_coinduct_strongN = dtor_mapN ^ "_" ^ coinduct_strongN
blanchet@57983
   368
val dtor_coinduct_strongN = dtorN ^ "_" ^ coinduct_strongN
traytel@56113
   369
val colN = "col"
blanchet@48975
   370
val set_inclN = "set_incl"
blanchet@49544
   371
val ctor_set_inclN = ctorN ^ "_" ^ set_inclN
blanchet@49544
   372
val dtor_set_inclN = dtorN ^ "_" ^ set_inclN
blanchet@48975
   373
val set_set_inclN = "set_set_incl"
blanchet@49544
   374
val ctor_set_set_inclN = ctorN ^ "_" ^ set_set_inclN
blanchet@49544
   375
val dtor_set_set_inclN = dtorN ^ "_" ^ set_set_inclN
blanchet@48975
   376
blanchet@49338
   377
val caseN = "case"
blanchet@49342
   378
val discN = "disc"
blanchet@57983
   379
val corec_discN = corecN ^ "_" ^ discN
blanchet@49594
   380
val iffN = "_iff"
blanchet@57983
   381
val corec_disc_iffN = corec_discN ^ iffN
blanchet@49592
   382
val distinctN = "distinct"
blanchet@49592
   383
val rel_distinctN = relN ^ "_" ^ distinctN
blanchet@49592
   384
val injectN = "inject"
desharna@57525
   385
val rel_casesN = relN ^ "_cases"
blanchet@49592
   386
val rel_injectN = relN ^ "_" ^ injectN
desharna@57493
   387
val rel_introsN = relN ^ "_intros"
traytel@51918
   388
val rel_coinductN = relN ^ "_" ^ coinductN
desharna@57563
   389
val rel_selN = relN ^ "_sel"
blanchet@55901
   390
val dtor_rel_coinductN = dtorN ^ "_" ^ rel_coinductN
traytel@51918
   391
val rel_inductN = relN ^ "_" ^ inductN
blanchet@55901
   392
val ctor_rel_inductN = ctorN ^ "_" ^ rel_inductN
blanchet@49342
   393
val selN = "sel"
blanchet@57983
   394
val corec_selN = corecN ^ "_" ^ selN
blanchet@49338
   395
blanchet@55966
   396
fun co_prefix fp = case_fp fp "" "co";
blanchet@51863
   397
blanchet@49264
   398
fun dest_sumT (Type (@{type_name sum}, [T, T'])) = (T, T');
blanchet@49264
   399
blanchet@49264
   400
val dest_sumTN_balanced = Balanced_Tree.dest dest_sumT;
blanchet@49264
   401
blanchet@55966
   402
fun dest_tupleT_balanced 0 @{typ unit} = []
blanchet@55966
   403
  | dest_tupleT_balanced n T = Balanced_Tree.dest HOLogic.dest_prodT n T;
blanchet@49264
   404
blanchet@55966
   405
fun dest_absumprodT absT repT n ms =
blanchet@55966
   406
  map2 dest_tupleT_balanced ms o dest_sumTN_balanced n o mk_repT absT repT;
traytel@55803
   407
blanchet@49264
   408
val mk_sumTN = Library.foldr1 mk_sumT;
blanchet@49264
   409
val mk_sumTN_balanced = Balanced_Tree.make mk_sumT;
blanchet@49255
   410
blanchet@55966
   411
fun mk_tupleT_balanced [] = HOLogic.unitT
blanchet@55966
   412
  | mk_tupleT_balanced Ts = Balanced_Tree.make HOLogic.mk_prodT Ts;
blanchet@55966
   413
blanchet@55966
   414
val mk_sumprodT_balanced = mk_sumTN_balanced o map mk_tupleT_balanced;
blanchet@55966
   415
blanchet@54923
   416
fun mk_proj T n k =
blanchet@54923
   417
  let val (binders, _) = strip_typeN n T in
blanchet@54923
   418
    fold_rev (fn T => fn t => Abs (Name.uu, T, t)) binders (Bound (n - k - 1))
blanchet@54923
   419
  end;
blanchet@54923
   420
traytel@53032
   421
fun mk_convol (f, g) =
traytel@53032
   422
  let
traytel@53032
   423
    val (fU, fTU) = `range_type (fastype_of f);
traytel@53032
   424
    val ((gT, gU), gTU) = `dest_funT (fastype_of g);
traytel@53032
   425
    val convolT = fTU --> gTU --> gT --> HOLogic.mk_prodT (fU, gU);
traytel@53032
   426
  in Const (@{const_name convol}, convolT) $ f $ g end;
blanchet@49368
   427
traytel@58443
   428
fun mk_rel_prod R S =
traytel@58443
   429
  let
traytel@58443
   430
    val ((A1, A2), RT) = `dest_pred2T (fastype_of R);
traytel@58443
   431
    val ((B1, B2), ST) = `dest_pred2T (fastype_of S);
traytel@58443
   432
    val rel_prodT = RT --> ST --> mk_pred2T (HOLogic.mk_prodT (A1, B1)) (HOLogic.mk_prodT (A2, B2));
traytel@58443
   433
  in Const (@{const_name rel_prod}, rel_prodT) $ R $ S end;
traytel@58443
   434
traytel@58443
   435
fun mk_rel_sum R S =
traytel@58443
   436
  let
traytel@58443
   437
    val ((A1, A2), RT) = `dest_pred2T (fastype_of R);
traytel@58443
   438
    val ((B1, B2), ST) = `dest_pred2T (fastype_of S);
traytel@58443
   439
    val rel_sumT = RT --> ST --> mk_pred2T (mk_sumT (A1, B1)) (mk_sumT (A2, B2));
traytel@58443
   440
  in Const (@{const_name rel_sum}, rel_sumT) $ R $ S end;
traytel@58443
   441
blanchet@49121
   442
fun Inl_const LT RT = Const (@{const_name Inl}, LT --> mk_sumT (LT, RT));
blanchet@49255
   443
fun mk_Inl RT t = Inl_const (fastype_of t) RT $ t;
blanchet@49121
   444
blanchet@49121
   445
fun Inr_const LT RT = Const (@{const_name Inr}, RT --> mk_sumT (LT, RT));
blanchet@49255
   446
fun mk_Inr LT t = Inr_const LT (fastype_of t) $ t;
blanchet@49121
   447
blanchet@55969
   448
fun mk_prod1 bound_Ts (t, u) =
blanchet@55969
   449
  HOLogic.pair_const (fastype_of1 (bound_Ts, t)) (fastype_of1 (bound_Ts, u)) $ t $ u;
blanchet@55969
   450
blanchet@55969
   451
fun mk_tuple1_balanced _ [] = HOLogic.unit
blanchet@55969
   452
  | mk_tuple1_balanced bound_Ts ts = Balanced_Tree.make (mk_prod1 bound_Ts) ts;
blanchet@55969
   453
blanchet@55969
   454
val mk_tuple_balanced = mk_tuple1_balanced [];
blanchet@55966
   455
blanchet@59873
   456
fun abs_curried_balanced Ts t =
blanchet@59873
   457
  t $ mk_tuple1_balanced (List.rev Ts) (map Bound (length Ts - 1 downto 0))
blanchet@59873
   458
  |> fold_rev (Term.abs o pair Name.uu) Ts;
blanchet@59873
   459
blanchet@59673
   460
fun mk_sumprod_balanced T n k ts = Sum_Tree.mk_inj T n k (mk_tuple_balanced ts);
blanchet@59673
   461
traytel@55803
   462
fun mk_absumprod absT abs0 n k ts =
traytel@55803
   463
  let val abs = mk_abs absT abs0;
blanchet@59673
   464
  in abs $ mk_sumprod_balanced (domain_type (fastype_of abs)) n k ts end;
traytel@55803
   465
blanchet@55414
   466
fun mk_case_sum (f, g) =
blanchet@49129
   467
  let
blanchet@55968
   468
    val (fT, T') = dest_funT (fastype_of f);
blanchet@55968
   469
    val (gT, _) = dest_funT (fastype_of g);
blanchet@49129
   470
  in
blanchet@55968
   471
    Sum_Tree.mk_sumcase fT gT T' f g
blanchet@49129
   472
  end;
blanchet@49129
   473
blanchet@55414
   474
val mk_case_sumN = Library.foldr1 mk_case_sum;
blanchet@55414
   475
val mk_case_sumN_balanced = Balanced_Tree.make mk_case_sum;
blanchet@49176
   476
traytel@55803
   477
fun mk_tupled_fun f x xs =
traytel@55803
   478
  if xs = [x] then f else HOLogic.tupled_lambda x (Term.list_comb (f, xs));
traytel@55803
   479
blanchet@55968
   480
fun mk_case_absumprod absT rep fs xss xss' =
blanchet@62124
   481
  HOLogic.mk_comp (mk_case_sumN_balanced
blanchet@62124
   482
    (@{map 3} mk_tupled_fun fs (map mk_tuple_balanced xss) xss'), mk_rep absT rep);
traytel@55803
   483
blanchet@53202
   484
fun If_const T = Const (@{const_name If}, HOLogic.boolT --> T --> T --> T);
blanchet@53202
   485
fun mk_If p t f = let val T = fastype_of t in If_const T $ p $ t $ f end;
blanchet@49275
   486
blanchet@48975
   487
fun mk_Field r =
blanchet@48975
   488
  let val T = fst (dest_relT (fastype_of r));
blanchet@48975
   489
  in Const (@{const_name Field}, mk_relT (T, T) --> HOLogic.mk_setT T) $ r end;
blanchet@48975
   490
blanchet@48975
   491
val mk_union = HOLogic.mk_binop @{const_name sup};
blanchet@48975
   492
blanchet@48975
   493
(*dangerous; use with monotonic, converging functions only!*)
blanchet@48975
   494
fun fixpoint eq f X = if subset eq (f X, X) then X else fixpoint eq f (f X);
blanchet@48975
   495
blanchet@48975
   496
(* stolen from "~~/src/HOL/Tools/Datatype/datatype_aux.ML" *)
blanchet@48975
   497
fun split_conj_thm th =
blanchet@49119
   498
  ((th RS conjunct1) :: split_conj_thm (th RS conjunct2)) handle THM _ => [th];
blanchet@48975
   499
blanchet@48975
   500
fun split_conj_prems limit th =
blanchet@48975
   501
  let
blanchet@48975
   502
    fun split n i th =
blanchet@48975
   503
      if i = n then th else split n (i + 1) (conjI RSN (i, th)) handle THM _ => th;
blanchet@48975
   504
  in split limit 1 th end;
blanchet@48975
   505
blanchet@49335
   506
fun mk_obj_sumEN_balanced n =
blanchet@49335
   507
  Balanced_Tree.make (fn (thm1, thm2) => thm1 RSN (1, thm2 RSN (2, @{thm obj_sumE_f})))
blanchet@49335
   508
    (replicate n asm_rl);
blanchet@49335
   509
blanchet@55966
   510
fun mk_tupled_allIN_balanced 0 = @{thm unit_all_impI}
blanchet@55966
   511
  | mk_tupled_allIN_balanced n =
blanchet@55966
   512
    let
blanchet@55966
   513
      val (tfrees, _) = BNF_Util.mk_TFrees n @{context};
blanchet@55966
   514
      val T = mk_tupleT_balanced tfrees;
blanchet@55966
   515
    in
blanchet@55966
   516
      @{thm asm_rl[of "ALL x. P x --> Q x" for P Q]}
wenzelm@60801
   517
      |> Thm.instantiate' [SOME (Thm.ctyp_of @{context} T)] []
blanchet@55966
   518
      |> Raw_Simplifier.rewrite_goals_rule @{context} @{thms split_paired_All[THEN eq_reflection]}
blanchet@55966
   519
      |> (fn thm => impI RS funpow n (fn th => allI RS th) thm)
blanchet@55966
   520
      |> Thm.varifyT_global
blanchet@55966
   521
    end;
blanchet@49335
   522
traytel@55803
   523
fun mk_absumprodE type_definition ms =
blanchet@49335
   524
  let val n = length ms in
blanchet@55966
   525
    mk_obj_sumEN_balanced n OF map mk_tupled_allIN_balanced ms RS
traytel@55803
   526
      (type_definition RS @{thm type_copy_obj_one_point_absE})
blanchet@49335
   527
  end;
blanchet@49264
   528
blanchet@55414
   529
fun mk_sum_caseN 1 1 = refl
blanchet@55414
   530
  | mk_sum_caseN _ 1 = @{thm sum.case(1)}
blanchet@55414
   531
  | mk_sum_caseN 2 2 = @{thm sum.case(2)}
blanchet@55414
   532
  | mk_sum_caseN n k = trans OF [@{thm case_sum_step(2)}, mk_sum_caseN (n - 1) (k - 1)];
blanchet@49264
   533
blanchet@49264
   534
fun mk_sum_step base step thm =
blanchet@49264
   535
  if Thm.eq_thm_prop (thm, refl) then base else trans OF [step, thm];
blanchet@49264
   536
blanchet@55414
   537
fun mk_sum_caseN_balanced 1 1 = refl
blanchet@55414
   538
  | mk_sum_caseN_balanced n k =
blanchet@55642
   539
    Balanced_Tree.access {left = mk_sum_step @{thm sum.case(1)} @{thm case_sum_step(1)},
blanchet@55642
   540
      right = mk_sum_step @{thm sum.case(2)} @{thm case_sum_step(2)}, init = refl} n k;
blanchet@49130
   541
blanchet@55851
   542
fun mk_sum_Cinfinite [thm] = thm
blanchet@55851
   543
  | mk_sum_Cinfinite (thm :: thms) = @{thm Cinfinite_csum_weak} OF [thm, mk_sum_Cinfinite thms];
blanchet@55851
   544
blanchet@55851
   545
fun mk_sum_card_order [thm] = thm
blanchet@55851
   546
  | mk_sum_card_order (thm :: thms) = @{thm card_order_csum} OF [thm, mk_sum_card_order thms];
blanchet@55851
   547
desharna@58579
   548
fun mk_xtor_rel_co_induct_thm fp pre_rels pre_phis rels phis xs ys xtors xtor's tac lthy =
traytel@52505
   549
  let
traytel@52505
   550
    val pre_relphis = map (fn rel => Term.list_comb (rel, phis @ pre_phis)) pre_rels;
traytel@52505
   551
    val relphis = map (fn rel => Term.list_comb (rel, phis)) rels;
traytel@52505
   552
    fun mk_xtor fp' xtor x = if fp = fp' then xtor $ x else x;
traytel@52505
   553
    val dtor = mk_xtor Greatest_FP;
traytel@52505
   554
    val ctor = mk_xtor Least_FP;
traytel@52505
   555
    fun flip f x y = if fp = Greatest_FP then f y x else f x y;
traytel@52505
   556
traytel@52505
   557
    fun mk_prem pre_relphi phi x y xtor xtor' =
traytel@52505
   558
      HOLogic.mk_Trueprop (list_all_free [x, y] (flip (curry HOLogic.mk_imp)
traytel@52505
   559
        (pre_relphi $ (dtor xtor x) $ (dtor xtor' y)) (phi $ (ctor xtor x) $ (ctor xtor' y))));
wenzelm@58634
   560
    val prems = @{map 6} mk_prem pre_relphis pre_phis xs ys xtors xtor's;
traytel@52505
   561
traytel@52505
   562
    val concl = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
traytel@52505
   563
      (map2 (flip mk_leq) relphis pre_phis));
traytel@52505
   564
  in
traytel@52506
   565
    Goal.prove_sorry lthy (map (fst o dest_Free) (phis @ pre_phis)) prems concl tac
traytel@52505
   566
    |> Thm.close_derivation
traytel@52505
   567
    |> (fn thm => thm OF (replicate (length pre_rels) @{thm allI[OF allI[OF impI]]}))
traytel@52505
   568
  end;
traytel@52505
   569
traytel@62827
   570
fun mk_xtor_co_iter_transfer_thms fp pre_rels pre_iphis pre_ophis rels phis un_folds un_folds' tac lthy =
traytel@52731
   571
  let
traytel@58443
   572
    val pre_relphis = map (fn rel => Term.list_comb (rel, phis @ pre_iphis)) pre_rels;
traytel@52731
   573
    val relphis = map (fn rel => Term.list_comb (rel, phis)) rels;
traytel@52731
   574
    fun flip f x y = if fp = Greatest_FP then f y x else f x y;
traytel@52731
   575
traytel@58443
   576
    val arg_rels = map2 (flip mk_rel_fun) pre_relphis pre_ophis;
traytel@52731
   577
    fun mk_transfer relphi pre_phi un_fold un_fold' =
blanchet@55945
   578
      fold_rev mk_rel_fun arg_rels (flip mk_rel_fun relphi pre_phi) $ un_fold $ un_fold';
wenzelm@58634
   579
    val transfers = @{map 4} mk_transfer relphis pre_ophis un_folds un_folds';
traytel@52731
   580
traytel@58443
   581
    val goal = fold_rev Logic.all (phis @ pre_ophis)
traytel@52731
   582
      (HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj transfers));
traytel@52731
   583
  in
traytel@52731
   584
    Goal.prove_sorry lthy [] [] goal tac
traytel@52731
   585
    |> Thm.close_derivation
traytel@52731
   586
    |> split_conj_thm
traytel@52731
   587
  end;
traytel@52731
   588
traytel@62827
   589
fun mk_xtor_co_iter_o_map_thms fp is_rec m un_fold_unique xtor_maps xtor_un_folds sym_map_comps
traytel@52913
   590
    map_cong0s =
traytel@52913
   591
  let
traytel@52913
   592
    val n = length sym_map_comps;
blanchet@55966
   593
    val rewrite_comp_comp2 = case_fp fp @{thm rewriteR_comp_comp2} @{thm rewriteL_comp_comp2};
blanchet@55966
   594
    val rewrite_comp_comp = case_fp fp @{thm rewriteR_comp_comp} @{thm rewriteL_comp_comp};
blanchet@55966
   595
    val map_cong_passive_args1 = replicate m (case_fp fp @{thm id_comp} @{thm comp_id} RS fun_cong);
traytel@52913
   596
    val map_cong_active_args1 = replicate n (if is_rec
blanchet@55966
   597
      then case_fp fp @{thm convol_o} @{thm o_case_sum} RS fun_cong
traytel@52913
   598
      else refl);
blanchet@55966
   599
    val map_cong_passive_args2 = replicate m (case_fp fp @{thm comp_id} @{thm id_comp} RS fun_cong);
traytel@52913
   600
    val map_cong_active_args2 = replicate n (if is_rec
blanchet@55966
   601
      then case_fp fp @{thm map_prod_o_convol_id} @{thm case_sum_o_map_sum_id}
blanchet@55966
   602
      else case_fp fp @{thm id_comp} @{thm comp_id} RS fun_cong);
wenzelm@55990
   603
    fun mk_map_congs passive active =
wenzelm@55990
   604
      map (fn thm => thm OF (passive @ active) RS @{thm ext}) map_cong0s;
traytel@52913
   605
    val map_cong1s = mk_map_congs map_cong_passive_args1 map_cong_active_args1;
traytel@52913
   606
    val map_cong2s = mk_map_congs map_cong_passive_args2 map_cong_active_args2;
desharna@57489
   607
traytel@52913
   608
    fun mk_rewrites map_congs = map2 (fn sym_map_comp => fn map_cong =>
traytel@52913
   609
      mk_trans sym_map_comp map_cong RS rewrite_comp_comp) sym_map_comps map_congs;
traytel@52913
   610
    val rewrite1s = mk_rewrites map_cong1s;
traytel@52913
   611
    val rewrite2s = mk_rewrites map_cong2s;
traytel@52913
   612
    val unique_prems =
wenzelm@58634
   613
      @{map 4} (fn xtor_map => fn un_fold => fn rewrite1 => fn rewrite2 =>
traytel@52913
   614
        mk_trans (rewrite_comp_comp2 OF [xtor_map, un_fold])
traytel@52913
   615
          (mk_trans rewrite1 (mk_sym rewrite2)))
traytel@52913
   616
      xtor_maps xtor_un_folds rewrite1s rewrite2s;
traytel@52913
   617
  in
blanchet@55966
   618
    split_conj_thm (un_fold_unique OF map (case_fp fp I mk_sym) unique_prems)
traytel@52913
   619
  end;
traytel@52913
   620
traytel@62905
   621
fun force_typ ctxt T =
traytel@62905
   622
  Term.map_types Type_Infer.paramify_vars
traytel@62905
   623
  #> Type.constraint T
traytel@62905
   624
  #> Syntax.check_term ctxt
traytel@62905
   625
  #> singleton (Variable.polymorphic ctxt);
traytel@62905
   626
traytel@62905
   627
fun mk_xtor_un_fold_xtor_thms fp xtor_un_fold_unique_thm map_id0s =
traytel@62905
   628
  (xtor_un_fold_unique_thm OF
traytel@62905
   629
    map (fn thm => case_fp fp
traytel@62905
   630
      (mk_trans @{thm id_o}
traytel@62905
   631
        (mk_sym (thm RS @{thm trans[OF arg_cong2[of _ _ _ _ "op o", OF refl] o_id]})))
traytel@62905
   632
      (mk_trans (thm RS @{thm arg_cong2[of _ _ _ _ "op o", OF _ refl]})
traytel@62905
   633
        @{thm trans[OF id_o o_id[symmetric]]}))
traytel@62905
   634
    map_id0s)
traytel@62905
   635
  |> split_conj_thm |> map mk_sym;
traytel@62905
   636
traytel@62905
   637
fun derive_xtor_co_recs fp bs mk_Ts (Dss, resDs) pre_bnfs xtors0 un_folds0
traytel@62905
   638
    xtor_un_fold_unique xtor_un_folds xtor_un_fold_transfers xtor_maps xtor_rels lthy =
traytel@62905
   639
  let
traytel@62905
   640
    fun co_swap pair = case_fp fp I swap pair;
traytel@62905
   641
    val mk_co_comp = curry (HOLogic.mk_comp o co_swap);
traytel@62905
   642
    fun mk_co_algT T U = case_fp fp (T --> U) (U --> T);
traytel@62905
   643
    val co_alg_funT = case_fp fp domain_type range_type;
traytel@62905
   644
    val mk_co_product = curry (case_fp fp mk_convol mk_case_sum);
traytel@62905
   645
    val co_proj1_const = case_fp fp fst_const (uncurry Inl_const o dest_sumT) o co_alg_funT;
traytel@62905
   646
    val co_proj2_const = case_fp fp snd_const (uncurry Inr_const o dest_sumT) o co_alg_funT;
traytel@62905
   647
    val mk_co_productT = curry (case_fp fp HOLogic.mk_prodT mk_sumT);
traytel@62905
   648
traytel@62905
   649
    val n = length pre_bnfs;
traytel@62905
   650
    val live = live_of_bnf (hd pre_bnfs);
traytel@62905
   651
    val m = live - n;
traytel@62905
   652
    val ks = 1 upto n;
traytel@62905
   653
traytel@62905
   654
    val map_id0s = map map_id0_of_bnf pre_bnfs;
traytel@62905
   655
    val map_comps = map map_comp_of_bnf pre_bnfs;
traytel@62905
   656
    val map_cong0s = map map_cong0_of_bnf pre_bnfs;
traytel@62905
   657
    val map_transfers = map map_transfer_of_bnf pre_bnfs;
traytel@62905
   658
    val sym_map_comp0s = map (mk_sym o map_comp0_of_bnf) pre_bnfs;
traytel@62905
   659
traytel@62905
   660
    val deads = fold (union (op =)) Dss resDs;
traytel@62905
   661
    val ((((As, Bs), Xs), Ys), names_lthy) = lthy
traytel@62905
   662
      |> fold Variable.declare_typ deads
traytel@62905
   663
      |> mk_TFrees m
traytel@62905
   664
      ||>> mk_TFrees m
traytel@62905
   665
      ||>> mk_TFrees n
traytel@62905
   666
      ||>> mk_TFrees n;
traytel@62905
   667
traytel@62905
   668
    val XFTs = @{map 2} (fn Ds => mk_T_of_bnf Ds (As @ Xs)) Dss pre_bnfs;
traytel@62905
   669
    val co_algXFTs = @{map 2} mk_co_algT XFTs Xs;
traytel@62905
   670
    val Ts = mk_Ts As;
traytel@62905
   671
    val un_foldTs = @{map 2} (fn T => fn X => co_algXFTs ---> mk_co_algT T X) Ts Xs;
traytel@62905
   672
    val un_folds = @{map 2} (force_typ names_lthy) un_foldTs un_folds0;
traytel@62905
   673
    val ABs = As ~~ Bs;
traytel@62905
   674
    val XYs = Xs ~~ Ys;
traytel@62905
   675
traytel@62905
   676
    val Us = map (typ_subst_atomic ABs) Ts;
traytel@62905
   677
traytel@62905
   678
    val TFTs = @{map 2} (fn Ds => mk_T_of_bnf Ds (As @ Ts)) Dss pre_bnfs;
traytel@62905
   679
traytel@62905
   680
    val xtors = @{map 3} (force_typ names_lthy oo mk_co_algT) TFTs Ts xtors0;
traytel@62905
   681
traytel@62905
   682
    val ids = map HOLogic.id_const As;
traytel@62905
   683
    val co_rec_Xs = @{map 2} mk_co_productT Ts Xs;
traytel@62905
   684
    val co_rec_Ys = @{map 2} mk_co_productT Us Ys;
traytel@62905
   685
    val co_rec_algXs = @{map 2} mk_co_algT co_rec_Xs Xs;
traytel@62905
   686
    val co_proj1s = map co_proj1_const co_rec_algXs;
traytel@62905
   687
    val co_rec_maps = @{map 2} (fn Ds =>
traytel@62905
   688
      mk_map_of_bnf Ds (As @ case_fp fp co_rec_Xs Ts) (As @ case_fp fp Ts co_rec_Xs)) Dss pre_bnfs;
traytel@62905
   689
    val co_rec_Ts = @{map 2} (fn Ds => mk_T_of_bnf Ds (As @ co_rec_Xs)) Dss pre_bnfs
traytel@62905
   690
    val co_rec_argTs = @{map 2} mk_co_algT co_rec_Ts Xs;
traytel@62905
   691
    val co_rec_resTs = @{map 2} mk_co_algT Ts Xs;
traytel@62905
   692
traytel@62905
   693
    val (((co_rec_ss, fs), xs), names_lthy) = names_lthy
traytel@62905
   694
      |> mk_Frees "s" co_rec_argTs
traytel@62905
   695
      ||>> mk_Frees "f" co_rec_resTs
traytel@62905
   696
      ||>> mk_Frees "x" (case_fp fp TFTs Xs);
traytel@62905
   697
traytel@62905
   698
    val co_rec_strs =
traytel@62905
   699
      @{map 3} (fn xtor => fn s => fn mapx =>
traytel@62905
   700
        mk_co_product (mk_co_comp xtor (list_comb (mapx, ids @ co_proj1s))) s)
traytel@62905
   701
      xtors co_rec_ss co_rec_maps;
traytel@62905
   702
traytel@62905
   703
    val theta = Xs ~~ co_rec_Xs;
traytel@62905
   704
    val co_rec_un_folds = map (subst_atomic_types theta) un_folds;
traytel@62905
   705
traytel@62905
   706
    val co_rec_spec0s = map (fn un_fold => list_comb (un_fold, co_rec_strs)) co_rec_un_folds;
traytel@62905
   707
traytel@62905
   708
    val co_rec_ids = @{map 2} (mk_co_comp o co_proj1_const) co_rec_algXs co_rec_spec0s;
traytel@62905
   709
    val co_rec_specs = @{map 2} (mk_co_comp o co_proj2_const) co_rec_algXs co_rec_spec0s;
traytel@62905
   710
traytel@62905
   711
    val co_recN = case_fp fp ctor_recN dtor_corecN;
traytel@62905
   712
    fun co_rec_bind i = nth bs (i - 1) |> Binding.prefix_name (co_recN ^ "_");
traytel@62905
   713
    val co_rec_def_bind = rpair [] o Binding.concealed o Thm.def_binding o co_rec_bind;
traytel@62905
   714
traytel@62905
   715
    fun co_rec_spec i =
traytel@62905
   716
      fold_rev (Term.absfree o Term.dest_Free) co_rec_ss (nth co_rec_specs (i - 1));
traytel@62905
   717
traytel@62905
   718
    val ((co_rec_frees, (_, co_rec_def_frees)), (lthy, lthy_old)) =
traytel@62905
   719
      lthy
traytel@62905
   720
      |> Local_Theory.open_target |> snd
traytel@62905
   721
      |> fold_map (fn i =>
traytel@62905
   722
        Local_Theory.define ((co_rec_bind i, NoSyn), (co_rec_def_bind i, co_rec_spec i))) ks
traytel@62905
   723
      |>> apsnd split_list o split_list
traytel@62905
   724
      ||> `Local_Theory.close_target;
traytel@62905
   725
traytel@62905
   726
    val phi = Proof_Context.export_morphism lthy_old lthy;
traytel@62905
   727
    val co_rec_names = map (fst o dest_Const o Morphism.term phi) co_rec_frees;
traytel@62905
   728
    val co_recs = @{map 2} (fn name => fn resT =>
traytel@62905
   729
      Const (name, co_rec_argTs ---> resT)) co_rec_names co_rec_resTs;
traytel@62905
   730
    val co_rec_defs = map (fn def =>
traytel@62905
   731
      mk_unabs_def n (Morphism.thm phi def RS meta_eq_to_obj_eq)) co_rec_def_frees;
traytel@62905
   732
traytel@62905
   733
    val xtor_un_fold_xtor_thms = mk_xtor_un_fold_xtor_thms fp xtor_un_fold_unique map_id0s;
traytel@62905
   734
traytel@62905
   735
    val co_rec_id_thms =
traytel@62905
   736
      let
traytel@62905
   737
        val goal = @{map 2} (fn T => fn t => HOLogic.mk_eq (t, HOLogic.id_const T)) Ts co_rec_ids
traytel@62905
   738
          |> Library.foldr1 HOLogic.mk_conj |> HOLogic.mk_Trueprop;
traytel@62905
   739
        val vars = Variable.add_free_names lthy goal [];
traytel@62905
   740
      in
traytel@62905
   741
        Goal.prove_sorry lthy vars [] goal
traytel@62905
   742
          (fn {context = ctxt, prems = _} => mk_xtor_co_rec_id_tac ctxt xtor_un_fold_xtor_thms
traytel@62905
   743
            xtor_un_fold_unique xtor_un_folds map_comps)
traytel@62905
   744
          |> Thm.close_derivation
traytel@62905
   745
          |> split_conj_thm
traytel@62905
   746
      end;
traytel@62905
   747
traytel@62905
   748
    val co_rec_app_ss = map (fn co_rec => list_comb (co_rec, co_rec_ss)) co_recs;
traytel@62905
   749
    val co_products = @{map 2} (fn T => mk_co_product (HOLogic.id_const T)) Ts co_rec_app_ss;
traytel@62905
   750
    val co_rec_maps_rev = @{map 2} (fn Ds =>
traytel@62905
   751
      mk_map_of_bnf Ds (As @ case_fp fp Ts co_rec_Xs) (As @ case_fp fp co_rec_Xs Ts)) Dss pre_bnfs;
traytel@62905
   752
    fun mk_co_app f g x = case_fp fp (f $ (g $ x)) (g $ (f $ x));
traytel@62905
   753
    val co_rec_expand_thms = map (fn thm => thm RS
traytel@62905
   754
      case_fp fp @{thm convol_expand_snd} @{thm case_sum_expand_Inr_pointfree}) co_rec_id_thms;
traytel@62905
   755
    val xtor_co_rec_thms =
traytel@62905
   756
      let
traytel@62905
   757
        fun mk_goal co_rec s mapx xtor x =
traytel@62905
   758
          let
traytel@62905
   759
            val lhs = mk_co_app co_rec xtor x;
traytel@62905
   760
            val rhs = mk_co_app s (list_comb (mapx, ids @ co_products)) x;
traytel@62905
   761
          in
traytel@62905
   762
            mk_Trueprop_eq (lhs, rhs)
traytel@62905
   763
          end;
traytel@62905
   764
        val goals = @{map 5} mk_goal co_rec_app_ss co_rec_ss co_rec_maps_rev xtors xs;
traytel@62905
   765
      in
traytel@62905
   766
        map2 (fn goal => fn un_fold =>
traytel@62905
   767
          Variable.add_free_names lthy goal []
traytel@62905
   768
          |> (fn vars => Goal.prove_sorry lthy vars [] goal
traytel@62905
   769
            (fn {context = ctxt, prems = _} =>
traytel@62905
   770
              mk_xtor_co_rec_tac ctxt un_fold co_rec_defs co_rec_expand_thms))
traytel@62905
   771
          |> Thm.close_derivation)
traytel@62905
   772
        goals xtor_un_folds
traytel@62905
   773
      end;
traytel@62905
   774
traytel@62905
   775
    val co_product_fs = @{map 2} (fn T => mk_co_product (HOLogic.id_const T)) Ts fs;
traytel@62905
   776
    val co_rec_expand'_thms = map (fn thm =>
traytel@62905
   777
      thm RS case_fp fp @{thm convol_expand_snd'} @{thm case_sum_expand_Inr'}) co_rec_id_thms;
traytel@62905
   778
    val xtor_co_rec_unique_thm =
traytel@62905
   779
      let
traytel@62905
   780
        fun mk_prem f s mapx xtor =
traytel@62905
   781
          let
traytel@62905
   782
            val lhs = mk_co_comp f xtor;
traytel@62905
   783
            val rhs = mk_co_comp s (list_comb (mapx, ids @ co_product_fs));
traytel@62905
   784
          in
traytel@62905
   785
            mk_Trueprop_eq (co_swap (lhs, rhs))
traytel@62905
   786
          end;
traytel@62905
   787
        val prems = @{map 4} mk_prem fs co_rec_ss co_rec_maps_rev xtors;
traytel@62905
   788
        val concl = @{map 2} (curry HOLogic.mk_eq) fs co_rec_app_ss
traytel@62905
   789
          |> Library.foldr1 HOLogic.mk_conj |> HOLogic.mk_Trueprop;
traytel@62905
   790
        val goal = Logic.list_implies (prems, concl);
traytel@62905
   791
        val vars = Variable.add_free_names lthy goal [];
traytel@62905
   792
      in
traytel@62905
   793
        Goal.prove_sorry lthy vars [] goal
traytel@62905
   794
          (fn {context = ctxt, prems = _} => mk_xtor_co_rec_unique_tac ctxt fp co_rec_defs
traytel@62905
   795
            co_rec_expand'_thms xtor_un_fold_unique map_id0s sym_map_comp0s)
traytel@62905
   796
        |> Thm.close_derivation
traytel@62905
   797
      end;
traytel@62905
   798
traytel@62905
   799
    val xtor_co_rec_o_map_thms = mk_xtor_co_iter_o_map_thms fp true m xtor_co_rec_unique_thm
traytel@62905
   800
      (map (mk_pointfree lthy) xtor_maps) (map (mk_pointfree lthy) xtor_co_rec_thms)
traytel@62905
   801
      sym_map_comp0s map_cong0s;
traytel@62905
   802
traytel@62905
   803
    val ABphiTs = @{map 2} mk_pred2T As Bs;
traytel@62905
   804
    val XYphiTs = @{map 2} mk_pred2T Xs Ys;
traytel@62905
   805
traytel@62905
   806
    val ((ABphis, XYphis), names_lthy) = names_lthy
traytel@62905
   807
      |> mk_Frees "R" ABphiTs
traytel@62905
   808
      ||>> mk_Frees "S" XYphiTs;
traytel@62905
   809
traytel@62905
   810
    val pre_rels =
traytel@62905
   811
      @{map 2} (fn Ds => mk_rel_of_bnf Ds (As @ co_rec_Xs) (Bs @ co_rec_Ys)) Dss pre_bnfs;
traytel@62905
   812
    val rels = @{map 3} (fn T => fn T' => Thm.prop_of #> HOLogic.dest_Trueprop
traytel@62905
   813
        #> fst o dest_comb #> fst o dest_comb #> funpow n (snd o dest_comb)
traytel@62905
   814
        #> case_fp fp (fst o dest_comb #> snd o dest_comb) (snd o dest_comb) #> head_of
traytel@62905
   815
        #> force_typ names_lthy (ABphiTs ---> mk_pred2T T T'))
traytel@62905
   816
      Ts Us xtor_un_fold_transfers;
traytel@62905
   817
traytel@62905
   818
    fun tac {context = ctxt, prems = _} = mk_xtor_co_rec_transfer_tac ctxt fp n m co_rec_defs
traytel@62905
   819
      xtor_un_fold_transfers map_transfers xtor_rels;
traytel@62905
   820
traytel@62905
   821
    val mk_rel_co_product = case_fp fp mk_rel_prod mk_rel_sum;
traytel@62905
   822
    val rec_phis =
traytel@62905
   823
      map2 (fn rel => mk_rel_co_product (Term.list_comb (rel, ABphis))) rels XYphis;
traytel@62905
   824
traytel@62905
   825
    val xtor_co_rec_transfer_thms =
traytel@62905
   826
      mk_xtor_co_iter_transfer_thms fp pre_rels rec_phis XYphis rels ABphis
traytel@62905
   827
        co_recs (map (subst_atomic_types (ABs @ XYs)) co_recs) tac lthy;
traytel@62905
   828
traytel@62905
   829
    val notes =
traytel@62905
   830
      [(case_fp fp ctor_recN dtor_corecN, xtor_co_rec_thms),
traytel@62905
   831
       (case_fp fp ctor_rec_uniqueN dtor_corec_uniqueN, split_conj_thm xtor_co_rec_unique_thm),
traytel@62905
   832
       (case_fp fp ctor_rec_o_mapN dtor_corec_o_mapN, xtor_co_rec_o_map_thms),
traytel@62905
   833
       (case_fp fp ctor_rec_transferN dtor_corec_transferN, xtor_co_rec_transfer_thms)]
traytel@62905
   834
      |> map (apsnd (map single))
traytel@62905
   835
      |> maps (fn (thmN, thmss) =>
traytel@62905
   836
        map2 (fn b => fn thms =>
traytel@62905
   837
          ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]))
traytel@62905
   838
        bs thmss);
traytel@62905
   839
traytel@62905
   840
     val lthy = lthy |> Config.get lthy bnf_internals ? snd o Local_Theory.notes notes;
traytel@62905
   841
  in
traytel@62905
   842
    ((co_recs,
traytel@62905
   843
     (xtor_co_rec_thms, xtor_co_rec_unique_thm, xtor_co_rec_o_map_thms, xtor_co_rec_transfer_thms)),
traytel@62905
   844
      lthy)
traytel@62905
   845
  end;
traytel@62905
   846
blanchet@62722
   847
fun fixpoint_bnf extra_qualify construct_fp bs resBs Ds0 fp_eqs comp_cache0 lthy =
blanchet@51868
   848
  let
traytel@53143
   849
    val time = time lthy;
blanchet@51868
   850
    val timer = time (Timer.startRealTimer ());
blanchet@58332
   851
blanchet@58332
   852
    val nn = length fp_eqs;
blanchet@53222
   853
    val (Xs, rhsXs) = split_list fp_eqs;
blanchet@48975
   854
blanchet@58332
   855
    fun flatten_tyargs Ass =
blanchet@53222
   856
      subtract (op =) Xs (filter (fn T => exists (fn Ts => member (op =) Ts T) Ass) resBs) @ Xs;
blanchet@51868
   857
blanchet@53263
   858
    fun raw_qualify base_b =
wenzelm@59858
   859
      let
wenzelm@59858
   860
        val qs = Binding.path_of base_b;
wenzelm@59858
   861
        val n = Binding.name_of base_b;
blanchet@53264
   862
      in
blanchet@53264
   863
        Binding.prefix_name rawN
blanchet@53264
   864
        #> fold_rev (fn (s, mand) => Binding.qualify mand s) (qs @ [(n, true)])
blanchet@62722
   865
        #> extra_qualify #> Binding.concealed
blanchet@53264
   866
      end;
blanchet@51868
   867
blanchet@62720
   868
    val ((bnfs, (deadss, livess)), (comp_cache, unfold_set)) =
blanchet@55706
   869
      apfst (apsnd split_list o split_list)
wenzelm@58634
   870
        (@{fold_map 2}
blanchet@62621
   871
          (fn b => bnf_of_typ true Smart_Inline (raw_qualify b) flatten_tyargs Xs Ds0) bs rhsXs
blanchet@62720
   872
          ((comp_cache0, empty_unfolds), lthy));
blanchet@51868
   873
blanchet@62722
   874
    fun norm_qualify i =
blanchet@62722
   875
      Binding.qualify true (Binding.name_of (nth bs (Int.max (0, i - 1))))
blanchet@62722
   876
      #> extra_qualify #> Binding.concealed;
blanchet@48975
   877
blanchet@49132
   878
    val Ass = map (map dest_TFree) livess;
blanchet@58332
   879
    val Ds' = fold (fold Term.add_tfreesT) deadss [];
blanchet@58332
   880
    val Ds = union (op =) Ds' Ds0;
blanchet@58332
   881
    val missing = resBs |> fold (subtract (op =)) (Ds' :: Ass);
blanchet@58332
   882
    val (dead_phantoms, live_phantoms) = List.partition (member (op =) Ds0) missing;
blanchet@58332
   883
    val resBs' = resBs |> fold (subtract (op =)) [dead_phantoms, Ds];
blanchet@49132
   884
blanchet@48975
   885
    val timer = time (timer "Construction of BNFs");
blanchet@48975
   886
blanchet@62720
   887
    val ((kill_poss, _), (bnfs', ((comp_cache', unfold_set'), lthy'))) =
blanchet@62720
   888
      normalize_bnfs norm_qualify Ass Ds (K (resBs' @ Xs)) bnfs (comp_cache, unfold_set);
blanchet@48975
   889
wenzelm@58634
   890
    val Dss = @{map 3} (uncurry append oo curry swap oo map o nth) livess kill_poss deadss;
blanchet@48975
   891
blanchet@62722
   892
    fun pre_qualify b =
blanchet@62722
   893
      Binding.qualify false (Binding.name_of b)
blanchet@62722
   894
      #> extra_qualify
wenzelm@62093
   895
      #> not (Config.get lthy' bnf_internals) ? Binding.concealed;
blanchet@53264
   896
traytel@55803
   897
    val ((pre_bnfs, (deadss, absT_infos)), lthy'') =
wenzelm@58634
   898
      @{fold_map 4} (fn b => seal_bnf (pre_qualify b) unfold_set' (Binding.prefix_name preN b))
blanchet@58332
   899
        bs (not (null live_phantoms) :: replicate (nn - 1) false) Dss bnfs' lthy'
traytel@55803
   900
      |>> split_list
traytel@55803
   901
      |>> apsnd split_list;
blanchet@48975
   902
blanchet@48975
   903
    val timer = time (timer "Normalization & sealing of BNFs");
blanchet@48975
   904
blanchet@58332
   905
    val res = construct_fp bs resBs (map TFree dead_phantoms, deadss) pre_bnfs absT_infos lthy'';
blanchet@48975
   906
blanchet@48975
   907
    val timer = time (timer "FP construction in total");
blanchet@48975
   908
  in
blanchet@62720
   909
    (((pre_bnfs, absT_infos), comp_cache'), res)
blanchet@48975
   910
  end;
blanchet@48975
   911
blanchet@58256
   912
fun fp_antiquote_setup binding =
blanchet@58256
   913
  Thy_Output.antiquotation binding (Args.type_name {proper = true, strict = true})
blanchet@58256
   914
    (fn {source = src, context = ctxt, ...} => fn fcT_name =>
blanchet@58256
   915
       (case Ctr_Sugar.ctr_sugar_of ctxt fcT_name of
blanchet@58256
   916
         NONE => error ("Not a known freely generated type name: " ^ quote fcT_name)
blanchet@58256
   917
       | SOME {T = T0, ctrs = ctrs0, ...} =>
blanchet@58256
   918
         let
blanchet@58256
   919
           val freezeT = Term.map_atyps (fn TVar ((s, _), S) => TFree (s, S) | T => T);
blanchet@58256
   920
blanchet@58256
   921
           val T = freezeT T0;
blanchet@58256
   922
           val ctrs = map (Term.map_types freezeT) ctrs0;
blanchet@58256
   923
blanchet@58256
   924
           val pretty_typ_bracket = Syntax.pretty_typ (Config.put pretty_priority 1001 ctxt);
blanchet@58256
   925
           fun pretty_ctr ctr =
blanchet@58256
   926
             Pretty.block (Pretty.breaks (Syntax.pretty_term ctxt ctr ::
blanchet@58256
   927
               map pretty_typ_bracket (binder_types (fastype_of ctr))));
blanchet@58256
   928
           val pretty_co_datatype =
blanchet@58256
   929
             Pretty.block (Pretty.keyword1 (Binding.name_of binding) :: Pretty.brk 1 ::
blanchet@58256
   930
               Syntax.pretty_typ ctxt T :: Pretty.str " =" :: Pretty.brk 1 ::
blanchet@58256
   931
               flat (separate [Pretty.brk 1, Pretty.str "| "] (map (single o pretty_ctr) ctrs)));
blanchet@58256
   932
         in
blanchet@58256
   933
           Thy_Output.output ctxt
blanchet@58256
   934
             (Thy_Output.maybe_pretty_source (K (K pretty_co_datatype)) ctxt src [()])
blanchet@58256
   935
         end));
blanchet@58256
   936
blanchet@48975
   937
end;