src/HOL/Tools/BNF/bnf_fp_n2m.ML
author wenzelm
Fri, 06 Mar 2015 15:58:56 +0100
changeset 59621 291934bac95e
parent 59580 cbc38731d42f
child 59819 dbec7f33093d
permissions -rw-r--r--
Thm.cterm_of and Thm.ctyp_of operate on local context;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55061
a0adf838e2d1 adjusted comments
blanchet
parents: 55060
diff changeset
     1
(*  Title:      HOL/Tools/BNF/bnf_fp_n2m.ML
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
     2
    Author:     Dmitriy Traytel, TU Muenchen
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
     3
    Copyright   2013
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
     4
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
     5
Flattening of nested to mutual (co)recursion.
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
     6
*)
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
     7
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
     8
signature BNF_FP_N2M =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
     9
sig
56484
c451cf8b29c8 thread mutual cliques through
blanchet
parents: 55966
diff changeset
    10
  val construct_mutualized_fp: BNF_Util.fp_kind -> int list -> typ list ->
58180
95397823f39e moved code around
blanchet
parents: 58162
diff changeset
    11
    BNF_FP_Def_Sugar.fp_sugar list -> binding list -> (string * sort) list ->
56484
c451cf8b29c8 thread mutual cliques through
blanchet
parents: 55966
diff changeset
    12
    typ list * typ list list -> BNF_Def.bnf list -> BNF_Comp.absT_info list -> local_theory ->
c451cf8b29c8 thread mutual cliques through
blanchet
parents: 55966
diff changeset
    13
    BNF_FP_Util.fp_result * local_theory
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    14
end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    15
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    16
structure BNF_FP_N2M : BNF_FP_N2M =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    17
struct
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    18
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    19
open BNF_Def
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    20
open BNF_Util
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    21
open BNF_Comp
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    22
open BNF_FP_Util
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    23
open BNF_FP_Def_Sugar
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    24
open BNF_Tactics
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    25
open BNF_FP_N2M_Tactics
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    26
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    27
fun force_typ ctxt T =
56638
092a306bcc3d generate size instances for new-style datatypes
blanchet
parents: 56487
diff changeset
    28
  Term.map_types Type_Infer.paramify_vars
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    29
  #> Type.constraint T
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    30
  #> Syntax.check_term ctxt
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    31
  #> singleton (Variable.polymorphic ctxt);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    32
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    33
fun mk_prod_map f g =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    34
  let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    35
    val ((fAT, fBT), fT) = `dest_funT (fastype_of f);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    36
    val ((gAT, gBT), gT) = `dest_funT (fastype_of g);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    37
  in
55932
68c5104d2204 renamed 'map_pair' to 'map_prod'
blanchet
parents: 55931
diff changeset
    38
    Const (@{const_name map_prod},
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    39
      fT --> gT --> HOLogic.mk_prodT (fAT, gAT) --> HOLogic.mk_prodT (fBT, gBT)) $ f $ g
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    40
  end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    41
55931
62156e694f3d renamed 'map_sum' to 'sum_map'
blanchet
parents: 55899
diff changeset
    42
fun mk_map_sum f g =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    43
  let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    44
    val ((fAT, fBT), fT) = `dest_funT (fastype_of f);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    45
    val ((gAT, gBT), gT) = `dest_funT (fastype_of g);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    46
  in
55931
62156e694f3d renamed 'map_sum' to 'sum_map'
blanchet
parents: 55899
diff changeset
    47
    Const (@{const_name map_sum}, fT --> gT --> mk_sumT (fAT, gAT) --> mk_sumT (fBT, gBT)) $ f $ g
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    48
  end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    49
58340
blanchet
parents: 58203
diff changeset
    50
fun construct_mutualized_fp fp mutual_cliques fpTs (fp_sugars : fp_sugar list) bs resBs (resDs, Dss)
blanchet
parents: 58203
diff changeset
    51
    bnfs (absT_infos : absT_info list) lthy =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    52
  let
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    53
    fun of_fp_res get =
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55531
diff changeset
    54
      map (fn {fp_res, fp_res_index, ...} => nth (get fp_res) fp_res_index) fp_sugars;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    55
55966
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
    56
    fun mk_co_algT T U = case_fp fp (T --> U) (U --> T);
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
    57
    fun co_swap pair = case_fp fp I swap pair;
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    58
    val mk_co_comp = HOLogic.mk_comp o co_swap;
55966
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
    59
    val co_productC = BNF_FP_Rec_Sugar_Util.case_fp fp @{type_name prod} @{type_name sum};
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    60
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    61
    val dest_co_algT = co_swap o dest_funT;
55966
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
    62
    val co_alg_argT = case_fp fp range_type domain_type;
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
    63
    val co_alg_funT = case_fp fp domain_type range_type;
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
    64
    val mk_co_product = curry (case_fp fp mk_convol mk_case_sum);
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
    65
    val mk_map_co_product = case_fp fp mk_prod_map mk_map_sum;
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
    66
    val co_proj1_const = case_fp fp (fst_const o fst) (uncurry Inl_const o dest_sumT o snd);
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
    67
    val mk_co_productT = curry (case_fp fp HOLogic.mk_prodT mk_sumT);
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
    68
    val dest_co_productT = case_fp fp HOLogic.dest_prodT dest_sumT;
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
    69
    val rewrite_comp_comp = case_fp fp @{thm rewriteL_comp_comp} @{thm rewriteR_comp_comp};
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    70
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    71
    val fp_absT_infos = map #absT_info fp_sugars;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    72
    val fp_bnfs = of_fp_res #bnfs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    73
    val pre_bnfs = map #pre_bnf fp_sugars;
