src/HOL/Tools/BNF/bnf_fp_n2m_sugar.ML
author blanchet
Wed, 09 Apr 2014 18:40:21 +0200
changeset 56485 008634379465
parent 56484 c451cf8b29c8
child 56521 20cfb18a53ba
permissions -rw-r--r--
generate cliques for 'prim(co)rec' N2M
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_sugar.ML
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
     2
    Author:     Jasmin Blanchette, 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
Suggared 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_SUGAR =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
     9
sig
55343
5ebf832b58a1 expand 'split' in direct corecursion as well
blanchet
parents: 55336
diff changeset
    10
  val unfold_lets_splits: term -> term
54243
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    11
  val dest_map: Proof.context -> string -> term -> term * term list
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    12
56484
c451cf8b29c8 thread mutual cliques through
blanchet
parents: 55966
diff changeset
    13
  val mutualize_fp_sugars: BNF_Util.fp_kind -> int list -> binding list -> typ list -> term list ->
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
    14
    term list list list list -> BNF_FP_Def_Sugar.fp_sugar list -> local_theory ->
53746
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53678
diff changeset
    15
    (BNF_FP_Def_Sugar.fp_sugar list
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53678
diff changeset
    16
     * (BNF_FP_Def_Sugar.lfp_sugar_thms option * BNF_FP_Def_Sugar.gfp_sugar_thms option))
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53678
diff changeset
    17
    * local_theory
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
    18
  val nested_to_mutual_fps: BNF_Util.fp_kind -> binding list -> typ list -> term list ->
54009
f138452e8265 got rid of dead feature
blanchet
parents: 54006
diff changeset
    19
    (term * term list list) list list -> local_theory ->
53746
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53678
diff changeset
    20
    (typ list * int list * BNF_FP_Def_Sugar.fp_sugar list
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53678
diff changeset
    21
     * (BNF_FP_Def_Sugar.lfp_sugar_thms option * BNF_FP_Def_Sugar.gfp_sugar_thms option))
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53678
diff changeset
    22
    * local_theory
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    23
end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    24
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    25
structure BNF_FP_N2M_Sugar : BNF_FP_N2M_SUGAR =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    26
struct
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    27
54006
9fe1bd54d437 renamed theory file
blanchet
parents: 53974
diff changeset
    28
open Ctr_Sugar
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    29
open BNF_Util
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    30
open BNF_Def
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    31
open BNF_FP_Util
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    32
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
    33
open BNF_FP_N2M
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    34
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    35
val n2mN = "n2m_"
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    36
54256
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    37
type n2m_sugar = fp_sugar list * (lfp_sugar_thms option * gfp_sugar_thms option);
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    38
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    39
structure Data = Generic_Data
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    40
(
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    41
  type T = n2m_sugar Typtab.table;
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    42
  val empty = Typtab.empty;
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    43
  val extend = I;
55394
d5ebe055b599 more liberal merging of BNFs and constructor sugar
blanchet
parents: 55343
diff changeset
    44
  fun merge data : T = Typtab.merge (K true) data;
54256
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    45
);
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    46
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    47
fun morph_n2m_sugar phi (fp_sugars, (lfp_sugar_thms_opt, gfp_sugar_thms_opt)) =
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    48
  (map (morph_fp_sugar phi) fp_sugars,
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    49
   (Option.map (morph_lfp_sugar_thms phi) lfp_sugar_thms_opt,
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    50
    Option.map (morph_gfp_sugar_thms phi) gfp_sugar_thms_opt));
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    51
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    52
val transfer_n2m_sugar =
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 54303
diff changeset
    53
  morph_n2m_sugar o Morphism.transfer_morphism o Proof_Context.theory_of;
54256
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    54
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    55
fun n2m_sugar_of ctxt =
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    56
  Typtab.lookup (Data.get (Context.Proof ctxt))
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    57
  #> Option.map (transfer_n2m_sugar ctxt);
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    58
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    59
fun register_n2m_sugar key n2m_sugar =
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    60
  Local_Theory.declaration {syntax = false, pervasive = false}
55444
ec73f81e49e7 iteration n in the 'default' vs. 'update_new' vs. 'update' saga -- 'update' makes sense now that we honor the canonical order on 'merge' (as opposed to raising 'DUP')
blanchet
parents: 55414
diff changeset
    61
    (fn phi => Data.map (Typtab.update (key, morph_n2m_sugar phi n2m_sugar)));
54256
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
    62
55343
5ebf832b58a1 expand 'split' in direct corecursion as well
blanchet
parents: 55336
diff changeset
    63
fun unfold_lets_splits (Const (@{const_name Let}, _) $ arg1 $ arg2) =
5ebf832b58a1 expand 'split' in direct corecursion as well
blanchet
parents: 55336
diff changeset
    64
    unfold_lets_splits (betapply (arg2, arg1))
55414
eab03e9cee8a renamed '{prod,sum,bool,unit}_case' to 'case_...'
blanchet
parents: 55409
diff changeset
    65
  | unfold_lets_splits (t as Const (@{const_name case_prod}, _) $ u) =
