src/HOL/BNF/Tools/bnf_fp_n2m_sugar.ML
author blanchet
Mon, 04 Nov 2013 10:52:41 +0100
changeset 55687 3aed2ae6eb91
parent 55686 b5310a1b807c
child 55691 9bd91d5d8a7b
permissions -rw-r--r--
tuning
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@55146
    10
  val mutualize_fp_sugars: bool -> BNF_FP_Util.fp_kind -> binding list -> typ list ->
blanchet@54440
    11
    (term -> int list) -> term list list list list -> BNF_FP_Def_Sugar.fp_sugar list ->
blanchet@54883
    12
    local_theory ->
blanchet@54883
    13
    (BNF_FP_Def_Sugar.fp_sugar list
blanchet@54883
    14
     * (BNF_FP_Def_Sugar.lfp_sugar_thms option * BNF_FP_Def_Sugar.gfp_sugar_thms option))
blanchet@54883
    15
    * local_theory
blanchet@54440
    16
  val pad_and_indexify_calls: BNF_FP_Def_Sugar.fp_sugar list -> int ->
blanchet@54440
    17
    (term * term list list) list list -> term list list list list
blanchet@55146
    18
  val nested_to_mutual_fps: BNF_FP_Util.fp_kind -> binding list -> typ list -> (term -> int list) ->
blanchet@55146
    19
    (term * term list list) list list -> local_theory ->
blanchet@54883
    20
    (typ list * int list * BNF_FP_Def_Sugar.fp_sugar list
blanchet@54883
    21
     * (BNF_FP_Def_Sugar.lfp_sugar_thms option * BNF_FP_Def_Sugar.gfp_sugar_thms option))
blanchet@54883
    22
    * local_theory
blanchet@54440
    23
end;
blanchet@54440
    24
blanchet@54440
    25
structure BNF_FP_N2M_Sugar : BNF_FP_N2M_SUGAR =
blanchet@54440
    26
struct
blanchet@54440
    27
blanchet@55143
    28
open Ctr_Sugar
blanchet@54440
    29
open BNF_Util
blanchet@54440
    30
open BNF_Def
blanchet@54440
    31
open BNF_FP_Util
blanchet@54440
    32
open BNF_FP_Def_Sugar
blanchet@54440
    33
open BNF_FP_N2M
blanchet@54440
    34
blanchet@54440
    35
val n2mN = "n2m_"
blanchet@54440
    36
blanchet@54440
    37
(* TODO: test with sort constraints on As *)
blanchet@54440
    38
(* TODO: use right sorting order for "fp_sort" w.r.t. original BNFs (?) -- treat new variables
blanchet@54440
    39
   as deads? *)
blanchet@55632
    40
fun mutualize_fp_sugars has_nested fp bs fpTs get_indices callssss fp_sugars0 no_defs_lthy0 =
blanchet@55632
    41
  if has_nested orelse has_duplicates (op =) fpTs then
blanchet@54440
    42
    let
blanchet@54440
    43
      val thy = Proof_Context.theory_of no_defs_lthy0;
blanchet@54440
    44
blanchet@54440
    45
      val qsotm = quote o Syntax.string_of_term no_defs_lthy0;
blanchet@54440
    46
blanchet@54440
    47
      fun heterogeneous_call t = error ("Heterogeneous recursive call: " ^ qsotm t);
blanchet@54440
    48
      fun incompatible_calls t1 t2 =
blanchet@54440
    49
        error ("Incompatible recursive calls: " ^ qsotm t1 ^ " vs. " ^ qsotm t2);
blanchet@54440
    50
blanchet@54440
    51
      val b_names = map Binding.name_of bs;
blanchet@54440
    52
      val fp_b_names = map base_name_of_typ fpTs;
blanchet@54440
    53
blanchet@54440
    54
      val nn = length fpTs;
blanchet@54440
    55
wenzelm@55111
    56
      fun target_ctr_sugar_of_fp_sugar fpT ({T, index, ctr_sugars, ...} : fp_sugar) =
blanchet@54440
    57
        let
blanchet@54440
    58
          val rho = Vartab.fold (cons o apsnd snd) (Sign.typ_match thy (T, fpT) Vartab.empty) [];
blanchet@54440
    59
          val phi = Morphism.term_morphism (Term.subst_TVars rho);
blanchet@54440
    60
        in
