src/HOL/Multivariate_Analysis/Brouwer_Fixpoint.thy
changeset 33741 4c414d0835ab
child 33758 53078b0d21f5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Multivariate_Analysis/Brouwer_Fixpoint.thy	Tue Nov 17 18:52:30 2009 +0100
     1.3 @@ -0,0 +1,1983 @@
     1.4 +
     1.5 +(* ========================================================================= *)
     1.6 +(* Results connected with topological dimension.                             *)
     1.7 +(*                                                                           *)
     1.8 +(* At the moment this is just Brouwer's fixpoint theorem. The proof is from  *)
     1.9 +(* Kuhn: "some combinatorial lemmas in topology", IBM J. v4. (1960) p. 518   *)
    1.10 +(* See "http://www.research.ibm.com/journal/rd/045/ibmrd0405K.pdf".          *)
    1.11 +(*                                                                           *)
    1.12 +(* The script below is quite messy, but at least we avoid formalizing any    *)
    1.13 +(* topological machinery; we don't even use barycentric subdivision; this is *)
    1.14 +(* the big advantage of Kuhn's proof over the usual Sperner's lemma one.     *)
    1.15 +(*                                                                           *)
    1.16 +(*              (c) Copyright, John Harrison 1998-2008                       *)
    1.17 +(* ========================================================================= *)
    1.18 +
    1.19 +(* Author:                       John Harrison
    1.20 +   Translated to from HOL light: Robert Himmelmann, TU Muenchen *)
    1.21 +
    1.22 +header {* Results connected with topological dimension. *}
    1.23 +
    1.24 +theory Brouwer_Fixpoint
    1.25 +  imports Convex_Euclidean_Space
    1.26 +begin
    1.27 +
    1.28 +declare norm_scaleR[simp]
    1.29 + 
    1.30 +lemma brouwer_compactness_lemma:
    1.31 +  assumes "compact s" "continuous_on s f" "\<not> (\<exists>x\<in>s. (f x = (0::real^'n::finite)))"
    1.32 +  obtains d where "0 < d" "\<forall>x\<in>s. d \<le> norm(f x)" proof(cases "s={}") case False
    1.33 +  have "continuous_on s (norm \<circ> f)" by(rule continuous_on_intros continuous_on_norm assms(2))+
    1.34 +  then obtain x where x:"x\<in>s" "\<forall>y\<in>s. (norm \<circ> f) x \<le> (norm \<circ> f) y"
    1.35 +    using continuous_attains_inf[OF assms(1), of "norm \<circ> f"] and False unfolding o_def by auto
    1.36 +  have "(norm \<circ> f) x > 0" using assms(3) and x(1) by auto
    1.37 +  thus ?thesis apply(rule that) using x(2) unfolding o_def by auto qed(rule that[of 1], auto)
    1.38 +
    1.39 +lemma kuhn_labelling_lemma:
    1.40 +  assumes "(\<forall>x::real^'n. P x \<longrightarrow> P (f x))"  "\<forall>x. P x \<longrightarrow> (\<forall>i::'n. Q i \<longrightarrow> 0 \<le> x$i \<and> x$i \<le> 1)"
    1.41 +  shows "\<exists>l. (\<forall>x i. l x i \<le> (1::nat)) \<and>
    1.42 +             (\<forall>x i. P x \<and> Q i \<and> (x$i = 0) \<longrightarrow> (l x i = 0)) \<and>
    1.43 +             (\<forall>x i. P x \<and> Q i \<and> (x$i = 1) \<longrightarrow> (l x i = 1)) \<and>
    1.44 +             (\<forall>x i. P x \<and> Q i \<and> (l x i = 0) \<longrightarrow> x$i \<le> f(x)$i) \<and>
    1.45 +             (\<forall>x i. P x \<and> Q i \<and> (l x i = 1) \<longrightarrow> f(x)$i \<le> x$i)" proof-
    1.46 +  have and_forall_thm:"\<And>P Q. (\<forall>x. P x) \<and> (\<forall>x. Q x) \<longleftrightarrow> (\<forall>x. P x \<and> Q x)" by auto
    1.47 +  have *:"\<forall>x y::real. 0 \<le> x \<and> x \<le> 1 \<and> 0 \<le> y \<and> y \<le> 1 \<longrightarrow> (x \<noteq> 1 \<and> x \<le> y \<or> x \<noteq> 0 \<and> y \<le> x)" by auto
    1.48 +  show ?thesis unfolding and_forall_thm apply(subst choice_iff[THEN sym])+ proof(rule,rule) case goal1
    1.49 +    let ?R = "\<lambda>y. (P x \<and> Q xa \<and> x $ xa = 0 \<longrightarrow> y = (0::nat)) \<and>
    1.50 +        (P x \<and> Q xa \<and> x $ xa = 1 \<longrightarrow> y = 1) \<and> (P x \<and> Q xa \<and> y = 0 \<longrightarrow> x $ xa \<le> f x $ xa) \<and> (P x \<and> Q xa \<and> y = 1 \<longrightarrow> f x $ xa \<le> x $ xa)"
    1.51 +    { assume "P x" "Q xa" hence "0 \<le> f x $ xa \<and> f x $ xa \<le> 1" using assms(2)[rule_format,of "f x" xa]
    1.52 +        apply(drule_tac assms(1)[rule_format]) by auto }
    1.53 +    hence "?R 0 \<or> ?R 1" by auto thus ?case by auto qed qed 
    1.54 + 
    1.55 +subsection {* The key "counting" observation, somewhat abstracted. *}
    1.56 +
    1.57 +lemma setsum_Un_disjoint':assumes "finite A" "finite B" "A \<inter> B = {}" "A \<union> B = C"
    1.58 +  shows "setsum g C = setsum g A + setsum g B"
    1.59 +  using setsum_Un_disjoint[OF assms(1-3)] and assms(4) by auto
    1.60 +
    1.61 +lemma kuhn_counting_lemma: assumes "finite faces" "finite simplices"
    1.62 +  "\<forall>f\<in>faces. bnd f  \<longrightarrow> (card {s \<in> simplices. face f s} = 1)"
    1.63 +  "\<forall>f\<in>faces. \<not> bnd f \<longrightarrow> (card {s \<in> simplices. face f s} = 2)"
    1.64 +  "\<forall>s\<in>simplices. compo s  \<longrightarrow> (card {f \<in> faces. face f s \<and> compo' f} = 1)"
    1.65 +  "\<forall>s\<in>simplices. \<not> compo s \<longrightarrow> (card {f \<in> faces. face f s \<and> compo' f} = 0) \<or>
    1.66 +                             (card {f \<in> faces. face f s \<and> compo' f} = 2)"
    1.67 +    "odd(card {f \<in> faces. compo' f \<and> bnd f})"
    1.68 +  shows "odd(card {s \<in> simplices. compo s})" proof-
    1.69 +  have "\<And>x. {f\<in>faces. compo' f \<and> bnd f \<and> face f x} \<union> {f\<in>faces. compo' f \<and> \<not>bnd f \<and> face f x} = {f\<in>faces. compo' f \<and> face f x}"
    1.70 +    "\<And>x. {f \<in> faces. compo' f \<and> bnd f \<and> face f x} \<inter> {f \<in> faces. compo' f \<and> \<not> bnd f \<and> face f x} = {}" by auto
    1.71 +  hence lem1:"setsum (\<lambda>s. (card {f \<in> faces. face f s \<and> compo' f})) simplices =
    1.72 +    setsum (\<lambda>s. card {f \<in> {f \<in> faces. compo' f \<and> bnd f}. face f s}) simplices +
    1.73 +    setsum (\<lambda>s. card {f \<in> {f \<in> faces. compo' f \<and> \<not> (bnd f)}. face f s}) simplices"
    1.74 +    unfolding setsum_addf[THEN sym] apply- apply(rule setsum_cong2)
    1.75 +    using assms(1) by(auto simp add: card_Un_Int, auto simp add:conj_commute)
    1.76 +  have lem2:"setsum (\<lambda>j. card {f \<in> {f \<in> faces. compo' f \<and> bnd f}. face f j}) simplices = 
    1.77 +              1 * card {f \<in> faces. compo' f \<and> bnd f}"
    1.78 +       "setsum (\<lambda>j. card {f \<in> {f \<in> faces. compo' f \<and> \<not> bnd f}. face f j}) simplices = 
    1.79 +              2 * card {f \<in> faces. compo' f \<and> \<not> bnd f}"
    1.80 +    apply(rule_tac[!] setsum_multicount) using assms by auto
    1.81 +  have lem3:"setsum (\<lambda>s. card {f \<in> faces. face f s \<and> compo' f}) simplices =
    1.82 +    setsum (\<lambda>s. card {f \<in> faces. face f s \<and> compo' f}) {s \<in> simplices.   compo s}+
    1.83 +    setsum (\<lambda>s. card {f \<in> faces. face f s \<and> compo' f}) {s \<in> simplices. \<not> compo s}"
    1.84 +    apply(rule setsum_Un_disjoint') using assms(2) by auto
    1.85 +  have lem4:"setsum (\<lambda>s. card {f \<in> faces. face f s \<and> compo' f}) {s \<in> simplices. compo s}
    1.86 +    = setsum (\<lambda>s. 1) {s \<in> simplices. compo s}"
    1.87 +    apply(rule setsum_cong2) using assms(5) by auto
    1.88 +  have lem5: "setsum (\<lambda>s. card {f \<in> faces. face f s \<and> compo' f}) {s \<in> simplices. \<not> compo s} =
    1.89 +    setsum (\<lambda>s. card {f \<in> faces. face f s \<and> compo' f})
    1.90 +           {s \<in> simplices. (\<not> compo s) \<and> (card {f \<in> faces. face f s \<and> compo' f} = 0)} +
    1.91 +    setsum (\<lambda>s. card {f \<in> faces. face f s \<and> compo' f})
    1.92 +           {s \<in> simplices. (\<not> compo s) \<and> (card {f \<in> faces. face f s \<and> compo' f} = 2)}"
    1.93 +    apply(rule setsum_Un_disjoint') using assms(2,6) by auto
    1.94 +  have *:"int (\<Sum>s\<in>{s \<in> simplices. compo s}. card {f \<in> faces. face f s \<and> compo' f}) =
    1.95 +    int (card {f \<in> faces. compo' f \<and> bnd f} + 2 * card {f \<in> faces. compo' f \<and> \<not> bnd f}) - 
    1.96 +    int (card {s \<in> simplices. \<not> compo s \<and> card {f \<in> faces. face f s \<and> compo' f} = 2} * 2)"
    1.97 +    using lem1[unfolded lem3 lem2 lem5] by auto
    1.98 +  have even_minus_odd:"\<And>x y. even x \<Longrightarrow> odd (y::int) \<Longrightarrow> odd (x - y)" using assms by auto
    1.99 +  have odd_minus_even:"\<And>x y. odd x \<Longrightarrow> even (y::int) \<Longrightarrow> odd (x - y)" using assms by auto
   1.100 +  show ?thesis unfolding even_nat_def unfolding card_def and lem4[THEN sym] and *[unfolded card_def]
   1.101 +    unfolding card_def[THEN sym] apply(rule odd_minus_even) unfolding zadd_int[THEN sym] apply(rule odd_plus_even)
   1.102 +    apply(rule assms(7)[unfolded even_nat_def]) unfolding int_mult by auto qed
   1.103 +
   1.104 +subsection {* The odd/even result for faces of complete vertices, generalized. *}
   1.105 +
   1.106 +lemma card_1_exists: "card s = 1 \<longleftrightarrow> (\<exists>!x. x \<in> s)" unfolding One_nat_def
   1.107 +  apply rule apply(drule card_eq_SucD) defer apply(erule ex1E) proof-
   1.108 +  fix x assume as:"x \<in> s" "\<forall>y. y \<in> s \<longrightarrow> y = x"
   1.109 +  have *:"s = insert x {}" apply- apply(rule set_ext,rule) unfolding singleton_iff
   1.110 +    apply(rule as(2)[rule_format]) using as(1) by auto
   1.111 +  show "card s = Suc 0" unfolding * using card_insert by auto qed auto
   1.112 +
   1.113 +lemma card_2_exists: "card s = 2 \<longleftrightarrow> (\<exists>x\<in>s. \<exists>y\<in>s. x \<noteq> y \<and> (\<forall>z\<in>s. (z = x) \<or> (z = y)))" proof
   1.114 +  assume "card s = 2" then obtain x y where obt:"s = {x, y}" "x\<noteq>y" unfolding numeral_2_eq_2 apply - apply(erule exE conjE|drule card_eq_SucD)+ by auto
   1.115 +  show "\<exists>x\<in>s. \<exists>y\<in>s. x \<noteq> y \<and> (\<forall>z\<in>s. z = x \<or> z = y)" using obt by auto next
   1.116 +  assume "\<exists>x\<in>s. \<exists>y\<in>s. x \<noteq> y \<and> (\<forall>z\<in>s. z = x \<or> z = y)" then guess x .. from this(2) guess y  ..
   1.117 +  with `x\<in>s` have *:"s = {x, y}" "x\<noteq>y" by auto
   1.118 +  from this(2) show "card s = 2" unfolding * by auto qed
   1.119 +
   1.120 +lemma image_lemma_0: assumes "card {a\<in>s. f ` (s - {a}) = t - {b}} = n"
   1.121 +  shows "card {s'. \<exists>a\<in>s. (s' = s - {a}) \<and> (f ` s' = t - {b})} = n" proof-
   1.122 +  have *:"{s'. \<exists>a\<in>s. (s' = s - {a}) \<and> (f ` s' = t - {b})} = (\<lambda>a. s - {a}) ` {a\<in>s. f ` (s - {a}) = t - {b}}" by auto
   1.123 +  show ?thesis unfolding * unfolding assms[THEN sym] apply(rule card_image) unfolding inj_on_def 
   1.124 +    apply(rule,rule,rule) unfolding mem_Collect_eq by auto qed
   1.125 +
   1.126 +lemma image_lemma_1: assumes "finite s" "finite t" "card s = card t" "f ` s = t" "b \<in> t"
   1.127 +  shows "card {s'. \<exists>a\<in>s. s' = s - {a} \<and>  f ` s' = t - {b}} = 1" proof-
   1.128 +  obtain a where a:"b = f a" "a\<in>s" using assms(4-5) by auto
   1.129 +  have inj:"inj_on f s" apply(rule eq_card_imp_inj_on) using assms(1-4) by auto
   1.130 +  have *:"{a \<in> s. f ` (s - {a}) = t - {b}} = {a}" apply(rule set_ext) unfolding singleton_iff
   1.131 +    apply(rule,rule inj[unfolded inj_on_def,rule_format]) unfolding a using a(2) and assms and inj[unfolded inj_on_def] by auto
   1.132 +  show ?thesis apply(rule image_lemma_0) unfolding *  by auto qed
   1.133 +
   1.134 +lemma image_lemma_2: assumes "finite s" "finite t" "card s = card t" "f ` s \<subseteq> t" "f ` s \<noteq> t" "b \<in> t"
   1.135 +  shows "(card {s'. \<exists>a\<in>s. (s' = s - {a}) \<and> f ` s' = t - {b}} = 0) \<or>
   1.136 +         (card {s'. \<exists>a\<in>s. (s' = s - {a}) \<and> f ` s' = t - {b}} = 2)" proof(cases "{a\<in>s. f ` (s - {a}) = t - {b}} = {}")
   1.137 +  case True thus ?thesis apply-apply(rule disjI1, rule image_lemma_0) using assms(1) by(auto simp add:card_0_eq)
   1.138 +next let ?M = "{a\<in>s. f ` (s - {a}) = t - {b}}"
   1.139 +  case False then obtain a where "a\<in>?M" by auto hence a:"a\<in>s" "f ` (s - {a}) = t - {b}" by auto
   1.140 +  have "f a \<in> t - {b}" using a and assms by auto
   1.141 +  hence "\<exists>c \<in> s - {a}. f a = f c" unfolding image_iff[symmetric] and a by auto
   1.142 +  then obtain c where c:"c \<in> s" "a \<noteq> c" "f a = f c" by auto
   1.143 +  hence *:"f ` (s - {c}) = f ` (s - {a})" apply-apply(rule set_ext,rule) proof-
   1.144 +    fix x assume "x \<in> f ` (s - {a})" then obtain y where y:"f y = x" "y\<in>s- {a}" by auto
   1.145 +    thus "x \<in> f ` (s - {c})" unfolding image_iff apply(rule_tac x="if y = c then a else y" in bexI) using c a by auto qed auto
   1.146 +  have "c\<in>?M" unfolding mem_Collect_eq and * using a and c(1) by auto
   1.147 +  show ?thesis apply(rule disjI2, rule image_lemma_0) unfolding card_2_exists
   1.148 +    apply(rule bexI[OF _ `a\<in>?M`], rule bexI[OF _ `c\<in>?M`],rule,rule `a\<noteq>c`) proof(rule,unfold mem_Collect_eq,erule conjE)
   1.149 +    fix z assume as:"z \<in> s" "f ` (s - {z}) = t - {b}"
   1.150 +    have inj:"inj_on f (s - {z})" apply(rule eq_card_imp_inj_on) unfolding as using as(1) and assms by auto
   1.151 +    show "z = a \<or> z = c" proof(rule ccontr)
   1.152 +      assume "\<not> (z = a \<or> z = c)" thus False using inj[unfolded inj_on_def,THEN bspec[where x=a],THEN bspec[where x=c]]
   1.153 +	using `a\<in>s` `c\<in>s` `f a = f c` `a\<noteq>c` by auto qed qed qed
   1.154 +
   1.155 +subsection {* Combine this with the basic counting lemma. *}
   1.156 +
   1.157 +lemma kuhn_complete_lemma:
   1.158 +  assumes "finite simplices"
   1.159 +  "\<forall>f s. face f s \<longleftrightarrow> (\<exists>a\<in>s. f = s - {a})" "\<forall>s\<in>simplices. card s = n + 2" "\<forall>s\<in>simplices. (rl ` s) \<subseteq> {0..n+1}"
   1.160 +  "\<forall>f\<in> {f. \<exists>s\<in>simplices. face f s}. bnd f  \<longrightarrow> (card {s\<in>simplices. face f s} = 1)"
   1.161 +  "\<forall>f\<in> {f. \<exists>s\<in>simplices. face f s}. \<not>bnd f \<longrightarrow> (card {s\<in>simplices. face f s} = 2)"
   1.162 +  "odd(card {f\<in>{f. \<exists>s\<in>simplices. face f s}. rl ` f = {0..n} \<and> bnd f})"
   1.163 +  shows "odd (card {s\<in>simplices. (rl ` s = {0..n+1})})" 
   1.164 +  apply(rule kuhn_counting_lemma) defer apply(rule assms)+ prefer 3 apply(rule assms) proof(rule_tac[1-2] ballI impI)+ 
   1.165 +  have *:"{f. \<exists>s\<in>simplices. \<exists>a\<in>s. f = s - {a}} = (\<Union>s\<in>simplices. {f. \<exists>a\<in>s. (f = s - {a})})" by auto
   1.166 +  have **: "\<forall>s\<in>simplices. card s = n + 2 \<and> finite s" using assms(3) by (auto intro: card_ge_0_finite)
   1.167 +  show "finite {f. \<exists>s\<in>simplices. face f s}" unfolding assms(2)[rule_format] and *
   1.168 +    apply(rule finite_UN_I[OF assms(1)]) using ** by auto
   1.169 +  have *:"\<And> P f s. s\<in>simplices \<Longrightarrow> (f \<in> {f. \<exists>s\<in>simplices. \<exists>a\<in>s. f = s - {a}}) \<and>
   1.170 +    (\<exists>a\<in>s. (f = s - {a})) \<and> P f \<longleftrightarrow> (\<exists>a\<in>s. (f = s - {a}) \<and> P f)" by auto
   1.171 +  fix s assume s:"s\<in>simplices" let ?S = "{f \<in> {f. \<exists>s\<in>simplices. face f s}. face f s \<and> rl ` f = {0..n}}"
   1.172 +    have "{0..n + 1} - {n + 1} = {0..n}" by auto
   1.173 +    hence S:"?S = {s'. \<exists>a\<in>s. s' = s - {a} \<and> rl ` s' = {0..n + 1} - {n + 1}}" apply- apply(rule set_ext)
   1.174 +      unfolding assms(2)[rule_format] mem_Collect_eq and *[OF s, unfolded mem_Collect_eq, where P="\<lambda>x. rl ` x = {0..n}"] by auto
   1.175 +    show "rl ` s = {0..n+1} \<Longrightarrow> card ?S = 1" "rl ` s \<noteq> {0..n+1} \<Longrightarrow> card ?S = 0 \<or> card ?S = 2" unfolding S
   1.176 +      apply(rule_tac[!] image_lemma_1 image_lemma_2) using ** assms(4) and s by auto qed
   1.177 +
   1.178 +subsection {*We use the following notion of ordering rather than pointwise indexing. *}
   1.179 +
   1.180 +definition "kle n x y \<longleftrightarrow> (\<exists>k\<subseteq>{1..n::nat}. (\<forall>j. y(j) = x(j) + (if j \<in> k then (1::nat) else 0)))"
   1.181 +
   1.182 +lemma kle_refl[intro]: "kle n x x" unfolding kle_def by auto
   1.183 +
   1.184 +lemma kle_antisym: "kle n x y \<and> kle n y x \<longleftrightarrow> (x = y)"
   1.185 +  unfolding kle_def apply rule apply(rule ext) by auto
   1.186 +
   1.187 +lemma pointwise_minimal_pointwise_maximal: fixes s::"(nat\<Rightarrow>nat) set"
   1.188 +  assumes  "finite s" "s \<noteq> {}" "\<forall>x\<in>s. \<forall>y\<in>s. (\<forall>j. x j \<le> y j) \<or> (\<forall>j. y j \<le> x j)"
   1.189 +  shows "\<exists>a\<in>s. \<forall>x\<in>s. \<forall>j. a j \<le> x j" "\<exists>a\<in>s. \<forall>x\<in>s. \<forall>j. x j \<le> a j"
   1.190 +  using assms unfolding atomize_conj apply- proof(induct s rule:finite_induct)
   1.191 +  fix x and F::"(nat\<Rightarrow>nat) set" assume as:"finite F" "x \<notin> F" 
   1.192 +    "\<lbrakk>F \<noteq> {}; \<forall>x\<in>F. \<forall>y\<in>F. (\<forall>j. x j \<le> y j) \<or> (\<forall>j. y j \<le> x j)\<rbrakk>
   1.193 +        \<Longrightarrow> (\<exists>a\<in>F. \<forall>x\<in>F. \<forall>j. a j \<le> x j) \<and> (\<exists>a\<in>F. \<forall>x\<in>F. \<forall>j. x j \<le> a j)" "insert x F \<noteq> {}"
   1.194 +    "\<forall>xa\<in>insert x F. \<forall>y\<in>insert x F. (\<forall>j. xa j \<le> y j) \<or> (\<forall>j. y j \<le> xa j)"
   1.195 +  show "(\<exists>a\<in>insert x F. \<forall>x\<in>insert x F. \<forall>j. a j \<le> x j) \<and> (\<exists>a\<in>insert x F. \<forall>x\<in>insert x F. \<forall>j. x j \<le> a j)" proof(cases "F={}")
   1.196 +    case True thus ?thesis apply-apply(rule,rule_tac[!] x=x in bexI) by auto next
   1.197 +    case False obtain a b where a:"a\<in>insert x F" "\<forall>x\<in>F. \<forall>j. a j \<le> x j" and
   1.198 +      b:"b\<in>insert x F" "\<forall>x\<in>F. \<forall>j. x j \<le> b j" using as(3)[OF False] using as(5) by auto
   1.199 +    have "\<exists>a\<in>insert x F. \<forall>x\<in>insert x F. \<forall>j. a j \<le> x j"
   1.200 +      using as(5)[rule_format,OF a(1) insertI1] apply- proof(erule disjE)
   1.201 +      assume "\<forall>j. a j \<le> x j" thus ?thesis apply(rule_tac x=a in bexI) using a by auto next
   1.202 +      assume "\<forall>j. x j \<le> a j" thus ?thesis apply(rule_tac x=x in bexI) apply(rule,rule) using a apply -
   1.203 +	apply(erule_tac x=xa in ballE) apply(erule_tac x=j in allE)+ by auto qed moreover
   1.204 +    have "\<exists>b\<in>insert x F. \<forall>x\<in>insert x F. \<forall>j. x j \<le> b j"
   1.205 +      using as(5)[rule_format,OF b(1) insertI1] apply- proof(erule disjE)
   1.206 +      assume "\<forall>j. x j \<le> b j" thus ?thesis apply(rule_tac x=b in bexI) using b by auto next
   1.207 +      assume "\<forall>j. b j \<le> x j" thus ?thesis apply(rule_tac x=x in bexI) apply(rule,rule) using b apply -
   1.208 +	apply(erule_tac x=xa in ballE) apply(erule_tac x=j in allE)+ by auto qed
   1.209 +    ultimately show  ?thesis by auto qed qed(auto)
   1.210 +
   1.211 +lemma kle_imp_pointwise: "kle n x y \<Longrightarrow> (\<forall>j. x j \<le> y j)" unfolding kle_def by auto
   1.212 +
   1.213 +lemma pointwise_antisym: fixes x::"nat \<Rightarrow> nat"
   1.214 +  shows "(\<forall>j. x j \<le> y j) \<and> (\<forall>j. y j \<le> x j) \<longleftrightarrow> (x = y)"
   1.215 +  apply(rule, rule ext,erule conjE) apply(erule_tac x=xa in allE)+ by auto
   1.216 +
   1.217 +lemma kle_trans: assumes "kle n x y" "kle n y z" "kle n x z \<or> kle n z x" shows "kle n x z"
   1.218 +  using assms apply- apply(erule disjE) apply assumption proof- case goal1
   1.219 +  hence "x=z" apply- apply(rule ext) apply(drule kle_imp_pointwise)+ 
   1.220 +    apply(erule_tac x=xa in allE)+ by auto thus ?case by auto qed
   1.221 +
   1.222 +lemma kle_strict: assumes "kle n x y" "x \<noteq> y"
   1.223 +  shows "\<forall>j. x j \<le> y j"  "\<exists>k. 1 \<le> k \<and> k \<le> n \<and> x(k) < y(k)"
   1.224 +  apply(rule kle_imp_pointwise[OF assms(1)]) proof-
   1.225 +  guess k using assms(1)[unfolded kle_def] .. note k = this
   1.226 +  show "\<exists>k. 1 \<le> k \<and> k \<le> n \<and> x(k) < y(k)" proof(cases "k={}")
   1.227 +    case True hence "x=y" apply-apply(rule ext) using k by auto
   1.228 +    thus ?thesis using assms(2) by auto next
   1.229 +    case False hence "(SOME k'. k' \<in> k) \<in> k" apply-apply(rule someI_ex) by auto
   1.230 +    thus ?thesis apply(rule_tac x="SOME k'. k' \<in> k" in exI) using k by auto qed qed
   1.231 +
   1.232 +lemma kle_minimal: assumes "finite s" "s \<noteq> {}" "\<forall>x\<in>s. \<forall>y\<in>s. kle n x y \<or> kle n y x"
   1.233 +  shows "\<exists>a\<in>s. \<forall>x\<in>s. kle n a x" proof-
   1.234 +  have "\<exists>a\<in>s. \<forall>x\<in>s. \<forall>j. a j \<le> x j" apply(rule pointwise_minimal_pointwise_maximal(1)[OF assms(1-2)])
   1.235 +    apply(rule,rule) apply(drule_tac assms(3)[rule_format],assumption) using kle_imp_pointwise by auto
   1.236 +  then guess a .. note a=this show ?thesis apply(rule_tac x=a in bexI) proof fix x assume "x\<in>s"
   1.237 +    show "kle n a x" using assms(3)[rule_format,OF a(1) `x\<in>s`] apply- proof(erule disjE)
   1.238 +      assume "kle n x a" hence "x = a" apply- unfolding pointwise_antisym[THEN sym]
   1.239 +	apply(drule kle_imp_pointwise) using a(2)[rule_format,OF `x\<in>s`] by auto
   1.240 +      thus ?thesis using kle_refl by auto  qed qed(insert a, auto) qed
   1.241 +
   1.242 +lemma kle_maximal: assumes "finite s" "s \<noteq> {}" "\<forall>x\<in>s. \<forall>y\<in>s. kle n x y \<or> kle n y x"
   1.243 +  shows "\<exists>a\<in>s. \<forall>x\<in>s. kle n x a" proof-
   1.244 +  have "\<exists>a\<in>s. \<forall>x\<in>s. \<forall>j. a j \<ge> x j" apply(rule pointwise_minimal_pointwise_maximal(2)[OF assms(1-2)])
   1.245 +    apply(rule,rule) apply(drule_tac assms(3)[rule_format],assumption) using kle_imp_pointwise by auto
   1.246 +  then guess a .. note a=this show ?thesis apply(rule_tac x=a in bexI) proof fix x assume "x\<in>s"
   1.247 +    show "kle n x a" using assms(3)[rule_format,OF a(1) `x\<in>s`] apply- proof(erule disjE)
   1.248 +      assume "kle n a x" hence "x = a" apply- unfolding pointwise_antisym[THEN sym]
   1.249 +	apply(drule kle_imp_pointwise) using a(2)[rule_format,OF `x\<in>s`] by auto
   1.250 +      thus ?thesis using kle_refl by auto  qed qed(insert a, auto) qed
   1.251 +
   1.252 +lemma kle_strict_set: assumes "kle n x y" "x \<noteq> y"
   1.253 +  shows "1 \<le> card {k\<in>{1..n}. x k < y k}" proof-
   1.254 +  guess i using kle_strict(2)[OF assms] ..
   1.255 +  hence "card {i} \<le> card {k\<in>{1..n}. x k < y k}" apply- apply(rule card_mono) by auto
   1.256 +  thus ?thesis by auto qed
   1.257 +
   1.258 +lemma kle_range_combine:
   1.259 +  assumes "kle n x y" "kle n y z" "kle n x z \<or> kle n z x"
   1.260 +  "m1 \<le> card {k\<in>{1..n}. x k < y k}"
   1.261 +  "m2 \<le> card {k\<in>{1..n}. y k < z k}"
   1.262 +  shows "kle n x z \<and> m1 + m2 \<le> card {k\<in>{1..n}. x k < z k}"
   1.263 +  apply(rule,rule kle_trans[OF assms(1-3)]) proof-
   1.264 +  have "\<And>j. x j < y j \<Longrightarrow> x j < z j" apply(rule less_le_trans) using kle_imp_pointwise[OF assms(2)] by auto moreover
   1.265 +  have "\<And>j. y j < z j \<Longrightarrow> x j < z j" apply(rule le_less_trans) using kle_imp_pointwise[OF assms(1)] by auto ultimately
   1.266 +  have *:"{k\<in>{1..n}. x k < y k} \<union> {k\<in>{1..n}. y k < z k} = {k\<in>{1..n}. x k < z k}" by auto
   1.267 +  have **:"{k \<in> {1..n}. x k < y k} \<inter> {k \<in> {1..n}. y k < z k} = {}" unfolding disjoint_iff_not_equal
   1.268 +    apply(rule,rule,unfold mem_Collect_eq,rule ccontr) apply(erule conjE)+ proof-
   1.269 +    fix i j assume as:"i \<in> {1..n}" "x i < y i" "j \<in> {1..n}" "y j < z j" "\<not> i \<noteq> j"
   1.270 +    guess kx using assms(1)[unfolded kle_def] .. note kx=this
   1.271 +    have "x i < y i" using as by auto hence "i \<in> kx" using as(1) kx apply(rule_tac ccontr) by auto 
   1.272 +    hence "x i + 1 = y i" using kx by auto moreover
   1.273 +    guess ky using assms(2)[unfolded kle_def] .. note ky=this
   1.274 +    have "y i < z i" using as by auto hence "i \<in> ky" using as(1) ky apply(rule_tac ccontr) by auto 
   1.275 +    hence "y i + 1 = z i" using ky by auto ultimately
   1.276 +    have "z i = x i + 2" by auto
   1.277 +    thus False using assms(3) unfolding kle_def by(auto simp add: split_if_eq1) qed
   1.278 +  have fin:"\<And>P. finite {x\<in>{1..n::nat}. P x}" by auto
   1.279 +  have "m1 + m2 \<le> card {k\<in>{1..n}. x k < y k} + card {k\<in>{1..n}. y k < z k}" using assms(4-5) by auto
   1.280 +  also have "\<dots> \<le>  card {k\<in>{1..n}. x k < z k}" unfolding card_Un_Int[OF fin fin] unfolding * ** by auto
   1.281 +  finally show " m1 + m2 \<le> card {k \<in> {1..n}. x k < z k}" by auto qed
   1.282 +
   1.283 +lemma kle_range_combine_l:
   1.284 +  assumes "kle n x y" "kle n y z" "kle n x z \<or> kle n z x" "m \<le> card {k\<in>{1..n}. y(k) < z(k)}"
   1.285 +  shows "kle n x z \<and> m \<le> card {k\<in>{1..n}. x(k) < z(k)}"
   1.286 +  using kle_range_combine[OF assms(1-3) _ assms(4), of 0] by auto
   1.287 +
   1.288 +lemma kle_range_combine_r:
   1.289 +  assumes "kle n x y" "kle n y z" "kle n x z \<or> kle n z x" "m \<le> card {k\<in>{1..n}. x k < y k}"
   1.290 +  shows "kle n x z \<and> m \<le> card {k\<in>{1..n}. x(k) < z(k)}"
   1.291 +  using kle_range_combine[OF assms(1-3) assms(4), of 0] by auto
   1.292 +
   1.293 +lemma kle_range_induct:
   1.294 +  assumes "card s = Suc m" "\<forall>x\<in>s. \<forall>y\<in>s. kle n x y \<or> kle n y x"
   1.295 +  shows "\<exists>x\<in>s. \<exists>y\<in>s. kle n x y \<and> m \<le> card {k\<in>{1..n}. x k < y k}" proof-
   1.296 +have "finite s" "s\<noteq>{}" using assms(1) by (auto intro: card_ge_0_finite)
   1.297 +thus ?thesis using assms apply- proof(induct m arbitrary: s)
   1.298 +  case 0 thus ?case using kle_refl by auto next
   1.299 +  case (Suc m) then obtain a where a:"a\<in>s" "\<forall>x\<in>s. kle n a x" using kle_minimal[of s n] by auto
   1.300 +  show ?case proof(cases "s \<subseteq> {a}") case False
   1.301 +    hence "card (s - {a}) = Suc m" "s - {a} \<noteq> {}" using card_Diff_singleton[OF _ a(1)] Suc(4) `finite s` by auto
   1.302 +    then obtain x b where xb:"x\<in>s - {a}" "b\<in>s - {a}" "kle n x b" "m \<le> card {k \<in> {1..n}. x k < b k}" 
   1.303 +      using Suc(1)[of "s - {a}"] using Suc(5) `finite s` by auto
   1.304 +    have "1 \<le> card {k \<in> {1..n}. a k < x k}" "m \<le> card {k \<in> {1..n}. x k < b k}"
   1.305 +      apply(rule kle_strict_set) apply(rule a(2)[rule_format]) using a and xb by auto
   1.306 +    thus ?thesis apply(rule_tac x=a in bexI, rule_tac x=b in bexI) 
   1.307 +      using kle_range_combine[OF a(2)[rule_format] xb(3) Suc(5)[rule_format], of 1 "m"] using a(1) xb(1-2) by auto next
   1.308 +    case True hence "s = {a}" using Suc(3) by auto hence "card s = 1" by auto
   1.309 +    hence False using Suc(4) `finite s` by auto thus ?thesis by auto qed qed qed
   1.310 +
   1.311 +lemma kle_Suc: "kle n x y \<Longrightarrow> kle (n + 1) x y"
   1.312 +  unfolding kle_def apply(erule exE) apply(rule_tac x=k in exI) by auto
   1.313 +
   1.314 +lemma kle_trans_1: assumes "kle n x y" shows "x j \<le> y j" "y j \<le> x j + 1"
   1.315 +  using assms[unfolded kle_def] by auto 
   1.316 +
   1.317 +lemma kle_trans_2: assumes "kle n a b" "kle n b c" "\<forall>j. c j \<le> a j + 1" shows "kle n a c" proof-
   1.318 +  guess kk1 using assms(1)[unfolded kle_def] .. note kk1=this
   1.319 +  guess kk2 using assms(2)[unfolded kle_def] .. note kk2=this
   1.320 +  show ?thesis unfolding kle_def apply(rule_tac x="kk1 \<union> kk2" in exI) apply(rule) defer proof
   1.321 +    fix i show "c i = a i + (if i \<in> kk1 \<union> kk2 then 1 else 0)" proof(cases "i\<in>kk1 \<union> kk2")
   1.322 +      case True hence "c i \<ge> a i + (if i \<in> kk1 \<union> kk2 then 1 else 0)"
   1.323 +	unfolding kk1[THEN conjunct2,rule_format,of i] kk2[THEN conjunct2,rule_format,of i] by auto
   1.324 +      moreover have "c i \<le> a i + (if i \<in> kk1 \<union> kk2 then 1 else 0)" using True assms(3) by auto  
   1.325 +      ultimately show ?thesis by auto next
   1.326 +      case False thus ?thesis using kk1 kk2 by auto qed qed(insert kk1 kk2, auto) qed
   1.327 +
   1.328 +lemma kle_between_r: assumes "kle n a b" "kle n b c" "kle n a x" "kle n c x" shows "kle n b x"
   1.329 +  apply(rule kle_trans_2[OF assms(2,4)]) proof have *:"\<And>c b x::nat. x \<le> c + 1 \<Longrightarrow> c \<le> b \<Longrightarrow> x \<le> b + 1" by auto
   1.330 +  fix j show "x j \<le> b j + 1" apply(rule *)using kle_trans_1[OF assms(1),of j] kle_trans_1[OF assms(3), of j] by auto qed
   1.331 +
   1.332 +lemma kle_between_l: assumes "kle n a b" "kle n b c" "kle n x a" "kle n x c" shows "kle n x b"
   1.333 +  apply(rule kle_trans_2[OF assms(3,1)]) proof have *:"\<And>c b x::nat. c \<le> x + 1 \<Longrightarrow> b \<le> c \<Longrightarrow> b \<le> x + 1" by auto
   1.334 +  fix j show "b j \<le> x j + 1" apply(rule *) using kle_trans_1[OF assms(2),of j] kle_trans_1[OF assms(4), of j] by auto qed
   1.335 +
   1.336 +lemma kle_adjacent:
   1.337 +  assumes "\<forall>j. b j = (if j = k then a(j) + 1 else a j)" "kle n a x" "kle n x b"
   1.338 +  shows "(x = a) \<or> (x = b)" proof(cases "x k = a k")
   1.339 +  case True show ?thesis apply(rule disjI1,rule ext) proof- fix j
   1.340 +    have "x j \<le> a j" using kle_imp_pointwise[OF assms(3),THEN spec[where x=j]] 
   1.341 +      unfolding assms(1)[rule_format] apply-apply(cases "j=k") using True by auto
   1.342 +    thus "x j = a j" using kle_imp_pointwise[OF assms(2),THEN spec[where x=j]] by auto qed next
   1.343 +  case False show ?thesis apply(rule disjI2,rule ext) proof- fix j
   1.344 +    have "x j \<ge> b j" using kle_imp_pointwise[OF assms(2),THEN spec[where x=j]]
   1.345 +      unfolding assms(1)[rule_format] apply-apply(cases "j=k") using False by auto
   1.346 +    thus "x j = b j" using kle_imp_pointwise[OF assms(3),THEN spec[where x=j]] by auto qed qed
   1.347 +
   1.348 +subsection {* kuhn's notion of a simplex (a reformulation to avoid so much indexing). *}
   1.349 +
   1.350 +definition "ksimplex p n (s::(nat \<Rightarrow> nat) set) \<longleftrightarrow>
   1.351 +        (card s = n + 1 \<and>
   1.352 +        (\<forall>x\<in>s. \<forall>j. x(j) \<le> p) \<and>
   1.353 +        (\<forall>x\<in>s. \<forall>j. j\<notin>{1..n} \<longrightarrow> (x j = p)) \<and>
   1.354 +        (\<forall>x\<in>s. \<forall>y\<in>s. kle n x y \<or> kle n y x))"
   1.355 +
   1.356 +lemma ksimplexI:"card s = n + 1 \<Longrightarrow>  \<forall>x\<in>s. \<forall>j. x j \<le> p \<Longrightarrow> \<forall>x\<in>s. \<forall>j. j \<notin> {1..?n} \<longrightarrow> x j = ?p \<Longrightarrow> \<forall>x\<in>s. \<forall>y\<in>s. kle n x y \<or> kle n y x \<Longrightarrow> ksimplex p n s"
   1.357 +  unfolding ksimplex_def by auto
   1.358 +
   1.359 +lemma ksimplex_eq: "ksimplex p n (s::(nat \<Rightarrow> nat) set) \<longleftrightarrow>
   1.360 +        (card s = n + 1 \<and> finite s \<and>
   1.361 +        (\<forall>x\<in>s. \<forall>j. x(j) \<le> p) \<and>
   1.362 +        (\<forall>x\<in>s. \<forall>j. j\<notin>{1..n} \<longrightarrow> (x j = p)) \<and>
   1.363 +        (\<forall>x\<in>s. \<forall>y\<in>s. kle n x y \<or> kle n y x))"
   1.364 +  unfolding ksimplex_def by (auto intro: card_ge_0_finite)
   1.365 +
   1.366 +lemma ksimplex_extrema: assumes "ksimplex p n s" obtains a b where "a \<in> s" "b \<in> s"
   1.367 +  "\<forall>x\<in>s. kle n a x \<and> kle n x b" "\<forall>i. b(i) = (if i \<in> {1..n} then a(i) + 1 else a(i))" proof(cases "n=0")
   1.368 +  case True obtain x where *:"s = {x}" using assms[unfolded ksimplex_eq True,THEN conjunct1]
   1.369 +    unfolding add_0_left card_1_exists by auto
   1.370 +  show ?thesis apply(rule that[of x x]) unfolding * True by auto next
   1.371 +  note assm = assms[unfolded ksimplex_eq]
   1.372 +  case False have "s\<noteq>{}" using assm by auto
   1.373 +  obtain a where a:"a\<in>s" "\<forall>x\<in>s. kle n a x" using `s\<noteq>{}` assm using kle_minimal[of s n] by auto
   1.374 +  obtain b where b:"b\<in>s" "\<forall>x\<in>s. kle n x b" using `s\<noteq>{}` assm using kle_maximal[of s n] by auto
   1.375 +  obtain c d where c_d:"c\<in>s" "d\<in>s" "kle n c d" "n \<le> card {k \<in> {1..n}. c k < d k}"
   1.376 +    using kle_range_induct[of s n n] using assm by auto
   1.377 +  have "kle n c b \<and> n \<le> card {k \<in> {1..n}. c k < b k}" apply(rule kle_range_combine_r[where y=d]) using c_d a b by auto
   1.378 +  hence "kle n a b \<and> n \<le> card {k\<in>{1..n}. a(k) < b(k)}" apply-apply(rule kle_range_combine_l[where y=c]) using a `c\<in>s` `b\<in>s` by auto
   1.379 +  moreover have "card {1..n} \<ge> card {k\<in>{1..n}. a(k) < b(k)}" apply(rule card_mono) by auto
   1.380 +  ultimately have *:"{k\<in>{1 .. n}. a k < b k} = {1..n}" apply- apply(rule card_subset_eq) by auto
   1.381 +  show ?thesis apply(rule that[OF a(1) b(1)]) defer apply(subst *[THEN sym]) unfolding mem_Collect_eq proof
   1.382 +    guess k using a(2)[rule_format,OF b(1),unfolded kle_def] .. note k=this
   1.383 +    fix i show "b i = (if i \<in> {1..n} \<and> a i < b i then a i + 1 else a i)" proof(cases "i \<in> {1..n}")
   1.384 +      case True thus ?thesis unfolding k[THEN conjunct2,rule_format] by auto next
   1.385 +      case False have "a i = p" using assm and False `a\<in>s` by auto
   1.386 +      moreover   have "b i = p" using assm and False `b\<in>s` by auto
   1.387 +      ultimately show ?thesis by auto qed qed(insert a(2) b(2) assm,auto) qed
   1.388 +
   1.389 +lemma ksimplex_extrema_strong:
   1.390 +  assumes "ksimplex p n s" "n \<noteq> 0" obtains a b where "a \<in> s" "b \<in> s" "a \<noteq> b"
   1.391 +  "\<forall>x\<in>s. kle n a x \<and> kle n x b" "\<forall>i. b(i) = (if i \<in> {1..n} then a(i) + 1 else a(i))" proof-
   1.392 +  obtain a b where ab:"a \<in> s" "b \<in> s" "\<forall>x\<in>s. kle n a x \<and> kle n x b" "\<forall>i. b(i) = (if i \<in> {1..n} then a(i) + 1 else a(i))" 
   1.393 +    apply(rule ksimplex_extrema[OF assms(1)]) by auto 
   1.394 +  have "a \<noteq> b" apply(rule ccontr) unfolding not_not apply(drule cong[of _ _ 1 1]) using ab(4) assms(2) by auto
   1.395 +  thus ?thesis apply(rule_tac that[of a b]) using ab by auto qed
   1.396 +
   1.397 +lemma ksimplexD:
   1.398 +  assumes "ksimplex p n s"
   1.399 +  shows "card s = n + 1" "finite s" "card s = n + 1" "\<forall>x\<in>s. \<forall>j. x j \<le> p" "\<forall>x\<in>s. \<forall>j. j \<notin> {1..?n} \<longrightarrow> x j = p"
   1.400 +  "\<forall>x\<in>s. \<forall>y\<in>s. kle n x y \<or> kle n y x" using assms unfolding ksimplex_eq by auto
   1.401 +
   1.402 +lemma ksimplex_successor:
   1.403 +  assumes "ksimplex p n s" "a \<in> s"
   1.404 +  shows "(\<forall>x\<in>s. kle n x a) \<or> (\<exists>y\<in>s. \<exists>k\<in>{1..n}. \<forall>j. y(j) = (if j = k then a(j) + 1 else a(j)))"
   1.405 +proof(cases "\<forall>x\<in>s. kle n x a") case True thus ?thesis by auto next note assm = ksimplexD[OF assms(1)]
   1.406 +  case False then obtain b where b:"b\<in>s" "\<not> kle n b a" "\<forall>x\<in>{x \<in> s. \<not> kle n x a}. kle n b x"
   1.407 +    using kle_minimal[of "{x\<in>s. \<not> kle n x a}" n] and assm by auto
   1.408 +  hence  **:"1 \<le> card {k\<in>{1..n}. a k < b k}" apply- apply(rule kle_strict_set) using assm(6) and `a\<in>s` by(auto simp add:kle_refl)
   1.409 +
   1.410 +  let ?kle1 = "{x \<in> s. \<not> kle n x a}" have "card ?kle1 > 0" apply(rule ccontr) using assm(2) and False by auto
   1.411 +  hence sizekle1: "card ?kle1 = Suc (card ?kle1 - 1)" using assm(2) by auto
   1.412 +  obtain c d where c_d: "c \<in> s" "\<not> kle n c a" "d \<in> s" "\<not> kle n d a" "kle n c d" "card ?kle1 - 1 \<le> card {k \<in> {1..n}. c k < d k}"
   1.413 +    using kle_range_induct[OF sizekle1, of n] using assm by auto
   1.414 +
   1.415 +  let ?kle2 = "{x \<in> s. kle n x a}"
   1.416 +  have "card ?kle2 > 0" apply(rule ccontr) using assm(6)[rule_format,of a a] and `a\<in>s` and assm(2) by auto
   1.417 +  hence sizekle2:"card ?kle2 = Suc (card ?kle2 - 1)" using assm(2) by auto
   1.418 +  obtain e f where e_f: "e \<in> s" "kle n e a" "f \<in> s" "kle n f a" "kle n e f" "card ?kle2 - 1 \<le> card {k \<in> {1..n}. e k < f k}"
   1.419 +    using kle_range_induct[OF sizekle2, of n] using assm by auto
   1.420 +
   1.421 +  have "card {k\<in>{1..n}. a k < b k} = 1" proof(rule ccontr) case goal1
   1.422 +    hence as:"card {k\<in>{1..n}. a k < b k} \<ge> 2" using ** by auto
   1.423 +    have *:"finite ?kle2" "finite ?kle1" "?kle2 \<union> ?kle1 = s" "?kle2 \<inter> ?kle1 = {}" using assm(2) by auto
   1.424 +    have "(card ?kle2 - 1) + 2 + (card ?kle1 - 1) = card ?kle2 + card ?kle1" using sizekle1 sizekle2 by auto
   1.425 +    also have "\<dots> = n + 1" unfolding card_Un_Int[OF *(1-2)] *(3-) using assm(3) by auto
   1.426 +    finally have n:"(card ?kle2 - 1) + (2 + (card ?kle1 - 1)) = n + 1" by auto
   1.427 +    have "kle n e a \<and> card {x \<in> s. kle n x a} - 1 \<le> card {k \<in> {1..n}. e k < a k}"
   1.428 +      apply(rule kle_range_combine_r[where y=f]) using e_f using `a\<in>s` assm(6) by auto
   1.429 +    moreover have "kle n b d \<and> card {x \<in> s. \<not> kle n x a} - 1 \<le> card {k \<in> {1..n}. b k < d k}"
   1.430 +      apply(rule kle_range_combine_l[where y=c]) using c_d using assm(6) and b by auto
   1.431 +    hence "kle n a d \<and> 2 + (card {x \<in> s. \<not> kle n x a} - 1) \<le> card {k \<in> {1..n}. a k < d k}" apply-
   1.432 +      apply(rule kle_range_combine[where y=b]) using as and b assm(6) `a\<in>s` `d\<in>s` apply- by blast+
   1.433 +    ultimately have "kle n e d \<and> (card ?kle2 - 1) + (2 + (card ?kle1 - 1)) \<le> card {k\<in>{1..n}. e k < d k}" apply-
   1.434 +      apply(rule kle_range_combine[where y=a]) using assm(6)[rule_format,OF `e\<in>s` `d\<in>s`] apply - by blast+ 
   1.435 +    moreover have "card {k \<in> {1..n}. e k < d k} \<le> card {1..n}" apply(rule card_mono) by auto
   1.436 +    ultimately show False unfolding n by auto qed
   1.437 +  then guess k unfolding card_1_exists .. note k=this[unfolded mem_Collect_eq]
   1.438 +
   1.439 +  show ?thesis apply(rule disjI2) apply(rule_tac x=b in bexI,rule_tac x=k in bexI) proof
   1.440 +    fix j::nat have "kle n a b" using b and assm(6)[rule_format, OF `a\<in>s` `b\<in>s`] by auto
   1.441 +    then guess kk unfolding kle_def .. note kk_raw=this note kk=this[THEN conjunct2,rule_format]
   1.442 +    have kkk:"k\<in>kk" apply(rule ccontr) using k(1) unfolding kk by auto 
   1.443 +    show "b j = (if j = k then a j + 1 else a j)" proof(cases "j\<in>kk")
   1.444 +      case True hence "j=k" apply-apply(rule k(2)[rule_format]) using kk_raw kkk by auto
   1.445 +      thus ?thesis unfolding kk using kkk by auto next
   1.446 +      case False hence "j\<noteq>k" using k(2)[rule_format, of j k] using kk_raw kkk by auto
   1.447 +      thus ?thesis unfolding kk using kkk using False by auto qed qed(insert k(1) `b\<in>s`, auto) qed
   1.448 +
   1.449 +lemma ksimplex_predecessor:
   1.450 +  assumes "ksimplex p n s" "a \<in> s"
   1.451 +  shows "(\<forall>x\<in>s. kle n a x) \<or> (\<exists>y\<in>s. \<exists>k\<in>{1..n}. \<forall>j. a(j) = (if j = k then y(j) + 1 else y(j)))"
   1.452 +proof(cases "\<forall>x\<in>s. kle n a x") case True thus ?thesis by auto next note assm = ksimplexD[OF assms(1)]
   1.453 +  case False then obtain b where b:"b\<in>s" "\<not> kle n a b" "\<forall>x\<in>{x \<in> s. \<not> kle n a x}. kle n x b" 
   1.454 +    using kle_maximal[of "{x\<in>s. \<not> kle n a x}" n] and assm by auto
   1.455 +  hence  **:"1 \<le> card {k\<in>{1..n}. a k > b k}" apply- apply(rule kle_strict_set) using assm(6) and `a\<in>s` by(auto simp add:kle_refl)
   1.456 +
   1.457 +  let ?kle1 = "{x \<in> s. \<not> kle n a x}" have "card ?kle1 > 0" apply(rule ccontr) using assm(2) and False by auto
   1.458 +  hence sizekle1:"card ?kle1 = Suc (card ?kle1 - 1)" using assm(2) by auto
   1.459 +  obtain c d where c_d: "c \<in> s" "\<not> kle n a c" "d \<in> s" "\<not> kle n a d" "kle n d c" "card ?kle1 - 1 \<le> card {k \<in> {1..n}. c k > d k}"
   1.460 +    using kle_range_induct[OF sizekle1, of n] using assm by auto
   1.461 +
   1.462 +  let ?kle2 = "{x \<in> s. kle n a x}"
   1.463 +  have "card ?kle2 > 0" apply(rule ccontr) using assm(6)[rule_format,of a a] and `a\<in>s` and assm(2) by auto
   1.464 +  hence sizekle2:"card ?kle2 = Suc (card ?kle2 - 1)" using assm(2) by auto
   1.465 +  obtain e f where e_f: "e \<in> s" "kle n a e" "f \<in> s" "kle n a f" "kle n f e" "card ?kle2 - 1 \<le> card {k \<in> {1..n}. e k > f k}"
   1.466 +    using kle_range_induct[OF sizekle2, of n] using assm by auto
   1.467 +
   1.468 +  have "card {k\<in>{1..n}. a k > b k} = 1" proof(rule ccontr) case goal1
   1.469 +    hence as:"card {k\<in>{1..n}. a k > b k} \<ge> 2" using ** by auto
   1.470 +    have *:"finite ?kle2" "finite ?kle1" "?kle2 \<union> ?kle1 = s" "?kle2 \<inter> ?kle1 = {}" using assm(2) by auto
   1.471 +    have "(card ?kle2 - 1) + 2 + (card ?kle1 - 1) = card ?kle2 + card ?kle1" using sizekle1 sizekle2 by auto
   1.472 +    also have "\<dots> = n + 1" unfolding card_Un_Int[OF *(1-2)] *(3-) using assm(3) by auto
   1.473 +    finally have n:"(card ?kle1 - 1) + 2 + (card ?kle2 - 1) = n + 1" by auto
   1.474 +    have "kle n a e \<and> card {x \<in> s. kle n a x} - 1 \<le> card {k \<in> {1..n}. e k > a k}"
   1.475 +      apply(rule kle_range_combine_l[where y=f]) using e_f using `a\<in>s` assm(6) by auto
   1.476 +    moreover have "kle n d b \<and> card {x \<in> s. \<not> kle n a x} - 1 \<le> card {k \<in> {1..n}. b k > d k}"
   1.477 +      apply(rule kle_range_combine_r[where y=c]) using c_d using assm(6) and b by auto
   1.478 +    hence "kle n d a \<and> (card {x \<in> s. \<not> kle n a x} - 1) + 2 \<le> card {k \<in> {1..n}. a k > d k}" apply-
   1.479 +      apply(rule kle_range_combine[where y=b]) using as and b assm(6) `a\<in>s` `d\<in>s` by blast+
   1.480 +    ultimately have "kle n d e \<and> (card ?kle1 - 1 + 2) + (card ?kle2 - 1) \<le> card {k\<in>{1..n}. e k > d k}" apply-
   1.481 +      apply(rule kle_range_combine[where y=a]) using assm(6)[rule_format,OF `e\<in>s` `d\<in>s`] apply - by blast+
   1.482 +    moreover have "card {k \<in> {1..n}. e k > d k} \<le> card {1..n}" apply(rule card_mono) by auto
   1.483 +    ultimately show False unfolding n by auto qed
   1.484 +  then guess k unfolding card_1_exists .. note k=this[unfolded mem_Collect_eq]
   1.485 +
   1.486 +  show ?thesis apply(rule disjI2) apply(rule_tac x=b in bexI,rule_tac x=k in bexI) proof
   1.487 +    fix j::nat have "kle n b a" using b and assm(6)[rule_format, OF `a\<in>s` `b\<in>s`] by auto
   1.488 +    then guess kk unfolding kle_def .. note kk_raw=this note kk=this[THEN conjunct2,rule_format]
   1.489 +    have kkk:"k\<in>kk" apply(rule ccontr) using k(1) unfolding kk by auto 
   1.490 +    show "a j = (if j = k then b j + 1 else b j)" proof(cases "j\<in>kk")
   1.491 +      case True hence "j=k" apply-apply(rule k(2)[rule_format]) using kk_raw kkk by auto
   1.492 +      thus ?thesis unfolding kk using kkk by auto next
   1.493 +      case False hence "j\<noteq>k" using k(2)[rule_format, of j k] using kk_raw kkk by auto
   1.494 +      thus ?thesis unfolding kk using kkk using False by auto qed qed(insert k(1) `b\<in>s`, auto) qed
   1.495 +
   1.496 +subsection {* The lemmas about simplices that we need. *}
   1.497 +
   1.498 +lemma card_funspace': assumes "finite s" "finite t" "card s = m" "card t = n"
   1.499 +  shows "card {f. (\<forall>x\<in>s. f x \<in> t) \<and> (\<forall>x\<in>UNIV - s. f x = d)} = n ^ m" (is "card (?M s) = _")
   1.500 +  using assms apply - proof(induct m arbitrary: s)
   1.501 +  have *:"{f. \<forall>x. f x = d} = {\<lambda>x. d}" apply(rule set_ext,rule)unfolding mem_Collect_eq apply(rule,rule ext) by auto
   1.502 +  case 0 thus ?case by(auto simp add: *) next
   1.503 +  case (Suc m) guess a using card_eq_SucD[OF Suc(4)] .. then guess s0
   1.504 +    apply(erule_tac exE) apply(erule conjE)+ . note as0 = this
   1.505 +  have **:"card s0 = m" using as0 using Suc(2) Suc(4) by auto
   1.506 +  let ?l = "(\<lambda>(b,g) x. if x = a then b else g x)" have *:"?M (insert a s0) = ?l ` {(b,g). b\<in>t \<and> g\<in>?M s0}"
   1.507 +    apply(rule set_ext,rule) unfolding mem_Collect_eq image_iff apply(erule conjE)
   1.508 +    apply(rule_tac x="(x a, \<lambda>y. if y\<in>s0 then x y else d)" in bexI) apply(rule ext) prefer 3 apply rule defer
   1.509 +    apply(erule bexE,rule) unfolding mem_Collect_eq apply(erule splitE)+ apply(erule conjE)+ proof-
   1.510 +    fix x xa xb xc y assume as:"x = (\<lambda>(b, g) x. if x = a then b else g x) xa" "xb \<in> UNIV - insert a s0" "xa = (xc, y)" "xc \<in> t"
   1.511 +      "\<forall>x\<in>s0. y x \<in> t" "\<forall>x\<in>UNIV - s0. y x = d" thus "x xb = d" unfolding as by auto qed auto
   1.512 +  have inj:"inj_on ?l {(b,g). b\<in>t \<and> g\<in>?M s0}" unfolding inj_on_def apply(rule,rule,rule) unfolding mem_Collect_eq apply(erule splitE conjE)+ proof-
   1.513 +    case goal1 note as = this(1,4-)[unfolded goal1 split_conv]
   1.514 +    have "xa = xb" using as(1)[THEN cong[of _ _ a]] by auto
   1.515 +    moreover have "ya = yb" proof(rule ext) fix x show "ya x = yb x" proof(cases "x = a") 
   1.516 +	case False thus ?thesis using as(1)[THEN cong[of _ _ x x]] by auto next
   1.517 +	case True thus ?thesis using as(5,7) using as0(2) by auto qed qed 
   1.518 +    ultimately show ?case unfolding goal1 by auto qed
   1.519 +  have "finite s0" using `finite s` unfolding as0 by simp
   1.520 +  show ?case unfolding as0 * card_image[OF inj] using assms
   1.521 +    unfolding SetCompr_Sigma_eq apply-
   1.522 +    unfolding card_cartesian_product
   1.523 +    using Suc(1)[OF `finite s0` `finite t` ** `card t = n`] by auto
   1.524 +qed
   1.525 +
   1.526 +lemma card_funspace: assumes  "finite s" "finite t"
   1.527 +  shows "card {f. (\<forall>x\<in>s. f x \<in> t) \<and> (\<forall>x\<in>UNIV - s. f x = d)} = (card t) ^ (card s)"
   1.528 +  using assms by (auto intro: card_funspace')
   1.529 +
   1.530 +lemma finite_funspace: assumes "finite s" "finite t"
   1.531 +  shows "finite {f. (\<forall>x\<in>s. f x \<in> t) \<and> (\<forall>x\<in>UNIV - s. f x = d)}" (is "finite ?S")
   1.532 +proof (cases "card t > 0")
   1.533 +  case True
   1.534 +  have "card ?S = (card t) ^ (card s)"
   1.535 +    using assms by (auto intro!: card_funspace)
   1.536 +  thus ?thesis using True by (auto intro: card_ge_0_finite)
   1.537 +next
   1.538 +  case False hence "t = {}" using `finite t` by auto
   1.539 +  show ?thesis
   1.540 +  proof (cases "s = {}")
   1.541 +    have *:"{f. \<forall>x. f x = d} = {\<lambda>x. d}" by (auto intro: ext)
   1.542 +    case True thus ?thesis using `t = {}` by (auto simp: *)
   1.543 +  next
   1.544 +    case False thus ?thesis using `t = {}` by simp
   1.545 +  qed
   1.546 +qed
   1.547 +
   1.548 +lemma finite_simplices: "finite {s. ksimplex p n s}"
   1.549 +  apply(rule finite_subset[of _ "{s. s\<subseteq>{f. (\<forall>i\<in>{1..n}. f i \<in> {0..p}) \<and> (\<forall>i\<in>UNIV-{1..n}. f i = p)}}"])
   1.550 +  unfolding ksimplex_def defer apply(rule finite_Collect_subsets) apply(rule finite_funspace) by auto
   1.551 +
   1.552 +lemma simplex_top_face: assumes "0<p" "\<forall>x\<in>f. x (n + 1) = p"
   1.553 +  shows "(\<exists>s a. ksimplex p (n + 1) s \<and> a \<in> s \<and> (f = s - {a})) \<longleftrightarrow> ksimplex p n f" (is "?ls = ?rs") proof
   1.554 +  assume ?ls then guess s .. then guess a apply-apply(erule exE,(erule conjE)+) . note sa=this
   1.555 +  show ?rs unfolding ksimplex_def sa(3) apply(rule) defer apply rule defer apply(rule,rule,rule,rule) defer apply(rule,rule) proof-
   1.556 +    fix x y assume as:"x \<in>s - {a}" "y \<in>s - {a}" have xyp:"x (n + 1) = y (n + 1)"
   1.557 +	using as(1)[unfolded sa(3)[THEN sym], THEN assms(2)[rule_format]]
   1.558 +	using as(2)[unfolded sa(3)[THEN sym], THEN assms(2)[rule_format]] by auto
   1.559 +    show "kle n x y \<or> kle n y x" proof(cases "kle (n + 1) x y")
   1.560 +      case True then guess k unfolding kle_def .. note k=this hence *:"n+1 \<notin> k" using xyp by auto
   1.561 +      have "\<not> (\<exists>x\<in>k. x\<notin>{1..n})" apply(rule ccontr) unfolding not_not apply(erule bexE) proof-
   1.562 +	fix x assume as:"x \<in> k" "x \<notin> {1..n}" have "x \<noteq> n+1" using as and * by auto
   1.563 +	thus False using as and k[THEN conjunct1,unfolded subset_eq] by auto qed
   1.564 +      thus ?thesis apply-apply(rule disjI1) unfolding kle_def using k apply(rule_tac x=k in exI) by auto next
   1.565 +      case False hence "kle (n + 1) y x" using ksimplexD(6)[OF sa(1),rule_format, of x y] using as by auto
   1.566 +      then guess k unfolding kle_def .. note k=this hence *:"n+1 \<notin> k" using xyp by auto
   1.567 +      hence "\<not> (\<exists>x\<in>k. x\<notin>{1..n})" apply-apply(rule ccontr) unfolding not_not apply(erule bexE) proof-
   1.568 +	fix x assume as:"x \<in> k" "x \<notin> {1..n}" have "x \<noteq> n+1" using as and * by auto
   1.569 +	thus False using as and k[THEN conjunct1,unfolded subset_eq] by auto qed
   1.570 +      thus ?thesis apply-apply(rule disjI2) unfolding kle_def using k apply(rule_tac x=k in exI) by auto qed next
   1.571 +    fix x j assume as:"x\<in>s - {a}" "j\<notin>{1..n}"
   1.572 +    thus "x j = p" using as(1)[unfolded sa(3)[THEN sym], THEN assms(2)[rule_format]]
   1.573 +      apply(cases "j = n+1") using sa(1)[unfolded ksimplex_def] by auto qed(insert sa ksimplexD[OF sa(1)], auto) next
   1.574 +  assume ?rs note rs=ksimplexD[OF this] guess a b apply(rule ksimplex_extrema[OF `?rs`]) . note ab = this
   1.575 +  def c \<equiv> "\<lambda>i. if i = (n + 1) then p - 1 else a i"
   1.576 +  have "c\<notin>f" apply(rule ccontr) unfolding not_not apply(drule assms(2)[rule_format]) unfolding c_def using assms(1) by auto
   1.577 +  thus ?ls apply(rule_tac x="insert c f" in exI,rule_tac x=c in exI) unfolding ksimplex_def conj_assoc
   1.578 +    apply(rule conjI) defer apply(rule conjI) defer apply(rule conjI) defer apply(rule conjI) defer  
   1.579 +  proof(rule_tac[3-5] ballI allI)+
   1.580 +    fix x j assume x:"x \<in> insert c f" thus "x j \<le> p" proof (cases "x=c")
   1.581 +      case True show ?thesis unfolding True c_def apply(cases "j=n+1") using ab(1) and rs(4) by auto 
   1.582 +    qed(insert x rs(4), auto simp add:c_def)
   1.583 +    show "j \<notin> {1..n + 1} \<longrightarrow> x j = p" apply(cases "x=c") using x ab(1) rs(5) unfolding c_def by auto
   1.584 +    { fix z assume z:"z \<in> insert c f" hence "kle (n + 1) c z" apply(cases "z = c") (*defer apply(rule kle_Suc)*) proof-
   1.585 +	case False hence "z\<in>f" using z by auto
   1.586 +	then guess k apply(drule_tac ab(3)[THEN bspec[where x=z], THEN conjunct1]) unfolding kle_def apply(erule exE) .
   1.587 +	thus "kle (n + 1) c z" unfolding kle_def apply(rule_tac x="insert (n + 1) k" in exI) unfolding c_def
   1.588 +	  using ab using rs(5)[rule_format,OF ab(1),of "n + 1"] assms(1) by auto qed auto } note * = this
   1.589 +    fix y assume y:"y \<in> insert c f" show "kle (n + 1) x y \<or> kle (n + 1) y x" proof(cases "x = c \<or> y = c")
   1.590 +      case False hence **:"x\<in>f" "y\<in>f" using x y by auto
   1.591 +      show ?thesis using rs(6)[rule_format,OF **] by(auto dest: kle_Suc) qed(insert * x y, auto)
   1.592 +  qed(insert rs, auto) qed
   1.593 +
   1.594 +lemma ksimplex_fix_plane:
   1.595 +  assumes "a \<in> s" "j\<in>{1..n::nat}" "\<forall>x\<in>s - {a}. x j = q" "a0 \<in> s" "a1 \<in> s"
   1.596 +  "\<forall>i. a1 i = ((if i\<in>{1..n} then a0 i + 1 else a0 i)::nat)"
   1.597 +  shows "(a = a0) \<or> (a = a1)" proof- have *:"\<And>P A x y. \<forall>x\<in>A. P x \<Longrightarrow> x\<in>A \<Longrightarrow> y\<in>A \<Longrightarrow> P x \<and> P y" by auto
   1.598 +  show ?thesis apply(rule ccontr) using *[OF assms(3), of a0 a1] unfolding assms(6)[THEN spec[where x=j]]
   1.599 +    using assms(1-2,4-5) by auto qed
   1.600 +
   1.601 +lemma ksimplex_fix_plane_0:
   1.602 +  assumes "a \<in> s" "j\<in>{1..n::nat}" "\<forall>x\<in>s - {a}. x j = 0" "a0 \<in> s" "a1 \<in> s"
   1.603 +  "\<forall>i. a1 i = ((if i\<in>{1..n} then a0 i + 1 else a0 i)::nat)"
   1.604 +  shows "a = a1" apply(rule ccontr) using ksimplex_fix_plane[OF assms]
   1.605 +  using assms(3)[THEN bspec[where x=a1]] using assms(2,5)  
   1.606 +  unfolding assms(6)[THEN spec[where x=j]] by simp
   1.607 +
   1.608 +lemma ksimplex_fix_plane_p:
   1.609 +  assumes "ksimplex p n s" "a \<in> s" "j\<in>{1..n}" "\<forall>x\<in>s - {a}. x j = p" "a0 \<in> s" "a1 \<in> s"
   1.610 +  "\<forall>i. a1 i = (if i\<in>{1..n} then a0 i + 1 else a0 i)"
   1.611 +  shows "a = a0" proof(rule ccontr) note s = ksimplexD[OF assms(1),rule_format]
   1.612 +  assume as:"a \<noteq> a0" hence *:"a0 \<in> s - {a}" using assms(5) by auto
   1.613 +  hence "a1 = a" using ksimplex_fix_plane[OF assms(2-)] by auto
   1.614 +  thus False using as using assms(3,5) and assms(7)[rule_format,of j]
   1.615 +    unfolding assms(4)[rule_format,OF *] using s(4)[OF assms(6), of j] by auto qed
   1.616 +
   1.617 +lemma ksimplex_replace_0:
   1.618 +  assumes "ksimplex p n s" "a \<in> s" "n \<noteq> 0" "j\<in>{1..n}" "\<forall>x\<in>s - {a}. x j = 0"
   1.619 +  shows "card {s'. ksimplex p n s' \<and> (\<exists>b\<in>s'. s' - {b} = s - {a})} = 1" proof-
   1.620 +  have *:"\<And>s' a a'. s' - {a'} = s - {a} \<Longrightarrow> a' = a \<Longrightarrow> a' \<in> s' \<Longrightarrow> a \<in> s \<Longrightarrow> (s' = s)" by auto
   1.621 +  have **:"\<And>s' a'. ksimplex p n s' \<Longrightarrow> a' \<in> s' \<Longrightarrow> s' - {a'} = s - {a} \<Longrightarrow> s' = s" proof- case goal1
   1.622 +    guess a0 a1 apply(rule ksimplex_extrema_strong[OF assms(1,3)]) . note exta = this[rule_format]
   1.623 +    have a:"a = a1" apply(rule ksimplex_fix_plane_0[OF assms(2,4-5)]) using exta(1-2,5) by auto moreover
   1.624 +    guess b0 b1 apply(rule ksimplex_extrema_strong[OF goal1(1) assms(3)]) . note extb = this[rule_format]
   1.625 +    have a':"a' = b1" apply(rule ksimplex_fix_plane_0[OF goal1(2) assms(4), of b0]) unfolding goal1(3) using assms extb goal1 by auto moreover
   1.626 +    have "b0 = a0" unfolding kle_antisym[THEN sym, of b0 a0 n] using exta extb using goal1(3) unfolding a a' by blast
   1.627 +    hence "b1 = a1" apply-apply(rule ext) unfolding exta(5) extb(5) by auto ultimately
   1.628 +    show "s' = s" apply-apply(rule *[of _ a1 b1]) using exta(1-2) extb(1-2) goal1 by auto qed
   1.629 +  show ?thesis unfolding card_1_exists apply-apply(rule ex1I[of _ s])
   1.630 +    unfolding mem_Collect_eq defer apply(erule conjE bexE)+ apply(rule_tac a'=b in **) using assms(1,2) by auto qed
   1.631 +
   1.632 +lemma ksimplex_replace_1:
   1.633 +  assumes "ksimplex p n s" "a \<in> s" "n \<noteq> 0" "j\<in>{1..n}" "\<forall>x\<in>s - {a}. x j = p"
   1.634 +  shows "card {s'. ksimplex p n s' \<and> (\<exists>b\<in>s'. s' - {b} = s - {a})} = 1" proof-
   1.635 +  have lem:"\<And>a a' s'. s' - {a'} = s - {a} \<Longrightarrow> a' = a \<Longrightarrow> a' \<in> s' \<Longrightarrow> a \<in> s \<Longrightarrow> s' = s" by auto
   1.636 +  have lem:"\<And>s' a'. ksimplex p n s' \<Longrightarrow> a'\<in>s' \<Longrightarrow> s' - {a'} = s - {a} \<Longrightarrow> s' = s" proof- case goal1
   1.637 +    guess a0 a1 apply(rule ksimplex_extrema_strong[OF assms(1,3)]) . note exta = this[rule_format]
   1.638 +    have a:"a = a0" apply(rule ksimplex_fix_plane_p[OF assms(1-2,4-5) exta(1,2)]) unfolding exta by auto moreover
   1.639 +    guess b0 b1 apply(rule ksimplex_extrema_strong[OF goal1(1) assms(3)]) . note extb = this[rule_format]
   1.640 +    have a':"a' = b0" apply(rule ksimplex_fix_plane_p[OF goal1(1-2) assms(4), of _ b1]) unfolding goal1 extb using extb(1,2) assms(5) by auto
   1.641 +    moreover have *:"b1 = a1" unfolding kle_antisym[THEN sym, of b1 a1 n] using exta extb using goal1(3) unfolding a a' by blast moreover
   1.642 +    have "a0 = b0" apply(rule ext) proof- case goal1 show "a0 x = b0 x"
   1.643 +	using *[THEN cong, of x x] unfolding exta extb apply-apply(cases "x\<in>{1..n}") by auto qed
   1.644 +    ultimately show "s' = s" apply-apply(rule lem[OF goal1(3) _ goal1(2) assms(2)]) by auto qed 
   1.645 +  show ?thesis unfolding card_1_exists apply(rule ex1I[of _ s]) unfolding mem_Collect_eq apply(rule,rule assms(1))
   1.646 +    apply(rule_tac x=a in bexI) prefer 3 apply(erule conjE bexE)+ apply(rule_tac a'=b in lem) using assms(1-2) by auto qed
   1.647 +
   1.648 +lemma ksimplex_replace_2:
   1.649 +  assumes "ksimplex p n s" "a \<in> s" "n \<noteq> 0" "~(\<exists>j\<in>{1..n}. \<forall>x\<in>s - {a}. x j = 0)" "~(\<exists>j\<in>{1..n}. \<forall>x\<in>s - {a}. x j = p)"
   1.650 +  shows "card {s'. ksimplex p n s' \<and> (\<exists>b\<in>s'. s' - {b} = s - {a})} = 2" (is "card ?A = 2")  proof-
   1.651 +  have lem1:"\<And>a a' s s'. s' - {a'} = s - {a} \<Longrightarrow> a' = a \<Longrightarrow> a' \<in> s' \<Longrightarrow> a \<in> s \<Longrightarrow> s' = s" by auto
   1.652 +  have lem2:"\<And>a b. a\<in>s \<Longrightarrow> b\<noteq>a \<Longrightarrow> s \<noteq> insert b (s - {a})" proof case goal1
   1.653 +    hence "a\<in>insert b (s - {a})" by auto hence "a\<in> s - {a}" unfolding insert_iff using goal1 by auto
   1.654 +    thus False by auto qed
   1.655 +  guess a0 a1 apply(rule ksimplex_extrema_strong[OF assms(1,3)]) . note a0a1=this
   1.656 +  { assume "a=a0"
   1.657 +    have *:"\<And>P Q. (P \<or> Q) \<Longrightarrow> \<not> P \<Longrightarrow> Q" by auto
   1.658 +    have "\<exists>x\<in>s. \<not> kle n x a0" apply(rule_tac x=a1 in bexI) proof assume as:"kle n a1 a0"
   1.659 +      show False using kle_imp_pointwise[OF as,THEN spec[where x=1]] unfolding a0a1(5)[THEN spec[where x=1]]
   1.660 +        using assms(3) by auto qed(insert a0a1,auto)
   1.661 +    hence "\<exists>y\<in>s. \<exists>k\<in>{1..n}. \<forall>j. y j = (if j = k then a0 j + 1 else a0 j)"
   1.662 +      apply(rule_tac *[OF ksimplex_successor[OF assms(1-2),unfolded `a=a0`]]) by auto
   1.663 +    then guess a2 .. from this(2) guess k .. note k=this note a2=`a2\<in>s`
   1.664 +    def a3 \<equiv> "\<lambda>j. if j = k then a1 j + 1 else a1 j"
   1.665 +    have "a3 \<notin> s" proof assume "a3\<in>s" hence "kle n a3 a1" using a0a1(4) by auto
   1.666 +      thus False apply(drule_tac kle_imp_pointwise) unfolding a3_def
   1.667 +        apply(erule_tac x=k in allE) by auto qed
   1.668 +    hence "a3 \<noteq> a0" "a3 \<noteq> a1" using a0a1 by auto
   1.669 +    have "a2 \<noteq> a0" using k(2)[THEN spec[where x=k]] by auto
   1.670 +    have lem3:"\<And>x. x\<in>(s - {a0}) \<Longrightarrow> kle n a2 x" proof(rule ccontr) case goal1 hence as:"x\<in>s" "x\<noteq>a0" by auto
   1.671 +      have "kle n a2 x \<or> kle n x a2" using ksimplexD(6)[OF assms(1)] and as `a2\<in>s` by auto moreover
   1.672 +      have "kle n a0 x" using a0a1(4) as by auto
   1.673 +      ultimately have "x = a0 \<or> x = a2" apply-apply(rule kle_adjacent[OF k(2)]) using goal1(2) by auto
   1.674 +      hence "x = a2" using as by auto thus False using goal1(2) using kle_refl by auto qed
   1.675 +    let ?s = "insert a3 (s - {a0})" have "ksimplex p n ?s" apply(rule ksimplexI) proof(rule_tac[2-] ballI,rule_tac[4] ballI)
   1.676 +      show "card ?s = n + 1" using ksimplexD(2-3)[OF assms(1)]
   1.677 +        using `a3\<noteq>a0` `a3\<notin>s` `a0\<in>s` by(auto simp add:card_insert_if)
   1.678 +      fix x assume x:"x \<in> insert a3 (s - {a0})"
   1.679 +      show "\<forall>j. x j \<le> p" proof(rule,cases "x = a3")
   1.680 +	fix j case False thus "x j\<le>p" using x ksimplexD(4)[OF assms(1)] by auto next
   1.681 +	fix j case True show "x j\<le>p" unfolding True proof(cases "j=k") 
   1.682 +	  case False thus "a3 j \<le>p" unfolding True a3_def using `a1\<in>s` ksimplexD(4)[OF assms(1)] by auto next
   1.683 +	  guess a4 using assms(5)[unfolded bex_simps ball_simps,rule_format,OF k(1)] .. note a4=this
   1.684 +	  have "a2 k \<le> a4 k" using lem3[OF a4(1)[unfolded `a=a0`],THEN kle_imp_pointwise] by auto
   1.685 +	  also have "\<dots> < p" using ksimplexD(4)[OF assms(1),rule_format,of a4 k] using a4 by auto
   1.686 +	  finally have *:"a0 k + 1 < p" unfolding k(2)[rule_format] by auto
   1.687 +	  case True thus "a3 j \<le>p" unfolding a3_def unfolding a0a1(5)[rule_format]
   1.688 +	    using k(1) k(2)assms(5) using * by simp qed qed
   1.689 +      show "\<forall>j. j \<notin> {1..n} \<longrightarrow> x j = p" proof(rule,rule,cases "x=a3") fix j::nat assume j:"j\<notin>{1..n}"
   1.690 +	{ case False thus "x j = p" using j x ksimplexD(5)[OF assms(1)] by auto }
   1.691 +	case True show "x j = p" unfolding True a3_def using j k(1) 
   1.692 +	  using ksimplexD(5)[OF assms(1),rule_format,OF `a1\<in>s` j] by auto qed
   1.693 +      fix y assume y:"y\<in>insert a3 (s - {a0})"
   1.694 +      have lem4:"\<And>x. x\<in>s \<Longrightarrow> x\<noteq>a0 \<Longrightarrow> kle n x a3" proof- case goal1
   1.695 +	guess kk using a0a1(4)[rule_format,OF `x\<in>s`,THEN conjunct2,unfolded kle_def] 
   1.696 +          apply-apply(erule exE,erule conjE) . note kk=this
   1.697 +	have "k\<notin>kk" proof assume "k\<in>kk"
   1.698 +	  hence "a1 k = x k + 1" using kk by auto
   1.699 +	  hence "a0 k = x k" unfolding a0a1(5)[rule_format] using k(1) by auto
   1.700 +	  hence "a2 k = x k + 1" unfolding k(2)[rule_format] by auto moreover
   1.701 +	  have "a2 k \<le> x k" using lem3[of x,THEN kle_imp_pointwise] goal1 by auto 
   1.702 +	  ultimately show False by auto qed
   1.703 +	thus ?case unfolding kle_def apply(rule_tac x="insert k kk" in exI) using kk(1)
   1.704 +	  unfolding a3_def kle_def kk(2)[rule_format] using k(1) by auto qed
   1.705 +      show "kle n x y \<or> kle n y x" proof(cases "y=a3")
   1.706 +	case True show ?thesis unfolding True apply(cases "x=a3") defer apply(rule disjI1,rule lem4)
   1.707 +	  using x by auto next
   1.708 +	case False show ?thesis proof(cases "x=a3") case True show ?thesis unfolding True
   1.709 +	    apply(rule disjI2,rule lem4) using y False by auto next
   1.710 +	  case False thus ?thesis apply(rule_tac ksimplexD(6)[OF assms(1),rule_format]) 
   1.711 +	    using x y `y\<noteq>a3` by auto qed qed qed
   1.712 +    hence "insert a3 (s - {a0}) \<in> ?A" unfolding mem_Collect_eq apply-apply(rule,assumption)
   1.713 +      apply(rule_tac x="a3" in bexI) unfolding `a=a0` using `a3\<notin>s` by auto moreover
   1.714 +    have "s \<in> ?A" using assms(1,2) by auto ultimately have  "?A \<supseteq> {s, insert a3 (s - {a0})}" by auto
   1.715 +    moreover have "?A \<subseteq> {s, insert a3 (s - {a0})}" apply(rule) unfolding mem_Collect_eq proof(erule conjE)
   1.716 +      fix s' assume as:"ksimplex p n s'" and "\<exists>b\<in>s'. s' - {b} = s - {a}"
   1.717 +      from this(2) guess a' .. note a'=this
   1.718 +      guess a_min a_max apply(rule ksimplex_extrema_strong[OF as assms(3)]) . note min_max=this
   1.719 +      have *:"\<forall>x\<in>s' - {a'}. x k = a2 k" unfolding a' proof fix x assume x:"x\<in>s-{a}"
   1.720 +	hence "kle n a2 x" apply-apply(rule lem3) using `a=a0` by auto
   1.721 +	hence "a2 k \<le> x k" apply(drule_tac kle_imp_pointwise) by auto moreover
   1.722 +	have "x k \<le> a2 k" unfolding k(2)[rule_format] using a0a1(4)[rule_format,of x,THEN conjunct1] 
   1.723 +	  unfolding kle_def using x by auto ultimately show "x k = a2 k" by auto qed
   1.724 +      have **:"a'=a_min \<or> a'=a_max" apply(rule ksimplex_fix_plane[OF a'(1) k(1) *]) using min_max by auto
   1.725 +      show "s' \<in> {s, insert a3 (s - {a0})}" proof(cases "a'=a_min")
   1.726 +	case True have "a_max = a1" unfolding kle_antisym[THEN sym,of a_max a1 n] apply(rule)
   1.727 +	  apply(rule a0a1(4)[rule_format,THEN conjunct2]) defer  proof(rule min_max(4)[rule_format,THEN conjunct2])
   1.728 +	  show "a1\<in>s'" using a' unfolding `a=a0` using a0a1 by auto
   1.729 +	  show "a_max \<in> s" proof(rule ccontr) assume "a_max\<notin>s"
   1.730 +	    hence "a_max = a'" using a' min_max by auto
   1.731 +	    thus False unfolding True using min_max by auto qed qed
   1.732 +	hence "\<forall>i. a_max i = a1 i" by auto
   1.733 +	hence "a' = a" unfolding True `a=a0` apply-apply(subst expand_fun_eq,rule)
   1.734 +	  apply(erule_tac x=x in allE) unfolding a0a1(5)[rule_format] min_max(5)[rule_format]
   1.735 +	proof- case goal1 thus ?case apply(cases "x\<in>{1..n}") by auto qed
   1.736 +	hence "s' = s" apply-apply(rule lem1[OF a'(2)]) using `a\<in>s` `a'\<in>s'` by auto
   1.737 +	thus ?thesis by auto next
   1.738 +	case False hence as:"a' = a_max" using ** by auto
   1.739 +	have "a_min = a2" unfolding kle_antisym[THEN sym, of _ _ n] apply rule
   1.740 +	  apply(rule min_max(4)[rule_format,THEN conjunct1]) defer proof(rule lem3)
   1.741 +	  show "a_min \<in> s - {a0}" unfolding a'(2)[THEN sym,unfolded `a=a0`] 
   1.742 +	    unfolding as using min_max(1-3) by auto
   1.743 +	  have "a2 \<noteq> a" unfolding `a=a0` using k(2)[rule_format,of k] by auto
   1.744 +	  hence "a2 \<in> s - {a}" using a2 by auto thus "a2 \<in> s'" unfolding a'(2)[THEN sym] by auto qed
   1.745 +	hence "\<forall>i. a_min i = a2 i" by auto
   1.746 +	hence "a' = a3" unfolding as `a=a0` apply-apply(subst expand_fun_eq,rule)
   1.747 +	  apply(erule_tac x=x in allE) unfolding a0a1(5)[rule_format] min_max(5)[rule_format]
   1.748 +	  unfolding a3_def k(2)[rule_format] unfolding a0a1(5)[rule_format] proof- case goal1
   1.749 +	  show ?case unfolding goal1 apply(cases "x\<in>{1..n}") defer apply(cases "x=k")
   1.750 +	    using `k\<in>{1..n}` by auto qed
   1.751 +	hence "s' = insert a3 (s - {a0})" apply-apply(rule lem1) defer apply assumption
   1.752 +	  apply(rule a'(1)) unfolding a' `a=a0` using `a3\<notin>s` by auto
   1.753 +	thus ?thesis by auto qed qed
   1.754 +    ultimately have *:"?A = {s, insert a3 (s - {a0})}" by blast
   1.755 +    have "s \<noteq> insert a3 (s - {a0})" using `a3\<notin>s` by auto
   1.756 +    hence ?thesis unfolding * by auto } moreover
   1.757 +  { assume "a=a1"
   1.758 +    have *:"\<And>P Q. (P \<or> Q) \<Longrightarrow> \<not> P \<Longrightarrow> Q" by auto
   1.759 +    have "\<exists>x\<in>s. \<not> kle n a1 x" apply(rule_tac x=a0 in bexI) proof assume as:"kle n a1 a0"
   1.760 +      show False using kle_imp_pointwise[OF as,THEN spec[where x=1]] unfolding a0a1(5)[THEN spec[where x=1]]
   1.761 +        using assms(3) by auto qed(insert a0a1,auto)
   1.762 +    hence "\<exists>y\<in>s. \<exists>k\<in>{1..n}. \<forall>j. a1 j = (if j = k then y j + 1 else y j)"
   1.763 +      apply(rule_tac *[OF ksimplex_predecessor[OF assms(1-2),unfolded `a=a1`]]) by auto
   1.764 +    then guess a2 .. from this(2) guess k .. note k=this note a2=`a2\<in>s`
   1.765 +    def a3 \<equiv> "\<lambda>j. if j = k then a0 j - 1 else a0 j"
   1.766 +    have "a2 \<noteq> a1" using k(2)[THEN spec[where x=k]] by auto
   1.767 +    have lem3:"\<And>x. x\<in>(s - {a1}) \<Longrightarrow> kle n x a2" proof(rule ccontr) case goal1 hence as:"x\<in>s" "x\<noteq>a1" by auto
   1.768 +      have "kle n a2 x \<or> kle n x a2" using ksimplexD(6)[OF assms(1)] and as `a2\<in>s` by auto moreover
   1.769 +      have "kle n x a1" using a0a1(4) as by auto
   1.770 +      ultimately have "x = a2 \<or> x = a1" apply-apply(rule kle_adjacent[OF k(2)]) using goal1(2) by auto
   1.771 +      hence "x = a2" using as by auto thus False using goal1(2) using kle_refl by auto qed
   1.772 +    have "a0 k \<noteq> 0" proof-
   1.773 +      guess a4 using assms(4)[unfolded bex_simps ball_simps,rule_format,OF `k\<in>{1..n}`] .. note a4=this
   1.774 +      have "a4 k \<le> a2 k" using lem3[OF a4(1)[unfolded `a=a1`],THEN kle_imp_pointwise] by auto
   1.775 +      moreover have "a4 k > 0" using a4 by auto ultimately have "a2 k > 0" by auto
   1.776 +      hence "a1 k > 1" unfolding k(2)[rule_format] by simp
   1.777 +      thus ?thesis unfolding a0a1(5)[rule_format] using k(1) by simp qed
   1.778 +    hence lem4:"\<forall>j. a0 j = (if j=k then a3 j + 1 else a3 j)" unfolding a3_def by simp
   1.779 +    have "\<not> kle n a0 a3" apply(rule ccontr) unfolding not_not apply(drule kle_imp_pointwise)
   1.780 +      unfolding lem4[rule_format] apply(erule_tac x=k in allE) by auto
   1.781 +    hence "a3 \<notin> s" using a0a1(4) by auto
   1.782 +    hence "a3 \<noteq> a1" "a3 \<noteq> a0" using a0a1 by auto
   1.783 +    let ?s = "insert a3 (s - {a1})" have "ksimplex p n ?s" apply(rule ksimplexI) proof(rule_tac[2-] ballI,rule_tac[4] ballI)
   1.784 +      show "card ?s = n+1" using ksimplexD(2-3)[OF assms(1)]
   1.785 +        using `a3\<noteq>a0` `a3\<notin>s` `a1\<in>s` by(auto simp add:card_insert_if)
   1.786 +      fix x assume x:"x \<in> insert a3 (s - {a1})"
   1.787 +      show "\<forall>j. x j \<le> p" proof(rule,cases "x = a3")
   1.788 +	fix j case False thus "x j\<le>p" using x ksimplexD(4)[OF assms(1)] by auto next
   1.789 +	fix j case True show "x j\<le>p" unfolding True proof(cases "j=k") 
   1.790 +	  case False thus "a3 j \<le>p" unfolding True a3_def using `a0\<in>s` ksimplexD(4)[OF assms(1)] by auto next
   1.791 +	  guess a4 using assms(5)[unfolded bex_simps ball_simps,rule_format,OF k(1)] .. note a4=this
   1.792 +          case True have "a3 k \<le> a0 k" unfolding lem4[rule_format] by auto
   1.793 +          also have "\<dots> \<le> p" using ksimplexD(4)[OF assms(1),rule_format,of a0 k] a0a1 by auto
   1.794 +          finally show "a3 j \<le> p" unfolding True by auto qed qed
   1.795 +      show "\<forall>j. j \<notin> {1..n} \<longrightarrow> x j = p" proof(rule,rule,cases "x=a3") fix j::nat assume j:"j\<notin>{1..n}"
   1.796 +	{ case False thus "x j = p" using j x ksimplexD(5)[OF assms(1)] by auto }
   1.797 +	case True show "x j = p" unfolding True a3_def using j k(1) 
   1.798 +	  using ksimplexD(5)[OF assms(1),rule_format,OF `a0\<in>s` j] by auto qed
   1.799 +      fix y assume y:"y\<in>insert a3 (s - {a1})"
   1.800 +      have lem4:"\<And>x. x\<in>s \<Longrightarrow> x\<noteq>a1 \<Longrightarrow> kle n a3 x" proof- case goal1 hence *:"x\<in>s - {a1}" by auto
   1.801 +        have "kle n a3 a2" proof- have "kle n a0 a1" using a0a1 by auto then guess kk unfolding kle_def ..
   1.802 +          thus ?thesis unfolding kle_def apply(rule_tac x=kk in exI) unfolding lem4[rule_format] k(2)[rule_format]
   1.803 +            apply(rule)defer proof(rule) case goal1 thus ?case apply-apply(erule conjE)
   1.804 +              apply(erule_tac[!] x=j in allE) apply(cases "j\<in>kk") apply(case_tac[!] "j=k") by auto qed auto qed moreover
   1.805 +        have "kle n a3 a0" unfolding kle_def lem4[rule_format] apply(rule_tac x="{k}" in exI) using k(1) by auto
   1.806 +        ultimately show ?case apply-apply(rule kle_between_l[of _ a0 _ a2]) using lem3[OF *]
   1.807 +          using a0a1(4)[rule_format,OF goal1(1)] by auto qed
   1.808 +      show "kle n x y \<or> kle n y x" proof(cases "y=a3")
   1.809 +	case True show ?thesis unfolding True apply(cases "x=a3") defer apply(rule disjI2,rule lem4)
   1.810 +	  using x by auto next
   1.811 +	case False show ?thesis proof(cases "x=a3") case True show ?thesis unfolding True
   1.812 +	    apply(rule disjI1,rule lem4) using y False by auto next
   1.813 +	  case False thus ?thesis apply(rule_tac ksimplexD(6)[OF assms(1),rule_format]) 
   1.814 +	    using x y `y\<noteq>a3` by auto qed qed qed
   1.815 +    hence "insert a3 (s - {a1}) \<in> ?A" unfolding mem_Collect_eq apply-apply(rule,assumption)
   1.816 +      apply(rule_tac x="a3" in bexI) unfolding `a=a1` using `a3\<notin>s` by auto moreover
   1.817 +    have "s \<in> ?A" using assms(1,2) by auto ultimately have  "?A \<supseteq> {s, insert a3 (s - {a1})}" by auto
   1.818 +    moreover have "?A \<subseteq> {s, insert a3 (s - {a1})}" apply(rule) unfolding mem_Collect_eq proof(erule conjE)
   1.819 +      fix s' assume as:"ksimplex p n s'" and "\<exists>b\<in>s'. s' - {b} = s - {a}"
   1.820 +      from this(2) guess a' .. note a'=this
   1.821 +      guess a_min a_max apply(rule ksimplex_extrema_strong[OF as assms(3)]) . note min_max=this
   1.822 +      have *:"\<forall>x\<in>s' - {a'}. x k = a2 k" unfolding a' proof fix x assume x:"x\<in>s-{a}"
   1.823 +	hence "kle n x a2" apply-apply(rule lem3) using `a=a1` by auto
   1.824 +	hence "x k \<le> a2 k" apply(drule_tac kle_imp_pointwise) by auto moreover
   1.825 +	{ have "a2 k \<le> a0 k" using k(2)[rule_format,of k] unfolding a0a1(5)[rule_format] using k(1) by simp
   1.826 +	  also have "\<dots> \<le> x k" using a0a1(4)[rule_format,of x,THEN conjunct1,THEN kle_imp_pointwise] x by auto
   1.827 +	  finally have "a2 k \<le> x k" . } ultimately show "x k = a2 k" by auto qed
   1.828 +      have **:"a'=a_min \<or> a'=a_max" apply(rule ksimplex_fix_plane[OF a'(1) k(1) *]) using min_max by auto
   1.829 +      have "a2 \<noteq> a1" proof assume as:"a2 = a1"
   1.830 +	show False using k(2) unfolding as apply(erule_tac x=k in allE) by auto qed
   1.831 +      hence a2':"a2 \<in> s' - {a'}" unfolding a' using a2 unfolding `a=a1` by auto
   1.832 +      show "s' \<in> {s, insert a3 (s - {a1})}" proof(cases "a'=a_min")
   1.833 +	case True have "a_max \<in> s - {a1}" using min_max unfolding a'(2)[unfolded `a=a1`,THEN sym] True by auto
   1.834 +	hence "a_max = a2" unfolding kle_antisym[THEN sym,of a_max a2 n] apply-apply(rule)
   1.835 +	  apply(rule lem3,assumption) apply(rule min_max(4)[rule_format,THEN conjunct2]) using a2' by auto
   1.836 +	hence a_max:"\<forall>i. a_max i = a2 i" by auto
   1.837 +	have *:"\<forall>j. a2 j = (if j\<in>{1..n} then a3 j + 1 else a3 j)" 
   1.838 +	  using k(2) unfolding lem4[rule_format] a0a1(5)[rule_format] apply-apply(rule,erule_tac x=j in allE)
   1.839 +	proof- case goal1 thus ?case apply(cases "j\<in>{1..n}",case_tac[!] "j=k") by auto qed
   1.840 +	have "\<forall>i. a_min i = a3 i" using a_max apply-apply(rule,erule_tac x=i in allE)
   1.841 +	  unfolding min_max(5)[rule_format] *[rule_format] proof- case goal1
   1.842 +	  thus ?case apply(cases "i\<in>{1..n}") by auto qed hence "a_min = a3" unfolding expand_fun_eq .
   1.843 +	hence "s' = insert a3 (s - {a1})" using a' unfolding `a=a1` True by auto thus ?thesis by auto next
   1.844 +	case False hence as:"a'=a_max" using ** by auto
   1.845 +	have "a_min = a0" unfolding kle_antisym[THEN sym,of _ _ n] apply(rule)
   1.846 +	  apply(rule min_max(4)[rule_format,THEN conjunct1]) defer apply(rule a0a1(4)[rule_format,THEN conjunct1]) proof-
   1.847 +	  have "a_min \<in> s - {a1}" using min_max(1,3) unfolding a'(2)[THEN sym,unfolded `a=a1`] as by auto
   1.848 +	  thus "a_min \<in> s" by auto have "a0 \<in> s - {a1}" using a0a1(1-3) by auto thus "a0 \<in> s'"
   1.849 +	    unfolding a'(2)[THEN sym,unfolded `a=a1`] by auto qed
   1.850 +	hence "\<forall>i. a_max i = a1 i" unfolding a0a1(5)[rule_format] min_max(5)[rule_format] by auto
   1.851 +	hence "s' = s" apply-apply(rule lem1[OF a'(2)]) using `a\<in>s` `a'\<in>s'` unfolding as `a=a1` unfolding expand_fun_eq by auto
   1.852 +	thus ?thesis by auto qed qed 
   1.853 +    ultimately have *:"?A = {s, insert a3 (s - {a1})}" by blast
   1.854 +    have "s \<noteq> insert a3 (s - {a1})" using `a3\<notin>s` by auto
   1.855 +    hence ?thesis unfolding * by auto } moreover
   1.856 +  { assume as:"a\<noteq>a0" "a\<noteq>a1" have "\<not> (\<forall>x\<in>s. kle n a x)" proof case goal1
   1.857 +      have "a=a0" unfolding kle_antisym[THEN sym,of _ _ n] apply(rule)
   1.858 +	using goal1 a0a1 assms(2) by auto thus False using as by auto qed
   1.859 +    hence "\<exists>y\<in>s. \<exists>k\<in>{1..n}. \<forall>j. a j = (if j = k then y j + 1 else y j)" using  ksimplex_predecessor[OF assms(1-2)] by blast
   1.860 +    then guess u .. from this(2) guess k .. note k = this[rule_format] note u = `u\<in>s`
   1.861 +    have "\<not> (\<forall>x\<in>s. kle n x a)" proof case goal1
   1.862 +      have "a=a1" unfolding kle_antisym[THEN sym,of _ _ n] apply(rule)
   1.863 +	using goal1 a0a1 assms(2) by auto thus False using as by auto qed
   1.864 +    hence "\<exists>y\<in>s. \<exists>k\<in>{1..n}. \<forall>j. y j = (if j = k then a j + 1 else a j)" using  ksimplex_successor[OF assms(1-2)] by blast
   1.865 +    then guess v .. from this(2) guess l .. note l = this[rule_format] note v = `v\<in>s`
   1.866 +    def a' \<equiv> "\<lambda>j. if j = l then u j + 1 else u j"
   1.867 +    have kl:"k \<noteq> l" proof assume "k=l" have *:"\<And>P. (if P then (1::nat) else 0) \<noteq> 2" by auto
   1.868 +      thus False using ksimplexD(6)[OF assms(1),rule_format,OF u v] unfolding kle_def
   1.869 +	unfolding l(2) k(2) `k=l` apply-apply(erule disjE)apply(erule_tac[!] exE conjE)+
   1.870 +	apply(erule_tac[!] x=l in allE)+ by(auto simp add: *) qed
   1.871 +    hence aa':"a'\<noteq>a" apply-apply rule unfolding expand_fun_eq unfolding a'_def k(2)
   1.872 +      apply(erule_tac x=l in allE) by auto
   1.873 +    have "a' \<notin> s" apply(rule) apply(drule ksimplexD(6)[OF assms(1),rule_format,OF `a\<in>s`]) proof(cases "kle n a a'")
   1.874 +      case goal2 hence "kle n a' a" by auto thus False apply(drule_tac kle_imp_pointwise)
   1.875 +	apply(erule_tac x=l in allE) unfolding a'_def k(2) using kl by auto next
   1.876 +      case True thus False apply(drule_tac kle_imp_pointwise)
   1.877 +	apply(erule_tac x=k in allE) unfolding a'_def k(2) using kl by auto qed
   1.878 +    have kle_uv:"kle n u a" "kle n u a'" "kle n a v" "kle n a' v" unfolding kle_def apply-
   1.879 +      apply(rule_tac[1] x="{k}" in exI,rule_tac[2] x="{l}" in exI)
   1.880 +      apply(rule_tac[3] x="{l}" in exI,rule_tac[4] x="{k}" in exI)
   1.881 +      unfolding l(2) k(2) a'_def using l(1) k(1) by auto
   1.882 +    have uxv:"\<And>x. kle n u x \<Longrightarrow> kle n x v \<Longrightarrow> (x = u) \<or> (x = a) \<or> (x = a') \<or> (x = v)"
   1.883 +    proof- case goal1 thus ?case proof(cases "x k = u k", case_tac[!] "x l = u l")
   1.884 +      assume as:"x l = u l" "x k = u k"
   1.885 +      have "x = u" unfolding expand_fun_eq
   1.886 +	using goal1(2)[THEN kle_imp_pointwise,unfolded l(2)] unfolding k(2) apply-
   1.887 +	using goal1(1)[THEN kle_imp_pointwise] apply-apply rule apply(erule_tac x=xa in allE)+ proof- case goal1
   1.888 +	thus ?case apply(cases "x=l") apply(case_tac[!] "x=k") using as by auto qed thus ?case by auto next
   1.889 +      assume as:"x l \<noteq> u l" "x k = u k"
   1.890 +      have "x = a'" unfolding expand_fun_eq unfolding a'_def
   1.891 +	using goal1(2)[THEN kle_imp_pointwise] unfolding l(2) k(2) apply-
   1.892 +	using goal1(1)[THEN kle_imp_pointwise] apply-apply rule apply(erule_tac x=xa in allE)+ proof- case goal1
   1.893 +	thus ?case apply(cases "x=l") apply(case_tac[!] "x=k") using as by auto qed thus ?case by auto next
   1.894 +      assume as:"x l = u l" "x k \<noteq> u k"
   1.895 +      have "x = a" unfolding expand_fun_eq
   1.896 +	using goal1(2)[THEN kle_imp_pointwise] unfolding l(2) k(2) apply-
   1.897 +	using goal1(1)[THEN kle_imp_pointwise] apply-apply rule apply(erule_tac x=xa in allE)+ proof- case goal1
   1.898 +	thus ?case apply(cases "x=l") apply(case_tac[!] "x=k") using as by auto qed thus ?case by auto next
   1.899 +      assume as:"x l \<noteq> u l" "x k \<noteq> u k"
   1.900 +      have "x = v" unfolding expand_fun_eq
   1.901 +	using goal1(2)[THEN kle_imp_pointwise] unfolding l(2) k(2) apply-
   1.902 +	using goal1(1)[THEN kle_imp_pointwise] apply-apply rule apply(erule_tac x=xa in allE)+ proof- case goal1
   1.903 +	thus ?case apply(cases "x=l") apply(case_tac[!] "x=k") using as `k\<noteq>l` by auto qed thus ?case by auto qed qed
   1.904 +    have uv:"kle n u v" apply(rule kle_trans[OF kle_uv(1,3)]) using ksimplexD(6)[OF assms(1)] using u v by auto
   1.905 +    have lem3:"\<And>x. x\<in>s \<Longrightarrow> kle n v x \<Longrightarrow> kle n a' x" apply(rule kle_between_r[of _ u _ v])
   1.906 +      prefer 3 apply(rule kle_trans[OF uv]) defer apply(rule ksimplexD(6)[OF assms(1),rule_format])
   1.907 +      using kle_uv `u\<in>s` by auto
   1.908 +    have lem4:"\<And>x. x\<in>s \<Longrightarrow> kle n x u \<Longrightarrow> kle n x a'" apply(rule kle_between_l[of _ u _ v])
   1.909 +      prefer 4 apply(rule kle_trans[OF _ uv]) defer apply(rule ksimplexD(6)[OF assms(1),rule_format])
   1.910 +      using kle_uv `v\<in>s` by auto
   1.911 +    have lem5:"\<And>x. x\<in>s \<Longrightarrow> x\<noteq>a \<Longrightarrow> kle n x a' \<or> kle n a' x" proof- case goal1 thus ?case
   1.912 +      proof(cases "kle n v x \<or> kle n x u") case True thus ?thesis using goal1 by(auto intro:lem3 lem4) next
   1.913 +	case False hence *:"kle n u x" "kle n x v" using ksimplexD(6)[OF assms(1)] using goal1 `u\<in>s` `v\<in>s` by auto
   1.914 +	show ?thesis using uxv[OF *] using kle_uv using goal1 by auto qed qed
   1.915 +    have "ksimplex p n (insert a' (s - {a}))" apply(rule ksimplexI) proof(rule_tac[2-] ballI,rule_tac[4] ballI)
   1.916 +      show "card (insert a' (s - {a})) = n + 1" using ksimplexD(2-3)[OF assms(1)]
   1.917 +        using `a'\<noteq>a` `a'\<notin>s` `a\<in>s` by(auto simp add:card_insert_if)
   1.918 +      fix x assume x:"x \<in> insert a' (s - {a})"
   1.919 +      show "\<forall>j. x j \<le> p" proof(rule,cases "x = a'")
   1.920 +	fix j case False thus "x j\<le>p" using x ksimplexD(4)[OF assms(1)] by auto next
   1.921 +	fix j case True show "x j\<le>p" unfolding True proof(cases "j=l") 
   1.922 +	  case False thus "a' j \<le>p" unfolding True a'_def using `u\<in>s` ksimplexD(4)[OF assms(1)] by auto next
   1.923 +	  case True have *:"a l = u l" "v l = a l + 1" using k(2)[of l] l(2)[of l] `k\<noteq>l` by auto
   1.924 +	  have "u l + 1 \<le> p" unfolding *[THEN sym] using ksimplexD(4)[OF assms(1)] using `v\<in>s` by auto
   1.925 +	  thus "a' j \<le>p" unfolding a'_def True by auto qed qed
   1.926 +      show "\<forall>j. j \<notin> {1..n} \<longrightarrow> x j = p" proof(rule,rule,cases "x=a'") fix j::nat assume j:"j\<notin>{1..n}"
   1.927 +	{ case False thus "x j = p" using j x ksimplexD(5)[OF assms(1)] by auto }
   1.928 +	case True show "x j = p" unfolding True a'_def using j l(1) 
   1.929 +	  using ksimplexD(5)[OF assms(1),rule_format,OF `u\<in>s` j] by auto qed
   1.930 +      fix y assume y:"y\<in>insert a' (s - {a})"
   1.931 +      show "kle n x y \<or> kle n y x" proof(cases "y=a'")
   1.932 +	case True show ?thesis unfolding True apply(cases "x=a'") defer apply(rule lem5) using x by auto next
   1.933 +	case False show ?thesis proof(cases "x=a'") case True show ?thesis unfolding True
   1.934 +	    using lem5[of y] using y by auto next
   1.935 +	  case False thus ?thesis apply(rule_tac ksimplexD(6)[OF assms(1),rule_format]) 
   1.936 +	    using x y `y\<noteq>a'` by auto qed qed qed
   1.937 +    hence "insert a' (s - {a}) \<in> ?A" unfolding mem_Collect_eq apply-apply(rule,assumption)
   1.938 +      apply(rule_tac x="a'" in bexI) using aa' `a'\<notin>s` by auto moreover
   1.939 +    have "s \<in> ?A" using assms(1,2) by auto ultimately have  "?A \<supseteq> {s, insert a' (s - {a})}" by auto
   1.940 +    moreover have "?A \<subseteq> {s, insert a' (s - {a})}" apply(rule) unfolding mem_Collect_eq proof(erule conjE)
   1.941 +      fix s' assume as:"ksimplex p n s'" and "\<exists>b\<in>s'. s' - {b} = s - {a}"
   1.942 +      from this(2) guess a'' .. note a''=this
   1.943 +      have "u\<noteq>v" unfolding expand_fun_eq unfolding l(2) k(2) by auto
   1.944 +      hence uv':"\<not> kle n v u" using uv using kle_antisym by auto
   1.945 +      have "u\<noteq>a" "v\<noteq>a" unfolding expand_fun_eq k(2) l(2) by auto 
   1.946 +      hence uvs':"u\<in>s'" "v\<in>s'" using `u\<in>s` `v\<in>s` using a'' by auto
   1.947 +      have lem6:"a \<in> s' \<or> a' \<in> s'" proof(cases "\<forall>x\<in>s'. kle n x u \<or> kle n v x")
   1.948 +	case False then guess w unfolding ball_simps .. note w=this
   1.949 +	hence "kle n u w" "kle n w v" using ksimplexD(6)[OF as] uvs' by auto
   1.950 +	hence "w = a' \<or> w = a" using uxv[of w] uvs' w by auto thus ?thesis using w by auto next
   1.951 +	case True have "\<not> (\<forall>x\<in>s'. kle n x u)" unfolding ball_simps apply(rule_tac x=v in bexI)
   1.952 +	  using uv `u\<noteq>v` unfolding kle_antisym[of n u v,THEN sym] using `v\<in>s'` by auto
   1.953 +	hence "\<exists>y\<in>s'. \<exists>k\<in>{1..n}. \<forall>j. y j = (if j = k then u j + 1 else u j)" using ksimplex_successor[OF as `u\<in>s'`] by blast
   1.954 +	then guess w .. note w=this from this(2) guess kk .. note kk=this[rule_format]
   1.955 +	have "\<not> kle n w u" apply-apply(rule,drule kle_imp_pointwise) 
   1.956 +	  apply(erule_tac x=kk in allE) unfolding kk by auto 
   1.957 +	hence *:"kle n v w" using True[rule_format,OF w(1)] by auto
   1.958 +	hence False proof(cases "kk\<noteq>l") case True thus False using *[THEN kle_imp_pointwise, unfolded l(2) kk k(2)]
   1.959 +	    apply(erule_tac x=l in allE) using `k\<noteq>l` by auto  next
   1.960 +	  case False hence "kk\<noteq>k" using `k\<noteq>l` by auto thus False using *[THEN kle_imp_pointwise, unfolded l(2) kk k(2)]
   1.961 +	    apply(erule_tac x=k in allE) using `k\<noteq>l` by auto qed
   1.962 +	thus ?thesis by auto qed
   1.963 +      thus "s' \<in> {s, insert a' (s - {a})}" proof(cases "a\<in>s'")
   1.964 +	case True hence "s' = s" apply-apply(rule lem1[OF a''(2)]) using a'' `a\<in>s` by auto
   1.965 +	thus ?thesis by auto next case False hence "a'\<in>s'" using lem6 by auto
   1.966 +	hence "s' = insert a' (s - {a})" apply-apply(rule lem1[of _ a'' _ a'])
   1.967 +	  unfolding a''(2)[THEN sym] using a'' using `a'\<notin>s` by auto
   1.968 +	thus ?thesis by auto qed qed 
   1.969 +    ultimately have *:"?A = {s, insert a' (s - {a})}" by blast
   1.970 +    have "s \<noteq> insert a' (s - {a})" using `a'\<notin>s` by auto
   1.971 +    hence ?thesis unfolding * by auto } 
   1.972 +  ultimately show ?thesis by auto qed
   1.973 +
   1.974 +subsection {* Hence another step towards concreteness. *}
   1.975 +
   1.976 +lemma kuhn_simplex_lemma:
   1.977 +  assumes "\<forall>s. ksimplex p (n + 1) s \<longrightarrow> (rl ` s \<subseteq>{0..n+1})"
   1.978 +  "odd (card{f. \<exists>s a. ksimplex p (n + 1) s \<and> a \<in> s \<and> (f = s - {a}) \<and>
   1.979 +  (rl ` f = {0 .. n}) \<and> ((\<exists>j\<in>{1..n+1}.\<forall>x\<in>f. x j = 0) \<or> (\<exists>j\<in>{1..n+1}.\<forall>x\<in>f. x j = p))})"
   1.980 +  shows "odd(card {s\<in>{s. ksimplex p (n + 1) s}. rl ` s = {0..n+1} })" proof-
   1.981 +  have *:"\<And>x y. x = y \<Longrightarrow> odd (card x) \<Longrightarrow> odd (card y)" by auto
   1.982 +  have *:"odd(card {f\<in>{f. \<exists>s\<in>{s. ksimplex p (n + 1) s}. (\<exists>a\<in>s. f = s - {a})}. 
   1.983 +                (rl ` f = {0..n}) \<and>
   1.984 +               ((\<exists>j\<in>{1..n+1}. \<forall>x\<in>f. x j = 0) \<or>
   1.985 +                (\<exists>j\<in>{1..n+1}. \<forall>x\<in>f. x j = p))})" apply(rule *[OF _ assms(2)]) by auto
   1.986 +  show ?thesis apply(rule kuhn_complete_lemma[OF finite_simplices]) prefer 6 apply(rule *) apply(rule,rule,rule)
   1.987 +    apply(subst ksimplex_def) defer apply(rule,rule assms(1)[rule_format]) unfolding mem_Collect_eq apply assumption
   1.988 +    apply default+ unfolding mem_Collect_eq apply(erule disjE bexE)+ defer apply(erule disjE bexE)+ defer 
   1.989 +    apply default+ unfolding mem_Collect_eq apply(erule disjE bexE)+ unfolding mem_Collect_eq proof-
   1.990 +    fix f s a assume as:"ksimplex p (n + 1) s" "a\<in>s" "f = s - {a}"
   1.991 +    let ?S = "{s. ksimplex p (n + 1) s \<and> (\<exists>a\<in>s. f = s - {a})}"
   1.992 +    have S:"?S = {s'. ksimplex p (n + 1) s' \<and> (\<exists>b\<in>s'. s' - {b} = s - {a})}" unfolding as by blast
   1.993 +    { fix j assume j:"j \<in> {1..n + 1}" "\<forall>x\<in>f. x j = 0" thus "card {s. ksimplex p (n + 1) s \<and> (\<exists>a\<in>s. f = s - {a})} = 1" unfolding S
   1.994 +	apply-apply(rule ksimplex_replace_0) apply(rule as)+ unfolding as by auto }
   1.995 +    { fix j assume j:"j \<in> {1..n + 1}" "\<forall>x\<in>f. x j = p" thus "card {s. ksimplex p (n + 1) s \<and> (\<exists>a\<in>s. f = s - {a})} = 1" unfolding S
   1.996 +	apply-apply(rule ksimplex_replace_1) apply(rule as)+ unfolding as by auto }
   1.997 +    show "\<not> ((\<exists>j\<in>{1..n+1}. \<forall>x\<in>f. x j = 0) \<or> (\<exists>j\<in>{1..n+1}. \<forall>x\<in>f. x j = p)) \<Longrightarrow> card ?S = 2"
   1.998 +      unfolding S apply(rule ksimplex_replace_2) apply(rule as)+ unfolding as by auto qed auto qed
   1.999 +
  1.1000 +subsection {* Reduced labelling. *}
  1.1001 +
  1.1002 +definition "reduced label (n::nat) (x::nat\<Rightarrow>nat) =
  1.1003 +  (SOME k. k \<le> n \<and> (\<forall>i. 1\<le>i \<and> i<k+1 \<longrightarrow> label x i = 0) \<and> (k = n \<or> label x (k + 1) \<noteq> (0::nat)))"
  1.1004 +
  1.1005 +lemma reduced_labelling: shows "reduced label n x \<le> n" (is ?t1) and
  1.1006 +  "\<forall>i. 1\<le>i \<and> i < reduced label n x + 1 \<longrightarrow> (label x i = 0)" (is ?t2)
  1.1007 +  "(reduced label n x = n) \<or> (label x (reduced label n x + 1) \<noteq> 0)"  (is ?t3) proof-
  1.1008 +  have num_WOP:"\<And>P k. P (k::nat) \<Longrightarrow> \<exists>n. P n \<and> (\<forall>m<n. \<not> P m)"
  1.1009 +    apply(drule ex_has_least_nat[where m="\<lambda>x. x"]) apply(erule exE,rule_tac x=x in exI) by auto
  1.1010 +  have *:"n \<le> n \<and> (label x (n + 1) \<noteq> 0 \<or> n = n)" by auto
  1.1011 +  then guess N apply(drule_tac num_WOP[of "\<lambda>j. j\<le>n \<and> (label x (j+1) \<noteq> 0 \<or> n = j)"]) apply(erule exE) . note N=this
  1.1012 +  have N':"N \<le> n" "\<forall>i. 1 \<le> i \<and> i < N + 1 \<longrightarrow> label x i = 0" "N = n \<or> label x (N + 1) \<noteq> 0" defer proof(rule,rule)
  1.1013 +    fix i assume i:"1\<le>i \<and> i<N+1" thus "label x i = 0" using N[THEN conjunct2,THEN spec[where x="i - 1"]] using N by auto qed(insert N, auto)
  1.1014 +  show ?t1 ?t2 ?t3 unfolding reduced_def apply(rule_tac[!] someI2_ex) using N' by(auto intro!: exI[where x=N]) qed
  1.1015 +
  1.1016 +lemma reduced_labelling_unique: fixes x::"nat \<Rightarrow> nat"
  1.1017 +  assumes "r \<le> n"  "\<forall>i. 1 \<le> i \<and> i < r + 1 \<longrightarrow> (label x i = 0)" "(r = n) \<or> (label x (r + 1) \<noteq> 0)"
  1.1018 +  shows "reduced label n x = r" apply(rule le_antisym) apply(rule_tac[!] ccontr) unfolding not_le
  1.1019 +  using reduced_labelling[of label n x] using assms by auto
  1.1020 +
  1.1021 +lemma reduced_labelling_0: assumes "j\<in>{1..n}" "label x j = 0" shows "reduced label n x \<noteq> j - 1"
  1.1022 +  using reduced_labelling[of label n x] using assms by fastsimp 
  1.1023 +
  1.1024 +lemma reduced_labelling_1: assumes "j\<in>{1..n}" "label x j \<noteq> 0" shows "reduced label n x < j"
  1.1025 +  using assms and reduced_labelling[of label n x] apply(erule_tac x=j in allE) by auto
  1.1026 +
  1.1027 +lemma reduced_labelling_Suc:
  1.1028 +  assumes "reduced lab (n + 1) x \<noteq> n + 1" shows "reduced lab (n + 1) x = reduced lab n x"
  1.1029 +  apply(subst eq_commute) apply(rule reduced_labelling_unique)
  1.1030 +  using reduced_labelling[of lab "n+1" x] and assms by auto 
  1.1031 +
  1.1032 +lemma complete_face_top:
  1.1033 +  assumes "\<forall>x\<in>f. \<forall>j\<in>{1..n+1}. x j = 0 \<longrightarrow> lab x j = 0"
  1.1034 +          "\<forall>x\<in>f. \<forall>j\<in>{1..n+1}. x j = p \<longrightarrow> lab x j = 1"
  1.1035 +  shows "((reduced lab (n + 1)) ` f = {0..n}) \<and> ((\<exists>j\<in>{1..n+1}. \<forall>x\<in>f. x j = 0) \<or> (\<exists>j\<in>{1..n+1}. \<forall>x\<in>f. x j = p)) \<longleftrightarrow>
  1.1036 +  ((reduced lab (n + 1)) ` f = {0..n}) \<and> (\<forall>x\<in>f. x (n + 1) = p)" (is "?l = ?r") proof
  1.1037 +  assume ?l (is "?as \<and> (?a \<or> ?b)") thus ?r apply-apply(rule,erule conjE,assumption) proof(cases ?a)
  1.1038 +    case True then guess j .. note j=this {fix x assume x:"x\<in>f"
  1.1039 +      have "reduced lab (n+1) x \<noteq> j - 1" using j apply-apply(rule reduced_labelling_0) defer apply(rule assms(1)[rule_format]) using x by auto }
  1.1040 +    moreover have "j - 1 \<in> {0..n}" using j by auto
  1.1041 +    then guess y unfolding `?l`[THEN conjunct1,THEN sym] and image_iff .. note y = this
  1.1042 +    ultimately have False by auto thus "\<forall>x\<in>f. x (n + 1) = p" by auto next
  1.1043 +
  1.1044 +    case False hence ?b using `?l` by blast then guess j .. note j=this {fix x assume x:"x\<in>f"
  1.1045 +      have "reduced lab (n+1) x < j" using j apply-apply(rule reduced_labelling_1) using assms(2)[rule_format,of x j] and x by auto } note * = this
  1.1046 +    have "j = n + 1" proof(rule ccontr) case goal1 hence "j < n + 1" using j by auto moreover
  1.1047 +      have "n \<in> {0..n}" by auto then guess y unfolding `?l`[THEN conjunct1,THEN sym] image_iff ..
  1.1048 +      ultimately show False using *[of y] by auto qed
  1.1049 +    thus "\<forall>x\<in>f. x (n + 1) = p" using j by auto qed qed(auto)
  1.1050 +
  1.1051 +subsection {* Hence we get just about the nice induction. *}
  1.1052 +
  1.1053 +lemma kuhn_induction:
  1.1054 +  assumes "0 < p" "\<forall>x. \<forall>j\<in>{1..n+1}. (\<forall>j. x j \<le> p) \<and> (x j = 0) \<longrightarrow> (lab x j = 0)"
  1.1055 +                  "\<forall>x. \<forall>j\<in>{1..n+1}. (\<forall>j. x j \<le> p) \<and> (x j = p) \<longrightarrow> (lab x j = 1)"
  1.1056 +        "odd (card {f. ksimplex p n f \<and> ((reduced lab n) ` f = {0..n})})"
  1.1057 +  shows "odd (card {s. ksimplex p (n+1) s \<and>((reduced lab (n+1)) `  s = {0..n+1})})" proof-
  1.1058 +  have *:"\<And>s t. odd (card s) \<Longrightarrow> s = t \<Longrightarrow> odd (card t)" "\<And>s f. (\<And>x. f x \<le> n +1 ) \<Longrightarrow> f ` s \<subseteq> {0..n+1}" by auto
  1.1059 +  show ?thesis apply(rule kuhn_simplex_lemma[unfolded mem_Collect_eq]) apply(rule,rule,rule *,rule reduced_labelling)
  1.1060 +    apply(rule *(1)[OF assms(4)]) apply(rule set_ext) unfolding mem_Collect_eq apply(rule,erule conjE) defer apply(rule) proof-(*(rule,rule)*)
  1.1061 +    fix f assume as:"ksimplex p n f" "reduced lab n ` f = {0..n}"
  1.1062 +    have *:"\<forall>x\<in>f. \<forall>j\<in>{1..n + 1}. x j = 0 \<longrightarrow> lab x j = 0" "\<forall>x\<in>f. \<forall>j\<in>{1..n + 1}. x j = p \<longrightarrow> lab x j = 1"
  1.1063 +      using assms(2-3) using as(1)[unfolded ksimplex_def] by auto
  1.1064 +    have allp:"\<forall>x\<in>f. x (n + 1) = p" using assms(2) using as(1)[unfolded ksimplex_def] by auto
  1.1065 +    { fix x assume "x\<in>f" hence "reduced lab (n + 1) x < n + 1" apply-apply(rule reduced_labelling_1)
  1.1066 +	defer using assms(3) using as(1)[unfolded ksimplex_def] by auto
  1.1067 +      hence "reduced lab (n + 1) x = reduced lab n x" apply-apply(rule reduced_labelling_Suc) using reduced_labelling(1) by auto }
  1.1068 +    hence "reduced lab (n + 1) ` f = {0..n}" unfolding as(2)[THEN sym] apply- apply(rule set_ext) unfolding image_iff by auto
  1.1069 +    moreover guess s using as(1)[unfolded simplex_top_face[OF assms(1) allp,THEN sym]] .. then guess a ..
  1.1070 +    ultimately show "\<exists>s a. ksimplex p (n + 1) s \<and>
  1.1071 +      a \<in> s \<and> f = s - {a} \<and> reduced lab (n + 1) ` f = {0..n} \<and> ((\<exists>j\<in>{1..n + 1}. \<forall>x\<in>f. x j = 0) \<or> (\<exists>j\<in>{1..n + 1}. \<forall>x\<in>f. x j = p))" (is ?ex)
  1.1072 +      apply(rule_tac x=s in exI,rule_tac x=a in exI) unfolding complete_face_top[OF *] using allp as(1) by auto
  1.1073 +  next fix f assume as:"\<exists>s a. ksimplex p (n + 1) s \<and>
  1.1074 +      a \<in> s \<and> f = s - {a} \<and> reduced lab (n + 1) ` f = {0..n} \<and> ((\<exists>j\<in>{1..n + 1}. \<forall>x\<in>f. x j = 0) \<or> (\<exists>j\<in>{1..n + 1}. \<forall>x\<in>f. x j = p))" (is ?ex)
  1.1075 +    then guess s .. then guess a apply-apply(erule exE,(erule conjE)+) . note sa=this
  1.1076 +    { fix x assume "x\<in>f" hence "reduced lab (n + 1) x \<in> reduced lab (n + 1) ` f" by auto
  1.1077 +      hence "reduced lab (n + 1) x < n + 1" using sa(4) by auto 
  1.1078 +      hence "reduced lab (n + 1) x = reduced lab n x" apply-apply(rule reduced_labelling_Suc)
  1.1079 +	using reduced_labelling(1) by auto }
  1.1080 +    thus part1:"reduced lab n ` f = {0..n}" unfolding sa(4)[THEN sym] apply-apply(rule set_ext) unfolding image_iff by auto
  1.1081 +    have *:"\<forall>x\<in>f. x (n + 1) = p" proof(cases "\<exists>j\<in>{1..n + 1}. \<forall>x\<in>f. x j = 0")
  1.1082 +      case True then guess j .. hence "\<And>x. x\<in>f \<Longrightarrow> reduced lab (n + 1) x \<noteq> j - 1" apply-apply(rule reduced_labelling_0) apply assumption
  1.1083 +	apply(rule assms(2)[rule_format]) using sa(1)[unfolded ksimplex_def] unfolding sa by auto moreover
  1.1084 +      have "j - 1 \<in> {0..n}" using `j\<in>{1..n+1}` by auto
  1.1085 +      ultimately have False unfolding sa(4)[THEN sym] unfolding image_iff by fastsimp thus ?thesis by auto next
  1.1086 +      case False hence "\<exists>j\<in>{1..n + 1}. \<forall>x\<in>f. x j = p" using sa(5) by fastsimp then guess j .. note j=this
  1.1087 +      thus ?thesis proof(cases "j = n+1")
  1.1088 +	case False hence *:"j\<in>{1..n}" using j by auto
  1.1089 +	hence "\<And>x. x\<in>f \<Longrightarrow> reduced lab n x < j" apply(rule reduced_labelling_1) proof- fix x assume "x\<in>f"
  1.1090 +	  hence "lab x j = 1" apply-apply(rule assms(3)[rule_format,OF j(1)]) 
  1.1091 +	    using sa(1)[unfolded ksimplex_def] using j unfolding sa by auto thus "lab x j \<noteq> 0" by auto qed
  1.1092 +	moreover have "j\<in>{0..n}" using * by auto
  1.1093 +	ultimately have False unfolding part1[THEN sym] using * unfolding image_iff by auto thus ?thesis by auto qed auto qed 
  1.1094 +    thus "ksimplex p n f" using as unfolding simplex_top_face[OF assms(1) *,THEN sym] by auto qed qed
  1.1095 +
  1.1096 +lemma kuhn_induction_Suc:
  1.1097 +  assumes "0 < p" "\<forall>x. \<forall>j\<in>{1..Suc n}. (\<forall>j. x j \<le> p) \<and> (x j = 0) \<longrightarrow> (lab x j = 0)"
  1.1098 +                  "\<forall>x. \<forall>j\<in>{1..Suc n}. (\<forall>j. x j \<le> p) \<and> (x j = p) \<longrightarrow> (lab x j = 1)"
  1.1099 +        "odd (card {f. ksimplex p n f \<and> ((reduced lab n) ` f = {0..n})})"
  1.1100 +  shows "odd (card {s. ksimplex p (Suc n) s \<and>((reduced lab (Suc n)) `  s = {0..Suc n})})"
  1.1101 +  using assms unfolding Suc_eq_plus1 by(rule kuhn_induction)
  1.1102 +
  1.1103 +subsection {* And so we get the final combinatorial result. *}
  1.1104 +
  1.1105 +lemma ksimplex_0: "ksimplex p 0 s \<longleftrightarrow> s = {(\<lambda>x. p)}" (is "?l = ?r") proof
  1.1106 +  assume l:?l guess a using ksimplexD(3)[OF l, unfolded add_0] unfolding card_1_exists .. note a=this
  1.1107 +  have "a = (\<lambda>x. p)" using ksimplexD(5)[OF l, rule_format, OF a(1)] by(rule,auto) thus ?r using a by auto next
  1.1108 +  assume r:?r show ?l unfolding r ksimplex_eq by auto qed
  1.1109 +
  1.1110 +lemma reduce_labelling_0[simp]: "reduced lab 0 x = 0" apply(rule reduced_labelling_unique) by auto
  1.1111 +
  1.1112 +lemma kuhn_combinatorial:
  1.1113 +  assumes "0 < p" "\<forall>x j. (\<forall>j. x(j) \<le> p) \<and> 1 \<le> j \<and> j \<le> n \<and> (x j = 0) \<longrightarrow> (lab x j = 0)"
  1.1114 +  "\<forall>x j. (\<forall>j. x(j) \<le> p) \<and> 1 \<le> j \<and> j \<le> n  \<and> (x j = p) \<longrightarrow> (lab x j = 1)"
  1.1115 +  shows " odd (card {s. ksimplex p n s \<and> ((reduced lab n) ` s = {0..n})})" using assms proof(induct n)
  1.1116 +  let ?M = "\<lambda>n. {s. ksimplex p n s \<and> ((reduced lab n) ` s = {0..n})}"
  1.1117 +  { case 0 have *:"?M 0 = {{(\<lambda>x. p)}}" unfolding ksimplex_0 by auto show ?case unfolding * by auto }
  1.1118 +  case (Suc n) have "odd (card (?M n))" apply(rule Suc(1)[OF Suc(2)]) using Suc(3-) by auto
  1.1119 +  thus ?case apply-apply(rule kuhn_induction_Suc) using Suc(2-) by auto qed
  1.1120 +
  1.1121 +lemma kuhn_lemma: assumes "0 < (p::nat)" "0 < (n::nat)"
  1.1122 +  "\<forall>x. (\<forall>i\<in>{1..n}. x i \<le> p) \<longrightarrow> (\<forall>i\<in>{1..n}. (label x i = (0::nat)) \<or> (label x i = 1))"
  1.1123 +  "\<forall>x. (\<forall>i\<in>{1..n}. x i \<le> p) \<longrightarrow> (\<forall>i\<in>{1..n}. (x i = 0) \<longrightarrow> (label x i = 0))"
  1.1124 +  "\<forall>x. (\<forall>i\<in>{1..n}. x i \<le> p) \<longrightarrow> (\<forall>i\<in>{1..n}. (x i = p) \<longrightarrow> (label x i = 1))"
  1.1125 +  obtains q where "\<forall>i\<in>{1..n}. q i < p"
  1.1126 +  "\<forall>i\<in>{1..n}. \<exists>r s. (\<forall>j\<in>{1..n}. q(j) \<le> r(j) \<and> r(j) \<le> q(j) + 1) \<and>
  1.1127 +                               (\<forall>j\<in>{1..n}. q(j) \<le> s(j) \<and> s(j) \<le> q(j) + 1) \<and>
  1.1128 +                               ~(label r i = label s i)" proof-
  1.1129 +  let ?A = "{s. ksimplex p n s \<and> reduced label n ` s = {0..n}}" have "n\<noteq>0" using assms by auto
  1.1130 +  have conjD:"\<And>P Q. P \<and> Q \<Longrightarrow> P" "\<And>P Q. P \<and> Q \<Longrightarrow> Q" by auto
  1.1131 +  have "odd (card ?A)" apply(rule kuhn_combinatorial[of p n label]) using assms by auto
  1.1132 +  hence "card ?A \<noteq> 0" apply-apply(rule ccontr) by auto hence "?A \<noteq> {}" unfolding card_eq_0_iff by auto
  1.1133 +  then obtain s where "s\<in>?A" by auto note s=conjD[OF this[unfolded mem_Collect_eq]]
  1.1134 +  guess a b apply(rule ksimplex_extrema_strong[OF s(1) `n\<noteq>0`]) . note ab=this
  1.1135 +  show ?thesis apply(rule that[of a]) proof(rule_tac[!] ballI) fix i assume "i\<in>{1..n}"
  1.1136 +    hence "a i + 1 \<le> p" apply-apply(rule order_trans[of _ "b i"]) apply(subst ab(5)[THEN spec[where x=i]])
  1.1137 +      using s(1)[unfolded ksimplex_def] defer apply- apply(erule conjE)+ apply(drule_tac bspec[OF _ ab(2)])+ by auto
  1.1138 +    thus "a i < p" by auto
  1.1139 +    case goal2 hence "i \<in> reduced label n ` s" using s by auto then guess u unfolding image_iff .. note u=this
  1.1140 +    from goal2 have "i - 1 \<in> reduced label n ` s" using s by auto then guess v unfolding image_iff .. note v=this
  1.1141 +    show ?case apply(rule_tac x=u in exI, rule_tac x=v in exI) apply(rule conjI) defer apply(rule conjI) defer 2 proof(rule_tac[1-2] ballI)
  1.1142 +      show "label u i \<noteq> label v i" using reduced_labelling[of label n u] reduced_labelling[of label n v]
  1.1143 +        unfolding u(2)[THEN sym] v(2)[THEN sym] using goal2 by auto
  1.1144 +      fix j assume j:"j\<in>{1..n}" show "a j \<le> u j \<and> u j \<le> a j + 1" "a j \<le> v j \<and> v j \<le> a j + 1"
  1.1145 +        using conjD[OF ab(4)[rule_format, OF u(1)]] and conjD[OF ab(4)[rule_format, OF v(1)]] apply- 
  1.1146 +        apply(drule_tac[!] kle_imp_pointwise)+ apply(erule_tac[!] x=j in allE)+ unfolding ab(5)[rule_format] using j
  1.1147 +        by auto qed qed qed
  1.1148 +
  1.1149 +subsection {* The main result for the unit cube. *}
  1.1150 +
  1.1151 +lemma kuhn_labelling_lemma':
  1.1152 +  assumes "(\<forall>x::nat\<Rightarrow>real. P x \<longrightarrow> P (f x))"  "\<forall>x. P x \<longrightarrow> (\<forall>i::nat. Q i \<longrightarrow> 0 \<le> x i \<and> x i \<le> 1)"
  1.1153 +  shows "\<exists>l. (\<forall>x i. l x i \<le> (1::nat)) \<and>
  1.1154 +             (\<forall>x i. P x \<and> Q i \<and> (x i = 0) \<longrightarrow> (l x i = 0)) \<and>
  1.1155 +             (\<forall>x i. P x \<and> Q i \<and> (x i = 1) \<longrightarrow> (l x i = 1)) \<and>
  1.1156 +             (\<forall>x i. P x \<and> Q i \<and> (l x i = 0) \<longrightarrow> x i \<le> f(x) i) \<and>
  1.1157 +             (\<forall>x i. P x \<and> Q i \<and> (l x i = 1) \<longrightarrow> f(x) i \<le> x i)" proof-
  1.1158 +  have and_forall_thm:"\<And>P Q. (\<forall>x. P x) \<and> (\<forall>x. Q x) \<longleftrightarrow> (\<forall>x. P x \<and> Q x)" by auto
  1.1159 +  have *:"\<forall>x y::real. 0 \<le> x \<and> x \<le> 1 \<and> 0 \<le> y \<and> y \<le> 1 \<longrightarrow> (x \<noteq> 1 \<and> x \<le> y \<or> x \<noteq> 0 \<and> y \<le> x)" by auto
  1.1160 +  show ?thesis unfolding and_forall_thm apply(subst choice_iff[THEN sym])+ proof(rule,rule) case goal1
  1.1161 +    let ?R = "\<lambda>y. (P x \<and> Q xa \<and> x xa = 0 \<longrightarrow> y = (0::nat)) \<and>
  1.1162 +        (P x \<and> Q xa \<and> x xa = 1 \<longrightarrow> y = 1) \<and> (P x \<and> Q xa \<and> y = 0 \<longrightarrow> x xa \<le> (f x) xa) \<and> (P x \<and> Q xa \<and> y = 1 \<longrightarrow> (f x) xa \<le> x xa)"
  1.1163 +    { assume "P x" "Q xa" hence "0 \<le> (f x) xa \<and> (f x) xa \<le> 1" using assms(2)[rule_format,of "f x" xa]
  1.1164 +        apply(drule_tac assms(1)[rule_format]) by auto }
  1.1165 +    hence "?R 0 \<or> ?R 1" by auto thus ?case by auto qed qed 
  1.1166 +
  1.1167 +lemma brouwer_cube: fixes f::"real^'n::finite \<Rightarrow> real^'n::finite"
  1.1168 +  assumes "continuous_on {0..1} f" "f ` {0..1} \<subseteq> {0..1}"
  1.1169 +  shows "\<exists>x\<in>{0..1}. f x = x" apply(rule ccontr) proof-
  1.1170 +  def n \<equiv> "CARD('n)" have n:"1 \<le> n" "0 < n" "n \<noteq> 0" unfolding n_def by auto
  1.1171 +  assume "\<not> (\<exists>x\<in>{0..1}. f x = x)" hence *:"\<not> (\<exists>x\<in>{0..1}. f x - x = 0)" by auto
  1.1172 +  guess d apply(rule brouwer_compactness_lemma[OF compact_interval _ *]) 
  1.1173 +    apply(rule continuous_on_intros assms)+ . note d=this[rule_format]
  1.1174 +  have *:"\<forall>x. x \<in> {0..1} \<longrightarrow> f x \<in> {0..1}"  "\<forall>x. x \<in> {0..1::real^'n} \<longrightarrow> (\<forall>i. True \<longrightarrow> 0 \<le> x $ i \<and> x $ i \<le> 1)"
  1.1175 +    using assms(2)[unfolded image_subset_iff Ball_def] unfolding mem_interval by auto
  1.1176 +  guess label using kuhn_labelling_lemma[OF *] apply-apply(erule exE,(erule conjE)+) . note label = this[rule_format]
  1.1177 +  have lem1:"\<forall>x\<in>{0..1}.\<forall>y\<in>{0..1}.\<forall>i. label x i \<noteq> label y i
  1.1178 +            \<longrightarrow> abs(f x $ i - x $ i) \<le> norm(f y - f x) + norm(y - x)" proof(rule,rule,rule,rule)
  1.1179 +    fix x y assume xy:"x\<in>{0..1::real^'n}" "y\<in>{0..1::real^'n}" fix i::'n assume i:"label x i \<noteq> label y i"
  1.1180 +    have *:"\<And>x y fx fy::real. (x \<le> fx \<and> fy \<le> y \<or> fx \<le> x \<and> y \<le> fy)
  1.1181 +             \<Longrightarrow> abs(fx - x) \<le> abs(fy - fx) + abs(y - x)" by auto
  1.1182 +    have "\<bar>(f x - x) $ i\<bar> \<le> abs((f y - f x)$i) + abs((y - x)$i)" unfolding vector_minus_component
  1.1183 +      apply(rule *) apply(cases "label x i = 0") apply(rule disjI1,rule) prefer 3 proof(rule disjI2,rule)
  1.1184 +      assume lx:"label x i = 0" hence ly:"label y i = 1" using i label(1)[of y i] by auto
  1.1185 +      show "x $ i \<le> f x $ i" apply(rule label(4)[rule_format]) using xy lx by auto
  1.1186 +      show "f y $ i \<le> y $ i" apply(rule label(5)[rule_format]) using xy ly by auto next
  1.1187 +      assume "label x i \<noteq> 0" hence l:"label x i = 1" "label y i = 0"
  1.1188 +        using i label(1)[of x i] label(1)[of y i] by auto
  1.1189 +      show "f x $ i \<le> x $ i" apply(rule label(5)[rule_format]) using xy l  by auto
  1.1190 +      show "y $ i \<le> f y $ i" apply(rule label(4)[rule_format]) using xy l  by auto qed 
  1.1191 +    also have "\<dots> \<le> norm (f y - f x) + norm (y - x)" apply(rule add_mono) by(rule component_le_norm)+
  1.1192 +    finally show "\<bar>f x $ i - x $ i\<bar> \<le> norm (f y - f x) + norm (y - x)" unfolding vector_minus_component . qed
  1.1193 +  have "\<exists>e>0. \<forall>x\<in>{0..1}. \<forall>y\<in>{0..1}. \<forall>z\<in>{0..1}. \<forall>i.
  1.1194 +    norm(x - z) < e \<and> norm(y - z) < e \<and> label x i \<noteq> label y i \<longrightarrow> abs((f(z) - z)$i) < d / (real n)" proof-
  1.1195 +    have d':"d / real n / 8 > 0" apply(rule divide_pos_pos)+ using d(1) unfolding n_def by auto
  1.1196 +    have *:"uniformly_continuous_on {0..1} f" by(rule compact_uniformly_continuous[OF assms(1) compact_interval])
  1.1197 +    guess e using *[unfolded uniformly_continuous_on_def,rule_format,OF d'] apply-apply(erule exE,(erule conjE)+) .
  1.1198 +    note e=this[rule_format,unfolded vector_dist_norm]
  1.1199 +    show ?thesis apply(rule_tac x="min (e/2) (d/real n/8)" in exI) apply(rule) defer
  1.1200 +      apply(rule,rule,rule,rule,rule) apply(erule conjE)+ proof-
  1.1201 +      show "0 < min (e / 2) (d / real n / 8)" using d' e by auto
  1.1202 +      fix x y z i assume as:"x \<in> {0..1}" "y \<in> {0..1}" "z \<in> {0..1}" "norm (x - z) < min (e / 2) (d / real n / 8)"
  1.1203 +        "norm (y - z) < min (e / 2) (d / real n / 8)" "label x i \<noteq> label y i"
  1.1204 +      have *:"\<And>z fz x fx n1 n2 n3 n4 d4 d::real. abs(fx - x) \<le> n1 + n2 \<Longrightarrow> abs(fx - fz) \<le> n3 \<Longrightarrow> abs(x - z) \<le> n4 \<Longrightarrow>
  1.1205 +        n1 < d4 \<Longrightarrow> n2 < 2 * d4 \<Longrightarrow> n3 < d4 \<Longrightarrow> n4 < d4 \<Longrightarrow> (8 * d4 = d) \<Longrightarrow> abs(fz - z) < d" by auto
  1.1206 +      show "\<bar>(f z - z) $ i\<bar> < d / real n" unfolding vector_minus_component proof(rule *)
  1.1207 +        show "\<bar>f x $ i - x $ i\<bar> \<le> norm (f y -f x) + norm (y - x)" apply(rule lem1[rule_format]) using as by auto
  1.1208 +        show "\<bar>f x $ i - f z $ i\<bar> \<le> norm (f x - f z)" "\<bar>x $ i - z $ i\<bar> \<le> norm (x - z)"
  1.1209 +          unfolding vector_minus_component[THEN sym] by(rule component_le_norm)+
  1.1210 +        have tria:"norm (y - x) \<le> norm (y - z) + norm (x - z)" using dist_triangle[of y x z,unfolded vector_dist_norm]
  1.1211 +          unfolding norm_minus_commute by auto
  1.1212 +        also have "\<dots> < e / 2 + e / 2" apply(rule add_strict_mono) using as(4,5) by auto
  1.1213 +        finally show "norm (f y - f x) < d / real n / 8" apply- apply(rule e(2)) using as by auto
  1.1214 +        have "norm (y - z) + norm (x - z) < d / real n / 8 + d / real n / 8" apply(rule add_strict_mono) using as by auto
  1.1215 +        thus "norm (y - x) < 2 * (d / real n / 8)" using tria by auto
  1.1216 +        show "norm (f x - f z) < d / real n / 8" apply(rule e(2)) using as e(1) by auto qed(insert as, auto) qed qed
  1.1217 +  then guess e apply-apply(erule exE,(erule conjE)+) . note e=this[rule_format] 
  1.1218 +  guess p using real_arch_simple[of "1 + real n / e"] .. note p=this
  1.1219 +  have "1 + real n / e > 0" apply(rule add_pos_pos) defer apply(rule divide_pos_pos) using e(1) n by auto
  1.1220 +  hence "p > 0" using p by auto
  1.1221 +  guess b using ex_bij_betw_nat_finite_1[OF finite_UNIV[where 'a='n]] .. note b=this
  1.1222 +  def b' \<equiv> "inv_into {1..n} b"
  1.1223 +  have b':"bij_betw b' UNIV {1..n}" using bij_betw_inv_into[OF b] unfolding b'_def n_def by auto
  1.1224 +  have bb'[simp]:"\<And>i. b (b' i) = i" unfolding b'_def apply(rule f_inv_into_f) unfolding n_def using b  
  1.1225 +    unfolding bij_betw_def by auto
  1.1226 +  have b'b[simp]:"\<And>i. i\<in>{1..n} \<Longrightarrow> b' (b i) = i" unfolding b'_def apply(rule inv_into_f_eq)
  1.1227 +    using b unfolding n_def bij_betw_def by auto
  1.1228 +  have *:"\<And>x::nat. x=0 \<or> x=1 \<longleftrightarrow> x\<le>1" by auto
  1.1229 +  have q1:"0 < p" "0 < n"  "\<forall>x. (\<forall>i\<in>{1..n}. x i \<le> p) \<longrightarrow>
  1.1230 +    (\<forall>i\<in>{1..n}. (label (\<chi> i. real (x (b' i)) / real p) \<circ> b) i = 0 \<or> (label (\<chi> i. real (x (b' i)) / real p) \<circ> b) i = 1)"
  1.1231 +    unfolding * using `p>0` `n>0` using label(1) by auto
  1.1232 +  have q2:"\<forall>x. (\<forall>i\<in>{1..n}. x i \<le> p) \<longrightarrow> (\<forall>i\<in>{1..n}. x i = 0 \<longrightarrow> (label (\<chi> i. real (x (b' i)) / real p) \<circ> b) i = 0)"
  1.1233 +    "\<forall>x. (\<forall>i\<in>{1..n}. x i \<le> p) \<longrightarrow> (\<forall>i\<in>{1..n}. x i = p \<longrightarrow> (label (\<chi> i. real (x (b' i)) / real p) \<circ> b) i = 1)"
  1.1234 +    apply(rule,rule,rule,rule) defer proof(rule,rule,rule,rule) fix x i 
  1.1235 +    assume as:"\<forall>i\<in>{1..n}. x i \<le> p" "i \<in> {1..n}"
  1.1236 +    { assume "x i = p \<or> x i = 0" 
  1.1237 +      have "(\<chi> i. real (x (b' i)) / real p) \<in> {0..1}" unfolding mem_interval Cart_lambda_beta proof(rule,rule)
  1.1238 +        fix j::'n have j:"b' j \<in> {1..n}" using b' unfolding n_def bij_betw_def by auto
  1.1239 +        show "0 $ j \<le> real (x (b' j)) / real p" unfolding zero_index
  1.1240 +          apply(rule divide_nonneg_pos) using `p>0` using as(1)[rule_format,OF j] by auto
  1.1241 +        show "real (x (b' j)) / real p \<le> 1 $ j" unfolding one_index divide_le_eq_1
  1.1242 +          using as(1)[rule_format,OF j] by auto qed } note cube=this
  1.1243 +    { assume "x i = p" thus "(label (\<chi> i. real (x (b' i)) / real p) \<circ> b) i = 1" unfolding o_def
  1.1244 +        apply-apply(rule label(3)) using cube using as `p>0` by auto }
  1.1245 +    { assume "x i = 0" thus "(label (\<chi> i. real (x (b' i)) / real p) \<circ> b) i = 0" unfolding o_def
  1.1246 +        apply-apply(rule label(2)) using cube using as `p>0` by auto } qed
  1.1247 +  guess q apply(rule kuhn_lemma[OF q1 q2]) . note q=this
  1.1248 +  def z \<equiv> "\<chi> i. real (q (b' i)) / real p"
  1.1249 +  have "\<exists>i. d / real n \<le> abs((f z - z)$i)" proof(rule ccontr)
  1.1250 +    have "\<forall>i. q (b' i) \<in> {0..<p}" using q(1) b'[unfolded bij_betw_def] by auto 
  1.1251 +    hence "\<forall>i. q (b' i) \<in> {0..p}" apply-apply(rule,erule_tac x=i in allE) by auto
  1.1252 +    hence "z\<in>{0..1}" unfolding z_def mem_interval unfolding one_index zero_index Cart_lambda_beta
  1.1253 +      apply-apply(rule,rule) apply(rule divide_nonneg_pos) using `p>0` unfolding divide_le_eq_1 by auto
  1.1254 +    hence d_fz_z:"d \<le> norm (f z - z)" apply(drule_tac d) .
  1.1255 +    case goal1 hence as:"\<forall>i. \<bar>f z $ i - z $ i\<bar> < d / real n" using `n>0` by(auto simp add:not_le)
  1.1256 +    have "norm (f z - z) \<le> (\<Sum>i\<in>UNIV. \<bar>f z $ i - z $ i\<bar>)" unfolding vector_minus_component[THEN sym] by(rule norm_le_l1)
  1.1257 +    also have "\<dots> < (\<Sum>(i::'n)\<in>UNIV. d / real n)" apply(rule setsum_strict_mono) using as by auto
  1.1258 +    also have "\<dots> = d" unfolding real_eq_of_nat n_def using n by auto
  1.1259 +    finally show False using d_fz_z by auto qed then guess i .. note i=this
  1.1260 +  have *:"b' i \<in> {1..n}" using b'[unfolded bij_betw_def] by auto
  1.1261 +  guess r using q(2)[rule_format,OF *] .. then guess s apply-apply(erule exE,(erule conjE)+) . note rs=this[rule_format]
  1.1262 +  have b'_im:"\<And>i. b' i \<in> {1..n}" using b' unfolding bij_betw_def by auto
  1.1263 +  def r' \<equiv> "\<chi> i. real (r (b' i)) / real p"
  1.1264 +  have "\<And>i. r (b' i) \<le> p" apply(rule order_trans) apply(rule rs(1)[OF b'_im,THEN conjunct2])
  1.1265 +    using q(1)[rule_format,OF b'_im] by(auto simp add: Suc_le_eq)
  1.1266 +  hence "r' \<in> {0..1::real^'n}" unfolding r'_def mem_interval Cart_lambda_beta one_index zero_index
  1.1267 +    apply-apply(rule,rule,rule divide_nonneg_pos)
  1.1268 +    using rs(1)[OF b'_im] q(1)[rule_format,OF b'_im] `p>0` by auto
  1.1269 +  def s' \<equiv> "\<chi> i. real (s (b' i)) / real p"
  1.1270 +  have "\<And>i. s (b' i) \<le> p" apply(rule order_trans) apply(rule rs(2)[OF b'_im,THEN conjunct2])
  1.1271 +    using q(1)[rule_format,OF b'_im] by(auto simp add: Suc_le_eq)
  1.1272 +  hence "s' \<in> {0..1::real^'n}" unfolding s'_def mem_interval Cart_lambda_beta one_index zero_index
  1.1273 +    apply-apply(rule,rule,rule divide_nonneg_pos)
  1.1274 +    using rs(1)[OF b'_im] q(1)[rule_format,OF b'_im] `p>0` by auto
  1.1275 +  have "z\<in>{0..1}" unfolding z_def mem_interval Cart_lambda_beta one_index zero_index 
  1.1276 +    apply(rule,rule,rule divide_nonneg_pos) using q(1)[rule_format,OF b'_im] `p>0` by(auto intro:less_imp_le)
  1.1277 +  have *:"\<And>x. 1 + real x = real (Suc x)" by auto
  1.1278 +  { have "(\<Sum>i\<in>UNIV. \<bar>real (r (b' i)) - real (q (b' i))\<bar>) \<le> (\<Sum>(i::'n)\<in>UNIV. 1)" 
  1.1279 +      apply(rule setsum_mono) using rs(1)[OF b'_im] by(auto simp add:* field_simps)
  1.1280 +    also have "\<dots> < e * real p" using p `e>0` `p>0` unfolding n_def real_of_nat_def
  1.1281 +      by(auto simp add:field_simps)
  1.1282 +    finally have "(\<Sum>i\<in>UNIV. \<bar>real (r (b' i)) - real (q (b' i))\<bar>) < e * real p" . } moreover
  1.1283 +  { have "(\<Sum>i\<in>UNIV. \<bar>real (s (b' i)) - real (q (b' i))\<bar>) \<le> (\<Sum>(i::'n)\<in>UNIV. 1)" 
  1.1284 +      apply(rule setsum_mono) using rs(2)[OF b'_im] by(auto simp add:* field_simps)
  1.1285 +    also have "\<dots> < e * real p" using p `e>0` `p>0` unfolding n_def real_of_nat_def
  1.1286 +      by(auto simp add:field_simps)
  1.1287 +    finally have "(\<Sum>i\<in>UNIV. \<bar>real (s (b' i)) - real (q (b' i))\<bar>) < e * real p" . } ultimately
  1.1288 +  have "norm (r' - z) < e" "norm (s' - z) < e" unfolding r'_def s'_def z_def apply-
  1.1289 +    apply(rule_tac[!] le_less_trans[OF norm_le_l1]) using `p>0`
  1.1290 +    by(auto simp add:field_simps setsum_divide_distrib[THEN sym])
  1.1291 +  hence "\<bar>(f z - z) $ i\<bar> < d / real n" apply-apply(rule e(2)[OF `r'\<in>{0..1}` `s'\<in>{0..1}` `z\<in>{0..1}`])
  1.1292 +    using rs(3) unfolding r'_def[symmetric] s'_def[symmetric] o_def bb' by auto
  1.1293 +  thus False using i by auto qed
  1.1294 +
  1.1295 +subsection {* Retractions. *}
  1.1296 +
  1.1297 +definition "retraction s t (r::real^'n::finite\<Rightarrow>real^'n) \<longleftrightarrow>
  1.1298 +  t \<subseteq> s \<and> continuous_on s r \<and> (r ` s \<subseteq> t) \<and> (\<forall>x\<in>t. r x = x)"
  1.1299 +
  1.1300 +definition retract_of (infixl "retract'_of" 12) where
  1.1301 +  "(t retract_of s) \<longleftrightarrow> (\<exists>r. retraction s t r)"
  1.1302 +
  1.1303 +lemma retraction_idempotent: "retraction s t r \<Longrightarrow> x \<in> s \<Longrightarrow>  r(r x) = r x"
  1.1304 +  unfolding retraction_def by auto
  1.1305 +
  1.1306 +subsection {*preservation of fixpoints under (more general notion of) retraction. *}
  1.1307 +
  1.1308 +lemma invertible_fixpoint_property: fixes s::"(real^'n::finite) set" and t::"(real^'m::finite) set" 
  1.1309 +  assumes "continuous_on t i" "i ` t \<subseteq> s" "continuous_on s r" "r ` s \<subseteq> t" "\<forall>y\<in>t. r (i y) = y"
  1.1310 +  "\<forall>f. continuous_on s f \<and> f ` s \<subseteq> s \<longrightarrow> (\<exists>x\<in>s. f x = x)" "continuous_on t g" "g ` t \<subseteq> t"
  1.1311 +  obtains y where "y\<in>t" "g y = y" proof-
  1.1312 +  have "\<exists>x\<in>s. (i \<circ> g \<circ> r) x = x" apply(rule assms(6)[rule_format],rule)
  1.1313 +    apply(rule continuous_on_compose assms)+ apply((rule continuous_on_subset)?,rule assms)+
  1.1314 +    using assms(2,4,8) unfolding image_compose by(auto,blast)
  1.1315 +    then guess x .. note x = this hence *:"g (r x) \<in> t" using assms(4,8) by auto
  1.1316 +    have "r ((i \<circ> g \<circ> r) x) = r x" using x by auto
  1.1317 +    thus ?thesis apply(rule_tac that[of "r x"]) using x unfolding o_def
  1.1318 +      unfolding assms(5)[rule_format,OF *] using assms(4) by auto qed
  1.1319 +
  1.1320 +lemma homeomorphic_fixpoint_property:
  1.1321 +  fixes s::"(real^'n::finite) set" and t::"(real^'m::finite) set" assumes "s homeomorphic t"
  1.1322 +  shows "(\<forall>f. continuous_on s f \<and> f ` s \<subseteq> s \<longrightarrow> (\<exists>x\<in>s. f x = x)) \<longleftrightarrow>
  1.1323 +         (\<forall>g. continuous_on t g \<and> g ` t \<subseteq> t \<longrightarrow> (\<exists>y\<in>t. g y = y))" proof-
  1.1324 +  guess r using assms[unfolded homeomorphic_def homeomorphism_def] .. then guess i ..
  1.1325 +  thus ?thesis apply- apply rule apply(rule_tac[!] allI impI)+ 
  1.1326 +    apply(rule_tac g=g in invertible_fixpoint_property[of t i s r]) prefer 10
  1.1327 +    apply(rule_tac g=f in invertible_fixpoint_property[of s r t i]) by auto qed
  1.1328 +
  1.1329 +lemma retract_fixpoint_property:
  1.1330 +  assumes "t retract_of s"  "\<forall>f. continuous_on s f \<and> f ` s \<subseteq> s \<longrightarrow> (\<exists>x\<in>s. f x = x)"  "continuous_on t g" "g ` t \<subseteq> t"
  1.1331 +  obtains y where "y \<in> t" "g y = y" proof- guess h using assms(1) unfolding retract_of_def .. 
  1.1332 +  thus ?thesis unfolding retraction_def apply-
  1.1333 +    apply(rule invertible_fixpoint_property[OF continuous_on_id _ _ _ _ assms(2), of t h g]) prefer 7
  1.1334 +    apply(rule_tac y=y in that) using assms by auto qed
  1.1335 +
  1.1336 +subsection {*So the Brouwer theorem for any set with nonempty interior. *}
  1.1337 +
  1.1338 +lemma brouwer_weak: fixes f::"real^'n::finite \<Rightarrow> real^'n::finite"
  1.1339 +  assumes "compact s" "convex s" "interior s \<noteq> {}" "continuous_on s f" "f ` s \<subseteq> s"
  1.1340 +  obtains x where "x \<in> s" "f x = x" proof-
  1.1341 +  have *:"interior {0..1::real^'n} \<noteq> {}" unfolding interior_closed_interval interval_eq_empty by auto
  1.1342 +  have *:"{0..1::real^'n} homeomorphic s" using homeomorphic_convex_compact[OF convex_interval(1) compact_interval * assms(2,1,3)] .
  1.1343 +  have "\<forall>f. continuous_on {0..1} f \<and> f ` {0..1} \<subseteq> {0..1} \<longrightarrow> (\<exists>x\<in>{0..1::real^'n}. f x = x)" using brouwer_cube by auto
  1.1344 +  thus ?thesis unfolding homeomorphic_fixpoint_property[OF *] apply(erule_tac x=f in allE)
  1.1345 +    apply(erule impE) defer apply(erule bexE) apply(rule_tac x=y in that) using assms by auto qed
  1.1346 +
  1.1347 +subsection {* And in particular for a closed ball. *}
  1.1348 +
  1.1349 +lemma brouwer_ball: fixes f::"real^'n::finite \<Rightarrow> real^'n::finite"
  1.1350 +  assumes "0 < e" "continuous_on (cball a e) f" "f ` (cball a e) \<subseteq> (cball a e)"
  1.1351 +  obtains x where "x \<in> cball a e" "f x = x"
  1.1352 +  using brouwer_weak[OF compact_cball convex_cball,of a e f] unfolding interior_cball ball_eq_empty
  1.1353 +  using assms by auto
  1.1354 +
  1.1355 +text {*Still more general form; could derive this directly without using the 
  1.1356 +  rather involved HOMEOMORPHIC_CONVEX_COMPACT theorem, just using
  1.1357 +  a scaling and translation to put the set inside the unit cube. *}
  1.1358 +
  1.1359 +lemma brouwer: fixes f::"real^'n::finite \<Rightarrow> real^'n::finite"
  1.1360 +  assumes "compact s" "convex s" "s \<noteq> {}" "continuous_on s f" "f ` s \<subseteq> s"
  1.1361 +  obtains x where "x \<in> s" "f x = x" proof-
  1.1362 +  have "\<exists>e>0. s \<subseteq> cball 0 e" using compact_imp_bounded[OF assms(1)] unfolding bounded_pos
  1.1363 +    apply(erule_tac exE,rule_tac x=b in exI) by(auto simp add: vector_dist_norm) 
  1.1364 +  then guess e apply-apply(erule exE,(erule conjE)+) . note e=this
  1.1365 +  have "\<exists>x\<in> cball 0 e. (f \<circ> closest_point s) x = x"
  1.1366 +    apply(rule_tac brouwer_ball[OF e(1), of 0 "f \<circ> closest_point s"]) apply(rule continuous_on_compose )
  1.1367 +    apply(rule continuous_on_closest_point[OF assms(2) compact_imp_closed[OF assms(1)] assms(3)])
  1.1368 +    apply(rule continuous_on_subset[OF assms(4)])
  1.1369 +    using closest_point_in_set[OF compact_imp_closed[OF assms(1)] assms(3)] apply - defer
  1.1370 +    using assms(5)[unfolded subset_eq] using e(2)[unfolded subset_eq mem_cball] by(auto simp add:vector_dist_norm)
  1.1371 +  then guess x .. note x=this
  1.1372 +  have *:"closest_point s x = x" apply(rule closest_point_self) 
  1.1373 +    apply(rule assms(5)[unfolded subset_eq,THEN bspec[where x="x"],unfolded image_iff])
  1.1374 +    apply(rule_tac x="closest_point s x" in bexI) using x unfolding o_def
  1.1375 +    using closest_point_in_set[OF compact_imp_closed[OF assms(1)] assms(3), of x] by auto
  1.1376 +  show thesis apply(rule_tac x="closest_point s x" in that) unfolding x(2)[unfolded o_def]
  1.1377 +    apply(rule closest_point_in_set[OF compact_imp_closed[OF assms(1)] assms(3)]) using * by auto qed
  1.1378 +
  1.1379 +text {*So we get the no-retraction theorem. *}                                      
  1.1380 +
  1.1381 +lemma no_retraction_cball: assumes "0 < e" 
  1.1382 +  shows "\<not> (frontier(cball a e) retract_of (cball a e))" proof case goal1
  1.1383 +  have *:"\<And>xa. a - (2 *\<^sub>R a - xa) = -(a - xa)" using scaleR_left_distrib[of 1 1 a] by auto
  1.1384 +  guess x apply(rule retract_fixpoint_property[OF goal1, of "\<lambda>x. scaleR 2 a - x"])
  1.1385 +    apply(rule,rule,erule conjE) apply(rule brouwer_ball[OF assms]) apply assumption+
  1.1386 +    apply(rule_tac x=x in bexI) apply assumption+ apply(rule continuous_on_intros)+
  1.1387 +    unfolding frontier_cball subset_eq Ball_def image_iff apply(rule,rule,erule bexE)
  1.1388 +    unfolding vector_dist_norm apply(simp add: * norm_minus_commute) . note x = this
  1.1389 +  hence "scaleR 2 a = scaleR 1 x + scaleR 1 x" by(auto simp add:group_simps)
  1.1390 +  hence "a = x" unfolding scaleR_left_distrib[THEN sym] by auto 
  1.1391 +  thus False using x using assms by auto qed
  1.1392 +
  1.1393 +subsection {*Bijections between intervals. *}
  1.1394 +
  1.1395 +definition "interval_bij = (\<lambda> (a,b) (u,v) (x::real^'n::finite).
  1.1396 +    (\<chi> i. u$i + (x$i - a$i) / (b$i - a$i) * (v$i - u$i))::real^'n)"
  1.1397 +
  1.1398 +lemma interval_bij_affine:
  1.1399 + "interval_bij (a,b) (u,v) = (\<lambda>x. (\<chi> i. (v$i - u$i) / (b$i - a$i) * x$i) +
  1.1400 +            (\<chi> i. u$i - (v$i - u$i) / (b$i - a$i) * a$i))"
  1.1401 +  apply rule unfolding Cart_eq interval_bij_def vector_component_simps
  1.1402 +  by(auto simp add:group_simps field_simps add_divide_distrib[THEN sym]) 
  1.1403 +
  1.1404 +lemma continuous_interval_bij:
  1.1405 +  "continuous (at x) (interval_bij (a,b::real^'n::finite) (u,v))" 
  1.1406 +  unfolding interval_bij_affine apply(rule continuous_intros)
  1.1407 +    apply(rule linear_continuous_at) unfolding linear_conv_bounded_linear[THEN sym]
  1.1408 +    unfolding linear_def unfolding Cart_eq unfolding Cart_lambda_beta defer
  1.1409 +    apply(rule continuous_intros) by(auto simp add:field_simps add_divide_distrib[THEN sym])
  1.1410 +
  1.1411 +lemma continuous_on_interval_bij: "continuous_on s (interval_bij (a,b) (u,v))"
  1.1412 +  apply(rule continuous_at_imp_continuous_on) by(rule, rule continuous_interval_bij)
  1.1413 +
  1.1414 +(** move this **)
  1.1415 +lemma divide_nonneg_nonneg:assumes "a \<ge> 0" "b \<ge> 0" shows "0 \<le> a / (b::real)"
  1.1416 +  apply(cases "b=0") defer apply(rule divide_nonneg_pos) using assms by auto
  1.1417 +
  1.1418 +lemma in_interval_interval_bij: assumes "x \<in> {a..b}" "{u..v} \<noteq> {}"
  1.1419 +  shows "interval_bij (a,b) (u,v) x \<in> {u..v::real^'n::finite}" 
  1.1420 +  unfolding interval_bij_def split_conv mem_interval Cart_lambda_beta proof(rule,rule) 
  1.1421 +  fix i::'n have "{a..b} \<noteq> {}" using assms by auto
  1.1422 +  hence *:"a$i \<le> b$i" "u$i \<le> v$i" using assms(2) unfolding interval_eq_empty not_ex apply-
  1.1423 +    apply(erule_tac[!] x=i in allE)+ by auto
  1.1424 +  have x:"a$i\<le>x$i" "x$i\<le>b$i" using assms(1)[unfolded mem_interval] by auto
  1.1425 +  have "0 \<le> (x $ i - a $ i) / (b $ i - a $ i) * (v $ i - u $ i)"
  1.1426 +    apply(rule mult_nonneg_nonneg) apply(rule divide_nonneg_nonneg)
  1.1427 +    using * x by(auto simp add:field_simps)
  1.1428 +  thus "u $ i \<le> u $ i + (x $ i - a $ i) / (b $ i - a $ i) * (v $ i - u $ i)" using * by auto
  1.1429 +  have "((x $ i - a $ i) / (b $ i - a $ i)) * (v $ i - u $ i) \<le> 1 * (v $ i - u $ i)"
  1.1430 +    apply(rule mult_right_mono) unfolding divide_le_eq_1 using * x by auto
  1.1431 +  thus "u $ i + (x $ i - a $ i) / (b $ i - a $ i) * (v $ i - u $ i) \<le> v $ i" using * by auto qed
  1.1432 +
  1.1433 +lemma interval_bij_bij: assumes "\<forall>i. a$i < b$i \<and> u$i < v$i" 
  1.1434 +  shows "interval_bij (a,b) (u,v) (interval_bij (u,v) (a,b) x) = x"
  1.1435 +  unfolding interval_bij_def split_conv Cart_eq Cart_lambda_beta
  1.1436 +  apply(rule,insert assms,erule_tac x=i in allE) by auto
  1.1437 +
  1.1438 +subsection {*Fashoda meet theorem. *}
  1.1439 +
  1.1440 +lemma infnorm_2: "infnorm (x::real^2) = max (abs(x$1)) (abs(x$2))"
  1.1441 +  unfolding infnorm_def UNIV_2 apply(rule Sup_eq) by auto
  1.1442 +
  1.1443 +lemma infnorm_eq_1_2: "infnorm (x::real^2) = 1 \<longleftrightarrow>
  1.1444 +        (abs(x$1) \<le> 1 \<and> abs(x$2) \<le> 1 \<and> (x$1 = -1 \<or> x$1 = 1 \<or> x$2 = -1 \<or> x$2 = 1))"
  1.1445 +  unfolding infnorm_2 by auto
  1.1446 +
  1.1447 +lemma infnorm_eq_1_imp: assumes "infnorm (x::real^2) = 1" shows "abs(x$1) \<le> 1" "abs(x$2) \<le> 1"
  1.1448 +  using assms unfolding infnorm_eq_1_2 by auto
  1.1449 +
  1.1450 +lemma fashoda_unit: fixes f g::"real^1 \<Rightarrow> real^2"
  1.1451 +  assumes "f ` {- 1..1} \<subseteq> {- 1..1}" "g ` {- 1..1} \<subseteq> {- 1..1}"
  1.1452 +  "continuous_on {- 1..1} f"  "continuous_on {- 1..1} g"
  1.1453 +  "f (- 1)$1 = - 1" "f 1$1 = 1" "g (- 1) $2 = -1" "g 1 $2 = 1"
  1.1454 +  shows "\<exists>s\<in>{- 1..1}. \<exists>t\<in>{- 1..1}. f s = g t" proof(rule ccontr)
  1.1455 +  case goal1 note as = this[unfolded bex_simps,rule_format]
  1.1456 +  def sqprojection \<equiv> "\<lambda>z::real^2. (inverse (infnorm z)) *\<^sub>R z" 
  1.1457 +  def negatex \<equiv> "\<lambda>x::real^2. (vector [-(x$1), x$2])::real^2" 
  1.1458 +  have lem1:"\<forall>z::real^2. infnorm(negatex z) = infnorm z"
  1.1459 +    unfolding negatex_def infnorm_2 vector_2 by auto
  1.1460 +  have lem2:"\<forall>z. z\<noteq>0 \<longrightarrow> infnorm(sqprojection z) = 1" unfolding sqprojection_def
  1.1461 +    unfolding infnorm_mul[unfolded smult_conv_scaleR] unfolding abs_inverse real_abs_infnorm
  1.1462 +    unfolding infnorm_eq_0[THEN sym] by auto
  1.1463 +  let ?F = "(\<lambda>w::real^2. (f \<circ> vec1 \<circ> (\<lambda>x. x$1)) w - (g \<circ> vec1 \<circ> (\<lambda>x. x$2)) w)"
  1.1464 +  have *:"\<And>i. vec1 ` (\<lambda>x::real^2. x $ i) ` {- 1..1} = {- 1..1::real^1}"
  1.1465 +    apply(rule set_ext) unfolding image_iff Bex_def mem_interval apply rule defer 
  1.1466 +    apply(rule_tac x="dest_vec1 x" in exI) apply rule apply(rule_tac x="vec (dest_vec1 x)" in exI)
  1.1467 +    by(auto simp add: dest_vec1_def[THEN sym]) 
  1.1468 +  { fix x assume "x \<in> (\<lambda>w. (f \<circ> vec1 \<circ> (\<lambda>x. x $ 1)) w - (g \<circ> vec1 \<circ> (\<lambda>x. x $ 2)) w) ` {- 1..1::real^2}"
  1.1469 +    then guess w unfolding image_iff .. note w = this
  1.1470 +    hence "x \<noteq> 0" using as[of "vec1 (w$1)" "vec1 (w$2)"] unfolding mem_interval by auto} note x0=this
  1.1471 +  have 21:"\<And>i::2. i\<noteq>1 \<Longrightarrow> i=2" using UNIV_2 by auto
  1.1472 +  have 1:"{- 1<..<1::real^2} \<noteq> {}" unfolding interval_eq_empty by auto
  1.1473 +  have 2:"continuous_on {- 1..1} (negatex \<circ> sqprojection \<circ> ?F)" apply(rule continuous_on_intros continuous_on_component continuous_on_vec1)+
  1.1474 +    prefer 2 apply(rule continuous_on_intros continuous_on_component continuous_on_vec1)+ unfolding *
  1.1475 +    apply(rule assms)+ apply(rule continuous_on_compose,subst sqprojection_def)
  1.1476 +    apply(rule continuous_on_mul ) apply(rule continuous_at_imp_continuous_on,rule) apply(rule continuous_at_inv[unfolded o_def])
  1.1477 +    apply(rule continuous_at_infnorm) unfolding infnorm_eq_0 defer apply(rule continuous_on_id) apply(rule linear_continuous_on) proof-
  1.1478 +    show "bounded_linear negatex" apply(rule bounded_linearI') unfolding Cart_eq proof(rule_tac[!] allI) fix i::2 and x y::"real^2" and c::real
  1.1479 +      show "negatex (x + y) $ i = (negatex x + negatex y) $ i" "negatex (c *s x) $ i = (c *s negatex x) $ i"
  1.1480 +	apply-apply(case_tac[!] "i\<noteq>1") prefer 3 apply(drule_tac[1-2] 21) 
  1.1481 +	unfolding negatex_def by(auto simp add:vector_2 ) qed qed(insert x0, auto)
  1.1482 +  have 3:"(negatex \<circ> sqprojection \<circ> ?F) ` {- 1..1} \<subseteq> {- 1..1}" unfolding subset_eq apply rule proof-
  1.1483 +    case goal1 then guess y unfolding image_iff .. note y=this have "?F y \<noteq> 0" apply(rule x0) using y(1) by auto
  1.1484 +    hence *:"infnorm (sqprojection (?F y)) = 1" unfolding y o_def apply- by(rule lem2[rule_format])
  1.1485 +    have "infnorm x = 1" unfolding *[THEN sym] y o_def by(rule lem1[rule_format])
  1.1486 +    thus "x\<in>{- 1..1}" unfolding mem_interval infnorm_2 apply- apply rule
  1.1487 +    proof-case goal1 thus ?case apply(cases "i=1") defer apply(drule 21) by auto qed qed
  1.1488 +  guess x apply(rule brouwer_weak[of "{- 1..1::real^2}" "negatex \<circ> sqprojection \<circ> ?F"])
  1.1489 +    apply(rule compact_interval convex_interval)+ unfolding interior_closed_interval
  1.1490 +    apply(rule 1 2 3)+ . note x=this
  1.1491 +  have "?F x \<noteq> 0" apply(rule x0) using x(1) by auto
  1.1492 +  hence *:"infnorm (sqprojection (?F x)) = 1" unfolding o_def by(rule lem2[rule_format])
  1.1493 +  have nx:"infnorm x = 1" apply(subst x(2)[THEN sym]) unfolding *[THEN sym] o_def by(rule lem1[rule_format])
  1.1494 +  have "\<forall>x i. x \<noteq> 0 \<longrightarrow> (0 < (sqprojection x)$i \<longleftrightarrow> 0 < x$i)"    "\<forall>x i. x \<noteq> 0 \<longrightarrow> ((sqprojection x)$i < 0 \<longleftrightarrow> x$i < 0)"
  1.1495 +    apply- apply(rule_tac[!] allI impI)+ proof- fix x::"real^2" and i::2 assume x:"x\<noteq>0"
  1.1496 +    have "inverse (infnorm x) > 0" using x[unfolded infnorm_pos_lt[THEN sym]] by auto
  1.1497 +    thus "(0 < sqprojection x $ i) = (0 < x $ i)"   "(sqprojection x $ i < 0) = (x $ i < 0)"
  1.1498 +      unfolding sqprojection_def vector_component_simps Cart_nth.scaleR real_scaleR_def
  1.1499 +      unfolding zero_less_mult_iff mult_less_0_iff by(auto simp add:field_simps) qed
  1.1500 +  note lem3 = this[rule_format]
  1.1501 +  have x1:"vec1 (x $ 1) \<in> {- 1..1::real^1}" "vec1 (x $ 2) \<in> {- 1..1::real^1}" using x(1) unfolding mem_interval by auto
  1.1502 +  hence nz:"f (vec1 (x $ 1)) - g (vec1 (x $ 2)) \<noteq> 0" unfolding right_minus_eq apply-apply(rule as) by auto
  1.1503 +  have "x $ 1 = -1 \<or> x $ 1 = 1 \<or> x $ 2 = -1 \<or> x $ 2 = 1" using nx unfolding infnorm_eq_1_2 by auto 
  1.1504 +  thus False proof- fix P Q R S 
  1.1505 +    presume "P \<or> Q \<or> R \<or> S" "P\<Longrightarrow>False" "Q\<Longrightarrow>False" "R\<Longrightarrow>False" "S\<Longrightarrow>False" thus False by auto
  1.1506 +  next assume as:"x$1 = 1" hence "vec1 (x$1) = 1" unfolding Cart_eq by auto
  1.1507 +    hence *:"f (vec1 (x $ 1)) $ 1 = 1" using assms(6) by auto
  1.1508 +    have "sqprojection (f (vec1 (x$1)) - g (vec1 (x$2))) $ 1 < 0"
  1.1509 +      using x(2)[unfolded o_def Cart_eq,THEN spec[where x=1]]
  1.1510 +      unfolding as negatex_def vector_2 by auto moreover
  1.1511 +    from x1 have "g (vec1 (x $ 2)) \<in> {- 1..1}" apply-apply(rule assms(2)[unfolded subset_eq,rule_format]) by auto
  1.1512 +    ultimately show False unfolding lem3[OF nz] vector_component_simps * mem_interval 
  1.1513 +      apply(erule_tac x=1 in allE) by auto 
  1.1514 +  next assume as:"x$1 = -1" hence "vec1 (x$1) = - 1" unfolding Cart_eq by auto
  1.1515 +    hence *:"f (vec1 (x $ 1)) $ 1 = - 1" using assms(5) by auto
  1.1516 +    have "sqprojection (f (vec1 (x$1)) - g (vec1 (x$2))) $ 1 > 0"
  1.1517 +      using x(2)[unfolded o_def Cart_eq,THEN spec[where x=1]]
  1.1518 +      unfolding as negatex_def vector_2 by auto moreover
  1.1519 +    from x1 have "g (vec1 (x $ 2)) \<in> {- 1..1}" apply-apply(rule assms(2)[unfolded subset_eq,rule_format]) by auto
  1.1520 +    ultimately show False unfolding lem3[OF nz] vector_component_simps * mem_interval 
  1.1521 +      apply(erule_tac x=1 in allE) by auto
  1.1522 +  next assume as:"x$2 = 1" hence "vec1 (x$2) = 1" unfolding Cart_eq by auto
  1.1523 +    hence *:"g (vec1 (x $ 2)) $ 2 = 1" using assms(8) by auto
  1.1524 +    have "sqprojection (f (vec1 (x$1)) - g (vec1 (x$2))) $ 2 > 0"
  1.1525 +      using x(2)[unfolded o_def Cart_eq,THEN spec[where x=2]]
  1.1526 +      unfolding as negatex_def vector_2 by auto moreover
  1.1527 +    from x1 have "f (vec1 (x $ 1)) \<in> {- 1..1}" apply-apply(rule assms(1)[unfolded subset_eq,rule_format]) by auto
  1.1528 +    ultimately show False unfolding lem3[OF nz] vector_component_simps * mem_interval 
  1.1529 +     apply(erule_tac x=2 in allE) by auto
  1.1530 + next assume as:"x$2 = -1" hence "vec1 (x$2) = - 1" unfolding Cart_eq by auto
  1.1531 +    hence *:"g (vec1 (x $ 2)) $ 2 = - 1" using assms(7) by auto
  1.1532 +    have "sqprojection (f (vec1 (x$1)) - g (vec1 (x$2))) $ 2 < 0"
  1.1533 +      using x(2)[unfolded o_def Cart_eq,THEN spec[where x=2]]
  1.1534 +      unfolding as negatex_def vector_2 by auto moreover
  1.1535 +    from x1 have "f (vec1 (x $ 1)) \<in> {- 1..1}" apply-apply(rule assms(1)[unfolded subset_eq,rule_format]) by auto
  1.1536 +    ultimately show False unfolding lem3[OF nz] vector_component_simps * mem_interval 
  1.1537 +      apply(erule_tac x=2 in allE) by auto qed(auto) qed
  1.1538 +
  1.1539 +lemma fashoda_unit_path: fixes f ::"real^1 \<Rightarrow> real^2" and g ::"real^1 \<Rightarrow> real^2"
  1.1540 +  assumes "path f" "path g" "path_image f \<subseteq> {- 1..1}" "path_image g \<subseteq> {- 1..1}"
  1.1541 +  "(pathstart f)$1 = -1" "(pathfinish f)$1 = 1"  "(pathstart g)$2 = -1" "(pathfinish g)$2 = 1"
  1.1542 +  obtains z where "z \<in> path_image f" "z \<in> path_image g" proof-
  1.1543 +  note assms=assms[unfolded path_def pathstart_def pathfinish_def path_image_def]
  1.1544 +  def iscale \<equiv> "\<lambda>z::real^1. inverse 2 *\<^sub>R (z + 1)"
  1.1545 +  have isc:"iscale ` {- 1..1} \<subseteq> {0..1}" unfolding iscale_def by(auto simp add:dest_vec1_add dest_vec1_neg)
  1.1546 +  have "\<exists>s\<in>{- 1..1}. \<exists>t\<in>{- 1..1}. (f \<circ> iscale) s = (g \<circ> iscale) t" proof(rule fashoda_unit) 
  1.1547 +    show "(f \<circ> iscale) ` {- 1..1} \<subseteq> {- 1..1}" "(g \<circ> iscale) ` {- 1..1} \<subseteq> {- 1..1}"
  1.1548 +      using isc and assms(3-4) unfolding image_compose by auto
  1.1549 +    have *:"continuous_on {- 1..1} iscale" unfolding iscale_def by(rule continuous_on_intros)+
  1.1550 +    show "continuous_on {- 1..1} (f \<circ> iscale)" "continuous_on {- 1..1} (g \<circ> iscale)"
  1.1551 +      apply-apply(rule_tac[!] continuous_on_compose[OF *]) apply(rule_tac[!] continuous_on_subset[OF _ isc])
  1.1552 +      by(rule assms)+ have *:"(1 / 2) *\<^sub>R (1 + (1::real^1)) = 1" unfolding Cart_eq by auto
  1.1553 +    show "(f \<circ> iscale) (- 1) $ 1 = - 1" "(f \<circ> iscale) 1 $ 1 = 1" "(g \<circ> iscale) (- 1) $ 2 = -1" "(g \<circ> iscale) 1 $ 2 = 1"
  1.1554 +      unfolding o_def iscale_def using assms by(auto simp add:*) qed
  1.1555 +  then guess s .. from this(2) guess t .. note st=this
  1.1556 +  show thesis apply(rule_tac z="f (iscale s)" in that)
  1.1557 +    using st `s\<in>{- 1..1}` unfolding o_def path_image_def image_iff apply-
  1.1558 +    apply(rule_tac x="iscale s" in bexI) prefer 3 apply(rule_tac x="iscale t" in bexI)
  1.1559 +    using isc[unfolded subset_eq, rule_format] by auto qed
  1.1560 +
  1.1561 +lemma fashoda: fixes b::"real^2"
  1.1562 +  assumes "path f" "path g" "path_image f \<subseteq> {a..b}" "path_image g \<subseteq> {a..b}"
  1.1563 +  "(pathstart f)$1 = a$1" "(pathfinish f)$1 = b$1"
  1.1564 +  "(pathstart g)$2 = a$2" "(pathfinish g)$2 = b$2"
  1.1565 +  obtains z where "z \<in> path_image f" "z \<in> path_image g" proof-
  1.1566 +  fix P Q S presume "P \<or> Q \<or> S" "P \<Longrightarrow> thesis" "Q \<Longrightarrow> thesis" "S \<Longrightarrow> thesis" thus thesis by auto
  1.1567 +next have "{a..b} \<noteq> {}" using assms(3) using path_image_nonempty by auto
  1.1568 +  hence "a \<le> b" unfolding interval_eq_empty vector_less_eq_def by(auto simp add: not_less)
  1.1569 +  thus "a$1 = b$1 \<or> a$2 = b$2 \<or> (a$1 < b$1 \<and> a$2 < b$2)" unfolding vector_less_eq_def forall_2 by auto
  1.1570 +next assume as:"a$1 = b$1" have "\<exists>z\<in>path_image g. z$2 = (pathstart f)$2" apply(rule connected_ivt_component)
  1.1571 +    apply(rule connected_path_image assms)+apply(rule pathstart_in_path_image,rule pathfinish_in_path_image)
  1.1572 +    unfolding assms using assms(3)[unfolded path_image_def subset_eq,rule_format,of "f 0"]
  1.1573 +    unfolding pathstart_def by(auto simp add: vector_less_eq_def) then guess z .. note z=this
  1.1574 +  have "z \<in> {a..b}" using z(1) assms(4) unfolding path_image_def by blast 
  1.1575 +  hence "z = f 0" unfolding Cart_eq forall_2 unfolding z(2) pathstart_def
  1.1576 +    using assms(3)[unfolded path_image_def subset_eq mem_interval,rule_format,of "f 0" 1]
  1.1577 +    unfolding mem_interval apply(erule_tac x=1 in allE) using as by auto
  1.1578 +  thus thesis apply-apply(rule that[OF _ z(1)]) unfolding path_image_def by auto
  1.1579 +next assume as:"a$2 = b$2" have "\<exists>z\<in>path_image f. z$1 = (pathstart g)$1" apply(rule connected_ivt_component)
  1.1580 +    apply(rule connected_path_image assms)+apply(rule pathstart_in_path_image,rule pathfinish_in_path_image)
  1.1581 +    unfolding assms using assms(4)[unfolded path_image_def subset_eq,rule_format,of "g 0"]
  1.1582 +    unfolding pathstart_def by(auto simp add: vector_less_eq_def) then guess z .. note z=this
  1.1583 +  have "z \<in> {a..b}" using z(1) assms(3) unfolding path_image_def by blast 
  1.1584 +  hence "z = g 0" unfolding Cart_eq forall_2 unfolding z(2) pathstart_def
  1.1585 +    using assms(4)[unfolded path_image_def subset_eq mem_interval,rule_format,of "g 0" 2]
  1.1586 +    unfolding mem_interval apply(erule_tac x=2 in allE) using as by auto
  1.1587 +  thus thesis apply-apply(rule that[OF z(1)]) unfolding path_image_def by auto
  1.1588 +next assume as:"a $ 1 < b $ 1 \<and> a $ 2 < b $ 2"
  1.1589 +  have int_nem:"{- 1..1::real^2} \<noteq> {}" unfolding interval_eq_empty by auto
  1.1590 +  guess z apply(rule fashoda_unit_path[of "interval_bij (a,b) (- 1,1) \<circ> f" "interval_bij (a,b) (- 1,1) \<circ> g"]) 
  1.1591 +    unfolding path_def path_image_def pathstart_def pathfinish_def
  1.1592 +    apply(rule_tac[1-2] continuous_on_compose) apply(rule assms[unfolded path_def] continuous_on_interval_bij)+
  1.1593 +    unfolding subset_eq apply(rule_tac[1-2] ballI)
  1.1594 +  proof- fix x assume "x \<in> (interval_bij (a, b) (- 1, 1) \<circ> f) ` {0..1}"
  1.1595 +    then guess y unfolding image_iff .. note y=this
  1.1596 +    show "x \<in> {- 1..1}" unfolding y o_def apply(rule in_interval_interval_bij)
  1.1597 +      using y(1) using assms(3)[unfolded path_image_def subset_eq] int_nem by auto
  1.1598 +  next fix x assume "x \<in> (interval_bij (a, b) (- 1, 1) \<circ> g) ` {0..1}"
  1.1599 +    then guess y unfolding image_iff .. note y=this
  1.1600 +    show "x \<in> {- 1..1}" unfolding y o_def apply(rule in_interval_interval_bij)
  1.1601 +      using y(1) using assms(4)[unfolded path_image_def subset_eq] int_nem by auto
  1.1602 +  next show "(interval_bij (a, b) (- 1, 1) \<circ> f) 0 $ 1 = -1"
  1.1603 +      "(interval_bij (a, b) (- 1, 1) \<circ> f) 1 $ 1 = 1"
  1.1604 +      "(interval_bij (a, b) (- 1, 1) \<circ> g) 0 $ 2 = -1"
  1.1605 +      "(interval_bij (a, b) (- 1, 1) \<circ> g) 1 $ 2 = 1" unfolding interval_bij_def Cart_lambda_beta vector_component_simps o_def split_conv
  1.1606 +      unfolding assms[unfolded pathstart_def pathfinish_def] using as by auto qed note z=this
  1.1607 +  from z(1) guess zf unfolding image_iff .. note zf=this
  1.1608 +  from z(2) guess zg unfolding image_iff .. note zg=this
  1.1609 +  have *:"\<forall>i. (- 1) $ i < (1::real^2) $ i \<and> a $ i < b $ i" unfolding forall_2 using as by auto
  1.1610 +  show thesis apply(rule_tac z="interval_bij (- 1,1) (a,b) z" in that)
  1.1611 +    apply(subst zf) defer apply(subst zg) unfolding o_def interval_bij_bij[OF *] path_image_def
  1.1612 +    using zf(1) zg(1) by auto qed
  1.1613 +
  1.1614 +subsection {*Some slightly ad hoc lemmas I use below*}
  1.1615 +
  1.1616 +lemma segment_vertical: fixes a::"real^2" assumes "a$1 = b$1"
  1.1617 +  shows "x \<in> closed_segment a b \<longleftrightarrow> (x$1 = a$1 \<and> x$1 = b$1 \<and>
  1.1618 +           (a$2 \<le> x$2 \<and> x$2 \<le> b$2 \<or> b$2 \<le> x$2 \<and> x$2 \<le> a$2))" (is "_ = ?R")
  1.1619 +proof- 
  1.1620 +  let ?L = "\<exists>u. (x $ 1 = (1 - u) * a $ 1 + u * b $ 1 \<and> x $ 2 = (1 - u) * a $ 2 + u * b $ 2) \<and> 0 \<le> u \<and> u \<le> 1"
  1.1621 +  { presume "?L \<Longrightarrow> ?R" "?R \<Longrightarrow> ?L" thus ?thesis unfolding closed_segment_def mem_Collect_eq
  1.1622 +      unfolding Cart_eq forall_2 smult_conv_scaleR[THEN sym] vector_component_simps by blast }
  1.1623 +  { assume ?L then guess u apply-apply(erule exE)apply(erule conjE)+ . note u=this
  1.1624 +    { fix b a assume "b + u * a > a + u * b"
  1.1625 +      hence "(1 - u) * b > (1 - u) * a" by(auto simp add:field_simps)
  1.1626 +      hence "b \<ge> a" apply(drule_tac mult_less_imp_less_left) using u by auto
  1.1627 +      hence "u * a \<le> u * b" apply-apply(rule mult_left_mono[OF _ u(3)]) 
  1.1628 +        using u(3-4) by(auto simp add:field_simps) } note * = this
  1.1629 +    { fix a b assume "u * b > u * a" hence "(1 - u) * a \<le> (1 - u) * b" apply-apply(rule mult_left_mono)
  1.1630 +        apply(drule mult_less_imp_less_left) using u by auto
  1.1631 +      hence "a + u * b \<le> b + u * a" by(auto simp add:field_simps) } note ** = this
  1.1632 +    thus ?R unfolding u assms using u by(auto simp add:field_simps not_le intro:* **) }
  1.1633 +  { assume ?R thus ?L proof(cases "x$2 = b$2")
  1.1634 +      case True thus ?L apply(rule_tac x="(x$2 - a$2) / (b$2 - a$2)" in exI) unfolding assms True
  1.1635 +        using `?R` by(auto simp add:field_simps)
  1.1636 +    next case False thus ?L apply(rule_tac x="1 - (x$2 - b$2) / (a$2 - b$2)" in exI) unfolding assms using `?R`
  1.1637 +        by(auto simp add:field_simps)
  1.1638 +    qed } qed
  1.1639 +
  1.1640 +lemma segment_horizontal: fixes a::"real^2" assumes "a$2 = b$2"
  1.1641 +  shows "x \<in> closed_segment a b \<longleftrightarrow> (x$2 = a$2 \<and> x$2 = b$2 \<and>
  1.1642 +           (a$1 \<le> x$1 \<and> x$1 \<le> b$1 \<or> b$1 \<le> x$1 \<and> x$1 \<le> a$1))" (is "_ = ?R")
  1.1643 +proof- 
  1.1644 +  let ?L = "\<exists>u. (x $ 1 = (1 - u) * a $ 1 + u * b $ 1 \<and> x $ 2 = (1 - u) * a $ 2 + u * b $ 2) \<and> 0 \<le> u \<and> u \<le> 1"
  1.1645 +  { presume "?L \<Longrightarrow> ?R" "?R \<Longrightarrow> ?L" thus ?thesis unfolding closed_segment_def mem_Collect_eq
  1.1646 +      unfolding Cart_eq forall_2 smult_conv_scaleR[THEN sym] vector_component_simps by blast }
  1.1647 +  { assume ?L then guess u apply-apply(erule exE)apply(erule conjE)+ . note u=this
  1.1648 +    { fix b a assume "b + u * a > a + u * b"
  1.1649 +      hence "(1 - u) * b > (1 - u) * a" by(auto simp add:field_simps)
  1.1650 +      hence "b \<ge> a" apply(drule_tac mult_less_imp_less_left) using u by auto
  1.1651 +      hence "u * a \<le> u * b" apply-apply(rule mult_left_mono[OF _ u(3)]) 
  1.1652 +        using u(3-4) by(auto simp add:field_simps) } note * = this
  1.1653 +    { fix a b assume "u * b > u * a" hence "(1 - u) * a \<le> (1 - u) * b" apply-apply(rule mult_left_mono)
  1.1654 +        apply(drule mult_less_imp_less_left) using u by auto
  1.1655 +      hence "a + u * b \<le> b + u * a" by(auto simp add:field_simps) } note ** = this
  1.1656 +    thus ?R unfolding u assms using u by(auto simp add:field_simps not_le intro:* **) }
  1.1657 +  { assume ?R thus ?L proof(cases "x$1 = b$1")
  1.1658 +      case True thus ?L apply(rule_tac x="(x$1 - a$1) / (b$1 - a$1)" in exI) unfolding assms True
  1.1659 +        using `?R` by(auto simp add:field_simps)
  1.1660 +    next case False thus ?L apply(rule_tac x="1 - (x$1 - b$1) / (a$1 - b$1)" in exI) unfolding assms using `?R`
  1.1661 +        by(auto simp add:field_simps)
  1.1662 +    qed } qed
  1.1663 +
  1.1664 +subsection {*useful Fashoda corollary pointed out to me by Tom Hales. *}
  1.1665 +
  1.1666 +lemma fashoda_interlace: fixes a::"real^2"
  1.1667 +  assumes "path f" "path g"
  1.1668 +  "path_image f \<subseteq> {a..b}" "path_image g \<subseteq> {a..b}"
  1.1669 +  "(pathstart f)$2 = a$2" "(pathfinish f)$2 = a$2"
  1.1670 +  "(pathstart g)$2 = a$2" "(pathfinish g)$2 = a$2"
  1.1671 +  "(pathstart f)$1 < (pathstart g)$1" "(pathstart g)$1 < (pathfinish f)$1"
  1.1672 +  "(pathfinish f)$1 < (pathfinish g)$1"
  1.1673 +  obtains z where "z \<in> path_image f" "z \<in> path_image g"
  1.1674 +proof-
  1.1675 +  have "{a..b} \<noteq> {}" using path_image_nonempty using assms(3) by auto
  1.1676 +  note ab=this[unfolded interval_eq_empty not_ex forall_2 not_less]
  1.1677 +  have "pathstart f \<in> {a..b}" "pathfinish f \<in> {a..b}" "pathstart g \<in> {a..b}" "pathfinish g \<in> {a..b}"
  1.1678 +    using pathstart_in_path_image pathfinish_in_path_image using assms(3-4) by auto
  1.1679 +  note startfin = this[unfolded mem_interval forall_2]
  1.1680 +  let ?P1 = "linepath (vector[a$1 - 2, a$2 - 2]) (vector[(pathstart f)$1,a$2 - 2]) +++
  1.1681 +     linepath(vector[(pathstart f)$1,a$2 - 2])(pathstart f) +++ f +++
  1.1682 +     linepath(pathfinish f)(vector[(pathfinish f)$1,a$2 - 2]) +++
  1.1683 +     linepath(vector[(pathfinish f)$1,a$2 - 2])(vector[b$1 + 2,a$2 - 2])" 
  1.1684 +  let ?P2 = "linepath(vector[(pathstart g)$1, (pathstart g)$2 - 3])(pathstart g) +++ g +++
  1.1685 +     linepath(pathfinish g)(vector[(pathfinish g)$1,a$2 - 1]) +++
  1.1686 +     linepath(vector[(pathfinish g)$1,a$2 - 1])(vector[b$1 + 1,a$2 - 1]) +++
  1.1687 +     linepath(vector[b$1 + 1,a$2 - 1])(vector[b$1 + 1,b$2 + 3])"
  1.1688 +  let ?a = "vector[a$1 - 2, a$2 - 3]"
  1.1689 +  let ?b = "vector[b$1 + 2, b$2 + 3]"
  1.1690 +  have P1P2:"path_image ?P1 = path_image (linepath (vector[a$1 - 2, a$2 - 2]) (vector[(pathstart f)$1,a$2 - 2])) \<union>
  1.1691 +      path_image (linepath(vector[(pathstart f)$1,a$2 - 2])(pathstart f)) \<union> path_image f \<union>
  1.1692 +      path_image (linepath(pathfinish f)(vector[(pathfinish f)$1,a$2 - 2])) \<union>
  1.1693 +      path_image (linepath(vector[(pathfinish f)$1,a$2 - 2])(vector[b$1 + 2,a$2 - 2]))"
  1.1694 +    "path_image ?P2 = path_image(linepath(vector[(pathstart g)$1, (pathstart g)$2 - 3])(pathstart g)) \<union> path_image g \<union>
  1.1695 +      path_image(linepath(pathfinish g)(vector[(pathfinish g)$1,a$2 - 1])) \<union>
  1.1696 +      path_image(linepath(vector[(pathfinish g)$1,a$2 - 1])(vector[b$1 + 1,a$2 - 1])) \<union>
  1.1697 +      path_image(linepath(vector[b$1 + 1,a$2 - 1])(vector[b$1 + 1,b$2 + 3]))" using assms(1-2)
  1.1698 +      by(auto simp add: pathstart_join pathfinish_join path_image_join path_image_linepath path_join path_linepath) 
  1.1699 +  have abab: "{a..b} \<subseteq> {?a..?b}" by(auto simp add:vector_less_eq_def forall_2 vector_2)
  1.1700 +  guess z apply(rule fashoda[of ?P1 ?P2 ?a ?b])
  1.1701 +    unfolding pathstart_join pathfinish_join pathstart_linepath pathfinish_linepath vector_2 proof-
  1.1702 +    show "path ?P1" "path ?P2" using assms by(auto simp add: pathstart_join pathfinish_join path_join)
  1.1703 +    have "path_image ?P1 \<subseteq> {?a .. ?b}" unfolding P1P2 path_image_linepath apply(rule Un_least)+ defer 3
  1.1704 +      apply(rule_tac[1-4] convex_interval(1)[unfolded convex_contains_segment,rule_format])
  1.1705 +      unfolding mem_interval forall_2 vector_2 using ab startfin abab assms(3)
  1.1706 +      using assms(9-) unfolding assms by(auto simp add:field_simps)
  1.1707 +    thus "path_image ?P1  \<subseteq> {?a .. ?b}" .
  1.1708 +    have "path_image ?P2 \<subseteq> {?a .. ?b}" unfolding P1P2 path_image_linepath apply(rule Un_least)+ defer 2
  1.1709 +      apply(rule_tac[1-4] convex_interval(1)[unfolded convex_contains_segment,rule_format])
  1.1710 +      unfolding mem_interval forall_2 vector_2 using ab startfin abab assms(4)
  1.1711 +      using assms(9-) unfolding assms  by(auto simp add:field_simps)
  1.1712 +    thus "path_image ?P2  \<subseteq> {?a .. ?b}" . 
  1.1713 +    show "a $ 1 - 2 = a $ 1 - 2"  "b $ 1 + 2 = b $ 1 + 2" "pathstart g $ 2 - 3 = a $ 2 - 3"  "b $ 2 + 3 = b $ 2 + 3"
  1.1714 +      by(auto simp add: assms)
  1.1715 +  qed note z=this[unfolded P1P2 path_image_linepath]
  1.1716 +  show thesis apply(rule that[of z]) proof-
  1.1717 +    have "(z \<in> closed_segment (vector [a $ 1 - 2, a $ 2 - 2]) (vector [pathstart f $ 1, a $ 2 - 2]) \<or>
  1.1718 +     z \<in> closed_segment (vector [pathstart f $ 1, a $ 2 - 2]) (pathstart f)) \<or>
  1.1719 +   z \<in> closed_segment (pathfinish f) (vector [pathfinish f $ 1, a $ 2 - 2]) \<or>
  1.1720 +  z \<in> closed_segment (vector [pathfinish f $ 1, a $ 2 - 2]) (vector [b $ 1 + 2, a $ 2 - 2]) \<Longrightarrow>
  1.1721 +  (((z \<in> closed_segment (vector [pathstart g $ 1, pathstart g $ 2 - 3]) (pathstart g)) \<or>
  1.1722 +    z \<in> closed_segment (pathfinish g) (vector [pathfinish g $ 1, a $ 2 - 1])) \<or>
  1.1723 +   z \<in> closed_segment (vector [pathfinish g $ 1, a $ 2 - 1]) (vector [b $ 1 + 1, a $ 2 - 1])) \<or>
  1.1724 +  z \<in> closed_segment (vector [b $ 1 + 1, a $ 2 - 1]) (vector [b $ 1 + 1, b $ 2 + 3]) \<Longrightarrow> False"
  1.1725 +      apply(simp only: segment_vertical segment_horizontal vector_2) proof- case goal1 note as=this
  1.1726 +      have "pathfinish f \<in> {a..b}" using assms(3) pathfinish_in_path_image[of f] by auto 
  1.1727 +      hence "1 + b $ 1 \<le> pathfinish f $ 1 \<Longrightarrow> False" unfolding mem_interval forall_2 by auto
  1.1728 +      hence "z$1 \<noteq> pathfinish f$1" using as(2) using assms ab by(auto simp add:field_simps)
  1.1729 +      moreover have "pathstart f \<in> {a..b}" using assms(3) pathstart_in_path_image[of f] by auto 
  1.1730 +      hence "1 + b $ 1 \<le> pathstart f $ 1 \<Longrightarrow> False" unfolding mem_interval forall_2 by auto
  1.1731 +      hence "z$1 \<noteq> pathstart f$1" using as(2) using assms ab by(auto simp add:field_simps)
  1.1732 +      ultimately have *:"z$2 = a$2 - 2" using goal1(1) by auto
  1.1733 +      have "z$1 \<noteq> pathfinish g$1" using as(2) using assms ab by(auto simp add:field_simps *)
  1.1734 +      moreover have "pathstart g \<in> {a..b}" using assms(4) pathstart_in_path_image[of g] by auto 
  1.1735 +      note this[unfolded mem_interval forall_2]
  1.1736 +      hence "z$1 \<noteq> pathstart g$1" using as(1) using assms ab by(auto simp add:field_simps *)
  1.1737 +      ultimately have "a $ 2 - 1 \<le> z $ 2 \<and> z $ 2 \<le> b $ 2 + 3 \<or> b $ 2 + 3 \<le> z $ 2 \<and> z $ 2 \<le> a $ 2 - 1"
  1.1738 +        using as(2) unfolding * assms by(auto simp add:field_simps)
  1.1739 +      thus False unfolding * using ab by auto
  1.1740 +    qed hence "z \<in> path_image f \<or> z \<in> path_image g" using z unfolding Un_iff by blast
  1.1741 +    hence z':"z\<in>{a..b}" using assms(3-4) by auto
  1.1742 +    have "a $ 2 = z $ 2 \<Longrightarrow> (z $ 1 = pathstart f $ 1 \<or> z $ 1 = pathfinish f $ 1) \<Longrightarrow> (z = pathstart f \<or> z = pathfinish f)"
  1.1743 +      unfolding Cart_eq forall_2 assms by auto
  1.1744 +    with z' show "z\<in>path_image f" using z(1) unfolding Un_iff mem_interval forall_2 apply-
  1.1745 +      apply(simp only: segment_vertical segment_horizontal vector_2) unfolding assms by auto
  1.1746 +    have "a $ 2 = z $ 2 \<Longrightarrow> (z $ 1 = pathstart g $ 1 \<or> z $ 1 = pathfinish g $ 1) \<Longrightarrow> (z = pathstart g \<or> z = pathfinish g)"
  1.1747 +      unfolding Cart_eq forall_2 assms by auto
  1.1748 +    with z' show "z\<in>path_image g" using z(2) unfolding Un_iff mem_interval forall_2 apply-
  1.1749 +      apply(simp only: segment_vertical segment_horizontal vector_2) unfolding assms by auto
  1.1750 +  qed qed
  1.1751 +
  1.1752 +(** The Following still needs to be translated. Maybe I will do that later.
  1.1753 +
  1.1754 +(* ------------------------------------------------------------------------- *)
  1.1755 +(* Complement in dimension N >= 2 of set homeomorphic to any interval in     *)
  1.1756 +(* any dimension is (path-)connected. This naively generalizes the argument  *)
  1.1757 +(* in Ryuji Maehara's paper "The Jordan curve theorem via the Brouwer        *)
  1.1758 +(* fixed point theorem", American Mathematical Monthly 1984.                 *)
  1.1759 +(* ------------------------------------------------------------------------- *)
  1.1760 +
  1.1761 +let RETRACTION_INJECTIVE_IMAGE_INTERVAL = prove
  1.1762 + (`!p:real^M->real^N a b.
  1.1763 +        ~(interval[a,b] = {}) /\
  1.1764 +        p continuous_on interval[a,b] /\
  1.1765 +        (!x y. x IN interval[a,b] /\ y IN interval[a,b] /\ p x = p y ==> x = y)
  1.1766 +        ==> ?f. f continuous_on (:real^N) /\
  1.1767 +                IMAGE f (:real^N) SUBSET (IMAGE p (interval[a,b])) /\
  1.1768 +                (!x. x IN (IMAGE p (interval[a,b])) ==> f x = x)`,
  1.1769 +  REPEAT STRIP_TAC THEN
  1.1770 +  FIRST_ASSUM(MP_TAC o GEN_REWRITE_RULE I [INJECTIVE_ON_LEFT_INVERSE]) THEN
  1.1771 +  DISCH_THEN(X_CHOOSE_TAC `q:real^N->real^M`) THEN
  1.1772 +  SUBGOAL_THEN `(q:real^N->real^M) continuous_on
  1.1773 +                (IMAGE p (interval[a:real^M,b]))`
  1.1774 +  ASSUME_TAC THENL
  1.1775 +   [MATCH_MP_TAC CONTINUOUS_ON_INVERSE THEN ASM_REWRITE_TAC[COMPACT_INTERVAL];
  1.1776 +    ALL_TAC] THEN
  1.1777 +  MP_TAC(ISPECL [`q:real^N->real^M`;
  1.1778 +                 `IMAGE (p:real^M->real^N)
  1.1779 +                 (interval[a,b])`;
  1.1780 +                 `a:real^M`; `b:real^M`]
  1.1781 +        TIETZE_CLOSED_INTERVAL) THEN
  1.1782 +  ASM_SIMP_TAC[COMPACT_INTERVAL; COMPACT_CONTINUOUS_IMAGE;
  1.1783 +               COMPACT_IMP_CLOSED] THEN
  1.1784 +  ANTS_TAC THENL [ASM SET_TAC[]; ALL_TAC] THEN
  1.1785 +  DISCH_THEN(X_CHOOSE_THEN `r:real^N->real^M` STRIP_ASSUME_TAC) THEN
  1.1786 +  EXISTS_TAC `(p:real^M->real^N) o (r:real^N->real^M)` THEN
  1.1787 +  REWRITE_TAC[SUBSET; FORALL_IN_IMAGE; o_THM; IN_UNIV] THEN
  1.1788 +  CONJ_TAC THENL [ALL_TAC; ASM SET_TAC[]] THEN
  1.1789 +  MATCH_MP_TAC CONTINUOUS_ON_COMPOSE THEN ASM_REWRITE_TAC[] THEN
  1.1790 +  FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP(REWRITE_RULE[IMP_CONJ]
  1.1791 +        CONTINUOUS_ON_SUBSET)) THEN ASM SET_TAC[]);;
  1.1792 +
  1.1793 +let UNBOUNDED_PATH_COMPONENTS_COMPLEMENT_HOMEOMORPHIC_INTERVAL = prove
  1.1794 + (`!s:real^N->bool a b:real^M.
  1.1795 +        s homeomorphic (interval[a,b])
  1.1796 +        ==> !x. ~(x IN s) ==> ~bounded(path_component((:real^N) DIFF s) x)`,
  1.1797 +  REPEAT GEN_TAC THEN REWRITE_TAC[homeomorphic; homeomorphism] THEN
  1.1798 +  REWRITE_TAC[LEFT_IMP_EXISTS_THM] THEN
  1.1799 +  MAP_EVERY X_GEN_TAC [`p':real^N->real^M`; `p:real^M->real^N`] THEN
  1.1800 +  DISCH_TAC THEN
  1.1801 +  SUBGOAL_THEN
  1.1802 +   `!x y. x IN interval[a,b] /\ y IN interval[a,b] /\
  1.1803 +          (p:real^M->real^N) x = p y ==> x = y`
  1.1804 +  ASSUME_TAC THENL [ASM_MESON_TAC[]; ALL_TAC] THEN
  1.1805 +  FIRST_X_ASSUM(MP_TAC o funpow 4 CONJUNCT2) THEN
  1.1806 +  DISCH_THEN(CONJUNCTS_THEN2 (SUBST1_TAC o SYM) ASSUME_TAC) THEN
  1.1807 +  ASM_CASES_TAC `interval[a:real^M,b] = {}` THEN
  1.1808 +  ASM_REWRITE_TAC[IMAGE_CLAUSES; DIFF_EMPTY; PATH_COMPONENT_UNIV;
  1.1809 +                  NOT_BOUNDED_UNIV] THEN
  1.1810 +  ABBREV_TAC `s = (:real^N) DIFF (IMAGE p (interval[a:real^M,b]))` THEN
  1.1811 +  X_GEN_TAC `c:real^N` THEN REPEAT STRIP_TAC THEN
  1.1812 +  SUBGOAL_THEN `(c:real^N) IN s` ASSUME_TAC THENL [ASM SET_TAC[]; ALL_TAC] THEN
  1.1813 +  SUBGOAL_THEN `bounded((path_component s c) UNION
  1.1814 +                        (IMAGE (p:real^M->real^N) (interval[a,b])))`
  1.1815 +  MP_TAC THENL
  1.1816 +   [ASM_SIMP_TAC[BOUNDED_UNION; COMPACT_IMP_BOUNDED; COMPACT_IMP_BOUNDED;
  1.1817 +                 COMPACT_CONTINUOUS_IMAGE; COMPACT_INTERVAL];
  1.1818 +    ALL_TAC] THEN
  1.1819 +  DISCH_THEN(MP_TAC o SPEC `c:real^N` o MATCH_MP BOUNDED_SUBSET_BALL) THEN
  1.1820 +  REWRITE_TAC[UNION_SUBSET] THEN
  1.1821 +  DISCH_THEN(X_CHOOSE_THEN `B:real` STRIP_ASSUME_TAC) THEN
  1.1822 +  MP_TAC(ISPECL [`p:real^M->real^N`; `a:real^M`; `b:real^M`]
  1.1823 +    RETRACTION_INJECTIVE_IMAGE_INTERVAL) THEN
  1.1824 +  ASM_REWRITE_TAC[SUBSET; IN_UNIV] THEN
  1.1825 +  DISCH_THEN(X_CHOOSE_THEN `r:real^N->real^N` MP_TAC) THEN
  1.1826 +  DISCH_THEN(CONJUNCTS_THEN2 ASSUME_TAC
  1.1827 +   (CONJUNCTS_THEN2 MP_TAC ASSUME_TAC)) THEN
  1.1828 +  REWRITE_TAC[FORALL_IN_IMAGE; IN_UNIV] THEN DISCH_TAC THEN
  1.1829 +  ABBREV_TAC `q = \z:real^N. if z IN path_component s c then r(z) else z` THEN
  1.1830 +  SUBGOAL_THEN
  1.1831 +    `(q:real^N->real^N) continuous_on
  1.1832 +     (closure(path_component s c) UNION ((:real^N) DIFF (path_component s c)))`
  1.1833 +  MP_TAC THENL
  1.1834 +   [EXPAND_TAC "q" THEN MATCH_MP_TAC CONTINUOUS_ON_CASES THEN
  1.1835 +    REWRITE_TAC[CLOSED_CLOSURE; CONTINUOUS_ON_ID; GSYM OPEN_CLOSED] THEN
  1.1836 +    REPEAT CONJ_TAC THENL
  1.1837 +     [MATCH_MP_TAC OPEN_PATH_COMPONENT THEN EXPAND_TAC "s" THEN
  1.1838 +      ASM_SIMP_TAC[GSYM CLOSED_OPEN; COMPACT_IMP_CLOSED;
  1.1839 +                   COMPACT_CONTINUOUS_IMAGE; COMPACT_INTERVAL];
  1.1840 +      ASM_MESON_TAC[CONTINUOUS_ON_SUBSET; SUBSET_UNIV];
  1.1841 +      ALL_TAC] THEN
  1.1842 +    X_GEN_TAC `z:real^N` THEN
  1.1843 +    REWRITE_TAC[SET_RULE `~(z IN (s DIFF t) /\ z IN t)`] THEN
  1.1844 +    STRIP_TAC THEN FIRST_X_ASSUM MATCH_MP_TAC THEN
  1.1845 +    MP_TAC(ISPECL
  1.1846 +     [`path_component s (z:real^N)`; `path_component s (c:real^N)`]
  1.1847 +     OPEN_INTER_CLOSURE_EQ_EMPTY) THEN
  1.1848 +    ASM_REWRITE_TAC[GSYM DISJOINT; PATH_COMPONENT_DISJOINT] THEN ANTS_TAC THENL
  1.1849 +     [MATCH_MP_TAC OPEN_PATH_COMPONENT THEN EXPAND_TAC "s" THEN
  1.1850 +      ASM_SIMP_TAC[GSYM CLOSED_OPEN; COMPACT_IMP_CLOSED;
  1.1851 +                   COMPACT_CONTINUOUS_IMAGE; COMPACT_INTERVAL];
  1.1852 +      REWRITE_TAC[DISJOINT; EXTENSION; IN_INTER; NOT_IN_EMPTY] THEN
  1.1853 +      DISCH_THEN(MP_TAC o SPEC `z:real^N`) THEN ASM_REWRITE_TAC[] THEN
  1.1854 +      GEN_REWRITE_TAC (LAND_CONV o ONCE_DEPTH_CONV) [IN] THEN
  1.1855 +      REWRITE_TAC[PATH_COMPONENT_REFL_EQ] THEN ASM SET_TAC[]];
  1.1856 +    ALL_TAC] THEN
  1.1857 +  SUBGOAL_THEN
  1.1858 +   `closure(path_component s c) UNION ((:real^N) DIFF (path_component s c)) =
  1.1859 +    (:real^N)`
  1.1860 +  SUBST1_TAC THENL
  1.1861 +   [MATCH_MP_TAC(SET_RULE `s SUBSET t ==> t UNION (UNIV DIFF s) = UNIV`) THEN
  1.1862 +    REWRITE_TAC[CLOSURE_SUBSET];
  1.1863 +    DISCH_TAC] THEN
  1.1864 +  MP_TAC(ISPECL
  1.1865 +   [`(\x. &2 % c - x) o
  1.1866 +     (\x. c + B / norm(x - c) % (x - c)) o (q:real^N->real^N)`;
  1.1867 +    `cball(c:real^N,B)`]
  1.1868 +    BROUWER) THEN
  1.1869 +  REWRITE_TAC[NOT_IMP; GSYM CONJ_ASSOC; COMPACT_CBALL; CONVEX_CBALL] THEN
  1.1870 +  ASM_SIMP_TAC[CBALL_EQ_EMPTY; REAL_LT_IMP_LE; REAL_NOT_LT] THEN
  1.1871 +  SUBGOAL_THEN `!x. ~((q:real^N->real^N) x = c)` ASSUME_TAC THENL
  1.1872 +   [X_GEN_TAC `x:real^N` THEN EXPAND_TAC "q" THEN
  1.1873 +    REWRITE_TAC[NORM_EQ_0; VECTOR_SUB_EQ] THEN COND_CASES_TAC THEN
  1.1874 +    ASM SET_TAC[PATH_COMPONENT_REFL_EQ];
  1.1875 +    ALL_TAC] THEN
  1.1876 +  REPEAT CONJ_TAC THENL
  1.1877 +   [MATCH_MP_TAC CONTINUOUS_ON_COMPOSE THEN
  1.1878 +    SIMP_TAC[CONTINUOUS_ON_SUB; CONTINUOUS_ON_ID; CONTINUOUS_ON_CONST] THEN
  1.1879 +    MATCH_MP_TAC CONTINUOUS_ON_COMPOSE THEN CONJ_TAC THENL
  1.1880 +     [ASM_MESON_TAC[CONTINUOUS_ON_SUBSET; SUBSET_UNIV]; ALL_TAC] THEN
  1.1881 +    MATCH_MP_TAC CONTINUOUS_ON_ADD THEN REWRITE_TAC[CONTINUOUS_ON_CONST] THEN
  1.1882 +    MATCH_MP_TAC CONTINUOUS_ON_MUL THEN
  1.1883 +    SIMP_TAC[CONTINUOUS_ON_SUB; CONTINUOUS_ON_ID; CONTINUOUS_ON_CONST] THEN
  1.1884 +    REWRITE_TAC[o_DEF; real_div; LIFT_CMUL] THEN
  1.1885 +    MATCH_MP_TAC CONTINUOUS_ON_CMUL THEN
  1.1886 +    MATCH_MP_TAC(REWRITE_RULE[o_DEF] CONTINUOUS_ON_INV) THEN
  1.1887 +    ASM_REWRITE_TAC[FORALL_IN_IMAGE; NORM_EQ_0; VECTOR_SUB_EQ] THEN
  1.1888 +    SUBGOAL_THEN
  1.1889 +     `(\x:real^N. lift(norm(x - c))) = (lift o norm) o (\x. x - c)`
  1.1890 +    SUBST1_TAC THENL [REWRITE_TAC[o_DEF]; ALL_TAC] THEN
  1.1891 +    MATCH_MP_TAC CONTINUOUS_ON_COMPOSE THEN
  1.1892 +    ASM_SIMP_TAC[CONTINUOUS_ON_SUB; CONTINUOUS_ON_ID; CONTINUOUS_ON_CONST;
  1.1893 +                 CONTINUOUS_ON_LIFT_NORM];
  1.1894 +    REWRITE_TAC[SUBSET; FORALL_IN_IMAGE; IN_CBALL; o_THM; dist] THEN
  1.1895 +    X_GEN_TAC `x:real^N` THEN DISCH_TAC THEN
  1.1896 +    REWRITE_TAC[VECTOR_ARITH `c - (&2 % c - (c + x)) = x`] THEN
  1.1897 +    REWRITE_TAC[NORM_MUL; REAL_ABS_DIV; REAL_ABS_NORM] THEN
  1.1898 +    ASM_SIMP_TAC[REAL_DIV_RMUL; NORM_EQ_0; VECTOR_SUB_EQ] THEN
  1.1899 +    ASM_REAL_ARITH_TAC;
  1.1900 +    REWRITE_TAC[NOT_EXISTS_THM; TAUT `~(c /\ b) <=> c ==> ~b`] THEN
  1.1901 +    REWRITE_TAC[IN_CBALL; o_THM; dist] THEN
  1.1902 +    X_GEN_TAC `x:real^N` THEN DISCH_TAC THEN
  1.1903 +    REWRITE_TAC[VECTOR_ARITH `&2 % c - (c + x') = x <=> --x' = x - c`] THEN
  1.1904 +    ASM_CASES_TAC `(x:real^N) IN path_component s c` THENL
  1.1905 +     [MATCH_MP_TAC(NORM_ARITH `norm(y) < B /\ norm(x) = B ==> ~(--x = y)`) THEN
  1.1906 +      REWRITE_TAC[NORM_MUL; REAL_ABS_DIV; REAL_ABS_NORM] THEN
  1.1907 +      ASM_SIMP_TAC[REAL_DIV_RMUL; NORM_EQ_0; VECTOR_SUB_EQ] THEN
  1.1908 +      ASM_SIMP_TAC[REAL_ARITH `&0 < B ==> abs B = B`] THEN
  1.1909 +      UNDISCH_TAC `path_component s c SUBSET ball(c:real^N,B)` THEN
  1.1910 +      REWRITE_TAC[SUBSET; IN_BALL] THEN ASM_MESON_TAC[dist; NORM_SUB];
  1.1911 +      EXPAND_TAC "q" THEN REWRITE_TAC[] THEN ASM_REWRITE_TAC[] THEN
  1.1912 +      REWRITE_TAC[VECTOR_ARITH `--(c % x) = x <=> (&1 + c) % x = vec 0`] THEN
  1.1913 +      ASM_REWRITE_TAC[DE_MORGAN_THM; VECTOR_SUB_EQ; VECTOR_MUL_EQ_0] THEN
  1.1914 +      SUBGOAL_THEN `~(x:real^N = c)` ASSUME_TAC THENL
  1.1915 +       [ASM_MESON_TAC[PATH_COMPONENT_REFL; IN]; ALL_TAC] THEN
  1.1916 +      ASM_REWRITE_TAC[] THEN
  1.1917 +      MATCH_MP_TAC(REAL_ARITH `&0 < x ==> ~(&1 + x = &0)`) THEN
  1.1918 +      ASM_SIMP_TAC[REAL_LT_DIV; NORM_POS_LT; VECTOR_SUB_EQ]]]);;
  1.1919 +
  1.1920 +let PATH_CONNECTED_COMPLEMENT_HOMEOMORPHIC_INTERVAL = prove
  1.1921 + (`!s:real^N->bool a b:real^M.
  1.1922 +        2 <= dimindex(:N) /\ s homeomorphic interval[a,b]
  1.1923 +        ==> path_connected((:real^N) DIFF s)`,
  1.1924 +  REPEAT STRIP_TAC THEN REWRITE_TAC[PATH_CONNECTED_IFF_PATH_COMPONENT] THEN
  1.1925 +  FIRST_ASSUM(MP_TAC o MATCH_MP
  1.1926 +    UNBOUNDED_PATH_COMPONENTS_COMPLEMENT_HOMEOMORPHIC_INTERVAL) THEN
  1.1927 +  ASM_REWRITE_TAC[SET_RULE `~(x IN s) <=> x IN (UNIV DIFF s)`] THEN
  1.1928 +  ABBREV_TAC `t = (:real^N) DIFF s` THEN
  1.1929 +  DISCH_TAC THEN MAP_EVERY X_GEN_TAC [`x:real^N`; `y:real^N`] THEN
  1.1930 +  STRIP_TAC THEN FIRST_ASSUM(MP_TAC o MATCH_MP HOMEOMORPHIC_COMPACTNESS) THEN
  1.1931 +  REWRITE_TAC[COMPACT_INTERVAL] THEN
  1.1932 +  DISCH_THEN(MP_TAC o MATCH_MP COMPACT_IMP_BOUNDED) THEN
  1.1933 +  REWRITE_TAC[BOUNDED_POS; LEFT_IMP_EXISTS_THM] THEN
  1.1934 +  X_GEN_TAC `B:real` THEN STRIP_TAC THEN
  1.1935 +  SUBGOAL_THEN `(?u:real^N. u IN path_component t x /\ B < norm(u)) /\
  1.1936 +                (?v:real^N. v IN path_component t y /\ B < norm(v))`
  1.1937 +  STRIP_ASSUME_TAC THENL
  1.1938 +   [ASM_MESON_TAC[BOUNDED_POS; REAL_NOT_LE]; ALL_TAC] THEN
  1.1939 +  MATCH_MP_TAC PATH_COMPONENT_TRANS THEN EXISTS_TAC `u:real^N` THEN
  1.1940 +  CONJ_TAC THENL [ASM_MESON_TAC[IN]; ALL_TAC] THEN
  1.1941 +  MATCH_MP_TAC PATH_COMPONENT_SYM THEN
  1.1942 +  MATCH_MP_TAC PATH_COMPONENT_TRANS THEN EXISTS_TAC `v:real^N` THEN
  1.1943 +  CONJ_TAC THENL [ASM_MESON_TAC[IN]; ALL_TAC] THEN
  1.1944 +  MATCH_MP_TAC PATH_COMPONENT_OF_SUBSET THEN
  1.1945 +  EXISTS_TAC `(:real^N) DIFF cball(vec 0,B)` THEN CONJ_TAC THENL
  1.1946 +   [EXPAND_TAC "t" THEN MATCH_MP_TAC(SET_RULE
  1.1947 +     `s SUBSET t ==> (u DIFF t) SUBSET (u DIFF s)`) THEN
  1.1948 +    ASM_REWRITE_TAC[SUBSET; IN_CBALL_0];
  1.1949 +    MP_TAC(ISPEC `cball(vec 0:real^N,B)`
  1.1950 +       PATH_CONNECTED_COMPLEMENT_BOUNDED_CONVEX) THEN
  1.1951 +    ASM_REWRITE_TAC[BOUNDED_CBALL; CONVEX_CBALL] THEN
  1.1952 +    REWRITE_TAC[PATH_CONNECTED_IFF_PATH_COMPONENT] THEN
  1.1953 +    DISCH_THEN MATCH_MP_TAC THEN
  1.1954 +    ASM_REWRITE_TAC[IN_DIFF; IN_UNIV; IN_CBALL_0; REAL_NOT_LE]]);;
  1.1955 +
  1.1956 +(* ------------------------------------------------------------------------- *)
  1.1957 +(* In particular, apply all these to the special case of an arc.             *)
  1.1958 +(* ------------------------------------------------------------------------- *)
  1.1959 +
  1.1960 +let RETRACTION_ARC = prove
  1.1961 + (`!p. arc p
  1.1962 +       ==> ?f. f continuous_on (:real^N) /\
  1.1963 +               IMAGE f (:real^N) SUBSET path_image p /\
  1.1964 +               (!x. x IN path_image p ==> f x = x)`,
  1.1965 +  REWRITE_TAC[arc; path; path_image] THEN REPEAT STRIP_TAC THEN
  1.1966 +  MATCH_MP_TAC RETRACTION_INJECTIVE_IMAGE_INTERVAL THEN
  1.1967 +  ASM_REWRITE_TAC[INTERVAL_EQ_EMPTY_1; DROP_VEC; REAL_NOT_LT; REAL_POS]);;
  1.1968 +
  1.1969 +let PATH_CONNECTED_ARC_COMPLEMENT = prove
  1.1970 + (`!p. 2 <= dimindex(:N) /\ arc p
  1.1971 +       ==> path_connected((:real^N) DIFF path_image p)`,
  1.1972 +  REWRITE_TAC[arc; path] THEN REPEAT STRIP_TAC THEN SIMP_TAC[path_image] THEN
  1.1973 +  MP_TAC(ISPECL [`path_image p:real^N->bool`; `vec 0:real^1`; `vec 1:real^1`]
  1.1974 +    PATH_CONNECTED_COMPLEMENT_HOMEOMORPHIC_INTERVAL) THEN
  1.1975 +  ASM_REWRITE_TAC[path_image] THEN DISCH_THEN MATCH_MP_TAC THEN
  1.1976 +  ONCE_REWRITE_TAC[HOMEOMORPHIC_SYM] THEN
  1.1977 +  MATCH_MP_TAC HOMEOMORPHIC_COMPACT THEN
  1.1978 +  EXISTS_TAC `p:real^1->real^N` THEN ASM_REWRITE_TAC[COMPACT_INTERVAL]);;
  1.1979 +
  1.1980 +let CONNECTED_ARC_COMPLEMENT = prove
  1.1981 + (`!p. 2 <= dimindex(:N) /\ arc p
  1.1982 +       ==> connected((:real^N) DIFF path_image p)`,
  1.1983 +  SIMP_TAC[PATH_CONNECTED_ARC_COMPLEMENT; PATH_CONNECTED_IMP_CONNECTED]);; *)
  1.1984 +
  1.1985 +end 
  1.1986 +