| author | wenzelm |
| Sun, 04 Jan 2015 21:01:27 +0100 | |
| changeset 59262 | 5cd92c743958 |
| parent 59058 | a78612c67ec0 |
| child 59275 | 77cd4992edcd |
| permissions | -rw-r--r-- |
| 55061 | 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 |
|
| 58315 | 6 |
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 | 11 |
datatype primrec_option = Nonexhaustive_Option |
12 |
||
|
58223
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
13 |
datatype rec_call = |
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
14 |
No_Rec of int * typ | |
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
15 |
Mutual_Rec of (int * typ) * (int * typ) | |
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
16 |
Nested_Rec of int * typ |
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
17 |
|
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
18 |
type rec_ctr_spec = |
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
19 |
{ctr: term,
|
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
20 |
offset: int, |
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
21 |
calls: rec_call list, |
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
22 |
rec_thm: thm} |
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
23 |
|
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
24 |
type rec_spec = |
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
25 |
{recx: term,
|
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
26 |
fp_nesting_map_ident0s: thm list, |
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
27 |
fp_nesting_map_comps: thm list, |
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
28 |
ctr_specs: rec_ctr_spec list} |
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
29 |
|
| 55571 | 30 |
type basic_lfp_sugar = |
31 |
{T: typ,
|
|
32 |
fp_res_index: int, |
|
|
55574
4a940ebceef8
rewrote a small portion of code to avoid dependency on low-level constant
blanchet
parents:
55573
diff
changeset
|
33 |
C: typ, |
|
4a940ebceef8
rewrote a small portion of code to avoid dependency on low-level constant
blanchet
parents:
55573
diff
changeset
|
34 |
fun_arg_Tsss : typ list list list, |
| 55571 | 35 |
ctr_sugar: Ctr_Sugar.ctr_sugar, |
36 |
recx: term, |
|
37 |
rec_thms: thm list}; |
|
38 |
||
39 |
type lfp_rec_extension = |
|
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
40 |
{nested_simps: thm list,
|
|
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
41 |
is_new_datatype: Proof.context -> string -> bool, |
| 58389 | 42 |
basic_lfp_sugars_of: binding list -> typ list -> term list -> |
|
55772
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55576
diff
changeset
|
43 |
(term * term list list) list list -> local_theory -> |
|
58283
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58281
diff
changeset
|
44 |
typ list * int list * basic_lfp_sugar list * thm list * thm list * thm * Token.src list |
|
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58281
diff
changeset
|
45 |
* bool * local_theory, |
| 58389 | 46 |
rewrite_nested_rec_call: (Proof.context -> (term -> bool) -> (string -> int) -> typ list -> |
47 |
term -> term -> term -> term) option}; |
|
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
48 |
|
|
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
49 |
exception PRIMREC of string * term list; |
| 55571 | 50 |
|
51 |
val register_lfp_rec_extension: lfp_rec_extension -> theory -> theory |
|
| 58389 | 52 |
val default_basic_lfp_sugars_of: binding list -> typ list -> term list -> |
53 |
(term * term list list) list list -> local_theory -> |
|
54 |
typ list * int list * basic_lfp_sugar list * thm list * thm list * thm * Token.src list * bool |
|
55 |
* local_theory |
|
|
58223
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
56 |
val rec_specs_of: binding list -> typ list -> typ list -> term list -> |
|
ba7a2d19880c
export useful functions for users of (co)recursors
blanchet
parents:
58220
diff
changeset
|
57 |
(term * term list list) list list -> local_theory -> |
|
58283
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58281
diff
changeset
|
58 |
(bool * rec_spec list * typ list * thm * thm list * Token.src list) * local_theory |
| 55571 | 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 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
|
61 |
(Attrib.binding * term) list -> local_theory -> (term list * thm list list) * local_theory |
| 56121 | 62 |
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
|
63 |
(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
|
64 |
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
|
65 |
(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
|
66 |
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
|
67 |
(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
|
68 |
(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
|
69 |
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
|
70 |
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
|
71 |
end; |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
72 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
73 |
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
|
74 |
struct |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
75 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
76 |
open Ctr_Sugar |
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
77 |
open Ctr_Sugar_Util |
|
55574
4a940ebceef8
rewrote a small portion of code to avoid dependency on low-level constant
blanchet
parents:
55573
diff
changeset
|
78 |
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
|
79 |
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
|
80 |
|
| 58387 | 81 |
val inductN = "induct"; |
82 |
val simpsN = "simps"; |
|
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
83 |
|
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
84 |
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
|
85 |
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
|
86 |
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
|
87 |
|
|
55528
c367f4f3e5d4
have 'primrec_new' fall back on old 'primrec' when given old-style datatypes
blanchet
parents:
55527
diff
changeset
|
88 |
exception OLD_PRIMREC of unit; |
| 55527 | 89 |
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
|
90 |
|
| 56121 | 91 |
datatype primrec_option = Nonexhaustive_Option; |
92 |
||
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
93 |
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
|
94 |
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
|
95 |
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
|
96 |
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
|
97 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
98 |
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
|
99 |
{ctr: term,
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
100 |
offset: int, |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
101 |
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
|
102 |
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
|
103 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
104 |
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
|
105 |
{recx: term,
|
| 57399 | 106 |
fp_nesting_map_ident0s: thm list, |
| 57397 | 107 |
fp_nesting_map_comps: thm list, |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
108 |
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
|
109 |
|
| 55538 | 110 |
type basic_lfp_sugar = |
111 |
{T: typ,
|
|
|
55539
0819931d652d
simplified data structure by reducing the incidence of clumsy indices
blanchet
parents:
55538
diff
changeset
|
112 |
fp_res_index: int, |
|
55574
4a940ebceef8
rewrote a small portion of code to avoid dependency on low-level constant
blanchet
parents:
55573
diff
changeset
|
113 |
C: typ, |
|
4a940ebceef8
rewrote a small portion of code to avoid dependency on low-level constant
blanchet
parents:
55573
diff
changeset
|
114 |
fun_arg_Tsss : typ list list list, |
|
55539
0819931d652d
simplified data structure by reducing the incidence of clumsy indices
blanchet
parents:
55538
diff
changeset
|
115 |
ctr_sugar: ctr_sugar, |
| 55570 | 116 |
recx: term, |
117 |
rec_thms: thm list}; |
|
| 55538 | 118 |
|
| 55571 | 119 |
type lfp_rec_extension = |
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
120 |
{nested_simps: thm list,
|
|
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
121 |
is_new_datatype: Proof.context -> string -> bool, |
| 58389 | 122 |
basic_lfp_sugars_of: binding list -> typ list -> term list -> |
|
55772
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55576
diff
changeset
|
123 |
(term * term list list) list list -> local_theory -> |
|
58283
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58281
diff
changeset
|
124 |
typ list * int list * basic_lfp_sugar list * thm list * thm list * thm * Token.src list * bool |
|
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58281
diff
changeset
|
125 |
* local_theory, |
| 58389 | 126 |
rewrite_nested_rec_call: (Proof.context -> (term -> bool) -> (string -> int) -> typ list -> |
127 |
term -> term -> term -> term) option}; |
|
| 55571 | 128 |
|
129 |
structure Data = Theory_Data |
|
130 |
( |
|
131 |
type T = lfp_rec_extension option; |
|
132 |
val empty = NONE; |
|
133 |
val extend = I; |
|
134 |
val merge = merge_options; |
|
135 |
); |
|
| 55538 | 136 |
|
| 55571 | 137 |
val register_lfp_rec_extension = Data.put o SOME; |
138 |
||
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
139 |
fun nested_simps ctxt = |
|
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
140 |
(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
|
141 |
SOME {nested_simps, ...} => nested_simps
|
|
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
142 |
| NONE => []); |
|
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
143 |
|
| 55571 | 144 |
fun is_new_datatype ctxt = |
145 |
(case Data.get (Proof_Context.theory_of ctxt) of |
|
146 |
SOME {is_new_datatype, ...} => is_new_datatype ctxt
|
|
| 58389 | 147 |
| NONE => K true); |
148 |
||
149 |
fun default_basic_lfp_sugars_of _ [Type (arg_T_name, _)] _ _ ctxt = |
|
150 |
let |
|
151 |
val ctr_sugar as {T, ctrs, casex, case_thms, ...} =
|
|
152 |
(case ctr_sugar_of ctxt arg_T_name of |
|
153 |
SOME ctr_sugar => ctr_sugar |
|
154 |
| NONE => error ("Unsupported type " ^ quote arg_T_name ^ " at this stage"));
|
|
155 |
||
156 |
val C = body_type (fastype_of casex); |
|
157 |
val fun_arg_Tsss = map (map single o binder_types o fastype_of) ctrs; |
|
| 55571 | 158 |
|
| 58389 | 159 |
val basic_lfp_sugar = |
160 |
{T = T, fp_res_index = 0, C = C, fun_arg_Tsss = fun_arg_Tsss, ctr_sugar = ctr_sugar,
|
|
161 |
recx = casex, rec_thms = case_thms}; |
|
162 |
in |
|
163 |
([], [0], [basic_lfp_sugar], [], [], TrueI, [], false, ctxt) |
|
164 |
end |
|
165 |
| default_basic_lfp_sugars_of _ _ _ _ _ = error "Unsupported mutual recursion at this stage"; |
|
166 |
||
167 |
fun basic_lfp_sugars_of bs arg_Ts callers callssss lthy = |
|
| 55571 | 168 |
(case Data.get (Proof_Context.theory_of lthy) of |
| 58389 | 169 |
SOME {basic_lfp_sugars_of, ...} => basic_lfp_sugars_of
|
170 |
| NONE => default_basic_lfp_sugars_of) bs arg_Ts callers callssss lthy; |
|
| 55571 | 171 |
|
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
172 |
fun rewrite_nested_rec_call ctxt = |
| 55571 | 173 |
(case Data.get (Proof_Context.theory_of ctxt) of |
| 58389 | 174 |
SOME {rewrite_nested_rec_call = SOME f, ...} => f ctxt
|
175 |
| _ => error "Unsupported nested recursion"); |
|
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
176 |
|
|
55772
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55576
diff
changeset
|
177 |
fun rec_specs_of bs arg_Ts res_Ts callers callssss0 lthy0 = |
| 55538 | 178 |
let |
179 |
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
|
180 |
|
| 57399 | 181 |
val (missing_arg_Ts, perm0_kks, basic_lfp_sugars, fp_nesting_map_ident0s, fp_nesting_map_comps, |
|
58283
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58281
diff
changeset
|
182 |
common_induct, induct_attrs, n2m, lthy) = |
| 58389 | 183 |
basic_lfp_sugars_of 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
|
184 |
|
|
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58996
diff
changeset
|
185 |
val perm_basic_lfp_sugars = sort (int_ord o apply2 #fp_res_index) basic_lfp_sugars; |
| 55538 | 186 |
|
|
55539
0819931d652d
simplified data structure by reducing the incidence of clumsy indices
blanchet
parents:
55538
diff
changeset
|
187 |
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
|
188 |
val perm_indices = map #fp_res_index perm_basic_lfp_sugars; |
| 55538 | 189 |
|
|
55539
0819931d652d
simplified data structure by reducing the incidence of clumsy indices
blanchet
parents:
55538
diff
changeset
|
190 |
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
|
191 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
192 |
val nn0 = length arg_Ts; |
|
55539
0819931d652d
simplified data structure by reducing the incidence of clumsy indices
blanchet
parents:
55538
diff
changeset
|
193 |
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
|
194 |
val kks = 0 upto nn - 1; |
|
55539
0819931d652d
simplified data structure by reducing the incidence of clumsy indices
blanchet
parents:
55538
diff
changeset
|
195 |
|
|
0819931d652d
simplified data structure by reducing the incidence of clumsy indices
blanchet
parents:
55538
diff
changeset
|
196 |
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
|
197 |
|
|
58187
d2ddd401d74d
fixed infinite loops in 'register' functions + more uniform API
blanchet
parents:
58112
diff
changeset
|
198 |
val perm_fpTs = map #T perm_basic_lfp_sugars; |
|
55574
4a940ebceef8
rewrote a small portion of code to avoid dependency on low-level constant
blanchet
parents:
55573
diff
changeset
|
199 |
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
|
200 |
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
|
201 |
|
|
55480
59cc4a8bc28a
allow different functions to recurse on the same type, like in the old package
blanchet
parents:
55464
diff
changeset
|
202 |
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
|
203 |
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
|
204 |
|
|
56857
aa2de99be748
note correct induction schemes in 'primrec' (for N2M)
blanchet
parents:
56651
diff
changeset
|
205 |
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
|
206 |
|
|
58187
d2ddd401d74d
fixed infinite loops in 'register' functions + more uniform API
blanchet
parents:
58112
diff
changeset
|
207 |
val fpTs = unpermute perm_fpTs; |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
208 |
val Cs = unpermute perm_Cs; |
|
55539
0819931d652d
simplified data structure by reducing the incidence of clumsy indices
blanchet
parents:
55538
diff
changeset
|
209 |
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
|
210 |
|
|
58187
d2ddd401d74d
fixed infinite loops in 'register' functions + more uniform API
blanchet
parents:
58112
diff
changeset
|
211 |
val As_rho = tvar_subst thy (take nn0 fpTs) arg_Ts; |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
212 |
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
|
213 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
214 |
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
|
215 |
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
|
216 |
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
|
217 |
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
|
218 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
219 |
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
|
220 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
221 |
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
|
222 |
| 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
|
223 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
224 |
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
|
225 |
let |
|
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_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
|
227 |
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
|
228 |
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
|
229 |
in |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
230 |
{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
|
231 |
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
|
232 |
end; |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
233 |
|
|
55539
0819931d652d
simplified data structure by reducing the incidence of clumsy indices
blanchet
parents:
55538
diff
changeset
|
234 |
fun mk_ctr_specs fp_res_index k ctrs rec_thms = |
|
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58389
diff
changeset
|
235 |
@{map 4} mk_ctr_spec ctrs (k upto k + length ctrs - 1) (nth perm_fun_arg_Tssss fp_res_index)
|
|
55539
0819931d652d
simplified data structure by reducing the incidence of clumsy indices
blanchet
parents:
55538
diff
changeset
|
236 |
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
|
237 |
|
|
55539
0819931d652d
simplified data structure by reducing the incidence of clumsy indices
blanchet
parents:
55538
diff
changeset
|
238 |
fun mk_spec ctr_offset |
| 55570 | 239 |
({T, fp_res_index, ctr_sugar = {ctrs, ...}, recx, rec_thms, ...} : basic_lfp_sugar) =
|
|
58211
c1f3fa32d322
extended 'datatype_compat' to generate the expected, old-style recursor in the presence of recursion through functions
blanchet
parents:
58187
diff
changeset
|
240 |
{recx = mk_co_rec thy Least_FP perm_Cs' (substAT T) recx,
|
| 57399 | 241 |
fp_nesting_map_ident0s = fp_nesting_map_ident0s, fp_nesting_map_comps = fp_nesting_map_comps, |
| 55570 | 242 |
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
|
243 |
in |
|
58283
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58281
diff
changeset
|
244 |
((n2m, map2 mk_spec ctr_offsets basic_lfp_sugars, missing_arg_Ts, common_induct, inducts, |
|
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58281
diff
changeset
|
245 |
induct_attrs), lthy) |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
246 |
end; |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
247 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
248 |
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
|
249 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
250 |
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
|
251 |
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
|
252 |
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
|
253 |
ctr: term, |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
254 |
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
|
255 |
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
|
256 |
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
|
257 |
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
|
258 |
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
|
259 |
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
|
260 |
}; |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
261 |
|
| 58302 | 262 |
fun dissect_eqn ctxt fun_names eqn' = |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
263 |
let |
|
54979
d7593bfccf25
correctly extract code RHS, with loose bound variables
blanchet
parents:
54952
diff
changeset
|
264 |
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
|
265 |
handle TERM _ => |
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
266 |
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
|
267 |
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
|
268 |
handle TERM _ => |
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
269 |
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
|
270 |
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
|
271 |
|>> (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
|
272 |
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
|
273 |
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
|
274 |
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
|
275 |
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
|
276 |
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
|
277 |
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
|
278 |
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
|
279 |
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
|
280 |
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
|
281 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
282 |
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
|
283 |
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
|
284 |
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
|
285 |
val _ = let val d = duplicates (op =) (left_args @ ctr_args @ right_args) in null d orelse |
| 58302 | 286 |
raise PRIMREC ("duplicate variable \"" ^ Syntax.string_of_term ctxt (hd d) ^
|
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
287 |
"\" 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
|
288 |
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
|
289 |
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
|
290 |
val _ = |
| 58302 | 291 |
let |
292 |
val b = |
|
293 |
fold_aterms (fn x as Free (v, _) => |
|
294 |
if (not (member (op =) (left_args @ ctr_args @ right_args) x) andalso |
|
295 |
not (member (op =) fun_names v) andalso not (Variable.is_fixed ctxt v)) then |
|
296 |
cons x |
|
297 |
else |
|
298 |
I |
|
299 |
| _ => I) rhs []; |
|
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
300 |
in |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
301 |
null b orelse |
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
302 |
raise PRIMREC ("extra variable(s) in right-hand side: " ^
|
| 58302 | 303 |
commas (map (Syntax.string_of_term ctxt) 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
|
304 |
end; |
|
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 |
{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
|
307 |
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
|
308 |
ctr = ctr, |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
309 |
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
|
310 |
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
|
311 |
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
|
312 |
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
|
313 |
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
|
314 |
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
|
315 |
end; |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
316 |
|
| 58302 | 317 |
fun subst_rec_calls ctxt get_ctr_pos has_call ctr_args mutual_calls nested_calls = |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
318 |
let |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
319 |
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
|
320 |
AList.lookup (op =) nested_calls y |
| 58302 | 321 |
|> Option.map (fn y' => rewrite_nested_rec_call ctxt 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
|
322 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
323 |
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
|
324 |
let |
|
58303
a0fe6d8c8ba2
fixed situation in 'primrec' when recursive calls are apparently nested, e.g. 'f (f x y) y', with the recursion in 'y'
blanchet
parents:
58302
diff
changeset
|
325 |
fun subst_comb (h $ z) = subst bound_Ts h $ subst bound_Ts z |
|
a0fe6d8c8ba2
fixed situation in 'primrec' when recursive calls are apparently nested, e.g. 'f (f x y) y', with the recursion in 'y'
blanchet
parents:
58302
diff
changeset
|
326 |
| subst_comb t = t; |
|
58301
de95aeedf49e
fixed situation in 'primrec' whereby the original value of a constructor argument of nested type was not translated correctly to a 'map fst'
blanchet
parents:
58283
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 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
|
329 |
in |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
330 |
if not (member (op =) ctr_args y_head) then |
|
58303
a0fe6d8c8ba2
fixed situation in 'primrec' when recursive calls are apparently nested, e.g. 'f (f x y) y', with the recursion in 'y'
blanchet
parents:
58302
diff
changeset
|
331 |
subst_comb t |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
332 |
else |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
333 |
(case try_nested_rec bound_Ts y_head t of |
|
58303
a0fe6d8c8ba2
fixed situation in 'primrec' when recursive calls are apparently nested, e.g. 'f (f x y) y', with the recursion in 'y'
blanchet
parents:
58302
diff
changeset
|
334 |
SOME t' => subst_comb t' |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
335 |
| NONE => |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
336 |
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
|
337 |
(case try (get_ctr_pos o fst o dest_Free) g of |
|
58303
a0fe6d8c8ba2
fixed situation in 'primrec' when recursive calls are apparently nested, e.g. 'f (f x y) y', with the recursion in 'y'
blanchet
parents:
58302
diff
changeset
|
338 |
SOME ~1 => subst_comb t |
| 57549 | 339 |
| SOME ctr_pos => |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
340 |
(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
|
341 |
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
|
342 |
(case AList.lookup (op =) mutual_calls y of |
|
58303
a0fe6d8c8ba2
fixed situation in 'primrec' when recursive calls are apparently nested, e.g. 'f (f x y) y', with the recursion in 'y'
blanchet
parents:
58302
diff
changeset
|
343 |
SOME y' => list_comb (y', map (subst bound_Ts) g_args) |
|
a0fe6d8c8ba2
fixed situation in 'primrec' when recursive calls are apparently nested, e.g. 'f (f x y) y', with the recursion in 'y'
blanchet
parents:
58302
diff
changeset
|
344 |
| NONE => subst_comb t)) |
|
a0fe6d8c8ba2
fixed situation in 'primrec' when recursive calls are apparently nested, e.g. 'f (f x y) y', with the recursion in 'y'
blanchet
parents:
58302
diff
changeset
|
345 |
| NONE => subst_comb t) |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
346 |
end) |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
347 |
end |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
348 |
| 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
|
349 |
| 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
|
350 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
351 |
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
|
352 |
if has_call t then |
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
353 |
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
|
354 |
else |
| 58304 | 355 |
try_nested_rec [] (head_of t) t |> the_default t; |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
356 |
in |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
357 |
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
|
358 |
end; |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
359 |
|
| 58302 | 360 |
fun build_rec_arg ctxt (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
|
361 |
(eqn_data_opt : eqn_data option) = |
|
28b621fce2f9
more SML-ish (less Haskell-ish) naming convention
blanchet
parents:
54851
diff
changeset
|
362 |
(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
|
363 |
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
|
364 |
| 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
|
365 |
let |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
366 |
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
|
367 |
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
|
368 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
369 |
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
|
370 |
|> 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
|
371 |
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
|
372 |
|> 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
|
373 |
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
|
374 |
|> 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
|
375 |
|
|
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
|
376 |
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
|
377 |
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
|
378 |
|
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
379 |
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
|
380 |
|> 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
|
381 |
|> map Free |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
382 |
|> 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
|
383 |
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
|
384 |
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
|
385 |
|> fold (fn (ctr_arg_idx, (arg_idx, T)) => fn xs => |
| 56651 | 386 |
nth_map arg_idx (K (ensure_unique xs |
387 |
(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
|
388 |
mutual_calls' |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
389 |
|> fold (fn (ctr_arg_idx, (arg_idx, T)) => |
| 56651 | 390 |
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
|
391 |
nested_calls'; |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
392 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
393 |
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
|
394 |
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
|
395 |
val get_ctr_pos = try (the o AList.lookup (op =) fun_name_ctr_pos_list) #> the_default ~1; |
| 57527 | 396 |
val mutual_calls = map (map_prod (nth ctr_args) (nth args o fst)) mutual_calls'; |
397 |
val nested_calls = map (map_prod (nth ctr_args) (nth args o fst)) nested_calls'; |
|
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
398 |
in |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
399 |
t |
| 58302 | 400 |
|> subst_rec_calls ctxt get_ctr_pos has_call ctr_args mutual_calls nested_calls |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
401 |
|> 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
|
402 |
end); |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
403 |
|
| 58302 | 404 |
fun build_defs ctxt nonexhaustive bs mxs (funs_data : eqn_data list list) |
| 56121 | 405 |
(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
|
406 |
let |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
407 |
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
|
408 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
409 |
val ctr_spec_eqn_data_list' = |
| 58304 | 410 |
map #ctr_specs (take n_funs rec_specs) ~~ funs_data |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
411 |
|> maps (uncurry (finds (fn (x, y) => #ctr x = #ctr y)) |
| 58304 | 412 |
##> (fn x => null x orelse |
413 |
raise PRIMREC ("excess equations in definition", map #rhs_term x)) #> fst);
|
|
| 56121 | 414 |
val _ = ctr_spec_eqn_data_list' |> map (fn ({ctr, ...}, x) =>
|
| 58281 | 415 |
if length x > 1 then raise PRIMREC ("multiple equations for constructor", map #user_eqn x)
|
416 |
else if length x = 1 orelse nonexhaustive then () |
|
| 58302 | 417 |
else warning ("no equation for constructor " ^ Syntax.string_of_term ctxt ctr));
|
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
418 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
419 |
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
|
420 |
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
|
421 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
422 |
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
|
423 |
val rec_args = ctr_spec_eqn_data_list |
|
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58996
diff
changeset
|
424 |
|> sort (op < o apply2 (#offset o fst) |> make_ord) |
| 58302 | 425 |
|> map (uncurry (build_rec_arg ctxt funs_data has_call) o apsnd (try the_single)); |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
426 |
val ctr_poss = map (fn x => |
|
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58996
diff
changeset
|
427 |
if length (distinct (op = o apply2 (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
|
428 |
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
|
429 |
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
|
430 |
else |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
431 |
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
|
432 |
in |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
433 |
(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
|
434 |
|-> map2 (fn recx => fn ctr_pos => list_comb (recx, rec_args) |> permute_args ctr_pos) |
| 58302 | 435 |
|> Syntax.check_terms ctxt |
|
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58389
diff
changeset
|
436 |
|> @{map 3} (fn b => fn mx => fn t => ((b, mx), ((Binding.conceal (Thm.def_binding b), []), t)))
|
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
437 |
bs mxs |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
438 |
end; |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
439 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
440 |
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
|
441 |
let |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
442 |
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
|
443 |
| 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
|
444 |
let |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
445 |
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
|
446 |
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
|
447 |
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
|
448 |
in |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
449 |
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
|
450 |
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
|
451 |
else |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
452 |
let |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
453 |
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
|
454 |
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
|
455 |
in |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
456 |
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
|
457 |
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
|
458 |
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
|
459 |
else |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
460 |
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
|
461 |
end |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
462 |
end |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
463 |
| find _ _ _ = []; |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
464 |
in |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
465 |
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
|
466 |
|> (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
|
467 |
end; |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
468 |
|
| 57399 | 469 |
fun mk_primrec_tac ctxt num_extra_args map_ident0s map_comps fun_defs recx = |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
470 |
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
|
471 |
HEADGOAL (rtac (funpow num_extra_args (fn thm => thm RS fun_cong) recx RS trans)) THEN |
| 57399 | 472 |
unfold_thms_tac ctxt (nested_simps ctxt @ map_ident0s @ map_comps) THEN |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
473 |
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
|
474 |
|
| 56121 | 475 |
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
|
476 |
let |
|
55535
10194808430d
name derivations in 'primrec' for code extraction from proof terms
blanchet
parents:
55530
diff
changeset
|
477 |
val thy = Proof_Context.theory_of lthy0; |
|
54272
9d623cada37f
avoid subtle failure in the presence of top sort
blanchet
parents:
54256
diff
changeset
|
478 |
|
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
479 |
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
|
480 |
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
|
481 |
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
|
482 |
val funs_data = eqns_data |
|
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58996
diff
changeset
|
483 |
|> partition_eq (op = o apply2 #fun_name) |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
484 |
|> finds (fn (x, y) => x = #fun_name (hd y)) fun_names |> fst |
| 55527 | 485 |
|> 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
|
486 |
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
|
487 |
|
|
55772
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55576
diff
changeset
|
488 |
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
|
489 |
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
|
490 |
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
|
491 |
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
|
492 |
val callssss = funs_data |
|
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58996
diff
changeset
|
493 |
|> map (partition_eq (op = o apply2 #ctr)) |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
494 |
|> 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
|
495 |
|
|
55528
c367f4f3e5d4
have 'primrec_new' fall back on old 'primrec' when given old-style datatypes
blanchet
parents:
55527
diff
changeset
|
496 |
fun is_only_old_datatype (Type (s, _)) = |
|
58112
8081087096ad
renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents:
57633
diff
changeset
|
497 |
is_some (Old_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
|
498 |
| is_only_old_datatype _ = false; |
|
c367f4f3e5d4
have 'primrec_new' fall back on old 'primrec' when given old-style datatypes
blanchet
parents:
55527
diff
changeset
|
499 |
|
|
c367f4f3e5d4
have 'primrec_new' fall back on old 'primrec' when given old-style datatypes
blanchet
parents:
55527
diff
changeset
|
500 |
val _ = if exists is_only_old_datatype arg_Ts then raise OLD_PRIMREC () else (); |
| 56254 | 501 |
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
|
502 |
[] => () |
|
55575
a5e33e18fb5c
moved 'primrec' up (for real this time) and removed temporary 'old_primrec'
blanchet
parents:
55574
diff
changeset
|
503 |
| (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
|
504 |
|
|
58283
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58281
diff
changeset
|
505 |
val ((n2m, rec_specs, _, common_induct, inducts, induct_attrs), lthy) = |
|
55772
367ec44763fd
correct most general type for mutual recursion when several identical types are involved
blanchet
parents:
55576
diff
changeset
|
506 |
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
|
507 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
508 |
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
|
509 |
|
|
55539
0819931d652d
simplified data structure by reducing the incidence of clumsy indices
blanchet
parents:
55538
diff
changeset
|
510 |
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
|
511 |
val _ = |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
512 |
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
|
513 |
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
|
514 |
" 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
|
515 |
|
| 56121 | 516 |
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
|
517 |
|
| 57399 | 518 |
fun prove lthy' def_thms' ({ctr_specs, fp_nesting_map_ident0s, fp_nesting_map_comps, ...}
|
| 57397 | 519 |
: rec_spec) (fun_data : eqn_data list) = |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
520 |
let |
|
55535
10194808430d
name derivations in 'primrec' for code extraction from proof terms
blanchet
parents:
55530
diff
changeset
|
521 |
val js = |
|
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58996
diff
changeset
|
522 |
find_indices (op = o apply2 (fn {fun_name, ctr, ...} => (fun_name, ctr)))
|
|
55535
10194808430d
name derivations in 'primrec' for code extraction from proof terms
blanchet
parents:
55530
diff
changeset
|
523 |
fun_data eqns_data; |
|
10194808430d
name derivations in 'primrec' for code extraction from proof terms
blanchet
parents:
55530
diff
changeset
|
524 |
|
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
525 |
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
|
526 |
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
|
527 |
|> fst |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
528 |
|> map_filter (try (fn (x, [y]) => |
|
55535
10194808430d
name derivations in 'primrec' for code extraction from proof terms
blanchet
parents:
55530
diff
changeset
|
529 |
(#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
|
530 |
|> map2 (fn j => fn (fun_name, user_eqn, num_extra_args, rec_thm) => |
| 57399 | 531 |
mk_primrec_tac lthy' num_extra_args fp_nesting_map_ident0s fp_nesting_map_comps |
| 57397 | 532 |
def_thms rec_thm |
|
55535
10194808430d
name derivations in 'primrec' for code extraction from proof terms
blanchet
parents:
55530
diff
changeset
|
533 |
|> K |> Goal.prove_sorry lthy' [] [] user_eqn |
| 58996 | 534 |
|> Thm.close_derivation) |
|
55535
10194808430d
name derivations in 'primrec' for code extraction from proof terms
blanchet
parents:
55530
diff
changeset
|
535 |
js; |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
536 |
in |
|
55535
10194808430d
name derivations in 'primrec' for code extraction from proof terms
blanchet
parents:
55530
diff
changeset
|
537 |
(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
|
538 |
end; |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
539 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
540 |
val notes = |
|
55482
61ffc2339a8c
removed assumption in 'primrec_new' that a given constructor can only occur once
blanchet
parents:
55480
diff
changeset
|
541 |
(if n2m then |
|
58283
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58281
diff
changeset
|
542 |
map2 (fn name => fn thm => (name, inductN, [thm], induct_attrs)) fun_names |
|
71d74e641538
preserve case names in '(co)induct' theorems generated by prim(co)rec'
blanchet
parents:
58281
diff
changeset
|
543 |
(take actual_nn inducts) |
|
55482
61ffc2339a8c
removed assumption in 'primrec_new' that a given constructor can only occur once
blanchet
parents:
55480
diff
changeset
|
544 |
else |
|
61ffc2339a8c
removed assumption in 'primrec_new' that a given constructor can only occur once
blanchet
parents:
55480
diff
changeset
|
545 |
[]) |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
546 |
|> 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
|
547 |
((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
|
548 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
549 |
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
|
550 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
551 |
val common_notes = |
|
56857
aa2de99be748
note correct induction schemes in 'primrec' (for N2M)
blanchet
parents:
56651
diff
changeset
|
552 |
(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
|
553 |
|> 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
|
554 |
((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
|
555 |
in |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
556 |
(((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
|
557 |
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
|
558 |
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
|
559 |
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
|
560 |
end; |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
561 |
|
| 56121 | 562 |
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
|
563 |
let |
| 56121 | 564 |
val nonexhaustive = member (op =) opts Nonexhaustive_Option; |
565 |
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
|
566 |
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
|
567 |
in |
| 55527 | 568 |
lthy' |
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
569 |
|> 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
|
570 |
|-> (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
|
571 |
end |
| 55527 | 572 |
handle PRIMREC (str, eqns) => |
573 |
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
|
574 |
error ("primrec error:\n " ^ str)
|
| 55527 | 575 |
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
|
576 |
error ("primrec error:\n " ^ str ^ "\nin\n " ^
|
| 55527 | 577 |
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
|
578 |
|
| 58220 | 579 |
fun add_primrec_simple fixes ts lthy = |
580 |
add_primrec_simple' [] fixes ts lthy |
|
581 |
handle OLD_PRIMREC () => |
|
582 |
Old_Primrec.add_primrec_simple fixes ts lthy |
|
583 |
|>> apsnd (apsnd (pair [] o single)) o apfst single; |
|
| 56121 | 584 |
|
585 |
fun gen_primrec old_primrec prep_spec opts |
|
| 56945 | 586 |
(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
|
587 |
let |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
588 |
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
|
589 |
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
|
590 |
|
| 56945 | 591 |
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
|
592 |
|
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
593 |
val mk_notes = |
|
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58389
diff
changeset
|
594 |
flat ooo @{map 3} (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
|
595 |
let |
|
55535
10194808430d
name derivations in 'primrec' for code extraction from proof terms
blanchet
parents:
55530
diff
changeset
|
596 |
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
|
597 |
val notes = |
|
58634
9f10d82e8188
added parameterized ML antiquotations @{map N}, @{fold N}, @{fold_map N}, @{split_list N};
wenzelm
parents:
58389
diff
changeset
|
598 |
@{map 3} (fn b => fn attrs => fn thm =>
|
| 55464 | 599 |
((Binding.qualify false prefix b, code_nitpicksimp_simp_attrs @ attrs), |
600 |
[([thm], [])])) |
|
601 |
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
|
602 |
in |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
603 |
((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
|
604 |
end); |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
605 |
in |
|
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
606 |
lthy |
| 56121 | 607 |
|> 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
|
608 |
|-> (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
|
609 |
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
|
610 |
#> 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
|
611 |
#>> 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
|
612 |
end |
| 56945 | 613 |
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
|
614 |
|
|
58112
8081087096ad
renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents:
57633
diff
changeset
|
615 |
val add_primrec = gen_primrec Old_Primrec.add_primrec Specification.check_spec []; |
|
8081087096ad
renamed modules defining old datatypes, as a step towards having 'datatype_new' take 'datatype's place
blanchet
parents:
57633
diff
changeset
|
616 |
val add_primrec_cmd = gen_primrec Old_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
|
617 |
|
|
55535
10194808430d
name derivations in 'primrec' for code extraction from proof terms
blanchet
parents:
55530
diff
changeset
|
618 |
fun add_primrec_global fixes specs = |
|
10194808430d
name derivations in 'primrec' for code extraction from proof terms
blanchet
parents:
55530
diff
changeset
|
619 |
Named_Target.theory_init |
|
10194808430d
name derivations in 'primrec' for code extraction from proof terms
blanchet
parents:
55530
diff
changeset
|
620 |
#> add_primrec fixes specs |
|
10194808430d
name derivations in 'primrec' for code extraction from proof terms
blanchet
parents:
55530
diff
changeset
|
621 |
##> 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
|
622 |
|
|
55535
10194808430d
name derivations in 'primrec' for code extraction from proof terms
blanchet
parents:
55530
diff
changeset
|
623 |
fun add_primrec_overloaded ops fixes specs = |
|
10194808430d
name derivations in 'primrec' for code extraction from proof terms
blanchet
parents:
55530
diff
changeset
|
624 |
Overloading.overloading ops |
|
10194808430d
name derivations in 'primrec' for code extraction from proof terms
blanchet
parents:
55530
diff
changeset
|
625 |
#> add_primrec fixes specs |
|
10194808430d
name derivations in 'primrec' for code extraction from proof terms
blanchet
parents:
55530
diff
changeset
|
626 |
##> 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
|
627 |
|
| 56121 | 628 |
val primrec_option_parser = Parse.group (fn () => "option") |
629 |
(Parse.reserved "nonexhaustive" >> K Nonexhaustive_Option) |
|
630 |
||
|
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
|
631 |
val _ = Outer_Syntax.local_theory @{command_spec "primrec"}
|
| 55529 | 632 |
"define primitive recursive functions" |
| 56121 | 633 |
((Scan.optional (@{keyword "("} |--
|
634 |
Parse.!!! (Parse.list1 primrec_option_parser) --| @{keyword ")"}) []) --
|
|
635 |
(Parse.fixes -- Parse_Spec.where_alt_specs) |
|
| 56945 | 636 |
>> (fn (opts, (fixes, specs)) => snd o add_primrec_cmd opts fixes specs)); |
| 55529 | 637 |
|
|
54246
8fdb4dc08ed1
split 'primrec_new' and 'primcorec' code (to ease bootstrapping, e.g. dependency on datatype 'String' in 'primcorec')
blanchet
parents:
diff
changeset
|
638 |
end; |