blanchet@54440
    61
          morph_ctr_sugar phi (nth ctr_sugars index)
blanchet@54440
    62
        end;
blanchet@54440
    63
blanchet@54440
    64
      val ctr_defss = map (of_fp_sugar #ctr_defss) fp_sugars0;
blanchet@54613
    65
      val mapss = map (of_fp_sugar #mapss) fp_sugars0;
blanchet@54440
    66
      val ctr_sugars0 = map2 target_ctr_sugar_of_fp_sugar fpTs fp_sugars0;
blanchet@54440
    67
blanchet@54440
    68
      val ctrss = map #ctrs ctr_sugars0;
blanchet@54440
    69
      val ctr_Tss = map (map fastype_of) ctrss;
blanchet@54440
    70
blanchet@54440
    71
      val As' = fold (fold Term.add_tfreesT) ctr_Tss [];
blanchet@54440
    72
      val As = map TFree As';
blanchet@54440
    73
blanchet@54440
    74
      val ((Cs, Xs), no_defs_lthy) =
blanchet@54440
    75
        no_defs_lthy0
blanchet@54440
    76
        |> fold Variable.declare_typ As
blanchet@54440
    77
        |> mk_TFrees nn
blanchet@54440
    78
        ||>> variant_tfrees fp_b_names;
blanchet@54440
    79
blanchet@54440
    80
      fun freeze_fp_default (T as Type (s, Ts)) =
blanchet@54440
    81
          (case find_index (curry (op =) T) fpTs of
blanchet@54440
    82
            ~1 => Type (s, map freeze_fp_default Ts)
blanchet@54440
    83
          | kk => nth Xs kk)
blanchet@54440
    84
        | freeze_fp_default T = T;
blanchet@54440
    85
blanchet@54440
    86
      fun get_indices_checked call =
blanchet@54440
    87
        (case get_indices call of
blanchet@54440
    88
          _ :: _ :: _ => heterogeneous_call call
blanchet@54440
    89
        | kks => kks);
blanchet@54440
    90
blanchet@55686
    91
      fun freeze_fp_map callss s Ts =
blanchet@55686
    92
        Type (s, map2 freeze_fp (flatten_type_args_of_bnf (the (bnf_of no_defs_lthy s)) []
blanchet@55686
    93
          (transpose callss)) Ts)
blanchet@55686
    94
      and freeze_fp calls (T as Type (s, Ts)) =
blanchet@54440
    95
          (case map_filter (try (snd o dest_map no_defs_lthy s)) calls of
blanchet@54440
    96
            [] =>
blanchet@55686
    97
            (case map_filter (try (snd o dest_map no_defs_lthy s o fst o dest_comb)) calls of
blanchet@55686
    98
              [] =>
blanchet@55686
    99
              (case union (op = o pairself fst)
blanchet@55686
   100
                  (maps (fn call => map (rpair call) (get_indices_checked call)) calls) [] of
blanchet@55686
   101
                [] => freeze_fp_default T
blanchet@55686
   102
              | [(kk, _)] => nth Xs kk
blanchet@55686
   103
              | (_, call1) :: (_, call2) :: _ => incompatible_calls call1 call2)
blanchet@55686
   104
            | callss => freeze_fp_map callss s Ts)
blanchet@55686
   105
          | callss => freeze_fp_map callss s Ts)
blanchet@54440
   106
        | freeze_fp _ T = T;
blanchet@54440
   107
blanchet@54440
   108
      val ctr_Tsss = map (map binder_types) ctr_Tss;
blanchet@54440
   109
      val ctrXs_Tsss = map2 (map2 (map2 freeze_fp)) callssss ctr_Tsss;
blanchet@54440
   110
      val ctrXs_sum_prod_Ts = map (mk_sumTN_balanced o map HOLogic.mk_tupleT) ctrXs_Tsss;
blanchet@54440
   111
      val Ts = map (body_type o hd) ctr_Tss;
blanchet@54440
   112
blanchet@54440
   113
      val ns = map length ctr_Tsss;
blanchet@54440
   114
      val kss = map (fn n => 1 upto n) ns;
blanchet@54440
   115
      val mss = map (map length) ctr_Tsss;
blanchet@54440
   116
blanchet@54440
   117
      val fp_eqs = map dest_TFree Xs ~~ ctrXs_sum_prod_Ts;
blanchet@54440
   118
blanchet@54440
   119
      val base_fp_names = Name.variant_list [] fp_b_names;
blanchet@54440
   120
      val fp_bs = map2 (fn b_name => fn base_fp_name =>
blanchet@54440
   121
          Binding.qualify true b_name (Binding.name (n2mN ^ base_fp_name)))
blanchet@54440
   122
        b_names base_fp_names;
blanchet@54440
   123
blanchet@54440
   124
      val (pre_bnfs, (fp_res as {xtor_co_iterss = xtor_co_iterss0, xtor_co_induct,
blanchet@54440
   125
             dtor_injects, dtor_ctors, xtor_co_iter_thmss, ...}, lthy)) =
blanchet@54440
   126
        fp_bnf (construct_mutualized_fp fp fpTs fp_sugars0) fp_bs As' fp_eqs no_defs_lthy;
blanchet@54440
   127
blanchet@54440
   128
      val nesting_bnfs = nesty_bnfs lthy ctrXs_Tsss As;
blanchet@54440
   129
      val nested_bnfs = nesty_bnfs lthy ctrXs_Tsss Xs;
blanchet@54440
   130
blanchet@54440
   131
      val ((xtor_co_iterss, iters_args_types, coiters_args_types), _) =
blanchet@54728
   132
        mk_co_iters_prelims fp ctr_Tsss fpTs Cs ns mss xtor_co_iterss0 lthy;
blanchet@54440
   133
blanchet@54440
   134
      fun mk_binding b suf = Binding.suffix_name ("_" ^ suf) b;
blanchet@54440
   135
blanchet@54440
   136
      val ((co_iterss, co_iter_defss), lthy) =
blanchet@54440
   137
        fold_map2 (fn b =>
blanchet@54440
   138
          (if fp = Least_FP then define_iters [foldN, recN] (the iters_args_types)
blanchet@54440
   139
           else define_coiters [unfoldN, corecN] (the coiters_args_types))
blanchet@54440
   140
            (mk_binding b) fpTs Cs) fp_bs xtor_co_iterss lthy
blanchet@54440
   141
        |>> split_list;
blanchet@54440
   142
blanchet@54440
   143
      val rho = tvar_subst thy Ts fpTs;
blanchet@54440
   144
      val ctr_sugar_phi =
blanchet@54440
   145
        Morphism.compose (Morphism.typ_morphism (Term.typ_subst_TVars rho))
blanchet@54440
   146
          (Morphism.term_morphism (Term.subst_TVars rho));
blanchet@54440
   147
      val inst_ctr_sugar = morph_ctr_sugar ctr_sugar_phi;
blanchet@54440
   148
blanchet@54440
   149
      val ctr_sugars = map inst_ctr_sugar ctr_sugars0;
blanchet@54440
   150
blanchet@54883
   151
      val ((co_inducts, un_fold_thmss, co_rec_thmss, disc_unfold_thmss, disc_corec_thmss,
blanchet@54883
   152
            sel_unfold_thmsss, sel_corec_thmsss), fp_sugar_thms) =
blanchet@54440
   153
        if fp = Least_FP then
blanchet@54440
   154
          derive_induct_iters_thms_for_types pre_bnfs (the iters_args_types) xtor_co_induct
blanchet@54440
   155
            xtor_co_iter_thmss nesting_bnfs nested_bnfs fpTs Cs Xs ctrXs_Tsss ctrss ctr_defss
blanchet@54440
   156
            co_iterss co_iter_defss lthy
blanchet@54945
   157
          |> `(fn ((_, induct, _), (fold_thmss, rec_thmss, _)) =>
blanchet@54612
   158
            ([induct], fold_thmss, rec_thmss, [], [], [], []))
blanchet@54883
   159
          ||> (fn info => (SOME info, NONE))
blanchet@54440
   160
        else
blanchet@54440
   161
          derive_coinduct_coiters_thms_for_types pre_bnfs (the coiters_args_types) xtor_co_induct
blanchet@55687
   162
            dtor_injects dtor_ctors xtor_co_iter_thmss nesting_bnfs fpTs Cs Xs ctrXs_Tsss kss mss ns
blanchet@55687
   163
            ctr_defss ctr_sugars co_iterss co_iter_defss (Proof_Context.export lthy no_defs_lthy)
blanchet@55687
   164
            lthy
blanchet@55167
   165
          |> `(fn ((coinduct_thms_pairs, _), (unfold_thmss, corec_thmss, _),
blanchet@54815
   166
                  (disc_unfold_thmss, disc_corec_thmss, _), _,
blanchet@54612
   167
                  (sel_unfold_thmsss, sel_corec_thmsss, _)) =>
blanchet@54612
   168
            (map snd coinduct_thms_pairs, unfold_thmss, corec_thmss, disc_unfold_thmss,
blanchet@54883
   169
             disc_corec_thmss, sel_unfold_thmsss, sel_corec_thmsss))
blanchet@54883
   170
          ||> (fn info => (NONE, SOME info));
blanchet@54440
   171
blanchet@54440
   172
      val phi = Proof_Context.export_morphism no_defs_lthy no_defs_lthy0;
blanchet@54440
   173
blanchet@54440
   174
      fun mk_target_fp_sugar (kk, T) =
blanchet@54440
   175
        {T = T, fp = fp, index = kk, pre_bnfs = pre_bnfs, nested_bnfs = nested_bnfs,
blanchet@54440
   176
         nesting_bnfs = nesting_bnfs, fp_res = fp_res, ctr_defss = ctr_defss,
blanchet@54613
   177
         ctr_sugars = ctr_sugars, co_iterss = co_iterss, mapss = mapss, co_inducts = co_inducts,
blanchet@54612
   178
         co_iter_thmsss = transpose [un_fold_thmss, co_rec_thmss],
blanchet@54612
   179
         disc_co_itersss = transpose [disc_unfold_thmss, disc_corec_thmss],
blanchet@54612
   180
         sel_co_iterssss = transpose [sel_unfold_thmsss, sel_corec_thmsss]}
blanchet@54440
   181
        |> morph_fp_sugar phi;
blanchet@54440
   182
    in
blanchet@54883
   183
      ((map_index mk_target_fp_sugar fpTs, fp_sugar_thms), lthy)
blanchet@54440
   184
    end
blanchet@54440
   185
  else
blanchet@54440
   186
    (* TODO: reorder hypotheses and predicates in (co)induction rules? *)
blanchet@54883
   187
    ((fp_sugars0, (NONE, NONE)), no_defs_lthy0);
blanchet@54440
   188
blanchet@54440
   189
fun indexify_callsss fp_sugar callsss =
blanchet@54440
   190
  let
blanchet@54440
   191
    val {ctrs, ...} = of_fp_sugar #ctr_sugars fp_sugar;
blanchet@54440
   192
    fun do_ctr ctr =
blanchet@54440
   193
      (case AList.lookup Term.aconv_untyped callsss ctr of
blanchet@54440
   194
        NONE => replicate (num_binder_types (fastype_of ctr)) []
blanchet@54440
   195
      | SOME callss => map (map Envir.beta_eta_contract) callss);
blanchet@54440
   196
  in
blanchet@54440
   197
    map do_ctr ctrs
blanchet@54440
   198
  end;
blanchet@54440
   199
blanchet@54440
   200
fun pad_and_indexify_calls fp_sugars0 = map2 indexify_callsss fp_sugars0 oo pad_list [];
blanchet@54440
   201
blanchet@55146
   202
fun nested_to_mutual_fps fp actual_bs actual_Ts get_indices actual_callssss0 lthy =
blanchet@54440
   203
  let
blanchet@54440
   204
    val qsoty = quote o Syntax.string_of_typ lthy;
blanchet@54440
   205
    val qsotys = space_implode " or " o map qsoty;
blanchet@54440
   206
blanchet@54440
   207
    fun not_co_datatype0 T = error (qsoty T ^ " is not a " ^ co_prefix fp ^ "datatype");
blanchet@54440
   208
    fun not_co_datatype (T as Type (s, _)) =
blanchet@54440
   209
        if fp = Least_FP andalso
blanchet@54440
   210
           is_some (Datatype_Data.get_info (Proof_Context.theory_of lthy) s) then
blanchet@54440
   211
          error (qsoty T ^ " is not a new-style datatype (cf. \"datatype_new\")")
blanchet@54440
   212
        else
blanchet@54440
   213
          not_co_datatype0 T
blanchet@54440
   214
      | not_co_datatype T = not_co_datatype0 T;
blanchet@54440
   215
    fun not_mutually_nested_rec Ts1 Ts2 =
blanchet@54440
   216
      error (qsotys Ts1 ^ " is neither mutually recursive with nor nested recursive via " ^
blanchet@54440
   217
        qsotys Ts2);
blanchet@54440
   218
blanchet@54440
   219
    val perm_actual_Ts as Type (_, ty_args0) :: _ =
blanchet@54440
   220
      sort (int_ord o pairself Term.size_of_typ) actual_Ts;
blanchet@54440
   221
blanchet@54440
   222
    fun check_enrich_with_mutuals _ [] = []
blanchet@54440
   223
      | check_enrich_with_mutuals seen ((T as Type (T_name, ty_args)) :: Ts) =
blanchet@54440
   224
        (case fp_sugar_of lthy T_name of
blanchet@54440
   225
          SOME ({fp = fp', fp_res = {Ts = Ts', ...}, ...}) =>
blanchet@54440
   226
          if fp = fp' then
blanchet@54440
   227
            let
blanchet@54440
   228
              val mutual_Ts = map (fn Type (s, _) => Type (s, ty_args)) Ts';
blanchet@54440
   229
              val _ =
blanchet@54440
   230
                seen = [] orelse exists (exists_subtype_in seen) mutual_Ts orelse
blanchet@54440
   231
                not_mutually_nested_rec mutual_Ts seen;
blanchet@54440
   232
              val (seen', Ts') = List.partition (member (op =) mutual_Ts) Ts;
blanchet@54440
   233
            in
blanchet@54440
   234
              mutual_Ts @ check_enrich_with_mutuals (seen @ T :: seen') Ts'
blanchet@54440
   235
            end
blanchet@54440
   236
          else
blanchet@54440
   237
            not_co_datatype T
blanchet@54440
   238
        | NONE => not_co_datatype T)
blanchet@54440
   239
      | check_enrich_with_mutuals _ (T :: _) = not_co_datatype T;
blanchet@54440
   240
blanchet@54440
   241
    val perm_Ts = check_enrich_with_mutuals [] perm_actual_Ts;
blanchet@54440
   242
blanchet@54440
   243
    val missing_Ts = perm_Ts |> subtract (op =) actual_Ts;
blanchet@54440
   244
    val Ts = actual_Ts @ missing_Ts;
blanchet@54440
   245
blanchet@54440
   246
    val nn = length Ts;
blanchet@54440
   247
    val kks = 0 upto nn - 1;
blanchet@54440
   248
blanchet@54440
   249
    val common_name = mk_common_name (map Binding.name_of actual_bs);
blanchet@54440
   250
    val bs = pad_list (Binding.name common_name) nn actual_bs;
blanchet@54440
   251
blanchet@54440
   252
    fun permute xs = permute_like (op =) Ts perm_Ts xs;
blanchet@54440
   253
    fun unpermute perm_xs = permute_like (op =) perm_Ts Ts perm_xs;
blanchet@54440
   254
blanchet@54440
   255
    val perm_bs = permute bs;
blanchet@54440
   256
    val perm_kks = permute kks;
blanchet@54440
   257
    val perm_fp_sugars0 = map (the o fp_sugar_of lthy o fst o dest_Type) perm_Ts;
blanchet@54440
   258
blanchet@55632
   259
    val has_nested = exists (fn Type (_, ty_args) => ty_args <> ty_args0) Ts;
blanchet@54440
   260
    val perm_callssss = pad_and_indexify_calls perm_fp_sugars0 nn actual_callssss0;
blanchet@54440
   261
blanchet@54440
   262
    val get_perm_indices = map (fn kk => find_index (curry (op =) kk) perm_kks) o get_indices;
blanchet@54440
   263
blanchet@54883
   264
    val ((perm_fp_sugars, fp_sugar_thms), lthy) =
blanchet@55632
   265
      mutualize_fp_sugars has_nested fp perm_bs perm_Ts get_perm_indices perm_callssss
blanchet@54440
   266
        perm_fp_sugars0 lthy;
blanchet@54440
   267
blanchet@54440
   268
    val fp_sugars = unpermute perm_fp_sugars;
blanchet@54440
   269
  in
blanchet@54883
   270
    ((missing_Ts, perm_kks, fp_sugars, fp_sugar_thms), lthy)
blanchet@54440
   271
  end;
blanchet@54440
   272
blanchet@54440
   273
end;