55343
5ebf832b58a1 expand 'split' in direct corecursion as well
blanchet
parents: 55336
diff changeset
    66
    (case unfold_lets_splits u of
55336
1401434a7e83 fixed handling of 'split'
blanchet
parents: 55099
diff changeset
    67
      u' as Abs (s1, T1, Abs (s2, T2, _)) =>
1401434a7e83 fixed handling of 'split'
blanchet
parents: 55099
diff changeset
    68
      let val v = Var ((s1 ^ s2, Term.maxidx_of_term u' + 1), HOLogic.mk_prodT (T1, T2)) in
1401434a7e83 fixed handling of 'split'
blanchet
parents: 55099
diff changeset
    69
        lambda v (incr_boundvars 1 (betapplys (u', [HOLogic.mk_fst v, HOLogic.mk_snd v])))
54243
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    70
      end
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    71
    | _ => t)
55343
5ebf832b58a1 expand 'split' in direct corecursion as well
blanchet
parents: 55336
diff changeset
    72
  | unfold_lets_splits (t $ u) = betapply (pairself unfold_lets_splits (t, u))
5ebf832b58a1 expand 'split' in direct corecursion as well
blanchet
parents: 55336
diff changeset
    73
  | unfold_lets_splits (Abs (s, T, t)) = Abs (s, T, unfold_lets_splits t)
5ebf832b58a1 expand 'split' in direct corecursion as well
blanchet
parents: 55336
diff changeset
    74
  | unfold_lets_splits t = t;
54243
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    75
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    76
fun mk_map_pattern ctxt s =
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    77
  let
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    78
    val bnf = the (bnf_of ctxt s);
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    79
    val mapx = map_of_bnf bnf;
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    80
    val live = live_of_bnf bnf;
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    81
    val (f_Ts, _) = strip_typeN live (fastype_of mapx);
54265
3e1d230f1c00 make local theory operations non-pervasive (makes more intuitive sense)
blanchet
parents: 54256
diff changeset
    82
    val fs = map_index (fn (i, T) => Var (("?f", i), T)) f_Ts;
54243
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    83
  in
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    84
    (mapx, betapplys (mapx, fs))
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    85
  end;
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    86
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    87
fun dest_map ctxt s call =
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    88
  let
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    89
    val (map0, pat) = mk_map_pattern ctxt s;
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    90
    val (_, tenv) = fo_match ctxt call pat;
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    91
  in
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    92
    (map0, Vartab.fold_rev (fn (_, (_, f)) => cons f) tenv [])
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    93
  end;
a596292be9a8 more robust n2m w.r.t. 'let's
blanchet
parents: 54240
diff changeset
    94
54276
d26b6b935a6f took out loopy code
blanchet
parents: 54275
diff changeset
    95
fun dest_abs_or_applied_map _ _ (Abs (_, _, t)) = (Term.dummy, [t])
d26b6b935a6f took out loopy code
blanchet
parents: 54275
diff changeset
    96
  | dest_abs_or_applied_map ctxt s (t1 $ _) = dest_map ctxt s t1;
54239
9bd91d5d8a7b handle constructor syntax in n2m primcorec
blanchet
parents: 54235
diff changeset
    97
54245
f91022745c85 better error handling
blanchet
parents: 54243
diff changeset
    98
fun map_partition f xs =
f91022745c85 better error handling
blanchet
parents: 54243
diff changeset
    99
  fold_rev (fn x => fn (ys, (good, bad)) =>
f91022745c85 better error handling
blanchet
parents: 54243
diff changeset
   100
      case f x of SOME y => (y :: ys, (x :: good, bad)) | NONE => (ys, (good, x :: bad)))
f91022745c85 better error handling
blanchet
parents: 54243
diff changeset
   101
    xs ([], ([], []));
f91022745c85 better error handling
blanchet
parents: 54243
diff changeset
   102
54256
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
   103
fun key_of_fp_eqs fp fpTs fp_eqs =
55966
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55868
diff changeset
   104
  Type (case_fp fp "l" "g", fpTs @ maps (fn (x, T) => [TFree x, T]) fp_eqs);
54256
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
   105
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   106
fun get_indices callers t =
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   107
  callers
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   108
  |> map_index (fn (i, v) => if exists_subterm (equal v) t then SOME i else NONE)
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   109
  |> map_filter I;
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   110
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   111
(* TODO: test with sort constraints on As *)
56484
c451cf8b29c8 thread mutual cliques through
blanchet
parents: 55966
diff changeset
   112
fun mutualize_fp_sugars fp cliques bs fpTs callers callssss fp_sugars0 no_defs_lthy0 =
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   113
  let
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   114
    val thy = Proof_Context.theory_of no_defs_lthy0;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   115
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   116
    val qsotm = quote o Syntax.string_of_term no_defs_lthy0;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   117
55479
ece4910c3ea0 improved 'datatype_new_compat': generate more fixpoint equations for types like 'datatype_new x = C (x list) (x list)' (here, one equation for each x list instead of a single for both), for higher compatibility + code generation attributes on the recursor
blanchet
parents: 55444
diff changeset
   118
    fun incompatible_calls ts =
ece4910c3ea0 improved 'datatype_new_compat': generate more fixpoint equations for types like 'datatype_new x = C (x list) (x list)' (here, one equation for each x list instead of a single for both), for higher compatibility + code generation attributes on the recursor
blanchet
parents: 55444
diff changeset
   119
      error ("Incompatible " ^ co_prefix fp ^ "recursive calls: " ^ commas (map qsotm ts));
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   120
    fun mutual_self_call caller t =
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   121
      error ("Unsupported mutual self-call " ^ qsotm t ^ " from " ^ qsotm caller);
54301
e0943a2ee400 added check to avoid odd situations the N2M code cannot handle
blanchet
parents: 54300
diff changeset
   122
    fun nested_self_call t =
e0943a2ee400 added check to avoid odd situations the N2M code cannot handle
blanchet
parents: 54300
diff changeset
   123
      error ("Unsupported nested self-call " ^ qsotm t);
54245
f91022745c85 better error handling
blanchet
parents: 54243
diff changeset
   124
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   125
    val b_names = map Binding.name_of bs;
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   126
    val fp_b_names = map base_name_of_typ fpTs;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   127
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   128
    val nn = length fpTs;
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55486
diff changeset
   129
    val kks = 0 upto nn - 1;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   130
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55486
diff changeset
   131
    fun target_ctr_sugar_of_fp_sugar fpT ({T, ctr_sugar, ...} : fp_sugar) =
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   132
      let
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   133
        val rho = Vartab.fold (cons o apsnd snd) (Sign.typ_match thy (T, fpT) Vartab.empty) [];
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 54303
diff changeset
   134
        val phi = Morphism.term_morphism "BNF" (Term.subst_TVars rho);
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   135
      in
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55486
diff changeset
   136
        morph_ctr_sugar phi ctr_sugar
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   137
      end;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   138
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55486
diff changeset
   139
    val ctr_defss = map #ctr_defs fp_sugars0;
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55486
diff changeset
   140
    val mapss = map #maps fp_sugars0;
54302
880e5417b800 removed dead code
blanchet
parents: 54301
diff changeset
   141
    val ctr_sugars = map2 target_ctr_sugar_of_fp_sugar fpTs fp_sugars0;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   142
54302
880e5417b800 removed dead code
blanchet
parents: 54301
diff changeset
   143
    val ctrss = map #ctrs ctr_sugars;
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   144
    val ctr_Tss = map (map fastype_of) ctrss;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   145
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   146
    val As' = fold (fold Term.add_tfreesT) ctr_Tss [];
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   147
    val As = map TFree As';
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   148
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   149
    val ((Cs, Xs), no_defs_lthy) =
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   150
      no_defs_lthy0
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   151
      |> fold Variable.declare_typ As
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   152
      |> mk_TFrees nn
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   153
      ||>> variant_tfrees fp_b_names;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   154
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   155
    fun check_call_dead live_call call =
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   156
      if null (get_indices callers call) then () else incompatible_calls [live_call, call];
54245
f91022745c85 better error handling
blanchet
parents: 54243
diff changeset
   157
55480
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55479
diff changeset
   158
    fun freeze_fpTs_type_based_default (T as Type (s, Ts)) =
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55479
diff changeset
   159
        (case filter (curry (op =) T o snd) (map_index I fpTs) of
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55479
diff changeset
   160
          [(kk, _)] => nth Xs kk
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55479
diff changeset
   161
        | _ => Type (s, map freeze_fpTs_type_based_default Ts))
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55479
diff changeset
   162
      | freeze_fpTs_type_based_default T = T;
55479
ece4910c3ea0 improved 'datatype_new_compat': generate more fixpoint equations for types like 'datatype_new x = C (x list) (x list)' (here, one equation for each x list instead of a single for both), for higher compatibility + code generation attributes on the recursor
blanchet
parents: 55444
diff changeset
   163
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   164
    fun freeze_fpTs_mutual_call kk fpT calls T =
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   165
      (case fold (union (op =)) (map (get_indices callers) calls) [] of
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   166
        [] => if T = fpT then nth Xs kk else freeze_fpTs_type_based_default T
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   167
      | [kk'] =>
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   168
        if T = fpT andalso kk' <> kk then
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   169
          mutual_self_call (nth callers kk)
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   170
            (the (find_first (not o null o get_indices callers) calls))
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   171
        else
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   172
          nth Xs kk'
55480
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55479
diff changeset
   173
      | _ => incompatible_calls calls);
54253
04cd231e2b9e nicer error message in case of duplicates
blanchet
parents: 54245
diff changeset
   174
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   175
    fun freeze_fpTs_map kk (fpT as Type (_, Ts')) (callss, (live_call :: _, dead_calls))
55480
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55479
diff changeset
   176
        (Type (s, Ts)) =
54301
e0943a2ee400 added check to avoid odd situations the N2M code cannot handle
blanchet
parents: 54300
diff changeset
   177
      if Ts' = Ts then
e0943a2ee400 added check to avoid odd situations the N2M code cannot handle
blanchet
parents: 54300
diff changeset
   178
        nested_self_call live_call
e0943a2ee400 added check to avoid odd situations the N2M code cannot handle
blanchet
parents: 54300
diff changeset
   179
      else
e0943a2ee400 added check to avoid odd situations the N2M code cannot handle
blanchet
parents: 54300
diff changeset
   180
        (List.app (check_call_dead live_call) dead_calls;
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   181
         Type (s, map2 (freeze_fpTs_call kk fpT)
55480
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55479
diff changeset
   182
           (flatten_type_args_of_bnf (the (bnf_of no_defs_lthy s)) [] (transpose callss)) Ts))
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   183
    and freeze_fpTs_call kk fpT calls (T as Type (s, _)) =
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   184
        (case map_partition (try (snd o dest_map no_defs_lthy s)) calls of
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   185
          ([], _) =>
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   186
          (case map_partition (try (snd o dest_abs_or_applied_map no_defs_lthy s)) calls of
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   187
            ([], _) => freeze_fpTs_mutual_call kk fpT calls T
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   188
          | callsp => freeze_fpTs_map kk fpT callsp T)
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   189
        | callsp => freeze_fpTs_map kk fpT callsp T)
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   190
      | freeze_fpTs_call _ _ _ T = T;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   191
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   192
    val ctr_Tsss = map (map binder_types) ctr_Tss;
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   193
    val ctrXs_Tsss = map4 (map2 o map2 oo freeze_fpTs_call) kks fpTs callssss ctr_Tsss;
55966
972f0aa7091b balance tuples that represent curried functions
blanchet
parents: 55868
diff changeset
   194
    val ctrXs_repTs = map mk_sumprodT_balanced ctrXs_Tsss;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   195
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   196
    val ns = map length ctr_Tsss;
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   197
    val kss = map (fn n => 1 upto n) ns;
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   198
    val mss = map (map length) ctr_Tsss;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   199
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55772
diff changeset
   200
    val fp_eqs = map dest_TFree Xs ~~ ctrXs_repTs;
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   201
    val key = key_of_fp_eqs fp fpTs fp_eqs;
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   202
  in
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   203
    (case n2m_sugar_of no_defs_lthy key of
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   204
      SOME n2m_sugar => (n2m_sugar, no_defs_lthy)
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   205
    | NONE =>
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   206
      let
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   207
        val base_fp_names = Name.variant_list [] fp_b_names;
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   208
        val fp_bs = map2 (fn b_name => fn base_fp_name =>
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   209
            Binding.qualify true b_name (Binding.name (n2mN ^ base_fp_name)))
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   210
          b_names base_fp_names;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   211
55702
63c80031d8dd improved accounting for dead variables when naming set functions (refines d71c2737ee21)
blanchet
parents: 55701
diff changeset
   212
        val Type (s, Us) :: _ = fpTs;
63c80031d8dd improved accounting for dead variables when naming set functions (refines d71c2737ee21)
blanchet
parents: 55701
diff changeset
   213
        val killed_As' =
63c80031d8dd improved accounting for dead variables when naming set functions (refines d71c2737ee21)
blanchet
parents: 55701
diff changeset
   214
          (case bnf_of no_defs_lthy s of
63c80031d8dd improved accounting for dead variables when naming set functions (refines d71c2737ee21)
blanchet
parents: 55701
diff changeset
   215
            NONE => As'
63c80031d8dd improved accounting for dead variables when naming set functions (refines d71c2737ee21)
blanchet
parents: 55701
diff changeset
   216
          | SOME bnf =>
63c80031d8dd improved accounting for dead variables when naming set functions (refines d71c2737ee21)
blanchet
parents: 55701
diff changeset
   217
            let
63c80031d8dd improved accounting for dead variables when naming set functions (refines d71c2737ee21)
blanchet
parents: 55701
diff changeset
   218
              val Type (_, Ts) = T_of_bnf bnf;
63c80031d8dd improved accounting for dead variables when naming set functions (refines d71c2737ee21)
blanchet
parents: 55701
diff changeset
   219
              val deads = deads_of_bnf bnf;
63c80031d8dd improved accounting for dead variables when naming set functions (refines d71c2737ee21)
blanchet
parents: 55701
diff changeset
   220
              val dead_Us =
63c80031d8dd improved accounting for dead variables when naming set functions (refines d71c2737ee21)
blanchet
parents: 55701
diff changeset
   221
                map_filter (fn (T, U) => if member (op =) deads T then SOME U else NONE) (Ts ~~ Us);
63c80031d8dd improved accounting for dead variables when naming set functions (refines d71c2737ee21)
blanchet
parents: 55701
diff changeset
   222
            in fold Term.add_tfreesT dead_Us [] end);
63c80031d8dd improved accounting for dead variables when naming set functions (refines d71c2737ee21)
blanchet
parents: 55701
diff changeset
   223
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55772
diff changeset
   224
        val fp_absT_infos = map #absT_info fp_sugars0;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55772
diff changeset
   225
55868
37b99986d435 rationalized internals
blanchet
parents: 55867
diff changeset
   226
        val ((pre_bnfs, absT_infos), (fp_res as {xtor_co_recs = xtor_co_recs0, xtor_co_induct,
37b99986d435 rationalized internals
blanchet
parents: 55867
diff changeset
   227
               dtor_injects, dtor_ctors, xtor_co_rec_thms, ...}, lthy)) =
56484
c451cf8b29c8 thread mutual cliques through
blanchet
parents: 55966
diff changeset
   228
          fp_bnf (construct_mutualized_fp fp cliques fpTs fp_sugars0) fp_bs As' killed_As' fp_eqs
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55772
diff changeset
   229
            no_defs_lthy0;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55772
diff changeset
   230
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55772
diff changeset
   231
        val fp_abs_inverses = map #abs_inverse fp_absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55772
diff changeset
   232
        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: 55772
diff changeset
   233
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55772
diff changeset
   234
        val abss = map #abs absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55772
diff changeset
   235
        val reps = map #rep absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55772
diff changeset
   236
        val absTs = map #absT absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55772
diff changeset
   237
        val repTs = map #repT absT_infos;
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55772
diff changeset
   238
        val abs_inverses = map #abs_inverse absT_infos;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   239
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   240
        val nesting_bnfs = nesty_bnfs lthy ctrXs_Tsss As;
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   241
        val nested_bnfs = nesty_bnfs lthy ctrXs_Tsss Xs;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   242
55867
79b915f26533 rationalized internals
blanchet
parents: 55864
diff changeset
   243
        val ((xtor_co_recs, recs_args_types, corecs_args_types), _) =
55868
37b99986d435 rationalized internals
blanchet
parents: 55867
diff changeset
   244
          mk_co_recs_prelims fp ctr_Tsss fpTs Cs absTs repTs ns mss xtor_co_recs0 lthy;
54256
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
   245
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   246
        fun mk_binding b suf = Binding.suffix_name ("_" ^ suf) b;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   247
55864
516ab2906e7e rationalized internals
blanchet
parents: 55863
diff changeset
   248
        val ((co_recs, co_rec_defs), lthy) =
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   249
          fold_map2 (fn b =>
55867
79b915f26533 rationalized internals
blanchet
parents: 55864
diff changeset
   250
              if fp = Least_FP then define_rec recs_args_types (mk_binding b) fpTs Cs reps
79b915f26533 rationalized internals
blanchet
parents: 55864
diff changeset
   251
              else define_corec (the corecs_args_types) (mk_binding b) fpTs Cs abss)
79b915f26533 rationalized internals
blanchet
parents: 55864
diff changeset
   252
            fp_bs xtor_co_recs lthy
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   253
          |>> split_list;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   254
55856
bddaada24074 got rid of automatically generated fold constant and theorems (to reduce overhead)
blanchet
parents: 55803
diff changeset
   255
        val ((common_co_inducts, co_inductss, co_rec_thmss, disc_corec_thmss, sel_corec_thmsss),
bddaada24074 got rid of automatically generated fold constant and theorems (to reduce overhead)
blanchet
parents: 55803
diff changeset
   256
             fp_sugar_thms) =
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   257
          if fp = Least_FP then
55867
79b915f26533 rationalized internals
blanchet
parents: 55864
diff changeset
   258
            derive_induct_recs_thms_for_types pre_bnfs recs_args_types xtor_co_induct
79b915f26533 rationalized internals
blanchet
parents: 55864
diff changeset
   259
              xtor_co_rec_thms nesting_bnfs nested_bnfs fpTs Cs Xs ctrXs_Tsss fp_abs_inverses
55864
516ab2906e7e rationalized internals
blanchet
parents: 55863
diff changeset
   260
              fp_type_definitions abs_inverses ctrss ctr_defss co_recs co_rec_defs lthy
55856
bddaada24074 got rid of automatically generated fold constant and theorems (to reduce overhead)
blanchet
parents: 55803
diff changeset
   261
            |> `(fn ((inducts, induct, _), (rec_thmss, _)) =>
bddaada24074 got rid of automatically generated fold constant and theorems (to reduce overhead)
blanchet
parents: 55803
diff changeset
   262
              ([induct], [inducts], rec_thmss, replicate nn [], replicate nn []))
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   263
            ||> (fn info => (SOME info, NONE))
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   264
          else
55867
79b915f26533 rationalized internals
blanchet
parents: 55864
diff changeset
   265
            derive_coinduct_corecs_thms_for_types pre_bnfs (the corecs_args_types) xtor_co_induct
79b915f26533 rationalized internals
blanchet
parents: 55864
diff changeset
   266
              dtor_injects dtor_ctors xtor_co_rec_thms nesting_bnfs fpTs Cs Xs ctrXs_Tsss kss mss ns
79b915f26533 rationalized internals
blanchet
parents: 55864
diff changeset
   267
              fp_abs_inverses abs_inverses (fn thm => thm RS @{thm vimage2p_refl}) ctr_defss
55864
516ab2906e7e rationalized internals
blanchet
parents: 55863
diff changeset
   268
              ctr_sugars co_recs co_rec_defs (Proof_Context.export lthy no_defs_lthy) lthy
55856
bddaada24074 got rid of automatically generated fold constant and theorems (to reduce overhead)
blanchet
parents: 55803
diff changeset
   269
            |> `(fn ((coinduct_thms_pairs, _), (corec_thmss, _), (disc_corec_thmss, _), _,
bddaada24074 got rid of automatically generated fold constant and theorems (to reduce overhead)
blanchet
parents: 55803
diff changeset
   270
                     (sel_corec_thmsss, _)) =>
bddaada24074 got rid of automatically generated fold constant and theorems (to reduce overhead)
blanchet
parents: 55803
diff changeset
   271
              (map snd coinduct_thms_pairs, map fst coinduct_thms_pairs, corec_thmss,
bddaada24074 got rid of automatically generated fold constant and theorems (to reduce overhead)
blanchet
parents: 55803
diff changeset
   272
               disc_corec_thmss, sel_corec_thmsss))
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   273
            ||> (fn info => (NONE, SOME info));
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   274
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   275
        val phi = Proof_Context.export_morphism no_defs_lthy no_defs_lthy0;
54256
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
   276
55863
fa3a1ec69a1b rationalize internals
blanchet
parents: 55862
diff changeset
   277
        fun mk_target_fp_sugar T X kk pre_bnf absT_info ctrXs_Tss ctr_defs ctr_sugar co_rec maps
55856
bddaada24074 got rid of automatically generated fold constant and theorems (to reduce overhead)
blanchet
parents: 55803
diff changeset
   278
            co_inducts co_rec_thms disc_corec_thms sel_corec_thmss =
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   279
          {T = T, X = X, fp = fp, fp_res = fp_res, fp_res_index = kk, pre_bnf = pre_bnf,
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55772
diff changeset
   280
           absT_info = absT_info, nested_bnfs = nested_bnfs, nesting_bnfs = nesting_bnfs,
55863
fa3a1ec69a1b rationalize internals
blanchet
parents: 55862
diff changeset
   281
           ctrXs_Tss = ctrXs_Tss, ctr_defs = ctr_defs, ctr_sugar = ctr_sugar, co_rec = co_rec,
55803
74d3fe9031d8 joint work with blanchet: intermediate typedef for the input to fp-operations
traytel
parents: 55772
diff changeset
   282
           maps = maps, common_co_inducts = common_co_inducts, co_inducts = co_inducts,
55863
fa3a1ec69a1b rationalize internals
blanchet
parents: 55862
diff changeset
   283
           co_rec_thms = co_rec_thms, disc_co_recs = disc_corec_thms,
fa3a1ec69a1b rationalize internals
blanchet
parents: 55862
diff changeset
   284
           sel_co_recss = sel_corec_thmss}
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   285
          |> morph_fp_sugar phi;
54256
4843082be7ef added some N2M caching
blanchet
parents: 54255
diff changeset
   286
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55486
diff changeset
   287
        val target_fp_sugars =
55856
bddaada24074 got rid of automatically generated fold constant and theorems (to reduce overhead)
blanchet
parents: 55803
diff changeset
   288
          map14 mk_target_fp_sugar fpTs Xs kks pre_bnfs absT_infos ctrXs_Tsss ctr_defss ctr_sugars
55864
516ab2906e7e rationalized internals
blanchet
parents: 55863
diff changeset
   289
            co_recs mapss (transpose co_inductss) co_rec_thmss disc_corec_thmss sel_corec_thmsss;
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55486
diff changeset
   290
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55486
diff changeset
   291
        val n2m_sugar = (target_fp_sugars, fp_sugar_thms);
54286
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   292
      in
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   293
        (n2m_sugar, lthy |> register_n2m_sugar key n2m_sugar)
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   294
      end)
22616f65d4ea properly detect when to perform n2m -- e.g. handle the case of two independent functions on irrelevant types being defined in parallel
blanchet
parents: 54284
diff changeset
   295
  end;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   296
55567
blanchet
parents: 55539
diff changeset
   297
fun indexify_callsss ctrs callsss =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   298
  let
54283
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   299
    fun indexify_ctr ctr =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   300
      (case AList.lookup Term.aconv_untyped callsss ctr of
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   301
        NONE => replicate (num_binder_types (fastype_of ctr)) []
55343
5ebf832b58a1 expand 'split' in direct corecursion as well
blanchet
parents: 55336
diff changeset
   302
      | SOME callss => map (map (Envir.beta_eta_contract o unfold_lets_splits)) callss);
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   303
  in
54283
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   304
    map indexify_ctr ctrs
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   305
  end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   306
54283
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   307
fun retypargs tyargs (Type (s, _)) = Type (s, tyargs);
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   308
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   309
fun fold_subtype_pairs f (T as Type (s, Ts), U as Type (s', Us)) =
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   310
    f (T, U) #> (if s = s' then fold (fold_subtype_pairs f) (Ts ~~ Us) else I)
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   311
  | fold_subtype_pairs f TU = f TU;
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   312
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   313
val impossible_caller = Bound ~1;
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   314
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   315
fun nested_to_mutual_fps fp actual_bs actual_Ts actual_callers actual_callssss0 lthy =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   316
  let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   317
    val qsoty = quote o Syntax.string_of_typ lthy;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   318
    val qsotys = space_implode " or " o map qsoty;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   319
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   320
    fun not_co_datatype0 T = error (qsoty T ^ " is not a " ^ co_prefix fp ^ "datatype");
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   321
    fun not_co_datatype (T as Type (s, _)) =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   322
        if fp = Least_FP andalso
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   323
           is_some (Datatype_Data.get_info (Proof_Context.theory_of lthy) s) then
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   324
          error (qsoty T ^ " is not a new-style datatype (cf. \"datatype_new\")")
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   325
        else
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   326
          not_co_datatype0 T
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   327
      | not_co_datatype T = not_co_datatype0 T;
54303
4f55054d197c reintroduced check from bd36da55d825 after all, due to hard-to-solve issues outside the N2M code
blanchet
parents: 54302
diff changeset
   328
    fun not_mutually_nested_rec Ts1 Ts2 =
4f55054d197c reintroduced check from bd36da55d825 after all, due to hard-to-solve issues outside the N2M code
blanchet
parents: 54302
diff changeset
   329
      error (qsotys Ts1 ^ " is neither mutually recursive with " ^ qsotys Ts2 ^
55480
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55479
diff changeset
   330
        " nor nested recursive through " ^
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55479
diff changeset
   331
        (if Ts1 = Ts2 andalso can the_single Ts1 then "itself" else qsotys Ts2));
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   332
55480
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55479
diff changeset
   333
    val sorted_actual_Ts =
54266
4e0738356ac9 stronger normalization, to increase n2m cache effectiveness
blanchet
parents: 54265
diff changeset
   334
      sort (prod_ord int_ord Term_Ord.typ_ord o pairself (`Term.size_of_typ)) actual_Ts;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   335
54283
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   336
    fun the_ctrs_of (Type (s, Ts)) = map (mk_ctr Ts) (#ctrs (the (ctr_sugar_of lthy s)));
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   337
54282
blanchet
parents: 54280
diff changeset
   338
    fun the_fp_sugar_of (T as Type (T_name, _)) =
blanchet
parents: 54280
diff changeset
   339
      (case fp_sugar_of lthy T_name of
blanchet
parents: 54280
diff changeset
   340
        SOME (fp_sugar as {fp = fp', ...}) => if fp = fp' then fp_sugar else not_co_datatype T
blanchet
parents: 54280
diff changeset
   341
      | NONE => not_co_datatype T);
blanchet
parents: 54280
diff changeset
   342
54283
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   343
    fun gen_rhss_in gen_Ts rho subTs =
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   344
      let
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   345
        fun maybe_insert (T, Type (_, gen_tyargs)) =
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   346
            if member (op =) subTs T then insert (op =) gen_tyargs else I
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   347
          | maybe_insert _ = I;
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   348
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   349
        val ctrs = maps the_ctrs_of gen_Ts;
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   350
        val gen_ctr_Ts = maps (binder_types o fastype_of) ctrs;
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   351
        val ctr_Ts = map (Term.typ_subst_atomic rho) gen_ctr_Ts;
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   352
      in
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   353
        fold (fold_subtype_pairs maybe_insert) (ctr_Ts ~~ gen_ctr_Ts) []
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   354
      end;
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   355
56485
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   356
    fun gather_types _ _ rev_seens gen_seen [] = (rev rev_seens, gen_seen)
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   357
      | gather_types lthy rho rev_seens gen_seen ((T as Type (_, tyargs)) :: Ts) =
54282
blanchet
parents: 54280
diff changeset
   358
        let
54283
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   359
          val {fp_res = {Ts = mutual_Ts0, ...}, ...} = the_fp_sugar_of T;
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   360
          val mutual_Ts = map (retypargs tyargs) mutual_Ts0;
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   361
56485
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   362
          val rev_seen = flat rev_seens;
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   363
          val _ = null rev_seens orelse exists (exists_strict_subtype_in rev_seen) mutual_Ts orelse
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   364
            not_mutually_nested_rec mutual_Ts rev_seen;
54303
4f55054d197c reintroduced check from bd36da55d825 after all, due to hard-to-solve issues outside the N2M code
blanchet
parents: 54302
diff changeset
   365
54283
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   366
          fun fresh_tyargs () =
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   367
            let
55567
blanchet
parents: 55539
diff changeset
   368
              (* The name "'zy" is unlikely to clash with the context, yielding more cache hits. *)
54283
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   369
              val (gen_tyargs, lthy') =
55567
blanchet
parents: 55539
diff changeset
   370
                variant_tfrees (replicate (length tyargs) "zy") lthy
54283
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   371
                |>> map Logic.varifyT_global;
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   372
              val rho' = (gen_tyargs ~~ tyargs) @ rho;
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   373
            in
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   374
              (rho', gen_tyargs, gen_seen, lthy')
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   375
            end;
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   376
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   377
          val (rho', gen_tyargs, gen_seen', lthy') =
56485
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   378
            if exists (exists_subtype_in rev_seen) mutual_Ts then
54283
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   379
              (case gen_rhss_in gen_seen rho mutual_Ts of
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   380
                [] => fresh_tyargs ()
54899
7a01387c47d5 added tactic to prove 'disc_iff' properties in 'primcorec'
blanchet
parents: 54740
diff changeset
   381
              | gen_tyargs :: gen_tyargss_tl =>
54283
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   382
                let
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   383
                  val unify_pairs = split_list (maps (curry (op ~~) gen_tyargs) gen_tyargss_tl);
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   384
                  val mgu = Type.raw_unifys unify_pairs Vartab.empty;
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   385
                  val gen_tyargs' = map (Envir.subst_type mgu) gen_tyargs;
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   386
                  val gen_seen' = map (Envir.subst_type mgu) gen_seen;
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   387
                in
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   388
                  (rho, gen_tyargs', gen_seen', lthy)
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   389
                end)
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   390
            else
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   391
              fresh_tyargs ();
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   392
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   393
          val gen_mutual_Ts = map (retypargs gen_tyargs) mutual_Ts0;
55480
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55479
diff changeset
   394
          val other_mutual_Ts = remove1 (op =) T mutual_Ts;
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55479
diff changeset
   395
          val Ts' = fold (remove1 (op =)) other_mutual_Ts Ts;
54282
blanchet
parents: 54280
diff changeset
   396
        in
56485
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   397
          gather_types lthy' rho' (mutual_Ts :: rev_seens) (gen_seen' @ gen_mutual_Ts) Ts'
54282
blanchet
parents: 54280
diff changeset
   398
        end
56485
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   399
      | gather_types _ _ _ _ (T :: _) = not_co_datatype T;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   400
56485
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   401
    val (perm_Tss, perm_gen_Ts) = gather_types lthy [] [] [] sorted_actual_Ts;
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   402
    val (perm_cliques, perm_Ts) =
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   403
      split_list (flat (map_index (fn (i, perm_Ts) => map (pair i) perm_Ts) perm_Tss));
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   404
54283
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   405
    val perm_frozen_gen_Ts = map Logic.unvarifyT_global perm_gen_Ts;
6f0a49ed1bb1 fourth attempt at generalizing N2M types (to leverage caching)
blanchet
parents: 54282
diff changeset
   406
56484
c451cf8b29c8 thread mutual cliques through
blanchet
parents: 55966
diff changeset
   407
    val missing_Ts = fold (remove1 (op =)) actual_Ts perm_Ts;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   408
    val Ts = actual_Ts @ missing_Ts;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   409
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   410
    val nn = length Ts;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   411
    val kks = 0 upto nn - 1;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   412
54267
78e8a178b690 use right permutation in 'map2'
blanchet
parents: 54266
diff changeset
   413
    val callssss0 = pad_list [] nn actual_callssss0;
78e8a178b690 use right permutation in 'map2'
blanchet
parents: 54266
diff changeset
   414
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   415
    val common_name = mk_common_name (map Binding.name_of actual_bs);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   416
    val bs = pad_list (Binding.name common_name) nn actual_bs;
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   417
    val callers = pad_list impossible_caller nn actual_callers;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   418
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   419
    fun permute xs = permute_like (op =) Ts perm_Ts xs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   420
    fun unpermute perm_xs = permute_like (op =) perm_Ts Ts perm_xs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   421
56485
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   422
    (* The assignment of callers to cliques is incorrect in general. This code would need to inspect
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   423
       the actual calls to discover the correct cliques in the presence of type duplicates. However,
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   424
       the naive scheme implemented here supports "prim(co)rec" specifications following reasonable
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   425
       ordering of the duplicates (e.g., keeping the cliques together). *)
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   426
    val perm_bs = permute bs;
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55702
diff changeset
   427
    val perm_callers = permute callers;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   428
    val perm_kks = permute kks;
54267
78e8a178b690 use right permutation in 'map2'
blanchet
parents: 54266
diff changeset
   429
    val perm_callssss0 = permute callssss0;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   430
    val perm_fp_sugars0 = map (the o fp_sugar_of lthy o fst o dest_Type) perm_Ts;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   431
55567
blanchet
parents: 55539
diff changeset
   432
    val perm_callssss = map2 (indexify_callsss o #ctrs o #ctr_sugar) perm_fp_sugars0 perm_callssss0;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   433
53746
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53678
diff changeset
   434
    val ((perm_fp_sugars, fp_sugar_thms), lthy) =
56485
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   435
      if can the_single perm_Tss then
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   436
        ((perm_fp_sugars0, (NONE, NONE)), lthy)
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   437
      else
56484
c451cf8b29c8 thread mutual cliques through
blanchet
parents: 55966
diff changeset
   438
        mutualize_fp_sugars fp perm_cliques perm_bs perm_frozen_gen_Ts perm_callers perm_callssss
56485
008634379465 generate cliques for 'prim(co)rec' N2M
blanchet
parents: 56484
diff changeset
   439
          perm_fp_sugars0 lthy;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   440
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   441
    val fp_sugars = unpermute perm_fp_sugars;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   442
  in
53746
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53678
diff changeset
   443
    ((missing_Ts, perm_kks, fp_sugars, fp_sugar_thms), lthy)
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   444
  end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   445
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   446
end;