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