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