src/HOL/Library/Ramsey.thy
author wenzelm
Wed, 08 Mar 2017 10:50:59 +0100
changeset 65151 a7394aa4d21c
parent 65075 03e6aa683c4d
child 67091 1393c2340eec
permissions -rw-r--r--
tuned proofs;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
     1
(*  Title:      HOL/Library/Ramsey.thy
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 30738
diff changeset
     2
    Author:     Tom Ridge.  Converted to structured Isar by L C Paulson
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
     3
*)
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
     4
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
     5
section \<open>Ramsey's Theorem\<close>
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
     6
25594
43c718438f9f switched import from Main to PreList
haftmann
parents: 24853
diff changeset
     7
theory Ramsey
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
     8
  imports Infinite_Set
25594
43c718438f9f switched import from Main to PreList
haftmann
parents: 24853
diff changeset
     9
begin
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
    10
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    11
subsection \<open>Finite Ramsey theorem(s)\<close>
40695
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
    12
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    13
text \<open>
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    14
  To distinguish the finite and infinite ones, lower and upper case
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    15
  names are used.
40695
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
    16
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    17
  This is the most basic version in terms of cliques and independent
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    18
  sets, i.e. the version for graphs and 2 colours.
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    19
\<close>
40695
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
    20
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    21
definition "clique V E \<longleftrightarrow> (\<forall>v\<in>V. \<forall>w\<in>V. v \<noteq> w \<longrightarrow> {v, w} \<in> E)"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    22
definition "indep V E \<longleftrightarrow> (\<forall>v\<in>V. \<forall>w\<in>V. v \<noteq> w \<longrightarrow> {v, w} \<notin> E)"
40695
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
    23
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
    24
lemma ramsey2:
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    25
  "\<exists>r\<ge>1. \<forall>(V::'a set) (E::'a set set). finite V \<and> card V \<ge> r \<longrightarrow>
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    26
    (\<exists>R \<subseteq> V. card R = m \<and> clique R E \<or> card R = n \<and> indep R E)"
40695
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
    27
  (is "\<exists>r\<ge>1. ?R m n r")
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    28
proof (induct k \<equiv> "m + n" arbitrary: m n)
40695
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
    29
  case 0
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    30
  show ?case (is "EX r. ?Q r")
40695
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
    31
  proof
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    32
    from 0 show "?Q 1"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    33
      by (clarsimp simp: indep_def) (metis card.empty emptyE empty_subsetI)
40695
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
    34
  qed
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
    35
next
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
    36
  case (Suc k)
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    37
  consider "m = 0 \<or> n = 0" | "m \<noteq> 0" "n \<noteq> 0" by auto
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    38
  then show ?case (is "EX r. ?Q r")
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    39
  proof cases
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    40
    case 1
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    41
    then have "?Q 1"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    42
      by (simp add: clique_def) (meson card_empty empty_iff empty_subsetI indep_def)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    43
    then show ?thesis ..
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    44
  next
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    45
    case 2
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    46
    with Suc(2) have "k = (m - 1) + n" "k = m + (n - 1)" by auto
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    47
    from this [THEN Suc(1)]
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    48
    obtain r1 r2 where "r1 \<ge> 1" "r2 \<ge> 1" "?R (m - 1) n r1" "?R m (n - 1) r2" by auto
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    49
    then have "r1 + r2 \<ge> 1" by arith
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    50
    moreover have "?R m n (r1 + r2)" (is "\<forall>V E. _ \<longrightarrow> ?EX V E m n")
40695
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
    51
    proof clarify
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    52
      fix V :: "'a set"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    53
      fix E :: "'a set set"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    54
      assume "finite V" "r1 + r2 \<le> card V"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    55
      with \<open>r1 \<ge> 1\<close> have "V \<noteq> {}" by auto
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    56
      then obtain v where "v \<in> V" by blast
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    57
      let ?M = "{w \<in> V. w \<noteq> v \<and> {v, w} \<in> E}"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    58
      let ?N = "{w \<in> V. w \<noteq> v \<and> {v, w} \<notin> E}"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    59
      from \<open>v \<in> V\<close> have "V = insert v (?M \<union> ?N)" by auto
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    60
      then have "card V = card (insert v (?M \<union> ?N))" by metis
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    61
      also from \<open>finite V\<close> have "\<dots> = card ?M + card ?N + 1"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    62
        by (fastforce intro: card_Un_disjoint)
