src/HOL/Tools/BNF/bnf_fp_util.ML
author wenzelm
Fri Mar 06 15:58:56 2015 +0100 (2015-03-06)
changeset 59621 291934bac95e
parent 59582 0fbed69ff081
child 59642 929984c529d3
permissions -rw-r--r--
Thm.cterm_of and Thm.ctyp_of operate on local context;
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,
blanchet@51839
    16
     ctors: term list,
blanchet@51819
    17
     dtors: term list,
blanchet@55868
    18
     xtor_co_recs: term list,
traytel@53106
    19
     xtor_co_induct: thm,
blanchet@51819
    20
     dtor_ctors: thm list,
blanchet@51819
    21
     ctor_dtors: thm list,
blanchet@51819
    22
     ctor_injects: thm list,
traytel@53203
    23
     dtor_injects: thm list,
desharna@58583
    24
     xtor_maps: thm list,
desharna@58584
    25
     xtor_setss: thm list list,
desharna@58585
    26
     xtor_rels: thm list,
blanchet@55868
    27
     xtor_co_rec_thms: thm list,
desharna@58578
    28
     xtor_co_rec_o_maps: thm list,
desharna@58579
    29
     xtor_rel_co_induct: thm,
desharna@58580
    30
     dtor_set_inducts: thm list,
desharna@58581
    31
     xtor_co_rec_transfers: thm list}
blanchet@49591
    32
blanchet@51823
    33
  val morph_fp_result: morphism -> fp_result -> fp_result
blanchet@51823
    34
traytel@53143
    35
  val time: Proof.context -> Timer.real_timer -> string -> Timer.real_timer
blanchet@48975
    36
blanchet@55851
    37
  val fixpoint: ('a * 'a -> bool) -> ('a list -> 'a list) -> 'a list -> 'a list
blanchet@55851
    38
blanchet@48975
    39
  val IITN: string
blanchet@48975
    40
  val LevN: string
blanchet@48975
    41
  val algN: string
blanchet@48975
    42
  val behN: string
blanchet@48975
    43
  val bisN: string
blanchet@48975
    44
  val carTN: string
blanchet@49338
    45
  val caseN: string
blanchet@48975
    46
  val coN: string
blanchet@48975
    47
  val coinductN: string
blanchet@57983
    48
  val coinduct_strongN: string
blanchet@48975
    49
  val corecN: string
blanchet@57983
    50
  val corec_discN: string
blanchet@57983
    51
  val corec_disc_iffN: string
blanchet@49501
    52
  val ctorN: string
blanchet@49501
    53
  val ctor_dtorN: string
blanchet@49501
    54
  val ctor_exhaustN: string
blanchet@49501
    55
  val ctor_induct2N: string
blanchet@49501
    56
  val ctor_inductN: string
blanchet@49501
    57
  val ctor_injectN: string
blanchet@49504
    58
  val ctor_foldN: string
traytel@52913
    59
  val ctor_fold_o_mapN: string
traytel@52913
    60
  val ctor_fold_transferN: string
blanchet@49504
    61
  val ctor_fold_uniqueN: string
blanchet@49541
    62
  val ctor_mapN: string
blanchet@49543
    63
  val ctor_map_uniqueN: string
blanchet@49501
    64
  val ctor_recN: string
traytel@52913
    65
  val ctor_rec_o_mapN: string
desharna@58444
    66
  val ctor_rec_transferN: string
traytel@51739
    67
  val ctor_rec_uniqueN: string
blanchet@49518
    68
  val ctor_relN: string
blanchet@55901
    69
  val ctor_rel_inductN: string
blanchet@49544
    70
  val ctor_set_inclN: string
blanchet@49544
    71
  val ctor_set_set_inclN: string
blanchet@49501
    72
  val dtorN: string
blanchet@49582
    73
  val dtor_coinductN: string
blanchet@49501
    74
  val dtor_corecN: string
traytel@52913
    75
  val dtor_corec_o_mapN: string
desharna@58445
    76
  val dtor_corec_transferN: string
traytel@51739
    77
  val dtor_corec_uniqueN: string
blanchet@49518
    78
  val dtor_ctorN: string
blanchet@49501
    79
  val dtor_exhaustN: string
blanchet@49501
    80
  val dtor_injectN: string
blanchet@49545
    81
  val dtor_mapN: string
blanchet@49581
    82
  val dtor_map_coinductN: string
blanchet@57983
    83
  val dtor_map_coinduct_strongN: string
blanchet@49543
    84
  val dtor_map_uniqueN: string
blanchet@49545
    85
  val dtor_relN: string
blanchet@55901
    86
  val dtor_rel_coinductN: string
blanchet@49544
    87
  val dtor_set_inclN: string
blanchet@49544
    88
  val dtor_set_set_inclN: string
blanchet@57983
    89
  val dtor_coinduct_strongN: string
blanchet@49516
    90
  val dtor_unfoldN: string
traytel@52913
    91
  val dtor_unfold_o_mapN: string
traytel@52913
    92
  val dtor_unfold_transferN: string
blanchet@49516
    93
  val dtor_unfold_uniqueN: string
blanchet@49020
    94
  val exhaustN: string
traytel@56113
    95
  val colN: string
blanchet@48975
    96
  val inductN: string
blanchet@49019
    97
  val injectN: string
blanchet@48975
    98
  val isNodeN: string
blanchet@48975
    99
  val lsbisN: string
blanchet@49594
   100
  val mapN: string
blanchet@48975
   101
  val map_uniqueN: string
blanchet@48975
   102
  val min_algN: string
blanchet@48975
   103
  val morN: string
blanchet@49020
   104
  val nchotomyN: string
blanchet@48975
   105
  val recN: string
desharna@57525
   106
  val rel_casesN: string
traytel@51918
   107
  val rel_coinductN: string
traytel@51918
   108
  val rel_inductN: string
blanchet@49592
   109
  val rel_injectN: string
desharna@57493
   110
  val rel_introsN: string
blanchet@49592
   111
  val rel_distinctN: string
desharna@57563
   112
  val rel_selN: string
blanchet@48975
   113
  val rvN: string
blanchet@57983
   114
  val corec_selN: string
blanchet@48975
   115
  val set_inclN: string
blanchet@48975
   116
  val set_set_inclN: string
blanchet@53694
   117
  val setN: string
