src/HOL/BNF/Tools/bnf_fp_rec_sugar_util.ML
author blanchet
Tue, 24 Sep 2013 20:40:36 +0200
changeset 53835 687116951569
parent 53833 ff09afd47b34
child 53864 a48d4bd3faaa
permissions -rw-r--r--
started adding support for "nat_case" as case study for all "case" constructs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
     1
(*  Title:      HOL/BNF/Tools/bnf_fp_rec_sugar_util.ML
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
     2
    Author:     Lorenz Panny, TU Muenchen
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
     3
    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
     4
    Copyright   2013
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
     5
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
     6
Library for recursor and corecursor sugar.
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
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
     9
signature BNF_FP_REC_SUGAR_UTIL =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    10
sig
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    11
  datatype rec_call =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    12
    No_Rec of int |
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    13
    Direct_Rec of int (*before*) * int (*after*) |
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    14
    Indirect_Rec of int
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
  datatype corec_call =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    17
    Dummy_No_Corec of int |
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    18
    No_Corec of int |
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    19
    Direct_Corec of int (*stop?*) * int (*end*) * int (*continue*) |
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    20
    Indirect_Corec of int
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    21
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    22
  type rec_ctr_spec =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    23
    {ctr: term,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    24
     offset: int,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    25
     calls: rec_call list,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    26
     rec_thm: thm}
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    27
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    28
  type corec_ctr_spec =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    29
    {ctr: term,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    30
     disc: term,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    31
     sels: term list,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    32
     pred: int option,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    33
     calls: corec_call list,
53705
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
    34
     discI: thm,
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
    35
     sel_thms: thm list,
53475
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
    36
     collapse: thm,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
    37
     corec_thm: thm,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
    38
     disc_corec: thm,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
    39
     sel_corecs: thm list}
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    40
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    41
  type rec_spec =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    42
    {recx: term,
53329
c31c0c311cf0 more canonical naming
blanchet
parents: 53303
diff changeset
    43
     nested_map_idents: thm list,
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    44
     nested_map_comps: thm list,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    45
     ctr_specs: rec_ctr_spec list}
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    46
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    47
  type corec_spec =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    48
    {corec: term,
53475
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
    49
     nested_maps: thm list,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
    50
     nested_map_idents: thm list,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
    51
     nested_map_comps: thm list,
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    52
     ctr_specs: corec_ctr_spec list}
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    53
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    54
  val massage_indirect_rec_call: Proof.context -> (term -> bool) -> (typ -> typ -> term -> term) ->
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    55
    typ list -> term -> term -> term -> term
53835
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
    56
  val massage_direct_corec_call: Proof.context -> (term -> bool) -> (term -> term) -> typ list ->
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
    57
    typ -> term -> term
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    58
  val massage_indirect_corec_call: Proof.context -> (term -> bool) ->
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    59
    (typ -> typ -> term -> term) -> typ list -> typ -> term -> term
53727
1d88a7ee4e3e split functionality into two functions to avoid redoing work over and over
blanchet
parents: 53726
diff changeset
    60
  val expand_corec_code_rhs: Proof.context -> (term -> bool) -> typ list -> term -> term
53835
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
    61
  val massage_corec_code_rhs: Proof.context -> (term -> term list -> term) -> typ list -> typ ->
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
    62
    term -> term
53731
aed1ee95cdfe added auxiliary function
blanchet
parents: 53729
diff changeset
    63
  val fold_rev_corec_code_rhs: (term -> term list -> 'a -> 'a) -> term -> 'a -> 'a
53729
b9d727a767ea added helper function for code equations in primcorec
blanchet
parents: 53727
diff changeset
    64
  val simplify_bool_ifs: theory -> term -> term list
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    65
  val rec_specs_of: binding list -> typ list -> typ list -> (term -> int list) ->
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    66
    ((term * term list list) list) list -> local_theory ->
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    67
    (bool * rec_spec list * typ list * thm * thm list) * local_theory
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    68
  val corec_specs_of: binding list -> typ list -> typ list -> (term -> int list) ->
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    69
    ((term * term list list) list) list -> local_theory ->
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    70
    (bool * corec_spec list * typ list * thm * thm * thm list * thm list) * local_theory
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    71
end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    72
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    73
structure BNF_FP_Rec_Sugar_Util : BNF_FP_REC_SUGAR_UTIL =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    74
struct
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    75
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    76
open BNF_Util
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    77
open BNF_Def
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    78
open BNF_Ctr_Sugar
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    79
open BNF_FP_Util
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    80
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
    81
open BNF_FP_N2M_Sugar
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    82
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    83
datatype rec_call =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    84
  No_Rec of int |
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    85
  Direct_Rec of int * int |
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    86
  Indirect_Rec of int;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    87
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    88
datatype corec_call =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    89
  Dummy_No_Corec of int |
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    90
  No_Corec of int |
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    91
  Direct_Corec of int * int * int |
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    92
  Indirect_Corec of int;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    93
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    94
type rec_ctr_spec =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    95
  {ctr: term,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    96
   offset: int,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    97
   calls: rec_call list,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    98
   rec_thm: thm};
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    99
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   100
type corec_ctr_spec =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   101
  {ctr: term,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   102
   disc: term,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   103
   sels: term list,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   104
   pred: int option,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   105
   calls: corec_call list,
53705
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   106
   discI: thm,
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   107
   sel_thms: thm list,
53475
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   108
   collapse: thm,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   109
   corec_thm: thm,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   110
   disc_corec: thm,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   111
   sel_corecs: thm list};
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   112
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   113
type rec_spec =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   114
  {recx: term,
53329
c31c0c311cf0 more canonical naming
blanchet
parents: 53303
diff changeset
   115
   nested_map_idents: thm list,
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   116
   nested_map_comps: thm list,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   117
   ctr_specs: rec_ctr_spec list};
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   118
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   119
type corec_spec =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   120
  {corec: term,
53475
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   121
   nested_maps: thm list,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   122
   nested_map_idents: thm list,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   123
   nested_map_comps: thm list,
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   124
   ctr_specs: corec_ctr_spec list};
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   125
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   126
val id_def = @{thm id_def};
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   127
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   128
exception AINT_NO_MAP of term;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   129
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   130
fun ill_formed_rec_call ctxt t =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   131
  error ("Ill-formed recursive call: " ^ quote (Syntax.string_of_term ctxt t));
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   132
fun ill_formed_corec_call ctxt t =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   133
  error ("Ill-formed corecursive call: " ^ quote (Syntax.string_of_term ctxt t));
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   134
fun invalid_map ctxt t =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   135
  error ("Invalid map function in " ^ quote (Syntax.string_of_term ctxt t));
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   136
fun unexpected_rec_call ctxt t =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   137
  error ("Unexpected recursive call: " ^ quote (Syntax.string_of_term ctxt t));
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   138
fun unexpected_corec_call ctxt t =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   139
  error ("Unexpected corecursive call: " ^ quote (Syntax.string_of_term ctxt t));
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   140
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   141
fun factor_out_types ctxt massage destU U T =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   142
  (case try destU U of
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   143
    SOME (U1, U2) => if U1 = T then massage T U2 else invalid_map ctxt
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   144
  | NONE => invalid_map ctxt);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   145
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   146
fun map_flattened_map_args ctxt s map_args fs =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   147
  let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   148
    val flat_fs = flatten_type_args_of_bnf (the (bnf_of ctxt s)) Term.dummy fs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   149
    val flat_fs' = map_args flat_fs;
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
    permute_like (op aconv) flat_fs fs flat_fs'
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   152
  end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   153
53734
7613573f023a avoid infinite loop for unapplied terms + tuning
blanchet
parents: 53732
diff changeset
   154
fun massage_indirect_rec_call ctxt has_call raw_massage_fun bound_Ts y y' =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   155
  let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   156
    val typof = curry fastype_of1 bound_Ts;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   157
    val build_map_fst = build_map ctxt (fst_const o fst);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   158
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   159
    val yT = typof y;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   160
    val yU = typof y';
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   161
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   162
    fun y_of_y' () = build_map_fst (yU, yT) $ y';
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   163
    val elim_y = Term.map_aterms (fn t => if t = y then y_of_y' () else t);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   164
53734
7613573f023a avoid infinite loop for unapplied terms + tuning
blanchet
parents: 53732
diff changeset
   165
    fun massage_direct_fun U T t =
7613573f023a avoid infinite loop for unapplied terms + tuning
blanchet
parents: 53732
diff changeset
   166
      if has_call t then factor_out_types ctxt raw_massage_fun HOLogic.dest_prodT U T t
7613573f023a avoid infinite loop for unapplied terms + tuning
blanchet
parents: 53732
diff changeset
   167
      else HOLogic.mk_comp (t, build_map_fst (U, T));
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   168
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   169
    fun massage_map (Type (_, Us)) (Type (s, Ts)) t =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   170
        (case try (dest_map ctxt s) t of
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   171
          SOME (map0, fs) =>
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   172
          let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   173
            val Type (_, ran_Ts) = range_type (typof t);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   174
            val map' = mk_map (length fs) Us ran_Ts map0;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   175
            val fs' = map_flattened_map_args ctxt s (map3 massage_map_or_map_arg Us Ts) fs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   176
          in
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   177
            list_comb (map', fs')
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   178
          end
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   179
        | NONE => raise AINT_NO_MAP t)
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   180
      | massage_map _ _ t = raise AINT_NO_MAP t
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   181
    and massage_map_or_map_arg U T t =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   182
      if T = U then
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   183
        if has_call t then unexpected_rec_call ctxt t else t
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   184
      else
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   185
        massage_map U T t
53734
7613573f023a avoid infinite loop for unapplied terms + tuning
blanchet
parents: 53732
diff changeset
   186
        handle AINT_NO_MAP _ => massage_direct_fun U T t;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   187
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   188
    fun massage_call (t as t1 $ t2) =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   189
        if t2 = y then
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   190
          massage_map yU yT (elim_y t1) $ y'
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   191
          handle AINT_NO_MAP t' => invalid_map ctxt t'
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   192
        else
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   193
          ill_formed_rec_call ctxt t
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   194
      | massage_call t = if t = y then y_of_y' () else ill_formed_rec_call ctxt t;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   195
  in
53724
cfcb987d4700 no need for beta-eta contraction
blanchet
parents: 53723
diff changeset
   196
    massage_call
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   197
  end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   198
53835
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   199
fun massage_let_if ctxt has_call massage_leaf bound_Ts U =
53723
73d63e2616aa generalize helper function
blanchet
parents: 53705
diff changeset
   200
  let
53835
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   201
    val typof = curry fastype_of1 bound_Ts;
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   202
    val check_obj = ((not o has_call) orf unexpected_corec_call ctxt);
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   203
53725
9e64151359e8 simplified code
blanchet
parents: 53724
diff changeset
   204
    fun massage_rec t =
53723
73d63e2616aa generalize helper function
blanchet
parents: 53705
diff changeset
   205
      (case Term.strip_comb t of
53725
9e64151359e8 simplified code
blanchet
parents: 53724
diff changeset
   206
        (Const (@{const_name Let}, _), [arg1, arg2]) => massage_rec (betapply (arg2, arg1))
53835
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   207
      | (Const (@{const_name If}, _), obj :: branches) =>
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   208
        list_comb (If_const U $ tap check_obj obj, map massage_rec branches)
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   209
      | (Const (@{const_name nat_case}, _), args) =>
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   210
        (* Proof of concept -- should be extensible to all case-like constructs *)
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   211
        let
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   212
          val (branches, obj) = split_last args;
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   213
          val branches' = map massage_rec branches
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   214
          (* FIXME: bound_Ts *)
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   215
          val casex' = Const (@{const_name nat_case}, map typof branches' ---> typof obj);
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   216
        in
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   217
          list_comb (casex', branches') $ tap check_obj obj
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   218
        end
53725
9e64151359e8 simplified code
blanchet
parents: 53724
diff changeset
   219
      | _ => massage_leaf t)
53723
73d63e2616aa generalize helper function
blanchet
parents: 53705
diff changeset
   220
  in
73d63e2616aa generalize helper function
blanchet
parents: 53705
diff changeset
   221
    massage_rec
73d63e2616aa generalize helper function
blanchet
parents: 53705
diff changeset
   222
  end;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   223
53832
blanchet
parents: 53746
diff changeset
   224
fun fold_rev_let_if f =
53731
aed1ee95cdfe added auxiliary function
blanchet
parents: 53729
diff changeset
   225
  let
aed1ee95cdfe added auxiliary function
blanchet
parents: 53729
diff changeset
   226
    fun fld t =
aed1ee95cdfe added auxiliary function
blanchet
parents: 53729
diff changeset
   227
      (case Term.strip_comb t of
aed1ee95cdfe added auxiliary function
blanchet
parents: 53729
diff changeset
   228
        (Const (@{const_name Let}, _), [arg1, arg2]) => fld (betapply (arg2, arg1))
53835
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   229
      | (Const (@{const_name If}, _), _ :: branches) => fold_rev fld branches
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   230
      | (Const (@{const_name nat_case}, _), args) => fold_rev fld (fst (split_last args))
53731
aed1ee95cdfe added auxiliary function
blanchet
parents: 53729
diff changeset
   231
      | _ => f t)
aed1ee95cdfe added auxiliary function
blanchet
parents: 53729
diff changeset
   232
  in
aed1ee95cdfe added auxiliary function
blanchet
parents: 53729
diff changeset
   233
    fld
aed1ee95cdfe added auxiliary function
blanchet
parents: 53729
diff changeset
   234
  end;
aed1ee95cdfe added auxiliary function
blanchet
parents: 53729
diff changeset
   235
53833
ff09afd47b34 made SML/NJ happy
blanchet
parents: 53832
diff changeset
   236
val massage_direct_corec_call = massage_let_if;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   237
53734
7613573f023a avoid infinite loop for unapplied terms + tuning
blanchet
parents: 53732
diff changeset
   238
fun massage_indirect_corec_call ctxt has_call raw_massage_call bound_Ts U t =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   239
  let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   240
    val typof = curry fastype_of1 bound_Ts;
53411
ab4edf89992f support indirect corecursion
panny
parents: 53329
diff changeset
   241
    val build_map_Inl = build_map ctxt (uncurry Inl_const o dest_sumT o snd)
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   242
53734
7613573f023a avoid infinite loop for unapplied terms + tuning
blanchet
parents: 53732
diff changeset
   243
    fun massage_direct_call U T t =
7613573f023a avoid infinite loop for unapplied terms + tuning
blanchet
parents: 53732
diff changeset
   244
      if has_call t then factor_out_types ctxt raw_massage_call dest_sumT U T t
53411
ab4edf89992f support indirect corecursion
panny
parents: 53329
diff changeset
   245
      else build_map_Inl (T, U) $ t;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   246
53734
7613573f023a avoid infinite loop for unapplied terms + tuning
blanchet
parents: 53732
diff changeset
   247
    fun massage_direct_fun U T t =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   248
      let val var = Var ((Name.uu, Term.maxidx_of_term t + 1), domain_type (typof t)) in
53734
7613573f023a avoid infinite loop for unapplied terms + tuning
blanchet
parents: 53732
diff changeset
   249
        Term.lambda var (massage_direct_call U T (t $ var))
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   250
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   251
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   252
    fun massage_map (Type (_, Us)) (Type (s, Ts)) t =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   253
        (case try (dest_map ctxt s) t of
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   254
          SOME (map0, fs) =>
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   255
          let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   256
            val Type (_, dom_Ts) = domain_type (typof t);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   257
            val map' = mk_map (length fs) dom_Ts Us map0;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   258
            val fs' = map_flattened_map_args ctxt s (map3 massage_map_or_map_arg Us Ts) fs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   259
          in
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   260
            list_comb (map', fs')
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   261
          end
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   262
        | NONE => raise AINT_NO_MAP t)
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   263
      | massage_map _ _ t = raise AINT_NO_MAP t
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   264
    and massage_map_or_map_arg U T t =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   265
      if T = U then
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   266
        if has_call t then unexpected_corec_call ctxt t else t
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   267
      else
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   268
        massage_map U T t
53734
7613573f023a avoid infinite loop for unapplied terms + tuning
blanchet
parents: 53732
diff changeset
   269
        handle AINT_NO_MAP _ => massage_direct_fun U T t;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   270
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   271
    fun massage_call U T =
53832
blanchet
parents: 53746
diff changeset
   272
      massage_let_if ctxt has_call (fn t =>
53732
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   273
        if has_call t then
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   274
          (case U of
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   275
            Type (s, Us) =>
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   276
            (case try (dest_ctr ctxt s) t of
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   277
              SOME (f, args) =>
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   278
              let val f' = mk_ctr Us f in
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   279
                list_comb (f', map3 massage_call (binder_types (typof f')) (map typof args) args)
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   280
              end
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   281
            | NONE =>
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   282
              (case t of
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   283
                t1 $ t2 =>
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   284
                (if has_call t2 then
53734
7613573f023a avoid infinite loop for unapplied terms + tuning
blanchet
parents: 53732
diff changeset
   285
                  massage_direct_call U T t
53732
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   286
                else
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   287
                  massage_map U T t1 $ t2
53734
7613573f023a avoid infinite loop for unapplied terms + tuning
blanchet
parents: 53732
diff changeset
   288
                  handle AINT_NO_MAP _ => massage_direct_call U T t)
53732
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   289
              | Abs (s, T', t') => Abs (s, T', massage_call (range_type U) (range_type T) t')
53734
7613573f023a avoid infinite loop for unapplied terms + tuning
blanchet
parents: 53732
diff changeset
   290
              | _ => massage_direct_call U T t))
53732
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   291
          | _ => ill_formed_corec_call ctxt t)
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   292
        else
