src/HOL/Analysis/Polytope.thy
author paulson <lp15@cam.ac.uk>
Wed Jul 19 16:41:26 2017 +0100 (2017-07-19)
changeset 66287 005a30862ed0
parent 65680 378a2f11bec9
child 66297 d425bdf419f5
permissions -rw-r--r--
new material: Colinearity, convex sets, polytopes
     1 section \<open>Faces, Extreme Points, Polytopes, Polyhedra etc.\<close>
     2 
     3 text\<open>Ported from HOL Light by L C Paulson\<close>
     4 
     5 theory Polytope
     6 imports Cartesian_Euclidean_Space
     7 begin
     8 
     9 subsection \<open>Faces of a (usually convex) set\<close>
    10 
    11 definition face_of :: "['a::real_vector set, 'a set] \<Rightarrow> bool" (infixr "(face'_of)" 50)
    12   where
    13   "T face_of S \<longleftrightarrow>
    14         T \<subseteq> S \<and> convex T \<and>
    15         (\<forall>a \<in> S. \<forall>b \<in> S. \<forall>x \<in> T. x \<in> open_segment a b \<longrightarrow> a \<in> T \<and> b \<in> T)"
    16 
    17 lemma face_ofD: "\<lbrakk>T face_of S; x \<in> open_segment a b; a \<in> S; b \<in> S; x \<in> T\<rbrakk> \<Longrightarrow> a \<in> T \<and> b \<in> T"
    18   unfolding face_of_def by blast
    19 
    20 lemma face_of_translation_eq [simp]:
    21     "(op + a ` T face_of op + a ` S) \<longleftrightarrow> T face_of S"
    22 proof -
    23   have *: "\<And>a T S. T face_of S \<Longrightarrow> (op + a ` T face_of op + a ` S)"
    24     apply (simp add: face_of_def Ball_def, clarify)
    25     apply (drule open_segment_translation_eq [THEN iffD1])
    26     using inj_image_mem_iff inj_add_left apply metis
    27     done
    28   show ?thesis
    29     apply (rule iffI)
    30     apply (force simp: image_comp o_def dest: * [where a = "-a"])
    31     apply (blast intro: *)
    32     done
    33 qed
    34 
    35 lemma face_of_linear_image:
    36   assumes "linear f" "inj f"
    37     shows "(f ` c face_of f ` S) \<longleftrightarrow> c face_of S"
    38 by (simp add: face_of_def inj_image_subset_iff inj_image_mem_iff open_segment_linear_image assms)
    39 
    40 lemma face_of_refl: "convex S \<Longrightarrow> S face_of S"
    41   by (auto simp: face_of_def)
    42 
    43 lemma face_of_refl_eq: "S face_of S \<longleftrightarrow> convex S"
    44   by (auto simp: face_of_def)
    45 
    46 lemma empty_face_of [iff]: "{} face_of S"
    47   by (simp add: face_of_def)
    48 
    49 lemma face_of_empty [simp]: "S face_of {} \<longleftrightarrow> S = {}"
    50   by (meson empty_face_of face_of_def subset_empty)
    51 
    52 lemma face_of_trans [trans]: "\<lbrakk>S face_of T; T face_of u\<rbrakk> \<Longrightarrow> S face_of u"
    53   unfolding face_of_def by (safe; blast)
    54 
    55 lemma face_of_face: "T face_of S \<Longrightarrow> (f face_of T \<longleftrightarrow> f face_of S \<and> f \<subseteq> T)"
    56   unfolding face_of_def by (safe; blast)
    57 
    58 lemma face_of_subset: "\<lbrakk>F face_of S; F \<subseteq> T; T \<subseteq> S\<rbrakk> \<Longrightarrow> F face_of T"
    59   unfolding face_of_def by (safe; blast)
    60 
    61 lemma face_of_slice: "\<lbrakk>F face_of S; convex T\<rbrakk> \<Longrightarrow> (F \<inter> T) face_of (S \<inter> T)"
    62   unfolding face_of_def by (blast intro: convex_Int)
    63 
    64 lemma face_of_Int: "\<lbrakk>t1 face_of S; t2 face_of S\<rbrakk> \<Longrightarrow> (t1 \<inter> t2) face_of S"
    65   unfolding face_of_def by (blast intro: convex_Int)
    66 
    67 lemma face_of_Inter: "\<lbrakk>A \<noteq> {}; \<And>T. T \<in> A \<Longrightarrow> T face_of S\<rbrakk> \<Longrightarrow> (\<Inter> A) face_of S"
    68   unfolding face_of_def by (blast intro: convex_Inter)
    69 
    70 lemma face_of_Int_Int: "\<lbrakk>F face_of T; F' face_of t'\<rbrakk> \<Longrightarrow> (F \<inter> F') face_of (T \<inter> t')"
    71   unfolding face_of_def by (blast intro: convex_Int)
    72 
    73 lemma face_of_imp_subset: "T face_of S \<Longrightarrow> T \<subseteq> S"
    74   unfolding face_of_def by blast
    75 
    76 lemma face_of_imp_eq_affine_Int:
    77   fixes S :: "'a::euclidean_space set"
    78   assumes S: "convex S"  and T: "T face_of S"
    79   shows "T = (affine hull T) \<inter> S"
    80 proof -
    81   have "convex T" using T by (simp add: face_of_def)
    82   have *: False if x: "x \<in> affine hull T" and "x \<in> S" "x \<notin> T" and y: "y \<in> rel_interior T" for x y
    83   proof -
    84     obtain e where "e>0" and e: "cball y e \<inter> affine hull T \<subseteq> T"
    85       using y by (auto simp: rel_interior_cball)
    86     have "y \<noteq> x" "y \<in> S" "y \<in> T"
    87       using face_of_imp_subset rel_interior_subset T that by blast+
    88     then have zne: "\<And>u. \<lbrakk>u \<in> {0<..<1}; (1 - u) *\<^sub>R y + u *\<^sub>R x \<in> T\<rbrakk> \<Longrightarrow>  False"
    89       using \<open>x \<in> S\<close> \<open>x \<notin> T\<close> \<open>T face_of S\<close> unfolding face_of_def
    90       apply clarify
    91       apply (drule_tac x=x in bspec, assumption)
    92       apply (drule_tac x=y in bspec, assumption)
    93       apply (subst (asm) open_segment_commute)
    94       apply (force simp: open_segment_image_interval image_def)
    95       done
    96     have in01: "min (1/2) (e / norm (x - y)) \<in> {0<..<1}"
    97       using \<open>y \<noteq> x\<close> \<open>e > 0\<close> by simp
    98     show ?thesis
    99       apply (rule zne [OF in01])
   100       apply (rule e [THEN subsetD])
   101       apply (rule IntI)
   102         using \<open>y \<noteq> x\<close> \<open>e > 0\<close>
   103         apply (simp add: cball_def dist_norm algebra_simps)
   104         apply (simp add: Real_Vector_Spaces.scaleR_diff_right [symmetric] norm_minus_commute min_mult_distrib_right)
   105       apply (rule mem_affine [OF affine_affine_hull _ x])
   106       using \<open>y \<in> T\<close>  apply (auto simp: hull_inc)
   107       done
   108   qed
   109   show ?thesis
   110     apply (rule subset_antisym)
   111     using assms apply (simp add: hull_subset face_of_imp_subset)
   112     apply (cases "T={}", simp)
   113     apply (force simp: rel_interior_eq_empty [symmetric] \<open>convex T\<close> intro: *)
   114     done
   115 qed
   116 
   117 lemma face_of_imp_closed:
   118      fixes S :: "'a::euclidean_space set"
   119      assumes "convex S" "closed S" "T face_of S" shows "closed T"
   120   by (metis affine_affine_hull affine_closed closed_Int face_of_imp_eq_affine_Int assms)
   121 
   122 lemma face_of_Int_supporting_hyperplane_le_strong:
   123     assumes "convex(S \<inter> {x. a \<bullet> x = b})" and aleb: "\<And>x. x \<in> S \<Longrightarrow> a \<bullet> x \<le> b"
   124       shows "(S \<inter> {x. a \<bullet> x = b}) face_of S"
   125 proof -
   126   have *: "a \<bullet> u = a \<bullet> x" if "x \<in> open_segment u v" "u \<in> S" "v \<in> S" and b: "b = a \<bullet> x"
   127           for u v x
   128   proof (rule antisym)
   129     show "a \<bullet> u \<le> a \<bullet> x"
   130       using aleb \<open>u \<in> S\<close> \<open>b = a \<bullet> x\<close> by blast
   131   next
   132     obtain \<xi> where "b = a \<bullet> ((1 - \<xi>) *\<^sub>R u + \<xi> *\<^sub>R v)" "0 < \<xi>" "\<xi> < 1"
   133       using \<open>b = a \<bullet> x\<close> \<open>x \<in> open_segment u v\<close> in_segment
   134       by (auto simp: open_segment_image_interval split: if_split_asm)
   135     then have "b + \<xi> * (a \<bullet> u) \<le> a \<bullet> u + \<xi> * b"
   136       using aleb [OF \<open>v \<in> S\<close>] by (simp add: algebra_simps)
   137     then have "(1 - \<xi>) * b \<le> (1 - \<xi>) * (a \<bullet> u)"
   138       by (simp add: algebra_simps)
   139     then have "b \<le> a \<bullet> u"
   140       using \<open>\<xi> < 1\<close> by auto
   141     with b show "a \<bullet> x \<le> a \<bullet> u" by simp
   142   qed
   143   show ?thesis
   144     apply (simp add: face_of_def assms)
   145     using "*" open_segment_commute by blast
   146 qed
   147 
   148 lemma face_of_Int_supporting_hyperplane_ge_strong:
   149    "\<lbrakk>convex(S \<inter> {x. a \<bullet> x = b}); \<And>x. x \<in> S \<Longrightarrow> a \<bullet> x \<ge> b\<rbrakk>
   150     \<Longrightarrow> (S \<inter> {x. a \<bullet> x = b}) face_of S"
   151   using face_of_Int_supporting_hyperplane_le_strong [of S "-a" "-b"] by simp
   152 
   153 lemma face_of_Int_supporting_hyperplane_le:
   154     "\<lbrakk>convex S; \<And>x. x \<in> S \<Longrightarrow> a \<bullet> x \<le> b\<rbrakk> \<Longrightarrow> (S \<inter> {x. a \<bullet> x = b}) face_of S"
   155   by (simp add: convex_Int convex_hyperplane face_of_Int_supporting_hyperplane_le_strong)
   156 
   157 lemma face_of_Int_supporting_hyperplane_ge:
   158     "\<lbrakk>convex S; \<And>x. x \<in> S \<Longrightarrow> a \<bullet> x \<ge> b\<rbrakk> \<Longrightarrow> (S \<inter> {x. a \<bullet> x = b}) face_of S"
   159   by (simp add: convex_Int convex_hyperplane face_of_Int_supporting_hyperplane_ge_strong)
   160 
   161 lemma face_of_imp_convex: "T face_of S \<Longrightarrow> convex T"
   162   using face_of_def by blast
   163 
   164 lemma face_of_imp_compact:
   165     fixes S :: "'a::euclidean_space set"
   166     shows "\<lbrakk>convex S; compact S; T face_of S\<rbrakk> \<Longrightarrow> compact T"
   167   by (meson bounded_subset compact_eq_bounded_closed face_of_imp_closed face_of_imp_subset)
   168 
   169 lemma face_of_Int_subface:
   170      "\<lbrakk>A \<inter> B face_of A; A \<inter> B face_of B; C face_of A; D face_of B\<rbrakk>
   171       \<Longrightarrow> (C \<inter> D) face_of C \<and> (C \<inter> D) face_of D"
   172   by (meson face_of_Int_Int face_of_face inf_le1 inf_le2)
   173 
   174 lemma subset_of_face_of:
   175     fixes S :: "'a::real_normed_vector set"
   176     assumes "T face_of S" "u \<subseteq> S" "T \<inter> (rel_interior u) \<noteq> {}"
   177       shows "u \<subseteq> T"
   178 proof
   179   fix c
   180   assume "c \<in> u"
   181   obtain b where "b \<in> T" "b \<in> rel_interior u" using assms by auto
   182   then obtain e where "e>0" "b \<in> u" and e: "cball b e \<inter> affine hull u \<subseteq> u"
   183     by (auto simp: rel_interior_cball)
   184   show "c \<in> T"
   185   proof (cases "b=c")
   186     case True with \<open>b \<in> T\<close> show ?thesis by blast
   187   next
   188     case False
   189     define d where "d = b + (e / norm(b - c)) *\<^sub>R (b - c)"
   190     have "d \<in> cball b e \<inter> affine hull u"
   191       using \<open>e > 0\<close> \<open>b \<in> u\<close> \<open>c \<in> u\<close>
   192       by (simp add: d_def dist_norm hull_inc mem_affine_3_minus False)
   193     with e have "d \<in> u" by blast
   194     have nbc: "norm (b - c) + e > 0" using \<open>e > 0\<close>
   195       by (metis add.commute le_less_trans less_add_same_cancel2 norm_ge_zero)
   196     then have [simp]: "d \<noteq> c" using False scaleR_cancel_left [of "1 + (e / norm (b - c))" b c]
   197       by (simp add: algebra_simps d_def) (simp add: divide_simps)
   198     have [simp]: "((e - e * e / (e + norm (b - c))) / norm (b - c)) = (e / (e + norm (b - c)))"
   199       using False nbc
   200       apply (simp add: algebra_simps divide_simps)
   201       by (metis mult_eq_0_iff norm_eq_zero norm_imp_pos_and_ge norm_pths(2) real_scaleR_def scaleR_left.add zero_less_norm_iff)
   202     have "b \<in> open_segment d c"
   203       apply (simp add: open_segment_image_interval)
   204       apply (simp add: d_def algebra_simps image_def)
   205       apply (rule_tac x="e / (e + norm (b - c))" in bexI)
   206       using False nbc \<open>0 < e\<close>
   207       apply (auto simp: algebra_simps)
   208       done
   209     then have "d \<in> T \<and> c \<in> T"
   210       apply (rule face_ofD [OF \<open>T face_of S\<close>])
   211       using \<open>d \<in> u\<close>  \<open>c \<in> u\<close> \<open>u \<subseteq> S\<close>  \<open>b \<in> T\<close>  apply auto
   212       done
   213     then show ?thesis ..
   214   qed
   215 qed
   216 
   217 lemma face_of_eq:
   218     fixes S :: "'a::real_normed_vector set"
   219     assumes "T face_of S" "u face_of S" "(rel_interior T) \<inter> (rel_interior u) \<noteq> {}"
   220       shows "T = u"
   221   apply (rule subset_antisym)
   222   apply (metis assms disjoint_iff_not_equal face_of_imp_subset rel_interior_subset subsetCE subset_of_face_of)
   223   by (metis assms disjoint_iff_not_equal face_of_imp_subset rel_interior_subset subset_iff subset_of_face_of)
   224 
   225 lemma face_of_disjoint_rel_interior:
   226       fixes S :: "'a::real_normed_vector set"
   227       assumes "T face_of S" "T \<noteq> S"
   228         shows "T \<inter> rel_interior S = {}"
   229   by (meson assms subset_of_face_of face_of_imp_subset order_refl subset_antisym)
   230 
   231 lemma face_of_disjoint_interior:
   232       fixes S :: "'a::real_normed_vector set"
   233       assumes "T face_of S" "T \<noteq> S"
   234         shows "T \<inter> interior S = {}"
   235 proof -
   236   have "T \<inter> interior S \<subseteq> rel_interior S"
   237     by (meson inf_sup_ord(2) interior_subset_rel_interior order.trans)
   238   thus ?thesis
   239     by (metis (no_types) Int_greatest assms face_of_disjoint_rel_interior inf_sup_ord(1) subset_empty)
   240 qed
   241 
   242 lemma face_of_subset_rel_boundary:
   243   fixes S :: "'a::real_normed_vector set"
   244   assumes "T face_of S" "T \<noteq> S"
   245     shows "T \<subseteq> (S - rel_interior S)"
   246 by (meson DiffI assms disjoint_iff_not_equal face_of_disjoint_rel_interior face_of_imp_subset rev_subsetD subsetI)
   247 
   248 lemma face_of_subset_rel_frontier:
   249     fixes S :: "'a::real_normed_vector set"
   250     assumes "T face_of S" "T \<noteq> S"
   251       shows "T \<subseteq> rel_frontier S"
   252   using assms closure_subset face_of_disjoint_rel_interior face_of_imp_subset rel_frontier_def by fastforce
   253 
   254 lemma face_of_aff_dim_lt:
   255   fixes S :: "'a::euclidean_space set"
   256   assumes "convex S" "T face_of S" "T \<noteq> S"
   257     shows "aff_dim T < aff_dim S"
   258 proof -
   259   have "aff_dim T \<le> aff_dim S"
   260     by (simp add: face_of_imp_subset aff_dim_subset assms)
   261   moreover have "aff_dim T \<noteq> aff_dim S"
   262   proof (cases "T = {}")
   263     case True then show ?thesis
   264       by (metis aff_dim_empty \<open>T \<noteq> S\<close>)
   265   next case False then show ?thesis
   266     by (metis Set.set_insert assms convex_rel_frontier_aff_dim dual_order.irrefl face_of_imp_convex face_of_subset_rel_frontier insert_not_empty subsetI)
   267   qed
   268   ultimately show ?thesis
   269     by simp
   270 qed
   271 
   272 lemma subset_of_face_of_affine_hull:
   273     fixes S :: "'a::euclidean_space set"
   274   assumes T: "T face_of S" and "convex S" "U \<subseteq> S" and dis: "~disjnt (affine hull T) (rel_interior U)"
   275   shows "U \<subseteq> T"
   276   apply (rule subset_of_face_of [OF T \<open>U \<subseteq> S\<close>])
   277   using face_of_imp_eq_affine_Int [OF \<open>convex S\<close> T]
   278   using rel_interior_subset [of U] dis
   279   using \<open>U \<subseteq> S\<close> disjnt_def by fastforce
   280 
   281 lemma affine_hull_face_of_disjoint_rel_interior:
   282     fixes S :: "'a::euclidean_space set"
   283   assumes "convex S" "F face_of S" "F \<noteq> S"
   284   shows "affine hull F \<inter> rel_interior S = {}"
   285   by (metis assms disjnt_def face_of_imp_subset order_refl subset_antisym subset_of_face_of_affine_hull)
   286 
   287 lemma affine_diff_divide:
   288     assumes "affine S" "k \<noteq> 0" "k \<noteq> 1" and xy: "x \<in> S" "y /\<^sub>R (1 - k) \<in> S"
   289       shows "(x - y) /\<^sub>R k \<in> S"
   290 proof -
   291   have "inverse(k) *\<^sub>R (x - y) = (1 - inverse k) *\<^sub>R inverse(1 - k) *\<^sub>R y + inverse(k) *\<^sub>R x"
   292     using assms
   293     by (simp add: algebra_simps) (simp add: scaleR_left_distrib [symmetric] divide_simps)
   294   then show ?thesis
   295     using \<open>affine S\<close> xy by (auto simp: affine_alt)
   296 qed
   297 
   298 lemma face_of_convex_hulls:
   299       assumes S: "finite S" "T \<subseteq> S" and disj: "affine hull T \<inter> convex hull (S - T) = {}"
   300       shows  "(convex hull T) face_of (convex hull S)"
   301 proof -
   302   have fin: "finite T" "finite (S - T)" using assms
   303     by (auto simp: finite_subset)
   304   have *: "x \<in> convex hull T"
   305           if x: "x \<in> convex hull S" and y: "y \<in> convex hull S" and w: "w \<in> convex hull T" "w \<in> open_segment x y"
   306           for x y w
   307   proof -
   308     have waff: "w \<in> affine hull T"
   309       using convex_hull_subset_affine_hull w by blast
   310     obtain a b where a: "\<And>i. i \<in> S \<Longrightarrow> 0 \<le> a i" and asum: "sum a S = 1" and aeqx: "(\<Sum>i\<in>S. a i *\<^sub>R i) = x"
   311                  and b: "\<And>i. i \<in> S \<Longrightarrow> 0 \<le> b i" and bsum: "sum b S = 1" and beqy: "(\<Sum>i\<in>S. b i *\<^sub>R i) = y"
   312       using x y by (auto simp: assms convex_hull_finite)
   313     obtain u where "(1 - u) *\<^sub>R x + u *\<^sub>R y \<in> convex hull T" "x \<noteq> y" and weq: "w = (1 - u) *\<^sub>R x + u *\<^sub>R y"
   314                and u01: "0 < u" "u < 1"
   315       using w by (auto simp: open_segment_image_interval split: if_split_asm)
   316     define c where "c i = (1 - u) * a i + u * b i" for i
   317     have cge0: "\<And>i. i \<in> S \<Longrightarrow> 0 \<le> c i"
   318       using a b u01 by (simp add: c_def)
   319     have sumc1: "sum c S = 1"
   320       by (simp add: c_def sum.distrib sum_distrib_left [symmetric] asum bsum)
   321     have sumci_xy: "(\<Sum>i\<in>S. c i *\<^sub>R i) = (1 - u) *\<^sub>R x + u *\<^sub>R y"
   322       apply (simp add: c_def sum.distrib scaleR_left_distrib)
   323       by (simp only: scaleR_scaleR [symmetric] Real_Vector_Spaces.scaleR_right.sum [symmetric] aeqx beqy)
   324     show ?thesis
   325     proof (cases "sum c (S - T) = 0")
   326       case True
   327       have ci0: "\<And>i. i \<in> (S - T) \<Longrightarrow> c i = 0"
   328         using True cge0 fin(2) sum_nonneg_eq_0_iff by auto
   329       have a0: "a i = 0" if "i \<in> (S - T)" for i
   330         using ci0 [OF that] u01 a [of i] b [of i] that
   331         by (simp add: c_def Groups.ordered_comm_monoid_add_class.add_nonneg_eq_0_iff)
   332       have [simp]: "sum a T = 1"
   333         using assms by (metis sum.mono_neutral_cong_right a0 asum)
   334       show ?thesis
   335         apply (simp add: convex_hull_finite \<open>finite T\<close>)
   336         apply (rule_tac x=a in exI)
   337         using a0 assms
   338         apply (auto simp: cge0 a aeqx [symmetric] sum.mono_neutral_right)
   339         done
   340     next
   341       case False
   342       define k where "k = sum c (S - T)"
   343       have "k > 0" using False
   344         unfolding k_def by (metis DiffD1 antisym_conv cge0 sum_nonneg not_less)
   345       have weq_sumsum: "w = sum (\<lambda>x. c x *\<^sub>R x) T + sum (\<lambda>x. c x *\<^sub>R x) (S - T)"
   346         by (metis (no_types) add.commute S(1) S(2) sum.subset_diff sumci_xy weq)
   347       show ?thesis
   348       proof (cases "k = 1")
   349         case True
   350         then have "sum c T = 0"
   351           by (simp add: S k_def sum_diff sumc1)
   352         then have [simp]: "sum c (S - T) = 1"
   353           by (simp add: S sum_diff sumc1)
   354         have ci0: "\<And>i. i \<in> T \<Longrightarrow> c i = 0"
   355           by (meson \<open>finite T\<close> \<open>sum c T = 0\<close> \<open>T \<subseteq> S\<close> cge0 sum_nonneg_eq_0_iff subsetCE)
   356         then have [simp]: "(\<Sum>i\<in>S-T. c i *\<^sub>R i) = w"
   357           by (simp add: weq_sumsum)
   358         have "w \<in> convex hull (S - T)"
   359           apply (simp add: convex_hull_finite fin)
   360           apply (rule_tac x=c in exI)
   361           apply (auto simp: cge0 weq True k_def)
   362           done
   363         then show ?thesis
   364           using disj waff by blast
   365       next
   366         case False
   367         then have sumcf: "sum c T = 1 - k"
   368           by (simp add: S k_def sum_diff sumc1)
   369         have "(\<Sum>i\<in>T. c i *\<^sub>R i) /\<^sub>R (1 - k) \<in> convex hull T"
   370           apply (simp add: convex_hull_finite fin)
   371           apply (rule_tac x="\<lambda>i. inverse (1-k) * c i" in exI)
   372           apply auto
   373           apply (metis sumcf cge0 inverse_nonnegative_iff_nonnegative mult_nonneg_nonneg S(2) sum_nonneg subsetCE)
   374           apply (metis False mult.commute right_inverse right_minus_eq sum_distrib_left sumcf)
   375           by (metis (mono_tags, lifting) scaleR_right.sum scaleR_scaleR sum.cong)
   376         with \<open>0 < k\<close>  have "inverse(k) *\<^sub>R (w - sum (\<lambda>i. c i *\<^sub>R i) T) \<in> affine hull T"
   377           by (simp add: affine_diff_divide [OF affine_affine_hull] False waff convex_hull_subset_affine_hull [THEN subsetD])
   378         moreover have "inverse(k) *\<^sub>R (w - sum (\<lambda>x. c x *\<^sub>R x) T) \<in> convex hull (S - T)"
   379           apply (simp add: weq_sumsum convex_hull_finite fin)
   380           apply (rule_tac x="\<lambda>i. inverse k * c i" in exI)
   381           using \<open>k > 0\<close> cge0
   382           apply (auto simp: scaleR_right.sum sum_distrib_left [symmetric] k_def [symmetric])
   383           done
   384         ultimately show ?thesis
   385           using disj by blast
   386       qed
   387     qed
   388   qed
   389   have [simp]: "convex hull T \<subseteq> convex hull S"
   390     by (simp add: \<open>T \<subseteq> S\<close> hull_mono)
   391   show ?thesis
   392     using open_segment_commute by (auto simp: face_of_def intro: *)
   393 qed
   394 
   395 proposition face_of_convex_hull_insert:
   396    "\<lbrakk>finite S; a \<notin> affine hull S; T face_of convex hull S\<rbrakk> \<Longrightarrow> T face_of convex hull insert a S"
   397   apply (rule face_of_trans, blast)
   398   apply (rule face_of_convex_hulls; force simp: insert_Diff_if)
   399   done
   400 
   401 proposition face_of_affine_trivial:
   402     assumes "affine S" "T face_of S"
   403     shows "T = {} \<or> T = S"
   404 proof (rule ccontr, clarsimp)
   405   assume "T \<noteq> {}" "T \<noteq> S"
   406   then obtain a where "a \<in> T" by auto
   407   then have "a \<in> S"
   408     using \<open>T face_of S\<close> face_of_imp_subset by blast
   409   have "S \<subseteq> T"
   410   proof
   411     fix b  assume "b \<in> S"
   412     show "b \<in> T"
   413     proof (cases "a = b")
   414       case True with \<open>a \<in> T\<close> show ?thesis by auto
   415     next
   416       case False
   417       then have "a \<in> open_segment (2 *\<^sub>R a - b) b"
   418         apply (auto simp: open_segment_def closed_segment_def)
   419         apply (rule_tac x="1/2" in exI)
   420         apply (simp add: algebra_simps)
   421         by (simp add: scaleR_2)
   422       moreover have "2 *\<^sub>R a - b \<in> S"
   423         by (rule mem_affine [OF \<open>affine S\<close> \<open>a \<in> S\<close> \<open>b \<in> S\<close>, of 2 "-1", simplified])
   424       moreover note \<open>b \<in> S\<close> \<open>a \<in> T\<close>
   425       ultimately show ?thesis
   426         by (rule face_ofD [OF \<open>T face_of S\<close>, THEN conjunct2])
   427     qed
   428   qed
   429   then show False
   430     using \<open>T \<noteq> S\<close> \<open>T face_of S\<close> face_of_imp_subset by blast
   431 qed
   432 
   433 
   434 lemma face_of_affine_eq:
   435    "affine S \<Longrightarrow> (T face_of S \<longleftrightarrow> T = {} \<or> T = S)"
   436 using affine_imp_convex face_of_affine_trivial face_of_refl by auto
   437 
   438 
   439 lemma Inter_faces_finite_altbound:
   440     fixes T :: "'a::euclidean_space set set"
   441     assumes cfaI: "\<And>c. c \<in> T \<Longrightarrow> c face_of S"
   442     shows "\<exists>F'. finite F' \<and> F' \<subseteq> T \<and> card F' \<le> DIM('a) + 2 \<and> \<Inter>F' = \<Inter>T"
   443 proof (cases "\<forall>F'. finite F' \<and> F' \<subseteq> T \<and> card F' \<le> DIM('a) + 2 \<longrightarrow> (\<exists>c. c \<in> T \<and> c \<inter> (\<Inter>F') \<subset> (\<Inter>F'))")
   444   case True
   445   then obtain c where c:
   446        "\<And>F'. \<lbrakk>finite F'; F' \<subseteq> T; card F' \<le> DIM('a) + 2\<rbrakk> \<Longrightarrow> c F' \<in> T \<and> c F' \<inter> (\<Inter>F') \<subset> (\<Inter>F')"
   447     by metis
   448   define d where "d = rec_nat {c{}} (\<lambda>n r. insert (c r) r)"
   449   have [simp]: "d 0 = {c {}}"
   450     by (simp add: d_def)
   451   have dSuc [simp]: "\<And>n. d (Suc n) = insert (c (d n)) (d n)"
   452     by (simp add: d_def)
   453   have dn_notempty: "d n \<noteq> {}" for n
   454     by (induction n) auto
   455   have dn_le_Suc: "d n \<subseteq> T \<and> finite(d n) \<and> card(d n) \<le> Suc n" if "n \<le> DIM('a) + 2" for n
   456   using that
   457   proof (induction n)
   458     case 0
   459     then show ?case by (simp add: c)
   460   next
   461     case (Suc n)
   462     then show ?case by (auto simp: c card_insert_if)
   463   qed
   464   have aff_dim_le: "aff_dim(\<Inter>(d n)) \<le> DIM('a) - int n" if "n \<le> DIM('a) + 2" for n
   465   using that
   466   proof (induction n)
   467     case 0
   468     then show ?case
   469       by (simp add: aff_dim_le_DIM)
   470   next
   471     case (Suc n)
   472     have fs: "\<Inter>d (Suc n) face_of S"
   473       by (meson Suc.prems cfaI dn_le_Suc dn_notempty face_of_Inter subsetCE)
   474     have condn: "convex (\<Inter>d n)"
   475       using Suc.prems nat_le_linear not_less_eq_eq
   476       by (blast intro: face_of_imp_convex cfaI convex_Inter dest: dn_le_Suc)
   477     have fdn: "\<Inter>d (Suc n) face_of \<Inter>d n"
   478       by (metis (no_types, lifting) Inter_anti_mono Suc.prems dSuc cfaI dn_le_Suc dn_notempty face_of_Inter face_of_imp_subset face_of_subset subset_iff subset_insertI)
   479     have ne: "\<Inter>d (Suc n) \<noteq> \<Inter>d n"
   480       by (metis (no_types, lifting) Suc.prems Suc_leD c complete_lattice_class.Inf_insert dSuc dn_le_Suc less_irrefl order.trans)
   481     have *: "\<And>m::int. \<And>d. \<And>d'::int. d < d' \<and> d' \<le> m - n \<Longrightarrow> d \<le> m - of_nat(n+1)"
   482       by arith
   483     have "aff_dim (\<Inter>d (Suc n)) < aff_dim (\<Inter>d n)"
   484       by (rule face_of_aff_dim_lt [OF condn fdn ne])
   485     moreover have "aff_dim (\<Inter>d n) \<le> int (DIM('a)) - int n"
   486       using Suc by auto
   487     ultimately
   488     have "aff_dim (\<Inter>d (Suc n)) \<le> int (DIM('a)) - (n+1)" by arith
   489     then show ?case by linarith
   490   qed
   491   have "aff_dim (\<Inter>d (DIM('a) + 2)) \<le> -2"
   492       using aff_dim_le [OF order_refl] by simp
   493   with aff_dim_geq [of "\<Inter>d (DIM('a) + 2)"] show ?thesis
   494     using order.trans by fastforce
   495 next
   496   case False
   497   then show ?thesis
   498     apply simp
   499     apply (erule ex_forward)
   500     by blast
   501 qed
   502 
   503 lemma faces_of_translation:
   504    "{F. F face_of image (\<lambda>x. a + x) S} = image (image (\<lambda>x. a + x)) {F. F face_of S}"
   505 apply (rule subset_antisym, clarify)
   506 apply (auto simp: image_iff)
   507 apply (metis face_of_imp_subset face_of_translation_eq subset_imageE)
   508 done
   509 
   510 proposition face_of_Times:
   511   assumes "F face_of S" and "F' face_of S'"
   512     shows "(F \<times> F') face_of (S \<times> S')"
   513 proof -
   514   have "F \<times> F' \<subseteq> S \<times> S'"
   515     using assms [unfolded face_of_def] by blast
   516   moreover
   517   have "convex (F \<times> F')"
   518     using assms [unfolded face_of_def] by (blast intro: convex_Times)
   519   moreover
   520     have "a \<in> F \<and> a' \<in> F' \<and> b \<in> F \<and> b' \<in> F'"
   521        if "a \<in> S" "b \<in> S" "a' \<in> S'" "b' \<in> S'" "x \<in> F \<times> F'" "x \<in> open_segment (a,a') (b,b')"
   522        for a b a' b' x
   523   proof (cases "b=a \<or> b'=a'")
   524     case True with that show ?thesis
   525       using assms
   526       by (force simp: in_segment dest: face_ofD)
   527   next
   528     case False with assms [unfolded face_of_def] that show ?thesis
   529       by (blast dest!: open_segment_PairD)
   530   qed
   531   ultimately show ?thesis
   532     unfolding face_of_def by blast
   533 qed
   534 
   535 corollary face_of_Times_decomp:
   536     fixes S :: "'a::euclidean_space set" and S' :: "'b::euclidean_space set"
   537     shows "c face_of (S \<times> S') \<longleftrightarrow> (\<exists>F F'. F face_of S \<and> F' face_of S' \<and> c = F \<times> F')"
   538      (is "?lhs = ?rhs")
   539 proof
   540   assume c: ?lhs
   541   show ?rhs
   542   proof (cases "c = {}")
   543     case True then show ?thesis by auto
   544   next
   545     case False
   546     have 1: "fst ` c \<subseteq> S" "snd ` c \<subseteq> S'"
   547       using c face_of_imp_subset by fastforce+
   548     have "convex c"
   549       using c by (metis face_of_imp_convex)
   550     have conv: "convex (fst ` c)" "convex (snd ` c)"
   551       by (simp_all add: \<open>convex c\<close> convex_linear_image fst_linear snd_linear)
   552     have fstab: "a \<in> fst ` c \<and> b \<in> fst ` c"
   553             if "a \<in> S" "b \<in> S" "x \<in> open_segment a b" "(x,x') \<in> c" for a b x x'
   554     proof -
   555       have *: "(x,x') \<in> open_segment (a,x') (b,x')"
   556         using that by (auto simp: in_segment)
   557       show ?thesis
   558         using face_ofD [OF c *] that face_of_imp_subset [OF c] by force
   559     qed
   560     have fst: "fst ` c face_of S"
   561       by (force simp: face_of_def 1 conv fstab)
   562     have sndab: "a' \<in> snd ` c \<and> b' \<in> snd ` c"
   563             if "a' \<in> S'" "b' \<in> S'" "x' \<in> open_segment a' b'" "(x,x') \<in> c" for a' b' x x'
   564     proof -
   565       have *: "(x,x') \<in> open_segment (x,a') (x,b')"
   566         using that by (auto simp: in_segment)
   567       show ?thesis
   568         using face_ofD [OF c *] that face_of_imp_subset [OF c] by force
   569     qed
   570     have snd: "snd ` c face_of S'"
   571       by (force simp: face_of_def 1 conv sndab)
   572     have cc: "rel_interior c \<subseteq> rel_interior (fst ` c) \<times> rel_interior (snd ` c)"
   573       by (force simp: face_of_Times rel_interior_Times conv fst snd \<open>convex c\<close> fst_linear snd_linear rel_interior_convex_linear_image [symmetric])
   574     have "c = fst ` c \<times> snd ` c"
   575       apply (rule face_of_eq [OF c])
   576       apply (simp_all add: face_of_Times rel_interior_Times conv fst snd)
   577       using False rel_interior_eq_empty \<open>convex c\<close> cc
   578       apply blast
   579       done
   580     with fst snd show ?thesis by metis
   581   qed
   582 next
   583   assume ?rhs with face_of_Times show ?lhs by auto
   584 qed
   585 
   586 lemma face_of_Times_eq:
   587     fixes S :: "'a::euclidean_space set" and S' :: "'b::euclidean_space set"
   588     shows "(F \<times> F') face_of (S \<times> S') \<longleftrightarrow>
   589            F = {} \<or> F' = {} \<or> F face_of S \<and> F' face_of S'"
   590 by (auto simp: face_of_Times_decomp times_eq_iff)
   591 
   592 lemma hyperplane_face_of_halfspace_le: "{x. a \<bullet> x = b} face_of {x. a \<bullet> x \<le> b}"
   593 proof -
   594   have "{x. a \<bullet> x \<le> b} \<inter> {x. a \<bullet> x = b} = {x. a \<bullet> x = b}"
   595     by auto
   596   with face_of_Int_supporting_hyperplane_le [OF convex_halfspace_le [of a b], of a b]
   597   show ?thesis by auto
   598 qed
   599 
   600 lemma hyperplane_face_of_halfspace_ge: "{x. a \<bullet> x = b} face_of {x. a \<bullet> x \<ge> b}"
   601 proof -
   602   have "{x. a \<bullet> x \<ge> b} \<inter> {x. a \<bullet> x = b} = {x. a \<bullet> x = b}"
   603     by auto
   604   with face_of_Int_supporting_hyperplane_ge [OF convex_halfspace_ge [of b a], of b a]
   605   show ?thesis by auto
   606 qed
   607 
   608 lemma face_of_halfspace_le:
   609   fixes a :: "'n::euclidean_space"
   610   shows "F face_of {x. a \<bullet> x \<le> b} \<longleftrightarrow>
   611          F = {} \<or> F = {x. a \<bullet> x = b} \<or> F = {x. a \<bullet> x \<le> b}"
   612      (is "?lhs = ?rhs")
   613 proof (cases "a = 0")
   614   case True then show ?thesis
   615     using face_of_affine_eq affine_UNIV by auto
   616 next
   617   case False
   618   then have ine: "interior {x. a \<bullet> x \<le> b} \<noteq> {}"
   619     using halfspace_eq_empty_lt interior_halfspace_le by blast
   620   show ?thesis
   621   proof
   622     assume L: ?lhs
   623     have "F \<noteq> {x. a \<bullet> x \<le> b} \<Longrightarrow> F face_of {x. a \<bullet> x = b}"
   624       using False
   625       apply (simp add: frontier_halfspace_le [symmetric] rel_frontier_nonempty_interior [OF ine, symmetric])
   626       apply (rule face_of_subset [OF L])
   627       apply (simp add: face_of_subset_rel_frontier [OF L])
   628       apply (force simp: rel_frontier_def closed_halfspace_le)
   629       done
   630     with L show ?rhs
   631       using affine_hyperplane face_of_affine_eq by blast
   632   next
   633     assume ?rhs
   634     then show ?lhs
   635       by (metis convex_halfspace_le empty_face_of face_of_refl hyperplane_face_of_halfspace_le)
   636   qed
   637 qed
   638 
   639 lemma face_of_halfspace_ge:
   640   fixes a :: "'n::euclidean_space"
   641   shows "F face_of {x. a \<bullet> x \<ge> b} \<longleftrightarrow>
   642          F = {} \<or> F = {x. a \<bullet> x = b} \<or> F = {x. a \<bullet> x \<ge> b}"
   643 using face_of_halfspace_le [of F "-a" "-b"] by simp
   644 
   645 subsection\<open>Exposed faces\<close>
   646 
   647 text\<open>That is, faces that are intersection with supporting hyperplane\<close>
   648 
   649 definition exposed_face_of :: "['a::euclidean_space set, 'a set] \<Rightarrow> bool"
   650                                (infixr "(exposed'_face'_of)" 50)
   651   where "T exposed_face_of S \<longleftrightarrow>
   652          T face_of S \<and> (\<exists>a b. S \<subseteq> {x. a \<bullet> x \<le> b} \<and> T = S \<inter> {x. a \<bullet> x = b})"
   653 
   654 lemma empty_exposed_face_of [iff]: "{} exposed_face_of S"
   655   apply (simp add: exposed_face_of_def)
   656   apply (rule_tac x=0 in exI)
   657   apply (rule_tac x=1 in exI, force)
   658   done
   659 
   660 lemma exposed_face_of_refl_eq [simp]: "S exposed_face_of S \<longleftrightarrow> convex S"
   661   apply (simp add: exposed_face_of_def face_of_refl_eq, auto)
   662   apply (rule_tac x=0 in exI)+
   663   apply force
   664   done
   665 
   666 lemma exposed_face_of_refl: "convex S \<Longrightarrow> S exposed_face_of S"
   667   by simp
   668 
   669 lemma exposed_face_of:
   670     "T exposed_face_of S \<longleftrightarrow>
   671      T face_of S \<and>
   672      (T = {} \<or> T = S \<or>
   673       (\<exists>a b. a \<noteq> 0 \<and> S \<subseteq> {x. a \<bullet> x \<le> b} \<and> T = S \<inter> {x. a \<bullet> x = b}))"
   674 proof (cases "T = {}")
   675   case True then show ?thesis
   676     by simp
   677 next
   678   case False
   679   show ?thesis
   680   proof (cases "T = S")
   681     case True then show ?thesis
   682       by (simp add: face_of_refl_eq)
   683   next
   684     case False
   685     with \<open>T \<noteq> {}\<close> show ?thesis
   686       apply (auto simp: exposed_face_of_def)
   687       apply (metis inner_zero_left)
   688       done
   689   qed
   690 qed
   691 
   692 lemma exposed_face_of_Int_supporting_hyperplane_le:
   693    "\<lbrakk>convex S; \<And>x. x \<in> S \<Longrightarrow> a \<bullet> x \<le> b\<rbrakk> \<Longrightarrow> (S \<inter> {x. a \<bullet> x = b}) exposed_face_of S"
   694 by (force simp: exposed_face_of_def face_of_Int_supporting_hyperplane_le)
   695 
   696 lemma exposed_face_of_Int_supporting_hyperplane_ge:
   697    "\<lbrakk>convex S; \<And>x. x \<in> S \<Longrightarrow> a \<bullet> x \<ge> b\<rbrakk> \<Longrightarrow> (S \<inter> {x. a \<bullet> x = b}) exposed_face_of S"
   698 using exposed_face_of_Int_supporting_hyperplane_le [of S "-a" "-b"] by simp
   699 
   700 proposition exposed_face_of_Int:
   701   assumes "T exposed_face_of S"
   702       and "u exposed_face_of S"
   703     shows "(T \<inter> u) exposed_face_of S"
   704 proof -
   705   obtain a b where T: "S \<inter> {x. a \<bullet> x = b} face_of S"
   706                and S: "S \<subseteq> {x. a \<bullet> x \<le> b}"
   707                and teq: "T = S \<inter> {x. a \<bullet> x = b}"
   708     using assms by (auto simp: exposed_face_of_def)
   709   obtain a' b' where u: "S \<inter> {x. a' \<bullet> x = b'} face_of S"
   710                  and s': "S \<subseteq> {x. a' \<bullet> x \<le> b'}"
   711                  and ueq: "u = S \<inter> {x. a' \<bullet> x = b'}"
   712     using assms by (auto simp: exposed_face_of_def)
   713   have tu: "T \<inter> u face_of S"
   714     using T teq u ueq by (simp add: face_of_Int)
   715   have ss: "S \<subseteq> {x. (a + a') \<bullet> x \<le> b + b'}"
   716     using S s' by (force simp: inner_left_distrib)
   717   show ?thesis
   718     apply (simp add: exposed_face_of_def tu)
   719     apply (rule_tac x="a+a'" in exI)
   720     apply (rule_tac x="b+b'" in exI)
   721     using S s'
   722     apply (fastforce simp: ss inner_left_distrib teq ueq)
   723     done
   724 qed
   725 
   726 proposition exposed_face_of_Inter:
   727     fixes P :: "'a::euclidean_space set set"
   728   assumes "P \<noteq> {}"
   729       and "\<And>T. T \<in> P \<Longrightarrow> T exposed_face_of S"
   730     shows "\<Inter>P exposed_face_of S"
   731 proof -
   732   obtain Q where "finite Q" and QsubP: "Q \<subseteq> P" "card Q \<le> DIM('a) + 2" and IntQ: "\<Inter>Q = \<Inter>P"
   733     using Inter_faces_finite_altbound [of P S] assms [unfolded exposed_face_of]
   734     by force
   735   show ?thesis
   736   proof (cases "Q = {}")
   737     case True then show ?thesis
   738       by (metis Inf_empty Inf_lower IntQ assms ex_in_conv subset_antisym top_greatest)
   739   next
   740     case False
   741     have "Q \<subseteq> {T. T exposed_face_of S}"
   742       using QsubP assms by blast
   743     moreover have "Q \<subseteq> {T. T exposed_face_of S} \<Longrightarrow> \<Inter>Q exposed_face_of S"
   744       using \<open>finite Q\<close> False
   745       apply (induction Q rule: finite_induct)
   746       using exposed_face_of_Int apply fastforce+
   747       done
   748     ultimately show ?thesis
   749       by (simp add: IntQ)
   750   qed
   751 qed
   752 
   753 proposition exposed_face_of_sums:
   754   assumes "convex S" and "convex T"
   755       and "F exposed_face_of {x + y | x y. x \<in> S \<and> y \<in> T}"
   756           (is "F exposed_face_of ?ST")
   757   obtains k l
   758     where "k exposed_face_of S" "l exposed_face_of T"
   759           "F = {x + y | x y. x \<in> k \<and> y \<in> l}"
   760 proof (cases "F = {}")
   761   case True then show ?thesis
   762     using that by blast
   763 next
   764   case False
   765   show ?thesis
   766   proof (cases "F = ?ST")
   767     case True then show ?thesis
   768       using assms exposed_face_of_refl_eq that by blast
   769   next
   770     case False
   771     obtain p where "p \<in> F" using \<open>F \<noteq> {}\<close> by blast
   772     moreover
   773     obtain u z where T: "?ST \<inter> {x. u \<bullet> x = z} face_of ?ST"
   774                  and S: "?ST \<subseteq> {x. u \<bullet> x \<le> z}"
   775                  and feq: "F = ?ST \<inter> {x. u \<bullet> x = z}"
   776       using assms by (auto simp: exposed_face_of_def)
   777     ultimately obtain a0 b0
   778             where p: "p = a0 + b0" and "a0 \<in> S" "b0 \<in> T" and z: "u \<bullet> p = z"
   779       by auto
   780     have lez: "u \<bullet> (x + y) \<le> z" if "x \<in> S" "y \<in> T" for x y
   781       using S that by auto
   782     have sef: "S \<inter> {x. u \<bullet> x = u \<bullet> a0} exposed_face_of S"
   783       apply (rule exposed_face_of_Int_supporting_hyperplane_le [OF \<open>convex S\<close>])
   784       apply (metis p z add_le_cancel_right inner_right_distrib lez [OF _ \<open>b0 \<in> T\<close>])
   785       done
   786     have tef: "T \<inter> {x. u \<bullet> x = u \<bullet> b0} exposed_face_of T"
   787       apply (rule exposed_face_of_Int_supporting_hyperplane_le [OF \<open>convex T\<close>])
   788       apply (metis p z add.commute add_le_cancel_right inner_right_distrib lez [OF \<open>a0 \<in> S\<close>])
   789       done
   790     have "{x + y |x y. x \<in> S \<and> u \<bullet> x = u \<bullet> a0 \<and> y \<in> T \<and> u \<bullet> y = u \<bullet> b0} \<subseteq> F"
   791       by (auto simp: feq) (metis inner_right_distrib p z)
   792     moreover have "F \<subseteq> {x + y |x y. x \<in> S \<and> u \<bullet> x = u \<bullet> a0 \<and> y \<in> T \<and> u \<bullet> y = u \<bullet> b0}"
   793       apply (auto simp: feq)
   794       apply (rename_tac x y)
   795       apply (rule_tac x=x in exI)
   796       apply (rule_tac x=y in exI, simp)
   797       using z p \<open>a0 \<in> S\<close> \<open>b0 \<in> T\<close>
   798       apply clarify
   799       apply (simp add: inner_right_distrib)
   800       apply (metis add_le_cancel_right antisym lez [unfolded inner_right_distrib] add.commute)
   801       done
   802     ultimately have "F = {x + y |x y. x \<in> S \<inter> {x. u \<bullet> x = u \<bullet> a0} \<and> y \<in> T \<inter> {x. u \<bullet> x = u \<bullet> b0}}"
   803       by blast
   804     then show ?thesis
   805       by (rule that [OF sef tef])
   806   qed
   807 qed
   808 
   809 subsection\<open>Extreme points of a set: its singleton faces\<close>
   810 
   811 definition extreme_point_of :: "['a::real_vector, 'a set] \<Rightarrow> bool"
   812                                (infixr "(extreme'_point'_of)" 50)
   813   where "x extreme_point_of S \<longleftrightarrow>
   814          x \<in> S \<and> (\<forall>a \<in> S. \<forall>b \<in> S. x \<notin> open_segment a b)"
   815 
   816 lemma extreme_point_of_stillconvex:
   817    "convex S \<Longrightarrow> (x extreme_point_of S \<longleftrightarrow> x \<in> S \<and> convex(S - {x}))"
   818   by (fastforce simp add: convex_contains_segment extreme_point_of_def open_segment_def)
   819 
   820 lemma face_of_singleton:
   821    "{x} face_of S \<longleftrightarrow> x extreme_point_of S"
   822 by (fastforce simp add: extreme_point_of_def face_of_def)
   823 
   824 lemma extreme_point_not_in_REL_INTERIOR:
   825     fixes S :: "'a::real_normed_vector set"
   826     shows "\<lbrakk>x extreme_point_of S; S \<noteq> {x}\<rbrakk> \<Longrightarrow> x \<notin> rel_interior S"
   827 apply (simp add: face_of_singleton [symmetric])
   828 apply (blast dest: face_of_disjoint_rel_interior)
   829 done
   830 
   831 lemma extreme_point_not_in_interior:
   832     fixes S :: "'a::{real_normed_vector, perfect_space} set"
   833     shows "x extreme_point_of S \<Longrightarrow> x \<notin> interior S"
   834 apply (case_tac "S = {x}")
   835 apply (simp add: empty_interior_finite)
   836 by (meson contra_subsetD extreme_point_not_in_REL_INTERIOR interior_subset_rel_interior)
   837 
   838 lemma extreme_point_of_face:
   839      "F face_of S \<Longrightarrow> v extreme_point_of F \<longleftrightarrow> v extreme_point_of S \<and> v \<in> F"
   840   by (meson empty_subsetI face_of_face face_of_singleton insert_subset)
   841 
   842 lemma extreme_point_of_convex_hull:
   843    "x extreme_point_of (convex hull S) \<Longrightarrow> x \<in> S"
   844 apply (simp add: extreme_point_of_stillconvex)
   845 using hull_minimal [of S "(convex hull S) - {x}" convex]
   846 using hull_subset [of S convex]
   847 apply blast
   848 done
   849 
   850 lemma extreme_points_of_convex_hull:
   851    "{x. x extreme_point_of (convex hull S)} \<subseteq> S"
   852 using extreme_point_of_convex_hull by auto
   853 
   854 lemma extreme_point_of_empty [simp]: "~ (x extreme_point_of {})"
   855   by (simp add: extreme_point_of_def)
   856 
   857 lemma extreme_point_of_singleton [iff]: "x extreme_point_of {a} \<longleftrightarrow> x = a"
   858   using extreme_point_of_stillconvex by auto
   859 
   860 lemma extreme_point_of_translation_eq:
   861    "(a + x) extreme_point_of (image (\<lambda>x. a + x) S) \<longleftrightarrow> x extreme_point_of S"
   862 by (auto simp: extreme_point_of_def)
   863 
   864 lemma extreme_points_of_translation:
   865    "{x. x extreme_point_of (image (\<lambda>x. a + x) S)} =
   866     (\<lambda>x. a + x) ` {x. x extreme_point_of S}"
   867 using extreme_point_of_translation_eq
   868 by auto (metis (no_types, lifting) image_iff mem_Collect_eq minus_add_cancel)
   869 
   870 lemma extreme_point_of_Int:
   871    "\<lbrakk>x extreme_point_of S; x extreme_point_of T\<rbrakk> \<Longrightarrow> x extreme_point_of (S \<inter> T)"
   872 by (simp add: extreme_point_of_def)
   873 
   874 lemma extreme_point_of_Int_supporting_hyperplane_le:
   875    "\<lbrakk>S \<inter> {x. a \<bullet> x = b} = {c}; \<And>x. x \<in> S \<Longrightarrow> a \<bullet> x \<le> b\<rbrakk> \<Longrightarrow> c extreme_point_of S"
   876 apply (simp add: face_of_singleton [symmetric])
   877 by (metis face_of_Int_supporting_hyperplane_le_strong convex_singleton)
   878 
   879 lemma extreme_point_of_Int_supporting_hyperplane_ge:
   880    "\<lbrakk>S \<inter> {x. a \<bullet> x = b} = {c}; \<And>x. x \<in> S \<Longrightarrow> a \<bullet> x \<ge> b\<rbrakk> \<Longrightarrow> c extreme_point_of S"
   881 apply (simp add: face_of_singleton [symmetric])
   882 by (metis face_of_Int_supporting_hyperplane_ge_strong convex_singleton)
   883 
   884 lemma exposed_point_of_Int_supporting_hyperplane_le:
   885    "\<lbrakk>S \<inter> {x. a \<bullet> x = b} = {c}; \<And>x. x \<in> S \<Longrightarrow> a \<bullet> x \<le> b\<rbrakk> \<Longrightarrow> {c} exposed_face_of S"
   886 apply (simp add: exposed_face_of_def face_of_singleton)
   887 apply (force simp: extreme_point_of_Int_supporting_hyperplane_le)
   888 done
   889 
   890 lemma exposed_point_of_Int_supporting_hyperplane_ge:
   891     "\<lbrakk>S \<inter> {x. a \<bullet> x = b} = {c}; \<And>x. x \<in> S \<Longrightarrow> a \<bullet> x \<ge> b\<rbrakk> \<Longrightarrow> {c} exposed_face_of S"
   892 using exposed_point_of_Int_supporting_hyperplane_le [of S "-a" "-b" c]
   893 by simp
   894 
   895 lemma extreme_point_of_convex_hull_insert:
   896    "\<lbrakk>finite S; a \<notin> convex hull S\<rbrakk> \<Longrightarrow> a extreme_point_of (convex hull (insert a S))"
   897 apply (case_tac "a \<in> S")
   898 apply (simp add: hull_inc)
   899 using face_of_convex_hulls [of "insert a S" "{a}"]
   900 apply (auto simp: face_of_singleton hull_same)
   901 done
   902 
   903 subsection\<open>Facets\<close>
   904 
   905 definition facet_of :: "['a::euclidean_space set, 'a set] \<Rightarrow> bool"
   906                     (infixr "(facet'_of)" 50)
   907   where "F facet_of S \<longleftrightarrow> F face_of S \<and> F \<noteq> {} \<and> aff_dim F = aff_dim S - 1"
   908 
   909 lemma facet_of_empty [simp]: "~ S facet_of {}"
   910   by (simp add: facet_of_def)
   911 
   912 lemma facet_of_irrefl [simp]: "~ S facet_of S "
   913   by (simp add: facet_of_def)
   914 
   915 lemma facet_of_imp_face_of: "F facet_of S \<Longrightarrow> F face_of S"
   916   by (simp add: facet_of_def)
   917 
   918 lemma facet_of_imp_subset: "F facet_of S \<Longrightarrow> F \<subseteq> S"
   919   by (simp add: face_of_imp_subset facet_of_def)
   920 
   921 lemma hyperplane_facet_of_halfspace_le:
   922    "a \<noteq> 0 \<Longrightarrow> {x. a \<bullet> x = b} facet_of {x. a \<bullet> x \<le> b}"
   923 unfolding facet_of_def hyperplane_eq_empty
   924 by (auto simp: hyperplane_face_of_halfspace_ge hyperplane_face_of_halfspace_le
   925            DIM_positive Suc_leI of_nat_diff aff_dim_halfspace_le)
   926 
   927 lemma hyperplane_facet_of_halfspace_ge:
   928     "a \<noteq> 0 \<Longrightarrow> {x. a \<bullet> x = b} facet_of {x. a \<bullet> x \<ge> b}"
   929 unfolding facet_of_def hyperplane_eq_empty
   930 by (auto simp: hyperplane_face_of_halfspace_le hyperplane_face_of_halfspace_ge
   931            DIM_positive Suc_leI of_nat_diff aff_dim_halfspace_ge)
   932 
   933 lemma facet_of_halfspace_le:
   934     "F facet_of {x. a \<bullet> x \<le> b} \<longleftrightarrow> a \<noteq> 0 \<and> F = {x. a \<bullet> x = b}"
   935     (is "?lhs = ?rhs")
   936 proof
   937   assume c: ?lhs
   938   with c facet_of_irrefl show ?rhs
   939     by (force simp: aff_dim_halfspace_le facet_of_def face_of_halfspace_le cong: conj_cong split: if_split_asm)
   940 next
   941   assume ?rhs then show ?lhs
   942     by (simp add: hyperplane_facet_of_halfspace_le)
   943 qed
   944 
   945 lemma facet_of_halfspace_ge:
   946     "F facet_of {x. a \<bullet> x \<ge> b} \<longleftrightarrow> a \<noteq> 0 \<and> F = {x. a \<bullet> x = b}"
   947 using facet_of_halfspace_le [of F "-a" "-b"] by simp
   948 
   949 subsection \<open>Edges: faces of affine dimension 1\<close>
   950 
   951 definition edge_of :: "['a::euclidean_space set, 'a set] \<Rightarrow> bool"  (infixr "(edge'_of)" 50)
   952   where "e edge_of S \<longleftrightarrow> e face_of S \<and> aff_dim e = 1"
   953 
   954 lemma edge_of_imp_subset:
   955    "S edge_of T \<Longrightarrow> S \<subseteq> T"
   956 by (simp add: edge_of_def face_of_imp_subset)
   957 
   958 subsection\<open>Existence of extreme points\<close>
   959 
   960 lemma different_norm_3_collinear_points:
   961   fixes a :: "'a::euclidean_space"
   962   assumes "x \<in> open_segment a b" "norm(a) = norm(b)" "norm(x) = norm(b)"
   963   shows False
   964 proof -
   965   obtain u where "norm ((1 - u) *\<^sub>R a + u *\<^sub>R b) = norm b"
   966              and "a \<noteq> b"
   967              and u01: "0 < u" "u < 1"
   968     using assms by (auto simp: open_segment_image_interval if_splits)
   969   then have "(1 - u) *\<^sub>R a \<bullet> (1 - u) *\<^sub>R a + ((1 - u) * 2) *\<^sub>R a \<bullet> u *\<^sub>R b =
   970              (1 - u * u) *\<^sub>R (a \<bullet> a)"
   971     using assms by (simp add: norm_eq algebra_simps inner_commute)
   972   then have "(1 - u) *\<^sub>R ((1 - u) *\<^sub>R a \<bullet> a + (2 * u) *\<^sub>R  a \<bullet> b) =
   973              (1 - u) *\<^sub>R ((1 + u) *\<^sub>R (a \<bullet> a))"
   974     by (simp add: algebra_simps)
   975   then have "(1 - u) *\<^sub>R (a \<bullet> a) + (2 * u) *\<^sub>R (a \<bullet> b) = (1 + u) *\<^sub>R (a \<bullet> a)"
   976     using u01 by auto
   977   then have "a \<bullet> b = a \<bullet> a"
   978     using u01 by (simp add: algebra_simps)
   979   then have "a = b"
   980     using \<open>norm(a) = norm(b)\<close> norm_eq vector_eq by fastforce
   981   then show ?thesis
   982     using \<open>a \<noteq> b\<close> by force
   983 qed
   984 
   985 proposition extreme_point_exists_convex:
   986   fixes S :: "'a::euclidean_space set"
   987   assumes "compact S" "convex S" "S \<noteq> {}"
   988   obtains x where "x extreme_point_of S"
   989 proof -
   990   obtain x where "x \<in> S" and xsup: "\<And>y. y \<in> S \<Longrightarrow> norm y \<le> norm x"
   991     using distance_attains_sup [of S 0] assms by auto
   992   have False if "a \<in> S" "b \<in> S" and x: "x \<in> open_segment a b" for a b
   993   proof -
   994     have noax: "norm a \<le> norm x" and nobx: "norm b \<le> norm x" using xsup that by auto
   995     have "a \<noteq> b"
   996       using empty_iff open_segment_idem x by auto
   997     have *: "(1 - u) * na + u * nb < norm x" if "na < norm x"  "nb \<le> norm x" "0 < u" "u < 1" for na nb u
   998     proof -
   999       have "(1 - u) * na + u * nb < (1 - u) * norm x + u * nb"
  1000         by (simp add: that)
  1001       also have "... \<le> (1 - u) * norm x + u * norm x"
  1002         by (simp add: that)
  1003       finally have "(1 - u) * na + u * nb < (1 - u) * norm x + u * norm x" .
  1004       then show ?thesis
  1005       using scaleR_collapse [symmetric, of "norm x" u] by auto
  1006     qed
  1007     have "norm x < norm x" if "norm a < norm x"
  1008       using x
  1009       apply (clarsimp simp only: open_segment_image_interval \<open>a \<noteq> b\<close> if_False)
  1010       apply (rule norm_triangle_lt)
  1011       apply (simp add: norm_mult)
  1012       using * [of "norm a" "norm b"] nobx that
  1013         apply blast
  1014       done
  1015     moreover have "norm x < norm x" if "norm b < norm x"
  1016       using x
  1017       apply (clarsimp simp only: open_segment_image_interval \<open>a \<noteq> b\<close> if_False)
  1018       apply (rule norm_triangle_lt)
  1019       apply (simp add: norm_mult)
  1020       using * [of "norm b" "norm a" "1-u" for u] noax that
  1021         apply (simp add: add.commute)
  1022       done
  1023     ultimately have "~ (norm a < norm x) \<and> ~ (norm b < norm x)"
  1024       by auto
  1025     then show ?thesis
  1026       using different_norm_3_collinear_points noax nobx that(3) by fastforce
  1027   qed
  1028   then show ?thesis
  1029     apply (rule_tac x=x in that)
  1030     apply (force simp: extreme_point_of_def \<open>x \<in> S\<close>)
  1031     done
  1032 qed
  1033 
  1034 subsection\<open>Krein-Milman, the weaker form\<close>
  1035 
  1036 proposition Krein_Milman:
  1037   fixes S :: "'a::euclidean_space set"
  1038   assumes "compact S" "convex S"
  1039     shows "S = closure(convex hull {x. x extreme_point_of S})"
  1040 proof (cases "S = {}")
  1041   case True then show ?thesis   by simp
  1042 next
  1043   case False
  1044   have "closed S"
  1045     by (simp add: \<open>compact S\<close> compact_imp_closed)
  1046   have "closure (convex hull {x. x extreme_point_of S}) \<subseteq> S"
  1047     apply (rule closure_minimal [OF hull_minimal \<open>closed S\<close>])
  1048     using assms
  1049     apply (auto simp: extreme_point_of_def)
  1050     done
  1051   moreover have "u \<in> closure (convex hull {x. x extreme_point_of S})"
  1052                 if "u \<in> S" for u
  1053   proof (rule ccontr)
  1054     assume unot: "u \<notin> closure(convex hull {x. x extreme_point_of S})"
  1055     then obtain a b where "a \<bullet> u < b"
  1056           and ab: "\<And>x. x \<in> closure(convex hull {x. x extreme_point_of S}) \<Longrightarrow> b < a \<bullet> x"
  1057       using separating_hyperplane_closed_point [of "closure(convex hull {x. x extreme_point_of S})"]
  1058       by blast
  1059     have "continuous_on S (op \<bullet> a)"
  1060       by (rule continuous_intros)+
  1061     then obtain m where "m \<in> S" and m: "\<And>y. y \<in> S \<Longrightarrow> a \<bullet> m \<le> a \<bullet> y"
  1062       using continuous_attains_inf [of S "\<lambda>x. a \<bullet> x"] \<open>compact S\<close> \<open>u \<in> S\<close>
  1063       by auto
  1064     define T where "T = S \<inter> {x. a \<bullet> x = a \<bullet> m}"
  1065     have "m \<in> T"
  1066       by (simp add: T_def \<open>m \<in> S\<close>)
  1067     moreover have "compact T"
  1068       by (simp add: T_def compact_Int_closed [OF \<open>compact S\<close> closed_hyperplane])
  1069     moreover have "convex T"
  1070       by (simp add: T_def convex_Int [OF \<open>convex S\<close> convex_hyperplane])
  1071     ultimately obtain v where v: "v extreme_point_of T"
  1072       using extreme_point_exists_convex [of T] by auto
  1073     then have "{v} face_of T"
  1074       by (simp add: face_of_singleton)
  1075     also have "T face_of S"
  1076       by (simp add: T_def m face_of_Int_supporting_hyperplane_ge [OF \<open>convex S\<close>])
  1077     finally have "v extreme_point_of S"
  1078       by (simp add: face_of_singleton)
  1079     then have "b < a \<bullet> v"
  1080       using closure_subset by (simp add: closure_hull hull_inc ab)
  1081     then show False
  1082       using \<open>a \<bullet> u < b\<close> \<open>{v} face_of T\<close> face_of_imp_subset m T_def that by fastforce
  1083   qed
  1084   ultimately show ?thesis
  1085     by blast
  1086 qed
  1087 
  1088 text\<open>Now the sharper form.\<close>
  1089 
  1090 lemma Krein_Milman_Minkowski_aux:
  1091   fixes S :: "'a::euclidean_space set"
  1092   assumes n: "dim S = n" and S: "compact S" "convex S" "0 \<in> S"
  1093     shows "0 \<in> convex hull {x. x extreme_point_of S}"
  1094 using n S
  1095 proof (induction n arbitrary: S rule: less_induct)
  1096   case (less n S) show ?case
  1097   proof (cases "0 \<in> rel_interior S")
  1098     case True with Krein_Milman show ?thesis
  1099       by (metis subsetD convex_convex_hull convex_rel_interior_closure less.prems(2) less.prems(3) rel_interior_subset)
  1100   next
  1101     case False
  1102     have "rel_interior S \<noteq> {}"
  1103       by (simp add: rel_interior_convex_nonempty_aux less)
  1104     then obtain c where c: "c \<in> rel_interior S" by blast
  1105     obtain a where "a \<noteq> 0"
  1106               and le_ay: "\<And>y. y \<in> S \<Longrightarrow> a \<bullet> 0 \<le> a \<bullet> y"
  1107               and less_ay: "\<And>y. y \<in> rel_interior S \<Longrightarrow> a \<bullet> 0 < a \<bullet> y"
  1108       by (blast intro: supporting_hyperplane_rel_boundary intro!: less False)
  1109     have face: "S \<inter> {x. a \<bullet> x = 0} face_of S"
  1110       apply (rule face_of_Int_supporting_hyperplane_ge [OF \<open>convex S\<close>])
  1111       using le_ay by auto
  1112     then have co: "compact (S \<inter> {x. a \<bullet> x = 0})" "convex (S \<inter> {x. a \<bullet> x = 0})"
  1113       using less.prems by (blast intro: face_of_imp_compact face_of_imp_convex)+
  1114     have "a \<bullet> y = 0" if "y \<in> span (S \<inter> {x. a \<bullet> x = 0})" for y
  1115     proof -
  1116       have "y \<in> span {x. a \<bullet> x = 0}"
  1117         by (metis inf.cobounded2 span_mono subsetCE that)
  1118       then show ?thesis
  1119         by (blast intro: span_induct [OF _ subspace_hyperplane])
  1120     qed
  1121     then have "dim (S \<inter> {x. a \<bullet> x = 0}) < n"
  1122       by (metis (no_types) less_ay c subsetD dim_eq_span inf.strict_order_iff
  1123            inf_le1 \<open>dim S = n\<close> not_le rel_interior_subset span_0 span_clauses(1))
  1124     then have "0 \<in> convex hull {x. x extreme_point_of (S \<inter> {x. a \<bullet> x = 0})}"
  1125       by (rule less.IH) (auto simp: co less.prems)
  1126     then show ?thesis
  1127       by (metis (mono_tags, lifting) Collect_mono_iff \<open>S \<inter> {x. a \<bullet> x = 0} face_of S\<close> extreme_point_of_face hull_mono subset_iff)
  1128   qed
  1129 qed
  1130 
  1131 
  1132 theorem Krein_Milman_Minkowski:
  1133   fixes S :: "'a::euclidean_space set"
  1134   assumes "compact S" "convex S"
  1135     shows "S = convex hull {x. x extreme_point_of S}"
  1136 proof
  1137   show "S \<subseteq> convex hull {x. x extreme_point_of S}"
  1138   proof
  1139     fix a assume [simp]: "a \<in> S"
  1140     have 1: "compact (op + (- a) ` S)"
  1141       by (simp add: \<open>compact S\<close> compact_translation)
  1142     have 2: "convex (op + (- a) ` S)"
  1143       by (simp add: \<open>convex S\<close> convex_translation)
  1144     show a_invex: "a \<in> convex hull {x. x extreme_point_of S}"
  1145       using Krein_Milman_Minkowski_aux [OF refl 1 2]
  1146             convex_hull_translation [of "-a"]
  1147       by (auto simp: extreme_points_of_translation translation_assoc)
  1148     qed
  1149 next
  1150   show "convex hull {x. x extreme_point_of S} \<subseteq> S"
  1151   proof -
  1152     have "{a. a extreme_point_of S} \<subseteq> S"
  1153       using extreme_point_of_def by blast
  1154     then show ?thesis
  1155       by (simp add: \<open>convex S\<close> hull_minimal)
  1156   qed
  1157 qed
  1158 
  1159 
  1160 subsection\<open>Applying it to convex hulls of explicitly indicated finite sets\<close>
  1161 
  1162 lemma Krein_Milman_polytope:
  1163   fixes S :: "'a::euclidean_space set"
  1164   shows
  1165    "finite S
  1166        \<Longrightarrow> convex hull S =
  1167            convex hull {x. x extreme_point_of (convex hull S)}"
  1168 by (simp add: Krein_Milman_Minkowski finite_imp_compact_convex_hull)
  1169 
  1170 lemma extreme_points_of_convex_hull_eq:
  1171   fixes S :: "'a::euclidean_space set"
  1172   shows
  1173    "\<lbrakk>compact S; \<And>T. T \<subset> S \<Longrightarrow> convex hull T \<noteq> convex hull S\<rbrakk>
  1174         \<Longrightarrow> {x. x extreme_point_of (convex hull S)} = S"
  1175 by (metis (full_types) Krein_Milman_Minkowski compact_convex_hull convex_convex_hull extreme_points_of_convex_hull psubsetI)
  1176 
  1177 
  1178 lemma extreme_point_of_convex_hull_eq:
  1179   fixes S :: "'a::euclidean_space set"
  1180   shows
  1181    "\<lbrakk>compact S; \<And>T. T \<subset> S \<Longrightarrow> convex hull T \<noteq> convex hull S\<rbrakk>
  1182     \<Longrightarrow> (x extreme_point_of (convex hull S) \<longleftrightarrow> x \<in> S)"
  1183 using extreme_points_of_convex_hull_eq by auto
  1184 
  1185 lemma extreme_point_of_convex_hull_convex_independent:
  1186   fixes S :: "'a::euclidean_space set"
  1187   assumes "compact S" and S: "\<And>a. a \<in> S \<Longrightarrow> a \<notin> convex hull (S - {a})"
  1188   shows "(x extreme_point_of (convex hull S) \<longleftrightarrow> x \<in> S)"
  1189 proof -
  1190   have "convex hull T \<noteq> convex hull S" if "T \<subset> S" for T
  1191   proof -
  1192     obtain a where  "T \<subseteq> S" "a \<in> S" "a \<notin> T" using \<open>T \<subset> S\<close> by blast
  1193     then show ?thesis
  1194       by (metis (full_types) Diff_eq_empty_iff Diff_insert0 S hull_mono hull_subset insert_Diff_single subsetCE)
  1195   qed
  1196   then show ?thesis
  1197     by (rule extreme_point_of_convex_hull_eq [OF \<open>compact S\<close>])
  1198 qed
  1199 
  1200 lemma extreme_point_of_convex_hull_affine_independent:
  1201   fixes S :: "'a::euclidean_space set"
  1202   shows
  1203    "~ affine_dependent S
  1204          \<Longrightarrow> (x extreme_point_of (convex hull S) \<longleftrightarrow> x \<in> S)"
  1205 by (metis aff_independent_finite affine_dependent_def affine_hull_convex_hull extreme_point_of_convex_hull_convex_independent finite_imp_compact hull_inc)
  1206 
  1207 text\<open>Elementary proofs exist, not requiring Euclidean spaces and all this development\<close>
  1208 lemma extreme_point_of_convex_hull_2:
  1209   fixes x :: "'a::euclidean_space"
  1210   shows "x extreme_point_of (convex hull {a,b}) \<longleftrightarrow> x = a \<or> x = b"
  1211 proof -
  1212   have "x extreme_point_of (convex hull {a,b}) \<longleftrightarrow> x \<in> {a,b}"
  1213     by (intro extreme_point_of_convex_hull_affine_independent affine_independent_2)
  1214   then show ?thesis
  1215     by simp
  1216 qed
  1217 
  1218 lemma extreme_point_of_segment:
  1219   fixes x :: "'a::euclidean_space"
  1220   shows
  1221    "x extreme_point_of closed_segment a b \<longleftrightarrow> x = a \<or> x = b"
  1222 by (simp add: extreme_point_of_convex_hull_2 segment_convex_hull)
  1223 
  1224 lemma face_of_convex_hull_subset:
  1225   fixes S :: "'a::euclidean_space set"
  1226   assumes "compact S" and T: "T face_of (convex hull S)"
  1227   obtains s' where "s' \<subseteq> S" "T = convex hull s'"
  1228 apply (rule_tac s' = "{x. x extreme_point_of T}" in that)
  1229 using T extreme_point_of_convex_hull extreme_point_of_face apply blast
  1230 by (metis (no_types) Krein_Milman_Minkowski assms compact_convex_hull convex_convex_hull face_of_imp_compact face_of_imp_convex)
  1231 
  1232 
  1233 proposition face_of_convex_hull_affine_independent:
  1234   fixes S :: "'a::euclidean_space set"
  1235   assumes "~ affine_dependent S"
  1236     shows "(T face_of (convex hull S) \<longleftrightarrow> (\<exists>c. c \<subseteq> S \<and> T = convex hull c))"
  1237           (is "?lhs = ?rhs")
  1238 proof
  1239   assume ?lhs
  1240   then show ?rhs
  1241     by (meson \<open>T face_of convex hull S\<close> aff_independent_finite assms face_of_convex_hull_subset finite_imp_compact)
  1242 next
  1243   assume ?rhs
  1244   then obtain c where "c \<subseteq> S" and T: "T = convex hull c"
  1245     by blast
  1246   have "affine hull c \<inter> affine hull (S - c) = {}"
  1247     apply (rule disjoint_affine_hull [OF assms \<open>c \<subseteq> S\<close>], auto)
  1248     done
  1249   then have "affine hull c \<inter> convex hull (S - c) = {}"
  1250     using convex_hull_subset_affine_hull by fastforce
  1251   then show ?lhs
  1252     by (metis face_of_convex_hulls \<open>c \<subseteq> S\<close> aff_independent_finite assms T)
  1253 qed
  1254 
  1255 lemma facet_of_convex_hull_affine_independent:
  1256   fixes S :: "'a::euclidean_space set"
  1257   assumes "~ affine_dependent S"
  1258     shows "T facet_of (convex hull S) \<longleftrightarrow>
  1259            T \<noteq> {} \<and> (\<exists>u. u \<in> S \<and> T = convex hull (S - {u}))"
  1260           (is "?lhs = ?rhs")
  1261 proof
  1262   assume ?lhs
  1263   then have "T face_of (convex hull S)" "T \<noteq> {}"
  1264         and afft: "aff_dim T = aff_dim (convex hull S) - 1"
  1265     by (auto simp: facet_of_def)
  1266   then obtain c where "c \<subseteq> S" and c: "T = convex hull c"
  1267     by (auto simp: face_of_convex_hull_affine_independent [OF assms])
  1268   then have affs: "aff_dim S = aff_dim c + 1"
  1269     by (metis aff_dim_convex_hull afft eq_diff_eq)
  1270   have "~ affine_dependent c"
  1271     using \<open>c \<subseteq> S\<close> affine_dependent_subset assms by blast
  1272   with affs have "card (S - c) = 1"
  1273     apply (simp add: aff_dim_affine_independent [symmetric] aff_dim_convex_hull)
  1274     by (metis aff_dim_affine_independent aff_independent_finite One_nat_def \<open>c \<subseteq> S\<close> add.commute
  1275                 add_diff_cancel_right' assms card_Diff_subset card_mono of_nat_1 of_nat_diff of_nat_eq_iff)
  1276   then obtain u where u: "u \<in> S - c"
  1277     by (metis DiffI \<open>c \<subseteq> S\<close> aff_independent_finite assms cancel_comm_monoid_add_class.diff_cancel
  1278                 card_Diff_subset subsetI subset_antisym zero_neq_one)
  1279   then have u: "S = insert u c"
  1280     by (metis Diff_subset \<open>c \<subseteq> S\<close> \<open>card (S - c) = 1\<close> card_1_singletonE double_diff insert_Diff insert_subset singletonD)
  1281   have "T = convex hull (c - {u})"
  1282     by (metis Diff_empty Diff_insert0 \<open>T facet_of convex hull S\<close> c facet_of_irrefl insert_absorb u)
  1283   with \<open>T \<noteq> {}\<close> show ?rhs
  1284     using c u by auto
  1285 next
  1286   assume ?rhs
  1287   then obtain u where "T \<noteq> {}" "u \<in> S" and u: "T = convex hull (S - {u})"
  1288     by (force simp: facet_of_def)
  1289   then have "\<not> S \<subseteq> {u}"
  1290     using \<open>T \<noteq> {}\<close> u by auto
  1291   have [simp]: "aff_dim (convex hull (S - {u})) = aff_dim (convex hull S) - 1"
  1292     using assms \<open>u \<in> S\<close>
  1293     apply (simp add: aff_dim_convex_hull affine_dependent_def)
  1294     apply (drule bspec, assumption)
  1295     by (metis add_diff_cancel_right' aff_dim_insert insert_Diff [of u S])
  1296   show ?lhs
  1297     apply (subst u)
  1298     apply (simp add: \<open>\<not> S \<subseteq> {u}\<close> facet_of_def face_of_convex_hull_affine_independent [OF assms], blast)
  1299     done
  1300 qed
  1301 
  1302 lemma facet_of_convex_hull_affine_independent_alt:
  1303   fixes S :: "'a::euclidean_space set"
  1304   shows
  1305    "~affine_dependent S
  1306         \<Longrightarrow> (T facet_of (convex hull S) \<longleftrightarrow>
  1307              2 \<le> card S \<and> (\<exists>u. u \<in> S \<and> T = convex hull (S - {u})))"
  1308 apply (simp add: facet_of_convex_hull_affine_independent)
  1309 apply (auto simp: Set.subset_singleton_iff)
  1310 apply (metis Diff_cancel Int_empty_right Int_insert_right_if1  aff_independent_finite card_eq_0_iff card_insert_if card_mono card_subset_eq convex_hull_eq_empty eq_iff equals0D finite_insert finite_subset inf.absorb_iff2 insert_absorb insert_not_empty  not_less_eq_eq numeral_2_eq_2)
  1311 done
  1312 
  1313 lemma segment_face_of:
  1314   assumes "(closed_segment a b) face_of S"
  1315   shows "a extreme_point_of S" "b extreme_point_of S"
  1316 proof -
  1317   have as: "{a} face_of S"
  1318     by (metis (no_types) assms convex_hull_singleton empty_iff extreme_point_of_convex_hull_insert face_of_face face_of_singleton finite.emptyI finite.insertI insert_absorb insert_iff segment_convex_hull)
  1319   moreover have "{b} face_of S"
  1320   proof -
  1321     have "b \<in> convex hull {a} \<or> b extreme_point_of convex hull {b, a}"
  1322       by (meson extreme_point_of_convex_hull_insert finite.emptyI finite.insertI)
  1323     moreover have "closed_segment a b = convex hull {b, a}"
  1324       using closed_segment_commute segment_convex_hull by blast
  1325     ultimately show ?thesis
  1326       by (metis as assms face_of_face convex_hull_singleton empty_iff face_of_singleton insertE)
  1327     qed
  1328   ultimately show "a extreme_point_of S" "b extreme_point_of S"
  1329     using face_of_singleton by blast+
  1330 qed
  1331 
  1332 
  1333 lemma Krein_Milman_frontier:
  1334   fixes S :: "'a::euclidean_space set"
  1335   assumes "convex S" "compact S"
  1336     shows "S = convex hull (frontier S)"
  1337           (is "?lhs = ?rhs")
  1338 proof
  1339   have "?lhs \<subseteq> convex hull {x. x extreme_point_of S}"
  1340     using Krein_Milman_Minkowski assms by blast
  1341   also have "... \<subseteq> ?rhs"
  1342     apply (rule hull_mono)
  1343     apply (auto simp: frontier_def extreme_point_not_in_interior)
  1344     using closure_subset apply (force simp: extreme_point_of_def)
  1345     done
  1346   finally show "?lhs \<subseteq> ?rhs" .
  1347 next
  1348   have "?rhs \<subseteq> convex hull S"
  1349     by (metis Diff_subset \<open>compact S\<close> closure_closed compact_eq_bounded_closed frontier_def hull_mono)
  1350   also have "... \<subseteq> ?lhs"
  1351     by (simp add: \<open>convex S\<close> hull_same)
  1352   finally show "?rhs \<subseteq> ?lhs" .
  1353 qed
  1354 
  1355 subsection\<open>Polytopes\<close>
  1356 
  1357 definition polytope where
  1358  "polytope S \<equiv> \<exists>v. finite v \<and> S = convex hull v"
  1359 
  1360 lemma polytope_translation_eq: "polytope (image (\<lambda>x. a + x) S) \<longleftrightarrow> polytope S"
  1361 apply (simp add: polytope_def, safe)
  1362 apply (metis convex_hull_translation finite_imageI translation_galois)
  1363 by (metis convex_hull_translation finite_imageI)
  1364 
  1365 lemma polytope_linear_image: "\<lbrakk>linear f; polytope p\<rbrakk> \<Longrightarrow> polytope(image f p)"
  1366   unfolding polytope_def using convex_hull_linear_image by blast
  1367 
  1368 lemma polytope_empty: "polytope {}"
  1369   using convex_hull_empty polytope_def by blast
  1370 
  1371 lemma polytope_convex_hull: "finite S \<Longrightarrow> polytope(convex hull S)"
  1372   using polytope_def by auto
  1373 
  1374 lemma polytope_Times: "\<lbrakk>polytope S; polytope T\<rbrakk> \<Longrightarrow> polytope(S \<times> T)"
  1375   unfolding polytope_def
  1376   by (metis finite_cartesian_product convex_hull_Times)
  1377 
  1378 lemma face_of_polytope_polytope:
  1379   fixes S :: "'a::euclidean_space set"
  1380   shows "\<lbrakk>polytope S; F face_of S\<rbrakk> \<Longrightarrow> polytope F"
  1381 unfolding polytope_def
  1382 by (meson face_of_convex_hull_subset finite_imp_compact finite_subset)
  1383 
  1384 lemma finite_polytope_faces:
  1385   fixes S :: "'a::euclidean_space set"
  1386   assumes "polytope S"
  1387   shows "finite {F. F face_of S}"
  1388 proof -
  1389   obtain v where "finite v" "S = convex hull v"
  1390     using assms polytope_def by auto
  1391   have "finite (op hull convex ` {T. T \<subseteq> v})"
  1392     by (simp add: \<open>finite v\<close>)
  1393   moreover have "{F. F face_of S} \<subseteq> (op hull convex ` {T. T \<subseteq> v})"
  1394     by (metis (no_types, lifting) \<open>finite v\<close> \<open>S = convex hull v\<close> face_of_convex_hull_subset finite_imp_compact image_eqI mem_Collect_eq subsetI)
  1395   ultimately show ?thesis
  1396     by (blast intro: finite_subset)
  1397 qed
  1398 
  1399 lemma finite_polytope_facets:
  1400   assumes "polytope S"
  1401   shows "finite {T. T facet_of S}"
  1402 by (simp add: assms facet_of_def finite_polytope_faces)
  1403 
  1404 lemma polytope_scaling:
  1405   assumes "polytope S"  shows "polytope (image (\<lambda>x. c *\<^sub>R x) S)"
  1406 by (simp add: assms polytope_linear_image)
  1407 
  1408 lemma polytope_imp_compact:
  1409   fixes S :: "'a::real_normed_vector set"
  1410   shows "polytope S \<Longrightarrow> compact S"
  1411 by (metis finite_imp_compact_convex_hull polytope_def)
  1412 
  1413 lemma polytope_imp_convex: "polytope S \<Longrightarrow> convex S"
  1414   by (metis convex_convex_hull polytope_def)
  1415 
  1416 lemma polytope_imp_closed:
  1417   fixes S :: "'a::real_normed_vector set"
  1418   shows "polytope S \<Longrightarrow> closed S"
  1419 by (simp add: compact_imp_closed polytope_imp_compact)
  1420 
  1421 lemma polytope_imp_bounded:
  1422   fixes S :: "'a::real_normed_vector set"
  1423   shows "polytope S \<Longrightarrow> bounded S"
  1424 by (simp add: compact_imp_bounded polytope_imp_compact)
  1425 
  1426 lemma polytope_interval: "polytope(cbox a b)"
  1427   unfolding polytope_def by (meson closed_interval_as_convex_hull)
  1428 
  1429 lemma polytope_sing: "polytope {a}"
  1430   using polytope_def by force
  1431 
  1432 
  1433 subsection\<open>Polyhedra\<close>
  1434 
  1435 definition polyhedron where
  1436  "polyhedron S \<equiv>
  1437         \<exists>F. finite F \<and>
  1438             S = \<Inter> F \<and>
  1439             (\<forall>h \<in> F. \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b})"
  1440 
  1441 lemma polyhedron_Int [intro,simp]:
  1442    "\<lbrakk>polyhedron S; polyhedron T\<rbrakk> \<Longrightarrow> polyhedron (S \<inter> T)"
  1443   apply (simp add: polyhedron_def, clarify)
  1444   apply (rename_tac F G)
  1445   apply (rule_tac x="F \<union> G" in exI, auto)
  1446   done
  1447 
  1448 lemma polyhedron_UNIV [iff]: "polyhedron UNIV"
  1449   unfolding polyhedron_def
  1450   by (rule_tac x="{}" in exI) auto
  1451 
  1452 lemma polyhedron_Inter [intro,simp]:
  1453    "\<lbrakk>finite F; \<And>S. S \<in> F \<Longrightarrow> polyhedron S\<rbrakk> \<Longrightarrow> polyhedron(\<Inter>F)"
  1454 by (induction F rule: finite_induct) auto
  1455 
  1456 
  1457 lemma polyhedron_empty [iff]: "polyhedron ({} :: 'a :: euclidean_space set)"
  1458 proof -
  1459   have "\<exists>a. a \<noteq> 0 \<and>
  1460              (\<exists>b. {x. (SOME i. i \<in> Basis) \<bullet> x \<le> - 1} = {x. a \<bullet> x \<le> b})"
  1461     by (rule_tac x="(SOME i. i \<in> Basis)" in exI) (force simp: SOME_Basis nonzero_Basis)
  1462   moreover have "\<exists>a b. a \<noteq> 0 \<and>
  1463                        {x. - (SOME i. i \<in> Basis) \<bullet> x \<le> - 1} = {x. a \<bullet> x \<le> b}"
  1464       apply (rule_tac x="-(SOME i. i \<in> Basis)" in exI)
  1465       apply (rule_tac x="-1" in exI)
  1466       apply (simp add: SOME_Basis nonzero_Basis)
  1467       done
  1468   ultimately show ?thesis
  1469     unfolding polyhedron_def
  1470     apply (rule_tac x="{{x. (SOME i. i \<in> Basis) \<bullet> x \<le> -1},
  1471                         {x. -(SOME i. i \<in> Basis) \<bullet> x \<le> -1}}" in exI)
  1472     apply force
  1473     done
  1474 qed
  1475 
  1476 lemma polyhedron_halfspace_le:
  1477   fixes a :: "'a :: euclidean_space"
  1478   shows "polyhedron {x. a \<bullet> x \<le> b}"
  1479 proof (cases "a = 0")
  1480   case True then show ?thesis by auto
  1481 next
  1482   case False
  1483   then show ?thesis
  1484     unfolding polyhedron_def
  1485     by (rule_tac x="{{x. a \<bullet> x \<le> b}}" in exI) auto
  1486 qed
  1487 
  1488 lemma polyhedron_halfspace_ge:
  1489   fixes a :: "'a :: euclidean_space"
  1490   shows "polyhedron {x. a \<bullet> x \<ge> b}"
  1491 using polyhedron_halfspace_le [of "-a" "-b"] by simp
  1492 
  1493 lemma polyhedron_hyperplane:
  1494   fixes a :: "'a :: euclidean_space"
  1495   shows "polyhedron {x. a \<bullet> x = b}"
  1496 proof -
  1497   have "{x. a \<bullet> x = b} = {x. a \<bullet> x \<le> b} \<inter> {x. a \<bullet> x \<ge> b}"
  1498     by force
  1499   then show ?thesis
  1500     by (simp add: polyhedron_halfspace_ge polyhedron_halfspace_le)
  1501 qed
  1502 
  1503 lemma affine_imp_polyhedron:
  1504   fixes S :: "'a :: euclidean_space set"
  1505   shows "affine S \<Longrightarrow> polyhedron S"
  1506 by (metis affine_hull_eq polyhedron_Inter polyhedron_hyperplane affine_hull_finite_intersection_hyperplanes [of S])
  1507 
  1508 lemma polyhedron_imp_closed:
  1509   fixes S :: "'a :: euclidean_space set"
  1510   shows "polyhedron S \<Longrightarrow> closed S"
  1511 apply (simp add: polyhedron_def)
  1512 using closed_halfspace_le by fastforce
  1513 
  1514 lemma polyhedron_imp_convex:
  1515   fixes S :: "'a :: euclidean_space set"
  1516   shows "polyhedron S \<Longrightarrow> convex S"
  1517 apply (simp add: polyhedron_def)
  1518 using convex_Inter convex_halfspace_le by fastforce
  1519 
  1520 lemma polyhedron_affine_hull:
  1521   fixes S :: "'a :: euclidean_space set"
  1522   shows "polyhedron(affine hull S)"
  1523 by (simp add: affine_imp_polyhedron)
  1524 
  1525 
  1526 subsection\<open>Canonical polyhedron representation making facial structure explicit\<close>
  1527 
  1528 lemma polyhedron_Int_affine:
  1529   fixes S :: "'a :: euclidean_space set"
  1530   shows "polyhedron S \<longleftrightarrow>
  1531            (\<exists>F. finite F \<and> S = (affine hull S) \<inter> \<Inter>F \<and>
  1532                 (\<forall>h \<in> F. \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b}))"
  1533         (is "?lhs = ?rhs")
  1534 proof
  1535   assume ?lhs then show ?rhs
  1536     apply (simp add: polyhedron_def)
  1537     apply (erule ex_forward)
  1538     using hull_subset apply force
  1539     done
  1540 next
  1541   assume ?rhs then show ?lhs
  1542     apply clarify
  1543     apply (erule ssubst)
  1544     apply (force intro: polyhedron_affine_hull polyhedron_halfspace_le)
  1545     done
  1546 qed
  1547 
  1548 proposition rel_interior_polyhedron_explicit:
  1549   assumes "finite F"
  1550       and seq: "S = affine hull S \<inter> \<Inter>F"
  1551       and faceq: "\<And>h. h \<in> F \<Longrightarrow> a h \<noteq> 0 \<and> h = {x. a h \<bullet> x \<le> b h}"
  1552       and psub: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subset> affine hull S \<inter> \<Inter>F'"
  1553     shows "rel_interior S = {x \<in> S. \<forall>h \<in> F. a h \<bullet> x < b h}"
  1554 proof -
  1555   have rels: "\<And>x. x \<in> rel_interior S \<Longrightarrow> x \<in> S"
  1556     by (meson IntE mem_rel_interior)
  1557   moreover have "a i \<bullet> x < b i" if x: "x \<in> rel_interior S" and "i \<in> F" for x i
  1558   proof -
  1559     have fif: "F - {i} \<subset> F"
  1560       using \<open>i \<in> F\<close> Diff_insert_absorb Diff_subset set_insert psubsetI by blast
  1561     then have "S \<subset> affine hull S \<inter> \<Inter>(F - {i})"
  1562       by (rule psub)
  1563     then obtain z where ssub: "S \<subseteq> \<Inter>(F - {i})" and zint: "z \<in> \<Inter>(F - {i})"
  1564                     and "z \<notin> S" and zaff: "z \<in> affine hull S"
  1565       by auto
  1566     have "z \<noteq> x"
  1567       using \<open>z \<notin> S\<close> rels x by blast
  1568     have "z \<notin> affine hull S \<inter> \<Inter>F"
  1569       using \<open>z \<notin> S\<close> seq by auto
  1570     then have aiz: "a i \<bullet> z > b i"
  1571       using faceq zint zaff by fastforce
  1572     obtain e where "e > 0" "x \<in> S" and e: "ball x e \<inter> affine hull S \<subseteq> S"
  1573       using x by (auto simp: mem_rel_interior_ball)
  1574     then have ins: "\<And>y. \<lbrakk>norm (x - y) < e; y \<in> affine hull S\<rbrakk> \<Longrightarrow> y \<in> S"
  1575       by (metis IntI subsetD dist_norm mem_ball)
  1576     define \<xi> where "\<xi> = min (1/2) (e / 2 / norm(z - x))"
  1577     have "norm (\<xi> *\<^sub>R x - \<xi> *\<^sub>R z) = norm (\<xi> *\<^sub>R (x - z))"
  1578       by (simp add: \<xi>_def algebra_simps norm_mult)
  1579     also have "... = \<xi> * norm (x - z)"
  1580       using \<open>e > 0\<close> by (simp add: \<xi>_def)
  1581     also have "... < e"
  1582       using \<open>z \<noteq> x\<close> \<open>e > 0\<close> by (simp add: \<xi>_def min_def divide_simps norm_minus_commute)
  1583     finally have lte: "norm (\<xi> *\<^sub>R x - \<xi> *\<^sub>R z) < e" .
  1584     have \<xi>_aff: "\<xi> *\<^sub>R z + (1 - \<xi>) *\<^sub>R x \<in> affine hull S"
  1585       by (metis \<open>x \<in> S\<close> add.commute affine_affine_hull diff_add_cancel hull_inc mem_affine zaff)
  1586     have "\<xi> *\<^sub>R z + (1 - \<xi>) *\<^sub>R x \<in> S"
  1587       apply (rule ins [OF _ \<xi>_aff])
  1588       apply (simp add: algebra_simps lte)
  1589       done
  1590     then obtain l where l: "0 < l" "l < 1" and ls: "(l *\<^sub>R z + (1 - l) *\<^sub>R x) \<in> S"
  1591       apply (rule_tac l = \<xi> in that)
  1592       using \<open>e > 0\<close> \<open>z \<noteq> x\<close>  apply (auto simp: \<xi>_def)
  1593       done
  1594     then have i: "l *\<^sub>R z + (1 - l) *\<^sub>R x \<in> i"
  1595       using seq \<open>i \<in> F\<close> by auto
  1596     have "b i * l + (a i \<bullet> x) * (1 - l) < a i \<bullet> (l *\<^sub>R z + (1 - l) *\<^sub>R x)"
  1597       using l by (simp add: algebra_simps aiz)
  1598     also have "\<dots> \<le> b i" using i l
  1599       using faceq mem_Collect_eq \<open>i \<in> F\<close> by blast
  1600     finally have "(a i \<bullet> x) * (1 - l) < b i * (1 - l)"
  1601       by (simp add: algebra_simps)
  1602     with l show ?thesis
  1603       by simp
  1604   qed
  1605   moreover have "x \<in> rel_interior S"
  1606            if "x \<in> S" and less: "\<And>h. h \<in> F \<Longrightarrow> a h \<bullet> x < b h" for x
  1607   proof -
  1608     have 1: "\<And>h. h \<in> F \<Longrightarrow> x \<in> interior h"
  1609       by (metis interior_halfspace_le mem_Collect_eq less faceq)
  1610     have 2: "\<And>y. \<lbrakk>\<forall>h\<in>F. y \<in> interior h; y \<in> affine hull S\<rbrakk> \<Longrightarrow> y \<in> S"
  1611       by (metis IntI Inter_iff contra_subsetD interior_subset seq)
  1612     show ?thesis
  1613       apply (simp add: rel_interior \<open>x \<in> S\<close>)
  1614       apply (rule_tac x="\<Inter>h\<in>F. interior h" in exI)
  1615       apply (auto simp: \<open>finite F\<close> open_INT 1 2)
  1616       done
  1617   qed
  1618   ultimately show ?thesis by blast
  1619 qed
  1620 
  1621 
  1622 lemma polyhedron_Int_affine_parallel:
  1623   fixes S :: "'a :: euclidean_space set"
  1624   shows "polyhedron S \<longleftrightarrow>
  1625          (\<exists>F. finite F \<and>
  1626               S = (affine hull S) \<inter> (\<Inter>F) \<and>
  1627               (\<forall>h \<in> F. \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b} \<and>
  1628                              (\<forall>x \<in> affine hull S. (x + a) \<in> affine hull S)))"
  1629     (is "?lhs = ?rhs")
  1630 proof
  1631   assume ?lhs
  1632   then obtain F where "finite F" and seq: "S = (affine hull S) \<inter> \<Inter>F"
  1633                   and faces: "\<And>h. h \<in> F \<Longrightarrow> \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b}"
  1634     by (fastforce simp add: polyhedron_Int_affine)
  1635   then obtain a b where ab: "\<And>h. h \<in> F \<Longrightarrow> a h \<noteq> 0 \<and> h = {x. a h \<bullet> x \<le> b h}"
  1636     by metis
  1637   show ?rhs
  1638   proof -
  1639     have "\<exists>a' b'. a' \<noteq> 0 \<and>
  1640                   affine hull S \<inter> {x. a' \<bullet> x \<le> b'} = affine hull S \<inter> h \<and>
  1641                   (\<forall>w \<in> affine hull S. (w + a') \<in> affine hull S)"
  1642         if "h \<in> F" "~(affine hull S \<subseteq> h)" for h
  1643     proof -
  1644       have "a h \<noteq> 0" and "h = {x. a h \<bullet> x \<le> b h}" "h \<inter> \<Inter>F = \<Inter>F"
  1645         using \<open>h \<in> F\<close> ab by auto
  1646       then have "(affine hull S) \<inter> {x. a h \<bullet> x \<le> b h} \<noteq> {}"
  1647         by (metis (no_types) affine_hull_eq_empty inf.absorb_iff2 inf_assoc inf_bot_right inf_commute seq that(2))
  1648       moreover have "~ (affine hull S \<subseteq> {x. a h \<bullet> x \<le> b h})"
  1649         using \<open>h = {x. a h \<bullet> x \<le> b h}\<close> that(2) by blast
  1650       ultimately show ?thesis
  1651         using affine_parallel_slice [of "affine hull S"]
  1652         by (metis \<open>h = {x. a h \<bullet> x \<le> b h}\<close> affine_affine_hull)
  1653     qed
  1654     then obtain a b
  1655          where ab: "\<And>h. \<lbrakk>h \<in> F; ~ (affine hull S \<subseteq> h)\<rbrakk>
  1656              \<Longrightarrow> a h \<noteq> 0 \<and>
  1657                   affine hull S \<inter> {x. a h \<bullet> x \<le> b h} = affine hull S \<inter> h \<and>
  1658                   (\<forall>w \<in> affine hull S. (w + a h) \<in> affine hull S)"
  1659       by metis
  1660     have seq2: "S = affine hull S \<inter> (\<Inter>h\<in>{h \<in> F. \<not> affine hull S \<subseteq> h}. {x. a h \<bullet> x \<le> b h})"
  1661       by (subst seq) (auto simp: ab INT_extend_simps)
  1662     show ?thesis
  1663       apply (rule_tac x="(\<lambda>h. {x. a h \<bullet> x \<le> b h}) ` {h. h \<in> F \<and> ~(affine hull S \<subseteq> h)}" in exI)
  1664       apply (intro conjI seq2)
  1665         using \<open>finite F\<close> apply force
  1666        using ab apply blast
  1667        done
  1668   qed
  1669 next
  1670   assume ?rhs then show ?lhs
  1671     apply (simp add: polyhedron_Int_affine)
  1672     by metis
  1673 qed
  1674 
  1675 
  1676 proposition polyhedron_Int_affine_parallel_minimal:
  1677   fixes S :: "'a :: euclidean_space set"
  1678   shows "polyhedron S \<longleftrightarrow>
  1679          (\<exists>F. finite F \<and>
  1680               S = (affine hull S) \<inter> (\<Inter>F) \<and>
  1681               (\<forall>h \<in> F. \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b} \<and>
  1682                              (\<forall>x \<in> affine hull S. (x + a) \<in> affine hull S)) \<and>
  1683               (\<forall>F'. F' \<subset> F \<longrightarrow> S \<subset> (affine hull S) \<inter> (\<Inter>F')))"
  1684     (is "?lhs = ?rhs")
  1685 proof
  1686   assume ?lhs
  1687   then obtain f0
  1688            where f0: "finite f0"
  1689                  "S = (affine hull S) \<inter> (\<Inter>f0)"
  1690                    (is "?P f0")
  1691                  "\<forall>h \<in> f0. \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b} \<and>
  1692                              (\<forall>x \<in> affine hull S. (x + a) \<in> affine hull S)"
  1693                    (is "?Q f0")
  1694     by (force simp: polyhedron_Int_affine_parallel)
  1695   define n where "n = (LEAST n. \<exists>F. card F = n \<and> finite F \<and> ?P F \<and> ?Q F)"
  1696   have nf: "\<exists>F. card F = n \<and> finite F \<and> ?P F \<and> ?Q F"
  1697     apply (simp add: n_def)
  1698     apply (rule LeastI [where k = "card f0"])
  1699     using f0 apply auto
  1700     done
  1701   then obtain F where F: "card F = n" "finite F" and seq: "?P F" and aff: "?Q F"
  1702     by blast
  1703   then have "~ (finite g \<and> ?P g \<and> ?Q g)" if "card g < n" for g
  1704     using that by (auto simp: n_def dest!: not_less_Least)
  1705   then have *: "~ (?P g \<and> ?Q g)" if "g \<subset> F" for g
  1706     using that \<open>finite F\<close> psubset_card_mono \<open>card F = n\<close>
  1707     by (metis finite_Int inf.strict_order_iff)
  1708   have 1: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subseteq> affine hull S \<inter> \<Inter>F'"
  1709     by (subst seq) blast
  1710   have 2: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<noteq> affine hull S \<inter> \<Inter>F'"
  1711     apply (frule *)
  1712     by (metis aff subsetCE subset_iff_psubset_eq)
  1713   show ?rhs
  1714     by (metis \<open>finite F\<close> seq aff psubsetI 1 2)
  1715 next
  1716   assume ?rhs then show ?lhs
  1717     by (auto simp: polyhedron_Int_affine_parallel)
  1718 qed
  1719 
  1720 
  1721 lemma polyhedron_Int_affine_minimal:
  1722   fixes S :: "'a :: euclidean_space set"
  1723   shows "polyhedron S \<longleftrightarrow>
  1724          (\<exists>F. finite F \<and> S = (affine hull S) \<inter> \<Inter>F \<and>
  1725               (\<forall>h \<in> F. \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b}) \<and>
  1726               (\<forall>F'. F' \<subset> F \<longrightarrow> S \<subset> (affine hull S) \<inter> \<Inter>F'))"
  1727 apply (rule iffI)
  1728  apply (force simp: polyhedron_Int_affine_parallel_minimal elim!: ex_forward)
  1729 apply (auto simp: polyhedron_Int_affine elim!: ex_forward)
  1730 done
  1731 
  1732 proposition facet_of_polyhedron_explicit:
  1733   assumes "finite F"
  1734       and seq: "S = affine hull S \<inter> \<Inter>F"
  1735       and faceq: "\<And>h. h \<in> F \<Longrightarrow> a h \<noteq> 0 \<and> h = {x. a h \<bullet> x \<le> b h}"
  1736       and psub: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subset> affine hull S \<inter> \<Inter>F'"
  1737     shows "c facet_of S \<longleftrightarrow> (\<exists>h. h \<in> F \<and> c = S \<inter> {x. a h \<bullet> x = b h})"
  1738 proof (cases "S = {}")
  1739   case True with psub show ?thesis by force
  1740 next
  1741   case False
  1742   have "polyhedron S"
  1743     apply (simp add: polyhedron_Int_affine)
  1744     apply (rule_tac x=F in exI)
  1745     using assms  apply force
  1746     done
  1747   then have "convex S"
  1748     by (rule polyhedron_imp_convex)
  1749   with False rel_interior_eq_empty have "rel_interior S \<noteq> {}" by blast
  1750   then obtain x where "x \<in> rel_interior S" by auto
  1751   then obtain T where "open T" "x \<in> T" "x \<in> S" "T \<inter> affine hull S \<subseteq> S"
  1752     by (force simp: mem_rel_interior)
  1753   then have xaff: "x \<in> affine hull S" and xint: "x \<in> \<Inter>F"
  1754     using seq hull_inc by auto
  1755   have "rel_interior S = {x \<in> S. \<forall>h\<in>F. a h \<bullet> x < b h}"
  1756     by (rule rel_interior_polyhedron_explicit [OF \<open>finite F\<close> seq faceq psub])
  1757   with \<open>x \<in> rel_interior S\<close>
  1758   have [simp]: "\<And>h. h\<in>F \<Longrightarrow> a h \<bullet> x < b h" by blast
  1759   have *: "(S \<inter> {x. a h \<bullet> x = b h}) facet_of S" if "h \<in> F" for h
  1760   proof -
  1761     have "S \<subset> affine hull S \<inter> \<Inter>(F - {h})"
  1762       using psub that by (metis Diff_disjoint Diff_subset insert_disjoint(2) psubsetI)
  1763     then obtain z where zaff: "z \<in> affine hull S" and zint: "z \<in> \<Inter>(F - {h})" and "z \<notin> S"
  1764       by force
  1765     then have "z \<noteq> x" "z \<notin> h" using seq \<open>x \<in> S\<close> by auto
  1766     have "x \<in> h" using that xint by auto
  1767     then have able: "a h \<bullet> x \<le> b h"
  1768       using faceq that by blast
  1769     also have "... < a h \<bullet> z" using \<open>z \<notin> h\<close> faceq [OF that] xint by auto
  1770     finally have xltz: "a h \<bullet> x < a h \<bullet> z" .
  1771     define l where "l = (b h - a h \<bullet> x) / (a h \<bullet> z - a h \<bullet> x)"
  1772     define w where "w = (1 - l) *\<^sub>R x + l *\<^sub>R z"
  1773     have "0 < l" "l < 1"
  1774       using able xltz \<open>b h < a h \<bullet> z\<close> \<open>h \<in> F\<close>
  1775       by (auto simp: l_def divide_simps)
  1776     have awlt: "a i \<bullet> w < b i" if "i \<in> F" "i \<noteq> h" for i
  1777     proof -
  1778       have "(1 - l) * (a i \<bullet> x) < (1 - l) * b i"
  1779         by (simp add: \<open>l < 1\<close> \<open>i \<in> F\<close>)
  1780       moreover have "l * (a i \<bullet> z) \<le> l * b i"
  1781         apply (rule mult_left_mono)
  1782         apply (metis Diff_insert_absorb Inter_iff Set.set_insert \<open>h \<in> F\<close> faceq insertE mem_Collect_eq that zint)
  1783         using \<open>0 < l\<close>
  1784         apply simp
  1785         done
  1786       ultimately show ?thesis by (simp add: w_def algebra_simps)
  1787     qed
  1788     have weq: "a h \<bullet> w = b h"
  1789       using xltz unfolding w_def l_def
  1790       by (simp add: algebra_simps) (simp add: field_simps)
  1791     have "w \<in> affine hull S"
  1792       by (simp add: w_def mem_affine xaff zaff)
  1793     moreover have "w \<in> \<Inter>F"
  1794       using \<open>a h \<bullet> w = b h\<close> awlt faceq less_eq_real_def by blast
  1795     ultimately have "w \<in> S"
  1796       using seq by blast
  1797     with weq have "S \<inter> {x. a h \<bullet> x = b h} \<noteq> {}" by blast
  1798     moreover have "S \<inter> {x. a h \<bullet> x = b h} face_of S"
  1799       apply (rule face_of_Int_supporting_hyperplane_le)
  1800       apply (rule \<open>convex S\<close>)
  1801       apply (subst (asm) seq)
  1802       using faceq that apply fastforce
  1803       done
  1804     moreover have "affine hull (S \<inter> {x. a h \<bullet> x = b h}) =
  1805                    (affine hull S) \<inter> {x. a h \<bullet> x = b h}"
  1806     proof
  1807       show "affine hull (S \<inter> {x. a h \<bullet> x = b h}) \<subseteq> affine hull S \<inter> {x. a h \<bullet> x = b h}"
  1808         apply (intro Int_greatest hull_mono Int_lower1)
  1809         apply (metis affine_hull_eq affine_hyperplane hull_mono inf_le2)
  1810         done
  1811     next
  1812       show "affine hull S \<inter> {x. a h \<bullet> x = b h} \<subseteq> affine hull (S \<inter> {x. a h \<bullet> x = b h})"
  1813       proof
  1814         fix y
  1815         assume yaff: "y \<in> affine hull S \<inter> {y. a h \<bullet> y = b h}"
  1816         obtain T where "0 < T"
  1817                  and T: "\<And>j. \<lbrakk>j \<in> F; j \<noteq> h\<rbrakk> \<Longrightarrow> T * (a j \<bullet> y - a j \<bullet> w) \<le> b j - a j \<bullet> w"
  1818         proof (cases "F - {h} = {}")
  1819           case True then show ?thesis
  1820             by (rule_tac T=1 in that) auto
  1821         next
  1822           case False
  1823           then obtain h' where h': "h' \<in> F - {h}" by auto
  1824           define inff where "inff =
  1825             (INF j:F - {h}.
  1826               if 0 < a j \<bullet> y - a j \<bullet> w
  1827               then (b j - a j \<bullet> w) / (a j \<bullet> y - a j \<bullet> w)
  1828               else 1)"
  1829           have "0 < inff"
  1830             apply (simp add: inff_def)
  1831             apply (rule finite_imp_less_Inf)
  1832               using \<open>finite F\<close> apply blast
  1833              using h' apply blast
  1834             apply simp
  1835             using awlt apply (force simp: divide_simps)
  1836             done
  1837           moreover have "inff * (a j \<bullet> y - a j \<bullet> w) \<le> b j - a j \<bullet> w"
  1838                         if "j \<in> F" "j \<noteq> h" for j
  1839           proof (cases "a j \<bullet> w < a j \<bullet> y")
  1840             case True
  1841             then have "inff \<le> (b j - a j \<bullet> w) / (a j \<bullet> y - a j \<bullet> w)"
  1842               apply (simp add: inff_def)
  1843               apply (rule cInf_le_finite)
  1844               using \<open>finite F\<close> apply blast
  1845               apply (simp add: that split: if_split_asm)
  1846               done
  1847             then show ?thesis
  1848               using \<open>0 < inff\<close> awlt [OF that] mult_strict_left_mono
  1849               by (fastforce simp add: algebra_simps divide_simps split: if_split_asm)
  1850           next
  1851             case False
  1852             with \<open>0 < inff\<close> have "inff * (a j \<bullet> y - a j \<bullet> w) \<le> 0"
  1853               by (simp add: mult_le_0_iff)
  1854             also have "... < b j - a j \<bullet> w"
  1855               by (simp add: awlt that)
  1856             finally show ?thesis by simp
  1857           qed
  1858           ultimately show ?thesis
  1859             by (blast intro: that)
  1860         qed
  1861         define c where "c = (1 - T) *\<^sub>R w + T *\<^sub>R y"
  1862         have "(1 - T) *\<^sub>R w + T *\<^sub>R y \<in> j" if "j \<in> F" for j
  1863         proof (cases "j = h")
  1864           case True
  1865           have "(1 - T) *\<^sub>R w + T *\<^sub>R y \<in> {x. a h \<bullet> x \<le> b h}"
  1866             using weq yaff by (auto simp: algebra_simps)
  1867           with True faceq [OF that] show ?thesis by metis
  1868         next
  1869           case False
  1870           with T that have "(1 - T) *\<^sub>R w + T *\<^sub>R y \<in> {x. a j \<bullet> x \<le> b j}"
  1871             by (simp add: algebra_simps)
  1872           with faceq [OF that] show ?thesis by simp
  1873         qed
  1874         moreover have "(1 - T) *\<^sub>R w + T *\<^sub>R y \<in> affine hull S"
  1875           apply (rule affine_affine_hull [simplified affine_alt, rule_format])
  1876           apply (simp add: \<open>w \<in> affine hull S\<close>)
  1877           using yaff apply blast
  1878           done
  1879         ultimately have "c \<in> S"
  1880           using seq by (force simp: c_def)
  1881         moreover have "a h \<bullet> c = b h"
  1882           using yaff by (force simp: c_def algebra_simps weq)
  1883         ultimately have caff: "c \<in> affine hull (S \<inter> {y. a h \<bullet> y = b h})"
  1884           by (simp add: hull_inc)
  1885         have waff: "w \<in> affine hull (S \<inter> {y. a h \<bullet> y = b h})"
  1886           using \<open>w \<in> S\<close> weq by (blast intro: hull_inc)
  1887         have yeq: "y = (1 - inverse T) *\<^sub>R w + c /\<^sub>R T"
  1888           using \<open>0 < T\<close> by (simp add: c_def algebra_simps)
  1889         show "y \<in> affine hull (S \<inter> {y. a h \<bullet> y = b h})"
  1890           by (metis yeq affine_affine_hull [simplified affine_alt, rule_format, OF waff caff])
  1891       qed
  1892     qed
  1893     ultimately show ?thesis
  1894       apply (simp add: facet_of_def)
  1895       apply (subst aff_dim_affine_hull [symmetric])
  1896       using  \<open>b h < a h \<bullet> z\<close> zaff
  1897       apply (force simp: aff_dim_affine_Int_hyperplane)
  1898       done
  1899   qed
  1900   show ?thesis
  1901   proof
  1902     show "\<exists>h. h \<in> F \<and> c = S \<inter> {x. a h \<bullet> x = b h} \<Longrightarrow> c facet_of S"
  1903       using * by blast
  1904   next
  1905     assume "c facet_of S"
  1906     then have "c face_of S" "convex c" "c \<noteq> {}" and affc: "aff_dim c = aff_dim S - 1"
  1907       by (auto simp: facet_of_def face_of_imp_convex)
  1908     then obtain x where x: "x \<in> rel_interior c"
  1909       by (force simp: rel_interior_eq_empty)
  1910     then have "x \<in> c"
  1911       by (meson subsetD rel_interior_subset)
  1912     then have "x \<in> S"
  1913       using \<open>c facet_of S\<close> facet_of_imp_subset by blast
  1914     have rels: "rel_interior S = {x \<in> S. \<forall>h\<in>F. a h \<bullet> x < b h}"
  1915       by (rule rel_interior_polyhedron_explicit [OF assms])
  1916     have "c \<noteq> S"
  1917       using \<open>c facet_of S\<close> facet_of_irrefl by blast
  1918     then have "x \<notin> rel_interior S"
  1919       by (metis IntI empty_iff \<open>x \<in> c\<close> \<open>c \<noteq> S\<close> \<open>c face_of S\<close> face_of_disjoint_rel_interior)
  1920     with rels \<open>x \<in> S\<close> obtain i where "i \<in> F" and i: "a i \<bullet> x \<ge> b i"
  1921       by force
  1922     have "x \<in> {u. a i \<bullet> u \<le> b i}"
  1923       by (metis IntD2 InterE \<open>i \<in> F\<close> \<open>x \<in> S\<close> faceq seq)
  1924     then have "a i \<bullet> x \<le> b i" by simp
  1925     then have "a i \<bullet> x = b i" using i by auto
  1926     have "c \<subseteq> S \<inter> {x. a i \<bullet> x = b i}"
  1927       apply (rule subset_of_face_of [of _ S])
  1928         apply (simp add: "*" \<open>i \<in> F\<close> facet_of_imp_face_of)
  1929        apply (simp add: \<open>c face_of S\<close> face_of_imp_subset)
  1930       using \<open>a i \<bullet> x = b i\<close> \<open>x \<in> S\<close> x by blast
  1931     then have cface: "c face_of (S \<inter> {x. a i \<bullet> x = b i})"
  1932       by (meson \<open>c face_of S\<close> face_of_subset inf_le1)
  1933     have con: "convex (S \<inter> {x. a i \<bullet> x = b i})"
  1934       by (simp add: \<open>convex S\<close> convex_Int convex_hyperplane)
  1935     show "\<exists>h. h \<in> F \<and> c = S \<inter> {x. a h \<bullet> x = b h}"
  1936       apply (rule_tac x=i in exI)
  1937       apply (simp add: \<open>i \<in> F\<close>)
  1938       by (metis (no_types) * \<open>i \<in> F\<close> affc facet_of_def less_irrefl face_of_aff_dim_lt [OF con cface])
  1939   qed
  1940 qed
  1941 
  1942 
  1943 lemma face_of_polyhedron_subset_explicit:
  1944   fixes S :: "'a :: euclidean_space set"
  1945   assumes "finite F"
  1946       and seq: "S = affine hull S \<inter> \<Inter>F"
  1947       and faceq: "\<And>h. h \<in> F \<Longrightarrow> a h \<noteq> 0 \<and> h = {x. a h \<bullet> x \<le> b h}"
  1948       and psub: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subset> affine hull S \<inter> \<Inter>F'"
  1949       and c: "c face_of S" and "c \<noteq> {}" "c \<noteq> S"
  1950    obtains h where "h \<in> F" "c \<subseteq> S \<inter> {x. a h \<bullet> x = b h}"
  1951 proof -
  1952   have "c \<subseteq> S" using \<open>c face_of S\<close>
  1953     by (simp add: face_of_imp_subset)
  1954   have "polyhedron S"
  1955     apply (simp add: polyhedron_Int_affine)
  1956     by (metis \<open>finite F\<close> faceq seq)
  1957   then have "convex S"
  1958     by (simp add: polyhedron_imp_convex)
  1959   then have *: "(S \<inter> {x. a h \<bullet> x = b h}) face_of S" if "h \<in> F" for h
  1960     apply (rule face_of_Int_supporting_hyperplane_le)
  1961     using faceq seq that by fastforce
  1962   have "rel_interior c \<noteq> {}"
  1963     using c \<open>c \<noteq> {}\<close> face_of_imp_convex rel_interior_eq_empty by blast
  1964   then obtain x where "x \<in> rel_interior c" by auto
  1965   have rels: "rel_interior S = {x \<in> S. \<forall>h\<in>F. a h \<bullet> x < b h}"
  1966     by (rule rel_interior_polyhedron_explicit [OF \<open>finite F\<close> seq faceq psub])
  1967   then have xnot: "x \<notin> rel_interior S"
  1968     by (metis IntI \<open>x \<in> rel_interior c\<close> c \<open>c \<noteq> S\<close> contra_subsetD empty_iff face_of_disjoint_rel_interior rel_interior_subset)
  1969   then have "x \<in> S"
  1970     using \<open>c \<subseteq> S\<close> \<open>x \<in> rel_interior c\<close> rel_interior_subset by auto
  1971   then have xint: "x \<in> \<Inter>F"
  1972     using seq by blast
  1973   have "F \<noteq> {}" using assms
  1974     by (metis affine_Int affine_Inter affine_affine_hull ex_in_conv face_of_affine_trivial)
  1975   then obtain i where "i \<in> F" "~ (a i \<bullet> x < b i)"
  1976     using \<open>x \<in> S\<close> rels xnot by auto
  1977   with xint have "a i \<bullet> x = b i"
  1978     by (metis eq_iff mem_Collect_eq not_le Inter_iff faceq)
  1979   have face: "S \<inter> {x. a i \<bullet> x = b i} face_of S"
  1980     by (simp add: "*" \<open>i \<in> F\<close>)
  1981   show ?thesis
  1982     apply (rule_tac h = i in that)
  1983      apply (rule \<open>i \<in> F\<close>)
  1984     apply (rule subset_of_face_of [OF face \<open>c \<subseteq> S\<close>])
  1985     using \<open>a i \<bullet> x = b i\<close> \<open>x \<in> rel_interior c\<close> \<open>x \<in> S\<close> apply blast
  1986     done
  1987 qed
  1988 
  1989 text\<open>Initial part of proof duplicates that above\<close>
  1990 proposition face_of_polyhedron_explicit:
  1991   fixes S :: "'a :: euclidean_space set"
  1992   assumes "finite F"
  1993       and seq: "S = affine hull S \<inter> \<Inter>F"
  1994       and faceq: "\<And>h. h \<in> F \<Longrightarrow> a h \<noteq> 0 \<and> h = {x. a h \<bullet> x \<le> b h}"
  1995       and psub: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subset> affine hull S \<inter> \<Inter>F'"
  1996       and c: "c face_of S" and "c \<noteq> {}" "c \<noteq> S"
  1997     shows "c = \<Inter>{S \<inter> {x. a h \<bullet> x = b h} | h. h \<in> F \<and> c \<subseteq> S \<inter> {x. a h \<bullet> x = b h}}"
  1998 proof -
  1999   let ?ab = "\<lambda>h. {x. a h \<bullet> x = b h}"
  2000   have "c \<subseteq> S" using \<open>c face_of S\<close>
  2001     by (simp add: face_of_imp_subset)
  2002   have "polyhedron S"
  2003     apply (simp add: polyhedron_Int_affine)
  2004     by (metis \<open>finite F\<close> faceq seq)
  2005   then have "convex S"
  2006     by (simp add: polyhedron_imp_convex)
  2007   then have *: "(S \<inter> ?ab h) face_of S" if "h \<in> F" for h
  2008     apply (rule face_of_Int_supporting_hyperplane_le)
  2009     using faceq seq that by fastforce
  2010   have "rel_interior c \<noteq> {}"
  2011     using c \<open>c \<noteq> {}\<close> face_of_imp_convex rel_interior_eq_empty by blast
  2012   then obtain z where z: "z \<in> rel_interior c" by auto
  2013   have rels: "rel_interior S = {z \<in> S. \<forall>h\<in>F. a h \<bullet> z < b h}"
  2014     by (rule rel_interior_polyhedron_explicit [OF \<open>finite F\<close> seq faceq psub])
  2015   then have xnot: "z \<notin> rel_interior S"
  2016     by (metis IntI \<open>z \<in> rel_interior c\<close> c \<open>c \<noteq> S\<close> contra_subsetD empty_iff face_of_disjoint_rel_interior rel_interior_subset)
  2017   then have "z \<in> S"
  2018     using \<open>c \<subseteq> S\<close> \<open>z \<in> rel_interior c\<close> rel_interior_subset by auto
  2019   with seq have xint: "z \<in> \<Inter>F" by blast
  2020   have "open (\<Inter>h\<in>{h \<in> F. a h \<bullet> z < b h}. {w. a h \<bullet> w < b h})"
  2021     by (auto simp: \<open>finite F\<close> open_halfspace_lt open_INT)
  2022   then obtain e where "0 < e"
  2023                  "ball z e \<subseteq> (\<Inter>h\<in>{h \<in> F. a h \<bullet> z < b h}. {w. a h \<bullet> w < b h})"
  2024     by (auto intro: openE [of _ z])
  2025   then have e: "\<And>h. \<lbrakk>h \<in> F; a h \<bullet> z < b h\<rbrakk> \<Longrightarrow> ball z e \<subseteq> {w. a h \<bullet> w < b h}"
  2026     by blast
  2027   have "c \<subseteq> (S \<inter> ?ab h) \<longleftrightarrow> z \<in> S \<inter> ?ab h" if "h \<in> F" for h
  2028   proof
  2029     show "z \<in> S \<inter> ?ab h \<Longrightarrow> c \<subseteq> S \<inter> ?ab h"
  2030       apply (rule subset_of_face_of [of _ S])
  2031       using that \<open>c \<subseteq> S\<close> \<open>z \<in> rel_interior c\<close>
  2032       using facet_of_polyhedron_explicit [OF \<open>finite F\<close> seq faceq psub]
  2033             unfolding facet_of_def
  2034       apply auto
  2035       done
  2036   next
  2037     show "c \<subseteq> S \<inter> ?ab h \<Longrightarrow> z \<in> S \<inter> ?ab h"
  2038       using \<open>z \<in> rel_interior c\<close> rel_interior_subset by force
  2039   qed
  2040   then have **: "{S \<inter> ?ab h | h. h \<in> F \<and> c \<subseteq> S \<and> c \<subseteq> ?ab h} =
  2041                  {S \<inter> ?ab h |h. h \<in> F \<and> z \<in> S \<inter> ?ab h}"
  2042     by blast
  2043   have bsub: "ball z e \<inter> affine hull \<Inter>{S \<inter> ?ab h |h. h \<in> F \<and> a h \<bullet> z = b h}
  2044              \<subseteq> affine hull S \<inter> \<Inter>F \<inter> \<Inter>{?ab h |h. h \<in> F \<and> a h \<bullet> z = b h}"
  2045             if "i \<in> F" and i: "a i \<bullet> z = b i" for i
  2046   proof -
  2047     have sub: "ball z e \<inter> \<Inter>{?ab h |h. h \<in> F \<and> a h \<bullet> z = b h} \<subseteq> j"
  2048              if "j \<in> F" for j
  2049     proof -
  2050       have "a j \<bullet> z \<le> b j" using faceq that xint by auto
  2051       then consider "a j \<bullet> z < b j" | "a j \<bullet> z = b j" by linarith
  2052       then have "\<exists>G. G \<in> {?ab h |h. h \<in> F \<and> a h \<bullet> z = b h} \<and> ball z e \<inter> G \<subseteq> j"
  2053       proof cases
  2054         assume "a j \<bullet> z < b j"
  2055         then have "ball z e \<inter> {x. a i \<bullet> x = b i} \<subseteq> j"
  2056           using e [OF \<open>j \<in> F\<close>] faceq that
  2057           by (fastforce simp: ball_def)
  2058         then show ?thesis
  2059           by (rule_tac x="{x. a i \<bullet> x = b i}" in exI) (force simp: \<open>i \<in> F\<close> i)
  2060       next
  2061         assume eq: "a j \<bullet> z = b j"
  2062         with faceq that show ?thesis
  2063           by (rule_tac x="{x. a j \<bullet> x = b j}" in exI) (fastforce simp add: \<open>j \<in> F\<close>)
  2064       qed
  2065       then show ?thesis  by blast
  2066     qed
  2067     have 1: "affine hull \<Inter>{S \<inter> ?ab h |h. h \<in> F \<and> a h \<bullet> z = b h} \<subseteq> affine hull S"
  2068       apply (rule hull_mono)
  2069       using that \<open>z \<in> S\<close> by auto
  2070     have 2: "affine hull \<Inter>{S \<inter> ?ab h |h. h \<in> F \<and> a h \<bullet> z = b h}
  2071           \<subseteq> \<Inter>{?ab h |h. h \<in> F \<and> a h \<bullet> z = b h}"
  2072       by (rule hull_minimal) (auto intro: affine_hyperplane)
  2073     have 3: "ball z e \<inter> \<Inter>{?ab h |h. h \<in> F \<and> a h \<bullet> z = b h} \<subseteq> \<Inter>F"
  2074       by (iprover intro: sub Inter_greatest)
  2075     have *: "\<lbrakk>A \<subseteq> (B :: 'a set); A \<subseteq> C; E \<inter> C \<subseteq> D\<rbrakk> \<Longrightarrow> E \<inter> A \<subseteq> (B \<inter> D) \<inter> C"
  2076              for A B C D E  by blast
  2077     show ?thesis by (intro * 1 2 3)
  2078   qed
  2079   have "\<exists>h. h \<in> F \<and> c \<subseteq> ?ab h"
  2080     apply (rule face_of_polyhedron_subset_explicit [OF \<open>finite F\<close> seq faceq psub])
  2081     using assms by auto
  2082   then have fac: "\<Inter>{S \<inter> ?ab h |h. h \<in> F \<and> c \<subseteq> S \<inter> ?ab h} face_of S"
  2083     using * by (force simp: \<open>c \<subseteq> S\<close> intro: face_of_Inter)
  2084   have red:
  2085      "(\<And>a. P a \<Longrightarrow> T \<subseteq> S \<inter> \<Inter>{F x |x. P x}) \<Longrightarrow> T \<subseteq> \<Inter>{S \<inter> F x |x. P x}"
  2086      for P T F   by blast
  2087   have "ball z e \<inter> affine hull \<Inter>{S \<inter> ?ab h |h. h \<in> F \<and> a h \<bullet> z = b h}
  2088         \<subseteq> \<Inter>{S \<inter> ?ab h |h. h \<in> F \<and> a h \<bullet> z = b h}"
  2089     apply (rule red)
  2090     apply (metis seq bsub)
  2091     done
  2092   with \<open>0 < e\<close> have zinrel: "z \<in> rel_interior
  2093                     (\<Inter>{S \<inter> ?ab h |h. h \<in> F \<and> z \<in> S \<and> a h \<bullet> z = b h})"
  2094     by (auto simp: mem_rel_interior_ball \<open>z \<in> S\<close>)
  2095   show ?thesis
  2096     apply (rule face_of_eq [OF c fac])
  2097     using z zinrel apply (force simp: **)
  2098     done
  2099 qed
  2100 
  2101 
  2102 subsection\<open>More general corollaries from the explicit representation\<close>
  2103 
  2104 corollary facet_of_polyhedron:
  2105   assumes "polyhedron S" and "c facet_of S"
  2106   obtains a b where "a \<noteq> 0" "S \<subseteq> {x. a \<bullet> x \<le> b}" "c = S \<inter> {x. a \<bullet> x = b}"
  2107 proof -
  2108   obtain F where "finite F" and seq: "S = affine hull S \<inter> \<Inter>F"
  2109              and faces: "\<And>h. h \<in> F \<Longrightarrow> \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b}"
  2110              and min: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subset> (affine hull S) \<inter> \<Inter>F'"
  2111     using assms by (simp add: polyhedron_Int_affine_minimal) meson
  2112   then obtain a b where ab: "\<And>h. h \<in> F \<Longrightarrow> a h \<noteq> 0 \<and> h = {x. a h \<bullet> x \<le> b h}"
  2113     by metis
  2114   obtain i where "i \<in> F" and c: "c = S \<inter> {x. a i \<bullet> x = b i}"
  2115     using facet_of_polyhedron_explicit [OF \<open>finite F\<close> seq ab min] assms
  2116     by force
  2117   moreover have ssub: "S \<subseteq> {x. a i \<bullet> x \<le> b i}"
  2118      apply (subst seq)
  2119      using \<open>i \<in> F\<close> ab by auto
  2120   ultimately show ?thesis
  2121     by (rule_tac a = "a i" and b = "b i" in that) (simp_all add: ab)
  2122 qed
  2123 
  2124 corollary face_of_polyhedron:
  2125   assumes "polyhedron S" and "c face_of S" and "c \<noteq> {}" and "c \<noteq> S"
  2126     shows "c = \<Inter>{F. F facet_of S \<and> c \<subseteq> F}"
  2127 proof -
  2128   obtain F where "finite F" and seq: "S = affine hull S \<inter> \<Inter>F"
  2129              and faces: "\<And>h. h \<in> F \<Longrightarrow> \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b}"
  2130              and min: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subset> (affine hull S) \<inter> \<Inter>F'"
  2131     using assms by (simp add: polyhedron_Int_affine_minimal) meson
  2132   then obtain a b where ab: "\<And>h. h \<in> F \<Longrightarrow> a h \<noteq> 0 \<and> h = {x. a h \<bullet> x \<le> b h}"
  2133     by metis
  2134   show ?thesis
  2135     apply (subst face_of_polyhedron_explicit [OF \<open>finite F\<close> seq ab min])
  2136     apply (auto simp: assms facet_of_polyhedron_explicit [OF \<open>finite F\<close> seq ab min] cong: Collect_cong)
  2137     done
  2138 qed
  2139 
  2140 lemma face_of_polyhedron_subset_facet:
  2141   assumes "polyhedron S" and "c face_of S" and "c \<noteq> {}" and "c \<noteq> S"
  2142   obtains F where "F facet_of S" "c \<subseteq> F"
  2143 using face_of_polyhedron assms
  2144 by (metis (no_types, lifting) Inf_greatest antisym_conv face_of_imp_subset mem_Collect_eq)
  2145 
  2146 
  2147 lemma exposed_face_of_polyhedron:
  2148   assumes "polyhedron S"
  2149     shows "F exposed_face_of S \<longleftrightarrow> F face_of S"
  2150 proof
  2151   show "F exposed_face_of S \<Longrightarrow> F face_of S"
  2152     by (simp add: exposed_face_of_def)
  2153 next
  2154   assume "F face_of S"
  2155   show "F exposed_face_of S"
  2156   proof (cases "F = {} \<or> F = S")
  2157     case True then show ?thesis
  2158       using \<open>F face_of S\<close> exposed_face_of by blast
  2159   next
  2160     case False
  2161     then have "{g. g facet_of S \<and> F \<subseteq> g} \<noteq> {}"
  2162       by (metis Collect_empty_eq_bot \<open>F face_of S\<close> assms empty_def face_of_polyhedron_subset_facet)
  2163     moreover have "\<And>T. \<lbrakk>T facet_of S; F \<subseteq> T\<rbrakk> \<Longrightarrow> T exposed_face_of S"
  2164       by (metis assms exposed_face_of facet_of_imp_face_of facet_of_polyhedron)
  2165     ultimately have "\<Inter>{fa.
  2166        fa facet_of S \<and> F \<subseteq> fa} exposed_face_of S"
  2167       by (metis (no_types, lifting) mem_Collect_eq exposed_face_of_Inter)
  2168     then show ?thesis
  2169       using False
  2170       apply (subst face_of_polyhedron [OF assms \<open>F face_of S\<close>], auto)
  2171       done
  2172   qed
  2173 qed
  2174 
  2175 lemma face_of_polyhedron_polyhedron:
  2176   fixes S :: "'a :: euclidean_space set"
  2177   assumes "polyhedron S" "c face_of S" shows "polyhedron c"
  2178 by (metis assms face_of_imp_eq_affine_Int polyhedron_Int polyhedron_affine_hull polyhedron_imp_convex)
  2179 
  2180 lemma finite_polyhedron_faces:
  2181   fixes S :: "'a :: euclidean_space set"
  2182   assumes "polyhedron S"
  2183     shows "finite {F. F face_of S}"
  2184 proof -
  2185   obtain F where "finite F" and seq: "S = affine hull S \<inter> \<Inter>F"
  2186              and faces: "\<And>h. h \<in> F \<Longrightarrow> \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b}"
  2187              and min:   "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subset> (affine hull S) \<inter> \<Inter>F'"
  2188     using assms by (simp add: polyhedron_Int_affine_minimal) meson
  2189   then obtain a b where ab: "\<And>h. h \<in> F \<Longrightarrow> a h \<noteq> 0 \<and> h = {x. a h \<bullet> x \<le> b h}"
  2190     by metis
  2191   have "finite {\<Inter>{S \<inter> {x. a h \<bullet> x = b h} |h. h \<in> F'}| F'. F' \<in> Pow F}"
  2192     by (simp add: \<open>finite F\<close>)
  2193   moreover have "{F. F face_of S} - {{}, S} \<subseteq> {\<Inter>{S \<inter> {x. a h \<bullet> x = b h} |h. h \<in> F'}| F'. F' \<in> Pow F}"
  2194     apply clarify
  2195     apply (rename_tac c)
  2196     apply (drule face_of_polyhedron_explicit [OF \<open>finite F\<close> seq ab min, simplified], simp_all)
  2197     apply (erule ssubst)
  2198     apply (rule_tac x="{h \<in> F. c \<subseteq> S \<inter> {x. a h \<bullet> x = b h}}" in exI, auto)
  2199     done
  2200   ultimately show ?thesis
  2201     by (meson finite.emptyI finite.insertI finite_Diff2 finite_subset)
  2202 qed
  2203 
  2204 lemma finite_polyhedron_exposed_faces:
  2205    "polyhedron S \<Longrightarrow> finite {F. F exposed_face_of S}"
  2206 using exposed_face_of_polyhedron finite_polyhedron_faces by fastforce
  2207 
  2208 lemma finite_polyhedron_extreme_points:
  2209   fixes S :: "'a :: euclidean_space set"
  2210   shows "polyhedron S \<Longrightarrow> finite {v. v extreme_point_of S}"
  2211 apply (simp add: face_of_singleton [symmetric])
  2212 apply (rule finite_subset [OF _ finite_vimageI [OF finite_polyhedron_faces]], auto)
  2213 done
  2214 
  2215 lemma finite_polyhedron_facets:
  2216   fixes S :: "'a :: euclidean_space set"
  2217   shows "polyhedron S \<Longrightarrow> finite {F. F facet_of S}"
  2218 unfolding facet_of_def
  2219 by (blast intro: finite_subset [OF _ finite_polyhedron_faces])
  2220 
  2221 
  2222 proposition rel_interior_of_polyhedron:
  2223   fixes S :: "'a :: euclidean_space set"
  2224   assumes "polyhedron S"
  2225     shows "rel_interior S = S - \<Union>{F. F facet_of S}"
  2226 proof -
  2227   obtain F where "finite F" and seq: "S = affine hull S \<inter> \<Inter>F"
  2228              and faces: "\<And>h. h \<in> F \<Longrightarrow> \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b}"
  2229              and min: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subset> (affine hull S) \<inter> \<Inter>F'"
  2230     using assms by (simp add: polyhedron_Int_affine_minimal) meson
  2231   then obtain a b where ab: "\<And>h. h \<in> F \<Longrightarrow> a h \<noteq> 0 \<and> h = {x. a h \<bullet> x \<le> b h}"
  2232     by metis
  2233   have facet: "(c facet_of S) \<longleftrightarrow> (\<exists>h. h \<in> F \<and> c = S \<inter> {x. a h \<bullet> x = b h})" for c
  2234     by (rule facet_of_polyhedron_explicit [OF \<open>finite F\<close> seq ab min])
  2235   have rel: "rel_interior S = {x \<in> S. \<forall>h\<in>F. a h \<bullet> x < b h}"
  2236     by (rule rel_interior_polyhedron_explicit [OF \<open>finite F\<close> seq ab min])
  2237   have "a h \<bullet> x < b h" if "x \<in> S" "h \<in> F" and xnot: "x \<notin> \<Union>{F. F facet_of S}" for x h
  2238   proof -
  2239     have "x \<in> \<Inter>F" using seq that by force
  2240     with \<open>h \<in> F\<close> ab have "a h \<bullet> x \<le> b h" by auto
  2241     then consider "a h \<bullet> x < b h" | "a h \<bullet> x = b h" by linarith
  2242     then show ?thesis
  2243     proof cases
  2244       case 1 then show ?thesis .
  2245     next
  2246       case 2
  2247       have "Collect (op \<in> x) \<notin> Collect (op \<in> (\<Union>{A. A facet_of S}))"
  2248         using xnot by fastforce
  2249       then have "F \<notin> Collect (op \<in> h)"
  2250         using 2 \<open>x \<in> S\<close> facet by blast
  2251       with \<open>h \<in> F\<close> have "\<Inter>F \<subseteq> S \<inter> {x. a h \<bullet> x = b h}" by blast
  2252       with 2 that \<open>x \<in> \<Inter>F\<close> show ?thesis
  2253         apply simp
  2254         apply (drule_tac x="\<Inter>F" in spec)
  2255         apply (simp add: facet)
  2256         apply (drule_tac x=h in spec)
  2257         using seq by auto
  2258       qed
  2259   qed
  2260   moreover have "\<exists>h\<in>F. a h \<bullet> x \<ge> b h" if "x \<in> \<Union>{F. F facet_of S}" for x
  2261     using that by (force simp: facet)
  2262   ultimately show ?thesis
  2263     by (force simp: rel)
  2264 qed
  2265 
  2266 lemma rel_boundary_of_polyhedron:
  2267   fixes S :: "'a :: euclidean_space set"
  2268   assumes "polyhedron S"
  2269     shows "S - rel_interior S = \<Union> {F. F facet_of S}"
  2270 using facet_of_imp_subset by (fastforce simp add: rel_interior_of_polyhedron assms)
  2271 
  2272 lemma rel_frontier_of_polyhedron:
  2273   fixes S :: "'a :: euclidean_space set"
  2274   assumes "polyhedron S"
  2275     shows "rel_frontier S = \<Union> {F. F facet_of S}"
  2276 by (simp add: assms rel_frontier_def polyhedron_imp_closed rel_boundary_of_polyhedron)
  2277 
  2278 lemma rel_frontier_of_polyhedron_alt:
  2279   fixes S :: "'a :: euclidean_space set"
  2280   assumes "polyhedron S"
  2281     shows "rel_frontier S = \<Union> {F. F face_of S \<and> (F \<noteq> S)}"
  2282 apply (rule subset_antisym)
  2283   apply (force simp: rel_frontier_of_polyhedron facet_of_def assms)
  2284 using face_of_subset_rel_frontier by fastforce
  2285 
  2286 
  2287 text\<open>A characterization of polyhedra as having finitely many faces\<close>
  2288 
  2289 proposition polyhedron_eq_finite_exposed_faces:
  2290   fixes S :: "'a :: euclidean_space set"
  2291   shows "polyhedron S \<longleftrightarrow> closed S \<and> convex S \<and> finite {F. F exposed_face_of S}"
  2292          (is "?lhs = ?rhs")
  2293 proof
  2294   assume ?lhs
  2295   then show ?rhs
  2296     by (auto simp: polyhedron_imp_closed polyhedron_imp_convex finite_polyhedron_exposed_faces)
  2297 next
  2298   assume ?rhs
  2299   then have "closed S" "convex S" and fin: "finite {F. F exposed_face_of S}" by auto
  2300   show ?lhs
  2301   proof (cases "S = {}")
  2302     case True then show ?thesis by auto
  2303   next
  2304     case False
  2305     define F where "F = {h. h exposed_face_of S \<and> h \<noteq> {} \<and> h \<noteq> S}"
  2306     have "finite F" by (simp add: fin F_def)
  2307     have hface: "h face_of S"
  2308       and "\<exists>a b. a \<noteq> 0 \<and> S \<subseteq> {x. a \<bullet> x \<le> b} \<and> h = S \<inter> {x. a \<bullet> x = b}"
  2309       if "h \<in> F" for h
  2310       using exposed_face_of F_def that by simp_all auto
  2311     then obtain a b where ab:
  2312       "\<And>h. h \<in> F \<Longrightarrow> a h \<noteq> 0 \<and> S \<subseteq> {x. a h \<bullet> x \<le> b h} \<and> h = S \<inter> {x. a h \<bullet> x = b h}"
  2313       by metis
  2314     have *: "False"
  2315       if paff: "p \<in> affine hull S" and "p \<notin> S"
  2316       and pint: "p \<in> \<Inter>{{x. a h \<bullet> x \<le> b h} |h. h \<in> F}" for p
  2317     proof -
  2318       have "rel_interior S \<noteq> {}"
  2319         by (simp add: \<open>S \<noteq> {}\<close> \<open>convex S\<close> rel_interior_eq_empty)
  2320       then obtain c where c: "c \<in> rel_interior S" by auto
  2321       with rel_interior_subset have "c \<in> S"  by blast
  2322       have ccp: "closed_segment c p \<subseteq> affine hull S"
  2323         by (meson affine_affine_hull affine_imp_convex c closed_segment_subset hull_subset paff rel_interior_subset subsetCE)
  2324       obtain x where xcl: "x \<in> closed_segment c p" and "x \<in> S" and xnot: "x \<notin> rel_interior S"
  2325         using connected_openin [of "closed_segment c p"]
  2326         apply simp
  2327         apply (drule_tac x="closed_segment c p \<inter> rel_interior S" in spec)
  2328         apply (erule impE)
  2329          apply (force simp: openin_rel_interior openin_Int intro: openin_subtopology_Int_subset [OF _ ccp])
  2330         apply (drule_tac x="closed_segment c p \<inter> (- S)" in spec)
  2331         using rel_interior_subset \<open>closed S\<close> c \<open>p \<notin> S\<close> apply blast
  2332         done
  2333       then obtain \<mu> where "0 \<le> \<mu>" "\<mu> \<le> 1" and xeq: "x = (1 - \<mu>) *\<^sub>R c + \<mu> *\<^sub>R p"
  2334         by (auto simp: in_segment)
  2335       show False
  2336       proof (cases "\<mu>=0 \<or> \<mu>=1")
  2337         case True with xeq c xnot \<open>x \<in> S\<close> \<open>p \<notin> S\<close>
  2338         show False by auto
  2339       next
  2340         case False
  2341         then have xos: "x \<in> open_segment c p"
  2342           using \<open>x \<in> S\<close> c open_segment_def that(2) xcl xnot by auto
  2343         have xclo: "x \<in> closure S"
  2344           using \<open>x \<in> S\<close> closure_subset by blast
  2345         obtain d where "d \<noteq> 0"
  2346               and dle: "\<And>y. y \<in> closure S \<Longrightarrow> d \<bullet> x \<le> d \<bullet> y"
  2347               and dless: "\<And>y. y \<in> rel_interior S \<Longrightarrow> d \<bullet> x < d \<bullet> y"
  2348           by (metis supporting_hyperplane_relative_frontier [OF \<open>convex S\<close> xclo xnot])
  2349         have sex: "S \<inter> {y. d \<bullet> y = d \<bullet> x} exposed_face_of S"
  2350           by (simp add: \<open>closed S\<close> dle exposed_face_of_Int_supporting_hyperplane_ge [OF \<open>convex S\<close>])
  2351         have sne: "S \<inter> {y. d \<bullet> y = d \<bullet> x} \<noteq> {}"
  2352           using \<open>x \<in> S\<close> by blast
  2353         have sns: "S \<inter> {y. d \<bullet> y = d \<bullet> x} \<noteq> S"
  2354           by (metis (mono_tags) Int_Collect c subsetD dless not_le order_refl rel_interior_subset)
  2355         obtain h where "h \<in> F" "x \<in> h"
  2356           apply (rule_tac h="S \<inter> {y. d \<bullet> y = d \<bullet> x}" in that)
  2357           apply (simp_all add: F_def sex sne sns \<open>x \<in> S\<close>)
  2358           done
  2359         have abface: "{y. a h \<bullet> y = b h} face_of {y. a h \<bullet> y \<le> b h}"
  2360           using hyperplane_face_of_halfspace_le by blast
  2361         then have "c \<in> h"
  2362           using face_ofD [OF abface xos] \<open>c \<in> S\<close> \<open>h \<in> F\<close> ab pint \<open>x \<in> h\<close> by blast
  2363         with c have "h \<inter> rel_interior S \<noteq> {}" by blast
  2364         then show False
  2365           using \<open>h \<in> F\<close> F_def face_of_disjoint_rel_interior hface by auto
  2366       qed
  2367     qed
  2368     have "S \<subseteq> affine hull S \<inter> \<Inter>{{x. a h \<bullet> x \<le> b h} |h. h \<in> F}"
  2369       using ab by (auto simp: hull_subset)
  2370     moreover have "affine hull S \<inter> \<Inter>{{x. a h \<bullet> x \<le> b h} |h. h \<in> F} \<subseteq> S"
  2371       using * by blast
  2372     ultimately have "S = affine hull S \<inter> \<Inter> {{x. a h \<bullet> x \<le> b h} |h. h \<in> F}" ..
  2373     then show ?thesis
  2374       apply (rule ssubst)
  2375       apply (force intro: polyhedron_affine_hull polyhedron_halfspace_le simp: \<open>finite F\<close>)
  2376       done
  2377   qed
  2378 qed
  2379 
  2380 corollary polyhedron_eq_finite_faces:
  2381   fixes S :: "'a :: euclidean_space set"
  2382   shows "polyhedron S \<longleftrightarrow> closed S \<and> convex S \<and> finite {F. F face_of S}"
  2383          (is "?lhs = ?rhs")
  2384 proof
  2385   assume ?lhs
  2386   then show ?rhs
  2387     by (simp add: finite_polyhedron_faces polyhedron_imp_closed polyhedron_imp_convex)
  2388 next
  2389   assume ?rhs
  2390   then show ?lhs
  2391     by (force simp: polyhedron_eq_finite_exposed_faces exposed_face_of intro: finite_subset)
  2392 qed
  2393 
  2394 lemma polyhedron_linear_image_eq:
  2395   fixes h :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  2396   assumes "linear h" "bij h"
  2397     shows "polyhedron (h ` S) \<longleftrightarrow> polyhedron S"
  2398 proof -
  2399   have *: "{f. P f} = (image h) ` {f. P (h ` f)}" for P
  2400     apply safe
  2401     apply (rule_tac x="inv h ` x" in image_eqI)
  2402     apply (auto simp: \<open>bij h\<close> bij_is_surj image_f_inv_f)
  2403     done
  2404   have "inj h" using bij_is_inj assms by blast
  2405   then have injim: "inj_on (op ` h) A" for A
  2406     by (simp add: inj_on_def inj_image_eq_iff)
  2407   show ?thesis
  2408     using \<open>linear h\<close> \<open>inj h\<close>
  2409     apply (simp add: polyhedron_eq_finite_faces closed_injective_linear_image_eq)
  2410     apply (simp add: * face_of_linear_image [of h _ S, symmetric] finite_image_iff injim)
  2411     done
  2412 qed
  2413 
  2414 lemma polyhedron_negations:
  2415   fixes S :: "'a :: euclidean_space set"
  2416   shows   "polyhedron S \<Longrightarrow> polyhedron(image uminus S)"
  2417 by (auto simp: polyhedron_linear_image_eq linear_uminus bij_uminus)
  2418 
  2419 subsection\<open>Relation between polytopes and polyhedra\<close>
  2420 
  2421 lemma polytope_eq_bounded_polyhedron:
  2422   fixes S :: "'a :: euclidean_space set"
  2423   shows "polytope S \<longleftrightarrow> polyhedron S \<and> bounded S"
  2424          (is "?lhs = ?rhs")
  2425 proof
  2426   assume ?lhs
  2427   then show ?rhs
  2428     by (simp add: finite_polytope_faces polyhedron_eq_finite_faces
  2429                   polytope_imp_closed polytope_imp_convex polytope_imp_bounded)
  2430 next
  2431   assume ?rhs then show ?lhs
  2432     unfolding polytope_def
  2433     apply (rule_tac x="{v. v extreme_point_of S}" in exI)
  2434     apply (simp add: finite_polyhedron_extreme_points Krein_Milman_Minkowski compact_eq_bounded_closed polyhedron_imp_closed polyhedron_imp_convex)
  2435     done
  2436 qed
  2437 
  2438 lemma polytope_Int:
  2439   fixes S :: "'a :: euclidean_space set"
  2440   shows "\<lbrakk>polytope S; polytope T\<rbrakk> \<Longrightarrow> polytope(S \<inter> T)"
  2441 by (simp add: polytope_eq_bounded_polyhedron bounded_Int)
  2442 
  2443 
  2444 lemma polytope_Int_polyhedron:
  2445   fixes S :: "'a :: euclidean_space set"
  2446   shows "\<lbrakk>polytope S; polyhedron T\<rbrakk> \<Longrightarrow> polytope(S \<inter> T)"
  2447 by (simp add: bounded_Int polytope_eq_bounded_polyhedron)
  2448 
  2449 lemma polyhedron_Int_polytope:
  2450   fixes S :: "'a :: euclidean_space set"
  2451   shows "\<lbrakk>polyhedron S; polytope T\<rbrakk> \<Longrightarrow> polytope(S \<inter> T)"
  2452 by (simp add: bounded_Int polytope_eq_bounded_polyhedron)
  2453 
  2454 lemma polytope_imp_polyhedron:
  2455   fixes S :: "'a :: euclidean_space set"
  2456   shows "polytope S \<Longrightarrow> polyhedron S"
  2457 by (simp add: polytope_eq_bounded_polyhedron)
  2458 
  2459 lemma polytope_facet_exists:
  2460   fixes p :: "'a :: euclidean_space set"
  2461   assumes "polytope p" "0 < aff_dim p"
  2462   obtains F where "F facet_of p"
  2463 proof (cases "p = {}")
  2464   case True with assms show ?thesis by auto
  2465 next
  2466   case False
  2467   then obtain v where "v extreme_point_of p"
  2468     using extreme_point_exists_convex
  2469     by (blast intro: \<open>polytope p\<close> polytope_imp_compact polytope_imp_convex)
  2470   then
  2471   show ?thesis
  2472     by (metis face_of_polyhedron_subset_facet polytope_imp_polyhedron aff_dim_sing
  2473        all_not_in_conv assms face_of_singleton less_irrefl singletonI that)
  2474 qed
  2475 
  2476 lemma polyhedron_interval [iff]: "polyhedron(cbox a b)"
  2477 by (metis polytope_imp_polyhedron polytope_interval)
  2478 
  2479 lemma polyhedron_convex_hull:
  2480   fixes S :: "'a :: euclidean_space set"
  2481   shows "finite S \<Longrightarrow> polyhedron(convex hull S)"
  2482 by (simp add: polytope_convex_hull polytope_imp_polyhedron)
  2483 
  2484 
  2485 subsection\<open>Relative and absolute frontier of a polytope\<close>
  2486 
  2487 lemma rel_boundary_of_convex_hull:
  2488     fixes S :: "'a::euclidean_space set"
  2489     assumes "~ affine_dependent S"
  2490       shows "(convex hull S) - rel_interior(convex hull S) = (\<Union>a\<in>S. convex hull (S - {a}))"
  2491 proof -
  2492   have "finite S" by (metis assms aff_independent_finite)
  2493   then consider "card S = 0" | "card S = 1" | "2 \<le> card S" by arith
  2494   then show ?thesis
  2495   proof cases
  2496     case 1 then have "S = {}" by (simp add: \<open>finite S\<close>)
  2497     then show ?thesis by simp
  2498   next
  2499     case 2 show ?thesis
  2500       by (auto intro: card_1_singletonE [OF \<open>card S = 1\<close>])
  2501   next
  2502     case 3
  2503     with assms show ?thesis
  2504       by (auto simp: polyhedron_convex_hull rel_boundary_of_polyhedron facet_of_convex_hull_affine_independent_alt \<open>finite S\<close>)
  2505   qed
  2506 qed
  2507 
  2508 proposition frontier_of_convex_hull:
  2509     fixes S :: "'a::euclidean_space set"
  2510     assumes "card S = Suc (DIM('a))"
  2511       shows "frontier(convex hull S) = \<Union> {convex hull (S - {a}) | a. a \<in> S}"
  2512 proof (cases "affine_dependent S")
  2513   case True
  2514     have [iff]: "finite S"
  2515       using assms using card_infinite by force
  2516     then have ccs: "closed (convex hull S)"
  2517       by (simp add: compact_imp_closed finite_imp_compact_convex_hull)
  2518     { fix x T
  2519       assume "finite T" "T \<subseteq> S" "int (card T) \<le> aff_dim S + 1" "x \<in> convex hull T"
  2520       then have "S \<noteq> T"
  2521         using True \<open>finite S\<close> aff_dim_le_card affine_independent_iff_card by fastforce
  2522       then obtain a where "a \<in> S" "a \<notin> T"
  2523         using \<open>T \<subseteq> S\<close> by blast
  2524       then have "x \<in> (\<Union>a\<in>S. convex hull (S - {a}))"
  2525         using True affine_independent_iff_card [of S]
  2526         apply simp
  2527         apply (metis (no_types, hide_lams) Diff_eq_empty_iff Diff_insert0 \<open>a \<notin> T\<close> \<open>T \<subseteq> S\<close> \<open>x \<in> convex hull T\<close>  hull_mono insert_Diff_single   subsetCE)
  2528         done
  2529     } note * = this
  2530     have 1: "convex hull S \<subseteq> (\<Union> a\<in>S. convex hull (S - {a}))"
  2531       apply (subst caratheodory_aff_dim)
  2532       apply (blast intro: *)
  2533       done
  2534     have 2: "\<Union>((\<lambda>a. convex hull (S - {a})) ` S) \<subseteq> convex hull S"
  2535       by (rule Union_least) (metis (no_types, lifting)  Diff_subset hull_mono imageE)
  2536     show ?thesis using True
  2537       apply (simp add: segment_convex_hull frontier_def)
  2538       using interior_convex_hull_eq_empty [OF assms]
  2539       apply (simp add: closure_closed [OF ccs])
  2540       apply (rule subset_antisym)
  2541       using 1 apply blast
  2542       using 2 apply blast
  2543       done
  2544 next
  2545   case False
  2546   then have "frontier (convex hull S) = (convex hull S) - rel_interior(convex hull S)"
  2547     apply (simp add: rel_boundary_of_convex_hull [symmetric] frontier_def)
  2548     by (metis aff_independent_finite assms closure_convex_hull finite_imp_compact_convex_hull hull_hull interior_convex_hull_eq_empty rel_interior_nonempty_interior)
  2549   also have "... = \<Union>{convex hull (S - {a}) |a. a \<in> S}"
  2550   proof -
  2551     have "convex hull S - rel_interior (convex hull S) = rel_frontier (convex hull S)"
  2552       by (simp add: False aff_independent_finite polyhedron_convex_hull rel_boundary_of_polyhedron rel_frontier_of_polyhedron)
  2553     then show ?thesis
  2554       by (simp add: False rel_frontier_convex_hull_cases)
  2555   qed
  2556   finally show ?thesis .
  2557 qed
  2558 
  2559 subsection\<open>Special case of a triangle\<close>
  2560 
  2561 proposition frontier_of_triangle:
  2562     fixes a :: "'a::euclidean_space"
  2563     assumes "DIM('a) = 2"
  2564     shows "frontier(convex hull {a,b,c}) = closed_segment a b \<union> closed_segment b c \<union> closed_segment c a"
  2565           (is "?lhs = ?rhs")
  2566 proof (cases "b = a \<or> c = a \<or> c = b")
  2567   case True then show ?thesis
  2568     by (auto simp: assms segment_convex_hull frontier_def empty_interior_convex_hull insert_commute card_insert_le_m1 hull_inc insert_absorb)
  2569 next
  2570   case False then have [simp]: "card {a, b, c} = Suc (DIM('a))"
  2571     by (simp add: card_insert Set.insert_Diff_if assms)
  2572   show ?thesis
  2573   proof
  2574     show "?lhs \<subseteq> ?rhs"
  2575       using False
  2576       by (force simp: segment_convex_hull frontier_of_convex_hull insert_Diff_if insert_commute split: if_split_asm)
  2577     show "?rhs \<subseteq> ?lhs"
  2578       using False
  2579       apply (simp add: frontier_of_convex_hull segment_convex_hull)
  2580       apply (intro conjI subsetI)
  2581         apply (rule_tac X="convex hull {a,b}" in UnionI; force simp: Set.insert_Diff_if)
  2582        apply (rule_tac X="convex hull {b,c}" in UnionI; force)
  2583       apply (rule_tac X="convex hull {a,c}" in UnionI; force simp: insert_commute Set.insert_Diff_if)
  2584       done
  2585   qed
  2586 qed
  2587 
  2588 corollary inside_of_triangle:
  2589     fixes a :: "'a::euclidean_space"
  2590     assumes "DIM('a) = 2"
  2591     shows "inside (closed_segment a b \<union> closed_segment b c \<union> closed_segment c a) = interior(convex hull {a,b,c})"
  2592 by (metis assms frontier_of_triangle bounded_empty bounded_insert convex_convex_hull inside_frontier_eq_interior bounded_convex_hull)
  2593 
  2594 corollary interior_of_triangle:
  2595     fixes a :: "'a::euclidean_space"
  2596     assumes "DIM('a) = 2"
  2597     shows "interior(convex hull {a,b,c}) =
  2598            convex hull {a,b,c} - (closed_segment a b \<union> closed_segment b c \<union> closed_segment c a)"
  2599   using interior_subset
  2600   by (force simp: frontier_of_triangle [OF assms, symmetric] frontier_def Diff_Diff_Int)
  2601 
  2602 subsection\<open>Subdividing a cell complex\<close>
  2603 
  2604 lemma subdivide_interval:
  2605   fixes x::real
  2606   assumes "a < \<bar>x - y\<bar>" "0 < a"
  2607   obtains n where "n \<in> \<int>" "x < n * a \<and> n * a < y \<or> y <  n * a \<and> n * a < x"
  2608 proof -
  2609   consider "a + x < y" | "a + y < x"
  2610     using assms by linarith
  2611   then show ?thesis
  2612   proof cases
  2613     case 1
  2614     let ?n = "of_int (floor (x/a)) + 1"
  2615     have x: "x < ?n * a"
  2616       by (meson \<open>0 < a\<close> divide_less_eq floor_unique_iff)
  2617     have "?n * a \<le> a + x"
  2618       apply (simp add: algebra_simps)
  2619       by (metis \<open>0 < a\<close> floor_correct less_irrefl nonzero_mult_div_cancel_left real_mult_le_cancel_iff2 times_divide_eq_right)
  2620     also have "... < y"
  2621       by (rule 1)
  2622     finally have "?n * a < y" .
  2623     with x show ?thesis
  2624       using Ints_1 Ints_add Ints_of_int that by blast
  2625   next
  2626     case 2
  2627     let ?n = "of_int (floor (y/a)) + 1"
  2628     have y: "y < ?n * a"
  2629       by (meson \<open>0 < a\<close> divide_less_eq floor_unique_iff)
  2630     have "?n * a \<le> a + y"
  2631       apply (simp add: algebra_simps)
  2632       by (metis \<open>0 < a\<close> floor_correct less_irrefl nonzero_mult_div_cancel_left real_mult_le_cancel_iff2 times_divide_eq_right)
  2633     also have "... < x"
  2634       by (rule 2)
  2635     finally have "?n * a < x" .
  2636     then show ?thesis
  2637       using Ints_1 Ints_add Ints_of_int that y by blast
  2638   qed
  2639 qed
  2640 
  2641 
  2642 lemma cell_subdivision_lemma:
  2643   assumes "finite \<F>"
  2644       and "\<And>X. X \<in> \<F> \<Longrightarrow> polytope X"
  2645       and "\<And>X. X \<in> \<F> \<Longrightarrow> aff_dim X \<le> d"
  2646       and "\<And>X Y. \<lbrakk>X \<in> \<F>; Y \<in> \<F>\<rbrakk> \<Longrightarrow> (X \<inter> Y) face_of X \<and> (X \<inter> Y) face_of Y"
  2647       and "finite I"
  2648     shows "\<exists>\<F>'. \<Union>\<F>' = \<Union>\<F> \<and>
  2649                  finite \<F>' \<and>
  2650                  (\<forall>X \<in> \<F>'. polytope X) \<and>
  2651                  (\<forall>X \<in> \<F>'. aff_dim X \<le> d) \<and>
  2652                  (\<forall>X \<in> \<F>'. \<forall>Y \<in> \<F>'. X \<inter> Y face_of X \<and> X \<inter> Y face_of Y) \<and>
  2653                  (\<forall>X \<in> \<F>'. \<forall>x \<in> X. \<forall>y \<in> X. \<forall>a b.
  2654                           (a,b) \<in> I \<longrightarrow> a \<bullet> x \<le> b \<and> a \<bullet> y \<le> b \<or>
  2655                                         a \<bullet> x \<ge> b \<and> a \<bullet> y \<ge> b)"
  2656   using \<open>finite I\<close>
  2657 proof induction
  2658   case empty
  2659   then show ?case
  2660     by (rule_tac x="\<F>" in exI) (simp add: assms)
  2661 next
  2662   case (insert ab I)
  2663   then obtain \<F>' where eq: "\<Union>\<F>' = \<Union>\<F>" and "finite \<F>'"
  2664                    and poly: "\<And>X. X \<in> \<F>' \<Longrightarrow> polytope X"
  2665                    and aff: "\<And>X. X \<in> \<F>' \<Longrightarrow> aff_dim X \<le> d"
  2666                    and face: "\<And>X Y. \<lbrakk>X \<in> \<F>'; Y \<in> \<F>'\<rbrakk> \<Longrightarrow> X \<inter> Y face_of X \<and> X \<inter> Y face_of Y"
  2667                    and I: "\<And>X x y a b.  \<lbrakk>X \<in> \<F>'; x \<in> X; y \<in> X; (a,b) \<in> I\<rbrakk> \<Longrightarrow>
  2668                                     a \<bullet> x \<le> b \<and> a \<bullet> y \<le> b \<or> a \<bullet> x \<ge> b \<and> a \<bullet> y \<ge> b"
  2669     by (auto simp: that)
  2670   obtain a b where "ab = (a,b)"
  2671     by fastforce
  2672   let ?\<G> = "(\<lambda>X. X \<inter> {x. a \<bullet> x \<le> b}) ` \<F>' \<union> (\<lambda>X. X \<inter> {x. a \<bullet> x \<ge> b}) ` \<F>'"
  2673   have eqInt: "(S \<inter> Collect P) \<inter> (T \<inter> Collect Q) = (S \<inter> T) \<inter> (Collect P \<inter> Collect Q)" for S T::"'a set" and P Q
  2674     by blast
  2675   show ?case
  2676   proof (intro conjI exI)
  2677     show "\<Union>?\<G> = \<Union>\<F>"
  2678       by (force simp: eq [symmetric])
  2679     show "finite ?\<G>"
  2680       using \<open>finite \<F>'\<close> by force
  2681     show "\<forall>X \<in> ?\<G>. polytope X"
  2682       by (force simp: poly polytope_Int_polyhedron polyhedron_halfspace_le polyhedron_halfspace_ge)
  2683     show "\<forall>X \<in> ?\<G>. aff_dim X \<le> d"
  2684       by (auto; metis order_trans aff aff_dim_subset inf_le1)
  2685     show "\<forall>X \<in> ?\<G>. \<forall>x \<in> X. \<forall>y \<in> X. \<forall>a b.
  2686                           (a,b) \<in> insert ab I \<longrightarrow> a \<bullet> x \<le> b \<and> a \<bullet> y \<le> b \<or>
  2687                                                   a \<bullet> x \<ge> b \<and> a \<bullet> y \<ge> b"
  2688       using \<open>ab = (a, b)\<close> I by fastforce
  2689     show "\<forall>X \<in> ?\<G>. \<forall>Y \<in> ?\<G>. X \<inter> Y face_of X \<and> X \<inter> Y face_of Y"
  2690       by (auto simp: eqInt halfspace_Int_eq face_of_Int_Int face face_of_halfspace_le face_of_halfspace_ge)
  2691   qed
  2692 qed
  2693 
  2694 
  2695 proposition cell_complex_subdivision_exists:
  2696   fixes \<F> :: "'a::euclidean_space set set"
  2697   assumes "0 < e" "finite \<F>"
  2698       and poly: "\<And>X. X \<in> \<F> \<Longrightarrow> polytope X"
  2699       and aff: "\<And>X. X \<in> \<F> \<Longrightarrow> aff_dim X \<le> d"
  2700       and face: "\<And>X Y. \<lbrakk>X \<in> \<F>; Y \<in> \<F>\<rbrakk> \<Longrightarrow> X \<inter> Y face_of X \<and> X \<inter> Y face_of Y"
  2701   obtains "\<F>'" where "finite \<F>'" "\<Union>\<F>' = \<Union>\<F>" "\<And>X. X \<in> \<F>' \<Longrightarrow> diameter X < e"
  2702                 "\<And>X. X \<in> \<F>' \<Longrightarrow> polytope X" "\<And>X. X \<in> \<F>' \<Longrightarrow> aff_dim X \<le> d"
  2703                 "\<And>X Y. \<lbrakk>X \<in> \<F>'; Y \<in> \<F>'\<rbrakk> \<Longrightarrow> X \<inter> Y face_of X \<and> X \<inter> Y face_of Y"
  2704 proof -
  2705   have "bounded(\<Union>\<F>)"
  2706     by (simp add: \<open>finite \<F>\<close> poly bounded_Union polytope_imp_bounded)
  2707   then obtain B where "B > 0" and B: "\<And>x. x \<in> \<Union>\<F> \<Longrightarrow> norm x < B"
  2708     by (meson bounded_pos_less)
  2709   define C where "C \<equiv> {z \<in> \<int>. \<bar>z * e / 2 / real DIM('a)\<bar> \<le> B}"
  2710   define I where "I \<equiv> \<Union>i \<in> Basis. \<Union>j \<in> C. { (i::'a, j * e / 2 / DIM('a)) }"
  2711   have "finite C"
  2712     using finite_int_segment [of "-B / (e / 2 / DIM('a))" "B / (e / 2 / DIM('a))"]
  2713     apply (simp add: C_def)
  2714     apply (erule rev_finite_subset)
  2715     using \<open>0 < e\<close>
  2716     apply (auto simp: divide_simps)
  2717     done
  2718   then have "finite I"
  2719     by (simp add: I_def)
  2720   obtain \<F>' where eq: "\<Union>\<F>' = \<Union>\<F>" and "finite \<F>'"
  2721               and poly: "\<And>X. X \<in> \<F>' \<Longrightarrow> polytope X"
  2722               and aff: "\<And>X. X \<in> \<F>' \<Longrightarrow> aff_dim X \<le> d"
  2723               and face: "\<And>X Y. \<lbrakk>X \<in> \<F>'; Y \<in> \<F>'\<rbrakk> \<Longrightarrow> X \<inter> Y face_of X \<and> X \<inter> Y face_of Y"
  2724               and I: "\<And>X x y a b.  \<lbrakk>X \<in> \<F>'; x \<in> X; y \<in> X; (a,b) \<in> I\<rbrakk> \<Longrightarrow>
  2725                                      a \<bullet> x \<le> b \<and> a \<bullet> y \<le> b \<or> a \<bullet> x \<ge> b \<and> a \<bullet> y \<ge> b"
  2726     apply (rule exE [OF cell_subdivision_lemma])
  2727          apply (rule assms \<open>finite I\<close> | assumption)+
  2728     apply (auto intro: that)
  2729     done
  2730   show ?thesis
  2731   proof (rule_tac \<F>'="\<F>'" in that)
  2732     show "diameter X < e" if "X \<in> \<F>'" for X
  2733     proof -
  2734       have "diameter X \<le> e/2"
  2735       proof (rule diameter_le)
  2736         show "norm (x - y) \<le> e / 2" if "x \<in> X" "y \<in> X" for x y
  2737         proof -
  2738           have "norm x < B" "norm y < B"
  2739             using B \<open>X \<in> \<F>'\<close> eq that by fastforce+
  2740           have "norm (x - y) \<le> (\<Sum>b\<in>Basis. \<bar>(x-y) \<bullet> b\<bar>)"
  2741             by (rule norm_le_l1)
  2742           also have "... \<le> of_nat (DIM('a)) * (e / 2 / DIM('a))"
  2743           proof (rule sum_bounded_above)
  2744             fix i::'a
  2745             assume "i \<in> Basis"
  2746             then have I': "\<And>z b. \<lbrakk>z \<in> C; b = z * e / (2 * real DIM('a))\<rbrakk> \<Longrightarrow> i \<bullet> x \<le> b \<and> i \<bullet> y \<le> b \<or> i \<bullet> x \<ge> b \<and> i \<bullet> y \<ge> b"
  2747               using I \<open>X \<in> \<F>'\<close> that
  2748               by (fastforce simp: I_def)
  2749             show "\<bar>(x - y) \<bullet> i\<bar> \<le> e / 2 / real DIM('a)"
  2750             proof (rule ccontr)
  2751               assume "\<not> \<bar>(x - y) \<bullet> i\<bar> \<le> e / 2 / real DIM('a)"
  2752               then have xyi: "\<bar>i \<bullet> x - i \<bullet> y\<bar> > e / 2 / real DIM('a)"
  2753                 by (simp add: inner_commute inner_diff_right)
  2754               obtain n where "n \<in> \<int>" and n: "i \<bullet> x < n * (e / 2 / real DIM('a)) \<and> n * (e / 2 / real DIM('a)) < i \<bullet> y \<or> i \<bullet> y < n * (e / 2 / real DIM('a)) \<and> n * (e / 2 / real DIM('a)) < i \<bullet> x"
  2755                 using subdivide_interval [OF xyi] DIM_positive \<open>0 < e\<close>
  2756                 by (auto simp: zero_less_divide_iff)
  2757               have "\<bar>i \<bullet> x\<bar> < B"
  2758                 by (metis \<open>i \<in> Basis\<close> \<open>norm x < B\<close> inner_commute norm_bound_Basis_lt)
  2759               have "\<bar>i \<bullet> y\<bar> < B"
  2760                 by (metis \<open>i \<in> Basis\<close> \<open>norm y < B\<close> inner_commute norm_bound_Basis_lt)
  2761               have *: "\<bar>n * e\<bar> \<le> B * (2 * real DIM('a))"
  2762                       if "\<bar>ix\<bar> < B" "\<bar>iy\<bar> < B"
  2763                          and ix: "ix * (2 * real DIM('a)) < n * e"
  2764                          and iy: "n * e < iy * (2 * real DIM('a))" for ix iy
  2765               proof (rule abs_leI)
  2766                 have "iy * (2 * real DIM('a)) \<le> B * (2 * real DIM('a))"
  2767                   by (rule mult_right_mono) (use \<open>\<bar>iy\<bar> < B\<close> in linarith)+
  2768                 then show "n * e \<le> B * (2 * real DIM('a))"
  2769                   using iy by linarith
  2770               next
  2771                 have "- ix * (2 * real DIM('a)) \<le> B * (2 * real DIM('a))"
  2772                   by (rule mult_right_mono) (use \<open>\<bar>ix\<bar> < B\<close> in linarith)+
  2773                 then show "- (n * e) \<le> B * (2 * real DIM('a))"
  2774                   using ix by linarith
  2775               qed
  2776               have "n \<in> C"
  2777                 using \<open>n \<in> \<int>\<close> n  by (auto simp: C_def divide_simps intro: * \<open>\<bar>i \<bullet> x\<bar> < B\<close> \<open>\<bar>i \<bullet> y\<bar> < B\<close>)
  2778               show False
  2779                 using  I' [OF \<open>n \<in> C\<close> refl] n  by auto
  2780             qed
  2781           qed
  2782           also have "... = e / 2"
  2783             by simp
  2784           finally show ?thesis .
  2785         qed
  2786       qed (use \<open>0 < e\<close> in force)
  2787       also have "... < e"
  2788         by (simp add: \<open>0 < e\<close>)
  2789       finally show ?thesis .
  2790     qed
  2791   qed (auto simp: eq poly aff face \<open>finite \<F>'\<close>)
  2792 qed
  2793 
  2794 
  2795 subsection\<open>Simplicial complexes and triangulations\<close>
  2796 
  2797 subsubsection\<open>The notion of n-simplex for integer @{term"n \<ge> -1"}\<close>
  2798 
  2799 definition simplex :: "int \<Rightarrow> 'a::euclidean_space set \<Rightarrow> bool" (infix "simplex" 50)
  2800   where "n simplex S \<equiv> \<exists>C. ~(affine_dependent C) \<and> int(card C) = n + 1 \<and> S = convex hull C"
  2801 
  2802 lemma simplex:
  2803     "n simplex S \<longleftrightarrow> (\<exists>C. finite C \<and>
  2804                        ~(affine_dependent C) \<and>
  2805                        int(card C) = n + 1 \<and>
  2806                        S = convex hull C)"
  2807   by (auto simp add: simplex_def intro: aff_independent_finite)
  2808 
  2809 lemma simplex_convex_hull:
  2810    "~affine_dependent C \<and> int(card C) = n + 1 \<Longrightarrow> n simplex (convex hull C)"
  2811   by (auto simp add: simplex_def)
  2812 
  2813 lemma convex_simplex: "n simplex S \<Longrightarrow> convex S"
  2814   by (metis convex_convex_hull simplex_def)
  2815 
  2816 lemma compact_simplex: "n simplex S \<Longrightarrow> compact S"
  2817   unfolding simplex
  2818   using finite_imp_compact_convex_hull by blast
  2819 
  2820 lemma closed_simplex: "n simplex S \<Longrightarrow> closed S"
  2821   by (simp add: compact_imp_closed compact_simplex)
  2822 
  2823 lemma simplex_imp_polytope:
  2824    "n simplex S \<Longrightarrow> polytope S"
  2825   unfolding simplex_def polytope_def
  2826   using aff_independent_finite by blast
  2827 
  2828 lemma simplex_imp_polyhedron:
  2829    "n simplex S \<Longrightarrow> polyhedron S"
  2830   by (simp add: polytope_imp_polyhedron simplex_imp_polytope)
  2831 
  2832 lemma simplex_dim_ge: "n simplex S \<Longrightarrow> -1 \<le> n"
  2833   by (metis (no_types, hide_lams) aff_dim_geq affine_independent_iff_card diff_add_cancel diff_diff_eq2 simplex_def)
  2834 
  2835 lemma simplex_empty [simp]: "n simplex {} \<longleftrightarrow> n = -1"
  2836 proof
  2837   assume "n simplex {}"
  2838   then show "n = -1"
  2839     unfolding simplex by (metis card_empty convex_hull_eq_empty diff_0 diff_eq_eq of_nat_0)
  2840 next
  2841   assume "n = -1" then show "n simplex {}"
  2842     by (fastforce simp: simplex)
  2843 qed
  2844 
  2845 lemma simplex_minus_1 [simp]: "-1 simplex S \<longleftrightarrow> S = {}"
  2846   by (metis simplex cancel_comm_monoid_add_class.diff_cancel card_0_eq diff_minus_eq_add of_nat_eq_0_iff simplex_empty)
  2847 
  2848 
  2849 lemma aff_dim_simplex:
  2850    "n simplex S \<Longrightarrow> aff_dim S = n"
  2851   by (metis simplex add.commute add_diff_cancel_left' aff_dim_convex_hull affine_independent_iff_card)
  2852 
  2853 lemma zero_simplex_sing: "0 simplex {a}"
  2854   apply (simp add: simplex_def)
  2855   by (metis affine_independent_1 card_empty card_insert_disjoint convex_hull_singleton empty_iff finite.emptyI)
  2856 
  2857 lemma simplex_sing [simp]: "n simplex {a} \<longleftrightarrow> n = 0"
  2858   using aff_dim_simplex aff_dim_sing zero_simplex_sing by blast
  2859 
  2860 lemma simplex_zero: "0 simplex S \<longleftrightarrow> (\<exists>a. S = {a})"
  2861 apply (auto simp: )
  2862   using aff_dim_eq_0 aff_dim_simplex by blast
  2863 
  2864 lemma one_simplex_segment: "a \<noteq> b \<Longrightarrow> 1 simplex closed_segment a b"
  2865   apply (simp add: simplex_def)
  2866   apply (rule_tac x="{a,b}" in exI)
  2867   apply (auto simp: segment_convex_hull)
  2868   done
  2869 
  2870 lemma simplex_segment_cases:
  2871    "(if a = b then 0 else 1) simplex closed_segment a b"
  2872   by (auto simp: one_simplex_segment)
  2873 
  2874 lemma simplex_segment:
  2875    "\<exists>n. n simplex closed_segment a b"
  2876   using simplex_segment_cases by metis
  2877 
  2878 lemma polytope_lowdim_imp_simplex:
  2879   assumes "polytope P" "aff_dim P \<le> 1"
  2880   obtains n where "n simplex P"
  2881 proof (cases "P = {}")
  2882   case True
  2883   then show ?thesis
  2884     by (simp add: that)
  2885 next
  2886   case False
  2887   then show ?thesis
  2888     by (metis assms compact_convex_collinear_segment collinear_aff_dim polytope_imp_compact polytope_imp_convex simplex_segment_cases that)
  2889 qed
  2890 
  2891 lemma simplex_insert_dimplus1:
  2892   fixes n::int
  2893   assumes "n simplex S" and a: "a \<notin> affine hull S"
  2894   shows "(n+1) simplex (convex hull (insert a S))"
  2895 proof -
  2896   obtain C where C: "finite C" "~(affine_dependent C)" "int(card C) = n+1" and S: "S = convex hull C"
  2897     using assms unfolding simplex by force
  2898   show ?thesis
  2899     unfolding simplex
  2900   proof (intro exI conjI)
  2901       have "aff_dim S = n"
  2902         using aff_dim_simplex assms(1) by blast
  2903       moreover have "a \<notin> affine hull C"
  2904         using S a affine_hull_convex_hull by blast
  2905       moreover have "a \<notin> C"
  2906           using S a hull_inc by fastforce
  2907       ultimately show "\<not> affine_dependent (insert a C)"
  2908         by (simp add: C S aff_dim_convex_hull aff_dim_insert affine_independent_iff_card)
  2909   next
  2910     have "a \<notin> C"
  2911       using S a hull_inc by fastforce
  2912     then show "int (card (insert a C)) = n + 1 + 1"
  2913       by (simp add: C)
  2914   next
  2915     show "convex hull insert a S = convex hull (insert a C)"
  2916       by (simp add: S convex_hull_insert_segments)
  2917   qed (use C in auto)
  2918 qed
  2919 
  2920 end