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