53835
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   293
          build_map_Inl (T, U) $ t) bound_Ts U
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   294
  in
53725
9e64151359e8 simplified code
blanchet
parents: 53724
diff changeset
   295
    massage_call U (typof t) t
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   296
  end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   297
53727
1d88a7ee4e3e split functionality into two functions to avoid redoing work over and over
blanchet
parents: 53726
diff changeset
   298
fun expand_ctr_term ctxt s Ts t =
53726
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   299
  (case fp_sugar_of ctxt s of
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   300
    SOME fp_sugar =>
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   301
    let
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   302
      val T = Type (s, Ts);
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   303
      val x = Bound 0;
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   304
      val {ctrs = ctrs0, discs = discs0, selss = selss0, ...} = of_fp_sugar #ctr_sugars fp_sugar;
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   305
      val ctrs = map (mk_ctr Ts) ctrs0;
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   306
      val discs = map (mk_disc_or_sel Ts) discs0;
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   307
      val selss = map (map (mk_disc_or_sel Ts)) selss0;
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   308
      val xdiscs = map (rapp x) discs;
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   309
      val xselss = map (map (rapp x)) selss;
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   310
      val xsel_ctrs = map2 (curry Term.list_comb) ctrs xselss;
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   311
      val xif = mk_IfN T xdiscs xsel_ctrs;
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   312
    in
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   313
      Const (@{const_name Let}, T --> (T --> T) --> T) $ t $ Abs (Name.uu, T, xif)
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   314
    end
53727
1d88a7ee4e3e split functionality into two functions to avoid redoing work over and over
blanchet
parents: 53726
diff changeset
   315
  | NONE => raise Fail "expand_ctr_term");
