src/HOL/Analysis/Polytope.thy
author Manuel Eberl <eberlm@in.tum.de>
Mon Mar 12 21:03:57 2018 +0100 (17 months ago)
changeset 67831 07f5588f2735
parent 67829 2a6ef5ba4822
child 67968 a5ad4c015d1c
permissions -rw-r--r--
Removed stray 'sledgehammer' invocation
     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     "((+) a ` T face_of (+) a ` S) \<longleftrightarrow> T face_of S"
    22 proof -
    23   have *: "\<And>a T S. T face_of S \<Longrightarrow> ((+) a ` T face_of (+) 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 IntQ Inter_UNIV_conv(2) assms(1) assms(2) ex_in_conv)
   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 lemma exposed_face_of_parallel:
   810    "T exposed_face_of S \<longleftrightarrow>
   811          T face_of S \<and>
   812          (\<exists>a b. S \<subseteq> {x. a \<bullet> x \<le> b} \<and> T = S \<inter> {x. a \<bullet> x = b} \<and>
   813                 (T \<noteq> {} \<longrightarrow> T \<noteq> S \<longrightarrow> a \<noteq> 0) \<and>
   814                 (T \<noteq> S \<longrightarrow> (\<forall>w \<in> affine hull S. (w + a) \<in> affine hull S)))"
   815   (is "?lhs = ?rhs")
   816 proof
   817   assume ?lhs then show ?rhs
   818   proof (clarsimp simp: exposed_face_of_def)
   819     fix a b
   820     assume faceS: "S \<inter> {x. a \<bullet> x = b} face_of S" and Ssub: "S \<subseteq> {x. a \<bullet> x \<le> b}" 
   821     show "\<exists>c d. S \<subseteq> {x. c \<bullet> x \<le> d} \<and>
   822                 S \<inter> {x. a \<bullet> x = b} = S \<inter> {x. c \<bullet> x = d} \<and>
   823                 (S \<inter> {x. a \<bullet> x = b} \<noteq> {} \<longrightarrow> S \<inter> {x. a \<bullet> x = b} \<noteq> S \<longrightarrow> c \<noteq> 0) \<and>
   824                 (S \<inter> {x. a \<bullet> x = b} \<noteq> S \<longrightarrow> (\<forall>w \<in> affine hull S. w + c \<in> affine hull S))"
   825     proof (cases "affine hull S \<inter> {x. -a \<bullet> x \<le> -b} = {} \<or> affine hull S \<subseteq> {x. - a \<bullet> x \<le> - b}")
   826       case True
   827       then show ?thesis
   828       proof
   829         assume "affine hull S \<inter> {x. - a \<bullet> x \<le> - b} = {}"
   830        then show ?thesis
   831          apply (rule_tac x="0" in exI)
   832          apply (rule_tac x="1" in exI)
   833          using hull_subset by fastforce
   834     next
   835       assume "affine hull S \<subseteq> {x. - a \<bullet> x \<le> - b}"
   836       then show ?thesis
   837          apply (rule_tac x="0" in exI)
   838          apply (rule_tac x="0" in exI)
   839         using Ssub hull_subset by fastforce
   840     qed
   841   next
   842     case False
   843     then obtain a' b' where "a' \<noteq> 0" 
   844       and le: "affine hull S \<inter> {x. a' \<bullet> x \<le> b'} = affine hull S \<inter> {x. - a \<bullet> x \<le> - b}" 
   845       and eq: "affine hull S \<inter> {x. a' \<bullet> x = b'} = affine hull S \<inter> {x. - a \<bullet> x = - b}" 
   846       and mem: "\<And>w. w \<in> affine hull S \<Longrightarrow> w + a' \<in> affine hull S"
   847       using affine_parallel_slice affine_affine_hull by metis 
   848     show ?thesis
   849     proof (intro conjI impI allI ballI exI)
   850       have *: "S \<subseteq> - (affine hull S \<inter> {x. P x}) \<union> affine hull S \<inter> {x. Q x} \<Longrightarrow> S \<subseteq> {x. ~P x \<or> Q x}" 
   851         for P Q 
   852         using hull_subset by fastforce  
   853       have "S \<subseteq> {x. ~ (a' \<bullet> x \<le> b') \<or> a' \<bullet> x = b'}"
   854         apply (rule *)
   855         apply (simp only: le eq)
   856         using Ssub by auto
   857       then show "S \<subseteq> {x. - a' \<bullet> x \<le> - b'}"
   858         by auto 
   859       show "S \<inter> {x. a \<bullet> x = b} = S \<inter> {x. - a' \<bullet> x = - b'}"
   860         using eq hull_subset [of S affine] by force
   861       show "\<lbrakk>S \<inter> {x. a \<bullet> x = b} \<noteq> {}; S \<inter> {x. a \<bullet> x = b} \<noteq> S\<rbrakk> \<Longrightarrow> - a' \<noteq> 0"
   862         using \<open>a' \<noteq> 0\<close> by auto
   863       show "w + - a' \<in> affine hull S"
   864         if "S \<inter> {x. a \<bullet> x = b} \<noteq> S" "w \<in> affine hull S" for w
   865       proof -
   866         have "w + 1 *\<^sub>R (w - (w + a')) \<in> affine hull S"
   867           using affine_affine_hull mem mem_affine_3_minus that(2) by blast
   868         then show ?thesis  by simp
   869       qed
   870     qed
   871   qed
   872 qed
   873 next
   874   assume ?rhs then show ?lhs
   875     unfolding exposed_face_of_def by blast
   876 qed
   877 
   878 subsection\<open>Extreme points of a set: its singleton faces\<close>
   879 
   880 definition extreme_point_of :: "['a::real_vector, 'a set] \<Rightarrow> bool"
   881                                (infixr "(extreme'_point'_of)" 50)
   882   where "x extreme_point_of S \<longleftrightarrow>
   883          x \<in> S \<and> (\<forall>a \<in> S. \<forall>b \<in> S. x \<notin> open_segment a b)"
   884 
   885 lemma extreme_point_of_stillconvex:
   886    "convex S \<Longrightarrow> (x extreme_point_of S \<longleftrightarrow> x \<in> S \<and> convex(S - {x}))"
   887   by (fastforce simp add: convex_contains_segment extreme_point_of_def open_segment_def)
   888 
   889 lemma face_of_singleton:
   890    "{x} face_of S \<longleftrightarrow> x extreme_point_of S"
   891 by (fastforce simp add: extreme_point_of_def face_of_def)
   892 
   893 lemma extreme_point_not_in_REL_INTERIOR:
   894     fixes S :: "'a::real_normed_vector set"
   895     shows "\<lbrakk>x extreme_point_of S; S \<noteq> {x}\<rbrakk> \<Longrightarrow> x \<notin> rel_interior S"
   896 apply (simp add: face_of_singleton [symmetric])
   897 apply (blast dest: face_of_disjoint_rel_interior)
   898 done
   899 
   900 lemma extreme_point_not_in_interior:
   901     fixes S :: "'a::{real_normed_vector, perfect_space} set"
   902     shows "x extreme_point_of S \<Longrightarrow> x \<notin> interior S"
   903 apply (case_tac "S = {x}")
   904 apply (simp add: empty_interior_finite)
   905 by (meson contra_subsetD extreme_point_not_in_REL_INTERIOR interior_subset_rel_interior)
   906 
   907 lemma extreme_point_of_face:
   908      "F face_of S \<Longrightarrow> v extreme_point_of F \<longleftrightarrow> v extreme_point_of S \<and> v \<in> F"
   909   by (meson empty_subsetI face_of_face face_of_singleton insert_subset)
   910 
   911 lemma extreme_point_of_convex_hull:
   912    "x extreme_point_of (convex hull S) \<Longrightarrow> x \<in> S"
   913 apply (simp add: extreme_point_of_stillconvex)
   914 using hull_minimal [of S "(convex hull S) - {x}" convex]
   915 using hull_subset [of S convex]
   916 apply blast
   917 done
   918 
   919 lemma extreme_points_of_convex_hull:
   920    "{x. x extreme_point_of (convex hull S)} \<subseteq> S"
   921 using extreme_point_of_convex_hull by auto
   922 
   923 lemma extreme_point_of_empty [simp]: "~ (x extreme_point_of {})"
   924   by (simp add: extreme_point_of_def)
   925 
   926 lemma extreme_point_of_singleton [iff]: "x extreme_point_of {a} \<longleftrightarrow> x = a"
   927   using extreme_point_of_stillconvex by auto
   928 
   929 lemma extreme_point_of_translation_eq:
   930    "(a + x) extreme_point_of (image (\<lambda>x. a + x) S) \<longleftrightarrow> x extreme_point_of S"
   931 by (auto simp: extreme_point_of_def)
   932 
   933 lemma extreme_points_of_translation:
   934    "{x. x extreme_point_of (image (\<lambda>x. a + x) S)} =
   935     (\<lambda>x. a + x) ` {x. x extreme_point_of S}"
   936 using extreme_point_of_translation_eq
   937 by auto (metis (no_types, lifting) image_iff mem_Collect_eq minus_add_cancel)
   938 
   939 lemma extreme_point_of_Int:
   940    "\<lbrakk>x extreme_point_of S; x extreme_point_of T\<rbrakk> \<Longrightarrow> x extreme_point_of (S \<inter> T)"
   941 by (simp add: extreme_point_of_def)
   942 
   943 lemma extreme_point_of_Int_supporting_hyperplane_le:
   944    "\<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"
   945 apply (simp add: face_of_singleton [symmetric])
   946 by (metis face_of_Int_supporting_hyperplane_le_strong convex_singleton)
   947 
   948 lemma extreme_point_of_Int_supporting_hyperplane_ge:
   949    "\<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"
   950 apply (simp add: face_of_singleton [symmetric])
   951 by (metis face_of_Int_supporting_hyperplane_ge_strong convex_singleton)
   952 
   953 lemma exposed_point_of_Int_supporting_hyperplane_le:
   954    "\<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"
   955 apply (simp add: exposed_face_of_def face_of_singleton)
   956 apply (force simp: extreme_point_of_Int_supporting_hyperplane_le)
   957 done
   958 
   959 lemma exposed_point_of_Int_supporting_hyperplane_ge:
   960     "\<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"
   961 using exposed_point_of_Int_supporting_hyperplane_le [of S "-a" "-b" c]
   962 by simp
   963 
   964 lemma extreme_point_of_convex_hull_insert:
   965    "\<lbrakk>finite S; a \<notin> convex hull S\<rbrakk> \<Longrightarrow> a extreme_point_of (convex hull (insert a S))"
   966 apply (case_tac "a \<in> S")
   967 apply (simp add: hull_inc)
   968 using face_of_convex_hulls [of "insert a S" "{a}"]
   969 apply (auto simp: face_of_singleton hull_same)
   970 done
   971 
   972 subsection\<open>Facets\<close>
   973 
   974 definition facet_of :: "['a::euclidean_space set, 'a set] \<Rightarrow> bool"
   975                     (infixr "(facet'_of)" 50)
   976   where "F facet_of S \<longleftrightarrow> F face_of S \<and> F \<noteq> {} \<and> aff_dim F = aff_dim S - 1"
   977 
   978 lemma facet_of_empty [simp]: "~ S facet_of {}"
   979   by (simp add: facet_of_def)
   980 
   981 lemma facet_of_irrefl [simp]: "~ S facet_of S "
   982   by (simp add: facet_of_def)
   983 
   984 lemma facet_of_imp_face_of: "F facet_of S \<Longrightarrow> F face_of S"
   985   by (simp add: facet_of_def)
   986 
   987 lemma facet_of_imp_subset: "F facet_of S \<Longrightarrow> F \<subseteq> S"
   988   by (simp add: face_of_imp_subset facet_of_def)
   989 
   990 lemma hyperplane_facet_of_halfspace_le:
   991    "a \<noteq> 0 \<Longrightarrow> {x. a \<bullet> x = b} facet_of {x. a \<bullet> x \<le> b}"
   992 unfolding facet_of_def hyperplane_eq_empty
   993 by (auto simp: hyperplane_face_of_halfspace_ge hyperplane_face_of_halfspace_le
   994            DIM_positive Suc_leI of_nat_diff aff_dim_halfspace_le)
   995 
   996 lemma hyperplane_facet_of_halfspace_ge:
   997     "a \<noteq> 0 \<Longrightarrow> {x. a \<bullet> x = b} facet_of {x. a \<bullet> x \<ge> b}"
   998 unfolding facet_of_def hyperplane_eq_empty
   999 by (auto simp: hyperplane_face_of_halfspace_le hyperplane_face_of_halfspace_ge
  1000            DIM_positive Suc_leI of_nat_diff aff_dim_halfspace_ge)
  1001 
  1002 lemma facet_of_halfspace_le:
  1003     "F facet_of {x. a \<bullet> x \<le> b} \<longleftrightarrow> a \<noteq> 0 \<and> F = {x. a \<bullet> x = b}"
  1004     (is "?lhs = ?rhs")
  1005 proof
  1006   assume c: ?lhs
  1007   with c facet_of_irrefl show ?rhs
  1008     by (force simp: aff_dim_halfspace_le facet_of_def face_of_halfspace_le cong: conj_cong split: if_split_asm)
  1009 next
  1010   assume ?rhs then show ?lhs
  1011     by (simp add: hyperplane_facet_of_halfspace_le)
  1012 qed
  1013 
  1014 lemma facet_of_halfspace_ge:
  1015     "F facet_of {x. a \<bullet> x \<ge> b} \<longleftrightarrow> a \<noteq> 0 \<and> F = {x. a \<bullet> x = b}"
  1016 using facet_of_halfspace_le [of F "-a" "-b"] by simp
  1017 
  1018 subsection \<open>Edges: faces of affine dimension 1\<close>
  1019 
  1020 definition edge_of :: "['a::euclidean_space set, 'a set] \<Rightarrow> bool"  (infixr "(edge'_of)" 50)
  1021   where "e edge_of S \<longleftrightarrow> e face_of S \<and> aff_dim e = 1"
  1022 
  1023 lemma edge_of_imp_subset:
  1024    "S edge_of T \<Longrightarrow> S \<subseteq> T"
  1025 by (simp add: edge_of_def face_of_imp_subset)
  1026 
  1027 subsection\<open>Existence of extreme points\<close>
  1028 
  1029 lemma different_norm_3_collinear_points:
  1030   fixes a :: "'a::euclidean_space"
  1031   assumes "x \<in> open_segment a b" "norm(a) = norm(b)" "norm(x) = norm(b)"
  1032   shows False
  1033 proof -
  1034   obtain u where "norm ((1 - u) *\<^sub>R a + u *\<^sub>R b) = norm b"
  1035              and "a \<noteq> b"
  1036              and u01: "0 < u" "u < 1"
  1037     using assms by (auto simp: open_segment_image_interval if_splits)
  1038   then have "(1 - u) *\<^sub>R a \<bullet> (1 - u) *\<^sub>R a + ((1 - u) * 2) *\<^sub>R a \<bullet> u *\<^sub>R b =
  1039              (1 - u * u) *\<^sub>R (a \<bullet> a)"
  1040     using assms by (simp add: norm_eq algebra_simps inner_commute)
  1041   then have "(1 - u) *\<^sub>R ((1 - u) *\<^sub>R a \<bullet> a + (2 * u) *\<^sub>R  a \<bullet> b) =
  1042              (1 - u) *\<^sub>R ((1 + u) *\<^sub>R (a \<bullet> a))"
  1043     by (simp add: algebra_simps)
  1044   then have "(1 - u) *\<^sub>R (a \<bullet> a) + (2 * u) *\<^sub>R (a \<bullet> b) = (1 + u) *\<^sub>R (a \<bullet> a)"
  1045     using u01 by auto
  1046   then have "a \<bullet> b = a \<bullet> a"
  1047     using u01 by (simp add: algebra_simps)
  1048   then have "a = b"
  1049     using \<open>norm(a) = norm(b)\<close> norm_eq vector_eq by fastforce
  1050   then show ?thesis
  1051     using \<open>a \<noteq> b\<close> by force
  1052 qed
  1053 
  1054 proposition extreme_point_exists_convex:
  1055   fixes S :: "'a::euclidean_space set"
  1056   assumes "compact S" "convex S" "S \<noteq> {}"
  1057   obtains x where "x extreme_point_of S"
  1058 proof -
  1059   obtain x where "x \<in> S" and xsup: "\<And>y. y \<in> S \<Longrightarrow> norm y \<le> norm x"
  1060     using distance_attains_sup [of S 0] assms by auto
  1061   have False if "a \<in> S" "b \<in> S" and x: "x \<in> open_segment a b" for a b
  1062   proof -
  1063     have noax: "norm a \<le> norm x" and nobx: "norm b \<le> norm x" using xsup that by auto
  1064     have "a \<noteq> b"
  1065       using empty_iff open_segment_idem x by auto
  1066     have *: "(1 - u) * na + u * nb < norm x" if "na < norm x"  "nb \<le> norm x" "0 < u" "u < 1" for na nb u
  1067     proof -
  1068       have "(1 - u) * na + u * nb < (1 - u) * norm x + u * nb"
  1069         by (simp add: that)
  1070       also have "... \<le> (1 - u) * norm x + u * norm x"
  1071         by (simp add: that)
  1072       finally have "(1 - u) * na + u * nb < (1 - u) * norm x + u * norm x" .
  1073       then show ?thesis
  1074       using scaleR_collapse [symmetric, of "norm x" u] by auto
  1075     qed
  1076     have "norm x < norm x" if "norm a < norm x"
  1077       using x
  1078       apply (clarsimp simp only: open_segment_image_interval \<open>a \<noteq> b\<close> if_False)
  1079       apply (rule norm_triangle_lt)
  1080       apply (simp add: norm_mult)
  1081       using * [of "norm a" "norm b"] nobx that
  1082         apply blast
  1083       done
  1084     moreover have "norm x < norm x" if "norm b < norm x"
  1085       using x
  1086       apply (clarsimp simp only: open_segment_image_interval \<open>a \<noteq> b\<close> if_False)
  1087       apply (rule norm_triangle_lt)
  1088       apply (simp add: norm_mult)
  1089       using * [of "norm b" "norm a" "1-u" for u] noax that
  1090         apply (simp add: add.commute)
  1091       done
  1092     ultimately have "~ (norm a < norm x) \<and> ~ (norm b < norm x)"
  1093       by auto
  1094     then show ?thesis
  1095       using different_norm_3_collinear_points noax nobx that(3) by fastforce
  1096   qed
  1097   then show ?thesis
  1098     apply (rule_tac x=x in that)
  1099     apply (force simp: extreme_point_of_def \<open>x \<in> S\<close>)
  1100     done
  1101 qed
  1102 
  1103 subsection\<open>Krein-Milman, the weaker form\<close>
  1104 
  1105 proposition Krein_Milman:
  1106   fixes S :: "'a::euclidean_space set"
  1107   assumes "compact S" "convex S"
  1108     shows "S = closure(convex hull {x. x extreme_point_of S})"
  1109 proof (cases "S = {}")
  1110   case True then show ?thesis   by simp
  1111 next
  1112   case False
  1113   have "closed S"
  1114     by (simp add: \<open>compact S\<close> compact_imp_closed)
  1115   have "closure (convex hull {x. x extreme_point_of S}) \<subseteq> S"
  1116     apply (rule closure_minimal [OF hull_minimal \<open>closed S\<close>])
  1117     using assms
  1118     apply (auto simp: extreme_point_of_def)
  1119     done
  1120   moreover have "u \<in> closure (convex hull {x. x extreme_point_of S})"
  1121                 if "u \<in> S" for u
  1122   proof (rule ccontr)
  1123     assume unot: "u \<notin> closure(convex hull {x. x extreme_point_of S})"
  1124     then obtain a b where "a \<bullet> u < b"
  1125           and ab: "\<And>x. x \<in> closure(convex hull {x. x extreme_point_of S}) \<Longrightarrow> b < a \<bullet> x"
  1126       using separating_hyperplane_closed_point [of "closure(convex hull {x. x extreme_point_of S})"]
  1127       by blast
  1128     have "continuous_on S ((\<bullet>) a)"
  1129       by (rule continuous_intros)+
  1130     then obtain m where "m \<in> S" and m: "\<And>y. y \<in> S \<Longrightarrow> a \<bullet> m \<le> a \<bullet> y"
  1131       using continuous_attains_inf [of S "\<lambda>x. a \<bullet> x"] \<open>compact S\<close> \<open>u \<in> S\<close>
  1132       by auto
  1133     define T where "T = S \<inter> {x. a \<bullet> x = a \<bullet> m}"
  1134     have "m \<in> T"
  1135       by (simp add: T_def \<open>m \<in> S\<close>)
  1136     moreover have "compact T"
  1137       by (simp add: T_def compact_Int_closed [OF \<open>compact S\<close> closed_hyperplane])
  1138     moreover have "convex T"
  1139       by (simp add: T_def convex_Int [OF \<open>convex S\<close> convex_hyperplane])
  1140     ultimately obtain v where v: "v extreme_point_of T"
  1141       using extreme_point_exists_convex [of T] by auto
  1142     then have "{v} face_of T"
  1143       by (simp add: face_of_singleton)
  1144     also have "T face_of S"
  1145       by (simp add: T_def m face_of_Int_supporting_hyperplane_ge [OF \<open>convex S\<close>])
  1146     finally have "v extreme_point_of S"
  1147       by (simp add: face_of_singleton)
  1148     then have "b < a \<bullet> v"
  1149       using closure_subset by (simp add: closure_hull hull_inc ab)
  1150     then show False
  1151       using \<open>a \<bullet> u < b\<close> \<open>{v} face_of T\<close> face_of_imp_subset m T_def that by fastforce
  1152   qed
  1153   ultimately show ?thesis
  1154     by blast
  1155 qed
  1156 
  1157 text\<open>Now the sharper form.\<close>
  1158 
  1159 lemma Krein_Milman_Minkowski_aux:
  1160   fixes S :: "'a::euclidean_space set"
  1161   assumes n: "dim S = n" and S: "compact S" "convex S" "0 \<in> S"
  1162     shows "0 \<in> convex hull {x. x extreme_point_of S}"
  1163 using n S
  1164 proof (induction n arbitrary: S rule: less_induct)
  1165   case (less n S) show ?case
  1166   proof (cases "0 \<in> rel_interior S")
  1167     case True with Krein_Milman show ?thesis
  1168       by (metis subsetD convex_convex_hull convex_rel_interior_closure less.prems(2) less.prems(3) rel_interior_subset)
  1169   next
  1170     case False
  1171     have "rel_interior S \<noteq> {}"
  1172       by (simp add: rel_interior_convex_nonempty_aux less)
  1173     then obtain c where c: "c \<in> rel_interior S" by blast
  1174     obtain a where "a \<noteq> 0"
  1175               and le_ay: "\<And>y. y \<in> S \<Longrightarrow> a \<bullet> 0 \<le> a \<bullet> y"
  1176               and less_ay: "\<And>y. y \<in> rel_interior S \<Longrightarrow> a \<bullet> 0 < a \<bullet> y"
  1177       by (blast intro: supporting_hyperplane_rel_boundary intro!: less False)
  1178     have face: "S \<inter> {x. a \<bullet> x = 0} face_of S"
  1179       apply (rule face_of_Int_supporting_hyperplane_ge [OF \<open>convex S\<close>])
  1180       using le_ay by auto
  1181     then have co: "compact (S \<inter> {x. a \<bullet> x = 0})" "convex (S \<inter> {x. a \<bullet> x = 0})"
  1182       using less.prems by (blast intro: face_of_imp_compact face_of_imp_convex)+
  1183     have "a \<bullet> y = 0" if "y \<in> span (S \<inter> {x. a \<bullet> x = 0})" for y
  1184     proof -
  1185       have "y \<in> span {x. a \<bullet> x = 0}"
  1186         by (metis inf.cobounded2 span_mono subsetCE that)
  1187       then show ?thesis
  1188         by (blast intro: span_induct [OF _ subspace_hyperplane])
  1189     qed
  1190     then have "dim (S \<inter> {x. a \<bullet> x = 0}) < n"
  1191       by (metis (no_types) less_ay c subsetD dim_eq_span inf.strict_order_iff
  1192            inf_le1 \<open>dim S = n\<close> not_le rel_interior_subset span_0 span_clauses(1))
  1193     then have "0 \<in> convex hull {x. x extreme_point_of (S \<inter> {x. a \<bullet> x = 0})}"
  1194       by (rule less.IH) (auto simp: co less.prems)
  1195     then show ?thesis
  1196       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)
  1197   qed
  1198 qed
  1199 
  1200 
  1201 theorem Krein_Milman_Minkowski:
  1202   fixes S :: "'a::euclidean_space set"
  1203   assumes "compact S" "convex S"
  1204     shows "S = convex hull {x. x extreme_point_of S}"
  1205 proof
  1206   show "S \<subseteq> convex hull {x. x extreme_point_of S}"
  1207   proof
  1208     fix a assume [simp]: "a \<in> S"
  1209     have 1: "compact ((+) (- a) ` S)"
  1210       by (simp add: \<open>compact S\<close> compact_translation)
  1211     have 2: "convex ((+) (- a) ` S)"
  1212       by (simp add: \<open>convex S\<close> convex_translation)
  1213     show a_invex: "a \<in> convex hull {x. x extreme_point_of S}"
  1214       using Krein_Milman_Minkowski_aux [OF refl 1 2]
  1215             convex_hull_translation [of "-a"]
  1216       by (auto simp: extreme_points_of_translation translation_assoc)
  1217     qed
  1218 next
  1219   show "convex hull {x. x extreme_point_of S} \<subseteq> S"
  1220   proof -
  1221     have "{a. a extreme_point_of S} \<subseteq> S"
  1222       using extreme_point_of_def by blast
  1223     then show ?thesis
  1224       by (simp add: \<open>convex S\<close> hull_minimal)
  1225   qed
  1226 qed
  1227 
  1228 
  1229 subsection\<open>Applying it to convex hulls of explicitly indicated finite sets\<close>
  1230 
  1231 lemma Krein_Milman_polytope:
  1232   fixes S :: "'a::euclidean_space set"
  1233   shows
  1234    "finite S
  1235        \<Longrightarrow> convex hull S =
  1236            convex hull {x. x extreme_point_of (convex hull S)}"
  1237 by (simp add: Krein_Milman_Minkowski finite_imp_compact_convex_hull)
  1238 
  1239 lemma extreme_points_of_convex_hull_eq:
  1240   fixes S :: "'a::euclidean_space set"
  1241   shows
  1242    "\<lbrakk>compact S; \<And>T. T \<subset> S \<Longrightarrow> convex hull T \<noteq> convex hull S\<rbrakk>
  1243         \<Longrightarrow> {x. x extreme_point_of (convex hull S)} = S"
  1244 by (metis (full_types) Krein_Milman_Minkowski compact_convex_hull convex_convex_hull extreme_points_of_convex_hull psubsetI)
  1245 
  1246 
  1247 lemma extreme_point_of_convex_hull_eq:
  1248   fixes S :: "'a::euclidean_space set"
  1249   shows
  1250    "\<lbrakk>compact S; \<And>T. T \<subset> S \<Longrightarrow> convex hull T \<noteq> convex hull S\<rbrakk>
  1251     \<Longrightarrow> (x extreme_point_of (convex hull S) \<longleftrightarrow> x \<in> S)"
  1252 using extreme_points_of_convex_hull_eq by auto
  1253 
  1254 lemma extreme_point_of_convex_hull_convex_independent:
  1255   fixes S :: "'a::euclidean_space set"
  1256   assumes "compact S" and S: "\<And>a. a \<in> S \<Longrightarrow> a \<notin> convex hull (S - {a})"
  1257   shows "(x extreme_point_of (convex hull S) \<longleftrightarrow> x \<in> S)"
  1258 proof -
  1259   have "convex hull T \<noteq> convex hull S" if "T \<subset> S" for T
  1260   proof -
  1261     obtain a where  "T \<subseteq> S" "a \<in> S" "a \<notin> T" using \<open>T \<subset> S\<close> by blast
  1262     then show ?thesis
  1263       by (metis (full_types) Diff_eq_empty_iff Diff_insert0 S hull_mono hull_subset insert_Diff_single subsetCE)
  1264   qed
  1265   then show ?thesis
  1266     by (rule extreme_point_of_convex_hull_eq [OF \<open>compact S\<close>])
  1267 qed
  1268 
  1269 lemma extreme_point_of_convex_hull_affine_independent:
  1270   fixes S :: "'a::euclidean_space set"
  1271   shows
  1272    "~ affine_dependent S
  1273          \<Longrightarrow> (x extreme_point_of (convex hull S) \<longleftrightarrow> x \<in> S)"
  1274 by (metis aff_independent_finite affine_dependent_def affine_hull_convex_hull extreme_point_of_convex_hull_convex_independent finite_imp_compact hull_inc)
  1275 
  1276 text\<open>Elementary proofs exist, not requiring Euclidean spaces and all this development\<close>
  1277 lemma extreme_point_of_convex_hull_2:
  1278   fixes x :: "'a::euclidean_space"
  1279   shows "x extreme_point_of (convex hull {a,b}) \<longleftrightarrow> x = a \<or> x = b"
  1280 proof -
  1281   have "x extreme_point_of (convex hull {a,b}) \<longleftrightarrow> x \<in> {a,b}"
  1282     by (intro extreme_point_of_convex_hull_affine_independent affine_independent_2)
  1283   then show ?thesis
  1284     by simp
  1285 qed
  1286 
  1287 lemma extreme_point_of_segment:
  1288   fixes x :: "'a::euclidean_space"
  1289   shows
  1290    "x extreme_point_of closed_segment a b \<longleftrightarrow> x = a \<or> x = b"
  1291 by (simp add: extreme_point_of_convex_hull_2 segment_convex_hull)
  1292 
  1293 lemma face_of_convex_hull_subset:
  1294   fixes S :: "'a::euclidean_space set"
  1295   assumes "compact S" and T: "T face_of (convex hull S)"
  1296   obtains s' where "s' \<subseteq> S" "T = convex hull s'"
  1297 apply (rule_tac s' = "{x. x extreme_point_of T}" in that)
  1298 using T extreme_point_of_convex_hull extreme_point_of_face apply blast
  1299 by (metis (no_types) Krein_Milman_Minkowski assms compact_convex_hull convex_convex_hull face_of_imp_compact face_of_imp_convex)
  1300 
  1301 
  1302 lemma face_of_convex_hull_aux:
  1303   assumes eq: "x *\<^sub>R p = u *\<^sub>R a + v *\<^sub>R b + w *\<^sub>R c"
  1304     and x: "u + v + w = x" "x \<noteq> 0" and S: "affine S" "a \<in> S" "b \<in> S" "c \<in> S"
  1305   shows "p \<in> S"
  1306 proof -
  1307   have "p = (u *\<^sub>R a + v *\<^sub>R b + w *\<^sub>R c) /\<^sub>R x"
  1308     by (metis \<open>x \<noteq> 0\<close> eq mult.commute right_inverse scaleR_one scaleR_scaleR)
  1309   moreover have "affine hull {a,b,c} \<subseteq> S"
  1310     by (simp add: S hull_minimal)
  1311   moreover have "(u *\<^sub>R a + v *\<^sub>R b + w *\<^sub>R c) /\<^sub>R x \<in> affine hull {a,b,c}"
  1312     apply (simp add: affine_hull_3)
  1313     apply (rule_tac x="u/x" in exI)
  1314     apply (rule_tac x="v/x" in exI)
  1315     apply (rule_tac x="w/x" in exI)
  1316     using x apply (auto simp: algebra_simps divide_simps)
  1317     done
  1318   ultimately show ?thesis by force
  1319 qed
  1320 
  1321 proposition face_of_convex_hull_insert_eq:
  1322   fixes a :: "'a :: euclidean_space"
  1323   assumes "finite S" and a: "a \<notin> affine hull S"
  1324   shows "(F face_of (convex hull (insert a S)) \<longleftrightarrow>
  1325           F face_of (convex hull S) \<or>
  1326           (\<exists>F'. F' face_of (convex hull S) \<and> F = convex hull (insert a F')))"
  1327          (is "F face_of ?CAS \<longleftrightarrow> _")
  1328 proof safe
  1329   assume F: "F face_of ?CAS"
  1330     and *: "\<nexists>F'. F' face_of convex hull S \<and> F = convex hull insert a F'"
  1331   obtain T where T: "T \<subseteq> insert a S" and FeqT: "F = convex hull T"
  1332     by (metis F \<open>finite S\<close> compact_insert finite_imp_compact face_of_convex_hull_subset)
  1333   show "F face_of convex hull S"
  1334   proof (cases "a \<in> T")
  1335     case True
  1336     have "F = convex hull insert a (convex hull T \<inter> convex hull S)"
  1337     proof
  1338       have "T \<subseteq> insert a (convex hull T \<inter> convex hull S)"
  1339         using T hull_subset by fastforce
  1340       then show "F \<subseteq> convex hull insert a (convex hull T \<inter> convex hull S)"
  1341         by (simp add: FeqT hull_mono)
  1342       show "convex hull insert a (convex hull T \<inter> convex hull S) \<subseteq> F"
  1343         apply (rule hull_minimal)
  1344         using True by (auto simp: \<open>F = convex hull T\<close> hull_inc)
  1345     qed
  1346     moreover have "convex hull T \<inter> convex hull S face_of convex hull S"
  1347       by (metis F FeqT convex_convex_hull face_of_slice hull_mono inf.absorb_iff2 subset_insertI)
  1348     ultimately show ?thesis
  1349       using * by force
  1350   next
  1351     case False
  1352     then show ?thesis
  1353       by (metis FeqT F T face_of_subset hull_mono subset_insert subset_insertI)
  1354   qed
  1355 next
  1356   assume "F face_of convex hull S"
  1357   show "F face_of ?CAS"
  1358     by (simp add: \<open>F face_of convex hull S\<close> a face_of_convex_hull_insert \<open>finite S\<close>)
  1359 next
  1360   fix F
  1361   assume F: "F face_of convex hull S"
  1362   show "convex hull insert a F face_of ?CAS"
  1363   proof (cases "S = {}")
  1364     case True
  1365     then show ?thesis
  1366       using F face_of_affine_eq by auto
  1367   next
  1368     case False
  1369     have anotc: "a \<notin> convex hull S"
  1370       by (metis (no_types) a affine_hull_convex_hull hull_inc)
  1371     show ?thesis
  1372     proof (cases "F = {}")
  1373       case True show ?thesis
  1374         using anotc by (simp add: \<open>F = {}\<close> \<open>finite S\<close> extreme_point_of_convex_hull_insert face_of_singleton)
  1375     next
  1376       case False
  1377       have "convex hull insert a F \<subseteq> ?CAS"
  1378         by (simp add: F a \<open>finite S\<close> convex_hull_subset face_of_convex_hull_insert face_of_imp_subset hull_inc)
  1379       moreover
  1380       have "(\<exists>y v. (1 - ub) *\<^sub>R a + ub *\<^sub>R b = (1 - v) *\<^sub>R a + v *\<^sub>R y \<and>
  1381                    0 \<le> v \<and> v \<le> 1 \<and> y \<in> F) \<and>
  1382             (\<exists>x u. (1 - uc) *\<^sub>R a + uc *\<^sub>R c = (1 - u) *\<^sub>R a + u *\<^sub>R x \<and>
  1383                    0 \<le> u \<and> u \<le> 1 \<and> x \<in> F)"
  1384         if *: "(1 - ux) *\<^sub>R a + ux *\<^sub>R x
  1385                \<in> open_segment ((1 - ub) *\<^sub>R a + ub *\<^sub>R b) ((1 - uc) *\<^sub>R a + uc *\<^sub>R c)"
  1386           and "0 \<le> ub" "ub \<le> 1" "0 \<le> uc" "uc \<le> 1" "0 \<le> ux" "ux \<le> 1"
  1387           and b: "b \<in> convex hull S" and c: "c \<in> convex hull S" and "x \<in> F"
  1388         for b c ub uc ux x
  1389       proof -
  1390         obtain v where ne: "(1 - ub) *\<^sub>R a + ub *\<^sub>R b \<noteq> (1 - uc) *\<^sub>R a + uc *\<^sub>R c"
  1391           and eq: "(1 - ux) *\<^sub>R a + ux *\<^sub>R x =
  1392                     (1 - v) *\<^sub>R ((1 - ub) *\<^sub>R a + ub *\<^sub>R b) + v *\<^sub>R ((1 - uc) *\<^sub>R a + uc *\<^sub>R c)"
  1393           and "0 < v" "v < 1"
  1394           using * by (auto simp: in_segment)
  1395         then have 0: "((1 - ux) - ((1 - v) * (1 - ub) + v * (1 - uc))) *\<^sub>R a +
  1396                       (ux *\<^sub>R x - (((1 - v) * ub) *\<^sub>R b + (v * uc) *\<^sub>R c)) = 0"
  1397           by (auto simp: algebra_simps)
  1398         then have "((1 - ux) - ((1 - v) * (1 - ub) + v * (1 - uc))) *\<^sub>R a =
  1399                    ((1 - v) * ub) *\<^sub>R b + (v * uc) *\<^sub>R c + (-ux) *\<^sub>R x"
  1400           by (auto simp: algebra_simps)
  1401         then have "a \<in> affine hull S" if "1 - ux - ((1 - v) * (1 - ub) + v * (1 - uc)) \<noteq> 0"
  1402           apply (rule face_of_convex_hull_aux)
  1403           using b c that apply (auto simp: algebra_simps)
  1404           using F convex_hull_subset_affine_hull face_of_imp_subset \<open>x \<in> F\<close> apply blast+
  1405           done
  1406         then have "1 - ux - ((1 - v) * (1 - ub) + v * (1 - uc)) = 0"
  1407           using a by blast
  1408         with 0 have equx: "(1 - v) * ub + v * uc = ux"
  1409           and uxx: "ux *\<^sub>R x = (((1 - v) * ub) *\<^sub>R b + (v * uc) *\<^sub>R c)"
  1410           by auto (auto simp: algebra_simps)
  1411         show ?thesis
  1412         proof (cases "uc = 0")
  1413           case True
  1414           then show ?thesis
  1415             using equx 0 \<open>0 \<le> ub\<close> \<open>ub \<le> 1\<close> \<open>v < 1\<close> \<open>x \<in> F\<close>
  1416             apply (auto simp: algebra_simps)
  1417              apply (rule_tac x=x in exI, simp)
  1418              apply (rule_tac x=ub in exI, auto)
  1419              apply (metis add.left_neutral diff_eq_eq less_irrefl mult.commute mult_cancel_right1 real_vector.scale_cancel_left real_vector.scale_left_diff_distrib)
  1420             using \<open>x \<in> F\<close> \<open>uc \<le> 1\<close> apply blast
  1421             done
  1422         next
  1423           case False
  1424           show ?thesis
  1425           proof (cases "ub = 0")
  1426             case True
  1427             then show ?thesis
  1428               using equx 0 \<open>0 \<le> uc\<close> \<open>uc \<le> 1\<close> \<open>0 < v\<close> \<open>x \<in> F\<close> \<open>uc \<noteq> 0\<close> by (force simp: algebra_simps)
  1429           next
  1430             case False
  1431             then have "0 < ub" "0 < uc"
  1432               using \<open>uc \<noteq> 0\<close> \<open>0 \<le> ub\<close> \<open>0 \<le> uc\<close> by auto
  1433             then have "ux \<noteq> 0"
  1434               by (metis \<open>0 < v\<close> \<open>v < 1\<close> diff_ge_0_iff_ge dual_order.strict_implies_order equx leD le_add_same_cancel2 zero_le_mult_iff zero_less_mult_iff)
  1435             have "b \<in> F \<and> c \<in> F"
  1436             proof (cases "b = c")
  1437               case True
  1438               then show ?thesis
  1439                 by (metis \<open>ux \<noteq> 0\<close> equx real_vector.scale_cancel_left scaleR_add_left uxx \<open>x \<in> F\<close>)
  1440             next
  1441               case False
  1442               have "x = (((1 - v) * ub) *\<^sub>R b + (v * uc) *\<^sub>R c) /\<^sub>R ux"
  1443                 by (metis \<open>ux \<noteq> 0\<close> uxx mult.commute right_inverse scaleR_one scaleR_scaleR)
  1444               also have "... = (1 - v * uc / ux) *\<^sub>R b + (v * uc / ux) *\<^sub>R c"
  1445                 using \<open>ux \<noteq> 0\<close> equx apply (auto simp: algebra_simps divide_simps)
  1446                 by (metis add.commute add_diff_eq add_divide_distrib diff_add_cancel scaleR_add_left)
  1447               finally have "x = (1 - v * uc / ux) *\<^sub>R b + (v * uc / ux) *\<^sub>R c" .
  1448               then have "x \<in> open_segment b c"
  1449                 apply (simp add: in_segment \<open>b \<noteq> c\<close>)
  1450                 apply (rule_tac x="(v * uc) / ux" in exI)
  1451                 using \<open>0 \<le> ux\<close> \<open>ux \<noteq> 0\<close> \<open>0 < uc\<close> \<open>0 < v\<close> \<open>0 < ub\<close> \<open>v < 1\<close> equx
  1452                 apply (force simp: algebra_simps divide_simps)
  1453                 done
  1454               then show ?thesis
  1455                 by (rule face_ofD [OF F _ b c \<open>x \<in> F\<close>])
  1456             qed
  1457             with \<open>0 \<le> ub\<close> \<open>ub \<le> 1\<close> \<open>0 \<le> uc\<close> \<open>uc \<le> 1\<close> show ?thesis by blast
  1458           qed
  1459         qed
  1460       qed
  1461       moreover have "convex hull F = F"
  1462         by (meson F convex_hull_eq face_of_imp_convex)
  1463       ultimately show ?thesis
  1464         unfolding face_of_def by (fastforce simp: convex_hull_insert_alt \<open>S \<noteq> {}\<close> \<open>F \<noteq> {}\<close>)
  1465     qed
  1466   qed
  1467 qed
  1468 
  1469 lemma face_of_convex_hull_insert2:
  1470   fixes a :: "'a :: euclidean_space"
  1471   assumes S: "finite S" and a: "a \<notin> affine hull S" and F: "F face_of convex hull S"
  1472   shows "convex hull (insert a F) face_of convex hull (insert a S)"
  1473   by (metis F face_of_convex_hull_insert_eq [OF S a])
  1474 
  1475 proposition face_of_convex_hull_affine_independent:
  1476   fixes S :: "'a::euclidean_space set"
  1477   assumes "~ affine_dependent S"
  1478     shows "(T face_of (convex hull S) \<longleftrightarrow> (\<exists>c. c \<subseteq> S \<and> T = convex hull c))"
  1479           (is "?lhs = ?rhs")
  1480 proof
  1481   assume ?lhs
  1482   then show ?rhs
  1483     by (meson \<open>T face_of convex hull S\<close> aff_independent_finite assms face_of_convex_hull_subset finite_imp_compact)
  1484 next
  1485   assume ?rhs
  1486   then obtain c where "c \<subseteq> S" and T: "T = convex hull c"
  1487     by blast
  1488   have "affine hull c \<inter> affine hull (S - c) = {}"
  1489     apply (rule disjoint_affine_hull [OF assms \<open>c \<subseteq> S\<close>], auto)
  1490     done
  1491   then have "affine hull c \<inter> convex hull (S - c) = {}"
  1492     using convex_hull_subset_affine_hull by fastforce
  1493   then show ?lhs
  1494     by (metis face_of_convex_hulls \<open>c \<subseteq> S\<close> aff_independent_finite assms T)
  1495 qed
  1496 
  1497 lemma facet_of_convex_hull_affine_independent:
  1498   fixes S :: "'a::euclidean_space set"
  1499   assumes "~ affine_dependent S"
  1500     shows "T facet_of (convex hull S) \<longleftrightarrow>
  1501            T \<noteq> {} \<and> (\<exists>u. u \<in> S \<and> T = convex hull (S - {u}))"
  1502           (is "?lhs = ?rhs")
  1503 proof
  1504   assume ?lhs
  1505   then have "T face_of (convex hull S)" "T \<noteq> {}"
  1506         and afft: "aff_dim T = aff_dim (convex hull S) - 1"
  1507     by (auto simp: facet_of_def)
  1508   then obtain c where "c \<subseteq> S" and c: "T = convex hull c"
  1509     by (auto simp: face_of_convex_hull_affine_independent [OF assms])
  1510   then have affs: "aff_dim S = aff_dim c + 1"
  1511     by (metis aff_dim_convex_hull afft eq_diff_eq)
  1512   have "~ affine_dependent c"
  1513     using \<open>c \<subseteq> S\<close> affine_dependent_subset assms by blast
  1514   with affs have "card (S - c) = 1"
  1515     apply (simp add: aff_dim_affine_independent [symmetric] aff_dim_convex_hull)
  1516     by (metis aff_dim_affine_independent aff_independent_finite One_nat_def \<open>c \<subseteq> S\<close> add.commute
  1517                 add_diff_cancel_right' assms card_Diff_subset card_mono of_nat_1 of_nat_diff of_nat_eq_iff)
  1518   then obtain u where u: "u \<in> S - c"
  1519     by (metis DiffI \<open>c \<subseteq> S\<close> aff_independent_finite assms cancel_comm_monoid_add_class.diff_cancel
  1520                 card_Diff_subset subsetI subset_antisym zero_neq_one)
  1521   then have u: "S = insert u c"
  1522     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)
  1523   have "T = convex hull (c - {u})"
  1524     by (metis Diff_empty Diff_insert0 \<open>T facet_of convex hull S\<close> c facet_of_irrefl insert_absorb u)
  1525   with \<open>T \<noteq> {}\<close> show ?rhs
  1526     using c u by auto
  1527 next
  1528   assume ?rhs
  1529   then obtain u where "T \<noteq> {}" "u \<in> S" and u: "T = convex hull (S - {u})"
  1530     by (force simp: facet_of_def)
  1531   then have "\<not> S \<subseteq> {u}"
  1532     using \<open>T \<noteq> {}\<close> u by auto
  1533   have [simp]: "aff_dim (convex hull (S - {u})) = aff_dim (convex hull S) - 1"
  1534     using assms \<open>u \<in> S\<close>
  1535     apply (simp add: aff_dim_convex_hull affine_dependent_def)
  1536     apply (drule bspec, assumption)
  1537     by (metis add_diff_cancel_right' aff_dim_insert insert_Diff [of u S])
  1538   show ?lhs
  1539     apply (subst u)
  1540     apply (simp add: \<open>\<not> S \<subseteq> {u}\<close> facet_of_def face_of_convex_hull_affine_independent [OF assms], blast)
  1541     done
  1542 qed
  1543 
  1544 lemma facet_of_convex_hull_affine_independent_alt:
  1545   fixes S :: "'a::euclidean_space set"
  1546   shows
  1547    "~affine_dependent S
  1548         \<Longrightarrow> (T facet_of (convex hull S) \<longleftrightarrow>
  1549              2 \<le> card S \<and> (\<exists>u. u \<in> S \<and> T = convex hull (S - {u})))"
  1550 apply (simp add: facet_of_convex_hull_affine_independent)
  1551 apply (auto simp: Set.subset_singleton_iff)
  1552 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)
  1553 done
  1554 
  1555 lemma segment_face_of:
  1556   assumes "(closed_segment a b) face_of S"
  1557   shows "a extreme_point_of S" "b extreme_point_of S"
  1558 proof -
  1559   have as: "{a} face_of S"
  1560     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)
  1561   moreover have "{b} face_of S"
  1562   proof -
  1563     have "b \<in> convex hull {a} \<or> b extreme_point_of convex hull {b, a}"
  1564       by (meson extreme_point_of_convex_hull_insert finite.emptyI finite.insertI)
  1565     moreover have "closed_segment a b = convex hull {b, a}"
  1566       using closed_segment_commute segment_convex_hull by blast
  1567     ultimately show ?thesis
  1568       by (metis as assms face_of_face convex_hull_singleton empty_iff face_of_singleton insertE)
  1569     qed
  1570   ultimately show "a extreme_point_of S" "b extreme_point_of S"
  1571     using face_of_singleton by blast+
  1572 qed
  1573 
  1574 
  1575 lemma Krein_Milman_frontier:
  1576   fixes S :: "'a::euclidean_space set"
  1577   assumes "convex S" "compact S"
  1578     shows "S = convex hull (frontier S)"
  1579           (is "?lhs = ?rhs")
  1580 proof
  1581   have "?lhs \<subseteq> convex hull {x. x extreme_point_of S}"
  1582     using Krein_Milman_Minkowski assms by blast
  1583   also have "... \<subseteq> ?rhs"
  1584     apply (rule hull_mono)
  1585     apply (auto simp: frontier_def extreme_point_not_in_interior)
  1586     using closure_subset apply (force simp: extreme_point_of_def)
  1587     done
  1588   finally show "?lhs \<subseteq> ?rhs" .
  1589 next
  1590   have "?rhs \<subseteq> convex hull S"
  1591     by (metis Diff_subset \<open>compact S\<close> closure_closed compact_eq_bounded_closed frontier_def hull_mono)
  1592   also have "... \<subseteq> ?lhs"
  1593     by (simp add: \<open>convex S\<close> hull_same)
  1594   finally show "?rhs \<subseteq> ?lhs" .
  1595 qed
  1596 
  1597 subsection\<open>Polytopes\<close>
  1598 
  1599 definition polytope where
  1600  "polytope S \<equiv> \<exists>v. finite v \<and> S = convex hull v"
  1601 
  1602 lemma polytope_translation_eq: "polytope (image (\<lambda>x. a + x) S) \<longleftrightarrow> polytope S"
  1603 apply (simp add: polytope_def, safe)
  1604 apply (metis convex_hull_translation finite_imageI translation_galois)
  1605 by (metis convex_hull_translation finite_imageI)
  1606 
  1607 lemma polytope_linear_image: "\<lbrakk>linear f; polytope p\<rbrakk> \<Longrightarrow> polytope(image f p)"
  1608   unfolding polytope_def using convex_hull_linear_image by blast
  1609 
  1610 lemma polytope_empty: "polytope {}"
  1611   using convex_hull_empty polytope_def by blast
  1612 
  1613 lemma polytope_convex_hull: "finite S \<Longrightarrow> polytope(convex hull S)"
  1614   using polytope_def by auto
  1615 
  1616 lemma polytope_Times: "\<lbrakk>polytope S; polytope T\<rbrakk> \<Longrightarrow> polytope(S \<times> T)"
  1617   unfolding polytope_def
  1618   by (metis finite_cartesian_product convex_hull_Times)
  1619 
  1620 lemma face_of_polytope_polytope:
  1621   fixes S :: "'a::euclidean_space set"
  1622   shows "\<lbrakk>polytope S; F face_of S\<rbrakk> \<Longrightarrow> polytope F"
  1623 unfolding polytope_def
  1624 by (meson face_of_convex_hull_subset finite_imp_compact finite_subset)
  1625 
  1626 lemma finite_polytope_faces:
  1627   fixes S :: "'a::euclidean_space set"
  1628   assumes "polytope S"
  1629   shows "finite {F. F face_of S}"
  1630 proof -
  1631   obtain v where "finite v" "S = convex hull v"
  1632     using assms polytope_def by auto
  1633   have "finite ((hull) convex ` {T. T \<subseteq> v})"
  1634     by (simp add: \<open>finite v\<close>)
  1635   moreover have "{F. F face_of S} \<subseteq> ((hull) convex ` {T. T \<subseteq> v})"
  1636     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)
  1637   ultimately show ?thesis
  1638     by (blast intro: finite_subset)
  1639 qed
  1640 
  1641 lemma finite_polytope_facets:
  1642   assumes "polytope S"
  1643   shows "finite {T. T facet_of S}"
  1644 by (simp add: assms facet_of_def finite_polytope_faces)
  1645 
  1646 lemma polytope_scaling:
  1647   assumes "polytope S"  shows "polytope (image (\<lambda>x. c *\<^sub>R x) S)"
  1648 by (simp add: assms polytope_linear_image)
  1649 
  1650 lemma polytope_imp_compact:
  1651   fixes S :: "'a::real_normed_vector set"
  1652   shows "polytope S \<Longrightarrow> compact S"
  1653 by (metis finite_imp_compact_convex_hull polytope_def)
  1654 
  1655 lemma polytope_imp_convex: "polytope S \<Longrightarrow> convex S"
  1656   by (metis convex_convex_hull polytope_def)
  1657 
  1658 lemma polytope_imp_closed:
  1659   fixes S :: "'a::real_normed_vector set"
  1660   shows "polytope S \<Longrightarrow> closed S"
  1661 by (simp add: compact_imp_closed polytope_imp_compact)
  1662 
  1663 lemma polytope_imp_bounded:
  1664   fixes S :: "'a::real_normed_vector set"
  1665   shows "polytope S \<Longrightarrow> bounded S"
  1666 by (simp add: compact_imp_bounded polytope_imp_compact)
  1667 
  1668 lemma polytope_interval: "polytope(cbox a b)"
  1669   unfolding polytope_def by (meson closed_interval_as_convex_hull)
  1670 
  1671 lemma polytope_sing: "polytope {a}"
  1672   using polytope_def by force
  1673 
  1674 lemma face_of_polytope_insert:
  1675      "\<lbrakk>polytope S; a \<notin> affine hull S; F face_of S\<rbrakk> \<Longrightarrow> F face_of convex hull (insert a S)"
  1676   by (metis (no_types, lifting) affine_hull_convex_hull face_of_convex_hull_insert hull_insert polytope_def)
  1677 
  1678 lemma face_of_polytope_insert2:
  1679   fixes a :: "'a :: euclidean_space"
  1680   assumes "polytope S" "a \<notin> affine hull S" "F face_of S"
  1681   shows "convex hull (insert a F) face_of convex hull (insert a S)"
  1682 proof -
  1683   obtain V where "finite V" "S = convex hull V"
  1684     using assms by (auto simp: polytope_def)
  1685   then have "convex hull (insert a F) face_of convex hull (insert a V)"
  1686     using affine_hull_convex_hull assms face_of_convex_hull_insert2 by blast
  1687   then show ?thesis
  1688     by (metis \<open>S = convex hull V\<close> hull_insert)
  1689 qed
  1690 
  1691 
  1692 subsection\<open>Polyhedra\<close>
  1693 
  1694 definition polyhedron where
  1695  "polyhedron S \<equiv>
  1696         \<exists>F. finite F \<and>
  1697             S = \<Inter> F \<and>
  1698             (\<forall>h \<in> F. \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b})"
  1699 
  1700 lemma polyhedron_Int [intro,simp]:
  1701    "\<lbrakk>polyhedron S; polyhedron T\<rbrakk> \<Longrightarrow> polyhedron (S \<inter> T)"
  1702   apply (simp add: polyhedron_def, clarify)
  1703   apply (rename_tac F G)
  1704   apply (rule_tac x="F \<union> G" in exI, auto)
  1705   done
  1706 
  1707 lemma polyhedron_UNIV [iff]: "polyhedron UNIV"
  1708   unfolding polyhedron_def
  1709   by (rule_tac x="{}" in exI) auto
  1710 
  1711 lemma polyhedron_Inter [intro,simp]:
  1712    "\<lbrakk>finite F; \<And>S. S \<in> F \<Longrightarrow> polyhedron S\<rbrakk> \<Longrightarrow> polyhedron(\<Inter>F)"
  1713 by (induction F rule: finite_induct) auto
  1714 
  1715 
  1716 lemma polyhedron_empty [iff]: "polyhedron ({} :: 'a :: euclidean_space set)"
  1717 proof -
  1718   have "\<exists>a. a \<noteq> 0 \<and>
  1719              (\<exists>b. {x. (SOME i. i \<in> Basis) \<bullet> x \<le> - 1} = {x. a \<bullet> x \<le> b})"
  1720     by (rule_tac x="(SOME i. i \<in> Basis)" in exI) (force simp: SOME_Basis nonzero_Basis)
  1721   moreover have "\<exists>a b. a \<noteq> 0 \<and>
  1722                        {x. - (SOME i. i \<in> Basis) \<bullet> x \<le> - 1} = {x. a \<bullet> x \<le> b}"
  1723       apply (rule_tac x="-(SOME i. i \<in> Basis)" in exI)
  1724       apply (rule_tac x="-1" in exI)
  1725       apply (simp add: SOME_Basis nonzero_Basis)
  1726       done
  1727   ultimately show ?thesis
  1728     unfolding polyhedron_def
  1729     apply (rule_tac x="{{x. (SOME i. i \<in> Basis) \<bullet> x \<le> -1},
  1730                         {x. -(SOME i. i \<in> Basis) \<bullet> x \<le> -1}}" in exI)
  1731     apply force
  1732     done
  1733 qed
  1734 
  1735 lemma polyhedron_halfspace_le:
  1736   fixes a :: "'a :: euclidean_space"
  1737   shows "polyhedron {x. a \<bullet> x \<le> b}"
  1738 proof (cases "a = 0")
  1739   case True then show ?thesis by auto
  1740 next
  1741   case False
  1742   then show ?thesis
  1743     unfolding polyhedron_def
  1744     by (rule_tac x="{{x. a \<bullet> x \<le> b}}" in exI) auto
  1745 qed
  1746 
  1747 lemma polyhedron_halfspace_ge:
  1748   fixes a :: "'a :: euclidean_space"
  1749   shows "polyhedron {x. a \<bullet> x \<ge> b}"
  1750 using polyhedron_halfspace_le [of "-a" "-b"] by simp
  1751 
  1752 lemma polyhedron_hyperplane:
  1753   fixes a :: "'a :: euclidean_space"
  1754   shows "polyhedron {x. a \<bullet> x = b}"
  1755 proof -
  1756   have "{x. a \<bullet> x = b} = {x. a \<bullet> x \<le> b} \<inter> {x. a \<bullet> x \<ge> b}"
  1757     by force
  1758   then show ?thesis
  1759     by (simp add: polyhedron_halfspace_ge polyhedron_halfspace_le)
  1760 qed
  1761 
  1762 lemma affine_imp_polyhedron:
  1763   fixes S :: "'a :: euclidean_space set"
  1764   shows "affine S \<Longrightarrow> polyhedron S"
  1765 by (metis affine_hull_eq polyhedron_Inter polyhedron_hyperplane affine_hull_finite_intersection_hyperplanes [of S])
  1766 
  1767 lemma polyhedron_imp_closed:
  1768   fixes S :: "'a :: euclidean_space set"
  1769   shows "polyhedron S \<Longrightarrow> closed S"
  1770 apply (simp add: polyhedron_def)
  1771 using closed_halfspace_le by fastforce
  1772 
  1773 lemma polyhedron_imp_convex:
  1774   fixes S :: "'a :: euclidean_space set"
  1775   shows "polyhedron S \<Longrightarrow> convex S"
  1776 apply (simp add: polyhedron_def)
  1777 using convex_Inter convex_halfspace_le by fastforce
  1778 
  1779 lemma polyhedron_affine_hull:
  1780   fixes S :: "'a :: euclidean_space set"
  1781   shows "polyhedron(affine hull S)"
  1782 by (simp add: affine_imp_polyhedron)
  1783 
  1784 
  1785 subsection\<open>Canonical polyhedron representation making facial structure explicit\<close>
  1786 
  1787 lemma polyhedron_Int_affine:
  1788   fixes S :: "'a :: euclidean_space set"
  1789   shows "polyhedron S \<longleftrightarrow>
  1790            (\<exists>F. finite F \<and> S = (affine hull S) \<inter> \<Inter>F \<and>
  1791                 (\<forall>h \<in> F. \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b}))"
  1792         (is "?lhs = ?rhs")
  1793 proof
  1794   assume ?lhs then show ?rhs
  1795     apply (simp add: polyhedron_def)
  1796     apply (erule ex_forward)
  1797     using hull_subset apply force
  1798     done
  1799 next
  1800   assume ?rhs then show ?lhs
  1801     apply clarify
  1802     apply (erule ssubst)
  1803     apply (force intro: polyhedron_affine_hull polyhedron_halfspace_le)
  1804     done
  1805 qed
  1806 
  1807 proposition rel_interior_polyhedron_explicit:
  1808   assumes "finite F"
  1809       and seq: "S = affine hull S \<inter> \<Inter>F"
  1810       and faceq: "\<And>h. h \<in> F \<Longrightarrow> a h \<noteq> 0 \<and> h = {x. a h \<bullet> x \<le> b h}"
  1811       and psub: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subset> affine hull S \<inter> \<Inter>F'"
  1812     shows "rel_interior S = {x \<in> S. \<forall>h \<in> F. a h \<bullet> x < b h}"
  1813 proof -
  1814   have rels: "\<And>x. x \<in> rel_interior S \<Longrightarrow> x \<in> S"
  1815     by (meson IntE mem_rel_interior)
  1816   moreover have "a i \<bullet> x < b i" if x: "x \<in> rel_interior S" and "i \<in> F" for x i
  1817   proof -
  1818     have fif: "F - {i} \<subset> F"
  1819       using \<open>i \<in> F\<close> Diff_insert_absorb Diff_subset set_insert psubsetI by blast
  1820     then have "S \<subset> affine hull S \<inter> \<Inter>(F - {i})"
  1821       by (rule psub)
  1822     then obtain z where ssub: "S \<subseteq> \<Inter>(F - {i})" and zint: "z \<in> \<Inter>(F - {i})"
  1823                     and "z \<notin> S" and zaff: "z \<in> affine hull S"
  1824       by auto
  1825     have "z \<noteq> x"
  1826       using \<open>z \<notin> S\<close> rels x by blast
  1827     have "z \<notin> affine hull S \<inter> \<Inter>F"
  1828       using \<open>z \<notin> S\<close> seq by auto
  1829     then have aiz: "a i \<bullet> z > b i"
  1830       using faceq zint zaff by fastforce
  1831     obtain e where "e > 0" "x \<in> S" and e: "ball x e \<inter> affine hull S \<subseteq> S"
  1832       using x by (auto simp: mem_rel_interior_ball)
  1833     then have ins: "\<And>y. \<lbrakk>norm (x - y) < e; y \<in> affine hull S\<rbrakk> \<Longrightarrow> y \<in> S"
  1834       by (metis IntI subsetD dist_norm mem_ball)
  1835     define \<xi> where "\<xi> = min (1/2) (e / 2 / norm(z - x))"
  1836     have "norm (\<xi> *\<^sub>R x - \<xi> *\<^sub>R z) = norm (\<xi> *\<^sub>R (x - z))"
  1837       by (simp add: \<xi>_def algebra_simps norm_mult)
  1838     also have "... = \<xi> * norm (x - z)"
  1839       using \<open>e > 0\<close> by (simp add: \<xi>_def)
  1840     also have "... < e"
  1841       using \<open>z \<noteq> x\<close> \<open>e > 0\<close> by (simp add: \<xi>_def min_def divide_simps norm_minus_commute)
  1842     finally have lte: "norm (\<xi> *\<^sub>R x - \<xi> *\<^sub>R z) < e" .
  1843     have \<xi>_aff: "\<xi> *\<^sub>R z + (1 - \<xi>) *\<^sub>R x \<in> affine hull S"
  1844       by (metis \<open>x \<in> S\<close> add.commute affine_affine_hull diff_add_cancel hull_inc mem_affine zaff)
  1845     have "\<xi> *\<^sub>R z + (1 - \<xi>) *\<^sub>R x \<in> S"
  1846       apply (rule ins [OF _ \<xi>_aff])
  1847       apply (simp add: algebra_simps lte)
  1848       done
  1849     then obtain l where l: "0 < l" "l < 1" and ls: "(l *\<^sub>R z + (1 - l) *\<^sub>R x) \<in> S"
  1850       apply (rule_tac l = \<xi> in that)
  1851       using \<open>e > 0\<close> \<open>z \<noteq> x\<close>  apply (auto simp: \<xi>_def)
  1852       done
  1853     then have i: "l *\<^sub>R z + (1 - l) *\<^sub>R x \<in> i"
  1854       using seq \<open>i \<in> F\<close> by auto
  1855     have "b i * l + (a i \<bullet> x) * (1 - l) < a i \<bullet> (l *\<^sub>R z + (1 - l) *\<^sub>R x)"
  1856       using l by (simp add: algebra_simps aiz)
  1857     also have "\<dots> \<le> b i" using i l
  1858       using faceq mem_Collect_eq \<open>i \<in> F\<close> by blast
  1859     finally have "(a i \<bullet> x) * (1 - l) < b i * (1 - l)"
  1860       by (simp add: algebra_simps)
  1861     with l show ?thesis
  1862       by simp
  1863   qed
  1864   moreover have "x \<in> rel_interior S"
  1865            if "x \<in> S" and less: "\<And>h. h \<in> F \<Longrightarrow> a h \<bullet> x < b h" for x
  1866   proof -
  1867     have 1: "\<And>h. h \<in> F \<Longrightarrow> x \<in> interior h"
  1868       by (metis interior_halfspace_le mem_Collect_eq less faceq)
  1869     have 2: "\<And>y. \<lbrakk>\<forall>h\<in>F. y \<in> interior h; y \<in> affine hull S\<rbrakk> \<Longrightarrow> y \<in> S"
  1870       by (metis IntI Inter_iff contra_subsetD interior_subset seq)
  1871     show ?thesis
  1872       apply (simp add: rel_interior \<open>x \<in> S\<close>)
  1873       apply (rule_tac x="\<Inter>h\<in>F. interior h" in exI)
  1874       apply (auto simp: \<open>finite F\<close> open_INT 1 2)
  1875       done
  1876   qed
  1877   ultimately show ?thesis by blast
  1878 qed
  1879 
  1880 
  1881 lemma polyhedron_Int_affine_parallel:
  1882   fixes S :: "'a :: euclidean_space set"
  1883   shows "polyhedron S \<longleftrightarrow>
  1884          (\<exists>F. finite F \<and>
  1885               S = (affine hull S) \<inter> (\<Inter>F) \<and>
  1886               (\<forall>h \<in> F. \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b} \<and>
  1887                              (\<forall>x \<in> affine hull S. (x + a) \<in> affine hull S)))"
  1888     (is "?lhs = ?rhs")
  1889 proof
  1890   assume ?lhs
  1891   then obtain F where "finite F" and seq: "S = (affine hull S) \<inter> \<Inter>F"
  1892                   and faces: "\<And>h. h \<in> F \<Longrightarrow> \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b}"
  1893     by (fastforce simp add: polyhedron_Int_affine)
  1894   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}"
  1895     by metis
  1896   show ?rhs
  1897   proof -
  1898     have "\<exists>a' b'. a' \<noteq> 0 \<and>
  1899                   affine hull S \<inter> {x. a' \<bullet> x \<le> b'} = affine hull S \<inter> h \<and>
  1900                   (\<forall>w \<in> affine hull S. (w + a') \<in> affine hull S)"
  1901         if "h \<in> F" "~(affine hull S \<subseteq> h)" for h
  1902     proof -
  1903       have "a h \<noteq> 0" and "h = {x. a h \<bullet> x \<le> b h}" "h \<inter> \<Inter>F = \<Inter>F"
  1904         using \<open>h \<in> F\<close> ab by auto
  1905       then have "(affine hull S) \<inter> {x. a h \<bullet> x \<le> b h} \<noteq> {}"
  1906         by (metis (no_types) affine_hull_eq_empty inf.absorb_iff2 inf_assoc inf_bot_right inf_commute seq that(2))
  1907       moreover have "~ (affine hull S \<subseteq> {x. a h \<bullet> x \<le> b h})"
  1908         using \<open>h = {x. a h \<bullet> x \<le> b h}\<close> that(2) by blast
  1909       ultimately show ?thesis
  1910         using affine_parallel_slice [of "affine hull S"]
  1911         by (metis \<open>h = {x. a h \<bullet> x \<le> b h}\<close> affine_affine_hull)
  1912     qed
  1913     then obtain a b
  1914          where ab: "\<And>h. \<lbrakk>h \<in> F; ~ (affine hull S \<subseteq> h)\<rbrakk>
  1915              \<Longrightarrow> a h \<noteq> 0 \<and>
  1916                   affine hull S \<inter> {x. a h \<bullet> x \<le> b h} = affine hull S \<inter> h \<and>
  1917                   (\<forall>w \<in> affine hull S. (w + a h) \<in> affine hull S)"
  1918       by metis
  1919     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})"
  1920       by (subst seq) (auto simp: ab INT_extend_simps)
  1921     show ?thesis
  1922       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)
  1923       apply (intro conjI seq2)
  1924         using \<open>finite F\<close> apply force
  1925        using ab apply blast
  1926        done
  1927   qed
  1928 next
  1929   assume ?rhs then show ?lhs
  1930     apply (simp add: polyhedron_Int_affine)
  1931     by metis
  1932 qed
  1933 
  1934 
  1935 proposition polyhedron_Int_affine_parallel_minimal:
  1936   fixes S :: "'a :: euclidean_space set"
  1937   shows "polyhedron S \<longleftrightarrow>
  1938          (\<exists>F. finite F \<and>
  1939               S = (affine hull S) \<inter> (\<Inter>F) \<and>
  1940               (\<forall>h \<in> F. \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b} \<and>
  1941                              (\<forall>x \<in> affine hull S. (x + a) \<in> affine hull S)) \<and>
  1942               (\<forall>F'. F' \<subset> F \<longrightarrow> S \<subset> (affine hull S) \<inter> (\<Inter>F')))"
  1943     (is "?lhs = ?rhs")
  1944 proof
  1945   assume ?lhs
  1946   then obtain f0
  1947            where f0: "finite f0"
  1948                  "S = (affine hull S) \<inter> (\<Inter>f0)"
  1949                    (is "?P f0")
  1950                  "\<forall>h \<in> f0. \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b} \<and>
  1951                              (\<forall>x \<in> affine hull S. (x + a) \<in> affine hull S)"
  1952                    (is "?Q f0")
  1953     by (force simp: polyhedron_Int_affine_parallel)
  1954   define n where "n = (LEAST n. \<exists>F. card F = n \<and> finite F \<and> ?P F \<and> ?Q F)"
  1955   have nf: "\<exists>F. card F = n \<and> finite F \<and> ?P F \<and> ?Q F"
  1956     apply (simp add: n_def)
  1957     apply (rule LeastI [where k = "card f0"])
  1958     using f0 apply auto
  1959     done
  1960   then obtain F where F: "card F = n" "finite F" and seq: "?P F" and aff: "?Q F"
  1961     by blast
  1962   then have "~ (finite g \<and> ?P g \<and> ?Q g)" if "card g < n" for g
  1963     using that by (auto simp: n_def dest!: not_less_Least)
  1964   then have *: "~ (?P g \<and> ?Q g)" if "g \<subset> F" for g
  1965     using that \<open>finite F\<close> psubset_card_mono \<open>card F = n\<close>
  1966     by (metis finite_Int inf.strict_order_iff)
  1967   have 1: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subseteq> affine hull S \<inter> \<Inter>F'"
  1968     by (subst seq) blast
  1969   have 2: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<noteq> affine hull S \<inter> \<Inter>F'"
  1970     apply (frule *)
  1971     by (metis aff subsetCE subset_iff_psubset_eq)
  1972   show ?rhs
  1973     by (metis \<open>finite F\<close> seq aff psubsetI 1 2)
  1974 next
  1975   assume ?rhs then show ?lhs
  1976     by (auto simp: polyhedron_Int_affine_parallel)
  1977 qed
  1978 
  1979 
  1980 lemma polyhedron_Int_affine_minimal:
  1981   fixes S :: "'a :: euclidean_space set"
  1982   shows "polyhedron S \<longleftrightarrow>
  1983          (\<exists>F. finite F \<and> S = (affine hull S) \<inter> \<Inter>F \<and>
  1984               (\<forall>h \<in> F. \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b}) \<and>
  1985               (\<forall>F'. F' \<subset> F \<longrightarrow> S \<subset> (affine hull S) \<inter> \<Inter>F'))"
  1986 apply (rule iffI)
  1987  apply (force simp: polyhedron_Int_affine_parallel_minimal elim!: ex_forward)
  1988 apply (auto simp: polyhedron_Int_affine elim!: ex_forward)
  1989 done
  1990 
  1991 proposition facet_of_polyhedron_explicit:
  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     shows "c facet_of S \<longleftrightarrow> (\<exists>h. h \<in> F \<and> c = S \<inter> {x. a h \<bullet> x = b h})"
  1997 proof (cases "S = {}")
  1998   case True with psub show ?thesis by force
  1999 next
  2000   case False
  2001   have "polyhedron S"
  2002     apply (simp add: polyhedron_Int_affine)
  2003     apply (rule_tac x=F in exI)
  2004     using assms  apply force
  2005     done
  2006   then have "convex S"
  2007     by (rule polyhedron_imp_convex)
  2008   with False rel_interior_eq_empty have "rel_interior S \<noteq> {}" by blast
  2009   then obtain x where "x \<in> rel_interior S" by auto
  2010   then obtain T where "open T" "x \<in> T" "x \<in> S" "T \<inter> affine hull S \<subseteq> S"
  2011     by (force simp: mem_rel_interior)
  2012   then have xaff: "x \<in> affine hull S" and xint: "x \<in> \<Inter>F"
  2013     using seq hull_inc by auto
  2014   have "rel_interior S = {x \<in> S. \<forall>h\<in>F. a h \<bullet> x < b h}"
  2015     by (rule rel_interior_polyhedron_explicit [OF \<open>finite F\<close> seq faceq psub])
  2016   with \<open>x \<in> rel_interior S\<close>
  2017   have [simp]: "\<And>h. h\<in>F \<Longrightarrow> a h \<bullet> x < b h" by blast
  2018   have *: "(S \<inter> {x. a h \<bullet> x = b h}) facet_of S" if "h \<in> F" for h
  2019   proof -
  2020     have "S \<subset> affine hull S \<inter> \<Inter>(F - {h})"
  2021       using psub that by (metis Diff_disjoint Diff_subset insert_disjoint(2) psubsetI)
  2022     then obtain z where zaff: "z \<in> affine hull S" and zint: "z \<in> \<Inter>(F - {h})" and "z \<notin> S"
  2023       by force
  2024     then have "z \<noteq> x" "z \<notin> h" using seq \<open>x \<in> S\<close> by auto
  2025     have "x \<in> h" using that xint by auto
  2026     then have able: "a h \<bullet> x \<le> b h"
  2027       using faceq that by blast
  2028     also have "... < a h \<bullet> z" using \<open>z \<notin> h\<close> faceq [OF that] xint by auto
  2029     finally have xltz: "a h \<bullet> x < a h \<bullet> z" .
  2030     define l where "l = (b h - a h \<bullet> x) / (a h \<bullet> z - a h \<bullet> x)"
  2031     define w where "w = (1 - l) *\<^sub>R x + l *\<^sub>R z"
  2032     have "0 < l" "l < 1"
  2033       using able xltz \<open>b h < a h \<bullet> z\<close> \<open>h \<in> F\<close>
  2034       by (auto simp: l_def divide_simps)
  2035     have awlt: "a i \<bullet> w < b i" if "i \<in> F" "i \<noteq> h" for i
  2036     proof -
  2037       have "(1 - l) * (a i \<bullet> x) < (1 - l) * b i"
  2038         by (simp add: \<open>l < 1\<close> \<open>i \<in> F\<close>)
  2039       moreover have "l * (a i \<bullet> z) \<le> l * b i"
  2040         apply (rule mult_left_mono)
  2041         apply (metis Diff_insert_absorb Inter_iff Set.set_insert \<open>h \<in> F\<close> faceq insertE mem_Collect_eq that zint)
  2042         using \<open>0 < l\<close>
  2043         apply simp
  2044         done
  2045       ultimately show ?thesis by (simp add: w_def algebra_simps)
  2046     qed
  2047     have weq: "a h \<bullet> w = b h"
  2048       using xltz unfolding w_def l_def
  2049       by (simp add: algebra_simps) (simp add: field_simps)
  2050     have "w \<in> affine hull S"
  2051       by (simp add: w_def mem_affine xaff zaff)
  2052     moreover have "w \<in> \<Inter>F"
  2053       using \<open>a h \<bullet> w = b h\<close> awlt faceq less_eq_real_def by blast
  2054     ultimately have "w \<in> S"
  2055       using seq by blast
  2056     with weq have "S \<inter> {x. a h \<bullet> x = b h} \<noteq> {}" by blast
  2057     moreover have "S \<inter> {x. a h \<bullet> x = b h} face_of S"
  2058       apply (rule face_of_Int_supporting_hyperplane_le)
  2059       apply (rule \<open>convex S\<close>)
  2060       apply (subst (asm) seq)
  2061       using faceq that apply fastforce
  2062       done
  2063     moreover have "affine hull (S \<inter> {x. a h \<bullet> x = b h}) =
  2064                    (affine hull S) \<inter> {x. a h \<bullet> x = b h}"
  2065     proof
  2066       show "affine hull (S \<inter> {x. a h \<bullet> x = b h}) \<subseteq> affine hull S \<inter> {x. a h \<bullet> x = b h}"
  2067         apply (intro Int_greatest hull_mono Int_lower1)
  2068         apply (metis affine_hull_eq affine_hyperplane hull_mono inf_le2)
  2069         done
  2070     next
  2071       show "affine hull S \<inter> {x. a h \<bullet> x = b h} \<subseteq> affine hull (S \<inter> {x. a h \<bullet> x = b h})"
  2072       proof
  2073         fix y
  2074         assume yaff: "y \<in> affine hull S \<inter> {y. a h \<bullet> y = b h}"
  2075         obtain T where "0 < T"
  2076                  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"
  2077         proof (cases "F - {h} = {}")
  2078           case True then show ?thesis
  2079             by (rule_tac T=1 in that) auto
  2080         next
  2081           case False
  2082           then obtain h' where h': "h' \<in> F - {h}" by auto
  2083           define inff where "inff =
  2084             (INF j:F - {h}.
  2085               if 0 < a j \<bullet> y - a j \<bullet> w
  2086               then (b j - a j \<bullet> w) / (a j \<bullet> y - a j \<bullet> w)
  2087               else 1)"
  2088           have "0 < inff"
  2089             apply (simp add: inff_def)
  2090             apply (rule finite_imp_less_Inf)
  2091               using \<open>finite F\<close> apply blast
  2092              using h' apply blast
  2093             apply simp
  2094             using awlt apply (force simp: divide_simps)
  2095             done
  2096           moreover have "inff * (a j \<bullet> y - a j \<bullet> w) \<le> b j - a j \<bullet> w"
  2097                         if "j \<in> F" "j \<noteq> h" for j
  2098           proof (cases "a j \<bullet> w < a j \<bullet> y")
  2099             case True
  2100             then have "inff \<le> (b j - a j \<bullet> w) / (a j \<bullet> y - a j \<bullet> w)"
  2101               apply (simp add: inff_def)
  2102               apply (rule cInf_le_finite)
  2103               using \<open>finite F\<close> apply blast
  2104               apply (simp add: that split: if_split_asm)
  2105               done
  2106             then show ?thesis
  2107               using \<open>0 < inff\<close> awlt [OF that] mult_strict_left_mono
  2108               by (fastforce simp add: algebra_simps divide_simps split: if_split_asm)
  2109           next
  2110             case False
  2111             with \<open>0 < inff\<close> have "inff * (a j \<bullet> y - a j \<bullet> w) \<le> 0"
  2112               by (simp add: mult_le_0_iff)
  2113             also have "... < b j - a j \<bullet> w"
  2114               by (simp add: awlt that)
  2115             finally show ?thesis by simp
  2116           qed
  2117           ultimately show ?thesis
  2118             by (blast intro: that)
  2119         qed
  2120         define c where "c = (1 - T) *\<^sub>R w + T *\<^sub>R y"
  2121         have "(1 - T) *\<^sub>R w + T *\<^sub>R y \<in> j" if "j \<in> F" for j
  2122         proof (cases "j = h")
  2123           case True
  2124           have "(1 - T) *\<^sub>R w + T *\<^sub>R y \<in> {x. a h \<bullet> x \<le> b h}"
  2125             using weq yaff by (auto simp: algebra_simps)
  2126           with True faceq [OF that] show ?thesis by metis
  2127         next
  2128           case False
  2129           with T that have "(1 - T) *\<^sub>R w + T *\<^sub>R y \<in> {x. a j \<bullet> x \<le> b j}"
  2130             by (simp add: algebra_simps)
  2131           with faceq [OF that] show ?thesis by simp
  2132         qed
  2133         moreover have "(1 - T) *\<^sub>R w + T *\<^sub>R y \<in> affine hull S"
  2134           apply (rule affine_affine_hull [simplified affine_alt, rule_format])
  2135           apply (simp add: \<open>w \<in> affine hull S\<close>)
  2136           using yaff apply blast
  2137           done
  2138         ultimately have "c \<in> S"
  2139           using seq by (force simp: c_def)
  2140         moreover have "a h \<bullet> c = b h"
  2141           using yaff by (force simp: c_def algebra_simps weq)
  2142         ultimately have caff: "c \<in> affine hull (S \<inter> {y. a h \<bullet> y = b h})"
  2143           by (simp add: hull_inc)
  2144         have waff: "w \<in> affine hull (S \<inter> {y. a h \<bullet> y = b h})"
  2145           using \<open>w \<in> S\<close> weq by (blast intro: hull_inc)
  2146         have yeq: "y = (1 - inverse T) *\<^sub>R w + c /\<^sub>R T"
  2147           using \<open>0 < T\<close> by (simp add: c_def algebra_simps)
  2148         show "y \<in> affine hull (S \<inter> {y. a h \<bullet> y = b h})"
  2149           by (metis yeq affine_affine_hull [simplified affine_alt, rule_format, OF waff caff])
  2150       qed
  2151     qed
  2152     ultimately show ?thesis
  2153       apply (simp add: facet_of_def)
  2154       apply (subst aff_dim_affine_hull [symmetric])
  2155       using  \<open>b h < a h \<bullet> z\<close> zaff
  2156       apply (force simp: aff_dim_affine_Int_hyperplane)
  2157       done
  2158   qed
  2159   show ?thesis
  2160   proof
  2161     show "\<exists>h. h \<in> F \<and> c = S \<inter> {x. a h \<bullet> x = b h} \<Longrightarrow> c facet_of S"
  2162       using * by blast
  2163   next
  2164     assume "c facet_of S"
  2165     then have "c face_of S" "convex c" "c \<noteq> {}" and affc: "aff_dim c = aff_dim S - 1"
  2166       by (auto simp: facet_of_def face_of_imp_convex)
  2167     then obtain x where x: "x \<in> rel_interior c"
  2168       by (force simp: rel_interior_eq_empty)
  2169     then have "x \<in> c"
  2170       by (meson subsetD rel_interior_subset)
  2171     then have "x \<in> S"
  2172       using \<open>c facet_of S\<close> facet_of_imp_subset by blast
  2173     have rels: "rel_interior S = {x \<in> S. \<forall>h\<in>F. a h \<bullet> x < b h}"
  2174       by (rule rel_interior_polyhedron_explicit [OF assms])
  2175     have "c \<noteq> S"
  2176       using \<open>c facet_of S\<close> facet_of_irrefl by blast
  2177     then have "x \<notin> rel_interior S"
  2178       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)
  2179     with rels \<open>x \<in> S\<close> obtain i where "i \<in> F" and i: "a i \<bullet> x \<ge> b i"
  2180       by force
  2181     have "x \<in> {u. a i \<bullet> u \<le> b i}"
  2182       by (metis IntD2 InterE \<open>i \<in> F\<close> \<open>x \<in> S\<close> faceq seq)
  2183     then have "a i \<bullet> x \<le> b i" by simp
  2184     then have "a i \<bullet> x = b i" using i by auto
  2185     have "c \<subseteq> S \<inter> {x. a i \<bullet> x = b i}"
  2186       apply (rule subset_of_face_of [of _ S])
  2187         apply (simp add: "*" \<open>i \<in> F\<close> facet_of_imp_face_of)
  2188        apply (simp add: \<open>c face_of S\<close> face_of_imp_subset)
  2189       using \<open>a i \<bullet> x = b i\<close> \<open>x \<in> S\<close> x by blast
  2190     then have cface: "c face_of (S \<inter> {x. a i \<bullet> x = b i})"
  2191       by (meson \<open>c face_of S\<close> face_of_subset inf_le1)
  2192     have con: "convex (S \<inter> {x. a i \<bullet> x = b i})"
  2193       by (simp add: \<open>convex S\<close> convex_Int convex_hyperplane)
  2194     show "\<exists>h. h \<in> F \<and> c = S \<inter> {x. a h \<bullet> x = b h}"
  2195       apply (rule_tac x=i in exI)
  2196       apply (simp add: \<open>i \<in> F\<close>)
  2197       by (metis (no_types) * \<open>i \<in> F\<close> affc facet_of_def less_irrefl face_of_aff_dim_lt [OF con cface])
  2198   qed
  2199 qed
  2200 
  2201 
  2202 lemma face_of_polyhedron_subset_explicit:
  2203   fixes S :: "'a :: euclidean_space set"
  2204   assumes "finite F"
  2205       and seq: "S = affine hull S \<inter> \<Inter>F"
  2206       and faceq: "\<And>h. h \<in> F \<Longrightarrow> a h \<noteq> 0 \<and> h = {x. a h \<bullet> x \<le> b h}"
  2207       and psub: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subset> affine hull S \<inter> \<Inter>F'"
  2208       and c: "c face_of S" and "c \<noteq> {}" "c \<noteq> S"
  2209    obtains h where "h \<in> F" "c \<subseteq> S \<inter> {x. a h \<bullet> x = b h}"
  2210 proof -
  2211   have "c \<subseteq> S" using \<open>c face_of S\<close>
  2212     by (simp add: face_of_imp_subset)
  2213   have "polyhedron S"
  2214     apply (simp add: polyhedron_Int_affine)
  2215     by (metis \<open>finite F\<close> faceq seq)
  2216   then have "convex S"
  2217     by (simp add: polyhedron_imp_convex)
  2218   then have *: "(S \<inter> {x. a h \<bullet> x = b h}) face_of S" if "h \<in> F" for h
  2219     apply (rule face_of_Int_supporting_hyperplane_le)
  2220     using faceq seq that by fastforce
  2221   have "rel_interior c \<noteq> {}"
  2222     using c \<open>c \<noteq> {}\<close> face_of_imp_convex rel_interior_eq_empty by blast
  2223   then obtain x where "x \<in> rel_interior c" by auto
  2224   have rels: "rel_interior S = {x \<in> S. \<forall>h\<in>F. a h \<bullet> x < b h}"
  2225     by (rule rel_interior_polyhedron_explicit [OF \<open>finite F\<close> seq faceq psub])
  2226   then have xnot: "x \<notin> rel_interior S"
  2227     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)
  2228   then have "x \<in> S"
  2229     using \<open>c \<subseteq> S\<close> \<open>x \<in> rel_interior c\<close> rel_interior_subset by auto
  2230   then have xint: "x \<in> \<Inter>F"
  2231     using seq by blast
  2232   have "F \<noteq> {}" using assms
  2233     by (metis affine_Int affine_Inter affine_affine_hull ex_in_conv face_of_affine_trivial)
  2234   then obtain i where "i \<in> F" "~ (a i \<bullet> x < b i)"
  2235     using \<open>x \<in> S\<close> rels xnot by auto
  2236   with xint have "a i \<bullet> x = b i"
  2237     by (metis eq_iff mem_Collect_eq not_le Inter_iff faceq)
  2238   have face: "S \<inter> {x. a i \<bullet> x = b i} face_of S"
  2239     by (simp add: "*" \<open>i \<in> F\<close>)
  2240   show ?thesis
  2241     apply (rule_tac h = i in that)
  2242      apply (rule \<open>i \<in> F\<close>)
  2243     apply (rule subset_of_face_of [OF face \<open>c \<subseteq> S\<close>])
  2244     using \<open>a i \<bullet> x = b i\<close> \<open>x \<in> rel_interior c\<close> \<open>x \<in> S\<close> apply blast
  2245     done
  2246 qed
  2247 
  2248 text\<open>Initial part of proof duplicates that above\<close>
  2249 proposition face_of_polyhedron_explicit:
  2250   fixes S :: "'a :: euclidean_space set"
  2251   assumes "finite F"
  2252       and seq: "S = affine hull S \<inter> \<Inter>F"
  2253       and faceq: "\<And>h. h \<in> F \<Longrightarrow> a h \<noteq> 0 \<and> h = {x. a h \<bullet> x \<le> b h}"
  2254       and psub: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subset> affine hull S \<inter> \<Inter>F'"
  2255       and c: "c face_of S" and "c \<noteq> {}" "c \<noteq> S"
  2256     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}}"
  2257 proof -
  2258   let ?ab = "\<lambda>h. {x. a h \<bullet> x = b h}"
  2259   have "c \<subseteq> S" using \<open>c face_of S\<close>
  2260     by (simp add: face_of_imp_subset)
  2261   have "polyhedron S"
  2262     apply (simp add: polyhedron_Int_affine)
  2263     by (metis \<open>finite F\<close> faceq seq)
  2264   then have "convex S"
  2265     by (simp add: polyhedron_imp_convex)
  2266   then have *: "(S \<inter> ?ab h) face_of S" if "h \<in> F" for h
  2267     apply (rule face_of_Int_supporting_hyperplane_le)
  2268     using faceq seq that by fastforce
  2269   have "rel_interior c \<noteq> {}"
  2270     using c \<open>c \<noteq> {}\<close> face_of_imp_convex rel_interior_eq_empty by blast
  2271   then obtain z where z: "z \<in> rel_interior c" by auto
  2272   have rels: "rel_interior S = {z \<in> S. \<forall>h\<in>F. a h \<bullet> z < b h}"
  2273     by (rule rel_interior_polyhedron_explicit [OF \<open>finite F\<close> seq faceq psub])
  2274   then have xnot: "z \<notin> rel_interior S"
  2275     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)
  2276   then have "z \<in> S"
  2277     using \<open>c \<subseteq> S\<close> \<open>z \<in> rel_interior c\<close> rel_interior_subset by auto
  2278   with seq have xint: "z \<in> \<Inter>F" by blast
  2279   have "open (\<Inter>h\<in>{h \<in> F. a h \<bullet> z < b h}. {w. a h \<bullet> w < b h})"
  2280     by (auto simp: \<open>finite F\<close> open_halfspace_lt open_INT)
  2281   then obtain e where "0 < e"
  2282                  "ball z e \<subseteq> (\<Inter>h\<in>{h \<in> F. a h \<bullet> z < b h}. {w. a h \<bullet> w < b h})"
  2283     by (auto intro: openE [of _ z])
  2284   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}"
  2285     by blast
  2286   have "c \<subseteq> (S \<inter> ?ab h) \<longleftrightarrow> z \<in> S \<inter> ?ab h" if "h \<in> F" for h
  2287   proof
  2288     show "z \<in> S \<inter> ?ab h \<Longrightarrow> c \<subseteq> S \<inter> ?ab h"
  2289       apply (rule subset_of_face_of [of _ S])
  2290       using that \<open>c \<subseteq> S\<close> \<open>z \<in> rel_interior c\<close>
  2291       using facet_of_polyhedron_explicit [OF \<open>finite F\<close> seq faceq psub]
  2292             unfolding facet_of_def
  2293       apply auto
  2294       done
  2295   next
  2296     show "c \<subseteq> S \<inter> ?ab h \<Longrightarrow> z \<in> S \<inter> ?ab h"
  2297       using \<open>z \<in> rel_interior c\<close> rel_interior_subset by force
  2298   qed
  2299   then have **: "{S \<inter> ?ab h | h. h \<in> F \<and> c \<subseteq> S \<and> c \<subseteq> ?ab h} =
  2300                  {S \<inter> ?ab h |h. h \<in> F \<and> z \<in> S \<inter> ?ab h}"
  2301     by blast
  2302   have bsub: "ball z e \<inter> affine hull \<Inter>{S \<inter> ?ab h |h. h \<in> F \<and> a h \<bullet> z = b h}
  2303              \<subseteq> affine hull S \<inter> \<Inter>F \<inter> \<Inter>{?ab h |h. h \<in> F \<and> a h \<bullet> z = b h}"
  2304             if "i \<in> F" and i: "a i \<bullet> z = b i" for i
  2305   proof -
  2306     have sub: "ball z e \<inter> \<Inter>{?ab h |h. h \<in> F \<and> a h \<bullet> z = b h} \<subseteq> j"
  2307              if "j \<in> F" for j
  2308     proof -
  2309       have "a j \<bullet> z \<le> b j" using faceq that xint by auto
  2310       then consider "a j \<bullet> z < b j" | "a j \<bullet> z = b j" by linarith
  2311       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"
  2312       proof cases
  2313         assume "a j \<bullet> z < b j"
  2314         then have "ball z e \<inter> {x. a i \<bullet> x = b i} \<subseteq> j"
  2315           using e [OF \<open>j \<in> F\<close>] faceq that
  2316           by (fastforce simp: ball_def)
  2317         then show ?thesis
  2318           by (rule_tac x="{x. a i \<bullet> x = b i}" in exI) (force simp: \<open>i \<in> F\<close> i)
  2319       next
  2320         assume eq: "a j \<bullet> z = b j"
  2321         with faceq that show ?thesis
  2322           by (rule_tac x="{x. a j \<bullet> x = b j}" in exI) (fastforce simp add: \<open>j \<in> F\<close>)
  2323       qed
  2324       then show ?thesis  by blast
  2325     qed
  2326     have 1: "affine hull \<Inter>{S \<inter> ?ab h |h. h \<in> F \<and> a h \<bullet> z = b h} \<subseteq> affine hull S"
  2327       apply (rule hull_mono)
  2328       using that \<open>z \<in> S\<close> by auto
  2329     have 2: "affine hull \<Inter>{S \<inter> ?ab h |h. h \<in> F \<and> a h \<bullet> z = b h}
  2330           \<subseteq> \<Inter>{?ab h |h. h \<in> F \<and> a h \<bullet> z = b h}"
  2331       by (rule hull_minimal) (auto intro: affine_hyperplane)
  2332     have 3: "ball z e \<inter> \<Inter>{?ab h |h. h \<in> F \<and> a h \<bullet> z = b h} \<subseteq> \<Inter>F"
  2333       by (iprover intro: sub Inter_greatest)
  2334     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"
  2335              for A B C D E  by blast
  2336     show ?thesis by (intro * 1 2 3)
  2337   qed
  2338   have "\<exists>h. h \<in> F \<and> c \<subseteq> ?ab h"
  2339     apply (rule face_of_polyhedron_subset_explicit [OF \<open>finite F\<close> seq faceq psub])
  2340     using assms by auto
  2341   then have fac: "\<Inter>{S \<inter> ?ab h |h. h \<in> F \<and> c \<subseteq> S \<inter> ?ab h} face_of S"
  2342     using * by (force simp: \<open>c \<subseteq> S\<close> intro: face_of_Inter)
  2343   have red:
  2344      "(\<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}"
  2345      for P T F   by blast
  2346   have "ball z e \<inter> affine hull \<Inter>{S \<inter> ?ab h |h. h \<in> F \<and> a h \<bullet> z = b h}
  2347         \<subseteq> \<Inter>{S \<inter> ?ab h |h. h \<in> F \<and> a h \<bullet> z = b h}"
  2348     apply (rule red)
  2349     apply (metis seq bsub)
  2350     done
  2351   with \<open>0 < e\<close> have zinrel: "z \<in> rel_interior
  2352                     (\<Inter>{S \<inter> ?ab h |h. h \<in> F \<and> z \<in> S \<and> a h \<bullet> z = b h})"
  2353     by (auto simp: mem_rel_interior_ball \<open>z \<in> S\<close>)
  2354   show ?thesis
  2355     apply (rule face_of_eq [OF c fac])
  2356     using z zinrel apply (force simp: **)
  2357     done
  2358 qed
  2359 
  2360 
  2361 subsection\<open>More general corollaries from the explicit representation\<close>
  2362 
  2363 corollary facet_of_polyhedron:
  2364   assumes "polyhedron S" and "c facet_of S"
  2365   obtains a b where "a \<noteq> 0" "S \<subseteq> {x. a \<bullet> x \<le> b}" "c = S \<inter> {x. a \<bullet> x = b}"
  2366 proof -
  2367   obtain F where "finite F" and seq: "S = affine hull S \<inter> \<Inter>F"
  2368              and faces: "\<And>h. h \<in> F \<Longrightarrow> \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b}"
  2369              and min: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subset> (affine hull S) \<inter> \<Inter>F'"
  2370     using assms by (simp add: polyhedron_Int_affine_minimal) meson
  2371   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}"
  2372     by metis
  2373   obtain i where "i \<in> F" and c: "c = S \<inter> {x. a i \<bullet> x = b i}"
  2374     using facet_of_polyhedron_explicit [OF \<open>finite F\<close> seq ab min] assms
  2375     by force
  2376   moreover have ssub: "S \<subseteq> {x. a i \<bullet> x \<le> b i}"
  2377      apply (subst seq)
  2378      using \<open>i \<in> F\<close> ab by auto
  2379   ultimately show ?thesis
  2380     by (rule_tac a = "a i" and b = "b i" in that) (simp_all add: ab)
  2381 qed
  2382 
  2383 corollary face_of_polyhedron:
  2384   assumes "polyhedron S" and "c face_of S" and "c \<noteq> {}" and "c \<noteq> S"
  2385     shows "c = \<Inter>{F. F facet_of S \<and> c \<subseteq> F}"
  2386 proof -
  2387   obtain F where "finite F" and seq: "S = affine hull S \<inter> \<Inter>F"
  2388              and faces: "\<And>h. h \<in> F \<Longrightarrow> \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b}"
  2389              and min: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subset> (affine hull S) \<inter> \<Inter>F'"
  2390     using assms by (simp add: polyhedron_Int_affine_minimal) meson
  2391   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}"
  2392     by metis
  2393   show ?thesis
  2394     apply (subst face_of_polyhedron_explicit [OF \<open>finite F\<close> seq ab min])
  2395     apply (auto simp: assms facet_of_polyhedron_explicit [OF \<open>finite F\<close> seq ab min] cong: Collect_cong)
  2396     done
  2397 qed
  2398 
  2399 lemma face_of_polyhedron_subset_facet:
  2400   assumes "polyhedron S" and "c face_of S" and "c \<noteq> {}" and "c \<noteq> S"
  2401   obtains F where "F facet_of S" "c \<subseteq> F"
  2402 using face_of_polyhedron assms
  2403 by (metis (no_types, lifting) Inf_greatest antisym_conv face_of_imp_subset mem_Collect_eq)
  2404 
  2405 
  2406 lemma exposed_face_of_polyhedron:
  2407   assumes "polyhedron S"
  2408     shows "F exposed_face_of S \<longleftrightarrow> F face_of S"
  2409 proof
  2410   show "F exposed_face_of S \<Longrightarrow> F face_of S"
  2411     by (simp add: exposed_face_of_def)
  2412 next
  2413   assume "F face_of S"
  2414   show "F exposed_face_of S"
  2415   proof (cases "F = {} \<or> F = S")
  2416     case True then show ?thesis
  2417       using \<open>F face_of S\<close> exposed_face_of by blast
  2418   next
  2419     case False
  2420     then have "{g. g facet_of S \<and> F \<subseteq> g} \<noteq> {}"
  2421       by (metis Collect_empty_eq_bot \<open>F face_of S\<close> assms empty_def face_of_polyhedron_subset_facet)
  2422     moreover have "\<And>T. \<lbrakk>T facet_of S; F \<subseteq> T\<rbrakk> \<Longrightarrow> T exposed_face_of S"
  2423       by (metis assms exposed_face_of facet_of_imp_face_of facet_of_polyhedron)
  2424     ultimately have "\<Inter>{fa.
  2425        fa facet_of S \<and> F \<subseteq> fa} exposed_face_of S"
  2426       by (metis (no_types, lifting) mem_Collect_eq exposed_face_of_Inter)
  2427     then show ?thesis
  2428       using False
  2429       apply (subst face_of_polyhedron [OF assms \<open>F face_of S\<close>], auto)
  2430       done
  2431   qed
  2432 qed
  2433 
  2434 lemma face_of_polyhedron_polyhedron:
  2435   fixes S :: "'a :: euclidean_space set"
  2436   assumes "polyhedron S" "c face_of S" shows "polyhedron c"
  2437 by (metis assms face_of_imp_eq_affine_Int polyhedron_Int polyhedron_affine_hull polyhedron_imp_convex)
  2438 
  2439 lemma finite_polyhedron_faces:
  2440   fixes S :: "'a :: euclidean_space set"
  2441   assumes "polyhedron S"
  2442     shows "finite {F. F face_of S}"
  2443 proof -
  2444   obtain F where "finite F" and seq: "S = affine hull S \<inter> \<Inter>F"
  2445              and faces: "\<And>h. h \<in> F \<Longrightarrow> \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b}"
  2446              and min:   "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subset> (affine hull S) \<inter> \<Inter>F'"
  2447     using assms by (simp add: polyhedron_Int_affine_minimal) meson
  2448   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}"
  2449     by metis
  2450   have "finite {\<Inter>{S \<inter> {x. a h \<bullet> x = b h} |h. h \<in> F'}| F'. F' \<in> Pow F}"
  2451     by (simp add: \<open>finite F\<close>)
  2452   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}"
  2453     apply clarify
  2454     apply (rename_tac c)
  2455     apply (drule face_of_polyhedron_explicit [OF \<open>finite F\<close> seq ab min, simplified], simp_all)
  2456     apply (erule ssubst)
  2457     apply (rule_tac x="{h \<in> F. c \<subseteq> S \<inter> {x. a h \<bullet> x = b h}}" in exI, auto)
  2458     done
  2459   ultimately show ?thesis
  2460     by (meson finite.emptyI finite.insertI finite_Diff2 finite_subset)
  2461 qed
  2462 
  2463 lemma finite_polyhedron_exposed_faces:
  2464    "polyhedron S \<Longrightarrow> finite {F. F exposed_face_of S}"
  2465 using exposed_face_of_polyhedron finite_polyhedron_faces by fastforce
  2466 
  2467 lemma finite_polyhedron_extreme_points:
  2468   fixes S :: "'a :: euclidean_space set"
  2469   shows "polyhedron S \<Longrightarrow> finite {v. v extreme_point_of S}"
  2470 apply (simp add: face_of_singleton [symmetric])
  2471 apply (rule finite_subset [OF _ finite_vimageI [OF finite_polyhedron_faces]], auto)
  2472 done
  2473 
  2474 lemma finite_polyhedron_facets:
  2475   fixes S :: "'a :: euclidean_space set"
  2476   shows "polyhedron S \<Longrightarrow> finite {F. F facet_of S}"
  2477 unfolding facet_of_def
  2478 by (blast intro: finite_subset [OF _ finite_polyhedron_faces])
  2479 
  2480 
  2481 proposition rel_interior_of_polyhedron:
  2482   fixes S :: "'a :: euclidean_space set"
  2483   assumes "polyhedron S"
  2484     shows "rel_interior S = S - \<Union>{F. F facet_of S}"
  2485 proof -
  2486   obtain F where "finite F" and seq: "S = affine hull S \<inter> \<Inter>F"
  2487              and faces: "\<And>h. h \<in> F \<Longrightarrow> \<exists>a b. a \<noteq> 0 \<and> h = {x. a \<bullet> x \<le> b}"
  2488              and min: "\<And>F'. F' \<subset> F \<Longrightarrow> S \<subset> (affine hull S) \<inter> \<Inter>F'"
  2489     using assms by (simp add: polyhedron_Int_affine_minimal) meson
  2490   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}"
  2491     by metis
  2492   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
  2493     by (rule facet_of_polyhedron_explicit [OF \<open>finite F\<close> seq ab min])
  2494   have rel: "rel_interior S = {x \<in> S. \<forall>h\<in>F. a h \<bullet> x < b h}"
  2495     by (rule rel_interior_polyhedron_explicit [OF \<open>finite F\<close> seq ab min])
  2496   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
  2497   proof -
  2498     have "x \<in> \<Inter>F" using seq that by force
  2499     with \<open>h \<in> F\<close> ab have "a h \<bullet> x \<le> b h" by auto
  2500     then consider "a h \<bullet> x < b h" | "a h \<bullet> x = b h" by linarith
  2501     then show ?thesis
  2502     proof cases
  2503       case 1 then show ?thesis .
  2504     next
  2505       case 2
  2506       have "Collect ((\<in>) x) \<notin> Collect ((\<in>) (\<Union>{A. A facet_of S}))"
  2507         using xnot by fastforce
  2508       then have "F \<notin> Collect ((\<in>) h)"
  2509         using 2 \<open>x \<in> S\<close> facet by blast
  2510       with \<open>h \<in> F\<close> have "\<Inter>F \<subseteq> S \<inter> {x. a h \<bullet> x = b h}" by blast
  2511       with 2 that \<open>x \<in> \<Inter>F\<close> show ?thesis
  2512         apply simp
  2513         apply (drule_tac x="\<Inter>F" in spec)
  2514         apply (simp add: facet)
  2515         apply (drule_tac x=h in spec)
  2516         using seq by auto
  2517       qed
  2518   qed
  2519   moreover have "\<exists>h\<in>F. a h \<bullet> x \<ge> b h" if "x \<in> \<Union>{F. F facet_of S}" for x
  2520     using that by (force simp: facet)
  2521   ultimately show ?thesis
  2522     by (force simp: rel)
  2523 qed
  2524 
  2525 lemma rel_boundary_of_polyhedron:
  2526   fixes S :: "'a :: euclidean_space set"
  2527   assumes "polyhedron S"
  2528     shows "S - rel_interior S = \<Union> {F. F facet_of S}"
  2529 using facet_of_imp_subset by (fastforce simp add: rel_interior_of_polyhedron assms)
  2530 
  2531 lemma rel_frontier_of_polyhedron:
  2532   fixes S :: "'a :: euclidean_space set"
  2533   assumes "polyhedron S"
  2534     shows "rel_frontier S = \<Union> {F. F facet_of S}"
  2535 by (simp add: assms rel_frontier_def polyhedron_imp_closed rel_boundary_of_polyhedron)
  2536 
  2537 lemma rel_frontier_of_polyhedron_alt:
  2538   fixes S :: "'a :: euclidean_space set"
  2539   assumes "polyhedron S"
  2540     shows "rel_frontier S = \<Union> {F. F face_of S \<and> (F \<noteq> S)}"
  2541 apply (rule subset_antisym)
  2542   apply (force simp: rel_frontier_of_polyhedron facet_of_def assms)
  2543 using face_of_subset_rel_frontier by fastforce
  2544 
  2545 
  2546 text\<open>A characterization of polyhedra as having finitely many faces\<close>
  2547 
  2548 proposition polyhedron_eq_finite_exposed_faces:
  2549   fixes S :: "'a :: euclidean_space set"
  2550   shows "polyhedron S \<longleftrightarrow> closed S \<and> convex S \<and> finite {F. F exposed_face_of S}"
  2551          (is "?lhs = ?rhs")
  2552 proof
  2553   assume ?lhs
  2554   then show ?rhs
  2555     by (auto simp: polyhedron_imp_closed polyhedron_imp_convex finite_polyhedron_exposed_faces)
  2556 next
  2557   assume ?rhs
  2558   then have "closed S" "convex S" and fin: "finite {F. F exposed_face_of S}" by auto
  2559   show ?lhs
  2560   proof (cases "S = {}")
  2561     case True then show ?thesis by auto
  2562   next
  2563     case False
  2564     define F where "F = {h. h exposed_face_of S \<and> h \<noteq> {} \<and> h \<noteq> S}"
  2565     have "finite F" by (simp add: fin F_def)
  2566     have hface: "h face_of S"
  2567       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}"
  2568       if "h \<in> F" for h
  2569       using exposed_face_of F_def that by simp_all auto
  2570     then obtain a b where ab:
  2571       "\<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}"
  2572       by metis
  2573     have *: "False"
  2574       if paff: "p \<in> affine hull S" and "p \<notin> S"
  2575       and pint: "p \<in> \<Inter>{{x. a h \<bullet> x \<le> b h} |h. h \<in> F}" for p
  2576     proof -
  2577       have "rel_interior S \<noteq> {}"
  2578         by (simp add: \<open>S \<noteq> {}\<close> \<open>convex S\<close> rel_interior_eq_empty)
  2579       then obtain c where c: "c \<in> rel_interior S" by auto
  2580       with rel_interior_subset have "c \<in> S"  by blast
  2581       have ccp: "closed_segment c p \<subseteq> affine hull S"
  2582         by (meson affine_affine_hull affine_imp_convex c closed_segment_subset hull_subset paff rel_interior_subset subsetCE)
  2583       obtain x where xcl: "x \<in> closed_segment c p" and "x \<in> S" and xnot: "x \<notin> rel_interior S"
  2584         using connected_openin [of "closed_segment c p"]
  2585         apply simp
  2586         apply (drule_tac x="closed_segment c p \<inter> rel_interior S" in spec)
  2587         apply (erule impE)
  2588          apply (force simp: openin_rel_interior openin_Int intro: openin_subtopology_Int_subset [OF _ ccp])
  2589         apply (drule_tac x="closed_segment c p \<inter> (- S)" in spec)
  2590         using rel_interior_subset \<open>closed S\<close> c \<open>p \<notin> S\<close> apply blast
  2591         done
  2592       then obtain \<mu> where "0 \<le> \<mu>" "\<mu> \<le> 1" and xeq: "x = (1 - \<mu>) *\<^sub>R c + \<mu> *\<^sub>R p"
  2593         by (auto simp: in_segment)
  2594       show False
  2595       proof (cases "\<mu>=0 \<or> \<mu>=1")
  2596         case True with xeq c xnot \<open>x \<in> S\<close> \<open>p \<notin> S\<close>
  2597         show False by auto
  2598       next
  2599         case False
  2600         then have xos: "x \<in> open_segment c p"
  2601           using \<open>x \<in> S\<close> c open_segment_def that(2) xcl xnot by auto
  2602         have xclo: "x \<in> closure S"
  2603           using \<open>x \<in> S\<close> closure_subset by blast
  2604         obtain d where "d \<noteq> 0"
  2605               and dle: "\<And>y. y \<in> closure S \<Longrightarrow> d \<bullet> x \<le> d \<bullet> y"
  2606               and dless: "\<And>y. y \<in> rel_interior S \<Longrightarrow> d \<bullet> x < d \<bullet> y"
  2607           by (metis supporting_hyperplane_relative_frontier [OF \<open>convex S\<close> xclo xnot])
  2608         have sex: "S \<inter> {y. d \<bullet> y = d \<bullet> x} exposed_face_of S"
  2609           by (simp add: \<open>closed S\<close> dle exposed_face_of_Int_supporting_hyperplane_ge [OF \<open>convex S\<close>])
  2610         have sne: "S \<inter> {y. d \<bullet> y = d \<bullet> x} \<noteq> {}"
  2611           using \<open>x \<in> S\<close> by blast
  2612         have sns: "S \<inter> {y. d \<bullet> y = d \<bullet> x} \<noteq> S"
  2613           by (metis (mono_tags) Int_Collect c subsetD dless not_le order_refl rel_interior_subset)
  2614         obtain h where "h \<in> F" "x \<in> h"
  2615           apply (rule_tac h="S \<inter> {y. d \<bullet> y = d \<bullet> x}" in that)
  2616           apply (simp_all add: F_def sex sne sns \<open>x \<in> S\<close>)
  2617           done
  2618         have abface: "{y. a h \<bullet> y = b h} face_of {y. a h \<bullet> y \<le> b h}"
  2619           using hyperplane_face_of_halfspace_le by blast
  2620         then have "c \<in> h"
  2621           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
  2622         with c have "h \<inter> rel_interior S \<noteq> {}" by blast
  2623         then show False
  2624           using \<open>h \<in> F\<close> F_def face_of_disjoint_rel_interior hface by auto
  2625       qed
  2626     qed
  2627     have "S \<subseteq> affine hull S \<inter> \<Inter>{{x. a h \<bullet> x \<le> b h} |h. h \<in> F}"
  2628       using ab by (auto simp: hull_subset)
  2629     moreover have "affine hull S \<inter> \<Inter>{{x. a h \<bullet> x \<le> b h} |h. h \<in> F} \<subseteq> S"
  2630       using * by blast
  2631     ultimately have "S = affine hull S \<inter> \<Inter> {{x. a h \<bullet> x \<le> b h} |h. h \<in> F}" ..
  2632     then show ?thesis
  2633       apply (rule ssubst)
  2634       apply (force intro: polyhedron_affine_hull polyhedron_halfspace_le simp: \<open>finite F\<close>)
  2635       done
  2636   qed
  2637 qed
  2638 
  2639 corollary polyhedron_eq_finite_faces:
  2640   fixes S :: "'a :: euclidean_space set"
  2641   shows "polyhedron S \<longleftrightarrow> closed S \<and> convex S \<and> finite {F. F face_of S}"
  2642          (is "?lhs = ?rhs")
  2643 proof
  2644   assume ?lhs
  2645   then show ?rhs
  2646     by (simp add: finite_polyhedron_faces polyhedron_imp_closed polyhedron_imp_convex)
  2647 next
  2648   assume ?rhs
  2649   then show ?lhs
  2650     by (force simp: polyhedron_eq_finite_exposed_faces exposed_face_of intro: finite_subset)
  2651 qed
  2652 
  2653 lemma polyhedron_linear_image_eq:
  2654   fixes h :: "'a :: euclidean_space \<Rightarrow> 'b :: euclidean_space"
  2655   assumes "linear h" "bij h"
  2656     shows "polyhedron (h ` S) \<longleftrightarrow> polyhedron S"
  2657 proof -
  2658   have *: "{f. P f} = (image h) ` {f. P (h ` f)}" for P
  2659     apply safe
  2660     apply (rule_tac x="inv h ` x" in image_eqI)
  2661     apply (auto simp: \<open>bij h\<close> bij_is_surj image_f_inv_f)
  2662     done
  2663   have "inj h" using bij_is_inj assms by blast
  2664   then have injim: "inj_on ((`) h) A" for A
  2665     by (simp add: inj_on_def inj_image_eq_iff)
  2666   show ?thesis
  2667     using \<open>linear h\<close> \<open>inj h\<close>
  2668     apply (simp add: polyhedron_eq_finite_faces closed_injective_linear_image_eq)
  2669     apply (simp add: * face_of_linear_image [of h _ S, symmetric] finite_image_iff injim)
  2670     done
  2671 qed
  2672 
  2673 lemma polyhedron_negations:
  2674   fixes S :: "'a :: euclidean_space set"
  2675   shows   "polyhedron S \<Longrightarrow> polyhedron(image uminus S)"
  2676 by (auto simp: polyhedron_linear_image_eq linear_uminus bij_uminus)
  2677 
  2678 subsection\<open>Relation between polytopes and polyhedra\<close>
  2679 
  2680 lemma polytope_eq_bounded_polyhedron:
  2681   fixes S :: "'a :: euclidean_space set"
  2682   shows "polytope S \<longleftrightarrow> polyhedron S \<and> bounded S"
  2683          (is "?lhs = ?rhs")
  2684 proof
  2685   assume ?lhs
  2686   then show ?rhs
  2687     by (simp add: finite_polytope_faces polyhedron_eq_finite_faces
  2688                   polytope_imp_closed polytope_imp_convex polytope_imp_bounded)
  2689 next
  2690   assume ?rhs then show ?lhs
  2691     unfolding polytope_def
  2692     apply (rule_tac x="{v. v extreme_point_of S}" in exI)
  2693     apply (simp add: finite_polyhedron_extreme_points Krein_Milman_Minkowski compact_eq_bounded_closed polyhedron_imp_closed polyhedron_imp_convex)
  2694     done
  2695 qed
  2696 
  2697 lemma polytope_Int:
  2698   fixes S :: "'a :: euclidean_space set"
  2699   shows "\<lbrakk>polytope S; polytope T\<rbrakk> \<Longrightarrow> polytope(S \<inter> T)"
  2700 by (simp add: polytope_eq_bounded_polyhedron bounded_Int)
  2701 
  2702 
  2703 lemma polytope_Int_polyhedron:
  2704   fixes S :: "'a :: euclidean_space set"
  2705   shows "\<lbrakk>polytope S; polyhedron T\<rbrakk> \<Longrightarrow> polytope(S \<inter> T)"
  2706 by (simp add: bounded_Int polytope_eq_bounded_polyhedron)
  2707 
  2708 lemma polyhedron_Int_polytope:
  2709   fixes S :: "'a :: euclidean_space set"
  2710   shows "\<lbrakk>polyhedron S; polytope T\<rbrakk> \<Longrightarrow> polytope(S \<inter> T)"
  2711 by (simp add: bounded_Int polytope_eq_bounded_polyhedron)
  2712 
  2713 lemma polytope_imp_polyhedron:
  2714   fixes S :: "'a :: euclidean_space set"
  2715   shows "polytope S \<Longrightarrow> polyhedron S"
  2716 by (simp add: polytope_eq_bounded_polyhedron)
  2717 
  2718 lemma polytope_facet_exists:
  2719   fixes p :: "'a :: euclidean_space set"
  2720   assumes "polytope p" "0 < aff_dim p"
  2721   obtains F where "F facet_of p"
  2722 proof (cases "p = {}")
  2723   case True with assms show ?thesis by auto
  2724 next
  2725   case False
  2726   then obtain v where "v extreme_point_of p"
  2727     using extreme_point_exists_convex
  2728     by (blast intro: \<open>polytope p\<close> polytope_imp_compact polytope_imp_convex)
  2729   then
  2730   show ?thesis
  2731     by (metis face_of_polyhedron_subset_facet polytope_imp_polyhedron aff_dim_sing
  2732        all_not_in_conv assms face_of_singleton less_irrefl singletonI that)
  2733 qed
  2734 
  2735 lemma polyhedron_interval [iff]: "polyhedron(cbox a b)"
  2736 by (metis polytope_imp_polyhedron polytope_interval)
  2737 
  2738 lemma polyhedron_convex_hull:
  2739   fixes S :: "'a :: euclidean_space set"
  2740   shows "finite S \<Longrightarrow> polyhedron(convex hull S)"
  2741 by (simp add: polytope_convex_hull polytope_imp_polyhedron)
  2742 
  2743 
  2744 subsection\<open>Relative and absolute frontier of a polytope\<close>
  2745 
  2746 lemma rel_boundary_of_convex_hull:
  2747     fixes S :: "'a::euclidean_space set"
  2748     assumes "~ affine_dependent S"
  2749       shows "(convex hull S) - rel_interior(convex hull S) = (\<Union>a\<in>S. convex hull (S - {a}))"
  2750 proof -
  2751   have "finite S" by (metis assms aff_independent_finite)
  2752   then consider "card S = 0" | "card S = 1" | "2 \<le> card S" by arith
  2753   then show ?thesis
  2754   proof cases
  2755     case 1 then have "S = {}" by (simp add: \<open>finite S\<close>)
  2756     then show ?thesis by simp
  2757   next
  2758     case 2 show ?thesis
  2759       by (auto intro: card_1_singletonE [OF \<open>card S = 1\<close>])
  2760   next
  2761     case 3
  2762     with assms show ?thesis
  2763       by (auto simp: polyhedron_convex_hull rel_boundary_of_polyhedron facet_of_convex_hull_affine_independent_alt \<open>finite S\<close>)
  2764   qed
  2765 qed
  2766 
  2767 proposition frontier_of_convex_hull:
  2768     fixes S :: "'a::euclidean_space set"
  2769     assumes "card S = Suc (DIM('a))"
  2770       shows "frontier(convex hull S) = \<Union> {convex hull (S - {a}) | a. a \<in> S}"
  2771 proof (cases "affine_dependent S")
  2772   case True
  2773     have [iff]: "finite S"
  2774       using assms using card_infinite by force
  2775     then have ccs: "closed (convex hull S)"
  2776       by (simp add: compact_imp_closed finite_imp_compact_convex_hull)
  2777     { fix x T
  2778       assume "finite T" "T \<subseteq> S" "int (card T) \<le> aff_dim S + 1" "x \<in> convex hull T"
  2779       then have "S \<noteq> T"
  2780         using True \<open>finite S\<close> aff_dim_le_card affine_independent_iff_card by fastforce
  2781       then obtain a where "a \<in> S" "a \<notin> T"
  2782         using \<open>T \<subseteq> S\<close> by blast
  2783       then have "x \<in> (\<Union>a\<in>S. convex hull (S - {a}))"
  2784         using True affine_independent_iff_card [of S]
  2785         apply simp
  2786         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)
  2787         done
  2788     } note * = this
  2789     have 1: "convex hull S \<subseteq> (\<Union> a\<in>S. convex hull (S - {a}))"
  2790       apply (subst caratheodory_aff_dim)
  2791       apply (blast intro: *)
  2792       done
  2793     have 2: "\<Union>((\<lambda>a. convex hull (S - {a})) ` S) \<subseteq> convex hull S"
  2794       by (rule Union_least) (metis (no_types, lifting)  Diff_subset hull_mono imageE)
  2795     show ?thesis using True
  2796       apply (simp add: segment_convex_hull frontier_def)
  2797       using interior_convex_hull_eq_empty [OF assms]
  2798       apply (simp add: closure_closed [OF ccs])
  2799       apply (rule subset_antisym)
  2800       using 1 apply blast
  2801       using 2 apply blast
  2802       done
  2803 next
  2804   case False
  2805   then have "frontier (convex hull S) = (convex hull S) - rel_interior(convex hull S)"
  2806     apply (simp add: rel_boundary_of_convex_hull [symmetric] frontier_def)
  2807     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)
  2808   also have "... = \<Union>{convex hull (S - {a}) |a. a \<in> S}"
  2809   proof -
  2810     have "convex hull S - rel_interior (convex hull S) = rel_frontier (convex hull S)"
  2811       by (simp add: False aff_independent_finite polyhedron_convex_hull rel_boundary_of_polyhedron rel_frontier_of_polyhedron)
  2812     then show ?thesis
  2813       by (simp add: False rel_frontier_convex_hull_cases)
  2814   qed
  2815   finally show ?thesis .
  2816 qed
  2817 
  2818 subsection\<open>Special case of a triangle\<close>
  2819 
  2820 proposition frontier_of_triangle:
  2821     fixes a :: "'a::euclidean_space"
  2822     assumes "DIM('a) = 2"
  2823     shows "frontier(convex hull {a,b,c}) = closed_segment a b \<union> closed_segment b c \<union> closed_segment c a"
  2824           (is "?lhs = ?rhs")
  2825 proof (cases "b = a \<or> c = a \<or> c = b")
  2826   case True then show ?thesis
  2827     by (auto simp: assms segment_convex_hull frontier_def empty_interior_convex_hull insert_commute card_insert_le_m1 hull_inc insert_absorb)
  2828 next
  2829   case False then have [simp]: "card {a, b, c} = Suc (DIM('a))"
  2830     by (simp add: card_insert Set.insert_Diff_if assms)
  2831   show ?thesis
  2832   proof
  2833     show "?lhs \<subseteq> ?rhs"
  2834       using False
  2835       by (force simp: segment_convex_hull frontier_of_convex_hull insert_Diff_if insert_commute split: if_split_asm)
  2836     show "?rhs \<subseteq> ?lhs"
  2837       using False
  2838       apply (simp add: frontier_of_convex_hull segment_convex_hull)
  2839       apply (intro conjI subsetI)
  2840         apply (rule_tac X="convex hull {a,b}" in UnionI; force simp: Set.insert_Diff_if)
  2841        apply (rule_tac X="convex hull {b,c}" in UnionI; force)
  2842       apply (rule_tac X="convex hull {a,c}" in UnionI; force simp: insert_commute Set.insert_Diff_if)
  2843       done
  2844   qed
  2845 qed
  2846 
  2847 corollary inside_of_triangle:
  2848     fixes a :: "'a::euclidean_space"
  2849     assumes "DIM('a) = 2"
  2850     shows "inside (closed_segment a b \<union> closed_segment b c \<union> closed_segment c a) = interior(convex hull {a,b,c})"
  2851 by (metis assms frontier_of_triangle bounded_empty bounded_insert convex_convex_hull inside_frontier_eq_interior bounded_convex_hull)
  2852 
  2853 corollary interior_of_triangle:
  2854     fixes a :: "'a::euclidean_space"
  2855     assumes "DIM('a) = 2"
  2856     shows "interior(convex hull {a,b,c}) =
  2857            convex hull {a,b,c} - (closed_segment a b \<union> closed_segment b c \<union> closed_segment c a)"
  2858   using interior_subset
  2859   by (force simp: frontier_of_triangle [OF assms, symmetric] frontier_def Diff_Diff_Int)
  2860 
  2861 subsection\<open>Subdividing a cell complex\<close>
  2862 
  2863 lemma subdivide_interval:
  2864   fixes x::real
  2865   assumes "a < \<bar>x - y\<bar>" "0 < a"
  2866   obtains n where "n \<in> \<int>" "x < n * a \<and> n * a < y \<or> y <  n * a \<and> n * a < x"
  2867 proof -
  2868   consider "a + x < y" | "a + y < x"
  2869     using assms by linarith
  2870   then show ?thesis
  2871   proof cases
  2872     case 1
  2873     let ?n = "of_int (floor (x/a)) + 1"
  2874     have x: "x < ?n * a"
  2875       by (meson \<open>0 < a\<close> divide_less_eq floor_eq_iff)
  2876     have "?n * a \<le> a + x"
  2877       apply (simp add: algebra_simps)
  2878       by (metis \<open>0 < a\<close> floor_correct less_irrefl nonzero_mult_div_cancel_left real_mult_le_cancel_iff2 times_divide_eq_right)
  2879     also have "... < y"
  2880       by (rule 1)
  2881     finally have "?n * a < y" .
  2882     with x show ?thesis
  2883       using Ints_1 Ints_add Ints_of_int that by blast
  2884   next
  2885     case 2
  2886     let ?n = "of_int (floor (y/a)) + 1"
  2887     have y: "y < ?n * a"
  2888       by (meson \<open>0 < a\<close> divide_less_eq floor_eq_iff)
  2889     have "?n * a \<le> a + y"
  2890       apply (simp add: algebra_simps)
  2891       by (metis \<open>0 < a\<close> floor_correct less_irrefl nonzero_mult_div_cancel_left real_mult_le_cancel_iff2 times_divide_eq_right)
  2892     also have "... < x"
  2893       by (rule 2)
  2894     finally have "?n * a < x" .
  2895     then show ?thesis
  2896       using Ints_1 Ints_add Ints_of_int that y by blast
  2897   qed
  2898 qed
  2899 
  2900 lemma cell_subdivision_lemma:
  2901   assumes "finite \<F>"
  2902       and "\<And>X. X \<in> \<F> \<Longrightarrow> polytope X"
  2903       and "\<And>X. X \<in> \<F> \<Longrightarrow> aff_dim X \<le> d"
  2904       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"
  2905       and "finite I"
  2906     shows "\<exists>\<G>. \<Union>\<G> = \<Union>\<F> \<and>
  2907                  finite \<G> \<and>
  2908                  (\<forall>C \<in> \<G>. \<exists>D. D \<in> \<F> \<and> C \<subseteq> D) \<and>
  2909                  (\<forall>C \<in> \<F>. \<forall>x \<in> C. \<exists>D. D \<in> \<G> \<and> x \<in> D \<and> D \<subseteq> C) \<and>
  2910                  (\<forall>X \<in> \<G>. polytope X) \<and>
  2911                  (\<forall>X \<in> \<G>. aff_dim X \<le> d) \<and>
  2912                  (\<forall>X \<in> \<G>. \<forall>Y \<in> \<G>. X \<inter> Y face_of X \<and> X \<inter> Y face_of Y) \<and>
  2913                  (\<forall>X \<in> \<G>. \<forall>x \<in> X. \<forall>y \<in> X. \<forall>a b.
  2914                           (a,b) \<in> I \<longrightarrow> a \<bullet> x \<le> b \<and> a \<bullet> y \<le> b \<or>
  2915                                         a \<bullet> x \<ge> b \<and> a \<bullet> y \<ge> b)"
  2916   using \<open>finite I\<close>
  2917 proof induction
  2918   case empty
  2919   then show ?case
  2920     by (rule_tac x="\<F>" in exI) (auto simp: assms)
  2921 next
  2922   case (insert ab I)
  2923   then obtain \<G> where eq: "\<Union>\<G> = \<Union>\<F>" and "finite \<G>"
  2924                    and sub1: "\<And>C. C \<in> \<G> \<Longrightarrow> \<exists>D. D \<in> \<F> \<and> C \<subseteq> D"
  2925                    and sub2: "\<And>C x. C \<in> \<F> \<and> x \<in> C \<Longrightarrow> \<exists>D. D \<in> \<G> \<and> x \<in> D \<and> D \<subseteq> C"
  2926                    and poly: "\<And>X. X \<in> \<G> \<Longrightarrow> polytope X"
  2927                    and aff: "\<And>X. X \<in> \<G> \<Longrightarrow> aff_dim X \<le> d"
  2928                    and face: "\<And>X Y. \<lbrakk>X \<in> \<G>; Y \<in> \<G>\<rbrakk> \<Longrightarrow> X \<inter> Y face_of X \<and> X \<inter> Y face_of Y"
  2929                    and I: "\<And>X x y a b.  \<lbrakk>X \<in> \<G>; x \<in> X; y \<in> X; (a,b) \<in> I\<rbrakk> \<Longrightarrow>
  2930                                     a \<bullet> x \<le> b \<and> a \<bullet> y \<le> b \<or> a \<bullet> x \<ge> b \<and> a \<bullet> y \<ge> b"
  2931     by (auto simp: that)
  2932   obtain a b where "ab = (a,b)"
  2933     by fastforce
  2934   let ?\<G> = "(\<lambda>X. X \<inter> {x. a \<bullet> x \<le> b}) ` \<G> \<union> (\<lambda>X. X \<inter> {x. a \<bullet> x \<ge> b}) ` \<G>"
  2935   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
  2936     by blast
  2937   show ?case
  2938   proof (intro conjI exI)
  2939     show "\<Union>?\<G> = \<Union>\<F>"
  2940       by (force simp: eq [symmetric])
  2941     show "finite ?\<G>"
  2942       using \<open>finite \<G>\<close> by force
  2943     show "\<forall>X \<in> ?\<G>. polytope X"
  2944       by (force simp: poly polytope_Int_polyhedron polyhedron_halfspace_le polyhedron_halfspace_ge)
  2945     show "\<forall>X \<in> ?\<G>. aff_dim X \<le> d"
  2946       by (auto; metis order_trans aff aff_dim_subset inf_le1)
  2947     show "\<forall>X \<in> ?\<G>. \<forall>x \<in> X. \<forall>y \<in> X. \<forall>a b.
  2948                           (a,b) \<in> insert ab I \<longrightarrow> a \<bullet> x \<le> b \<and> a \<bullet> y \<le> b \<or>
  2949                                                   a \<bullet> x \<ge> b \<and> a \<bullet> y \<ge> b"
  2950       using \<open>ab = (a, b)\<close> I by fastforce
  2951     show "\<forall>X \<in> ?\<G>. \<forall>Y \<in> ?\<G>. X \<inter> Y face_of X \<and> X \<inter> Y face_of Y"
  2952       by (auto simp: eqInt halfspace_Int_eq face_of_Int_Int face face_of_halfspace_le face_of_halfspace_ge)
  2953     show "\<forall>C \<in> ?\<G>. \<exists>D. D \<in> \<F> \<and> C \<subseteq> D"
  2954       using sub1 by force
  2955     show "\<forall>C\<in>\<F>. \<forall>x\<in>C. \<exists>D. D \<in> ?\<G> \<and> x \<in> D \<and> D \<subseteq> C"
  2956     proof (intro ballI)
  2957       fix C z
  2958       assume "C \<in> \<F>" "z \<in> C"
  2959       with sub2 obtain D where D: "D \<in> \<G>" "z \<in> D" "D \<subseteq> C" by blast
  2960       have "D \<in> \<G> \<and> z \<in> D \<inter> {x. a \<bullet> x \<le> b} \<and> D \<inter> {x. a \<bullet> x \<le> b} \<subseteq> C \<or>
  2961             D \<in> \<G> \<and> z \<in> D \<inter> {x. a \<bullet> x \<ge> b} \<and> D \<inter> {x. a \<bullet> x \<ge> b} \<subseteq> C"
  2962         using linorder_class.linear [of "a \<bullet> z" b] D by blast
  2963       then show "\<exists>D. D \<in> ?\<G> \<and> z \<in> D \<and> D \<subseteq> C"
  2964         by blast
  2965     qed
  2966   qed
  2967 qed
  2968 
  2969 
  2970 proposition cell_complex_subdivision_exists:
  2971   fixes \<F> :: "'a::euclidean_space set set"
  2972   assumes "0 < e" "finite \<F>"
  2973       and poly: "\<And>X. X \<in> \<F> \<Longrightarrow> polytope X"
  2974       and aff: "\<And>X. X \<in> \<F> \<Longrightarrow> aff_dim X \<le> d"
  2975       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"
  2976   obtains "\<F>'" where "finite \<F>'" "\<Union>\<F>' = \<Union>\<F>" "\<And>X. X \<in> \<F>' \<Longrightarrow> diameter X < e"
  2977                 "\<And>X. X \<in> \<F>' \<Longrightarrow> polytope X" "\<And>X. X \<in> \<F>' \<Longrightarrow> aff_dim X \<le> d"
  2978                 "\<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"
  2979                 "\<And>C. C \<in> \<F>' \<Longrightarrow> \<exists>D. D \<in> \<F> \<and> C \<subseteq> D"
  2980                 "\<And>C x. C \<in> \<F> \<and> x \<in> C \<Longrightarrow> \<exists>D. D \<in> \<F>' \<and> x \<in> D \<and> D \<subseteq> C"
  2981 proof -
  2982   have "bounded(\<Union>\<F>)"
  2983     by (simp add: \<open>finite \<F>\<close> poly bounded_Union polytope_imp_bounded)
  2984   then obtain B where "B > 0" and B: "\<And>x. x \<in> \<Union>\<F> \<Longrightarrow> norm x < B"
  2985     by (meson bounded_pos_less)
  2986   define C where "C \<equiv> {z \<in> \<int>. \<bar>z * e / 2 / real DIM('a)\<bar> \<le> B}"
  2987   define I where "I \<equiv> \<Union>i \<in> Basis. \<Union>j \<in> C. { (i::'a, j * e / 2 / DIM('a)) }"
  2988   have "finite C"
  2989     using finite_int_segment [of "-B / (e / 2 / DIM('a))" "B / (e / 2 / DIM('a))"]
  2990     apply (simp add: C_def)
  2991     apply (erule rev_finite_subset)
  2992     using \<open>0 < e\<close>
  2993     apply (auto simp: divide_simps)
  2994     done
  2995   then have "finite I"
  2996     by (simp add: I_def)
  2997   obtain \<F>' where eq: "\<Union>\<F>' = \<Union>\<F>" and "finite \<F>'"
  2998               and poly: "\<And>X. X \<in> \<F>' \<Longrightarrow> polytope X"
  2999               and aff: "\<And>X. X \<in> \<F>' \<Longrightarrow> aff_dim X \<le> d"
  3000               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"
  3001               and I: "\<And>X x y a b.  \<lbrakk>X \<in> \<F>'; x \<in> X; y \<in> X; (a,b) \<in> I\<rbrakk> \<Longrightarrow>
  3002                                      a \<bullet> x \<le> b \<and> a \<bullet> y \<le> b \<or> a \<bullet> x \<ge> b \<and> a \<bullet> y \<ge> b"
  3003               and sub1: "\<And>C. C \<in> \<F>' \<Longrightarrow> \<exists>D. D \<in> \<F> \<and> C \<subseteq> D"
  3004               and sub2: "\<And>C x. C \<in> \<F> \<and> x \<in> C \<Longrightarrow> \<exists>D. D \<in> \<F>' \<and> x \<in> D \<and> D \<subseteq> C"
  3005     apply (rule exE [OF cell_subdivision_lemma])
  3006     using assms \<open>finite I\<close> apply auto
  3007     done
  3008   show ?thesis
  3009   proof (rule_tac \<F>'="\<F>'" in that)
  3010     show "diameter X < e" if "X \<in> \<F>'" for X
  3011     proof -
  3012       have "diameter X \<le> e/2"
  3013       proof (rule diameter_le)
  3014         show "norm (x - y) \<le> e / 2" if "x \<in> X" "y \<in> X" for x y
  3015         proof -
  3016           have "norm x < B" "norm y < B"
  3017             using B \<open>X \<in> \<F>'\<close> eq that by fastforce+
  3018           have "norm (x - y) \<le> (\<Sum>b\<in>Basis. \<bar>(x-y) \<bullet> b\<bar>)"
  3019             by (rule norm_le_l1)
  3020           also have "... \<le> of_nat (DIM('a)) * (e / 2 / DIM('a))"
  3021           proof (rule sum_bounded_above)
  3022             fix i::'a
  3023             assume "i \<in> Basis"
  3024             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"
  3025               using I \<open>X \<in> \<F>'\<close> that
  3026               by (fastforce simp: I_def)
  3027             show "\<bar>(x - y) \<bullet> i\<bar> \<le> e / 2 / real DIM('a)"
  3028             proof (rule ccontr)
  3029               assume "\<not> \<bar>(x - y) \<bullet> i\<bar> \<le> e / 2 / real DIM('a)"
  3030               then have xyi: "\<bar>i \<bullet> x - i \<bullet> y\<bar> > e / 2 / real DIM('a)"
  3031                 by (simp add: inner_commute inner_diff_right)
  3032               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"
  3033                 using subdivide_interval [OF xyi] DIM_positive \<open>0 < e\<close>
  3034                 by (auto simp: zero_less_divide_iff)
  3035               have "\<bar>i \<bullet> x\<bar> < B"
  3036                 by (metis \<open>i \<in> Basis\<close> \<open>norm x < B\<close> inner_commute norm_bound_Basis_lt)
  3037               have "\<bar>i \<bullet> y\<bar> < B"
  3038                 by (metis \<open>i \<in> Basis\<close> \<open>norm y < B\<close> inner_commute norm_bound_Basis_lt)
  3039               have *: "\<bar>n * e\<bar> \<le> B * (2 * real DIM('a))"
  3040                       if "\<bar>ix\<bar> < B" "\<bar>iy\<bar> < B"
  3041                          and ix: "ix * (2 * real DIM('a)) < n * e"
  3042                          and iy: "n * e < iy * (2 * real DIM('a))" for ix iy
  3043               proof (rule abs_leI)
  3044                 have "iy * (2 * real DIM('a)) \<le> B * (2 * real DIM('a))"
  3045                   by (rule mult_right_mono) (use \<open>\<bar>iy\<bar> < B\<close> in linarith)+
  3046                 then show "n * e \<le> B * (2 * real DIM('a))"
  3047                   using iy by linarith
  3048               next
  3049                 have "- ix * (2 * real DIM('a)) \<le> B * (2 * real DIM('a))"
  3050                   by (rule mult_right_mono) (use \<open>\<bar>ix\<bar> < B\<close> in linarith)+
  3051                 then show "- (n * e) \<le> B * (2 * real DIM('a))"
  3052                   using ix by linarith
  3053               qed
  3054               have "n \<in> C"
  3055                 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>)
  3056               show False
  3057                 using  I' [OF \<open>n \<in> C\<close> refl] n  by auto
  3058             qed
  3059           qed
  3060           also have "... = e / 2"
  3061             by simp
  3062           finally show ?thesis .
  3063         qed
  3064       qed (use \<open>0 < e\<close> in force)
  3065       also have "... < e"
  3066         by (simp add: \<open>0 < e\<close>)
  3067       finally show ?thesis .
  3068     qed
  3069   qed (auto simp: eq poly aff face sub1 sub2 \<open>finite \<F>'\<close>)
  3070 qed
  3071 
  3072 
  3073 subsection\<open>Simplexes\<close>
  3074 
  3075 text\<open>The notion of n-simplex for integer @{term"n \<ge> -1"}\<close>
  3076 definition simplex :: "int \<Rightarrow> 'a::euclidean_space set \<Rightarrow> bool" (infix "simplex" 50)
  3077   where "n simplex S \<equiv> \<exists>C. ~(affine_dependent C) \<and> int(card C) = n + 1 \<and> S = convex hull C"
  3078 
  3079 lemma simplex:
  3080     "n simplex S \<longleftrightarrow> (\<exists>C. finite C \<and>
  3081                        ~(affine_dependent C) \<and>
  3082                        int(card C) = n + 1 \<and>
  3083                        S = convex hull C)"
  3084   by (auto simp add: simplex_def intro: aff_independent_finite)
  3085 
  3086 lemma simplex_convex_hull:
  3087    "~affine_dependent C \<and> int(card C) = n + 1 \<Longrightarrow> n simplex (convex hull C)"
  3088   by (auto simp add: simplex_def)
  3089 
  3090 lemma convex_simplex: "n simplex S \<Longrightarrow> convex S"
  3091   by (metis convex_convex_hull simplex_def)
  3092 
  3093 lemma compact_simplex: "n simplex S \<Longrightarrow> compact S"
  3094   unfolding simplex
  3095   using finite_imp_compact_convex_hull by blast
  3096 
  3097 lemma closed_simplex: "n simplex S \<Longrightarrow> closed S"
  3098   by (simp add: compact_imp_closed compact_simplex)
  3099 
  3100 lemma simplex_imp_polytope:
  3101    "n simplex S \<Longrightarrow> polytope S"
  3102   unfolding simplex_def polytope_def
  3103   using aff_independent_finite by blast
  3104 
  3105 lemma simplex_imp_polyhedron:
  3106    "n simplex S \<Longrightarrow> polyhedron S"
  3107   by (simp add: polytope_imp_polyhedron simplex_imp_polytope)
  3108 
  3109 lemma simplex_dim_ge: "n simplex S \<Longrightarrow> -1 \<le> n"
  3110   by (metis (no_types, hide_lams) aff_dim_geq affine_independent_iff_card diff_add_cancel diff_diff_eq2 simplex_def)
  3111 
  3112 lemma simplex_empty [simp]: "n simplex {} \<longleftrightarrow> n = -1"
  3113 proof
  3114   assume "n simplex {}"
  3115   then show "n = -1"
  3116     unfolding simplex by (metis card_empty convex_hull_eq_empty diff_0 diff_eq_eq of_nat_0)
  3117 next
  3118   assume "n = -1" then show "n simplex {}"
  3119     by (fastforce simp: simplex)
  3120 qed
  3121 
  3122 lemma simplex_minus_1 [simp]: "-1 simplex S \<longleftrightarrow> S = {}"
  3123   by (metis simplex cancel_comm_monoid_add_class.diff_cancel card_0_eq diff_minus_eq_add of_nat_eq_0_iff simplex_empty)
  3124 
  3125 
  3126 lemma aff_dim_simplex:
  3127    "n simplex S \<Longrightarrow> aff_dim S = n"
  3128   by (metis simplex add.commute add_diff_cancel_left' aff_dim_convex_hull affine_independent_iff_card)
  3129 
  3130 lemma zero_simplex_sing: "0 simplex {a}"
  3131   apply (simp add: simplex_def)
  3132   by (metis affine_independent_1 card_empty card_insert_disjoint convex_hull_singleton empty_iff finite.emptyI)
  3133 
  3134 lemma simplex_sing [simp]: "n simplex {a} \<longleftrightarrow> n = 0"
  3135   using aff_dim_simplex aff_dim_sing zero_simplex_sing by blast
  3136 
  3137 lemma simplex_zero: "0 simplex S \<longleftrightarrow> (\<exists>a. S = {a})"
  3138 apply (auto simp: )
  3139   using aff_dim_eq_0 aff_dim_simplex by blast
  3140 
  3141 lemma one_simplex_segment: "a \<noteq> b \<Longrightarrow> 1 simplex closed_segment a b"
  3142   apply (simp add: simplex_def)
  3143   apply (rule_tac x="{a,b}" in exI)
  3144   apply (auto simp: segment_convex_hull)
  3145   done
  3146 
  3147 lemma simplex_segment_cases:
  3148    "(if a = b then 0 else 1) simplex closed_segment a b"
  3149   by (auto simp: one_simplex_segment)
  3150 
  3151 lemma simplex_segment:
  3152    "\<exists>n. n simplex closed_segment a b"
  3153   using simplex_segment_cases by metis
  3154 
  3155 lemma polytope_lowdim_imp_simplex:
  3156   assumes "polytope P" "aff_dim P \<le> 1"
  3157   obtains n where "n simplex P"
  3158 proof (cases "P = {}")
  3159   case True
  3160   then show ?thesis
  3161     by (simp add: that)
  3162 next
  3163   case False
  3164   then show ?thesis
  3165     by (metis assms compact_convex_collinear_segment collinear_aff_dim polytope_imp_compact polytope_imp_convex simplex_segment_cases that)
  3166 qed
  3167 
  3168 lemma simplex_insert_dimplus1:
  3169   fixes n::int
  3170   assumes "n simplex S" and a: "a \<notin> affine hull S"
  3171   shows "(n+1) simplex (convex hull (insert a S))"
  3172 proof -
  3173   obtain C where C: "finite C" "~(affine_dependent C)" "int(card C) = n+1" and S: "S = convex hull C"
  3174     using assms unfolding simplex by force
  3175   show ?thesis
  3176     unfolding simplex
  3177   proof (intro exI conjI)
  3178       have "aff_dim S = n"
  3179         using aff_dim_simplex assms(1) by blast
  3180       moreover have "a \<notin> affine hull C"
  3181         using S a affine_hull_convex_hull by blast
  3182       moreover have "a \<notin> C"
  3183           using S a hull_inc by fastforce
  3184       ultimately show "\<not> affine_dependent (insert a C)"
  3185         by (simp add: C S aff_dim_convex_hull aff_dim_insert affine_independent_iff_card)
  3186   next
  3187     have "a \<notin> C"
  3188       using S a hull_inc by fastforce
  3189     then show "int (card (insert a C)) = n + 1 + 1"
  3190       by (simp add: C)
  3191   next
  3192     show "convex hull insert a S = convex hull (insert a C)"
  3193       by (simp add: S convex_hull_insert_segments)
  3194   qed (use C in auto)
  3195 qed
  3196 
  3197 subsection\<open>Simplicial complexes and triangulations\<close>
  3198 
  3199 definition simplicial_complex where
  3200  "simplicial_complex \<C> \<equiv>
  3201         finite \<C> \<and>
  3202         (\<forall>S \<in> \<C>. \<exists>n. n simplex S) \<and>
  3203         (\<forall>F S. S \<in> \<C> \<and> F face_of S \<longrightarrow> F \<in> \<C>) \<and>
  3204         (\<forall>S S'. S \<in> \<C> \<and> S' \<in> \<C>
  3205                 \<longrightarrow> (S \<inter> S') face_of S \<and> (S \<inter> S') face_of S')"
  3206 
  3207 definition triangulation where
  3208  "triangulation \<T> \<equiv>
  3209         finite \<T> \<and>
  3210         (\<forall>T \<in> \<T>. \<exists>n. n simplex T) \<and>
  3211         (\<forall>T T'. T \<in> \<T> \<and> T' \<in> \<T>
  3212                 \<longrightarrow> (T \<inter> T') face_of T \<and> (T \<inter> T') face_of T')"
  3213 
  3214 
  3215 subsection\<open>Refining a cell complex to a simplicial complex\<close>
  3216 
  3217 lemma convex_hull_insert_Int_eq:
  3218   fixes z :: "'a :: euclidean_space"
  3219   assumes z: "z \<in> rel_interior S"
  3220       and T: "T \<subseteq> rel_frontier S"
  3221       and U: "U \<subseteq> rel_frontier S"
  3222       and "convex S" "convex T" "convex U"
  3223   shows "convex hull (insert z T) \<inter> convex hull (insert z U) = convex hull (insert z (T \<inter> U))"
  3224     (is "?lhs = ?rhs")
  3225 proof
  3226   show "?lhs \<subseteq> ?rhs"
  3227   proof (cases "T={} \<or> U={}")
  3228     case True then show ?thesis by auto
  3229   next
  3230     case False
  3231     then have "T \<noteq> {}" "U \<noteq> {}" by auto
  3232     have TU: "convex (T \<inter> U)"
  3233       by (simp add: \<open>convex T\<close> \<open>convex U\<close> convex_Int)
  3234     have "(\<Union>x\<in>T. closed_segment z x) \<inter> (\<Union>x\<in>U. closed_segment z x)
  3235           \<subseteq> (if T \<inter> U = {} then {z} else UNION (T \<inter> U) (closed_segment z))" (is "_ \<subseteq> ?IF")
  3236     proof clarify
  3237       fix x t u
  3238       assume xt: "x \<in> closed_segment z t"
  3239         and xu: "x \<in> closed_segment z u"
  3240         and "t \<in> T" "u \<in> U"
  3241       then have ne: "t \<noteq> z" "u \<noteq> z"
  3242         using T U z unfolding rel_frontier_def by blast+
  3243       show "x \<in> ?IF"
  3244       proof (cases "x = z")
  3245         case True then show ?thesis by auto
  3246       next
  3247         case False
  3248         have t: "t \<in> closure S"
  3249           using T \<open>t \<in> T\<close> rel_frontier_def by auto
  3250         have u: "u \<in> closure S"
  3251           using U \<open>u \<in> U\<close> rel_frontier_def by auto
  3252         show ?thesis
  3253         proof (cases "t = u")
  3254           case True
  3255           then show ?thesis
  3256             using \<open>t \<in> T\<close> \<open>u \<in> U\<close> xt by auto
  3257         next
  3258           case False
  3259           have tnot: "t \<notin> closed_segment u z"
  3260           proof -
  3261             have "t \<in> closure S - rel_interior S"
  3262               using T \<open>t \<in> T\<close> rel_frontier_def by blast
  3263             then have "t \<notin> open_segment z u"
  3264               by (meson DiffD2 rel_interior_closure_convex_segment [OF \<open>convex S\<close> z u] subsetD)
  3265             then show ?thesis
  3266               by (simp add: \<open>t \<noteq> u\<close> \<open>t \<noteq> z\<close> open_segment_commute open_segment_def)
  3267           qed
  3268           moreover have "u \<notin> closed_segment z t"
  3269             using rel_interior_closure_convex_segment [OF \<open>convex S\<close> z t] \<open>u \<in> U\<close> \<open>u \<noteq> z\<close>
  3270               U [unfolded rel_frontier_def] tnot
  3271             by (auto simp: closed_segment_eq_open)
  3272           ultimately
  3273           have "~(between (t,u) z | between (u,z) t | between (z,t) u)" if "x \<noteq> z"
  3274             using that xt xu
  3275             apply (simp add: between_mem_segment [symmetric])
  3276             by (metis between_commute between_trans_2 between_antisym)
  3277           then have "~ collinear {t, z, u}" if "x \<noteq> z"
  3278             by (auto simp: that collinear_between_cases between_commute)
  3279           moreover have "collinear {t, z, x}"
  3280             by (metis closed_segment_commute collinear_2 collinear_closed_segment collinear_triples ends_in_segment(1) insert_absorb insert_absorb2 xt)
  3281           moreover have "collinear {z, x, u}"
  3282             by (metis closed_segment_commute collinear_2 collinear_closed_segment collinear_triples ends_in_segment(1) insert_absorb insert_absorb2 xu)
  3283           ultimately have False
  3284             using collinear_3_trans [of t z x u] \<open>x \<noteq> z\<close> by blast
  3285           then show ?thesis by metis
  3286         qed
  3287       qed
  3288     qed
  3289     then show ?thesis
  3290       using False \<open>convex T\<close> \<open>convex U\<close> TU
  3291       by (simp add: convex_hull_insert_segments hull_same split: if_split_asm)
  3292   qed
  3293   show "?rhs \<subseteq> ?lhs"
  3294     by (metis inf_greatest hull_mono inf.cobounded1 inf.cobounded2 insert_mono)
  3295 qed
  3296 
  3297 lemma simplicial_subdivision_aux:
  3298   assumes "finite \<M>"
  3299       and "\<And>C. C \<in> \<M> \<Longrightarrow> polytope C"
  3300       and "\<And>C. C \<in> \<M> \<Longrightarrow> aff_dim C \<le> of_nat n"
  3301       and "\<And>C F. \<lbrakk>C \<in> \<M>; F face_of C\<rbrakk> \<Longrightarrow> F \<in> \<M>"
  3302       and "\<And>C1 C2. \<lbrakk>C1 \<in> \<M>; C2 \<in> \<M>\<rbrakk> \<Longrightarrow> C1 \<inter> C2 face_of C1 \<and> C1 \<inter> C2 face_of C2"
  3303     shows "\<exists>\<T>. simplicial_complex \<T> \<and>
  3304                 (\<forall>K \<in> \<T>. aff_dim K \<le> of_nat n) \<and>
  3305                 \<Union>\<T> = \<Union>\<M> \<and>
  3306                 (\<forall>C \<in> \<M>. \<exists>F. finite F \<and> F \<subseteq> \<T> \<and> C = \<Union>F) \<and>
  3307                 (\<forall>K \<in> \<T>. \<exists>C. C \<in> \<M> \<and> K \<subseteq> C)"
  3308   using assms
  3309 proof (induction n arbitrary: \<M> rule: less_induct)
  3310   case (less n)
  3311   then have poly\<M>: "\<And>C. C \<in> \<M> \<Longrightarrow> polytope C"
  3312       and aff\<M>:    "\<And>C. C \<in> \<M> \<Longrightarrow> aff_dim C \<le> of_nat n"
  3313       and face\<M>:   "\<And>C F. \<lbrakk>C \<in> \<M>; F face_of C\<rbrakk> \<Longrightarrow> F \<in> \<M>"
  3314       and intface\<M>: "\<And>C1 C2. \<lbrakk>C1 \<in> \<M>; C2 \<in> \<M>\<rbrakk> \<Longrightarrow> C1 \<inter> C2 face_of C1 \<and> C1 \<inter> C2 face_of C2"
  3315     by metis+
  3316   show ?case
  3317   proof (cases "n \<le> 1")
  3318     case True
  3319     have "\<And>s. \<lbrakk>n \<le> 1; s \<in> \<M>\<rbrakk> \<Longrightarrow> \<exists>m. m simplex s"
  3320       using poly\<M> aff\<M> by (force intro: polytope_lowdim_imp_simplex)
  3321     then show ?thesis
  3322       unfolding simplicial_complex_def
  3323       apply (rule_tac x="\<M>" in exI)
  3324       using True by (auto simp: less.prems)
  3325   next
  3326     case False
  3327     define \<S> where "\<S> \<equiv> {C \<in> \<M>. aff_dim C < n}"
  3328     have "finite \<S>" "\<And>C. C \<in> \<S> \<Longrightarrow> polytope C" "\<And>C. C \<in> \<S> \<Longrightarrow> aff_dim C \<le> int (n - 1)"
  3329          "\<And>C F. \<lbrakk>C \<in> \<S>; F face_of C\<rbrakk> \<Longrightarrow> F \<in> \<S>"
  3330          "\<And>C1 C2. \<lbrakk>C1 \<in> \<S>; C2 \<in> \<S>\<rbrakk>  \<Longrightarrow> C1 \<inter> C2 face_of C1 \<and> C1 \<inter> C2 face_of C2"
  3331       using less.prems
  3332       apply (auto simp: \<S>_def)
  3333       by (metis aff_dim_subset face_of_imp_subset less_le not_le)
  3334     with less.IH [of "n-1" \<S>] False
  3335     obtain \<U> where "simplicial_complex \<U>"
  3336            and aff_dim\<U>: "\<And>K. K \<in> \<U> \<Longrightarrow> aff_dim K \<le> int (n - 1)"
  3337            and        "\<Union>\<U> = \<Union>\<S>"
  3338            and fin\<U>:  "\<And>C. C \<in> \<S> \<Longrightarrow> \<exists>F. finite F \<and> F \<subseteq> \<U> \<and> C = \<Union>F"
  3339            and C\<U>:    "\<And>K. K \<in> \<U> \<Longrightarrow> \<exists>C. C \<in> \<S> \<and> K \<subseteq> C"
  3340       by auto
  3341     then have "finite \<U>"
  3342          and simpl\<U>: "\<And>S. S \<in> \<U> \<Longrightarrow> \<exists>n. n simplex S"
  3343          and face\<U>:  "\<And>F S. \<lbrakk>S \<in> \<U>; F face_of S\<rbrakk> \<Longrightarrow> F \<in> \<U>"
  3344          and faceI\<U>: "\<And>S S'. \<lbrakk>S \<in> \<U>; S' \<in> \<U>\<rbrakk> \<Longrightarrow> (S \<inter> S') face_of S \<and> (S \<inter> S') face_of S'"
  3345       by (auto simp: simplicial_complex_def)
  3346     define \<N> where "\<N> \<equiv> {C \<in> \<M>. aff_dim C = n}"
  3347     have "finite \<N>"
  3348       by (simp add: \<N>_def less.prems(1))
  3349     have poly\<N>: "\<And>C. C \<in> \<N> \<Longrightarrow> polytope C"
  3350       and convex\<N>: "\<And>C. C \<in> \<N> \<Longrightarrow> convex C"
  3351       and closed\<N>: "\<And>C. C \<in> \<N> \<Longrightarrow> closed C"
  3352       by (auto simp: \<N>_def poly\<M> polytope_imp_convex polytope_imp_closed)
  3353     have in_rel_interior: "(SOME z. z \<in> rel_interior C) \<in> rel_interior C" if "C \<in> \<N>" for C
  3354         using that poly\<M> polytope_imp_convex rel_interior_aff_dim some_in_eq by (fastforce simp: \<N>_def)
  3355     have *: "\<exists>T. ~affine_dependent T \<and> card T \<le> n \<and> aff_dim K < n \<and> K = convex hull T"
  3356       if "K \<in> \<U>" for K
  3357     proof -
  3358       obtain r where r: "r simplex K"
  3359         using \<open>K \<in> \<U>\<close> simpl\<U> by blast
  3360       have "r = aff_dim K"
  3361         using \<open>r simplex K\<close> aff_dim_simplex by blast
  3362       with r
  3363       show ?thesis
  3364         unfolding simplex_def
  3365         using False \<open>\<And>K. K \<in> \<U> \<Longrightarrow> aff_dim K \<le> int (n - 1)\<close> that by fastforce
  3366     qed
  3367     have ahK_C_disjoint: "affine hull K \<inter> rel_interior C = {}"
  3368       if "C \<in> \<N>" "K \<in> \<U>" "K \<subseteq> rel_frontier C" for C K
  3369     proof -
  3370       have "convex C" "closed C"
  3371         by (auto simp: convex\<N> closed\<N> \<open>C \<in> \<N>\<close>)
  3372       obtain F where F: "F face_of C" and "F \<noteq> C" "K \<subseteq> F"
  3373       proof -
  3374         obtain L where "L \<in> \<S>" "K \<subseteq> L"
  3375           using \<open>K \<in> \<U>\<close> C\<U> by blast
  3376         have "K \<le> rel_frontier C"
  3377           by (simp add: \<open>K \<subseteq> rel_frontier C\<close>)
  3378         also have "... \<le> C"
  3379           by (simp add: \<open>closed C\<close> rel_frontier_def subset_iff)
  3380         finally have "K \<subseteq> C" .
  3381         have "L \<inter> C face_of C"
  3382           using \<N>_def \<S>_def \<open>C \<in> \<N>\<close> \<open>L \<in> \<S>\<close> intface\<M> by auto
  3383         moreover have "L \<inter> C \<noteq> C"
  3384           using \<open>C \<in> \<N>\<close> \<open>L \<in> \<S>\<close>
  3385           apply (clarsimp simp: \<N>_def \<S>_def)
  3386           by (metis aff_dim_subset inf_le1 not_le)
  3387         moreover have "K \<subseteq> L \<inter> C"
  3388           using \<open>C \<in> \<N>\<close> \<open>L \<in> \<S>\<close> \<open>K \<subseteq> C\<close> \<open>K \<subseteq> L\<close>
  3389           by (auto simp: \<N>_def \<S>_def)
  3390         ultimately show ?thesis using that by metis
  3391       qed
  3392       have "affine hull F \<inter> rel_interior C = {}"
  3393         by (rule affine_hull_face_of_disjoint_rel_interior [OF \<open>convex C\<close> F \<open>F \<noteq> C\<close>])
  3394       with hull_mono [OF \<open>K \<subseteq> F\<close>]
  3395       show "affine hull K \<inter> rel_interior C = {}"
  3396         by fastforce
  3397     qed
  3398     let ?\<T> = "(\<Union>C \<in> \<N>. \<Union>K \<in> \<U> \<inter> Pow (rel_frontier C).
  3399                      {convex hull (insert (SOME z. z \<in> rel_interior C) K)})"
  3400     have "\<exists>\<T>. simplicial_complex \<T> \<and>
  3401               (\<forall>K \<in> \<T>. aff_dim K \<le> of_nat n) \<and>
  3402               (\<forall>C \<in> \<M>. \<exists>F. F \<subseteq> \<T> \<and> C = \<Union>F) \<and>
  3403               (\<forall>K \<in> \<T>. \<exists>C. C \<in> \<M> \<and> K \<subseteq> C)"
  3404     proof (rule exI, intro conjI ballI)
  3405       show "simplicial_complex (\<U> \<union> ?\<T>)"
  3406         unfolding simplicial_complex_def
  3407       proof (intro conjI impI ballI allI)
  3408         show "finite (\<U> \<union> ?\<T>)"
  3409           using \<open>finite \<U>\<close> \<open>finite \<N>\<close> by simp
  3410         show "\<exists>n. n simplex S" if "S \<in> \<U> \<union> ?\<T>" for S
  3411           using that ahK_C_disjoint in_rel_interior simpl\<U> simplex_insert_dimplus1 by fastforce
  3412         show "F \<in> \<U> \<union> ?\<T>" if S: "S \<in> \<U> \<union> ?\<T> \<and> F face_of S" for F S
  3413         proof -
  3414           have "F \<in> \<U>" if "S \<in> \<U>"
  3415             using S face\<U> that by blast
  3416           moreover have "F \<in> \<U> \<union> ?\<T>"
  3417             if "F face_of S" "C \<in> \<N>" "K \<in> \<U>" and "K \<subseteq> rel_frontier C"
  3418               and S: "S = convex hull insert (SOME z. z \<in> rel_interior C) K" for C K
  3419           proof -
  3420             let ?z = "SOME z. z \<in> rel_interior C"
  3421             have "?z \<in> rel_interior C"
  3422               by (simp add: in_rel_interior \<open>C \<in> \<N>\<close>)
  3423             moreover
  3424             obtain I where "\<not> affine_dependent I" "card I \<le> n" "aff_dim K < int n" "K = convex hull I"
  3425               using * [OF \<open>K \<in> \<U>\<close>] by auto
  3426             ultimately have "?z \<notin> affine hull I"
  3427               using ahK_C_disjoint affine_hull_convex_hull that by blast
  3428             have "compact I" "finite I"
  3429               by (auto simp: \<open>\<not> affine_dependent I\<close> aff_independent_finite finite_imp_compact)
  3430             moreover have "F face_of convex hull insert ?z I"
  3431               by (metis S \<open>F face_of S\<close> \<open>K = convex hull I\<close> convex_hull_eq_empty convex_hull_insert_segments hull_hull)
  3432             ultimately obtain J where "J \<subseteq> insert ?z I" "F = convex hull J"
  3433               using face_of_convex_hull_subset [of "insert ?z I" F] by auto
  3434             show ?thesis
  3435             proof (cases "?z \<in> J")
  3436               case True
  3437               have "F \<in> (\<Union>K\<in>\<U> \<inter> Pow (rel_frontier C). {convex hull insert ?z K})"
  3438               proof
  3439                 have "convex hull (J - {?z}) face_of K"
  3440                   by (metis True \<open>J \<subseteq> insert ?z I\<close> \<open>K = convex hull I\<close> \<open>\<not> affine_dependent I\<close> face_of_convex_hull_affine_independent subset_insert_iff)
  3441                 then have "convex hull (J - {?z}) \<in> \<U>"
  3442                   by (rule face\<U> [OF \<open>K \<in> \<U>\<close>])
  3443                 moreover
  3444                 have "\<And>x. x \<in> convex hull (J - {?z}) \<Longrightarrow> x \<in> rel_frontier C"
  3445                   by (metis True \<open>J \<subseteq> insert ?z I\<close> \<open>K = convex hull I\<close> subsetD hull_mono subset_insert_iff that(4))
  3446                 ultimately show "convex hull (J - {?z}) \<in> \<U> \<inter> Pow (rel_frontier C)" by auto
  3447                 let ?F = "convex hull insert ?z (convex hull (J - {?z}))"
  3448                 have "F \<subseteq> ?F"
  3449                   apply (clarsimp simp: \<open>F = convex hull J\<close>)
  3450                   by (metis True subsetD hull_mono hull_subset subset_insert_iff)
  3451                 moreover have "?F \<subseteq> F"
  3452                   apply (clarsimp simp: \<open>F = convex hull J\<close>)
  3453                   by (metis (no_types, lifting) True convex_hull_eq_empty convex_hull_insert_segments hull_hull insert_Diff)
  3454                 ultimately
  3455                 show "F \<in> {?F}" by auto
  3456               qed
  3457               with \<open>C\<in>\<N>\<close> show ?thesis by auto
  3458             next
  3459               case False
  3460               then have "F \<in> \<U>"
  3461                 using face_of_convex_hull_affine_independent [OF \<open>\<not> affine_dependent I\<close>]
  3462                 by (metis Int_absorb2 Int_insert_right_if0 \<open>F = convex hull J\<close> \<open>J \<subseteq> insert ?z I\<close> \<open>K = convex hull I\<close> face\<U> inf_le2 \<open>K \<in> \<U>\<close>)
  3463               then show "F \<in> \<U> \<union> ?\<T>"
  3464                 by blast
  3465             qed
  3466           qed
  3467           ultimately show ?thesis
  3468             using that by auto
  3469         qed
  3470         have "(S \<inter> S' face_of S) \<and> (S \<inter> S' face_of S')"
  3471           if "S \<in> \<U> \<union> ?\<T>" "S' \<in> \<U> \<union> ?\<T>" for S S'
  3472         proof -
  3473           have symmy: "\<lbrakk>\<And>X Y. R X Y \<Longrightarrow> R Y X;
  3474                         \<And>X Y. \<lbrakk>X \<in> \<U>; Y \<in> \<U>\<rbrakk> \<Longrightarrow> R X Y;
  3475                         \<And>X Y. \<lbrakk>X \<in> \<U>; Y \<in> ?\<T>\<rbrakk> \<Longrightarrow> R X Y;
  3476                         \<And>X Y. \<lbrakk>X \<in> ?\<T>; Y \<in> ?\<T>\<rbrakk> \<Longrightarrow> R X Y\<rbrakk> \<Longrightarrow> R S S'" for R
  3477             using that by (metis (no_types, lifting) Un_iff)
  3478           show ?thesis
  3479           proof (rule symmy)
  3480             show "Y \<inter> X face_of Y \<and> Y \<inter> X face_of X"
  3481               if "X \<inter> Y face_of X \<and> X \<inter> Y face_of Y" for X Y :: "'a set"
  3482               by (simp add: inf_commute that)
  3483           next
  3484             show "X \<inter> Y face_of X \<and> X \<inter> Y face_of Y"
  3485               if "X \<in> \<U>" and "Y \<in> \<U>" for X Y
  3486               by (simp add: faceI\<U> that)
  3487           next
  3488             show "X \<inter> Y face_of X \<and> X \<inter> Y face_of Y"
  3489               if XY: "X \<in> \<U>" "Y \<in> ?\<T>" for X Y
  3490             proof -
  3491               obtain C K
  3492                 where "C \<in> \<N>" "K \<in> \<U>" "K \<subseteq> rel_frontier C"
  3493                 and Y: "Y = convex hull insert (SOME z. z \<in> rel_interior C) K"
  3494                 using XY by blast
  3495               have "convex C"
  3496                 by (simp add: \<open>C \<in> \<N>\<close> convex\<N>)
  3497               have "K \<subseteq> C"
  3498                 by (metis DiffE \<open>C \<in> \<N>\<close> \<open>K \<subseteq> rel_frontier C\<close> closed\<N> closure_closed rel_frontier_def subset_iff)
  3499               let ?z = "(SOME z. z \<in> rel_interior C)"
  3500               have z: "?z \<in> rel_interior C"
  3501                 using \<open>C \<in> \<N>\<close> in_rel_interior by blast
  3502               obtain D where "D \<in> \<S>" "X \<subseteq> D"
  3503                 using C\<U> \<open>X \<in> \<U>\<close> by blast
  3504               have "D \<inter> rel_interior C = (C \<inter> D) \<inter> rel_interior C"
  3505                 using rel_interior_subset by blast
  3506               also have "(C \<inter> D) \<inter> rel_interior C = {}"
  3507               proof (rule face_of_disjoint_rel_interior)
  3508                 show "C \<inter> D face_of C"
  3509                   using \<N>_def \<S>_def \<open>C \<in> \<N>\<close> \<open>D \<in> \<S>\<close> intface\<M> by blast
  3510                 show "C \<inter> D \<noteq> C"
  3511                   by (metis (mono_tags, lifting) Int_lower2 \<N>_def \<S>_def \<open>C \<in> \<N>\<close> \<open>D \<in> \<S>\<close> aff_dim_subset mem_Collect_eq not_le)
  3512               qed
  3513               finally have DC: "D \<inter> rel_interior C = {}" .
  3514               have eq: "X \<inter> convex hull (insert ?z K) = X \<inter> convex hull K"
  3515                 apply (rule Int_convex_hull_insert_rel_exterior [OF \<open>convex C\<close> \<open>K \<subseteq> C\<close> z])
  3516                 using DC by (meson \<open>X \<subseteq> D\<close> disjnt_def disjnt_subset1)
  3517               obtain I where I: "\<not> affine_dependent I"
  3518                          and Keq: "K = convex hull I" and [simp]: "convex hull K = K"
  3519                 using "*" \<open>K \<in> \<U>\<close> by force
  3520               then have "?z \<notin> affine hull I"
  3521                 using ahK_C_disjoint \<open>C \<in> \<N>\<close> \<open>K \<in> \<U>\<close> \<open>K \<subseteq> rel_frontier C\<close> affine_hull_convex_hull z by blast
  3522               have "X \<inter> K face_of K"
  3523                 by (simp add: \<open>K \<in> \<U>\<close> faceI\<U> \<open>X \<in> \<U>\<close>)
  3524               also have "... face_of convex hull insert ?z K"
  3525                 by (metis I Keq \<open>?z \<notin> affine hull I\<close> aff_independent_finite convex_convex_hull face_of_convex_hull_insert face_of_refl hull_insert)
  3526               finally have "X \<inter> K face_of convex hull insert ?z K" .
  3527               then show ?thesis
  3528                 using "*" \<open>K \<in> \<U>\<close> faceI\<U> that(1) by (fastforce simp add: Y eq)
  3529             qed
  3530           next
  3531             show "X \<inter> Y face_of X \<and> X \<inter> Y face_of Y"
  3532               if XY: "X \<in> ?\<T>" "Y \<in> ?\<T>" for X Y
  3533             proof -
  3534               obtain C K D L
  3535                 where "C \<in> \<N>" "K \<in> \<U>" "K \<subseteq> rel_frontier C"
  3536                 and X: "X = convex hull insert (SOME z. z \<in> rel_interior C) K"
  3537                 and "D \<in> \<N>" "L \<in> \<U>" "L \<subseteq> rel_frontier D"
  3538                 and Y: "Y = convex hull insert (SOME z. z \<in> rel_interior D) L"
  3539                 using XY by blast
  3540               let ?z = "(SOME z. z \<in> rel_interior C)"
  3541               have z: "?z \<in> rel_interior C"
  3542                 using \<open>C \<in> \<N>\<close> in_rel_interior by blast
  3543               have "convex C"
  3544                 by (simp add: \<open>C \<in> \<N>\<close> convex\<N>)
  3545               have "convex K"
  3546                 using "*" \<open>K \<in> \<U>\<close> by blast
  3547               have "convex L"
  3548                 by (meson \<open>L \<in> \<U>\<close> convex_simplex simpl\<U>)
  3549               show ?thesis
  3550               proof (cases "D=C")
  3551                 case True
  3552                 then have "L \<subseteq> rel_frontier C"
  3553                   using \<open>L \<subseteq> rel_frontier D\<close> by auto
  3554                 show ?thesis
  3555                   apply (simp add: X Y True)
  3556                   apply (simp add: convex_hull_insert_Int_eq [OF z] \<open>K \<subseteq> rel_frontier C\<close> \<open>L \<subseteq> rel_frontier C\<close> \<open>convex C\<close> \<open>convex K\<close> \<open>convex L\<close>)
  3557                   using face_of_polytope_insert2
  3558                   by (metis "*" IntI \<open>C \<in> \<N>\<close> \<open>K \<in> \<U>\<close> \<open>L \<in> \<U>\<close>\<open>K \<subseteq> rel_frontier C\<close> \<open>L \<subseteq> rel_frontier C\<close> aff_independent_finite ahK_C_disjoint empty_iff faceI\<U> polytope_convex_hull z)
  3559               next
  3560                 case False
  3561                 have "convex D"
  3562                   by (simp add: \<open>D \<in> \<N>\<close> convex\<N>)
  3563                 have "K \<subseteq> C"
  3564                   by (metis DiffE \<open>C \<in> \<N>\<close> \<open>K \<subseteq> rel_frontier C\<close> closed\<N> closure_closed rel_frontier_def subset_eq)
  3565                 have "L \<subseteq> D"
  3566                   by (metis DiffE \<open>D \<in> \<N>\<close> \<open>L \<subseteq> rel_frontier D\<close> closed\<N> closure_closed rel_frontier_def subset_eq)
  3567                 let ?w = "(SOME w. w \<in> rel_interior D)"
  3568                 have w: "?w \<in> rel_interior D"
  3569                   using \<open>D \<in> \<N>\<close> in_rel_interior by blast
  3570                 have "C \<inter> rel_interior D = (D \<inter> C) \<inter> rel_interior D"
  3571                   using rel_interior_subset by blast
  3572                 also have "(D \<inter> C) \<inter> rel_interior D = {}"
  3573                 proof (rule face_of_disjoint_rel_interior)
  3574                   show "D \<inter> C face_of D"
  3575                     using \<N>_def \<open>C \<in> \<N>\<close> \<open>D \<in> \<N>\<close> intface\<M> by blast
  3576                   have "D \<in> \<M> \<and> aff_dim D = int n"
  3577                     using \<N>_def \<open>D \<in> \<N>\<close> by blast
  3578                   moreover have "C \<in> \<M> \<and> aff_dim C = int n"
  3579                     using \<N>_def \<open>C \<in> \<N>\<close> by blast
  3580                   ultimately show "D \<inter> C \<noteq> D"
  3581                     by (metis False face_of_aff_dim_lt inf.idem inf_le1 intface\<M> not_le poly\<M> polytope_imp_convex)
  3582                 qed
  3583                 finally have CD: "C \<inter> (rel_interior D) = {}" .
  3584                 have zKC: "(convex hull insert ?z K) \<subseteq> C"
  3585                   by (metis DiffE \<open>C \<in> \<N>\<close> \<open>K \<subseteq> rel_frontier C\<close> closed\<N> closure_closed convex\<N> hull_minimal insert_subset rel_frontier_def rel_interior_subset subset_iff z)
  3586                 have eq: "convex hull (insert ?z K) \<inter> convex hull (insert ?w L) =
  3587                           convex hull (insert ?z K) \<inter> convex hull L"
  3588                   apply (rule Int_convex_hull_insert_rel_exterior [OF \<open>convex D\<close> \<open>L \<subseteq> D\<close> w])
  3589                   using zKC CD apply (force simp: disjnt_def)
  3590                   done
  3591                 have ch_id: "convex hull K = K" "convex hull L = L"
  3592                   using "*" \<open>K \<in> \<U>\<close> \<open>L \<in> \<U>\<close> hull_same by auto
  3593                 have "convex C"
  3594                   by (simp add: \<open>C \<in> \<N>\<close> convex\<N>)
  3595                 have "convex hull (insert ?z K) \<inter> L = L \<inter> convex hull (insert ?z K)"
  3596                   by blast
  3597                 also have "... = convex hull K \<inter> L"
  3598                 proof (subst Int_convex_hull_insert_rel_exterior [OF \<open>convex C\<close> \<open>K \<subseteq> C\<close> z])
  3599                   have "(C \<inter> D) \<inter> rel_interior C = {}"
  3600                   proof (rule face_of_disjoint_rel_interior)
  3601                     show "C \<inter> D face_of C"
  3602                       using \<N>_def \<open>C \<in> \<N>\<close> \<open>D \<in> \<N>\<close> intface\<M> by blast
  3603                     have "D \<in> \<M>" "aff_dim D = int n"
  3604                       using \<N>_def \<open>D \<in> \<N>\<close> by fastforce+
  3605                     moreover have "C \<in> \<M>" "aff_dim C = int n"
  3606                       using \<N>_def \<open>C \<in> \<N>\<close> by fastforce+
  3607                     ultimately have "aff_dim D + - 1 * aff_dim C \<le> 0"
  3608                       by fastforce
  3609                     then have "\<not> C face_of D"
  3610                       using False \<open>convex D\<close> face_of_aff_dim_lt by fastforce
  3611                     show "C \<inter> D \<noteq> C"
  3612                       using \<open>C \<in> \<M>\<close> \<open>D \<in> \<M>\<close> \<open>\<not> C face_of D\<close> intface\<M> by fastforce
  3613                   qed
  3614                   then have "D \<inter> rel_interior C = {}"
  3615                     by (metis inf.absorb_iff2 inf_assoc inf_sup_aci(1) rel_interior_subset)
  3616                   then show "disjnt L (rel_interior C)"
  3617                     by (meson \<open>L \<subseteq> D\<close> disjnt_def disjnt_subset1)
  3618                 next
  3619                   show "L \<inter> convex hull K = convex hull K \<inter> L"
  3620                     by force
  3621                 qed
  3622                 finally have chKL: "convex hull (insert ?z K) \<inter> L = convex hull K \<inter> L" .
  3623                 have "convex hull insert ?z K \<inter> convex hull L face_of K"
  3624                   by (simp add: \<open>K \<in> \<U>\<close> \<open>L \<in> \<U>\<close> ch_id chKL faceI\<U>)
  3625                 also have "... face_of convex hull insert ?z K"
  3626                 proof -
  3627                   obtain I where I: "\<not> affine_dependent I" "K = convex hull I"
  3628                     using * [OF \<open>K \<in> \<U>\<close>] by auto
  3629                   then have "\<And>a. a \<notin> rel_interior C \<or> a \<notin> affine hull I"
  3630                     using ahK_C_disjoint \<open>C \<in> \<N>\<close> \<open>K \<in> \<U>\<close> \<open>K \<subseteq> rel_frontier C\<close> affine_hull_convex_hull by blast
  3631                   then show ?thesis
  3632                     by (metis I affine_independent_insert face_of_convex_hull_affine_independent hull_insert subset_insertI z)
  3633                 qed
  3634                 finally have 1: "convex hull insert ?z K \<inter> convex hull L face_of convex hull insert ?z K" .
  3635                 have "convex hull insert ?z K \<inter> convex hull L face_of L"
  3636                   by (simp add: \<open>K \<in> \<U>\<close> \<open>L \<in> \<U>\<close> ch_id chKL faceI\<U>)
  3637                 also have "... face_of convex hull insert ?w L"
  3638                 proof -
  3639                   obtain I where I: "\<not> affine_dependent I" "L = convex hull I"
  3640                     using * [OF \<open>L \<in> \<U>\<close>] by auto
  3641                   then have "\<And>a. a \<notin> rel_interior D \<or> a \<notin> affine hull I"
  3642                     using \<open>D \<in> \<N>\<close> \<open>L \<in> \<U>\<close> \<open>L \<subseteq> rel_frontier D\<close> affine_hull_convex_hull ahK_C_disjoint by blast
  3643                   then show ?thesis
  3644                     by (metis I aff_independent_finite convex_convex_hull face_of_convex_hull_insert face_of_refl hull_insert w)
  3645                 qed
  3646                 finally have 2: "convex hull insert ?z K \<inter> convex hull L face_of convex hull insert ?w L" .
  3647                 show ?thesis
  3648                   by (simp add: X Y eq 1 2)
  3649               qed
  3650             qed
  3651           qed
  3652         qed
  3653         then
  3654         show "S \<inter> S' face_of S" "S \<inter> S' face_of S'" if "S \<in> \<U> \<union> ?\<T> \<and> S' \<in> \<U> \<union> ?\<T>" for S S'
  3655           using that by auto
  3656       qed
  3657       show "\<exists>F \<subseteq> \<U> \<union> ?\<T>. C = \<Union>F" if "C \<in> \<M>" for C
  3658       proof (cases "C \<in> \<S>")
  3659         case True
  3660         then show ?thesis
  3661           by (meson UnCI fin\<U> subsetD subsetI)
  3662       next
  3663         case False
  3664         then have "C \<in> \<N>"
  3665           by (simp add: \<N>_def \<S>_def aff\<M> less_le that)
  3666         let ?z = "SOME z. z \<in> rel_interior C"
  3667         have z: "?z \<in> rel_interior C"
  3668           using \<open>C \<in> \<N>\<close> in_rel_interior by blast
  3669         let ?F = "\<Union>K \<in> \<U> \<inter> Pow (rel_frontier C). {convex hull (insert ?z K)}"
  3670         have "?F \<subseteq> ?\<T>"
  3671           using \<open>C \<in> \<N>\<close> by blast
  3672         moreover have "C \<subseteq> \<Union>?F"
  3673         proof
  3674           fix x
  3675           assume "x \<in> C"
  3676           have "convex C"
  3677             using \<open>C \<in> \<N>\<close> convex\<N> by blast
  3678           have "bounded C"
  3679             using \<open>C \<in> \<N>\<close> by (simp add: poly\<M> polytope_imp_bounded that)
  3680           have "polytope C"
  3681             using \<open>C \<in> \<N>\<close> poly\<N> by auto
  3682           have "\<not> (?z = x \<and> C = {?z})"
  3683             using \<open>C \<in> \<N>\<close> aff_dim_sing [of ?z] \<open>\<not> n \<le> 1\<close> by (force simp: \<N>_def)
  3684           then obtain y where y: "y \<in> rel_frontier C" and xzy: "x \<in> closed_segment ?z y"
  3685             and sub: "open_segment ?z y \<subseteq> rel_interior C"
  3686             by (blast intro: segment_to_rel_frontier [OF \<open>convex C\<close> \<open>bounded C\<close> z \<open>x \<in> C\<close>])
  3687           then obtain F where "y \<in> F" "F face_of C" "F \<noteq> C"
  3688             by (auto simp: rel_frontier_of_polyhedron_alt [OF polytope_imp_polyhedron [OF \<open>polytope C\<close>]])
  3689           then obtain \<G> where "finite \<G>" "\<G> \<subseteq> \<U>" "F = \<Union>\<G>"
  3690             by (metis (mono_tags, lifting) \<S>_def \<open>C \<in> \<M>\<close> \<open>convex C\<close> aff\<M> face\<M> face_of_aff_dim_lt fin\<U> le_less_trans mem_Collect_eq not_less)
  3691           then obtain K where "y \<in> K" "K \<in> \<G>"
  3692             using \<open>y \<in> F\<close> by blast
  3693           moreover have x: "x \<in> convex hull {?z,y}"
  3694             using segment_convex_hull xzy by auto
  3695           moreover have "convex hull {?z,y} \<subseteq> convex hull insert ?z K"
  3696             by (metis (full_types) \<open>y \<in> K\<close> hull_mono empty_subsetI insertCI insert_subset)
  3697           moreover have "K \<in> \<U>"
  3698             using \<open>K \<in> \<G>\<close> \<open>\<G> \<subseteq> \<U>\<close> by blast
  3699           moreover have "K \<subseteq> rel_frontier C"
  3700             using \<open>F = \<Union>\<G>\<close> \<open>F \<noteq> C\<close> \<open>F face_of C\<close> \<open>K \<in> \<G>\<close> face_of_subset_rel_frontier by fastforce
  3701           ultimately show "x \<in> \<Union>?F"
  3702             by force
  3703         qed
  3704         moreover
  3705         have "convex hull insert (SOME z. z \<in> rel_interior C) K \<subseteq> C"
  3706           if "K \<in> \<U>" "K \<subseteq> rel_frontier C" for K
  3707         proof (rule hull_minimal)
  3708           show "insert (SOME z. z \<in> rel_interior C) K \<subseteq> C"
  3709             using that \<open>C \<in> \<N>\<close> in_rel_interior rel_interior_subset
  3710             by (force simp: closure_eq rel_frontier_def closed\<N>)
  3711           show "convex C"
  3712             by (simp add: \<open>C \<in> \<N>\<close> convex\<N>)
  3713         qed
  3714         then have "\<Union>?F \<subseteq> C"
  3715           by auto
  3716         ultimately show ?thesis
  3717           by blast
  3718       qed
  3719 
  3720       have "(\<exists>C. C \<in> \<M> \<and> L \<subseteq> C) \<and> aff_dim L \<le> int n"  if "L \<in> \<U> \<union> ?\<T>" for L
  3721         using that
  3722       proof
  3723         assume "L \<in> \<U>"
  3724         then show ?thesis
  3725           using C\<U> \<S>_def "*" by fastforce
  3726       next
  3727         assume "L \<in> ?\<T>"
  3728         then obtain C K where "C \<in> \<N>"
  3729           and L: "L = convex hull insert (SOME z. z \<in> rel_interior C) K"
  3730           and K: "K \<in> \<U>" "K \<subseteq> rel_frontier C"
  3731           by auto
  3732         then have "convex hull C = C"
  3733           by (meson convex\<N> convex_hull_eq)
  3734         then have "convex C"
  3735           by (metis (no_types) convex_convex_hull)
  3736         have "rel_frontier C \<subseteq> C"
  3737           by (metis DiffE closed\<N> \<open>C \<in> \<N>\<close> closure_closed rel_frontier_def subsetI)
  3738         have "K \<subseteq> C"
  3739           using K \<open>rel_frontier C \<subseteq> C\<close> by blast
  3740         have "C \<in> \<M>"
  3741           using \<N>_def \<open>C \<in> \<N>\<close> by auto
  3742         moreover have "L \<subseteq> C"
  3743           using K L \<open>C \<in> \<N>\<close>
  3744           by (metis \<open>K \<subseteq> C\<close> \<open>convex hull C = C\<close> contra_subsetD hull_mono in_rel_interior insert_subset rel_interior_subset)
  3745         ultimately show ?thesis
  3746           using \<open>rel_frontier C \<subseteq> C\<close> \<open>L \<subseteq> C\<close> aff\<M> aff_dim_subset \<open>C \<in> \<M>\<close> dual_order.trans by blast
  3747       qed
  3748       then show "\<exists>C. C \<in> \<M> \<and> L \<subseteq> C" "aff_dim L \<le> int n" if "L \<in> \<U> \<union> ?\<T>" for L
  3749         using that by auto
  3750     qed
  3751     then show ?thesis
  3752       apply (rule ex_forward, safe)
  3753         apply (meson Union_iff subsetCE, fastforce)
  3754       by (meson infinite_super simplicial_complex_def)
  3755   qed
  3756 qed
  3757 
  3758 
  3759 lemma simplicial_subdivision_of_cell_complex_lowdim:
  3760   assumes "finite \<M>"
  3761       and poly: "\<And>C. C \<in> \<M> \<Longrightarrow> polytope C"
  3762       and face: "\<And>C1 C2. \<lbrakk>C1 \<in> \<M>; C2 \<in> \<M>\<rbrakk> \<Longrightarrow> C1 \<inter> C2 face_of C1 \<and> C1 \<inter> C2 face_of C2"
  3763       and aff: "\<And>C. C \<in> \<M> \<Longrightarrow> aff_dim C \<le> d"
  3764   obtains \<T> where "simplicial_complex \<T>" "\<And>K. K \<in> \<T> \<Longrightarrow> aff_dim K \<le> d"
  3765                   "\<Union>\<T> = \<Union>\<M>"
  3766                   "\<And>C. C \<in> \<M> \<Longrightarrow> \<exists>F. finite F \<and> F \<subseteq> \<T> \<and> C = \<Union>F"
  3767                   "\<And>K. K \<in> \<T> \<Longrightarrow> \<exists>C. C \<in> \<M> \<and> K \<subseteq> C"
  3768 proof (cases "d \<ge> 0")
  3769   case True
  3770   then obtain n where n: "d = of_nat n"
  3771     using zero_le_imp_eq_int by blast
  3772   have "\<exists>\<T>. simplicial_complex \<T> \<and>
  3773             (\<forall>K\<in>\<T>. aff_dim K \<le> int n) \<and>
  3774             \<Union>\<T> = \<Union>(\<Union>C\<in>\<M>. {F. F face_of C}) \<and>
  3775             (\<forall>C\<in>\<Union>C\<in>\<M>. {F. F face_of C}.
  3776                 \<exists>F. finite F \<and> F \<subseteq> \<T> \<and> C = \<Union>F) \<and>
  3777             (\<forall>K\<in>\<T>. \<exists>C. C \<in> (\<Union>C\<in>\<M>. {F. F face_of C}) \<and> K \<subseteq> C)"
  3778   proof (rule simplicial_subdivision_aux)
  3779     show "finite (\<Union>C\<in>\<M>. {F. F face_of C})"
  3780       using \<open>finite \<M>\<close> poly polyhedron_eq_finite_faces polytope_imp_polyhedron by fastforce
  3781     show "polytope F" if "F \<in> (\<Union>C\<in>\<M>. {F. F face_of C})" for F
  3782       using poly that face_of_polytope_polytope by blast
  3783     show "aff_dim F \<le> int n" if "F \<in> (\<Union>C\<in>\<M>. {F. F face_of C})" for F
  3784       using that
  3785       by clarify (metis n aff_dim_subset aff face_of_imp_subset order_trans)
  3786     show "F \<in> (\<Union>C\<in>\<M>. {F. F face_of C})"
  3787       if "G \<in> (\<Union>C\<in>\<M>. {F. F face_of C})" and "F face_of G" for F G
  3788       using that face_of_trans by blast
  3789   next
  3790     show "F1 \<inter> F2 face_of F1 \<and> F1 \<inter> F2 face_of F2"
  3791       if "F1 \<in> (\<Union>C\<in>\<M>. {F. F face_of C})" and "F2 \<in> (\<Union>C\<in>\<M>. {F. F face_of C})" for F1 F2
  3792       using that
  3793       by safe (meson face face_of_Int_subface)+
  3794   qed
  3795   moreover
  3796   have "\<Union>(\<Union>C\<in>\<M>. {F. F face_of C}) = \<Union>\<M>"
  3797     using face_of_imp_subset face by blast
  3798   ultimately show ?thesis
  3799     apply clarify
  3800     apply (rule that, assumption+)
  3801        using n apply blast
  3802       apply (simp_all add: poly face_of_refl polytope_imp_convex)
  3803     using face_of_imp_subset by fastforce
  3804 next
  3805   case False
  3806   then have m1: "\<And>C. C \<in> \<M> \<Longrightarrow> aff_dim C = -1"
  3807     by (metis aff aff_dim_empty_eq aff_dim_negative_iff dual_order.trans not_less)
  3808   then have face\<M>: "\<And>F S. \<lbrakk>S \<in> \<M>; F face_of S\<rbrakk> \<Longrightarrow> F \<in> \<M>"
  3809     by (metis aff_dim_empty face_of_empty)
  3810   show ?thesis
  3811   proof
  3812     have "\<And>S. S \<in> \<M> \<Longrightarrow> \<exists>n. n simplex S"
  3813       by (metis (no_types) m1 aff_dim_empty simplex_minus_1)
  3814     then show "simplicial_complex \<M>"
  3815       by (auto simp: simplicial_complex_def \<open>finite \<M>\<close> face intro: face\<M>)
  3816     show "aff_dim K \<le> d" if "K \<in> \<M>" for K
  3817       by (simp add: that aff)
  3818     show "\<exists>F. finite F \<and> F \<subseteq> \<M> \<and> C = \<Union>F" if "C \<in> \<M>" for C
  3819       using \<open>C \<in> \<M>\<close> equals0I by auto
  3820     show "\<exists>C. C \<in> \<M> \<and> K \<subseteq> C" if "K \<in> \<M>" for K
  3821       using \<open>K \<in> \<M>\<close> by blast
  3822   qed auto
  3823 qed
  3824 
  3825 proposition simplicial_subdivision_of_cell_complex:
  3826   assumes "finite \<M>"
  3827       and poly: "\<And>C. C \<in> \<M> \<Longrightarrow> polytope C"
  3828       and face: "\<And>C1 C2. \<lbrakk>C1 \<in> \<M>; C2 \<in> \<M>\<rbrakk> \<Longrightarrow> C1 \<inter> C2 face_of C1 \<and> C1 \<inter> C2 face_of C2"
  3829   obtains \<T> where "simplicial_complex \<T>"
  3830                   "\<Union>\<T> = \<Union>\<M>"
  3831                   "\<And>C. C \<in> \<M> \<Longrightarrow> \<exists>F. finite F \<and> F \<subseteq> \<T> \<and> C = \<Union>F"
  3832                   "\<And>K. K \<in> \<T> \<Longrightarrow> \<exists>C. C \<in> \<M> \<and> K \<subseteq> C"
  3833   by (blast intro: simplicial_subdivision_of_cell_complex_lowdim [OF assms aff_dim_le_DIM])
  3834 
  3835 corollary fine_simplicial_subdivision_of_cell_complex:
  3836   assumes "0 < e" "finite \<M>"
  3837       and poly: "\<And>C. C \<in> \<M> \<Longrightarrow> polytope C"
  3838       and face: "\<And>C1 C2. \<lbrakk>C1 \<in> \<M>; C2 \<in> \<M>\<rbrakk> \<Longrightarrow> C1 \<inter> C2 face_of C1 \<and> C1 \<inter> C2 face_of C2"
  3839   obtains \<T> where "simplicial_complex \<T>"
  3840                   "\<And>K. K \<in> \<T> \<Longrightarrow> diameter K < e"
  3841                   "\<Union>\<T> = \<Union>\<M>"
  3842                   "\<And>C. C \<in> \<M> \<Longrightarrow> \<exists>F. finite F \<and> F \<subseteq> \<T> \<and> C = \<Union>F"
  3843                   "\<And>K. K \<in> \<T> \<Longrightarrow> \<exists>C. C \<in> \<M> \<and> K \<subseteq> C"
  3844 proof -
  3845   obtain \<N> where \<N>: "finite \<N>" "\<Union>\<N> = \<Union>\<M>" 
  3846               and diapoly: "\<And>X. X \<in> \<N> \<Longrightarrow> diameter X < e" "\<And>X. X \<in> \<N> \<Longrightarrow> polytope X"
  3847                and      "\<And>X Y. \<lbrakk>X \<in> \<N>; Y \<in> \<N>\<rbrakk> \<Longrightarrow> X \<inter> Y face_of X \<and> X \<inter> Y face_of Y"
  3848                and \<N>covers: "\<And>C x. C \<in> \<M> \<and> x \<in> C \<Longrightarrow> \<exists>D. D \<in> \<N> \<and> x \<in> D \<and> D \<subseteq> C"
  3849                and \<N>covered: "\<And>C. C \<in> \<N> \<Longrightarrow> \<exists>D. D \<in> \<M> \<and> C \<subseteq> D"
  3850     by (blast intro: cell_complex_subdivision_exists [OF \<open>0 < e\<close> \<open>finite \<M>\<close> poly aff_dim_le_DIM face])
  3851   then obtain \<T> where \<T>: "simplicial_complex \<T>" "\<Union>\<T> = \<Union>\<N>"
  3852                    and \<T>covers: "\<And>C. C \<in> \<N> \<Longrightarrow> \<exists>F. finite F \<and> F \<subseteq> \<T> \<and> C = \<Union>F"
  3853                    and \<T>covered: "\<And>K. K \<in> \<T> \<Longrightarrow> \<exists>C. C \<in> \<N> \<and> K \<subseteq> C"
  3854     using simplicial_subdivision_of_cell_complex [OF \<open>finite \<N>\<close>] by metis
  3855   show ?thesis
  3856   proof
  3857     show "simplicial_complex \<T>"
  3858       by (rule \<T>)
  3859     show "diameter K < e" if "K \<in> \<T>" for K
  3860       by (metis le_less_trans diapoly \<T>covered diameter_subset polytope_imp_bounded that)
  3861     show "\<Union>\<T> = \<Union>\<M>"
  3862       by (simp add: \<N>(2) \<open>\<Union>\<T> = \<Union>\<N>\<close>)
  3863     show "\<exists>F. finite F \<and> F \<subseteq> \<T> \<and> C = \<Union>F" if "C \<in> \<M>" for C
  3864     proof -
  3865       { fix x
  3866         assume "x \<in> C"
  3867         then obtain D where "D \<in> \<T>" "x \<in> D" "D \<subseteq> C"
  3868           using \<N>covers \<open>C \<in> \<M>\<close> \<T>covers by force
  3869         then have "\<exists>X\<in>\<T> \<inter> Pow C. x \<in> X"
  3870           using \<open>D \<in> \<T>\<close> \<open>D \<subseteq> C\<close> \<open>x \<in> D\<close> by blast
  3871       }
  3872       moreover
  3873       have "finite (\<T> \<inter> Pow C)"
  3874         using \<open>simplicial_complex \<T>\<close> simplicial_complex_def by auto
  3875       ultimately show ?thesis
  3876         by (rule_tac x="(\<T> \<inter> Pow C)" in exI) auto
  3877     qed
  3878     show "\<exists>C. C \<in> \<M> \<and> K \<subseteq> C" if "K \<in> \<T>" for K
  3879       by (meson \<N>covered \<T>covered order_trans that)
  3880   qed
  3881 qed
  3882 
  3883 subsection\<open>Some results on cell division with full-dimensional cells only\<close>
  3884 
  3885 lemma convex_Union_fulldim_cells:
  3886   assumes "finite \<S>" and clo: "\<And>C. C \<in> \<S> \<Longrightarrow> closed C" and con: "\<And>C. C \<in> \<S> \<Longrightarrow> convex C"
  3887       and eq: "\<Union>\<S> = U"and  "convex U"
  3888  shows "\<Union>{C \<in> \<S>. aff_dim C = aff_dim U} = U"  (is "?lhs = U")
  3889 proof -
  3890   have "closed U"
  3891     using \<open>finite \<S>\<close> clo eq by blast
  3892   have "?lhs \<subseteq> U"
  3893     using eq by blast
  3894   moreover have "U \<subseteq> ?lhs"
  3895   proof (cases "\<forall>C \<in> \<S>. aff_dim C = aff_dim U")
  3896     case True
  3897     then show ?thesis
  3898       using eq by blast
  3899   next
  3900     case False
  3901     have "closed ?lhs"
  3902       by (simp add: \<open>finite \<S>\<close> clo closed_Union)
  3903     moreover have "U \<subseteq> closure ?lhs"
  3904     proof -
  3905       have "U \<subseteq> closure(\<Inter>{U - C |C. C \<in> \<S> \<and> aff_dim C < aff_dim U})"
  3906       proof (rule Baire [OF \<open>closed U\<close>])
  3907         show "countable {U - C |C. C \<in> \<S> \<and> aff_dim C < aff_dim U}"
  3908           using \<open>finite \<S>\<close> uncountable_infinite by fastforce
  3909         have "\<And>C. C \<in> \<S> \<Longrightarrow> openin (subtopology euclidean U) (U-C)"
  3910           by (metis Sup_upper clo closed_limpt closedin_limpt eq openin_diff openin_subtopology_self)
  3911         then show "openin (subtopology euclidean U) T \<and> U \<subseteq> closure T"
  3912           if "T \<in> {U - C |C. C \<in> \<S> \<and> aff_dim C < aff_dim U}" for T
  3913           using that dense_complement_convex_closed \<open>closed U\<close> \<open>convex U\<close> by auto
  3914       qed
  3915       also have "... \<subseteq> closure ?lhs"
  3916       proof -
  3917         obtain C where "C \<in> \<S>" "aff_dim C < aff_dim U"
  3918           by (metis False Sup_upper aff_dim_subset eq eq_iff not_le)
  3919         have "\<exists>X. X \<in> \<S> \<and> aff_dim X = aff_dim U \<and> x \<in> X"
  3920           if "\<And>V. (\<exists>C. V = U - C \<and> C \<in> \<S> \<and> aff_dim C < aff_dim U) \<Longrightarrow> x \<in> V" for x
  3921         proof -
  3922           have "x \<in> U \<and> x \<in> \<Union>\<S>"
  3923             using \<open>C \<in> \<S>\<close> \<open>aff_dim C < aff_dim U\<close> eq that by blast
  3924           then show ?thesis
  3925             by (metis Diff_iff Sup_upper Union_iff aff_dim_subset dual_order.order_iff_strict eq that)
  3926         qed
  3927         then show ?thesis
  3928           by (auto intro!: closure_mono)
  3929       qed
  3930       finally show ?thesis .
  3931     qed
  3932     ultimately show ?thesis
  3933       using closure_subset_eq by blast
  3934   qed
  3935   ultimately show ?thesis by blast
  3936 qed
  3937 
  3938 proposition fine_triangular_subdivision_of_cell_complex:
  3939   assumes "0 < e" "finite \<M>"
  3940       and poly: "\<And>C. C \<in> \<M> \<Longrightarrow> polytope C"
  3941       and aff: "\<And>C. C \<in> \<M> \<Longrightarrow> aff_dim C = d"
  3942       and face: "\<And>C1 C2. \<lbrakk>C1 \<in> \<M>; C2 \<in> \<M>\<rbrakk> \<Longrightarrow> C1 \<inter> C2 face_of C1 \<and> C1 \<inter> C2 face_of C2"
  3943   obtains \<T> where "triangulation \<T>" "\<And>k. k \<in> \<T> \<Longrightarrow> diameter k < e"
  3944                  "\<And>k. k \<in> \<T> \<Longrightarrow> aff_dim k = d" "\<Union>\<T> = \<Union>\<M>"
  3945                  "\<And>C. C \<in> \<M> \<Longrightarrow> \<exists>f. finite f \<and> f \<subseteq> \<T> \<and> C = \<Union>f"
  3946                  "\<And>k. k \<in> \<T> \<Longrightarrow> \<exists>C. C \<in> \<M> \<and> k \<subseteq> C"
  3947 proof -
  3948   obtain \<T> where "simplicial_complex \<T>"
  3949              and dia\<T>: "\<And>K. K \<in> \<T> \<Longrightarrow> diameter K < e"
  3950              and "\<Union>\<T> = \<Union>\<M>"
  3951              and in\<M>: "\<And>C. C \<in> \<M> \<Longrightarrow> \<exists>F. finite F \<and> F \<subseteq> \<T> \<and> C = \<Union>F"
  3952              and in\<T>: "\<And>K. K \<in> \<T> \<Longrightarrow> \<exists>C. C \<in> \<M> \<and> K \<subseteq> C"
  3953     by (blast intro: fine_simplicial_subdivision_of_cell_complex [OF \<open>e > 0\<close> \<open>finite \<M>\<close> poly face])
  3954   let ?\<T> = "{K \<in> \<T>. aff_dim K = d}"
  3955   show thesis
  3956   proof
  3957     show "triangulation ?\<T>"
  3958       using \<open>simplicial_complex \<T>\<close> by (auto simp: triangulation_def simplicial_complex_def)
  3959     show "diameter L < e" if "L \<in> {K \<in> \<T>. aff_dim K = d}" for L
  3960       using that by (auto simp: dia\<T>)
  3961     show "aff_dim L = d" if "L \<in> {K \<in> \<T>. aff_dim K = d}" for L
  3962       using that by auto
  3963     show "\<exists>F. finite F \<and> F \<subseteq> {K \<in> \<T>. aff_dim K = d} \<and> C = \<Union>F" if "C \<in> \<M>" for C
  3964     proof -
  3965       obtain F where "finite F" "F \<subseteq> \<T>" "C = \<Union>F"
  3966         using in\<M> [OF \<open>C \<in> \<M>\<close>] by auto
  3967       show ?thesis
  3968       proof (intro exI conjI)
  3969         show "finite {K \<in> F. aff_dim K = d}"
  3970           by (simp add: \<open>finite F\<close>)
  3971         show "{K \<in> F. aff_dim K = d} \<subseteq> {K \<in> \<T>. aff_dim K = d}"
  3972           using \<open>F \<subseteq> \<T>\<close> by blast
  3973         have "d = aff_dim C"
  3974           by (simp add: aff that)
  3975         moreover have "\<And>K. K \<in> F \<Longrightarrow> closed K \<and> convex K"
  3976           using \<open>simplicial_complex \<T>\<close> \<open>F \<subseteq> \<T>\<close>
  3977           unfolding simplicial_complex_def by (metis subsetCE \<open>F \<subseteq> \<T>\<close> closed_simplex convex_simplex)
  3978         moreover have "convex (\<Union>F)"
  3979           using \<open>C = \<Union>F\<close> poly polytope_imp_convex that by blast
  3980         ultimately show "C = \<Union>{K \<in> F. aff_dim K = d}"
  3981           by (simp add: convex_Union_fulldim_cells \<open>C = \<Union>F\<close> \<open>finite F\<close>)
  3982       qed
  3983     qed
  3984     then show "\<Union>{K \<in> \<T>. aff_dim K = d} = \<Union>\<M>"
  3985       by auto (meson in\<T> subsetCE)
  3986     show "\<exists>C. C \<in> \<M> \<and> L \<subseteq> C"
  3987       if "L \<in> {K \<in> \<T>. aff_dim K = d}" for L
  3988       using that by (auto simp: in\<T>)
  3989   qed
  3990 qed
  3991 
  3992 end