src/HOL/Library/Ramsey.thy
author paulson
Fri Jun 23 09:55:01 2006 +0200 (2006-06-23)
changeset 19944 60e0cbeae3d8
child 19946 e3ddb0812840
permissions -rwxr-xr-x
Introduction of Ramsey's theorem
paulson@19944
     1
(*  Title:      HOL/Library/Ramsey.thy
paulson@19944
     2
    ID:         $Id$
paulson@19944
     3
    Author:     Tom Ridge. Converted to structured Isar by L C Paulson
paulson@19944
     4
*)
paulson@19944
     5
paulson@19944
     6
header "Ramsey's Theorem"
paulson@19944
     7
paulson@19944
     8
theory Ramsey imports Main begin
paulson@19944
     9
paulson@19944
    10
paulson@19944
    11
subsection{*``Axiom'' of Dependent Choice*}
paulson@19944
    12
paulson@19944
    13
consts choice :: "('a => bool) => (('a * 'a) set) => nat => 'a"
paulson@19944
    14
  --{*An integer-indexed chain of choices*}
paulson@19944
    15
primrec
paulson@19944
    16
  choice_0:   "choice P r 0 = (SOME x. P x)"
paulson@19944
    17
paulson@19944
    18
  choice_Suc: "choice P r (Suc n) = (SOME y. P y & (choice P r n, y) \<in> r)"
paulson@19944
    19
paulson@19944
    20
paulson@19944
    21
lemma choice_n: 
paulson@19944
    22
  assumes P0: "P x0"
paulson@19944
    23
      and Pstep: "!!x. P x ==> \<exists>y. P y & (x,y) \<in> r"
paulson@19944
    24
  shows "P (choice P r n)"
paulson@19944
    25
 proof (induct n)
paulson@19944
    26
   case 0 show ?case by (force intro: someI P0) 
paulson@19944
    27
 next
paulson@19944
    28
   case (Suc n) thus ?case by (auto intro: someI2_ex [OF Pstep]) 
paulson@19944
    29
 qed
paulson@19944
    30
paulson@19944
    31
lemma dependent_choice: 
paulson@19944
    32
  assumes trans: "trans r"
paulson@19944
    33
      and P0: "P x0"
paulson@19944
    34
      and Pstep: "!!x. P x ==> \<exists>y. P y & (x,y) \<in> r"
paulson@19944
    35
  shows "\<exists>f::nat=>'a. (\<forall>n. P (f n)) & (\<forall>n m. n<m --> (f n, f m) \<in> r)"
paulson@19944
    36
proof (intro exI conjI)
paulson@19944
    37
  show "\<forall>n. P (choice P r n)" by (blast intro: choice_n [OF P0 Pstep]) 
paulson@19944
    38
next
paulson@19944
    39
  have PSuc: "\<forall>n. (choice P r n, choice P r (Suc n)) \<in> r" 
paulson@19944
    40
    using Pstep [OF choice_n [OF P0 Pstep]]
paulson@19944
    41
    by (auto intro: someI2_ex)
paulson@19944
    42
  show "\<forall>n m. n<m --> (choice P r n, choice P r m) \<in> r"
paulson@19944
    43
  proof (intro strip)
paulson@19944
    44
    fix n and m::nat
paulson@19944
    45
    assume less: "n<m"
paulson@19944
    46
    show "(choice P r n, choice P r m) \<in> r" using PSuc
paulson@19944
    47
      by (auto intro: less_Suc_induct [OF less] transD [OF trans])
paulson@19944
    48
  qed
paulson@19944
    49
qed 
paulson@19944
    50
paulson@19944
    51
paulson@19944
    52
subsection {*Partitions of a Set*}
paulson@19944
    53
paulson@19944
    54
constdefs part :: "nat => nat => 'a set => ('a set => nat) => bool"
paulson@19944
    55
  --{*the function @{term f} partitions the @{term r}-subsets of the typically
paulson@19944
    56
       infinite set @{term Y} into @{term s} distinct categories.*}
paulson@19944
    57
  "part r s Y f == \<forall>X. X \<subseteq> Y & finite X & card X = r --> f X < s"
paulson@19944
    58
paulson@19944
    59