40695
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
    63
      finally have "card V = card ?M + card ?N + 1" .
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    64
      with \<open>r1 + r2 \<le> card V\<close> have "r1 + r2 \<le> card ?M + card ?N + 1" by simp
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    65
      then consider "r1 \<le> card ?M" | "r2 \<le> card ?N" by arith
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    66
      then show "?EX V E m n"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    67
      proof cases
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    68
        case 1
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    69
        from \<open>finite V\<close> have "finite ?M" by auto
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    70
        with \<open>?R (m - 1) n r1\<close> and 1 have "?EX ?M E (m - 1) n" by blast
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    71
        then obtain R where "R \<subseteq> ?M" "v \<notin> R"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    72
          and CI: "card R = m - 1 \<and> clique R E \<or> card R = n \<and> indep R E" (is "?C \<or> ?I")
40695
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
    73
          by blast
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    74
        from \<open>R \<subseteq> ?M\<close> have "R \<subseteq> V" by auto
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    75
        with \<open>finite V\<close> have "finite R" by (metis finite_subset)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    76
        from CI show ?thesis
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    77
        proof
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    78
          assume "?I"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    79
          with \<open>R \<subseteq> V\<close> show ?thesis by blast
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    80
        next
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    81
          assume "?C"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    82
          with \<open>R \<subseteq> ?M\<close> have *: "clique (insert v R) E"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    83
            by (auto simp: clique_def insert_commute)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    84
          from \<open>?C\<close> \<open>finite R\<close> \<open>v \<notin> R\<close> \<open>m \<noteq> 0\<close> have "card (insert v R) = m" by simp
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    85
          with \<open>R \<subseteq> V\<close> \<open>v \<in> V\<close> * show ?thesis by (metis insert_subset)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    86
        qed
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    87
      next
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    88
        case 2
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    89
        from \<open>finite V\<close> have "finite ?N" by auto
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    90
        with \<open>?R m (n - 1) r2\<close> 2 have "?EX ?N E m (n - 1)" by blast
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    91
        then obtain R where "R \<subseteq> ?N" "v \<notin> R"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    92
          and CI: "card R = m \<and> clique R E \<or> card R = n - 1 \<and> indep R E" (is "?C \<or> ?I")
40695
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
    93
          by blast
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    94
        from \<open>R \<subseteq> ?N\<close> have "R \<subseteq> V" by auto
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    95
        with \<open>finite V\<close> have "finite R" by (metis finite_subset)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    96
        from CI show ?thesis
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    97
        proof
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    98
          assume "?C"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
    99
          with \<open>R \<subseteq> V\<close> show ?thesis by blast
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   100
        next
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   101
          assume "?I"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   102
          with \<open>R \<subseteq> ?N\<close> have *: "indep (insert v R) E"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   103
            by (auto simp: indep_def insert_commute)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   104
          from \<open>?I\<close> \<open>finite R\<close> \<open>v \<notin> R\<close> \<open>n \<noteq> 0\<close> have "card (insert v R) = n" by simp
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   105
          with \<open>R \<subseteq> V\<close> \<open>v \<in> V\<close> * show ?thesis by (metis insert_subset)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   106
        qed
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   107
      qed
40695
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
   108
    qed
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   109
    ultimately show ?thesis by blast
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   110
  qed
40695
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
   111
qed
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
   112
1b2573c3b222 Added the simplest finite Ramsey theorem
nipkow
parents: 40077
diff changeset
   113
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 58881
diff changeset
   114
subsection \<open>Preliminaries\<close>
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   115
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 58881
diff changeset
   116
subsubsection \<open>``Axiom'' of Dependent Choice\<close>
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   117
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   118
primrec choice :: "('a \<Rightarrow> bool) \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> nat \<Rightarrow> 'a"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   119
  where \<comment> \<open>An integer-indexed chain of choices\<close>
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   120
    choice_0: "choice P r 0 = (SOME x. P x)"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   121
  | choice_Suc: "choice P r (Suc n) = (SOME y. P y \<and> (choice P r n, y) \<in> r)"
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   122
46575
f1e387195a56 misc tuning;
wenzelm
parents: 44890
diff changeset
   123
lemma choice_n:
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   124
  assumes P0: "P x0"
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   125
    and Pstep: "\<And>x. P x \<Longrightarrow> \<exists>y. P y \<and> (x, y) \<in> r"
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   126
  shows "P (choice P r n)"
