src/HOL/BNF/Tools/bnf_fp_util.ML
author traytel
Mon Jul 15 15:50:39 2013 +0200 (2013-07-15)
changeset 52660 7f7311d04727
parent 52506 eb80a16a2b72
child 52731 dacd47a0633f
permissions -rw-r--r--
killed unused theorems
blanchet@51850
     1
(*  Title:      HOL/BNF/Tools/bnf_fp_util.ML
blanchet@48975
     2
    Author:     Dmitriy Traytel, TU Muenchen
blanchet@51823
     3
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@51823
     4
    Copyright   2012, 2013
blanchet@48975
     5
blanchet@49389
     6
Shared library for the datatype and codatatype constructions.
blanchet@48975
     7
*)
blanchet@48975
     8
blanchet@51850
     9
signature BNF_FP_UTIL =
blanchet@48975
    10
sig
blanchet@52207
    11
  datatype fp_kind = Least_FP | Greatest_FP
blanchet@52207
    12
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@52328
    18
     xtor_co_iterss: term list list,
blanchet@52344
    19
     xtor_co_inducts: thm list,
blanchet@51819
    20
     dtor_ctors: thm list,
blanchet@51819
    21
     ctor_dtors: thm list,
blanchet@51819
    22
     ctor_injects: thm list,
blanchet@52314
    23
     xtor_map_thms: thm list,
blanchet@52314
    24
     xtor_set_thmss: thm list list,
blanchet@52314
    25
     xtor_rel_thms: thm list,
blanchet@52328
    26
     xtor_co_iter_thmss: thm list list}
blanchet@49591
    27
blanchet@51823
    28
  val morph_fp_result: morphism -> fp_result -> fp_result
blanchet@51823
    29
  val eq_fp_result: fp_result * fp_result -> bool
blanchet@52344
    30
  val co_induct_of: 'a list -> 'a
blanchet@52343
    31
  val strong_co_induct_of: 'a list -> 'a
blanchet@52330
    32
  val un_fold_of: 'a list -> 'a
blanchet@52330
    33
  val co_rec_of: 'a list -> 'a
blanchet@51823
    34
blanchet@48975
    35
  val time: Timer.real_timer -> string -> Timer.real_timer
blanchet@48975
    36
blanchet@48975
    37
  val IITN: string
blanchet@48975
    38
  val LevN: string
blanchet@48975
    39
  val algN: string
blanchet@48975
    40
  val behN: string
blanchet@48975
    41
  val bisN: string
blanchet@48975
    42
  val carTN: string
blanchet@49338
    43
  val caseN: string
blanchet@48975
    44
  val coN: string
blanchet@48975
    45
  val coinductN: string
blanchet@48975
    46
  val corecN: string
blanchet@49501
    47
  val ctorN: string
blanchet@49501
    48
  val ctor_dtorN: string
blanchet@49594
    49
  val ctor_dtor_corecN: string
blanchet@49594
    50
  val ctor_dtor_unfoldN: string
blanchet@49501
    51
  val ctor_exhaustN: string
blanchet@49501
    52
  val ctor_induct2N: string
blanchet@49501
    53
  val ctor_inductN: string
blanchet@49501
    54
  val ctor_injectN: string
blanchet@49504
    55
  val ctor_foldN: string
blanchet@49504
    56
  val ctor_fold_uniqueN: string
blanchet@49541
    57
  val ctor_mapN: string
blanchet@49543
    58
  val ctor_map_uniqueN: string
blanchet@49501
    59
  val ctor_recN: string
traytel@51739
    60
  val ctor_rec_uniqueN: string
blanchet@49518
    61
  val ctor_relN: string
blanchet@49544
    62
  val ctor_set_inclN: string
blanchet@49544
    63
  val ctor_set_set_inclN: string
blanchet@49594
    64
  val disc_unfoldN: string
blanchet@49594
    65
  val disc_unfold_iffN: string
blanchet@49594
    66
  val disc_corecN: string
blanchet@49594
    67
  val disc_corec_iffN: string
blanchet@49501
    68
  val dtorN: string
blanchet@49582
    69
  val dtor_coinductN: string
blanchet@49501
    70
  val dtor_corecN: string
traytel@51739
    71
  val dtor_corec_uniqueN: string
blanchet@49518
    72
  val dtor_ctorN: string
blanchet@49501
    73
  val dtor_exhaustN: string
blanchet@49501
    74
  val dtor_injectN: string
blanchet@49545
    75
  val dtor_mapN: string
blanchet@49581
    76
  val dtor_map_coinductN: string
blanchet@49581
    77
  val dtor_map_strong_coinductN: string
blanchet@49543
    78
  val dtor_map_uniqueN: string
blanchet@49545
    79
  val dtor_relN: string
blanchet@49544
    80
  val dtor_set_inclN: string
