src/HOL/BNF/Tools/bnf_fp_rec_sugar_util.ML
author blanchet
Thu, 24 Oct 2013 18:37:54 +0200
changeset 54200 064f88a41096
parent 54199 20a52b55f8ea
child 54202 0a06b51ffa56
permissions -rw-r--r--
more correct (!) types for recursive calls
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 =
54200
064f88a41096 more correct (!) types for recursive calls
blanchet
parents: 54199
diff changeset
    12
    No_Rec of int * typ |
064f88a41096 more correct (!) types for recursive calls
blanchet
parents: 54199
diff changeset
    13
    Mutual_Rec of (int * typ) (*before*) * (int * typ) (*after*) |
064f88a41096 more correct (!) types for recursive calls
blanchet
parents: 54199
diff changeset
    14
    Nested_Rec of int * typ
53303
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 |
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
    19
    Mutual_Corec of int (*stop?*) * int (*end*) * int (*continue*) |
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
    20
    Nested_Corec of int
53303
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
54134
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
    28
  type basic_corec_ctr_spec =
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
    29
    {ctr: term,
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
    30
     disc: term,
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
    31
     sels: term list}
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
    32
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    33
  type corec_ctr_spec =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    34
    {ctr: term,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    35
     disc: term,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    36
     sels: term list,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    37
     pred: int option,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    38
     calls: corec_call list,
53705
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
    39
     discI: thm,
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
    40
     sel_thms: thm list,
53475
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
    41
     collapse: thm,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
    42
     corec_thm: thm,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
    43
     disc_corec: thm,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
    44
     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
    45
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    46
  type rec_spec =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    47
    {recx: term,
53329
c31c0c311cf0 more canonical naming
blanchet
parents: 53303
diff changeset
    48
     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
    49
     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
    50
     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
    51
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    52
  type corec_spec =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    53
    {corec: term,
53475
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
    54
     nested_maps: thm list,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
    55
     nested_map_idents: thm list,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
    56
     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
    57
     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
    58
54067
7be49e2bfccc rationalized negation code
blanchet
parents: 54016
diff changeset
    59
  val s_not: term -> term
7be49e2bfccc rationalized negation code
blanchet
parents: 54016
diff changeset
    60
  val s_not_conj: term list -> term list
54068
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
    61
  val s_conjs: term list -> term
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
    62
  val s_disjs: term list -> term
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
    63
  val s_dnf: term list list -> term list
53878
eb3075935edf move useful functions to library
blanchet
parents: 53871
diff changeset
    64
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
    65
  val massage_nested_rec_call: Proof.context -> (term -> bool) -> (typ -> typ -> term -> term) ->
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    66
    typ list -> term -> term -> term -> term
54074
43cdae9524bf allow 'let's around constructors in constructor view
blanchet
parents: 54069
diff changeset
    67
  val unfold_let: term -> term
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
    68
  val massage_mutual_corec_call: Proof.context -> (term -> bool) -> (typ list -> term -> term) ->
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
    69
    typ list -> term -> term
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
    70
  val massage_nested_corec_call: Proof.context -> (term -> bool) ->
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
    71
    (typ list -> typ -> typ -> term -> term) -> typ list -> typ -> term -> term
54188
5288fa24c9db made lower-level function available
blanchet
parents: 54170
diff changeset
    72
  val fold_rev_corec_call:  Proof.context -> (term list -> term -> 'a -> 'a) -> typ list -> term ->
5288fa24c9db made lower-level function available
blanchet
parents: 54170
diff changeset
    73
    'a -> string list * 'a
53727
1d88a7ee4e3e split functionality into two functions to avoid redoing work over and over
blanchet
parents: 53726
diff changeset
    74
  val expand_corec_code_rhs: Proof.context -> (term -> bool) -> typ list -> term -> term
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
    75
  val massage_corec_code_rhs: Proof.context -> (typ list -> term -> term list -> term) ->
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
    76
    typ list -> term -> term
53871
a1a52423601f more powerful fold
blanchet
parents: 53870
diff changeset
    77
  val fold_rev_corec_code_rhs: Proof.context -> (term list -> term -> term list -> 'a -> 'a) ->
a1a52423601f more powerful fold
blanchet
parents: 53870
diff changeset
    78
    typ list -> term -> 'a -> 'a
53909
7c10e75e62b3 use needed case theorems
blanchet
parents: 53899
diff changeset
    79
  val case_thms_of_term: Proof.context -> typ list -> term ->
53925
blanchet
parents: 53924
diff changeset
    80
    thm list * thm list * thm list * thm list
53878
eb3075935edf move useful functions to library
blanchet
parents: 53871
diff changeset
    81
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    82
  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
    83
    ((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
    84
    (bool * rec_spec list * typ list * thm * thm list) * local_theory
54134
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
    85
  val basic_corec_specs_of: Proof.context -> typ -> basic_corec_ctr_spec list
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    86
  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
    87
    ((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
    88
    (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
    89
end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    90
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    91
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
    92
struct
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    93
54006
9fe1bd54d437 renamed theory file
blanchet
parents: 53974
diff changeset
    94
open Ctr_Sugar
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    95
open BNF_Util
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    96
open BNF_Def
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    97
open BNF_FP_Util
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
    98
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
    99
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
   100
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   101
datatype rec_call =
54200
064f88a41096 more correct (!) types for recursive calls
blanchet
parents: 54199
diff changeset
   102
  No_Rec of int * typ |
064f88a41096 more correct (!) types for recursive calls
blanchet
parents: 54199
diff changeset
   103
  Mutual_Rec of (int * typ) * (int * typ) |
064f88a41096 more correct (!) types for recursive calls
blanchet
parents: 54199
diff changeset
   104
  Nested_Rec of int * typ;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   105
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   106
datatype corec_call =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   107
  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
   108
  No_Corec of int |
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
   109
  Mutual_Corec of int * int * int |
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
   110
  Nested_Corec of int;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   111
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   112
type rec_ctr_spec =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   113
  {ctr: term,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   114
   offset: int,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   115
   calls: rec_call list,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   116
   rec_thm: thm};
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   117
54134
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   118
type basic_corec_ctr_spec =
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   119
  {ctr: term,
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   120
   disc: term,
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   121
   sels: term list};
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   122
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   123
type corec_ctr_spec =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   124
  {ctr: term,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   125
   disc: term,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   126
   sels: term list,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   127
   pred: int option,
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   128
   calls: corec_call list,
53705
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   129
   discI: thm,
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   130
   sel_thms: thm list,
53475
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   131
   collapse: thm,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   132
   corec_thm: thm,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   133
   disc_corec: thm,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   134
   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
   135
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   136
type rec_spec =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   137
  {recx: term,
53329
c31c0c311cf0 more canonical naming
blanchet
parents: 53303
diff changeset
   138
   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
   139
   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
   140
   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
   141
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   142
type corec_spec =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   143
  {corec: term,
53475
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   144
   nested_maps: thm list,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   145
   nested_map_idents: thm list,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   146
   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
   147
   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
   148
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   149
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
   150
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   151
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
   152
54134
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   153
fun not_codatatype ctxt T =
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   154
  error ("Not a codatatype: " ^ Syntax.string_of_typ ctxt T);
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   155
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
   156
  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
   157
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
   158
  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
   159
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
   160
  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
   161
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
   162
  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
   163
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
   164
  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
   165
53878
eb3075935edf move useful functions to library
blanchet
parents: 53871
diff changeset
   166
val mk_conjs = try (foldr1 HOLogic.mk_conj) #> the_default @{const True};
eb3075935edf move useful functions to library
blanchet
parents: 53871
diff changeset
   167
val mk_disjs = try (foldr1 HOLogic.mk_disj) #> the_default @{const False};
eb3075935edf move useful functions to library
blanchet
parents: 53871
diff changeset
   168
54067
7be49e2bfccc rationalized negation code
blanchet
parents: 54016
diff changeset
   169
val conjuncts_s = filter_out (curry (op =) @{const True}) o HOLogic.conjuncts;
53878
eb3075935edf move useful functions to library
blanchet
parents: 53871
diff changeset
   170
54067
7be49e2bfccc rationalized negation code
blanchet
parents: 54016
diff changeset
   171
fun s_not @{const True} = @{const False}
7be49e2bfccc rationalized negation code
blanchet
parents: 54016
diff changeset
   172
  | s_not @{const False} = @{const True}
7be49e2bfccc rationalized negation code
blanchet
parents: 54016
diff changeset
   173
  | s_not (@{const Not} $ t) = t
7be49e2bfccc rationalized negation code
blanchet
parents: 54016
diff changeset
   174
  | s_not (@{const conj} $ t $ u) = @{const disj} $ s_not t $ s_not u
7be49e2bfccc rationalized negation code
blanchet
parents: 54016
diff changeset
   175
  | s_not (@{const disj} $ t $ u) = @{const conj} $ s_not t $ s_not u
7be49e2bfccc rationalized negation code
blanchet
parents: 54016
diff changeset
   176
  | s_not t = @{const Not} $ t;
53878
eb3075935edf move useful functions to library
blanchet
parents: 53871
diff changeset
   177
54067
7be49e2bfccc rationalized negation code
blanchet
parents: 54016
diff changeset
   178
val s_not_conj = conjuncts_s o s_not o mk_conjs;
53878
eb3075935edf move useful functions to library
blanchet
parents: 53871
diff changeset
   179
54068
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   180
fun s_conj c @{const True} = c
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   181
  | s_conj c d = HOLogic.mk_conj (c, d);
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   182
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   183
fun propagate_unit_pos u cs = if member (op aconv) cs u then [@{const False}] else cs;
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   184
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   185
fun propagate_unit_neg not_u cs = remove (op aconv) not_u cs;
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   186
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   187
fun propagate_units css =
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   188
  (case List.partition (can the_single) css of
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   189
     ([], _) => css
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   190
   | ([u] :: uss, css') =>
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   191
     [u] :: propagate_units (map (propagate_unit_neg (s_not u))
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   192
       (map (propagate_unit_pos u) (uss @ css'))));
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   193
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   194
fun s_conjs cs =
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   195
  if member (op aconv) cs @{const False} then @{const False}
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   196
  else mk_conjs (remove (op aconv) @{const True} cs);
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   197
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   198
fun s_disjs ds =
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   199
  if member (op aconv) ds @{const True} then @{const True}
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   200
  else mk_disjs (remove (op aconv) @{const False} ds);
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   201
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   202
fun s_dnf css0 =
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   203
  let val css = propagate_units css0 in
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   204
    if null css then
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   205
      [@{const False}]
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   206
    else if exists null css then
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   207
      []
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   208
    else
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   209
      map (fn c :: cs => (c, cs)) css
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   210
      |> AList.coalesce (op =)
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   211
      |> map (fn (c, css) => c :: s_dnf css)
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   212
      |> (fn [cs] => cs | css => [s_disjs (map s_conjs css)])
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   213
  end;
447354985f6a generate optimized DNF formula
blanchet
parents: 54067
diff changeset
   214
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   215
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
   216
  (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
   217
    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
   218
  | NONE => invalid_map ctxt);
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   219
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   220
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
   221
  let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   222
    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
   223
    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
   224
  in
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   225
    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
   226
  end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   227
54162
faa9c35fe79b handle composition for multiple arguments correctly
blanchet
parents: 54159
diff changeset
   228
fun mk_partial_comp gT fT g =
faa9c35fe79b handle composition for multiple arguments correctly
blanchet
parents: 54159
diff changeset
   229
  let val T = domain_type fT --> range_type gT in
faa9c35fe79b handle composition for multiple arguments correctly
blanchet
parents: 54159
diff changeset
   230
    Const (@{const_name Fun.comp}, gT --> fT --> T) $ g
faa9c35fe79b handle composition for multiple arguments correctly
blanchet
parents: 54159
diff changeset
   231
  end;
faa9c35fe79b handle composition for multiple arguments correctly
blanchet
parents: 54159
diff changeset
   232
faa9c35fe79b handle composition for multiple arguments correctly
blanchet
parents: 54159
diff changeset
   233
fun mk_compN' 0 _ _ g = g
faa9c35fe79b handle composition for multiple arguments correctly
blanchet
parents: 54159
diff changeset
   234
  | mk_compN' n gT fT g =
faa9c35fe79b handle composition for multiple arguments correctly
blanchet
parents: 54159
diff changeset
   235
    let val g' = mk_compN' (n - 1) gT (range_type fT) g in
faa9c35fe79b handle composition for multiple arguments correctly
blanchet
parents: 54159
diff changeset
   236
      mk_partial_comp (fastype_of g') fT g'
faa9c35fe79b handle composition for multiple arguments correctly
blanchet
parents: 54159
diff changeset
   237
    end;
faa9c35fe79b handle composition for multiple arguments correctly
blanchet
parents: 54159
diff changeset
   238
faa9c35fe79b handle composition for multiple arguments correctly
blanchet
parents: 54159
diff changeset
   239
fun mk_compN bound_Ts n (g, f) =
faa9c35fe79b handle composition for multiple arguments correctly
blanchet
parents: 54159
diff changeset
   240
  let val typof = curry fastype_of1 bound_Ts in
faa9c35fe79b handle composition for multiple arguments correctly
blanchet
parents: 54159
diff changeset
   241
    mk_compN' n (typof g) (typof f) g $ f
faa9c35fe79b handle composition for multiple arguments correctly
blanchet
parents: 54159
diff changeset
   242
  end;
faa9c35fe79b handle composition for multiple arguments correctly
blanchet
parents: 54159
diff changeset
   243
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
   244
fun massage_nested_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
   245
  let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   246
    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
   247
    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
   248
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   249
    val yT = typof y;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   250
    val yU = typof y';
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 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
   253
    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
   254
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
   255
    fun massage_mutual_fun U T t =
53734
7613573f023a avoid infinite loop for unapplied terms + tuning
blanchet
parents: 53732
diff changeset
   256
      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
   257
      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
   258
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   259
    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
   260
        (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
   261
          SOME (map0, fs) =>
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   262
          let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   263
            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
   264
            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
   265
            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
   266
          in
53870
5d45882b4f36 properly fold over branches
blanchet
parents: 53868
diff changeset
   267
            Term.list_comb (map', fs')
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   268
          end
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   269
        | 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
   270
      | 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
   271
    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
   272
      if T = U then
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   273
        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
   274
      else
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   275
        massage_map U T t
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
   276
        handle AINT_NO_MAP _ => massage_mutual_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
   277
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   278
    fun massage_call (t as t1 $ t2) =
54170
402fcacb5c88 gracefully handle case where a nested recursive variable appears directly on the rhs of a primcorec equation
blanchet
parents: 54162
diff changeset
   279
        if has_call t then
402fcacb5c88 gracefully handle case where a nested recursive variable appears directly on the rhs of a primcorec equation
blanchet
parents: 54162
diff changeset
   280
          if t2 = y then
402fcacb5c88 gracefully handle case where a nested recursive variable appears directly on the rhs of a primcorec equation
blanchet
parents: 54162
diff changeset
   281
            massage_map yU yT (elim_y t1) $ y'
402fcacb5c88 gracefully handle case where a nested recursive variable appears directly on the rhs of a primcorec equation
blanchet
parents: 54162
diff changeset
   282
            handle AINT_NO_MAP t' => invalid_map ctxt t'
402fcacb5c88 gracefully handle case where a nested recursive variable appears directly on the rhs of a primcorec equation
blanchet
parents: 54162
diff changeset
   283
          else
402fcacb5c88 gracefully handle case where a nested recursive variable appears directly on the rhs of a primcorec equation
blanchet
parents: 54162
diff changeset
   284
            let val (g, xs) = Term.strip_comb t2 in
402fcacb5c88 gracefully handle case where a nested recursive variable appears directly on the rhs of a primcorec equation
blanchet
parents: 54162
diff changeset
   285
              if g = y then
402fcacb5c88 gracefully handle case where a nested recursive variable appears directly on the rhs of a primcorec equation
blanchet
parents: 54162
diff changeset
   286
                if exists has_call xs then unexpected_rec_call ctxt t2
402fcacb5c88 gracefully handle case where a nested recursive variable appears directly on the rhs of a primcorec equation
blanchet
parents: 54162
diff changeset
   287
                else Term.list_comb (massage_call (mk_compN bound_Ts (length xs) (t1, y)), xs)
402fcacb5c88 gracefully handle case where a nested recursive variable appears directly on the rhs of a primcorec equation
blanchet
parents: 54162
diff changeset
   288
              else
402fcacb5c88 gracefully handle case where a nested recursive variable appears directly on the rhs of a primcorec equation
blanchet
parents: 54162
diff changeset
   289
                ill_formed_rec_call ctxt t
402fcacb5c88 gracefully handle case where a nested recursive variable appears directly on the rhs of a primcorec equation
blanchet
parents: 54162
diff changeset
   290
            end
54162
faa9c35fe79b handle composition for multiple arguments correctly
blanchet
parents: 54159
diff changeset
   291
        else
54170
402fcacb5c88 gracefully handle case where a nested recursive variable appears directly on the rhs of a primcorec equation
blanchet
parents: 54162
diff changeset
   292
          elim_y t
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   293
      | 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
   294
  in
53724
cfcb987d4700 no need for beta-eta contraction
blanchet
parents: 53723
diff changeset
   295
    massage_call
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
54135
0d5ed72c4c60 also unfold let (_, _) = ... syntax
blanchet
parents: 54134
diff changeset
   298
fun unfold_let (Const (@{const_name Let}, _) $ arg1 $ arg2) = unfold_let (betapply (arg2, arg1))
54137
e475d86ab2ca handle nested tuples in 'let's
blanchet
parents: 54135
diff changeset
   299
  | unfold_let (Const (@{const_name prod_case}, _) $ t) =
e475d86ab2ca handle nested tuples in 'let's
blanchet
parents: 54135
diff changeset
   300
    (case unfold_let t of
e475d86ab2ca handle nested tuples in 'let's
blanchet
parents: 54135
diff changeset
   301
      t' as Abs (s1, T1, Abs (s2, T2, _)) =>
e475d86ab2ca handle nested tuples in 'let's
blanchet
parents: 54135
diff changeset
   302
      let
e475d86ab2ca handle nested tuples in 'let's
blanchet
parents: 54135
diff changeset
   303
        val x = (s1 ^ s2, Term.maxidx_of_term t + 1);
e475d86ab2ca handle nested tuples in 'let's
blanchet
parents: 54135
diff changeset
   304
        val v = Var (x, HOLogic.mk_prodT (T1, T2));
e475d86ab2ca handle nested tuples in 'let's
blanchet
parents: 54135
diff changeset
   305
      in
e475d86ab2ca handle nested tuples in 'let's
blanchet
parents: 54135
diff changeset
   306
        lambda v (unfold_let (betapplys (t', [HOLogic.mk_fst v, HOLogic.mk_snd v])))
e475d86ab2ca handle nested tuples in 'let's
blanchet
parents: 54135
diff changeset
   307
      end
e475d86ab2ca handle nested tuples in 'let's
blanchet
parents: 54135
diff changeset
   308
    | _ => t)
e475d86ab2ca handle nested tuples in 'let's
blanchet
parents: 54135
diff changeset
   309
  | unfold_let (t $ u) = betapply (unfold_let t, u)
54135
0d5ed72c4c60 also unfold let (_, _) = ... syntax
blanchet
parents: 54134
diff changeset
   310
  | unfold_let t = t;
0d5ed72c4c60 also unfold let (_, _) = ... syntax
blanchet
parents: 54134
diff changeset
   311
53909
7c10e75e62b3 use needed case theorems
blanchet
parents: 53899
diff changeset
   312
fun fold_rev_let_if_case ctxt f bound_Ts t =
53866
7c23df53af01 generalized case-handling code a bit
blanchet
parents: 53865
diff changeset
   313
  let
53870
5d45882b4f36 properly fold over branches
blanchet
parents: 53868
diff changeset
   314
    val thy = Proof_Context.theory_of ctxt;
5d45882b4f36 properly fold over branches
blanchet
parents: 53868
diff changeset
   315
53871
a1a52423601f more powerful fold
blanchet
parents: 53870
diff changeset
   316
    fun fld conds t =
53866
7c23df53af01 generalized case-handling code a bit
blanchet
parents: 53865
diff changeset
   317
      (case Term.strip_comb t of
54135
0d5ed72c4c60 also unfold let (_, _) = ... syntax
blanchet
parents: 54134
diff changeset
   318
        (Const (@{const_name Let}, _), [_, _]) => fld conds (unfold_let t)
53871
a1a52423601f more powerful fold
blanchet
parents: 53870
diff changeset
   319
      | (Const (@{const_name If}, _), [cond, then_branch, else_branch]) =>
54199
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   320
        fld (conds @ conjuncts_s cond) then_branch o fld (conds @ s_not_conj [cond]) else_branch
53896
e5dedcbd823b don't generate wrong type
blanchet
parents: 53895
diff changeset
   321
      | (Const (c, _), args as _ :: _ :: _) =>
e5dedcbd823b don't generate wrong type
blanchet
parents: 53895
diff changeset
   322
        let val n = num_binder_types (Sign.the_const_type thy c) - 1 in
53924
b19d300db73b avoid calls to nth with ~1
blanchet
parents: 53918
diff changeset
   323
          if n >= 0 andalso n < length args then
53896
e5dedcbd823b don't generate wrong type
blanchet
parents: 53895
diff changeset
   324
            (case fastype_of1 (bound_Ts, nth args n) of
e5dedcbd823b don't generate wrong type
blanchet
parents: 53895
diff changeset
   325
              Type (s, Ts) =>
e5dedcbd823b don't generate wrong type
blanchet
parents: 53895
diff changeset
   326
              (case dest_case ctxt s Ts t of
53909
7c10e75e62b3 use needed case theorems
blanchet
parents: 53899
diff changeset
   327
                NONE => apsnd (f conds t)
53896
e5dedcbd823b don't generate wrong type
blanchet
parents: 53895
diff changeset
   328
              | SOME (conds', branches) =>
53909
7c10e75e62b3 use needed case theorems
blanchet
parents: 53899
diff changeset
   329
                apfst (cons s) o fold_rev (uncurry fld)
54067
7be49e2bfccc rationalized negation code
blanchet
parents: 54016
diff changeset
   330
                  (map (append conds o conjuncts_s) conds' ~~ branches))
53909
7c10e75e62b3 use needed case theorems
blanchet
parents: 53899
diff changeset
   331
            | _ => apsnd (f conds t))
53896
e5dedcbd823b don't generate wrong type
blanchet
parents: 53895
diff changeset
   332
          else
53909
7c10e75e62b3 use needed case theorems
blanchet
parents: 53899
diff changeset
   333
            apsnd (f conds t)
53866
7c23df53af01 generalized case-handling code a bit
blanchet
parents: 53865
diff changeset
   334
        end
53909
7c10e75e62b3 use needed case theorems
blanchet
parents: 53899
diff changeset
   335
      | _ => apsnd (f conds t))
53866
7c23df53af01 generalized case-handling code a bit
blanchet
parents: 53865
diff changeset
   336
  in
53909
7c10e75e62b3 use needed case theorems
blanchet
parents: 53899
diff changeset
   337
    fld [] t o pair []
53866
7c23df53af01 generalized case-handling code a bit
blanchet
parents: 53865
diff changeset
   338
  end;
7c23df53af01 generalized case-handling code a bit
blanchet
parents: 53865
diff changeset
   339
53870
5d45882b4f36 properly fold over branches
blanchet
parents: 53868
diff changeset
   340
fun case_of ctxt = ctr_sugar_of ctxt #> Option.map (fst o dest_Const o #casex);
5d45882b4f36 properly fold over branches
blanchet
parents: 53868
diff changeset
   341
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   342
fun massage_let_if_case ctxt has_call massage_leaf =
53723
73d63e2616aa generalize helper function
blanchet
parents: 53705
diff changeset
   343
  let
53888
7031775668e8 improved massaging of case expressions
blanchet
parents: 53887
diff changeset
   344
    val thy = Proof_Context.theory_of ctxt;
7031775668e8 improved massaging of case expressions
blanchet
parents: 53887
diff changeset
   345
7031775668e8 improved massaging of case expressions
blanchet
parents: 53887
diff changeset
   346
    fun check_no_call t = if has_call t then unexpected_corec_call ctxt t else ();
53835
687116951569 started adding support for "nat_case" as case study for all "case" constructs
blanchet
parents: 53833
diff changeset
   347
54105
865b05fcc8b8 tuning (simplified parts of 92c5bd3b342d)
blanchet
parents: 54104
diff changeset
   348
    fun massage_abs bound_Ts 0 t = massage_rec bound_Ts t
865b05fcc8b8 tuning (simplified parts of 92c5bd3b342d)
blanchet
parents: 54104
diff changeset
   349
      | massage_abs bound_Ts m (Abs (s, T, t)) = Abs (s, T, massage_abs (T :: bound_Ts) (m - 1) t)
865b05fcc8b8 tuning (simplified parts of 92c5bd3b342d)
blanchet
parents: 54104
diff changeset
   350
      | massage_abs bound_Ts m t =
865b05fcc8b8 tuning (simplified parts of 92c5bd3b342d)
blanchet
parents: 54104
diff changeset
   351
        let val T = domain_type (fastype_of1 (bound_Ts, t)) in
865b05fcc8b8 tuning (simplified parts of 92c5bd3b342d)
blanchet
parents: 54104
diff changeset
   352
          Abs (Name.uu, T, massage_abs (T :: bound_Ts) (m - 1) (incr_boundvars 1 t $ Bound 0))
865b05fcc8b8 tuning (simplified parts of 92c5bd3b342d)
blanchet
parents: 54104
diff changeset
   353
        end
53893
865da57dee93 further improved 'code' helper functions
blanchet
parents: 53892
diff changeset
   354
    and massage_rec bound_Ts t =
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   355
      let val typof = curry fastype_of1 bound_Ts in
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   356
        (case Term.strip_comb t of
54135
0d5ed72c4c60 also unfold let (_, _) = ... syntax
blanchet
parents: 54134
diff changeset
   357
          (Const (@{const_name Let}, _), [_, _]) => massage_rec bound_Ts (unfold_let t)
53893
865da57dee93 further improved 'code' helper functions
blanchet
parents: 53892
diff changeset
   358
        | (Const (@{const_name If}, _), obj :: (branches as [_, _])) =>
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   359
          let val branches' = map (massage_rec bound_Ts) branches in
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   360
            Term.list_comb (If_const (typof (hd branches')) $ tap check_no_call obj, branches')
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   361
          end
54105
865b05fcc8b8 tuning (simplified parts of 92c5bd3b342d)
blanchet
parents: 54104
diff changeset
   362
        | (Const (c, _), args as _ :: _ :: _) =>
54199
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   363
          (case try strip_fun_type (Sign.the_const_type thy c) of
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   364
            SOME (gen_branch_Ts, gen_body_fun_T) =>
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   365
            let
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   366
              val gen_branch_ms = map num_binder_types gen_branch_Ts;
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   367
              val n = length gen_branch_ms;
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   368
            in
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   369
              if n < length args then
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   370
                (case gen_body_fun_T of
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   371
                  Type (_, [Type (T_name, _), _]) =>
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   372
                  if case_of ctxt T_name = SOME c then
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   373
                    let
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   374
                      val (branches, obj_leftovers) = chop n args;
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   375
                      val branches' = map2 (massage_abs bound_Ts) gen_branch_ms branches;
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   376
                      val branch_Ts' = map typof branches';
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   377
                      val body_T' = snd (strip_typeN (hd gen_branch_ms) (hd branch_Ts'));
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   378
                      val casex' = Const (c, branch_Ts' ---> map typof obj_leftovers ---> body_T');
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   379
                    in
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   380
                      Term.list_comb (casex', branches' @ tap (List.app check_no_call) obj_leftovers)
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   381
                    end
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   382
                  else
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   383
                    massage_leaf bound_Ts t
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   384
                | _ => massage_leaf bound_Ts t)
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   385
              else
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   386
                massage_leaf bound_Ts t
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   387
            end
20a52b55f8ea watch out for 'case' constant canditates whose general type is 'a (e.g. 'undefined')
blanchet
parents: 54188
diff changeset
   388
          | NONE => massage_leaf bound_Ts t)
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   389
        | _ => massage_leaf bound_Ts t)
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   390
      end
53723
73d63e2616aa generalize helper function
blanchet
parents: 53705
diff changeset
   391
  in
73d63e2616aa generalize helper function
blanchet
parents: 53705
diff changeset
   392
    massage_rec
73d63e2616aa generalize helper function
blanchet
parents: 53705
diff changeset
   393
  end;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   394
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
   395
val massage_mutual_corec_call = massage_let_if_case;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   396
54016
769fcbdf2918 allow uncurried lambda-abstractions on rhs of "primcorec"
blanchet
parents: 54009
diff changeset
   397
fun curried_type (Type (@{type_name fun}, [Type (@{type_name prod}, Ts), T])) = Ts ---> T;
769fcbdf2918 allow uncurried lambda-abstractions on rhs of "primcorec"
blanchet
parents: 54009
diff changeset
   398
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
   399
fun massage_nested_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
   400
  let
53411
ab4edf89992f support indirect corecursion
panny
parents: 53329
diff changeset
   401
    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
   402
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
   403
    fun massage_mutual_call bound_Ts U T t =
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   404
      if has_call t then factor_out_types ctxt (raw_massage_call bound_Ts) dest_sumT U T t
53411
ab4edf89992f support indirect corecursion
panny
parents: 53329
diff changeset
   405
      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
   406
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
   407
    fun massage_mutual_fun bound_Ts U T t =
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   408
      let
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   409
        val var = Var ((Name.uu, Term.maxidx_of_term t + 1),
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   410
          domain_type (fastype_of1 (bound_Ts, t)));
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   411
      in
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
   412
        Term.lambda var (massage_mutual_call bound_Ts 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
   413
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   414
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   415
    fun massage_map bound_Ts (Type (_, Us)) (Type (s, Ts)) t =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   416
        (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
   417
          SOME (map0, fs) =>
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   418
          let
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   419
            val Type (_, dom_Ts) = domain_type (fastype_of1 (bound_Ts, t));
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   420
            val map' = mk_map (length fs) dom_Ts Us map0;
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   421
            val fs' =
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   422
              map_flattened_map_args ctxt s (map3 (massage_map_or_map_arg bound_Ts) Us Ts) fs;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   423
          in
53870
5d45882b4f36 properly fold over branches
blanchet
parents: 53868
diff changeset
   424
            Term.list_comb (map', fs')
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   425
          end
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   426
        | NONE => raise AINT_NO_MAP t)
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   427
      | massage_map _ _ _ t = raise AINT_NO_MAP t
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   428
    and massage_map_or_map_arg bound_Ts U T t =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   429
      if T = U then
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   430
        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
   431
      else
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   432
        massage_map bound_Ts U T t
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
   433
        handle AINT_NO_MAP _ => massage_mutual_fun bound_Ts U T t;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   434
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   435
    fun massage_call bound_Ts U T =
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   436
      massage_let_if_case ctxt has_call (fn bound_Ts => fn t =>
53732
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   437
        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
   438
          (case U of
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   439
            Type (s, Us) =>
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   440
            (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
   441
              SOME (f, args) =>
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   442
              let
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   443
                val typof = curry fastype_of1 bound_Ts;
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   444
                val f' = mk_ctr Us f
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   445
                val f'_T = typof f';
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   446
                val arg_Ts = map typof args;
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   447
              in
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   448
                Term.list_comb (f', map3 (massage_call bound_Ts) (binder_types f'_T) arg_Ts args)
53732
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   449
              end
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   450
            | NONE =>
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   451
              (case t of
54016
769fcbdf2918 allow uncurried lambda-abstractions on rhs of "primcorec"
blanchet
parents: 54009
diff changeset
   452
                Const (@{const_name prod_case}, _) $ t' =>
769fcbdf2918 allow uncurried lambda-abstractions on rhs of "primcorec"
blanchet
parents: 54009
diff changeset
   453
                let
769fcbdf2918 allow uncurried lambda-abstractions on rhs of "primcorec"
blanchet
parents: 54009
diff changeset
   454
                  val U' = curried_type U;
769fcbdf2918 allow uncurried lambda-abstractions on rhs of "primcorec"
blanchet
parents: 54009
diff changeset
   455
                  val T' = curried_type T;
769fcbdf2918 allow uncurried lambda-abstractions on rhs of "primcorec"
blanchet
parents: 54009
diff changeset
   456
                in
769fcbdf2918 allow uncurried lambda-abstractions on rhs of "primcorec"
blanchet
parents: 54009
diff changeset
   457
                  Const (@{const_name prod_case}, U' --> U) $ massage_call bound_Ts U' T' t'
769fcbdf2918 allow uncurried lambda-abstractions on rhs of "primcorec"
blanchet
parents: 54009
diff changeset
   458
                end
769fcbdf2918 allow uncurried lambda-abstractions on rhs of "primcorec"
blanchet
parents: 54009
diff changeset
   459
              | t1 $ t2 =>
53732
e2c0d0426d2b give lambda abstractions a chance, as an alternative to function composition, for corecursion via "fun"
blanchet
parents: 53731
diff changeset
   460
                (if has_call t2 then
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
   461
                  massage_mutual_call bound_Ts 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
   462
                else
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   463
                  massage_map bound_Ts U T t1 $ t2
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
   464
                  handle AINT_NO_MAP _ => massage_mutual_call bound_Ts U T t)
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   465
              | Abs (s, T', t') =>
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   466
                Abs (s, T', massage_call (T' :: bound_Ts) (range_type U) (range_type T) t')
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
   467
              | _ => massage_mutual_call bound_Ts 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
   468
          | _ => 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
   469
        else
53889
d1bd94eb5d0e killed redundant argument
blanchet
parents: 53888
diff changeset
   470
          build_map_Inl (T, U) $ t) bound_Ts;
53960
949cef2095e2 faster exit in common case
blanchet
parents: 53925
diff changeset
   471
949cef2095e2 faster exit in common case
blanchet
parents: 53925
diff changeset
   472
    val T = fastype_of1 (bound_Ts, t);
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   473
  in
53960
949cef2095e2 faster exit in common case
blanchet
parents: 53925
diff changeset
   474
    if has_call t then massage_call bound_Ts U T t 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
   475
  end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   476
54188
5288fa24c9db made lower-level function available
blanchet
parents: 54170
diff changeset
   477
val fold_rev_corec_call = fold_rev_let_if_case;
5288fa24c9db made lower-level function available
blanchet
parents: 54170
diff changeset
   478
54159
eb5d58c99049 set stage for more flexible 'primrec' syntax for recursion through functions
blanchet
parents: 54137
diff changeset
   479
fun expand_to_ctr_term ctxt s Ts t =
53867
8ad44ecc0d15 keep a database of free constructor type information
blanchet
parents: 53866
diff changeset
   480
  (case ctr_sugar_of ctxt s of
53870
5d45882b4f36 properly fold over branches
blanchet
parents: 53868
diff changeset
   481
    SOME {ctrs, casex, ...} =>
5d45882b4f36 properly fold over branches
blanchet
parents: 53868
diff changeset
   482
    Term.list_comb (mk_case Ts (Type (s, Ts)) casex, map (mk_ctr Ts) ctrs) $ t
54159
eb5d58c99049 set stage for more flexible 'primrec' syntax for recursion through functions
blanchet
parents: 54137
diff changeset
   483
  | NONE => raise Fail "expand_to_ctr_term");
53726
d41a30db83d9 added massaging function for primcorec code equations
blanchet
parents: 53725
diff changeset
   484
53727
1d88a7ee4e3e split functionality into two functions to avoid redoing work over and over
blanchet
parents: 53726
diff changeset
   485
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
   486
  (case fastype_of1 (bound_Ts, t) of
53889
d1bd94eb5d0e killed redundant argument
blanchet
parents: 53888
diff changeset
   487
    Type (s, Ts) =>
53893
865da57dee93 further improved 'code' helper functions
blanchet
parents: 53892
diff changeset
   488
    massage_let_if_case ctxt has_call (fn _ => fn t =>
54159
eb5d58c99049 set stage for more flexible 'primrec' syntax for recursion through functions
blanchet
parents: 54137
diff changeset
   489
      if can (dest_ctr ctxt s) t then t else expand_to_ctr_term ctxt s Ts t) bound_Ts t
53727
1d88a7ee4e3e split functionality into two functions to avoid redoing work over and over
blanchet
parents: 53726
diff changeset
   490
  | _ => raise Fail "expand_corec_code_rhs");
1d88a7ee4e3e split functionality into two functions to avoid redoing work over and over
blanchet
parents: 53726
diff changeset
   491
1d88a7ee4e3e split functionality into two functions to avoid redoing work over and over
blanchet
parents: 53726
diff changeset
   492
fun massage_corec_code_rhs ctxt massage_ctr =
53890
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   493
  massage_let_if_case ctxt (K false)
5f647a5bd46e thread through bound types
blanchet
parents: 53889
diff changeset
   494
    (fn bound_Ts => uncurry (massage_ctr bound_Ts) o Term.strip_comb);
53865
cadccda5be03 support cases for new-style (co)datatypes
blanchet
parents: 53864
diff changeset
   495
53871
a1a52423601f more powerful fold
blanchet
parents: 53870
diff changeset
   496
fun fold_rev_corec_code_rhs ctxt f =
53909
7c10e75e62b3 use needed case theorems
blanchet
parents: 53899
diff changeset
   497
  snd ooo fold_rev_let_if_case ctxt (fn conds => uncurry (f conds) o Term.strip_comb);
7c10e75e62b3 use needed case theorems
blanchet
parents: 53899
diff changeset
   498
7c10e75e62b3 use needed case theorems
blanchet
parents: 53899
diff changeset
   499
fun case_thms_of_term ctxt bound_Ts t =
7c10e75e62b3 use needed case theorems
blanchet
parents: 53899
diff changeset
   500
  let
7c10e75e62b3 use needed case theorems
blanchet
parents: 53899
diff changeset
   501
    val (caseT_names, _) = fold_rev_let_if_case ctxt (K (K I)) bound_Ts t ();
7c10e75e62b3 use needed case theorems
blanchet
parents: 53899
diff changeset
   502
    val ctr_sugars = map (the o ctr_sugar_of ctxt) caseT_names;
7c10e75e62b3 use needed case theorems
blanchet
parents: 53899
diff changeset
   503
  in
53925
blanchet
parents: 53924
diff changeset
   504
    (maps #distincts ctr_sugars, maps #discIs ctr_sugars, maps #sel_splits ctr_sugars,
blanchet
parents: 53924
diff changeset
   505
     maps #sel_split_asms ctr_sugars)
53909
7c10e75e62b3 use needed case theorems
blanchet
parents: 53899
diff changeset
   506
  end;
53731
aed1ee95cdfe added auxiliary function
blanchet
parents: 53729
diff changeset
   507
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   508
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
   509
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
   510
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
   511
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
   512
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   513
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
   514
53476
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   515
(*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
   516
fun map_thms_of_typ ctxt (Type (s, _)) =
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   517
    if s = @{type_name prod} then
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   518
      @{thms map_pair_simp}
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   519
    else if s = @{type_name sum} then
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   520
      @{thms sum_map.simps}
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   521
    else
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   522
      (case fp_sugar_of ctxt s of
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   523
        SOME {index, mapss, ...} => nth mapss index
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   524
      | NONE => [])
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   525
  | map_thms_of_typ _ _ = [];
eb3865c3ee58 include map theorems in datastructure for "primcorec"
blanchet
parents: 53475
diff changeset
   526
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   527
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
   528
  let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   529
    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
   530
53746
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53741
diff changeset
   531
    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
   532
          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
   533
            co_inducts = [induct_thm], ...} :: _, (lfp_sugar_thms, _)), lthy') =
54009
f138452e8265 got rid of dead feature
blanchet
parents: 54006
diff changeset
   534
      nested_to_mutual_fps Least_FP bs arg_Ts get_indices callssss0 lthy;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   535
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   536
    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
   537
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   538
    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
   539
    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
   540
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   541
    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
   542
    val perm_ctr_Tsss = map (map (binder_types o fastype_of)) perm_ctrss;
54134
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   543
    val perm_lfpTs = map (body_type o fastype_of o hd) perm_ctrss;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   544
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   545
    val nn0 = length arg_Ts;
54134
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   546
    val nn = length perm_lfpTs;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   547
    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
   548
    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
   549
    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
   550
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   551
    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
   552
      perm_fp_sugars;
53592
5a7bf8c859f6 made non-co case more robust as well (cf. b6e2993fd0d3)
blanchet
parents: 53591
diff changeset
   553
    val perm_fun_arg_Tssss =
5a7bf8c859f6 made non-co case more robust as well (cf. b6e2993fd0d3)
blanchet
parents: 53591
diff changeset
   554
      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
   555
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   556
    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
   557
    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
   558
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   559
    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
   560
54134
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   561
    val lfpTs = unpermute perm_lfpTs;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   562
    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
   563
54134
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   564
    val As_rho = tvar_subst thy (take nn0 lfpTs) arg_Ts;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   565
    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
   566
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   567
    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
   568
    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
   569
    val substCT = Term.typ_subst_TVars Cs_rho;
54200
064f88a41096 more correct (!) types for recursive calls
blanchet
parents: 54199
diff changeset
   570
    val substACT = substAT o substCT;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   571
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   572
    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
   573
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   574
    fun offset_of_ctr 0 _ = 0
53974
612505263257 make SML/NJ more happy;
wenzelm
parents: 53960
diff changeset
   575
      | offset_of_ctr n (({ctrs, ...} : ctr_sugar) :: ctr_sugars) =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   576
        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
   577
54200
064f88a41096 more correct (!) types for recursive calls
blanchet
parents: 54199
diff changeset
   578
    fun call_of [i] [T] = (if exists_subtype_in Cs T then Nested_Rec else No_Rec) (i, substACT T)
064f88a41096 more correct (!) types for recursive calls
blanchet
parents: 54199
diff changeset
   579
      | call_of [i, i'] [T, T'] = Mutual_Rec ((i, substACT T), (i', substACT T'));
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   580
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   581
    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
   582
      let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   583
        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
   584
        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
   585
        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
   586
      in
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   587
        {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
   588
         rec_thm = rec_thm}
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   589
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   590
53974
612505263257 make SML/NJ more happy;
wenzelm
parents: 53960
diff changeset
   591
    fun mk_ctr_specs index (ctr_sugars : ctr_sugar list) iter_thmsss =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   592
      let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   593
        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
   594
        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
   595
        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
   596
        val n = length ctrs;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   597
      in
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   598
        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
   599
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   600
53974
612505263257 make SML/NJ more happy;
wenzelm
parents: 53960
diff changeset
   601
    fun mk_spec ({T, index, ctr_sugars, co_iterss = iterss, co_iter_thmsss = iter_thmsss, ...}
612505263257 make SML/NJ more happy;
wenzelm
parents: 53960
diff changeset
   602
      : fp_sugar) =
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   603
      {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
   604
       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
   605
       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
   606
       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
   607
  in
53746
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53741
diff changeset
   608
    ((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
   609
     lthy')
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   610
  end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   611
54134
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   612
fun basic_corec_specs_of ctxt res_T =
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   613
  (case res_T of
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   614
    Type (T_name, _) =>
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   615
    (case Ctr_Sugar.ctr_sugar_of ctxt T_name of
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   616
      NONE => not_codatatype ctxt res_T
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   617
    | SOME {ctrs, discs, selss, ...} =>
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   618
      let
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   619
        val thy = Proof_Context.theory_of ctxt;
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   620
        val gfpT = body_type (fastype_of (hd ctrs));
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   621
        val As_rho = tvar_subst thy [gfpT] [res_T];
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   622
        val substA = Term.subst_TVars As_rho;
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   623
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   624
        fun mk_spec ctr disc sels = {ctr = substA ctr, disc = substA disc, sels = map substA sels};
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   625
      in
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   626
        map3 mk_spec ctrs discs selss
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   627
      end)
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   628
  | _ => not_codatatype ctxt res_T);
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   629
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   630
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
   631
  let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   632
    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
   633
53746
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53741
diff changeset
   634
    val ((missing_res_Ts, perm0_kks,
53475
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   635
          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
   636
            co_inducts = coinduct_thms, ...} :: _, (_, gfp_sugar_thms)), lthy') =
54009
f138452e8265 got rid of dead feature
blanchet
parents: 54006
diff changeset
   637
      nested_to_mutual_fps Greatest_FP bs res_Ts get_indices callssss0 lthy;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   638
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   639
    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
   640
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   641
    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
   642
    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
   643
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   644
    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
   645
    val perm_ctr_Tsss = map (map (binder_types o fastype_of)) perm_ctrss;
54134
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   646
    val perm_gfpTs = map (body_type o fastype_of o hd) perm_ctrss;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   647
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   648
    val nn0 = length res_Ts;
54134
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   649
    val nn = length perm_gfpTs;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   650
    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
   651
    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
   652
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   653
    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
   654
      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
   655
    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
   656
      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
   657
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   658
    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
   659
    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
   660
    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
   661
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   662
    val fun_arg_hs =
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   663
      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
   664
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   665
    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
   666
    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
   667
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   668
    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
   669
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   670
    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
   671
    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
   672
    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
   673
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   674
    val f_Tssss = unpermute perm_f_Tssss;
54134
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   675
    val gfpTs = unpermute perm_gfpTs;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   676
    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
   677
54134
b26baa7f8262 added helper function (that solves chicken-and-egg problem w.r.t. "callssss")
blanchet
parents: 54105
diff changeset
   678
    val As_rho = tvar_subst thy (take nn0 gfpTs) res_Ts;
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   679
    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
   680
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   681
    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
   682
    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
   683
    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
   684
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   685
    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
   686
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   687
    fun call_of nullary [] [g_i] [Type (@{type_name fun}, [_, T])] =
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
   688
        (if exists_subtype_in Cs T then Nested_Corec
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   689
         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
   690
         else No_Corec) g_i
54102
82ada0a92dde tuned names
blanchet
parents: 54098
diff changeset
   691
      | call_of _ [q_i] [g_i, g_i'] _ = Mutual_Corec (q_i, g_i, g_i');
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   692
53705
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   693
    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
   694
        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
   695
      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
   696
        {ctr = substA ctr, disc = substA disc, sels = map substA sels, pred = p_ho,
53705
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   697
         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
   698
         collapse = collapse, corec_thm = corec_thm, disc_corec = disc_corec,
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   699
         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
   700
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   701
53974
612505263257 make SML/NJ more happy;
wenzelm
parents: 53960
diff changeset
   702
    fun mk_ctr_specs index (ctr_sugars : ctr_sugar list) p_is q_isss f_isss f_Tsss
612505263257 make SML/NJ more happy;
wenzelm
parents: 53960
diff changeset
   703
        coiter_thmsss 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
   704
      let
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   705
        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
   706
        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
   707
        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
   708
        val p_ios = map SOME p_is @ [NONE];
53705
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   709
        val discIs = #discIs (nth ctr_sugars index);
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   710
        val sel_thmss = #sel_thmss (nth ctr_sugars index);
53475
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   711
        val collapses = #collapses (nth ctr_sugars index);
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   712
        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
   713
        val disc_corecs = co_rec_of (nth disc_coitersss index);
53475
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   714
        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
   715
      in
53705
f58e289eceba enrich data structure
blanchet
parents: 53592
diff changeset
   716
        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
   717
          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
   718
      end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   719
53974
612505263257 make SML/NJ more happy;
wenzelm
parents: 53960
diff changeset
   720
    fun mk_spec ({T, index, ctr_sugars, co_iterss = coiterss, co_iter_thmsss = coiter_thmsss,
612505263257 make SML/NJ more happy;
wenzelm
parents: 53960
diff changeset
   721
          disc_co_itersss = disc_coitersss, sel_co_iterssss = sel_coiterssss, ...} : fp_sugar)
53303
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   722
        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
   723
      {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
   724
       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
   725
       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
   726
       nested_map_comps = map map_comp_of_bnf nested_bnfs,
185ad6cf6576 enriched data structure with necessary theorems
blanchet
parents: 53411
diff changeset
   727
       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
   728
         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
   729
  in
53746
bd038e48526d have "datatype_new_compat" register induction and recursion theorems in nested case
blanchet
parents: 53741
diff changeset
   730
    ((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
   731
      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
   732
      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
   733
  end;
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   734
ae49b835ca01 moved files related to "primrec_new", "primcorec", and "datatype_compat" from bitbucket co-rec repository
blanchet
parents:
diff changeset
   735
end;