src/HOL/Library/Ramsey.thy
author chaieb
Mon Jun 11 11:06:04 2007 +0200 (2007-06-11)
changeset 23315 df3a7e9ebadb
parent 22665 cf152ff55d16
child 24853 aab5798e5a33
permissions -rwxr-xr-x
tuned Proof
     1 (*  Title:      HOL/Library/Ramsey.thy
     2     ID:         $Id$
     3     Author:     Tom Ridge. Converted to structured Isar by L C Paulson
     4 *)
     5 
     6 header "Ramsey's Theorem"
     7 
     8 theory Ramsey imports Main Infinite_Set begin
     9 
    10 subsection {* Preliminaries *}
    11 
    12 subsubsection {* ``Axiom'' of Dependent Choice *}
    13 
    14 consts choice :: "('a => bool) => ('a * 'a) set => nat => 'a"
    15   --{*An integer-indexed chain of choices*}
    16 primrec
    17   choice_0:   "choice P r 0 = (SOME x. P x)"
    18 
    19   choice_Suc: "choice P r (Suc n) = (SOME y. P y & (choice P r n, y) \<in> r)"
    20 
    21 
    22 lemma choice_n: 
    23   assumes P0: "P x0"
    24       and Pstep: "!!x. P x ==> \<exists>y. P y & (x,y) \<in> r"
    25   shows "P (choice P r n)"
    26 proof (induct n)
    27   case 0 show ?case by (force intro: someI P0) 
    28 next
    29   case Suc thus ?case by (auto intro: someI2_ex [OF Pstep]) 
    30 qed
    31 
    32 lemma dependent_choice: 
    33   assumes trans: "trans r"
    34       and P0: "P x0"
    35       and Pstep: "!!x. P x ==> \<exists>y. P y & (x,y) \<in> r"
    36   obtains f :: "nat => 'a" where
    37     "!!n. P (f n)" and "!!n m. n < m ==> (f n, f m) \<in> r"
    38 proof
    39   fix n
    40   show "P (choice P r n)" by (blast intro: choice_n [OF P0 Pstep])
    41 next
    42   have PSuc: "\<forall>n. (choice P r n, choice P r (Suc n)) \<in> r" 
    43     using Pstep [OF choice_n [OF P0 Pstep]]
    44     by (auto intro: someI2_ex)
    45   fix n m :: nat
    46   assume less: "n < m"
    47   show "(choice P r n, choice P r m) \<in> r" using PSuc
    48     by (auto intro: less_Suc_induct [OF less] transD [OF trans])
    49 qed
    50 
    51 
    52 subsubsection {* Partitions of a Set *}
    53 
    54 definition
    55   part :: "nat => nat => 'a set => ('a set => nat) => bool"
    56   --{*the function @{term f} partitions the @{term r}-subsets of the typically
    57        infinite set @{term Y} into @{term s} distinct categories.*}
    58 where
    59   "part r s Y f = (\<forall>X. X \<subseteq> Y & finite X & card X = r --> f X < s)"
    60 
    61 text{*For induction, we decrease the value of @{term r} in partitions.*}
    62 lemma part_Suc_imp_part:
    63      "[| infinite Y; part (Suc r) s Y f; y \<in> Y |] 
    64       ==> part r s (Y - {y}) (%u. f (insert y u))"
    65   apply(simp add: part_def, clarify)
    66   apply(drule_tac x="insert y X" in spec)
    67   apply(force simp:card_Diff_singleton_if)
    68   done
    69 
    70 lemma part_subset: "part r s YY f ==> Y \<subseteq> YY ==> part r s Y f" 
    71   unfolding part_def by blast
    72   
    73 
    74 subsection {* Ramsey's Theorem: Infinitary Version *}
    75 
    76 lemma Ramsey_induction: 
    77   fixes s and r::nat
    78   shows
    79   "!!(YY::'a set) (f::'a set => nat). 
    80       [|infinite YY; part r s YY f|]
    81       ==> \<exists>Y' t'. Y' \<subseteq> YY & infinite Y' & t' < s & 
    82                   (\<forall>X. X \<subseteq> Y' & finite X & card X = r --> f X = t')"
    83 proof (induct r)
    84   case 0
    85   thus ?case by (auto simp add: part_def card_eq_0_iff cong: conj_cong) 
    86 next
    87   case (Suc r) 
    88   show ?case
    89   proof -
    90     from Suc.prems infinite_imp_nonempty obtain yy where yy: "yy \<in> YY" by blast
    91     let ?ramr = "{((y,Y,t),(y',Y',t')). y' \<in> Y & Y' \<subseteq> Y}"
    92     let ?propr = "%(y,Y,t).     
    93 		 y \<in> YY & y \<notin> Y & Y \<subseteq> YY & infinite Y & t < s
    94 		 & (\<forall>X. X\<subseteq>Y & finite X & card X = r --> (f o insert y) X = t)"
    95     have infYY': "infinite (YY-{yy})" using Suc.prems by auto
    96     have partf': "part r s (YY - {yy}) (f \<circ> insert yy)"
    97       by (simp add: o_def part_Suc_imp_part yy Suc.prems)
    98     have transr: "trans ?ramr" by (force simp add: trans_def) 
    99     from Suc.hyps [OF infYY' partf']
   100     obtain Y0 and t0
   101     where "Y0 \<subseteq> YY - {yy}"  "infinite Y0"  "t0 < s"
   102           "\<forall>X. X\<subseteq>Y0 \<and> finite X \<and> card X = r \<longrightarrow> (f \<circ> insert yy) X = t0"
   103         by blast 
   104     with yy have propr0: "?propr(yy,Y0,t0)" by blast
   105     have proprstep: "\<And>x. ?propr x \<Longrightarrow> \<exists>y. ?propr y \<and> (x, y) \<in> ?ramr" 
   106     proof -
   107       fix x
   108       assume px: "?propr x" thus "?thesis x"
   109       proof (cases x)
   110         case (fields yx Yx tx)
   111         then obtain yx' where yx': "yx' \<in> Yx" using px
   112                by (blast dest: infinite_imp_nonempty)
   113         have infYx': "infinite (Yx-{yx'})" using fields px by auto
   114         with fields px yx' Suc.prems
   115         have partfx': "part r s (Yx - {yx'}) (f \<circ> insert yx')"
   116           by (simp add: o_def part_Suc_imp_part part_subset [where ?YY=YY]) 
   117 	from Suc.hyps [OF infYx' partfx']
   118 	obtain Y' and t'
   119 	where Y': "Y' \<subseteq> Yx - {yx'}"  "infinite Y'"  "t' < s"
   120 	       "\<forall>X. X\<subseteq>Y' \<and> finite X \<and> card X = r \<longrightarrow> (f \<circ> insert yx') X = t'"
   121 	    by blast 
   122 	show ?thesis
   123 	proof
   124 	  show "?propr (yx',Y',t') & (x, (yx',Y',t')) \<in> ?ramr"
   125   	    using fields Y' yx' px by blast
   126 	qed
   127       qed
   128     qed
   129     from dependent_choice [OF transr propr0 proprstep]
   130     obtain g where pg: "!!n::nat.  ?propr (g n)"
   131       and rg: "!!n m. n<m ==> (g n, g m) \<in> ?ramr" by blast
   132     let ?gy = "(\<lambda>n. let (y,Y,t) = g n in y)"
   133     let ?gt = "(\<lambda>n. let (y,Y,t) = g n in t)"
   134     have rangeg: "\<exists>k. range ?gt \<subseteq> {..<k}"
   135     proof (intro exI subsetI)
   136       fix x
   137       assume "x \<in> range ?gt"
   138       then obtain n where "x = ?gt n" ..
   139       with pg [of n] show "x \<in> {..<s}" by (cases "g n") auto
   140     qed
   141     have "finite (range ?gt)"
   142       by (simp add: finite_nat_iff_bounded rangeg)
   143     then obtain s' and n'
   144       where s': "s' = ?gt n'"
   145         and infeqs': "infinite {n. ?gt n = s'}"
   146       by (rule inf_img_fin_domE) (auto simp add: vimage_def intro: nat_infinite)
   147     with pg [of n'] have less': "s'<s" by (cases "g n'") auto
   148     have inj_gy: "inj ?gy"
   149     proof (rule linorder_injI)
   150       fix m m' :: nat assume less: "m < m'" show "?gy m \<noteq> ?gy m'"
   151         using rg [OF less] pg [of m] by (cases "g m", cases "g m'") auto
   152     qed
   153     show ?thesis
   154     proof (intro exI conjI)
   155       show "?gy ` {n. ?gt n = s'} \<subseteq> YY" using pg
   156         by (auto simp add: Let_def split_beta) 
   157       show "infinite (?gy ` {n. ?gt n = s'})" using infeqs'
   158         by (blast intro: inj_gy [THEN subset_inj_on] dest: finite_imageD) 
   159       show "s' < s" by (rule less')
   160       show "\<forall>X. X \<subseteq> ?gy ` {n. ?gt n = s'} & finite X & card X = Suc r 
   161           --> f X = s'"
   162       proof -
   163         {fix X 
   164          assume "X \<subseteq> ?gy ` {n. ?gt n = s'}"
   165             and cardX: "finite X" "card X = Suc r"
   166          then obtain AA where AA: "AA \<subseteq> {n. ?gt n = s'}" and Xeq: "X = ?gy`AA" 
   167              by (auto simp add: subset_image_iff) 
   168          with cardX have "AA\<noteq>{}" by auto
   169          hence AAleast: "(LEAST x. x \<in> AA) \<in> AA" by (auto intro: LeastI_ex) 
   170          have "f X = s'"
   171          proof (cases "g (LEAST x. x \<in> AA)") 
   172            case (fields ya Ya ta)
   173            with AAleast Xeq 
   174            have ya: "ya \<in> X" by (force intro!: rev_image_eqI) 
   175            hence "f X = f (insert ya (X - {ya}))" by (simp add: insert_absorb)
   176            also have "... = ta" 
   177            proof -
   178              have "X - {ya} \<subseteq> Ya"
   179              proof 
   180                fix x assume x: "x \<in> X - {ya}"
   181                then obtain a' where xeq: "x = ?gy a'" and a': "a' \<in> AA" 
   182                  by (auto simp add: Xeq) 
   183                hence "a' \<noteq> (LEAST x. x \<in> AA)" using x fields by auto
   184                hence lessa': "(LEAST x. x \<in> AA) < a'"
   185                  using Least_le [of "%x. x \<in> AA", OF a'] by arith
   186                show "x \<in> Ya" using xeq fields rg [OF lessa'] by auto
   187              qed
   188              moreover
   189              have "card (X - {ya}) = r"
   190                by (simp add: card_Diff_singleton_if cardX ya)
   191              ultimately show ?thesis 
   192                using pg [of "LEAST x. x \<in> AA"] fields cardX
   193 	       by (clarsimp simp del:insert_Diff_single)
   194            qed
   195            also have "... = s'" using AA AAleast fields by auto
   196            finally show ?thesis .
   197          qed}
   198         thus ?thesis by blast
   199       qed 
   200     qed 
   201   qed
   202 qed
   203 
   204 
   205 theorem Ramsey:
   206   fixes s r :: nat and Z::"'a set" and f::"'a set => nat"
   207   shows
   208    "[|infinite Z;
   209       \<forall>X. X \<subseteq> Z & finite X & card X = r --> f X < s|]
   210   ==> \<exists>Y t. Y \<subseteq> Z & infinite Y & t < s 
   211             & (\<forall>X. X \<subseteq> Y & finite X & card X = r --> f X = t)"
   212 by (blast intro: Ramsey_induction [unfolded part_def])
   213 
   214 
   215 corollary Ramsey2:
   216   fixes s::nat and Z::"'a set" and f::"'a set => nat"
   217   assumes infZ: "infinite Z"
   218       and part: "\<forall>x\<in>Z. \<forall>y\<in>Z. x\<noteq>y --> f{x,y} < s"
   219   shows
   220    "\<exists>Y t. Y \<subseteq> Z & infinite Y & t < s & (\<forall>x\<in>Y. \<forall>y\<in>Y. x\<noteq>y --> f{x,y} = t)"
   221 proof -
   222   have part2: "\<forall>X. X \<subseteq> Z & finite X & card X = 2 --> f X < s"
   223     by (auto simp add: numeral_2_eq_2 card_2_eq part) 
   224   obtain Y t 
   225     where "Y \<subseteq> Z" "infinite Y" "t < s"
   226           "(\<forall>X. X \<subseteq> Y & finite X & card X = 2 --> f X = t)"
   227     by (insert Ramsey [OF infZ part2]) auto
   228   moreover from this have  "\<forall>x\<in>Y. \<forall>y\<in>Y. x \<noteq> y \<longrightarrow> f {x, y} = t" by auto
   229   ultimately show ?thesis by iprover
   230 qed
   231 
   232 
   233 subsection {* Disjunctive Well-Foundedness *}
   234 
   235 text {*
   236   An application of Ramsey's theorem to program termination. See
   237   \cite{Podelski-Rybalchenko}.
   238 *}
   239 
   240 definition
   241   disj_wf         :: "('a * 'a)set => bool"
   242 where
   243   "disj_wf r = (\<exists>T. \<exists>n::nat. (\<forall>i<n. wf(T i)) & r = (\<Union>i<n. T i))"
   244 
   245 definition
   246   transition_idx :: "[nat => 'a, nat => ('a*'a)set, nat set] => nat"
   247 where
   248   "transition_idx s T A =
   249     (LEAST k. \<exists>i j. A = {i,j} & i<j & (s j, s i) \<in> T k)"
   250 
   251 
   252 lemma transition_idx_less:
   253     "[|i<j; (s j, s i) \<in> T k; k<n|] ==> transition_idx s T {i,j} < n"
   254 apply (subgoal_tac "transition_idx s T {i, j} \<le> k", simp) 
   255 apply (simp add: transition_idx_def, blast intro: Least_le) 
   256 done
   257 
   258 lemma transition_idx_in:
   259     "[|i<j; (s j, s i) \<in> T k|] ==> (s j, s i) \<in> T (transition_idx s T {i,j})"
   260 apply (simp add: transition_idx_def doubleton_eq_iff conj_disj_distribR 
   261             cong: conj_cong) 
   262 apply (erule LeastI) 
   263 done
   264 
   265 text{*To be equal to the union of some well-founded relations is equivalent
   266 to being the subset of such a union.*}
   267 lemma disj_wf:
   268      "disj_wf(r) = (\<exists>T. \<exists>n::nat. (\<forall>i<n. wf(T i)) & r \<subseteq> (\<Union>i<n. T i))"
   269 apply (auto simp add: disj_wf_def) 
   270 apply (rule_tac x="%i. T i Int r" in exI) 
   271 apply (rule_tac x=n in exI) 
   272 apply (force simp add: wf_Int1) 
   273 done
   274 
   275 theorem trans_disj_wf_implies_wf:
   276   assumes transr: "trans r"
   277       and dwf:    "disj_wf(r)"
   278   shows "wf r"
   279 proof (simp only: wf_iff_no_infinite_down_chain, rule notI)
   280   assume "\<exists>s. \<forall>i. (s (Suc i), s i) \<in> r"
   281   then obtain s where sSuc: "\<forall>i. (s (Suc i), s i) \<in> r" ..
   282   have s: "!!i j. i < j ==> (s j, s i) \<in> r"
   283   proof -
   284     fix i and j::nat
   285     assume less: "i<j"
   286     thus "(s j, s i) \<in> r"
   287     proof (rule less_Suc_induct)
   288       show "\<And>i. (s (Suc i), s i) \<in> r" by (simp add: sSuc) 
   289       show "\<And>i j k. \<lbrakk>(s j, s i) \<in> r; (s k, s j) \<in> r\<rbrakk> \<Longrightarrow> (s k, s i) \<in> r"
   290         using transr by (unfold trans_def, blast) 
   291     qed
   292   qed    
   293   from dwf
   294   obtain T and n::nat where wfT: "\<forall>k<n. wf(T k)" and r: "r = (\<Union>k<n. T k)"
   295     by (auto simp add: disj_wf_def)
   296   have s_in_T: "\<And>i j. i<j ==> \<exists>k. (s j, s i) \<in> T k & k<n"
   297   proof -
   298     fix i and j::nat
   299     assume less: "i<j"
   300     hence "(s j, s i) \<in> r" by (rule s [of i j]) 
   301     thus "\<exists>k. (s j, s i) \<in> T k & k<n" by (auto simp add: r)
   302   qed    
   303   have trless: "!!i j. i\<noteq>j ==> transition_idx s T {i,j} < n"
   304     apply (auto simp add: linorder_neq_iff)
   305     apply (blast dest: s_in_T transition_idx_less) 
   306     apply (subst insert_commute)   
   307     apply (blast dest: s_in_T transition_idx_less) 
   308     done
   309   have
   310    "\<exists>K k. K \<subseteq> UNIV & infinite K & k < n & 
   311           (\<forall>i\<in>K. \<forall>j\<in>K. i\<noteq>j --> transition_idx s T {i,j} = k)"
   312     by (rule Ramsey2) (auto intro: trless nat_infinite) 
   313   then obtain K and k 
   314     where infK: "infinite K" and less: "k < n" and
   315           allk: "\<forall>i\<in>K. \<forall>j\<in>K. i\<noteq>j --> transition_idx s T {i,j} = k"
   316     by auto
   317   have "\<forall>m. (s (enumerate K (Suc m)), s(enumerate K m)) \<in> T k"
   318   proof
   319     fix m::nat
   320     let ?j = "enumerate K (Suc m)"
   321     let ?i = "enumerate K m"
   322     have jK: "?j \<in> K" by (simp add: enumerate_in_set infK) 
   323     have iK: "?i \<in> K" by (simp add: enumerate_in_set infK) 
   324     have ij: "?i < ?j" by (simp add: enumerate_step infK) 
   325     have ijk: "transition_idx s T {?i,?j} = k" using iK jK ij 
   326       by (simp add: allk)
   327     obtain k' where "(s ?j, s ?i) \<in> T k'" "k'<n" 
   328       using s_in_T [OF ij] by blast
   329     thus "(s ?j, s ?i) \<in> T k" 
   330       by (simp add: ijk [symmetric] transition_idx_in ij) 
   331   qed
   332   hence "~ wf(T k)" by (force simp add: wf_iff_no_infinite_down_chain) 
   333   thus False using wfT less by blast
   334 qed
   335 
   336 end