tuned proofs;
authorwenzelm
Thu Sep 27 14:52:50 2012 +0200 (2012-09-27)
changeset 4960989e10ed7668b
parent 49608 ce1c34c98eeb
child 49610 1b36c6676685
tuned proofs;
src/HOL/Library/Convex.thy
     1.1 --- a/src/HOL/Library/Convex.thy	Thu Sep 27 14:50:06 2012 +0200
     1.2 +++ b/src/HOL/Library/Convex.thy	Thu Sep 27 14:52:50 2012 +0200
     1.3 @@ -11,9 +11,8 @@
     1.4  
     1.5  subsection {* Convexity. *}
     1.6  
     1.7 -definition
     1.8 -  convex :: "'a::real_vector set \<Rightarrow> bool" where
     1.9 -  "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s)"
    1.10 +definition convex :: "'a::real_vector set \<Rightarrow> bool"
    1.11 +  where "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s)"
    1.12  
    1.13  lemma convex_alt:
    1.14    "convex s \<longleftrightarrow> (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u. 0 \<le> u \<and> u \<le> 1 \<longrightarrow> ((1 - u) *\<^sub>R x + u *\<^sub>R y) \<in> s)"
    1.15 @@ -21,10 +20,10 @@
    1.16  proof
    1.17    assume alt[rule_format]: ?alt
    1.18    { fix x y and u v :: real assume mem: "x \<in> s" "y \<in> s"
    1.19 -    assume "0 \<le> u" "0 \<le> v" "u + v = 1"
    1.20 -    moreover hence "u = 1 - v" by auto
    1.21 +    assume "0 \<le> u" "0 \<le> v"
    1.22 +    moreover assume "u + v = 1" then have "u = 1 - v" by auto
    1.23      ultimately have "u *\<^sub>R x + v *\<^sub>R y \<in> s" using alt[OF mem] by auto }
    1.24 -  thus "convex s" unfolding convex_def by auto
    1.25 +  then show "convex s" unfolding convex_def by auto
    1.26  qed (auto simp: convex_def)
    1.27  
    1.28  lemma mem_convex:
    1.29 @@ -53,13 +52,13 @@
    1.30  
    1.31  lemma convex_halfspace_ge: "convex {x. inner a x \<ge> b}"
    1.32  proof -
    1.33 -  have *:"{x. inner a x \<ge> b} = {x. inner (-a) x \<le> -b}" by auto
    1.34 +  have *: "{x. inner a x \<ge> b} = {x. inner (-a) x \<le> -b}" by auto
    1.35    show ?thesis unfolding * using convex_halfspace_le[of "-a" "-b"] by auto
    1.36  qed
    1.37  
    1.38  lemma convex_hyperplane: "convex {x. inner a x = b}"
    1.39 -proof-
    1.40 -  have *:"{x. inner a x = b} = {x. inner a x \<le> b} \<inter> {x. inner a x \<ge> b}" by auto
    1.41 +proof -
    1.42 +  have *: "{x. inner a x = b} = {x. inner a x \<le> b} \<inter> {x. inner a x \<ge> b}" by auto
    1.43    show ?thesis using convex_halfspace_le convex_halfspace_ge
    1.44      by (auto intro!: convex_Int simp: *)
    1.45  qed
    1.46 @@ -74,78 +73,83 @@
    1.47  lemma convex_real_interval:
    1.48    fixes a b :: "real"
    1.49    shows "convex {a..}" and "convex {..b}"
    1.50 -  and "convex {a<..}" and "convex {..<b}"
    1.51 -  and "convex {a..b}" and "convex {a<..b}"
    1.52 -  and "convex {a..<b}" and "convex {a<..<b}"
    1.53 +    and "convex {a<..}" and "convex {..<b}"
    1.54 +    and "convex {a..b}" and "convex {a<..b}"
    1.55 +    and "convex {a..<b}" and "convex {a<..<b}"
    1.56  proof -
    1.57    have "{a..} = {x. a \<le> inner 1 x}" by auto
    1.58 -  thus 1: "convex {a..}" by (simp only: convex_halfspace_ge)
    1.59 +  then show 1: "convex {a..}" by (simp only: convex_halfspace_ge)
    1.60    have "{..b} = {x. inner 1 x \<le> b}" by auto
    1.61 -  thus 2: "convex {..b}" by (simp only: convex_halfspace_le)
    1.62 +  then show 2: "convex {..b}" by (simp only: convex_halfspace_le)
    1.63    have "{a<..} = {x. a < inner 1 x}" by auto
    1.64 -  thus 3: "convex {a<..}" by (simp only: convex_halfspace_gt)
    1.65 +  then show 3: "convex {a<..}" by (simp only: convex_halfspace_gt)
    1.66    have "{..<b} = {x. inner 1 x < b}" by auto
    1.67 -  thus 4: "convex {..<b}" by (simp only: convex_halfspace_lt)
    1.68 +  then show 4: "convex {..<b}" by (simp only: convex_halfspace_lt)
    1.69    have "{a..b} = {a..} \<inter> {..b}" by auto
    1.70 -  thus "convex {a..b}" by (simp only: convex_Int 1 2)
    1.71 +  then show "convex {a..b}" by (simp only: convex_Int 1 2)
    1.72    have "{a<..b} = {a<..} \<inter> {..b}" by auto
    1.73 -  thus "convex {a<..b}" by (simp only: convex_Int 3 2)
    1.74 +  then show "convex {a<..b}" by (simp only: convex_Int 3 2)
    1.75    have "{a..<b} = {a..} \<inter> {..<b}" by auto
    1.76 -  thus "convex {a..<b}" by (simp only: convex_Int 1 4)
    1.77 +  then show "convex {a..<b}" by (simp only: convex_Int 1 4)
    1.78    have "{a<..<b} = {a<..} \<inter> {..<b}" by auto
    1.79 -  thus "convex {a<..<b}" by (simp only: convex_Int 3 4)
    1.80 +  then show "convex {a<..<b}" by (simp only: convex_Int 3 4)
    1.81  qed
    1.82  
    1.83 +
    1.84  subsection {* Explicit expressions for convexity in terms of arbitrary sums. *}
    1.85  
    1.86  lemma convex_setsum:
    1.87    fixes C :: "'a::real_vector set"
    1.88    assumes "finite s" and "convex C" and "(\<Sum> i \<in> s. a i) = 1"
    1.89 -  assumes "\<And> i. i \<in> s \<Longrightarrow> a i \<ge> 0" and "\<And> i. i \<in> s \<Longrightarrow> y i \<in> C"
    1.90 +  assumes "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0" and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
    1.91    shows "(\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C"
    1.92 -using assms
    1.93 -proof (induct s arbitrary:a rule:finite_induct)
    1.94 -  case empty thus ?case by auto
    1.95 +  using assms
    1.96 +proof (induct s arbitrary:a rule: finite_induct)
    1.97 +  case empty
    1.98 +  then show ?case by auto
    1.99  next
   1.100    case (insert i s) note asms = this
   1.101    { assume "a i = 1"
   1.102 -    hence "(\<Sum> j \<in> s. a j) = 0"
   1.103 +    then have "(\<Sum> j \<in> s. a j) = 0"
   1.104        using asms by auto
   1.105 -    hence "\<And> j. j \<in> s \<Longrightarrow> a j = 0"
   1.106 +    then have "\<And>j. j \<in> s \<Longrightarrow> a j = 0"
   1.107        using setsum_nonneg_0[where 'b=real] asms by fastforce
   1.108 -    hence ?case using asms by auto }
   1.109 +    then have ?case using asms by auto }
   1.110    moreover
   1.111    { assume asm: "a i \<noteq> 1"
   1.112      from asms have yai: "y i \<in> C" "a i \<ge> 0" by auto
   1.113      have fis: "finite (insert i s)" using asms by auto
   1.114 -    hence ai1: "a i \<le> 1" using setsum_nonneg_leq_bound[of "insert i s" a 1] asms by simp
   1.115 -    hence "a i < 1" using asm by auto
   1.116 -    hence i0: "1 - a i > 0" by auto
   1.117 -    let "?a j" = "a j / (1 - a i)"
   1.118 +    then have ai1: "a i \<le> 1" using setsum_nonneg_leq_bound[of "insert i s" a 1] asms by simp
   1.119 +    then have "a i < 1" using asm by auto
   1.120 +    then have i0: "1 - a i > 0" by auto
   1.121 +    let ?a = "\<lambda>j. a j / (1 - a i)"
   1.122      { fix j assume "j \<in> s"
   1.123 -      hence "?a j \<ge> 0"
   1.124 +      then have "?a j \<ge> 0"
   1.125          using i0 asms divide_nonneg_pos
   1.126 -        by fastforce } note a_nonneg = this
   1.127 +        by fastforce
   1.128 +    } note a_nonneg = this
   1.129      have "(\<Sum> j \<in> insert i s. a j) = 1" using asms by auto
   1.130 -    hence "(\<Sum> j \<in> s. a j) = 1 - a i" using setsum.insert asms by fastforce
   1.131 -    hence "(\<Sum> j \<in> s. a j) / (1 - a i) = 1" using i0 by auto
   1.132 -    hence a1: "(\<Sum> j \<in> s. ?a j) = 1" unfolding setsum_divide_distrib by simp
   1.133 -    from this asms
   1.134 -    have "(\<Sum>j\<in>s. ?a j *\<^sub>R y j) \<in> C" using a_nonneg by fastforce
   1.135 -    hence "a i *\<^sub>R y i + (1 - a i) *\<^sub>R (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
   1.136 +    then have "(\<Sum> j \<in> s. a j) = 1 - a i" using setsum.insert asms by fastforce
   1.137 +    then have "(\<Sum> j \<in> s. a j) / (1 - a i) = 1" using i0 by auto
   1.138 +    then have a1: "(\<Sum> j \<in> s. ?a j) = 1" unfolding setsum_divide_distrib by simp
   1.139 +    with asms have "(\<Sum>j\<in>s. ?a j *\<^sub>R y j) \<in> C" using a_nonneg by fastforce
   1.140 +    then have "a i *\<^sub>R y i + (1 - a i) *\<^sub>R (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
   1.141        using asms[unfolded convex_def, rule_format] yai ai1 by auto
   1.142 -    hence "a i *\<^sub>R y i + (\<Sum> j \<in> s. (1 - a i) *\<^sub>R (?a j *\<^sub>R y j)) \<in> C"
   1.143 +    then have "a i *\<^sub>R y i + (\<Sum> j \<in> s. (1 - a i) *\<^sub>R (?a j *\<^sub>R y j)) \<in> C"
   1.144        using scaleR_right.setsum[of "(1 - a i)" "\<lambda> j. ?a j *\<^sub>R y j" s] by auto
   1.145 -    hence "a i *\<^sub>R y i + (\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C" using i0 by auto
   1.146 -    hence ?case using setsum.insert asms by auto }
   1.147 +    then have "a i *\<^sub>R y i + (\<Sum> j \<in> s. a j *\<^sub>R y j) \<in> C" using i0 by auto
   1.148 +    then have ?case using setsum.insert asms by auto
   1.149 +  }
   1.150    ultimately show ?case by auto
   1.151  qed
   1.152  
   1.153  lemma convex:
   1.154 -  shows "convex s \<longleftrightarrow> (\<forall>(k::nat) u x. (\<forall>i. 1\<le>i \<and> i\<le>k \<longrightarrow> 0 \<le> u i \<and> x i \<in>s) \<and> (setsum u {1..k} = 1)
   1.155 -           \<longrightarrow> setsum (\<lambda>i. u i *\<^sub>R x i) {1..k} \<in> s)"
   1.156 +  "convex s \<longleftrightarrow> (\<forall>(k::nat) u x. (\<forall>i. 1\<le>i \<and> i\<le>k \<longrightarrow> 0 \<le> u i \<and> x i \<in>s) \<and> (setsum u {1..k} = 1)
   1.157 +      \<longrightarrow> setsum (\<lambda>i. u i *\<^sub>R x i) {1..k} \<in> s)"
   1.158  proof safe
   1.159 -  fix k :: nat fix u :: "nat \<Rightarrow> real" fix x
   1.160 +  fix k :: nat
   1.161 +  fix u :: "nat \<Rightarrow> real"
   1.162 +  fix x
   1.163    assume "convex s"
   1.164      "\<forall>i. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s"
   1.165      "setsum u {1..k} = 1"
   1.166 @@ -154,35 +158,39 @@
   1.167  next
   1.168    assume asm: "\<forall>k u x. (\<forall> i :: nat. 1 \<le> i \<and> i \<le> k \<longrightarrow> 0 \<le> u i \<and> x i \<in> s) \<and> setsum u {1..k} = 1
   1.169      \<longrightarrow> (\<Sum>i = 1..k. u i *\<^sub>R (x i :: 'a)) \<in> s"
   1.170 -  { fix \<mu> :: real fix x y :: 'a assume xy: "x \<in> s" "y \<in> s" assume mu: "\<mu> \<ge> 0" "\<mu> \<le> 1"
   1.171 -    let "?u i" = "if (i :: nat) = 1 then \<mu> else 1 - \<mu>"
   1.172 -    let "?x i" = "if (i :: nat) = 1 then x else y"
   1.173 +  { fix \<mu> :: real
   1.174 +    fix x y :: 'a
   1.175 +    assume xy: "x \<in> s" "y \<in> s"
   1.176 +    assume mu: "\<mu> \<ge> 0" "\<mu> \<le> 1"
   1.177 +    let ?u = "\<lambda>i. if (i :: nat) = 1 then \<mu> else 1 - \<mu>"
   1.178 +    let ?x = "\<lambda>i. if (i :: nat) = 1 then x else y"
   1.179      have "{1 :: nat .. 2} \<inter> - {x. x = 1} = {2}" by auto
   1.180 -    hence card: "card ({1 :: nat .. 2} \<inter> - {x. x = 1}) = 1" by simp
   1.181 -    hence "setsum ?u {1 .. 2} = 1"
   1.182 +    then have card: "card ({1 :: nat .. 2} \<inter> - {x. x = 1}) = 1" by simp
   1.183 +    then have "setsum ?u {1 .. 2} = 1"
   1.184        using setsum_cases[of "{(1 :: nat) .. 2}" "\<lambda> x. x = 1" "\<lambda> x. \<mu>" "\<lambda> x. 1 - \<mu>"]
   1.185        by auto
   1.186 -    from this asm[rule_format, of "2" ?u ?x]
   1.187 -    have s: "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) \<in> s"
   1.188 +    with asm[rule_format, of "2" ?u ?x] have s: "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) \<in> s"
   1.189        using mu xy by auto
   1.190      have grarr: "(\<Sum>j \<in> {Suc (Suc 0)..2}. ?u j *\<^sub>R ?x j) = (1 - \<mu>) *\<^sub>R y"
   1.191        using setsum_head_Suc[of "Suc (Suc 0)" 2 "\<lambda> j. (1 - \<mu>) *\<^sub>R y"] by auto
   1.192      from setsum_head_Suc[of "Suc 0" 2 "\<lambda> j. ?u j *\<^sub>R ?x j", simplified this]
   1.193      have "(\<Sum>j \<in> {1..2}. ?u j *\<^sub>R ?x j) = \<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y" by auto
   1.194 -    hence "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x \<in> s" using s by (auto simp:add_commute) }
   1.195 -  thus "convex s" unfolding convex_alt by auto
   1.196 +    then have "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x \<in> s" using s by (auto simp:add_commute)
   1.197 +  }
   1.198 +  then show "convex s" unfolding convex_alt by auto
   1.199  qed
   1.200  
   1.201  
   1.202  lemma convex_explicit:
   1.203    fixes s :: "'a::real_vector set"
   1.204    shows "convex s \<longleftrightarrow>
   1.205 -  (\<forall>t u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and> setsum u t = 1 \<longrightarrow> setsum (\<lambda>x. u x *\<^sub>R x) t \<in> s)"
   1.206 +    (\<forall>t u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x) \<and> setsum u t = 1 \<longrightarrow> setsum (\<lambda>x. u x *\<^sub>R x) t \<in> s)"
   1.207  proof safe
   1.208 -  fix t fix u :: "'a \<Rightarrow> real"
   1.209 +  fix t
   1.210 +  fix u :: "'a \<Rightarrow> real"
   1.211    assume "convex s" "finite t"
   1.212      "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = 1"
   1.213 -  thus "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   1.214 +  then show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   1.215      using convex_setsum[of t s u "\<lambda> x. x"] by auto
   1.216  next
   1.217    assume asm0: "\<forall>t. \<forall> u. finite t \<and> t \<subseteq> s \<and> (\<forall>x\<in>t. 0 \<le> u x)
   1.218 @@ -190,39 +198,42 @@
   1.219    show "convex s"
   1.220      unfolding convex_alt
   1.221    proof safe
   1.222 -    fix x y fix \<mu> :: real
   1.223 +    fix x y
   1.224 +    fix \<mu> :: real
   1.225      assume asm: "x \<in> s" "y \<in> s" "0 \<le> \<mu>" "\<mu> \<le> 1"
   1.226      { assume "x \<noteq> y"
   1.227 -      hence "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
   1.228 +      then have "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
   1.229          using asm0[rule_format, of "{x, y}" "\<lambda> z. if z = x then 1 - \<mu> else \<mu>"]
   1.230            asm by auto }
   1.231      moreover
   1.232      { assume "x = y"
   1.233 -      hence "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
   1.234 +      then have "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s"
   1.235          using asm0[rule_format, of "{x, y}" "\<lambda> z. 1"]
   1.236            asm by (auto simp:field_simps real_vector.scale_left_diff_distrib) }
   1.237      ultimately show "(1 - \<mu>) *\<^sub>R x + \<mu> *\<^sub>R y \<in> s" by blast
   1.238    qed
   1.239  qed
   1.240  
   1.241 -lemma convex_finite: assumes "finite s"
   1.242 +lemma convex_finite:
   1.243 +  assumes "finite s"
   1.244    shows "convex s \<longleftrightarrow> (\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> setsum u s = 1
   1.245                        \<longrightarrow> setsum (\<lambda>x. u x *\<^sub>R x) s \<in> s)"
   1.246    unfolding convex_explicit
   1.247 -proof (safe)
   1.248 -  fix t u assume sum: "\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> setsum u s = 1 \<longrightarrow> (\<Sum>x\<in>s. u x *\<^sub>R x) \<in> s"
   1.249 +proof safe
   1.250 +  fix t u
   1.251 +  assume sum: "\<forall>u. (\<forall>x\<in>s. 0 \<le> u x) \<and> setsum u s = 1 \<longrightarrow> (\<Sum>x\<in>s. u x *\<^sub>R x) \<in> s"
   1.252      and as: "finite t" "t \<subseteq> s" "\<forall>x\<in>t. 0 \<le> u x" "setsum u t = (1::real)"
   1.253 -  have *:"s \<inter> t = t" using as(2) by auto
   1.254 -  have if_distrib_arg: "\<And>P f g x. (if P then f else g) x = (if P then f x else g x)" by simp
   1.255 +  have *: "s \<inter> t = t" using as(2) by auto
   1.256 +  have if_distrib_arg: "\<And>P f g x. (if P then f else g) x = (if P then f x else g x)"
   1.257 +    by simp
   1.258    show "(\<Sum>x\<in>t. u x *\<^sub>R x) \<in> s"
   1.259     using sum[THEN spec[where x="\<lambda>x. if x\<in>t then u x else 0"]] as *
   1.260     by (auto simp: assms setsum_cases if_distrib if_distrib_arg)
   1.261  qed (erule_tac x=s in allE, erule_tac x=u in allE, auto)
   1.262  
   1.263 -definition
   1.264 -  convex_on :: "'a::real_vector set \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool" where
   1.265 -  "convex_on s f \<longleftrightarrow>
   1.266 -  (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y)"
   1.267 +definition convex_on :: "'a::real_vector set \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool"
   1.268 +  where "convex_on s f \<longleftrightarrow>
   1.269 +    (\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y)"
   1.270  
   1.271  lemma convex_on_subset: "convex_on t f \<Longrightarrow> s \<subseteq> t \<Longrightarrow> convex_on s f"
   1.272    unfolding convex_on_def by auto
   1.273 @@ -230,21 +241,29 @@
   1.274  lemma convex_add[intro]:
   1.275    assumes "convex_on s f" "convex_on s g"
   1.276    shows "convex_on s (\<lambda>x. f x + g x)"
   1.277 -proof-
   1.278 -  { fix x y assume "x\<in>s" "y\<in>s" moreover
   1.279 -    fix u v ::real assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   1.280 -    ultimately have "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> (u * f x + v * f y) + (u * g x + v * g y)"
   1.281 -      using assms unfolding convex_on_def by (auto simp add:add_mono)
   1.282 -    hence "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> u * (f x + g x) + v * (f y + g y)" by (simp add: field_simps)  }
   1.283 -  thus ?thesis unfolding convex_on_def by auto
   1.284 +proof -
   1.285 +  { fix x y
   1.286 +    assume "x\<in>s" "y\<in>s"
   1.287 +    moreover
   1.288 +    fix u v :: real
   1.289 +    assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   1.290 +    ultimately
   1.291 +    have "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> (u * f x + v * f y) + (u * g x + v * g y)"
   1.292 +      using assms unfolding convex_on_def by (auto simp add: add_mono)
   1.293 +    then have "f (u *\<^sub>R x + v *\<^sub>R y) + g (u *\<^sub>R x + v *\<^sub>R y) \<le> u * (f x + g x) + v * (f y + g y)"
   1.294 +      by (simp add: field_simps)
   1.295 +  }
   1.296 +  then show ?thesis unfolding convex_on_def by auto
   1.297  qed
   1.298  
   1.299  lemma convex_cmul[intro]:
   1.300    assumes "0 \<le> (c::real)" "convex_on s f"
   1.301    shows "convex_on s (\<lambda>x. c * f x)"
   1.302  proof-
   1.303 -  have *:"\<And>u c fx v fy ::real. u * (c * fx) + v * (c * fy) = c * (u * fx + v * fy)" by (simp add: field_simps)
   1.304 -  show ?thesis using assms(2) and mult_left_mono [OF _ assms(1)] unfolding convex_on_def and * by auto
   1.305 +  have *: "\<And>u c fx v fy ::real. u * (c * fx) + v * (c * fy) = c * (u * fx + v * fy)"
   1.306 +    by (simp add: field_simps)
   1.307 +  show ?thesis using assms(2) and mult_left_mono [OF _ assms(1)]
   1.308 +    unfolding convex_on_def and * by auto
   1.309  qed
   1.310  
   1.311  lemma convex_lower:
   1.312 @@ -254,7 +273,7 @@
   1.313    let ?m = "max (f x) (f y)"
   1.314    have "u * f x + v * f y \<le> u * max (f x) (f y) + v * max (f x) (f y)"
   1.315      using assms(4,5) by (auto simp add: mult_left_mono add_mono)
   1.316 -  also have "\<dots> = max (f x) (f y)" using assms(6) unfolding distrib[THEN sym] by auto
   1.317 +  also have "\<dots> = max (f x) (f y)" using assms(6) unfolding distrib[symmetric] by auto
   1.318    finally show ?thesis
   1.319      using assms unfolding convex_on_def by fastforce
   1.320  qed
   1.321 @@ -262,24 +281,30 @@
   1.322  lemma convex_distance[intro]:
   1.323    fixes s :: "'a::real_normed_vector set"
   1.324    shows "convex_on s (\<lambda>x. dist a x)"
   1.325 -proof(auto simp add: convex_on_def dist_norm)
   1.326 -  fix x y assume "x\<in>s" "y\<in>s"
   1.327 -  fix u v ::real assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   1.328 -  have "a = u *\<^sub>R a + v *\<^sub>R a" unfolding scaleR_left_distrib[THEN sym] and `u+v=1` by simp
   1.329 -  hence *:"a - (u *\<^sub>R x + v *\<^sub>R y) = (u *\<^sub>R (a - x)) + (v *\<^sub>R (a - y))"
   1.330 +proof (auto simp add: convex_on_def dist_norm)
   1.331 +  fix x y
   1.332 +  assume "x\<in>s" "y\<in>s"
   1.333 +  fix u v :: real
   1.334 +  assume "0 \<le> u" "0 \<le> v" "u + v = 1"
   1.335 +  have "a = u *\<^sub>R a + v *\<^sub>R a"
   1.336 +    unfolding scaleR_left_distrib[symmetric] and `u+v=1` by simp
   1.337 +  then have *: "a - (u *\<^sub>R x + v *\<^sub>R y) = (u *\<^sub>R (a - x)) + (v *\<^sub>R (a - y))"
   1.338      by (auto simp add: algebra_simps)
   1.339    show "norm (a - (u *\<^sub>R x + v *\<^sub>R y)) \<le> u * norm (a - x) + v * norm (a - y)"
   1.340      unfolding * using norm_triangle_ineq[of "u *\<^sub>R (a - x)" "v *\<^sub>R (a - y)"]
   1.341      using `0 \<le> u` `0 \<le> v` by auto
   1.342  qed
   1.343  
   1.344 +
   1.345  subsection {* Arithmetic operations on sets preserve convexity. *}
   1.346 +
   1.347  lemma convex_scaling:
   1.348    assumes "convex s"
   1.349    shows"convex ((\<lambda>x. c *\<^sub>R x) ` s)"
   1.350 -using assms unfolding convex_def image_iff
   1.351 +  using assms unfolding convex_def image_iff
   1.352  proof safe
   1.353 -  fix x xa y xb :: "'a::real_vector" fix u v :: real
   1.354 +  fix x xa y xb :: "'a::real_vector"
   1.355 +  fix u v :: real
   1.356    assume asm: "\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s"
   1.357      "xa \<in> s" "xb \<in> s" "0 \<le> u" "0 \<le> v" "u + v = 1"
   1.358    show "\<exists>x\<in>s. u *\<^sub>R c *\<^sub>R xa + v *\<^sub>R c *\<^sub>R xb = c *\<^sub>R x"
   1.359 @@ -287,9 +312,10 @@
   1.360  qed
   1.361  
   1.362  lemma convex_negations: "convex s \<Longrightarrow> convex ((\<lambda>x. -x)` s)"
   1.363 -using assms unfolding convex_def image_iff
   1.364 +  using assms unfolding convex_def image_iff
   1.365  proof safe
   1.366 -  fix x xa y xb :: "'a::real_vector" fix u v :: real
   1.367 +  fix x xa y xb :: "'a::real_vector"
   1.368 +  fix u v :: real
   1.369    assume asm: "\<forall>x\<in>s. \<forall>y\<in>s. \<forall>u\<ge>0. \<forall>v\<ge>0. u + v = 1 \<longrightarrow> u *\<^sub>R x + v *\<^sub>R y \<in> s"
   1.370      "xa \<in> s" "xb \<in> s" "0 \<le> u" "0 \<le> v" "u + v = 1"
   1.371    show "\<exists>x\<in>s. u *\<^sub>R - xa + v *\<^sub>R - xb = - x"
   1.372 @@ -299,10 +325,12 @@
   1.373  lemma convex_sums:
   1.374    assumes "convex s" "convex t"
   1.375    shows "convex {x + y| x y. x \<in> s \<and> y \<in> t}"
   1.376 -using assms unfolding convex_def image_iff
   1.377 +  using assms unfolding convex_def image_iff
   1.378  proof safe
   1.379 -  fix xa xb ya yb assume xy:"xa\<in>s" "xb\<in>s" "ya\<in>t" "yb\<in>t"
   1.380 -  fix u v ::real assume uv:"0 \<le> u" "0 \<le> v" "u + v = 1"
   1.381 +  fix xa xb ya yb
   1.382 +  assume xy:"xa\<in>s" "xb\<in>s" "ya\<in>t" "yb\<in>t"
   1.383 +  fix u v :: real
   1.384 +  assume uv: "0 \<le> u" "0 \<le> v" "u + v = 1"
   1.385    show "\<exists>x y. u *\<^sub>R (xa + ya) + v *\<^sub>R (xb + yb) = x + y \<and> x \<in> s \<and> y \<in> t"
   1.386      using exI[of _ "u *\<^sub>R xa + v *\<^sub>R xb"] exI[of _ "u *\<^sub>R ya + v *\<^sub>R yb"]
   1.387        assms[unfolded convex_def] uv xy by (auto simp add:scaleR_right_distrib)
   1.388 @@ -314,105 +342,120 @@
   1.389  proof -
   1.390    have "{x - y| x y. x \<in> s \<and> y \<in> t} = {x + y |x y. x \<in> s \<and> y \<in> uminus ` t}"
   1.391    proof safe
   1.392 -    fix x x' y assume "x' \<in> s" "y \<in> t"
   1.393 -    thus "\<exists>x y'. x' - y = x + y' \<and> x \<in> s \<and> y' \<in> uminus ` t"
   1.394 +    fix x x' y
   1.395 +    assume "x' \<in> s" "y \<in> t"
   1.396 +    then show "\<exists>x y'. x' - y = x + y' \<and> x \<in> s \<and> y' \<in> uminus ` t"
   1.397        using exI[of _ x'] exI[of _ "-y"] by auto
   1.398    next
   1.399 -    fix x x' y y' assume "x' \<in> s" "y' \<in> t"
   1.400 -    thus "\<exists>x y. x' + - y' = x - y \<and> x \<in> s \<and> y \<in> t"
   1.401 +    fix x x' y y'
   1.402 +    assume "x' \<in> s" "y' \<in> t"
   1.403 +    then show "\<exists>x y. x' + - y' = x - y \<and> x \<in> s \<and> y \<in> t"
   1.404        using exI[of _ x'] exI[of _ y'] by auto
   1.405    qed
   1.406 -  thus ?thesis using convex_sums[OF assms(1)  convex_negations[OF assms(2)]] by auto
   1.407 +  then show ?thesis
   1.408 +    using convex_sums[OF assms(1) convex_negations[OF assms(2)]] by auto
   1.409  qed
   1.410  
   1.411 -lemma convex_translation: assumes "convex s" shows "convex ((\<lambda>x. a + x) ` s)"
   1.412 -proof- have "{a + y |y. y \<in> s} = (\<lambda>x. a + x) ` s" by auto
   1.413 -  thus ?thesis using convex_sums[OF convex_singleton[of a] assms] by auto qed
   1.414 +lemma convex_translation:
   1.415 +  assumes "convex s"
   1.416 +  shows "convex ((\<lambda>x. a + x) ` s)"
   1.417 +proof -
   1.418 +  have "{a + y |y. y \<in> s} = (\<lambda>x. a + x) ` s" by auto
   1.419 +  then show ?thesis
   1.420 +    using convex_sums[OF convex_singleton[of a] assms] by auto
   1.421 +qed
   1.422  
   1.423 -lemma convex_affinity: assumes "convex s" shows "convex ((\<lambda>x. a + c *\<^sub>R x) ` s)"
   1.424 -proof- have "(\<lambda>x. a + c *\<^sub>R x) ` s = op + a ` op *\<^sub>R c ` s" by auto
   1.425 -  thus ?thesis using convex_translation[OF convex_scaling[OF assms], of a c] by auto qed
   1.426 +lemma convex_affinity:
   1.427 +  assumes "convex s"
   1.428 +  shows "convex ((\<lambda>x. a + c *\<^sub>R x) ` s)"
   1.429 +proof -
   1.430 +  have "(\<lambda>x. a + c *\<^sub>R x) ` s = op + a ` op *\<^sub>R c ` s" by auto
   1.431 +  then show ?thesis
   1.432 +    using convex_translation[OF convex_scaling[OF assms], of a c] by auto
   1.433 +qed
   1.434  
   1.435  lemma convex_linear_image:
   1.436    assumes c:"convex s" and l:"bounded_linear f"
   1.437    shows "convex(f ` s)"
   1.438 -proof(auto simp add: convex_def)
   1.439 +proof (auto simp add: convex_def)
   1.440    interpret f: bounded_linear f by fact
   1.441 -  fix x y assume xy:"x \<in> s" "y \<in> s"
   1.442 -  fix u v ::real assume uv:"0 \<le> u" "0 \<le> v" "u + v = 1"
   1.443 +  fix x y
   1.444 +  assume xy: "x \<in> s" "y \<in> s"
   1.445 +  fix u v :: real
   1.446 +  assume uv: "0 \<le> u" "0 \<le> v" "u + v = 1"
   1.447    show "u *\<^sub>R f x + v *\<^sub>R f y \<in> f ` s" unfolding image_iff
   1.448      using bexI[of _ "u *\<^sub>R x + v *\<^sub>R y"] f.add f.scaleR
   1.449        c[unfolded convex_def] xy uv by auto
   1.450  qed
   1.451  
   1.452  
   1.453 -lemma pos_is_convex:
   1.454 -  shows "convex {0 :: real <..}"
   1.455 -unfolding convex_alt
   1.456 +lemma pos_is_convex: "convex {0 :: real <..}"
   1.457 +  unfolding convex_alt
   1.458  proof safe
   1.459    fix y x \<mu> :: real
   1.460    assume asms: "y > 0" "x > 0" "\<mu> \<ge> 0" "\<mu> \<le> 1"
   1.461    { assume "\<mu> = 0"
   1.462 -    hence "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y = y" by simp
   1.463 -    hence "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms by simp }
   1.464 +    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y = y" by simp
   1.465 +    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms by simp }
   1.466    moreover
   1.467    { assume "\<mu> = 1"
   1.468 -    hence "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms by simp }
   1.469 +    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms by simp }
   1.470    moreover
   1.471    { assume "\<mu> \<noteq> 1" "\<mu> \<noteq> 0"
   1.472 -    hence "\<mu> > 0" "(1 - \<mu>) > 0" using asms by auto
   1.473 -    hence "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms
   1.474 +    then have "\<mu> > 0" "(1 - \<mu>) > 0" using asms by auto
   1.475 +    then have "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y > 0" using asms
   1.476        by (auto simp add: add_pos_pos mult_pos_pos) }
   1.477    ultimately show "(1 - \<mu>) *\<^sub>R y + \<mu> *\<^sub>R x > 0" using assms by fastforce
   1.478  qed
   1.479  
   1.480  lemma convex_on_setsum:
   1.481    fixes a :: "'a \<Rightarrow> real"
   1.482 -  fixes y :: "'a \<Rightarrow> 'b::real_vector"
   1.483 -  fixes f :: "'b \<Rightarrow> real"
   1.484 +    and y :: "'a \<Rightarrow> 'b::real_vector"
   1.485 +    and f :: "'b \<Rightarrow> real"
   1.486    assumes "finite s" "s \<noteq> {}"
   1.487 -  assumes "convex_on C f"
   1.488 -  assumes "convex C"
   1.489 -  assumes "(\<Sum> i \<in> s. a i) = 1"
   1.490 -  assumes "\<And> i. i \<in> s \<Longrightarrow> a i \<ge> 0"
   1.491 -  assumes "\<And> i. i \<in> s \<Longrightarrow> y i \<in> C"
   1.492 +    and "convex_on C f"
   1.493 +    and "convex C"
   1.494 +    and "(\<Sum> i \<in> s. a i) = 1"
   1.495 +    and "\<And>i. i \<in> s \<Longrightarrow> a i \<ge> 0"
   1.496 +    and "\<And>i. i \<in> s \<Longrightarrow> y i \<in> C"
   1.497    shows "f (\<Sum> i \<in> s. a i *\<^sub>R y i) \<le> (\<Sum> i \<in> s. a i * f (y i))"
   1.498 -using assms
   1.499 -proof (induct s arbitrary:a rule:finite_ne_induct)
   1.500 +  using assms
   1.501 +proof (induct s arbitrary: a rule: finite_ne_induct)
   1.502    case (singleton i)
   1.503 -  hence ai: "a i = 1" by auto
   1.504 -  thus ?case by auto
   1.505 +  then have ai: "a i = 1" by auto
   1.506 +  then show ?case by auto
   1.507  next
   1.508    case (insert i s) note asms = this
   1.509 -  hence "convex_on C f" by simp
   1.510 +  then have "convex_on C f" by simp
   1.511    from this[unfolded convex_on_def, rule_format]
   1.512 -  have conv: "\<And> x y \<mu>. \<lbrakk>x \<in> C; y \<in> C; 0 \<le> \<mu>; \<mu> \<le> 1\<rbrakk>
   1.513 -  \<Longrightarrow> f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   1.514 +  have conv: "\<And>x y \<mu>. x \<in> C \<Longrightarrow> y \<in> C \<Longrightarrow> 0 \<le> \<mu> \<Longrightarrow> \<mu> \<le> 1
   1.515 +      \<Longrightarrow> f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   1.516      by simp
   1.517    { assume "a i = 1"
   1.518 -    hence "(\<Sum> j \<in> s. a j) = 0"
   1.519 +    then have "(\<Sum> j \<in> s. a j) = 0"
   1.520        using asms by auto
   1.521 -    hence "\<And> j. j \<in> s \<Longrightarrow> a j = 0"
   1.522 +    then have "\<And>j. j \<in> s \<Longrightarrow> a j = 0"
   1.523        using setsum_nonneg_0[where 'b=real] asms by fastforce
   1.524 -    hence ?case using asms by auto }
   1.525 +    then have ?case using asms by auto }
   1.526    moreover
   1.527    { assume asm: "a i \<noteq> 1"
   1.528      from asms have yai: "y i \<in> C" "a i \<ge> 0" by auto
   1.529      have fis: "finite (insert i s)" using asms by auto
   1.530 -    hence ai1: "a i \<le> 1" using setsum_nonneg_leq_bound[of "insert i s" a] asms by simp
   1.531 -    hence "a i < 1" using asm by auto
   1.532 -    hence i0: "1 - a i > 0" by auto
   1.533 -    let "?a j" = "a j / (1 - a i)"
   1.534 +    then have ai1: "a i \<le> 1" using setsum_nonneg_leq_bound[of "insert i s" a] asms by simp
   1.535 +    then have "a i < 1" using asm by auto
   1.536 +    then have i0: "1 - a i > 0" by auto
   1.537 +    let ?a = "\<lambda>j. a j / (1 - a i)"
   1.538      { fix j assume "j \<in> s"
   1.539 -      hence "?a j \<ge> 0"
   1.540 +      then have "?a j \<ge> 0"
   1.541          using i0 asms divide_nonneg_pos
   1.542 -        by fastforce } note a_nonneg = this
   1.543 +        by fastforce }
   1.544 +    note a_nonneg = this
   1.545      have "(\<Sum> j \<in> insert i s. a j) = 1" using asms by auto
   1.546 -    hence "(\<Sum> j \<in> s. a j) = 1 - a i" using setsum.insert asms by fastforce
   1.547 -    hence "(\<Sum> j \<in> s. a j) / (1 - a i) = 1" using i0 by auto
   1.548 -    hence a1: "(\<Sum> j \<in> s. ?a j) = 1" unfolding setsum_divide_distrib by simp
   1.549 +    then have "(\<Sum> j \<in> s. a j) = 1 - a i" using setsum.insert asms by fastforce
   1.550 +    then have "(\<Sum> j \<in> s. a j) / (1 - a i) = 1" using i0 by auto
   1.551 +    then have a1: "(\<Sum> j \<in> s. ?a j) = 1" unfolding setsum_divide_distrib by simp
   1.552      have "convex C" using asms by auto
   1.553 -    hence asum: "(\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
   1.554 +    then have asum: "(\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<in> C"
   1.555        using asms convex_setsum[OF `finite s`
   1.556          `convex C` a1 a_nonneg] by auto
   1.557      have asum_le: "f (\<Sum> j \<in> s. ?a j *\<^sub>R y j) \<le> (\<Sum> j \<in> s. ?a j * f (y j))"
   1.558 @@ -423,7 +466,8 @@
   1.559      also have "\<dots> = f (((1 - a i) * inverse (1 - a i)) *\<^sub>R (\<Sum> j \<in> s. a j *\<^sub>R y j) + a i *\<^sub>R y i)"
   1.560        using i0 by auto
   1.561      also have "\<dots> = f ((1 - a i) *\<^sub>R (\<Sum> j \<in> s. (a j * inverse (1 - a i)) *\<^sub>R y j) + a i *\<^sub>R y i)"
   1.562 -      using scaleR_right.setsum[of "inverse (1 - a i)" "\<lambda> j. a j *\<^sub>R y j" s, symmetric] by (auto simp:algebra_simps)
   1.563 +      using scaleR_right.setsum[of "inverse (1 - a i)" "\<lambda> j. a j *\<^sub>R y j" s, symmetric]
   1.564 +      by (auto simp:algebra_simps)
   1.565      also have "\<dots> = f ((1 - a i) *\<^sub>R (\<Sum> j \<in> s. ?a j *\<^sub>R y j) + a i *\<^sub>R y i)"
   1.566        by (auto simp: divide_inverse)
   1.567      also have "\<dots> \<le> (1 - a i) *\<^sub>R f ((\<Sum> j \<in> s. ?a j *\<^sub>R y j)) + a i * f (y i)"
   1.568 @@ -448,27 +492,30 @@
   1.569    (\<forall> x \<in> C. \<forall> y \<in> C. \<forall> \<mu> :: real. \<mu> \<ge> 0 \<and> \<mu> \<le> 1
   1.570        \<longrightarrow> f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y)"
   1.571  proof safe
   1.572 -  fix x y fix \<mu> :: real
   1.573 +  fix x y
   1.574 +  fix \<mu> :: real
   1.575    assume asms: "convex_on C f" "x \<in> C" "y \<in> C" "0 \<le> \<mu>" "\<mu> \<le> 1"
   1.576    from this[unfolded convex_on_def, rule_format]
   1.577 -  have "\<And> u v. \<lbrakk>0 \<le> u; 0 \<le> v; u + v = 1\<rbrakk> \<Longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y" by auto
   1.578 +  have "\<And>u v. \<lbrakk>0 \<le> u; 0 \<le> v; u + v = 1\<rbrakk> \<Longrightarrow> f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y" by auto
   1.579    from this[of "\<mu>" "1 - \<mu>", simplified] asms
   1.580 -  show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y)
   1.581 -          \<le> \<mu> * f x + (1 - \<mu>) * f y" by auto
   1.582 +  show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y" by auto
   1.583  next
   1.584    assume asm: "\<forall>x\<in>C. \<forall>y\<in>C. \<forall>\<mu>. 0 \<le> \<mu> \<and> \<mu> \<le> 1 \<longrightarrow> f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   1.585 -  {fix x y fix u v :: real
   1.586 +  { fix x y
   1.587 +    fix u v :: real
   1.588      assume lasm: "x \<in> C" "y \<in> C" "u \<ge> 0" "v \<ge> 0" "u + v = 1"
   1.589 -    hence[simp]: "1 - u = v" by auto
   1.590 +    then have[simp]: "1 - u = v" by auto
   1.591      from asm[rule_format, of x y u]
   1.592 -    have "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y" using lasm by auto }
   1.593 -  thus "convex_on C f" unfolding convex_on_def by auto
   1.594 +    have "f (u *\<^sub>R x + v *\<^sub>R y) \<le> u * f x + v * f y" using lasm by auto
   1.595 +  }
   1.596 +  then show "convex_on C f" unfolding convex_on_def by auto
   1.597  qed
   1.598  
   1.599  lemma convex_on_diff:
   1.600    fixes f :: "real \<Rightarrow> real"
   1.601    assumes f: "convex_on I f" and I: "x\<in>I" "y\<in>I" and t: "x < t" "t < y"
   1.602 -  shows "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)" "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
   1.603 +  shows "(f x - f t) / (x - t) \<le> (f x - f y) / (x - y)"
   1.604 +    "(f x - f y) / (x - y) \<le> (f t - f y) / (t - y)"
   1.605  proof -
   1.606    def a \<equiv> "(t - y) / (x - y)"
   1.607    with t have "0 \<le> a" "0 \<le> 1 - a" by (auto simp: field_simps)
   1.608 @@ -488,46 +535,48 @@
   1.609  lemma pos_convex_function:
   1.610    fixes f :: "real \<Rightarrow> real"
   1.611    assumes "convex C"
   1.612 -  assumes leq: "\<And> x y. \<lbrakk>x \<in> C ; y \<in> C\<rbrakk> \<Longrightarrow> f' x * (y - x) \<le> f y - f x"
   1.613 +    and leq: "\<And>x y. \<lbrakk>x \<in> C ; y \<in> C\<rbrakk> \<Longrightarrow> f' x * (y - x) \<le> f y - f x"
   1.614    shows "convex_on C f"
   1.615 -unfolding convex_on_alt[OF assms(1)]
   1.616 -using assms
   1.617 +  unfolding convex_on_alt[OF assms(1)]
   1.618 +  using assms
   1.619  proof safe
   1.620    fix x y \<mu> :: real
   1.621    let ?x = "\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y"
   1.622    assume asm: "convex C" "x \<in> C" "y \<in> C" "\<mu> \<ge> 0" "\<mu> \<le> 1"
   1.623 -  hence "1 - \<mu> \<ge> 0" by auto
   1.624 -  hence xpos: "?x \<in> C" using asm unfolding convex_alt by fastforce
   1.625 +  then have "1 - \<mu> \<ge> 0" by auto
   1.626 +  then have xpos: "?x \<in> C" using asm unfolding convex_alt by fastforce
   1.627    have geq: "\<mu> * (f x - f ?x) + (1 - \<mu>) * (f y - f ?x)
   1.628              \<ge> \<mu> * f' ?x * (x - ?x) + (1 - \<mu>) * f' ?x * (y - ?x)"
   1.629      using add_mono[OF mult_left_mono[OF leq[OF xpos asm(2)] `\<mu> \<ge> 0`]
   1.630        mult_left_mono[OF leq[OF xpos asm(3)] `1 - \<mu> \<ge> 0`]] by auto
   1.631 -  hence "\<mu> * f x + (1 - \<mu>) * f y - f ?x \<ge> 0"
   1.632 -    by (auto simp add:field_simps)
   1.633 -  thus "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   1.634 +  then have "\<mu> * f x + (1 - \<mu>) * f y - f ?x \<ge> 0"
   1.635 +    by (auto simp add: field_simps)
   1.636 +  then show "f (\<mu> *\<^sub>R x + (1 - \<mu>) *\<^sub>R y) \<le> \<mu> * f x + (1 - \<mu>) * f y"
   1.637      using convex_on_alt by auto
   1.638  qed
   1.639  
   1.640  lemma atMostAtLeast_subset_convex:
   1.641    fixes C :: "real set"
   1.642    assumes "convex C"
   1.643 -  assumes "x \<in> C" "y \<in> C" "x < y"
   1.644 +    and "x \<in> C" "y \<in> C" "x < y"
   1.645    shows "{x .. y} \<subseteq> C"
   1.646  proof safe
   1.647    fix z assume zasm: "z \<in> {x .. y}"
   1.648    { assume asm: "x < z" "z < y"
   1.649 -    let "?\<mu>" = "(y - z) / (y - x)"
   1.650 -    have "0 \<le> ?\<mu>" "?\<mu> \<le> 1" using assms asm by (auto simp add:field_simps)
   1.651 -    hence comb: "?\<mu> * x + (1 - ?\<mu>) * y \<in> C"
   1.652 -      using assms iffD1[OF convex_alt, rule_format, of C y x ?\<mu>] by (simp add:algebra_simps)
   1.653 +    let ?\<mu> = "(y - z) / (y - x)"
   1.654 +    have "0 \<le> ?\<mu>" "?\<mu> \<le> 1" using assms asm by (auto simp add: field_simps)
   1.655 +    then have comb: "?\<mu> * x + (1 - ?\<mu>) * y \<in> C"
   1.656 +      using assms iffD1[OF convex_alt, rule_format, of C y x ?\<mu>]
   1.657 +      by (simp add: algebra_simps)
   1.658      have "?\<mu> * x + (1 - ?\<mu>) * y = (y - z) * x / (y - x) + (1 - (y - z) / (y - x)) * y"
   1.659 -      by (auto simp add:field_simps)
   1.660 +      by (auto simp add: field_simps)
   1.661      also have "\<dots> = ((y - z) * x + (y - x - (y - z)) * y) / (y - x)"
   1.662 -      using assms unfolding add_divide_distrib by (auto simp:field_simps)
   1.663 +      using assms unfolding add_divide_distrib by (auto simp: field_simps)
   1.664      also have "\<dots> = z"
   1.665 -      using assms by (auto simp:field_simps)
   1.666 +      using assms by (auto simp: field_simps)
   1.667      finally have "z \<in> C"
   1.668 -      using comb by auto } note less = this
   1.669 +      using comb by auto }
   1.670 +  note less = this
   1.671    show "z \<in> C" using zasm less assms
   1.672      unfolding atLeastAtMost_iff le_less by auto
   1.673  qed
   1.674 @@ -535,21 +584,22 @@
   1.675  lemma f''_imp_f':
   1.676    fixes f :: "real \<Rightarrow> real"
   1.677    assumes "convex C"
   1.678 -  assumes f': "\<And> x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
   1.679 -  assumes f'': "\<And> x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
   1.680 -  assumes pos: "\<And> x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
   1.681 -  assumes "x \<in> C" "y \<in> C"
   1.682 +    and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
   1.683 +    and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
   1.684 +    and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
   1.685 +    and "x \<in> C" "y \<in> C"
   1.686    shows "f' x * (y - x) \<le> f y - f x"
   1.687 -using assms
   1.688 +  using assms
   1.689  proof -
   1.690 -  { fix x y :: real assume asm: "x \<in> C" "y \<in> C" "y > x"
   1.691 -    hence ge: "y - x > 0" "y - x \<ge> 0" by auto
   1.692 +  { fix x y :: real
   1.693 +    assume asm: "x \<in> C" "y \<in> C" "y > x"
   1.694 +    then have ge: "y - x > 0" "y - x \<ge> 0" by auto
   1.695      from asm have le: "x - y < 0" "x - y \<le> 0" by auto
   1.696      then obtain z1 where z1: "z1 > x" "z1 < y" "f y - f x = (y - x) * f' z1"
   1.697        using subsetD[OF atMostAtLeast_subset_convex[OF `convex C` `x \<in> C` `y \<in> C` `x < y`],
   1.698          THEN f', THEN MVT2[OF `x < y`, rule_format, unfolded atLeastAtMost_iff[symmetric]]]
   1.699        by auto
   1.700 -    hence "z1 \<in> C" using atMostAtLeast_subset_convex
   1.701 +    then have "z1 \<in> C" using atMostAtLeast_subset_convex
   1.702        `convex C` `x \<in> C` `y \<in> C` `x < y` by fastforce
   1.703      from z1 have z1': "f x - f y = (x - y) * f' z1"
   1.704        by (simp add:field_simps)
   1.705 @@ -568,14 +618,14 @@
   1.706      have A': "y - z1 \<ge> 0" using z1 by auto
   1.707      have "z3 \<in> C" using z3 asm atMostAtLeast_subset_convex
   1.708        `convex C` `x \<in> C` `z1 \<in> C` `x < z1` by fastforce
   1.709 -    hence B': "f'' z3 \<ge> 0" using assms by auto
   1.710 +    then have B': "f'' z3 \<ge> 0" using assms by auto
   1.711      from A' B' have "(y - z1) * f'' z3 \<ge> 0" using mult_nonneg_nonneg by auto
   1.712      from cool' this have "f' y - (f x - f y) / (x - y) \<ge> 0" by auto
   1.713      from mult_right_mono_neg[OF this le(2)]
   1.714      have "f' y * (x - y) - (f x - f y) / (x - y) * (x - y) \<le> 0 * (x - y)"
   1.715        by (simp add: algebra_simps)
   1.716 -    hence "f' y * (x - y) - (f x - f y) \<le> 0" using le by auto
   1.717 -    hence res: "f' y * (x - y) \<le> f x - f y" by auto
   1.718 +    then have "f' y * (x - y) - (f x - f y) \<le> 0" using le by auto
   1.719 +    then have res: "f' y * (x - y) \<le> f x - f y" by auto
   1.720      have "(f y - f x) / (y - x) - f' x = f' z1 - f' x"
   1.721        using asm z1 by auto
   1.722      also have "\<dots> = (z1 - x) * f'' z2" using z2 by auto
   1.723 @@ -583,30 +633,32 @@
   1.724      have A: "z1 - x \<ge> 0" using z1 by auto
   1.725      have "z2 \<in> C" using z2 z1 asm atMostAtLeast_subset_convex
   1.726        `convex C` `z1 \<in> C` `y \<in> C` `z1 < y` by fastforce
   1.727 -    hence B: "f'' z2 \<ge> 0" using assms by auto
   1.728 +    then have B: "f'' z2 \<ge> 0" using assms by auto
   1.729      from A B have "(z1 - x) * f'' z2 \<ge> 0" using mult_nonneg_nonneg by auto
   1.730      from cool this have "(f y - f x) / (y - x) - f' x \<ge> 0" by auto
   1.731      from mult_right_mono[OF this ge(2)]
   1.732      have "(f y - f x) / (y - x) * (y - x) - f' x * (y - x) \<ge> 0 * (y - x)"
   1.733        by (simp add: algebra_simps)
   1.734 -    hence "f y - f x - f' x * (y - x) \<ge> 0" using ge by auto
   1.735 -    hence "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
   1.736 +    then have "f y - f x - f' x * (y - x) \<ge> 0" using ge by auto
   1.737 +    then have "f y - f x \<ge> f' x * (y - x)" "f' y * (x - y) \<le> f x - f y"
   1.738        using res by auto } note less_imp = this
   1.739 -  { fix x y :: real assume "x \<in> C" "y \<in> C" "x \<noteq> y"
   1.740 -    hence"f y - f x \<ge> f' x * (y - x)"
   1.741 +  { fix x y :: real
   1.742 +    assume "x \<in> C" "y \<in> C" "x \<noteq> y"
   1.743 +    then have"f y - f x \<ge> f' x * (y - x)"
   1.744      unfolding neq_iff using less_imp by auto } note neq_imp = this
   1.745    moreover
   1.746 -  { fix x y :: real assume asm: "x \<in> C" "y \<in> C" "x = y"
   1.747 -    hence "f y - f x \<ge> f' x * (y - x)" by auto }
   1.748 +  { fix x y :: real
   1.749 +    assume asm: "x \<in> C" "y \<in> C" "x = y"
   1.750 +    then have "f y - f x \<ge> f' x * (y - x)" by auto }
   1.751    ultimately show ?thesis using assms by blast
   1.752  qed
   1.753  
   1.754  lemma f''_ge0_imp_convex:
   1.755    fixes f :: "real \<Rightarrow> real"
   1.756    assumes conv: "convex C"
   1.757 -  assumes f': "\<And> x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
   1.758 -  assumes f'': "\<And> x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
   1.759 -  assumes pos: "\<And> x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
   1.760 +    and f': "\<And>x. x \<in> C \<Longrightarrow> DERIV f x :> (f' x)"
   1.761 +    and f'': "\<And>x. x \<in> C \<Longrightarrow> DERIV f' x :> (f'' x)"
   1.762 +    and pos: "\<And>x. x \<in> C \<Longrightarrow> f'' x \<ge> 0"
   1.763    shows "convex_on C f"
   1.764  using f''_imp_f'[OF conv f' f'' pos] assms pos_convex_function by fastforce
   1.765  
   1.766 @@ -615,18 +667,19 @@
   1.767    assumes "b > 1"
   1.768    shows "convex_on {0 <..} (\<lambda> x. - log b x)"
   1.769  proof -
   1.770 -  have "\<And> z. z > 0 \<Longrightarrow> DERIV (log b) z :> 1 / (ln b * z)" using DERIV_log by auto
   1.771 -  hence f': "\<And> z. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - log b z) z :> - 1 / (ln b * z)"
   1.772 +  have "\<And>z. z > 0 \<Longrightarrow> DERIV (log b) z :> 1 / (ln b * z)" using DERIV_log by auto
   1.773 +  then have f': "\<And>z. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - log b z) z :> - 1 / (ln b * z)"
   1.774      using DERIV_minus by auto
   1.775 -  have "\<And> z :: real. z > 0 \<Longrightarrow> DERIV inverse z :> - (inverse z ^ Suc (Suc 0))"
   1.776 +  have "\<And>z :: real. z > 0 \<Longrightarrow> DERIV inverse z :> - (inverse z ^ Suc (Suc 0))"
   1.777      using less_imp_neq[THEN not_sym, THEN DERIV_inverse] by auto
   1.778    from this[THEN DERIV_cmult, of _ "- 1 / ln b"]
   1.779 -  have "\<And> z :: real. z > 0 \<Longrightarrow> DERIV (\<lambda> z. (- 1 / ln b) * inverse z) z :> (- 1 / ln b) * (- (inverse z ^ Suc (Suc 0)))"
   1.780 +  have "\<And>z :: real. z > 0 \<Longrightarrow>
   1.781 +    DERIV (\<lambda> z. (- 1 / ln b) * inverse z) z :> (- 1 / ln b) * (- (inverse z ^ Suc (Suc 0)))"
   1.782      by auto
   1.783 -  hence f''0: "\<And> z :: real. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - 1 / (ln b * z)) z :> 1 / (ln b * z * z)"
   1.784 +  then have f''0: "\<And>z :: real. z > 0 \<Longrightarrow> DERIV (\<lambda> z. - 1 / (ln b * z)) z :> 1 / (ln b * z * z)"
   1.785      unfolding inverse_eq_divide by (auto simp add: mult_assoc)
   1.786 -  have f''_ge0: "\<And> z :: real. z > 0 \<Longrightarrow> 1 / (ln b * z * z) \<ge> 0"
   1.787 -    using `b > 1` by (auto intro!:less_imp_le simp add:divide_pos_pos[of 1] mult_pos_pos)
   1.788 +  have f''_ge0: "\<And>z :: real. z > 0 \<Longrightarrow> 1 / (ln b * z * z) \<ge> 0"
   1.789 +    using `b > 1` by (auto intro!:less_imp_le simp add: divide_pos_pos[of 1] mult_pos_pos)
   1.790    from f''_ge0_imp_convex[OF pos_is_convex,
   1.791      unfolded greaterThan_iff, OF f' f''0 f''_ge0]
   1.792    show ?thesis by auto