src/HOL/Codatatype/Tools/bnf_fp_util.ML
author blanchet
Wed Sep 12 17:26:05 2012 +0200 (2012-09-12)
changeset 49338 4a922800531d
parent 49337 538687a77075
child 49342 8ea4bad49ed5
permissions -rw-r--r--
tuning
blanchet@48975
     1
(*  Title:      HOL/Codatatype/Tools/bnf_fp_util.ML
blanchet@48975
     2
    Author:     Dmitriy Traytel, TU Muenchen
blanchet@48975
     3
    Copyright   2012
blanchet@48975
     4
blanchet@48975
     5
Shared library for the datatype and the codatatype construction.
blanchet@48975
     6
*)
blanchet@48975
     7
blanchet@48975
     8
signature BNF_FP_UTIL =
blanchet@48975
     9
sig
blanchet@48975
    10
  val time: Timer.real_timer -> string -> Timer.real_timer
blanchet@48975
    11
blanchet@48975
    12
  val IITN: string
blanchet@48975
    13
  val LevN: string
blanchet@48975
    14
  val algN: string
blanchet@48975
    15
  val behN: string
blanchet@48975
    16
  val bisN: string
blanchet@48975
    17
  val carTN: string
blanchet@49338
    18
  val caseN: string
blanchet@48975
    19
  val coN: string
blanchet@48975
    20
  val coinductN: string
blanchet@49338
    21
  val coinductsN: string
blanchet@48975
    22
  val coiterN: string
blanchet@49338
    23
  val coitersN: string
blanchet@48975
    24
  val corecN: string
blanchet@49338
    25
  val corecsN: string
blanchet@49338
    26
  val disc_coitersN: string
blanchet@49338
    27
  val disc_corecsN: string
blanchet@49020
    28
  val exhaustN: string
blanchet@48975
    29
  val fldN: string
blanchet@48975
    30
  val fld_exhaustN: string
blanchet@48975
    31
  val fld_induct2N: string
blanchet@48975
    32
  val fld_inductN: string
blanchet@48975
    33
  val fld_injectN: string
blanchet@49128
    34
  val fld_iterN: string
blanchet@49338
    35
  val fld_iter_uniqueN: string
blanchet@49222
    36
  val fld_itersN: string
blanchet@49128
    37
  val fld_recN: string
blanchet@49222
    38
  val fld_recsN: string
blanchet@48975
    39
  val fld_unfN: string
blanchet@49231
    40
  val fld_unf_coitersN: string
blanchet@49231
    41
  val fld_unf_corecsN: string
blanchet@48975
    42
  val hsetN: string
blanchet@48975
    43
  val hset_recN: string
blanchet@48975
    44
  val inductN: string
blanchet@49338
    45
  val inductsN: string
blanchet@49019
    46
  val injectN: string
blanchet@48975
    47
  val isNodeN: string
blanchet@48975
    48
  val iterN: string
blanchet@49338
    49
  val itersN: string
blanchet@48975
    50
  val lsbisN: string
blanchet@48975
    51
  val map_simpsN: string
blanchet@48975
    52
  val map_uniqueN: string
blanchet@48975
    53
  val min_algN: string
blanchet@48975
    54
  val morN: string
blanchet@49020
    55
  val nchotomyN: string
blanchet@48975
    56
  val pred_coinductN: string
blanchet@48975
    57
  val pred_coinduct_uptoN: string
blanchet@48975
    58
  val recN: string
blanchet@49338
    59
  val recsN: string
blanchet@48975
    60
  val rel_coinductN: string
blanchet@48975
    61
  val rel_coinduct_uptoN: string
blanchet@48975
    62
  val rvN: string
blanchet@49338
    63
  val sel_coitersN: string
blanchet@49338
    64
  val sel_corecsN: string
blanchet@48975
    65
  val set_inclN: string
blanchet@48975
    66
  val set_set_inclN: string
