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