19948
1be283f3f1ba minor tuning of definitions/proofs;
wenzelm
parents: 19946
diff changeset
   127
proof (induct n)
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   128
  case 0
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   129
  show ?case by (force intro: someI P0)
19948
1be283f3f1ba minor tuning of definitions/proofs;
wenzelm
parents: 19946
diff changeset
   130
next
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   131
  case Suc
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   132
  then show ?case by (auto intro: someI2_ex [OF Pstep])
19948
1be283f3f1ba minor tuning of definitions/proofs;
wenzelm
parents: 19946
diff changeset
   133
qed
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   134
46575
f1e387195a56 misc tuning;
wenzelm
parents: 44890
diff changeset
   135
lemma dependent_choice:
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   136
  assumes trans: "trans r"
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   137
    and P0: "P x0"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   138
    and Pstep: "\<And>x. P x \<Longrightarrow> \<exists>y. P y \<and> (x, y) \<in> r"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   139
  obtains f :: "nat \<Rightarrow> 'a" where "\<And>n. P (f n)" and "\<And>n m. n < m \<Longrightarrow> (f n, f m) \<in> r"
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   140
proof
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   141
  fix n
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   142
  show "P (choice P r n)"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   143
    by (blast intro: choice_n [OF P0 Pstep])
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   144
next
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   145
  fix n m :: nat
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   146
  assume "n < m"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   147
  from Pstep [OF choice_n [OF P0 Pstep]] have "(choice P r k, choice P r (Suc k)) \<in> r" for k
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   148
    by (auto intro: someI2_ex)
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   149
  then show "(choice P r n, choice P r m) \<in> r"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   150
    by (auto intro: less_Suc_induct [OF \<open>n < m\<close>] transD [OF trans])
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   151
qed
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   152
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   153
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 58881
diff changeset
   154
subsubsection \<open>Partitions of a Set\<close>
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   155
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   156
definition part :: "nat \<Rightarrow> nat \<Rightarrow> 'a set \<Rightarrow> ('a set \<Rightarrow> nat) \<Rightarrow> bool"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   157
  \<comment> \<open>the function @{term f} partitions the @{term r}-subsets of the typically
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   158
      infinite set @{term Y} into @{term s} distinct categories.\<close>
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   159
  where "part r s Y f \<longleftrightarrow> (\<forall>X. X \<subseteq> Y \<and> finite X \<and> card X = r \<longrightarrow> f X < s)"
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   160
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   161
text \<open>For induction, we decrease the value of @{term r} in partitions.\<close>
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   162
lemma part_Suc_imp_part:
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   163
  "\<lbrakk>infinite Y; part (Suc r) s Y f; y \<in> Y\<rbrakk> \<Longrightarrow> part r s (Y - {y}) (\<lambda>u. f (insert y u))"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   164
  apply (simp add: part_def)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   165
  apply clarify
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   166
  apply (drule_tac x="insert y X" in spec)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   167
  apply force
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   168
  done
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   169
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   170
lemma part_subset: "part r s YY f \<Longrightarrow> Y \<subseteq> YY \<Longrightarrow> part r s Y f"
19948
1be283f3f1ba minor tuning of definitions/proofs;
wenzelm
parents: 19946
diff changeset
   171
  unfolding part_def by blast
46575
f1e387195a56 misc tuning;
wenzelm
parents: 44890
diff changeset
   172
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   173
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 58881
diff changeset
   174
subsection \<open>Ramsey's Theorem: Infinitary Version\<close>
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   175
46575
f1e387195a56 misc tuning;
wenzelm
parents: 44890
diff changeset
   176
