1 (* Title: HOL/Complete_Partial_Order.thy
2 Author: Brian Huffman, Portland State University
3 Author: Alexander Krauss, TU Muenchen
6 header {* Chain-complete partial orders and their fixpoints *}
8 theory Complete_Partial_Order
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.
34 chain :: "('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> bool"
36 "chain ord S \<longleftrightarrow> (\<forall>x\<in>S. \<forall>y\<in>S. ord x y \<or> ord y x)"
39 assumes "\<And>x y. x \<in> S \<Longrightarrow> y \<in> S \<Longrightarrow> ord x y \<or> ord y x"
41 using assms unfolding chain_def by fast
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
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 subsection {* Chain-complete partial orders *}
56 A ccpo has a least upper bound for any chain. In particular, the
57 empty set is a chain, so every ccpo must have a bottom element.
61 fixes lub :: "'a set \<Rightarrow> 'a"
62 assumes lub_upper: "chain (op \<le>) A \<Longrightarrow> x \<in> A \<Longrightarrow> x \<le> lub A"
63 assumes lub_least: "chain (op \<le>) A \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> x \<le> z) \<Longrightarrow> lub A \<le> z"
66 subsection {* Transfinite iteration of a function *}
68 inductive_set iterates :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a set"
69 for f :: "'a \<Rightarrow> 'a"
71 step: "x \<in> iterates f \<Longrightarrow> f x \<in> iterates f"
72 | lub: "chain (op \<le>) M \<Longrightarrow> \<forall>x\<in>M. x \<in> iterates f \<Longrightarrow> lub M \<in> iterates f"
75 "x \<in> iterates f \<Longrightarrow> monotone (op \<le>) (op \<le>) f \<Longrightarrow> x \<le> f x"
76 by (induct x rule: iterates.induct)
77 (force dest: monotoneD intro!: lub_upper lub_least)+
80 assumes f: "monotone (op \<le>) (op \<le>) f"
81 shows "chain (op \<le>) (iterates f)" (is "chain _ ?C")
83 fix x y assume "x \<in> ?C" "y \<in> ?C"
84 then show "x \<le> y \<or> y \<le> x"
85 proof (induct x arbitrary: y rule: iterates.induct)
86 fix x y assume y: "y \<in> ?C"
87 and IH: "\<And>z. z \<in> ?C \<Longrightarrow> x \<le> z \<or> z \<le> x"
88 from y show "f x \<le> y \<or> y \<le> f x"
89 proof (induct y rule: iterates.induct)
90 case (step y) with IH f show ?case by (auto dest: monotoneD)
93 then have chM: "chain (op \<le>) M"
94 and IH': "\<And>z. z \<in> M \<Longrightarrow> f x \<le> z \<or> z \<le> f x" by auto
95 show "f x \<le> lub M \<or> lub M \<le> f x"
96 proof (cases "\<exists>z\<in>M. f x \<le> z")
97 case True then have "f x \<le> lub M"
99 apply (erule order_trans)
100 by (rule lub_upper[OF chM])
104 show ?thesis by (auto intro: lub_least[OF chM])
110 proof (cases "\<exists>x\<in>M. y \<le> x")
111 case True then have "y \<le> lub M"
113 apply (erule order_trans)
114 by (rule lub_upper[OF lub(1)])
118 show ?thesis by (auto intro: lub_least)
123 subsection {* Fixpoint combinator *}
126 fixp :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a"
128 "fixp f = lub (iterates f)"
131 assumes f: "monotone (op \<le>) (op \<le>) f" shows "fixp f \<in> iterates f"
133 by (simp add: iterates.lub chain_iterates f)
136 assumes f: "monotone (op \<le>) (op \<le>) f"
137 shows "fixp f = f (fixp f)"
139 show "fixp f \<le> f (fixp f)"
140 by (intro iterates_le_f iterates_fixp f)
141 have "f (fixp f) \<le> lub (iterates f)"
142 by (intro lub_upper chain_iterates f iterates.step iterates_fixp)
143 thus "f (fixp f) \<le> fixp f"
147 lemma fixp_lowerbound:
148 assumes f: "monotone (op \<le>) (op \<le>) f" and z: "f z \<le> z" shows "fixp f \<le> z"
150 proof (rule lub_least[OF chain_iterates[OF f]])
151 fix x assume "x \<in> iterates f"
153 proof (induct x rule: iterates.induct)
154 fix x assume "x \<le> z" with f have "f x \<le> f z" by (rule monotoneD)
155 also note z finally show "f x \<le> z" .
156 qed (auto intro: lub_least)
160 subsection {* Fixpoint induction *}
163 admissible :: "('a \<Rightarrow> bool) \<Rightarrow> bool"
165 "admissible P = (\<forall>A. chain (op \<le>) A \<longrightarrow> (\<forall>x\<in>A. P x) \<longrightarrow> P (lub A))"
168 assumes "\<And>A. chain (op \<le>) A \<Longrightarrow> \<forall>x\<in>A. P x \<Longrightarrow> P (lub A)"
170 using assms unfolding admissible_def by fast
173 assumes "admissible P"
174 assumes "chain (op \<le>) A"
175 assumes "\<And>x. x \<in> A \<Longrightarrow> P x"
177 using assms by (auto simp: admissible_def)
180 assumes adm: "admissible P"
181 assumes mono: "monotone (op \<le>) (op \<le>) f"
182 assumes step: "\<And>x. P x \<Longrightarrow> P (f x)"
184 unfolding fixp_def using adm chain_iterates[OF mono]
185 proof (rule admissibleD)
186 fix x assume "x \<in> iterates f"
188 by (induct rule: iterates.induct)
189 (auto intro: step admissibleD adm)
192 lemma admissible_True: "admissible (\<lambda>x. True)"
193 unfolding admissible_def by simp
195 lemma admissible_False: "\<not> admissible (\<lambda>x. False)"
196 unfolding admissible_def chain_def by simp
198 lemma admissible_const: "admissible (\<lambda>x. t) = t"
199 by (cases t, simp_all add: admissible_True admissible_False)
201 lemma admissible_conj:
202 assumes "admissible (\<lambda>x. P x)"
203 assumes "admissible (\<lambda>x. Q x)"
204 shows "admissible (\<lambda>x. P x \<and> Q x)"
205 using assms unfolding admissible_def by simp
207 lemma admissible_all:
208 assumes "\<And>y. admissible (\<lambda>x. P x y)"
209 shows "admissible (\<lambda>x. \<forall>y. P x y)"
210 using assms unfolding admissible_def by fast
212 lemma admissible_ball:
213 assumes "\<And>y. y \<in> A \<Longrightarrow> admissible (\<lambda>x. P x y)"
214 shows "admissible (\<lambda>x. \<forall>y\<in>A. P x y)"
215 using assms unfolding admissible_def by fast
217 lemma chain_compr: "chain (op \<le>) A \<Longrightarrow> chain (op \<le>) {x \<in> A. P x}"
218 unfolding chain_def by fast
220 lemma admissible_disj_lemma:
221 assumes A: "chain (op \<le>)A"
222 assumes P: "\<forall>x\<in>A. \<exists>y\<in>A. x \<le> y \<and> P y"
223 shows "lub A = lub {x \<in> A. P x}"
225 have *: "chain (op \<le>) {x \<in> A. P x}"
226 by (rule chain_compr [OF A])
227 show "lub A \<le> lub {x \<in> A. P x}"
228 apply (rule lub_least [OF A])
229 apply (drule P [rule_format], clarify)
230 apply (erule order_trans)
231 apply (simp add: lub_upper [OF *])
233 show "lub {x \<in> A. P x} \<le> lub A"
234 apply (rule lub_least [OF *])
236 apply (simp add: lub_upper [OF A])
240 lemma admissible_disj:
241 fixes P Q :: "'a \<Rightarrow> bool"
242 assumes P: "admissible (\<lambda>x. P x)"
243 assumes Q: "admissible (\<lambda>x. Q x)"
244 shows "admissible (\<lambda>x. P x \<or> Q x)"
245 proof (rule admissibleI)
246 fix A :: "'a set" assume A: "chain (op \<le>) A"
247 assume "\<forall>x\<in>A. P x \<or> Q x"
248 hence "(\<forall>x\<in>A. \<exists>y\<in>A. x \<le> y \<and> P y) \<or> (\<forall>x\<in>A. \<exists>y\<in>A. x \<le> y \<and> Q y)"
249 using chainD[OF A] by blast
250 hence "lub A = lub {x \<in> A. P x} \<or> lub A = lub {x \<in> A. Q x}"
251 using admissible_disj_lemma [OF A] by fast
252 thus "P (lub A) \<or> Q (lub A)"
253 apply (rule disjE, simp_all)
254 apply (rule disjI1, rule admissibleD [OF P chain_compr [OF A]], simp)
255 apply (rule disjI2, rule admissibleD [OF Q chain_compr [OF A]], simp)
261 hide_const (open) lub iterates fixp admissible