text{*For induction, we decrease the value of @{term r} in partitions.*}
paulson@19944
    60
lemma part_Suc_imp_part:
paulson@19944
    61
     "[| infinite Y; part (Suc r) s Y f; y \<in> Y |] 
paulson@19944
    62
      ==> part r s (Y - {y}) (%u. f (insert y u))"
paulson@19944
    63
  apply(simp add: part_def, clarify)
paulson@19944
    64
  apply(drule_tac x="insert y X" in spec)
paulson@19944
    65
  apply(force simp:card_Diff_singleton_if)
paulson@19944
    66
  done
paulson@19944
    67
paulson@19944
    68
lemma part_subset: "part r s YY f ==> Y \<subseteq> YY ==> part r s Y f" 
paulson@19944
    69
  by (simp add: part_def, blast)
paulson@19944
    70
  
paulson@19944
    71
paulson@19944
    72
subsection {*Ramsey's Theorem: Infinitary Version*}
paulson@19944
    73
paulson@19944
    74
lemma ramsey_induction: 
paulson@19944
    75
  fixes s::nat and r::nat
paulson@19944
    76
  shows
paulson@19944
    77
  "!!(YY::'a set) (f::'a set => nat). 
paulson@19944
    78
      [|infinite YY; part r s YY f|]
paulson@19944
    79
      ==> \<exists>Y' t'. Y' \<subseteq> YY & infinite Y' & t' < s & 
paulson@19944
    80
                  (\<forall>X. X \<subseteq> Y' & finite X & card X = r --> f X = t')"
paulson@19944
    81
proof (induct r)
paulson@19944
    82
  case 0
paulson@19944
    83
  thus ?case by (auto simp add: part_def card_eq_0_iff cong: conj_cong) 
paulson@19944
    84
next
paulson@19944
    85
  case (Suc r) 
paulson@19944
    86
  show ?case
paulson@19944
    87
  proof -
paulson@19944
    88
    from Suc.prems infinite_imp_nonempty obtain yy where yy: "yy \<in> YY" by blast
paulson@19944
    89
    let ?ramr = "{((y,Y,t),(y',Y',t')). y' \<in> Y & Y' \<subseteq> Y}"
paulson@19944
    90
    let ?propr = "%(y,Y,t).     
paulson@19944
    91
		 y \<in> YY & y \<notin> Y & Y \<subseteq> YY & infinite Y & t < s
paulson@19944
    92
		 & (\<forall>X. X\<subseteq>Y & finite X & card X = r --> (f o insert y) X = t)"
paulson@19944
    93
    have infYY': "infinite (YY-{yy})" using Suc.prems by auto
paulson@19944
    94
    have partf': "part r s (YY - {yy}) (f \<circ> insert yy)"
paulson@19944
    95
      by (simp add: o_def part_Suc_imp_part yy Suc.prems)
paulson@19944
    96
    have transr: "trans ?ramr" by (force simp add: trans_def) 
paulson@19944
    97
    from Suc.hyps [OF infYY' partf']
paulson@19944
    98
    obtain Y0 and t0
paulson@19944
    99
    where "Y0 \<subseteq> YY - {yy}"  "infinite Y0"  "t0 < s"
paulson@19944
   100
          "\<forall>X. X\<subseteq>Y0 \<and> finite X \<and> card X = r \<longrightarrow> (f \<circ> insert yy) X = t0"
paulson@19944
   101
        by blast 
paulson@19944
   102
    with yy have propr0: "?propr(yy,Y0,t0)" by blast
paulson@19944
   103
    have proprstep: "\<And>x. ?propr x \<Longrightarrow> \<exists>y. ?propr y \<and> (x, y) \<in> ?ramr" 
paulson@19944
   104
    proof -
paulson@19944
   105
      fix x
paulson@19944
   106
      assume px: "?propr x" thus "?thesis x"
paulson@19944
   107
      proof (cases x)
paulson@19944
   108
        case (fields yx Yx tx)
paulson@19944
   109
        then obtain yx' where yx': "yx' \<in> Yx" using px
paulson@19944
   110
               by (blast dest: infinite_imp_nonempty)
paulson@19944
   111
        have infYx': "infinite (Yx-{yx'})" using fields px by auto
