author | Christian Urban <urbanc@in.tum.de> |
Sat, 07 Feb 2009 12:05:02 +0000 | |
changeset 102 | 5e309df58557 |
parent 91 | 667a0943c40b |
child 110 | 12533bb49615 |
permissions | -rw-r--r-- |
32 | 1 |
(* @chunk SIMPLE_INDUCTIVE_PACKAGE *) |
2 |
signature SIMPLE_INDUCTIVE_PACKAGE = |
|
3 |
sig |
|
4 |
val add_inductive_i: |
|
76
b99fa5fa63fc
adapted to changes in binding.ML
Christian Urban <urbanc@in.tum.de>
parents:
55
diff
changeset
|
5 |
((Binding.binding * typ) * mixfix) list -> (*{predicates}*) |
b99fa5fa63fc
adapted to changes in binding.ML
Christian Urban <urbanc@in.tum.de>
parents:
55
diff
changeset
|
6 |
(Binding.binding * typ) list -> (*{parameters}*) |
32 | 7 |
(Attrib.binding * term) list -> (*{rules}*) |
8 |
local_theory -> (thm list * thm list) * local_theory |
|
9 |
val add_inductive: |
|
76
b99fa5fa63fc
adapted to changes in binding.ML
Christian Urban <urbanc@in.tum.de>
parents:
55
diff
changeset
|
10 |
(Binding.binding * string option * mixfix) list -> (*{predicates}*) |
b99fa5fa63fc
adapted to changes in binding.ML
Christian Urban <urbanc@in.tum.de>
parents:
55
diff
changeset
|
11 |
(Binding.binding * string option * mixfix) list -> (*{parameters}*) |
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
12 |
(Attrib.binding * string list) list list -> (*{rules}*) |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
13 |
local_theory -> local_theory |
32 | 14 |
end; |
15 |
(* @end *) |
|
16 |
||
17 |
structure SimpleInductivePackage: SIMPLE_INDUCTIVE_PACKAGE = |
|
18 |
struct |
|
19 |
||
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
20 |
fun mk_all x P = HOLogic.all_const (fastype_of x) $ lambda x P |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
21 |
|
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
22 |
fun inst_spec ct = Drule.instantiate' |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
23 |
[SOME (ctyp_of_term ct)] [NONE, SOME ct] @{thm spec}; |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
24 |
|
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
25 |
val all_elims = fold (fn ct => fn th => th RS inst_spec ct); |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
26 |
val imp_elims = fold (fn th => fn th' => [th', th] MRS @{thm mp}); |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
27 |
|
32 | 28 |
fun add_inductive_i preds_syn params intrs lthy = |
29 |
let |
|
55 | 30 |
val params' = map (fn (p, T) => Free (Binding.base_name p, T)) params; |
32 | 31 |
val preds = map (fn ((R, T), _) => |
55 | 32 |
list_comb (Free (Binding.base_name R, T), params')) preds_syn; |
32 | 33 |
val Tss = map (binder_types o fastype_of) preds; |
34 |
||
35 |
(* making the definition *) |
|
36 |
||
37 |
val intrs' = map |
|
38 |
(ObjectLogic.atomize_term (ProofContext.theory_of lthy) o snd) intrs; |
|
39 |
||
40 |
val (defs, lthy1) = fold_map (fn ((((R, _), syn), pred), Ts) => |
|
41 |
let val zs = map Free (Variable.variant_frees lthy intrs' |
|
42 |
(map (pair "z") Ts)) |
|
43 |
in |
|
44 |
LocalTheory.define Thm.internalK |
|
55 | 45 |
((R, syn), (Attrib.empty_binding, fold_rev lambda (params' @ zs) |
32 | 46 |
(fold_rev mk_all preds (fold_rev (curry HOLogic.mk_imp) |
47 |
intrs' (list_comb (pred, zs)))))) #>> snd #>> snd |
|
48 |
end) (preds_syn ~~ preds ~~ Tss) lthy; |
|
49 |
||
55 | 50 |
val (_, lthy2) = Variable.add_fixes (map (Binding.base_name o fst) params) lthy1; |
91
667a0943c40b
added a section that will eventually describe the code
Christian Urban <urbanc@in.tum.de>
parents:
76
diff
changeset
|
51 |
|
667a0943c40b
added a section that will eventually describe the code
Christian Urban <urbanc@in.tum.de>
parents:
76
diff
changeset
|
52 |
|
32 | 53 |
(* proving the induction rules *) |
91
667a0943c40b
added a section that will eventually describe the code
Christian Urban <urbanc@in.tum.de>
parents:
76
diff
changeset
|
54 |
(* @chunk induction_rules *) |
32 | 55 |
val (Pnames, lthy3) = |
56 |
Variable.variant_fixes (replicate (length preds) "P") lthy2; |
|
57 |
val Ps = map (fn (s, Ts) => Free (s, Ts ---> HOLogic.boolT)) |
|
58 |
(Pnames ~~ Tss); |
|
59 |
val cPs = map (cterm_of (ProofContext.theory_of lthy3)) Ps; |
|
60 |
val intrs'' = map (subst_free (preds ~~ Ps) o snd) intrs; |
|
61 |
||
62 |
fun prove_indrule ((R, P), Ts) = |
|
63 |
let |
|
64 |
val (znames, lthy4) = |
|
65 |
Variable.variant_fixes (replicate (length Ts) "z") lthy3; |
|
66 |
val zs = map Free (znames ~~ Ts) |
|
67 |
in |
|
68 |
Goal.prove lthy4 [] |
|
69 |
[HOLogic.mk_Trueprop (list_comb (R, zs))] |
|
70 |
(Logic.list_implies (intrs'', |
|
71 |
HOLogic.mk_Trueprop (list_comb (P, zs)))) |
|
72 |
(fn {prems, ...} => EVERY |
|
73 |
([ObjectLogic.full_atomize_tac 1, |
|
74 |
cut_facts_tac prems 1, |
|
75 |
rewrite_goals_tac defs] @ |
|
76 |
map (fn ct => dtac (inst_spec ct) 1) cPs @ |
|
77 |
[assume_tac 1])) |> |
|
78 |
singleton (ProofContext.export lthy4 lthy1) |
|
79 |
end; |
|
80 |
||
81 |
val indrules = map prove_indrule (preds ~~ Ps ~~ Tss); |
|
91
667a0943c40b
added a section that will eventually describe the code
Christian Urban <urbanc@in.tum.de>
parents:
76
diff
changeset
|
82 |
(* @end *) |
32 | 83 |
|
84 |
(* proving the introduction rules *) |
|
91
667a0943c40b
added a section that will eventually describe the code
Christian Urban <urbanc@in.tum.de>
parents:
76
diff
changeset
|
85 |
(* @chunk intro_rules *) |
32 | 86 |
fun prove_intr (i, (_, r)) = |
87 |
Goal.prove lthy2 [] [] r |
|
88 |
(fn {prems, context = ctxt} => EVERY |
|
89 |
[ObjectLogic.rulify_tac 1, |
|
90 |
rewrite_goals_tac defs, |
|
91 |
REPEAT (resolve_tac [allI, impI] 1), |
|
92 |
SUBPROOF (fn {params, prems, context = ctxt', ...} => |
|
93 |
let |
|
94 |
val (prems1, prems2) = |
|
95 |
chop (length prems - length intrs) prems; |
|
96 |
val (params1, params2) = |
|
97 |
chop (length params - length preds) params |
|
98 |
in |
|
99 |
rtac (ObjectLogic.rulify |
|
100 |
(all_elims params1 (nth prems2 i))) 1 THEN |
|
101 |
EVERY (map (fn prem => |
|
102 |
SUBPROOF (fn {prems = prems', concl, ...} => |
|
103 |
let |
|
104 |
val prem' = prems' MRS prem; |
|
105 |
val prem'' = case prop_of prem' of |
|
106 |
_ $ (Const (@{const_name All}, _) $ _) => |
|
107 |
prem' |> all_elims params2 |> |
|
108 |
imp_elims prems2 |
|
109 |
| _ => prem' |
|
110 |
in rtac prem'' 1 end) ctxt' 1) prems1) |
|
111 |
end) ctxt 1]) |> |
|
112 |
singleton (ProofContext.export lthy2 lthy1); |
|
113 |
||
114 |
val intr_ths = map_index prove_intr intrs; |
|
91
667a0943c40b
added a section that will eventually describe the code
Christian Urban <urbanc@in.tum.de>
parents:
76
diff
changeset
|
115 |
(* @end *) |
32 | 116 |
|
117 |
(* storing the theorems *) |
|
91
667a0943c40b
added a section that will eventually describe the code
Christian Urban <urbanc@in.tum.de>
parents:
76
diff
changeset
|
118 |
(* @chunk storing *) |
55 | 119 |
val mut_name = space_implode "_" (map (Binding.base_name o fst o fst) preds_syn); |
120 |
val case_names = map (Binding.base_name o fst o fst) intrs |
|
91
667a0943c40b
added a section that will eventually describe the code
Christian Urban <urbanc@in.tum.de>
parents:
76
diff
changeset
|
121 |
(* @end *) |
32 | 122 |
in |
123 |
lthy1 |> |
|
124 |
LocalTheory.notes Thm.theoremK (map (fn (((a, atts), _), th) => |
|
55 | 125 |
((Binding.qualify mut_name a, atts), [([th], [])])) |
32 | 126 |
(intrs ~~ intr_ths)) |-> |
127 |
(fn intr_thss => LocalTheory.note Thm.theoremK |
|
55 | 128 |
((Binding.qualify mut_name (Binding.name "intros"), []), maps snd intr_thss)) |>> |
32 | 129 |
snd ||>> |
130 |
(LocalTheory.notes Thm.theoremK (map (fn (((R, _), _), th) => |
|
55 | 131 |
((Binding.qualify (Binding.base_name R) (Binding.name "induct"), |
32 | 132 |
[Attrib.internal (K (RuleCases.case_names case_names)), |
133 |
Attrib.internal (K (RuleCases.consumes 1)), |
|
134 |
Attrib.internal (K (Induct.induct_pred ""))]), [([th], [])])) |
|
135 |
(preds_syn ~~ indrules)) #>> maps snd) |
|
136 |
end; |
|
91
667a0943c40b
added a section that will eventually describe the code
Christian Urban <urbanc@in.tum.de>
parents:
76
diff
changeset
|
137 |
|
32 | 138 |
(* @chunk add_inductive *) |
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
139 |
fun add_inductive preds params specs lthy = |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
140 |
let |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
141 |
val ((vars, specs'), _) = Specification.read_specification (preds @ params) specs lthy; |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
142 |
val (preds', params') = chop (length preds) vars; |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
143 |
val specs'' = map (apsnd the_single) specs' |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
144 |
val params'' = map fst params' |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
145 |
in |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
146 |
snd (add_inductive_i preds' params'' specs'' lthy) |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
147 |
end; |
32 | 148 |
(* @end *) |
149 |
||
150 |
||
151 |
(* outer syntax *) |
|
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
152 |
(* @chunk syntax *) |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
153 |
val parser = |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
154 |
OuterParse.opt_target -- |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
155 |
OuterParse.fixes -- |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
156 |
OuterParse.for_fixes -- |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
157 |
Scan.optional |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
158 |
(OuterParse.$$$ "where" |-- |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
159 |
OuterParse.!!! |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
160 |
(OuterParse.enum1 "|" |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
161 |
((SpecParse.opt_thm_name ":" -- |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
162 |
(OuterParse.prop >> single)) >> single))) [] |
32 | 163 |
|
164 |
||
165 |
val ind_decl = |
|
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
166 |
parser >> |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
167 |
(fn (((loc, preds), params), specs) => |
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
168 |
Toplevel.local_theory loc (add_inductive preds params specs)); |
32 | 169 |
|
170 |
val _ = OuterSyntax.command "simple_inductive" "define inductive predicates" |
|
102
5e309df58557
general cleaning up; deleted antiquotation ML_text; adjusted pathnames of various files in the distribution
Christian Urban <urbanc@in.tum.de>
parents:
91
diff
changeset
|
171 |
OuterKeyword.thy_decl ind_decl; |
32 | 172 |
(* @end *) |
173 |
||
174 |
end; |