blanchet@49438
   118
  val simpsN: string
blanchet@48975
   119
  val strTN: string
blanchet@48975
   120
  val str_initN: string
blanchet@48975
   121
  val sum_bdN: string
blanchet@48975
   122
  val sum_bdTN: string
blanchet@48975
   123
  val uniqueN: string
blanchet@48975
   124
blanchet@49585
   125
  (* TODO: Don't index set facts. Isabelle packages traditionally generate uniform names. *)
blanchet@49584
   126
  val mk_ctor_setN: int -> string
blanchet@49584
   127
  val mk_dtor_setN: int -> string
blanchet@49542
   128
  val mk_dtor_set_inductN: int -> string
blanchet@48975
   129
  val mk_set_inductN: int -> string
blanchet@48975
   130
blanchet@55575
   131
  val co_prefix: BNF_Util.fp_kind -> string
blanchet@51863
   132
blanchet@48975
   133
  val split_conj_thm: thm -> thm list
blanchet@48975
   134
  val split_conj_prems: int -> thm -> thm
blanchet@48975
   135
blanchet@49255
   136
  val mk_sumTN: typ list -> typ
blanchet@58159
   137
  val mk_sumTN_balanced: typ list -> typ
blanchet@55969
   138
  val mk_tupleT_balanced: typ list -> typ
blanchet@55966
   139
  val mk_sumprodT_balanced: typ list list -> typ
blanchet@49255
   140
blanchet@54923
   141
  val mk_proj: typ -> int -> int -> term
blanchet@54923
   142
traytel@53032
   143
  val mk_convol: term * term -> term
traytel@58443
   144
  val mk_rel_prod: term -> term -> term
traytel@58443
   145
  val mk_rel_sum: term -> term -> term
blanchet@49368
   146
blanchet@49121
   147
  val Inl_const: typ -> typ -> term
blanchet@49121
   148
  val Inr_const: typ -> typ -> term
blanchet@55969
   149
  val mk_tuple_balanced: term list -> term
blanchet@55969
   150
  val mk_tuple1_balanced: typ list -> term list -> term
blanchet@49121
   151
blanchet@55414
   152
  val mk_case_sum: term * term -> term
blanchet@55414
   153
  val mk_case_sumN: term list -> term
blanchet@55968
   154
  val mk_case_absumprod: typ -> term -> term list -> term list list -> term list list -> term
traytel@55803
   155
blanchet@49255
   156
  val mk_Inl: typ -> term -> term
blanchet@49255
   157
  val mk_Inr: typ -> term -> term
traytel@55803
   158
  val mk_absumprod: typ -> term -> int -> int -> term list -> term
blanchet@49121
   159
blanchet@49255
   160
  val dest_sumT: typ -> typ * typ
blanchet@58159
   161
  val dest_sumTN_balanced: int -> typ -> typ list
blanchet@58159
   162
  val dest_tupleT_balanced: int -> typ -> typ list
traytel@55803
   163
  val dest_absumprodT: typ -> typ -> int -> int list -> typ -> typ list list
blanchet@49176
   164
blanchet@53202
   165
  val If_const: typ -> term
blanchet@53202
   166
blanchet@48975
   167
  val mk_Field: term -> term
blanchet@49275
   168
  val mk_If: term -> term -> term -> term
blanchet@48975
   169
  val mk_union: term * term -> term
blanchet@48975
   170
traytel@55803
   171
  val mk_absumprodE: thm -> int list -> thm
traytel@55803
   172
blanchet@55414
   173
  val mk_sum_caseN: int -> int -> thm
blanchet@55414
   174
  val mk_sum_caseN_balanced: int -> int -> thm
blanchet@49125
   175
blanchet@55851
   176
  val mk_sum_Cinfinite: thm list -> thm
blanchet@55851
   177
  val mk_sum_card_order: thm list -> thm
blanchet@48975
   178
desharna@58579
   179
  val mk_xtor_rel_co_induct_thm: BNF_Util.fp_kind -> term list -> term list -> term list ->
blanchet@55575
   180
    term list -> term list -> term list -> term list -> term list ->
traytel@52505
   181
    ({prems: thm list, context: Proof.context} -> tactic) -> Proof.context -> thm
traytel@58443
   182
  val mk_co_iter_transfer_thms: BNF_Util.fp_kind -> term list -> term list -> term list ->
traytel@58443
   183
    term list -> term list -> term list -> term list ->
traytel@58443
   184
    ({prems: thm list, context: Proof.context} -> tactic) -> Proof.context -> thm list
blanchet@55575
   185
  val mk_xtor_un_fold_o_map_thms: BNF_Util.fp_kind -> bool -> int -> thm -> thm list -> thm list ->
traytel@52913
   186
    thm list -> thm list -> thm list
traytel@52505
   187
blanchet@57983
   188
  val mk_coinduct_strong_thm: thm -> thm list -> thm list -> (thm -> thm) -> Proof.context -> thm
traytel@53105
   189
blanchet@51867
   190
  val fp_bnf: (binding list -> (string * sort) list -> typ list * typ list list ->
traytel@55803
   191
      BNF_Def.bnf list -> BNF_Comp.absT_info list -> local_theory -> 'a) ->
blanchet@55701
   192
    binding list -> (string * sort) list -> (string * sort) list -> ((string * sort) * typ) list ->
traytel@55803
   193
    local_theory -> (BNF_Def.bnf list * BNF_Comp.absT_info list) * 'a
blanchet@58256
   194
  val fp_antiquote_setup: binding -> theory -> theory
blanchet@48975
   195
end;
blanchet@48975
   196
blanchet@51850
   197
structure BNF_FP_Util : BNF_FP_UTIL =
blanchet@48975
   198
struct
blanchet@48975
   199
blanchet@56650
   200
open Ctr_Sugar
blanchet@48975
   201
open BNF_Comp
blanchet@48975
   202
open BNF_Def
blanchet@48975
   203
open BNF_Util
blanchet@48975
   204
blanchet@49591
   205
type fp_result =
blanchet@51859
   206
  {Ts: typ list,
blanchet@51859
   207
   bnfs: BNF_Def.bnf list,
blanchet@51839
   208
   ctors: term list,
blanchet@51819
   209
   dtors: term list,
blanchet@55868
   210
   xtor_co_recs: term list,
traytel@53106
   211
   xtor_co_induct: thm,
blanchet@51819
   212
   dtor_ctors: thm list,
blanchet@51819
   213
   ctor_dtors: thm list,
blanchet@51819
   214
   ctor_injects: thm list,
traytel@53203
   215
   dtor_injects: thm list,
desharna@58583
   216
   xtor_maps: thm list,
desharna@58584
   217
   xtor_setss: thm list list,
desharna@58585
   218
   xtor_rels: thm list,
blanchet@55868
   219
   xtor_co_rec_thms: thm list,
desharna@58578
   220
   xtor_co_rec_o_maps: thm list,
desharna@58579
   221
   xtor_rel_co_induct: thm,
desharna@58580
   222
   dtor_set_inducts: thm list,
desharna@58581
   223
   xtor_co_rec_transfers: thm list};
blanchet@49591
   224
traytel@55899
   225
fun morph_fp_result phi {Ts, bnfs, ctors, dtors, xtor_co_recs, xtor_co_induct,
desharna@58584
   226
    dtor_ctors, ctor_dtors, ctor_injects, dtor_injects, xtor_maps, xtor_setss,
desharna@58585
   227
    xtor_rels, xtor_co_rec_thms, xtor_co_rec_o_maps, xtor_rel_co_induct,
desharna@58581
   228
    dtor_set_inducts, xtor_co_rec_transfers} =
blanchet@51859
   229
  {Ts = map (Morphism.typ phi) Ts,
blanchet@51859
   230
   bnfs = map (morph_bnf phi) bnfs,
blanchet@51839
   231
   ctors = map (Morphism.term phi) ctors,
blanchet@51823
   232
   dtors = map (Morphism.term phi) dtors,
blanchet@55868
   233
   xtor_co_recs = map (Morphism.term phi) xtor_co_recs,
traytel@53106
   234
   xtor_co_induct = Morphism.thm phi xtor_co_induct,
blanchet@51823
   235
   dtor_ctors = map (Morphism.thm phi) dtor_ctors,
blanchet@51823
   236
   ctor_dtors = map (Morphism.thm phi) ctor_dtors,
blanchet@51823
   237
   ctor_injects = map (Morphism.thm phi) ctor_injects,
traytel@53203
   238
   dtor_injects = map (Morphism.thm phi) dtor_injects,
desharna@58583
   239
   xtor_maps = map (Morphism.thm phi) xtor_maps,
desharna@58584
   240
   xtor_setss = map (map (Morphism.thm phi)) xtor_setss,
desharna@58585
   241
   xtor_rels = map (Morphism.thm phi) xtor_rels,
blanchet@55868
   242
   xtor_co_rec_thms = map (Morphism.thm phi) xtor_co_rec_thms,
desharna@58578
   243
   xtor_co_rec_o_maps = map (Morphism.thm phi) xtor_co_rec_o_maps,
desharna@58579
   244
   xtor_rel_co_induct = Morphism.thm phi xtor_rel_co_induct,
desharna@58580
   245
   dtor_set_inducts = map (Morphism.thm phi) dtor_set_inducts,
desharna@58581
   246
   xtor_co_rec_transfers = map (Morphism.thm phi) xtor_co_rec_transfers};
blanchet@51823
   247
traytel@53143
   248
fun time ctxt timer msg = (if Config.get ctxt bnf_timing
traytel@55811
   249
  then warning (msg ^ ": " ^ string_of_int (Time.toMilliseconds (Timer.checkRealTimer timer)) ^
traytel@55811
   250
    "ms")
blanchet@48975
   251
  else (); Timer.startRealTimer ());
blanchet@48975
   252
blanchet@49223
   253
val preN = "pre_"
blanchet@49223
   254
val rawN = "raw_"
blanchet@49218
   255
blanchet@48975
   256
val coN = "co"
blanchet@49504
   257
val unN = "un"
blanchet@48975
   258
val algN = "alg"
blanchet@48975
   259
val IITN = "IITN"
blanchet@49504
   260
val foldN = "fold"
blanchet@49504
   261
val unfoldN = unN ^ foldN
blanchet@48975
   262
val uniqueN = "_unique"
traytel@52731
   263
val transferN = "_transfer"
blanchet@49438
   264
val simpsN = "simps"
blanchet@49501
   265
val ctorN = "ctor"
blanchet@49501
   266
val dtorN = "dtor"
blanchet@49504
   267
val ctor_foldN = ctorN ^ "_" ^ foldN
blanchet@49504
   268
val dtor_unfoldN = dtorN ^ "_" ^ unfoldN
blanchet@49504
   269
val ctor_fold_uniqueN = ctor_foldN ^ uniqueN
traytel@52913
   270
val ctor_fold_o_mapN = ctor_foldN ^ "_o_" ^ mapN
blanchet@49504
   271
val dtor_unfold_uniqueN = dtor_unfoldN ^ uniqueN
traytel@52913
   272
val dtor_unfold_o_mapN = dtor_unfoldN ^ "_o_" ^ mapN
traytel@52731
   273
val ctor_fold_transferN = ctor_foldN ^ transferN
traytel@52731
   274
val dtor_unfold_transferN = dtor_unfoldN ^ transferN
blanchet@49541
   275
val ctor_mapN = ctorN ^ "_" ^ mapN
blanchet@49541
   276
val dtor_mapN = dtorN ^ "_" ^ mapN
blanchet@48975
   277
val map_uniqueN = mapN ^ uniqueN
blanchet@49543
   278
val ctor_map_uniqueN = ctorN ^ "_" ^ map_uniqueN
blanchet@49543
   279
val dtor_map_uniqueN = dtorN ^ "_" ^ map_uniqueN
blanchet@48975
   280
val min_algN = "min_alg"
blanchet@48975
   281
val morN = "mor"
blanchet@48975
   282
val bisN = "bis"
blanchet@48975
   283
val lsbisN = "lsbis"
blanchet@48975
   284
val sum_bdTN = "sbdT"
blanchet@48975
   285
val sum_bdN = "sbd"
blanchet@48975
   286
val carTN = "carT"
blanchet@48975
   287
val strTN = "strT"
blanchet@48975
   288
val isNodeN = "isNode"
blanchet@48975
   289
val LevN = "Lev"
blanchet@48975
   290
val rvN = "recover"
blanchet@48975
   291
val behN = "beh"
blanchet@53694
   292
val setN = "set"
blanchet@49584
   293
val mk_ctor_setN = prefix (ctorN ^ "_") o mk_setN
blanchet@49584
   294
val mk_dtor_setN = prefix (dtorN ^ "_") o mk_setN
blanchet@48975
   295
fun mk_set_inductN i = mk_setN i ^ "_induct"
blanchet@49542
   296
val mk_dtor_set_inductN = prefix (dtorN ^ "_") o mk_set_inductN
blanchet@48975
   297
blanchet@48975
   298
val str_initN = "str_init"
blanchet@48975
   299
val recN = "rec"
blanchet@48975
   300
val corecN = coN ^ recN
blanchet@49501
   301
val ctor_recN = ctorN ^ "_" ^ recN
traytel@52913
   302
val ctor_rec_o_mapN = ctor_recN ^ "_o_" ^ mapN
desharna@58444
   303
val ctor_rec_transferN = ctor_recN ^ transferN
traytel@51739
   304
val ctor_rec_uniqueN = ctor_recN ^ uniqueN
blanchet@49501
   305
val dtor_corecN = dtorN ^ "_" ^ corecN
traytel@52913
   306
val dtor_corec_o_mapN = dtor_corecN ^ "_o_" ^ mapN
desharna@58445
   307
val dtor_corec_transferN = dtor_corecN ^ transferN
traytel@51739
   308
val dtor_corec_uniqueN = dtor_corecN ^ uniqueN
blanchet@48975
   309
blanchet@49501
   310
val ctor_dtorN = ctorN ^ "_" ^ dtorN
blanchet@49501
   311
val dtor_ctorN = dtorN ^ "_" ^ ctorN
blanchet@49020
   312
val nchotomyN = "nchotomy"
blanchet@49019
   313
val injectN = "inject"
blanchet@49020
   314
val exhaustN = "exhaust"
blanchet@49585
   315
val ctor_injectN = ctorN ^ "_" ^ injectN
blanchet@49585
   316
val ctor_exhaustN = ctorN ^ "_" ^ exhaustN
blanchet@49585
   317
val dtor_injectN = dtorN ^ "_" ^ injectN
blanchet@49585
   318
val dtor_exhaustN = dtorN ^ "_" ^ exhaustN
blanchet@49545
   319
val ctor_relN = ctorN ^ "_" ^ relN
blanchet@49545
   320
val dtor_relN = dtorN ^ "_" ^ relN
blanchet@48975
   321
val inductN = "induct"
blanchet@48975
   322
val coinductN = coN ^ inductN
blanchet@49501
   323
val ctor_inductN = ctorN ^ "_" ^ inductN
blanchet@49501
   324
val ctor_induct2N = ctor_inductN ^ "2"
blanchet@49581
   325
val dtor_map_coinductN = dtor_mapN ^ "_" ^ coinductN
blanchet@49582
   326
val dtor_coinductN = dtorN ^ "_" ^ coinductN
blanchet@57983
   327
val coinduct_strongN = coinductN ^ "_strong"
blanchet@57983
   328
val dtor_map_coinduct_strongN = dtor_mapN ^ "_" ^ coinduct_strongN
blanchet@57983
   329
val dtor_coinduct_strongN = dtorN ^ "_" ^ coinduct_strongN
traytel@56113
   330
val colN = "col"
blanchet@48975
   331
val set_inclN = "set_incl"
blanchet@49544
   332
val ctor_set_inclN = ctorN ^ "_" ^ set_inclN
blanchet@49544
   333
val dtor_set_inclN = dtorN ^ "_" ^ set_inclN
blanchet@48975
   334
val set_set_inclN = "set_set_incl"
blanchet@49544
   335
val ctor_set_set_inclN = ctorN ^ "_" ^ set_set_inclN
blanchet@49544
   336
val dtor_set_set_inclN = dtorN ^ "_" ^ set_set_inclN
blanchet@48975
   337
blanchet@49338
   338
val caseN = "case"
blanchet@49342
   339
val discN = "disc"
blanchet@57983
   340
val corec_discN = corecN ^ "_" ^ discN
blanchet@49594
   341
val iffN = "_iff"
blanchet@57983
   342
val corec_disc_iffN = corec_discN ^ iffN
blanchet@49592
   343
val distinctN = "distinct"
blanchet@49592
   344
val rel_distinctN = relN ^ "_" ^ distinctN
blanchet@49592
   345
val injectN = "inject"
desharna@57525
   346
val rel_casesN = relN ^ "_cases"
blanchet@49592
   347
val rel_injectN = relN ^ "_" ^ injectN
desharna@57493
   348
val rel_introsN = relN ^ "_intros"
traytel@51918
   349
val rel_coinductN = relN ^ "_" ^ coinductN
desharna@57563
   350
val rel_selN = relN ^ "_sel"
blanchet@55901
   351
val dtor_rel_coinductN = dtorN ^ "_" ^ rel_coinductN
traytel@51918
   352
val rel_inductN = relN ^ "_" ^ inductN
blanchet@55901
   353
val ctor_rel_inductN = ctorN ^ "_" ^ rel_inductN
blanchet@49342
   354
val selN = "sel"
blanchet@57983
   355
val corec_selN = corecN ^ "_" ^ selN
blanchet@49338
   356
blanchet@55966
   357
fun co_prefix fp = case_fp fp "" "co";
blanchet@51863
   358
blanchet@49264
   359
fun dest_sumT (Type (@{type_name sum}, [T, T'])) = (T, T');
blanchet@49264
   360
blanchet@49264
   361
val dest_sumTN_balanced = Balanced_Tree.dest dest_sumT;
blanchet@49264
   362
blanchet@55966
   363
fun dest_tupleT_balanced 0 @{typ unit} = []
blanchet@55966
   364
  | dest_tupleT_balanced n T = Balanced_Tree.dest HOLogic.dest_prodT n T;
blanchet@49264
   365
blanchet@55966
   366
fun dest_absumprodT absT repT n ms =
blanchet@55966
   367
  map2 dest_tupleT_balanced ms o dest_sumTN_balanced n o mk_repT absT repT;
traytel@55803
   368
blanchet@49264
   369
val mk_sumTN = Library.foldr1 mk_sumT;
blanchet@49264
   370
val mk_sumTN_balanced = Balanced_Tree.make mk_sumT;
blanchet@49255
   371
blanchet@55966
   372
fun mk_tupleT_balanced [] = HOLogic.unitT
blanchet@55966
   373
  | mk_tupleT_balanced Ts = Balanced_Tree.make HOLogic.mk_prodT Ts;
blanchet@55966
   374
blanchet@55966
   375
val mk_sumprodT_balanced = mk_sumTN_balanced o map mk_tupleT_balanced;
blanchet@55966
   376
blanchet@54923
   377
fun mk_proj T n k =
blanchet@54923
   378
  let val (binders, _) = strip_typeN n T in
blanchet@54923
   379
    fold_rev (fn T => fn t => Abs (Name.uu, T, t)) binders (Bound (n - k - 1))
blanchet@54923
   380
  end;
blanchet@54923
   381
traytel@53032
   382
fun mk_convol (f, g) =
traytel@53032
   383
  let
traytel@53032
   384
    val (fU, fTU) = `range_type (fastype_of f);
traytel@53032
   385
    val ((gT, gU), gTU) = `dest_funT (fastype_of g);
traytel@53032
   386
    val convolT = fTU --> gTU --> gT --> HOLogic.mk_prodT (fU, gU);
traytel@53032
   387
  in Const (@{const_name convol}, convolT) $ f $ g end;
blanchet@49368
   388
traytel@58443
   389
fun mk_rel_prod R S =
traytel@58443
   390
  let
traytel@58443
   391
    val ((A1, A2), RT) = `dest_pred2T (fastype_of R);
traytel@58443
   392
    val ((B1, B2), ST) = `dest_pred2T (fastype_of S);
traytel@58443
   393
    val rel_prodT = RT --> ST --> mk_pred2T (HOLogic.mk_prodT (A1, B1)) (HOLogic.mk_prodT (A2, B2));
traytel@58443
   394
  in Const (@{const_name rel_prod}, rel_prodT) $ R $ S end;
traytel@58443
   395
traytel@58443
   396
fun mk_rel_sum R S =
traytel@58443
   397
  let
traytel@58443
   398
    val ((A1, A2), RT) = `dest_pred2T (fastype_of R);
traytel@58443
   399
    val ((B1, B2), ST) = `dest_pred2T (fastype_of S);
traytel@58443
   400
    val rel_sumT = RT --> ST --> mk_pred2T (mk_sumT (A1, B1)) (mk_sumT (A2, B2));
traytel@58443
   401
  in Const (@{const_name rel_sum}, rel_sumT) $ R $ S end;
traytel@58443
   402
blanchet@49121
   403
fun Inl_const LT RT = Const (@{const_name Inl}, LT --> mk_sumT (LT, RT));
blanchet@49255
   404
fun mk_Inl RT t = Inl_const (fastype_of t) RT $ t;
blanchet@49121
   405
blanchet@49121
   406
fun Inr_const LT RT = Const (@{const_name Inr}, RT --> mk_sumT (LT, RT));
blanchet@49255
   407
fun mk_Inr LT t = Inr_const LT (fastype_of t) $ t;
blanchet@49121
   408
blanchet@55969
   409
fun mk_prod1 bound_Ts (t, u) =
blanchet@55969
   410
  HOLogic.pair_const (fastype_of1 (bound_Ts, t)) (fastype_of1 (bound_Ts, u)) $ t $ u;
blanchet@55969
   411
blanchet@55969
   412
fun mk_tuple1_balanced _ [] = HOLogic.unit
blanchet@55969
   413
  | mk_tuple1_balanced bound_Ts ts = Balanced_Tree.make (mk_prod1 bound_Ts) ts;
blanchet@55969
   414
blanchet@55969
   415
val mk_tuple_balanced = mk_tuple1_balanced [];
blanchet@55966
   416
traytel@55803
   417
fun mk_absumprod absT abs0 n k ts =
traytel@55803
   418
  let val abs = mk_abs absT abs0;
blanchet@55968
   419
  in abs $ Sum_Tree.mk_inj (domain_type (fastype_of abs)) n k (mk_tuple_balanced ts) end;
traytel@55803
   420
blanchet@55414
   421
fun mk_case_sum (f, g) =
blanchet@49129
   422
  let
blanchet@55968
   423
    val (fT, T') = dest_funT (fastype_of f);
blanchet@55968
   424
    val (gT, _) = dest_funT (fastype_of g);
blanchet@49129
   425
  in
blanchet@55968
   426
    Sum_Tree.mk_sumcase fT gT T' f g
blanchet@49129
   427
  end;
blanchet@49129
   428
blanchet@55414
   429
val mk_case_sumN = Library.foldr1 mk_case_sum;
blanchet@55414
   430
val mk_case_sumN_balanced = Balanced_Tree.make mk_case_sum;
blanchet@49176
   431
traytel@55803
   432
fun mk_tupled_fun f x xs =
traytel@55803
   433
  if xs = [x] then f else HOLogic.tupled_lambda x (Term.list_comb (f, xs));
traytel@55803
   434
blanchet@55968
   435
fun mk_case_absumprod absT rep fs xss xss' =
wenzelm@58634
   436
  HOLogic.mk_comp (mk_case_sumN_balanced (@{map 3} mk_tupled_fun fs (map mk_tuple_balanced xss) xss'),
blanchet@55968
   437
    mk_rep absT rep);
traytel@55803
   438
blanchet@53202
   439
fun If_const T = Const (@{const_name If}, HOLogic.boolT --> T --> T --> T);
blanchet@53202
   440
fun mk_If p t f = let val T = fastype_of t in If_const T $ p $ t $ f end;
blanchet@49275
   441
blanchet@48975
   442
fun mk_Field r =
blanchet@48975
   443
  let val T = fst (dest_relT (fastype_of r));
blanchet@48975
   444
  in Const (@{const_name Field}, mk_relT (T, T) --> HOLogic.mk_setT T) $ r end;
blanchet@48975
   445
blanchet@48975
   446
val mk_union = HOLogic.mk_binop @{const_name sup};
blanchet@48975
   447
blanchet@48975
   448
(*dangerous; use with monotonic, converging functions only!*)
blanchet@48975
   449
fun fixpoint eq f X = if subset eq (f X, X) then X else fixpoint eq f (f X);
blanchet@48975
   450
blanchet@48975
   451
(* stolen from "~~/src/HOL/Tools/Datatype/datatype_aux.ML" *)
blanchet@48975
   452
fun split_conj_thm th =
blanchet@49119
   453
  ((th RS conjunct1) :: split_conj_thm (th RS conjunct2)) handle THM _ => [th];
blanchet@48975
   454
blanchet@48975
   455
fun split_conj_prems limit th =
blanchet@48975
   456
  let
blanchet@48975
   457
    fun split n i th =
blanchet@48975
   458
      if i = n then th else split n (i + 1) (conjI RSN (i, th)) handle THM _ => th;
blanchet@48975
   459
  in split limit 1 th end;
blanchet@48975
   460
blanchet@49335
   461
fun mk_obj_sumEN_balanced n =
blanchet@49335
   462
  Balanced_Tree.make (fn (thm1, thm2) => thm1 RSN (1, thm2 RSN (2, @{thm obj_sumE_f})))
blanchet@49335
   463
    (replicate n asm_rl);
blanchet@49335
   464
blanchet@55966
   465
fun mk_tupled_allIN_balanced 0 = @{thm unit_all_impI}
blanchet@55966
   466
  | mk_tupled_allIN_balanced n =
blanchet@55966
   467
    let
blanchet@55966
   468
      val (tfrees, _) = BNF_Util.mk_TFrees n @{context};
blanchet@55966
   469
      val T = mk_tupleT_balanced tfrees;
blanchet@55966
   470
    in
blanchet@55966
   471
      @{thm asm_rl[of "ALL x. P x --> Q x" for P Q]}
wenzelm@59621
   472
      |> Drule.instantiate' [SOME (Thm.global_ctyp_of @{theory} T)] []
blanchet@55966
   473
      |> Raw_Simplifier.rewrite_goals_rule @{context} @{thms split_paired_All[THEN eq_reflection]}
blanchet@55966
   474
      |> (fn thm => impI RS funpow n (fn th => allI RS th) thm)
blanchet@55966
   475
      |> Thm.varifyT_global
blanchet@55966
   476
    end;
blanchet@49335
   477
traytel@55803
   478
fun mk_absumprodE type_definition ms =
blanchet@49335
   479
  let val n = length ms in
blanchet@55966
   480
    mk_obj_sumEN_balanced n OF map mk_tupled_allIN_balanced ms RS
traytel@55803
   481
      (type_definition RS @{thm type_copy_obj_one_point_absE})
blanchet@49335
   482
  end;
blanchet@49264
   483
blanchet@55414
   484
fun mk_sum_caseN 1 1 = refl
blanchet@55414
   485
  | mk_sum_caseN _ 1 = @{thm sum.case(1)}
blanchet@55414
   486
  | mk_sum_caseN 2 2 = @{thm sum.case(2)}
blanchet@55414
   487
  | mk_sum_caseN n k = trans OF [@{thm case_sum_step(2)}, mk_sum_caseN (n - 1) (k - 1)];
blanchet@49264
   488
blanchet@49264
   489
fun mk_sum_step base step thm =
blanchet@49264
   490
  if Thm.eq_thm_prop (thm, refl) then base else trans OF [step, thm];
blanchet@49264
   491
blanchet@55414
   492
fun mk_sum_caseN_balanced 1 1 = refl
blanchet@55414
   493
  | mk_sum_caseN_balanced n k =
blanchet@55642
   494
    Balanced_Tree.access {left = mk_sum_step @{thm sum.case(1)} @{thm case_sum_step(1)},
blanchet@55642
   495
      right = mk_sum_step @{thm sum.case(2)} @{thm case_sum_step(2)}, init = refl} n k;
blanchet@49130
   496
blanchet@55851
   497
fun mk_sum_Cinfinite [thm] = thm
blanchet@55851
   498
  | mk_sum_Cinfinite (thm :: thms) = @{thm Cinfinite_csum_weak} OF [thm, mk_sum_Cinfinite thms];
blanchet@55851
   499
blanchet@55851
   500
fun mk_sum_card_order [thm] = thm
blanchet@55851
   501
  | mk_sum_card_order (thm :: thms) = @{thm card_order_csum} OF [thm, mk_sum_card_order thms];
blanchet@55851
   502
desharna@58579
   503
fun mk_xtor_rel_co_induct_thm fp pre_rels pre_phis rels phis xs ys xtors xtor's tac lthy =
traytel@52505
   504
  let
traytel@52505
   505
    val pre_relphis = map (fn rel => Term.list_comb (rel, phis @ pre_phis)) pre_rels;
traytel@52505
   506
    val relphis = map (fn rel => Term.list_comb (rel, phis)) rels;
traytel@52505
   507
    fun mk_xtor fp' xtor x = if fp = fp' then xtor $ x else x;
traytel@52505
   508
    val dtor = mk_xtor Greatest_FP;
traytel@52505
   509
    val ctor = mk_xtor Least_FP;
traytel@52505
   510
    fun flip f x y = if fp = Greatest_FP then f y x else f x y;
traytel@52505
   511
traytel@52505
   512
    fun mk_prem pre_relphi phi x y xtor xtor' =
traytel@52505
   513
      HOLogic.mk_Trueprop (list_all_free [x, y] (flip (curry HOLogic.mk_imp)
traytel@52505
   514
        (pre_relphi $ (dtor xtor x) $ (dtor xtor' y)) (phi $ (ctor xtor x) $ (ctor xtor' y))));
wenzelm@58634
   515
    val prems = @{map 6} mk_prem pre_relphis pre_phis xs ys xtors xtor's;
traytel@52505
   516
traytel@52505
   517
    val concl = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
traytel@52505
   518
      (map2 (flip mk_leq) relphis pre_phis));
traytel@52505
   519
  in
traytel@52506
   520
    Goal.prove_sorry lthy (map (fst o dest_Free) (phis @ pre_phis)) prems concl tac
traytel@52505
   521
    |> Thm.close_derivation
traytel@52505
   522
    |> (fn thm => thm OF (replicate (length pre_rels) @{thm allI[OF allI[OF impI]]}))
traytel@52505
   523
  end;
traytel@52505
   524
traytel@58443
   525
fun mk_co_iter_transfer_thms fp pre_rels pre_iphis pre_ophis rels phis un_folds un_folds' tac lthy =
traytel@52731
   526
  let
traytel@58443
   527
    val pre_relphis = map (fn rel => Term.list_comb (rel, phis @ pre_iphis)) pre_rels;
traytel@52731
   528
    val relphis = map (fn rel => Term.list_comb (rel, phis)) rels;
traytel@52731
   529
    fun flip f x y = if fp = Greatest_FP then f y x else f x y;
traytel@52731
   530
traytel@58443
   531
    val arg_rels = map2 (flip mk_rel_fun) pre_relphis pre_ophis;
traytel@52731
   532
    fun mk_transfer relphi pre_phi un_fold un_fold' =
blanchet@55945
   533
      fold_rev mk_rel_fun arg_rels (flip mk_rel_fun relphi pre_phi) $ un_fold $ un_fold';
wenzelm@58634
   534
    val transfers = @{map 4} mk_transfer relphis pre_ophis un_folds un_folds';
traytel@52731
   535
traytel@58443
   536
    val goal = fold_rev Logic.all (phis @ pre_ophis)
traytel@52731
   537
      (HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj transfers));
traytel@52731
   538
  in
traytel@52731
   539
    Goal.prove_sorry lthy [] [] goal tac
traytel@52731
   540
    |> Thm.close_derivation
traytel@52731
   541
    |> split_conj_thm
traytel@52731
   542
  end;
traytel@52731
   543
traytel@52913
   544
fun mk_xtor_un_fold_o_map_thms fp is_rec m un_fold_unique xtor_maps xtor_un_folds sym_map_comps
traytel@52913
   545
    map_cong0s =
traytel@52913
   546
  let
traytel@52913
   547
    val n = length sym_map_comps;
blanchet@55966
   548
    val rewrite_comp_comp2 = case_fp fp @{thm rewriteR_comp_comp2} @{thm rewriteL_comp_comp2};
blanchet@55966
   549
    val rewrite_comp_comp = case_fp fp @{thm rewriteR_comp_comp} @{thm rewriteL_comp_comp};
blanchet@55966
   550
    val map_cong_passive_args1 = replicate m (case_fp fp @{thm id_comp} @{thm comp_id} RS fun_cong);
traytel@52913
   551
    val map_cong_active_args1 = replicate n (if is_rec
blanchet@55966
   552
      then case_fp fp @{thm convol_o} @{thm o_case_sum} RS fun_cong
traytel@52913
   553
      else refl);
blanchet@55966
   554
    val map_cong_passive_args2 = replicate m (case_fp fp @{thm comp_id} @{thm id_comp} RS fun_cong);
traytel@52913
   555
    val map_cong_active_args2 = replicate n (if is_rec
blanchet@55966
   556
      then case_fp fp @{thm map_prod_o_convol_id} @{thm case_sum_o_map_sum_id}
blanchet@55966
   557
      else case_fp fp @{thm id_comp} @{thm comp_id} RS fun_cong);
wenzelm@55990
   558
    fun mk_map_congs passive active =
wenzelm@55990
   559
      map (fn thm => thm OF (passive @ active) RS @{thm ext}) map_cong0s;
traytel@52913
   560
    val map_cong1s = mk_map_congs map_cong_passive_args1 map_cong_active_args1;
traytel@52913
   561
    val map_cong2s = mk_map_congs map_cong_passive_args2 map_cong_active_args2;
desharna@57489
   562
traytel@52913
   563
    fun mk_rewrites map_congs = map2 (fn sym_map_comp => fn map_cong =>
traytel@52913
   564
      mk_trans sym_map_comp map_cong RS rewrite_comp_comp) sym_map_comps map_congs;
traytel@52913
   565
    val rewrite1s = mk_rewrites map_cong1s;
traytel@52913
   566
    val rewrite2s = mk_rewrites map_cong2s;
traytel@52913
   567
    val unique_prems =
wenzelm@58634
   568
      @{map 4} (fn xtor_map => fn un_fold => fn rewrite1 => fn rewrite2 =>
traytel@52913
   569
        mk_trans (rewrite_comp_comp2 OF [xtor_map, un_fold])
traytel@52913
   570
          (mk_trans rewrite1 (mk_sym rewrite2)))
traytel@52913
   571
      xtor_maps xtor_un_folds rewrite1s rewrite2s;
traytel@52913
   572
  in
blanchet@55966
   573
    split_conj_thm (un_fold_unique OF map (case_fp fp I mk_sym) unique_prems)
traytel@52913
   574
  end;
traytel@52913
   575
blanchet@57983
   576
fun mk_coinduct_strong_thm coind rel_eqs rel_monos mk_vimage2p ctxt =
traytel@53105
   577
  let
traytel@53105
   578
    val n = Thm.nprems_of coind;
traytel@53106
   579
    val m = Thm.nprems_of (hd rel_monos) - n;
traytel@53105
   580
    fun mk_inst phi = (phi, mk_union (phi, HOLogic.eq_const (fst (dest_pred2T (fastype_of phi)))))
wenzelm@59621
   581
      |> apply2 (Thm.cterm_of ctxt);
traytel@53105
   582
    val insts = Term.add_vars (Thm.prop_of coind) [] |> rev |> take n |> map (mk_inst o Var);
traytel@53105
   583
    fun mk_unfold rel_eq rel_mono =
traytel@53105
   584
      let
traytel@53105
   585
        val eq = iffD2 OF [rel_eq RS @{thm predicate2_eqD}, refl];
traytel@53105
   586
        val mono = rel_mono OF (replicate m @{thm order_refl} @ replicate n @{thm eq_subset});
desharna@57529
   587
      in mk_vimage2p (eq RS (mono RS @{thm predicate2D})) RS eqTrueI end;
traytel@53105
   588
    val unfolds = map2 mk_unfold rel_eqs rel_monos @ @{thms sup_fun_def sup_bool_def
traytel@53105
   589
      imp_disjL all_conj_distrib subst_eq_imp simp_thms(18,21,35)};
traytel@53105
   590
  in
traytel@53105
   591
    Thm.instantiate ([], insts) coind
traytel@53105
   592
    |> unfold_thms ctxt unfolds
traytel@53105
   593
  end;
traytel@53105
   594
blanchet@55701
   595
fun fp_bnf construct_fp bs resBs Ds0 fp_eqs lthy =
blanchet@51868
   596
  let
traytel@53143
   597
    val time = time lthy;
blanchet@51868
   598
    val timer = time (Timer.startRealTimer ());
blanchet@58332
   599
blanchet@58332
   600
    val nn = length fp_eqs;
blanchet@53222
   601
    val (Xs, rhsXs) = split_list fp_eqs;
blanchet@48975
   602
blanchet@58332
   603
    fun flatten_tyargs Ass =
blanchet@53222
   604
      subtract (op =) Xs (filter (fn T => exists (fn Ts => member (op =) Ts T) Ass) resBs) @ Xs;
blanchet@51868
   605
blanchet@53263
   606
    fun raw_qualify base_b =
blanchet@53264
   607
      let val (_, qs, n) = Binding.dest base_b;
blanchet@53264
   608
      in
blanchet@53264
   609
        Binding.prefix_name rawN
blanchet@53264
   610
        #> fold_rev (fn (s, mand) => Binding.qualify mand s) (qs @ [(n, true)])
traytel@53566
   611
        #> Binding.conceal
blanchet@53264
   612
      end;
blanchet@51868
   613
blanchet@55904
   614
    val ((bnfs, (deadss, livess)), accum) =
blanchet@55706
   615
      apfst (apsnd split_list o split_list)
wenzelm@58634
   616
        (@{fold_map 2}
wenzelm@58634
   617
          (fn b => bnf_of_typ Smart_Inline (raw_qualify b) flatten_tyargs Xs Ds0) bs rhsXs
blanchet@55706
   618
          ((empty_comp_cache, empty_unfolds), lthy));
blanchet@51868
   619
traytel@53566
   620
    fun norm_qualify i = Binding.qualify true (Binding.name_of (nth bs (Int.max (0, i - 1))))
traytel@53566
   621
      #> Binding.conceal;
blanchet@48975
   622
blanchet@49132
   623
    val Ass = map (map dest_TFree) livess;
blanchet@58332
   624
    val Ds' = fold (fold Term.add_tfreesT) deadss [];
blanchet@58332
   625
    val Ds = union (op =) Ds' Ds0;
blanchet@58332
   626
    val missing = resBs |> fold (subtract (op =)) (Ds' :: Ass);
blanchet@58332
   627
    val (dead_phantoms, live_phantoms) = List.partition (member (op =) Ds0) missing;
blanchet@58332
   628
    val resBs' = resBs |> fold (subtract (op =)) [dead_phantoms, Ds];
blanchet@49132
   629
blanchet@48975
   630
    val timer = time (timer "Construction of BNFs");
blanchet@48975
   631
blanchet@55904
   632
    val ((kill_poss, _), (bnfs', ((_, unfold_set'), lthy'))) =
blanchet@58332
   633
      normalize_bnfs norm_qualify Ass Ds (K (resBs' @ Xs)) bnfs accum;
blanchet@48975
   634
wenzelm@58634
   635
    val Dss = @{map 3} (uncurry append oo curry swap oo map o nth) livess kill_poss deadss;
blanchet@48975
   636
traytel@53566
   637
    fun pre_qualify b = Binding.qualify false (Binding.name_of b)
blanchet@58208
   638
      #> not (Config.get lthy' bnf_note_all) ? Binding.conceal;
blanchet@53264
   639
traytel@55803
   640
    val ((pre_bnfs, (deadss, absT_infos)), lthy'') =
wenzelm@58634
   641
      @{fold_map 4} (fn b => seal_bnf (pre_qualify b) unfold_set' (Binding.prefix_name preN b))
blanchet@58332
   642
        bs (not (null live_phantoms) :: replicate (nn - 1) false) Dss bnfs' lthy'
traytel@55803
   643
      |>> split_list
traytel@55803
   644
      |>> apsnd split_list;
blanchet@48975
   645
blanchet@48975
   646
    val timer = time (timer "Normalization & sealing of BNFs");
blanchet@48975
   647
blanchet@58332
   648
    val res = construct_fp bs resBs (map TFree dead_phantoms, deadss) pre_bnfs absT_infos lthy'';
blanchet@48975
   649
blanchet@48975
   650
    val timer = time (timer "FP construction in total");
blanchet@48975
   651
  in
traytel@55803
   652
    timer; ((pre_bnfs, absT_infos), res)
blanchet@48975
   653
  end;
blanchet@48975
   654
blanchet@58256
   655
fun fp_antiquote_setup binding =
blanchet@58256
   656
  Thy_Output.antiquotation binding (Args.type_name {proper = true, strict = true})
blanchet@58256
   657
    (fn {source = src, context = ctxt, ...} => fn fcT_name =>
blanchet@58256
   658
       (case Ctr_Sugar.ctr_sugar_of ctxt fcT_name of
blanchet@58256
   659
         NONE => error ("Not a known freely generated type name: " ^ quote fcT_name)
blanchet@58256
   660
       | SOME {T = T0, ctrs = ctrs0, ...} =>
blanchet@58256
   661
         let
blanchet@58256
   662
           val freezeT = Term.map_atyps (fn TVar ((s, _), S) => TFree (s, S) | T => T);
blanchet@58256
   663
blanchet@58256
   664
           val T = freezeT T0;
blanchet@58256
   665
           val ctrs = map (Term.map_types freezeT) ctrs0;
blanchet@58256
   666
blanchet@58256
   667
           val pretty_typ_bracket = Syntax.pretty_typ (Config.put pretty_priority 1001 ctxt);
blanchet@58256
   668
           fun pretty_ctr ctr =
blanchet@58256
   669
             Pretty.block (Pretty.breaks (Syntax.pretty_term ctxt ctr ::
blanchet@58256
   670
               map pretty_typ_bracket (binder_types (fastype_of ctr))));
blanchet@58256
   671
           val pretty_co_datatype =
blanchet@58256
   672
             Pretty.block (Pretty.keyword1 (Binding.name_of binding) :: Pretty.brk 1 ::
blanchet@58256
   673
               Syntax.pretty_typ ctxt T :: Pretty.str " =" :: Pretty.brk 1 ::
blanchet@58256
   674
               flat (separate [Pretty.brk 1, Pretty.str "| "] (map (single o pretty_ctr) ctrs)));
blanchet@58256
   675
         in
blanchet@58256
   676
           Thy_Output.output ctxt
blanchet@58256
   677
             (Thy_Output.maybe_pretty_source (K (K pretty_co_datatype)) ctxt src [()])
blanchet@58256
   678
         end));
blanchet@58256
   679
blanchet@48975
   680
end;