src/HOL/Inductive.thy
 author haftmann Wed Jan 30 10:57:44 2008 +0100 (2008-01-30) changeset 26013 8764a1f1253b parent 25557 ea6b11021e79 child 26793 e36a92ff543e permissions -rw-r--r--
Theorem Inductive.lfp_ordinal_induct generalized to complete lattices
1 (*  Title:      HOL/Inductive.thy
2     ID:         \$Id\$
3     Author:     Markus Wenzel, TU Muenchen
4 *)
6 header {* Knaster-Tarski Fixpoint Theorem and inductive definitions *}
8 theory Inductive
9 imports Lattices Sum_Type
10 uses
11   ("Tools/inductive_package.ML")
12   "Tools/dseq.ML"
13   ("Tools/inductive_codegen.ML")
14   ("Tools/datatype_aux.ML")
15   ("Tools/datatype_prop.ML")
16   ("Tools/datatype_rep_proofs.ML")
17   ("Tools/datatype_abs_proofs.ML")
18   ("Tools/datatype_case.ML")
19   ("Tools/datatype_package.ML")
20   ("Tools/old_primrec_package.ML")
21   ("Tools/primrec_package.ML")
22   ("Tools/datatype_codegen.ML")
23 begin
25 subsection {* Least and greatest fixed points *}
27 context complete_lattice
28 begin
30 definition
31   lfp :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a" where
32   "lfp f = Inf {u. f u \<le> u}"    --{*least fixed point*}
34 definition
35   gfp :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a" where
36   "gfp f = Sup {u. u \<le> f u}"    --{*greatest fixed point*}
39 subsection{* Proof of Knaster-Tarski Theorem using @{term lfp} *}
41 text{*@{term "lfp f"} is the least upper bound of
42       the set @{term "{u. f(u) \<le> u}"} *}
44 lemma lfp_lowerbound: "f A \<le> A ==> lfp f \<le> A"
45   by (auto simp add: lfp_def intro: Inf_lower)
47 lemma lfp_greatest: "(!!u. f u \<le> u ==> A \<le> u) ==> A \<le> lfp f"
48   by (auto simp add: lfp_def intro: Inf_greatest)
50 end
52 lemma lfp_lemma2: "mono f ==> f (lfp f) \<le> lfp f"
53   by (iprover intro: lfp_greatest order_trans monoD lfp_lowerbound)
55 lemma lfp_lemma3: "mono f ==> lfp f \<le> f (lfp f)"
56   by (iprover intro: lfp_lemma2 monoD lfp_lowerbound)
58 lemma lfp_unfold: "mono f ==> lfp f = f (lfp f)"
59   by (iprover intro: order_antisym lfp_lemma2 lfp_lemma3)
61 lemma lfp_const: "lfp (\<lambda>x. t) = t"
62   by (rule lfp_unfold) (simp add:mono_def)
65 subsection {* General induction rules for least fixed points *}
67 theorem lfp_induct:
68   assumes mono: "mono f" and ind: "f (inf (lfp f) P) <= P"
69   shows "lfp f <= P"
70 proof -
71   have "inf (lfp f) P <= lfp f" by (rule inf_le1)
72   with mono have "f (inf (lfp f) P) <= f (lfp f)" ..
73   also from mono have "f (lfp f) = lfp f" by (rule lfp_unfold [symmetric])
74   finally have "f (inf (lfp f) P) <= lfp f" .
75   from this and ind have "f (inf (lfp f) P) <= inf (lfp f) P" by (rule le_infI)
76   hence "lfp f <= inf (lfp f) P" by (rule lfp_lowerbound)
77   also have "inf (lfp f) P <= P" by (rule inf_le2)
78   finally show ?thesis .
79 qed
81 lemma lfp_induct_set:
82   assumes lfp: "a: lfp(f)"
83       and mono: "mono(f)"
84       and indhyp: "!!x. [| x: f(lfp(f) Int {x. P(x)}) |] ==> P(x)"
85   shows "P(a)"
86   by (rule lfp_induct [THEN subsetD, THEN CollectD, OF mono _ lfp])
87     (auto simp: inf_set_eq intro: indhyp)
89 lemma lfp_ordinal_induct:
90   fixes f :: "'a\<Colon>complete_lattice \<Rightarrow> 'a"
91   assumes mono: "mono f"
92   and P_f: "\<And>S. P S \<Longrightarrow> P (f S)"
93   and P_Union: "\<And>M. \<forall>S\<in>M. P S \<Longrightarrow> P (Sup M)"
94   shows "P (lfp f)"
95 proof -
96   let ?M = "{S. S \<le> lfp f \<and> P S}"
97   have "P (Sup ?M)" using P_Union by simp
98   also have "Sup ?M = lfp f"
99   proof (rule antisym)
100     show "Sup ?M \<le> lfp f" by (blast intro: Sup_least)
101     hence "f (Sup ?M) \<le> f (lfp f)" by (rule mono [THEN monoD])
102     hence "f (Sup ?M) \<le> lfp f" using mono [THEN lfp_unfold] by simp
103     hence "f (Sup ?M) \<in> ?M" using P_f P_Union by simp
104     hence "f (Sup ?M) \<le> Sup ?M" by (rule Sup_upper)
105     thus "lfp f \<le> Sup ?M" by (rule lfp_lowerbound)
106   qed
107   finally show ?thesis .
108 qed
110 lemma lfp_ordinal_induct_set:
111   assumes mono: "mono f"
112   and P_f: "!!S. P S ==> P(f S)"
113   and P_Union: "!!M. !S:M. P S ==> P(Union M)"
114   shows "P(lfp f)"
115   using assms unfolding Sup_set_def [symmetric]
116   by (rule lfp_ordinal_induct)
119 text{*Definition forms of @{text lfp_unfold} and @{text lfp_induct},
120     to control unfolding*}
122 lemma def_lfp_unfold: "[| h==lfp(f);  mono(f) |] ==> h = f(h)"
123 by (auto intro!: lfp_unfold)
125 lemma def_lfp_induct:
126     "[| A == lfp(f); mono(f);
127         f (inf A P) \<le> P
128      |] ==> A \<le> P"
129   by (blast intro: lfp_induct)
131 lemma def_lfp_induct_set:
132     "[| A == lfp(f);  mono(f);   a:A;
133         !!x. [| x: f(A Int {x. P(x)}) |] ==> P(x)
134      |] ==> P(a)"
135   by (blast intro: lfp_induct_set)
137 (*Monotonicity of lfp!*)
138 lemma lfp_mono: "(!!Z. f Z \<le> g Z) ==> lfp f \<le> lfp g"
139   by (rule lfp_lowerbound [THEN lfp_greatest], blast intro: order_trans)
142 subsection {* Proof of Knaster-Tarski Theorem using @{term gfp} *}
144 text{*@{term "gfp f"} is the greatest lower bound of
145       the set @{term "{u. u \<le> f(u)}"} *}
147 lemma gfp_upperbound: "X \<le> f X ==> X \<le> gfp f"
148   by (auto simp add: gfp_def intro: Sup_upper)
150 lemma gfp_least: "(!!u. u \<le> f u ==> u \<le> X) ==> gfp f \<le> X"
151   by (auto simp add: gfp_def intro: Sup_least)
153 lemma gfp_lemma2: "mono f ==> gfp f \<le> f (gfp f)"
154   by (iprover intro: gfp_least order_trans monoD gfp_upperbound)
156 lemma gfp_lemma3: "mono f ==> f (gfp f) \<le> gfp f"
157   by (iprover intro: gfp_lemma2 monoD gfp_upperbound)
159 lemma gfp_unfold: "mono f ==> gfp f = f (gfp f)"
160   by (iprover intro: order_antisym gfp_lemma2 gfp_lemma3)
163 subsection {* Coinduction rules for greatest fixed points *}
165 text{*weak version*}
166 lemma weak_coinduct: "[| a: X;  X \<subseteq> f(X) |] ==> a : gfp(f)"
167 by (rule gfp_upperbound [THEN subsetD], auto)
169 lemma weak_coinduct_image: "!!X. [| a : X; g`X \<subseteq> f (g`X) |] ==> g a : gfp f"
170 apply (erule gfp_upperbound [THEN subsetD])
171 apply (erule imageI)
172 done
174 lemma coinduct_lemma:
175      "[| X \<le> f (sup X (gfp f));  mono f |] ==> sup X (gfp f) \<le> f (sup X (gfp f))"
176   apply (frule gfp_lemma2)
177   apply (drule mono_sup)
178   apply (rule le_supI)
179   apply assumption
180   apply (rule order_trans)
181   apply (rule order_trans)
182   apply assumption
183   apply (rule sup_ge2)
184   apply assumption
185   done
187 text{*strong version, thanks to Coen and Frost*}
188 lemma coinduct_set: "[| mono(f);  a: X;  X \<subseteq> f(X Un gfp(f)) |] ==> a : gfp(f)"
189 by (blast intro: weak_coinduct [OF _ coinduct_lemma, simplified sup_set_eq])
191 lemma coinduct: "[| mono(f); X \<le> f (sup X (gfp f)) |] ==> X \<le> gfp(f)"
192   apply (rule order_trans)
193   apply (rule sup_ge1)
194   apply (erule gfp_upperbound [OF coinduct_lemma])
195   apply assumption
196   done
198 lemma gfp_fun_UnI2: "[| mono(f);  a: gfp(f) |] ==> a: f(X Un gfp(f))"
199 by (blast dest: gfp_lemma2 mono_Un)
202 subsection {* Even Stronger Coinduction Rule, by Martin Coen *}
204 text{* Weakens the condition @{term "X \<subseteq> f(X)"} to one expressed using both
205   @{term lfp} and @{term gfp}*}
207 lemma coinduct3_mono_lemma: "mono(f) ==> mono(%x. f(x) Un X Un B)"
208 by (iprover intro: subset_refl monoI Un_mono monoD)
210 lemma coinduct3_lemma:
211      "[| X \<subseteq> f(lfp(%x. f(x) Un X Un gfp(f)));  mono(f) |]
212       ==> lfp(%x. f(x) Un X Un gfp(f)) \<subseteq> f(lfp(%x. f(x) Un X Un gfp(f)))"
213 apply (rule subset_trans)
214 apply (erule coinduct3_mono_lemma [THEN lfp_lemma3])
215 apply (rule Un_least [THEN Un_least])
216 apply (rule subset_refl, assumption)
217 apply (rule gfp_unfold [THEN equalityD1, THEN subset_trans], assumption)
218 apply (rule monoD, assumption)
219 apply (subst coinduct3_mono_lemma [THEN lfp_unfold], auto)
220 done
222 lemma coinduct3:
223   "[| mono(f);  a:X;  X \<subseteq> f(lfp(%x. f(x) Un X Un gfp(f))) |] ==> a : gfp(f)"
224 apply (rule coinduct3_lemma [THEN  weak_coinduct])
225 apply (rule coinduct3_mono_lemma [THEN lfp_unfold, THEN ssubst], auto)
226 done
229 text{*Definition forms of @{text gfp_unfold} and @{text coinduct},
230     to control unfolding*}
232 lemma def_gfp_unfold: "[| A==gfp(f);  mono(f) |] ==> A = f(A)"
233 by (auto intro!: gfp_unfold)
235 lemma def_coinduct:
236      "[| A==gfp(f);  mono(f);  X \<le> f(sup X A) |] ==> X \<le> A"
237 by (iprover intro!: coinduct)
239 lemma def_coinduct_set:
240      "[| A==gfp(f);  mono(f);  a:X;  X \<subseteq> f(X Un A) |] ==> a: A"
241 by (auto intro!: coinduct_set)
243 (*The version used in the induction/coinduction package*)
244 lemma def_Collect_coinduct:
245     "[| A == gfp(%w. Collect(P(w)));  mono(%w. Collect(P(w)));
246         a: X;  !!z. z: X ==> P (X Un A) z |] ==>
247      a : A"
248 apply (erule def_coinduct_set, auto)
249 done
251 lemma def_coinduct3:
252     "[| A==gfp(f); mono(f);  a:X;  X \<subseteq> f(lfp(%x. f(x) Un X Un A)) |] ==> a: A"
253 by (auto intro!: coinduct3)
255 text{*Monotonicity of @{term gfp}!*}
256 lemma gfp_mono: "(!!Z. f Z \<le> g Z) ==> gfp f \<le> gfp g"
257   by (rule gfp_upperbound [THEN gfp_least], blast intro: order_trans)
260 subsection {* Inductive predicates and sets *}
262 text {* Inversion of injective functions. *}
264 constdefs
265   myinv :: "('a => 'b) => ('b => 'a)"
266   "myinv (f :: 'a => 'b) == \<lambda>y. THE x. f x = y"
268 lemma myinv_f_f: "inj f ==> myinv f (f x) = x"
269 proof -
270   assume "inj f"
271   hence "(THE x'. f x' = f x) = (THE x'. x' = x)"
272     by (simp only: inj_eq)
273   also have "... = x" by (rule the_eq_trivial)
274   finally show ?thesis by (unfold myinv_def)
275 qed
277 lemma f_myinv_f: "inj f ==> y \<in> range f ==> f (myinv f y) = y"
278 proof (unfold myinv_def)
279   assume inj: "inj f"
280   assume "y \<in> range f"
281   then obtain x where "y = f x" ..
282   hence x: "f x = y" ..
283   thus "f (THE x. f x = y) = y"
284   proof (rule theI)
285     fix x' assume "f x' = y"
286     with x have "f x' = f x" by simp
287     with inj show "x' = x" by (rule injD)
288   qed
289 qed
291 hide const myinv
294 text {* Package setup. *}
296 theorems basic_monos =
297   subset_refl imp_refl disj_mono conj_mono ex_mono all_mono if_bool_eq_conj
298   Collect_mono in_mono vimage_mono
299   imp_conv_disj not_not de_Morgan_disj de_Morgan_conj
300   not_all not_ex
301   Ball_def Bex_def
302   induct_rulify_fallback
304 ML {*
305 val def_lfp_unfold = @{thm def_lfp_unfold}
306 val def_gfp_unfold = @{thm def_gfp_unfold}
307 val def_lfp_induct = @{thm def_lfp_induct}
308 val def_coinduct = @{thm def_coinduct}
309 val inf_bool_eq = @{thm inf_bool_eq} RS @{thm eq_reflection}
310 val inf_fun_eq = @{thm inf_fun_eq} RS @{thm eq_reflection}
311 val sup_bool_eq = @{thm sup_bool_eq} RS @{thm eq_reflection}
312 val sup_fun_eq = @{thm sup_fun_eq} RS @{thm eq_reflection}
313 val le_boolI = @{thm le_boolI}
314 val le_boolI' = @{thm le_boolI'}
315 val le_funI = @{thm le_funI}
316 val le_boolE = @{thm le_boolE}
317 val le_funE = @{thm le_funE}
318 val le_boolD = @{thm le_boolD}
319 val le_funD = @{thm le_funD}
320 val le_bool_def = @{thm le_bool_def} RS @{thm eq_reflection}
321 val le_fun_def = @{thm le_fun_def} RS @{thm eq_reflection}
322 *}
324 use "Tools/inductive_package.ML"
325 setup InductivePackage.setup
327 theorems [mono] =
328   imp_refl disj_mono conj_mono ex_mono all_mono if_bool_eq_conj
329   imp_conv_disj not_not de_Morgan_disj de_Morgan_conj
330   not_all not_ex
331   Ball_def Bex_def
332   induct_rulify_fallback
335 subsection {* Inductive datatypes and primitive recursion *}
337 text {* Package setup. *}
339 use "Tools/datatype_aux.ML"
340 use "Tools/datatype_prop.ML"
341 use "Tools/datatype_rep_proofs.ML"
342 use "Tools/datatype_abs_proofs.ML"
343 use "Tools/datatype_case.ML"
344 use "Tools/datatype_package.ML"
345 setup DatatypePackage.setup
346 use "Tools/old_primrec_package.ML"
347 use "Tools/primrec_package.ML"
349 use "Tools/datatype_codegen.ML"
350 setup DatatypeCodegen.setup
352 use "Tools/inductive_codegen.ML"
353 setup InductiveCodegen.setup
355 text{* Lambda-abstractions with pattern matching: *}
357 syntax
358   "_lam_pats_syntax" :: "cases_syn => 'a => 'b"               ("(%_)" 10)
359 syntax (xsymbols)
360   "_lam_pats_syntax" :: "cases_syn => 'a => 'b"               ("(\<lambda>_)" 10)
362 parse_translation (advanced) {*
363 let
364   fun fun_tr ctxt [cs] =
365     let
366       val x = Free (Name.variant (add_term_free_names (cs, [])) "x", dummyT);
367       val ft = DatatypeCase.case_tr true DatatypePackage.datatype_of_constr
368                  ctxt [x, cs]
369     in lambda x ft end
370 in [("_lam_pats_syntax", fun_tr)] end
371 *}
373 end