53726
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   316
53727
1d88a7ee4e3e split functionality into two functions to avoid redoing work over and over
blanchet
parents: 53726
diff changeset
   317
fun expand_corec_code_rhs ctxt has_call bound_Ts t =
1d88a7ee4e3e split functionality into two functions to avoid redoing work over and over
blanchet
parents: 53726
diff changeset
   318
  (case fastype_of1 (bound_Ts, t) of
1d88a7ee4e3e split functionality into two functions to avoid redoing work over and over
blanchet
parents: 53726
diff changeset
   319
    T as Type (s, Ts) =>
53832
blanchet
parents: 53746
diff changeset
   320
    massage_let_if ctxt has_call (fn t =>
53727
1d88a7ee4e3e split functionality into two functions to avoid redoing work over and over
blanchet
parents: 53726
diff changeset
   321
      if can (dest_ctr ctxt s) t then t
53835
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   322
      else massage_let_if ctxt has_call I bound_Ts T (expand_ctr_term ctxt s Ts t)) bound_Ts T t
53727
1d88a7ee4e3e split functionality into two functions to avoid redoing work over and over
blanchet
parents: 53726
diff changeset
   323
  | _ => raise Fail "expand_corec_code_rhs");
1d88a7ee4e3e split functionality into two functions to avoid redoing work over and over
blanchet
parents: 53726
diff changeset
   324
