src/HOL/Tools/BNF/bnf_lfp_rec_sugar.ML
author blanchet
Tue, 13 May 2014 11:10:22 +0200
changeset 56945 3d1ead21a055
parent 56857 aa2de99be748
child 57397 5004aca20821
permissions -rw-r--r--
tuning
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55061
a0adf838e2d1 adjusted comments
blanchet
parents: 55060
diff changeset
     1
(*  Title:      HOL/Tools/BNF/bnf_lfp_rec_sugar.ML
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
     2
    Author:     Lorenz Panny, TU Muenchen
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
     3
    Author:     Jasmin Blanchette, TU Muenchen
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
     4
    Copyright   2013
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
     5
56638
092a306bcc3d generate size instances for new-style datatypes
blanchet
parents: 56254
diff changeset
     6
New-style recursor sugar ("primrec").
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
     7
*)
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
     8
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
     9
signature BNF_LFP_REC_SUGAR =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    10
sig
56121
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
    11
  datatype primrec_option = Nonexhaustive_Option
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
    12
55571
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
    13
  type basic_lfp_sugar =
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
    14
    {T: typ,
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
    15
     fp_res_index: int,
55574
4a940ebceef8 rewrote a small portion of code to avoid dependency on low-level constant
blanchet
parents: 55573
diff changeset
    16
     C: typ,
4a940ebceef8 rewrote a small portion of code to avoid dependency on low-level constant
blanchet
parents: 55573
diff changeset
    17
     fun_arg_Tsss : typ list list list,
55571
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
    18
     ctr_defs: thm list,
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
    19
     ctr_sugar: Ctr_Sugar.ctr_sugar,
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
    20
     recx: term,
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
    21
     rec_thms: thm list};
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
    22
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
    23
  type lfp_rec_extension =
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
    24
    {nested_simps: thm list,
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
    25
     is_new_datatype: Proof.context -> string -> bool,
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55576
diff changeset
    26
     get_basic_lfp_sugars: binding list -> typ list -> term list ->
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55576
diff changeset
    27
       (term * term list list) list list -> local_theory ->
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55576
diff changeset
    28
       typ list * int list * basic_lfp_sugar list * thm list * thm list * thm * bool * local_theory,
55576
315dd5920114 adapted Nitpick to 'primrec' refactoring
blanchet
parents: 55575
diff changeset
    29
     rewrite_nested_rec_call: Proof.context -> (term -> bool) -> (string -> int) -> typ list ->
315dd5920114 adapted Nitpick to 'primrec' refactoring
blanchet
parents: 55575
diff changeset
    30
       term -> term -> term -> term};
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
    31
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
    32
  exception PRIMREC of string * term list;
55571
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
    33
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
    34
  val register_lfp_rec_extension: lfp_rec_extension -> theory -> theory
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
    35
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    36
  val add_primrec: (binding * typ option * mixfix) list ->
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    37
    (Attrib.binding * term) list -> local_theory -> (term list * thm list list) * local_theory
56121
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
    38
  val add_primrec_cmd: primrec_option list -> (binding * string option * mixfix) list ->
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    39
    (Attrib.binding * string) list -> local_theory -> (term list * thm list list) * local_theory
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    40
  val add_primrec_global: (binding * typ option * mixfix) list ->
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    41
    (Attrib.binding * term) list -> theory -> (term list * thm list list) * theory
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    42
  val add_primrec_overloaded: (string * (string * typ) * bool) list ->
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    43
    (binding * typ option * mixfix) list ->
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    44
    (Attrib.binding * term) list -> theory -> (term list * thm list list) * theory
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    45
  val add_primrec_simple: ((binding * typ) * mixfix) list -> term list ->
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    46
    local_theory -> (string list * (term list * (int list list * thm list list))) * local_theory
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    47
end;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    48
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    49
structure BNF_LFP_Rec_Sugar : BNF_LFP_REC_SUGAR =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    50
struct
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    51
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    52
open Ctr_Sugar
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
    53
open Ctr_Sugar_Util
55574
4a940ebceef8 rewrote a small portion of code to avoid dependency on low-level constant
blanchet
parents: 55573
diff changeset
    54
open Ctr_Sugar_General_Tactics
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    55
open BNF_FP_Rec_Sugar_Util
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    56
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
    57
val inductN = "induct"
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
    58
val simpsN = "simps"
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
    59
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    60
val nitpicksimp_attrs = @{attributes [nitpick_simp]};
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    61
val simp_attrs = @{attributes [simp]};
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    62
val code_nitpicksimp_simp_attrs = Code.add_default_eqn_attrib :: nitpicksimp_attrs @ simp_attrs;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    63
55528
c367f4f3e5d4 have 'primrec_new' fall back on old 'primrec' when given old-style datatypes
blanchet
parents: 55527
diff changeset
    64
exception OLD_PRIMREC of unit;
55527
blanchet
parents: 55482
diff changeset
    65
exception PRIMREC of string * term list;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    66
56121
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
    67
datatype primrec_option = Nonexhaustive_Option;
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
    68
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    69
datatype rec_call =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    70
  No_Rec of int * typ |
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    71
  Mutual_Rec of (int * typ) * (int * typ) |
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    72
  Nested_Rec of int * typ;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    73
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    74
type rec_ctr_spec =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    75
  {ctr: term,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    76
   offset: int,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    77
   calls: rec_call list,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    78
   rec_thm: thm};
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    79
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    80
type rec_spec =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    81
  {recx: term,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    82
   nested_map_idents: thm list,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    83
   nested_map_comps: thm list,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    84
   ctr_specs: rec_ctr_spec list};
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
    85