blanchet@49544
    81
  val dtor_set_set_inclN: string
blanchet@49582
    82
  val dtor_strong_coinductN: string
blanchet@49516
    83
  val dtor_unfoldN: string
blanchet@49516
    84
  val dtor_unfold_uniqueN: string
blanchet@49020
    85
  val exhaustN: string
blanchet@49504
    86
  val foldN: string
blanchet@48975
    87
  val hsetN: string
blanchet@48975
    88
  val hset_recN: string
blanchet@48975
    89
  val inductN: string
blanchet@49019
    90
  val injectN: string
blanchet@48975
    91
  val isNodeN: string
blanchet@48975
    92
  val lsbisN: string
blanchet@49594
    93
  val mapN: string
blanchet@48975
    94
  val map_uniqueN: string
blanchet@48975
    95
  val min_algN: string
blanchet@48975
    96
  val morN: string
blanchet@49020
    97
  val nchotomyN: string
blanchet@48975
    98
  val recN: string
traytel@51918
    99
  val rel_coinductN: string
traytel@51918
   100
  val rel_inductN: string
blanchet@49592
   101
  val rel_injectN: string
blanchet@49592
   102
  val rel_distinctN: string
blanchet@48975
   103
  val rvN: string
blanchet@49594
   104
  val sel_corecN: string
blanchet@48975
   105
  val set_inclN: string
blanchet@48975
   106
  val set_set_inclN: string
blanchet@49594
   107
  val sel_unfoldN: string
blanchet@49585
   108
  val setsN: string
blanchet@49438
   109
  val simpsN: string
blanchet@48975
   110
  val strTN: string
blanchet@48975
   111
  val str_initN: string
blanchet@49591
   112
  val strong_coinductN: string
blanchet@48975
   113
  val sum_bdN: string
blanchet@48975
   114
  val sum_bdTN: string
blanchet@49504
   115
  val unfoldN: string
blanchet@48975
   116
  val uniqueN: string
blanchet@48975
   117
