author wenzelm
Sat Jul 18 22:58:50 2015 +0200 (2015-07-18)
changeset 60758 d8d85a8172b5
parent 60061 279472fa0b1d
child 61169 4de9ff3ea29a
permissions -rw-r--r--
isabelle update_cartouches;
     1 (* Title:    HOL/Complete_Partial_Order.thy
     2    Author:   Brian Huffman, Portland State University
     3    Author:   Alexander Krauss, TU Muenchen
     4 *)
     6 section \<open>Chain-complete partial orders and their fixpoints\<close>
     8 theory Complete_Partial_Order
     9 imports Product_Type
    10 begin
    12 subsection \<open>Monotone functions\<close>
    14 text \<open>Dictionary-passing version of @{const Orderings.mono}.\<close>
    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 \<open>Chains\<close>
    29 text \<open>A chain is a totally-ordered set. Chains are parameterized over
    30   the order for maximal flexibility, since type classes are not enough.
    31 \<close>
    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 {}"
    54 by(simp add: chain_def)
    56 lemma chain_equality: "chain op = A \<longleftrightarrow> (\<forall>x\<in>A. \<forall>y\<in>A. x = y)"
    57 by(auto simp add: chain_def)
    59 lemma chain_subset:
    60   "\<lbrakk> chain ord A; B \<subseteq> A \<rbrakk>
    61   \<Longrightarrow> chain ord B"
    62 by(rule chainI)(blast dest: chainD)
    64 lemma chain_imageI: 
    65   assumes chain: "chain le_a Y"
    66   and mono: "\<And>x y. \<lbrakk> x \<in> Y; y \<in> Y; le_a x y \<rbrakk> \<Longrightarrow> le_b (f x) (f y)"
    67   shows "chain le_b (f ` Y)"
    68 by(blast intro: chainI dest: chainD[OF chain] mono)
    70 subsection \<open>Chain-complete partial orders\<close>
    72 text \<open>
    73   A ccpo has a least upper bound for any chain.  In particular, the
    74   empty set is a chain, so every ccpo must have a bottom element.
    75 \<close>
    77 class ccpo = order + Sup +
    78   assumes ccpo_Sup_upper: "\<lbrakk>chain (op \<le>) A; x \<in> A\<rbrakk> \<Longrightarrow> x \<le> Sup A"
    79   assumes ccpo_Sup_least: "\<lbrakk>chain (op \<le>) A; \<And>x. x \<in> A \<Longrightarrow> x \<le> z\<rbrakk> \<Longrightarrow> Sup A \<le> z"
    80 begin
    82 lemma chain_singleton: "Complete_Partial_Order.chain op \<le> {x}"
    83 by(rule chainI) simp
    85 lemma ccpo_Sup_singleton [simp]: "\<Squnion>{x} = x"
    86 by(rule antisym)(auto intro: ccpo_Sup_least ccpo_Sup_upper simp add: chain_singleton)
    88 subsection \<open>Transfinite iteration of a function\<close>
    90 inductive_set iterates :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a set"
    91 for f :: "'a \<Rightarrow> 'a"
    92 where
    93   step: "x \<in> iterates f \<Longrightarrow> f x \<in> iterates f"
    94 | Sup: "chain (op \<le>) M \<Longrightarrow> \<forall>x\<in>M. x \<in> iterates f \<Longrightarrow> Sup M \<in> iterates f"
    96 lemma iterates_le_f:
    97   "x \<in> iterates f \<Longrightarrow> monotone (op \<le>) (op \<le>) f \<Longrightarrow> x \<le> f x"
    98 by (induct x rule: iterates.induct)
    99   (force dest: monotoneD intro!: ccpo_Sup_upper ccpo_Sup_least)+
   101 lemma chain_iterates:
   102   assumes f: "monotone (op \<le>) (op \<le>) f"
   103   shows "chain (op \<le>) (iterates f)" (is "chain _ ?C")
   104 proof (rule chainI)
   105   fix x y assume "x \<in> ?C" "y \<in> ?C"
   106   then show "x \<le> y \<or> y \<le> x"
   107   proof (induct x arbitrary: y rule: iterates.induct)
   108     fix x y assume y: "y \<in> ?C"
   109     and IH: "\<And>z. z \<in> ?C \<Longrightarrow> x \<le> z \<or> z \<le> x"
   110     from y show "f x \<le> y \<or> y \<le> f x"
   111     proof (induct y rule: iterates.induct)
   112       case (step y) with IH f show ?case by (auto dest: monotoneD)
   113     next
   114       case (Sup M)
   115       then have chM: "chain (op \<le>) M"
   116         and IH': "\<And>z. z \<in> M \<Longrightarrow> f x \<le> z \<or> z \<le> f x" by auto
   117       show "f x \<le> Sup M \<or> Sup M \<le> f x"
   118       proof (cases "\<exists>z\<in>M. f x \<le> z")
   119         case True then have "f x \<le> Sup M"
   120           apply rule
   121           apply (erule order_trans)
   122           by (rule ccpo_Sup_upper[OF chM])
   123         thus ?thesis ..
   124       next
   125         case False with IH'
   126         show ?thesis by (auto intro: ccpo_Sup_least[OF chM])
   127       qed
   128     qed
   129   next
   130     case (Sup M y)
   131     show ?case
   132     proof (cases "\<exists>x\<in>M. y \<le> x")
   133       case True then have "y \<le> Sup M"
   134         apply rule
   135         apply (erule order_trans)
   136         by (rule ccpo_Sup_upper[OF Sup(1)])
   137       thus ?thesis ..
   138     next
   139       case False with Sup
   140       show ?thesis by (auto intro: ccpo_Sup_least)
   141     qed
   142   qed
   143 qed
   145 lemma bot_in_iterates: "Sup {} \<in> iterates f"
   146 by(auto intro: iterates.Sup simp add: chain_empty)
   148 subsection \<open>Fixpoint combinator\<close>
   150 definition
   151   fixp :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a"
   152 where
   153   "fixp f = Sup (iterates f)"
   155 lemma iterates_fixp:
   156   assumes f: "monotone (op \<le>) (op \<le>) f" shows "fixp f \<in> iterates f"
   157 unfolding fixp_def
   158 by (simp add: iterates.Sup chain_iterates f)
   160 lemma fixp_unfold:
   161   assumes f: "monotone (op \<le>) (op \<le>) f"
   162   shows "fixp f = f (fixp f)"
   163 proof (rule antisym)
   164   show "fixp f \<le> f (fixp f)"
   165     by (intro iterates_le_f iterates_fixp f)
   166   have "f (fixp f) \<le> Sup (iterates f)"
   167     by (intro ccpo_Sup_upper chain_iterates f iterates.step iterates_fixp)
   168   thus "f (fixp f) \<le> fixp f"
   169     unfolding fixp_def .
   170 qed
   172 lemma fixp_lowerbound:
   173   assumes f: "monotone (op \<le>) (op \<le>) f" and z: "f z \<le> z" shows "fixp f \<le> z"
   174 unfolding fixp_def
   175 proof (rule ccpo_Sup_least[OF chain_iterates[OF f]])
   176   fix x assume "x \<in> iterates f"
   177   thus "x \<le> z"
   178   proof (induct x rule: iterates.induct)
   179     fix x assume "x \<le> z" with f have "f x \<le> f z" by (rule monotoneD)
   180     also note z finally show "f x \<le> z" .
   181   qed (auto intro: ccpo_Sup_least)
   182 qed
   184 end
   186 subsection \<open>Fixpoint induction\<close>
   188 setup \<open>Sign.map_naming (Name_Space.mandatory_path "ccpo")\<close>
   190 definition admissible :: "('a set \<Rightarrow> 'a) \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
   191 where "admissible lub ord P = (\<forall>A. chain ord A \<longrightarrow> (A \<noteq> {}) \<longrightarrow> (\<forall>x\<in>A. P x) \<longrightarrow> P (lub A))"
   193 lemma admissibleI:
   194   assumes "\<And>A. chain ord A \<Longrightarrow> A \<noteq> {} \<Longrightarrow> \<forall>x\<in>A. P x \<Longrightarrow> P (lub A)"
   195   shows "ccpo.admissible lub ord P"
   196 using assms unfolding ccpo.admissible_def by fast
   198 lemma admissibleD:
   199   assumes "ccpo.admissible lub ord P"
   200   assumes "chain ord A"
   201   assumes "A \<noteq> {}"
   202   assumes "\<And>x. x \<in> A \<Longrightarrow> P x"
   203   shows "P (lub A)"
   204 using assms by (auto simp: ccpo.admissible_def)
   206 setup \<open>Sign.map_naming Name_Space.parent_path\<close>
   208 lemma (in ccpo) fixp_induct:
   209   assumes adm: "ccpo.admissible Sup (op \<le>) P"
   210   assumes mono: "monotone (op \<le>) (op \<le>) f"
   211   assumes bot: "P (Sup {})"
   212   assumes step: "\<And>x. P x \<Longrightarrow> P (f x)"
   213   shows "P (fixp f)"
   214 unfolding fixp_def using adm chain_iterates[OF mono]
   215 proof (rule ccpo.admissibleD)
   216   show "iterates f \<noteq> {}" using bot_in_iterates by auto
   217   fix x assume "x \<in> iterates f"
   218   thus "P x"
   219     by (induct rule: iterates.induct)
   220       (case_tac "M = {}", auto intro: step bot ccpo.admissibleD adm)
   221 qed
   223 lemma admissible_True: "ccpo.admissible lub ord (\<lambda>x. True)"
   224 unfolding ccpo.admissible_def by simp
   226 (*lemma admissible_False: "\<not> ccpo.admissible lub ord (\<lambda>x. False)"
   227 unfolding ccpo.admissible_def chain_def by simp
   228 *)
   229 lemma admissible_const: "ccpo.admissible lub ord (\<lambda>x. t)"
   230 by(auto intro: ccpo.admissibleI)
   232 lemma admissible_conj:
   233   assumes "ccpo.admissible lub ord (\<lambda>x. P x)"
   234   assumes "ccpo.admissible lub ord (\<lambda>x. Q x)"
   235   shows "ccpo.admissible lub ord (\<lambda>x. P x \<and> Q x)"
   236 using assms unfolding ccpo.admissible_def by simp
   238 lemma admissible_all:
   239   assumes "\<And>y. ccpo.admissible lub ord (\<lambda>x. P x y)"
   240   shows "ccpo.admissible lub ord (\<lambda>x. \<forall>y. P x y)"
   241 using assms unfolding ccpo.admissible_def by fast
   243 lemma admissible_ball:
   244   assumes "\<And>y. y \<in> A \<Longrightarrow> ccpo.admissible lub ord (\<lambda>x. P x y)"
   245   shows "ccpo.admissible lub ord (\<lambda>x. \<forall>y\<in>A. P x y)"
   246 using assms unfolding ccpo.admissible_def by fast
   248 lemma chain_compr: "chain ord A \<Longrightarrow> chain ord {x \<in> A. P x}"
   249 unfolding chain_def by fast
   251 context ccpo begin
   253 lemma admissible_disj_lemma:
   254   assumes A: "chain (op \<le>)A"
   255   assumes P: "\<forall>x\<in>A. \<exists>y\<in>A. x \<le> y \<and> P y"
   256   shows "Sup A = Sup {x \<in> A. P x}"
   257 proof (rule antisym)
   258   have *: "chain (op \<le>) {x \<in> A. P x}"
   259     by (rule chain_compr [OF A])
   260   show "Sup A \<le> Sup {x \<in> A. P x}"
   261     apply (rule ccpo_Sup_least [OF A])
   262     apply (drule P [rule_format], clarify)
   263     apply (erule order_trans)
   264     apply (simp add: ccpo_Sup_upper [OF *])
   265     done
   266   show "Sup {x \<in> A. P x} \<le> Sup A"
   267     apply (rule ccpo_Sup_least [OF *])
   268     apply clarify
   269     apply (simp add: ccpo_Sup_upper [OF A])
   270     done
   271 qed
   273 lemma admissible_disj:
   274   fixes P Q :: "'a \<Rightarrow> bool"
   275   assumes P: "ccpo.admissible Sup (op \<le>) (\<lambda>x. P x)"
   276   assumes Q: "ccpo.admissible Sup (op \<le>) (\<lambda>x. Q x)"
   277   shows "ccpo.admissible Sup (op \<le>) (\<lambda>x. P x \<or> Q x)"
   278 proof (rule ccpo.admissibleI)
   279   fix A :: "'a set" assume A: "chain (op \<le>) A"
   280   assume "A \<noteq> {}"
   281     and "\<forall>x\<in>A. P x \<or> Q x"
   282   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)"
   283     using chainD[OF A] by blast
   284   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}"
   285     using admissible_disj_lemma [OF A] by blast
   286   thus "P (Sup A) \<or> Q (Sup A)"
   287     apply (rule disjE, simp_all)
   288     apply (rule disjI1, rule ccpo.admissibleD [OF P chain_compr [OF A]], simp, simp)
   289     apply (rule disjI2, rule ccpo.admissibleD [OF Q chain_compr [OF A]], simp, simp)
   290     done
   291 qed
   293 end
   295 instance complete_lattice \<subseteq> ccpo
   296   by default (fast intro: Sup_upper Sup_least)+
   298 lemma lfp_eq_fixp:
   299   assumes f: "mono f" shows "lfp f = fixp f"
   300 proof (rule antisym)
   301   from f have f': "monotone (op \<le>) (op \<le>) f"
   302     unfolding mono_def monotone_def .
   303   show "lfp f \<le> fixp f"
   304     by (rule lfp_lowerbound, subst fixp_unfold [OF f'], rule order_refl)
   305   show "fixp f \<le> lfp f"
   306     by (rule fixp_lowerbound [OF f'], subst lfp_unfold [OF f], rule order_refl)
   307 qed
   309 hide_const (open) iterates fixp
   311 end