|
1 (* Title: HOL/Tools/inductive_set_package.ML |
|
2 ID: $Id$ |
|
3 Author: Stefan Berghofer, TU Muenchen |
|
4 |
|
5 Wrapper for defining inductive sets using package for inductive predicates, |
|
6 including infrastructure for converting between predicates and sets. |
|
7 *) |
|
8 |
|
9 signature INDUCTIVE_SET_PACKAGE = |
|
10 sig |
|
11 val to_set_att: thm list -> attribute |
|
12 val to_pred_att: thm list -> attribute |
|
13 val pred_set_conv_att: attribute |
|
14 val add_inductive_i: bool -> bstring -> bool -> bool -> bool -> |
|
15 (string * typ option * mixfix) list -> |
|
16 (string * typ option) list -> ((bstring * Attrib.src list) * term) list -> thm list -> |
|
17 local_theory -> InductivePackage.inductive_result * local_theory |
|
18 val add_inductive: bool -> bool -> (string * string option * mixfix) list -> |
|
19 (string * string option * mixfix) list -> |
|
20 ((bstring * Attrib.src list) * string) list -> (thmref * Attrib.src list) list -> |
|
21 local_theory -> InductivePackage.inductive_result * local_theory |
|
22 val setup: theory -> theory |
|
23 end; |
|
24 |
|
25 structure InductiveSetPackage: INDUCTIVE_SET_PACKAGE = |
|
26 struct |
|
27 |
|
28 val note_theorem = LocalTheory.note Thm.theoremK; |
|
29 |
|
30 |
|
31 (**** simplify {(x1, ..., xn). (x1, ..., xn) : S} to S ****) |
|
32 |
|
33 val subset_antisym = thm "subset_antisym"; |
|
34 |
|
35 val collect_mem_simproc = |
|
36 Simplifier.simproc (theory "Set") "Collect_mem" ["Collect t"] (fn thy => fn ss => |
|
37 fn S as Const ("Collect", Type ("fun", [_, T])) $ t => |
|
38 let val (u, Ts, ps) = HOLogic.strip_split t |
|
39 in case u of |
|
40 (c as Const ("op :", _)) $ q $ S' => |
|
41 (case try (HOLogic.dest_tuple' ps) q of |
|
42 NONE => NONE |
|
43 | SOME ts => |
|
44 if not (loose_bvar (S', 0)) andalso |
|
45 ts = map Bound (length ps downto 0) |
|
46 then |
|
47 let val simp = full_simp_tac (Simplifier.inherit_context ss |
|
48 (HOL_basic_ss addsimps [split_paired_all, split_conv])) 1 |
|
49 in |
|
50 SOME (Goal.prove (Simplifier.the_context ss) [] [] |
|
51 (Const ("==", T --> T --> propT) $ S $ S') |
|
52 (K (EVERY |
|
53 [rtac eq_reflection 1, rtac subset_antisym 1, |
|
54 rtac subsetI 1, dtac CollectD 1, simp, |
|
55 rtac subsetI 1, rtac CollectI 1, simp]))) |
|
56 end |
|
57 else NONE) |
|
58 | _ => NONE |
|
59 end |
|
60 | _ => NONE); |
|
61 |
|
62 (***********************************************************************************) |
|
63 (* simplifies (%x y. (x, y) : S & P x y) to (%x y. (x, y) : S Int {(x, y). P x y}) *) |
|
64 (* and (%x y. (x, y) : S | P x y) to (%x y. (x, y) : S Un {(x, y). P x y}) *) |
|
65 (* used for converting "strong" (co)induction rules *) |
|
66 (***********************************************************************************) |
|
67 |
|
68 val strong_ind_simproc = |
|
69 Simplifier.simproc HOL.thy "strong_ind" ["t"] (fn thy => fn ss => fn t => |
|
70 let |
|
71 val xs = strip_abs_vars t; |
|
72 fun close t = fold (fn x => fn u => all (fastype_of x) $ lambda x u) |
|
73 (term_vars t) t; |
|
74 fun mkop "op &" T x = SOME (Const ("op Int", T --> T --> T), x) |
|
75 | mkop "op |" T x = SOME (Const ("op Un", T --> T --> T), x) |
|
76 | mkop _ _ _ = NONE; |
|
77 fun mk_collect p T t = |
|
78 let val U = HOLogic.dest_setT T |
|
79 in HOLogic.Collect_const U $ |
|
80 HOLogic.ap_split' (HOLogic.prod_factors p) U HOLogic.boolT t |
|
81 end; |
|
82 fun decomp (Const (s, _) $ ((m as Const ("op :", |
|
83 Type (_, [_, Type (_, [T, _])]))) $ p $ S) $ u) = |
|
84 mkop s T (m, p, S, mk_collect p T (head_of u)) |
|
85 | decomp (Const (s, _) $ u $ ((m as Const ("op :", |
|
86 Type (_, [_, Type (_, [T, _])]))) $ p $ S)) = |
|
87 mkop s T (m, p, mk_collect p T (head_of u), S) |
|
88 | decomp _ = NONE; |
|
89 val simp = full_simp_tac (Simplifier.inherit_context ss |
|
90 (HOL_basic_ss addsimps [mem_Collect_eq, split_conv])) 1; |
|
91 in |
|
92 if null xs then NONE |
|
93 else case decomp (strip_abs_body t) of |
|
94 NONE => NONE |
|
95 | SOME (bop, (m, p, S, S')) => |
|
96 SOME (mk_meta_eq (Goal.prove (Simplifier.the_context ss) [] [] |
|
97 (close (HOLogic.mk_Trueprop (HOLogic.mk_eq |
|
98 (t, list_abs (xs, m $ p $ (bop $ S $ S')))))) |
|
99 (K (EVERY |
|
100 [REPEAT (rtac ext 1), rtac iffI 1, |
|
101 EVERY [etac conjE 1, rtac IntI 1, simp, simp, |
|
102 etac IntE 1, rtac conjI 1, simp, simp] ORELSE |
|
103 EVERY [etac disjE 1, rtac UnI1 1, simp, rtac UnI2 1, simp, |
|
104 etac UnE 1, rtac disjI1 1, simp, rtac disjI2 1, simp]])))) |
|
105 handle ERROR _ => NONE |
|
106 end); |
|
107 |
|
108 (* only eta contract terms occurring as arguments of functions satisfying p *) |
|
109 fun eta_contract p = |
|
110 let |
|
111 fun eta b (Abs (a, T, body)) = |
|
112 (case eta b body of |
|
113 body' as (f $ Bound 0) => |
|
114 if loose_bvar1 (f, 0) orelse not b then Abs (a, T, body') |
|
115 else incr_boundvars ~1 f |
|
116 | body' => Abs (a, T, body')) |
|
117 | eta b (t $ u) = eta b t $ eta (p (head_of t)) u |
|
118 | eta b t = t |
|
119 in eta false end; |
|
120 |
|
121 fun eta_contract_thm p = |
|
122 Conv.fconv_rule (Conv.then_conv (Thm.beta_conversion true, fn ct => |
|
123 Thm.transitive (Thm.eta_conversion ct) |
|
124 (Thm.symmetric (Thm.eta_conversion |
|
125 (cterm_of (theory_of_cterm ct) (eta_contract p (term_of ct))))))); |
|
126 |
|
127 |
|
128 (***********************************************************) |
|
129 (* rules for converting between predicate and set notation *) |
|
130 (* *) |
|
131 (* rules for converting predicates to sets have the form *) |
|
132 (* P (%x y. (x, y) : s) = (%x y. (x, y) : S s) *) |
|
133 (* *) |
|
134 (* rules for converting sets to predicates have the form *) |
|
135 (* S {(x, y). p x y} = {(x, y). P p x y} *) |
|
136 (* *) |
|
137 (* where s and p are parameters *) |
|
138 (***********************************************************) |
|
139 |
|
140 structure PredSetConvData = GenericDataFun |
|
141 ( |
|
142 type T = |
|
143 {(* rules for converting predicates to sets *) |
|
144 to_set_simps: thm list, |
|
145 (* rules for converting sets to predicates *) |
|
146 to_pred_simps: thm list, |
|
147 (* arities of functions of type t set => ... => u set *) |
|
148 set_arities: (typ * (int list list option list * int list list option)) list Symtab.table, |
|
149 (* arities of functions of type (t => ... => bool) => u => ... => bool *) |
|
150 pred_arities: (typ * (int list list option list * int list list option)) list Symtab.table}; |
|
151 val empty = {to_set_simps = [], to_pred_simps = [], |
|
152 set_arities = Symtab.empty, pred_arities = Symtab.empty}; |
|
153 val extend = I; |
|
154 fun merge _ |
|
155 ({to_set_simps = to_set_simps1, to_pred_simps = to_pred_simps1, |
|
156 set_arities = set_arities1, pred_arities = pred_arities1}, |
|
157 {to_set_simps = to_set_simps2, to_pred_simps = to_pred_simps2, |
|
158 set_arities = set_arities2, pred_arities = pred_arities2}) = |
|
159 {to_set_simps = Drule.merge_rules (to_set_simps1, to_set_simps2), |
|
160 to_pred_simps = Drule.merge_rules (to_pred_simps1, to_pred_simps2), |
|
161 set_arities = Symtab.merge_list op = (set_arities1, set_arities2), |
|
162 pred_arities = Symtab.merge_list op = (pred_arities1, pred_arities2)}; |
|
163 ); |
|
164 |
|
165 fun name_type_of (Free p) = SOME p |
|
166 | name_type_of (Const p) = SOME p |
|
167 | name_type_of _ = NONE; |
|
168 |
|
169 fun map_type f (Free (s, T)) = Free (s, f T) |
|
170 | map_type f (Var (ixn, T)) = Var (ixn, f T) |
|
171 | map_type f _ = error "map_type"; |
|
172 |
|
173 fun find_most_specific is_inst f eq xs T = |
|
174 find_first (fn U => is_inst (T, f U) |
|
175 andalso forall (fn U' => eq (f U, f U') orelse not |
|
176 (is_inst (T, f U') andalso is_inst (f U', f U))) |
|
177 xs) xs; |
|
178 |
|
179 fun lookup_arity thy arities (s, T) = case Symtab.lookup arities s of |
|
180 NONE => NONE |
|
181 | SOME xs => find_most_specific (Sign.typ_instance thy) fst (op =) xs T; |
|
182 |
|
183 fun lookup_rule thy f rules = find_most_specific |
|
184 (swap #> Pattern.matches thy) (f #> fst) (op aconv) rules; |
|
185 |
|
186 fun infer_arities thy arities (optf, t) fs = case strip_comb t of |
|
187 (Abs (s, T, u), []) => infer_arities thy arities (NONE, u) fs |
|
188 | (Abs _, _) => infer_arities thy arities (NONE, Envir.beta_norm t) fs |
|
189 | (u, ts) => (case Option.map (lookup_arity thy arities) (name_type_of u) of |
|
190 SOME (SOME (_, (arity, _))) => |
|
191 (fold (infer_arities thy arities) (arity ~~ List.take (ts, length arity)) fs |
|
192 handle Subscript => error "infer_arities: bad term") |
|
193 | _ => fold (infer_arities thy arities) (map (pair NONE) ts) |
|
194 (case optf of |
|
195 NONE => fs |
|
196 | SOME f => AList.update op = (u, the_default f |
|
197 (Option.map (curry op inter f) (AList.lookup op = fs u))) fs)); |
|
198 |
|
199 |
|
200 (**************************************************************) |
|
201 (* derive the to_pred equation from the to_set equation *) |
|
202 (* *) |
|
203 (* 1. instantiate each set parameter with {(x, y). p x y} *) |
|
204 (* 2. apply %P. {(x, y). P x y} to both sides of the equation *) |
|
205 (* 3. simplify *) |
|
206 (**************************************************************) |
|
207 |
|
208 fun mk_to_pred_inst thy fs = |
|
209 map (fn (x, ps) => |
|
210 let |
|
211 val U = HOLogic.dest_setT (fastype_of x); |
|
212 val x' = map_type (K (HOLogic.prodT_factors' ps U ---> HOLogic.boolT)) x |
|
213 in |
|
214 (cterm_of thy x, |
|
215 cterm_of thy (HOLogic.Collect_const U $ |
|
216 HOLogic.ap_split' ps U HOLogic.boolT x')) |
|
217 end) fs; |
|
218 |
|
219 fun mk_to_pred_eq p fs optfs' T thm = |
|
220 let |
|
221 val thy = theory_of_thm thm; |
|
222 val insts = mk_to_pred_inst thy fs; |
|
223 val thm' = Thm.instantiate ([], insts) thm; |
|
224 val thm'' = (case optfs' of |
|
225 NONE => thm' RS sym |
|
226 | SOME fs' => |
|
227 let |
|
228 val U = HOLogic.dest_setT (body_type T); |
|
229 val Ts = HOLogic.prodT_factors' fs' U; |
|
230 (* FIXME: should cterm_instantiate increment indexes? *) |
|
231 val arg_cong' = Thm.incr_indexes (Thm.maxidx_of thm + 1) arg_cong; |
|
232 val (arg_cong_f, _) = arg_cong' |> cprop_of |> Drule.strip_imp_concl |> |
|
233 Thm.dest_comb |> snd |> Drule.strip_comb |> snd |> hd |> Thm.dest_comb |
|
234 in |
|
235 thm' RS (Drule.cterm_instantiate [(arg_cong_f, |
|
236 cterm_of thy (Abs ("P", Ts ---> HOLogic.boolT, |
|
237 HOLogic.Collect_const U $ HOLogic.ap_split' fs' U |
|
238 HOLogic.boolT (Bound 0))))] arg_cong' RS sym) |
|
239 end) |
|
240 in |
|
241 Simplifier.simplify (HOL_basic_ss addsimps [mem_Collect_eq, split_conv] |
|
242 addsimprocs [collect_mem_simproc]) thm'' |> |
|
243 zero_var_indexes |> eta_contract_thm (equal p) |
|
244 end; |
|
245 |
|
246 |
|
247 (**** declare rules for converting predicates to sets ****) |
|
248 |
|
249 fun add ctxt thm {to_set_simps, to_pred_simps, set_arities, pred_arities} = |
|
250 case prop_of thm of |
|
251 Const ("Trueprop", _) $ (Const ("op =", Type (_, [T, _])) $ lhs $ rhs) => |
|
252 (case body_type T of |
|
253 Type ("bool", []) => |
|
254 let |
|
255 val thy = Context.theory_of ctxt; |
|
256 fun factors_of t fs = case strip_abs_body t of |
|
257 Const ("op :", _) $ u $ S => |
|
258 if is_Free S orelse is_Var S then |
|
259 let val ps = HOLogic.prod_factors u |
|
260 in (SOME ps, (S, ps) :: fs) end |
|
261 else (NONE, fs) |
|
262 | _ => (NONE, fs); |
|
263 val (h, ts) = strip_comb lhs |
|
264 val (pfs, fs) = fold_map factors_of ts []; |
|
265 val ((h', ts'), fs') = (case rhs of |
|
266 Abs _ => (case strip_abs_body rhs of |
|
267 Const ("op :", _) $ u $ S => |
|
268 (strip_comb S, SOME (HOLogic.prod_factors u)) |
|
269 | _ => error "member symbol on right-hand side expected") |
|
270 | _ => (strip_comb rhs, NONE)) |
|
271 in |
|
272 case (name_type_of h, name_type_of h') of |
|
273 (SOME (s, T), SOME (s', T')) => |
|
274 (case Symtab.lookup set_arities s' of |
|
275 NONE => () |
|
276 | SOME xs => if exists (fn (U, _) => |
|
277 Sign.typ_instance thy (T', U) andalso |
|
278 Sign.typ_instance thy (U, T')) xs |
|
279 then |
|
280 error ("Clash of conversion rules for operator " ^ s') |
|
281 else (); |
|
282 {to_set_simps = thm :: to_set_simps, |
|
283 to_pred_simps = |
|
284 mk_to_pred_eq h fs fs' T' thm :: to_pred_simps, |
|
285 set_arities = Symtab.insert_list op = (s', |
|
286 (T', (map (AList.lookup op = fs) ts', fs'))) set_arities, |
|
287 pred_arities = Symtab.insert_list op = (s, |
|
288 (T, (pfs, fs'))) pred_arities}) |
|
289 | _ => error "set / predicate constant expected" |
|
290 end |
|
291 | _ => error "equation between predicates expected") |
|
292 | _ => error "equation expected"; |
|
293 |
|
294 val pred_set_conv_att = Thm.declaration_attribute |
|
295 (fn thm => fn ctxt => PredSetConvData.map (add ctxt thm) ctxt); |
|
296 |
|
297 |
|
298 (**** convert theorem in set notation to predicate notation ****) |
|
299 |
|
300 fun is_pred tab t = |
|
301 case Option.map (Symtab.lookup tab o fst) (name_type_of t) of |
|
302 SOME (SOME _) => true | _ => false; |
|
303 |
|
304 fun to_pred_simproc rules = |
|
305 let val rules' = map mk_meta_eq rules |
|
306 in |
|
307 Simplifier.simproc HOL.thy "to_pred" ["t"] |
|
308 (fn thy => K (lookup_rule thy (prop_of #> Logic.dest_equals) rules')) |
|
309 end; |
|
310 |
|
311 fun to_pred_proc thy rules t = case lookup_rule thy I rules t of |
|
312 NONE => NONE |
|
313 | SOME (lhs, rhs) => |
|
314 SOME (Envir.subst_vars |
|
315 (Pattern.match thy (lhs, t) (Vartab.empty, Vartab.empty)) rhs); |
|
316 |
|
317 fun to_pred thms ctxt thm = |
|
318 let |
|
319 val thy = Context.theory_of ctxt; |
|
320 val {to_pred_simps, set_arities, pred_arities, ...} = |
|
321 fold (add ctxt) thms (PredSetConvData.get ctxt); |
|
322 val fs = filter (is_Var o fst) |
|
323 (infer_arities thy set_arities (NONE, prop_of thm) []); |
|
324 (* instantiate each set parameter with {(x, y). p x y} *) |
|
325 val insts = mk_to_pred_inst thy fs |
|
326 in |
|
327 thm |> |
|
328 Thm.instantiate ([], insts) |> |
|
329 Simplifier.full_simplify (HOL_basic_ss addsimprocs |
|
330 [to_pred_simproc (mem_Collect_eq :: split_conv :: to_pred_simps)]) |> |
|
331 eta_contract_thm (is_pred pred_arities) |
|
332 end; |
|
333 |
|
334 val to_pred_att = Thm.rule_attribute o to_pred; |
|
335 |
|
336 |
|
337 (**** convert theorem in predicate notation to set notation ****) |
|
338 |
|
339 fun to_set thms ctxt thm = |
|
340 let |
|
341 val thy = Context.theory_of ctxt; |
|
342 val {to_set_simps, pred_arities, ...} = |
|
343 fold (add ctxt) thms (PredSetConvData.get ctxt); |
|
344 val fs = filter (is_Var o fst) |
|
345 (infer_arities thy pred_arities (NONE, prop_of thm) []); |
|
346 (* instantiate each predicate parameter with %x y. (x, y) : s *) |
|
347 val insts = map (fn (x, ps) => |
|
348 let |
|
349 val Ts = binder_types (fastype_of x); |
|
350 val T = HOLogic.mk_tupleT ps Ts; |
|
351 val x' = map_type (K (HOLogic.mk_setT T)) x |
|
352 in |
|
353 (cterm_of thy x, |
|
354 cterm_of thy (list_abs (map (pair "x") Ts, HOLogic.mk_mem |
|
355 (HOLogic.mk_tuple' ps T (map Bound (length ps downto 0)), x')))) |
|
356 end) fs |
|
357 in |
|
358 Simplifier.full_simplify (HOL_basic_ss addsimps to_set_simps |
|
359 addsimprocs [strong_ind_simproc]) |
|
360 (Thm.instantiate ([], insts) thm) |
|
361 end; |
|
362 |
|
363 val to_set_att = Thm.rule_attribute o to_set; |
|
364 |
|
365 |
|
366 (**** preprocessor for code generator ****) |
|
367 |
|
368 fun codegen_preproc thy = |
|
369 let |
|
370 val {to_pred_simps, set_arities, pred_arities, ...} = |
|
371 PredSetConvData.get (Context.Theory thy); |
|
372 fun preproc thm = |
|
373 if exists_Const (fn (s, _) => case Symtab.lookup set_arities s of |
|
374 NONE => false |
|
375 | SOME arities => exists (fn (_, (xs, _)) => |
|
376 forall is_none xs) arities) (prop_of thm) |
|
377 then |
|
378 thm |> |
|
379 Simplifier.full_simplify (HOL_basic_ss addsimprocs |
|
380 [to_pred_simproc (mem_Collect_eq :: split_conv :: to_pred_simps)]) |> |
|
381 eta_contract_thm (is_pred pred_arities) |
|
382 else thm |
|
383 in map preproc end; |
|
384 |
|
385 fun code_ind_att optmod = to_pred_att [] #> InductiveCodegen.add optmod NONE; |
|
386 |
|
387 |
|
388 (**** definition of inductive sets ****) |
|
389 |
|
390 fun add_ind_set_def verbose alt_name coind no_elim no_ind cs |
|
391 intros monos params cnames_syn ctxt = |
|
392 let |
|
393 val thy = ProofContext.theory_of ctxt; |
|
394 val {set_arities, pred_arities, to_pred_simps, ...} = |
|
395 PredSetConvData.get (Context.Proof ctxt); |
|
396 fun infer (Abs (_, _, t)) = infer t |
|
397 | infer (Const ("op :", _) $ t $ u) = |
|
398 infer_arities thy set_arities (SOME (HOLogic.prod_factors t), u) |
|
399 | infer (t $ u) = infer t #> infer u |
|
400 | infer _ = I; |
|
401 val new_arities = filter_out |
|
402 (fn (x as Free (_, Type ("fun", _)), _) => x mem params |
|
403 | _ => false) (fold (snd #> infer) intros []); |
|
404 val params' = map (fn x => (case AList.lookup op = new_arities x of |
|
405 SOME fs => |
|
406 let |
|
407 val T = HOLogic.dest_setT (fastype_of x); |
|
408 val Ts = HOLogic.prodT_factors' fs T; |
|
409 val x' = map_type (K (Ts ---> HOLogic.boolT)) x |
|
410 in |
|
411 (x, (x', |
|
412 (HOLogic.Collect_const T $ |
|
413 HOLogic.ap_split' fs T HOLogic.boolT x', |
|
414 list_abs (map (pair "x") Ts, HOLogic.mk_mem |
|
415 (HOLogic.mk_tuple' fs T (map Bound (length fs downto 0)), |
|
416 x))))) |
|
417 end |
|
418 | NONE => (x, (x, (x, x))))) params; |
|
419 val (params1, (params2, params3)) = |
|
420 params' |> map snd |> split_list ||> split_list; |
|
421 |
|
422 (* equations for converting sets to predicates *) |
|
423 val ((cs', cs_info), eqns) = cs |> map (fn c as Free (s, T) => |
|
424 let |
|
425 val fs = the_default [] (AList.lookup op = new_arities c); |
|
426 val U = HOLogic.dest_setT (body_type T); |
|
427 val Ts = HOLogic.prodT_factors' fs U; |
|
428 val c' = Free (s ^ "p", |
|
429 map fastype_of params1 @ Ts ---> HOLogic.boolT) |
|
430 in |
|
431 ((c', (fs, U, Ts)), |
|
432 (list_comb (c, params2), |
|
433 HOLogic.Collect_const U $ HOLogic.ap_split' fs U HOLogic.boolT |
|
434 (list_comb (c', params1)))) |
|
435 end) |> split_list |>> split_list; |
|
436 val eqns' = eqns @ |
|
437 map (prop_of #> HOLogic.dest_Trueprop #> HOLogic.dest_eq) |
|
438 (mem_Collect_eq :: split_conv :: to_pred_simps); |
|
439 |
|
440 (* predicate version of the introduction rules *) |
|
441 val intros' = |
|
442 map (fn (name_atts, t) => (name_atts, |
|
443 t |> |
|
444 map_aterms (fn u => |
|
445 (case AList.lookup op = params' u of |
|
446 SOME (_, (u', _)) => u' |
|
447 | NONE => u)) |> |
|
448 Pattern.rewrite_term thy [] [to_pred_proc thy eqns'] |> |
|
449 eta_contract (member op = cs' orf is_pred pred_arities))) intros; |
|
450 val cnames_syn' = map (fn (s, _) => (s ^ "p", NoSyn)) cnames_syn; |
|
451 val monos' = map (to_pred [] (Context.Proof ctxt)) monos; |
|
452 val ({preds, intrs, elims, raw_induct, ...}, ctxt1) = |
|
453 InductivePackage.add_ind_def verbose "" coind |
|
454 no_elim no_ind cs' intros' monos' params1 cnames_syn' ctxt; |
|
455 |
|
456 (* define inductive sets using previously defined predicates *) |
|
457 val (defs, ctxt2) = LocalTheory.defs Thm.internalK |
|
458 (map (fn ((c_syn, (fs, U, _)), p) => (c_syn, (("", []), |
|
459 fold_rev lambda params (HOLogic.Collect_const U $ |
|
460 HOLogic.ap_split' fs U HOLogic.boolT (list_comb (p, params3)))))) |
|
461 (cnames_syn ~~ cs_info ~~ preds)) ctxt1; |
|
462 |
|
463 (* prove theorems for converting predicate to set notation *) |
|
464 val ctxt3 = fold |
|
465 (fn (((p, c as Free (s, _)), (fs, U, Ts)), (_, (_, def))) => fn ctxt => |
|
466 let val conv_thm = |
|
467 Goal.prove ctxt (map (fst o dest_Free) params) [] |
|
468 (HOLogic.mk_Trueprop (HOLogic.mk_eq |
|
469 (list_comb (p, params3), |
|
470 list_abs (map (pair "x") Ts, HOLogic.mk_mem |
|
471 (HOLogic.mk_tuple' fs U (map Bound (length fs downto 0)), |
|
472 list_comb (c, params)))))) |
|
473 (K (REPEAT (rtac ext 1) THEN simp_tac (HOL_basic_ss addsimps |
|
474 [def, mem_Collect_eq, split_conv]) 1)) |
|
475 in |
|
476 ctxt |> note_theorem ((s ^ "p_" ^ s ^ "_eq", |
|
477 [Attrib.internal (K pred_set_conv_att)]), |
|
478 [conv_thm]) |> snd |
|
479 end) (preds ~~ cs ~~ cs_info ~~ defs) ctxt2; |
|
480 |
|
481 (* convert theorems to set notation *) |
|
482 val rec_name = if alt_name = "" then |
|
483 space_implode "_" (map fst cnames_syn) else alt_name; |
|
484 val cnames = map (Sign.full_name (ProofContext.theory_of ctxt3) o #1) cnames_syn; (* FIXME *) |
|
485 val (intr_names, intr_atts) = split_list (map fst intros); |
|
486 val raw_induct' = to_set [] (Context.Proof ctxt3) raw_induct; |
|
487 val (intrs', elims', induct, ctxt4) = |
|
488 InductivePackage.declare_rules rec_name coind no_ind cnames |
|
489 (map (to_set [] (Context.Proof ctxt3)) intrs) intr_names intr_atts |
|
490 (map (fn th => (to_set [] (Context.Proof ctxt3) th, |
|
491 map fst (fst (RuleCases.get th)))) elims) |
|
492 raw_induct' ctxt3 |
|
493 in |
|
494 ({intrs = intrs', elims = elims', induct = induct, |
|
495 raw_induct = raw_induct', preds = map fst defs}, |
|
496 ctxt4) |
|
497 end; |
|
498 |
|
499 val add_inductive_i = InductivePackage.gen_add_inductive_i add_ind_set_def; |
|
500 val add_inductive = InductivePackage.gen_add_inductive add_ind_set_def; |
|
501 |
|
502 val mono_add_att = to_pred_att [] #> InductivePackage.mono_add; |
|
503 val mono_del_att = to_pred_att [] #> InductivePackage.mono_del; |
|
504 |
|
505 |
|
506 (** package setup **) |
|
507 |
|
508 (* setup theory *) |
|
509 |
|
510 val setup = |
|
511 Attrib.add_attributes |
|
512 [("pred_set_conv", Attrib.no_args pred_set_conv_att, |
|
513 "declare rules for converting between predicate and set notation"), |
|
514 ("to_set", Attrib.syntax (Attrib.thms >> to_set_att), |
|
515 "convert rule to set notation"), |
|
516 ("to_pred", Attrib.syntax (Attrib.thms >> to_pred_att), |
|
517 "convert rule to predicate notation")] #> |
|
518 Codegen.add_attribute "ind_set" |
|
519 (Scan.option (Args.$$$ "target" |-- Args.colon |-- Args.name) >> code_ind_att) #> |
|
520 Codegen.add_preprocessor codegen_preproc #> |
|
521 Attrib.add_attributes [("mono_set", Attrib.add_del_args mono_add_att mono_del_att, |
|
522 "declaration of monotonicity rule for set operators")] #> |
|
523 Context.theory_map (Simplifier.map_ss (fn ss => |
|
524 ss addsimprocs [collect_mem_simproc])); |
|
525 |
|
526 (* outer syntax *) |
|
527 |
|
528 local structure P = OuterParse and K = OuterKeyword in |
|
529 |
|
530 val ind_set_decl = InductivePackage.gen_ind_decl add_ind_set_def; |
|
531 |
|
532 val inductive_setP = |
|
533 OuterSyntax.command "inductive_set" "define inductive sets" K.thy_decl (ind_set_decl false); |
|
534 |
|
535 val coinductive_setP = |
|
536 OuterSyntax.command "coinductive_set" "define coinductive sets" K.thy_decl (ind_set_decl true); |
|
537 |
|
538 val _ = OuterSyntax.add_parsers [inductive_setP, coinductive_setP]; |
|
539 |
|
540 end; |
|
541 |
|
542 end; |