blanchet@48975
    67
  val strTN: string
blanchet@48975
    68
  val str_initN: string
blanchet@48975
    69
  val sum_bdN: string
blanchet@48975
    70
  val sum_bdTN: string
blanchet@48975
    71
  val unfN: string
blanchet@48975
    72
  val unf_coinductN: string
blanchet@48975
    73
  val unf_coinduct_uptoN: string
blanchet@49128
    74
  val unf_coiterN: string
blanchet@49338
    75
  val unf_coiter_uniqueN: string
blanchet@49222
    76
  val unf_coitersN: string
blanchet@49128
    77
  val unf_corecN: string
blanchet@49222
    78
  val unf_corecsN: string
blanchet@48975
    79
  val unf_exhaustN: string
blanchet@48975
    80
  val unf_fldN: string
blanchet@48975
    81
  val unf_injectN: string
blanchet@48975
    82
  val uniqueN: string
blanchet@48975
    83
  val uptoN: string
blanchet@48975
    84
blanchet@48975
    85
  val mk_exhaustN: string -> string
blanchet@48975
    86
  val mk_injectN: string -> string
blanchet@48975
    87
  val mk_nchotomyN: string -> string
blanchet@48975
    88
  val mk_set_simpsN: int -> string
blanchet@48975
    89
  val mk_set_minimalN: int -> string
blanchet@48975
    90
  val mk_set_inductN: int -> string
blanchet@48975
    91
blanchet@49337
    92
  val mk_common_name: binding list -> string
blanchet@49327
    93
blanchet@48975
    94
  val split_conj_thm: thm -> thm list
blanchet@48975
    95
  val split_conj_prems: int -> thm -> thm
blanchet@48975
    96
blanchet@49330
    97
  val retype_free: typ -> term -> term
blanchet@49330
    98
blanchet@49330
    99
  val mk_predT: typ -> typ;
blanchet@49330
   100
blanchet@49255
   101
  val mk_sumTN: typ list -> typ
blanchet@49264
   102
  val mk_sumTN_balanced: typ list -> typ
blanchet@49255
   103
blanchet@49121
   104
  val Inl_const: typ -> typ -> term
blanchet@49121
   105
  val Inr_const: typ -> typ -> term
blanchet@49121
   106
blanchet@49255
   107
  val mk_Inl: typ -> term -> term
blanchet@49255
   108
  val mk_Inr: typ -> term -> term
blanchet@49121
   109
  val mk_InN: typ list -> term -> int -> term
blanchet@49264
   110
  val mk_InN_balanced: typ -> int -> term -> int -> term
blanchet@49255
   111
  val mk_sum_case: term * term -> term
blanchet@49129
   112
  val mk_sum_caseN: term list -> term
blanchet@49264
   113
  val mk_sum_caseN_balanced: term list -> term
blanchet@49121
   114
blanchet@49255
   115
  val dest_sumT: typ -> typ * typ
blanchet@49176
   116
  val dest_sumTN: int -> typ -> typ list
blanchet@49264
   117
  val dest_sumTN_balanced: int -> typ -> typ list
blanchet@49176
   118
  val dest_tupleT: int -> typ -> typ list
blanchet@49176
   119
blanchet@48975
   120
  val mk_Field: term -> term
blanchet@49275
   121
  val mk_If: term -> term -> term -> term
blanchet@48975
   122
  val mk_union: term * term -> term
blanchet@48975
   123
blanchet@49125
   124
  val mk_sumEN: int -> thm
blanchet@49264
   125
  val mk_sumEN_balanced: int -> thm
blanchet@49335
   126
  val mk_sumEN_tupled_balanced: int list -> thm
blanchet@49130
   127
  val mk_sum_casesN: int -> int -> thm
blanchet@49264
   128
  val mk_sum_casesN_balanced: int -> int -> thm
blanchet@49125
   129
