src/HOL/Complete_Partial_Order.thy
 author wenzelm Sun Nov 02 18:21:45 2014 +0100 (2014-11-02) changeset 58889 5b7a9633cfa8 parent 54630 9061af4d5ebc child 60057 86fa63ce8156 permissions -rw-r--r--
1 (* Title:    HOL/Complete_Partial_Order.thy
2    Author:   Brian Huffman, Portland State University
3    Author:   Alexander Krauss, TU Muenchen
4 *)
6 section {* Chain-complete partial orders and their fixpoints *}
8 theory Complete_Partial_Order
9 imports Product_Type
10 begin
12 subsection {* Monotone functions *}
14 text {* Dictionary-passing version of @{const Orderings.mono}. *}
16 definition monotone :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> bool"
17 where "monotone orda ordb f \<longleftrightarrow> (\<forall>x y. orda x y \<longrightarrow> ordb (f x) (f y))"
19 lemma monotoneI[intro?]: "(\<And>x y. orda x y \<Longrightarrow> ordb (f x) (f y))
20  \<Longrightarrow> monotone orda ordb f"
21 unfolding monotone_def by iprover
23 lemma monotoneD[dest?]: "monotone orda ordb f \<Longrightarrow> orda x y \<Longrightarrow> ordb (f x) (f y)"
24 unfolding monotone_def by iprover
27 subsection {* Chains *}
29 text {* A chain is a totally-ordered set. Chains are parameterized over
30   the order for maximal flexibility, since type classes are not enough.
31 *}
33 definition
34   chain :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool"
35 where
36   "chain ord S \<longleftrightarrow> (\<forall>x\<in>S. \<forall>y\<in>S. ord x y \<or> ord y x)"
38 lemma chainI:
39   assumes "\<And>x y. x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> ord x y \<or> ord y x"
40   shows "chain ord S"
41 using assms unfolding chain_def by fast
43 lemma chainD:
44   assumes "chain ord S" and "x \<in> S" and "y \<in> S"
45   shows "ord x y \<or> ord y x"
46 using assms unfolding chain_def by fast
48 lemma chainE:
49   assumes "chain ord S" and "x \<in> S" and "y \<in> S"
50   obtains "ord x y" | "ord y x"
51 using assms unfolding chain_def by fast
53 lemma chain_empty: "chain ord {}"
56 subsection {* Chain-complete partial orders *}
58 text {*
59   A ccpo has a least upper bound for any chain.  In particular, the
60   empty set is a chain, so every ccpo must have a bottom element.
61 *}
63 class ccpo = order + Sup +
64   assumes ccpo_Sup_upper: "\<lbrakk>chain (op \<le>) A; x \<in> A\<rbrakk> \<Longrightarrow> x \<le> Sup A"
65   assumes ccpo_Sup_least: "\<lbrakk>chain (op \<le>) A; \<And>x. x \<in> A \<Longrightarrow> x \<le> z\<rbrakk> \<Longrightarrow> Sup A \<le> z"
66 begin
68 subsection {* Transfinite iteration of a function *}
70 inductive_set iterates :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a set"
71 for f :: "'a \<Rightarrow> 'a"
72 where
73   step: "x \<in> iterates f \<Longrightarrow> f x \<in> iterates f"
74 | Sup: "chain (op \<le>) M \<Longrightarrow> \<forall>x\<in>M. x \<in> iterates f \<Longrightarrow> Sup M \<in> iterates f"
76 lemma iterates_le_f:
77   "x \<in> iterates f \<Longrightarrow> monotone (op \<le>) (op \<le>) f \<Longrightarrow> x \<le> f x"
78 by (induct x rule: iterates.induct)
79   (force dest: monotoneD intro!: ccpo_Sup_upper ccpo_Sup_least)+
81 lemma chain_iterates:
82   assumes f: "monotone (op \<le>) (op \<le>) f"
83   shows "chain (op \<le>) (iterates f)" (is "chain _ ?C")
84 proof (rule chainI)
85   fix x y assume "x \<in> ?C" "y \<in> ?C"
86   then show "x \<le> y \<or> y \<le> x"
87   proof (induct x arbitrary: y rule: iterates.induct)
88     fix x y assume y: "y \<in> ?C"
89     and IH: "\<And>z. z \<in> ?C \<Longrightarrow> x \<le> z \<or> z \<le> x"
90     from y show "f x \<le> y \<or> y \<le> f x"
91     proof (induct y rule: iterates.induct)
92       case (step y) with IH f show ?case by (auto dest: monotoneD)
93     next
94       case (Sup M)
95       then have chM: "chain (op \<le>) M"
96         and IH': "\<And>z. z \<in> M \<Longrightarrow> f x \<le> z \<or> z \<le> f x" by auto
97       show "f x \<le> Sup M \<or> Sup M \<le> f x"
98       proof (cases "\<exists>z\<in>M. f x \<le> z")
99         case True then have "f x \<le> Sup M"
100           apply rule
101           apply (erule order_trans)
102           by (rule ccpo_Sup_upper[OF chM])
103         thus ?thesis ..
104       next
105         case False with IH'
106         show ?thesis by (auto intro: ccpo_Sup_least[OF chM])
107       qed
108     qed
109   next
110     case (Sup M y)
111     show ?case
112     proof (cases "\<exists>x\<in>M. y \<le> x")
113       case True then have "y \<le> Sup M"
114         apply rule
115         apply (erule order_trans)
116         by (rule ccpo_Sup_upper[OF Sup(1)])
117       thus ?thesis ..
118     next
119       case False with Sup
120       show ?thesis by (auto intro: ccpo_Sup_least)
121     qed
122   qed
123 qed
125 lemma bot_in_iterates: "Sup {} \<in> iterates f"
126 by(auto intro: iterates.Sup simp add: chain_empty)
128 subsection {* Fixpoint combinator *}
130 definition
131   fixp :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a"
132 where
133   "fixp f = Sup (iterates f)"
135 lemma iterates_fixp:
136   assumes f: "monotone (op \<le>) (op \<le>) f" shows "fixp f \<in> iterates f"
137 unfolding fixp_def
138 by (simp add: iterates.Sup chain_iterates f)
140 lemma fixp_unfold:
141   assumes f: "monotone (op \<le>) (op \<le>) f"
142   shows "fixp f = f (fixp f)"
143 proof (rule antisym)
144   show "fixp f \<le> f (fixp f)"
145     by (intro iterates_le_f iterates_fixp f)
146   have "f (fixp f) \<le> Sup (iterates f)"
147     by (intro ccpo_Sup_upper chain_iterates f iterates.step iterates_fixp)
148   thus "f (fixp f) \<le> fixp f"
149     unfolding fixp_def .
150 qed
152 lemma fixp_lowerbound:
153   assumes f: "monotone (op \<le>) (op \<le>) f" and z: "f z \<le> z" shows "fixp f \<le> z"
154 unfolding fixp_def
155 proof (rule ccpo_Sup_least[OF chain_iterates[OF f]])
156   fix x assume "x \<in> iterates f"
157   thus "x \<le> z"
158   proof (induct x rule: iterates.induct)
159     fix x assume "x \<le> z" with f have "f x \<le> f z" by (rule monotoneD)
160     also note z finally show "f x \<le> z" .
161   qed (auto intro: ccpo_Sup_least)
162 qed
164 end
166 subsection {* Fixpoint induction *}
168 setup {* Sign.map_naming (Name_Space.mandatory_path "ccpo") *}
170 definition admissible :: "('a set \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
171 where "admissible lub ord P = (\<forall>A. chain ord A \<longrightarrow> (A \<noteq> {}) \<longrightarrow> (\<forall>x\<in>A. P x) \<longrightarrow> P (lub A))"
174   assumes "\<And>A. chain ord A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> \<forall>x\<in>A. P x \<Longrightarrow> P (lub A)"
175   shows "ccpo.admissible lub ord P"
176 using assms unfolding ccpo.admissible_def by fast
179   assumes "ccpo.admissible lub ord P"
180   assumes "chain ord A"
181   assumes "A \<noteq> {}"
182   assumes "\<And>x. x \<in> A \<Longrightarrow> P x"
183   shows "P (lub A)"
184 using assms by (auto simp: ccpo.admissible_def)
186 setup {* Sign.map_naming Name_Space.parent_path *}
188 lemma (in ccpo) fixp_induct:
190   assumes mono: "monotone (op \<le>) (op \<le>) f"
191   assumes bot: "P (Sup {})"
192   assumes step: "\<And>x. P x \<Longrightarrow> P (f x)"
193   shows "P (fixp f)"
194 unfolding fixp_def using adm chain_iterates[OF mono]
196   show "iterates f \<noteq> {}" using bot_in_iterates by auto
197   fix x assume "x \<in> iterates f"
198   thus "P x"
199     by (induct rule: iterates.induct)
201 qed
207 unfolding ccpo.admissible_def chain_def by simp
208 *)
213   assumes "ccpo.admissible lub ord (\<lambda>x. P x)"
214   assumes "ccpo.admissible lub ord (\<lambda>x. Q x)"
215   shows "ccpo.admissible lub ord (\<lambda>x. P x \<and> Q x)"
216 using assms unfolding ccpo.admissible_def by simp
219   assumes "\<And>y. ccpo.admissible lub ord (\<lambda>x. P x y)"
220   shows "ccpo.admissible lub ord (\<lambda>x. \<forall>y. P x y)"
221 using assms unfolding ccpo.admissible_def by fast
224   assumes "\<And>y. y \<in> A \<Longrightarrow> ccpo.admissible lub ord (\<lambda>x. P x y)"
225   shows "ccpo.admissible lub ord (\<lambda>x. \<forall>y\<in>A. P x y)"
226 using assms unfolding ccpo.admissible_def by fast
228 lemma chain_compr: "chain ord A \<Longrightarrow> chain ord {x \<in> A. P x}"
229 unfolding chain_def by fast
231 context ccpo begin
234   assumes A: "chain (op \<le>)A"
235   assumes P: "\<forall>x\<in>A. \<exists>y\<in>A. x \<le> y \<and> P y"
236   shows "Sup A = Sup {x \<in> A. P x}"
237 proof (rule antisym)
238   have *: "chain (op \<le>) {x \<in> A. P x}"
239     by (rule chain_compr [OF A])
240   show "Sup A \<le> Sup {x \<in> A. P x}"
241     apply (rule ccpo_Sup_least [OF A])
242     apply (drule P [rule_format], clarify)
243     apply (erule order_trans)
244     apply (simp add: ccpo_Sup_upper [OF *])
245     done
246   show "Sup {x \<in> A. P x} \<le> Sup A"
247     apply (rule ccpo_Sup_least [OF *])
248     apply clarify
249     apply (simp add: ccpo_Sup_upper [OF A])
250     done
251 qed
254   fixes P Q :: "'a \<Rightarrow> bool"
255   assumes P: "ccpo.admissible Sup (op \<le>) (\<lambda>x. P x)"
256   assumes Q: "ccpo.admissible Sup (op \<le>) (\<lambda>x. Q x)"
257   shows "ccpo.admissible Sup (op \<le>) (\<lambda>x. P x \<or> Q x)"
259   fix A :: "'a set" assume A: "chain (op \<le>) A"
260   assume "A \<noteq> {}"
261     and "\<forall>x\<in>A. P x \<or> Q x"
262   hence "(\<exists>x\<in>A. P x) \<and> (\<forall>x\<in>A. \<exists>y\<in>A. x \<le> y \<and> P y) \<or> (\<exists>x\<in>A. Q x) \<and> (\<forall>x\<in>A. \<exists>y\<in>A. x \<le> y \<and> Q y)"
263     using chainD[OF A] by blast
264   hence "(\<exists>x. x \<in> A \<and> P x) \<and> Sup A = Sup {x \<in> A. P x} \<or> (\<exists>x. x \<in> A \<and> Q x) \<and> Sup A = Sup {x \<in> A. Q x}"
265     using admissible_disj_lemma [OF A] by blast
266   thus "P (Sup A) \<or> Q (Sup A)"
267     apply (rule disjE, simp_all)
268     apply (rule disjI1, rule ccpo.admissibleD [OF P chain_compr [OF A]], simp, simp)
269     apply (rule disjI2, rule ccpo.admissibleD [OF Q chain_compr [OF A]], simp, simp)
270     done
271 qed
273 end
275 instance complete_lattice \<subseteq> ccpo
276   by default (fast intro: Sup_upper Sup_least)+
278 lemma lfp_eq_fixp:
279   assumes f: "mono f" shows "lfp f = fixp f"
280 proof (rule antisym)
281   from f have f': "monotone (op \<le>) (op \<le>) f"
282     unfolding mono_def monotone_def .
283   show "lfp f \<le> fixp f"
284     by (rule lfp_lowerbound, subst fixp_unfold [OF f'], rule order_refl)
285   show "fixp f \<le> lfp f"
286     by (rule fixp_lowerbound [OF f'], subst lfp_unfold [OF f], rule order_refl)
287 qed
289 hide_const (open) iterates fixp
291 end