src/HOL/Complete_Partial_Order.thy
author wenzelm
Thu, 07 Jul 2011 23:55:15 +0200
changeset 44572 91c4d7397f0e
parent 40487 029400b6c893
child 46912 1e3ff542e83e
permissions -rw-r--r--
simplified make_option/dest_option;
added make_variant/dest_variant -- usual representation of datatypes;
     1 (* Title:    HOL/Complete_Partial_Order.thy
     2    Author:   Brian Huffman, Portland State University
     3    Author:   Alexander Krauss, TU Muenchen
     4 *)
     5 
     6 header {* Chain-complete partial orders and their fixpoints *}
     7 
     8 theory Complete_Partial_Order
     9 imports Product_Type
    10 begin
    11 
    12 subsection {* Monotone functions *}
    13 
    14 text {* Dictionary-passing version of @{const Orderings.mono}. *}
    15 
    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))"
    18 
    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
    22 
    23 lemma monotoneD[dest?]: "monotone orda ordb f \<Longrightarrow> orda x y \<Longrightarrow> ordb (f x) (f y)"
    24 unfolding monotone_def by iprover
    25 
    26 
    27 subsection {* Chains *}
    28 
    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 *}
    32 
    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)"
    37 
    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
    42 
    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
    47 
    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
    52 
    53 subsection {* Chain-complete partial orders *}
    54 
    55 text {*
    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.
    58 *}
    59 
    60 class ccpo = order +
    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"
    64 begin
    65 
    66 subsection {* Transfinite iteration of a function *}
    67 
    68 inductive_set iterates :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a set"
    69 for f :: "'a \<Rightarrow> 'a"
    70 where
    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"
    73 
    74 lemma iterates_le_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)+
    78 
    79 lemma chain_iterates:
    80   assumes f: "monotone (op \<le>) (op \<le>) f"
    81   shows "chain (op \<le>) (iterates f)" (is "chain _ ?C")
    82 proof (rule chainI)
    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)
    91     next
    92       case (lub M)
    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"
    98           apply rule
    99           apply (erule order_trans)
   100           by (rule lub_upper[OF chM])
   101         thus ?thesis ..
   102       next
   103         case False with IH'
   104         show ?thesis by (auto intro: lub_least[OF chM])
   105       qed
   106     qed
   107   next
   108     case (lub M y)
   109     show ?case
   110     proof (cases "\<exists>x\<in>M. y \<le> x")
   111       case True then have "y \<le> lub M"
   112         apply rule
   113         apply (erule order_trans)
   114         by (rule lub_upper[OF lub(1)])
   115       thus ?thesis ..
   116     next
   117       case False with lub
   118       show ?thesis by (auto intro: lub_least)
   119     qed
   120   qed
   121 qed
   122 
   123 subsection {* Fixpoint combinator *}
   124 
   125 definition
   126   fixp :: "('a \<Rightarrow> 'a) \<Rightarrow> 'a"
   127 where
   128   "fixp f = lub (iterates f)"
   129 
   130 lemma iterates_fixp:
   131   assumes f: "monotone (op \<le>) (op \<le>) f" shows "fixp f \<in> iterates f"
   132 unfolding fixp_def
   133 by (simp add: iterates.lub chain_iterates f)
   134 
   135 lemma fixp_unfold:
   136   assumes f: "monotone (op \<le>) (op \<le>) f"
   137   shows "fixp f = f (fixp f)"
   138 proof (rule antisym)
   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"
   144     unfolding fixp_def .
   145 qed
   146 
   147 lemma fixp_lowerbound:
   148   assumes f: "monotone (op \<le>) (op \<le>) f" and z: "f z \<le> z" shows "fixp f \<le> z"
   149 unfolding fixp_def
   150 proof (rule lub_least[OF chain_iterates[OF f]])
   151   fix x assume "x \<in> iterates f"
   152   thus "x \<le> z"
   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)
   157 qed
   158 
   159 
   160 subsection {* Fixpoint induction *}
   161 
   162 definition
   163   admissible :: "('a \<Rightarrow> bool) \<Rightarrow> bool"
   164 where
   165   "admissible P = (\<forall>A. chain (op \<le>) A \<longrightarrow> (\<forall>x\<in>A. P x) \<longrightarrow> P (lub A))"
   166 
   167 lemma admissibleI:
   168   assumes "\<And>A. chain (op \<le>) A \<Longrightarrow> \<forall>x\<in>A. P x \<Longrightarrow> P (lub A)"
   169   shows "admissible P"
   170 using assms unfolding admissible_def by fast
   171 
   172 lemma admissibleD:
   173   assumes "admissible P"
   174   assumes "chain (op \<le>) A"
   175   assumes "\<And>x. x \<in> A \<Longrightarrow> P x"
   176   shows "P (lub A)"
   177 using assms by (auto simp: admissible_def)
   178 
   179 lemma fixp_induct:
   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)"
   183   shows "P (fixp f)"
   184 unfolding fixp_def using adm chain_iterates[OF mono]
   185 proof (rule admissibleD)
   186   fix x assume "x \<in> iterates f"
   187   thus "P x"
   188     by (induct rule: iterates.induct)
   189       (auto intro: step admissibleD adm)
   190 qed
   191 
   192 lemma admissible_True: "admissible (\<lambda>x. True)"
   193 unfolding admissible_def by simp
   194 
   195 lemma admissible_False: "\<not> admissible (\<lambda>x. False)"
   196 unfolding admissible_def chain_def by simp
   197 
   198 lemma admissible_const: "admissible (\<lambda>x. t) = t"
   199 by (cases t, simp_all add: admissible_True admissible_False)
   200 
   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
   206 
   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
   211 
   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
   216 
   217 lemma chain_compr: "chain (op \<le>) A \<Longrightarrow> chain (op \<le>) {x \<in> A. P x}"
   218 unfolding chain_def by fast
   219 
   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}"
   224 proof (rule antisym)
   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 *])
   232     done
   233   show "lub {x \<in> A. P x} \<le> lub A"
   234     apply (rule lub_least [OF *])
   235     apply clarify
   236     apply (simp add: lub_upper [OF A])
   237     done
   238 qed
   239 
   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)
   256     done
   257 qed
   258 
   259 end
   260 
   261 hide_const (open) lub iterates fixp admissible
   262 
   263 end