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