wenzelm@6429
|
1 |
(* Title: HOL/Tools/recdef_package.ML
|
wenzelm@6429
|
2 |
ID: $Id$
|
wenzelm@6429
|
3 |
Author: Markus Wenzel, TU Muenchen
|
wenzelm@6429
|
4 |
|
wenzelm@6429
|
5 |
Wrapper module for Konrad Slind's TFL package.
|
wenzelm@6429
|
6 |
*)
|
wenzelm@6429
|
7 |
|
wenzelm@6429
|
8 |
signature RECDEF_PACKAGE =
|
wenzelm@6429
|
9 |
sig
|
wenzelm@6429
|
10 |
val quiet_mode: bool ref
|
wenzelm@6439
|
11 |
val print_recdefs: theory -> unit
|
wenzelm@8657
|
12 |
val get_recdef: theory -> string
|
wenzelm@8657
|
13 |
-> {simps: thm list, rules: thm list list, induct: thm, tcs: term list} option
|
wenzelm@9859
|
14 |
val simp_add_global: theory attribute
|
wenzelm@9859
|
15 |
val simp_del_global: theory attribute
|
wenzelm@9859
|
16 |
val cong_add_global: theory attribute
|
wenzelm@9859
|
17 |
val cong_del_global: theory attribute
|
wenzelm@9859
|
18 |
val wf_add_global: theory attribute
|
wenzelm@9859
|
19 |
val wf_del_global: theory attribute
|
wenzelm@9859
|
20 |
val simp_add_local: Proof.context attribute
|
wenzelm@9859
|
21 |
val simp_del_local: Proof.context attribute
|
wenzelm@9859
|
22 |
val cong_add_local: Proof.context attribute
|
wenzelm@9859
|
23 |
val cong_del_local: Proof.context attribute
|
wenzelm@9859
|
24 |
val wf_add_local: Proof.context attribute
|
wenzelm@9859
|
25 |
val wf_del_local: Proof.context attribute
|
wenzelm@15703
|
26 |
val add_recdef: bool -> xstring -> string -> ((bstring * string) * Attrib.src list) list ->
|
wenzelm@15703
|
27 |
Attrib.src option -> theory -> theory
|
wenzelm@9859
|
28 |
* {simps: thm list, rules: thm list list, induct: thm, tcs: term list}
|
wenzelm@11629
|
29 |
val add_recdef_i: bool -> xstring -> term -> ((bstring * term) * theory attribute list) list ->
|
wenzelm@9859
|
30 |
theory -> theory * {simps: thm list, rules: thm list list, induct: thm, tcs: term list}
|
wenzelm@15703
|
31 |
val defer_recdef: xstring -> string list -> (thmref * Attrib.src list) list
|
wenzelm@6557
|
32 |
-> theory -> theory * {induct_rules: thm}
|
wenzelm@12708
|
33 |
val defer_recdef_i: xstring -> term list -> (thm list * theory attribute list) list
|
wenzelm@6557
|
34 |
-> theory -> theory * {induct_rules: thm}
|
wenzelm@17336
|
35 |
val recdef_tc: bstring * Attrib.src list -> xstring -> int option -> theory -> Proof.state
|
wenzelm@10775
|
36 |
val recdef_tc_i: bstring * theory attribute list -> string -> int option
|
wenzelm@17336
|
37 |
-> theory -> Proof.state
|
wenzelm@6439
|
38 |
val setup: (theory -> theory) list
|
wenzelm@6429
|
39 |
end;
|
wenzelm@6429
|
40 |
|
wenzelm@6429
|
41 |
structure RecdefPackage: RECDEF_PACKAGE =
|
wenzelm@6429
|
42 |
struct
|
wenzelm@6429
|
43 |
|
wenzelm@9859
|
44 |
|
wenzelm@6429
|
45 |
val quiet_mode = Tfl.quiet_mode;
|
wenzelm@6429
|
46 |
val message = Tfl.message;
|
wenzelm@6429
|
47 |
|
wenzelm@6429
|
48 |
|
wenzelm@9859
|
49 |
(** recdef hints **)
|
wenzelm@6429
|
50 |
|
wenzelm@9859
|
51 |
(* type hints *)
|
wenzelm@6439
|
52 |
|
wenzelm@9859
|
53 |
type hints = {simps: thm list, congs: (string * thm) list, wfs: thm list};
|
wenzelm@9859
|
54 |
|
wenzelm@9859
|
55 |
fun mk_hints (simps, congs, wfs) = {simps = simps, congs = congs, wfs = wfs}: hints;
|
wenzelm@9859
|
56 |
fun map_hints f ({simps, congs, wfs}: hints) = mk_hints (f (simps, congs, wfs));
|
wenzelm@9859
|
57 |
|
wenzelm@9859
|
58 |
fun map_simps f = map_hints (fn (simps, congs, wfs) => (f simps, congs, wfs));
|
wenzelm@9859
|
59 |
fun map_congs f = map_hints (fn (simps, congs, wfs) => (simps, f congs, wfs));
|
wenzelm@9859
|
60 |
fun map_wfs f = map_hints (fn (simps, congs, wfs) => (simps, congs, f wfs));
|
wenzelm@9859
|
61 |
|
wenzelm@9859
|
62 |
fun pretty_hints ({simps, congs, wfs}: hints) =
|
wenzelm@9859
|
63 |
[Pretty.big_list "recdef simp hints:" (map Display.pretty_thm simps),
|
wenzelm@9859
|
64 |
Pretty.big_list "recdef cong hints:" (map Display.pretty_thm (map #2 congs)),
|
wenzelm@9859
|
65 |
Pretty.big_list "recdef wf hints:" (map Display.pretty_thm wfs)];
|
wenzelm@9859
|
66 |
|
wenzelm@9859
|
67 |
|
wenzelm@9859
|
68 |
(* congruence rules *)
|
wenzelm@9859
|
69 |
|
wenzelm@9859
|
70 |
local
|
wenzelm@9859
|
71 |
|
wenzelm@9859
|
72 |
val cong_head =
|
wenzelm@9859
|
73 |
fst o Term.dest_Const o Term.head_of o fst o Logic.dest_equals o Thm.concl_of;
|
wenzelm@9859
|
74 |
|
wenzelm@9859
|
75 |
fun prep_cong raw_thm =
|
wenzelm@9859
|
76 |
let val thm = safe_mk_meta_eq raw_thm in (cong_head thm, thm) end;
|
wenzelm@9859
|
77 |
|
wenzelm@9859
|
78 |
in
|
wenzelm@9859
|
79 |
|
wenzelm@9859
|
80 |
fun add_cong raw_thm congs =
|
wenzelm@9859
|
81 |
let val (c, thm) = prep_cong raw_thm
|
haftmann@17593
|
82 |
in overwrite_warn (congs, (c, thm)) ("Overwriting recdef congruence rule for " ^ quote c) end;
|
wenzelm@9859
|
83 |
|
wenzelm@9859
|
84 |
fun del_cong raw_thm congs =
|
wenzelm@9859
|
85 |
let
|
wenzelm@9859
|
86 |
val (c, thm) = prep_cong raw_thm;
|
wenzelm@9859
|
87 |
val (del, rest) = Library.partition (Library.equal c o fst) congs;
|
wenzelm@9859
|
88 |
in if null del then (warning ("No recdef congruence rule for " ^ quote c); congs) else rest end;
|
wenzelm@9859
|
89 |
|
skalberg@15574
|
90 |
val add_congs = foldr (uncurry add_cong);
|
wenzelm@9859
|
91 |
|
wenzelm@9859
|
92 |
end;
|
wenzelm@9859
|
93 |
|
wenzelm@9859
|
94 |
|
wenzelm@9859
|
95 |
|
wenzelm@9859
|
96 |
(** global and local recdef data **)
|
wenzelm@9859
|
97 |
|
wenzelm@17920
|
98 |
(* theory data *)
|
wenzelm@6439
|
99 |
|
wenzelm@8657
|
100 |
type recdef_info = {simps: thm list, rules: thm list list, induct: thm, tcs: term list};
|
wenzelm@6439
|
101 |
|
wenzelm@16458
|
102 |
structure GlobalRecdefData = TheoryDataFun
|
wenzelm@16458
|
103 |
(struct
|
wenzelm@6439
|
104 |
val name = "HOL/recdef";
|
wenzelm@9859
|
105 |
type T = recdef_info Symtab.table * hints;
|
wenzelm@6439
|
106 |
|
wenzelm@9879
|
107 |
val empty = (Symtab.empty, mk_hints ([], [], [])): T;
|
wenzelm@6557
|
108 |
val copy = I;
|
wenzelm@16458
|
109 |
val extend = I;
|
wenzelm@16458
|
110 |
fun merge _
|
wenzelm@9859
|
111 |
((tab1, {simps = simps1, congs = congs1, wfs = wfs1}),
|
wenzelm@16458
|
112 |
(tab2, {simps = simps2, congs = congs2, wfs = wfs2})) : T =
|
wenzelm@9859
|
113 |
(Symtab.merge (K true) (tab1, tab2),
|
wenzelm@9859
|
114 |
mk_hints (Drule.merge_rules (simps1, simps2),
|
wenzelm@9859
|
115 |
Library.merge_alists congs1 congs2,
|
wenzelm@9859
|
116 |
Drule.merge_rules (wfs1, wfs2)));
|
wenzelm@6439
|
117 |
|
wenzelm@16458
|
118 |
fun print thy (tab, hints) =
|
wenzelm@16458
|
119 |
(Pretty.strs ("recdefs:" :: map #1 (NameSpace.extern_table (Sign.const_space thy, tab))) ::
|
wenzelm@16364
|
120 |
pretty_hints hints) |> Pretty.chunks |> Pretty.writeln;
|
wenzelm@16458
|
121 |
end);
|
wenzelm@6439
|
122 |
|
wenzelm@9859
|
123 |
val print_recdefs = GlobalRecdefData.print;
|
wenzelm@6439
|
124 |
|
wenzelm@6439
|
125 |
|
wenzelm@17412
|
126 |
val get_recdef = Symtab.lookup o #1 o GlobalRecdefData.get;
|
wenzelm@6439
|
127 |
|
wenzelm@6439
|
128 |
fun put_recdef name info thy =
|
wenzelm@6429
|
129 |
let
|
wenzelm@9859
|
130 |
val (tab, hints) = GlobalRecdefData.get thy;
|
wenzelm@17412
|
131 |
val tab' = Symtab.update_new (name, info) tab
|
wenzelm@6439
|
132 |
handle Symtab.DUP _ => error ("Duplicate recursive function definition " ^ quote name);
|
wenzelm@9859
|
133 |
in GlobalRecdefData.put (tab', hints) thy end;
|
wenzelm@9859
|
134 |
|
wenzelm@9859
|
135 |
val get_global_hints = #2 o GlobalRecdefData.get;
|
wenzelm@9859
|
136 |
val map_global_hints = GlobalRecdefData.map o apsnd;
|
wenzelm@9859
|
137 |
|
wenzelm@9859
|
138 |
|
wenzelm@17920
|
139 |
(* proof data *)
|
wenzelm@9859
|
140 |
|
wenzelm@16458
|
141 |
structure LocalRecdefData = ProofDataFun
|
wenzelm@16458
|
142 |
(struct
|
wenzelm@9859
|
143 |
val name = "HOL/recdef";
|
wenzelm@9859
|
144 |
type T = hints;
|
wenzelm@9859
|
145 |
val init = get_global_hints;
|
wenzelm@9859
|
146 |
fun print _ hints = pretty_hints hints |> Pretty.chunks |> Pretty.writeln;
|
wenzelm@16458
|
147 |
end);
|
wenzelm@9859
|
148 |
|
wenzelm@9859
|
149 |
val get_local_hints = LocalRecdefData.get;
|
wenzelm@9859
|
150 |
val map_local_hints = LocalRecdefData.map;
|
wenzelm@9859
|
151 |
|
wenzelm@9859
|
152 |
|
wenzelm@9859
|
153 |
(* attributes *)
|
wenzelm@9859
|
154 |
|
wenzelm@9859
|
155 |
local
|
wenzelm@9859
|
156 |
|
wenzelm@9859
|
157 |
fun global_local f g =
|
wenzelm@9859
|
158 |
(fn (thy, thm) => (map_global_hints (f (g thm)) thy, thm),
|
wenzelm@9859
|
159 |
fn (ctxt, thm) => (map_local_hints (f (g thm)) ctxt, thm));
|
wenzelm@9859
|
160 |
|
wenzelm@9859
|
161 |
fun mk_attr (add1, add2) (del1, del2) =
|
wenzelm@9859
|
162 |
(Attrib.add_del_args add1 del1, Attrib.add_del_args add2 del2);
|
wenzelm@9859
|
163 |
|
wenzelm@9859
|
164 |
in
|
wenzelm@9859
|
165 |
|
wenzelm@12371
|
166 |
val (simp_add_global, simp_add_local) = global_local map_simps Drule.add_rule;
|
wenzelm@12371
|
167 |
val (simp_del_global, simp_del_local) = global_local map_simps Drule.del_rule;
|
wenzelm@9859
|
168 |
val (cong_add_global, cong_add_local) = global_local map_congs add_cong;
|
wenzelm@9859
|
169 |
val (cong_del_global, cong_del_local) = global_local map_congs del_cong;
|
wenzelm@12371
|
170 |
val (wf_add_global, wf_add_local) = global_local map_wfs Drule.add_rule;
|
wenzelm@12371
|
171 |
val (wf_del_global, wf_del_local) = global_local map_wfs Drule.del_rule;
|
wenzelm@9859
|
172 |
|
wenzelm@9859
|
173 |
val simp_attr = mk_attr (simp_add_global, simp_add_local) (simp_del_global, simp_del_local);
|
wenzelm@9859
|
174 |
val cong_attr = mk_attr (cong_add_global, cong_add_local) (cong_del_global, cong_del_local);
|
wenzelm@9859
|
175 |
val wf_attr = mk_attr (wf_add_global, wf_add_local) (wf_del_global, wf_del_local);
|
wenzelm@9859
|
176 |
|
wenzelm@9859
|
177 |
end;
|
wenzelm@9859
|
178 |
|
wenzelm@9859
|
179 |
|
wenzelm@9949
|
180 |
(* modifiers *)
|
wenzelm@9949
|
181 |
|
wenzelm@9949
|
182 |
val recdef_simpN = "recdef_simp";
|
wenzelm@9949
|
183 |
val recdef_congN = "recdef_cong";
|
wenzelm@9949
|
184 |
val recdef_wfN = "recdef_wf";
|
wenzelm@9949
|
185 |
|
wenzelm@9949
|
186 |
val recdef_modifiers =
|
wenzelm@9949
|
187 |
[Args.$$$ recdef_simpN -- Args.colon >> K ((I, simp_add_local): Method.modifier),
|
wenzelm@10032
|
188 |
Args.$$$ recdef_simpN -- Args.add -- Args.colon >> K (I, simp_add_local),
|
wenzelm@10032
|
189 |
Args.$$$ recdef_simpN -- Args.del -- Args.colon >> K (I, simp_del_local),
|
wenzelm@9949
|
190 |
Args.$$$ recdef_congN -- Args.colon >> K (I, cong_add_local),
|
wenzelm@10032
|
191 |
Args.$$$ recdef_congN -- Args.add -- Args.colon >> K (I, cong_add_local),
|
wenzelm@10032
|
192 |
Args.$$$ recdef_congN -- Args.del -- Args.colon >> K (I, cong_del_local),
|
wenzelm@9949
|
193 |
Args.$$$ recdef_wfN -- Args.colon >> K (I, wf_add_local),
|
wenzelm@10032
|
194 |
Args.$$$ recdef_wfN -- Args.add -- Args.colon >> K (I, wf_add_local),
|
wenzelm@10032
|
195 |
Args.$$$ recdef_wfN -- Args.del -- Args.colon >> K (I, wf_del_local)] @
|
wenzelm@9949
|
196 |
Clasimp.clasimp_modifiers;
|
wenzelm@9949
|
197 |
|
wenzelm@9949
|
198 |
|
wenzelm@9859
|
199 |
|
wenzelm@9859
|
200 |
(** prepare_hints(_i) **)
|
wenzelm@9859
|
201 |
|
wenzelm@9859
|
202 |
fun prepare_hints thy opt_src =
|
wenzelm@9859
|
203 |
let
|
wenzelm@9859
|
204 |
val ctxt0 = ProofContext.init thy;
|
wenzelm@9859
|
205 |
val ctxt =
|
wenzelm@9859
|
206 |
(case opt_src of
|
skalberg@15531
|
207 |
NONE => ctxt0
|
skalberg@15531
|
208 |
| SOME src => Method.only_sectioned_args recdef_modifiers I src ctxt0);
|
wenzelm@9859
|
209 |
val {simps, congs, wfs} = get_local_hints ctxt;
|
wenzelm@15032
|
210 |
val cs = local_claset_of ctxt;
|
wenzelm@15032
|
211 |
val ss = local_simpset_of ctxt addsimps simps;
|
wenzelm@9859
|
212 |
in (cs, ss, map #2 congs, wfs) end;
|
wenzelm@9859
|
213 |
|
wenzelm@9859
|
214 |
fun prepare_hints_i thy () =
|
wenzelm@15032
|
215 |
let
|
wenzelm@15032
|
216 |
val ctxt0 = ProofContext.init thy;
|
wenzelm@15032
|
217 |
val {simps, congs, wfs} = get_global_hints thy;
|
wenzelm@15032
|
218 |
in (local_claset_of ctxt0, local_simpset_of ctxt0 addsimps simps, map #2 congs, wfs) end;
|
wenzelm@9859
|
219 |
|
wenzelm@6439
|
220 |
|
wenzelm@6439
|
221 |
|
wenzelm@6439
|
222 |
(** add_recdef(_i) **)
|
wenzelm@6439
|
223 |
|
wenzelm@6557
|
224 |
fun requires_recdef thy = Theory.requires thy "Recdef" "recursive functions";
|
wenzelm@6557
|
225 |
|
wenzelm@17920
|
226 |
fun gen_add_recdef tfl_fn prep_att prep_hints not_permissive raw_name R eq_srcs hints thy =
|
wenzelm@6439
|
227 |
let
|
wenzelm@9859
|
228 |
val _ = requires_recdef thy;
|
wenzelm@9859
|
229 |
|
wenzelm@16458
|
230 |
val name = Sign.intern_const thy raw_name;
|
wenzelm@6439
|
231 |
val bname = Sign.base_name name;
|
wenzelm@6429
|
232 |
val _ = message ("Defining recursive function " ^ quote name ^ " ...");
|
wenzelm@6429
|
233 |
|
wenzelm@8657
|
234 |
val ((eq_names, eqs), raw_eq_atts) = apfst split_list (split_list eq_srcs);
|
wenzelm@8657
|
235 |
val eq_atts = map (map (prep_att thy)) raw_eq_atts;
|
wenzelm@8657
|
236 |
|
wenzelm@9859
|
237 |
val (cs, ss, congs, wfs) = prep_hints thy hints;
|
paulson@14241
|
238 |
(*We must remove imp_cong to prevent looping when the induction rule
|
paulson@14241
|
239 |
is simplified. Many induction rules have nested implications that would
|
paulson@14241
|
240 |
give rise to looping conditional rewriting.*)
|
paulson@14241
|
241 |
val (thy, {rules = rules_idx, induct, tcs}) =
|
wenzelm@17920
|
242 |
tfl_fn not_permissive thy cs (ss delcongs [imp_cong])
|
paulson@14241
|
243 |
congs wfs name R eqs;
|
haftmann@17496
|
244 |
val rules = map (map #1) (Library.partition_eq (Library.eq_snd (op =)) rules_idx);
|
berghofe@12448
|
245 |
val simp_att = if null tcs then
|
berghofe@16646
|
246 |
[Simplifier.simp_add_global, RecfunCodegen.add NONE] else [];
|
wenzelm@8657
|
247 |
|
haftmann@18377
|
248 |
val ((simps' :: rules', [induct']), thy) =
|
wenzelm@7798
|
249 |
thy
|
wenzelm@6439
|
250 |
|> Theory.add_path bname
|
skalberg@15570
|
251 |
|> PureThy.add_thmss ((("simps", List.concat rules), simp_att) :: ((eq_names ~~ rules) ~~ eq_atts))
|
haftmann@18377
|
252 |
||>> PureThy.add_thms [(("induct", induct), [])];
|
wenzelm@8657
|
253 |
val result = {simps = simps', rules = rules', induct = induct', tcs = tcs};
|
wenzelm@7798
|
254 |
val thy =
|
wenzelm@7798
|
255 |
thy
|
wenzelm@6439
|
256 |
|> put_recdef name result
|
wenzelm@6429
|
257 |
|> Theory.parent_path;
|
wenzelm@7798
|
258 |
in (thy, result) end;
|
wenzelm@6429
|
259 |
|
wenzelm@9859
|
260 |
val add_recdef = gen_add_recdef Tfl.define Attrib.global_attribute prepare_hints;
|
wenzelm@11629
|
261 |
fun add_recdef_i x y z w = gen_add_recdef Tfl.define_i (K I) prepare_hints_i x y z w ();
|
wenzelm@9859
|
262 |
|
wenzelm@9859
|
263 |
|
wenzelm@6557
|
264 |
|
wenzelm@6557
|
265 |
(** defer_recdef(_i) **)
|
wenzelm@6557
|
266 |
|
wenzelm@6557
|
267 |
fun gen_defer_recdef tfl_fn app_thms raw_name eqs raw_congs thy =
|
wenzelm@6557
|
268 |
let
|
wenzelm@16458
|
269 |
val name = Sign.intern_const thy raw_name;
|
wenzelm@6557
|
270 |
val bname = Sign.base_name name;
|
wenzelm@6557
|
271 |
|
wenzelm@6557
|
272 |
val _ = requires_recdef thy;
|
wenzelm@6557
|
273 |
val _ = message ("Deferred recursive function " ^ quote name ^ " ...");
|
wenzelm@6557
|
274 |
|
wenzelm@6557
|
275 |
val (thy1, congs) = thy |> app_thms raw_congs;
|
wenzelm@9859
|
276 |
val (thy2, induct_rules) = tfl_fn thy1 congs name eqs;
|
haftmann@18377
|
277 |
val ([induct_rules'], thy3) =
|
wenzelm@6557
|
278 |
thy2
|
wenzelm@6557
|
279 |
|> Theory.add_path bname
|
wenzelm@6557
|
280 |
|> PureThy.add_thms [(("induct_rules", induct_rules), [])]
|
haftmann@18377
|
281 |
||> Theory.parent_path;
|
wenzelm@8430
|
282 |
in (thy3, {induct_rules = induct_rules'}) end;
|
wenzelm@6557
|
283 |
|
wenzelm@6557
|
284 |
val defer_recdef = gen_defer_recdef Tfl.defer IsarThy.apply_theorems;
|
wenzelm@6557
|
285 |
val defer_recdef_i = gen_defer_recdef Tfl.defer_i IsarThy.apply_theorems_i;
|
wenzelm@6557
|
286 |
|
wenzelm@6557
|
287 |
|
wenzelm@6557
|
288 |
|
wenzelm@10775
|
289 |
(** recdef_tc(_i) **)
|
wenzelm@10775
|
290 |
|
wenzelm@17336
|
291 |
fun gen_recdef_tc prep_att prep_name (bname, raw_atts) raw_name opt_i thy =
|
wenzelm@10775
|
292 |
let
|
wenzelm@16458
|
293 |
val name = prep_name thy raw_name;
|
wenzelm@10775
|
294 |
val atts = map (prep_att thy) raw_atts;
|
wenzelm@10775
|
295 |
val tcs =
|
wenzelm@10775
|
296 |
(case get_recdef thy name of
|
skalberg@15531
|
297 |
NONE => error ("No recdef definition of constant: " ^ quote name)
|
skalberg@15531
|
298 |
| SOME {tcs, ...} => tcs);
|
skalberg@15570
|
299 |
val i = getOpt (opt_i, 1);
|
skalberg@15570
|
300 |
val tc = List.nth (tcs, i - 1) handle Subscript =>
|
wenzelm@10775
|
301 |
error ("No termination condition #" ^ string_of_int i ^
|
wenzelm@10775
|
302 |
" in recdef definition of " ^ quote name);
|
wenzelm@17336
|
303 |
in IsarThy.theorem_i Drule.internalK (bname, atts) (HOLogic.mk_Trueprop tc, ([], [])) thy end;
|
wenzelm@10775
|
304 |
|
wenzelm@10775
|
305 |
val recdef_tc = gen_recdef_tc Attrib.global_attribute Sign.intern_const;
|
wenzelm@10775
|
306 |
val recdef_tc_i = gen_recdef_tc (K I) (K I);
|
wenzelm@10775
|
307 |
|
wenzelm@10775
|
308 |
|
wenzelm@10775
|
309 |
|
wenzelm@6439
|
310 |
(** package setup **)
|
wenzelm@6439
|
311 |
|
wenzelm@6439
|
312 |
(* setup theory *)
|
wenzelm@6439
|
313 |
|
wenzelm@9859
|
314 |
val setup =
|
wenzelm@9859
|
315 |
[GlobalRecdefData.init, LocalRecdefData.init,
|
wenzelm@9859
|
316 |
Attrib.add_attributes
|
wenzelm@9949
|
317 |
[(recdef_simpN, simp_attr, "declaration of recdef simp rule"),
|
wenzelm@9949
|
318 |
(recdef_congN, cong_attr, "declaration of recdef cong rule"),
|
wenzelm@9949
|
319 |
(recdef_wfN, wf_attr, "declaration of recdef wf rule")]];
|
wenzelm@6439
|
320 |
|
wenzelm@6439
|
321 |
|
wenzelm@6429
|
322 |
(* outer syntax *)
|
wenzelm@6429
|
323 |
|
wenzelm@17057
|
324 |
local structure P = OuterParse and K = OuterKeyword in
|
wenzelm@6429
|
325 |
|
wenzelm@9859
|
326 |
val hints =
|
wenzelm@9859
|
327 |
P.$$$ "(" |-- P.!!! (P.position (P.$$$ "hints" -- P.arguments) --| P.$$$ ")") >> Args.src;
|
wenzelm@9859
|
328 |
|
wenzelm@6429
|
329 |
val recdef_decl =
|
wenzelm@11629
|
330 |
Scan.optional (P.$$$ "(" -- P.!!! (P.$$$ "permissive" -- P.$$$ ")") >> K false) true --
|
wenzelm@12876
|
331 |
P.name -- P.term -- Scan.repeat1 (P.opt_thm_name ":" -- P.prop) -- Scan.option hints
|
wenzelm@11629
|
332 |
>> (fn ((((p, f), R), eqs), src) => #1 o add_recdef p f R (map P.triple_swap eqs) src);
|
wenzelm@6429
|
333 |
|
wenzelm@6429
|
334 |
val recdefP =
|
wenzelm@6723
|
335 |
OuterSyntax.command "recdef" "define general recursive functions (TFL)" K.thy_decl
|
wenzelm@6429
|
336 |
(recdef_decl >> Toplevel.theory);
|
wenzelm@6429
|
337 |
|
wenzelm@6557
|
338 |
|
wenzelm@6557
|
339 |
val defer_recdef_decl =
|
wenzelm@8657
|
340 |
P.name -- Scan.repeat1 P.prop --
|
wenzelm@6723
|
341 |
Scan.optional (P.$$$ "(" |-- P.$$$ "congs" |-- P.!!! (P.xthms1 --| P.$$$ ")")) []
|
wenzelm@6557
|
342 |
>> (fn ((f, eqs), congs) => #1 o defer_recdef f eqs congs);
|
wenzelm@6557
|
343 |
|
wenzelm@6557
|
344 |
val defer_recdefP =
|
wenzelm@6723
|
345 |
OuterSyntax.command "defer_recdef" "defer general recursive functions (TFL)" K.thy_decl
|
wenzelm@6557
|
346 |
(defer_recdef_decl >> Toplevel.theory);
|
wenzelm@6557
|
347 |
|
wenzelm@10775
|
348 |
val recdef_tcP =
|
wenzelm@10775
|
349 |
OuterSyntax.command "recdef_tc" "recommence proof of termination condition (TFL)" K.thy_goal
|
wenzelm@17336
|
350 |
(P.opt_thm_name ":" -- P.xname -- Scan.option (P.$$$ "(" |-- P.nat --| P.$$$ ")")
|
wenzelm@17336
|
351 |
>> (fn ((thm_name, name), i) =>
|
wenzelm@17336
|
352 |
Toplevel.print o Toplevel.theory_to_proof (recdef_tc thm_name name i)));
|
wenzelm@10775
|
353 |
|
wenzelm@10775
|
354 |
|
wenzelm@11629
|
355 |
val _ = OuterSyntax.add_keywords ["permissive", "congs", "hints"];
|
wenzelm@10775
|
356 |
val _ = OuterSyntax.add_parsers [recdefP, defer_recdefP, recdef_tcP];
|
wenzelm@6429
|
357 |
|
wenzelm@6429
|
358 |
end;
|
wenzelm@6429
|
359 |
|
wenzelm@6429
|
360 |
end;
|