57397
5004aca20821 tuned variable names
blanchet
parents: 57303
diff changeset
    74
    val nesting_bnfss =
5004aca20821 tuned variable names
blanchet
parents: 57303
diff changeset
    75
      map (fn sugar => #fp_nesting_bnfs sugar @ #live_nesting_bnfs sugar) fp_sugars;
5004aca20821 tuned variable names
blanchet
parents: 57303
diff changeset
    76
    val fp_or_nesting_bnfss = fp_bnfs :: nesting_bnfss;
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 59049
diff changeset
    77
    val fp_or_nesting_bnfs = distinct (op = o apply2 T_of_bnf) (flat fp_or_nesting_bnfss);
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    78
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    79
    val fp_absTs = map #absT fp_absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    80
    val fp_repTs = map #repT fp_absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    81
    val fp_abss = map #abs fp_absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    82
    val fp_reps = map #rep fp_absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    83
    val fp_type_definitions = map #type_definition fp_absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    84
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    85
    val absTs = map #absT absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    86
    val repTs = map #repT absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    87
    val absTs' = map (Logic.type_map (singleton (Variable.polymorphic lthy))) absTs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    88
    val repTs' = map (Logic.type_map (singleton (Variable.polymorphic lthy))) repTs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    89
    val abss = map #abs absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    90
    val reps = map #rep absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    91
    val abs_inverses = map #abs_inverse absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    92
    val type_definitions = map #type_definition absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    93
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    94
    val n = length bnfs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    95
    val deads = fold (union (op =)) Dss resDs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    96
    val As = subtract (op =) deads (map TFree resBs);
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    97
    val names_lthy = fold Variable.declare_typ (As @ deads) lthy;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    98
    val m = length As;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    99
    val live = m + n;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   100
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   101
    val ((Xs, Bs), names_lthy) = names_lthy
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   102
      |> mk_TFrees n
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   103
      ||>> mk_TFrees m;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   104
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   105
    val allAs = As @ Xs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   106
    val allBs = Bs @ Xs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   107
    val phiTs = map2 mk_pred2T As Bs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   108
    val thetaBs = As ~~ Bs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   109
    val fpTs' = map (Term.typ_subst_atomic thetaBs) fpTs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   110
    val fold_thetaAs = Xs ~~ fpTs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   111
    val fold_thetaBs = Xs ~~ fpTs';
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   112
    val rec_theta = Xs ~~ map2 mk_co_productT fpTs Xs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   113
    val pre_phiTs = map2 mk_pred2T fpTs fpTs';
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   114
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   115
    val ((ctors, dtors), (xtor's, xtors)) =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   116
      let
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   117
        val ctors = map2 (force_typ names_lthy o (fn T => dummyT --> T)) fpTs (of_fp_res #ctors);
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   118
        val dtors = map2 (force_typ names_lthy o (fn T => T --> dummyT)) fpTs (of_fp_res #dtors);
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   119
      in
55966
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
   120
        ((ctors, dtors), `(map (Term.subst_atomic_types thetaBs)) (case_fp fp ctors dtors))
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   121
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   122
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   123
    val absATs = map (domain_type o fastype_of) ctors;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   124
    val absBTs = map (Term.typ_subst_atomic thetaBs) absATs;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   125
    val xTs = map (domain_type o fastype_of) xtors;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   126
    val yTs = map (domain_type o fastype_of) xtor's;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   127
58634
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58585
diff changeset
   128
    val absAs = @{map 3} (fn Ds => mk_abs o mk_T_of_bnf Ds allAs) Dss bnfs abss;
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58585
diff changeset
   129
    val absBs = @{map 3} (fn Ds => mk_abs o mk_T_of_bnf Ds allBs) Dss bnfs abss;
55819
e21d83c8e1c7 made SML/NJ happier
traytel
parents: 55810
diff changeset
   130
    val fp_repAs = map2 mk_rep absATs fp_reps;
e21d83c8e1c7 made SML/NJ happier
traytel
parents: 55810
diff changeset
   131
    val fp_repBs = map2 mk_rep absBTs fp_reps;
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   132
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   133
    val (((((phis, phis'), pre_phis), xs), ys), names_lthy) = names_lthy
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   134
      |> mk_Frees' "R" phiTs
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   135
      ||>> mk_Frees "S" pre_phiTs
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   136
      ||>> mk_Frees "x" xTs
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   137
      ||>> mk_Frees "y" yTs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   138
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   139
    val rels =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   140
      let
57397
5004aca20821 tuned variable names
blanchet
parents: 57303
diff changeset
   141
        fun find_rel T As Bs = fp_or_nesting_bnfss
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 59049
diff changeset
   142
          |> map (filter_out (curry (op = o apply2 name_of_bnf) BNF_Comp.DEADID_bnf))
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   143
          |> get_first (find_first (fn bnf => Type.could_unify (T_of_bnf bnf, T)))
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   144
          |> Option.map (fn bnf =>
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   145
            let val live = live_of_bnf bnf;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   146
            in (mk_rel live As Bs (rel_of_bnf bnf), live) end)
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   147
          |> the_default (HOLogic.eq_const T, 0);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   148
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   149
        fun mk_rel (T as Type (_, Ts)) (Type (_, Us)) =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   150
              let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   151
                val (rel, live) = find_rel T Ts Us;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   152
                val (Ts', Us') = fastype_of rel |> strip_typeN live |> fst |> map_split dest_pred2T;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   153
                val rels = map2 mk_rel Ts' Us';
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   154
              in
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   155
                Term.list_comb (rel, rels)
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   156
              end
54244
0753e8866ac8 more robust treatment of dead variables in n2m
traytel
parents: 54242
diff changeset
   157
          | mk_rel (T as TFree _) _ = (nth phis (find_index (curry op = T) As)
0753e8866ac8 more robust treatment of dead variables in n2m
traytel
parents: 54242
diff changeset
   158
              handle General.Subscript => HOLogic.eq_const T)
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   159
          | mk_rel _ _ = raise Fail "fpTs contains schematic type variables";
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   160
      in
54298
347c3b0cab44 handle independent functions defined in parallel in N2M (in presence of type variables, see ce58fb149ff6)
traytel
parents: 54244
diff changeset
   161
        map2 (fold_rev Term.absfree phis' oo mk_rel) fpTs fpTs'
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   162
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   163
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   164
    val pre_rels = map2 (fn Ds => mk_rel_of_bnf Ds (As @ fpTs) (Bs @ fpTs')) Dss bnfs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   165
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55531
diff changeset
   166
    val rel_unfolds = maps (no_refl o single o rel_def_of_bnf) pre_bnfs;
58579
b7bc5ba2f3fb rename 'rel_xtor_co_induct_thm' to 'xtor_rel_co_induct'
desharna
parents: 58578
diff changeset
   167
    val rel_xtor_co_inducts = of_fp_res (split_conj_thm o #xtor_rel_co_induct)
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55531
diff changeset
   168
      |> map (unfold_thms lthy (id_apply :: rel_unfolds));
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   169
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   170
    val rel_defs = map rel_def_of_bnf bnfs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   171
    val rel_monos = map rel_mono_of_bnf bnfs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   172
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   173
    fun cast castA castB pre_rel =
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   174
      let
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   175
        val castAB = mk_vimage2p (Term.subst_atomic_types fold_thetaAs castA)
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   176
          (Term.subst_atomic_types fold_thetaBs castB);
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   177
      in
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   178
        fold_rev (fold_rev Term.absdummy) [phiTs, pre_phiTs]
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   179
          (castAB $ Term.list_comb (pre_rel, map Bound (live - 1 downto 0)))
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   180
      end;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   181
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   182
    val castAs = map2 (curry HOLogic.mk_comp) absAs fp_repAs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   183
    val castBs = map2 (curry HOLogic.mk_comp) absBs fp_repBs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   184
58375
7b92932ffea5 careful with op = in n2m (actually by Dmitriy Traytel)
blanchet
parents: 58352
diff changeset
   185
    val fp_or_nesting_rel_eqs = no_refl (map rel_eq_of_bnf fp_or_nesting_bnfs);
7b92932ffea5 careful with op = in n2m (actually by Dmitriy Traytel)
blanchet
parents: 58352
diff changeset
   186
    val fp_or_nesting_rel_monos = map rel_mono_of_bnf fp_or_nesting_bnfs;
58203
9003cc8ac94d made tactic more robust w.r.t. dead variables
traytel
parents: 58180
diff changeset
   187
59049
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   188
    val rel_xtor_co_inducts_inst =
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   189
      let
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   190
        val extract =
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   191
          case_fp fp (snd o Term.dest_comb) (snd o Term.dest_comb o fst o Term.dest_comb);
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   192
        val raw_phis = map (extract o HOLogic.dest_Trueprop o Thm.concl_of) rel_xtor_co_inducts;
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   193
        val thetas = AList.group (op =)
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59580
diff changeset
   194
          (mutual_cliques ~~ map (apply2 (Thm.cterm_of lthy)) (raw_phis ~~ pre_phis));
59049
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   195
      in
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   196
        map2 (Drule.cterm_instantiate o the o AList.lookup (op =) thetas)
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   197
        mutual_cliques rel_xtor_co_inducts
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   198
      end
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   199
58579
b7bc5ba2f3fb rename 'rel_xtor_co_induct_thm' to 'xtor_rel_co_induct'
desharna
parents: 58578
diff changeset
   200
    val xtor_rel_co_induct =
58634
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58585
diff changeset
   201
      mk_xtor_rel_co_induct_thm fp (@{map 3} cast castAs castBs pre_rels) pre_phis rels phis xs ys
59049
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   202
        xtors xtor's (mk_rel_xtor_co_induct_tactic fp abs_inverses rel_xtor_co_inducts_inst rel_defs
58375
7b92932ffea5 careful with op = in n2m (actually by Dmitriy Traytel)
blanchet
parents: 58352
diff changeset
   203
          rel_monos fp_or_nesting_rel_eqs fp_or_nesting_rel_monos)
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   204
        lthy;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   205
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   206
    val map_id0s = no_refl (map map_id0_of_bnf bnfs);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   207
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   208
    val xtor_co_induct_thm =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   209
      (case fp of
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   210
        Least_FP =>
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   211
          let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   212
            val (Ps, names_lthy) = names_lthy
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   213
              |> mk_Frees "P" (map (fn T => T --> HOLogic.boolT) fpTs);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   214
            fun mk_Grp_id P =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   215
              let val T = domain_type (fastype_of P);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   216
              in mk_Grp (HOLogic.Collect_const T $ P) (HOLogic.id_const T) end;
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59580
diff changeset
   217
            val cts = map (SOME o Thm.cterm_of lthy) (map HOLogic.eq_const As @ map mk_Grp_id Ps);
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   218
            fun mk_fp_type_copy_thms thm = map (curry op RS thm)
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   219
              @{thms type_copy_Abs_o_Rep type_copy_vimage2p_Grp_Rep};
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   220
            fun mk_type_copy_thms thm = map (curry op RS thm)
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   221
              @{thms type_copy_Rep_o_Abs type_copy_vimage2p_Grp_Abs};
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   222
          in
58579
b7bc5ba2f3fb rename 'rel_xtor_co_induct_thm' to 'xtor_rel_co_induct'
desharna
parents: 58578
diff changeset
   223
            cterm_instantiate_pos cts xtor_rel_co_induct
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   224
            |> singleton (Proof_Context.export names_lthy lthy)
58375
7b92932ffea5 careful with op = in n2m (actually by Dmitriy Traytel)
blanchet
parents: 58352
diff changeset
   225
            |> unfold_thms lthy (@{thms eq_le_Grp_id_iff all_simps(1,2)[symmetric]} @
7b92932ffea5 careful with op = in n2m (actually by Dmitriy Traytel)
blanchet
parents: 58352
diff changeset
   226
                fp_or_nesting_rel_eqs)
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   227
            |> funpow n (fn thm => thm RS spec)
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   228
            |> unfold_thms lthy (@{thm eq_alt} :: map rel_Grp_of_bnf bnfs @ map_id0s)
55854
ee270328a781 make 'typedef' optional, depending on size of original type
blanchet
parents: 55819
diff changeset
   229
            |> unfold_thms lthy (@{thms vimage2p_id vimage2p_comp comp_apply comp_id
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   230
               Grp_id_mono_subst eqTrueI[OF subset_UNIV] simp_thms(22)} @
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   231
               maps mk_fp_type_copy_thms fp_type_definitions @
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   232
               maps mk_type_copy_thms type_definitions)
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   233
            |> unfold_thms lthy @{thms subset_iff mem_Collect_eq
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   234
               atomize_conjL[symmetric] atomize_all[symmetric] atomize_imp[symmetric]}
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   235
          end
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   236
      | Greatest_FP =>
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   237
          let
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59580
diff changeset
   238
            val cts = NONE :: map (SOME o Thm.cterm_of lthy) (map HOLogic.eq_const As);
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   239
          in
58579
b7bc5ba2f3fb rename 'rel_xtor_co_induct_thm' to 'xtor_rel_co_induct'
desharna
parents: 58578
diff changeset
   240
            cterm_instantiate_pos cts xtor_rel_co_induct
58375
7b92932ffea5 careful with op = in n2m (actually by Dmitriy Traytel)
blanchet
parents: 58352
diff changeset
   241
            |> unfold_thms lthy (@{thms le_fun_def le_bool_def all_simps(1,2)[symmetric]} @
7b92932ffea5 careful with op = in n2m (actually by Dmitriy Traytel)
blanchet
parents: 58352
diff changeset
   242
                fp_or_nesting_rel_eqs)
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   243
            |> funpow (2 * n) (fn thm => thm RS spec)
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 54740
diff changeset
   244
            |> Conv.fconv_rule (Object_Logic.atomize lthy)
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   245
            |> funpow n (fn thm => thm RS mp)
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   246
          end);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   247
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   248
    val fold_preTs = map2 (fn Ds => mk_T_of_bnf Ds allAs) Dss bnfs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   249
    val rec_preTs = map (Term.typ_subst_atomic rec_theta) fold_preTs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   250
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   251
    val rec_strTs = map2 mk_co_algT rec_preTs Xs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   252
    val resTs = map2 mk_co_algT fpTs Xs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   253
55894
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   254
    val ((rec_strs, rec_strs'), names_lthy) = names_lthy
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   255
      |> mk_Frees' "s" rec_strTs;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   256
55868
37b99986d435 rationalized internals
blanchet
parents: 55854
diff changeset
   257
    val co_recs = of_fp_res #xtor_co_recs;
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55531
diff changeset
   258
    val ns = map (length o #Ts o #fp_res) fp_sugars;
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55531
diff changeset
   259
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   260
    val typ_subst_nonatomic_sorted = fold_rev (typ_subst_nonatomic o single);
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   261
55899
8c0a13e84963 N2M does not use the low-level 'fold'; removed the latter from the fp_result interface;
traytel
parents: 55894
diff changeset
   262
    fun foldT_of_recT recT =
8c0a13e84963 N2M does not use the low-level 'fold'; removed the latter from the fp_result interface;
traytel
parents: 55894
diff changeset
   263
      let
57802
9c065009cd8a handle deep nesting in N2M
traytel
parents: 57801
diff changeset
   264
        val ((FTXs, Ys), TX) = strip_fun_type recT |>> map_split dest_co_algT;
9c065009cd8a handle deep nesting in N2M
traytel
parents: 57801
diff changeset
   265
        val Zs = union op = Xs Ys;
55899
8c0a13e84963 N2M does not use the low-level 'fold'; removed the latter from the fp_result interface;
traytel
parents: 55894
diff changeset
   266
        fun subst (Type (C, Ts as [_, X])) =
57802
9c065009cd8a handle deep nesting in N2M
traytel
parents: 57801
diff changeset
   267
            if C = co_productC andalso member op = Zs X then X else Type (C, map subst Ts)
55899
8c0a13e84963 N2M does not use the low-level 'fold'; removed the latter from the fp_result interface;
traytel
parents: 55894
diff changeset
   268
          | subst (Type (C, Ts)) = Type (C, map subst Ts)
8c0a13e84963 N2M does not use the low-level 'fold'; removed the latter from the fp_result interface;
traytel
parents: 55894
diff changeset
   269
          | subst T = T;
8c0a13e84963 N2M does not use the low-level 'fold'; removed the latter from the fp_result interface;
traytel
parents: 55894
diff changeset
   270
      in
57802
9c065009cd8a handle deep nesting in N2M
traytel
parents: 57801
diff changeset
   271
        map2 (mk_co_algT o subst) FTXs Ys ---> TX
55899
8c0a13e84963 N2M does not use the low-level 'fold'; removed the latter from the fp_result interface;
traytel
parents: 55894
diff changeset
   272
      end;
8c0a13e84963 N2M does not use the low-level 'fold'; removed the latter from the fp_result interface;
traytel
parents: 55894
diff changeset
   273
57802
9c065009cd8a handle deep nesting in N2M
traytel
parents: 57801
diff changeset
   274
    fun force_rec i TU raw_rec =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   275
      let
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   276
        val thy = Proof_Context.theory_of lthy;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   277
55899
8c0a13e84963 N2M does not use the low-level 'fold'; removed the latter from the fp_result interface;
traytel
parents: 55894
diff changeset
   278
        val approx_rec = raw_rec
57802
9c065009cd8a handle deep nesting in N2M
traytel
parents: 57801
diff changeset
   279
          |> force_typ names_lthy (replicate (nth ns i) dummyT ---> TU);
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   280
        val subst = Term.typ_subst_atomic fold_thetaAs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   281
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   282
        fun mk_fp_absT_repT fp_repT fp_absT = mk_absT thy fp_repT fp_absT ooo mk_repT;
58634
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58585
diff changeset
   283
        val mk_fp_absT_repTs = @{map 5} mk_fp_absT_repT fp_repTs fp_absTs absTs repTs;
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   284
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   285
        val fold_preTs' = mk_fp_absT_repTs (map subst fold_preTs);
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   286
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   287
        val fold_pre_deads_only_Ts =
58162
df15198c6309 lessen the burden on the caller: sort where necessary in n2m
traytel
parents: 57802
diff changeset
   288
          map (typ_subst_nonatomic_sorted (map (rpair dummyT)
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 59049
diff changeset
   289
            (As @ sort (int_ord o apply2 Term.size_of_typ) fpTs))) fold_preTs';
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   290
58340
blanchet
parents: 58203
diff changeset
   291
        val (mutual_clique, TUs) =
56487
961b34963fa4 use mutual clique information (cf. c451cf8b29c8) to make N2M more robust
traytel
parents: 56486
diff changeset
   292
          map_split dest_co_algT (binder_fun_types (foldT_of_recT (fastype_of approx_rec)))
55478
3a6efda01da4 made N2M more robust w.r.t. identical nested types
traytel
parents: 55414
diff changeset
   293
          |>> map subst
56487
961b34963fa4 use mutual clique information (cf. c451cf8b29c8) to make N2M more robust
traytel
parents: 56486
diff changeset
   294
          |> `(fn (_, Ys) =>
58340
blanchet
parents: 58203
diff changeset
   295
            nth mutual_cliques (find_index (fn X => X = the (find_first (can dest_TFree) Ys)) Xs))
56487
961b34963fa4 use mutual clique information (cf. c451cf8b29c8) to make N2M more robust
traytel
parents: 56486
diff changeset
   296
          ||> uncurry (map2 mk_co_algT);
58340
blanchet
parents: 58203
diff changeset
   297
        val cands = mutual_cliques ~~ map2 mk_co_algT fold_preTs' Xs;
56487
961b34963fa4 use mutual clique information (cf. c451cf8b29c8) to make N2M more robust
traytel
parents: 56486
diff changeset
   298
        val js = find_indices (fn ((cl, cand), TU) =>
58340
blanchet
parents: 58203
diff changeset
   299
          cl = mutual_clique andalso Type.could_unify (TU, cand)) TUs cands;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   300
        val Tpats = map (fn j => mk_co_algT (nth fold_pre_deads_only_Ts j) (nth Xs j)) js;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   301
      in
55894
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   302
        force_typ names_lthy (Tpats ---> TU) raw_rec
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   303
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   304
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   305
    fun mk_co_comp_abs_rep fp_absT absT fp_abs fp_rep abs rep t =
55966
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
   306
      case_fp fp (HOLogic.mk_comp (HOLogic.mk_comp (t, mk_abs absT abs), mk_rep fp_absT fp_rep))
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   307
        (HOLogic.mk_comp (mk_abs fp_absT fp_abs, HOLogic.mk_comp (mk_rep absT rep, t)));
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   308
55894
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   309
    fun mk_rec b_opt recs lthy TU =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   310
      let
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   311
        val thy = Proof_Context.theory_of lthy;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   312
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   313
        val x = co_alg_argT TU;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   314
        val i = find_index (fn T => x = T) Xs;
55894
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   315
        val TUrec =
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   316
          (case find_first (fn f => body_fun_type (fastype_of f) = TU) recs of
57802
9c065009cd8a handle deep nesting in N2M
traytel
parents: 57801
diff changeset
   317
            NONE => force_rec i TU (nth co_recs i)
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   318
          | SOME f => f);
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   319
55894
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   320
        val TUs = binder_fun_types (fastype_of TUrec);
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   321
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   322
        fun mk_s TU' =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   323
          let
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   324
            fun mk_absT_fp_repT repT absT = mk_absT thy repT absT ooo mk_repT;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   325
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   326
            val i = find_index (fn T => co_alg_argT TU' = T) Xs;
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   327
            val fp_abs = nth fp_abss i;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   328
            val fp_rep = nth fp_reps i;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   329
            val abs = nth abss i;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   330
            val rep = nth reps i;
54233
6d64669184ae conceal definition
blanchet
parents: 53753
diff changeset
   331
            val sF = co_alg_funT TU';
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   332
            val sF' =
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   333
              mk_absT_fp_repT (nth repTs' i) (nth absTs' i) (nth fp_absTs i) (nth fp_repTs i) sF
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   334
                handle Term.TYPE _ => sF;
55894
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   335
            val F = nth rec_preTs i;
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   336
            val s = nth rec_strs i;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   337
          in
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   338
            if sF = F then s
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   339
            else if sF' = F then mk_co_comp_abs_rep sF sF' fp_abs fp_rep abs rep s
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   340
            else
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   341
              let
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   342
                val smapT = replicate live dummyT ---> mk_co_algT sF' F;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   343
                fun hidden_to_unit t =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   344
                  Term.subst_TVars (map (rpair HOLogic.unitT) (Term.add_tvar_names t [])) t;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   345
                val smap = map_of_bnf (nth bnfs i)
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   346
                  |> force_typ names_lthy smapT
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   347
                  |> hidden_to_unit;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   348
                val smap_argTs = strip_typeN live (fastype_of smap) |> fst;
58352
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   349
                fun mk_smap_arg T_to_U =
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   350
                  (if domain_type T_to_U = range_type T_to_U then
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   351
                    HOLogic.id_const (domain_type T_to_U)
55894
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   352
                  else
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   353
                    let
58352
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   354
                      val (TY, (U, X)) = T_to_U |> dest_co_algT ||> dest_co_productT;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   355
                      val T = mk_co_algT TY U;
58352
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   356
                      fun mk_co_proj TU =
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   357
                        build_map lthy [] (fn TU =>
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   358
                          let val ((T1, T2), U) = TU |> co_swap |>> dest_co_productT in
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   359
                            if T1 = U then co_proj1_const TU
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   360
                            else mk_co_comp (mk_co_proj (co_swap (T1, U)),
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   361
                              co_proj1_const (co_swap (mk_co_productT T1 T2, T1)))
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   362
                          end)
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   363
                          TU;
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   364
                      fun default () =
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   365
                        mk_co_product (mk_co_proj (dest_funT T))
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   366
                          (fst (fst (mk_rec NONE recs lthy (mk_co_algT TY X))));
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   367
                    in
58352
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   368
                      if can dest_co_productT TY then
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   369
                        mk_map_co_product (mk_co_proj (co_swap (dest_co_productT TY |> fst, U)))
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   370
                          (HOLogic.id_const X)
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   371
                        handle TYPE _ => default () (*N2M involving "prod" type*)
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   372
                      else
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   373
                        default ()
55894
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   374
                    end)
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   375
                val smap_args = map mk_smap_arg smap_argTs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   376
              in
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   377
                mk_co_comp_abs_rep sF sF' fp_abs fp_rep abs rep
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   378
                  (mk_co_comp (s, Term.list_comb (smap, smap_args)))
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   379
              end
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   380
          end;
55894
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   381
        val t = Term.list_comb (TUrec, map mk_s TUs);
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   382
      in
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   383
        (case b_opt of
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   384
          NONE => ((t, Drule.dummy_thm), lthy)
54233
6d64669184ae conceal definition
blanchet
parents: 53753
diff changeset
   385
        | SOME b => Local_Theory.define ((b, NoSyn), ((Binding.conceal (Thm.def_binding b), []),
55894
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   386
            fold_rev Term.absfree rec_strs' t)) lthy |>> apsnd snd)
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   387
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   388
55966
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
   389
    val recN = case_fp fp ctor_recN dtor_corecN;
55894
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   390
    fun mk_recs lthy =
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   391
      fold2 (fn TU => fn b => fn ((recs, defs), lthy) =>
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   392
        mk_rec (SOME b) recs lthy TU |>> (fn (f, d) => (f :: recs, d :: defs)))
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   393
      resTs (map (Binding.suffix_name ("_" ^ recN)) bs) (([], []), lthy)
57527
1b07ca054327 add helper function map_prod
desharna
parents: 57397
diff changeset
   394
      |>> map_prod rev rev;
55894
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   395
    val ((raw_co_recs, raw_co_rec_defs), (lthy, raw_lthy)) = lthy
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   396
      |> mk_recs
53331
20440c789759 prove theorem in the right context (that knows about local variables)
traytel
parents: 53309
diff changeset
   397
      ||> `Local_Theory.restore;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   398
53331
20440c789759 prove theorem in the right context (that knows about local variables)
traytel
parents: 53309
diff changeset
   399
    val phi = Proof_Context.export_morphism raw_lthy lthy;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   400
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   401
    val co_recs = map (Morphism.term phi) raw_co_recs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   402
58578
9ff8ca957c02 rename 'xtor_co_rec_o_map_thms' to 'xtor_co_rec_o_maps'
desharna
parents: 58448
diff changeset
   403
    val fp_rec_o_maps = of_fp_res #xtor_co_rec_o_maps
56486
753b779d070d made N2M tactic more robust
traytel
parents: 56484
diff changeset
   404
      |> maps (fn thm => [thm, thm RS rewrite_comp_comp]);
55868
37b99986d435 rationalized internals
blanchet
parents: 55854
diff changeset
   405
55894
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   406
    val xtor_co_rec_thms =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   407
      let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   408
        val recs = map (fn r => Term.list_comb (r, rec_strs)) raw_co_recs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   409
        val rec_mapTs = co_swap (As @ fpTs, As @ map2 mk_co_productT fpTs Xs);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   410
        val pre_rec_maps =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   411
          map2 (fn Ds => fn bnf =>
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   412
            Term.list_comb (uncurry (mk_map_of_bnf Ds) rec_mapTs bnf,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   413
              map HOLogic.id_const As @ map2 (mk_co_product o HOLogic.id_const) fpTs recs))
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   414
          Dss bnfs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   415
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   416
        fun mk_goals f xtor s smap fp_abs fp_rep abs rep =
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   417
          let
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   418
            val lhs = mk_co_comp (f, xtor);
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   419
            val rhs = mk_co_comp (s, smap);
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   420
          in
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   421
            HOLogic.mk_eq (lhs,
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   422
              mk_co_comp_abs_rep (co_alg_funT (fastype_of lhs)) (co_alg_funT (fastype_of rhs))
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   423
                fp_abs fp_rep abs rep rhs)
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   424
          end;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   425
58634
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58585
diff changeset
   426
        val goals = @{map 8} mk_goals recs xtors rec_strs pre_rec_maps fp_abss fp_reps abss reps;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   427
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   428
        val pre_map_defs = no_refl (map map_def_of_bnf bnfs);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   429
        val fp_pre_map_defs = no_refl (map map_def_of_bnf pre_bnfs);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   430
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   431
        val unfold_map = map (unfold_thms lthy (id_apply :: pre_map_defs));
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   432
55868
37b99986d435 rationalized internals
blanchet
parents: 55854
diff changeset
   433
        val fp_xtor_co_recs = map (mk_pointfree lthy) (of_fp_res #xtor_co_rec_thms);
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   434
55966
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
   435
        val fold_thms = case_fp fp @{thm comp_assoc} @{thm comp_assoc[symmetric]} ::
55932
68c5104d2204 renamed 'map_pair' to 'map_prod'
blanchet
parents: 55931
diff changeset
   436
          map (fn thm => thm RS rewrite_comp_comp) @{thms map_prod.comp map_sum.comp} @
68c5104d2204 renamed 'map_pair' to 'map_prod'
blanchet
parents: 55931
diff changeset
   437
          @{thms id_apply comp_id id_comp map_prod.comp map_prod.id map_sum.comp map_sum.id};
55966
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
   438
        val rec_thms = fold_thms @ case_fp fp
57802
9c065009cd8a handle deep nesting in N2M
traytel
parents: 57801
diff changeset
   439
          @{thms fst_convol map_prod_o_convol convol_o fst_comp_map_prod}
9c065009cd8a handle deep nesting in N2M
traytel
parents: 57801
diff changeset
   440
          @{thms case_sum_o_inj(1) case_sum_o_map_sum o_case_sum map_sum_o_inj(1)};
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   441
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 59049
diff changeset
   442
        val eq_thm_prop_untyped = Term.aconv_untyped o apply2 Thm.full_prop_of;
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   443
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   444
        val map_thms = no_refl (maps (fn bnf =>
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   445
           let val map_comp0 = map_comp0_of_bnf bnf RS sym
57397
5004aca20821 tuned variable names
blanchet
parents: 57303
diff changeset
   446
           in [map_comp0, map_comp0 RS rewrite_comp_comp, map_id0_of_bnf bnf] end)
5004aca20821 tuned variable names
blanchet
parents: 57303
diff changeset
   447
          fp_or_nesting_bnfs) @
55966
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
   448
          remove eq_thm_prop_untyped (case_fp fp @{thm comp_assoc[symmetric]} @{thm comp_assoc})
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   449
          (map2 (fn thm => fn bnf =>
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   450
            @{thm type_copy_map_comp0_undo} OF
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   451
              (replicate 3 thm @ unfold_map [map_comp0_of_bnf bnf]) RS
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   452
              rewrite_comp_comp)
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   453
          type_definitions bnfs);
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   454
57801
4adfa833072b made tactic more robust
traytel
parents: 57700
diff changeset
   455
        fun mk_Rep_o_Abs thm = (thm RS @{thm type_copy_Rep_o_Abs})
4adfa833072b made tactic more robust
traytel
parents: 57700
diff changeset
   456
          |> (fn thm => [thm, thm RS rewrite_comp_comp]);
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   457
57801
4adfa833072b made tactic more robust
traytel
parents: 57700
diff changeset
   458
        val fp_Rep_o_Abss = maps mk_Rep_o_Abs fp_type_definitions;
4adfa833072b made tactic more robust
traytel
parents: 57700
diff changeset
   459
        val Rep_o_Abss = maps mk_Rep_o_Abs type_definitions;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   460
55894
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   461
        fun tac {context = ctxt, prems = _} =
58352
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   462
          unfold_thms_tac ctxt (flat [rec_thms, raw_co_rec_defs, pre_map_defs,
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   463
            fp_pre_map_defs, fp_xtor_co_recs, fp_rec_o_maps, map_thms, fp_Rep_o_Abss,
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   464
            Rep_o_Abss]) THEN
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   465
          CONJ_WRAP (K (HEADGOAL (rtac refl))) bnfs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   466
      in
55894
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   467
        Library.foldr1 HOLogic.mk_conj goals
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   468
        |> HOLogic.mk_Trueprop
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   469
        |> fold_rev Logic.all rec_strs
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   470
        |> (fn goal => Goal.prove_sorry raw_lthy [] [] goal tac)
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   471
        |> Thm.close_derivation
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   472
        |> Morphism.thm phi
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   473
        |> split_conj_thm
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   474
        |> map (fn thm => thm RS @{thm comp_eq_dest})
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   475
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   476
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   477
    (* These results are half broken. This is deliberate. We care only about those fields that are
55531
601ca8efa000 renamed 'datatype_new_compat' to 'datatype_compat'
blanchet
parents: 55530
diff changeset
   478
       used by "primrec", "primcorecursive", and "datatype_compat". *)
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   479
    val fp_res =
55894
8f3fe443948a simplified N2M code now that 'fold' is no longer used by the sugar layer + use right context in all 'force_typ' calls
blanchet
parents: 55868
diff changeset
   480
      ({Ts = fpTs, bnfs = of_fp_res #bnfs, dtors = dtors, ctors = ctors,
55868
37b99986d435 rationalized internals
blanchet
parents: 55854
diff changeset
   481
        xtor_co_recs = co_recs, xtor_co_induct = xtor_co_induct_thm,
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   482
        dtor_ctors = of_fp_res #dtor_ctors (*too general types*),
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   483
        ctor_dtors = of_fp_res #ctor_dtors (*too general types*),
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   484
        ctor_injects = of_fp_res #ctor_injects (*too general types*),
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   485
        dtor_injects = of_fp_res #dtor_injects (*too general types*),
58583
1dd83cbba636 rename 'xtor_map_thms' to 'xtor_maps'
desharna
parents: 58581
diff changeset
   486
        xtor_maps = of_fp_res #xtor_maps (*too general types and terms*),
58584
b6492a7abb59 rename 'xtor_set_thmss' to 'xtor_setss'
desharna
parents: 58583
diff changeset
   487
        xtor_setss = of_fp_res #xtor_setss (*too general types and terms*),
58585
efc8b2c54a38 rename 'xtor_rel_thms' to 'xtor_rels'
desharna
parents: 58584
diff changeset
   488
        xtor_rels = of_fp_res #xtor_rels (*too general types and terms*),
55868
37b99986d435 rationalized internals
blanchet
parents: 55854
diff changeset
   489
        xtor_co_rec_thms = xtor_co_rec_thms,
58578
9ff8ca957c02 rename 'xtor_co_rec_o_map_thms' to 'xtor_co_rec_o_maps'
desharna
parents: 58448
diff changeset
   490
        xtor_co_rec_o_maps = fp_rec_o_maps (*theorems about old constants*),
58579
b7bc5ba2f3fb rename 'rel_xtor_co_induct_thm' to 'xtor_rel_co_induct'
desharna
parents: 58578
diff changeset
   491
        xtor_rel_co_induct = xtor_rel_co_induct,
58580
8ee2d984caa8 rename 'dtor_set_induct_thms' to 'dtor_set_inducts'
desharna
parents: 58579
diff changeset
   492
        dtor_set_inducts = [],
58581
e2e2d775869c rename 'xtor_co_rec_transfer_thms' to 'xtor_co_rec_transfers'
desharna
parents: 58580
diff changeset
   493
        xtor_co_rec_transfers = []}
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 54298
diff changeset
   494
       |> morph_fp_result (Morphism.term_morphism "BNF" (singleton (Variable.polymorphic lthy))));
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   495
  in
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   496
    (fp_res, lthy)
54242
blanchet
parents: 54233
diff changeset
   497
  end;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   498
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   499
end;