src/HOL/BNF/Tools/bnf_fp_n2m_sugar.ML
author blanchet
Tue, 05 Nov 2013 05:48:08 +0100
changeset 55707 4f7c016d5bc6
parent 55706 d1478807f287
child 55708 4843082be7ef
permissions -rw-r--r--
also generalize fixed types
blanchet@54440
     1
(*  Title:      HOL/BNF/Tools/bnf_fp_n2m_sugar.ML
blanchet@54440
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@54440
     3
    Copyright   2013
blanchet@54440
     4
blanchet@54440
     5
Suggared flattening of nested to mutual (co)recursion.
blanchet@54440
     6
*)
blanchet@54440
     7
blanchet@54440
     8
signature BNF_FP_N2M_SUGAR =
blanchet@54440
     9
sig
blanchet@55695
    10
  val unfold_let: term -> term
blanchet@55695
    11
  val dest_map: Proof.context -> string -> term -> term * term list
blanchet@55695
    12
blanchet@55146
    13
  val mutualize_fp_sugars: bool -> BNF_FP_Util.fp_kind -> binding list -> typ list ->
blanchet@54440
    14
    (term -> int list) -> term list list list list -> BNF_FP_Def_Sugar.fp_sugar list ->
blanchet@54883
    15
    local_theory ->
blanchet@54883
    16
    (BNF_FP_Def_Sugar.fp_sugar list
blanchet@54883
    17
     * (BNF_FP_Def_Sugar.lfp_sugar_thms option * BNF_FP_Def_Sugar.gfp_sugar_thms option))
blanchet@54883
    18
    * local_theory
blanchet@54440
    19
  val pad_and_indexify_calls: BNF_FP_Def_Sugar.fp_sugar list -> int ->
blanchet@54440
    20
    (term * term list list) list list -> term list list list list
blanchet@55146
    21
  val nested_to_mutual_fps: BNF_FP_Util.fp_kind -> binding list -> typ list -> (term -> int list) ->
blanchet@55146
    22
    (term * term list list) list list -> local_theory ->
blanchet@54883
    23
    (typ list * int list * BNF_FP_Def_Sugar.fp_sugar list
blanchet@54883
    24
     * (BNF_FP_Def_Sugar.lfp_sugar_thms option * BNF_FP_Def_Sugar.gfp_sugar_thms option))
blanchet@54883
    25
    * local_theory
blanchet@54440
    26
end;
blanchet@54440
    27
blanchet@54440
    28
structure BNF_FP_N2M_Sugar : BNF_FP_N2M_SUGAR =
blanchet@54440
    29
struct
blanchet@54440
    30
blanchet@55143
    31
open Ctr_Sugar
blanchet@54440
    32
open BNF_Util
blanchet@54440
    33
open BNF_Def
blanchet@54440
    34
open BNF_FP_Util
blanchet@54440
    35
open BNF_FP_Def_Sugar
blanchet@54440
    36
open BNF_FP_N2M
blanchet@54440
    37
blanchet@54440
    38
val n2mN = "n2m_"
blanchet@54440
    39
blanchet@55695
    40
fun unfold_let (Const (@{const_name Let}, _) $ arg1 $ arg2) = unfold_let (betapply (arg2, arg1))
blanchet@55695
    41
  | unfold_let (Const (@{const_name prod_case}, _) $ t) =
