author | wenzelm |
Wed, 13 Sep 2000 22:28:50 +0200 | |
changeset 9949 | 1741a61d4b33 |
parent 9893 | 93d2fde0306c |
child 9991 | fdead18501ca |
permissions | -rw-r--r-- |
6429 | 1 |
(* Title: HOL/Tools/recdef_package.ML |
2 |
ID: $Id$ |
|
3 |
Author: Markus Wenzel, TU Muenchen |
|
9876 | 4 |
License: GPL (GNU GENERAL PUBLIC LICENSE) |
6429 | 5 |
|
6 |
Wrapper module for Konrad Slind's TFL package. |
|
7 |
*) |
|
8 |
||
9 |
signature RECDEF_PACKAGE = |
|
10 |
sig |
|
11 |
val quiet_mode: bool ref |
|
6439 | 12 |
val print_recdefs: theory -> unit |
8657 | 13 |
val get_recdef: theory -> string |
14 |
-> {simps: thm list, rules: thm list list, induct: thm, tcs: term list} option |
|
9859 | 15 |
val simp_add_global: theory attribute |
16 |
val simp_del_global: theory attribute |
|
17 |
val cong_add_global: theory attribute |
|
18 |
val cong_del_global: theory attribute |
|
19 |
val wf_add_global: theory attribute |
|
20 |
val wf_del_global: theory attribute |
|
21 |
val simp_add_local: Proof.context attribute |
|
22 |
val simp_del_local: Proof.context attribute |
|
23 |
val cong_add_local: Proof.context attribute |
|
24 |
val cong_del_local: Proof.context attribute |
|
25 |
val wf_add_local: Proof.context attribute |
|
26 |
val wf_del_local: Proof.context attribute |
|
27 |
val add_recdef: xstring -> string -> ((bstring * string) * Args.src list) list -> |
|
28 |
Args.src option -> theory -> theory |
|
29 |
* {simps: thm list, rules: thm list list, induct: thm, tcs: term list} |
|
30 |
val add_recdef_i: xstring -> term -> ((bstring * term) * theory attribute list) list -> |
|
31 |
theory -> theory * {simps: thm list, rules: thm list list, induct: thm, tcs: term list} |
|
32 |
val add_recdef_old: xstring -> string -> ((bstring * string) * Args.src list) list -> |
|
33 |
simpset * thm list -> theory -> |
|
34 |
theory * {simps: thm list, rules: thm list list, induct: thm, tcs: term list} |
|
6557 | 35 |
val defer_recdef: xstring -> string list -> (xstring * Args.src list) list |
36 |
-> theory -> theory * {induct_rules: thm} |
|
37 |
val defer_recdef_i: xstring -> term list -> (thm * theory attribute list) list |
|
38 |
-> theory -> theory * {induct_rules: thm} |
|
6439 | 39 |
val setup: (theory -> theory) list |
6429 | 40 |
end; |
41 |
||
42 |
structure RecdefPackage: RECDEF_PACKAGE = |
|
43 |
struct |
|
44 |
||
9859 | 45 |
|
6429 | 46 |
val quiet_mode = Tfl.quiet_mode; |
47 |
val message = Tfl.message; |
|
48 |
||
49 |
||
9859 | 50 |
(** recdef hints **) |
6439 | 51 |
|
9859 | 52 |
(* type hints *) |
53 |
||
54 |
type hints = {simps: thm list, congs: (string * thm) list, wfs: thm list}; |
|
55 |
||
56 |
fun mk_hints (simps, congs, wfs) = {simps = simps, congs = congs, wfs = wfs}: hints; |
|
57 |
fun map_hints f ({simps, congs, wfs}: hints) = mk_hints (f (simps, congs, wfs)); |
|
58 |
||
59 |
fun map_simps f = map_hints (fn (simps, congs, wfs) => (f simps, congs, wfs)); |
|
60 |
fun map_congs f = map_hints (fn (simps, congs, wfs) => (simps, f congs, wfs)); |
|
61 |
fun map_wfs f = map_hints (fn (simps, congs, wfs) => (simps, congs, f wfs)); |
|
62 |
||
63 |
fun pretty_hints ({simps, congs, wfs}: hints) = |
|
64 |
[Pretty.big_list "recdef simp hints:" (map Display.pretty_thm simps), |
|
65 |
Pretty.big_list "recdef cong hints:" (map Display.pretty_thm (map #2 congs)), |
|
66 |
Pretty.big_list "recdef wf hints:" (map Display.pretty_thm wfs)]; |
|
67 |
||
68 |
||
69 |
(* congruence rules *) |
|
70 |
||
71 |
local |
|
72 |
||
73 |
val cong_head = |
|
74 |
fst o Term.dest_Const o Term.head_of o fst o Logic.dest_equals o Thm.concl_of; |
|
6439 | 75 |
|
9859 | 76 |
fun prep_cong raw_thm = |
77 |
let val thm = safe_mk_meta_eq raw_thm in (cong_head thm, thm) end; |
|
78 |
||
79 |
in |
|
80 |
||
81 |
fun add_cong raw_thm congs = |
|
82 |
let val (c, thm) = prep_cong raw_thm |
|
83 |
in overwrite_warn (congs, (c, thm)) ("Overwriting recdef congruence rule for " ^ quote c) end; |
|
84 |
||
85 |
fun del_cong raw_thm congs = |
|
86 |
let |
|
87 |
val (c, thm) = prep_cong raw_thm; |
|
88 |
val (del, rest) = Library.partition (Library.equal c o fst) congs; |
|
89 |
in if null del then (warning ("No recdef congruence rule for " ^ quote c); congs) else rest end; |
|
90 |
||
91 |
val add_congs = curry (foldr (uncurry add_cong)); |
|
92 |
||
93 |
end; |
|
94 |
||
95 |
||
96 |
||
97 |
(** global and local recdef data **) |
|
98 |
||
99 |
(* theory data kind 'HOL/recdef' *) |
|
6439 | 100 |
|
8657 | 101 |
type recdef_info = {simps: thm list, rules: thm list list, induct: thm, tcs: term list}; |
6439 | 102 |
|
9859 | 103 |
structure GlobalRecdefArgs = |
6439 | 104 |
struct |
105 |
val name = "HOL/recdef"; |
|
9859 | 106 |
type T = recdef_info Symtab.table * hints; |
6439 | 107 |
|
9879 | 108 |
val empty = (Symtab.empty, mk_hints ([], [], [])): T; |
6557 | 109 |
val copy = I; |
6439 | 110 |
val prep_ext = I; |
9859 | 111 |
fun merge |
112 |
((tab1, {simps = simps1, congs = congs1, wfs = wfs1}), |
|
113 |
(tab2, {simps = simps2, congs = congs2, wfs = wfs2})) = |
|
114 |
(Symtab.merge (K true) (tab1, tab2), |
|
115 |
mk_hints (Drule.merge_rules (simps1, simps2), |
|
116 |
Library.merge_alists congs1 congs2, |
|
117 |
Drule.merge_rules (wfs1, wfs2))); |
|
6439 | 118 |
|
9859 | 119 |
fun print sg (tab, hints) = |
120 |
(Pretty.strs ("recdefs:" :: map #1 (Sign.cond_extern_table sg Sign.constK tab)) :: |
|
121 |
pretty_hints hints) |> Pretty.chunks |> Pretty.writeln; |
|
6439 | 122 |
end; |
123 |
||
9859 | 124 |
structure GlobalRecdefData = TheoryDataFun(GlobalRecdefArgs); |
125 |
val print_recdefs = GlobalRecdefData.print; |
|
6429 | 126 |
|
6439 | 127 |
|
9859 | 128 |
fun get_recdef thy name = Symtab.lookup (#1 (GlobalRecdefData.get thy), name); |
6439 | 129 |
|
130 |
fun put_recdef name info thy = |
|
6429 | 131 |
let |
9859 | 132 |
val (tab, hints) = GlobalRecdefData.get thy; |
133 |
val tab' = Symtab.update_new ((name, info), tab) |
|
6439 | 134 |
handle Symtab.DUP _ => error ("Duplicate recursive function definition " ^ quote name); |
9859 | 135 |
in GlobalRecdefData.put (tab', hints) thy end; |
136 |
||
137 |
val get_global_hints = #2 o GlobalRecdefData.get; |
|
138 |
val map_global_hints = GlobalRecdefData.map o apsnd; |
|
139 |
||
140 |
||
141 |
(* proof data kind 'HOL/recdef' *) |
|
142 |
||
143 |
structure LocalRecdefArgs = |
|
144 |
struct |
|
145 |
val name = "HOL/recdef"; |
|
146 |
type T = hints; |
|
147 |
val init = get_global_hints; |
|
148 |
fun print _ hints = pretty_hints hints |> Pretty.chunks |> Pretty.writeln; |
|
149 |
end; |
|
150 |
||
151 |
structure LocalRecdefData = ProofDataFun(LocalRecdefArgs); |
|
152 |
val get_local_hints = LocalRecdefData.get; |
|
153 |
val map_local_hints = LocalRecdefData.map; |
|
154 |
||
155 |
||
156 |
(* attributes *) |
|
157 |
||
158 |
local |
|
159 |
||
160 |
fun global_local f g = |
|
161 |
(fn (thy, thm) => (map_global_hints (f (g thm)) thy, thm), |
|
162 |
fn (ctxt, thm) => (map_local_hints (f (g thm)) ctxt, thm)); |
|
163 |
||
164 |
fun mk_attr (add1, add2) (del1, del2) = |
|
165 |
(Attrib.add_del_args add1 del1, Attrib.add_del_args add2 del2); |
|
166 |
||
167 |
in |
|
168 |
||
169 |
val (simp_add_global, simp_add_local) = global_local map_simps (Drule.add_rules o single); |
|
170 |
val (simp_del_global, simp_del_local) = global_local map_simps (Drule.del_rules o single); |
|
171 |
val (cong_add_global, cong_add_local) = global_local map_congs add_cong; |
|
172 |
val (cong_del_global, cong_del_local) = global_local map_congs del_cong; |
|
173 |
val (wf_add_global, wf_add_local) = global_local map_wfs (Drule.add_rules o single); |
|
174 |
val (wf_del_global, wf_del_local) = global_local map_wfs (Drule.del_rules o single); |
|
175 |
||
176 |
val simp_attr = mk_attr (simp_add_global, simp_add_local) (simp_del_global, simp_del_local); |
|
177 |
val cong_attr = mk_attr (cong_add_global, cong_add_local) (cong_del_global, cong_del_local); |
|
178 |
val wf_attr = mk_attr (wf_add_global, wf_add_local) (wf_del_global, wf_del_local); |
|
179 |
||
180 |
end; |
|
181 |
||
182 |
||
9949 | 183 |
(* modifiers *) |
9859 | 184 |
|
9949 | 185 |
val recdef_simpN = "recdef_simp"; |
186 |
val recdef_congN = "recdef_cong"; |
|
187 |
val recdef_wfN = "recdef_wf"; |
|
9859 | 188 |
|
189 |
val recdef_modifiers = |
|
9949 | 190 |
[Args.$$$ recdef_simpN -- Args.colon >> K ((I, simp_add_local): Method.modifier), |
191 |
Args.$$$ recdef_simpN -- Args.$$$ Args.addN -- Args.colon >> K (I, simp_add_local), |
|
192 |
Args.$$$ recdef_simpN -- Args.$$$ Args.delN -- Args.colon >> K (I, simp_del_local), |
|
193 |
Args.$$$ recdef_congN -- Args.colon >> K (I, cong_add_local), |
|
194 |
Args.$$$ recdef_congN -- Args.$$$ Args.addN -- Args.colon >> K (I, cong_add_local), |
|
195 |
Args.$$$ recdef_congN -- Args.$$$ Args.delN -- Args.colon >> K (I, cong_del_local), |
|
196 |
Args.$$$ recdef_wfN -- Args.colon >> K (I, wf_add_local), |
|
197 |
Args.$$$ recdef_wfN -- Args.$$$ Args.addN -- Args.colon >> K (I, wf_add_local), |
|
198 |
Args.$$$ recdef_wfN -- Args.$$$ Args.delN -- Args.colon >> K (I, wf_del_local)] @ |
|
199 |
Clasimp.clasimp_modifiers; |
|
9859 | 200 |
|
9949 | 201 |
|
9859 | 202 |
|
9949 | 203 |
(** prepare_hints(_i) **) |
9859 | 204 |
|
205 |
fun prepare_hints thy opt_src = |
|
206 |
let |
|
207 |
val ctxt0 = ProofContext.init thy; |
|
208 |
val ctxt = |
|
209 |
(case opt_src of |
|
210 |
None => ctxt0 |
|
9949 | 211 |
| Some src => Method.only_sectioned_args recdef_modifiers I src ctxt0); |
9859 | 212 |
val {simps, congs, wfs} = get_local_hints ctxt; |
213 |
val cs = Classical.get_local_claset ctxt; |
|
214 |
val ss = Simplifier.get_local_simpset ctxt addsimps simps; |
|
215 |
in (cs, ss, map #2 congs, wfs) end; |
|
216 |
||
217 |
fun prepare_hints_i thy () = |
|
218 |
let val {simps, congs, wfs} = get_global_hints thy |
|
219 |
in (Classical.claset_of thy, Simplifier.simpset_of thy addsimps simps, map #2 congs, wfs) end; |
|
220 |
||
6439 | 221 |
|
222 |
||
223 |
(** add_recdef(_i) **) |
|
224 |
||
6557 | 225 |
fun requires_recdef thy = Theory.requires thy "Recdef" "recursive functions"; |
226 |
||
9859 | 227 |
fun gen_add_recdef tfl_fn prep_att prep_hints raw_name R eq_srcs hints thy = |
6439 | 228 |
let |
9859 | 229 |
val _ = requires_recdef thy; |
230 |
||
6439 | 231 |
val name = Sign.intern_const (Theory.sign_of thy) raw_name; |
232 |
val bname = Sign.base_name name; |
|
6429 | 233 |
val _ = message ("Defining recursive function " ^ quote name ^ " ..."); |
234 |
||
8657 | 235 |
val ((eq_names, eqs), raw_eq_atts) = apfst split_list (split_list eq_srcs); |
236 |
val eq_atts = map (map (prep_att thy)) raw_eq_atts; |
|
237 |
||
9859 | 238 |
val (cs, ss, congs, wfs) = prep_hints thy hints; |
239 |
val (thy, {rules = rules_idx, induct, tcs}) = tfl_fn thy cs ss congs wfs name R eqs; |
|
8657 | 240 |
val rules = map (map #1) (Library.partition_eq Library.eq_snd rules_idx); |
241 |
val simp_att = if null tcs then [Simplifier.simp_add_global] else []; |
|
242 |
||
8430 | 243 |
val case_numbers = map Library.string_of_int (1 upto Thm.nprems_of induct); |
8657 | 244 |
val (thy, (simps' :: rules', [induct'])) = |
7798
42e94b618f34
return stored thms with proper naming in derivation;
wenzelm
parents:
7262
diff
changeset
|
245 |
thy |
6439 | 246 |
|> Theory.add_path bname |
8657 | 247 |
|> PureThy.add_thmss ((("simps", flat rules), simp_att) :: ((eq_names ~~ rules) ~~ eq_atts)) |
8430 | 248 |
|>>> PureThy.add_thms [(("induct", induct), [RuleCases.case_names case_numbers])]; |
8657 | 249 |
val result = {simps = simps', rules = rules', induct = induct', tcs = tcs}; |
7798
42e94b618f34
return stored thms with proper naming in derivation;
wenzelm
parents:
7262
diff
changeset
|
250 |
val thy = |
42e94b618f34
return stored thms with proper naming in derivation;
wenzelm
parents:
7262
diff
changeset
|
251 |
thy |
6439 | 252 |
|> put_recdef name result |
6429 | 253 |
|> Theory.parent_path; |
7798
42e94b618f34
return stored thms with proper naming in derivation;
wenzelm
parents:
7262
diff
changeset
|
254 |
in (thy, result) end; |
6429 | 255 |
|
9859 | 256 |
val add_recdef = gen_add_recdef Tfl.define Attrib.global_attribute prepare_hints; |
257 |
fun add_recdef_i x y z = gen_add_recdef Tfl.define_i (K I) prepare_hints_i x y z (); |
|
258 |
||
259 |
||
260 |
(* add_recdef_old -- legacy interface *) |
|
261 |
||
262 |
fun prepare_hints_old thy (ss, thms) = |
|
263 |
let val {simps, congs, wfs} = get_global_hints thy |
|
264 |
in (Classical.claset_of thy, ss addsimps simps, map #2 (add_congs thms congs), wfs) end; |
|
265 |
||
266 |
val add_recdef_old = gen_add_recdef Tfl.define Attrib.global_attribute prepare_hints_old; |
|
6429 | 267 |
|
268 |
||
6557 | 269 |
|
270 |
(** defer_recdef(_i) **) |
|
271 |
||
272 |
fun gen_defer_recdef tfl_fn app_thms raw_name eqs raw_congs thy = |
|
273 |
let |
|
274 |
val name = Sign.intern_const (Theory.sign_of thy) raw_name; |
|
275 |
val bname = Sign.base_name name; |
|
276 |
||
277 |
val _ = requires_recdef thy; |
|
278 |
val _ = message ("Deferred recursive function " ^ quote name ^ " ..."); |
|
279 |
||
280 |
val (thy1, congs) = thy |> app_thms raw_congs; |
|
9859 | 281 |
val (thy2, induct_rules) = tfl_fn thy1 congs name eqs; |
8430 | 282 |
val (thy3, [induct_rules']) = |
6557 | 283 |
thy2 |
284 |
|> Theory.add_path bname |
|
285 |
|> PureThy.add_thms [(("induct_rules", induct_rules), [])] |
|
8430 | 286 |
|>> Theory.parent_path; |
287 |
in (thy3, {induct_rules = induct_rules'}) end; |
|
6557 | 288 |
|
289 |
val defer_recdef = gen_defer_recdef Tfl.defer IsarThy.apply_theorems; |
|
290 |
val defer_recdef_i = gen_defer_recdef Tfl.defer_i IsarThy.apply_theorems_i; |
|
291 |
||
292 |
||
293 |
||
6439 | 294 |
(** package setup **) |
295 |
||
296 |
(* setup theory *) |
|
297 |
||
9859 | 298 |
val setup = |
299 |
[GlobalRecdefData.init, LocalRecdefData.init, |
|
300 |
Attrib.add_attributes |
|
9949 | 301 |
[(recdef_simpN, simp_attr, "declaration of recdef simp rule"), |
302 |
(recdef_congN, cong_attr, "declaration of recdef cong rule"), |
|
303 |
(recdef_wfN, wf_attr, "declaration of recdef wf rule")]]; |
|
6439 | 304 |
|
305 |
||
6429 | 306 |
(* outer syntax *) |
307 |
||
6723 | 308 |
local structure P = OuterParse and K = OuterSyntax.Keyword in |
6429 | 309 |
|
9859 | 310 |
val hints = |
311 |
P.$$$ "(" |-- P.!!! (P.position (P.$$$ "hints" -- P.arguments) --| P.$$$ ")") >> Args.src; |
|
312 |
||
6429 | 313 |
val recdef_decl = |
9859 | 314 |
P.name -- P.term -- Scan.repeat1 (P.opt_thm_name ":" -- P.prop --| P.marg_comment) |
315 |
-- Scan.option hints |
|
316 |
>> (fn (((f, R), eqs), src) => #1 o add_recdef f R (map P.triple_swap eqs) src); |
|
6429 | 317 |
|
318 |
val recdefP = |
|
6723 | 319 |
OuterSyntax.command "recdef" "define general recursive functions (TFL)" K.thy_decl |
6429 | 320 |
(recdef_decl >> Toplevel.theory); |
321 |
||
6557 | 322 |
|
323 |
val defer_recdef_decl = |
|
8657 | 324 |
P.name -- Scan.repeat1 P.prop -- |
6723 | 325 |
Scan.optional (P.$$$ "(" |-- P.$$$ "congs" |-- P.!!! (P.xthms1 --| P.$$$ ")")) [] |
6557 | 326 |
>> (fn ((f, eqs), congs) => #1 o defer_recdef f eqs congs); |
327 |
||
328 |
val defer_recdefP = |
|
6723 | 329 |
OuterSyntax.command "defer_recdef" "defer general recursive functions (TFL)" K.thy_decl |
6557 | 330 |
(defer_recdef_decl >> Toplevel.theory); |
331 |
||
332 |
val _ = OuterSyntax.add_parsers [recdefP, defer_recdefP]; |
|
6429 | 333 |
|
334 |
end; |
|
335 |
||
336 |
||
337 |
end; |