blanchet@48975
   130
  val mk_tactics: 'a -> 'a -> 'a -> 'a list -> 'a -> 'a -> 'a list -> 'a -> 'a -> 'a list
blanchet@48975
   131
blanchet@48975
   132
  val fixpoint: ('a * 'a -> bool) -> ('a list -> 'a list) -> 'a list -> 'a list
blanchet@48975
   133
traytel@49185
   134
  val fp_bnf: (mixfix list -> (string * sort) list option -> binding list ->
traytel@49185
   135
    typ list * typ list list -> BNF_Def.BNF list -> local_theory -> 'a) ->
blanchet@49169
   136
    binding list -> mixfix list -> (string * sort) list -> ((string * sort) * typ) list ->
blanchet@49226
   137
    local_theory -> BNF_Def.BNF list * 'a
traytel@49185
   138
  val fp_bnf_cmd: (mixfix list -> (string * sort) list option -> binding list ->
traytel@49185
   139
    typ list * typ list list -> BNF_Def.BNF list -> local_theory -> 'a) ->
blanchet@49134
   140
    binding list * (string list * string list) -> local_theory -> 'a
blanchet@48975
   141
end;
blanchet@48975
   142
blanchet@48975
   143
structure BNF_FP_Util : BNF_FP_UTIL =
blanchet@48975
   144
struct
blanchet@48975
   145
blanchet@48975
   146
open BNF_Comp
blanchet@48975
   147
open BNF_Def
blanchet@48975
   148
open BNF_Util
blanchet@48975
   149
blanchet@48975
   150
val timing = true;
blanchet@48975
   151
fun time timer msg = (if timing
blanchet@48975
   152
  then warning (msg ^ ": " ^ ATP_Util.string_from_time (Timer.checkRealTimer timer))
blanchet@48975
   153
  else (); Timer.startRealTimer ());
blanchet@48975
   154
blanchet@49223
   155
val preN = "pre_"
blanchet@49223
   156
val rawN = "raw_"
blanchet@49218
   157
blanchet@48975
   158
val coN = "co"
blanchet@48975
   159
val algN = "alg"
blanchet@48975
   160
val IITN = "IITN"
blanchet@48975
   161
val iterN = "iter"
blanchet@48975
   162
val coiterN = coN ^ iterN
blanchet@48975
   163
val uniqueN = "_unique"
blanchet@48975
   164
val fldN = "fld"
blanchet@48975
   165
val unfN = "unf"
blanchet@49128
   166
val fld_iterN = fldN ^ "_" ^ iterN
blanchet@49222
   167
val fld_itersN = fld_iterN ^ "s"
blanchet@49128
   168
val unf_coiterN = unfN ^ "_" ^ coiterN
blanchet@49222
   169
val unf_coitersN = unf_coiterN ^ "s"
blanchet@49128
   170
val fld_iter_uniqueN = fld_iterN ^ uniqueN
blanchet@49128
   171
val unf_coiter_uniqueN = unf_coiterN ^ uniqueN
blanchet@49225
   172
val fld_unf_coitersN = fldN ^ "_" ^ unf_coiterN ^ "s"
blanchet@48975
   173
val map_simpsN = mapN ^ "_simps"
blanchet@48975
   174
val map_uniqueN = mapN ^ uniqueN
blanchet@48975
   175
val min_algN = "min_alg"
blanchet@48975
   176
val morN = "mor"
blanchet@48975
   177
val bisN = "bis"
blanchet@48975
   178
val lsbisN = "lsbis"
blanchet@48975
   179
val sum_bdTN = "sbdT"
blanchet@48975
   180
val sum_bdN = "sbd"
blanchet@48975
   181
val carTN = "carT"
blanchet@48975
   182
val strTN = "strT"
blanchet@48975
   183
val isNodeN = "isNode"
blanchet@48975
   184
val LevN = "Lev"
blanchet@48975
   185
val rvN = "recover"
blanchet@48975
   186
val behN = "beh"
blanchet@48975
   187
fun mk_set_simpsN i = mk_setN i ^ "_simps"
blanchet@48975
   188
fun mk_set_minimalN i = mk_setN i ^ "_minimal"
blanchet@48975
   189
fun mk_set_inductN i = mk_setN i ^ "_induct"
blanchet@48975
   190
blanchet@48975
   191
val str_initN = "str_init"
blanchet@48975
   192
val recN = "rec"
blanchet@48975
   193
val corecN = coN ^ recN
blanchet@49128
   194
val fld_recN = fldN ^ "_" ^ recN
blanchet@49222
   195
val fld_recsN = fld_recN ^ "s"
blanchet@49128
   196
val unf_corecN = unfN ^ "_" ^ corecN
blanchet@49222
   197
val unf_corecsN = unf_corecN ^ "s"
blanchet@49231
   198
val fld_unf_corecsN = fldN ^ "_" ^ unf_corecN ^ "s"
blanchet@48975
   199
blanchet@48975
   200
val fld_unfN = fldN ^ "_" ^ unfN
blanchet@48975
   201
val unf_fldN = unfN ^ "_" ^ fldN
blanchet@49020
   202
val nchotomyN = "nchotomy"
blanchet@49020
   203
fun mk_nchotomyN s = s ^ "_" ^ nchotomyN
blanchet@49019
   204
val injectN = "inject"
blanchet@49019
   205
fun mk_injectN s = s ^ "_" ^ injectN
blanchet@49020
   206
val exhaustN = "exhaust"
blanchet@49020
   207
fun mk_exhaustN s = s ^ "_" ^ exhaustN
blanchet@48975
   208
val fld_injectN = mk_injectN fldN
blanchet@48975
   209
val fld_exhaustN = mk_exhaustN fldN
blanchet@48975
   210
val unf_injectN = mk_injectN unfN
blanchet@48975
   211
val unf_exhaustN = mk_exhaustN unfN
blanchet@48975
   212
val inductN = "induct"
blanchet@48975
   213
val coinductN = coN ^ inductN
blanchet@48975
   214
val fld_inductN = fldN ^ "_" ^ inductN
blanchet@48975
   215
val fld_induct2N = fld_inductN ^ "2"
blanchet@48975
   216
val unf_coinductN = unfN ^ "_" ^ coinductN
blanchet@48975
   217
val rel_coinductN = relN ^ "_" ^ coinductN
blanchet@48975
   218
val pred_coinductN = predN ^ "_" ^ coinductN
blanchet@48975
   219
val uptoN = "upto"
blanchet@48975
   220
val unf_coinduct_uptoN = unf_coinductN ^ "_" ^ uptoN
blanchet@48975
   221
val rel_coinduct_uptoN = rel_coinductN ^ "_" ^ uptoN
blanchet@48975
   222
val pred_coinduct_uptoN = pred_coinductN ^ "_" ^ uptoN
blanchet@48975
   223
val hsetN = "Hset"
blanchet@48975
   224
val hset_recN = hsetN ^ "_rec"
blanchet@48975
   225
val set_inclN = "set_incl"
blanchet@48975
   226
val set_set_inclN = "set_set_incl"
blanchet@48975
   227
blanchet@49338
   228
val caseN = "case"
blanchet@49338
   229
val coinductsN = "coinducts"
blanchet@49338
   230
val coitersN = "coiters"
blanchet@49338
   231
val corecsN = "corecs"
blanchet@49338
   232
val disc_coitersN = "disc_coiters"
blanchet@49338
   233
val disc_corecsN = "disc_corecs"
blanchet@49338
   234
val inductsN = "inducts"
blanchet@49338
   235
val itersN = "iters"
blanchet@49338
   236
val recsN = "recs"
blanchet@49338
   237
val sel_coitersN = "sel_coiters"
blanchet@49338
   238
val sel_corecsN = "sel_corecs"
blanchet@49338
   239
blanchet@49337
   240
val mk_common_name = space_implode "_" o map Binding.name_of;
blanchet@49327
   241
blanchet@49330
   242
fun mk_predT T = T --> HOLogic.boolT;
blanchet@49330
   243
blanchet@49330
   244
fun retype_free T (Free (s, _)) = Free (s, T);
blanchet@49330
   245
blanchet@49264
   246
fun dest_sumT (Type (@{type_name sum}, [T, T'])) = (T, T');
blanchet@49264
   247
blanchet@49264
   248
fun dest_sumTN 1 T = [T]
blanchet@49264
   249
  | dest_sumTN n (Type (@{type_name sum}, [T, T'])) = T :: dest_sumTN (n - 1) T';
blanchet@49264
   250
blanchet@49264
   251
val dest_sumTN_balanced = Balanced_Tree.dest dest_sumT;
blanchet@49264
   252
blanchet@49264
   253
(* TODO: move something like this to "HOLogic"? *)
blanchet@49264
   254
fun dest_tupleT 0 @{typ unit} = []
blanchet@49264
   255
  | dest_tupleT 1 T = [T]
blanchet@49264
   256
  | dest_tupleT n (Type (@{type_name prod}, [T, T'])) = T :: dest_tupleT (n - 1) T';
blanchet@49264
   257
blanchet@49264
   258
val mk_sumTN = Library.foldr1 mk_sumT;
blanchet@49264
   259
val mk_sumTN_balanced = Balanced_Tree.make mk_sumT;
blanchet@49255
   260
blanchet@49121
   261
fun Inl_const LT RT = Const (@{const_name Inl}, LT --> mk_sumT (LT, RT));
blanchet@49255
   262
fun mk_Inl RT t = Inl_const (fastype_of t) RT $ t;
blanchet@49121
   263
blanchet@49121
   264
fun Inr_const LT RT = Const (@{const_name Inr}, RT --> mk_sumT (LT, RT));
blanchet@49255
   265
fun mk_Inr LT t = Inr_const LT (fastype_of t) $ t;
blanchet@49121
   266
blanchet@49121
   267
fun mk_InN [_] t 1 = t
blanchet@49255
   268
  | mk_InN (_ :: Ts) t 1 = mk_Inl (mk_sumTN Ts) t
blanchet@49255
   269
  | mk_InN (LT :: Ts) t m = mk_Inr LT (mk_InN Ts t (m - 1))
blanchet@49121
   270
  | mk_InN Ts t _ = raise (TYPE ("mk_InN", Ts, [t]));
blanchet@49121
   271
blanchet@49264
   272
fun mk_InN_balanced sum_T n t k =
blanchet@49264
   273
  let
blanchet@49264
   274
    fun repair_types T (Const (s as @{const_name Inl}, _) $ t) = repair_inj_types T s fst t
blanchet@49264
   275
      | repair_types T (Const (s as @{const_name Inr}, _) $ t) = repair_inj_types T s snd t
blanchet@49264
   276
      | repair_types _ t = t
blanchet@49264
   277
    and repair_inj_types T s get t =
blanchet@49264
   278
      let val T' = get (dest_sumT T) in
blanchet@49264
   279
        Const (s, T' --> T) $ repair_types T' t
blanchet@49264
   280
      end;
blanchet@49264
   281
  in
blanchet@49264
   282
    Balanced_Tree.access {left = mk_Inl dummyT, right = mk_Inr dummyT, init = t} n k
blanchet@49264
   283
    |> repair_types sum_T
blanchet@49264
   284
  end;
blanchet@49264
   285
blanchet@49255
   286
fun mk_sum_case (f, g) =
blanchet@49129
   287
  let
blanchet@49129
   288
    val fT = fastype_of f;
blanchet@49129
   289
    val gT = fastype_of g;
blanchet@49129
   290
  in
blanchet@49129
   291
    Const (@{const_name sum_case},
blanchet@49129
   292
      fT --> gT --> mk_sumT (domain_type fT, domain_type gT) --> range_type fT) $ f $ g
blanchet@49129
   293
  end;
blanchet@49129
   294
blanchet@49264
   295
val mk_sum_caseN = Library.foldr1 mk_sum_case;
blanchet@49264
   296
val mk_sum_caseN_balanced = Balanced_Tree.make mk_sum_case;
blanchet@49176
   297
blanchet@49275
   298
fun mk_If p t f =
blanchet@49275
   299
  let val T = fastype_of t;
blanchet@49275
   300
  in Const (@{const_name If}, HOLogic.boolT --> T --> T --> T) $ p $ t $ f end;
blanchet@49275
   301
blanchet@48975
   302
fun mk_Field r =
blanchet@48975
   303
  let val T = fst (dest_relT (fastype_of r));
blanchet@48975
   304
  in Const (@{const_name Field}, mk_relT (T, T) --> HOLogic.mk_setT T) $ r end;
blanchet@48975
   305
blanchet@48975
   306
val mk_union = HOLogic.mk_binop @{const_name sup};
blanchet@48975
   307
blanchet@48975
   308
(*dangerous; use with monotonic, converging functions only!*)
blanchet@48975
   309
fun fixpoint eq f X = if subset eq (f X, X) then X else fixpoint eq f (f X);
blanchet@48975
   310
blanchet@48975
   311
(* stolen from "~~/src/HOL/Tools/Datatype/datatype_aux.ML" *)
blanchet@48975
   312
fun split_conj_thm th =
blanchet@49119
   313
  ((th RS conjunct1) :: split_conj_thm (th RS conjunct2)) handle THM _ => [th];
blanchet@48975
   314
blanchet@48975
   315
fun split_conj_prems limit th =
blanchet@48975
   316
  let
blanchet@48975
   317
    fun split n i th =
blanchet@48975
   318
      if i = n then th else split n (i + 1) (conjI RSN (i, th)) handle THM _ => th;
blanchet@48975
   319
  in split limit 1 th end;
blanchet@48975
   320
blanchet@49255
   321
fun mk_sumEN 1 = @{thm one_pointE}
traytel@49240
   322
  | mk_sumEN 2 = @{thm sumE}
traytel@49240
   323
  | mk_sumEN n =
traytel@49240
   324
    (fold (fn i => fn thm => @{thm obj_sum_step} RSN (i, thm)) (2 upto n - 1) @{thm obj_sumE}) OF
traytel@49240
   325
      replicate n (impI RS allI);
blanchet@49125
   326
blanchet@49335
   327
fun mk_obj_sumEN_balanced n =
blanchet@49335
   328
  Balanced_Tree.make (fn (thm1, thm2) => thm1 RSN (1, thm2 RSN (2, @{thm obj_sumE_f})))
blanchet@49335
   329
    (replicate n asm_rl);
blanchet@49335
   330
blanchet@49335
   331
fun mk_sumEN_balanced' n all_impIs = mk_obj_sumEN_balanced n OF all_impIs RS @{thm obj_one_pointE};
blanchet@49335
   332
blanchet@49335
   333
fun mk_sumEN_balanced 1 = @{thm one_pointE} (*optimization*)
blanchet@49264
   334
  | mk_sumEN_balanced 2 = @{thm sumE} (*optimization*)
blanchet@49335
   335
  | mk_sumEN_balanced n = mk_sumEN_balanced' n (replicate n (impI RS allI));
blanchet@49335
   336
blanchet@49335
   337
fun mk_tupled_allIN 0 = @{thm unit_all_impI}
blanchet@49335
   338
  | mk_tupled_allIN 1 = @{thm impI[THEN allI]}
blanchet@49335
   339
  | mk_tupled_allIN 2 = @{thm prod_all_impI} (*optimization*)
blanchet@49335
   340
  | mk_tupled_allIN n = mk_tupled_allIN (n - 1) RS @{thm prod_all_impI_step};
blanchet@49335
   341
blanchet@49335
   342
fun mk_sumEN_tupled_balanced ms =
blanchet@49335
   343
  let val n = length ms in
blanchet@49335
   344
    if forall (curry (op =) 1) ms then mk_sumEN_balanced n
blanchet@49335
   345
    else mk_sumEN_balanced' n (map mk_tupled_allIN ms)
blanchet@49335
   346
  end;
blanchet@49264
   347
blanchet@49264
   348
fun mk_sum_casesN 1 1 = refl
blanchet@49130
   349
  | mk_sum_casesN _ 1 = @{thm sum.cases(1)}
blanchet@49130
   350
  | mk_sum_casesN 2 2 = @{thm sum.cases(2)}
blanchet@49264
   351
  | mk_sum_casesN n k = trans OF [@{thm sum_case_step(2)}, mk_sum_casesN (n - 1) (k - 1)];
blanchet@49264
   352
blanchet@49264
   353
fun mk_sum_step base step thm =
blanchet@49264
   354
  if Thm.eq_thm_prop (thm, refl) then base else trans OF [step, thm];
blanchet@49264
   355
blanchet@49264
   356
fun mk_sum_casesN_balanced 1 1 = refl
blanchet@49264
   357
  | mk_sum_casesN_balanced n k =
blanchet@49264
   358
    Balanced_Tree.access {left = mk_sum_step @{thm sum.cases(1)} @{thm sum_case_step(1)},
blanchet@49264
   359
      right = mk_sum_step @{thm sum.cases(2)} @{thm sum_case_step(2)}, init = refl} n k;
blanchet@49130
   360
blanchet@48975
   361
fun mk_tactics mid mcomp mcong snat bdco bdinf sbd inbd wpull =
blanchet@48975
   362
  [mid, mcomp, mcong] @ snat @ [bdco, bdinf] @ sbd @ [inbd, wpull];
blanchet@48975
   363
blanchet@49141
   364
(* FIXME: because of "@ lhss", the output could contain type variables that are not in the input;
blanchet@49141
   365
   also, "fp_sort" should put the "resBs" first and in the order in which they appear *)
traytel@49185
   366
fun fp_sort lhss NONE Ass = Library.sort (Term_Ord.typ_ord o pairself TFree)
traytel@49185
   367
    (subtract (op =) lhss (fold (fold (insert (op =))) Ass [])) @ lhss
traytel@49185
   368
  | fp_sort lhss (SOME resBs) Ass =
traytel@49185
   369
    (subtract (op =) lhss (filter (fn T => exists (fn Ts => member (op =) Ts T) Ass) resBs)) @ lhss;
blanchet@48975
   370
traytel@49185
   371
fun mk_fp_bnf timer construct resBs bs sort lhss bnfs deadss livess unfold lthy =
blanchet@48975
   372
  let
blanchet@49337
   373
    val name = mk_common_name bs;
blanchet@48975
   374
    fun qualify i bind =
blanchet@48975
   375
      let val namei = if i > 0 then name ^ string_of_int i else name;
blanchet@48975
   376
      in
blanchet@48975
   377
        if member (op =) (#2 (Binding.dest bind)) (namei, true) then bind
blanchet@48975
   378
        else Binding.prefix_name namei bind
blanchet@48975
   379
      end;
blanchet@48975
   380
blanchet@49132
   381
    val Ass = map (map dest_TFree) livess;
traytel@49185
   382
    val resDs = (case resBs of NONE => [] | SOME Ts => fold (subtract (op =)) Ass Ts);
traytel@49185
   383
    val Ds = fold (fold Term.add_tfreesT) deadss [];
blanchet@49132
   384
traytel@49156
   385
    val _ = (case Library.inter (op =) Ds lhss of [] => ()
blanchet@49132
   386
      | A :: _ => error ("Nonadmissible type recursion (cannot take fixed point of dead type \
blanchet@49132
   387
        \variable " ^ quote (Syntax.string_of_typ lthy (TFree A)) ^ ")"));
blanchet@48975
   388
blanchet@48975
   389
    val timer = time (timer "Construction of BNFs");
blanchet@48975
   390
blanchet@48975
   391
    val ((kill_poss, _), (bnfs', (unfold', lthy'))) =
blanchet@48975
   392
      normalize_bnfs qualify Ass Ds sort bnfs unfold lthy;
blanchet@48975
   393
blanchet@49132
   394
    val Dss = map3 (append oo map o nth) livess kill_poss deadss;
blanchet@48975
   395
traytel@49185
   396
    val ((bnfs'', deadss), lthy'') =
blanchet@49223
   397
      fold_map3 (seal_bnf unfold') (map (Binding.prefix_name preN) bs) Dss bnfs' lthy'
traytel@49185
   398
      |>> split_list;
blanchet@48975
   399
blanchet@48975
   400
    val timer = time (timer "Normalization & sealing of BNFs");
blanchet@48975
   401
traytel@49185
   402
    val res = construct resBs bs (map TFree resDs, deadss) bnfs'' lthy'';
blanchet@48975
   403
blanchet@48975
   404
    val timer = time (timer "FP construction in total");
blanchet@48975
   405
  in
blanchet@49308
   406
    timer; (bnfs'', res)
blanchet@48975
   407
  end;
blanchet@48975
   408
blanchet@49169
   409
fun fp_bnf construct bs mixfixes resBs eqs lthy =
blanchet@48975
   410
  let
blanchet@48975
   411
    val timer = time (Timer.startRealTimer ());
blanchet@48975
   412
    val (lhss, rhss) = split_list eqs;
traytel@49185
   413
    val sort = fp_sort lhss (SOME resBs);
blanchet@48975
   414
    val ((bnfs, (Dss, Ass)), (unfold, lthy')) = apfst (apsnd split_list o split_list)
blanchet@49223
   415
      (fold_map2 (fn b => bnf_of_typ Smart_Inline (Binding.prefix_name rawN b) I sort) bs rhss
blanchet@48975
   416
        (empty_unfold, lthy));
blanchet@48975
   417
  in
traytel@49185
   418
    mk_fp_bnf timer (construct mixfixes) (SOME resBs) bs sort lhss bnfs Dss Ass unfold lthy'
blanchet@48975
   419
  end;
blanchet@48975
   420
blanchet@48975
   421
fun fp_bnf_cmd construct (bs, (raw_lhss, raw_bnfs)) lthy =
blanchet@48975
   422
  let
blanchet@48975
   423
    val timer = time (Timer.startRealTimer ());
blanchet@48975
   424
    val lhss = map (dest_TFree o Syntax.read_typ lthy) raw_lhss;
traytel@49185
   425
    val sort = fp_sort lhss NONE;
blanchet@48975
   426
    val ((bnfs, (Dss, Ass)), (unfold, lthy')) = apfst (apsnd split_list o split_list)
blanchet@48975
   427
      (fold_map2 (fn b => fn rawT =>
blanchet@49223
   428
        (bnf_of_typ Smart_Inline (Binding.prefix_name rawN b) I sort (Syntax.read_typ lthy rawT)))
traytel@49185
   429
      bs raw_bnfs (empty_unfold, lthy));
blanchet@48975
   430
  in
blanchet@49207
   431
    snd (mk_fp_bnf timer (construct (map (K NoSyn) bs)) NONE bs sort lhss bnfs Dss Ass unfold lthy')
blanchet@48975
   432
  end;
blanchet@48975
   433
blanchet@48975
   434
end;