55538
blanchet
parents: 55535
diff changeset
    86
type basic_lfp_sugar =
blanchet
parents: 55535
diff changeset
    87
  {T: typ,
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
    88
   fp_res_index: int,
55574
4a940ebceef8 rewrote a small portion of code to avoid dependency on low-level constant
blanchet
parents: 55573
diff changeset
    89
   C: typ,
4a940ebceef8 rewrote a small portion of code to avoid dependency on low-level constant
blanchet
parents: 55573
diff changeset
    90
   fun_arg_Tsss : typ list list list,
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
    91
   ctr_defs: thm list,
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
    92
   ctr_sugar: ctr_sugar,
55570
blanchet
parents: 55539
diff changeset
    93
   recx: term,
blanchet
parents: 55539
diff changeset
    94
   rec_thms: thm list};
55538
blanchet
parents: 55535
diff changeset
    95
55571
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
    96
type lfp_rec_extension =
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
    97
  {nested_simps: thm list,
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
    98
   is_new_datatype: Proof.context -> string -> bool,
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55576
diff changeset
    99
   get_basic_lfp_sugars: binding list -> typ list -> term list ->
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55576
diff changeset
   100
     (term * term list list) list list -> local_theory ->
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55576
diff changeset
   101
     typ list * int list * basic_lfp_sugar list * thm list * thm list * thm * bool * local_theory,
55576
315dd5920114 adapted Nitpick to 'primrec' refactoring
blanchet
parents: 55575
diff changeset
   102
   rewrite_nested_rec_call: Proof.context -> (term -> bool) -> (string -> int) -> typ list ->
315dd5920114 adapted Nitpick to 'primrec' refactoring
blanchet
parents: 55575
diff changeset
   103
     term -> term -> term -> term};
55571
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   104
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   105
structure Data = Theory_Data
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   106
(
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   107
  type T = lfp_rec_extension option;
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   108
  val empty = NONE;
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   109
  val extend = I;
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   110
  val merge = merge_options;
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   111
);
55538
blanchet
parents: 55535
diff changeset
   112
55571
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   113
val register_lfp_rec_extension = Data.put o SOME;
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   114
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   115
fun nested_simps ctxt =
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   116
  (case Data.get (Proof_Context.theory_of ctxt) of
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   117
    SOME {nested_simps, ...} => nested_simps
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   118
  | NONE => []);
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   119
55571
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   120
fun is_new_datatype ctxt =
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   121
  (case Data.get (Proof_Context.theory_of ctxt) of
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   122
    SOME {is_new_datatype, ...} => is_new_datatype ctxt
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   123
  | NONE => K false);
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   124
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55576
diff changeset
   125
fun get_basic_lfp_sugars bs arg_Ts callers callssss lthy =
55571
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   126
  (case Data.get (Proof_Context.theory_of lthy) of
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55576
diff changeset
   127
    SOME {get_basic_lfp_sugars, ...} => get_basic_lfp_sugars bs arg_Ts callers callssss lthy
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55576
diff changeset
   128
  | NONE => error "Functionality not loaded yet");
55571
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   129
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   130
fun rewrite_nested_rec_call ctxt =
55571
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   131
  (case Data.get (Proof_Context.theory_of ctxt) of
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   132
    SOME {rewrite_nested_rec_call, ...} => rewrite_nested_rec_call ctxt);
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   133
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55576
diff changeset
   134
fun rec_specs_of bs arg_Ts res_Ts callers callssss0 lthy0 =
55538
blanchet
parents: 55535
diff changeset
   135
  let
blanchet
parents: 55535
diff changeset
   136
    val thy = Proof_Context.theory_of lthy0;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   137