1d88a7ee4e3e split functionality into two functions to avoid redoing work over and over
blanchet
parents: 53726
diff changeset
   325
fun massage_corec_code_rhs ctxt massage_ctr =
53832
blanchet
parents: 53746
diff changeset
   326
  massage_let_if ctxt (K false) (uncurry massage_ctr o Term.strip_comb);
53726
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   327
53832
blanchet
parents: 53746
diff changeset
   328
fun fold_rev_corec_code_rhs f = fold_rev_let_if (uncurry f o Term.strip_comb);
53731
aed1ee95cdfe added auxiliary function
blanchet
parents: 53729
diff changeset
   329
53729
b9d727a767ea added helper function for code equations in primcorec
blanchet
parents: 53727
diff changeset
   330
fun add_conjuncts (Const (@{const_name conj}, _) $ t $ t') = add_conjuncts t o add_conjuncts t'
b9d727a767ea added helper function for code equations in primcorec
blanchet
parents: 53727
diff changeset
   331
  | add_conjuncts t = cons t;
b9d727a767ea added helper function for code equations in primcorec
blanchet
parents: 53727
diff changeset
   332
b9d727a767ea added helper function for code equations in primcorec
blanchet
parents: 53727
diff changeset
   333
fun conjuncts t = add_conjuncts t [];
b9d727a767ea added helper function for code equations in primcorec
blanchet
parents: 53727
diff changeset
   334
b9d727a767ea added helper function for code equations in primcorec
blanchet
parents: 53727
diff changeset
   335
fun simplify_bool_ifs thy =
b9d727a767ea added helper function for code equations in primcorec
blanchet
parents: 53727
diff changeset
   336
  Raw_Simplifier.rewrite_term thy @{thms bool_if_simps[THEN eq_reflection]} []
b9d727a767ea added helper function for code equations in primcorec
blanchet
parents: 53727
diff changeset
   337
  #> conjuncts #> (fn [@{term True}] => [] | ts => ts);
b9d727a767ea added helper function for code equations in primcorec
blanchet
parents: 53727
diff changeset
   338
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   339
fun indexed xs h = let val h' = h + length xs in (h upto h' - 1, h') end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   340
fun indexedd xss = fold_map indexed xss;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   341
fun indexeddd xsss = fold_map indexedd xsss;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   342
fun indexedddd xssss = fold_map indexeddd xssss;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   343
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   344
fun find_index_eq hs h = find_index (curry (op =) h) hs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   345
53476
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   346
(*FIXME: remove special cases for products and sum once they are registered as datatypes*)
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   347
fun map_thms_of_typ ctxt (Type (s, _)) =
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   348
    if s = @{type_name prod} then
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   349
      @{thms map_pair_simp}
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   350
    else if s = @{type_name sum} then
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   351
      @{thms sum_map.simps}
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   352
    else
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   353
      (case fp_sugar_of ctxt s of
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   354
        SOME {index, mapss, ...} => nth mapss index
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   355
      | NONE => [])
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   356
  | map_thms_of_typ _ _ = [];
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   357
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   358
val lose_co_rec = false (*FIXME: try true?*);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   359
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   360
fun rec_specs_of bs arg_Ts res_Ts get_indices callssss0 lthy =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   361
  let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   362
    val thy = Proof_Context.theory_of lthy;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   363
53746
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53741
diff changeset
   364
    val ((missing_arg_Ts, perm0_kks,
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   365
          fp_sugars as {nested_bnfs, fp_res = {xtor_co_iterss = ctor_iters1 :: _, ...},
53746
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53741
diff changeset
   366
            co_inducts = [induct_thm], ...} :: _, (lfp_sugar_thms, _)), lthy') =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   367
      nested_to_mutual_fps lose_co_rec Least_FP bs arg_Ts get_indices callssss0 lthy;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   368
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   369
    val perm_fp_sugars = sort (int_ord o pairself #index) fp_sugars;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   370
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   371
    val indices = map #index fp_sugars;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   372
    val perm_indices = map #index perm_fp_sugars;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   373
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   374
    val perm_ctrss = map (#ctrs o of_fp_sugar #ctr_sugars) perm_fp_sugars;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   375
    val perm_ctr_Tsss = map (map (binder_types o fastype_of)) perm_ctrss;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   376
    val perm_fpTs = map (body_type o fastype_of o hd) perm_ctrss;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   377
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   378
    val nn0 = length arg_Ts;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   379
    val nn = length perm_fpTs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   380
    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
   381
    val perm_ns = map length perm_ctr_Tsss;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   382
    val perm_mss = map (map length) perm_ctr_Tsss;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   383
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   384
    val perm_Cs = map (body_type o fastype_of o co_rec_of o of_fp_sugar (#xtor_co_iterss o #fp_res))
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   385
      perm_fp_sugars;
53592
5a7bf8c859f6 made non-co case more robust as well (cf. b6e2993fd0d3)
blanchet
parents: 53591
diff changeset
   386
    val perm_fun_arg_Tssss =
5a7bf8c859f6 made non-co case more robust as well (cf. b6e2993fd0d3)
blanchet
parents: 53591
diff changeset
   387
      mk_iter_fun_arg_types perm_ctr_Tsss perm_ns perm_mss (co_rec_of ctor_iters1);
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   388
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   389
    fun unpermute0 perm0_xs = permute_like (op =) perm0_kks kks perm0_xs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   390
    fun unpermute perm_xs = permute_like (op =) perm_indices indices perm_xs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   391
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   392
    val induct_thms = unpermute0 (conj_dests nn induct_thm);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   393
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   394
    val fpTs = unpermute perm_fpTs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   395
    val Cs = unpermute perm_Cs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   396
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   397
    val As_rho = tvar_subst thy (take nn0 fpTs) arg_Ts;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   398
    val Cs_rho = map (fst o dest_TVar) Cs ~~ pad_list HOLogic.unitT nn res_Ts;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   399
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   400
    val substA = Term.subst_TVars As_rho;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   401
    val substAT = Term.typ_subst_TVars As_rho;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   402
    val substCT = Term.typ_subst_TVars Cs_rho;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   403
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   404
    val perm_Cs' = map substCT perm_Cs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   405
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   406
    fun offset_of_ctr 0 _ = 0
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   407
      | offset_of_ctr n ({ctrs, ...} :: ctr_sugars) =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   408
        length ctrs + offset_of_ctr (n - 1) ctr_sugars;
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
    fun call_of [i] [T] = (if exists_subtype_in Cs T then Indirect_Rec else No_Rec) i
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   411
      | call_of [i, i'] _ = Direct_Rec (i, i');
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   412
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   413
    fun mk_ctr_spec ctr offset fun_arg_Tss rec_thm =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   414
      let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   415
        val (fun_arg_hss, _) = indexedd fun_arg_Tss 0;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   416
        val fun_arg_hs = flat_rec_arg_args fun_arg_hss;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   417
        val fun_arg_iss = map (map (find_index_eq fun_arg_hs)) fun_arg_hss;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   418
      in
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   419
        {ctr = substA ctr, offset = offset, calls = map2 call_of fun_arg_iss fun_arg_Tss,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   420
         rec_thm = rec_thm}
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   421
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   422
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   423
    fun mk_ctr_specs index ctr_sugars iter_thmsss =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   424
      let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   425
        val ctrs = #ctrs (nth ctr_sugars index);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   426
        val rec_thmss = co_rec_of (nth iter_thmsss index);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   427
        val k = offset_of_ctr index ctr_sugars;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   428
        val n = length ctrs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   429
      in
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   430
        map4 mk_ctr_spec ctrs (k upto k + n - 1) (nth perm_fun_arg_Tssss index) rec_thmss
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   431
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   432
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   433
    fun mk_spec {T, index, ctr_sugars, co_iterss = iterss, co_iter_thmsss = iter_thmsss, ...} =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   434
      {recx = mk_co_iter thy Least_FP (substAT T) perm_Cs' (co_rec_of (nth iterss index)),
53329
c31c0c311cf0 more canonical naming
blanchet
parents: 53303
diff changeset
   435
       nested_map_idents = map (unfold_thms lthy [id_def] o map_id0_of_bnf) nested_bnfs,
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   436
       nested_map_comps = map map_comp_of_bnf nested_bnfs,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   437
       ctr_specs = mk_ctr_specs index ctr_sugars iter_thmsss};
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   438
  in
53746
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53741
diff changeset
   439
    ((is_some lfp_sugar_thms, map mk_spec fp_sugars, missing_arg_Ts, induct_thm, induct_thms),
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53741
diff changeset
   440
     lthy')
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   441
  end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   442
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   443
fun corec_specs_of bs arg_Ts res_Ts get_indices callssss0 lthy =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   444
  let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   445
    val thy = Proof_Context.theory_of lthy;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   446
53746
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53741
diff changeset
   447
    val ((missing_res_Ts, perm0_kks,
53475
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   448
          fp_sugars as {nested_bnfs, fp_res = {xtor_co_iterss = dtor_coiters1 :: _, ...},
53746
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53741
diff changeset
   449
            co_inducts = coinduct_thms, ...} :: _, (_, gfp_sugar_thms)), lthy') =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   450
      nested_to_mutual_fps lose_co_rec Greatest_FP bs res_Ts get_indices callssss0 lthy;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   451
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   452
    val perm_fp_sugars = sort (int_ord o pairself #index) fp_sugars;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   453
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   454
    val indices = map #index fp_sugars;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   455
    val perm_indices = map #index perm_fp_sugars;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   456
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   457
    val perm_ctrss = map (#ctrs o of_fp_sugar #ctr_sugars) perm_fp_sugars;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   458
    val perm_ctr_Tsss = map (map (binder_types o fastype_of)) perm_ctrss;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   459
    val perm_fpTs = map (body_type o fastype_of o hd) perm_ctrss;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   460
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   461
    val nn0 = length res_Ts;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   462
    val nn = length perm_fpTs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   463
    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
   464
    val perm_ns = map length perm_ctr_Tsss;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   465
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   466
    val perm_Cs = map (domain_type o body_fun_type o fastype_of o co_rec_of o
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   467
      of_fp_sugar (#xtor_co_iterss o #fp_res)) perm_fp_sugars;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   468
    val (perm_p_Tss, (perm_q_Tssss, _, perm_f_Tssss, _)) =
53591
b6e2993fd0d3 don't wrongly destroy sum types in coiterators
blanchet
parents: 53590
diff changeset
   469
      mk_coiter_fun_arg_types perm_ctr_Tsss perm_Cs perm_ns (co_rec_of dtor_coiters1);
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   470
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   471
    val (perm_p_hss, h) = indexedd perm_p_Tss 0;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   472
    val (perm_q_hssss, h') = indexedddd perm_q_Tssss h;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   473
    val (perm_f_hssss, _) = indexedddd perm_f_Tssss h';
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   474
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   475
    val fun_arg_hs =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   476
      flat (map3 flat_corec_preds_predsss_gettersss perm_p_hss perm_q_hssss perm_f_hssss);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   477
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   478
    fun unpermute0 perm0_xs = permute_like (op =) perm0_kks kks perm0_xs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   479
    fun unpermute perm_xs = permute_like (op =) perm_indices indices perm_xs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   480
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   481
    val coinduct_thmss = map (unpermute0 o conj_dests nn) coinduct_thms;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   482
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   483
    val p_iss = map (map (find_index_eq fun_arg_hs)) (unpermute perm_p_hss);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   484
    val q_issss = map (map (map (map (find_index_eq fun_arg_hs)))) (unpermute perm_q_hssss);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   485
    val f_issss = map (map (map (map (find_index_eq fun_arg_hs)))) (unpermute perm_f_hssss);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   486
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   487
    val f_Tssss = unpermute perm_f_Tssss;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   488
    val fpTs = unpermute perm_fpTs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   489
    val Cs = unpermute perm_Cs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   490
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   491
    val As_rho = tvar_subst thy (take nn0 fpTs) res_Ts;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   492
    val Cs_rho = map (fst o dest_TVar) Cs ~~ pad_list HOLogic.unitT nn arg_Ts;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   493
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   494
    val substA = Term.subst_TVars As_rho;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   495
    val substAT = Term.typ_subst_TVars As_rho;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   496
    val substCT = Term.typ_subst_TVars Cs_rho;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   497
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   498
    val perm_Cs' = map substCT perm_Cs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   499
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   500
    fun call_of nullary [] [g_i] [Type (@{type_name fun}, [_, T])] =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   501
        (if exists_subtype_in Cs T then Indirect_Corec
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   502
         else if nullary then Dummy_No_Corec
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   503
         else No_Corec) g_i
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   504
      | call_of _ [q_i] [g_i, g_i'] _ = Direct_Corec (q_i, g_i, g_i');
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   505
53705
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   506
    fun mk_ctr_spec ctr disc sels p_ho q_iss f_iss f_Tss discI sel_thms collapse corec_thm
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   507
        disc_corec sel_corecs =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   508
      let val nullary = not (can dest_funT (fastype_of ctr)) in
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   509
        {ctr = substA ctr, disc = substA disc, sels = map substA sels, pred = p_ho,
53705
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   510
         calls = map3 (call_of nullary) q_iss f_iss f_Tss, discI = discI, sel_thms = sel_thms,
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   511
         collapse = collapse, corec_thm = corec_thm, disc_corec = disc_corec,
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   512
         sel_corecs = sel_corecs}
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   513
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   514
53475
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   515
    fun mk_ctr_specs index ctr_sugars p_is q_isss f_isss f_Tsss coiter_thmsss disc_coitersss
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   516
        sel_coiterssss =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   517
      let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   518
        val ctrs = #ctrs (nth ctr_sugars index);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   519
        val discs = #discs (nth ctr_sugars index);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   520
        val selss = #selss (nth ctr_sugars index);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   521
        val p_ios = map SOME p_is @ [NONE];
53705
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   522
        val discIs = #discIs (nth ctr_sugars index);
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   523
        val sel_thmss = #sel_thmss (nth ctr_sugars index);
53475
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   524
        val collapses = #collapses (nth ctr_sugars index);
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   525
        val corec_thms = co_rec_of (nth coiter_thmsss index);
53741
c9068aade859 killed exceptional code that is anyway no longer needed, now that the 'simp' attribute has been taken away -- this solves issues in 'primcorec'
blanchet
parents: 53734
diff changeset
   526
        val disc_corecs = co_rec_of (nth disc_coitersss index);
53475
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   527
        val sel_corecss = co_rec_of (nth sel_coiterssss index);
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   528
      in
53705
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   529
        map13 mk_ctr_spec ctrs discs selss p_ios q_isss f_isss f_Tsss discIs sel_thmss collapses
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   530
          corec_thms disc_corecs sel_corecss
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   531
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   532
53475
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   533
    fun mk_spec {T, index, ctr_sugars, co_iterss = coiterss, co_iter_thmsss = coiter_thmsss,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   534
          disc_co_itersss = disc_coitersss, sel_co_iterssss = sel_coiterssss, ...}
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   535
        p_is q_isss f_isss f_Tsss =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   536
      {corec = mk_co_iter thy Greatest_FP (substAT T) perm_Cs' (co_rec_of (nth coiterss index)),
53476
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   537
       nested_maps = maps (map_thms_of_typ lthy o T_of_bnf) nested_bnfs,
53475
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   538
       nested_map_idents = map (unfold_thms lthy [id_def] o map_id0_of_bnf) nested_bnfs,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   539
       nested_map_comps = map map_comp_of_bnf nested_bnfs,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   540
       ctr_specs = mk_ctr_specs index ctr_sugars p_is q_isss f_isss f_Tsss coiter_thmsss
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   541
         disc_coitersss sel_coiterssss};
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   542
  in
53746
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53741
diff changeset
   543
    ((is_some gfp_sugar_thms, map5 mk_spec fp_sugars p_iss q_issss f_issss f_Tssss, missing_res_Ts,
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   544
      co_induct_of coinduct_thms, strong_co_induct_of coinduct_thms, co_induct_of coinduct_thmss,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   545
      strong_co_induct_of coinduct_thmss), lthy')
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   546
  end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   547
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   548
end;