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