55538
blanchet
parents: 55535
diff changeset
   138
    val (missing_arg_Ts, perm0_kks, basic_lfp_sugars, nested_map_idents, nested_map_comps,
56857
aa2de99be748 note correct induction schemes in 'primrec' (for N2M)
blanchet
parents: 56651
diff changeset
   139
         common_induct, n2m, lthy) =
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55576
diff changeset
   140
      get_basic_lfp_sugars bs arg_Ts callers callssss0 lthy0;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   141
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
   142
    val perm_basic_lfp_sugars = sort (int_ord o pairself #fp_res_index) basic_lfp_sugars;
55538
blanchet
parents: 55535
diff changeset
   143
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
   144
    val indices = map #fp_res_index basic_lfp_sugars;
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
   145
    val perm_indices = map #fp_res_index perm_basic_lfp_sugars;
55538
blanchet
parents: 55535
diff changeset
   146
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
   147
    val perm_ctrss = map (#ctrs o #ctr_sugar) perm_basic_lfp_sugars;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   148
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   149
    val nn0 = length arg_Ts;
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
   150
    val nn = length perm_ctrss;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   151
    val kks = 0 upto nn - 1;
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
   152
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
   153
    val perm_ctr_offsets = map (fn kk => Integer.sum (map length (take kk perm_ctrss))) kks;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   154
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
   155
    val perm_lfpTs = map (body_type o fastype_of o hd) perm_ctrss;
55574
4a940ebceef8 rewrote a small portion of code to avoid dependency on low-level constant
blanchet
parents: 55573
diff changeset
   156
    val perm_Cs = map #C perm_basic_lfp_sugars;
4a940ebceef8 rewrote a small portion of code to avoid dependency on low-level constant
blanchet
parents: 55573
diff changeset
   157
    val perm_fun_arg_Tssss = map #fun_arg_Tsss perm_basic_lfp_sugars;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   158
55480
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55464
diff changeset
   159
    fun unpermute0 perm0_xs = permute_like_unique (op =) perm0_kks kks perm0_xs;
59cc4a8bc28a allow different functions to recurse on the same type, like in the old package
blanchet
parents: 55464
diff changeset
   160
    fun unpermute perm_xs = permute_like_unique (op =) perm_indices indices perm_xs;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   161
56857
aa2de99be748 note correct induction schemes in 'primrec' (for N2M)
blanchet
parents: 56651
diff changeset
   162
    val inducts = unpermute0 (conj_dests nn common_induct);
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   163
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   164
    val lfpTs = unpermute perm_lfpTs;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   165
    val Cs = unpermute perm_Cs;
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
   166
    val ctr_offsets = unpermute perm_ctr_offsets;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   167
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   168
    val As_rho = tvar_subst thy (take nn0 lfpTs) arg_Ts;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   169
    val Cs_rho = map (fst o dest_TVar) Cs ~~ pad_list HOLogic.unitT nn res_Ts;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   170
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   171
    val substA = Term.subst_TVars As_rho;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   172
    val substAT = Term.typ_subst_TVars As_rho;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   173
    val substCT = Term.typ_subst_TVars Cs_rho;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   174
    val substACT = substAT o substCT;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   175
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   176
    val perm_Cs' = map substCT perm_Cs;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   177
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   178
    fun call_of [i] [T] = (if exists_subtype_in Cs T then Nested_Rec else No_Rec) (i, substACT T)
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   179
      | call_of [i, i'] [T, T'] = Mutual_Rec ((i, substACT T), (i', substACT T'));
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   180
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   181
    fun mk_ctr_spec ctr offset fun_arg_Tss rec_thm =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   182
      let
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   183
        val (fun_arg_hss, _) = indexedd fun_arg_Tss 0;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   184
        val fun_arg_hs = flat_rec_arg_args fun_arg_hss;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   185
        val fun_arg_iss = map (map (find_index_eq fun_arg_hs)) fun_arg_hss;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   186
      in
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   187
        {ctr = substA ctr, offset = offset, calls = map2 call_of fun_arg_iss fun_arg_Tss,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   188
         rec_thm = rec_thm}
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   189
      end;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   190
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
   191
    fun mk_ctr_specs fp_res_index k ctrs rec_thms =
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
   192
      map4 mk_ctr_spec ctrs (k upto k + length ctrs - 1) (nth perm_fun_arg_Tssss fp_res_index)
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
   193
        rec_thms;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   194
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
   195
    fun mk_spec ctr_offset
55570
blanchet
parents: 55539
diff changeset
   196
        ({T, fp_res_index, ctr_sugar = {ctrs, ...}, recx, rec_thms, ...} : basic_lfp_sugar) =
55869
54ddb003e128 rationalized internals
blanchet
parents: 55772
diff changeset
   197
      {recx = mk_co_rec thy Least_FP (substAT T) perm_Cs' recx,
55538
blanchet
parents: 55535
diff changeset
   198
       nested_map_idents = nested_map_idents, nested_map_comps = nested_map_comps,
55570
blanchet
parents: 55539
diff changeset
   199
       ctr_specs = mk_ctr_specs fp_res_index ctr_offset ctrs rec_thms};
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   200
  in
56857
aa2de99be748 note correct induction schemes in 'primrec' (for N2M)
blanchet
parents: 56651
diff changeset
   201
    ((n2m, map2 mk_spec ctr_offsets basic_lfp_sugars, missing_arg_Ts, common_induct, inducts), lthy)
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   202
  end;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   203
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   204
val undef_const = Const (@{const_name undefined}, dummyT);
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   205
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   206
type eqn_data = {
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   207
  fun_name: string,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   208
  rec_type: typ,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   209
  ctr: term,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   210
  ctr_args: term list,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   211
  left_args: term list,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   212
  right_args: term list,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   213
  res_type: typ,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   214
  rhs_term: term,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   215
  user_eqn: term
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   216
};
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   217
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   218
fun dissect_eqn lthy fun_names eqn' =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   219
  let
54979
d7593bfccf25 correctly extract code RHS, with loose bound variables
blanchet
parents: 54952
diff changeset
   220
    val eqn = drop_all eqn' |> HOLogic.dest_Trueprop
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   221
      handle TERM _ =>
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   222
             raise PRIMREC ("malformed function equation (expected \"lhs = rhs\")", [eqn']);
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   223
    val (lhs, rhs) = HOLogic.dest_eq eqn
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   224
        handle TERM _ =>
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   225
               raise PRIMREC ("malformed function equation (expected \"lhs = rhs\")", [eqn']);
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   226
    val (fun_name, args) = strip_comb lhs
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   227
      |>> (fn x => if is_Free x then fst (dest_Free x)
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   228
          else raise PRIMREC ("malformed function equation (does not start with free)", [eqn]));
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   229
    val (left_args, rest) = take_prefix is_Free args;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   230
    val (nonfrees, right_args) = take_suffix is_Free rest;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   231
    val num_nonfrees = length nonfrees;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   232
    val _ = num_nonfrees = 1 orelse if num_nonfrees = 0 then
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   233
      raise PRIMREC ("constructor pattern missing in left-hand side", [eqn]) else
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   234
      raise PRIMREC ("more than one non-variable argument in left-hand side", [eqn]);
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   235
    val _ = member (op =) fun_names fun_name orelse
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   236
      raise PRIMREC ("malformed function equation (does not start with function name)", [eqn]);
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   237
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   238
    val (ctr, ctr_args) = strip_comb (the_single nonfrees);
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   239
    val _ = try (num_binder_types o fastype_of) ctr = SOME (length ctr_args) orelse
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   240
      raise PRIMREC ("partially applied constructor in pattern", [eqn]);
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   241
    val _ = let val d = duplicates (op =) (left_args @ ctr_args @ right_args) in null d orelse
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   242
      raise PRIMREC ("duplicate variable \"" ^ Syntax.string_of_term lthy (hd d) ^
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   243
        "\" in left-hand side", [eqn]) end;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   244
    val _ = forall is_Free ctr_args orelse
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   245
      raise PRIMREC ("non-primitive pattern in left-hand side", [eqn]);
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   246
    val _ =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   247
      let val b = fold_aterms (fn x as Free (v, _) =>
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   248
        if (not (member (op =) (left_args @ ctr_args @ right_args) x) andalso
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   249
        not (member (op =) fun_names v) andalso
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   250
        not (Variable.is_fixed lthy v)) then cons x else I | _ => I) rhs []
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   251
      in
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   252
        null b orelse
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   253
        raise PRIMREC ("extra variable(s) in right-hand side: " ^
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   254
          commas (map (Syntax.string_of_term lthy) b), [eqn])
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   255
      end;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   256
  in
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   257
    {fun_name = fun_name,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   258
     rec_type = body_type (type_of ctr),
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   259
     ctr = ctr,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   260
     ctr_args = ctr_args,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   261
     left_args = left_args,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   262
     right_args = right_args,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   263
     res_type = map fastype_of (left_args @ right_args) ---> fastype_of rhs,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   264
     rhs_term = rhs,
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   265
     user_eqn = eqn'}
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   266
  end;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   267
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   268
fun subst_rec_calls lthy get_ctr_pos has_call ctr_args mutual_calls nested_calls =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   269
  let
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   270
    fun try_nested_rec bound_Ts y t =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   271
      AList.lookup (op =) nested_calls y
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   272
      |> Option.map (fn y' => rewrite_nested_rec_call lthy has_call get_ctr_pos bound_Ts y y' t);
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   273
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   274
    fun subst bound_Ts (t as g' $ y) =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   275
        let
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   276
          fun subst_rec () = subst bound_Ts g' $ subst bound_Ts y;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   277
          val y_head = head_of y;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   278
        in
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   279
          if not (member (op =) ctr_args y_head) then
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   280
            subst_rec ()
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   281
          else
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   282
            (case try_nested_rec bound_Ts y_head t of
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   283
              SOME t' => t'
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   284
            | NONE =>
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   285
              let val (g, g_args) = strip_comb g' in
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   286
                (case try (get_ctr_pos o fst o dest_Free) g of
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   287
                  SOME ctr_pos =>
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   288
                  (length g_args >= ctr_pos orelse
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   289
                   raise PRIMREC ("too few arguments in recursive call", [t]);
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   290
                   (case AList.lookup (op =) mutual_calls y of
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   291
                     SOME y' => list_comb (y', g_args)
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   292
                   | NONE => subst_rec ()))
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   293
                | NONE => subst_rec ())
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   294
              end)
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   295
        end
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   296
      | subst bound_Ts (Abs (v, T, b)) = Abs (v, T, subst (T :: bound_Ts) b)
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   297
      | subst _ t = t
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   298
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   299
    fun subst' t =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   300
      if has_call t then
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   301
        (* FIXME detect this case earlier? *)
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   302
        raise PRIMREC ("recursive call not directly applied to constructor argument", [t])
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   303
      else
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   304
        try_nested_rec [] (head_of t) t |> the_default t
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   305
  in
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   306
    subst' o subst []
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   307
  end;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   308
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   309
fun build_rec_arg lthy (funs_data : eqn_data list list) has_call (ctr_spec : rec_ctr_spec)
54926
28b621fce2f9 more SML-ish (less Haskell-ish) naming convention
blanchet
parents: 54851
diff changeset
   310
    (eqn_data_opt : eqn_data option) =
28b621fce2f9 more SML-ish (less Haskell-ish) naming convention
blanchet
parents: 54851
diff changeset
   311
  (case eqn_data_opt of
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   312
    NONE => undef_const
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   313
  | SOME {ctr_args, left_args, right_args, rhs_term = t, ...} =>
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   314
    let
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   315
      val calls = #calls ctr_spec;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   316
      val n_args = fold (Integer.add o (fn Mutual_Rec _ => 2 | _ => 1)) calls 0;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   317
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   318
      val no_calls' = tag_list 0 calls
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   319
        |> map_filter (try (apsnd (fn No_Rec p => p | Mutual_Rec (p, _) => p)));
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   320
      val mutual_calls' = tag_list 0 calls
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   321
        |> map_filter (try (apsnd (fn Mutual_Rec (_, p) => p)));
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   322
      val nested_calls' = tag_list 0 calls
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   323
        |> map_filter (try (apsnd (fn Nested_Rec p => p)));
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   324
54851
48a24d371ebb prevent ambiguity when mutual recursion maps a datatype to itself, which yielded wrong definitions in some cases (e.g. nat)
panny
parents: 54272
diff changeset
   325
      fun ensure_unique frees t =
48a24d371ebb prevent ambiguity when mutual recursion maps a datatype to itself, which yielded wrong definitions in some cases (e.g. nat)
panny
parents: 54272
diff changeset
   326
        if member (op =) frees t then Free (the_single (Term.variant_frees t [dest_Free t])) else t;
48a24d371ebb prevent ambiguity when mutual recursion maps a datatype to itself, which yielded wrong definitions in some cases (e.g. nat)
panny
parents: 54272
diff changeset
   327
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   328
      val args = replicate n_args ("", dummyT)
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   329
        |> Term.rename_wrt_term t
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   330
        |> map Free
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   331
        |> fold (fn (ctr_arg_idx, (arg_idx, _)) =>
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   332
            nth_map arg_idx (K (nth ctr_args ctr_arg_idx)))
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   333
          no_calls'
54851
48a24d371ebb prevent ambiguity when mutual recursion maps a datatype to itself, which yielded wrong definitions in some cases (e.g. nat)
panny
parents: 54272
diff changeset
   334
        |> fold (fn (ctr_arg_idx, (arg_idx, T)) => fn xs =>
56651
fc105315822a localize new size function generation code
blanchet
parents: 56638
diff changeset
   335
            nth_map arg_idx (K (ensure_unique xs
fc105315822a localize new size function generation code
blanchet
parents: 56638
diff changeset
   336
              (retype_const_or_free T (nth ctr_args ctr_arg_idx)))) xs)
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   337
          mutual_calls'
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   338
        |> fold (fn (ctr_arg_idx, (arg_idx, T)) =>
56651
fc105315822a localize new size function generation code
blanchet
parents: 56638
diff changeset
   339
            nth_map arg_idx (K (retype_const_or_free T (nth ctr_args ctr_arg_idx))))
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   340
          nested_calls';
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   341
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   342
      val fun_name_ctr_pos_list =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   343
        map (fn (x :: _) => (#fun_name x, length (#left_args x))) funs_data;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   344
      val get_ctr_pos = try (the o AList.lookup (op =) fun_name_ctr_pos_list) #> the_default ~1;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   345
      val mutual_calls = map (apfst (nth ctr_args) o apsnd (nth args o fst)) mutual_calls';
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   346
      val nested_calls = map (apfst (nth ctr_args) o apsnd (nth args o fst)) nested_calls';
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   347
    in
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   348
      t
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   349
      |> subst_rec_calls lthy get_ctr_pos has_call ctr_args mutual_calls nested_calls
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   350
      |> fold_rev lambda (args @ left_args @ right_args)
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   351
    end);
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   352
56121
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   353
fun build_defs lthy nonexhaustive bs mxs (funs_data : eqn_data list list)
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   354
    (rec_specs : rec_spec list) has_call =
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   355
  let
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   356
    val n_funs = length funs_data;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   357
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   358
    val ctr_spec_eqn_data_list' =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   359
      (take n_funs rec_specs |> map #ctr_specs) ~~ funs_data
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   360
      |> maps (uncurry (finds (fn (x, y) => #ctr x = #ctr y))
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   361
          ##> (fn x => null x orelse
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   362
            raise PRIMREC ("excess equations in definition", map #rhs_term x)) #> fst);
56121
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   363
    val _ = ctr_spec_eqn_data_list' |> map (fn ({ctr, ...}, x) =>
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   364
        if length x > 1 then raise PRIMREC ("multiple equations for constructor", map #user_eqn x)
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   365
        else if length x = 1 orelse nonexhaustive then ()
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   366
        else warning ("no equation for constructor " ^ Syntax.string_of_term lthy ctr));
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   367
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   368
    val ctr_spec_eqn_data_list =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   369
      ctr_spec_eqn_data_list' @ (drop n_funs rec_specs |> maps #ctr_specs |> map (rpair []));
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   370
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   371
    val recs = take n_funs rec_specs |> map #recx;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   372
    val rec_args = ctr_spec_eqn_data_list
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   373
      |> sort ((op <) o pairself (#offset o fst) |> make_ord)
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   374
      |> map (uncurry (build_rec_arg lthy funs_data has_call) o apsnd (try the_single));
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   375
    val ctr_poss = map (fn x =>
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   376
      if length (distinct ((op =) o pairself (length o #left_args)) x) <> 1 then
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   377
        raise PRIMREC ("inconstant constructor pattern position for function " ^
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   378
          quote (#fun_name (hd x)), [])
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   379
      else
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   380
        hd x |> #left_args |> length) funs_data;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   381
  in
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   382
    (recs, ctr_poss)
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   383
    |-> map2 (fn recx => fn ctr_pos => list_comb (recx, rec_args) |> permute_args ctr_pos)
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   384
    |> Syntax.check_terms lthy
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   385
    |> map3 (fn b => fn mx => fn t => ((b, mx), ((Binding.conceal (Thm.def_binding b), []), t)))
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   386
      bs mxs
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   387
  end;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   388
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   389
fun find_rec_calls has_call ({ctr, ctr_args, rhs_term, ...} : eqn_data) =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   390
  let
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   391
    fun find bound_Ts (Abs (_, T, b)) ctr_arg = find (T :: bound_Ts) b ctr_arg
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   392
      | find bound_Ts (t as _ $ _) ctr_arg =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   393
        let
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   394
          val typof = curry fastype_of1 bound_Ts;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   395
          val (f', args') = strip_comb t;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   396
          val n = find_index (equal ctr_arg o head_of) args';
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   397
        in
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   398
          if n < 0 then
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   399
            find bound_Ts f' ctr_arg @ maps (fn x => find bound_Ts x ctr_arg) args'
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   400
          else
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   401
            let
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   402
              val (f, args as arg :: _) = chop n args' |>> curry list_comb f'
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   403
              val (arg_head, arg_args) = Term.strip_comb arg;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   404
            in
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   405
              if has_call f then
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   406
                mk_partial_compN (length arg_args) (typof arg_head) f ::
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   407
                maps (fn x => find bound_Ts x ctr_arg) args
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   408
              else
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   409
                find bound_Ts f ctr_arg @ maps (fn x => find bound_Ts x ctr_arg) args
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   410
            end
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   411
        end
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   412
      | find _ _ _ = [];
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   413
  in
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   414
    map (find [] rhs_term) ctr_args
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   415
    |> (fn [] => NONE | callss => SOME (ctr, callss))
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   416
  end;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   417
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   418
fun mk_primrec_tac ctxt num_extra_args map_idents map_comps fun_defs recx =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   419
  unfold_thms_tac ctxt fun_defs THEN
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   420
  HEADGOAL (rtac (funpow num_extra_args (fn thm => thm RS fun_cong) recx RS trans)) THEN
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   421
  unfold_thms_tac ctxt (nested_simps ctxt @ map_comps @ map_idents) THEN
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   422
  HEADGOAL (rtac refl);
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   423
56121
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   424
fun prepare_primrec nonexhaustive fixes specs lthy0 =
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   425
  let
55535
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   426
    val thy = Proof_Context.theory_of lthy0;
54272
9d623cada37f avoid subtle failure in the presence of top sort
blanchet
parents: 54256
diff changeset
   427
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   428
    val (bs, mxs) = map_split (apfst fst) fixes;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   429
    val fun_names = map Binding.name_of bs;
55535
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   430
    val eqns_data = map (dissect_eqn lthy0 fun_names) specs;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   431
    val funs_data = eqns_data
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   432
      |> partition_eq ((op =) o pairself #fun_name)
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   433
      |> finds (fn (x, y) => x = #fun_name (hd y)) fun_names |> fst
55527
blanchet
parents: 55482
diff changeset
   434
      |> map (fn (x, y) => the_single y
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   435
          handle List.Empty => raise PRIMREC ("missing equations for function " ^ quote x, []));
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   436
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55576
diff changeset
   437
    val frees = map (fst #>> Binding.name_of #> Free) fixes;
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55576
diff changeset
   438
    val has_call = exists_subterm (member (op =) frees);
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   439
    val arg_Ts = map (#rec_type o hd) funs_data;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   440
    val res_Ts = map (#res_type o hd) funs_data;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   441
    val callssss = funs_data
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   442
      |> map (partition_eq ((op =) o pairself #ctr))
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   443
      |> map (maps (map_filter (find_rec_calls has_call)));
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   444
55528
c367f4f3e5d4 have 'primrec_new' fall back on old 'primrec' when given old-style datatypes
blanchet
parents: 55527
diff changeset
   445
    fun is_only_old_datatype (Type (s, _)) =
55571
a6153343c44f prepare two-stage 'primrec' setup
blanchet
parents: 55570
diff changeset
   446
        is_some (Datatype_Data.get_info thy s) andalso not (is_new_datatype lthy0 s)
55528
c367f4f3e5d4 have 'primrec_new' fall back on old 'primrec' when given old-style datatypes
blanchet
parents: 55527
diff changeset
   447
      | is_only_old_datatype _ = false;
c367f4f3e5d4 have 'primrec_new' fall back on old 'primrec' when given old-style datatypes
blanchet
parents: 55527
diff changeset
   448
c367f4f3e5d4 have 'primrec_new' fall back on old 'primrec' when given old-style datatypes
blanchet
parents: 55527
diff changeset
   449
    val _ = if exists is_only_old_datatype arg_Ts then raise OLD_PRIMREC () else ();
56254
a2dd9200854d more antiquotations;
wenzelm
parents: 56121
diff changeset
   450
    val _ = (case filter_out (fn (_, T) => Sign.of_sort thy (T, @{sort type})) (bs ~~ res_Ts) of
54272
9d623cada37f avoid subtle failure in the presence of top sort
blanchet
parents: 54256
diff changeset
   451
        [] => ()
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   452
      | (b, _) :: _ => raise PRIMREC ("type of " ^ Binding.print b ^ " contains top sort", []));
54272
9d623cada37f avoid subtle failure in the presence of top sort
blanchet
parents: 54256
diff changeset
   453
56857
aa2de99be748 note correct induction schemes in 'primrec' (for N2M)
blanchet
parents: 56651
diff changeset
   454
    val ((n2m, rec_specs, _, common_induct, inducts), lthy) =
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55576
diff changeset
   455
      rec_specs_of bs arg_Ts res_Ts frees callssss lthy0;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   456
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   457
    val actual_nn = length funs_data;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   458
55539
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
   459
    val ctrs = maps (map #ctr o #ctr_specs) rec_specs;
0819931d652d simplified data structure by reducing the incidence of clumsy indices
blanchet
parents: 55538
diff changeset
   460
    val _ =
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   461
      map (fn {ctr, user_eqn, ...} => member (op =) ctrs ctr orelse
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   462
        raise PRIMREC ("argument " ^ quote (Syntax.string_of_term lthy ctr) ^
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   463
          " is not a constructor in left-hand side", [user_eqn])) eqns_data;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   464
56121
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   465
    val defs = build_defs lthy nonexhaustive bs mxs funs_data rec_specs has_call;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   466
55535
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   467
    fun prove lthy' def_thms' ({ctr_specs, nested_map_idents, nested_map_comps, ...} : rec_spec)
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   468
        (fun_data : eqn_data list) =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   469
      let
55535
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   470
        val js =
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   471
          find_indices (op = o pairself (fn {fun_name, ctr, ...} => (fun_name, ctr)))
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   472
            fun_data eqns_data;
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   473
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   474
        val def_thms = map (snd o snd) def_thms';
55535
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   475
        val simp_thms = finds (fn (x, y) => #ctr x = #ctr y) fun_data ctr_specs
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   476
          |> fst
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   477
          |> map_filter (try (fn (x, [y]) =>
55535
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   478
            (#fun_name x, #user_eqn x, length (#left_args x) + length (#right_args x), #rec_thm y)))
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   479
          |> map2 (fn j => fn (fun_name, user_eqn, num_extra_args, rec_thm) =>
55772
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55576
diff changeset
   480
              mk_primrec_tac lthy' num_extra_args nested_map_idents nested_map_comps def_thms
367ec44763fd correct most general type for mutual recursion when several identical types are involved
blanchet
parents: 55576
diff changeset
   481
                rec_thm
55535
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   482
              |> K |> Goal.prove_sorry lthy' [] [] user_eqn
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   483
              (* for code extraction from proof terms: *)
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   484
              |> singleton (Proof_Context.export lthy' lthy)
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   485
              |> Thm.name_derivation (Sign.full_name thy (Binding.name fun_name) ^
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   486
                Long_Name.separator ^ simpsN ^
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   487
                (if js = [0] then "" else "_" ^ string_of_int (j + 1))))
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   488
            js;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   489
      in
55535
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   490
        (js, simp_thms)
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   491
      end;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   492
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   493
    val notes =
55482
61ffc2339a8c removed assumption in 'primrec_new' that a given constructor can only occur once
blanchet
parents: 55480
diff changeset
   494
      (if n2m then
56857
aa2de99be748 note correct induction schemes in 'primrec' (for N2M)
blanchet
parents: 56651
diff changeset
   495
         map2 (fn name => fn thm => (name, inductN, [thm], [])) fun_names (take actual_nn inducts)
55482
61ffc2339a8c removed assumption in 'primrec_new' that a given constructor can only occur once
blanchet
parents: 55480
diff changeset
   496
       else
61ffc2339a8c removed assumption in 'primrec_new' that a given constructor can only occur once
blanchet
parents: 55480
diff changeset
   497
         [])
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   498
      |> map (fn (prefix, thmN, thms, attrs) =>
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   499
        ((Binding.qualify true prefix (Binding.name thmN), attrs), [(thms, [])]));
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   500
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   501
    val common_name = mk_common_name fun_names;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   502
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   503
    val common_notes =
56857
aa2de99be748 note correct induction schemes in 'primrec' (for N2M)
blanchet
parents: 56651
diff changeset
   504
      (if n2m then [(inductN, [common_induct], [])] else [])
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   505
      |> map (fn (thmN, thms, attrs) =>
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   506
        ((Binding.qualify true common_name (Binding.name thmN), attrs), [(thms, [])]));
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   507
  in
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   508
    (((fun_names, defs),
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   509
      fn lthy => fn defs =>
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   510
        split_list (map2 (prove lthy defs) (take actual_nn rec_specs) funs_data)),
55535
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   511
      lthy |> Local_Theory.notes (notes @ common_notes) |> snd)
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   512
  end;
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   513
56121
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   514
fun add_primrec_simple' opts fixes ts lthy =
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   515
  let
56121
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   516
    val nonexhaustive = member (op =) opts Nonexhaustive_Option;
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   517
    val (((names, defs), prove), lthy') = prepare_primrec nonexhaustive fixes ts lthy
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   518
      handle ERROR str => raise PRIMREC (str, []);
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   519
  in
55527
blanchet
parents: 55482
diff changeset
   520
    lthy'
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   521
    |> fold_map Local_Theory.define defs
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   522
    |-> (fn defs => `(fn lthy => (names, (map fst defs, prove lthy defs))))
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   523
  end
55527
blanchet
parents: 55482
diff changeset
   524
  handle PRIMREC (str, eqns) =>
blanchet
parents: 55482
diff changeset
   525
         if null eqns then
55530
3dfb724db099 renamed 'primrec_new' to 'primrec', overriding the old command (which it still uses as a fallback for old-style datatypes)
blanchet
parents: 55529
diff changeset
   526
           error ("primrec error:\n  " ^ str)
55527
blanchet
parents: 55482
diff changeset
   527
         else
55530
3dfb724db099 renamed 'primrec_new' to 'primrec', overriding the old command (which it still uses as a fallback for old-style datatypes)
blanchet
parents: 55529
diff changeset
   528
           error ("primrec error:\n  " ^ str ^ "\nin\n  " ^
55527
blanchet
parents: 55482
diff changeset
   529
             space_implode "\n  " (map (quote o Syntax.string_of_term lthy) eqns));
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   530
56121
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   531
val add_primrec_simple = add_primrec_simple' [];
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   532
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   533
fun gen_primrec old_primrec prep_spec opts
56945
blanchet
parents: 56857
diff changeset
   534
    (raw_fixes : (binding * 'a option * mixfix) list) raw_specs lthy =
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   535
  let
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   536
    val d = duplicates (op =) (map (Binding.name_of o #1) raw_fixes)
55575
a5e33e18fb5c moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents: 55574
diff changeset
   537
    val _ = null d orelse raise PRIMREC ("duplicate function name(s): " ^ commas d, []);
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   538
56945
blanchet
parents: 56857
diff changeset
   539
    val (fixes, specs) = fst (prep_spec raw_fixes raw_specs lthy);
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   540
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   541
    val mk_notes =
55535
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   542
      flat ooo map3 (fn js => fn prefix => fn thms =>
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   543
        let
55535
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   544
          val (bs, attrss) = map_split (fst o nth specs) js;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   545
          val notes =
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   546
            map3 (fn b => fn attrs => fn thm =>
55464
56fa33537869 have 'Ctr_Sugar' register its 'Spec_Rules'
blanchet
parents: 55067
diff changeset
   547
                ((Binding.qualify false prefix b, code_nitpicksimp_simp_attrs @ attrs),
56fa33537869 have 'Ctr_Sugar' register its 'Spec_Rules'
blanchet
parents: 55067
diff changeset
   548
                 [([thm], [])]))
56fa33537869 have 'Ctr_Sugar' register its 'Spec_Rules'
blanchet
parents: 55067
diff changeset
   549
              bs attrss thms;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   550
        in
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   551
          ((Binding.qualify true prefix (Binding.name simpsN), []), [(thms, [])]) :: notes
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   552
        end);
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   553
  in
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   554
    lthy
56121
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   555
    |> add_primrec_simple' opts fixes (map snd specs)
55535
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   556
    |-> (fn (names, (ts, (jss, simpss))) =>
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   557
      Spec_Rules.add Spec_Rules.Equational (ts, flat simpss)
55535
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   558
      #> Local_Theory.notes (mk_notes jss names simpss)
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   559
      #>> pair ts o map snd)
55528
c367f4f3e5d4 have 'primrec_new' fall back on old 'primrec' when given old-style datatypes
blanchet
parents: 55527
diff changeset
   560
  end
56945
blanchet
parents: 56857
diff changeset
   561
  handle OLD_PRIMREC () => old_primrec raw_fixes raw_specs lthy |>> apsnd single;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   562
56121
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   563
val add_primrec = gen_primrec Primrec.add_primrec Specification.check_spec [];
55528
c367f4f3e5d4 have 'primrec_new' fall back on old 'primrec' when given old-style datatypes
blanchet
parents: 55527
diff changeset
   564
val add_primrec_cmd = gen_primrec Primrec.add_primrec_cmd Specification.read_spec;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   565
55535
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   566
fun add_primrec_global fixes specs =
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   567
  Named_Target.theory_init
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   568
  #> add_primrec fixes specs
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   569
  ##> Local_Theory.exit_global;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   570
55535
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   571
fun add_primrec_overloaded ops fixes specs =
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   572
  Overloading.overloading ops
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   573
  #> add_primrec fixes specs
10194808430d name derivations in 'primrec' for code extraction from proof terms
blanchet
parents: 55530
diff changeset
   574
  ##> Local_Theory.exit_global;
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   575
56121
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   576
val primrec_option_parser = Parse.group (fn () => "option")
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   577
  (Parse.reserved "nonexhaustive" >> K Nonexhaustive_Option)
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   578
55530
3dfb724db099 renamed 'primrec_new' to 'primrec', overriding the old command (which it still uses as a fallback for old-style datatypes)
blanchet
parents: 55529
diff changeset
   579
val _ = Outer_Syntax.local_theory @{command_spec "primrec"}
55529
51998cb9d6b8 tuning: moved code where it belongs
blanchet
parents: 55528
diff changeset
   580
  "define primitive recursive functions"
56121
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   581
  ((Scan.optional (@{keyword "("} |--
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   582
      Parse.!!! (Parse.list1 primrec_option_parser) --| @{keyword ")"}) []) --
52e8f110fec3 print warning if some constructors are missing;
panny
parents: 55869
diff changeset
   583
    (Parse.fixes -- Parse_Spec.where_alt_specs)
56945
blanchet
parents: 56857
diff changeset
   584
    >> (fn (opts, (fixes, specs)) => snd o add_primrec_cmd opts fixes specs));
55529
51998cb9d6b8 tuning: moved code where it belongs
blanchet
parents: 55528
diff changeset
   585
54246
8fdb4dc08ed1 split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff changeset
   586
end;