src/HOL/Tools/BNF/bnf_fp_n2m.ML
author traytel
Thu, 14 Apr 2016 20:29:42 +0200
changeset 63045 c50c764aab10
parent 62907 9ad0bac25a84
child 63046 8053ef5a0174
permissions -rw-r--r--
n2m operates on (un)folds
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 ->
62684
cb20e8828196 document that n2m does not depend on most things in fp_sugar in its type
traytel
parents: 62649
diff changeset
    11
    (int * BNF_FP_Util.fp_result) 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
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
    27
fun mk_arg_cong ctxt n t =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    28
  let
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
    29
    val Us = fastype_of t |> strip_typeN n |> fst;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
    30
    val ((xs, ys), _) = ctxt
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
    31
      |> mk_Frees "x" Us
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
    32
      ||>> mk_Frees "y" Us;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
    33
    val goal = Logic.list_implies (@{map 2} (curry mk_Trueprop_eq) xs ys,
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
    34
      mk_Trueprop_eq (list_comb (t, xs), list_comb (t, ys)));
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
    35
    val vars = Variable.add_free_names ctxt goal [];
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    36
  in
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
    37
    Goal.prove_sorry ctxt vars [] goal (fn {context = ctxt, prems = _} =>
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
    38
      HEADGOAL (hyp_subst_tac ctxt THEN' rtac ctxt refl))
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
    39
    |> Thm.close_derivation
53303
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
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
    42
fun construct_mutualized_fp fp mutual_cliques fpTs indexed_fp_ress bs resBs (resDs, Dss) bnfs
62719
blanchet
parents: 62689
diff changeset
    43
    (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
    44
  let
62689
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
    45
    val time = time lthy;
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
    46
    val timer = time (Timer.startRealTimer ());
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
    47
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
    48
    val b_names = map Binding.name_of bs;
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
    49
    val b_name = mk_common_name b_names;
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
    50
    val b = Binding.name b_name;
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
    51
62719
blanchet
parents: 62689
diff changeset
    52
    fun of_fp_res get = map (uncurry nth o swap o apsnd get) indexed_fp_ress;
55966
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
    53
    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
    54
    fun co_swap pair = case_fp fp I swap pair;
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
    55
    val mk_co_comp = curry (HOLogic.mk_comp o co_swap);
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    56
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    57
    val dest_co_algT = co_swap o dest_funT;
55966
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
    58
    val co_alg_argT = case_fp fp range_type domain_type;
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
    59
    val co_alg_funT = case_fp fp domain_type range_type;
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
    60
    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
    61
62684
cb20e8828196 document that n2m does not depend on most things in fp_sugar in its type
traytel
parents: 62649
diff changeset
    62
    val fp_absT_infos = of_fp_res #absT_infos;
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    63
    val fp_bnfs = of_fp_res #bnfs;
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
    64
    val fp_pre_bnfs = of_fp_res #pre_bnfs;
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    65
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    66
    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
    67
    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
    68
    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
    69
    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
    70
    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
    71
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    72
    val absTs = map #absT absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    73
    val repTs = map #repT absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    74
    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
    75
    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
    76
    val abss = map #abs absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    77
    val reps = map #rep absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    78
    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
    79
    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
    80
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    81
    val n = length bnfs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    82
    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
    83
    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
    84
    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
    85
    val m = length As;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    86
    val live = m + n;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    87
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
    88
    val (((Xs, Ys), Bs), names_lthy) = names_lthy
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    89
      |> mk_TFrees n
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
    90
      ||>> mk_TFrees n
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    91
      ||>> mk_TFrees m;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    92
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    93
    val allAs = As @ Xs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    94
    val allBs = Bs @ Xs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    95
    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
    96
    val thetaBs = As ~~ Bs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    97
    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
    98
    val fold_thetaAs = Xs ~~ fpTs;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
    99
    val fold_thetaBs = Xs ~~ fpTs';
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   100
    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
   101
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   102
    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
   103
      let
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   104
        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
   105
        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
   106
      in
55966
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55932
diff changeset
   107
        ((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
   108
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   109
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   110
    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
   111
    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
   112
    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
   113
    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
   114
58634
9f10d82e8188 added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents: 58585
diff changeset
   115
    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
   116
    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
   117
    val fp_repAs = map2 mk_rep absATs fp_reps;
e21d83c8e1c7 made SML/NJ happier
traytel
parents: 55810
diff changeset
   118
    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
   119
62684
cb20e8828196 document that n2m does not depend on most things in fp_sugar in its type
traytel
parents: 62649
diff changeset
   120
    val typ_subst_nonatomic_sorted = fold_rev (typ_subst_nonatomic o single);
cb20e8828196 document that n2m does not depend on most things in fp_sugar in its type
traytel
parents: 62649
diff changeset
   121
    val sorted_theta = sort (int_ord o apply2 (Term.size_of_typ o fst)) (fpTs ~~ Xs)
cb20e8828196 document that n2m does not depend on most things in fp_sugar in its type
traytel
parents: 62649
diff changeset
   122
    val sorted_fpTs = map fst sorted_theta;
cb20e8828196 document that n2m does not depend on most things in fp_sugar in its type
traytel
parents: 62649
diff changeset
   123
cb20e8828196 document that n2m does not depend on most things in fp_sugar in its type
traytel
parents: 62649
diff changeset
   124
    val nesting_bnfs = nesting_bnfs lthy
cb20e8828196 document that n2m does not depend on most things in fp_sugar in its type
traytel
parents: 62649
diff changeset
   125
      [[map (typ_subst_nonatomic_sorted (rev sorted_theta) o range_type o fastype_of) fp_repAs]]
cb20e8828196 document that n2m does not depend on most things in fp_sugar in its type
traytel
parents: 62649
diff changeset
   126
      allAs;
cb20e8828196 document that n2m does not depend on most things in fp_sugar in its type
traytel
parents: 62649
diff changeset
   127
    val fp_or_nesting_bnfs = distinct (op = o apply2 T_of_bnf) (fp_bnfs @ nesting_bnfs);
cb20e8828196 document that n2m does not depend on most things in fp_sugar in its type
traytel
parents: 62649
diff changeset
   128
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   129
    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
   130
      |> mk_Frees' "R" phiTs
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   131
      ||>> 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
   132
      ||>> mk_Frees "x" xTs
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   133
      ||>> mk_Frees "y" yTs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   134
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   135
    val rels =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   136
      let
62684
cb20e8828196 document that n2m does not depend on most things in fp_sugar in its type
traytel
parents: 62649
diff changeset
   137
        fun find_rel T As Bs = fp_or_nesting_bnfs
cb20e8828196 document that n2m does not depend on most things in fp_sugar in its type
traytel
parents: 62649
diff changeset
   138
          |> filter_out (curry (op = o apply2 name_of_bnf) BNF_Comp.DEADID_bnf)
cb20e8828196 document that n2m does not depend on most things in fp_sugar in its type
traytel
parents: 62649
diff changeset
   139
          |> find_first (fn bnf => Type.could_unify (T_of_bnf bnf, T))
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   140
          |> Option.map (fn bnf =>
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   141
            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
   142
            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
   143
          |> 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
   144
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   145
        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
   146
              let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   147
                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
   148
                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
   149
                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
   150
              in
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   151
                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
   152
              end
54244
0753e8866ac8 more robust treatment of dead variables in n2m
traytel
parents: 54242
diff changeset
   153
          | 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
   154
              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
   155
          | 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
   156
      in
54298
347c3b0cab44 handle independent functions defined in parallel in N2M (in presence of type variables, see ce58fb149ff6)
traytel
parents: 54244
diff changeset
   157
        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
   158
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   159
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   160
    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
   161
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   162
    val rel_unfolds = maps (no_refl o single o rel_def_of_bnf) fp_pre_bnfs;
58579
b7bc5ba2f3fb rename 'rel_xtor_co_induct_thm' to 'xtor_rel_co_induct'
desharna
parents: 58578
diff changeset
   163
    val rel_xtor_co_inducts = of_fp_res (split_conj_thm o #xtor_rel_co_induct)
62649
d23be25c0835 normalize schematic names since they are used to instantiate the theorem later
traytel
parents: 61101
diff changeset
   164
      |> map (zero_var_indexes o 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
   165
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   166
    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
   167
    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
   168
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   169
    fun cast castA castB pre_rel =
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   170
      let
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   171
        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
   172
          (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
   173
      in
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   174
        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
   175
          (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
   176
      end;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   177
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   178
    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
   179
    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
   180
58375
7b92932ffea5 careful with op = in n2m (actually by Dmitriy Traytel)
blanchet
parents: 58352
diff changeset
   181
    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
   182
    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
   183
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   184
    fun mutual_instantiate ctxt inst =
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   185
      let
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   186
        val thetas = AList.group (op =) (mutual_cliques ~~ inst);
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   187
      in
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   188
        map2 (infer_instantiate ctxt o the o AList.lookup (op =) thetas) mutual_cliques
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   189
      end;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   190
59049
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   191
    val rel_xtor_co_inducts_inst =
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   192
      let
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   193
        val extract =
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   194
          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
   195
        val raw_phis = map (extract o HOLogic.dest_Trueprop o Thm.concl_of) rel_xtor_co_inducts;
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   196
        val inst = map (fn (t, u) => (#1 (dest_Var t), Thm.cterm_of lthy u)) (raw_phis ~~ pre_phis);
59049
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   197
      in
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   198
        mutual_instantiate lthy inst rel_xtor_co_inducts
59049
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   199
      end
0d1bfc982501 preinstantiate (co)inductions in N2M to handle mutual but separated SCCs
traytel
parents: 58634
diff changeset
   200
58579
b7bc5ba2f3fb rename 'rel_xtor_co_induct_thm' to 'xtor_rel_co_induct'
desharna
parents: 58578
diff changeset
   201
    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
   202
      mk_xtor_rel_co_induct_thm fp (@{map 3} cast castAs castBs pre_rels) pre_phis rels phis xs ys
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   203
        xtors xtor's (mk_rel_xtor_co_induct_tac 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
   204
          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
   205
        lthy;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   206
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   207
    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
   208
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   209
    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
   210
      (case fp of
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   211
        Least_FP =>
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   212
          let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   213
            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
   214
              |> 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
   215
            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
   216
              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
   217
              in mk_Grp (HOLogic.Collect_const T $ P) (HOLogic.id_const T) end;
60788
5e2df6bd906c updated to infer_instantiate;
wenzelm
parents: 60728
diff changeset
   218
            val cts =
5e2df6bd906c updated to infer_instantiate;
wenzelm
parents: 60728
diff changeset
   219
              map (SOME o Thm.cterm_of names_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
   220
            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
   221
              @{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
   222
            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
   223
              @{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
   224
          in
60788
5e2df6bd906c updated to infer_instantiate;
wenzelm
parents: 60728
diff changeset
   225
            infer_instantiate' names_lthy 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
   226
            |> singleton (Proof_Context.export names_lthy lthy)
58375
7b92932ffea5 careful with op = in n2m (actually by Dmitriy Traytel)
blanchet
parents: 58352
diff changeset
   227
            |> 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
   228
                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
   229
            |> 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
   230
            |> 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
   231
            |> 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
   232
               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
   233
               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
   234
               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
   235
            |> 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
   236
               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
   237
          end
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   238
      | Greatest_FP =>
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   239
          let
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59580
diff changeset
   240
            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
   241
          in
60788
5e2df6bd906c updated to infer_instantiate;
wenzelm
parents: 60728
diff changeset
   242
            infer_instantiate' lthy cts xtor_rel_co_induct
58375
7b92932ffea5 careful with op = in n2m (actually by Dmitriy Traytel)
blanchet
parents: 58352
diff changeset
   243
            |> 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
   244
                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
   245
            |> 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
   246
            |> 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
   247
            |> 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
   248
          end);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   249
62689
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   250
    val timer = time (timer "Nested-to-mutual (co)induction");
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   251
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   252
    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
   253
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   254
    val fold_strTs = map2 mk_co_algT fold_preTs Xs;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   255
    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
   256
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   257
    val ((fold_strs, fold_strs'), names_lthy) = names_lthy
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   258
      |> mk_Frees' "s" fold_strTs;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   259
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   260
    val fp_un_folds = of_fp_res #xtor_un_folds;
62719
blanchet
parents: 62689
diff changeset
   261
    val ns = map (length o #Ts o snd) indexed_fp_ress;
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   262
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   263
    fun force_fold i TU raw_un_fold =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   264
      let
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   265
        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
   266
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   267
        val approx_un_fold = raw_un_fold
57802
9c065009cd8a handle deep nesting in N2M
traytel
parents: 57801
diff changeset
   268
          |> 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
   269
        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
   270
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   271
        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
   272
        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
   273
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   274
        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
   275
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   276
        val fold_pre_deads_only_Ts =
62684
cb20e8828196 document that n2m does not depend on most things in fp_sugar in its type
traytel
parents: 62649
diff changeset
   277
          map (typ_subst_nonatomic_sorted (map (rpair dummyT) (As @ sorted_fpTs))) fold_preTs';
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   278
58340
blanchet
parents: 58203
diff changeset
   279
        val (mutual_clique, TUs) =
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   280
          map_split dest_co_algT (binder_fun_types (fastype_of approx_un_fold))
55478
3a6efda01da4 made N2M more robust w.r.t. identical nested types
traytel
parents: 55414
diff changeset
   281
          |>> map subst
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   282
          |> `(fn (_, Ys) => nth mutual_cliques
62719
blanchet
parents: 62689
diff changeset
   283
            (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
   284
          ||> uncurry (map2 mk_co_algT);
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   285
        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
   286
        val js = find_indices (fn ((cl, cand), TU) =>
58340
blanchet
parents: 58203
diff changeset
   287
          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
   288
        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
   289
      in
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   290
        force_typ names_lthy (Tpats ---> TU) raw_un_fold
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   291
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   292
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   293
    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
   294
      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
   295
        (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
   296
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   297
    fun mk_un_fold b_opt un_folds lthy TU =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   298
      let
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   299
        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
   300
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   301
        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
   302
        val i = find_index (fn T => x = T) Xs;
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   303
        val TUfold =
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   304
          (case find_first (fn f => body_fun_type (fastype_of f) = TU) un_folds of
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   305
            NONE => force_fold i TU (nth fp_un_folds i)
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   306
          | SOME f => f);
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   307
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   308
        val TUs = binder_fun_types (fastype_of TUfold);
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   309
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   310
        fun mk_s TU' =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   311
          let
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   312
            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
   313
53303
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 => 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
   315
            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
   316
            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
   317
            val abs = nth abss i;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   318
            val rep = nth reps i;
54233
6d64669184ae conceal definition
blanchet
parents: 53753
diff changeset
   319
            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
   320
            val sF' =
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   321
              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
   322
                handle Term.TYPE _ => sF;
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   323
            val F = nth fold_preTs i;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   324
            val s = nth fold_strs i;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   325
          in
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   326
            if sF = F then s
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   327
            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
   328
            else
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   329
              let
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   330
                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
   331
                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
   332
                  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
   333
                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
   334
                  |> 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
   335
                  |> hidden_to_unit;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   336
                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
   337
                fun mk_smap_arg T_to_U =
37745650a3f4 register 'prod' and 'sum' as datatypes, to allow N2M through them
blanchet
parents: 58340
diff changeset
   338
                  (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
   339
                    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
   340
                  else
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   341
                    fst (fst (mk_un_fold NONE un_folds lthy T_to_U)));
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   342
                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
   343
              in
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   344
                mk_co_comp_abs_rep sF sF' fp_abs fp_rep abs rep
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   345
                  (mk_co_comp s (Term.list_comb (smap, smap_args)))
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   346
              end
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   347
          end;
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   348
        val t = Term.list_comb (TUfold, 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
   349
      in
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   350
        (case b_opt of
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   351
          NONE => ((t, Drule.dummy_thm), lthy)
59859
f9d1442c70f3 tuned signature;
wenzelm
parents: 59819
diff changeset
   352
        | SOME b => Local_Theory.define ((b, NoSyn), ((Binding.concealed (Thm.def_binding b), []),
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   353
            fold_rev Term.absfree fold_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
   354
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   355
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   356
    val foldN = case_fp fp ctor_foldN dtor_unfoldN;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   357
    fun mk_un_folds lthy =
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   358
      fold2 (fn TU => fn b => fn ((un_folds, defs), lthy) =>
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   359
        mk_un_fold (SOME b) un_folds lthy TU |>> (fn (f, d) => (f :: un_folds, d :: defs)))
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   360
      resTs (map (Binding.suffix_name ("_" ^ foldN)) bs) (([], []), lthy)
57527
1b07ca054327 add helper function map_prod
desharna
parents: 57397
diff changeset
   361
      |>> map_prod rev rev;
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   362
    val ((raw_xtor_un_folds, raw_xtor_un_fold_defs), (lthy, raw_lthy)) = lthy
61101
7b915ca69af1 use open/close_target rather than Local_Theory.restore to get polymorphic definitions;
traytel
parents: 60788
diff changeset
   363
      |> Local_Theory.open_target |> snd
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   364
      |> mk_un_folds
61101
7b915ca69af1 use open/close_target rather than Local_Theory.restore to get polymorphic definitions;
traytel
parents: 60788
diff changeset
   365
      ||> `Local_Theory.close_target;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   366
53331
20440c789759 prove theorem in the right context (that knows about local variables)
traytel
parents: 53309
diff changeset
   367
    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
   368
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   369
    val xtor_un_folds = map (Morphism.term phi) raw_xtor_un_folds;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   370
    val xtor_un_fold_defs = map (Morphism.thm phi) raw_xtor_un_fold_defs;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   371
    val xtor_un_folds' = map2 (fn raw => fn t => Const (fst (dest_Const t), fastype_of raw)) raw_xtor_un_folds xtor_un_folds;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   372
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   373
    val fp_un_fold_o_maps = of_fp_res #xtor_un_fold_o_maps
56486
753b779d070d made N2M tactic more robust
traytel
parents: 56484
diff changeset
   374
      |> maps (fn thm => [thm, thm RS rewrite_comp_comp]);
55868
37b99986d435 rationalized internals
blanchet
parents: 55854
diff changeset
   375
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   376
    val un_folds = map (fn fold => Term.list_comb (fold, fold_strs)) raw_xtor_un_folds;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   377
    val fold_mapTs = co_swap (As @ fpTs, As @ Xs);
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   378
    val pre_fold_maps = @{map 2} (fn Ds => uncurry (mk_map_of_bnf Ds) fold_mapTs) Dss bnfs
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   379
    fun mk_pre_fold_maps fs =
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   380
      map (fn mapx => Term.list_comb (mapx, map HOLogic.id_const As @ fs)) pre_fold_maps;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   381
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   382
    val pre_map_defs = no_refl (map map_def_of_bnf bnfs);
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   383
    val fp_map_defs = no_refl (map map_def_of_bnf fp_pre_bnfs);
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   384
    val map_defs = pre_map_defs @ fp_map_defs;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   385
    val pre_rel_defs = no_refl (map rel_def_of_bnf bnfs);
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   386
    val fp_rel_defs = no_refl (map rel_def_of_bnf fp_pre_bnfs);
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   387
    val rel_defs = pre_rel_defs @ fp_rel_defs;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   388
    fun mk_Rep_o_Abs thm = (thm RS @{thm type_copy_Rep_o_Abs})
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   389
      |> (fn thm => [thm, thm RS rewrite_comp_comp]);
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   390
    val fp_Rep_o_Abss = maps mk_Rep_o_Abs fp_type_definitions;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   391
    val pre_Rep_o_Abss = maps mk_Rep_o_Abs type_definitions;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   392
    val Rep_o_Abss = fp_Rep_o_Abss @ pre_Rep_o_Abss;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   393
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   394
    val unfold_map = map (unfold_thms lthy (id_apply :: pre_map_defs));
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   395
    val simp_thms = case_fp fp @{thm comp_assoc} @{thm comp_assoc[symmetric]} ::
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   396
      @{thms id_apply comp_id id_comp};
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   397
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   398
    val eq_thm_prop_untyped = Term.aconv_untyped o apply2 Thm.full_prop_of;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   399
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   400
    val map_thms = no_refl (maps (fn bnf =>
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   401
       let val map_comp0 = map_comp0_of_bnf bnf RS sym
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   402
       in [map_comp0, map_comp0 RS rewrite_comp_comp, map_id0_of_bnf bnf] end)
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   403
      fp_or_nesting_bnfs) @
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   404
      remove eq_thm_prop_untyped (case_fp fp @{thm comp_assoc[symmetric]} @{thm comp_assoc})
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   405
      (map2 (fn thm => fn bnf =>
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   406
        @{thm type_copy_map_comp0_undo} OF
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   407
          (replicate 3 thm @ unfold_map [map_comp0_of_bnf bnf]) RS
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   408
          rewrite_comp_comp)
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   409
      type_definitions bnfs);
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   410
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   411
    val xtor_un_fold_thms =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   412
      let
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   413
        val pre_fold_maps = mk_pre_fold_maps un_folds;
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   414
        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
   415
          let
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   416
            val lhs = mk_co_comp f xtor;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   417
            val rhs = mk_co_comp s smap;
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   418
          in
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   419
            HOLogic.mk_eq (lhs,
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   420
              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
   421
                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
   422
          end;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   423
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   424
        val goals = @{map 8} mk_goals un_folds xtors fold_strs pre_fold_maps fp_abss fp_reps abss reps;
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   425
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   426
        val fp_un_folds = map (mk_pointfree lthy) (of_fp_res #xtor_un_fold_thms);
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55575
diff changeset
   427
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   428
        val simps = flat [simp_thms, raw_xtor_un_fold_defs, map_defs, fp_un_folds,
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   429
          fp_un_fold_o_maps, map_thms, Rep_o_Abss];
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   430
      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
   431
        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
   432
        |> HOLogic.mk_Trueprop
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   433
        |> fold_rev Logic.all fold_strs
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   434
        |> (fn goal => Goal.prove_sorry raw_lthy [] [] goal
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   435
          (fn {context = ctxt, prems = _} => mk_xtor_un_fold_tac ctxt n simps))
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
   436
        |> 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
   437
        |> 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
   438
        |> 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
   439
        |> 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
   440
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   441
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   442
    val xtor_un_fold_o_maps = of_fp_res #xtor_un_fold_o_maps
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   443
      |> maps (fn thm => [thm, thm RS rewrite_comp_comp]);
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   444
    val xtor_un_fold_unique_thm =
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   445
      let
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   446
        val (fs, _) = names_lthy |> mk_Frees "f" resTs;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   447
        val fold_maps = mk_pre_fold_maps fs;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   448
        fun mk_prem f s mapx xtor fp_abs fp_rep abs rep =
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   449
          let
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   450
            val lhs = mk_co_comp f xtor;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   451
            val rhs = mk_co_comp s mapx;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   452
          in
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   453
            mk_Trueprop_eq (lhs,
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   454
              mk_co_comp_abs_rep (co_alg_funT (fastype_of lhs)) (co_alg_funT (fastype_of rhs))
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   455
                fp_abs fp_rep abs rep rhs)
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   456
          end;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   457
        val prems = @{map 8} mk_prem fs fold_strs fold_maps xtors fp_abss fp_reps abss reps;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   458
        val concl = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   459
          (map2 (curry HOLogic.mk_eq) fs un_folds));
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   460
        val vars = Variable.add_free_names raw_lthy concl [];
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   461
        val fp_un_fold_uniques0 = of_fp_res (split_conj_thm o #xtor_un_fold_unique)
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   462
          |> map (Drule.zero_var_indexes o unfold_thms lthy fp_map_defs);
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   463
        val names = fp_un_fold_uniques0
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   464
          |> map (Thm.concl_of #> HOLogic.dest_Trueprop
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   465
            #> HOLogic.dest_eq #> fst #> dest_Var #> fst);
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   466
        val inst = names ~~ map (Thm.cterm_of lthy) fs;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   467
        val fp_un_fold_uniques = mutual_instantiate lthy inst fp_un_fold_uniques0;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   468
        val map_arg_congs =
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   469
          map (fn bnf => mk_arg_cong lthy (live_of_bnf bnf) (map_of_bnf bnf)
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   470
            |> unfold_thms lthy (pre_map_defs @ simp_thms)) nesting_bnfs;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   471
      in
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   472
        Goal.prove_sorry raw_lthy vars prems concl
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   473
          (mk_xtor_un_fold_unique_tac fp raw_xtor_un_fold_defs map_arg_congs xtor_un_fold_o_maps
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   474
            Rep_o_Abss fp_un_fold_uniques simp_thms map_thms map_defs)
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   475
          |> Thm.close_derivation
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   476
          |> case_fp fp I (fn thm => thm OF replicate n sym)
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   477
          |> Morphism.thm phi
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   478
      end;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   479
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   480
    val ABs = As ~~ Bs;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   481
    val XYs = Xs ~~ Ys;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   482
    val ABphiTs = @{map 2} mk_pred2T As Bs;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   483
    val XYphiTs = @{map 2} mk_pred2T Xs Ys;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   484
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   485
    val ((ABphis, XYphis), _) = names_lthy
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   486
      |> mk_Frees "R" ABphiTs
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   487
      ||>> mk_Frees "S" XYphiTs;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   488
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   489
    val pre_rels = @{map 2} (fn Ds => mk_rel_of_bnf Ds (As @ Xs) (Bs @ Ys)) Dss bnfs;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   490
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   491
    val ns = map (fn i => length (filter (fn c => i = c) mutual_cliques)) mutual_cliques;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   492
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   493
    val map_transfers = map (funpow live (fn thm => thm RS @{thm rel_funD})
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   494
        #> unfold_thms lthy pre_rel_defs)
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   495
      (map map_transfer_of_bnf bnfs);
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   496
    val fp_un_fold_transfers = map2 (fn n => funpow n (fn thm => thm RS @{thm rel_funD})
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   497
        #> unfold_thms lthy fp_rel_defs)
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   498
      ns (of_fp_res #xtor_un_fold_transfers);
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   499
    val pre_Abs_transfers = map (fn thm => @{thm Abs_transfer} OF [thm, thm]) type_definitions;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   500
    val fp_Abs_transfers = map (fn thm => @{thm Abs_transfer} OF [thm, thm]) fp_type_definitions;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   501
    val Abs_transfers = pre_Abs_transfers @ fp_Abs_transfers;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   502
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   503
    fun tac {context = ctxt, prems = _} =
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   504
      mk_xtor_un_fold_transfer_tac ctxt n xtor_un_fold_defs rel_defs fp_un_fold_transfers
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   505
        map_transfers Abs_transfers fp_or_nesting_rel_eqs;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   506
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   507
    val xtor_un_fold_transfer_thms =
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   508
      mk_xtor_co_iter_transfer_thms fp pre_rels XYphis XYphis rels ABphis
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   509
        xtor_un_folds' (map (subst_atomic_types (ABs @ XYs)) xtor_un_folds') tac lthy;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   510
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   511
    val timer = time (timer "Nested-to-mutual (co)iteration");
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   512
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   513
    val xtor_maps = of_fp_res #xtor_maps;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   514
    val xtor_rels = of_fp_res #xtor_rels;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   515
    fun mk_Ts Cs = map (typ_subst_atomic (As ~~ Cs)) fpTs;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   516
    val phi = Local_Theory.target_morphism lthy;
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   517
    val export = map (Morphism.term phi);
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   518
    val ((xtor_co_recs, (xtor_co_rec_thms, xtor_co_rec_unique_thm, xtor_co_rec_o_map_thms,
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   519
        xtor_co_rec_transfer_thms)), lthy) = lthy
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   520
      |> derive_xtor_co_recs fp bs mk_Ts (Dss, resDs) bnfs
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   521
        (export xtors) (export xtor_un_folds)
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   522
        xtor_un_fold_unique_thm xtor_un_fold_thms xtor_un_fold_transfer_thms xtor_maps xtor_rels
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   523
        (@{map 2} (curry (SOME o @{apply 2} (morph_absT_info phi))) fp_absT_infos absT_infos);
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   524
62689
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   525
    val timer = time (timer "Nested-to-mutual (co)recursion");
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   526
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   527
    val common_notes =
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   528
      (case fp of
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   529
        Least_FP =>
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   530
        [(ctor_inductN, [xtor_co_induct_thm]),
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   531
         (ctor_rel_inductN, [xtor_rel_co_induct])]
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   532
      | Greatest_FP =>
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   533
        [(dtor_coinductN, [xtor_co_induct_thm]),
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   534
         (dtor_rel_coinductN, [xtor_rel_co_induct])])
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   535
      |> map (fn (thmN, thms) =>
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   536
        ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]));
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   537
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   538
    val notes =
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   539
      (case fp of
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   540
        Least_FP => [(ctor_foldN, xtor_un_fold_thms)]
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   541
      | Greatest_FP => [(dtor_unfoldN, xtor_un_fold_thms)])
62689
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   542
      |> map (apsnd (map single))
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   543
      |> maps (fn (thmN, thmss) =>
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   544
        map2 (fn b => fn thms =>
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   545
          ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]))
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   546
        bs thmss);
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   547
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   548
    val lthy = lthy |> Config.get lthy bnf_internals
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   549
      ? snd o Local_Theory.notes (common_notes @ notes);
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   550
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   551
    (* 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
   552
       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
   553
    val fp_res =
62684
cb20e8828196 document that n2m does not depend on most things in fp_sugar in its type
traytel
parents: 62649
diff changeset
   554
      ({Ts = fpTs, bnfs = of_fp_res #bnfs, pre_bnfs = bnfs, absT_infos = absT_infos,
cb20e8828196 document that n2m does not depend on most things in fp_sugar in its type
traytel
parents: 62649
diff changeset
   555
        dtors = dtors, ctors = ctors,
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   556
        xtor_un_folds = xtor_un_folds, xtor_co_recs = xtor_co_recs,
59819
dbec7f33093d store low-level (un)fold constants
blanchet
parents: 59621
diff changeset
   557
        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
   558
        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
   559
        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
   560
        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
   561
        dtor_injects = of_fp_res #dtor_injects (*too general types*),
62863
e0b894bba6ff single uniqueness theorems for map, (un)fold, (co)rec for mutual (co)datatypes
traytel
parents: 62721
diff changeset
   562
        xtor_maps = of_fp_res #xtor_maps (*too general types and terms*),
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   563
        xtor_map_unique = xtor_un_fold_unique_thm (*wrong*),
58584
b6492a7abb59 rename 'xtor_set_thmss' to 'xtor_setss'
desharna
parents: 58583
diff changeset
   564
        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
   565
        xtor_rels = of_fp_res #xtor_rels (*too general types and terms*),
63045
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   566
        xtor_un_fold_thms = xtor_un_fold_thms,
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   567
        xtor_co_rec_thms = xtor_co_rec_thms,
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   568
        xtor_un_fold_unique = xtor_un_fold_unique_thm,
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   569
        xtor_co_rec_unique = xtor_co_rec_unique_thm,
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   570
        xtor_un_fold_o_maps = fp_un_fold_o_maps (*wrong*),
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   571
        xtor_co_rec_o_maps = xtor_co_rec_o_map_thms (*wrong*),
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   572
        xtor_un_fold_transfers = xtor_un_fold_transfer_thms,
c50c764aab10 n2m operates on (un)folds
traytel
parents: 62907
diff changeset
   573
        xtor_co_rec_transfers = xtor_co_rec_transfer_thms (*wrong*),
59856
ed0ca9029021 export more low-level theorems in data structure (partly for 'corec')
blanchet
parents: 59819
diff changeset
   574
        xtor_rel_co_induct = xtor_rel_co_induct, dtor_set_inducts = []}
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 54298
diff changeset
   575
       |> 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
   576
  in
62689
9b8b3db6ac03 more debugging
blanchet
parents: 62684
diff changeset
   577
    timer; (fp_res, lthy)
54242
blanchet
parents: 54233
diff changeset
   578
  end;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   579
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   580
end;