8 *) |
8 *) |
9 |
9 |
10 signature FUNDEF_PACKAGE = |
10 signature FUNDEF_PACKAGE = |
11 sig |
11 sig |
12 val add_fundef : (string * string option * mixfix) list |
12 val add_fundef : (string * string option * mixfix) list |
13 -> ((bstring * Attrib.src list) * (string * bool)) list |
13 -> ((bstring * Attrib.src list) * string) list |
14 -> FundefCommon.fundef_config |
14 -> FundefCommon.fundef_config |
|
15 -> bool list |
15 -> local_theory |
16 -> local_theory |
16 -> Proof.state |
17 -> Proof.state |
17 |
18 |
18 val add_fundef_i: (string * typ option * mixfix) list |
19 val add_fundef_i: (string * typ option * mixfix) list |
19 -> ((bstring * Attrib.src list) * (term * bool)) list |
20 -> ((bstring * Attrib.src list) * term) list |
20 -> FundefCommon.fundef_config |
21 -> FundefCommon.fundef_config |
|
22 -> bool list |
21 -> local_theory |
23 -> local_theory |
22 -> Proof.state |
24 -> Proof.state |
23 |
25 |
24 val setup_termination_proof : string option -> local_theory -> Proof.state |
26 val setup_termination_proof : string option -> local_theory -> Proof.state |
25 |
27 |
30 val setup_case_cong_hook : theory -> theory |
32 val setup_case_cong_hook : theory -> theory |
31 val get_congs : theory -> thm list |
33 val get_congs : theory -> thm list |
32 end |
34 end |
33 |
35 |
34 |
36 |
35 structure FundefPackage (*: FUNDEF_PACKAGE*) = |
37 structure FundefPackage : FUNDEF_PACKAGE = |
36 struct |
38 struct |
37 |
39 |
38 open FundefLib |
40 open FundefLib |
39 open FundefCommon |
41 open FundefCommon |
40 |
42 |
41 val note_theorem = LocalTheory.note Thm.theoremK |
43 val note_theorem = LocalTheory.note Thm.theoremK |
42 |
44 |
43 fun mk_defname fixes = fixes |> map (fst o fst) |> space_implode "_" |
45 fun mk_defname fixes = fixes |> map (fst o fst) |> space_implode "_" |
44 |
46 |
45 |
47 fun add_simps fixes post sort label moreatts simps lthy = |
46 (* Check for all sorts of errors in the input *) |
|
47 fun check_def ctxt fixes eqs = |
|
48 let |
48 let |
49 val fnames = map (fst o fst) fixes |
49 val fnames = map (fst o fst) fixes |
50 |
|
51 fun check geq = |
|
52 let |
|
53 fun input_error msg = cat_lines [msg, ProofContext.string_of_term ctxt geq] |
|
54 |
|
55 val fqgar as (fname, qs, gs, args, rhs) = split_def geq |
|
56 |
|
57 val _ = fname mem fnames |
|
58 orelse error (input_error ("Head symbol of left hand side must be " ^ plural "" "one out of " fnames |
|
59 ^ commas_quote fnames)) |
|
60 |
|
61 fun add_bvs t is = add_loose_bnos (t, 0, is) |
|
62 val rvs = (add_bvs rhs [] \\ fold add_bvs args []) |
|
63 |> map (fst o nth (rev qs)) |
|
64 |
|
65 val _ = null rvs orelse error (input_error ("Variable" ^ plural " " "s " rvs ^ commas_quote rvs |
|
66 ^ " occur" ^ plural "s" "" rvs ^ " on right hand side only:")) |
|
67 |
|
68 val _ = forall (forall_aterms (fn Free (n, _) => not (n mem fnames) | _ => true)) gs orelse |
|
69 error (input_error "Recursive Calls not allowed in premises") |
|
70 in |
|
71 fqgar |
|
72 end |
|
73 in |
|
74 (mk_arities (map check eqs); ()) |
|
75 handle ArgumentCount fname => |
|
76 error ("Function " ^ quote fname ^ " has different numbers of arguments in different equations") |
|
77 end |
|
78 |
|
79 |
|
80 fun mk_catchall fixes arities = |
|
81 let |
|
82 fun mk_eqn ((fname, fT), _) = |
|
83 let |
|
84 val n = the (Symtab.lookup arities fname) |
|
85 val (argTs, rT) = chop n (binder_types fT) |
|
86 |> apsnd (fn Ts => Ts ---> body_type fT) |
|
87 |
|
88 val qs = map Free (Name.invent_list [] "a" n ~~ argTs) |
|
89 in |
|
90 HOLogic.mk_eq(list_comb (Free (fname, fT), qs), |
|
91 Const ("HOL.undefined", rT)) |
|
92 |> HOLogic.mk_Trueprop |
|
93 |> fold_rev mk_forall qs |
|
94 end |
|
95 in |
|
96 map mk_eqn fixes |
|
97 end |
|
98 |
|
99 fun add_catchall fixes spec = |
|
100 let |
|
101 val catchalls = mk_catchall fixes (mk_arities (map split_def (map (snd o snd) spec))) |
|
102 in |
|
103 spec @ map (pair ("",[]) o pair true) catchalls |
|
104 end |
|
105 |
|
106 fun burrow_snd f ps = (* ('a list -> 'b list) -> ('c * 'a) list -> ('c * 'b) list *) |
|
107 let val (xs, ys) = split_list ps |
|
108 in xs ~~ f ys end |
|
109 |
|
110 fun restore_spec_structure reps spec = |
|
111 (burrow_snd o burrow o K) reps spec |
|
112 |
|
113 fun add_simps fixes spec sort label moreatts simps lthy = |
|
114 let |
|
115 val fnames = map (fst o fst) fixes |
|
116 |
50 |
117 val (saved_spec_simps, lthy) = |
51 val (saved_spec_simps, lthy) = |
118 fold_map note_theorem (restore_spec_structure simps spec) lthy |
52 fold_map note_theorem (post simps) lthy |
119 val saved_simps = flat (map snd saved_spec_simps) |
53 val saved_simps = flat (map snd saved_spec_simps) |
120 |
54 |
121 val simps_by_f = sort saved_simps |
55 val simps_by_f = sort saved_simps |
122 |
56 |
123 fun add_for_f fname simps = |
57 fun add_for_f fname simps = |
127 in |
61 in |
128 (saved_simps, |
62 (saved_simps, |
129 fold2 add_for_f fnames simps_by_f lthy) |
63 fold2 add_for_f fnames simps_by_f lthy) |
130 end |
64 end |
131 |
65 |
132 fun fundef_afterqed config fixes spec defname cont sort_cont [[proof]] lthy = |
66 fun fundef_afterqed config fixes post defname cont sort_cont [[proof]] lthy = |
133 let |
67 let |
134 val FundefResult {fs, R, psimps, trsimps, subset_pinducts, simple_pinducts, termination, domintros, cases, ...} = |
68 val FundefResult {fs, R, psimps, trsimps, subset_pinducts, simple_pinducts, termination, domintros, cases, ...} = |
135 cont (Goal.close_result proof) |
69 cont (Goal.close_result proof) |
136 |
70 |
137 val qualify = NameSpace.qualified defname |
71 val qualify = NameSpace.qualified defname |
138 val addsmps = add_simps fixes spec sort_cont |
72 val addsmps = add_simps fixes post sort_cont |
139 |
73 |
140 val (((psimps', pinducts'), (_, [termination'])), lthy) = |
74 val (((psimps', pinducts'), (_, [termination'])), lthy) = |
141 lthy |
75 lthy |
142 |> addsmps "psimps" [] psimps |
76 |> addsmps "psimps" [] psimps |
143 ||> fold_option (snd oo addsmps "simps" []) trsimps |
77 ||> fold_option (snd oo addsmps "simps" []) trsimps |
155 |> LocalTheory.declaration (fn phi => add_fundef_data (morph_fundef_data phi cdata)) (* save in target *) |
89 |> LocalTheory.declaration (fn phi => add_fundef_data (morph_fundef_data phi cdata)) (* save in target *) |
156 |> Context.proof_map (add_fundef_data cdata') (* also save in local context *) |
90 |> Context.proof_map (add_fundef_data cdata') (* also save in local context *) |
157 end (* FIXME: Add cases for induct and cases thm *) |
91 end (* FIXME: Add cases for induct and cases thm *) |
158 |
92 |
159 |
93 |
160 |
94 fun prepare_spec prep fixspec eqnss lthy = (* FIXME: obsolete when "read_specification" etc. is there *) |
161 fun prep_with_flags prep fixspec eqnss_flags global_flag lthy = |
95 let |
162 let |
96 val eqns = map (apsnd single) eqnss |
163 val flags = map (fn x => global_flag orelse (snd (snd x))) eqnss_flags |
97 |
164 val eqns = map (apsnd (single o fst)) eqnss_flags |
98 val ((fixes, _), ctxt') = prep fixspec [] lthy |
165 |
|
166 val ((fixes, _), ctxt') = prep fixspec [] lthy |
|
167 |
|
168 fun prep_eqn e = the_single (snd (fst (prep [] [e] ctxt'))) |
99 fun prep_eqn e = the_single (snd (fst (prep [] [e] ctxt'))) |
169 |> apsnd the_single |
100 |
170 |
101 val spec = map prep_eqn eqns |
171 val raw_spec = map prep_eqn eqns |
102 |> map (apsnd (map (fn t => fold_rev (mk_forall o Free) (frees_in_term ctxt' t) t))) (* Add quantifiers *) |
172 |> map (apsnd (fn t => fold_rev (mk_forall o Free) (frees_in_term ctxt' t) t)) (* Add quantifiers *) |
|
173 |
|
174 val _ = check_def ctxt' fixes (map snd raw_spec) |
|
175 |
|
176 val spec = raw_spec |
|
177 |> burrow_snd (fn ts => flags ~~ ts) |
|
178 (*|> (if global_flag then add_catchall fixes else I) *) (* Completion: still disabled *) |
|
179 |> burrow_snd (FundefSplit.split_some_equations ctxt') |
|
180 |
|
181 in |
103 in |
182 ((fixes, spec), ctxt') |
104 ((fixes, spec), ctxt') |
183 end |
105 end |
184 |
106 |
185 fun gen_add_fundef prep_spec fixspec eqnss_flags config lthy = |
107 fun gen_add_fundef prep fixspec eqnss config flags lthy = |
186 let |
108 let |
187 val FundefConfig {sequential, ...} = config |
109 val ((fixes, spec), ctxt') = prepare_spec prep fixspec eqnss lthy |
188 |
110 val (eqs, post) = FundefCommon.get_preproc lthy config flags ctxt' fixes spec |
189 val ((fixes, spec), ctxt') = prep_with_flags prep_spec fixspec eqnss_flags sequential lthy |
|
190 |
111 |
191 val defname = mk_defname fixes |
112 val defname = mk_defname fixes |
192 |
113 |
193 val t_eqns = spec |> map snd |> flat (* flatten external structure *) |
|
194 |
|
195 val ((goalstate, cont, sort_cont), lthy) = |
114 val ((goalstate, cont, sort_cont), lthy) = |
196 FundefMutual.prepare_fundef_mutual config defname fixes t_eqns lthy |
115 FundefMutual.prepare_fundef_mutual config defname fixes eqs lthy |
197 |
116 |
198 val afterqed = fundef_afterqed config fixes spec defname cont sort_cont |
117 val afterqed = fundef_afterqed config fixes post defname cont sort_cont |
199 in |
118 in |
200 lthy |
119 lthy |
201 |> Proof.theorem_i NONE afterqed [[(Logic.unprotect (concl_of goalstate), [])]] |
120 |> Proof.theorem_i NONE afterqed [[(Logic.unprotect (concl_of goalstate), [])]] |
202 |> Proof.refine (Method.primitive_text (fn _ => goalstate)) |> Seq.hd |
121 |> Proof.refine (Method.primitive_text (fn _ => goalstate)) |> Seq.hd |
203 end |
122 end |
204 |
123 |
205 |
|
206 fun total_termination_afterqed data [[totality]] lthy = |
124 fun total_termination_afterqed data [[totality]] lthy = |
207 let |
125 let |
208 val FundefCtxData { add_simps, psimps, pinducts, defname, ... } = data |
126 val FundefCtxData { add_simps, psimps, pinducts, defname, ... } = data |
209 |
127 |
210 val totality = Goal.close_result totality |
128 val totality = Goal.close_result totality |
212 val remove_domain_condition = full_simplify (HOL_basic_ss addsimps [totality, True_implies_equals]) |
130 val remove_domain_condition = full_simplify (HOL_basic_ss addsimps [totality, True_implies_equals]) |
213 |
131 |
214 val tsimps = map remove_domain_condition psimps |
132 val tsimps = map remove_domain_condition psimps |
215 val tinduct = map remove_domain_condition pinducts |
133 val tinduct = map remove_domain_condition pinducts |
216 |
134 |
217 (* FIXME: How to generate code from (possibly) local contexts*) |
|
218 val has_guards = exists ((fn (Const ("Trueprop", _) $ _) => false | _ => true) o prop_of) tsimps |
135 val has_guards = exists ((fn (Const ("Trueprop", _) $ _) => false | _ => true) o prop_of) tsimps |
219 val allatts = if has_guards then [] else [Attrib.internal (K (RecfunCodegen.add NONE))] |
136 val allatts = if has_guards then [] else [Attrib.internal (K (RecfunCodegen.add NONE))] |
220 |
137 |
221 val qualify = NameSpace.qualified defname; |
138 val qualify = NameSpace.qualified defname; |
222 in |
139 in |
291 local structure P = OuterParse and K = OuterKeyword in |
208 local structure P = OuterParse and K = OuterKeyword in |
292 |
209 |
293 val functionP = |
210 val functionP = |
294 OuterSyntax.command "function" "define general recursive functions" K.thy_goal |
211 OuterSyntax.command "function" "define general recursive functions" K.thy_goal |
295 (fundef_parser default_config |
212 (fundef_parser default_config |
296 >> (fn ((config, fixes), statements) => |
213 >> (fn ((config, fixes), (flags, statements)) => |
297 Toplevel.local_theory_to_proof (target_of config) (add_fundef fixes statements config) |
214 Toplevel.local_theory_to_proof (target_of config) (add_fundef fixes statements config flags) |
298 #> Toplevel.print)); |
215 #> Toplevel.print)); |
299 |
216 |
300 val terminationP = |
217 val terminationP = |
301 OuterSyntax.command "termination" "prove termination of a recursive function" K.thy_goal |
218 OuterSyntax.command "termination" "prove termination of a recursive function" K.thy_goal |
302 (P.opt_target -- Scan.option P.term |
219 (P.opt_target -- Scan.option P.term |