blanchet@49585
   118
  (* TODO: Don't index set facts. Isabelle packages traditionally generate uniform names. *)
blanchet@49584
   119
  val mk_ctor_setN: int -> string
blanchet@49584
   120
  val mk_dtor_setN: int -> string
blanchet@49542
   121
  val mk_dtor_set_inductN: int -> string
blanchet@48975
   122
  val mk_set_inductN: int -> string
blanchet@48975
   123
blanchet@52207
   124
  val datatype_word: fp_kind -> string
blanchet@51863
   125
blanchet@51858
   126
  val base_name_of_typ: typ -> string
blanchet@49498
   127
  val mk_common_name: string list -> string
blanchet@49327
   128
blanchet@51858
   129
  val variant_types: string list -> sort list -> Proof.context ->
blanchet@51858
   130
    (string * sort) list * Proof.context
blanchet@51858
   131
  val variant_tfrees: string list -> Proof.context -> typ list * Proof.context
blanchet@51858
   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@49264
   137
  val mk_sumTN_balanced: typ list -> typ
blanchet@49255
   138
blanchet@49368
   139
  val id_const: typ -> term
blanchet@49368
   140
blanchet@49121
   141
  val Inl_const: typ -> typ -> term
blanchet@49121
   142
  val Inr_const: typ -> typ -> term
blanchet@49121
   143
blanchet@49255
   144
  val mk_Inl: typ -> term -> term
blanchet@49255
   145
  val mk_Inr: typ -> term -> term
blanchet@49121
   146
  val mk_InN: typ list -> term -> int -> term
blanchet@49264
   147
  val mk_InN_balanced: typ -> int -> term -> int -> term
blanchet@49255
   148
  val mk_sum_case: term * term -> term
blanchet@49129
   149
  val mk_sum_caseN: term list -> term
blanchet@49264
   150
  val mk_sum_caseN_balanced: term list -> term
blanchet@49121
   151
blanchet@49255
   152
  val dest_sumT: typ -> typ * typ
blanchet@49176
   153
  val dest_sumTN: int -> typ -> typ list
blanchet@49264
   154
  val dest_sumTN_balanced: int -> typ -> typ list
blanchet@49176
   155
  val dest_tupleT: int -> typ -> typ list
blanchet@49176
   156
blanchet@48975
   157
  val mk_Field: term -> term
blanchet@49275
   158
  val mk_If: term -> term -> term -> term
blanchet@48975
   159
  val mk_union: term * term -> term
blanchet@48975
   160
blanchet@49125
   161
  val mk_sumEN: int -> thm
blanchet@49264
   162
  val mk_sumEN_balanced: int -> thm
blanchet@49335
   163
  val mk_sumEN_tupled_balanced: int list -> thm
blanchet@49130
   164
  val mk_sum_casesN: int -> int -> thm
blanchet@49264
   165
  val mk_sum_casesN_balanced: int -> int -> thm
blanchet@49125
   166
blanchet@48975
   167
  val fixpoint: ('a * 'a -> bool) -> ('a list -> 'a list) -> 'a list -> 'a list
blanchet@48975
   168
traytel@52505
   169
  val mk_rel_co_induct_thm: fp_kind -> term list -> term list -> term list -> term list ->
traytel@52505
   170
    term list -> term list -> term list -> term list ->
traytel@52505
   171
    ({prems: thm list, context: Proof.context} -> tactic) -> Proof.context -> thm
traytel@52505
   172
blanchet@51867
   173
  val fp_bnf: (binding list -> (string * sort) list -> typ list * typ list list ->
blanchet@51868
   174
      BNF_Def.bnf list -> local_theory -> 'a) ->
blanchet@51868
   175
    binding list -> (string * sort) list -> ((string * sort) * typ) list -> local_theory ->
blanchet@51868
   176
    BNF_Def.bnf list * 'a
blanchet@48975
   177
end;
blanchet@48975
   178
blanchet@51850
   179
structure BNF_FP_Util : BNF_FP_UTIL =
blanchet@48975
   180
struct
blanchet@48975
   181
blanchet@48975
   182
open BNF_Comp
blanchet@48975
   183
open BNF_Def
blanchet@48975
   184
open BNF_Util
blanchet@48975
   185
blanchet@52207
   186
datatype fp_kind = Least_FP | Greatest_FP;
blanchet@52207
   187
blanchet@49591
   188
type fp_result =
blanchet@51859
   189
  {Ts: typ list,
blanchet@51859
   190
   bnfs: BNF_Def.bnf list,
blanchet@51839
   191
   ctors: term list,
blanchet@51819
   192
   dtors: term list,
blanchet@52328
   193
   xtor_co_iterss: term list list,
blanchet@52344
   194
   xtor_co_inducts: thm list,
blanchet@51819
   195
   dtor_ctors: thm list,
blanchet@51819
   196
   ctor_dtors: thm list,
blanchet@51819
   197
   ctor_injects: thm list,
blanchet@52314
   198
   xtor_map_thms: thm list,
blanchet@52314
   199
   xtor_set_thmss: thm list list,
blanchet@52314
   200
   xtor_rel_thms: thm list,
blanchet@52328
   201
   xtor_co_iter_thmss: thm list list};
blanchet@49591
   202
blanchet@52344
   203
fun morph_fp_result phi {Ts, bnfs, ctors, dtors, xtor_co_iterss, xtor_co_inducts, dtor_ctors,
blanchet@52344
   204
    ctor_dtors, ctor_injects, xtor_map_thms, xtor_set_thmss, xtor_rel_thms, xtor_co_iter_thmss} =
blanchet@51859
   205
  {Ts = map (Morphism.typ phi) Ts,
blanchet@51859
   206
   bnfs = map (morph_bnf phi) bnfs,
blanchet@51839
   207
   ctors = map (Morphism.term phi) ctors,
blanchet@51823
   208
   dtors = map (Morphism.term phi) dtors,
blanchet@52328
   209
   xtor_co_iterss = map (map (Morphism.term phi)) xtor_co_iterss,
blanchet@52344
   210
   xtor_co_inducts = map (Morphism.thm phi) xtor_co_inducts,
blanchet@51823
   211
   dtor_ctors = map (Morphism.thm phi) dtor_ctors,
blanchet@51823
   212
   ctor_dtors = map (Morphism.thm phi) ctor_dtors,
blanchet@51823
   213
   ctor_injects = map (Morphism.thm phi) ctor_injects,
blanchet@52314
   214
   xtor_map_thms = map (Morphism.thm phi) xtor_map_thms,
blanchet@52314
   215
   xtor_set_thmss = map (map (Morphism.thm phi)) xtor_set_thmss,
blanchet@52314
   216
   xtor_rel_thms = map (Morphism.thm phi) xtor_rel_thms,
blanchet@52328
   217
   xtor_co_iter_thmss = map (map (Morphism.thm phi)) xtor_co_iter_thmss};
blanchet@51823
   218
blanchet@51823
   219
fun eq_fp_result ({bnfs = bnfs1, ...} : fp_result, {bnfs = bnfs2, ...} : fp_result) =
blanchet@51823
   220
  eq_list eq_bnf (bnfs1, bnfs2);
blanchet@51823
   221
blanchet@52344
   222
fun co_induct_of (i :: _) = i;
blanchet@52343
   223
fun strong_co_induct_of [_, s] = s;
blanchet@52343
   224
blanchet@52330
   225
fun un_fold_of [f, _] = f;
blanchet@52330
   226
fun co_rec_of [_, r] = r;
blanchet@52330
   227
blanchet@48975
   228
val timing = true;
blanchet@48975
   229
fun time timer msg = (if timing
blanchet@52031
   230
  then warning (msg ^ ": " ^ ATP_Util.string_of_time (Timer.checkRealTimer timer))
blanchet@48975
   231
  else (); Timer.startRealTimer ());
blanchet@48975
   232
blanchet@49223
   233
val preN = "pre_"
blanchet@49223
   234
val rawN = "raw_"
blanchet@49218
   235
blanchet@48975
   236
val coN = "co"
blanchet@49504
   237
val unN = "un"
blanchet@48975
   238
val algN = "alg"
blanchet@48975
   239
val IITN = "IITN"
blanchet@49504
   240
val foldN = "fold"
blanchet@49504
   241
val unfoldN = unN ^ foldN
blanchet@48975
   242
val uniqueN = "_unique"
blanchet@49438
   243
val simpsN = "simps"
blanchet@49501
   244
val ctorN = "ctor"
blanchet@49501
   245
val dtorN = "dtor"
blanchet@49504
   246
val ctor_foldN = ctorN ^ "_" ^ foldN
blanchet@49504
   247
val dtor_unfoldN = dtorN ^ "_" ^ unfoldN
blanchet@49504
   248
val ctor_fold_uniqueN = ctor_foldN ^ uniqueN
blanchet@49504
   249
val dtor_unfold_uniqueN = dtor_unfoldN ^ uniqueN
blanchet@49594
   250
val ctor_dtor_unfoldN = ctorN ^ "_" ^ dtor_unfoldN
blanchet@49541
   251
val ctor_mapN = ctorN ^ "_" ^ mapN
blanchet@49541
   252
val dtor_mapN = dtorN ^ "_" ^ mapN
blanchet@48975
   253
val map_uniqueN = mapN ^ uniqueN
blanchet@49543
   254
val ctor_map_uniqueN = ctorN ^ "_" ^ map_uniqueN
blanchet@49543
   255
val dtor_map_uniqueN = dtorN ^ "_" ^ map_uniqueN
blanchet@48975
   256
val min_algN = "min_alg"
blanchet@48975
   257
val morN = "mor"
blanchet@48975
   258
val bisN = "bis"
blanchet@48975
   259
val lsbisN = "lsbis"
blanchet@48975
   260
val sum_bdTN = "sbdT"
blanchet@48975
   261
val sum_bdN = "sbd"
blanchet@48975
   262
val carTN = "carT"
blanchet@48975
   263
val strTN = "strT"
blanchet@48975
   264
val isNodeN = "isNode"
blanchet@48975
   265
val LevN = "Lev"
blanchet@48975
   266
val rvN = "recover"
blanchet@48975
   267
val behN = "beh"
blanchet@49585
   268
val setsN = "sets"
blanchet@49584
   269
val mk_ctor_setN = prefix (ctorN ^ "_") o mk_setN
blanchet@49584
   270
val mk_dtor_setN = prefix (dtorN ^ "_") o mk_setN
blanchet@48975
   271
fun mk_set_inductN i = mk_setN i ^ "_induct"
blanchet@49542
   272
val mk_dtor_set_inductN = prefix (dtorN ^ "_") o mk_set_inductN
blanchet@48975
   273
blanchet@48975
   274
val str_initN = "str_init"
blanchet@48975
   275
val recN = "rec"
blanchet@48975
   276
val corecN = coN ^ recN
blanchet@49501
   277
val ctor_recN = ctorN ^ "_" ^ recN
traytel@51739
   278
val ctor_rec_uniqueN = ctor_recN ^ uniqueN
blanchet@49501
   279
val dtor_corecN = dtorN ^ "_" ^ corecN
traytel@51739
   280
val dtor_corec_uniqueN = dtor_corecN ^ uniqueN
blanchet@49594
   281
val ctor_dtor_corecN = ctorN ^ "_" ^ dtor_corecN
blanchet@48975
   282
blanchet@49501
   283
val ctor_dtorN = ctorN ^ "_" ^ dtorN
blanchet@49501
   284
val dtor_ctorN = dtorN ^ "_" ^ ctorN
blanchet@49020
   285
val nchotomyN = "nchotomy"
blanchet@49019
   286
val injectN = "inject"
blanchet@49020
   287
val exhaustN = "exhaust"
blanchet@49585
   288
val ctor_injectN = ctorN ^ "_" ^ injectN
blanchet@49585
   289
val ctor_exhaustN = ctorN ^ "_" ^ exhaustN
blanchet@49585
   290
val dtor_injectN = dtorN ^ "_" ^ injectN
blanchet@49585
   291
val dtor_exhaustN = dtorN ^ "_" ^ exhaustN
blanchet@49545
   292
val ctor_relN = ctorN ^ "_" ^ relN
blanchet@49545
   293
val dtor_relN = dtorN ^ "_" ^ relN
blanchet@48975
   294
val inductN = "induct"
blanchet@48975
   295
val coinductN = coN ^ inductN
blanchet@49501
   296
val ctor_inductN = ctorN ^ "_" ^ inductN
blanchet@49501
   297
val ctor_induct2N = ctor_inductN ^ "2"
blanchet@49581
   298
val dtor_map_coinductN = dtor_mapN ^ "_" ^ coinductN
blanchet@49582
   299
val dtor_coinductN = dtorN ^ "_" ^ coinductN
blanchet@49591
   300
val strong_coinductN = "strong_" ^ coinductN
blanchet@49591
   301
val dtor_map_strong_coinductN = dtor_mapN ^ "_" ^ strong_coinductN
blanchet@49591
   302
val dtor_strong_coinductN = dtorN ^ "_" ^ strong_coinductN
blanchet@48975
   303
val hsetN = "Hset"
blanchet@48975
   304
val hset_recN = hsetN ^ "_rec"
blanchet@48975
   305
val set_inclN = "set_incl"
blanchet@49544
   306
val ctor_set_inclN = ctorN ^ "_" ^ set_inclN
blanchet@49544
   307
val dtor_set_inclN = dtorN ^ "_" ^ set_inclN
blanchet@48975
   308
val set_set_inclN = "set_set_incl"
blanchet@49544
   309
val ctor_set_set_inclN = ctorN ^ "_" ^ set_set_inclN
blanchet@49544
   310
val dtor_set_set_inclN = dtorN ^ "_" ^ set_set_inclN
blanchet@48975
   311
blanchet@49338
   312
val caseN = "case"
blanchet@49342
   313
val discN = "disc"
blanchet@49594
   314
val disc_unfoldN = discN ^ "_" ^ unfoldN
blanchet@49594
   315
val disc_corecN = discN ^ "_" ^ corecN
blanchet@49594
   316
val iffN = "_iff"
blanchet@49594
   317
val disc_unfold_iffN = discN ^ "_" ^ unfoldN ^ iffN
blanchet@49594
   318
val disc_corec_iffN = discN ^ "_" ^ corecN ^ iffN
blanchet@49592
   319
val distinctN = "distinct"
blanchet@49592
   320
val rel_distinctN = relN ^ "_" ^ distinctN
blanchet@49592
   321
val injectN = "inject"
blanchet@49592
   322
val rel_injectN = relN ^ "_" ^ injectN
traytel@51918
   323
val rel_coinductN = relN ^ "_" ^ coinductN
traytel@51918
   324
val rel_inductN = relN ^ "_" ^ inductN
blanchet@49342
   325
val selN = "sel"
blanchet@49594
   326
val sel_unfoldN = selN ^ "_" ^ unfoldN
blanchet@49594
   327
val sel_corecN = selN ^ "_" ^ corecN
blanchet@49338
   328
blanchet@52207
   329
fun datatype_word fp = (if fp = Greatest_FP then "co" else "") ^ "datatype";
blanchet@51863
   330
blanchet@51858
   331
fun add_components_of_typ (Type (s, Ts)) =
blanchet@51858
   332
    fold add_components_of_typ Ts #> cons (Long_Name.base_name s)
blanchet@51858
   333
  | add_components_of_typ _ = I;
blanchet@51858
   334
blanchet@51858
   335
fun base_name_of_typ T = space_implode "_" (add_components_of_typ T []);
blanchet@51858
   336
blanchet@49498
   337
val mk_common_name = space_implode "_";
blanchet@49327
   338
blanchet@49264
   339
fun dest_sumT (Type (@{type_name sum}, [T, T'])) = (T, T');
blanchet@49264
   340
blanchet@49264
   341
fun dest_sumTN 1 T = [T]
blanchet@49264
   342
  | dest_sumTN n (Type (@{type_name sum}, [T, T'])) = T :: dest_sumTN (n - 1) T';
blanchet@49264
   343
blanchet@49264
   344
val dest_sumTN_balanced = Balanced_Tree.dest dest_sumT;
blanchet@49264
   345
blanchet@49264
   346
(* TODO: move something like this to "HOLogic"? *)
blanchet@49264
   347
fun dest_tupleT 0 @{typ unit} = []
blanchet@49264
   348
  | dest_tupleT 1 T = [T]
blanchet@49264
   349
  | dest_tupleT n (Type (@{type_name prod}, [T, T'])) = T :: dest_tupleT (n - 1) T';
blanchet@49264
   350
blanchet@49264
   351
val mk_sumTN = Library.foldr1 mk_sumT;
blanchet@49264
   352
val mk_sumTN_balanced = Balanced_Tree.make mk_sumT;
blanchet@49255
   353
blanchet@49368
   354
fun id_const T = Const (@{const_name id}, T --> T);
blanchet@49368
   355
blanchet@49121
   356
fun Inl_const LT RT = Const (@{const_name Inl}, LT --> mk_sumT (LT, RT));
blanchet@49255
   357
fun mk_Inl RT t = Inl_const (fastype_of t) RT $ t;
blanchet@49121
   358
blanchet@49121
   359
fun Inr_const LT RT = Const (@{const_name Inr}, RT --> mk_sumT (LT, RT));
blanchet@49255
   360
fun mk_Inr LT t = Inr_const LT (fastype_of t) $ t;
blanchet@49121
   361
blanchet@49121
   362
fun mk_InN [_] t 1 = t
blanchet@49255
   363
  | mk_InN (_ :: Ts) t 1 = mk_Inl (mk_sumTN Ts) t
blanchet@49255
   364
  | mk_InN (LT :: Ts) t m = mk_Inr LT (mk_InN Ts t (m - 1))
blanchet@49121
   365
  | mk_InN Ts t _ = raise (TYPE ("mk_InN", Ts, [t]));
blanchet@49121
   366
blanchet@49264
   367
fun mk_InN_balanced sum_T n t k =
blanchet@49264
   368
  let
blanchet@49264
   369
    fun repair_types T (Const (s as @{const_name Inl}, _) $ t) = repair_inj_types T s fst t
blanchet@49264
   370
      | repair_types T (Const (s as @{const_name Inr}, _) $ t) = repair_inj_types T s snd t
blanchet@49264
   371
      | repair_types _ t = t
blanchet@49264
   372
    and repair_inj_types T s get t =
blanchet@49264
   373
      let val T' = get (dest_sumT T) in
blanchet@49264
   374
        Const (s, T' --> T) $ repair_types T' t
blanchet@49264
   375
      end;
blanchet@49264
   376
  in
blanchet@49264
   377
    Balanced_Tree.access {left = mk_Inl dummyT, right = mk_Inr dummyT, init = t} n k
blanchet@49264
   378
    |> repair_types sum_T
blanchet@49264
   379
  end;
blanchet@49264
   380
blanchet@49255
   381
fun mk_sum_case (f, g) =
blanchet@49129
   382
  let
blanchet@49129
   383
    val fT = fastype_of f;
blanchet@49129
   384
    val gT = fastype_of g;
blanchet@49129
   385
  in
blanchet@49129
   386
    Const (@{const_name sum_case},
blanchet@49129
   387
      fT --> gT --> mk_sumT (domain_type fT, domain_type gT) --> range_type fT) $ f $ g
blanchet@49129
   388
  end;
blanchet@49129
   389
blanchet@49264
   390
val mk_sum_caseN = Library.foldr1 mk_sum_case;
blanchet@49264
   391
val mk_sum_caseN_balanced = Balanced_Tree.make mk_sum_case;
blanchet@49176
   392
blanchet@49275
   393
fun mk_If p t f =
blanchet@49275
   394
  let val T = fastype_of t;
blanchet@49275
   395
  in Const (@{const_name If}, HOLogic.boolT --> T --> T --> T) $ p $ t $ f end;
blanchet@49275
   396
blanchet@48975
   397
fun mk_Field r =
blanchet@48975
   398
  let val T = fst (dest_relT (fastype_of r));
blanchet@48975
   399
  in Const (@{const_name Field}, mk_relT (T, T) --> HOLogic.mk_setT T) $ r end;
blanchet@48975
   400
blanchet@48975
   401
val mk_union = HOLogic.mk_binop @{const_name sup};
blanchet@48975
   402
blanchet@48975
   403
(*dangerous; use with monotonic, converging functions only!*)
blanchet@48975
   404
fun fixpoint eq f X = if subset eq (f X, X) then X else fixpoint eq f (f X);
blanchet@48975
   405
blanchet@51858
   406
fun variant_types ss Ss ctxt =
blanchet@51858
   407
  let
blanchet@51858
   408
    val (tfrees, _) =
blanchet@51858
   409
      fold_map2 (fn s => fn S => Name.variant s #> apfst (rpair S)) ss Ss (Variable.names_of ctxt);
blanchet@51858
   410
    val ctxt' = fold (Variable.declare_constraints o Logic.mk_type o TFree) tfrees ctxt;
blanchet@51858
   411
  in (tfrees, ctxt') end;
blanchet@51858
   412
blanchet@51858
   413
fun variant_tfrees ss =
blanchet@51858
   414
  apfst (map TFree) o variant_types (map (prefix "'") ss) (replicate (length ss) HOLogic.typeS);
blanchet@51858
   415
blanchet@48975
   416
(* stolen from "~~/src/HOL/Tools/Datatype/datatype_aux.ML" *)
blanchet@48975
   417
fun split_conj_thm th =
blanchet@49119
   418
  ((th RS conjunct1) :: split_conj_thm (th RS conjunct2)) handle THM _ => [th];
blanchet@48975
   419
blanchet@48975
   420
fun split_conj_prems limit th =
blanchet@48975
   421
  let
blanchet@48975
   422
    fun split n i th =
blanchet@48975
   423
      if i = n then th else split n (i + 1) (conjI RSN (i, th)) handle THM _ => th;
blanchet@48975
   424
  in split limit 1 th end;
blanchet@48975
   425
blanchet@49255
   426
fun mk_sumEN 1 = @{thm one_pointE}
traytel@49240
   427
  | mk_sumEN 2 = @{thm sumE}
traytel@49240
   428
  | mk_sumEN n =
traytel@52660
   429
    (fold (fn i => fn thm => @{thm obj_sumE_f} RSN (i, thm)) (2 upto n - 1) @{thm obj_sumE}) OF
traytel@49240
   430
      replicate n (impI RS allI);
blanchet@49125
   431
blanchet@49335
   432
fun mk_obj_sumEN_balanced n =
blanchet@49335
   433
  Balanced_Tree.make (fn (thm1, thm2) => thm1 RSN (1, thm2 RSN (2, @{thm obj_sumE_f})))
blanchet@49335
   434
    (replicate n asm_rl);
blanchet@49335
   435
blanchet@49335
   436
fun mk_sumEN_balanced' n all_impIs = mk_obj_sumEN_balanced n OF all_impIs RS @{thm obj_one_pointE};
blanchet@49335
   437
blanchet@49335
   438
fun mk_sumEN_balanced 1 = @{thm one_pointE} (*optimization*)
blanchet@49264
   439
  | mk_sumEN_balanced 2 = @{thm sumE} (*optimization*)
blanchet@49335
   440
  | mk_sumEN_balanced n = mk_sumEN_balanced' n (replicate n (impI RS allI));
blanchet@49335
   441
blanchet@49335
   442
fun mk_tupled_allIN 0 = @{thm unit_all_impI}
blanchet@49335
   443
  | mk_tupled_allIN 1 = @{thm impI[THEN allI]}
blanchet@49335
   444
  | mk_tupled_allIN 2 = @{thm prod_all_impI} (*optimization*)
blanchet@49335
   445
  | mk_tupled_allIN n = mk_tupled_allIN (n - 1) RS @{thm prod_all_impI_step};
blanchet@49335
   446
blanchet@49335
   447
fun mk_sumEN_tupled_balanced ms =
blanchet@49335
   448
  let val n = length ms in
blanchet@49335
   449
    if forall (curry (op =) 1) ms then mk_sumEN_balanced n
blanchet@49335
   450
    else mk_sumEN_balanced' n (map mk_tupled_allIN ms)
blanchet@49335
   451
  end;
blanchet@49264
   452
blanchet@49264
   453
fun mk_sum_casesN 1 1 = refl
blanchet@49130
   454
  | mk_sum_casesN _ 1 = @{thm sum.cases(1)}
blanchet@49130
   455
  | mk_sum_casesN 2 2 = @{thm sum.cases(2)}
blanchet@49264
   456
  | mk_sum_casesN n k = trans OF [@{thm sum_case_step(2)}, mk_sum_casesN (n - 1) (k - 1)];
blanchet@49264
   457
blanchet@49264
   458
fun mk_sum_step base step thm =
blanchet@49264
   459
  if Thm.eq_thm_prop (thm, refl) then base else trans OF [step, thm];
blanchet@49264
   460
blanchet@49264
   461
fun mk_sum_casesN_balanced 1 1 = refl
blanchet@49264
   462
  | mk_sum_casesN_balanced n k =
blanchet@49264
   463
    Balanced_Tree.access {left = mk_sum_step @{thm sum.cases(1)} @{thm sum_case_step(1)},
blanchet@49264
   464
      right = mk_sum_step @{thm sum.cases(2)} @{thm sum_case_step(2)}, init = refl} n k;
blanchet@49130
   465
traytel@52505
   466
fun mk_rel_co_induct_thm fp pre_rels pre_phis rels phis xs ys xtors xtor's tac lthy =
traytel@52505
   467
  let
traytel@52505
   468
    val pre_relphis = map (fn rel => Term.list_comb (rel, phis @ pre_phis)) pre_rels;
traytel@52505
   469
    val relphis = map (fn rel => Term.list_comb (rel, phis)) rels;
traytel@52505
   470
    fun mk_xtor fp' xtor x = if fp = fp' then xtor $ x else x;
traytel@52505
   471
    val dtor = mk_xtor Greatest_FP;
traytel@52505
   472
    val ctor = mk_xtor Least_FP;
traytel@52505
   473
    fun flip f x y = if fp = Greatest_FP then f y x else f x y;
traytel@52505
   474
traytel@52505
   475
    fun mk_prem pre_relphi phi x y xtor xtor' =
traytel@52505
   476
      HOLogic.mk_Trueprop (list_all_free [x, y] (flip (curry HOLogic.mk_imp)
traytel@52505
   477
        (pre_relphi $ (dtor xtor x) $ (dtor xtor' y)) (phi $ (ctor xtor x) $ (ctor xtor' y))));
traytel@52505
   478
    val prems = map6 mk_prem pre_relphis pre_phis xs ys xtors xtor's;
traytel@52505
   479
traytel@52505
   480
    val concl = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
traytel@52505
   481
      (map2 (flip mk_leq) relphis pre_phis));
traytel@52505
   482
  in
traytel@52506
   483
    Goal.prove_sorry lthy (map (fst o dest_Free) (phis @ pre_phis)) prems concl tac
traytel@52505
   484
    |> Thm.close_derivation
traytel@52505
   485
    |> (fn thm => thm OF (replicate (length pre_rels) @{thm allI[OF allI[OF impI]]}))
traytel@52505
   486
  end;
traytel@52505
   487
blanchet@51868
   488
fun fp_bnf construct_fp bs resBs eqs lthy =
blanchet@51868
   489
  let
blanchet@51868
   490
    val timer = time (Timer.startRealTimer ());
blanchet@51868
   491
    val (lhss, rhss) = split_list eqs;
blanchet@48975
   492
blanchet@51868
   493
    (* FIXME: because of "@ lhss", the output could contain type variables that are not in the
blanchet@51868
   494
       input; also, "fp_sort" should put the "resBs" first and in the order in which they appear *)
blanchet@51868
   495
    fun fp_sort Ass =
blanchet@51868
   496
      subtract (op =) lhss (filter (fn T => exists (fn Ts => member (op =) Ts T) Ass) resBs) @ lhss;
blanchet@51868
   497
blanchet@51868
   498
    fun raw_qualify b = Binding.qualify true (Binding.name_of (Binding.prefix_name rawN b));
blanchet@51868
   499
blanchet@51868
   500
    val ((bnfs, (deadss, livess)), (unfold_set, lthy)) = apfst (apsnd split_list o split_list)
blanchet@51868
   501
      (fold_map2 (fn b => bnf_of_typ Smart_Inline (raw_qualify b) fp_sort) bs rhss
blanchet@51868
   502
        (empty_unfolds, lthy));
blanchet@51868
   503
blanchet@49498
   504
    val name = mk_common_name (map Binding.name_of bs);
traytel@49425
   505
    fun qualify i =
traytel@49425
   506
      let val namei = name ^ nonzero_string_of_int i;
traytel@49425
   507
      in Binding.qualify true namei end;
blanchet@48975
   508
blanchet@49132
   509
    val Ass = map (map dest_TFree) livess;
blanchet@51866
   510
    val resDs = fold (subtract (op =)) Ass resBs;
traytel@49185
   511
    val Ds = fold (fold Term.add_tfreesT) deadss [];
blanchet@49132
   512
traytel@49156
   513
    val _ = (case Library.inter (op =) Ds lhss of [] => ()
blanchet@49591
   514
      | A :: _ => error ("Inadmissible type recursion (cannot take fixed point of dead type \
blanchet@49132
   515
        \variable " ^ quote (Syntax.string_of_typ lthy (TFree A)) ^ ")"));
blanchet@48975
   516
blanchet@48975
   517
    val timer = time (timer "Construction of BNFs");
blanchet@48975
   518
blanchet@49502
   519
    val ((kill_poss, _), (bnfs', (unfold_set', lthy'))) =
blanchet@51868
   520
      normalize_bnfs qualify Ass Ds fp_sort bnfs unfold_set lthy;
blanchet@48975
   521
blanchet@49132
   522
    val Dss = map3 (append oo map o nth) livess kill_poss deadss;
blanchet@48975
   523
blanchet@51865
   524
    val ((pre_bnfs, deadss), lthy'') =
blanchet@49502
   525
      fold_map3 (seal_bnf unfold_set') (map (Binding.prefix_name preN) bs) Dss bnfs' lthy'
traytel@49185
   526
      |>> split_list;
blanchet@48975
   527
blanchet@48975
   528
    val timer = time (timer "Normalization & sealing of BNFs");
blanchet@48975
   529
blanchet@51867
   530
    val res = construct_fp bs resBs (map TFree resDs, deadss) pre_bnfs lthy'';
blanchet@48975
   531
blanchet@48975
   532
    val timer = time (timer "FP construction in total");
blanchet@48975
   533
  in
blanchet@51865
   534
    timer; (pre_bnfs, res)
blanchet@48975
   535
  end;
blanchet@48975
   536
blanchet@48975
   537
end;