lemma Ramsey_induction:
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   177
  fixes s r :: nat
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   178
    and YY :: "'a set"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   179
    and f :: "'a set \<Rightarrow> nat"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   180
  assumes "infinite YY" "part r s YY f"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   181
  shows "\<exists>Y' t'. Y' \<subseteq> YY \<and> infinite Y' \<and> t' < s \<and> (\<forall>X. X \<subseteq> Y' \<and> finite X \<and> card X = r \<longrightarrow> f X = t')"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   182
  using assms
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   183
proof (induct r arbitrary: YY f)
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   184
  case 0
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   185
  then show ?case
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   186
    by (auto simp add: part_def card_eq_0_iff cong: conj_cong)
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   187
next
46575
f1e387195a56 misc tuning;
wenzelm
parents: 44890
diff changeset
   188
  case (Suc r)
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   189
  show ?case
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   190
  proof -
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   191
    from Suc.prems infinite_imp_nonempty obtain yy where yy: "yy \<in> YY"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   192
      by blast
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   193
    let ?ramr = "{((y, Y, t), (y', Y', t')). y' \<in> Y \<and> Y' \<subseteq> Y}"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   194
    let ?propr = "\<lambda>(y, Y, t).
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   195
                 y \<in> YY \<and> y \<notin> Y \<and> Y \<subseteq> YY \<and> infinite Y \<and> t < s
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   196
                 \<and> (\<forall>X. X\<subseteq>Y \<and> finite X \<and> card X = r \<longrightarrow> (f \<circ> insert y) X = t)"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   197
    from Suc.prems have infYY': "infinite (YY - {yy})" by auto
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   198
    from Suc.prems have partf': "part r s (YY - {yy}) (f \<circ> insert yy)"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   199
      by (simp add: o_def part_Suc_imp_part yy)
46575
f1e387195a56 misc tuning;
wenzelm
parents: 44890
diff changeset
   200
    have transr: "trans ?ramr" by (force simp add: trans_def)
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   201
    from Suc.hyps [OF infYY' partf']
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   202
    obtain Y0 and t0 where "Y0 \<subseteq> YY - {yy}" "infinite Y0" "t0 < s"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   203
      "X \<subseteq> Y0 \<and> finite X \<and> card X = r \<longrightarrow> (f \<circ> insert yy) X = t0" for X
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   204
      by blast
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   205
    with yy have propr0: "?propr(yy, Y0, t0)" by blast
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   206
    have proprstep: "\<exists>y. ?propr y \<and> (x, y) \<in> ?ramr" if x: "?propr x" for x
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   207
    proof (cases x)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   208
      case (fields yx Yx tx)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   209
      with x obtain yx' where yx': "yx' \<in> Yx"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   210
        by (blast dest: infinite_imp_nonempty)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   211
      from fields x have infYx': "infinite (Yx - {yx'})" by auto
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   212
      with fields x yx' Suc.prems have partfx': "part r s (Yx - {yx'}) (f \<circ> insert yx')"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   213
        by (simp add: o_def part_Suc_imp_part part_subset [where YY=YY and Y=Yx])
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   214
      from Suc.hyps [OF infYx' partfx'] obtain Y' and t'
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   215
        where Y': "Y' \<subseteq> Yx - {yx'}" "infinite Y'" "t' < s"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   216
          "X \<subseteq> Y' \<and> finite X \<and> card X = r \<longrightarrow> (f \<circ> insert yx') X = t'" for X
46575
f1e387195a56 misc tuning;
wenzelm
parents: 44890
diff changeset
   217
        by blast
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   218
      from fields x Y' yx' have "?propr (yx', Y', t') \<and> (x, (yx', Y', t')) \<in> ?ramr"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   219
        by blast
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   220
      then show ?thesis ..
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   221
    qed
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   222
    from dependent_choice [OF transr propr0 proprstep]
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   223
    obtain g where pg: "?propr (g n)" and rg: "n < m \<Longrightarrow> (g n, g m) \<in> ?ramr" for n m :: nat
63060
293ede07b775 some uses of 'obtain' with structure statement;
wenzelm
parents: 61585
diff changeset
   224
      by blast
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   225
    let ?gy = "fst \<circ> g"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   226
    let ?gt = "snd \<circ> snd \<circ> g"
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   227
    have rangeg: "\<exists>k. range ?gt \<subseteq> {..<k}"
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   228
    proof (intro exI subsetI)
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   229
      fix x
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   230
      assume "x \<in> range ?gt"
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   231
      then obtain n where "x = ?gt n" ..
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   232
      with pg [of n] show "x \<in> {..<s}" by (cases "g n") auto
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   233
    qed
20810
3377a830b727 moved theory Infinite_Set to Library;
wenzelm
parents: 19954
diff changeset
   234
    have "finite (range ?gt)"
3377a830b727 moved theory Infinite_Set to Library;
wenzelm
parents: 19954
diff changeset
   235
      by (simp add: finite_nat_iff_bounded rangeg)
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   236
    then obtain s' and n' where s': "s' = ?gt n'" and infeqs': "infinite {n. ?gt n = s'}"
54580
7b9336176a1c adapt to 9733ab5c1df6
traytel
parents: 53374
diff changeset
   237
      by (rule inf_img_fin_domE) (auto simp add: vimage_def intro: infinite_UNIV_nat)
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   238
    with pg [of n'] have less': "s'<s" by (cases "g n'") auto
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   239
    have inj_gy: "inj ?gy"
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   240
    proof (rule linorder_injI)
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   241
      fix m m' :: nat
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   242
      assume "m < m'"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   243
      from rg [OF this] pg [of m] show "?gy m \<noteq> ?gy m'"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   244
        by (cases "g m", cases "g m'") auto
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   245
    qed
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   246
    show ?thesis
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   247
    proof (intro exI conjI)
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   248
      from pg show "?gy ` {n. ?gt n = s'} \<subseteq> YY"
46575
f1e387195a56 misc tuning;
wenzelm
parents: 44890
diff changeset
   249
        by (auto simp add: Let_def split_beta)
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   250
      from infeqs' show "infinite (?gy ` {n. ?gt n = s'})"