paulson@19944
   112
        with fields px yx' Suc.prems
paulson@19944
   113
        have partfx': "part r s (Yx - {yx'}) (f \<circ> insert yx')"
paulson@19944
   114
          by (simp add: o_def part_Suc_imp_part part_subset [where ?YY=YY]) 
paulson@19944
   115
	from Suc.hyps [OF infYx' partfx']
paulson@19944
   116
	obtain Y' and t'
paulson@19944
   117
	where Y': "Y' \<subseteq> Yx - {yx'}"  "infinite Y'"  "t' < s"
paulson@19944
   118
	       "\<forall>X. X\<subseteq>Y' \<and> finite X \<and> card X = r \<longrightarrow> (f \<circ> insert yx') X = t'"
paulson@19944
   119
	    by blast 
paulson@19944
   120
	show ?thesis
paulson@19944
   121
	proof
paulson@19944
   122
	  show "?propr (yx',Y',t') & (x, (yx',Y',t')) \<in> ?ramr"
paulson@19944
   123
  	    using fields Y' yx' px by blast
paulson@19944
   124
	qed
paulson@19944
   125
      qed
paulson@19944
   126
    qed
paulson@19944
   127
    from dependent_choice [OF transr propr0 proprstep]
paulson@19944
   128
    obtain g where "(\<forall>n::nat. ?propr(g n)) & (\<forall>n m. n<m -->(g n, g m) \<in> ?ramr)"
paulson@19944
   129
      .. --{*for some reason, can't derive the following directly from dc*}
paulson@19944
   130
    hence pg: "!!n.  ?propr (g n)"
paulson@19944
   131
      and rg: "!!n m. n<m ==> (g n, g m) \<in> ?ramr" by auto
paulson@19944
   132
    let ?gy = "(\<lambda>n. let (y,Y,t) = g n in y)"
paulson@19944
   133
    let ?gt = "(\<lambda>n. let (y,Y,t) = g n in t)"
paulson@19944
   134
    have rangeg: "\<exists>k. range ?gt \<subseteq> {..<k}"
paulson@19944
   135
    proof (intro exI subsetI)
paulson@19944
   136
      fix x
paulson@19944
   137
      assume "x \<in> range ?gt"
paulson@19944
   138
      then obtain n where "x = ?gt n" ..
paulson@19944
   139
      with pg [of n] show "x \<in> {..<s}" by (cases "g n") auto
paulson@19944
   140
    qed
paulson@19944
   141
    have "\<exists>s' \<in> range ?gt. infinite (?gt -` {s'})" 
paulson@19944
   142
     by (rule inf_img_fin_dom [OF _ nat_infinite]) 
paulson@19944
   143
        (simp add: finite_nat_iff_bounded rangeg)
paulson@19944
   144
    then obtain s' and n'
paulson@19944
   145
            where s':      "s' = ?gt n'"
paulson@19944
   146
              and infeqs': "infinite {n. ?gt n = s'}"
paulson@19944
   147
       by (auto simp add: vimage_def)
paulson@19944
   148
    with pg [of n'] have less': "s'<s" by (cases "g n'") auto
paulson@19944
   149
    have inj_gy: "inj ?gy"
paulson@19944
   150
    proof (rule linorder_injI)
paulson@19944
   151
      fix m and m'::nat assume less: "m < m'" show "?gy m \<noteq> ?gy m'"
paulson@19944
   152
        using rg [OF less] pg [of m] by (cases "g m", cases "g m'", auto) 
paulson@19944
   153
    qed
paulson@19944
   154
    show ?thesis
paulson@19944
   155
    proof (intro exI conjI)
paulson@19944
   156
      show "?gy ` {n. ?gt n = s'} \<subseteq> YY" using pg
paulson@19944
   157
        by (auto simp add: Let_def split_beta) 
paulson@19944
   158
    next
paulson@19944
   159
      show "infinite (?gy ` {n. ?gt n = s'})" using infeqs'
paulson@19944
   160
        by (blast intro: inj_gy [THEN subset_inj_on] dest: finite_imageD) 
paulson@19944
   161
    next
paulson@19944
   162
      show "s' < s" by (rule less')
paulson@19944
   163
    next
paulson@19944
   164
      show "\<forall>X. X \<subseteq> ?gy ` {n. ?gt n = s'} & finite X & card X = Suc r 
paulson@19944
   165
          --> f X = s'"
paulson@19944
   166
      proof -
paulson@19944
   167
        {fix X 
paulson@19944
   168
         assume "X \<subseteq> ?gy ` {n. ?gt n = s'}"
paulson@19944
   169
            and cardX: "finite X" "card X = Suc r"
paulson@19944
   170
         then obtain AA where AA: "AA \<subseteq> {n. ?gt n = s'}" and Xeq: "X = ?gy`AA" 
paulson@19944
   171
             by (auto simp add: subset_image_iff) 
paulson@19944
   172
         with cardX have "AA\<noteq>{}" by auto
paulson@19944
   173
         hence AAleast: "(LEAST x. x \<in> AA) \<in> AA" by (auto intro: LeastI_ex) 
paulson@19944
   174
         have "f X = s'"
paulson@19944
   175
         proof (cases "g (LEAST x. x \<in> AA)") 
paulson@19944
   176
           case (fields ya Ya ta)
paulson@19944
   177
           with AAleast Xeq 
paulson@19944
   178
           have ya: "ya \<in> X" by (force intro!: rev_image_eqI) 
paulson@19944
   179
           hence "f X = f (insert ya (X - {ya}))" by (simp add: insert_absorb)
paulson@19944
   180
           also have "... = ta" 
paulson@19944
   181
           proof -
paulson@19944
   182
             have "X - {ya} \<subseteq> Ya"
paulson@19944
   183
             proof 
paulson@19944
   184
               fix x
paulson@19944
   185
               assume x: "x \<in> X - {ya}"
paulson@19944
   186
               then obtain a' where xeq: "x = ?gy a'" and a': "a' \<in> AA" 
paulson@19944
   187
                 by (auto simp add: Xeq) 
paulson@19944
   188
               hence "a' \<noteq> (LEAST x. x \<in> AA)" using x fields by auto
paulson@19944
   189
               hence lessa': "(LEAST x. x \<in> AA) < a'"
paulson@19944
   190
                 using Least_le [of "%x. x \<in> AA", OF a'] by arith
paulson@19944
   191
               show "x \<in> Ya" using xeq fields rg [OF lessa'] by auto
paulson@19944
   192
             qed
paulson@19944
   193
             moreover
paulson@19944
   194
             have "card (X - {ya}) = r"
paulson@19944
   195
               by (simp add: card_Diff_singleton_if cardX ya)
paulson@19944
   196
             ultimately show ?thesis 
paulson@19944
   197
               using pg [of "LEAST x. x \<in> AA"] fields cardX
paulson@19944
   198
               by (clarify, drule_tac x="X-{ya}" in spec, simp)
paulson@19944
   199
           qed
paulson@19944
   200
           also have "... = s'" using AA AAleast fields by auto
paulson@19944
   201
           finally show ?thesis .
paulson@19944
   202
         qed}
paulson@19944
   203
        thus ?thesis by blast
paulson@19944
   204
      qed 
paulson@19944
   205
    qed 
paulson@19944
   206
  qed
paulson@19944
   207
qed
paulson@19944
   208
paulson@19944
   209
paulson@19944
   210
text{*Repackaging of Tom Ridge's final result*}
paulson@19944
   211
theorem Ramsey:
paulson@19944
   212
  fixes s::nat and r::nat and Z::"'a set" and f::"'a set => nat"
paulson@19944
   213
  shows
paulson@19944
   214
   "[|infinite Z;
paulson@19944
   215
      \<forall>X. X \<subseteq> Z & finite X & card X = r --> f X < s|]
paulson@19944
   216
  ==> \<exists>Y t. Y \<subseteq> Z & infinite Y & t < s 
paulson@19944
   217
            & (\<forall>X. X \<subseteq> Y & finite X & card X = r --> f X = t)"
paulson@19944
   218
by (blast intro: ramsey_induction [unfolded part_def, rule_format]) 
paulson@19944
   219
paulson@19944
   220
end
paulson@19944
   221