blanchet@55695
    42
    (case unfold_let t of
blanchet@55695
    43
      t' as Abs (s1, T1, Abs (s2, T2, _)) =>
blanchet@55695
    44
      let
blanchet@55695
    45
        val x = (s1 ^ s2, Term.maxidx_of_term t + 1);
blanchet@55695
    46
        val v = Var (x, HOLogic.mk_prodT (T1, T2));
blanchet@55695
    47
      in
blanchet@55695
    48
        lambda v (unfold_let (betapplys (t', [HOLogic.mk_fst v, HOLogic.mk_snd v])))
blanchet@55695
    49
      end
blanchet@55695
    50
    | _ => t)
blanchet@55695
    51
  | unfold_let (t $ u) = betapply (unfold_let t, unfold_let u)
blanchet@55695
    52
  | unfold_let (Abs (s, T, t)) = Abs (s, T, unfold_let t)
blanchet@55695
    53
  | unfold_let t = t;
blanchet@55695
    54
blanchet@55695
    55
val dummy_var_name = "?f"
blanchet@55695
    56
blanchet@55695
    57
fun mk_map_pattern ctxt s =
blanchet@55695
    58
  let
blanchet@55695
    59
    val bnf = the (bnf_of ctxt s);
blanchet@55695
    60
    val mapx = map_of_bnf bnf;
blanchet@55695
    61
    val live = live_of_bnf bnf;
blanchet@55695
    62
    val (f_Ts, _) = strip_typeN live (fastype_of mapx);
blanchet@55695
    63
    val fs = map_index (fn (i, T) => Var ((dummy_var_name, i), T)) f_Ts;
blanchet@55695
    64
  in
blanchet@55695
    65
    (mapx, betapplys (mapx, fs))
blanchet@55695
    66
  end;
blanchet@55695
    67
blanchet@55695
    68
fun dest_map ctxt s call =
blanchet@55695
    69
  let
blanchet@55695
    70
    val (map0, pat) = mk_map_pattern ctxt s;
blanchet@55695
    71
    val (_, tenv) = fo_match ctxt call pat;
blanchet@55695
    72
  in
blanchet@55695
    73
    (map0, Vartab.fold_rev (fn (_, (_, f)) => cons f) tenv [])
blanchet@55695
    74
  end;
blanchet@55695
    75
blanchet@55695
    76
fun dest_abs_or_applied_map_or_ctr _ _ (Abs (_, _, t)) = (Term.dummy, [t])
blanchet@55695
    77
  | dest_abs_or_applied_map_or_ctr ctxt s (t as t1 $ _) =
blanchet@55695
    78
    (case try (dest_map ctxt s) t1 of
blanchet@55695
    79
      SOME res => res
blanchet@55695
    80
    | NONE =>
blanchet@55695
    81
      let
blanchet@55695
    82
        val thy = Proof_Context.theory_of ctxt;
blanchet@55695
    83
        val map_thms = of_fp_sugar #mapss (the (fp_sugar_of ctxt s))
blanchet@55695
    84
        val map_thms' = map (fn thm => thm RS sym RS eq_reflection) map_thms;
blanchet@55695
    85
        val t' = Raw_Simplifier.rewrite_term thy map_thms' [] t;
blanchet@55695
    86
      in
blanchet@55695
    87
        if t aconv t' then raise Fail "dest_applied_map_or_ctr"
blanchet@55695
    88
        else dest_map ctxt s (fst (dest_comb t'))
blanchet@55695
    89
      end);
blanchet@55691
    90
blanchet@55697
    91
fun map_partition f xs =
blanchet@55697
    92
  fold_rev (fn x => fn (ys, (good, bad)) =>
blanchet@55697
    93
      case f x of SOME y => (y :: ys, (x :: good, bad)) | NONE => (ys, (good, x :: bad)))
blanchet@55697
    94
    xs ([], ([], []));
blanchet@55697
    95
blanchet@54440
    96
(* TODO: test with sort constraints on As *)
blanchet@54440
    97
(* TODO: use right sorting order for "fp_sort" w.r.t. original BNFs (?) -- treat new variables
blanchet@54440
    98
   as deads? *)
blanchet@55697
    99
fun mutualize_fp_sugars has_nested fp bs fpTs get_indices callssss fp_sugars0 no_defs_lthy0 =
blanchet@55705
   100
  if has_nested then
blanchet@54440
   101
    let
blanchet@54440
   102
      val thy = Proof_Context.theory_of no_defs_lthy0;
blanchet@54440
   103
blanchet@54440
   104
      val qsotm = quote o Syntax.string_of_term no_defs_lthy0;
blanchet@54440
   105
blanchet@55697
   106
      fun incompatible_calls t1 t2 =
blanchet@55697
   107
        error ("Incompatible " ^ co_prefix fp ^ "recursive calls: " ^ qsotm t1 ^ " vs. " ^
blanchet@55697
   108
          qsotm t2);
blanchet@55697
   109
blanchet@54440
   110
      val b_names = map Binding.name_of bs;
blanchet@54440
   111
      val fp_b_names = map base_name_of_typ fpTs;
blanchet@54440
   112
blanchet@54440
   113
      val nn = length fpTs;
blanchet@54440
   114
wenzelm@55111
   115
      fun target_ctr_sugar_of_fp_sugar fpT ({T, index, ctr_sugars, ...} : fp_sugar) =
blanchet@54440
   116
        let
blanchet@54440
   117
          val rho = Vartab.fold (cons o apsnd snd) (Sign.typ_match thy (T, fpT) Vartab.empty) [];
blanchet@54440
   118
          val phi = Morphism.term_morphism (Term.subst_TVars rho);
blanchet@54440
   119
        in
blanchet@54440
   120
          morph_ctr_sugar phi (nth ctr_sugars index)
blanchet@54440
   121
        end;
blanchet@54440
   122
blanchet@54440
   123
      val ctr_defss = map (of_fp_sugar #ctr_defss) fp_sugars0;
blanchet@54613
   124
      val mapss = map (of_fp_sugar #mapss) fp_sugars0;
blanchet@54440
   125
      val ctr_sugars0 = map2 target_ctr_sugar_of_fp_sugar fpTs fp_sugars0;
blanchet@54440
   126
blanchet@54440
   127
      val ctrss = map #ctrs ctr_sugars0;
blanchet@54440
   128
      val ctr_Tss = map (map fastype_of) ctrss;
blanchet@54440
   129
blanchet@54440
   130
      val As' = fold (fold Term.add_tfreesT) ctr_Tss [];
blanchet@54440
   131
      val As = map TFree As';
blanchet@54440
   132
blanchet@54440
   133
      val ((Cs, Xs), no_defs_lthy) =
blanchet@54440
   134
        no_defs_lthy0
blanchet@54440
   135
        |> fold Variable.declare_typ As
blanchet@54440
   136
        |> mk_TFrees nn
blanchet@54440
   137
        ||>> variant_tfrees fp_b_names;
blanchet@54440
   138
blanchet@55697
   139
      fun check_call_dead live_call call =
blanchet@55697
   140
        if null (get_indices call) then () else incompatible_calls live_call call;
blanchet@55697
   141
blanchet@55707
   142
      fun freeze_fpTs_simple (T as Type (s, Ts)) =
blanchet@55705
   143
          (case find_index (curry (op =) T) fpTs of
blanchet@55707
   144
            ~1 => Type (s, map freeze_fpTs_simple Ts)
blanchet@55705
   145
          | kk => nth Xs kk)
blanchet@55707
   146
        | freeze_fpTs_simple T = T;
blanchet@55705
   147
blanchet@55707
   148
      fun freeze_fpTs_map (callss, (live_call :: _, dead_calls)) s Ts =
blanchet@55697
   149
        (List.app (check_call_dead live_call) dead_calls;
blanchet@55707
   150
         Type (s, map2 freeze_fpTs (flatten_type_args_of_bnf (the (bnf_of no_defs_lthy s)) []
blanchet@55697
   151
           (transpose callss)) Ts))
blanchet@55707
   152
      and freeze_fpTs calls (T as Type (s, Ts)) =
blanchet@55697
   153
          (case map_partition (try (snd o dest_map no_defs_lthy s)) calls of
blanchet@55697
   154
            ([], _) =>
blanchet@55697
   155
            (case map_partition (try (snd o dest_abs_or_applied_map_or_ctr no_defs_lthy s)) calls of
blanchet@55707
   156
              ([], _) => freeze_fpTs_simple T
blanchet@55707
   157
            | callsp => freeze_fpTs_map callsp s Ts)
blanchet@55707
   158
          | callsp => freeze_fpTs_map callsp s Ts)
blanchet@55707
   159
        | freeze_fpTs _ T = T;
blanchet@54440
   160
blanchet@54440
   161
      val ctr_Tsss = map (map binder_types) ctr_Tss;
blanchet@55707
   162
      val ctrXs_Tsss = map2 (map2 (map2 freeze_fpTs)) callssss ctr_Tsss;
blanchet@54440
   163
      val ctrXs_sum_prod_Ts = map (mk_sumTN_balanced o map HOLogic.mk_tupleT) ctrXs_Tsss;
blanchet@54440
   164
      val Ts = map (body_type o hd) ctr_Tss;
blanchet@54440
   165
blanchet@54440
   166
      val ns = map length ctr_Tsss;
blanchet@54440
   167
      val kss = map (fn n => 1 upto n) ns;
blanchet@54440
   168
      val mss = map (map length) ctr_Tsss;
blanchet@54440
   169
blanchet@54440
   170
      val fp_eqs = map dest_TFree Xs ~~ ctrXs_sum_prod_Ts;
blanchet@54440
   171
blanchet@54440
   172
      val base_fp_names = Name.variant_list [] fp_b_names;
blanchet@54440
   173
      val fp_bs = map2 (fn b_name => fn base_fp_name =>
blanchet@54440
   174
          Binding.qualify true b_name (Binding.name (n2mN ^ base_fp_name)))
blanchet@54440
   175
        b_names base_fp_names;
blanchet@54440
   176
blanchet@54440
   177
      val (pre_bnfs, (fp_res as {xtor_co_iterss = xtor_co_iterss0, xtor_co_induct,
blanchet@54440
   178
             dtor_injects, dtor_ctors, xtor_co_iter_thmss, ...}, lthy)) =
blanchet@54440
   179
        fp_bnf (construct_mutualized_fp fp fpTs fp_sugars0) fp_bs As' fp_eqs no_defs_lthy;
blanchet@54440
   180
blanchet@54440
   181
      val nesting_bnfs = nesty_bnfs lthy ctrXs_Tsss As;
blanchet@54440
   182
      val nested_bnfs = nesty_bnfs lthy ctrXs_Tsss Xs;
blanchet@54440
   183
blanchet@54440
   184
      val ((xtor_co_iterss, iters_args_types, coiters_args_types), _) =
blanchet@54728
   185
        mk_co_iters_prelims fp ctr_Tsss fpTs Cs ns mss xtor_co_iterss0 lthy;
blanchet@54440
   186
blanchet@54440
   187
      fun mk_binding b suf = Binding.suffix_name ("_" ^ suf) b;
blanchet@54440
   188
blanchet@54440
   189
      val ((co_iterss, co_iter_defss), lthy) =
blanchet@54440
   190
        fold_map2 (fn b =>
blanchet@54440
   191
          (if fp = Least_FP then define_iters [foldN, recN] (the iters_args_types)
blanchet@54440
   192
           else define_coiters [unfoldN, corecN] (the coiters_args_types))
blanchet@54440
   193
            (mk_binding b) fpTs Cs) fp_bs xtor_co_iterss lthy
blanchet@54440
   194
        |>> split_list;
blanchet@54440
   195
blanchet@54440
   196
      val rho = tvar_subst thy Ts fpTs;
blanchet@54440
   197
      val ctr_sugar_phi =
blanchet@54440
   198
        Morphism.compose (Morphism.typ_morphism (Term.typ_subst_TVars rho))
blanchet@54440
   199
          (Morphism.term_morphism (Term.subst_TVars rho));
blanchet@54440
   200
      val inst_ctr_sugar = morph_ctr_sugar ctr_sugar_phi;
blanchet@54440
   201
blanchet@54440
   202
      val ctr_sugars = map inst_ctr_sugar ctr_sugars0;
blanchet@54440
   203
blanchet@54883
   204
      val ((co_inducts, un_fold_thmss, co_rec_thmss, disc_unfold_thmss, disc_corec_thmss,
blanchet@54883
   205
            sel_unfold_thmsss, sel_corec_thmsss), fp_sugar_thms) =
blanchet@54440
   206
        if fp = Least_FP then
blanchet@54440
   207
          derive_induct_iters_thms_for_types pre_bnfs (the iters_args_types) xtor_co_induct
blanchet@54440
   208
            xtor_co_iter_thmss nesting_bnfs nested_bnfs fpTs Cs Xs ctrXs_Tsss ctrss ctr_defss
blanchet@54440
   209
            co_iterss co_iter_defss lthy
blanchet@54945
   210
          |> `(fn ((_, induct, _), (fold_thmss, rec_thmss, _)) =>
blanchet@54612
   211
            ([induct], fold_thmss, rec_thmss, [], [], [], []))
blanchet@54883
   212
          ||> (fn info => (SOME info, NONE))
blanchet@54440
   213
        else
blanchet@54440
   214
          derive_coinduct_coiters_thms_for_types pre_bnfs (the coiters_args_types) xtor_co_induct
blanchet@55687
   215
            dtor_injects dtor_ctors xtor_co_iter_thmss nesting_bnfs fpTs Cs Xs ctrXs_Tsss kss mss ns
blanchet@55687
   216
            ctr_defss ctr_sugars co_iterss co_iter_defss (Proof_Context.export lthy no_defs_lthy)
blanchet@55687
   217
            lthy
blanchet@55167
   218
          |> `(fn ((coinduct_thms_pairs, _), (unfold_thmss, corec_thmss, _),
blanchet@54815
   219
                  (disc_unfold_thmss, disc_corec_thmss, _), _,
blanchet@54612
   220
                  (sel_unfold_thmsss, sel_corec_thmsss, _)) =>
blanchet@54612
   221
            (map snd coinduct_thms_pairs, unfold_thmss, corec_thmss, disc_unfold_thmss,
blanchet@54883
   222
             disc_corec_thmss, sel_unfold_thmsss, sel_corec_thmsss))
blanchet@54883
   223
          ||> (fn info => (NONE, SOME info));
blanchet@54440
   224
blanchet@54440
   225
      val phi = Proof_Context.export_morphism no_defs_lthy no_defs_lthy0;
blanchet@54440
   226
blanchet@54440
   227
      fun mk_target_fp_sugar (kk, T) =
blanchet@54440
   228
        {T = T, fp = fp, index = kk, pre_bnfs = pre_bnfs, nested_bnfs = nested_bnfs,
blanchet@54440
   229
         nesting_bnfs = nesting_bnfs, fp_res = fp_res, ctr_defss = ctr_defss,
blanchet@54613
   230
         ctr_sugars = ctr_sugars, co_iterss = co_iterss, mapss = mapss, co_inducts = co_inducts,
blanchet@54612
   231
         co_iter_thmsss = transpose [un_fold_thmss, co_rec_thmss],
blanchet@54612
   232
         disc_co_itersss = transpose [disc_unfold_thmss, disc_corec_thmss],
blanchet@54612
   233
         sel_co_iterssss = transpose [sel_unfold_thmsss, sel_corec_thmsss]}
blanchet@54440
   234
        |> morph_fp_sugar phi;
blanchet@54440
   235
    in
blanchet@54883
   236
      ((map_index mk_target_fp_sugar fpTs, fp_sugar_thms), lthy)
blanchet@54440
   237
    end
blanchet@54440
   238
  else
blanchet@54440
   239
    (* TODO: reorder hypotheses and predicates in (co)induction rules? *)
blanchet@54883
   240
    ((fp_sugars0, (NONE, NONE)), no_defs_lthy0);
blanchet@54440
   241
blanchet@54440
   242
fun indexify_callsss fp_sugar callsss =
blanchet@54440
   243
  let
blanchet@54440
   244
    val {ctrs, ...} = of_fp_sugar #ctr_sugars fp_sugar;
blanchet@54440
   245
    fun do_ctr ctr =
blanchet@54440
   246
      (case AList.lookup Term.aconv_untyped callsss ctr of
blanchet@54440
   247
        NONE => replicate (num_binder_types (fastype_of ctr)) []
blanchet@55695
   248
      | SOME callss => map (map (Envir.beta_eta_contract o unfold_let)) callss);
blanchet@54440
   249
  in
blanchet@54440
   250
    map do_ctr ctrs
blanchet@54440
   251
  end;
blanchet@54440
   252
blanchet@54440
   253
fun pad_and_indexify_calls fp_sugars0 = map2 indexify_callsss fp_sugars0 oo pad_list [];
blanchet@54440
   254
blanchet@55146
   255
fun nested_to_mutual_fps fp actual_bs actual_Ts get_indices actual_callssss0 lthy =
blanchet@54440
   256
  let
blanchet@54440
   257
    val qsoty = quote o Syntax.string_of_typ lthy;
blanchet@54440
   258
    val qsotys = space_implode " or " o map qsoty;
blanchet@54440
   259
blanchet@55705
   260
    fun duplicate_datatype T = error (qsoty T ^ " is not mutually recursive with itself");
blanchet@54440
   261
    fun not_co_datatype0 T = error (qsoty T ^ " is not a " ^ co_prefix fp ^ "datatype");
blanchet@54440
   262
    fun not_co_datatype (T as Type (s, _)) =
blanchet@54440
   263
        if fp = Least_FP andalso
blanchet@54440
   264
           is_some (Datatype_Data.get_info (Proof_Context.theory_of lthy) s) then
blanchet@54440
   265
          error (qsoty T ^ " is not a new-style datatype (cf. \"datatype_new\")")
blanchet@54440
   266
        else
blanchet@54440
   267
          not_co_datatype0 T
blanchet@54440
   268
      | not_co_datatype T = not_co_datatype0 T;
blanchet@54440
   269
    fun not_mutually_nested_rec Ts1 Ts2 =
blanchet@54440
   270
      error (qsotys Ts1 ^ " is neither mutually recursive with nor nested recursive via " ^
blanchet@54440
   271
        qsotys Ts2);
blanchet@54440
   272
blanchet@55705
   273
    val _ = (case Library.duplicates (op =) actual_Ts of [] => () | T :: _ => duplicate_datatype T);
blanchet@55705
   274
blanchet@54440
   275
    val perm_actual_Ts as Type (_, ty_args0) :: _ =
blanchet@54440
   276
      sort (int_ord o pairself Term.size_of_typ) actual_Ts;
blanchet@54440
   277
blanchet@54440
   278
    fun check_enrich_with_mutuals _ [] = []
blanchet@54440
   279
      | check_enrich_with_mutuals seen ((T as Type (T_name, ty_args)) :: Ts) =
blanchet@54440
   280
        (case fp_sugar_of lthy T_name of
blanchet@54440
   281
          SOME ({fp = fp', fp_res = {Ts = Ts', ...}, ...}) =>
blanchet@54440
   282
          if fp = fp' then
blanchet@54440
   283
            let
blanchet@54440
   284
              val mutual_Ts = map (fn Type (s, _) => Type (s, ty_args)) Ts';
blanchet@54440
   285
              val _ =
blanchet@54440
   286
                seen = [] orelse exists (exists_subtype_in seen) mutual_Ts orelse
blanchet@54440
   287
                not_mutually_nested_rec mutual_Ts seen;
blanchet@54440
   288
              val (seen', Ts') = List.partition (member (op =) mutual_Ts) Ts;
blanchet@54440
   289
            in
blanchet@54440
   290
              mutual_Ts @ check_enrich_with_mutuals (seen @ T :: seen') Ts'
blanchet@54440
   291
            end
blanchet@54440
   292
          else
blanchet@54440
   293
            not_co_datatype T
blanchet@54440
   294
        | NONE => not_co_datatype T)
blanchet@54440
   295
      | check_enrich_with_mutuals _ (T :: _) = not_co_datatype T;
blanchet@54440
   296
blanchet@54440
   297
    val perm_Ts = check_enrich_with_mutuals [] perm_actual_Ts;
blanchet@54440
   298
    val missing_Ts = perm_Ts |> subtract (op =) actual_Ts;
blanchet@54440
   299
    val Ts = actual_Ts @ missing_Ts;
blanchet@54440
   300
blanchet@55707
   301
    fun generalize_simple_type T (seen, lthy) =
blanchet@55707
   302
      mk_TFrees 1 lthy |> (fn ([U], lthy) => (U, ((T, U) :: seen, lthy)));
blanchet@55707
   303
blanchet@55707
   304
    fun generalize_type T (seen_lthy as (seen, _)) =
blanchet@55707
   305
      (case AList.lookup (op =) seen T of
blanchet@55707
   306
        SOME U => (U, seen_lthy)
blanchet@55707
   307
      | NONE =>
blanchet@55707
   308
        (case T of
blanchet@55707
   309
          Type (s, Ts) =>
blanchet@55706
   310
          if exists_subtype_in Ts T then fold_map generalize_type Ts seen_lthy |>> curry Type s
blanchet@55707
   311
          else generalize_simple_type T seen_lthy
blanchet@55707
   312
        | _ => generalize_simple_type T seen_lthy));
blanchet@55706
   313
blanchet@55706
   314
    val (perm_Us, _) = fold_map generalize_type perm_Ts ([], lthy);
blanchet@55706
   315
blanchet@54440
   316
    val nn = length Ts;
blanchet@54440
   317
    val kks = 0 upto nn - 1;
blanchet@54440
   318
blanchet@54440
   319
    val common_name = mk_common_name (map Binding.name_of actual_bs);
blanchet@54440
   320
    val bs = pad_list (Binding.name common_name) nn actual_bs;
blanchet@54440
   321
blanchet@54440
   322
    fun permute xs = permute_like (op =) Ts perm_Ts xs;
blanchet@54440
   323
    fun unpermute perm_xs = permute_like (op =) perm_Ts Ts perm_xs;
blanchet@54440
   324
blanchet@54440
   325
    val perm_bs = permute bs;
blanchet@54440
   326
    val perm_kks = permute kks;
blanchet@54440
   327
    val perm_fp_sugars0 = map (the o fp_sugar_of lthy o fst o dest_Type) perm_Ts;
blanchet@54440
   328
blanchet@55632
   329
    val has_nested = exists (fn Type (_, ty_args) => ty_args <> ty_args0) Ts;
blanchet@54440
   330
    val perm_callssss = pad_and_indexify_calls perm_fp_sugars0 nn actual_callssss0;
blanchet@54440
   331
blanchet@54440
   332
    val get_perm_indices = map (fn kk => find_index (curry (op =) kk) perm_kks) o get_indices;
blanchet@54440
   333
blanchet@54883
   334
    val ((perm_fp_sugars, fp_sugar_thms), lthy) =
blanchet@55706
   335
      mutualize_fp_sugars has_nested fp perm_bs perm_Us get_perm_indices perm_callssss
blanchet@54440
   336
        perm_fp_sugars0 lthy;
blanchet@54440
   337
blanchet@54440
   338
    val fp_sugars = unpermute perm_fp_sugars;
blanchet@54440
   339
  in
blanchet@54883
   340
    ((missing_Ts, perm_kks, fp_sugars, fp_sugar_thms), lthy)
blanchet@54440
   341
  end;
blanchet@54440
   342
blanchet@54440
   343
end;