46575
f1e387195a56 misc tuning;
wenzelm
parents: 44890
diff changeset
   251
        by (blast intro: inj_gy [THEN subset_inj_on] dest: finite_imageD)
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   252
      show "s' < s" by (rule less')
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   253
      show "\<forall>X. X \<subseteq> ?gy ` {n. ?gt n = s'} \<and> finite X \<and> card X = Suc r \<longrightarrow> f X = s'"
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   254
      proof -
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   255
        have "f X = s'"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   256
          if X: "X \<subseteq> ?gy ` {n. ?gt n = s'}"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   257
          and cardX: "finite X" "card X = Suc r"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   258
          for X
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   259
        proof -
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   260
          from X obtain AA where AA: "AA \<subseteq> {n. ?gt n = s'}" and Xeq: "X = ?gy`AA"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   261
            by (auto simp add: subset_image_iff)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   262
          with cardX have "AA \<noteq> {}" by auto
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   263
          then have AAleast: "(LEAST x. x \<in> AA) \<in> AA" by (auto intro: LeastI_ex)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   264
          show ?thesis
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   265
          proof (cases "g (LEAST x. x \<in> AA)")
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   266
            case (fields ya Ya ta)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   267
            with AAleast Xeq have ya: "ya \<in> X" by (force intro!: rev_image_eqI)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   268
            then have "f X = f (insert ya (X - {ya}))" by (simp add: insert_absorb)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   269
            also have "\<dots> = ta"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   270
            proof -
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   271
              have *: "X - {ya} \<subseteq> Ya"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   272
              proof
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   273
                fix x assume x: "x \<in> X - {ya}"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   274
                then obtain a' where xeq: "x = ?gy a'" and a': "a' \<in> AA"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   275
                  by (auto simp add: Xeq)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   276
                with fields x have "a' \<noteq> (LEAST x. x \<in> AA)" by auto
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   277
                with Least_le [of "\<lambda>x. x \<in> AA", OF a'] have "(LEAST x. x \<in> AA) < a'"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   278
                  by arith
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   279
                from xeq fields rg [OF this] show "x \<in> Ya" by auto
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   280
              qed
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   281
              have "card (X - {ya}) = r"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   282
                by (simp add: cardX ya)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   283
              with pg [of "LEAST x. x \<in> AA"] fields cardX * show ?thesis
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   284
                by (auto simp del: insert_Diff_single)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   285
            qed
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   286
            also from AA AAleast fields have "\<dots> = s'" by auto
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   287
            finally show ?thesis .
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   288
          qed
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   289
        qed
46575
f1e387195a56 misc tuning;
wenzelm
parents: 44890
diff changeset
   290
        then show ?thesis by blast
f1e387195a56 misc tuning;
wenzelm
parents: 44890
diff changeset
   291
      qed
f1e387195a56 misc tuning;
wenzelm
parents: 44890
diff changeset
   292
    qed
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   293
  qed
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   294
qed
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   295
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   296
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   297
theorem Ramsey:
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   298
  fixes s r :: nat
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   299
    and Z :: "'a set"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   300
    and f :: "'a set \<Rightarrow> nat"
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   301
  shows
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   302
   "\<lbrakk>infinite Z;
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   303
      \<forall>X. X \<subseteq> Z \<and> finite X \<and> card X = r \<longrightarrow> f X < s\<rbrakk>
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   304
    \<Longrightarrow> \<exists>Y t. Y \<subseteq> Z \<and> infinite Y \<and> t < s
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   305
            \<and> (\<forall>X. X \<subseteq> Y \<and> finite X \<and> card X = r \<longrightarrow> f X = t)"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   306
  by (blast intro: Ramsey_induction [unfolded part_def])
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   307
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   308
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   309
corollary Ramsey2:
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   310
  fixes s :: nat
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   311
    and Z :: "'a set"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   312
    and f :: "'a set \<Rightarrow> nat"
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   313
  assumes infZ: "infinite Z"
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   314
    and part: "\<forall>x\<in>Z. \<forall>y\<in>Z. x \<noteq> y \<longrightarrow> f {x, y} < s"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   315
  shows "\<exists>Y t. Y \<subseteq> Z \<and> infinite Y \<and> t < s \<and> (\<forall>x\<in>Y. \<forall>y\<in>Y. x\<noteq>y \<longrightarrow> f {x, y} = t)"
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   316
proof -
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   317
  from part have part2: "\<forall>X. X \<subseteq> Z \<and> finite X \<and> card X = 2 \<longrightarrow> f X < s"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   318
    by (fastforce simp add: eval_nat_numeral card_Suc_eq)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   319
  obtain Y t where *:
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   320
    "Y \<subseteq> Z" "infinite Y" "t < s" "(\<forall>X. X \<subseteq> Y \<and> finite X \<and> card X = 2 \<longrightarrow> f X = t)"
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   321
    by (insert Ramsey [OF infZ part2]) auto
53374
a14d2a854c02 tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents: 46575
diff changeset
   322
  then have "\<forall>x\<in>Y. \<forall>y\<in>Y. x \<noteq> y \<longrightarrow> f {x, y} = t" by auto
a14d2a854c02 tuned proofs -- clarified flow of facts wrt. calculation;
wenzelm
parents: 46575
diff changeset
   323
  with * show ?thesis by iprover
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   324
qed
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   325
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   326
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 58881
diff changeset
   327
subsection \<open>Disjunctive Well-Foundedness\<close>
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   328
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 58881
diff changeset
   329
text \<open>
22367
6860f09242bf tuned document;
wenzelm
parents: 21634
diff changeset
   330
  An application of Ramsey's theorem to program termination. See
58622
aa99568f56de more antiquotations;
wenzelm
parents: 54580
diff changeset
   331
  @{cite "Podelski-Rybalchenko"}.
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 58881
diff changeset
   332
\<close>
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   333
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   334
definition disj_wf :: "('a \<times> 'a) set \<Rightarrow> bool"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   335
  where "disj_wf r \<longleftrightarrow> (\<exists>T. \<exists>n::nat. (\<forall>i<n. wf (T i)) \<and> r = (\<Union>i<n. T i))"
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   336
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   337
definition transition_idx :: "(nat \<Rightarrow> 'a) \<Rightarrow> (nat \<Rightarrow> ('a \<times> 'a) set) \<Rightarrow> nat set \<Rightarrow> nat"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   338
  where "transition_idx s T A = (LEAST k. \<exists>i j. A = {i, j} \<and> i < j \<and> (s j, s i) \<in> T k)"
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   339
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   340
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   341
lemma transition_idx_less:
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   342
  assumes "i < j" "(s j, s i) \<in> T k" "k < n"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   343
  shows "transition_idx s T {i, j} < n"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   344
proof -
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   345
  from assms(1,2) have "transition_idx s T {i, j} \<le> k"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   346
    by (simp add: transition_idx_def, blast intro: Least_le)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   347
  with assms(3) show ?thesis by simp
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   348
qed
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   349
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   350
lemma transition_idx_in:
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   351
  assumes "i < j" "(s j, s i) \<in> T k"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   352
  shows "(s j, s i) \<in> T (transition_idx s T {i, j})"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   353
  using assms
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   354
  by (simp add: transition_idx_def doubleton_eq_iff conj_disj_distribR cong: conj_cong) (erule LeastI)
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   355
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   356
text \<open>To be equal to the union of some well-founded relations is equivalent
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   357
  to being the subset of such a union.\<close>
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   358
lemma disj_wf: "disj_wf r \<longleftrightarrow> (\<exists>T. \<exists>n::nat. (\<forall>i<n. wf(T i)) \<and> r \<subseteq> (\<Union>i<n. T i))"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   359
  apply (auto simp add: disj_wf_def)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   360
  apply (rule_tac x="\<lambda>i. T i Int r" in exI)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   361
  apply (rule_tac x=n in exI)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   362
  apply (force simp add: wf_Int1)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   363
  done
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   364
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   365
theorem trans_disj_wf_implies_wf:
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   366
  assumes "trans r"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   367
    and "disj_wf r"
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   368
  shows "wf r"
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   369
proof (simp only: wf_iff_no_infinite_down_chain, rule notI)
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   370
  assume "\<exists>s. \<forall>i. (s (Suc i), s i) \<in> r"
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   371
  then obtain s where sSuc: "\<forall>i. (s (Suc i), s i) \<in> r" ..
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   372
  from \<open>disj_wf r\<close> obtain T and n :: nat where wfT: "\<forall>k<n. wf(T k)" and r: "r = (\<Union>k<n. T k)"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   373
    by (auto simp add: disj_wf_def)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   374
  have s_in_T: "\<exists>k. (s j, s i) \<in> T k \<and> k<n" if "i < j" for i j
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   375
  proof -
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   376
    from \<open>i < j\<close> have "(s j, s i) \<in> r"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   377
    proof (induct rule: less_Suc_induct)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   378
      case 1
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   379
      then show ?case by (simp add: sSuc)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   380
    next
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   381
      case 2
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   382
      with \<open>trans r\<close> show ?case
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   383
        unfolding trans_def by blast
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   384
    qed
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   385
    then show ?thesis by (auto simp add: r)
46575
f1e387195a56 misc tuning;
wenzelm
parents: 44890
diff changeset
   386
  qed
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   387
  have trless: "i \<noteq> j \<Longrightarrow> transition_idx s T {i, j} < n" for i j
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   388
    apply (auto simp add: linorder_neq_iff)
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   389
     apply (blast dest: s_in_T transition_idx_less)
46575
f1e387195a56 misc tuning;
wenzelm
parents: 44890
diff changeset
   390
    apply (subst insert_commute)
f1e387195a56 misc tuning;
wenzelm
parents: 44890
diff changeset
   391
    apply (blast dest: s_in_T transition_idx_less)
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   392
    done
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   393
  have "\<exists>K k. K \<subseteq> UNIV \<and> infinite K \<and> k < n \<and>
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   394
      (\<forall>i\<in>K. \<forall>j\<in>K. i \<noteq> j \<longrightarrow> transition_idx s T {i, j} = k)"
54580
7b9336176a1c adapt to 9733ab5c1df6
traytel
parents: 53374
diff changeset
   395
    by (rule Ramsey2) (auto intro: trless infinite_UNIV_nat)
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   396
  then obtain K and k where infK: "infinite K" and "k < n"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   397
    and allk: "\<forall>i\<in>K. \<forall>j\<in>K. i \<noteq> j \<longrightarrow> transition_idx s T {i, j} = k"
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   398
    by auto
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   399
  have "(s (enumerate K (Suc m)), s (enumerate K m)) \<in> T k" for m :: nat
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   400
  proof -
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   401
    let ?j = "enumerate K (Suc m)"
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   402
    let ?i = "enumerate K m"
46575
f1e387195a56 misc tuning;
wenzelm
parents: 44890
diff changeset
   403
    have ij: "?i < ?j" by (simp add: enumerate_step infK)
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   404
    have "?j \<in> K" "?i \<in> K" by (simp_all add: enumerate_in_set infK)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   405
    with ij have k: "k = transition_idx s T {?i, ?j}" by (simp add: allk)
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   406
    from s_in_T [OF ij] obtain k' where "(s ?j, s ?i) \<in> T k'" "k'<n" by blast
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   407
    then show "(s ?j, s ?i) \<in> T k" by (simp add: k transition_idx_in ij)
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   408
  qed
65075
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   409
  then have "\<not> wf (T k)"
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   410
    unfolding wf_iff_no_infinite_down_chain by fast
03e6aa683c4d misc tuning and modernization;
wenzelm
parents: 63060
diff changeset
   411
  with wfT \<open>k < n\<close> show False by blast
19954
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   412
qed
e4c9f6946db3 disjunctive wellfoundedness
paulson
parents: 19949
diff changeset
   413
19944
60e0cbeae3d8 Introduction of Ramsey's theorem
paulson
parents:
diff changeset
   414
end