src/HOL/Multivariate_Analysis/Integration.thy
author wenzelm
Wed Nov 28 15:59:18 2012 +0100 (2012-11-28)
changeset 50252 4aa34bd43228
parent 50241 8b0fdeeefef7
child 50348 4b4fe0d5ee22
permissions -rw-r--r--
eliminated slightly odd identifiers;
     1 header {* Kurzweil-Henstock Gauge Integration in many dimensions. *}
     2 (*  Author:                     John Harrison
     3     Translation from HOL light: Robert Himmelmann, TU Muenchen *)
     4 
     5 theory Integration
     6 imports
     7   Derivative
     8   "~~/src/HOL/Library/Indicator_Function"
     9 begin
    10 
    11 declare [[smt_certificates = "Integration.certs"]]
    12 declare [[smt_read_only_certificates = true]]
    13 declare [[smt_oracle = false]]
    14 
    15 (*declare not_less[simp] not_le[simp]*)
    16 
    17 lemmas scaleR_simps = scaleR_zero_left scaleR_minus_left scaleR_left_diff_distrib
    18   scaleR_zero_right scaleR_minus_right scaleR_right_diff_distrib scaleR_eq_0_iff
    19   scaleR_cancel_left scaleR_cancel_right scaleR_add_right scaleR_add_left real_vector_class.scaleR_one
    20 
    21 lemma real_arch_invD:
    22   "0 < (e::real) \<Longrightarrow> (\<exists>n::nat. n \<noteq> 0 \<and> 0 < inverse (real n) \<and> inverse (real n) < e)"
    23   by (subst(asm) real_arch_inv)
    24 
    25 
    26 subsection {* Sundries *}
    27 
    28 lemma conjunctD2: assumes "a \<and> b" shows a b using assms by auto
    29 lemma conjunctD3: assumes "a \<and> b \<and> c" shows a b c using assms by auto
    30 lemma conjunctD4: assumes "a \<and> b \<and> c \<and> d" shows a b c d using assms by auto
    31 lemma conjunctD5: assumes "a \<and> b \<and> c \<and> d \<and> e" shows a b c d e using assms by auto
    32 
    33 declare norm_triangle_ineq4[intro] 
    34 
    35 lemma simple_image: "{f x |x . x \<in> s} = f ` s" by blast
    36 
    37 lemma linear_simps:
    38   assumes "bounded_linear f"
    39   shows
    40     "f (a + b) = f a + f b"
    41     "f (a - b) = f a - f b"
    42     "f 0 = 0"
    43     "f (- a) = - f a"
    44     "f (s *\<^sub>R v) = s *\<^sub>R (f v)"
    45   apply (rule_tac[!] additive.add additive.minus additive.diff additive.zero bounded_linear.scaleR)
    46   using assms unfolding bounded_linear_def additive_def
    47   apply auto
    48   done
    49 
    50 lemma bounded_linearI:
    51   assumes "\<And>x y. f (x + y) = f x + f y"
    52     and "\<And>r x. f (r *\<^sub>R x) = r *\<^sub>R f x" "\<And>x. norm (f x) \<le> norm x * K"
    53   shows "bounded_linear f"
    54   unfolding bounded_linear_def additive_def bounded_linear_axioms_def using assms by auto
    55  
    56 lemma real_le_inf_subset:
    57   assumes "t \<noteq> {}" "t \<subseteq> s" "\<exists>b. b <=* s"
    58   shows "Inf s <= Inf (t::real set)"
    59   apply (rule isGlb_le_isLb)
    60   apply (rule Inf[OF assms(1)])
    61   apply (insert assms)
    62   apply (erule exE)
    63   apply (rule_tac x = b in exI)
    64   apply (auto simp: isLb_def setge_def)
    65   done
    66 
    67 lemma real_ge_sup_subset:
    68   assumes "t \<noteq> {}" "t \<subseteq> s" "\<exists>b. s *<= b"
    69   shows "Sup s >= Sup (t::real set)"
    70   apply (rule isLub_le_isUb)
    71   apply (rule Sup[OF assms(1)])
    72   apply (insert assms)
    73   apply (erule exE)
    74   apply (rule_tac x = b in exI)
    75   apply (auto simp: isUb_def setle_def)
    76   done
    77 
    78 lemma bounded_linear_component [intro]: "bounded_linear (\<lambda>x::'a::euclidean_space. x $$ k)"
    79   apply (rule bounded_linearI[where K=1])
    80   using component_le_norm[of _ k]
    81   unfolding real_norm_def
    82   apply auto
    83   done
    84 
    85 lemma transitive_stepwise_lt_eq:
    86   assumes "(\<And>x y z::nat. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z)"
    87   shows "((\<forall>m. \<forall>n>m. R m n) \<longleftrightarrow> (\<forall>n. R n (Suc n)))" (is "?l = ?r")
    88 proof (safe)
    89   assume ?r
    90   fix n m :: nat
    91   assume "m < n"
    92   then show "R m n"
    93   proof (induct n arbitrary: m)
    94     case (Suc n)
    95     show ?case 
    96     proof (cases "m < n")
    97       case True
    98       show ?thesis
    99         apply (rule assms[OF Suc(1)[OF True]])
   100         using `?r` apply auto
   101         done
   102     next
   103       case False
   104       then have "m = n" using Suc(2) by auto
   105       then show ?thesis using `?r` by auto
   106     qed
   107   qed auto
   108 qed auto
   109 
   110 lemma transitive_stepwise_gt:
   111   assumes "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z" "\<And>n. R n (Suc n) "
   112   shows "\<forall>n>m. R m n"
   113 proof -
   114   have "\<forall>m. \<forall>n>m. R m n"
   115     apply (subst transitive_stepwise_lt_eq)
   116     apply (rule assms)
   117     apply assumption
   118     apply assumption
   119     using assms(2) apply auto
   120     done
   121   then show ?thesis by auto
   122 qed
   123 
   124 lemma transitive_stepwise_le_eq:
   125   assumes "\<And>x. R x x" "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z"
   126   shows "(\<forall>m. \<forall>n\<ge>m. R m n) \<longleftrightarrow> (\<forall>n. R n (Suc n))" (is "?l = ?r")
   127 proof safe
   128   assume ?r
   129   fix m n :: nat
   130   assume "m \<le> n"
   131   thus "R m n"
   132   proof (induct n arbitrary: m)
   133     case 0
   134     with assms show ?case by auto
   135   next
   136     case (Suc n)
   137     show ?case
   138     proof (cases "m \<le> n")
   139       case True
   140       show ?thesis
   141         apply (rule assms(2))
   142         apply (rule Suc(1)[OF True])
   143         using `?r` apply auto
   144         done
   145     next
   146       case False
   147       hence "m = Suc n" using Suc(2) by auto
   148       thus ?thesis using assms(1) by auto
   149     qed
   150   qed
   151 qed auto
   152 
   153 lemma transitive_stepwise_le:
   154   assumes "\<And>x. R x x" "\<And>x y z. R x y \<Longrightarrow> R y z \<Longrightarrow> R x z" "\<And>n. R n (Suc n) "
   155   shows "\<forall>n\<ge>m. R m n"
   156 proof -
   157   have "\<forall>m. \<forall>n\<ge>m. R m n"
   158     apply (subst transitive_stepwise_le_eq)
   159     apply (rule assms)
   160     apply (rule assms,assumption,assumption)
   161     using assms(3) apply auto
   162     done
   163   then show ?thesis by auto
   164 qed
   165 
   166 
   167 subsection {* Some useful lemmas about intervals. *}
   168 
   169 abbreviation One  where "One \<equiv> ((\<chi>\<chi> i. 1)::_::ordered_euclidean_space)"
   170 
   171 lemma empty_as_interval: "{} = {One..0}"
   172   apply (rule set_eqI, rule)
   173   defer
   174   unfolding mem_interval
   175   using UNIV_witness[where 'a='n]
   176   apply (erule_tac exE, rule_tac x = x in allE)
   177   apply auto
   178   done
   179 
   180 lemma interior_subset_union_intervals: 
   181   assumes "i = {a..b::'a::ordered_euclidean_space}" "j = {c..d}"
   182     "interior j \<noteq> {}" "i \<subseteq> j \<union> s" "interior(i) \<inter> interior(j) = {}"
   183   shows "interior i \<subseteq> interior s"
   184 proof -
   185   have "{a<..<b} \<inter> {c..d} = {}"
   186     using inter_interval_mixed_eq_empty[of c d a b] and assms(3,5)
   187     unfolding assms(1,2) interior_closed_interval by auto
   188   moreover
   189   have "{a<..<b} \<subseteq> {c..d} \<union> s"
   190     apply (rule order_trans,rule interval_open_subset_closed)
   191     using assms(4) unfolding assms(1,2)
   192     apply auto
   193     done
   194   ultimately
   195   show ?thesis
   196     apply -
   197     apply (rule interior_maximal)
   198     defer
   199     apply (rule open_interior)
   200     unfolding assms(1,2) interior_closed_interval
   201     apply auto
   202     done
   203 qed
   204 
   205 lemma inter_interior_unions_intervals:
   206   fixes f::"('a::ordered_euclidean_space) set set"
   207   assumes "finite f" "open s" "\<forall>t\<in>f. \<exists>a b. t = {a..b}" "\<forall>t\<in>f. s \<inter> (interior t) = {}"
   208   shows "s \<inter> interior(\<Union>f) = {}"
   209 proof (rule ccontr, unfold ex_in_conv[THEN sym])
   210   case goal1
   211   have lem1: "\<And>x e s U. ball x e \<subseteq> s \<inter> interior U \<longleftrightarrow> ball x e \<subseteq> s \<inter> U"
   212     apply rule
   213     defer
   214     apply (rule_tac Int_greatest)
   215     unfolding open_subset_interior[OF open_ball]
   216     using interior_subset
   217     apply auto
   218     done
   219   have lem2: "\<And>x s P. \<exists>x\<in>s. P x \<Longrightarrow> \<exists>x\<in>insert x s. P x" by auto
   220   have "\<And>f. finite f \<Longrightarrow> (\<forall>t\<in>f. \<exists>a b. t = {a..b}) \<Longrightarrow>
   221     (\<exists>x. x \<in> s \<inter> interior (\<Union>f)) \<Longrightarrow> (\<exists>t\<in>f. \<exists>x. \<exists>e>0. ball x e \<subseteq> s \<inter> t)"
   222   proof -
   223     case goal1
   224     then show ?case
   225     proof (induct rule: finite_induct)
   226       case empty from this(2) guess x ..
   227       hence False unfolding Union_empty interior_empty by auto
   228       thus ?case by auto
   229     next
   230       case (insert i f) guess x using insert(5) .. note x = this
   231       then guess e unfolding open_contains_ball_eq[OF open_Int[OF assms(2) open_interior],rule_format] .. note e=this
   232       guess a using insert(4)[rule_format,OF insertI1] ..
   233       then guess b .. note ab = this
   234       show ?case
   235       proof (cases "x\<in>i")
   236         case False
   237         hence "x \<in> UNIV - {a..b}" unfolding ab by auto
   238         then guess d unfolding open_contains_ball_eq[OF open_Diff[OF open_UNIV closed_interval],rule_format] ..
   239         hence "0 < d" "ball x (min d e) \<subseteq> UNIV - i" unfolding ab ball_min_Int by auto
   240         hence "ball x (min d e) \<subseteq> s \<inter> interior (\<Union>f)"
   241           using e unfolding lem1 unfolding  ball_min_Int by auto
   242         hence "x \<in> s \<inter> interior (\<Union>f)" using `d>0` e by auto
   243         hence "\<exists>t\<in>f. \<exists>x e. 0 < e \<and> ball x e \<subseteq> s \<inter> t"
   244           apply -
   245           apply (rule insert(3))
   246           using insert(4)
   247           apply auto
   248           done
   249         thus ?thesis by auto
   250       next
   251         case True show ?thesis
   252         proof (cases "x\<in>{a<..<b}")
   253           case True
   254           then guess d unfolding open_contains_ball_eq[OF open_interval,rule_format] ..
   255           thus ?thesis
   256             apply (rule_tac x=i in bexI, rule_tac x=x in exI, rule_tac x="min d e" in exI)
   257             unfolding ab
   258             using interval_open_subset_closed[of a b] and e apply fastforce+
   259             done
   260         next
   261           case False
   262           then obtain k where "x$$k \<le> a$$k \<or> x$$k \<ge> b$$k" and k:"k<DIM('a)"
   263             unfolding mem_interval by (auto simp add: not_less)
   264           hence "x$$k = a$$k \<or> x$$k = b$$k"
   265             using True unfolding ab and mem_interval
   266               apply (erule_tac x = k in allE)
   267               apply auto
   268               done
   269           hence "\<exists>x. ball x (e/2) \<subseteq> s \<inter> (\<Union>f)"
   270           proof (erule_tac disjE)
   271             let ?z = "x - (e/2) *\<^sub>R basis k"
   272             assume as: "x$$k = a$$k"
   273             have "ball ?z (e / 2) \<inter> i = {}"
   274               apply (rule ccontr)
   275               unfolding ex_in_conv[THEN sym]
   276             proof (erule exE)
   277               fix y
   278               assume "y \<in> ball ?z (e / 2) \<inter> i"
   279               hence "dist ?z y < e/2" and yi:"y\<in>i" by auto
   280               hence "\<bar>(?z - y) $$ k\<bar> < e/2"
   281                 using component_le_norm[of "?z - y" k] unfolding dist_norm by auto
   282               hence "y$$k < a$$k"
   283                 using e[THEN conjunct1] k by (auto simp add: field_simps as)
   284               hence "y \<notin> i"
   285                 unfolding ab mem_interval not_all
   286                 apply (rule_tac x=k in exI)
   287                 using k apply auto
   288                 done
   289               thus False using yi by auto
   290             qed
   291             moreover
   292             have "ball ?z (e/2) \<subseteq> s \<inter> (\<Union>insert i f)"
   293               apply(rule order_trans[OF _ e[THEN conjunct2, unfolded lem1]])
   294             proof
   295               fix y
   296               assume as: "y\<in> ball ?z (e/2)"
   297               have "norm (x - y) \<le> \<bar>e\<bar> / 2 + norm (x - y - (e / 2) *\<^sub>R basis k)"
   298                 apply -
   299                 apply (rule order_trans,rule norm_triangle_sub[of "x - y" "(e/2) *\<^sub>R basis k"])
   300                 unfolding norm_scaleR norm_basis
   301                 apply auto
   302                 done
   303               also have "\<dots> < \<bar>e\<bar> / 2 + \<bar>e\<bar> / 2"
   304                 apply (rule add_strict_left_mono)
   305                 using as unfolding mem_ball dist_norm
   306                 using e apply (auto simp add: field_simps)
   307                 done
   308               finally show "y\<in>ball x e"
   309                 unfolding mem_ball dist_norm using e by (auto simp add:field_simps)
   310             qed
   311             ultimately show ?thesis
   312               apply (rule_tac x="?z" in exI)
   313               unfolding Union_insert
   314               apply auto
   315               done
   316           next
   317             let ?z = "x + (e/2) *\<^sub>R basis k"
   318             assume as: "x$$k = b$$k"
   319             have "ball ?z (e / 2) \<inter> i = {}"
   320               apply (rule ccontr)
   321               unfolding ex_in_conv[THEN sym]
   322             proof(erule exE)
   323               fix y
   324               assume "y \<in> ball ?z (e / 2) \<inter> i"
   325               hence "dist ?z y < e/2" and yi:"y\<in>i" by auto
   326               hence "\<bar>(?z - y) $$ k\<bar> < e/2"
   327                 using component_le_norm[of "?z - y" k] unfolding dist_norm by auto
   328               hence "y$$k > b$$k"
   329                 using e[THEN conjunct1] k by(auto simp add:field_simps as)
   330               hence "y \<notin> i"
   331                 unfolding ab mem_interval not_all
   332                 using k apply (rule_tac x=k in exI)
   333                 apply auto
   334                 done
   335               thus False using yi by auto
   336             qed
   337             moreover
   338             have "ball ?z (e/2) \<subseteq> s \<inter> (\<Union>insert i f)"
   339               apply (rule order_trans[OF _ e[THEN conjunct2, unfolded lem1]])
   340             proof
   341               fix y
   342               assume as: "y\<in> ball ?z (e/2)"
   343               have "norm (x - y) \<le> \<bar>e\<bar> / 2 + norm (x - y + (e / 2) *\<^sub>R basis k)"
   344                 apply -
   345                 apply(rule order_trans,rule norm_triangle_sub[of "x - y" "- (e/2) *\<^sub>R basis k"])
   346                 unfolding norm_scaleR
   347                 apply auto
   348                 done
   349               also have "\<dots> < \<bar>e\<bar> / 2 + \<bar>e\<bar> / 2"
   350                 apply (rule add_strict_left_mono)
   351                 using as unfolding mem_ball dist_norm
   352                 using e apply (auto simp add: field_simps)
   353                 done
   354               finally show "y\<in>ball x e"
   355                 unfolding mem_ball dist_norm using e by(auto simp add:field_simps)
   356             qed
   357             ultimately show ?thesis
   358               apply (rule_tac x="?z" in exI)
   359               unfolding Union_insert
   360               apply auto
   361               done
   362           qed 
   363           then guess x ..
   364           hence "x \<in> s \<inter> interior (\<Union>f)"
   365             unfolding lem1[where U="\<Union>f",THEN sym]
   366             using centre_in_ball e[THEN conjunct1] by auto
   367           thus ?thesis
   368             apply -
   369             apply (rule lem2, rule insert(3))
   370             using insert(4) apply auto
   371             done
   372         qed
   373       qed
   374     qed
   375   qed
   376   note * = this
   377   guess t using *[OF assms(1,3) goal1] ..
   378   from this(2) guess x ..
   379   then guess e ..
   380   hence "x \<in> s" "x\<in>interior t"
   381     defer
   382     using open_subset_interior[OF open_ball, of x e t] apply auto
   383     done
   384   thus False using `t\<in>f` assms(4) by auto
   385 qed
   386 
   387 
   388 subsection {* Bounds on intervals where they exist. *}
   389 
   390 definition "interval_upperbound (s::('a::ordered_euclidean_space) set) =
   391   ((\<chi>\<chi> i. Sup {a. \<exists>x\<in>s. x$$i = a})::'a)"
   392 
   393 definition "interval_lowerbound (s::('a::ordered_euclidean_space) set) =
   394   ((\<chi>\<chi> i. Inf {a. \<exists>x\<in>s. x$$i = a})::'a)"
   395 
   396 lemma interval_upperbound[simp]:
   397   assumes "\<forall>i<DIM('a::ordered_euclidean_space). a$$i \<le> (b::'a)$$i"
   398   shows "interval_upperbound {a..b} = b"
   399   using assms
   400   unfolding interval_upperbound_def
   401   apply (subst euclidean_eq[where 'a='a])
   402   apply safe
   403   unfolding euclidean_lambda_beta'
   404   apply (erule_tac x=i in allE)
   405   apply (rule Sup_unique)
   406   unfolding setle_def
   407   apply rule
   408   unfolding mem_Collect_eq
   409   apply (erule bexE)
   410   unfolding mem_interval
   411   defer
   412   apply (rule, rule)
   413   apply (rule_tac x="b$$i" in bexI)
   414   defer
   415   unfolding mem_Collect_eq
   416   apply (rule_tac x=b in bexI)
   417   unfolding mem_interval
   418   using assms apply auto
   419   done
   420 
   421 lemma interval_lowerbound[simp]:
   422   assumes "\<forall>i<DIM('a::ordered_euclidean_space). a$$i \<le> (b::'a)$$i"
   423   shows "interval_lowerbound {a..b} = a"
   424   using assms
   425   unfolding interval_lowerbound_def
   426   apply (subst euclidean_eq[where 'a='a])
   427   apply safe
   428   unfolding euclidean_lambda_beta'
   429   apply (erule_tac x=i in allE)
   430   apply (rule Inf_unique)
   431   unfolding setge_def
   432   apply rule
   433   unfolding mem_Collect_eq
   434   apply (erule bexE)
   435   unfolding mem_interval
   436   defer
   437   apply (rule, rule)
   438   apply (rule_tac x = "a$$i" in bexI)
   439   defer
   440   unfolding mem_Collect_eq
   441   apply (rule_tac x=a in bexI)
   442   unfolding mem_interval
   443   using assms apply auto
   444   done
   445 
   446 lemmas interval_bounds = interval_upperbound interval_lowerbound
   447 
   448 lemma interval_bounds'[simp]:
   449   assumes "{a..b}\<noteq>{}"
   450   shows "interval_upperbound {a..b} = b" "interval_lowerbound {a..b} = a"
   451   using assms unfolding interval_ne_empty by auto
   452 
   453 subsection {* Content (length, area, volume...) of an interval. *}
   454 
   455 definition "content (s::('a::ordered_euclidean_space) set) =
   456   (if s = {} then 0 else (\<Prod>i<DIM('a). (interval_upperbound s)$$i - (interval_lowerbound s)$$i))"
   457 
   458 lemma interval_not_empty:"\<forall>i<DIM('a). a$$i \<le> b$$i \<Longrightarrow> {a..b::'a::ordered_euclidean_space} \<noteq> {}"
   459   unfolding interval_eq_empty unfolding not_ex not_less by auto
   460 
   461 lemma content_closed_interval:
   462   fixes a::"'a::ordered_euclidean_space"
   463   assumes "\<forall>i<DIM('a). a$$i \<le> b$$i"
   464   shows "content {a..b} = (\<Prod>i<DIM('a). b$$i - a$$i)"
   465   using interval_not_empty[OF assms]
   466   unfolding content_def interval_upperbound[OF assms] interval_lowerbound[OF assms]
   467   by auto
   468 
   469 lemma content_closed_interval':
   470   fixes a::"'a::ordered_euclidean_space"
   471   assumes "{a..b}\<noteq>{}"
   472   shows "content {a..b} = (\<Prod>i<DIM('a). b$$i - a$$i)"
   473   apply (rule content_closed_interval)
   474   using assms unfolding interval_ne_empty
   475   apply assumption
   476   done
   477 
   478 lemma content_real:
   479   assumes "a\<le>b"
   480   shows "content {a..b} = b-a"
   481 proof -
   482   have *: "{..<Suc 0} = {0}" by auto
   483   show ?thesis unfolding content_def using assms by (auto simp: *)
   484 qed
   485 
   486 lemma content_singleton[simp]: "content {a} = 0"
   487 proof -
   488   have "content {a .. a} = 0"
   489     by (subst content_closed_interval) auto
   490   then show ?thesis by simp
   491 qed
   492 
   493 lemma content_unit[intro]: "content{0..One::'a::ordered_euclidean_space} = 1"
   494 proof -
   495   have *: "\<forall>i<DIM('a). (0::'a)$$i \<le> (One::'a)$$i" by auto
   496   have "0 \<in> {0..One::'a}" unfolding mem_interval by auto
   497   thus ?thesis unfolding content_def interval_bounds[OF *] using setprod_1 by auto
   498 qed
   499 
   500 lemma content_pos_le[intro]:
   501   fixes a::"'a::ordered_euclidean_space"
   502   shows "0 \<le> content {a..b}"
   503 proof (cases "{a..b} = {}")
   504   case False
   505   hence *: "\<forall>i<DIM('a). a $$ i \<le> b $$ i" unfolding interval_ne_empty .
   506   have "(\<Prod>i<DIM('a). interval_upperbound {a..b} $$ i - interval_lowerbound {a..b} $$ i) \<ge> 0"
   507     apply (rule setprod_nonneg)
   508     unfolding interval_bounds[OF *]
   509     using *
   510     apply (erule_tac x=x in allE)
   511     apply auto
   512     done
   513   thus ?thesis unfolding content_def by (auto simp del:interval_bounds')
   514 qed (unfold content_def, auto)
   515 
   516 lemma content_pos_lt:
   517   fixes a::"'a::ordered_euclidean_space"
   518   assumes "\<forall>i<DIM('a). a$$i < b$$i"
   519   shows "0 < content {a..b}"
   520 proof -
   521   have help_lemma1: "\<forall>i<DIM('a). a$$i < b$$i \<Longrightarrow> \<forall>i<DIM('a). a$$i \<le> ((b$$i)::real)"
   522     apply (rule, erule_tac x=i in allE)
   523     apply auto
   524     done
   525   show ?thesis unfolding content_closed_interval[OF help_lemma1[OF assms]]
   526     apply(rule setprod_pos)
   527     using assms apply (erule_tac x=x in allE)
   528     apply auto
   529     done
   530 qed
   531 
   532 lemma content_eq_0: "content{a..b::'a::ordered_euclidean_space} = 0 \<longleftrightarrow> (\<exists>i<DIM('a). b$$i \<le> a$$i)"
   533 proof (cases "{a..b} = {}")
   534   case True
   535   thus ?thesis
   536     unfolding content_def if_P[OF True]
   537     unfolding interval_eq_empty
   538     apply -
   539     apply (rule, erule exE)
   540     apply (rule_tac x = i in exI)
   541     apply auto
   542     done
   543 next
   544   case False
   545   from this[unfolded interval_eq_empty not_ex not_less]
   546   have as: "\<forall>i<DIM('a). b $$ i \<ge> a $$ i" by fastforce
   547   show ?thesis
   548     unfolding content_def if_not_P[OF False] setprod_zero_iff[OF finite_lessThan]
   549     apply rule
   550     apply (erule_tac[!] exE bexE)
   551     unfolding interval_bounds[OF as]
   552     apply (rule_tac x=x in exI)
   553     defer
   554     apply (rule_tac x=i in bexI)
   555     using as apply (erule_tac x=i in allE)
   556     apply auto
   557     done
   558 qed
   559 
   560 lemma cond_cases:"(P \<Longrightarrow> Q x) \<Longrightarrow> (\<not> P \<Longrightarrow> Q y) \<Longrightarrow> Q (if P then x else y)" by auto
   561 
   562 lemma content_closed_interval_cases:
   563   "content {a..b::'a::ordered_euclidean_space} =
   564     (if \<forall>i<DIM('a). a$$i \<le> b$$i then setprod (\<lambda>i. b$$i - a$$i) {..<DIM('a)} else 0)"
   565   apply (rule cond_cases) 
   566   apply (rule content_closed_interval)
   567   unfolding content_eq_0 not_all not_le
   568   defer
   569   apply (erule exE,rule_tac x=x in exI)
   570   apply auto
   571   done
   572 
   573 lemma content_eq_0_interior: "content {a..b} = 0 \<longleftrightarrow> interior({a..b}) = {}"
   574   unfolding content_eq_0 interior_closed_interval interval_eq_empty by auto
   575 
   576 (*lemma content_eq_0_1: "content {a..b::real^1} = 0 \<longleftrightarrow> dest_vec1 b \<le> dest_vec1 a"
   577   unfolding content_eq_0 by auto*)
   578 
   579 lemma content_pos_lt_eq: "0 < content {a..b::'a::ordered_euclidean_space} \<longleftrightarrow> (\<forall>i<DIM('a). a$$i < b$$i)"
   580   apply rule
   581   defer
   582   apply (rule content_pos_lt, assumption)
   583 proof -
   584   assume "0 < content {a..b}"
   585   hence "content {a..b} \<noteq> 0" by auto
   586   thus "\<forall>i<DIM('a). a$$i < b$$i"
   587     unfolding content_eq_0 not_ex not_le by fastforce
   588 qed
   589 
   590 lemma content_empty [simp]: "content {} = 0" unfolding content_def by auto
   591 
   592 lemma content_subset:
   593   assumes "{a..b} \<subseteq> {c..d}"
   594   shows "content {a..b::'a::ordered_euclidean_space} \<le> content {c..d}"
   595 proof (cases "{a..b} = {}")
   596   case True
   597   thus ?thesis using content_pos_le[of c d] by auto
   598 next
   599   case False
   600   hence ab_ne:"\<forall>i<DIM('a). a $$ i \<le> b $$ i" unfolding interval_ne_empty by auto
   601   hence ab_ab:"a\<in>{a..b}" "b\<in>{a..b}" unfolding mem_interval by auto
   602   have "{c..d} \<noteq> {}" using assms False by auto
   603   hence cd_ne:"\<forall>i<DIM('a). c $$ i \<le> d $$ i" using assms unfolding interval_ne_empty by auto
   604   show ?thesis
   605     unfolding content_def
   606     unfolding interval_bounds[OF ab_ne] interval_bounds[OF cd_ne]
   607     unfolding if_not_P[OF False] if_not_P[OF `{c..d} \<noteq> {}`]
   608     apply(rule setprod_mono,rule)
   609   proof
   610     fix i
   611     assume i:"i\<in>{..<DIM('a)}"
   612     show "0 \<le> b $$ i - a $$ i" using ab_ne[THEN spec[where x=i]] i by auto
   613     show "b $$ i - a $$ i \<le> d $$ i - c $$ i"
   614       using assms[unfolded subset_eq mem_interval,rule_format,OF ab_ab(2),of i]
   615       using assms[unfolded subset_eq mem_interval,rule_format,OF ab_ab(1),of i]
   616       using i by auto
   617   qed
   618 qed
   619 
   620 lemma content_lt_nz: "0 < content {a..b} \<longleftrightarrow> content {a..b} \<noteq> 0"
   621   unfolding content_pos_lt_eq content_eq_0 unfolding not_ex not_le by fastforce
   622 
   623 
   624 subsection {* The notion of a gauge --- simply an open set containing the point. *}
   625 
   626 definition gauge where "gauge d \<longleftrightarrow> (\<forall>x. x\<in>(d x) \<and> open(d x))"
   627 
   628 lemma gaugeI: assumes "\<And>x. x\<in>g x" "\<And>x. open (g x)" shows "gauge g"
   629   using assms unfolding gauge_def by auto
   630 
   631 lemma gaugeD[dest]: assumes "gauge d" shows "x\<in>d x" "open (d x)"
   632   using assms unfolding gauge_def by auto
   633 
   634 lemma gauge_ball_dependent: "\<forall>x. 0 < e x \<Longrightarrow> gauge (\<lambda>x. ball x (e x))"
   635   unfolding gauge_def by auto 
   636 
   637 lemma gauge_ball[intro]: "0 < e \<Longrightarrow> gauge (\<lambda>x. ball x e)" unfolding gauge_def by auto 
   638 
   639 lemma gauge_trivial[intro]: "gauge (\<lambda>x. ball x 1)"
   640   by (rule gauge_ball) auto
   641 
   642 lemma gauge_inter[intro]: "gauge d1 \<Longrightarrow> gauge d2 \<Longrightarrow> gauge (\<lambda>x. (d1 x) \<inter> (d2 x))"
   643   unfolding gauge_def by auto 
   644 
   645 lemma gauge_inters:
   646   assumes "finite s" "\<forall>d\<in>s. gauge (f d)"
   647   shows "gauge(\<lambda>x. \<Inter> {f d x | d. d \<in> s})"
   648 proof -
   649   have *:"\<And>x. {f d x |d. d \<in> s} = (\<lambda>d. f d x) ` s" by auto
   650   show ?thesis
   651     unfolding gauge_def unfolding * 
   652     using assms unfolding Ball_def Inter_iff mem_Collect_eq gauge_def by auto
   653 qed
   654 
   655 lemma gauge_existence_lemma: "(\<forall>x. \<exists>d::real. p x \<longrightarrow> 0 < d \<and> q d x) \<longleftrightarrow> (\<forall>x. \<exists>d>0. p x \<longrightarrow> q d x)"
   656   by(meson zero_less_one)
   657 
   658 
   659 subsection {* Divisions. *}
   660 
   661 definition division_of (infixl "division'_of" 40) where
   662   "s division_of i \<equiv>
   663         finite s \<and>
   664         (\<forall>k\<in>s. k \<subseteq> i \<and> k \<noteq> {} \<and> (\<exists>a b. k = {a..b})) \<and>
   665         (\<forall>k1\<in>s. \<forall>k2\<in>s. k1 \<noteq> k2 \<longrightarrow> interior(k1) \<inter> interior(k2) = {}) \<and>
   666         (\<Union>s = i)"
   667 
   668 lemma division_ofD[dest]:
   669   assumes "s division_of i"
   670   shows "finite s" "\<And>k. k\<in>s \<Longrightarrow> k \<subseteq> i" "\<And>k. k\<in>s \<Longrightarrow>  k \<noteq> {}" "\<And>k. k\<in>s \<Longrightarrow> (\<exists>a b. k = {a..b})"
   671     "\<And>k1 k2. \<lbrakk>k1\<in>s; k2\<in>s; k1 \<noteq> k2\<rbrakk> \<Longrightarrow> interior(k1) \<inter> interior(k2) = {}" "\<Union>s = i"
   672   using assms unfolding division_of_def by auto
   673 
   674 lemma division_ofI:
   675   assumes "finite s" "\<And>k. k\<in>s \<Longrightarrow> k \<subseteq> i" "\<And>k. k\<in>s \<Longrightarrow>  k \<noteq> {}" "\<And>k. k\<in>s \<Longrightarrow> (\<exists>a b. k = {a..b})"
   676     "\<And>k1 k2. \<lbrakk>k1\<in>s; k2\<in>s; k1 \<noteq> k2\<rbrakk> \<Longrightarrow> interior(k1) \<inter> interior(k2) = {}" "\<Union>s = i"
   677   shows "s division_of i" using assms unfolding division_of_def by auto
   678 
   679 lemma division_of_finite: "s division_of i \<Longrightarrow> finite s"
   680   unfolding division_of_def by auto
   681 
   682 lemma division_of_self[intro]: "{a..b} \<noteq> {} \<Longrightarrow> {{a..b}} division_of {a..b}"
   683   unfolding division_of_def by auto
   684 
   685 lemma division_of_trivial[simp]: "s division_of {} \<longleftrightarrow> s = {}" unfolding division_of_def by auto 
   686 
   687 lemma division_of_sing[simp]:
   688   "s division_of {a..a::'a::ordered_euclidean_space} \<longleftrightarrow> s = {{a..a}}" (is "?l = ?r")
   689 proof
   690   assume ?r
   691   moreover {
   692     assume "s = {{a}}"
   693     moreover fix k assume "k\<in>s" 
   694     ultimately have"\<exists>x y. k = {x..y}"
   695       apply (rule_tac x=a in exI)+ unfolding interval_sing by auto
   696   }
   697   ultimately show ?l unfolding division_of_def interval_sing by auto
   698 next
   699   assume ?l
   700   note as=conjunctD4[OF this[unfolded division_of_def interval_sing]]
   701   { fix x assume x:"x\<in>s" have "x={a}" using as(2)[rule_format,OF x] by auto }
   702   moreover have "s \<noteq> {}" using as(4) by auto
   703   ultimately show ?r unfolding interval_sing by auto
   704 qed
   705 
   706 lemma elementary_empty: obtains p where "p division_of {}"
   707   unfolding division_of_trivial by auto
   708 
   709 lemma elementary_interval: obtains p where "p division_of {a..b}"
   710   by (metis division_of_trivial division_of_self)
   711 
   712 lemma division_contains: "s division_of i \<Longrightarrow> \<forall>x\<in>i. \<exists>k\<in>s. x \<in> k"
   713   unfolding division_of_def by auto
   714 
   715 lemma forall_in_division:
   716  "d division_of i \<Longrightarrow> ((\<forall>x\<in>d. P x) \<longleftrightarrow> (\<forall>a b. {a..b} \<in> d \<longrightarrow> P {a..b}))"
   717   unfolding division_of_def by fastforce
   718 
   719 lemma division_of_subset: assumes "p division_of (\<Union>p)" "q \<subseteq> p" shows "q division_of (\<Union>q)"
   720   apply (rule division_ofI)
   721 proof -
   722   note as=division_ofD[OF assms(1)]
   723   show "finite q"
   724     apply (rule finite_subset)
   725     using as(1) assms(2) apply auto
   726     done
   727   { fix k
   728     assume "k \<in> q"
   729     hence kp:"k\<in>p" using assms(2) by auto
   730     show "k\<subseteq>\<Union>q" using `k \<in> q` by auto
   731     show "\<exists>a b. k = {a..b}" using as(4)[OF kp]
   732       by auto show "k \<noteq> {}" using as(3)[OF kp] by auto }
   733   fix k1 k2
   734   assume "k1 \<in> q" "k2 \<in> q" "k1 \<noteq> k2"
   735   hence *: "k1\<in>p" "k2\<in>p" "k1\<noteq>k2" using assms(2) by auto
   736   show "interior k1 \<inter> interior k2 = {}" using as(5)[OF *] by auto
   737 qed auto
   738 
   739 lemma division_of_union_self[intro]: "p division_of s \<Longrightarrow> p division_of (\<Union>p)"
   740   unfolding division_of_def by auto
   741 
   742 lemma division_of_content_0:
   743   assumes "content {a..b} = 0" "d division_of {a..b}"
   744   shows "\<forall>k\<in>d. content k = 0"
   745   unfolding forall_in_division[OF assms(2)]
   746   apply(rule,rule,rule)
   747   apply(drule division_ofD(2)[OF assms(2)])
   748   apply(drule content_subset) unfolding assms(1)
   749 proof -
   750   case goal1
   751   thus ?case using content_pos_le[of a b] by auto
   752 qed
   753 
   754 lemma division_inter:
   755   assumes "p1 division_of s1" "p2 division_of (s2::('a::ordered_euclidean_space) set)"
   756   shows "{k1 \<inter> k2 | k1 k2 .k1 \<in> p1 \<and> k2 \<in> p2 \<and> k1 \<inter> k2 \<noteq> {}} division_of (s1 \<inter> s2)"
   757   (is "?A' division_of _")
   758 proof -
   759   let ?A = "{s. s \<in>  (\<lambda>(k1,k2). k1 \<inter> k2) ` (p1 \<times> p2) \<and> s \<noteq> {}}"
   760   have *:"?A' = ?A" by auto
   761   show ?thesis unfolding *
   762   proof (rule division_ofI)
   763     have "?A \<subseteq> (\<lambda>(x, y). x \<inter> y) ` (p1 \<times> p2)" by auto
   764     moreover have "finite (p1 \<times> p2)" using assms unfolding division_of_def by auto
   765     ultimately show "finite ?A" by auto
   766     have *:"\<And>s. \<Union>{x\<in>s. x \<noteq> {}} = \<Union>s" by auto
   767     show "\<Union>?A = s1 \<inter> s2"
   768       apply (rule set_eqI)
   769       unfolding * and Union_image_eq UN_iff
   770       using division_ofD(6)[OF assms(1)] and division_ofD(6)[OF assms(2)]
   771       apply auto
   772       done
   773     { fix k
   774       assume "k\<in>?A"
   775       then obtain k1 k2 where k: "k = k1 \<inter> k2" "k1\<in>p1" "k2\<in>p2" "k\<noteq>{}" by auto
   776       thus "k \<noteq> {}" by auto
   777       show "k \<subseteq> s1 \<inter> s2"
   778         using division_ofD(2)[OF assms(1) k(2)] and division_ofD(2)[OF assms(2) k(3)]
   779         unfolding k by auto
   780       guess a1 using division_ofD(4)[OF assms(1) k(2)] ..
   781       then guess b1 .. note ab1=this
   782       guess a2 using division_ofD(4)[OF assms(2) k(3)] ..
   783       then guess b2 .. note ab2=this
   784       show "\<exists>a b. k = {a..b}"
   785         unfolding k ab1 ab2 unfolding inter_interval by auto }
   786     fix k1 k2
   787     assume "k1\<in>?A"
   788     then obtain x1 y1 where k1:"k1 = x1 \<inter> y1" "x1\<in>p1" "y1\<in>p2" "k1\<noteq>{}" by auto
   789     assume "k2\<in>?A"
   790     then obtain x2 y2 where k2:"k2 = x2 \<inter> y2" "x2\<in>p1" "y2\<in>p2" "k2\<noteq>{}" by auto
   791     assume "k1 \<noteq> k2"
   792     hence th:"x1\<noteq>x2 \<or> y1\<noteq>y2" unfolding k1 k2 by auto
   793     have *:"(interior x1 \<inter> interior x2 = {} \<or> interior y1 \<inter> interior y2 = {}) \<Longrightarrow>
   794       interior(x1 \<inter> y1) \<subseteq> interior(x1) \<Longrightarrow> interior(x1 \<inter> y1) \<subseteq> interior(y1) \<Longrightarrow>
   795       interior(x2 \<inter> y2) \<subseteq> interior(x2) \<Longrightarrow> interior(x2 \<inter> y2) \<subseteq> interior(y2)
   796       \<Longrightarrow> interior(x1 \<inter> y1) \<inter> interior(x2 \<inter> y2) = {}" by auto
   797     show "interior k1 \<inter> interior k2 = {}"
   798       unfolding k1 k2
   799       apply (rule *)
   800       defer
   801       apply (rule_tac[1-4] interior_mono)
   802       using division_ofD(5)[OF assms(1) k1(2) k2(2)]
   803       using division_ofD(5)[OF assms(2) k1(3) k2(3)]
   804       using th apply auto done
   805   qed
   806 qed
   807 
   808 lemma division_inter_1:
   809   assumes "d division_of i" "{a..b::'a::ordered_euclidean_space} \<subseteq> i"
   810   shows "{ {a..b} \<inter> k |k. k \<in> d \<and> {a..b} \<inter> k \<noteq> {} } division_of {a..b}"
   811 proof (cases "{a..b} = {}")
   812   case True
   813   show ?thesis unfolding True and division_of_trivial by auto
   814 next
   815   case False
   816   have *: "{a..b} \<inter> i = {a..b}" using assms(2) by auto
   817   show ?thesis using division_inter[OF division_of_self[OF False] assms(1)] unfolding * by auto
   818 qed
   819 
   820 lemma elementary_inter:
   821   assumes "p1 division_of s" "p2 division_of (t::('a::ordered_euclidean_space) set)"
   822   shows "\<exists>p. p division_of (s \<inter> t)"
   823   by (rule, rule division_inter[OF assms])
   824 
   825 lemma elementary_inters:
   826   assumes "finite f" "f\<noteq>{}" "\<forall>s\<in>f. \<exists>p. p division_of (s::('a::ordered_euclidean_space) set)"
   827   shows "\<exists>p. p division_of (\<Inter> f)"
   828   using assms
   829 proof (induct f rule: finite_induct)
   830   case (insert x f)
   831   show ?case
   832   proof (cases "f = {}")
   833     case True
   834     thus ?thesis unfolding True using insert by auto
   835   next
   836     case False
   837     guess p using insert(3)[OF False insert(5)[unfolded ball_simps,THEN conjunct2]] ..
   838     moreover guess px using insert(5)[rule_format,OF insertI1] ..
   839     ultimately show ?thesis
   840       unfolding Inter_insert
   841       apply (rule_tac elementary_inter)
   842       apply assumption
   843       apply assumption
   844       done
   845   qed
   846 qed auto
   847 
   848 lemma division_disjoint_union:
   849   assumes "p1 division_of s1" "p2 division_of s2" "interior s1 \<inter> interior s2 = {}"
   850   shows "(p1 \<union> p2) division_of (s1 \<union> s2)" proof(rule division_ofI) 
   851   note d1 = division_ofD[OF assms(1)] and d2 = division_ofD[OF assms(2)]
   852   show "finite (p1 \<union> p2)" using d1(1) d2(1) by auto
   853   show "\<Union>(p1 \<union> p2) = s1 \<union> s2" using d1(6) d2(6) by auto
   854   { fix k1 k2 assume as:"k1 \<in> p1 \<union> p2" "k2 \<in> p1 \<union> p2" "k1 \<noteq> k2" moreover let ?g="interior k1 \<inter> interior k2 = {}"
   855   { assume as:"k1\<in>p1" "k2\<in>p2" have ?g using interior_mono[OF d1(2)[OF as(1)]] interior_mono[OF d2(2)[OF as(2)]]
   856       using assms(3) by blast } moreover
   857   { assume as:"k1\<in>p2" "k2\<in>p1" have ?g using interior_mono[OF d1(2)[OF as(2)]] interior_mono[OF d2(2)[OF as(1)]]
   858       using assms(3) by blast} ultimately
   859   show ?g using d1(5)[OF _ _ as(3)] and d2(5)[OF _ _ as(3)] by auto }
   860   fix k assume k:"k \<in> p1 \<union> p2"  show "k \<subseteq> s1 \<union> s2" using k d1(2) d2(2) by auto
   861   show "k \<noteq> {}" using k d1(3) d2(3) by auto show "\<exists>a b. k = {a..b}" using k d1(4) d2(4) by auto qed
   862 
   863 lemma partial_division_extend_1:
   864   assumes "{c..d} \<subseteq> {a..b::'a::ordered_euclidean_space}" "{c..d} \<noteq> {}"
   865   obtains p where "p division_of {a..b}" "{c..d} \<in> p"
   866 proof- def n \<equiv> "DIM('a)" have n:"1 \<le> n" "0 < n" "n \<noteq> 0" unfolding n_def using DIM_positive[where 'a='a] by auto
   867   guess \<pi> using ex_bij_betw_nat_finite_1[OF finite_lessThan[of "DIM('a)"]] .. note \<pi>=this
   868   def \<pi>' \<equiv> "inv_into {1..n} \<pi>"
   869   have \<pi>':"bij_betw \<pi>' {..<DIM('a)} {1..n}" using bij_betw_inv_into[OF \<pi>] unfolding \<pi>'_def n_def by auto
   870   hence \<pi>'_i:"\<And>i. i<DIM('a) \<Longrightarrow> \<pi>' i \<in> {1..n}" unfolding bij_betw_def by auto 
   871   have \<pi>_i:"\<And>i. i\<in>{1..n} \<Longrightarrow> \<pi> i <DIM('a)" using \<pi> unfolding bij_betw_def n_def by auto 
   872   have \<pi>_\<pi>'[simp]:"\<And>i. i<DIM('a) \<Longrightarrow> \<pi> (\<pi>' i) = i" unfolding \<pi>'_def
   873     apply(rule f_inv_into_f) unfolding n_def using \<pi> unfolding bij_betw_def by auto
   874   have \<pi>'_\<pi>[simp]:"\<And>i. i\<in>{1..n} \<Longrightarrow> \<pi>' (\<pi> i) = i" unfolding \<pi>'_def apply(rule inv_into_f_eq)
   875     using \<pi> unfolding n_def bij_betw_def by auto
   876   have "{c..d} \<noteq> {}" using assms by auto
   877   let ?p1 = "\<lambda>l. {(\<chi>\<chi> i. if \<pi>' i < l then c$$i else a$$i)::'a .. (\<chi>\<chi> i. if \<pi>' i < l then d$$i else if \<pi>' i = l then c$$\<pi> l else b$$i)}"
   878   let ?p2 = "\<lambda>l. {(\<chi>\<chi> i. if \<pi>' i < l then c$$i else if \<pi>' i = l then d$$\<pi> l else a$$i)::'a .. (\<chi>\<chi> i. if \<pi>' i < l then d$$i else b$$i)}"
   879   let ?p =  "{?p1 l |l. l \<in> {1..n+1}} \<union> {?p2 l |l. l \<in> {1..n+1}}"
   880   have abcd:"\<And>i. i<DIM('a) \<Longrightarrow> a $$ i \<le> c $$ i \<and> c$$i \<le> d$$i \<and> d $$ i \<le> b $$ i" using assms
   881     unfolding subset_interval interval_eq_empty by auto
   882   show ?thesis apply(rule that[of ?p]) apply(rule division_ofI)
   883   proof- have "\<And>i. i<DIM('a) \<Longrightarrow> \<pi>' i < Suc n"
   884     proof(rule ccontr,unfold not_less) fix i assume i:"i<DIM('a)" and "Suc n \<le> \<pi>' i"
   885       hence "\<pi>' i \<notin> {1..n}" by auto thus False using \<pi>' i unfolding bij_betw_def by auto
   886     qed hence "c = (\<chi>\<chi> i. if \<pi>' i < Suc n then c $$ i else a $$ i)"
   887         "d = (\<chi>\<chi> i. if \<pi>' i < Suc n then d $$ i else if \<pi>' i = n + 1 then c $$ \<pi> (n + 1) else b $$ i)"
   888       unfolding euclidean_eq[where 'a='a] using \<pi>' unfolding bij_betw_def by auto
   889     thus cdp:"{c..d} \<in> ?p" apply-apply(rule UnI1) unfolding mem_Collect_eq apply(rule_tac x="n + 1" in exI) by auto
   890     have "\<And>l. l\<in>{1..n+1} \<Longrightarrow> ?p1 l \<subseteq> {a..b}"  "\<And>l. l\<in>{1..n+1} \<Longrightarrow> ?p2 l \<subseteq> {a..b}"
   891       unfolding subset_eq apply(rule_tac[!] ballI,rule_tac[!] ccontr)
   892     proof- fix l assume l:"l\<in>{1..n+1}" fix x assume "x\<notin>{a..b}"
   893       then guess i unfolding mem_interval not_all not_imp .. note i=conjunctD2[OF this]
   894       show "x \<in> ?p1 l \<Longrightarrow> False" "x \<in> ?p2 l \<Longrightarrow> False" unfolding mem_interval apply(erule_tac[!] x=i in allE)
   895         apply(case_tac[!] "\<pi>' i < l", case_tac[!] "\<pi>' i = l") using abcd[of i] i by auto 
   896     qed moreover have "\<And>x. x \<in> {a..b} \<Longrightarrow> x \<in> \<Union>?p"
   897     proof- fix x assume x:"x\<in>{a..b}"
   898       { presume "x\<notin>{c..d} \<Longrightarrow> x \<in> \<Union>?p" thus "x \<in> \<Union>?p" using cdp by blast }
   899       let ?M = "{i. i\<in>{1..n+1} \<and> \<not> (c $$ \<pi> i \<le> x $$ \<pi> i \<and> x $$ \<pi> i \<le> d $$ \<pi> i)}"
   900       assume "x\<notin>{c..d}" then guess i0 unfolding mem_interval not_all not_imp ..
   901       hence "\<pi>' i0 \<in> ?M" using \<pi>' unfolding bij_betw_def by(auto intro!:le_SucI)
   902       hence M:"finite ?M" "?M \<noteq> {}" by auto
   903       def l \<equiv> "Min ?M" note l = Min_less_iff[OF M,unfolded l_def[symmetric]] Min_in[OF M,unfolded mem_Collect_eq l_def[symmetric]]
   904         Min_gr_iff[OF M,unfolded l_def[symmetric]]
   905       have "x\<in>?p1 l \<or> x\<in>?p2 l" using l(2)[THEN conjunct2] unfolding de_Morgan_conj not_le
   906         apply- apply(erule disjE) apply(rule disjI1) defer apply(rule disjI2)
   907       proof- assume as:"x $$ \<pi> l < c $$ \<pi> l"
   908         show "x \<in> ?p1 l" unfolding mem_interval apply safe unfolding euclidean_lambda_beta'
   909         proof- case goal1 have "\<pi>' i \<in> {1..n}" using \<pi>' unfolding bij_betw_def not_le using goal1 by auto
   910           thus ?case using as x[unfolded mem_interval,rule_format,of i]
   911             apply auto using l(3)[of "\<pi>' i"] using goal1 by(auto elim!:ballE[where x="\<pi>' i"])
   912         next case goal2 have "\<pi>' i \<in> {1..n}" using \<pi>' unfolding bij_betw_def not_le using goal2 by auto
   913           thus ?case using as x[unfolded mem_interval,rule_format,of i]
   914             apply auto using l(3)[of "\<pi>' i"] using goal2 by(auto elim!:ballE[where x="\<pi>' i"])
   915         qed
   916       next assume as:"x $$ \<pi> l > d $$ \<pi> l"
   917         show "x \<in> ?p2 l" unfolding mem_interval apply safe unfolding euclidean_lambda_beta'
   918         proof- fix i assume i:"i<DIM('a)"
   919           have "\<pi>' i \<in> {1..n}" using \<pi>' unfolding bij_betw_def not_le using i by auto
   920           thus "(if \<pi>' i < l then c $$ i else if \<pi>' i = l then d $$ \<pi> l else a $$ i) \<le> x $$ i"
   921             "x $$ i \<le> (if \<pi>' i < l then d $$ i else b $$ i)"
   922             using as x[unfolded mem_interval,rule_format,of i]
   923             apply auto using l(3)[of "\<pi>' i"] i by(auto elim!:ballE[where x="\<pi>' i"])
   924         qed qed
   925       thus "x \<in> \<Union>?p" using l(2) by blast 
   926     qed ultimately show "\<Union>?p = {a..b}" apply-apply(rule) defer apply(rule) by(assumption,blast)
   927     
   928     show "finite ?p" by auto
   929     fix k assume k:"k\<in>?p" then obtain l where l:"k = ?p1 l \<or> k = ?p2 l" "l \<in> {1..n + 1}" by auto
   930     show "k\<subseteq>{a..b}" apply(rule,unfold mem_interval,rule,rule) 
   931     proof fix i x assume i:"i<DIM('a)" assume "x \<in> k" moreover have "\<pi>' i < l \<or> \<pi>' i = l \<or> \<pi>' i > l" by auto
   932       ultimately show "a$$i \<le> x$$i" "x$$i \<le> b$$i" using abcd[of i] using l using i
   933         by(auto elim!:allE[where x=i] simp add:eucl_le[where 'a='a]) (* FIXME: SLOW *)
   934     qed have "\<And>l. ?p1 l \<noteq> {}" "\<And>l. ?p2 l \<noteq> {}" unfolding interval_eq_empty not_ex apply(rule_tac[!] allI)
   935     proof- case goal1 thus ?case using abcd[of x] by auto
   936     next   case goal2 thus ?case using abcd[of x] by auto
   937     qed thus "k \<noteq> {}" using k by auto
   938     show "\<exists>a b. k = {a..b}" using k by auto
   939     fix k' assume k':"k' \<in> ?p" "k \<noteq> k'" then obtain l' where l':"k' = ?p1 l' \<or> k' = ?p2 l'" "l' \<in> {1..n + 1}" by auto
   940     { fix k k' l l'
   941       assume k:"k\<in>?p" and l:"k = ?p1 l \<or> k = ?p2 l" "l \<in> {1..n + 1}" 
   942       assume k':"k' \<in> ?p" "k \<noteq> k'" and  l':"k' = ?p1 l' \<or> k' = ?p2 l'" "l' \<in> {1..n + 1}" 
   943       assume "l \<le> l'" fix x
   944       have "x \<notin> interior k \<inter> interior k'" 
   945       proof(rule,cases "l' = n+1") assume x:"x \<in> interior k \<inter> interior k'"
   946         case True hence "\<And>i. i<DIM('a) \<Longrightarrow> \<pi>' i < l'" using \<pi>'_i using l' by(auto simp add:less_Suc_eq_le)
   947         hence *:"\<And> P Q. (\<chi>\<chi> i. if \<pi>' i < l' then P i else Q i) = ((\<chi>\<chi> i. P i)::'a)" apply-apply(subst euclidean_eq) by auto
   948         hence k':"k' = {c..d}" using l'(1) unfolding * by auto
   949         have ln:"l < n + 1" 
   950         proof(rule ccontr) case goal1 hence l2:"l = n+1" using l by auto
   951           hence "\<And>i. i<DIM('a) \<Longrightarrow> \<pi>' i < l" using \<pi>'_i by(auto simp add:less_Suc_eq_le)
   952           hence *:"\<And> P Q. (\<chi>\<chi> i. if \<pi>' i < l then P i else Q i) = ((\<chi>\<chi> i. P i)::'a)" apply-apply(subst euclidean_eq) by auto
   953           hence "k = {c..d}" using l(1) \<pi>'_i unfolding * by(auto)
   954           thus False using `k\<noteq>k'` k' by auto
   955         qed have **:"\<pi>' (\<pi> l) = l" using \<pi>'_\<pi>[of l] using l ln by auto
   956         have "x $$ \<pi> l < c $$ \<pi> l \<or> d $$ \<pi> l < x $$ \<pi> l" using l(1) apply-
   957         proof(erule disjE)
   958           assume as:"k = ?p1 l" note * = conjunct1[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
   959           show ?thesis using *[of "\<pi> l"] using ln l(2) using \<pi>_i[of l] by(auto simp add:** not_less)
   960         next assume as:"k = ?p2 l" note * = conjunct1[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
   961           show ?thesis using *[of "\<pi> l"] using ln l(2) using \<pi>_i[of l] unfolding ** by auto
   962         qed thus False using x unfolding k' unfolding Int_iff interior_closed_interval mem_interval
   963           by(auto elim!:allE[where x="\<pi> l"])
   964       next case False hence "l < n + 1" using l'(2) using `l\<le>l'` by auto
   965         hence ln:"l \<in> {1..n}" "l' \<in> {1..n}" using l l' False by auto
   966         note \<pi>_l = \<pi>'_\<pi>[OF ln(1)] \<pi>'_\<pi>[OF ln(2)]
   967         assume x:"x \<in> interior k \<inter> interior k'"
   968         show False using l(1) l'(1) apply-
   969         proof(erule_tac[!] disjE)+
   970           assume as:"k = ?p1 l" "k' = ?p1 l'"
   971           note * = conjunctD2[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
   972           have "l \<noteq> l'" using k'(2)[unfolded as] by auto
   973           thus False using *[of "\<pi> l'"] *[of "\<pi> l"] ln using \<pi>_i[OF ln(1)] \<pi>_i[OF ln(2)] apply(cases "l<l'")
   974             by(auto simp add:euclidean_lambda_beta' \<pi>_l \<pi>_i n_def)
   975         next assume as:"k = ?p2 l" "k' = ?p2 l'"
   976           note * = conjunctD2[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
   977           have "l \<noteq> l'" apply(rule) using k'(2)[unfolded as] by auto
   978           thus False using *[of "\<pi> l"] *[of "\<pi> l'"]  `l \<le> l'` ln by(auto simp add:euclidean_lambda_beta' \<pi>_l \<pi>_i n_def)
   979         next assume as:"k = ?p1 l" "k' = ?p2 l'"
   980           note * = conjunctD2[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
   981           show False using abcd[of "\<pi> l'"] using *[of "\<pi> l"] *[of "\<pi> l'"]  `l \<le> l'` ln apply(cases "l=l'")
   982             by(auto simp add:euclidean_lambda_beta' \<pi>_l \<pi>_i n_def)
   983         next assume as:"k = ?p2 l" "k' = ?p1 l'"
   984           note * = conjunctD2[OF x[unfolded as Int_iff interior_closed_interval mem_interval],rule_format]
   985           show False using *[of "\<pi> l"] *[of "\<pi> l'"] ln `l \<le> l'` apply(cases "l=l'") using abcd[of "\<pi> l'"] 
   986             by(auto simp add:euclidean_lambda_beta' \<pi>_l \<pi>_i n_def)
   987         qed qed } 
   988     from this[OF k l k' l'] this[OF k'(1) l' k _ l] have "\<And>x. x \<notin> interior k \<inter> interior k'"
   989       apply - apply(cases "l' \<le> l") using k'(2) by auto            
   990     thus "interior k \<inter> interior k' = {}" by auto        
   991 qed qed
   992 
   993 lemma partial_division_extend_interval: assumes "p division_of (\<Union>p)" "(\<Union>p) \<subseteq> {a..b}"
   994   obtains q where "p \<subseteq> q" "q division_of {a..b::'a::ordered_euclidean_space}" proof(cases "p = {}")
   995   case True guess q apply(rule elementary_interval[of a b]) .
   996   thus ?thesis apply- apply(rule that[of q]) unfolding True by auto next
   997   case False note p = division_ofD[OF assms(1)]
   998   have *:"\<forall>k\<in>p. \<exists>q. q division_of {a..b} \<and> k\<in>q" proof case goal1
   999     guess c using p(4)[OF goal1] .. then guess d .. note "cd" = this
  1000     have *:"{c..d} \<subseteq> {a..b}" "{c..d} \<noteq> {}" using p(2,3)[OF goal1, unfolded "cd"] using assms(2) by auto
  1001     guess q apply(rule partial_division_extend_1[OF *]) . thus ?case unfolding "cd" by auto qed
  1002   guess q using bchoice[OF *] .. note q = conjunctD2[OF this[rule_format]]
  1003   have "\<And>x. x\<in>p \<Longrightarrow> \<exists>d. d division_of \<Union>(q x - {x})" apply(rule,rule_tac p="q x" in division_of_subset) proof-
  1004     fix x assume x:"x\<in>p" show "q x division_of \<Union>q x" apply-apply(rule division_ofI)
  1005       using division_ofD[OF q(1)[OF x]] by auto show "q x - {x} \<subseteq> q x" by auto qed
  1006   hence "\<exists>d. d division_of \<Inter> ((\<lambda>i. \<Union>(q i - {i})) ` p)" apply- apply(rule elementary_inters)
  1007     apply(rule finite_imageI[OF p(1)]) unfolding image_is_empty apply(rule False) by auto
  1008   then guess d .. note d = this
  1009   show ?thesis apply(rule that[of "d \<union> p"]) proof-
  1010     have *:"\<And>s f t. s \<noteq> {} \<Longrightarrow> (\<forall>i\<in>s. f i \<union> i = t) \<Longrightarrow> t = \<Inter> (f ` s) \<union> (\<Union>s)" by auto
  1011     have *:"{a..b} = \<Inter> (\<lambda>i. \<Union>(q i - {i})) ` p \<union> \<Union>p" apply(rule *[OF False]) proof fix i assume i:"i\<in>p"
  1012       show "\<Union>(q i - {i}) \<union> i = {a..b}" using division_ofD(6)[OF q(1)[OF i]] using q(2)[OF i] by auto qed
  1013     show "d \<union> p division_of {a..b}" unfolding * apply(rule division_disjoint_union[OF d assms(1)])
  1014       apply(rule inter_interior_unions_intervals) apply(rule p open_interior ballI)+ proof(assumption,rule)
  1015       fix k assume k:"k\<in>p" have *:"\<And>u t s. u \<subseteq> s \<Longrightarrow> s \<inter> t = {} \<Longrightarrow> u \<inter> t = {}" by auto
  1016       show "interior (\<Inter>(\<lambda>i. \<Union>(q i - {i})) ` p) \<inter> interior k = {}" apply(rule *[of _ "interior (\<Union>(q k - {k}))"])
  1017         defer apply(subst Int_commute) apply(rule inter_interior_unions_intervals) proof- note qk=division_ofD[OF q(1)[OF k]]
  1018         show "finite (q k - {k})" "open (interior k)"  "\<forall>t\<in>q k - {k}. \<exists>a b. t = {a..b}" using qk by auto
  1019         show "\<forall>t\<in>q k - {k}. interior k \<inter> interior t = {}" using qk(5) using q(2)[OF k] by auto
  1020         have *:"\<And>x s. x \<in> s \<Longrightarrow> \<Inter>s \<subseteq> x" by auto show "interior (\<Inter>(\<lambda>i. \<Union>(q i - {i})) ` p) \<subseteq> interior (\<Union>(q k - {k}))"
  1021           apply(rule interior_mono *)+ using k by auto qed qed qed auto qed
  1022 
  1023 lemma elementary_bounded[dest]: "p division_of s \<Longrightarrow> bounded (s::('a::ordered_euclidean_space) set)"
  1024   unfolding division_of_def by(metis bounded_Union bounded_interval) 
  1025 
  1026 lemma elementary_subset_interval: "p division_of s \<Longrightarrow> \<exists>a b. s \<subseteq> {a..b::'a::ordered_euclidean_space}"
  1027   by(meson elementary_bounded bounded_subset_closed_interval)
  1028 
  1029 lemma division_union_intervals_exists: assumes "{a..b::'a::ordered_euclidean_space} \<noteq> {}"
  1030   obtains p where "(insert {a..b} p) division_of ({a..b} \<union> {c..d})" proof(cases "{c..d} = {}")
  1031   case True show ?thesis apply(rule that[of "{}"]) unfolding True using assms by auto next
  1032   case False note false=this show ?thesis proof(cases "{a..b} \<inter> {c..d} = {}")
  1033   have *:"\<And>a b. {a,b} = {a} \<union> {b}" by auto
  1034   case True show ?thesis apply(rule that[of "{{c..d}}"]) unfolding * apply(rule division_disjoint_union)
  1035     using false True assms using interior_subset by auto next
  1036   case False obtain u v where uv:"{a..b} \<inter> {c..d} = {u..v}" unfolding inter_interval by auto
  1037   have *:"{u..v} \<subseteq> {c..d}" using uv by auto
  1038   guess p apply(rule partial_division_extend_1[OF * False[unfolded uv]]) . note p=this division_ofD[OF this(1)]
  1039   have *:"{a..b} \<union> {c..d} = {a..b} \<union> \<Union>(p - {{u..v}})" "\<And>x s. insert x s = {x} \<union> s" using p(8) unfolding uv[THEN sym] by auto
  1040   show thesis apply(rule that[of "p - {{u..v}}"]) unfolding *(1) apply(subst *(2)) apply(rule division_disjoint_union)
  1041     apply(rule,rule assms) apply(rule division_of_subset[of p]) apply(rule division_of_union_self[OF p(1)]) defer
  1042     unfolding interior_inter[THEN sym] proof-
  1043     have *:"\<And>cd p uv ab. p \<subseteq> cd \<Longrightarrow> ab \<inter> cd = uv \<Longrightarrow> ab \<inter> p = uv \<inter> p" by auto
  1044     have "interior ({a..b} \<inter> \<Union>(p - {{u..v}})) = interior({u..v} \<inter> \<Union>(p - {{u..v}}))" 
  1045       apply(rule arg_cong[of _ _ interior]) apply(rule *[OF _ uv]) using p(8) by auto
  1046     also have "\<dots> = {}" unfolding interior_inter apply(rule inter_interior_unions_intervals) using p(6) p(7)[OF p(2)] p(3) by auto
  1047     finally show "interior ({a..b} \<inter> \<Union>(p - {{u..v}})) = {}" by assumption qed auto qed qed
  1048 
  1049 lemma division_of_unions: assumes "finite f"  "\<And>p. p\<in>f \<Longrightarrow> p division_of (\<Union>p)"
  1050   "\<And>k1 k2. \<lbrakk>k1 \<in> \<Union>f; k2 \<in> \<Union>f; k1 \<noteq> k2\<rbrakk> \<Longrightarrow> interior k1 \<inter> interior k2 = {}"
  1051   shows "\<Union>f division_of \<Union>\<Union>f" apply(rule division_ofI) prefer 5 apply(rule assms(3)|assumption)+
  1052   apply(rule finite_Union assms(1))+ prefer 3 apply(erule UnionE) apply(rule_tac s=X in division_ofD(3)[OF assms(2)])
  1053   using division_ofD[OF assms(2)] by auto
  1054   
  1055 lemma elementary_union_interval: assumes "p division_of \<Union>p"
  1056   obtains q where "q division_of ({a..b::'a::ordered_euclidean_space} \<union> \<Union>p)" proof-
  1057   note assm=division_ofD[OF assms]
  1058   have lem1:"\<And>f s. \<Union>\<Union> (f ` s) = \<Union>(\<lambda>x.\<Union>(f x)) ` s" by auto
  1059   have lem2:"\<And>f s. f \<noteq> {} \<Longrightarrow> \<Union>{s \<union> t |t. t \<in> f} = s \<union> \<Union>f" by auto
  1060 { presume "p={} \<Longrightarrow> thesis" "{a..b} = {} \<Longrightarrow> thesis" "{a..b} \<noteq> {} \<Longrightarrow> interior {a..b} = {} \<Longrightarrow> thesis"
  1061     "p\<noteq>{} \<Longrightarrow> interior {a..b}\<noteq>{} \<Longrightarrow> {a..b} \<noteq> {} \<Longrightarrow> thesis"
  1062   thus thesis by auto
  1063 next assume as:"p={}" guess p apply(rule elementary_interval[of a b]) .
  1064   thus thesis apply(rule_tac that[of p]) unfolding as by auto 
  1065 next assume as:"{a..b}={}" show thesis apply(rule that) unfolding as using assms by auto
  1066 next assume as:"interior {a..b} = {}" "{a..b} \<noteq> {}"
  1067   show thesis apply(rule that[of "insert {a..b} p"],rule division_ofI)
  1068     unfolding finite_insert apply(rule assm(1)) unfolding Union_insert  
  1069     using assm(2-4) as apply- by(fastforce dest: assm(5))+
  1070 next assume as:"p \<noteq> {}" "interior {a..b} \<noteq> {}" "{a..b}\<noteq>{}"
  1071   have "\<forall>k\<in>p. \<exists>q. (insert {a..b} q) division_of ({a..b} \<union> k)" proof case goal1
  1072     from assm(4)[OF this] guess c .. then guess d ..
  1073     thus ?case apply-apply(rule division_union_intervals_exists[OF as(3),of c d]) by auto
  1074   qed from bchoice[OF this] guess q .. note q=division_ofD[OF this[rule_format]]
  1075   let ?D = "\<Union>{insert {a..b} (q k) | k. k \<in> p}"
  1076   show thesis apply(rule that[of "?D"]) proof(rule division_ofI)
  1077     have *:"{insert {a..b} (q k) |k. k \<in> p} = (\<lambda>k. insert {a..b} (q k)) ` p" by auto
  1078     show "finite ?D" apply(rule finite_Union) unfolding * apply(rule finite_imageI) using assm(1) q(1) by auto
  1079     show "\<Union>?D = {a..b} \<union> \<Union>p" unfolding * lem1 unfolding lem2[OF as(1), of "{a..b}",THEN sym]
  1080       using q(6) by auto
  1081     fix k assume k:"k\<in>?D" thus " k \<subseteq> {a..b} \<union> \<Union>p" using q(2) by auto
  1082     show "k \<noteq> {}" using q(3) k by auto show "\<exists>a b. k = {a..b}" using q(4) k by auto
  1083     fix k' assume k':"k'\<in>?D" "k\<noteq>k'"
  1084     obtain x  where x: "k \<in>insert {a..b} (q x)"  "x\<in>p"  using k  by auto
  1085     obtain x' where x':"k'\<in>insert {a..b} (q x')" "x'\<in>p" using k' by auto
  1086     show "interior k \<inter> interior k' = {}" proof(cases "x=x'")
  1087       case True show ?thesis apply(rule q(5)) using x x' k' unfolding True by auto
  1088     next case False 
  1089       { presume "k = {a..b} \<Longrightarrow> ?thesis" "k' = {a..b} \<Longrightarrow> ?thesis" 
  1090         "k \<noteq> {a..b} \<Longrightarrow> k' \<noteq> {a..b} \<Longrightarrow> ?thesis"
  1091         thus ?thesis by auto }
  1092       { assume as':"k  = {a..b}" show ?thesis apply(rule q(5)) using x' k'(2) unfolding as' by auto }
  1093       { assume as':"k' = {a..b}" show ?thesis apply(rule q(5)) using x  k'(2) unfolding as' by auto }
  1094       assume as':"k \<noteq> {a..b}" "k' \<noteq> {a..b}"
  1095       guess c using q(4)[OF x(2,1)] .. then guess d .. note c_d=this
  1096       have "interior k  \<inter> interior {a..b} = {}" apply(rule q(5)) using x  k'(2) using as' by auto
  1097       hence "interior k \<subseteq> interior x" apply-
  1098         apply(rule interior_subset_union_intervals[OF c_d _ as(2) q(2)[OF x(2,1)]]) by auto moreover
  1099       guess c using q(4)[OF x'(2,1)] .. then guess d .. note c_d=this
  1100       have "interior k' \<inter> interior {a..b} = {}" apply(rule q(5)) using x' k'(2) using as' by auto
  1101       hence "interior k' \<subseteq> interior x'" apply-
  1102         apply(rule interior_subset_union_intervals[OF c_d _ as(2) q(2)[OF x'(2,1)]]) by auto
  1103       ultimately show ?thesis using assm(5)[OF x(2) x'(2) False] by auto
  1104     qed qed } qed
  1105 
  1106 lemma elementary_unions_intervals:
  1107   assumes "finite f" "\<And>s. s \<in> f \<Longrightarrow> \<exists>a b. s = {a..b::'a::ordered_euclidean_space}"
  1108   obtains p where "p division_of (\<Union>f)" proof-
  1109   have "\<exists>p. p division_of (\<Union>f)" proof(induct_tac f rule:finite_subset_induct) 
  1110     show "\<exists>p. p division_of \<Union>{}" using elementary_empty by auto
  1111     fix x F assume as:"finite F" "x \<notin> F" "\<exists>p. p division_of \<Union>F" "x\<in>f"
  1112     from this(3) guess p .. note p=this
  1113     from assms(2)[OF as(4)] guess a .. then guess b .. note ab=this
  1114     have *:"\<Union>F = \<Union>p" using division_ofD[OF p] by auto
  1115     show "\<exists>p. p division_of \<Union>insert x F" using elementary_union_interval[OF p[unfolded *], of a b]
  1116       unfolding Union_insert ab * by auto
  1117   qed(insert assms,auto) thus ?thesis apply-apply(erule exE,rule that) by auto qed
  1118 
  1119 lemma elementary_union: assumes "ps division_of s" "pt division_of (t::('a::ordered_euclidean_space) set)"
  1120   obtains p where "p division_of (s \<union> t)"
  1121 proof- have "s \<union> t = \<Union>ps \<union> \<Union>pt" using assms unfolding division_of_def by auto
  1122   hence *:"\<Union>(ps \<union> pt) = s \<union> t" by auto
  1123   show ?thesis apply-apply(rule elementary_unions_intervals[of "ps\<union>pt"])
  1124     unfolding * prefer 3 apply(rule_tac p=p in that)
  1125     using assms[unfolded division_of_def] by auto qed
  1126 
  1127 lemma partial_division_extend: fixes t::"('a::ordered_euclidean_space) set"
  1128   assumes "p division_of s" "q division_of t" "s \<subseteq> t"
  1129   obtains r where "p \<subseteq> r" "r division_of t" proof-
  1130   note divp = division_ofD[OF assms(1)] and divq = division_ofD[OF assms(2)]
  1131   obtain a b where ab:"t\<subseteq>{a..b}" using elementary_subset_interval[OF assms(2)] by auto
  1132   guess r1 apply(rule partial_division_extend_interval) apply(rule assms(1)[unfolded divp(6)[THEN sym]])
  1133     apply(rule subset_trans) by(rule ab assms[unfolded divp(6)[THEN sym]])+  note r1 = this division_ofD[OF this(2)]
  1134   guess p' apply(rule elementary_unions_intervals[of "r1 - p"]) using r1(3,6) by auto 
  1135   then obtain r2 where r2:"r2 division_of (\<Union>(r1 - p)) \<inter> (\<Union>q)" 
  1136     apply- apply(drule elementary_inter[OF _ assms(2)[unfolded divq(6)[THEN sym]]]) by auto
  1137   { fix x assume x:"x\<in>t" "x\<notin>s"
  1138     hence "x\<in>\<Union>r1" unfolding r1 using ab by auto
  1139     then guess r unfolding Union_iff .. note r=this moreover
  1140     have "r \<notin> p" proof assume "r\<in>p" hence "x\<in>s" using divp(2) r by auto
  1141       thus False using x by auto qed
  1142     ultimately have "x\<in>\<Union>(r1 - p)" by auto }
  1143   hence *:"t = \<Union>p \<union> (\<Union>(r1 - p) \<inter> \<Union>q)" unfolding divp divq using assms(3) by auto
  1144   show ?thesis apply(rule that[of "p \<union> r2"]) unfolding * defer apply(rule division_disjoint_union)
  1145     unfolding divp(6) apply(rule assms r2)+
  1146   proof- have "interior s \<inter> interior (\<Union>(r1-p)) = {}"
  1147     proof(rule inter_interior_unions_intervals)
  1148       show "finite (r1 - p)" "open (interior s)" "\<forall>t\<in>r1-p. \<exists>a b. t = {a..b}" using r1 by auto
  1149       have *:"\<And>s. (\<And>x. x \<in> s \<Longrightarrow> False) \<Longrightarrow> s = {}" by auto
  1150       show "\<forall>t\<in>r1-p. interior s \<inter> interior t = {}" proof(rule)
  1151         fix m x assume as:"m\<in>r1-p"
  1152         have "interior m \<inter> interior (\<Union>p) = {}" proof(rule inter_interior_unions_intervals)
  1153           show "finite p" "open (interior m)" "\<forall>t\<in>p. \<exists>a b. t = {a..b}" using divp by auto
  1154           show "\<forall>t\<in>p. interior m \<inter> interior t = {}" apply(rule, rule r1(7)) using as using r1 by auto
  1155         qed thus "interior s \<inter> interior m = {}" unfolding divp by auto
  1156       qed qed        
  1157     thus "interior s \<inter> interior (\<Union>(r1-p) \<inter> (\<Union>q)) = {}" using interior_subset by auto
  1158   qed auto qed
  1159 
  1160 subsection {* Tagged (partial) divisions. *}
  1161 
  1162 definition tagged_partial_division_of (infixr "tagged'_partial'_division'_of" 40) where
  1163   "(s tagged_partial_division_of i) \<equiv>
  1164         finite s \<and>
  1165         (\<forall>x k. (x,k) \<in> s \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = {a..b})) \<and>
  1166         (\<forall>x1 k1 x2 k2. (x1,k1) \<in> s \<and> (x2,k2) \<in> s \<and> ((x1,k1) \<noteq> (x2,k2))
  1167                        \<longrightarrow> (interior(k1) \<inter> interior(k2) = {}))"
  1168 
  1169 lemma tagged_partial_division_ofD[dest]: assumes "s tagged_partial_division_of i"
  1170   shows "finite s" "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k" "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"
  1171   "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = {a..b}"
  1172   "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow> (x2,k2) \<in> s \<Longrightarrow> (x1,k1) \<noteq> (x2,k2) \<Longrightarrow> interior(k1) \<inter> interior(k2) = {}"
  1173   using assms unfolding tagged_partial_division_of_def  apply- by blast+ 
  1174 
  1175 definition tagged_division_of (infixr "tagged'_division'_of" 40) where
  1176   "(s tagged_division_of i) \<equiv>
  1177         (s tagged_partial_division_of i) \<and> (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  1178 
  1179 lemma tagged_division_of_finite: "s tagged_division_of i \<Longrightarrow> finite s"
  1180   unfolding tagged_division_of_def tagged_partial_division_of_def by auto
  1181 
  1182 lemma tagged_division_of:
  1183  "(s tagged_division_of i) \<longleftrightarrow>
  1184         finite s \<and>
  1185         (\<forall>x k. (x,k) \<in> s
  1186                \<longrightarrow> x \<in> k \<and> k \<subseteq> i \<and> (\<exists>a b. k = {a..b})) \<and>
  1187         (\<forall>x1 k1 x2 k2. (x1,k1) \<in> s \<and> (x2,k2) \<in> s \<and> ~((x1,k1) = (x2,k2))
  1188                        \<longrightarrow> (interior(k1) \<inter> interior(k2) = {})) \<and>
  1189         (\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  1190   unfolding tagged_division_of_def tagged_partial_division_of_def by auto
  1191 
  1192 lemma tagged_division_ofI: assumes
  1193   "finite s" "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k" "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"  "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = {a..b}"
  1194   "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow> (x2,k2) \<in> s \<Longrightarrow> ~((x1,k1) = (x2,k2)) \<Longrightarrow> (interior(k1) \<inter> interior(k2) = {})"
  1195   "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)"
  1196   shows "s tagged_division_of i"
  1197   unfolding tagged_division_of apply(rule) defer apply rule
  1198   apply(rule allI impI conjI assms)+ apply assumption
  1199   apply(rule, rule assms, assumption) apply(rule assms, assumption)
  1200   using assms(1,5-) apply- by blast+
  1201 
  1202 lemma tagged_division_ofD[dest]: assumes "s tagged_division_of i"
  1203   shows "finite s" "\<And>x k. (x,k) \<in> s \<Longrightarrow> x \<in> k" "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> i"  "\<And>x k. (x,k) \<in> s \<Longrightarrow> \<exists>a b. k = {a..b}"
  1204   "\<And>x1 k1 x2 k2. (x1,k1) \<in> s \<Longrightarrow> (x2,k2) \<in> s \<Longrightarrow> ~((x1,k1) = (x2,k2)) \<Longrightarrow> (interior(k1) \<inter> interior(k2) = {})"
  1205   "(\<Union>{k. \<exists>x. (x,k) \<in> s} = i)" using assms unfolding tagged_division_of apply- by blast+
  1206 
  1207 lemma division_of_tagged_division: assumes"s tagged_division_of i"  shows "(snd ` s) division_of i"
  1208 proof(rule division_ofI) note assm=tagged_division_ofD[OF assms]
  1209   show "\<Union>snd ` s = i" "finite (snd ` s)" using assm by auto
  1210   fix k assume k:"k \<in> snd ` s" then obtain xk where xk:"(xk, k) \<in> s" by auto
  1211   thus  "k \<subseteq> i" "k \<noteq> {}" "\<exists>a b. k = {a..b}" using assm apply- by fastforce+
  1212   fix k' assume k':"k' \<in> snd ` s" "k \<noteq> k'" from this(1) obtain xk' where xk':"(xk', k') \<in> s" by auto
  1213   thus "interior k \<inter> interior k' = {}" apply-apply(rule assm(5)) apply(rule xk xk')+ using k' by auto
  1214 qed
  1215 
  1216 lemma partial_division_of_tagged_division: assumes "s tagged_partial_division_of i"
  1217   shows "(snd ` s) division_of \<Union>(snd ` s)"
  1218 proof(rule division_ofI) note assm=tagged_partial_division_ofD[OF assms]
  1219   show "finite (snd ` s)" "\<Union>snd ` s = \<Union>snd ` s" using assm by auto
  1220   fix k assume k:"k \<in> snd ` s" then obtain xk where xk:"(xk, k) \<in> s" by auto
  1221   thus "k\<noteq>{}" "\<exists>a b. k = {a..b}" "k \<subseteq> \<Union>snd ` s" using assm by auto
  1222   fix k' assume k':"k' \<in> snd ` s" "k \<noteq> k'" from this(1) obtain xk' where xk':"(xk', k') \<in> s" by auto
  1223   thus "interior k \<inter> interior k' = {}" apply-apply(rule assm(5)) apply(rule xk xk')+ using k' by auto
  1224 qed
  1225 
  1226 lemma tagged_partial_division_subset: assumes "s tagged_partial_division_of i" "t \<subseteq> s"
  1227   shows "t tagged_partial_division_of i"
  1228   using assms unfolding tagged_partial_division_of_def using finite_subset[OF assms(2)] by blast
  1229 
  1230 lemma setsum_over_tagged_division_lemma: fixes d::"('m::ordered_euclidean_space) set \<Rightarrow> 'a::real_normed_vector"
  1231   assumes "p tagged_division_of i" "\<And>u v. {u..v} \<noteq> {} \<Longrightarrow> content {u..v} = 0 \<Longrightarrow> d {u..v} = 0"
  1232   shows "setsum (\<lambda>(x,k). d k) p = setsum d (snd ` p)"
  1233 proof- note assm=tagged_division_ofD[OF assms(1)]
  1234   have *:"(\<lambda>(x,k). d k) = d \<circ> snd" unfolding o_def apply(rule ext) by auto
  1235   show ?thesis unfolding * apply(subst eq_commute) proof(rule setsum_reindex_nonzero)
  1236     show "finite p" using assm by auto
  1237     fix x y assume as:"x\<in>p" "y\<in>p" "x\<noteq>y" "snd x = snd y" 
  1238     obtain a b where ab:"snd x = {a..b}" using assm(4)[of "fst x" "snd x"] as(1) by auto
  1239     have "(fst x, snd y) \<in> p" "(fst x, snd y) \<noteq> y" unfolding as(4)[THEN sym] using as(1-3) by auto
  1240     hence "interior (snd x) \<inter> interior (snd y) = {}" apply-apply(rule assm(5)[of "fst x" _ "fst y"]) using as by auto 
  1241     hence "content {a..b} = 0" unfolding as(4)[THEN sym] ab content_eq_0_interior by auto
  1242     hence "d {a..b} = 0" apply-apply(rule assms(2)) using assm(2)[of "fst x" "snd x"] as(1) unfolding ab[THEN sym] by auto
  1243     thus "d (snd x) = 0" unfolding ab by auto qed qed
  1244 
  1245 lemma tag_in_interval: "p tagged_division_of i \<Longrightarrow> (x,k) \<in> p \<Longrightarrow> x \<in> i" by auto
  1246 
  1247 lemma tagged_division_of_empty: "{} tagged_division_of {}"
  1248   unfolding tagged_division_of by auto
  1249 
  1250 lemma tagged_partial_division_of_trivial[simp]:
  1251  "p tagged_partial_division_of {} \<longleftrightarrow> p = {}"
  1252   unfolding tagged_partial_division_of_def by auto
  1253 
  1254 lemma tagged_division_of_trivial[simp]:
  1255  "p tagged_division_of {} \<longleftrightarrow> p = {}"
  1256   unfolding tagged_division_of by auto
  1257 
  1258 lemma tagged_division_of_self:
  1259  "x \<in> {a..b} \<Longrightarrow> {(x,{a..b})} tagged_division_of {a..b}"
  1260   apply(rule tagged_division_ofI) by auto
  1261 
  1262 lemma tagged_division_union:
  1263   assumes "p1 tagged_division_of s1"  "p2 tagged_division_of s2" "interior s1 \<inter> interior s2 = {}"
  1264   shows "(p1 \<union> p2) tagged_division_of (s1 \<union> s2)"
  1265 proof(rule tagged_division_ofI) note p1=tagged_division_ofD[OF assms(1)] and p2=tagged_division_ofD[OF assms(2)]
  1266   show "finite (p1 \<union> p2)" using p1(1) p2(1) by auto
  1267   show "\<Union>{k. \<exists>x. (x, k) \<in> p1 \<union> p2} = s1 \<union> s2" using p1(6) p2(6) by blast
  1268   fix x k assume xk:"(x,k)\<in>p1\<union>p2" show "x\<in>k" "\<exists>a b. k = {a..b}" using xk p1(2,4) p2(2,4) by auto
  1269   show "k\<subseteq>s1\<union>s2" using xk p1(3) p2(3) by blast
  1270   fix x' k' assume xk':"(x',k')\<in>p1\<union>p2" "(x,k) \<noteq> (x',k')"
  1271   have *:"\<And>a b. a\<subseteq> s1 \<Longrightarrow> b\<subseteq> s2 \<Longrightarrow> interior a \<inter> interior b = {}" using assms(3) interior_mono by blast
  1272   show "interior k \<inter> interior k' = {}" apply(cases "(x,k)\<in>p1", case_tac[!] "(x',k')\<in>p1")
  1273     apply(rule p1(5)) prefer 4 apply(rule *) prefer 6 apply(subst Int_commute,rule *) prefer 8 apply(rule p2(5))
  1274     using p1(3) p2(3) using xk xk' by auto qed 
  1275 
  1276 lemma tagged_division_unions:
  1277   assumes "finite iset" "\<forall>i\<in>iset. (pfn(i) tagged_division_of i)"
  1278   "\<forall>i1 \<in> iset. \<forall>i2 \<in> iset. ~(i1 = i2) \<longrightarrow> (interior(i1) \<inter> interior(i2) = {})"
  1279   shows "\<Union>(pfn ` iset) tagged_division_of (\<Union>iset)"
  1280 proof(rule tagged_division_ofI)
  1281   note assm = tagged_division_ofD[OF assms(2)[rule_format]]
  1282   show "finite (\<Union>pfn ` iset)" apply(rule finite_Union) using assms by auto
  1283   have "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>pfn ` iset} = \<Union>(\<lambda>i. \<Union>{k. \<exists>x. (x, k) \<in> pfn i}) ` iset" by blast 
  1284   also have "\<dots> = \<Union>iset" using assm(6) by auto
  1285   finally show "\<Union>{k. \<exists>x. (x, k) \<in> \<Union>pfn ` iset} = \<Union>iset" . 
  1286   fix x k assume xk:"(x,k)\<in>\<Union>pfn ` iset" then obtain i where i:"i \<in> iset" "(x, k) \<in> pfn i" by auto
  1287   show "x\<in>k" "\<exists>a b. k = {a..b}" "k \<subseteq> \<Union>iset" using assm(2-4)[OF i] using i(1) by auto
  1288   fix x' k' assume xk':"(x',k')\<in>\<Union>pfn ` iset" "(x, k) \<noteq> (x', k')" then obtain i' where i':"i' \<in> iset" "(x', k') \<in> pfn i'" by auto
  1289   have *:"\<And>a b. i\<noteq>i' \<Longrightarrow> a\<subseteq> i \<Longrightarrow> b\<subseteq> i' \<Longrightarrow> interior a \<inter> interior b = {}" using i(1) i'(1)
  1290     using assms(3)[rule_format] interior_mono by blast
  1291   show "interior k \<inter> interior k' = {}" apply(cases "i=i'")
  1292     using assm(5)[OF i _ xk'(2)]  i'(2) using assm(3)[OF i] assm(3)[OF i'] defer apply-apply(rule *) by auto
  1293 qed
  1294 
  1295 lemma tagged_partial_division_of_union_self:
  1296   assumes "p tagged_partial_division_of s" shows "p tagged_division_of (\<Union>(snd ` p))"
  1297   apply(rule tagged_division_ofI) using tagged_partial_division_ofD[OF assms] by auto
  1298 
  1299 lemma tagged_division_of_union_self: assumes "p tagged_division_of s"
  1300   shows "p tagged_division_of (\<Union>(snd ` p))"
  1301   apply(rule tagged_division_ofI) using tagged_division_ofD[OF assms] by auto
  1302 
  1303 subsection {* Fine-ness of a partition w.r.t. a gauge. *}
  1304 
  1305 definition fine (infixr "fine" 46) where
  1306   "d fine s \<longleftrightarrow> (\<forall>(x,k) \<in> s. k \<subseteq> d(x))"
  1307 
  1308 lemma fineI: assumes "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> d x"
  1309   shows "d fine s" using assms unfolding fine_def by auto
  1310 
  1311 lemma fineD[dest]: assumes "d fine s"
  1312   shows "\<And>x k. (x,k) \<in> s \<Longrightarrow> k \<subseteq> d x" using assms unfolding fine_def by auto
  1313 
  1314 lemma fine_inter: "(\<lambda>x. d1 x \<inter> d2 x) fine p \<longleftrightarrow> d1 fine p \<and> d2 fine p"
  1315   unfolding fine_def by auto
  1316 
  1317 lemma fine_inters:
  1318  "(\<lambda>x. \<Inter> {f d x | d.  d \<in> s}) fine p \<longleftrightarrow> (\<forall>d\<in>s. (f d) fine p)"
  1319   unfolding fine_def by blast
  1320 
  1321 lemma fine_union:
  1322   "d fine p1 \<Longrightarrow> d fine p2 \<Longrightarrow> d fine (p1 \<union> p2)"
  1323   unfolding fine_def by blast
  1324 
  1325 lemma fine_unions:"(\<And>p. p \<in> ps \<Longrightarrow> d fine p) \<Longrightarrow> d fine (\<Union>ps)"
  1326   unfolding fine_def by auto
  1327 
  1328 lemma fine_subset:  "p \<subseteq> q \<Longrightarrow> d fine q \<Longrightarrow> d fine p"
  1329   unfolding fine_def by blast
  1330 
  1331 subsection {* Gauge integral. Define on compact intervals first, then use a limit. *}
  1332 
  1333 definition has_integral_compact_interval (infixr "has'_integral'_compact'_interval" 46) where
  1334   "(f has_integral_compact_interval y) i \<equiv>
  1335         (\<forall>e>0. \<exists>d. gauge d \<and>
  1336           (\<forall>p. p tagged_division_of i \<and> d fine p
  1337                         \<longrightarrow> norm(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - y) < e))"
  1338 
  1339 definition has_integral (infixr "has'_integral" 46) where 
  1340 "((f::('n::ordered_euclidean_space \<Rightarrow> 'b::real_normed_vector)) has_integral y) i \<equiv>
  1341         if (\<exists>a b. i = {a..b}) then (f has_integral_compact_interval y) i
  1342         else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b}
  1343               \<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> i then f x else 0) has_integral_compact_interval z) {a..b} \<and>
  1344                                        norm(z - y) < e))"
  1345 
  1346 lemma has_integral:
  1347  "(f has_integral y) ({a..b}) \<longleftrightarrow>
  1348         (\<forall>e>0. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p
  1349                         \<longrightarrow> norm(setsum (\<lambda>(x,k). content(k) *\<^sub>R f x) p - y) < e))"
  1350   unfolding has_integral_def has_integral_compact_interval_def by auto
  1351 
  1352 lemma has_integralD[dest]: assumes
  1353  "(f has_integral y) ({a..b})" "e>0"
  1354   obtains d where "gauge d" "\<And>p. p tagged_division_of {a..b} \<Longrightarrow> d fine p
  1355                         \<Longrightarrow> norm(setsum (\<lambda>(x,k). content(k) *\<^sub>R f(x)) p - y) < e"
  1356   using assms unfolding has_integral by auto
  1357 
  1358 lemma has_integral_alt:
  1359  "(f has_integral y) i \<longleftrightarrow>
  1360       (if (\<exists>a b. i = {a..b}) then (f has_integral y) i
  1361        else (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b}
  1362                                \<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> i then f(x) else 0)
  1363                                         has_integral z) ({a..b}) \<and>
  1364                                        norm(z - y) < e)))"
  1365   unfolding has_integral unfolding has_integral_compact_interval_def has_integral_def by auto
  1366 
  1367 lemma has_integral_altD:
  1368   assumes "(f has_integral y) i" "\<not> (\<exists>a b. i = {a..b})" "e>0"
  1369   obtains B where "B>0" "\<forall>a b. ball 0 B \<subseteq> {a..b}\<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> i then f(x) else 0) has_integral z) ({a..b}) \<and> norm(z - y) < e)"
  1370   using assms unfolding has_integral unfolding has_integral_compact_interval_def has_integral_def by auto
  1371 
  1372 definition integrable_on (infixr "integrable'_on" 46) where
  1373   "(f integrable_on i) \<equiv> \<exists>y. (f has_integral y) i"
  1374 
  1375 definition "integral i f \<equiv> SOME y. (f has_integral y) i"
  1376 
  1377 lemma integrable_integral[dest]:
  1378  "f integrable_on i \<Longrightarrow> (f has_integral (integral i f)) i"
  1379   unfolding integrable_on_def integral_def by(rule someI_ex)
  1380 
  1381 lemma has_integral_integrable[intro]: "(f has_integral i) s \<Longrightarrow> f integrable_on s"
  1382   unfolding integrable_on_def by auto
  1383 
  1384 lemma has_integral_integral:"f integrable_on s \<longleftrightarrow> (f has_integral (integral s f)) s"
  1385   by auto
  1386 
  1387 lemma setsum_content_null:
  1388   assumes "content({a..b}) = 0" "p tagged_division_of {a..b}"
  1389   shows "setsum (\<lambda>(x,k). content k *\<^sub>R f x) p = (0::'a::real_normed_vector)"
  1390 proof(rule setsum_0',rule) fix y assume y:"y\<in>p"
  1391   obtain x k where xk:"y = (x,k)" using surj_pair[of y] by blast
  1392   note assm = tagged_division_ofD(3-4)[OF assms(2) y[unfolded xk]]
  1393   from this(2) guess c .. then guess d .. note c_d=this
  1394   have "(\<lambda>(x, k). content k *\<^sub>R f x) y = content k *\<^sub>R f x" unfolding xk by auto
  1395   also have "\<dots> = 0" using content_subset[OF assm(1)[unfolded c_d]] content_pos_le[of c d]
  1396     unfolding assms(1) c_d by auto
  1397   finally show "(\<lambda>(x, k). content k *\<^sub>R f x) y = 0" .
  1398 qed
  1399 
  1400 subsection {* Some basic combining lemmas. *}
  1401 
  1402 lemma tagged_division_unions_exists:
  1403   assumes "finite iset" "\<forall>i \<in> iset. \<exists>p. p tagged_division_of i \<and> d fine p"
  1404   "\<forall>i1\<in>iset. \<forall>i2\<in>iset. ~(i1 = i2) \<longrightarrow> (interior(i1) \<inter> interior(i2) = {})" "(\<Union>iset = i)"
  1405    obtains p where "p tagged_division_of i" "d fine p"
  1406 proof- guess pfn using bchoice[OF assms(2)] .. note pfn = conjunctD2[OF this[rule_format]]
  1407   show thesis apply(rule_tac p="\<Union>(pfn ` iset)" in that) unfolding assms(4)[THEN sym]
  1408     apply(rule tagged_division_unions[OF assms(1) _ assms(3)]) defer 
  1409     apply(rule fine_unions) using pfn by auto
  1410 qed
  1411 
  1412 subsection {* The set we're concerned with must be closed. *}
  1413 
  1414 lemma division_of_closed: "s division_of i \<Longrightarrow> closed (i::('n::ordered_euclidean_space) set)"
  1415   unfolding division_of_def by fastforce
  1416 
  1417 subsection {* General bisection principle for intervals; might be useful elsewhere. *}
  1418 
  1419 lemma interval_bisection_step:  fixes type::"'a::ordered_euclidean_space"
  1420   assumes "P {}" "(\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P(s \<union> t))" "~(P {a..b::'a})"
  1421   obtains c d where "~(P{c..d})"
  1422   "\<forall>i<DIM('a). a$$i \<le> c$$i \<and> c$$i \<le> d$$i \<and> d$$i \<le> b$$i \<and> 2 * (d$$i - c$$i) \<le> b$$i - a$$i"
  1423 proof- have "{a..b} \<noteq> {}" using assms(1,3) by auto
  1424   note ab=this[unfolded interval_eq_empty not_ex not_less]
  1425   { fix f have "finite f \<Longrightarrow>
  1426         (\<forall>s\<in>f. P s) \<Longrightarrow>
  1427         (\<forall>s\<in>f. \<exists>a b. s = {a..b}) \<Longrightarrow>
  1428         (\<forall>s\<in>f.\<forall>t\<in>f. ~(s = t) \<longrightarrow> interior(s) \<inter> interior(t) = {}) \<Longrightarrow> P(\<Union>f)"
  1429     proof(induct f rule:finite_induct)
  1430       case empty show ?case using assms(1) by auto
  1431     next case (insert x f) show ?case unfolding Union_insert apply(rule assms(2)[rule_format])
  1432         apply rule defer apply rule defer apply(rule inter_interior_unions_intervals)
  1433         using insert by auto
  1434     qed } note * = this
  1435   let ?A = "{{c..d} | c d::'a. \<forall>i<DIM('a). (c$$i = a$$i) \<and> (d$$i = (a$$i + b$$i) / 2) \<or> (c$$i = (a$$i + b$$i) / 2) \<and> (d$$i = b$$i)}"
  1436   let ?PP = "\<lambda>c d. \<forall>i<DIM('a). a$$i \<le> c$$i \<and> c$$i \<le> d$$i \<and> d$$i \<le> b$$i \<and> 2 * (d$$i - c$$i) \<le> b$$i - a$$i"
  1437   { presume "\<forall>c d. ?PP c d \<longrightarrow> P {c..d} \<Longrightarrow> False"
  1438     thus thesis unfolding atomize_not not_all apply-apply(erule exE)+ apply(rule_tac c=x and d=xa in that) by auto }
  1439   assume as:"\<forall>c d. ?PP c d \<longrightarrow> P {c..d}"
  1440   have "P (\<Union> ?A)" proof(rule *, rule_tac[2-] ballI, rule_tac[4] ballI, rule_tac[4] impI) 
  1441     let ?B = "(\<lambda>s.{(\<chi>\<chi> i. if i \<in> s then a$$i else (a$$i + b$$i) / 2)::'a ..
  1442       (\<chi>\<chi> i. if i \<in> s then (a$$i + b$$i) / 2 else b$$i)}) ` {s. s \<subseteq> {..<DIM('a)}}"
  1443     have "?A \<subseteq> ?B" proof case goal1
  1444       then guess c unfolding mem_Collect_eq .. then guess d apply- by(erule exE,(erule conjE)+) note c_d=this[rule_format]
  1445       have *:"\<And>a b c d. a = c \<Longrightarrow> b = d \<Longrightarrow> {a..b} = {c..d}" by auto
  1446       show "x\<in>?B" unfolding image_iff apply(rule_tac x="{i. i<DIM('a) \<and> c$$i = a$$i}" in bexI)
  1447         unfolding c_d apply(rule * ) unfolding euclidean_eq[where 'a='a] apply safe unfolding euclidean_lambda_beta' mem_Collect_eq
  1448       proof- fix i assume "i<DIM('a)" thus " c $$ i = (if i < DIM('a) \<and> c $$ i = a $$ i then a $$ i else (a $$ i + b $$ i) / 2)"
  1449           "d $$ i = (if i < DIM('a) \<and> c $$ i = a $$ i then (a $$ i + b $$ i) / 2 else b $$ i)"
  1450           using c_d(2)[of i] ab[THEN spec[where x=i]] by(auto simp add:field_simps)
  1451       qed qed
  1452     thus "finite ?A" apply(rule finite_subset) by auto
  1453     fix s assume "s\<in>?A" then guess c unfolding mem_Collect_eq .. then guess d apply- by(erule exE,(erule conjE)+)
  1454     note c_d=this[rule_format]
  1455     show "P s" unfolding c_d apply(rule as[rule_format]) proof- case goal1 thus ?case 
  1456         using c_d(2)[of i] using ab[THEN spec[where x=i]] by auto qed
  1457     show "\<exists>a b. s = {a..b}" unfolding c_d by auto
  1458     fix t assume "t\<in>?A" then guess e unfolding mem_Collect_eq .. then guess f apply- by(erule exE,(erule conjE)+)
  1459     note e_f=this[rule_format]
  1460     assume "s \<noteq> t" hence "\<not> (c = e \<and> d = f)" unfolding c_d e_f by auto
  1461     then obtain i where "c$$i \<noteq> e$$i \<or> d$$i \<noteq> f$$i" and i':"i<DIM('a)" unfolding de_Morgan_conj euclidean_eq[where 'a='a] by auto
  1462     hence i:"c$$i \<noteq> e$$i" "d$$i \<noteq> f$$i" apply- apply(erule_tac[!] disjE)
  1463     proof- assume "c$$i \<noteq> e$$i" thus "d$$i \<noteq> f$$i" using c_d(2)[of i] e_f(2)[of i] by fastforce
  1464     next   assume "d$$i \<noteq> f$$i" thus "c$$i \<noteq> e$$i" using c_d(2)[of i] e_f(2)[of i] by fastforce
  1465     qed have *:"\<And>s t. (\<And>a. a\<in>s \<Longrightarrow> a\<in>t \<Longrightarrow> False) \<Longrightarrow> s \<inter> t = {}" by auto
  1466     show "interior s \<inter> interior t = {}" unfolding e_f c_d interior_closed_interval proof(rule *)
  1467       fix x assume "x\<in>{c<..<d}" "x\<in>{e<..<f}"
  1468       hence x:"c$$i < d$$i" "e$$i < f$$i" "c$$i < f$$i" "e$$i < d$$i" unfolding mem_interval using i'
  1469         apply-apply(erule_tac[!] x=i in allE)+ by auto
  1470       show False using c_d(2)[OF i'] apply- apply(erule_tac disjE)
  1471       proof(erule_tac[!] conjE) assume as:"c $$ i = a $$ i" "d $$ i = (a $$ i + b $$ i) / 2"
  1472         show False using e_f(2)[of i] and i x unfolding as by(fastforce simp add:field_simps)
  1473       next assume as:"c $$ i = (a $$ i + b $$ i) / 2" "d $$ i = b $$ i"
  1474         show False using e_f(2)[of i] and i x unfolding as by(fastforce simp add:field_simps)
  1475       qed qed qed
  1476   also have "\<Union> ?A = {a..b}" proof(rule set_eqI,rule)
  1477     fix x assume "x\<in>\<Union>?A" then guess Y unfolding Union_iff ..
  1478     from this(1) guess c unfolding mem_Collect_eq .. then guess d ..
  1479     note c_d = this[THEN conjunct2,rule_format] `x\<in>Y`[unfolded this[THEN conjunct1]]
  1480     show "x\<in>{a..b}" unfolding mem_interval proof safe
  1481       fix i assume "i<DIM('a)" thus "a $$ i \<le> x $$ i" "x $$ i \<le> b $$ i"
  1482         using c_d(1)[of i] c_d(2)[unfolded mem_interval,THEN spec[where x=i]] by auto qed
  1483   next fix x assume x:"x\<in>{a..b}"
  1484     have "\<forall>i<DIM('a). \<exists>c d. (c = a$$i \<and> d = (a$$i + b$$i) / 2 \<or> c = (a$$i + b$$i) / 2 \<and> d = b$$i) \<and> c\<le>x$$i \<and> x$$i \<le> d"
  1485       (is "\<forall>i<DIM('a). \<exists>c d. ?P i c d") unfolding mem_interval proof(rule,rule) fix i
  1486       have "?P i (a$$i) ((a $$ i + b $$ i) / 2) \<or> ?P i ((a $$ i + b $$ i) / 2) (b$$i)"
  1487         using x[unfolded mem_interval,THEN spec[where x=i]] by auto thus "\<exists>c d. ?P i c d" by blast
  1488     qed thus "x\<in>\<Union>?A" unfolding Union_iff unfolding lambda_skolem' unfolding Bex_def mem_Collect_eq
  1489       apply-apply(erule exE)+ apply(rule_tac x="{xa..xaa}" in exI) unfolding mem_interval by auto
  1490   qed finally show False using assms by auto qed
  1491 
  1492 lemma interval_bisection: fixes type::"'a::ordered_euclidean_space"
  1493   assumes "P {}" "(\<forall>s t. P s \<and> P t \<and> interior(s) \<inter> interior(t) = {} \<longrightarrow> P(s \<union> t))" "\<not> P {a..b::'a}"
  1494   obtains x where "x \<in> {a..b}" "\<forall>e>0. \<exists>c d. x \<in> {c..d} \<and> {c..d} \<subseteq> ball x e \<and> {c..d} \<subseteq> {a..b} \<and> ~P({c..d})"
  1495 proof-
  1496   have "\<forall>x. \<exists>y. \<not> P {fst x..snd x} \<longrightarrow> (\<not> P {fst y..snd y} \<and>
  1497     (\<forall>i<DIM('a). fst x$$i \<le> fst y$$i \<and> fst y$$i \<le> snd y$$i \<and> snd y$$i \<le> snd x$$i \<and>
  1498                            2 * (snd y$$i - fst y$$i) \<le> snd x$$i - fst x$$i))" proof case goal1 thus ?case proof-
  1499       presume "\<not> P {fst x..snd x} \<Longrightarrow> ?thesis"
  1500       thus ?thesis apply(cases "P {fst x..snd x}") by auto
  1501     next assume as:"\<not> P {fst x..snd x}" from interval_bisection_step[of P, OF assms(1-2) as] guess c d . 
  1502       thus ?thesis apply- apply(rule_tac x="(c,d)" in exI) by auto
  1503     qed qed then guess f apply-apply(drule choice) by(erule exE) note f=this
  1504   def AB \<equiv> "\<lambda>n. (f ^^ n) (a,b)" def A \<equiv> "\<lambda>n. fst(AB n)" and B \<equiv> "\<lambda>n. snd(AB n)" note ab_def = this AB_def
  1505   have "A 0 = a" "B 0 = b" "\<And>n. \<not> P {A(Suc n)..B(Suc n)} \<and>
  1506     (\<forall>i<DIM('a). A(n)$$i \<le> A(Suc n)$$i \<and> A(Suc n)$$i \<le> B(Suc n)$$i \<and> B(Suc n)$$i \<le> B(n)$$i \<and> 
  1507     2 * (B(Suc n)$$i - A(Suc n)$$i) \<le> B(n)$$i - A(n)$$i)" (is "\<And>n. ?P n")
  1508   proof- show "A 0 = a" "B 0 = b" unfolding ab_def by auto
  1509     case goal3 note S = ab_def funpow.simps o_def id_apply show ?case
  1510     proof(induct n) case 0 thus ?case unfolding S apply(rule f[rule_format]) using assms(3) by auto
  1511     next case (Suc n) show ?case unfolding S apply(rule f[rule_format]) using Suc unfolding S by auto
  1512     qed qed note AB = this(1-2) conjunctD2[OF this(3),rule_format]
  1513 
  1514   have interv:"\<And>e. 0 < e \<Longrightarrow> \<exists>n. \<forall>x\<in>{A n..B n}. \<forall>y\<in>{A n..B n}. dist x y < e"
  1515   proof- case goal1 guess n using real_arch_pow2[of "(setsum (\<lambda>i. b$$i - a$$i) {..<DIM('a)}) / e"] .. note n=this
  1516     show ?case apply(rule_tac x=n in exI) proof(rule,rule)
  1517       fix x y assume xy:"x\<in>{A n..B n}" "y\<in>{A n..B n}"
  1518       have "dist x y \<le> setsum (\<lambda>i. abs((x - y)$$i)) {..<DIM('a)}" unfolding dist_norm by(rule norm_le_l1)
  1519       also have "\<dots> \<le> setsum (\<lambda>i. B n$$i - A n$$i) {..<DIM('a)}"
  1520       proof(rule setsum_mono) fix i show "\<bar>(x - y) $$ i\<bar> \<le> B n $$ i - A n $$ i"
  1521           using xy[unfolded mem_interval,THEN spec[where x=i]] by auto qed
  1522       also have "\<dots> \<le> setsum (\<lambda>i. b$$i - a$$i) {..<DIM('a)} / 2^n" unfolding setsum_divide_distrib
  1523       proof(rule setsum_mono) case goal1 thus ?case
  1524         proof(induct n) case 0 thus ?case unfolding AB by auto
  1525         next case (Suc n) have "B (Suc n) $$ i - A (Suc n) $$ i \<le> (B n $$ i - A n $$ i) / 2"
  1526             using AB(4)[of i n] using goal1 by auto
  1527           also have "\<dots> \<le> (b $$ i - a $$ i) / 2 ^ Suc n" using Suc by(auto simp add:field_simps) finally show ?case .
  1528         qed qed
  1529       also have "\<dots> < e" using n using goal1 by(auto simp add:field_simps) finally show "dist x y < e" .
  1530     qed qed
  1531   { fix n m ::nat assume "m \<le> n" then guess d unfolding le_Suc_ex_iff .. note d=this
  1532     have "{A n..B n} \<subseteq> {A m..B m}" unfolding d 
  1533     proof(induct d) case 0 thus ?case by auto
  1534     next case (Suc d) show ?case apply(rule subset_trans[OF _ Suc])
  1535         apply(rule) unfolding mem_interval apply(rule,erule_tac x=i in allE)
  1536       proof- case goal1 thus ?case using AB(4)[of i "m + d"] by(auto simp add:field_simps)
  1537       qed qed } note ABsubset = this 
  1538   have "\<exists>a. \<forall>n. a\<in>{A n..B n}" apply(rule decreasing_closed_nest[rule_format,OF closed_interval _ ABsubset interv])
  1539   proof- fix n show "{A n..B n} \<noteq> {}" apply(cases "0<n") using AB(3)[of "n - 1"] assms(1,3) AB(1-2) by auto qed auto
  1540   then guess x0 .. note x0=this[rule_format]
  1541   show thesis proof(rule that[rule_format,of x0])
  1542     show "x0\<in>{a..b}" using x0[of 0] unfolding AB .
  1543     fix e assume "0 < (e::real)" from interv[OF this] guess n .. note n=this
  1544     show "\<exists>c d. x0 \<in> {c..d} \<and> {c..d} \<subseteq> ball x0 e \<and> {c..d} \<subseteq> {a..b} \<and> \<not> P {c..d}"
  1545       apply(rule_tac x="A n" in exI,rule_tac x="B n" in exI) apply(rule,rule x0) apply rule defer 
  1546     proof show "\<not> P {A n..B n}" apply(cases "0<n") using AB(3)[of "n - 1"] assms(3) AB(1-2) by auto
  1547       show "{A n..B n} \<subseteq> ball x0 e" using n using x0[of n] by auto
  1548       show "{A n..B n} \<subseteq> {a..b}" unfolding AB(1-2)[symmetric] apply(rule ABsubset) by auto
  1549     qed qed qed 
  1550 
  1551 subsection {* Cousin's lemma. *}
  1552 
  1553 lemma fine_division_exists: assumes "gauge g" 
  1554   obtains p where "p tagged_division_of {a..b::'a::ordered_euclidean_space}" "g fine p"
  1555 proof- presume "\<not> (\<exists>p. p tagged_division_of {a..b} \<and> g fine p) \<Longrightarrow> False"
  1556   then guess p unfolding atomize_not not_not .. thus thesis apply-apply(rule that[of p]) by auto
  1557 next assume as:"\<not> (\<exists>p. p tagged_division_of {a..b} \<and> g fine p)"
  1558   guess x apply(rule interval_bisection[of "\<lambda>s. \<exists>p. p tagged_division_of s \<and> g fine p",rule_format,OF _ _ as])
  1559     apply(rule_tac x="{}" in exI) defer apply(erule conjE exE)+
  1560   proof- show "{} tagged_division_of {} \<and> g fine {}" unfolding fine_def by auto
  1561     fix s t p p' assume "p tagged_division_of s" "g fine p" "p' tagged_division_of t" "g fine p'" "interior s \<inter> interior t = {}"
  1562     thus "\<exists>p. p tagged_division_of s \<union> t \<and> g fine p" apply-apply(rule_tac x="p \<union> p'" in exI) apply rule
  1563       apply(rule tagged_division_union) prefer 4 apply(rule fine_union) by auto
  1564   qed note x=this
  1565   obtain e where e:"e>0" "ball x e \<subseteq> g x" using gaugeD[OF assms, of x] unfolding open_contains_ball by auto
  1566   from x(2)[OF e(1)] guess c d apply-apply(erule exE conjE)+ . note c_d = this
  1567   have "g fine {(x, {c..d})}" unfolding fine_def using e using c_d(2) by auto
  1568   thus False using tagged_division_of_self[OF c_d(1)] using c_d by auto qed
  1569 
  1570 subsection {* Basic theorems about integrals. *}
  1571 
  1572 lemma has_integral_unique: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::real_normed_vector"
  1573   assumes "(f has_integral k1) i" "(f has_integral k2) i" shows "k1 = k2"
  1574 proof(rule ccontr) let ?e = "norm(k1 - k2) / 2" assume as:"k1 \<noteq> k2" hence e:"?e > 0" by auto
  1575   have lem:"\<And>f::'n \<Rightarrow> 'a.  \<And> a b k1 k2.
  1576     (f has_integral k1) ({a..b}) \<Longrightarrow> (f has_integral k2) ({a..b}) \<Longrightarrow> k1 \<noteq> k2 \<Longrightarrow> False"
  1577   proof- case goal1 let ?e = "norm(k1 - k2) / 2" from goal1(3) have e:"?e > 0" by auto
  1578     guess d1 by(rule has_integralD[OF goal1(1) e]) note d1=this
  1579     guess d2 by(rule has_integralD[OF goal1(2) e]) note d2=this
  1580     guess p by(rule fine_division_exists[OF gauge_inter[OF d1(1) d2(1)],of a b]) note p=this
  1581     let ?c = "(\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)" have "norm (k1 - k2) \<le> norm (?c - k2) + norm (?c - k1)"
  1582       using norm_triangle_ineq4[of "k1 - ?c" "k2 - ?c"] by(auto simp add:algebra_simps norm_minus_commute)
  1583     also have "\<dots> < norm (k1 - k2) / 2 + norm (k1 - k2) / 2"
  1584       apply(rule add_strict_mono) apply(rule_tac[!] d2(2) d1(2)) using p unfolding fine_def by auto
  1585     finally show False by auto
  1586   qed { presume "\<not> (\<exists>a b. i = {a..b}) \<Longrightarrow> False"
  1587     thus False apply-apply(cases "\<exists>a b. i = {a..b}")
  1588       using assms by(auto simp add:has_integral intro:lem[OF _ _ as]) }
  1589   assume as:"\<not> (\<exists>a b. i = {a..b})"
  1590   guess B1 by(rule has_integral_altD[OF assms(1) as,OF e]) note B1=this[rule_format]
  1591   guess B2 by(rule has_integral_altD[OF assms(2) as,OF e]) note B2=this[rule_format]
  1592   have "\<exists>a b::'n. ball 0 B1 \<union> ball 0 B2 \<subseteq> {a..b}" apply(rule bounded_subset_closed_interval)
  1593     using bounded_Un bounded_ball by auto then guess a b apply-by(erule exE)+
  1594   note ab=conjunctD2[OF this[unfolded Un_subset_iff]]
  1595   guess w using B1(2)[OF ab(1)] .. note w=conjunctD2[OF this]
  1596   guess z using B2(2)[OF ab(2)] .. note z=conjunctD2[OF this]
  1597   have "z = w" using lem[OF w(1) z(1)] by auto
  1598   hence "norm (k1 - k2) \<le> norm (z - k2) + norm (w - k1)"
  1599     using norm_triangle_ineq4[of "k1 - w" "k2 - z"] by(auto simp add: norm_minus_commute) 
  1600   also have "\<dots> < norm (k1 - k2) / 2 + norm (k1 - k2) / 2" apply(rule add_strict_mono) by(rule_tac[!] z(2) w(2))
  1601   finally show False by auto qed
  1602 
  1603 lemma integral_unique[intro]:
  1604   "(f has_integral y) k \<Longrightarrow> integral k f = y"
  1605   unfolding integral_def apply(rule some_equality) by(auto intro: has_integral_unique) 
  1606 
  1607 lemma has_integral_is_0: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::real_normed_vector" 
  1608   assumes "\<forall>x\<in>s. f x = 0" shows "(f has_integral 0) s"
  1609 proof- have lem:"\<And>a b. \<And>f::'n \<Rightarrow> 'a.
  1610     (\<forall>x\<in>{a..b}. f(x) = 0) \<Longrightarrow> (f has_integral 0) ({a..b})" unfolding has_integral
  1611   proof(rule,rule) fix a b e and f::"'n \<Rightarrow> 'a"
  1612     assume as:"\<forall>x\<in>{a..b}. f x = 0" "0 < (e::real)"
  1613     show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e)"
  1614       apply(rule_tac x="\<lambda>x. ball x 1" in exI)  apply(rule,rule gaugeI) unfolding centre_in_ball defer apply(rule open_ball)
  1615     proof(rule,rule,erule conjE) case goal1
  1616       have "(\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) = 0" proof(rule setsum_0',rule)
  1617         fix x assume x:"x\<in>p" have "f (fst x) = 0" using tagged_division_ofD(2-3)[OF goal1(1), of "fst x" "snd x"] using as x by auto
  1618         thus "(\<lambda>(x, k). content k *\<^sub>R f x) x = 0" apply(subst surjective_pairing[of x]) unfolding split_conv by auto
  1619       qed thus ?case using as by auto
  1620     qed auto qed  { presume "\<not> (\<exists>a b. s = {a..b}) \<Longrightarrow> ?thesis"
  1621     thus ?thesis apply-apply(cases "\<exists>a b. s = {a..b}")
  1622       using assms by(auto simp add:has_integral intro:lem) }
  1623   have *:"(\<lambda>x. if x \<in> s then f x else 0) = (\<lambda>x. 0)" apply(rule ext) using assms by auto
  1624   assume "\<not> (\<exists>a b. s = {a..b})" thus ?thesis apply(subst has_integral_alt) unfolding if_not_P *
  1625   apply(rule,rule,rule_tac x=1 in exI,rule) defer apply(rule,rule,rule)
  1626   proof- fix e::real and a b assume "e>0"
  1627     thus "\<exists>z. ((\<lambda>x::'n. 0::'a) has_integral z) {a..b} \<and> norm (z - 0) < e"
  1628       apply(rule_tac x=0 in exI) apply(rule,rule lem) by auto
  1629   qed auto qed
  1630 
  1631 lemma has_integral_0[simp]: "((\<lambda>x::'n::ordered_euclidean_space. 0) has_integral 0) s"
  1632   apply(rule has_integral_is_0) by auto 
  1633 
  1634 lemma has_integral_0_eq[simp]: "((\<lambda>x. 0) has_integral i) s \<longleftrightarrow> i = 0"
  1635   using has_integral_unique[OF has_integral_0] by auto
  1636 
  1637 lemma has_integral_linear: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::real_normed_vector"
  1638   assumes "(f has_integral y) s" "bounded_linear h" shows "((h o f) has_integral ((h y))) s"
  1639 proof- interpret bounded_linear h using assms(2) . from pos_bounded guess B .. note B=conjunctD2[OF this,rule_format]
  1640   have lem:"\<And>f::'n \<Rightarrow> 'a. \<And> y a b.
  1641     (f has_integral y) ({a..b}) \<Longrightarrow> ((h o f) has_integral h(y)) ({a..b})"
  1642   proof(subst has_integral,rule,rule) case goal1
  1643     from pos_bounded guess B .. note B=conjunctD2[OF this,rule_format]
  1644     have *:"e / B > 0" apply(rule divide_pos_pos) using goal1(2) B by auto
  1645     guess g using has_integralD[OF goal1(1) *] . note g=this
  1646     show ?case apply(rule_tac x=g in exI) apply(rule,rule g(1))
  1647     proof(rule,rule,erule conjE) fix p assume as:"p tagged_division_of {a..b}" "g fine p" 
  1648       have *:"\<And>x k. h ((\<lambda>(x, k). content k *\<^sub>R f x) x) = (\<lambda>(x, k). h (content k *\<^sub>R f x)) x" by auto
  1649       have "(\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) = setsum (h \<circ> (\<lambda>(x, k). content k *\<^sub>R f x)) p"
  1650         unfolding o_def unfolding scaleR[THEN sym] * by simp
  1651       also have "\<dots> = h (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)" using setsum[of "\<lambda>(x,k). content k *\<^sub>R f x" p] using as by auto
  1652       finally have *:"(\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) = h (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)" .
  1653       show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (h \<circ> f) x) - h y) < e" unfolding * diff[THEN sym]
  1654         apply(rule le_less_trans[OF B(2)]) using g(2)[OF as] B(1) by(auto simp add:field_simps)
  1655     qed qed { presume "\<not> (\<exists>a b. s = {a..b}) \<Longrightarrow> ?thesis"
  1656     thus ?thesis apply-apply(cases "\<exists>a b. s = {a..b}") using assms by(auto simp add:has_integral intro!:lem) }
  1657   assume as:"\<not> (\<exists>a b. s = {a..b})" thus ?thesis apply(subst has_integral_alt) unfolding if_not_P
  1658   proof(rule,rule) fix e::real  assume e:"0<e"
  1659     have *:"0 < e/B" by(rule divide_pos_pos,rule e,rule B(1))
  1660     guess M using has_integral_altD[OF assms(1) as *,rule_format] . note M=this
  1661     show "\<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b} \<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> s then (h \<circ> f) x else 0) has_integral z) {a..b} \<and> norm (z - h y) < e)"
  1662       apply(rule_tac x=M in exI) apply(rule,rule M(1))
  1663     proof(rule,rule,rule) case goal1 guess z using M(2)[OF goal1(1)] .. note z=conjunctD2[OF this]
  1664       have *:"(\<lambda>x. if x \<in> s then (h \<circ> f) x else 0) = h \<circ> (\<lambda>x. if x \<in> s then f x else 0)"
  1665         unfolding o_def apply(rule ext) using zero by auto
  1666       show ?case apply(rule_tac x="h z" in exI,rule) unfolding * apply(rule lem[OF z(1)]) unfolding diff[THEN sym]
  1667         apply(rule le_less_trans[OF B(2)]) using B(1) z(2) by(auto simp add:field_simps)
  1668     qed qed qed
  1669 
  1670 lemma has_integral_cmul:
  1671   shows "(f has_integral k) s \<Longrightarrow> ((\<lambda>x. c *\<^sub>R f x) has_integral (c *\<^sub>R k)) s"
  1672   unfolding o_def[THEN sym] apply(rule has_integral_linear,assumption)
  1673   by(rule bounded_linear_scaleR_right)
  1674 
  1675 lemma has_integral_cmult_real:
  1676   fixes c :: real
  1677   assumes "c \<noteq> 0 \<Longrightarrow> (f has_integral x) A"
  1678   shows "((\<lambda>x. c * f x) has_integral c * x) A"
  1679 proof cases
  1680   assume "c \<noteq> 0"
  1681   from has_integral_cmul[OF assms[OF this], of c] show ?thesis
  1682     unfolding real_scaleR_def .
  1683 qed simp
  1684 
  1685 lemma has_integral_neg:
  1686   shows "(f has_integral k) s \<Longrightarrow> ((\<lambda>x. -(f x)) has_integral (-k)) s"
  1687   apply(drule_tac c="-1" in has_integral_cmul) by auto
  1688 
  1689 lemma has_integral_add: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::real_normed_vector" 
  1690   assumes "(f has_integral k) s" "(g has_integral l) s"
  1691   shows "((\<lambda>x. f x + g x) has_integral (k + l)) s"
  1692 proof- have lem:"\<And>f g::'n \<Rightarrow> 'a. \<And>a b k l.
  1693     (f has_integral k) ({a..b}) \<Longrightarrow> (g has_integral l) ({a..b}) \<Longrightarrow>
  1694      ((\<lambda>x. f(x) + g(x)) has_integral (k + l)) ({a..b})" proof- case goal1
  1695     show ?case unfolding has_integral proof(rule,rule) fix e::real assume e:"e>0" hence *:"e/2>0" by auto
  1696       guess d1 using has_integralD[OF goal1(1) *] . note d1=this
  1697       guess d2 using has_integralD[OF goal1(2) *] . note d2=this
  1698       show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) < e)"
  1699         apply(rule_tac x="\<lambda>x. (d1 x) \<inter> (d2 x)" in exI) apply(rule,rule gauge_inter[OF d1(1) d2(1)])
  1700       proof(rule,rule,erule conjE) fix p assume as:"p tagged_division_of {a..b}" "(\<lambda>x. d1 x \<inter> d2 x) fine p"
  1701         have *:"(\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) = (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p. content k *\<^sub>R g x)"
  1702           unfolding scaleR_right_distrib setsum_addf[of "\<lambda>(x,k). content k *\<^sub>R f x" "\<lambda>(x,k). content k *\<^sub>R g x" p,THEN sym]
  1703           by(rule setsum_cong2,auto)
  1704         have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) = norm (((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - k) + ((\<Sum>(x, k)\<in>p. content k *\<^sub>R g x) - l))"
  1705           unfolding * by(auto simp add:algebra_simps) also let ?res = "\<dots>"
  1706         from as have *:"d1 fine p" "d2 fine p" unfolding fine_inter by auto
  1707         have "?res < e/2 + e/2" apply(rule le_less_trans[OF norm_triangle_ineq])
  1708           apply(rule add_strict_mono) using d1(2)[OF as(1) *(1)] and d2(2)[OF as(1) *(2)] by auto
  1709         finally show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R (f x + g x)) - (k + l)) < e" by auto
  1710       qed qed qed { presume "\<not> (\<exists>a b. s = {a..b}) \<Longrightarrow> ?thesis"
  1711     thus ?thesis apply-apply(cases "\<exists>a b. s = {a..b}") using assms by(auto simp add:has_integral intro!:lem) }
  1712   assume as:"\<not> (\<exists>a b. s = {a..b})" thus ?thesis apply(subst has_integral_alt) unfolding if_not_P
  1713   proof(rule,rule) case goal1 hence *:"e/2 > 0" by auto
  1714     from has_integral_altD[OF assms(1) as *] guess B1 . note B1=this[rule_format]
  1715     from has_integral_altD[OF assms(2) as *] guess B2 . note B2=this[rule_format]
  1716     show ?case apply(rule_tac x="max B1 B2" in exI) apply(rule,rule min_max.less_supI1,rule B1)
  1717     proof(rule,rule,rule) fix a b assume "ball 0 (max B1 B2) \<subseteq> {a..b::'n}"
  1718       hence *:"ball 0 B1 \<subseteq> {a..b::'n}" "ball 0 B2 \<subseteq> {a..b::'n}" by auto
  1719       guess w using B1(2)[OF *(1)] .. note w=conjunctD2[OF this]
  1720       guess z using B2(2)[OF *(2)] .. note z=conjunctD2[OF this]
  1721       have *:"\<And>x. (if x \<in> s then f x + g x else 0) = (if x \<in> s then f x else 0) + (if x \<in> s then g x else 0)" by auto
  1722       show "\<exists>z. ((\<lambda>x. if x \<in> s then f x + g x else 0) has_integral z) {a..b} \<and> norm (z - (k + l)) < e"
  1723         apply(rule_tac x="w + z" in exI) apply(rule,rule lem[OF w(1) z(1), unfolded *[THEN sym]])
  1724         using norm_triangle_ineq[of "w - k" "z - l"] w(2) z(2) by(auto simp add:field_simps)
  1725     qed qed qed
  1726 
  1727 lemma has_integral_sub:
  1728   shows "(f has_integral k) s \<Longrightarrow> (g has_integral l) s \<Longrightarrow> ((\<lambda>x. f(x) - g(x)) has_integral (k - l)) s"
  1729   using has_integral_add[OF _ has_integral_neg,of f k s g l] unfolding algebra_simps by auto
  1730 
  1731 lemma integral_0: "integral s (\<lambda>x::'n::ordered_euclidean_space. 0::'m::real_normed_vector) = 0"
  1732   by(rule integral_unique has_integral_0)+
  1733 
  1734 lemma integral_add:
  1735   shows "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow>
  1736    integral s (\<lambda>x. f x + g x) = integral s f + integral s g"
  1737   apply(rule integral_unique) apply(drule integrable_integral)+
  1738   apply(rule has_integral_add) by assumption+
  1739 
  1740 lemma integral_cmul:
  1741   shows "f integrable_on s \<Longrightarrow> integral s (\<lambda>x. c *\<^sub>R f x) = c *\<^sub>R integral s f"
  1742   apply(rule integral_unique) apply(drule integrable_integral)+
  1743   apply(rule has_integral_cmul) by assumption+
  1744 
  1745 lemma integral_neg:
  1746   shows "f integrable_on s \<Longrightarrow> integral s (\<lambda>x. - f x) = - integral s f"
  1747   apply(rule integral_unique) apply(drule integrable_integral)+
  1748   apply(rule has_integral_neg) by assumption+
  1749 
  1750 lemma integral_sub:
  1751   shows "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow> integral s (\<lambda>x. f x - g x) = integral s f - integral s g"
  1752   apply(rule integral_unique) apply(drule integrable_integral)+
  1753   apply(rule has_integral_sub) by assumption+
  1754 
  1755 lemma integrable_0: "(\<lambda>x. 0) integrable_on s"
  1756   unfolding integrable_on_def using has_integral_0 by auto
  1757 
  1758 lemma integrable_add:
  1759   shows "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow> (\<lambda>x. f x + g x) integrable_on s"
  1760   unfolding integrable_on_def by(auto intro: has_integral_add)
  1761 
  1762 lemma integrable_cmul:
  1763   shows "f integrable_on s \<Longrightarrow> (\<lambda>x. c *\<^sub>R f(x)) integrable_on s"
  1764   unfolding integrable_on_def by(auto intro: has_integral_cmul)
  1765 
  1766 lemma integrable_on_cmult_iff:
  1767   fixes c :: real assumes "c \<noteq> 0"
  1768   shows "(\<lambda>x. c * f x) integrable_on s \<longleftrightarrow> f integrable_on s"
  1769   using integrable_cmul[of "\<lambda>x. c * f x" s "1 / c"] integrable_cmul[of f s c] `c \<noteq> 0`
  1770   by auto
  1771 
  1772 lemma integrable_neg:
  1773   shows "f integrable_on s \<Longrightarrow> (\<lambda>x. -f(x)) integrable_on s"
  1774   unfolding integrable_on_def by(auto intro: has_integral_neg)
  1775 
  1776 lemma integrable_sub:
  1777   shows "f integrable_on s \<Longrightarrow> g integrable_on s \<Longrightarrow> (\<lambda>x. f x - g x) integrable_on s"
  1778   unfolding integrable_on_def by(auto intro: has_integral_sub)
  1779 
  1780 lemma integrable_linear:
  1781   shows "f integrable_on s \<Longrightarrow> bounded_linear h \<Longrightarrow> (h o f) integrable_on s"
  1782   unfolding integrable_on_def by(auto intro: has_integral_linear)
  1783 
  1784 lemma integral_linear:
  1785   shows "f integrable_on s \<Longrightarrow> bounded_linear h \<Longrightarrow> integral s (h o f) = h(integral s f)"
  1786   apply(rule has_integral_unique) defer unfolding has_integral_integral 
  1787   apply(drule has_integral_linear,assumption,assumption) unfolding has_integral_integral[THEN sym]
  1788   apply(rule integrable_linear) by assumption+
  1789 
  1790 lemma integral_component_eq[simp]: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'm::ordered_euclidean_space"
  1791   assumes "f integrable_on s" shows "integral s (\<lambda>x. f x $$ k) = integral s f $$ k"
  1792   unfolding integral_linear[OF assms(1) bounded_linear_component,unfolded o_def] ..
  1793 
  1794 lemma has_integral_setsum:
  1795   assumes "finite t" "\<forall>a\<in>t. ((f a) has_integral (i a)) s"
  1796   shows "((\<lambda>x. setsum (\<lambda>a. f a x) t) has_integral (setsum i t)) s"
  1797 proof(insert assms(1) subset_refl[of t],induct rule:finite_subset_induct)
  1798   case (insert x F) show ?case unfolding setsum_insert[OF insert(1,3)]
  1799     apply(rule has_integral_add) using insert assms by auto
  1800 qed auto
  1801 
  1802 lemma integral_setsum:
  1803   shows "finite t \<Longrightarrow> \<forall>a\<in>t. (f a) integrable_on s \<Longrightarrow>
  1804   integral s (\<lambda>x. setsum (\<lambda>a. f a x) t) = setsum (\<lambda>a. integral s (f a)) t"
  1805   apply(rule integral_unique) apply(rule has_integral_setsum)
  1806   using integrable_integral by auto
  1807 
  1808 lemma integrable_setsum:
  1809   shows "finite t \<Longrightarrow> \<forall>a \<in> t.(f a) integrable_on s \<Longrightarrow> (\<lambda>x. setsum (\<lambda>a. f a x) t) integrable_on s"
  1810   unfolding integrable_on_def apply(drule bchoice) using has_integral_setsum[of t] by auto
  1811 
  1812 lemma has_integral_eq:
  1813   assumes "\<forall>x\<in>s. f x = g x" "(f has_integral k) s" shows "(g has_integral k) s"
  1814   using has_integral_sub[OF assms(2), of "\<lambda>x. f x - g x" 0]
  1815   using has_integral_is_0[of s "\<lambda>x. f x - g x"] using assms(1) by auto
  1816 
  1817 lemma integrable_eq:
  1818   shows "\<forall>x\<in>s. f x = g x \<Longrightarrow> f integrable_on s \<Longrightarrow> g integrable_on s"
  1819   unfolding integrable_on_def using has_integral_eq[of s f g] by auto
  1820 
  1821 lemma has_integral_eq_eq:
  1822   shows "\<forall>x\<in>s. f x = g x \<Longrightarrow> ((f has_integral k) s \<longleftrightarrow> (g has_integral k) s)"
  1823   using has_integral_eq[of s f g] has_integral_eq[of s g f] by rule auto
  1824 
  1825 lemma has_integral_null[dest]:
  1826   assumes "content({a..b}) = 0" shows  "(f has_integral 0) ({a..b})"
  1827   unfolding has_integral apply(rule,rule,rule_tac x="\<lambda>x. ball x 1" in exI,rule) defer
  1828 proof(rule,rule,erule conjE) fix e::real assume e:"e>0" thus "gauge (\<lambda>x. ball x 1)" by auto
  1829   fix p assume p:"p tagged_division_of {a..b}" (*"(\<lambda>x. ball x 1) fine p"*)
  1830   have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) = 0" unfolding norm_eq_zero diff_0_right
  1831     using setsum_content_null[OF assms(1) p, of f] . 
  1832   thus "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e" using e by auto qed
  1833 
  1834 lemma has_integral_null_eq[simp]:
  1835   shows "content({a..b}) = 0 \<Longrightarrow> ((f has_integral i) ({a..b}) \<longleftrightarrow> i = 0)"
  1836   apply rule apply(rule has_integral_unique,assumption) 
  1837   apply(drule has_integral_null,assumption)
  1838   apply(drule has_integral_null) by auto
  1839 
  1840 lemma integral_null[dest]: shows "content({a..b}) = 0 \<Longrightarrow> integral({a..b}) f = 0"
  1841   by(rule integral_unique,drule has_integral_null)
  1842 
  1843 lemma integrable_on_null[dest]: shows "content({a..b}) = 0 \<Longrightarrow> f integrable_on {a..b}"
  1844   unfolding integrable_on_def apply(drule has_integral_null) by auto
  1845 
  1846 lemma has_integral_empty[intro]: shows "(f has_integral 0) {}"
  1847   unfolding empty_as_interval apply(rule has_integral_null) 
  1848   using content_empty unfolding empty_as_interval .
  1849 
  1850 lemma has_integral_empty_eq[simp]: shows "(f has_integral i) {} \<longleftrightarrow> i = 0"
  1851   apply(rule,rule has_integral_unique,assumption) by auto
  1852 
  1853 lemma integrable_on_empty[intro]: shows "f integrable_on {}" unfolding integrable_on_def by auto
  1854 
  1855 lemma integral_empty[simp]: shows "integral {} f = 0"
  1856   apply(rule integral_unique) using has_integral_empty .
  1857 
  1858 lemma has_integral_refl[intro]: shows "(f has_integral 0) {a..a}" "(f has_integral 0) {a::'a::ordered_euclidean_space}"
  1859 proof- have *:"{a} = {a..a}" apply(rule set_eqI) unfolding mem_interval singleton_iff euclidean_eq[where 'a='a]
  1860     apply safe prefer 3 apply(erule_tac x=i in allE) by(auto simp add: field_simps)
  1861   show "(f has_integral 0) {a..a}" "(f has_integral 0) {a}" unfolding *
  1862     apply(rule_tac[!] has_integral_null) unfolding content_eq_0_interior
  1863     unfolding interior_closed_interval using interval_sing by auto qed
  1864 
  1865 lemma integrable_on_refl[intro]: shows "f integrable_on {a..a}" unfolding integrable_on_def by auto
  1866 
  1867 lemma integral_refl: shows "integral {a..a} f = 0" apply(rule integral_unique) by auto
  1868 
  1869 subsection {* Cauchy-type criterion for integrability. *}
  1870 
  1871 (* XXXXXXX *)
  1872 lemma integrable_cauchy: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::{real_normed_vector,complete_space}" 
  1873   shows "f integrable_on {a..b} \<longleftrightarrow>
  1874   (\<forall>e>0.\<exists>d. gauge d \<and> (\<forall>p1 p2. p1 tagged_division_of {a..b} \<and> d fine p1 \<and>
  1875                             p2 tagged_division_of {a..b} \<and> d fine p2
  1876                             \<longrightarrow> norm(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p1 -
  1877                                      setsum (\<lambda>(x,k). content k *\<^sub>R f x) p2) < e))" (is "?l = (\<forall>e>0. \<exists>d. ?P e d)")
  1878 proof assume ?l
  1879   then guess y unfolding integrable_on_def has_integral .. note y=this
  1880   show "\<forall>e>0. \<exists>d. ?P e d" proof(rule,rule) case goal1 hence "e/2 > 0" by auto
  1881     then guess d apply- apply(drule y[rule_format]) by(erule exE,erule conjE) note d=this[rule_format]
  1882     show ?case apply(rule_tac x=d in exI,rule,rule d) apply(rule,rule,rule,(erule conjE)+)
  1883     proof- fix p1 p2 assume as:"p1 tagged_division_of {a..b}" "d fine p1" "p2 tagged_division_of {a..b}" "d fine p2"
  1884       show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
  1885         apply(rule dist_triangle_half_l[where y=y,unfolded dist_norm])
  1886         using d(2)[OF conjI[OF as(1-2)]] d(2)[OF conjI[OF as(3-4)]] .
  1887     qed qed
  1888 next assume "\<forall>e>0. \<exists>d. ?P e d" hence "\<forall>n::nat. \<exists>d. ?P (inverse(real (n + 1))) d" by auto
  1889   from choice[OF this] guess d .. note d=conjunctD2[OF this[rule_format],rule_format]
  1890   have "\<And>n. gauge (\<lambda>x. \<Inter>{d i x |i. i \<in> {0..n}})" apply(rule gauge_inters) using d(1) by auto
  1891   hence "\<forall>n. \<exists>p. p tagged_division_of {a..b} \<and> (\<lambda>x. \<Inter>{d i x |i. i \<in> {0..n}}) fine p" apply-
  1892   proof case goal1 from this[of n] show ?case apply(drule_tac fine_division_exists) by auto qed
  1893   from choice[OF this] guess p .. note p = conjunctD2[OF this[rule_format]]
  1894   have dp:"\<And>i n. i\<le>n \<Longrightarrow> d i fine p n" using p(2) unfolding fine_inters by auto
  1895   have "Cauchy (\<lambda>n. setsum (\<lambda>(x,k). content k *\<^sub>R (f x)) (p n))"
  1896   proof(rule CauchyI) case goal1 then guess N unfolding real_arch_inv[of e] .. note N=this
  1897     show ?case apply(rule_tac x=N in exI)
  1898     proof(rule,rule,rule,rule) fix m n assume mn:"N \<le> m" "N \<le> n" have *:"N = (N - 1) + 1" using N by auto
  1899       show "norm ((\<Sum>(x, k)\<in>p m. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p n. content k *\<^sub>R f x)) < e"
  1900         apply(rule less_trans[OF _ N[THEN conjunct2,THEN conjunct2]]) apply(subst *) apply(rule d(2))
  1901         using dp p(1) using mn by auto 
  1902     qed qed
  1903   then guess y unfolding convergent_eq_cauchy[THEN sym] .. note y=this[THEN LIMSEQ_D]
  1904   show ?l unfolding integrable_on_def has_integral apply(rule_tac x=y in exI)
  1905   proof(rule,rule) fix e::real assume "e>0" hence *:"e/2 > 0" by auto
  1906     then guess N1 unfolding real_arch_inv[of "e/2"] .. note N1=this hence N1':"N1 = N1 - 1 + 1" by auto
  1907     guess N2 using y[OF *] .. note N2=this
  1908     show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - y) < e)"
  1909       apply(rule_tac x="d (N1 + N2)" in exI) apply rule defer 
  1910     proof(rule,rule,erule conjE) show "gauge (d (N1 + N2))" using d by auto
  1911       fix q assume as:"q tagged_division_of {a..b}" "d (N1 + N2) fine q"
  1912       have *:"inverse (real (N1 + N2 + 1)) < e / 2" apply(rule less_trans) using N1 by auto
  1913       show "norm ((\<Sum>(x, k)\<in>q. content k *\<^sub>R f x) - y) < e" apply(rule norm_triangle_half_r)
  1914         apply(rule less_trans[OF _ *]) apply(subst N1', rule d(2)[of "p (N1+N2)"]) defer
  1915         using N2[rule_format,of "N1+N2"]
  1916         using as dp[of "N1 - 1 + 1 + N2" "N1 + N2"] using p(1)[of "N1 + N2"] using N1 by auto qed qed qed
  1917 
  1918 subsection {* Additivity of integral on abutting intervals. *}
  1919 
  1920 lemma interval_split: fixes a::"'a::ordered_euclidean_space" assumes "k<DIM('a)" shows
  1921   "{a..b} \<inter> {x. x$$k \<le> c} = {a .. (\<chi>\<chi> i. if i = k then min (b$$k) c else b$$i)}"
  1922   "{a..b} \<inter> {x. x$$k \<ge> c} = {(\<chi>\<chi> i. if i = k then max (a$$k) c else a$$i) .. b}"
  1923   apply(rule_tac[!] set_eqI) unfolding Int_iff mem_interval mem_Collect_eq using assms by auto
  1924 
  1925 lemma content_split: fixes a::"'a::ordered_euclidean_space" assumes "k<DIM('a)" shows
  1926   "content {a..b} = content({a..b} \<inter> {x. x$$k \<le> c}) + content({a..b} \<inter> {x. x$$k >= c})"
  1927 proof- note simps = interval_split[OF assms] content_closed_interval_cases eucl_le[where 'a='a]
  1928   { presume "a\<le>b \<Longrightarrow> ?thesis" thus ?thesis apply(cases "a\<le>b") unfolding simps using assms by auto }
  1929   have *:"{..<DIM('a)} = insert k ({..<DIM('a)} - {k})" "\<And>x. finite ({..<DIM('a)}-{x})" "\<And>x. x\<notin>{..<DIM('a)}-{x}"
  1930     using assms by auto
  1931   have *:"\<And>X Y Z. (\<Prod>i\<in>{..<DIM('a)}. Z i (if i = k then X else Y i)) = Z k X * (\<Prod>i\<in>{..<DIM('a)}-{k}. Z i (Y i))"
  1932     "(\<Prod>i\<in>{..<DIM('a)}. b$$i - a$$i) = (\<Prod>i\<in>{..<DIM('a)}-{k}. b$$i - a$$i) * (b$$k - a$$k)" 
  1933     apply(subst *(1)) defer apply(subst *(1)) unfolding setprod_insert[OF *(2-)] by auto
  1934   assume as:"a\<le>b" moreover have "\<And>x. min (b $$ k) c = max (a $$ k) c
  1935     \<Longrightarrow> x* (b$$k - a$$k) = x*(max (a $$ k) c - a $$ k) + x*(b $$ k - max (a $$ k) c)"
  1936     by  (auto simp add:field_simps)
  1937   moreover have **:"(\<Prod>i<DIM('a). ((\<chi>\<chi> i. if i = k then min (b $$ k) c else b $$ i)::'a) $$ i - a $$ i) = 
  1938     (\<Prod>i<DIM('a). (if i = k then min (b $$ k) c else b $$ i) - a $$ i)"
  1939     "(\<Prod>i<DIM('a). b $$ i - ((\<chi>\<chi> i. if i = k then max (a $$ k) c else a $$ i)::'a) $$ i) =
  1940     (\<Prod>i<DIM('a). b $$ i - (if i = k then max (a $$ k) c else a $$ i))"
  1941     apply(rule_tac[!] setprod.cong) by auto
  1942   have "\<not> a $$ k \<le> c \<Longrightarrow> \<not> c \<le> b $$ k \<Longrightarrow> False"
  1943     unfolding not_le using as[unfolded eucl_le[where 'a='a],rule_format,of k] assms by auto
  1944   ultimately show ?thesis using assms unfolding simps **
  1945     unfolding *(1)[of "\<lambda>i x. b$$i - x"] *(1)[of "\<lambda>i x. x - a$$i"] unfolding  *(2) 
  1946     apply(subst(2) euclidean_lambda_beta''[where 'a='a])
  1947     apply(subst(3) euclidean_lambda_beta''[where 'a='a]) by auto
  1948 qed
  1949 
  1950 lemma division_split_left_inj: fixes type::"'a::ordered_euclidean_space"
  1951   assumes "d division_of i" "k1 \<in> d" "k2 \<in> d"  "k1 \<noteq> k2" 
  1952   "k1 \<inter> {x::'a. x$$k \<le> c} = k2 \<inter> {x. x$$k \<le> c}"and k:"k<DIM('a)"
  1953   shows "content(k1 \<inter> {x. x$$k \<le> c}) = 0"
  1954 proof- note d=division_ofD[OF assms(1)]
  1955   have *:"\<And>a b::'a. \<And> c. (content({a..b} \<inter> {x. x$$k \<le> c}) = 0 \<longleftrightarrow> interior({a..b} \<inter> {x. x$$k \<le> c}) = {})"
  1956     unfolding  interval_split[OF k] content_eq_0_interior by auto
  1957   guess u1 v1 using d(4)[OF assms(2)] apply-by(erule exE)+ note uv1=this
  1958   guess u2 v2 using d(4)[OF assms(3)] apply-by(erule exE)+ note uv2=this
  1959   have **:"\<And>s t u. s \<inter> t = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<subseteq> t \<Longrightarrow> u = {}" by auto
  1960   show ?thesis unfolding uv1 uv2 * apply(rule **[OF d(5)[OF assms(2-4)]])
  1961     defer apply(subst assms(5)[unfolded uv1 uv2]) unfolding uv1 uv2 by auto qed
  1962  
  1963 lemma division_split_right_inj: fixes type::"'a::ordered_euclidean_space"
  1964   assumes "d division_of i" "k1 \<in> d" "k2 \<in> d"  "k1 \<noteq> k2"
  1965   "k1 \<inter> {x::'a. x$$k \<ge> c} = k2 \<inter> {x. x$$k \<ge> c}" and k:"k<DIM('a)"
  1966   shows "content(k1 \<inter> {x. x$$k \<ge> c}) = 0"
  1967 proof- note d=division_ofD[OF assms(1)]
  1968   have *:"\<And>a b::'a. \<And> c. (content({a..b} \<inter> {x. x$$k >= c}) = 0 \<longleftrightarrow> interior({a..b} \<inter> {x. x$$k >= c}) = {})"
  1969     unfolding interval_split[OF k] content_eq_0_interior by auto
  1970   guess u1 v1 using d(4)[OF assms(2)] apply-by(erule exE)+ note uv1=this
  1971   guess u2 v2 using d(4)[OF assms(3)] apply-by(erule exE)+ note uv2=this
  1972   have **:"\<And>s t u. s \<inter> t = {} \<Longrightarrow> u \<subseteq> s \<Longrightarrow> u \<subseteq> t \<Longrightarrow> u = {}" by auto
  1973   show ?thesis unfolding uv1 uv2 * apply(rule **[OF d(5)[OF assms(2-4)]])
  1974     defer apply(subst assms(5)[unfolded uv1 uv2]) unfolding uv1 uv2 by auto qed
  1975 
  1976 lemma tagged_division_split_left_inj: fixes x1::"'a::ordered_euclidean_space"
  1977   assumes "d tagged_division_of i" "(x1,k1) \<in> d" "(x2,k2) \<in> d" "k1 \<noteq> k2"  "k1 \<inter> {x. x$$k \<le> c} = k2 \<inter> {x. x$$k \<le> c}" 
  1978   and k:"k<DIM('a)"
  1979   shows "content(k1 \<inter> {x. x$$k \<le> c}) = 0"
  1980 proof- have *:"\<And>a b c. (a,b) \<in> c \<Longrightarrow> b \<in> snd ` c" unfolding image_iff apply(rule_tac x="(a,b)" in bexI) by auto
  1981   show ?thesis apply(rule division_split_left_inj[OF division_of_tagged_division[OF assms(1)]])
  1982     apply(rule_tac[1-2] *) using assms(2-) by auto qed
  1983 
  1984 lemma tagged_division_split_right_inj: fixes x1::"'a::ordered_euclidean_space"
  1985   assumes "d tagged_division_of i" "(x1,k1) \<in> d" "(x2,k2) \<in> d" "k1 \<noteq> k2"  "k1 \<inter> {x. x$$k \<ge> c} = k2 \<inter> {x. x$$k \<ge> c}" 
  1986   and k:"k<DIM('a)"
  1987   shows "content(k1 \<inter> {x. x$$k \<ge> c}) = 0"
  1988 proof- have *:"\<And>a b c. (a,b) \<in> c \<Longrightarrow> b \<in> snd ` c" unfolding image_iff apply(rule_tac x="(a,b)" in bexI) by auto
  1989   show ?thesis apply(rule division_split_right_inj[OF division_of_tagged_division[OF assms(1)]])
  1990     apply(rule_tac[1-2] *) using assms(2-) by auto qed
  1991 
  1992 lemma division_split: fixes a::"'a::ordered_euclidean_space"
  1993   assumes "p division_of {a..b}" and k:"k<DIM('a)"
  1994   shows "{l \<inter> {x. x$$k \<le> c} | l. l \<in> p \<and> ~(l \<inter> {x. x$$k \<le> c} = {})} division_of({a..b} \<inter> {x. x$$k \<le> c})" (is "?p1 division_of ?I1") and 
  1995         "{l \<inter> {x. x$$k \<ge> c} | l. l \<in> p \<and> ~(l \<inter> {x. x$$k \<ge> c} = {})} division_of ({a..b} \<inter> {x. x$$k \<ge> c})" (is "?p2 division_of ?I2")
  1996 proof(rule_tac[!] division_ofI) note p=division_ofD[OF assms(1)]
  1997   show "finite ?p1" "finite ?p2" using p(1) by auto show "\<Union>?p1 = ?I1" "\<Union>?p2 = ?I2" unfolding p(6)[THEN sym] by auto
  1998   { fix k assume "k\<in>?p1" then guess l unfolding mem_Collect_eq apply-by(erule exE,(erule conjE)+) note l=this
  1999     guess u v using p(4)[OF l(2)] apply-by(erule exE)+ note uv=this
  2000     show "k\<subseteq>?I1" "k \<noteq> {}" "\<exists>a b. k = {a..b}" unfolding l
  2001       using p(2-3)[OF l(2)] l(3) unfolding uv apply- prefer 3 apply(subst interval_split[OF k]) by auto
  2002     fix k' assume "k'\<in>?p1" then guess l' unfolding mem_Collect_eq apply-by(erule exE,(erule conjE)+) note l'=this
  2003     assume "k\<noteq>k'" thus "interior k \<inter> interior k' = {}" unfolding l l' using p(5)[OF l(2) l'(2)] by auto }
  2004   { fix k assume "k\<in>?p2" then guess l unfolding mem_Collect_eq apply-by(erule exE,(erule conjE)+) note l=this
  2005     guess u v using p(4)[OF l(2)] apply-by(erule exE)+ note uv=this
  2006     show "k\<subseteq>?I2" "k \<noteq> {}" "\<exists>a b. k = {a..b}" unfolding l
  2007       using p(2-3)[OF l(2)] l(3) unfolding uv apply- prefer 3 apply(subst interval_split[OF k]) by auto
  2008     fix k' assume "k'\<in>?p2" then guess l' unfolding mem_Collect_eq apply-by(erule exE,(erule conjE)+) note l'=this
  2009     assume "k\<noteq>k'" thus "interior k \<inter> interior k' = {}" unfolding l l' using p(5)[OF l(2) l'(2)] by auto }
  2010 qed
  2011 
  2012 lemma has_integral_split: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::real_normed_vector"
  2013   assumes "(f has_integral i) ({a..b} \<inter> {x. x$$k \<le> c})"  "(f has_integral j) ({a..b} \<inter> {x. x$$k \<ge> c})" and k:"k<DIM('a)"
  2014   shows "(f has_integral (i + j)) ({a..b})"
  2015 proof(unfold has_integral,rule,rule) case goal1 hence e:"e/2>0" by auto
  2016   guess d1 using has_integralD[OF assms(1)[unfolded interval_split[OF k]] e] . note d1=this[unfolded interval_split[THEN sym,OF k]]
  2017   guess d2 using has_integralD[OF assms(2)[unfolded interval_split[OF k]] e] . note d2=this[unfolded interval_split[THEN sym,OF k]]
  2018   let ?d = "\<lambda>x. if x$$k = c then (d1 x \<inter> d2 x) else ball x (abs(x$$k - c)) \<inter> d1 x \<inter> d2 x"
  2019   show ?case apply(rule_tac x="?d" in exI,rule) defer apply(rule,rule,(erule conjE)+)
  2020   proof- show "gauge ?d" using d1(1) d2(1) unfolding gauge_def by auto
  2021     fix p assume "p tagged_division_of {a..b}" "?d fine p" note p = this tagged_division_ofD[OF this(1)]
  2022     have lem0:"\<And>x kk. (x,kk) \<in> p \<Longrightarrow> ~(kk \<inter> {x. x$$k \<le> c} = {}) \<Longrightarrow> x$$k \<le> c"
  2023          "\<And>x kk. (x,kk) \<in> p \<Longrightarrow> ~(kk \<inter> {x. x$$k \<ge> c} = {}) \<Longrightarrow> x$$k \<ge> c"
  2024     proof- fix x kk assume as:"(x,kk)\<in>p"
  2025       show "~(kk \<inter> {x. x$$k \<le> c} = {}) \<Longrightarrow> x$$k \<le> c"
  2026       proof(rule ccontr) case goal1
  2027         from this(2)[unfolded not_le] have "kk \<subseteq> ball x \<bar>x $$ k - c\<bar>"
  2028           using p(2)[unfolded fine_def,rule_format,OF as,unfolded split_conv] by auto
  2029         hence "\<exists>y. y \<in> ball x \<bar>x $$ k - c\<bar> \<inter> {x. x $$ k \<le> c}" using goal1(1) by blast 
  2030         then guess y .. hence "\<bar>x $$ k - y $$ k\<bar> < \<bar>x $$ k - c\<bar>" "y$$k \<le> c" apply-apply(rule le_less_trans)
  2031           using component_le_norm[of "x - y" k] by(auto simp add:dist_norm)
  2032         thus False using goal1(2)[unfolded not_le] by(auto simp add:field_simps)
  2033       qed
  2034       show "~(kk \<inter> {x. x$$k \<ge> c} = {}) \<Longrightarrow> x$$k \<ge> c"
  2035       proof(rule ccontr) case goal1
  2036         from this(2)[unfolded not_le] have "kk \<subseteq> ball x \<bar>x $$ k - c\<bar>"
  2037           using p(2)[unfolded fine_def,rule_format,OF as,unfolded split_conv] by auto
  2038         hence "\<exists>y. y \<in> ball x \<bar>x $$ k - c\<bar> \<inter> {x. x $$ k \<ge> c}" using goal1(1) by blast 
  2039         then guess y .. hence "\<bar>x $$ k - y $$ k\<bar> < \<bar>x $$ k - c\<bar>" "y$$k \<ge> c" apply-apply(rule le_less_trans)
  2040           using component_le_norm[of "x - y" k] by(auto simp add:dist_norm)
  2041         thus False using goal1(2)[unfolded not_le] by(auto simp add:field_simps)
  2042       qed
  2043     qed
  2044 
  2045     have lem1: "\<And>f P Q. (\<forall>x k. (x,k) \<in> {(x,f k) | x k. P x k} \<longrightarrow> Q x k) \<longleftrightarrow> (\<forall>x k. P x k \<longrightarrow> Q x (f k))" by auto
  2046     have lem2: "\<And>f s P f. finite s \<Longrightarrow> finite {(x,f k) | x k. (x,k) \<in> s \<and> P x k}"
  2047     proof- case goal1 thus ?case apply-apply(rule finite_subset[of _ "(\<lambda>(x,k). (x,f k)) ` s"]) by auto qed
  2048     have lem3: "\<And>g::'a set \<Rightarrow> 'a set. finite p \<Longrightarrow>
  2049       setsum (\<lambda>(x,k). content k *\<^sub>R f x) {(x,g k) |x k. (x,k) \<in> p \<and> ~(g k = {})}
  2050                = setsum (\<lambda>(x,k). content k *\<^sub>R f x) ((\<lambda>(x,k). (x,g k)) ` p)"
  2051       apply(rule setsum_mono_zero_left) prefer 3
  2052     proof fix g::"'a set \<Rightarrow> 'a set" and i::"('a) \<times> (('a) set)"
  2053       assume "i \<in> (\<lambda>(x, k). (x, g k)) ` p - {(x, g k) |x k. (x, k) \<in> p \<and> g k \<noteq> {}}"
  2054       then obtain x k where xk:"i=(x,g k)" "(x,k)\<in>p" "(x,g k) \<notin> {(x, g k) |x k. (x, k) \<in> p \<and> g k \<noteq> {}}" by auto
  2055       have "content (g k) = 0" using xk using content_empty by auto
  2056       thus "(\<lambda>(x, k). content k *\<^sub>R f x) i = 0" unfolding xk split_conv by auto
  2057     qed auto
  2058     have lem4:"\<And>g. (\<lambda>(x,l). content (g l) *\<^sub>R f x) = (\<lambda>(x,l). content l *\<^sub>R f x) o (\<lambda>(x,l). (x,g l))" apply(rule ext) by auto
  2059 
  2060     let ?M1 = "{(x,kk \<inter> {x. x$$k \<le> c}) |x kk. (x,kk) \<in> p \<and> kk \<inter> {x. x$$k \<le> c} \<noteq> {}}"
  2061     have "norm ((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) < e/2" apply(rule d1(2),rule tagged_division_ofI)
  2062       apply(rule lem2 p(3))+ prefer 6 apply(rule fineI)
  2063     proof- show "\<Union>{k. \<exists>x. (x, k) \<in> ?M1} = {a..b} \<inter> {x. x$$k \<le> c}" unfolding p(8)[THEN sym] by auto
  2064       fix x l assume xl:"(x,l)\<in>?M1"
  2065       then guess x' l' unfolding mem_Collect_eq apply- unfolding Pair_eq apply((erule exE)+,(erule conjE)+) .  note xl'=this
  2066       have "l' \<subseteq> d1 x'" apply(rule order_trans[OF fineD[OF p(2) xl'(3)]]) by auto
  2067       thus "l \<subseteq> d1 x" unfolding xl' by auto
  2068       show "x\<in>l" "l \<subseteq> {a..b} \<inter> {x. x $$ k \<le> c}" unfolding xl' using p(4-6)[OF xl'(3)] using xl'(4)
  2069         using lem0(1)[OF xl'(3-4)] by auto
  2070       show  "\<exists>a b. l = {a..b}" unfolding xl' using p(6)[OF xl'(3)] by(fastforce simp add: interval_split[OF k,where c=c])
  2071       fix y r let ?goal = "interior l \<inter> interior r = {}" assume yr:"(y,r)\<in>?M1"
  2072       then guess y' r' unfolding mem_Collect_eq apply- unfolding Pair_eq apply((erule exE)+,(erule conjE)+) .  note yr'=this
  2073       assume as:"(x,l) \<noteq> (y,r)" show "interior l \<inter> interior r = {}"
  2074       proof(cases "l' = r' \<longrightarrow> x' = y'")
  2075         case False thus ?thesis using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  2076       next case True hence "l' \<noteq> r'" using as unfolding xl' yr' by auto
  2077         thus ?thesis using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  2078       qed qed moreover
  2079 
  2080     let ?M2 = "{(x,kk \<inter> {x. x$$k \<ge> c}) |x kk. (x,kk) \<in> p \<and> kk \<inter> {x. x$$k \<ge> c} \<noteq> {}}" 
  2081     have "norm ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j) < e/2" apply(rule d2(2),rule tagged_division_ofI)
  2082       apply(rule lem2 p(3))+ prefer 6 apply(rule fineI)
  2083     proof- show "\<Union>{k. \<exists>x. (x, k) \<in> ?M2} = {a..b} \<inter> {x. x$$k \<ge> c}" unfolding p(8)[THEN sym] by auto
  2084       fix x l assume xl:"(x,l)\<in>?M2"
  2085       then guess x' l' unfolding mem_Collect_eq apply- unfolding Pair_eq apply((erule exE)+,(erule conjE)+) .  note xl'=this
  2086       have "l' \<subseteq> d2 x'" apply(rule order_trans[OF fineD[OF p(2) xl'(3)]]) by auto
  2087       thus "l \<subseteq> d2 x" unfolding xl' by auto
  2088       show "x\<in>l" "l \<subseteq> {a..b} \<inter> {x. x $$ k \<ge> c}" unfolding xl' using p(4-6)[OF xl'(3)] using xl'(4)
  2089         using lem0(2)[OF xl'(3-4)] by auto
  2090       show  "\<exists>a b. l = {a..b}" unfolding xl' using p(6)[OF xl'(3)] by(fastforce simp add: interval_split[OF k, where c=c])
  2091       fix y r let ?goal = "interior l \<inter> interior r = {}" assume yr:"(y,r)\<in>?M2"
  2092       then guess y' r' unfolding mem_Collect_eq apply- unfolding Pair_eq apply((erule exE)+,(erule conjE)+) .  note yr'=this
  2093       assume as:"(x,l) \<noteq> (y,r)" show "interior l \<inter> interior r = {}"
  2094       proof(cases "l' = r' \<longrightarrow> x' = y'")
  2095         case False thus ?thesis using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  2096       next case True hence "l' \<noteq> r'" using as unfolding xl' yr' by auto
  2097         thus ?thesis using p(7)[OF xl'(3) yr'(3)] using as unfolding xl' yr' by auto
  2098       qed qed ultimately
  2099 
  2100     have "norm (((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) + ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j)) < e/2 + e/2"
  2101       apply- apply(rule norm_triangle_lt) by auto
  2102     also { have *:"\<And>x y. x = (0::real) \<Longrightarrow> x *\<^sub>R (y::'b) = 0" using scaleR_zero_left by auto
  2103       have "((\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) - i) + ((\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - j)
  2104        = (\<Sum>(x, k)\<in>?M1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>?M2. content k *\<^sub>R f x) - (i + j)" by auto
  2105       also have "\<dots> = (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. x $$ k \<le> c}) *\<^sub>R f x) +
  2106         (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. c \<le> x $$ k}) *\<^sub>R f x) - (i + j)"
  2107         unfolding lem3[OF p(3)] apply(subst setsum_reindex_nonzero[OF p(3)]) defer apply(subst setsum_reindex_nonzero[OF p(3)])
  2108         defer unfolding lem4[THEN sym] apply(rule refl) unfolding split_paired_all split_conv apply(rule_tac[!] *)
  2109       proof- case goal1 thus ?case apply- apply(rule tagged_division_split_left_inj [OF p(1), of a b aa ba]) using k by auto
  2110       next case   goal2 thus ?case apply- apply(rule tagged_division_split_right_inj[OF p(1), of a b aa ba]) using k by auto
  2111       qed also note setsum_addf[THEN sym]
  2112       also have *:"\<And>x. x\<in>p \<Longrightarrow> (\<lambda>(x, ka). content (ka \<inter> {x. x $$ k \<le> c}) *\<^sub>R f x) x + (\<lambda>(x, ka). content (ka \<inter> {x. c \<le> x $$ k}) *\<^sub>R f x) x
  2113         = (\<lambda>(x,ka). content ka *\<^sub>R f x) x" unfolding split_paired_all split_conv
  2114       proof- fix a b assume "(a,b) \<in> p" from p(6)[OF this] guess u v apply-by(erule exE)+ note uv=this
  2115         thus "content (b \<inter> {x. x $$ k \<le> c}) *\<^sub>R f a + content (b \<inter> {x. c \<le> x $$ k}) *\<^sub>R f a = content b *\<^sub>R f a"
  2116           unfolding scaleR_left_distrib[THEN sym] unfolding uv content_split[OF k,of u v c] by auto
  2117       qed note setsum_cong2[OF this]
  2118       finally have "(\<Sum>(x, k)\<in>{(x, kk \<inter> {x. x $$ k \<le> c}) |x kk. (x, kk) \<in> p \<and> kk \<inter> {x. x $$ k \<le> c} \<noteq> {}}. content k *\<^sub>R f x) - i +
  2119         ((\<Sum>(x, k)\<in>{(x, kk \<inter> {x. c \<le> x $$ k}) |x kk. (x, kk) \<in> p \<and> kk \<inter> {x. c \<le> x $$ k} \<noteq> {}}. content k *\<^sub>R f x) - j) =
  2120         (\<Sum>(x, ka)\<in>p. content ka *\<^sub>R f x) - (i + j)" by auto }
  2121     finally show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - (i + j)) < e" by auto qed qed
  2122 
  2123 (*lemma has_integral_split_cart: fixes f::"real^'n \<Rightarrow> 'a::real_normed_vector"
  2124   assumes "(f has_integral i) ({a..b} \<inter> {x. x$k \<le> c})"  "(f has_integral j) ({a..b} \<inter> {x. x$k \<ge> c})"
  2125   shows "(f has_integral (i + j)) ({a..b})" *)
  2126 
  2127 subsection {* A sort of converse, integrability on subintervals. *}
  2128 
  2129 lemma tagged_division_union_interval: fixes a::"'a::ordered_euclidean_space"
  2130   assumes "p1 tagged_division_of ({a..b} \<inter> {x. x$$k \<le> (c::real)})"  "p2 tagged_division_of ({a..b} \<inter> {x. x$$k \<ge> c})"
  2131   and k:"k<DIM('a)"
  2132   shows "(p1 \<union> p2) tagged_division_of ({a..b})"
  2133 proof- have *:"{a..b} = ({a..b} \<inter> {x. x$$k \<le> c}) \<union> ({a..b} \<inter> {x. x$$k \<ge> c})" by auto
  2134   show ?thesis apply(subst *) apply(rule tagged_division_union[OF assms(1-2)])
  2135     unfolding interval_split[OF k] interior_closed_interval using k
  2136     by(auto simp add: eucl_less[where 'a='a] elim!:allE[where x=k]) qed
  2137 
  2138 lemma has_integral_separate_sides: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::real_normed_vector"
  2139   assumes "(f has_integral i) ({a..b})" "e>0" and k:"k<DIM('a)"
  2140   obtains d where "gauge d" "(\<forall>p1 p2. p1 tagged_division_of ({a..b} \<inter> {x. x$$k \<le> c}) \<and> d fine p1 \<and>
  2141                                 p2 tagged_division_of ({a..b} \<inter> {x. x$$k \<ge> c}) \<and> d fine p2
  2142                                 \<longrightarrow> norm((setsum (\<lambda>(x,k). content k *\<^sub>R f x) p1 +
  2143                                           setsum (\<lambda>(x,k). content k *\<^sub>R f x) p2) - i) < e)"
  2144 proof- guess d using has_integralD[OF assms(1-2)] . note d=this
  2145   show ?thesis apply(rule that[of d]) apply(rule d) apply(rule,rule,rule,(erule conjE)+)
  2146   proof- fix p1 p2 assume "p1 tagged_division_of {a..b} \<inter> {x. x $$ k \<le> c}" "d fine p1" note p1=tagged_division_ofD[OF this(1)] this
  2147                    assume "p2 tagged_division_of {a..b} \<inter> {x. c \<le> x $$ k}" "d fine p2" note p2=tagged_division_ofD[OF this(1)] this
  2148     note tagged_division_union_interval[OF p1(7) p2(7)] note p12 = tagged_division_ofD[OF this] this
  2149     have "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x) - i) = norm ((\<Sum>(x, k)\<in>p1 \<union> p2. content k *\<^sub>R f x) - i)"
  2150       apply(subst setsum_Un_zero) apply(rule p1 p2)+ apply(rule) unfolding split_paired_all split_conv
  2151     proof- fix a b assume ab:"(a,b) \<in> p1 \<inter> p2"
  2152       have "(a,b) \<in> p1" using ab by auto from p1(4)[OF this] guess u v apply-by(erule exE)+ note uv =this
  2153       have "b \<subseteq> {x. x$$k = c}" using ab p1(3)[of a b] p2(3)[of a b] by fastforce
  2154       moreover have "interior {x::'a. x $$ k = c} = {}" 
  2155       proof(rule ccontr) case goal1 then obtain x where x:"x\<in>interior {x::'a. x$$k = c}" by auto
  2156         then guess e unfolding mem_interior .. note e=this
  2157         have x:"x$$k = c" using x interior_subset by fastforce
  2158         have *:"\<And>i. i<DIM('a) \<Longrightarrow> \<bar>(x - (x + (\<chi>\<chi> i. if i = k then e / 2 else 0))) $$ i\<bar>
  2159           = (if i = k then e/2 else 0)" using e by auto
  2160         have "(\<Sum>i<DIM('a). \<bar>(x - (x + (\<chi>\<chi> i. if i = k then e / 2 else 0))) $$ i\<bar>) =
  2161           (\<Sum>i<DIM('a). (if i = k then e / 2 else 0))" apply(rule setsum_cong2) apply(subst *) by auto
  2162         also have "... < e" apply(subst setsum_delta) using e by auto 
  2163         finally have "x + (\<chi>\<chi> i. if i = k then e/2 else 0) \<in> ball x e" unfolding mem_ball dist_norm
  2164           by(rule le_less_trans[OF norm_le_l1])
  2165         hence "x + (\<chi>\<chi> i. if i = k then e/2 else 0) \<in> {x. x$$k = c}" using e by auto
  2166         thus False unfolding mem_Collect_eq using e x k by auto
  2167       qed ultimately have "content b = 0" unfolding uv content_eq_0_interior apply-apply(drule interior_mono) by auto
  2168       thus "content b *\<^sub>R f a = 0" by auto
  2169     qed auto
  2170     also have "\<dots> < e" by(rule k d(2) p12 fine_union p1 p2)+
  2171     finally show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x) - i) < e" . qed qed
  2172 
  2173 lemma integrable_split[intro]: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::{real_normed_vector,complete_space}"
  2174   assumes "f integrable_on {a..b}" and k:"k<DIM('a)"
  2175   shows "f integrable_on ({a..b} \<inter> {x. x$$k \<le> c})" (is ?t1) and "f integrable_on ({a..b} \<inter> {x. x$$k \<ge> c})" (is ?t2) 
  2176 proof- guess y using assms(1) unfolding integrable_on_def .. note y=this
  2177   def b' \<equiv> "(\<chi>\<chi> i. if i = k then min (b$$k) c else b$$i)::'a"
  2178   and a' \<equiv> "(\<chi>\<chi> i. if i = k then max (a$$k) c else a$$i)::'a"
  2179   show ?t1 ?t2 unfolding interval_split[OF k] integrable_cauchy unfolding interval_split[THEN sym,OF k]
  2180   proof(rule_tac[!] allI impI)+ fix e::real assume "e>0" hence "e/2>0" by auto
  2181     from has_integral_separate_sides[OF y this k,of c] guess d . note d=this[rule_format]
  2182     let ?P = "\<lambda>A. \<exists>d. gauge d \<and> (\<forall>p1 p2. p1 tagged_division_of {a..b} \<inter> A \<and> d fine p1
  2183       \<and> p2 tagged_division_of {a..b} \<inter> A \<and> d fine p2 \<longrightarrow>
  2184       norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e)"
  2185     show "?P {x. x $$ k \<le> c}" apply(rule_tac x=d in exI) apply(rule,rule d) apply(rule,rule,rule)
  2186     proof- fix p1 p2 assume as:"p1 tagged_division_of {a..b} \<inter> {x. x $$ k \<le> c} \<and> d fine p1
  2187         \<and> p2 tagged_division_of {a..b} \<inter> {x. x $$ k \<le> c} \<and> d fine p2"
  2188       show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
  2189       proof- guess p using fine_division_exists[OF d(1), of a' b] . note p=this
  2190         show ?thesis using norm_triangle_half_l[OF d(2)[of p1 p] d(2)[of p2 p]]
  2191           using as unfolding interval_split[OF k] b'_def[symmetric] a'_def[symmetric]
  2192           using p using assms by(auto simp add:algebra_simps)
  2193       qed qed  
  2194     show "?P {x. x $$ k \<ge> c}" apply(rule_tac x=d in exI) apply(rule,rule d) apply(rule,rule,rule)
  2195     proof- fix p1 p2 assume as:"p1 tagged_division_of {a..b} \<inter> {x. x $$ k \<ge> c} \<and> d fine p1
  2196         \<and> p2 tagged_division_of {a..b} \<inter> {x. x $$ k \<ge> c} \<and> d fine p2"
  2197       show "norm ((\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)) < e"
  2198       proof- guess p using fine_division_exists[OF d(1), of a b'] . note p=this
  2199         show ?thesis using norm_triangle_half_l[OF d(2)[of p p1] d(2)[of p p2]]
  2200           using as unfolding interval_split[OF k] b'_def[symmetric] a'_def[symmetric]
  2201           using p using assms by(auto simp add:algebra_simps) qed qed qed qed
  2202 
  2203 subsection {* Generalized notion of additivity. *}
  2204 
  2205 definition "neutral opp = (SOME x. \<forall>y. opp x y = y \<and> opp y x = y)"
  2206 
  2207 definition operative :: "('a \<Rightarrow> 'a \<Rightarrow> 'a) \<Rightarrow> (('b::ordered_euclidean_space) set \<Rightarrow> 'a) \<Rightarrow> bool" where
  2208   "operative opp f \<equiv> 
  2209     (\<forall>a b. content {a..b} = 0 \<longrightarrow> f {a..b} = neutral(opp)) \<and>
  2210     (\<forall>a b c. \<forall>k<DIM('b). f({a..b}) =
  2211                    opp (f({a..b} \<inter> {x. x$$k \<le> c}))
  2212                        (f({a..b} \<inter> {x. x$$k \<ge> c})))"
  2213 
  2214 lemma operativeD[dest]: fixes type::"'a::ordered_euclidean_space"  assumes "operative opp f"
  2215   shows "\<And>a b. content {a..b} = 0 \<Longrightarrow> f {a..b::'a} = neutral(opp)"
  2216   "\<And>a b c k. k<DIM('a) \<Longrightarrow> f({a..b}) = opp (f({a..b} \<inter> {x. x$$k \<le> c})) (f({a..b} \<inter> {x. x$$k \<ge> c}))"
  2217   using assms unfolding operative_def by auto
  2218 
  2219 lemma operative_trivial:
  2220  "operative opp f \<Longrightarrow> content({a..b}) = 0 \<Longrightarrow> f({a..b}) = neutral opp"
  2221   unfolding operative_def by auto
  2222 
  2223 lemma property_empty_interval:
  2224  "(\<forall>a b. content({a..b}) = 0 \<longrightarrow> P({a..b})) \<Longrightarrow> P {}" 
  2225   using content_empty unfolding empty_as_interval by auto
  2226 
  2227 lemma operative_empty: "operative opp f \<Longrightarrow> f {} = neutral opp"
  2228   unfolding operative_def apply(rule property_empty_interval) by auto
  2229 
  2230 subsection {* Using additivity of lifted function to encode definedness. *}
  2231 
  2232 lemma forall_option: "(\<forall>x. P x) \<longleftrightarrow> P None \<and> (\<forall>x. P(Some x))"
  2233   by (metis option.nchotomy)
  2234 
  2235 lemma exists_option: "(\<exists>x. P x) \<longleftrightarrow> P None \<or> (\<exists>x. P(Some x))"
  2236   by (metis option.nchotomy)
  2237 
  2238 fun lifted
  2239 where
  2240   "lifted (opp::'a\<Rightarrow>'a\<Rightarrow>'b) (Some x) (Some y) = Some (opp x y)"
  2241 | "lifted opp None _ = (None::'b option)"
  2242 | "lifted opp _ None = None"
  2243 
  2244 lemma lifted_simp_1[simp]: "lifted opp v None = None"
  2245   by (induct v) auto
  2246 
  2247 definition "monoidal opp \<equiv>  (\<forall>x y. opp x y = opp y x) \<and>
  2248                    (\<forall>x y z. opp x (opp y z) = opp (opp x y) z) \<and>
  2249                    (\<forall>x. opp (neutral opp) x = x)"
  2250 
  2251 lemma monoidalI:
  2252   assumes "\<And>x y. opp x y = opp y x"
  2253   "\<And>x y z. opp x (opp y z) = opp (opp x y) z"
  2254   "\<And>x. opp (neutral opp) x = x" shows "monoidal opp"
  2255   unfolding monoidal_def using assms by fastforce
  2256 
  2257 lemma monoidal_ac:
  2258   assumes "monoidal opp"
  2259   shows "opp (neutral opp) a = a" "opp a (neutral opp) = a" "opp a b = opp b a"
  2260   "opp (opp a b) c = opp a (opp b c)"  "opp a (opp b c) = opp b (opp a c)"
  2261   using assms unfolding monoidal_def by metis+
  2262 
  2263 lemma monoidal_simps[simp]: assumes "monoidal opp"
  2264   shows "opp (neutral opp) a = a" "opp a (neutral opp) = a"
  2265   using monoidal_ac[OF assms] by auto
  2266 
  2267 lemma neutral_lifted[cong]: assumes "monoidal opp"
  2268   shows "neutral (lifted opp) = Some(neutral opp)"
  2269   apply(subst neutral_def) apply(rule some_equality) apply(rule,induct_tac y) prefer 3
  2270 proof -
  2271   fix x assume "\<forall>y. lifted opp x y = y \<and> lifted opp y x = y"
  2272   thus "x = Some (neutral opp)"
  2273     apply(induct x) defer
  2274     apply rule apply(subst neutral_def) apply(subst eq_commute,rule some_equality)
  2275     apply(rule,erule_tac x="Some y" in allE) defer apply(erule_tac x="Some x" in allE)
  2276     apply auto
  2277     done
  2278 qed(auto simp add:monoidal_ac[OF assms])
  2279 
  2280 lemma monoidal_lifted[intro]: assumes "monoidal opp" shows "monoidal(lifted opp)"
  2281   unfolding monoidal_def forall_option neutral_lifted[OF assms] using monoidal_ac[OF assms] by auto
  2282 
  2283 definition "support opp f s = {x. x\<in>s \<and> f x \<noteq> neutral opp}"
  2284 definition "fold' opp e s \<equiv> (if finite s then Finite_Set.fold opp e s else e)"
  2285 definition "iterate opp s f \<equiv> fold' (\<lambda>x a. opp (f x) a) (neutral opp) (support opp f s)"
  2286 
  2287 lemma support_subset[intro]:"support opp f s \<subseteq> s" unfolding support_def by auto
  2288 lemma support_empty[simp]:"support opp f {} = {}" using support_subset[of opp f "{}"] by auto
  2289 
  2290 lemma comp_fun_commute_monoidal[intro]: assumes "monoidal opp" shows "comp_fun_commute opp"
  2291   unfolding comp_fun_commute_def using monoidal_ac[OF assms] by auto
  2292 
  2293 lemma support_clauses:
  2294   "\<And>f g s. support opp f {} = {}"
  2295   "\<And>f g s. support opp f (insert x s) =
  2296     (if f(x) = neutral opp then support opp f s else insert x (support opp f s))"
  2297   "\<And>f g s. support opp f (s - {x}) = (support opp f s) - {x}"
  2298   "\<And>f g s. support opp f (s \<union> t) = (support opp f s) \<union> (support opp f t)"
  2299   "\<And>f g s. support opp f (s \<inter> t) = (support opp f s) \<inter> (support opp f t)"
  2300   "\<And>f g s. support opp f (s - t) = (support opp f s) - (support opp f t)"
  2301   "\<And>f g s. support opp g (f ` s) = f ` (support opp (g o f) s)"
  2302 unfolding support_def by auto
  2303 
  2304 lemma finite_support[intro]:"finite s \<Longrightarrow> finite (support opp f s)"
  2305   unfolding support_def by auto
  2306 
  2307 lemma iterate_empty[simp]:"iterate opp {} f = neutral opp"
  2308   unfolding iterate_def fold'_def by auto 
  2309 
  2310 lemma iterate_insert[simp]: assumes "monoidal opp" "finite s"
  2311   shows "iterate opp (insert x s) f = (if x \<in> s then iterate opp s f else opp (f x) (iterate opp s f))" 
  2312 proof(cases "x\<in>s") case True hence *:"insert x s = s" by auto
  2313   show ?thesis unfolding iterate_def if_P[OF True] * by auto
  2314 next case False note x=this
  2315   note * = comp_fun_commute.comp_comp_fun_commute [OF comp_fun_commute_monoidal[OF assms(1)]]
  2316   show ?thesis proof(cases "f x = neutral opp")
  2317     case True show ?thesis unfolding iterate_def if_not_P[OF x] support_clauses if_P[OF True]
  2318       unfolding True monoidal_simps[OF assms(1)] by auto
  2319   next case False show ?thesis unfolding iterate_def fold'_def  if_not_P[OF x] support_clauses if_not_P[OF False]
  2320       apply(subst comp_fun_commute.fold_insert[OF * finite_support, simplified comp_def])
  2321       using `finite s` unfolding support_def using False x by auto qed qed 
  2322 
  2323 lemma iterate_some:
  2324   assumes "monoidal opp"  "finite s"
  2325   shows "iterate (lifted opp) s (\<lambda>x. Some(f x)) = Some (iterate opp s f)" using assms(2)
  2326 proof(induct s) case empty thus ?case using assms by auto
  2327 next case (insert x F) show ?case apply(subst iterate_insert) prefer 3 apply(subst if_not_P)
  2328     defer unfolding insert(3) lifted.simps apply rule using assms insert by auto qed
  2329 subsection {* Two key instances of additivity. *}
  2330 
  2331 lemma neutral_add[simp]:
  2332   "neutral op + = (0::_::comm_monoid_add)" unfolding neutral_def 
  2333   apply(rule some_equality) defer apply(erule_tac x=0 in allE) by auto
  2334 
  2335 lemma operative_content[intro]: "operative (op +) content" 
  2336   unfolding operative_def neutral_add apply safe 
  2337   unfolding content_split[THEN sym] ..
  2338 
  2339 lemma neutral_monoid: "neutral ((op +)::('a::comm_monoid_add) \<Rightarrow> 'a \<Rightarrow> 'a) = 0"
  2340   by (rule neutral_add) (* FIXME: duplicate *)
  2341 
  2342 lemma monoidal_monoid[intro]:
  2343   shows "monoidal ((op +)::('a::comm_monoid_add) \<Rightarrow> 'a \<Rightarrow> 'a)"
  2344   unfolding monoidal_def neutral_monoid by(auto simp add: algebra_simps) 
  2345 
  2346 lemma operative_integral: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::banach"
  2347   shows "operative (lifted(op +)) (\<lambda>i. if f integrable_on i then Some(integral i f) else None)"
  2348   unfolding operative_def unfolding neutral_lifted[OF monoidal_monoid] neutral_add
  2349   apply(rule,rule,rule,rule) defer apply(rule allI impI)+
  2350 proof- fix a b c k assume k:"k<DIM('a)" show "(if f integrable_on {a..b} then Some (integral {a..b} f) else None) =
  2351     lifted op + (if f integrable_on {a..b} \<inter> {x. x $$ k \<le> c} then Some (integral ({a..b} \<inter> {x. x $$ k \<le> c}) f) else None)
  2352     (if f integrable_on {a..b} \<inter> {x. c \<le> x $$ k} then Some (integral ({a..b} \<inter> {x. c \<le> x $$ k}) f) else None)"
  2353   proof(cases "f integrable_on {a..b}") 
  2354     case True show ?thesis unfolding if_P[OF True] using k apply-
  2355       unfolding if_P[OF integrable_split(1)[OF True]] unfolding if_P[OF integrable_split(2)[OF True]]
  2356       unfolding lifted.simps option.inject apply(rule integral_unique) apply(rule has_integral_split[OF _ _ k]) 
  2357       apply(rule_tac[!] integrable_integral integrable_split)+ using True k by auto
  2358   next case False have "(\<not> (f integrable_on {a..b} \<inter> {x. x $$ k \<le> c})) \<or> (\<not> ( f integrable_on {a..b} \<inter> {x. c \<le> x $$ k}))"
  2359     proof(rule ccontr) case goal1 hence "f integrable_on {a..b}" apply- unfolding integrable_on_def
  2360         apply(rule_tac x="integral ({a..b} \<inter> {x. x $$ k \<le> c}) f + integral ({a..b} \<inter> {x. x $$ k \<ge> c}) f" in exI)
  2361         apply(rule has_integral_split[OF _ _ k]) apply(rule_tac[!] integrable_integral) by auto
  2362       thus False using False by auto
  2363     qed thus ?thesis using False by auto 
  2364   qed next 
  2365   fix a b assume as:"content {a..b::'a} = 0"
  2366   thus "(if f integrable_on {a..b} then Some (integral {a..b} f) else None) = Some 0"
  2367     unfolding if_P[OF integrable_on_null[OF as]] using has_integral_null_eq[OF as] by auto qed
  2368 
  2369 subsection {* Points of division of a partition. *}
  2370 
  2371 definition "division_points (k::('a::ordered_euclidean_space) set) d = 
  2372     {(j,x). j<DIM('a) \<and> (interval_lowerbound k)$$j < x \<and> x < (interval_upperbound k)$$j \<and>
  2373            (\<exists>i\<in>d. (interval_lowerbound i)$$j = x \<or> (interval_upperbound i)$$j = x)}"
  2374 
  2375 lemma division_points_finite: fixes i::"('a::ordered_euclidean_space) set"
  2376   assumes "d division_of i" shows "finite (division_points i d)"
  2377 proof- note assm = division_ofD[OF assms]
  2378   let ?M = "\<lambda>j. {(j,x)|x. (interval_lowerbound i)$$j < x \<and> x < (interval_upperbound i)$$j \<and>
  2379            (\<exists>i\<in>d. (interval_lowerbound i)$$j = x \<or> (interval_upperbound i)$$j = x)}"
  2380   have *:"division_points i d = \<Union>(?M ` {..<DIM('a)})"
  2381     unfolding division_points_def by auto
  2382   show ?thesis unfolding * using assm by auto qed
  2383 
  2384 lemma division_points_subset: fixes a::"'a::ordered_euclidean_space"
  2385   assumes "d division_of {a..b}" "\<forall>i<DIM('a). a$$i < b$$i"  "a$$k < c" "c < b$$k" and k:"k<DIM('a)"
  2386   shows "division_points ({a..b} \<inter> {x. x$$k \<le> c}) {l \<inter> {x. x$$k \<le> c} | l . l \<in> d \<and> ~(l \<inter> {x. x$$k \<le> c} = {})}
  2387                   \<subseteq> division_points ({a..b}) d" (is ?t1) and
  2388         "division_points ({a..b} \<inter> {x. x$$k \<ge> c}) {l \<inter> {x. x$$k \<ge> c} | l . l \<in> d \<and> ~(l \<inter> {x. x$$k \<ge> c} = {})}
  2389                   \<subseteq> division_points ({a..b}) d" (is ?t2)
  2390 proof- note assm = division_ofD[OF assms(1)]
  2391   have *:"\<forall>i<DIM('a). a$$i \<le> b$$i"   "\<forall>i<DIM('a). a$$i \<le> ((\<chi>\<chi> i. if i = k then min (b $$ k) c else b $$ i)::'a) $$ i"
  2392     "\<forall>i<DIM('a). ((\<chi>\<chi> i. if i = k then max (a $$ k) c else a $$ i)::'a) $$ i \<le> b$$i"  "min (b $$ k) c = c" "max (a $$ k) c = c"
  2393     using assms using less_imp_le by auto
  2394   show ?t1 unfolding division_points_def interval_split[OF k, of a b]
  2395     unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)] unfolding *
  2396     unfolding subset_eq apply(rule) unfolding mem_Collect_eq split_beta apply(erule bexE conjE)+
  2397     unfolding mem_Collect_eq apply(erule exE conjE)+ unfolding euclidean_lambda_beta'
  2398   proof- fix i l x assume as:"a $$ fst x < snd x" "snd x < (if fst x = k then c else b $$ fst x)"
  2399       "interval_lowerbound i $$ fst x = snd x \<or> interval_upperbound i $$ fst x = snd x"
  2400       "i = l \<inter> {x. x $$ k \<le> c}" "l \<in> d" "l \<inter> {x. x $$ k \<le> c} \<noteq> {}" and fstx:"fst x <DIM('a)"
  2401     from assm(4)[OF this(5)] guess u v apply-by(erule exE)+ note l=this
  2402     have *:"\<forall>i<DIM('a). u $$ i \<le> ((\<chi>\<chi> i. if i = k then min (v $$ k) c else v $$ i)::'a) $$ i"
  2403       using as(6) unfolding l interval_split[OF k] interval_ne_empty as .
  2404     have **:"\<forall>i<DIM('a). u$$i \<le> v$$i" using l using as(6) unfolding interval_ne_empty[THEN sym] by auto
  2405     show "fst x <DIM('a) \<and> a $$ fst x < snd x \<and> snd x < b $$ fst x \<and> (\<exists>i\<in>d. interval_lowerbound i $$ fst x = snd x
  2406       \<or> interval_upperbound i $$ fst x = snd x)" apply(rule,rule fstx)
  2407       using as(1-3,5) unfolding l interval_split[OF k] interval_ne_empty as interval_bounds[OF *] apply-
  2408       apply(rule,assumption,rule) defer apply(rule_tac x="{u..v}" in bexI) unfolding interval_bounds[OF **]
  2409       apply(case_tac[!] "fst x = k") using assms fstx apply- unfolding euclidean_lambda_beta by auto
  2410   qed
  2411   show ?t2 unfolding division_points_def interval_split[OF k, of a b]
  2412     unfolding interval_bounds[OF *(1)] interval_bounds[OF *(2)] interval_bounds[OF *(3)] unfolding *
  2413     unfolding subset_eq apply(rule) unfolding mem_Collect_eq split_beta apply(erule bexE conjE)+
  2414     unfolding mem_Collect_eq apply(erule exE conjE)+ unfolding euclidean_lambda_beta' apply(rule,assumption)
  2415   proof- fix i l x assume as:"(if fst x = k then c else a $$ fst x) < snd x" "snd x < b $$ fst x"
  2416       "interval_lowerbound i $$ fst x = snd x \<or> interval_upperbound i $$ fst x = snd x" 
  2417       "i = l \<inter> {x. c \<le> x $$ k}" "l \<in> d" "l \<inter> {x. c \<le> x $$ k} \<noteq> {}" and fstx:"fst x < DIM('a)"
  2418     from assm(4)[OF this(5)] guess u v apply-by(erule exE)+ note l=this
  2419     have *:"\<forall>i<DIM('a). ((\<chi>\<chi> i. if i = k then max (u $$ k) c else u $$ i)::'a) $$ i \<le> v $$ i"
  2420       using as(6) unfolding l interval_split[OF k] interval_ne_empty as .
  2421     have **:"\<forall>i<DIM('a). u$$i \<le> v$$i" using l using as(6) unfolding interval_ne_empty[THEN sym] by auto
  2422     show "a $$ fst x < snd x \<and> snd x < b $$ fst x \<and> (\<exists>i\<in>d. interval_lowerbound i $$ fst x = snd x \<or>
  2423       interval_upperbound i $$ fst x = snd x)"
  2424       using as(1-3,5) unfolding l interval_split[OF k] interval_ne_empty as interval_bounds[OF *] apply-
  2425       apply rule defer apply(rule,assumption) apply(rule_tac x="{u..v}" in bexI) unfolding interval_bounds[OF **]
  2426       apply(case_tac[!] "fst x = k") using assms fstx apply-  by(auto simp add:euclidean_lambda_beta'[OF k]) qed qed
  2427 
  2428 lemma division_points_psubset: fixes a::"'a::ordered_euclidean_space"
  2429   assumes "d division_of {a..b}"  "\<forall>i<DIM('a). a$$i < b$$i"  "a$$k < c" "c < b$$k"
  2430   "l \<in> d" "interval_lowerbound l$$k = c \<or> interval_upperbound l$$k = c" and k:"k<DIM('a)"
  2431   shows "division_points ({a..b} \<inter> {x. x$$k \<le> c}) {l \<inter> {x. x$$k \<le> c} | l. l\<in>d \<and> l \<inter> {x. x$$k \<le> c} \<noteq> {}}
  2432               \<subset> division_points ({a..b}) d" (is "?D1 \<subset> ?D") 
  2433         "division_points ({a..b} \<inter> {x. x$$k \<ge> c}) {l \<inter> {x. x$$k \<ge> c} | l. l\<in>d \<and> l \<inter> {x. x$$k \<ge> c} \<noteq> {}}
  2434               \<subset> division_points ({a..b}) d" (is "?D2 \<subset> ?D") 
  2435 proof- have ab:"\<forall>i<DIM('a). a$$i \<le> b$$i" using assms(2) by(auto intro!:less_imp_le)
  2436   guess u v using division_ofD(4)[OF assms(1,5)] apply-by(erule exE)+ note l=this
  2437   have uv:"\<forall>i<DIM('a). u$$i \<le> v$$i" "\<forall>i<DIM('a). a$$i \<le> u$$i \<and> v$$i \<le> b$$i"
  2438     using division_ofD(2,2,3)[OF assms(1,5)] unfolding l interval_ne_empty
  2439     unfolding subset_eq apply- defer apply(erule_tac x=u in ballE, erule_tac x=v in ballE) unfolding mem_interval by auto
  2440   have *:"interval_upperbound ({a..b} \<inter> {x. x $$ k \<le> interval_upperbound l $$ k}) $$ k = interval_upperbound l $$ k"
  2441          "interval_upperbound ({a..b} \<inter> {x. x $$ k \<le> interval_lowerbound l $$ k}) $$ k = interval_lowerbound l $$ k"
  2442     unfolding interval_split[OF k] apply(subst interval_bounds) prefer 3 apply(subst interval_bounds)
  2443     unfolding l interval_bounds[OF uv(1)] using uv[rule_format,of k] ab k by auto
  2444   have "\<exists>x. x \<in> ?D - ?D1" using assms(2-) apply-apply(erule disjE)
  2445     apply(rule_tac x="(k,(interval_lowerbound l)$$k)" in exI) defer
  2446     apply(rule_tac x="(k,(interval_upperbound l)$$k)" in exI)
  2447     unfolding division_points_def unfolding interval_bounds[OF ab] by(auto simp add:*) 
  2448   thus "?D1 \<subset> ?D" apply-apply(rule,rule division_points_subset[OF assms(1-4)]) using k by auto
  2449 
  2450   have *:"interval_lowerbound ({a..b} \<inter> {x. x $$ k \<ge> interval_lowerbound l $$ k}) $$ k = interval_lowerbound l $$ k"
  2451          "interval_lowerbound ({a..b} \<inter> {x. x $$ k \<ge> interval_upperbound l $$ k}) $$ k = interval_upperbound l $$ k"
  2452     unfolding interval_split[OF k] apply(subst interval_bounds) prefer 3 apply(subst interval_bounds)
  2453     unfolding l interval_bounds[OF uv(1)] using uv[rule_format,of k] ab k by auto
  2454   have "\<exists>x. x \<in> ?D - ?D2" using assms(2-) apply-apply(erule disjE)
  2455     apply(rule_tac x="(k,(interval_lowerbound l)$$k)" in exI) defer
  2456     apply(rule_tac x="(k,(interval_upperbound l)$$k)" in exI)
  2457     unfolding division_points_def unfolding interval_bounds[OF ab] by(auto simp add:*) 
  2458   thus "?D2 \<subset> ?D" apply-apply(rule,rule division_points_subset[OF assms(1-4) k]) by auto qed
  2459 
  2460 subsection {* Preservation by divisions and tagged divisions. *}
  2461 
  2462 lemma support_support[simp]:"support opp f (support opp f s) = support opp f s"
  2463   unfolding support_def by auto
  2464 
  2465 lemma iterate_support[simp]: "iterate opp (support opp f s) f = iterate opp s f"
  2466   unfolding iterate_def support_support by auto
  2467 
  2468 lemma iterate_expand_cases:
  2469   "iterate opp s f = (if finite(support opp f s) then iterate opp (support opp f s) f else neutral opp)"
  2470   apply(cases) apply(subst if_P,assumption) unfolding iterate_def support_support fold'_def by auto 
  2471 
  2472 lemma iterate_image: assumes "monoidal opp"  "inj_on f s"
  2473   shows "iterate opp (f ` s) g = iterate opp s (g \<circ> f)"
  2474 proof- have *:"\<And>s. finite s \<Longrightarrow>  \<forall>x\<in>s. \<forall>y\<in>s. f x = f y \<longrightarrow> x = y \<Longrightarrow>
  2475      iterate opp (f ` s) g = iterate opp s (g \<circ> f)"
  2476   proof- case goal1 show ?case using goal1
  2477     proof(induct s) case empty thus ?case using assms(1) by auto
  2478     next case (insert x s) show ?case unfolding iterate_insert[OF assms(1) insert(1)]
  2479         unfolding if_not_P[OF insert(2)] apply(subst insert(3)[THEN sym])
  2480         unfolding image_insert defer apply(subst iterate_insert[OF assms(1)])
  2481         apply(rule finite_imageI insert)+ apply(subst if_not_P)
  2482         unfolding image_iff o_def using insert(2,4) by auto
  2483     qed qed
  2484   show ?thesis 
  2485     apply(cases "finite (support opp g (f ` s))")
  2486     apply(subst (1) iterate_support[THEN sym],subst (2) iterate_support[THEN sym])
  2487     unfolding support_clauses apply(rule *)apply(rule finite_imageD,assumption) unfolding inj_on_def[symmetric]
  2488     apply(rule subset_inj_on[OF assms(2) support_subset])+
  2489     apply(subst iterate_expand_cases) unfolding support_clauses apply(simp only: if_False)
  2490     apply(subst iterate_expand_cases) apply(subst if_not_P) by auto qed
  2491 
  2492 
  2493 (* This lemma about iterations comes up in a few places.                     *)
  2494 lemma iterate_nonzero_image_lemma:
  2495   assumes "monoidal opp" "finite s" "g(a) = neutral opp"
  2496   "\<forall>x\<in>s. \<forall>y\<in>s. f x = f y \<and> x \<noteq> y \<longrightarrow> g(f x) = neutral opp"
  2497   shows "iterate opp {f x | x. x \<in> s \<and> f x \<noteq> a} g = iterate opp s (g \<circ> f)"
  2498 proof- have *:"{f x |x. x \<in> s \<and> ~(f x = a)} = f ` {x. x \<in> s \<and> ~(f x = a)}" by auto
  2499   have **:"support opp (g \<circ> f) {x \<in> s. f x \<noteq> a} = support opp (g \<circ> f) s"
  2500     unfolding support_def using assms(3) by auto
  2501   show ?thesis unfolding *
  2502     apply(subst iterate_support[THEN sym]) unfolding support_clauses
  2503     apply(subst iterate_image[OF assms(1)]) defer
  2504     apply(subst(2) iterate_support[THEN sym]) apply(subst **)
  2505     unfolding inj_on_def using assms(3,4) unfolding support_def by auto qed
  2506 
  2507 lemma iterate_eq_neutral:
  2508   assumes "monoidal opp"  "\<forall>x \<in> s. (f(x) = neutral opp)"
  2509   shows "(iterate opp s f = neutral opp)"
  2510 proof- have *:"support opp f s = {}" unfolding support_def using assms(2) by auto
  2511   show ?thesis apply(subst iterate_support[THEN sym]) 
  2512     unfolding * using assms(1) by auto qed
  2513 
  2514 lemma iterate_op: assumes "monoidal opp" "finite s"
  2515   shows "iterate opp s (\<lambda>x. opp (f x) (g x)) = opp (iterate opp s f) (iterate opp s g)" using assms(2)
  2516 proof(induct s) case empty thus ?case unfolding iterate_insert[OF assms(1)] using assms(1) by auto
  2517 next case (insert x F) show ?case unfolding iterate_insert[OF assms(1) insert(1)] if_not_P[OF insert(2)] insert(3)
  2518     unfolding monoidal_ac[OF assms(1)] by(rule refl) qed
  2519 
  2520 lemma iterate_eq: assumes "monoidal opp" "\<And>x. x \<in> s \<Longrightarrow> f x = g x"
  2521   shows "iterate opp s f = iterate opp s g"
  2522 proof- have *:"support opp g s = support opp f s"
  2523     unfolding support_def using assms(2) by auto
  2524   show ?thesis
  2525   proof(cases "finite (support opp f s)")
  2526     case False thus ?thesis apply(subst iterate_expand_cases,subst(2) iterate_expand_cases)
  2527       unfolding * by auto
  2528   next def su \<equiv> "support opp f s"
  2529     case True note support_subset[of opp f s] 
  2530     thus ?thesis apply- apply(subst iterate_support[THEN sym],subst(2) iterate_support[THEN sym]) unfolding * using True
  2531       unfolding su_def[symmetric]
  2532     proof(induct su) case empty show ?case by auto
  2533     next case (insert x s) show ?case unfolding iterate_insert[OF assms(1) insert(1)] 
  2534         unfolding if_not_P[OF insert(2)] apply(subst insert(3))
  2535         defer apply(subst assms(2)[of x]) using insert by auto qed qed qed
  2536 
  2537 lemma nonempty_witness: assumes "s \<noteq> {}" obtains x where "x \<in> s" using assms by auto
  2538 
  2539 lemma operative_division: fixes f::"('a::ordered_euclidean_space) set \<Rightarrow> 'b"
  2540   assumes "monoidal opp" "operative opp f" "d division_of {a..b}"
  2541   shows "iterate opp d f = f {a..b}"
  2542 proof- def C \<equiv> "card (division_points {a..b} d)" thus ?thesis using assms
  2543   proof(induct C arbitrary:a b d rule:full_nat_induct)
  2544     case goal1
  2545     { presume *:"content {a..b} \<noteq> 0 \<Longrightarrow> ?case"
  2546       thus ?case apply-apply(cases) defer apply assumption
  2547       proof- assume as:"content {a..b} = 0"
  2548         show ?case unfolding operativeD(1)[OF assms(2) as] apply(rule iterate_eq_neutral[OF goal1(2)])
  2549         proof fix x assume x:"x\<in>d"
  2550           then guess u v apply(drule_tac division_ofD(4)[OF goal1(4)]) by(erule exE)+
  2551           thus "f x = neutral opp" using division_of_content_0[OF as goal1(4)] 
  2552             using operativeD(1)[OF assms(2)] x by auto
  2553         qed qed }
  2554     assume "content {a..b} \<noteq> 0" note ab = this[unfolded content_lt_nz[THEN sym] content_pos_lt_eq]
  2555     hence ab':"\<forall>i<DIM('a). a$$i \<le> b$$i" by (auto intro!: less_imp_le) show ?case 
  2556     proof(cases "division_points {a..b} d = {}")
  2557       case True have d':"\<forall>i\<in>d. \<exists>u v. i = {u..v} \<and>
  2558         (\<forall>j<DIM('a). u$$j = a$$j \<and> v$$j = a$$j \<or> u$$j = b$$j \<and> v$$j = b$$j \<or> u$$j = a$$j \<and> v$$j = b$$j)"
  2559         unfolding forall_in_division[OF goal1(4)] apply(rule,rule,rule)
  2560         apply(rule_tac x=a in exI,rule_tac x=b in exI) apply(rule,rule refl) apply(rule,rule)
  2561       proof- fix u v j assume j:"j<DIM('a)" assume as:"{u..v} \<in> d" note division_ofD(3)[OF goal1(4) this]
  2562         hence uv:"\<forall>i<DIM('a). u$$i \<le> v$$i" "u$$j \<le> v$$j" using j unfolding interval_ne_empty by auto
  2563         have *:"\<And>p r Q. \<not> j<DIM('a) \<or> p \<or> r \<or> (\<forall>x\<in>d. Q x) \<Longrightarrow> p \<or> r \<or> (Q {u..v})" using as j by auto
  2564         have "(j, u$$j) \<notin> division_points {a..b} d"
  2565           "(j, v$$j) \<notin> division_points {a..b} d" using True by auto
  2566         note this[unfolded de_Morgan_conj division_points_def mem_Collect_eq split_conv interval_bounds[OF ab'] bex_simps]
  2567         note *[OF this(1)] *[OF this(2)] note this[unfolded interval_bounds[OF uv(1)]]
  2568         moreover have "a$$j \<le> u$$j" "v$$j \<le> b$$j" using division_ofD(2,2,3)[OF goal1(4) as] 
  2569           unfolding subset_eq apply- apply(erule_tac x=u in ballE,erule_tac[3] x=v in ballE)
  2570           unfolding interval_ne_empty mem_interval using j by auto
  2571         ultimately show "u$$j = a$$j \<and> v$$j = a$$j \<or> u$$j = b$$j \<and> v$$j = b$$j \<or> u$$j = a$$j \<and> v$$j = b$$j"
  2572           unfolding not_less de_Morgan_disj using ab[rule_format,of j] uv(2) j by auto
  2573       qed have "(1/2) *\<^sub>R (a+b) \<in> {a..b}" unfolding mem_interval using ab by(auto intro!:less_imp_le)
  2574       note this[unfolded division_ofD(6)[OF goal1(4),THEN sym] Union_iff]
  2575       then guess i .. note i=this guess u v using d'[rule_format,OF i(1)] apply-by(erule exE conjE)+ note uv=this
  2576       have "{a..b} \<in> d"
  2577       proof- { presume "i = {a..b}" thus ?thesis using i by auto }
  2578         { presume "u = a" "v = b" thus "i = {a..b}" using uv by auto }
  2579         show "u = a" "v = b" unfolding euclidean_eq[where 'a='a]
  2580         proof(safe) fix j assume j:"j<DIM('a)" note i(2)[unfolded uv mem_interval,rule_format,of j]
  2581           thus "u $$ j = a $$ j" "v $$ j = b $$ j" using uv(2)[rule_format,of j] j by auto
  2582         qed qed
  2583       hence *:"d = insert {a..b} (d - {{a..b}})" by auto
  2584       have "iterate opp (d - {{a..b}}) f = neutral opp" apply(rule iterate_eq_neutral[OF goal1(2)])
  2585       proof fix x assume x:"x \<in> d - {{a..b}}" hence "x\<in>d" by auto note d'[rule_format,OF this]
  2586         then guess u v apply-by(erule exE conjE)+ note uv=this
  2587         have "u\<noteq>a \<or> v\<noteq>b" using x[unfolded uv] by auto  
  2588         then obtain j where "u$$j \<noteq> a$$j \<or> v$$j \<noteq> b$$j" and j:"j<DIM('a)" unfolding euclidean_eq[where 'a='a] by auto
  2589         hence "u$$j = v$$j" using uv(2)[rule_format,OF j] by auto
  2590         hence "content {u..v} = 0"  unfolding content_eq_0 apply(rule_tac x=j in exI) using j by auto
  2591         thus "f x = neutral opp" unfolding uv(1) by(rule operativeD(1)[OF goal1(3)])
  2592       qed thus "iterate opp d f = f {a..b}" apply-apply(subst *) 
  2593         apply(subst iterate_insert[OF goal1(2)]) using goal1(2,4) by auto
  2594     next case False hence "\<exists>x. x\<in>division_points {a..b} d" by auto
  2595       then guess k c unfolding split_paired_Ex apply- unfolding division_points_def mem_Collect_eq split_conv
  2596         by(erule exE conjE)+ note this(2-4,1) note kc=this[unfolded interval_bounds[OF ab']]
  2597       from this(3) guess j .. note j=this
  2598       def d1 \<equiv> "{l \<inter> {x. x$$k \<le> c} | l. l \<in> d \<and> l \<inter> {x. x$$k \<le> c} \<noteq> {}}"
  2599       def d2 \<equiv> "{l \<inter> {x. x$$k \<ge> c} | l. l \<in> d \<and> l \<inter> {x. x$$k \<ge> c} \<noteq> {}}"
  2600       def cb \<equiv> "(\<chi>\<chi> i. if i = k then c else b$$i)::'a" and ca \<equiv> "(\<chi>\<chi> i. if i = k then c else a$$i)::'a"
  2601       note division_points_psubset[OF goal1(4) ab kc(1-2) j]
  2602       note psubset_card_mono[OF _ this(1)] psubset_card_mono[OF _ this(2)]
  2603       hence *:"(iterate opp d1 f) = f ({a..b} \<inter> {x. x$$k \<le> c})" "(iterate opp d2 f) = f ({a..b} \<inter> {x. x$$k \<ge> c})"
  2604         apply- unfolding interval_split[OF kc(4)] apply(rule_tac[!] goal1(1)[rule_format])
  2605         using division_split[OF goal1(4), where k=k and c=c]
  2606         unfolding interval_split[OF kc(4)] d1_def[symmetric] d2_def[symmetric] unfolding goal1(2) Suc_le_mono
  2607         using goal1(2-3) using division_points_finite[OF goal1(4)] using kc(4) by auto
  2608       have "f {a..b} = opp (iterate opp d1 f) (iterate opp d2 f)" (is "_ = ?prev")
  2609         unfolding * apply(rule operativeD(2)) using goal1(3) using kc(4) by auto 
  2610       also have "iterate opp d1 f = iterate opp d (\<lambda>l. f(l \<inter> {x. x$$k \<le> c}))"
  2611         unfolding d1_def apply(rule iterate_nonzero_image_lemma[unfolded o_def])
  2612         unfolding empty_as_interval apply(rule goal1 division_of_finite operativeD[OF goal1(3)])+
  2613         unfolding empty_as_interval[THEN sym] apply(rule content_empty)
  2614       proof(rule,rule,rule,erule conjE) fix l y assume as:"l \<in> d" "y \<in> d" "l \<inter> {x. x $$ k \<le> c} = y \<inter> {x. x $$ k \<le> c}" "l \<noteq> y" 
  2615         from division_ofD(4)[OF goal1(4) this(1)] guess u v apply-by(erule exE)+ note l=this
  2616         show "f (l \<inter> {x. x $$ k \<le> c}) = neutral opp" unfolding l interval_split[OF kc(4)] 
  2617           apply(rule operativeD(1) goal1)+ unfolding interval_split[THEN sym,OF kc(4)] apply(rule division_split_left_inj)
  2618           apply(rule goal1) unfolding l[THEN sym] apply(rule as(1),rule as(2)) by(rule kc(4) as)+
  2619       qed also have "iterate opp d2 f = iterate opp d (\<lambda>l. f(l \<inter> {x. x$$k \<ge> c}))"
  2620         unfolding d2_def apply(rule iterate_nonzero_image_lemma[unfolded o_def])
  2621         unfolding empty_as_interval apply(rule goal1 division_of_finite operativeD[OF goal1(3)])+
  2622         unfolding empty_as_interval[THEN sym] apply(rule content_empty)
  2623       proof(rule,rule,rule,erule conjE) fix l y assume as:"l \<in> d" "y \<in> d" "l \<inter> {x. c \<le> x $$ k} = y \<inter> {x. c \<le> x $$ k}" "l \<noteq> y" 
  2624         from division_ofD(4)[OF goal1(4) this(1)] guess u v apply-by(erule exE)+ note l=this
  2625         show "f (l \<inter> {x. x $$ k \<ge> c}) = neutral opp" unfolding l interval_split[OF kc(4)] 
  2626           apply(rule operativeD(1) goal1)+ unfolding interval_split[THEN sym,OF kc(4)] apply(rule division_split_right_inj)
  2627           apply(rule goal1) unfolding l[THEN sym] apply(rule as(1),rule as(2)) by(rule as kc(4))+
  2628       qed also have *:"\<forall>x\<in>d. f x = opp (f (x \<inter> {x. x $$ k \<le> c})) (f (x \<inter> {x. c \<le> x $$ k}))"
  2629         unfolding forall_in_division[OF goal1(4)] apply(rule,rule,rule,rule operativeD(2)) using goal1(3) kc by auto 
  2630       have "opp (iterate opp d (\<lambda>l. f (l \<inter> {x. x $$ k \<le> c}))) (iterate opp d (\<lambda>l. f (l \<inter> {x. c \<le> x $$ k})))
  2631         = iterate opp d f" apply(subst(3) iterate_eq[OF _ *[rule_format]]) prefer 3
  2632         apply(rule iterate_op[THEN sym]) using goal1 by auto
  2633       finally show ?thesis by auto
  2634     qed qed qed 
  2635 
  2636 lemma iterate_image_nonzero: assumes "monoidal opp"
  2637   "finite s" "\<forall>x\<in>s. \<forall>y\<in>s. ~(x = y) \<and> f x = f y \<longrightarrow> g(f x) = neutral opp"
  2638   shows "iterate opp (f ` s) g = iterate opp s (g \<circ> f)" using assms
  2639 proof(induct rule:finite_subset_induct[OF assms(2) subset_refl])
  2640   case goal1 show ?case using assms(1) by auto
  2641 next case goal2 have *:"\<And>x y. y = neutral opp \<Longrightarrow> x = opp y x" using assms(1) by auto
  2642   show ?case unfolding image_insert apply(subst iterate_insert[OF assms(1)])
  2643     apply(rule finite_imageI goal2)+
  2644     apply(cases "f a \<in> f ` F") unfolding if_P if_not_P apply(subst goal2(4)[OF assms(1) goal2(1)]) defer
  2645     apply(subst iterate_insert[OF assms(1) goal2(1)]) defer
  2646     apply(subst iterate_insert[OF assms(1) goal2(1)])
  2647     unfolding if_not_P[OF goal2(3)] defer unfolding image_iff defer apply(erule bexE)
  2648     apply(rule *) unfolding o_def apply(rule_tac y=x in goal2(7)[rule_format])
  2649     using goal2 unfolding o_def by auto qed 
  2650 
  2651 lemma operative_tagged_division: assumes "monoidal opp" "operative opp f" "d tagged_division_of {a..b}"
  2652   shows "iterate(opp) d (\<lambda>(x,l). f l) = f {a..b}"
  2653 proof- have *:"(\<lambda>(x,l). f l) = (f o snd)" unfolding o_def by(rule,auto) note assm = tagged_division_ofD[OF assms(3)]
  2654   have "iterate(opp) d (\<lambda>(x,l). f l) = iterate opp (snd ` d) f" unfolding *
  2655     apply(rule iterate_image_nonzero[THEN sym,OF assms(1)]) apply(rule tagged_division_of_finite assms)+ 
  2656     unfolding Ball_def split_paired_All snd_conv apply(rule,rule,rule,rule,rule,rule,rule,erule conjE)
  2657   proof- fix a b aa ba assume as:"(a, b) \<in> d" "(aa, ba) \<in> d" "(a, b) \<noteq> (aa, ba)" "b = ba"
  2658     guess u v using assm(4)[OF as(1)] apply-by(erule exE)+ note uv=this
  2659     show "f b = neutral opp" unfolding uv apply(rule operativeD(1)[OF assms(2)])
  2660       unfolding content_eq_0_interior using tagged_division_ofD(5)[OF assms(3) as(1-3)]
  2661       unfolding as(4)[THEN sym] uv by auto
  2662   qed also have "\<dots> = f {a..b}" 
  2663     using operative_division[OF assms(1-2) division_of_tagged_division[OF assms(3)]] .
  2664   finally show ?thesis . qed
  2665 
  2666 subsection {* Additivity of content. *}
  2667 
  2668 lemma setsum_iterate:assumes "finite s" shows "setsum f s = iterate op + s f"
  2669 proof- have *:"setsum f s = setsum f (support op + f s)"
  2670     apply(rule setsum_mono_zero_right)
  2671     unfolding support_def neutral_monoid using assms by auto
  2672   thus ?thesis unfolding * setsum_def iterate_def fold_image_def fold'_def
  2673     unfolding neutral_monoid . qed
  2674 
  2675 lemma additive_content_division: assumes "d division_of {a..b}"
  2676   shows "setsum content d = content({a..b})"
  2677   unfolding operative_division[OF monoidal_monoid operative_content assms,THEN sym]
  2678   apply(subst setsum_iterate) using assms by auto
  2679 
  2680 lemma additive_content_tagged_division:
  2681   assumes "d tagged_division_of {a..b}"
  2682   shows "setsum (\<lambda>(x,l). content l) d = content({a..b})"
  2683   unfolding operative_tagged_division[OF monoidal_monoid operative_content assms,THEN sym]
  2684   apply(subst setsum_iterate) using assms by auto
  2685 
  2686 subsection {* Finally, the integral of a constant *}
  2687 
  2688 lemma has_integral_const[intro]:
  2689   "((\<lambda>x. c) has_integral (content({a..b::'a::ordered_euclidean_space}) *\<^sub>R c)) ({a..b})"
  2690   unfolding has_integral apply(rule,rule,rule_tac x="\<lambda>x. ball x 1" in exI)
  2691   apply(rule,rule gauge_trivial)apply(rule,rule,erule conjE)
  2692   unfolding split_def apply(subst scaleR_left.setsum[THEN sym, unfolded o_def])
  2693   defer apply(subst additive_content_tagged_division[unfolded split_def]) apply assumption by auto
  2694 
  2695 lemma integral_const[simp]:
  2696   fixes a b :: "'a::ordered_euclidean_space"
  2697   shows "integral {a .. b} (\<lambda>x. c) = content {a .. b} *\<^sub>R c"
  2698   by (rule integral_unique) (rule has_integral_const)
  2699 
  2700 subsection {* Bounds on the norm of Riemann sums and the integral itself. *}
  2701 
  2702 lemma dsum_bound: assumes "p division_of {a..b}" "norm(c) \<le> e"
  2703   shows "norm(setsum (\<lambda>l. content l *\<^sub>R c) p) \<le> e * content({a..b})" (is "?l \<le> ?r")
  2704   apply(rule order_trans,rule norm_setsum) unfolding norm_scaleR setsum_left_distrib[THEN sym]
  2705   apply(rule order_trans[OF mult_left_mono],rule assms,rule setsum_abs_ge_zero)
  2706   apply(subst mult_commute) apply(rule mult_left_mono)
  2707   apply(rule order_trans[of _ "setsum content p"]) apply(rule eq_refl,rule setsum_cong2)
  2708   apply(subst abs_of_nonneg) unfolding additive_content_division[OF assms(1)]
  2709 proof- from order_trans[OF norm_ge_zero[of c] assms(2)] show "0 \<le> e" .
  2710   fix x assume "x\<in>p" from division_ofD(4)[OF assms(1) this] guess u v apply-by(erule exE)+
  2711   thus "0 \<le> content x" using content_pos_le by auto
  2712 qed(insert assms,auto)
  2713 
  2714 lemma rsum_bound: assumes "p tagged_division_of {a..b}" "\<forall>x\<in>{a..b}. norm(f x) \<le> e"
  2715   shows "norm(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p) \<le> e * content({a..b})"
  2716 proof(cases "{a..b} = {}") case True
  2717   show ?thesis using assms(1) unfolding True tagged_division_of_trivial by auto
  2718 next case False show ?thesis
  2719     apply(rule order_trans,rule norm_setsum) unfolding split_def norm_scaleR
  2720     apply(rule order_trans[OF setsum_mono]) apply(rule mult_left_mono[OF _ abs_ge_zero, of _ e]) defer
  2721     unfolding setsum_left_distrib[THEN sym] apply(subst mult_commute) apply(rule mult_left_mono)
  2722     apply(rule order_trans[of _ "setsum (content \<circ> snd) p"]) apply(rule eq_refl,rule setsum_cong2)
  2723     apply(subst o_def, rule abs_of_nonneg)
  2724   proof- show "setsum (content \<circ> snd) p \<le> content {a..b}" apply(rule eq_refl)
  2725       unfolding additive_content_tagged_division[OF assms(1),THEN sym] split_def by auto
  2726     guess w using nonempty_witness[OF False] .
  2727     thus "e\<ge>0" apply-apply(rule order_trans) defer apply(rule assms(2)[rule_format],assumption) by auto
  2728     fix xk assume *:"xk\<in>p" guess x k  using surj_pair[of xk] apply-by(erule exE)+ note xk = this *[unfolded this]
  2729     from tagged_division_ofD(4)[OF assms(1) xk(2)] guess u v apply-by(erule exE)+ note uv=this
  2730     show "0\<le> content (snd xk)" unfolding xk snd_conv uv by(rule content_pos_le)
  2731     show "norm (f (fst xk)) \<le> e" unfolding xk fst_conv using tagged_division_ofD(2,3)[OF assms(1) xk(2)] assms(2) by auto
  2732   qed qed
  2733 
  2734 lemma rsum_diff_bound:
  2735   assumes "p tagged_division_of {a..b}"  "\<forall>x\<in>{a..b}. norm(f x - g x) \<le> e"
  2736   shows "norm(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - setsum (\<lambda>(x,k). content k *\<^sub>R g x) p) \<le> e * content({a..b})"
  2737   apply(rule order_trans[OF _ rsum_bound[OF assms]]) apply(rule eq_refl) apply(rule arg_cong[where f=norm])
  2738   unfolding setsum_subtractf[THEN sym] apply(rule setsum_cong2) unfolding scaleR_diff_right by auto
  2739 
  2740 lemma has_integral_bound: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::real_normed_vector"
  2741   assumes "0 \<le> B" "(f has_integral i) ({a..b})" "\<forall>x\<in>{a..b}. norm(f x) \<le> B"
  2742   shows "norm i \<le> B * content {a..b}"
  2743 proof- let ?P = "content {a..b} > 0" { presume "?P \<Longrightarrow> ?thesis"
  2744     thus ?thesis proof(cases ?P) case False
  2745       hence *:"content {a..b} = 0" using content_lt_nz by auto
  2746       hence **:"i = 0" using assms(2) apply(subst has_integral_null_eq[THEN sym]) by auto
  2747       show ?thesis unfolding * ** using assms(1) by auto
  2748     qed auto } assume ab:?P
  2749   { presume "\<not> ?thesis \<Longrightarrow> False" thus ?thesis by auto }
  2750   assume "\<not> ?thesis" hence *:"norm i - B * content {a..b} > 0" by auto
  2751   from assms(2)[unfolded has_integral,rule_format,OF *] guess d apply-by(erule exE conjE)+ note d=this[rule_format]
  2752   from fine_division_exists[OF this(1), of a b] guess p . note p=this
  2753   have *:"\<And>s B. norm s \<le> B \<Longrightarrow> \<not> (norm (s - i) < norm i - B)"
  2754   proof- case goal1 thus ?case unfolding not_less
  2755     using norm_triangle_sub[of i s] unfolding norm_minus_commute by auto
  2756   qed show False using d(2)[OF conjI[OF p]] *[OF rsum_bound[OF p(1) assms(3)]] by auto qed
  2757 
  2758 subsection {* Similar theorems about relationship among components. *}
  2759 
  2760 lemma rsum_component_le: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::euclidean_space"
  2761   assumes "p tagged_division_of {a..b}"  "\<forall>x\<in>{a..b}. (f x)$$i \<le> (g x)$$i"
  2762   shows "(setsum (\<lambda>(x,k). content k *\<^sub>R f x) p)$$i \<le> (setsum (\<lambda>(x,k). content k *\<^sub>R g x) p)$$i"
  2763   unfolding  euclidean_component_setsum apply(rule setsum_mono) apply safe
  2764 proof- fix a b assume ab:"(a,b) \<in> p" note assm = tagged_division_ofD(2-4)[OF assms(1) ab]
  2765   from this(3) guess u v apply-by(erule exE)+ note b=this
  2766   show "(content b *\<^sub>R f a) $$ i \<le> (content b *\<^sub>R g a) $$ i" unfolding b
  2767     unfolding euclidean_simps real_scaleR_def apply(rule mult_left_mono)
  2768     defer apply(rule content_pos_le,rule assms(2)[rule_format]) using assm by auto qed
  2769 
  2770 lemma has_integral_component_le: fixes f g::"'a::ordered_euclidean_space \<Rightarrow> 'b::euclidean_space"
  2771   assumes "(f has_integral i) s" "(g has_integral j) s"  "\<forall>x\<in>s. (f x)$$k \<le> (g x)$$k"
  2772   shows "i$$k \<le> j$$k"
  2773 proof- have lem:"\<And>a b i (j::'b). \<And>g f::'a \<Rightarrow> 'b. (f has_integral i) ({a..b}) \<Longrightarrow> 
  2774     (g has_integral j) ({a..b}) \<Longrightarrow> \<forall>x\<in>{a..b}. (f x)$$k \<le> (g x)$$k \<Longrightarrow> i$$k \<le> j$$k"
  2775   proof(rule ccontr) case goal1 hence *:"0 < (i$$k - j$$k) / 3" by auto
  2776     guess d1 using goal1(1)[unfolded has_integral,rule_format,OF *] apply-by(erule exE conjE)+ note d1=this[rule_format]
  2777     guess d2 using goal1(2)[unfolded has_integral,rule_format,OF *] apply-by(erule exE conjE)+ note d2=this[rule_format]
  2778     guess p using fine_division_exists[OF gauge_inter[OF d1(1) d2(1)], of a b] unfolding fine_inter .
  2779     note p = this(1) conjunctD2[OF this(2)]  note le_less_trans[OF component_le_norm, of _ _ k] term g
  2780     note this[OF d1(2)[OF conjI[OF p(1,2)]]] this[OF d2(2)[OF conjI[OF p(1,3)]]]
  2781     thus False unfolding euclidean_simps using rsum_component_le[OF p(1) goal1(3)] apply simp
  2782       using [[z3_with_extensions]] by smt
  2783   qed let ?P = "\<exists>a b. s = {a..b}"
  2784   { presume "\<not> ?P \<Longrightarrow> ?thesis" thus ?thesis proof(cases ?P)
  2785       case True then guess a b apply-by(erule exE)+ note s=this
  2786       show ?thesis apply(rule lem) using assms[unfolded s] by auto
  2787     qed auto } assume as:"\<not> ?P"
  2788   { presume "\<not> ?thesis \<Longrightarrow> False" thus ?thesis by auto }
  2789   assume "\<not> i$$k \<le> j$$k" hence ij:"(i$$k - j$$k) / 3 > 0" by auto
  2790   note has_integral_altD[OF _ as this] from this[OF assms(1)] this[OF assms(2)] guess B1 B2 . note B=this[rule_format]
  2791   have "bounded (ball 0 B1 \<union> ball (0::'a) B2)" unfolding bounded_Un by(rule conjI bounded_ball)+
  2792   from bounded_subset_closed_interval[OF this] guess a b apply- by(erule exE)+
  2793   note ab = conjunctD2[OF this[unfolded Un_subset_iff]]
  2794   guess w1 using B(2)[OF ab(1)] .. note w1=conjunctD2[OF this]
  2795   guess w2 using B(4)[OF ab(2)] .. note w2=conjunctD2[OF this]
  2796   have *:"\<And>w1 w2 j i::real .\<bar>w1 - i\<bar> < (i - j) / 3 \<Longrightarrow> \<bar>w2 - j\<bar> < (i - j) / 3 \<Longrightarrow> w1 \<le> w2 \<Longrightarrow> False" using [[z3_with_extensions]] by smt
  2797   note le_less_trans[OF component_le_norm[of _ k]] note this[OF w1(2)] this[OF w2(2)] moreover
  2798   have "w1$$k \<le> w2$$k" apply(rule lem[OF w1(1) w2(1)]) using assms by auto ultimately
  2799   show False unfolding euclidean_simps by(rule *) qed
  2800 
  2801 lemma integral_component_le: fixes g f::"'a::ordered_euclidean_space \<Rightarrow> 'b::euclidean_space"
  2802   assumes "f integrable_on s" "g integrable_on s"  "\<forall>x\<in>s. (f x)$$k \<le> (g x)$$k"
  2803   shows "(integral s f)$$k \<le> (integral s g)$$k"
  2804   apply(rule has_integral_component_le) using integrable_integral assms by auto
  2805 
  2806 (*lemma has_integral_dest_vec1_le: fixes f::"'a::ordered_euclidean_space \<Rightarrow> real^1"
  2807   assumes "(f has_integral i) s"  "(g has_integral j) s" "\<forall>x\<in>s. f x \<le> g x"
  2808   shows "dest_vec1 i \<le> dest_vec1 j" apply(rule has_integral_component_le[OF assms(1-2)])
  2809   using assms(3) unfolding vector_le_def by auto
  2810 
  2811 lemma integral_dest_vec1_le: fixes f::"real^'n \<Rightarrow> real^1"
  2812   assumes "f integrable_on s" "g integrable_on s" "\<forall>x\<in>s. f x \<le> g x"
  2813   shows "dest_vec1(integral s f) \<le> dest_vec1(integral s g)"
  2814   apply(rule has_integral_dest_vec1_le) apply(rule_tac[1-2] integrable_integral) using assms by auto*)
  2815 
  2816 lemma has_integral_component_nonneg: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::euclidean_space"
  2817   assumes "(f has_integral i) s" "\<forall>x\<in>s. 0 \<le> (f x)$$k" shows "0 \<le> i$$k" 
  2818   using has_integral_component_le[OF has_integral_0 assms(1)] using assms(2-) by auto
  2819 
  2820 lemma integral_component_nonneg: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::euclidean_space"
  2821   assumes "f integrable_on s" "\<forall>x\<in>s. 0 \<le> (f x)$$k" shows "0 \<le> (integral s f)$$k"
  2822   apply(rule has_integral_component_nonneg) using assms by auto
  2823 
  2824 (*lemma has_integral_dest_vec1_nonneg: fixes f::"real^'n \<Rightarrow> real^1"
  2825   assumes "(f has_integral i) s" "\<forall>x\<in>s. 0 \<le> f x" shows "0 \<le> i"
  2826   using has_integral_component_nonneg[OF assms(1), of 1]
  2827   using assms(2) unfolding vector_le_def by auto
  2828 
  2829 lemma integral_dest_vec1_nonneg: fixes f::"real^'n \<Rightarrow> real^1"
  2830   assumes "f integrable_on s" "\<forall>x\<in>s. 0 \<le> f x" shows "0 \<le> integral s f"
  2831   apply(rule has_integral_dest_vec1_nonneg) using assms by auto*)
  2832 
  2833 lemma has_integral_component_neg: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::ordered_euclidean_space"
  2834   assumes "(f has_integral i) s" "\<forall>x\<in>s. (f x)$$k \<le> 0"shows "i$$k \<le> 0" 
  2835   using has_integral_component_le[OF assms(1) has_integral_0] assms(2-) by auto
  2836 
  2837 (*lemma has_integral_dest_vec1_neg: fixes f::"real^'n \<Rightarrow> real^1"
  2838   assumes "(f has_integral i) s" "\<forall>x\<in>s. f x \<le> 0" shows "i \<le> 0"
  2839   using has_integral_component_neg[OF assms(1),of 1] using assms(2) by auto*)
  2840 
  2841 lemma has_integral_component_lbound: fixes f::"'a::ordered_euclidean_space => 'b::ordered_euclidean_space"
  2842   assumes "(f has_integral i) {a..b}"  "\<forall>x\<in>{a..b}. B \<le> f(x)$$k" "k<DIM('b)" shows "B * content {a..b} \<le> i$$k"
  2843   using has_integral_component_le[OF has_integral_const assms(1),of "(\<chi>\<chi> i. B)::'b" k] assms(2-)
  2844   unfolding euclidean_simps euclidean_lambda_beta'[OF assms(3)] by(auto simp add:field_simps)
  2845 
  2846 lemma has_integral_component_ubound: fixes f::"'a::ordered_euclidean_space => 'b::ordered_euclidean_space"
  2847   assumes "(f has_integral i) {a..b}" "\<forall>x\<in>{a..b}. f x$$k \<le> B" "k<DIM('b)"
  2848   shows "i$$k \<le> B * content({a..b})"
  2849   using has_integral_component_le[OF assms(1) has_integral_const, of k "\<chi>\<chi> i. B"]
  2850   unfolding euclidean_simps euclidean_lambda_beta'[OF assms(3)] using assms(2) by(auto simp add:field_simps)
  2851 
  2852 lemma integral_component_lbound: fixes f::"'a::ordered_euclidean_space => 'b::ordered_euclidean_space"
  2853   assumes "f integrable_on {a..b}" "\<forall>x\<in>{a..b}. B \<le> f(x)$$k" "k<DIM('b)"
  2854   shows "B * content({a..b}) \<le> (integral({a..b}) f)$$k"
  2855   apply(rule has_integral_component_lbound) using assms unfolding has_integral_integral by auto
  2856 
  2857 lemma integral_component_ubound: fixes f::"'a::ordered_euclidean_space => 'b::ordered_euclidean_space"
  2858   assumes "f integrable_on {a..b}" "\<forall>x\<in>{a..b}. f(x)$$k \<le> B" "k<DIM('b)" 
  2859   shows "(integral({a..b}) f)$$k \<le> B * content({a..b})"
  2860   apply(rule has_integral_component_ubound) using assms unfolding has_integral_integral by auto
  2861 
  2862 subsection {* Uniform limit of integrable functions is integrable. *}
  2863 
  2864 lemma integrable_uniform_limit: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::banach"
  2865   assumes "\<forall>e>0. \<exists>g. (\<forall>x\<in>{a..b}. norm(f x - g x) \<le> e) \<and> g integrable_on {a..b}"
  2866   shows "f integrable_on {a..b}"
  2867 proof- { presume *:"content {a..b} > 0 \<Longrightarrow> ?thesis"
  2868     show ?thesis apply cases apply(rule *,assumption)
  2869       unfolding content_lt_nz integrable_on_def using has_integral_null by auto }
  2870   assume as:"content {a..b} > 0"
  2871   have *:"\<And>P. \<forall>e>(0::real). P e \<Longrightarrow> \<forall>n::nat. P (inverse (real n+1))" by auto
  2872   from choice[OF *[OF assms]] guess g .. note g=conjunctD2[OF this[rule_format],rule_format]
  2873   from choice[OF allI[OF g(2)[unfolded integrable_on_def], of "\<lambda>x. x"]] guess i .. note i=this[rule_format]
  2874   
  2875   have "Cauchy i" unfolding Cauchy_def
  2876   proof(rule,rule) fix e::real assume "e>0"
  2877     hence "e / 4 / content {a..b} > 0" using as by(auto simp add:field_simps)
  2878     then guess M apply-apply(subst(asm) real_arch_inv) by(erule exE conjE)+ note M=this
  2879     show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (i m) (i n) < e" apply(rule_tac x=M in exI,rule,rule,rule,rule)
  2880     proof- case goal1 have "e/4>0" using `e>0` by auto note * = i[unfolded has_integral,rule_format,OF this]
  2881       from *[of m] guess gm apply-by(erule conjE exE)+ note gm=this[rule_format]
  2882       from *[of n] guess gn apply-by(erule conjE exE)+ note gn=this[rule_format]
  2883       from fine_division_exists[OF gauge_inter[OF gm(1) gn(1)], of a b] guess p . note p=this
  2884       have lem2:"\<And>s1 s2 i1 i2. norm(s2 - s1) \<le> e/2 \<Longrightarrow> norm(s1 - i1) < e / 4 \<Longrightarrow> norm(s2 - i2) < e / 4 \<Longrightarrow>norm(i1 - i2) < e"
  2885       proof- case goal1 have "norm (i1 - i2) \<le> norm (i1 - s1) + norm (s1 - s2) + norm (s2 - i2)"
  2886           using norm_triangle_ineq[of "i1 - s1" "s1 - i2"]
  2887           using norm_triangle_ineq[of "s1 - s2" "s2 - i2"] by(auto simp add:algebra_simps)
  2888         also have "\<dots> < e" using goal1 unfolding norm_minus_commute by(auto simp add:algebra_simps)
  2889         finally show ?case .
  2890       qed
  2891       show ?case unfolding dist_norm apply(rule lem2) defer
  2892         apply(rule gm(2)[OF conjI[OF p(1)]],rule_tac[2] gn(2)[OF conjI[OF p(1)]])
  2893         using conjunctD2[OF p(2)[unfolded fine_inter]] apply- apply assumption+ apply(rule order_trans)
  2894         apply(rule rsum_diff_bound[OF p(1), where e="2 / real M"])
  2895       proof show "2 / real M * content {a..b} \<le> e / 2" unfolding divide_inverse 
  2896           using M as by(auto simp add:field_simps)
  2897         fix x assume x:"x \<in> {a..b}"
  2898         have "norm (f x - g n x) + norm (f x - g m x) \<le> inverse (real n + 1) + inverse (real m + 1)" 
  2899             using g(1)[OF x, of n] g(1)[OF x, of m] by auto
  2900         also have "\<dots> \<le> inverse (real M) + inverse (real M)" apply(rule add_mono)
  2901           apply(rule_tac[!] le_imp_inverse_le) using goal1 M by auto
  2902         also have "\<dots> = 2 / real M" unfolding divide_inverse by auto
  2903         finally show "norm (g n x - g m x) \<le> 2 / real M"
  2904           using norm_triangle_le[of "g n x - f x" "f x - g m x" "2 / real M"]
  2905           by(auto simp add:algebra_simps simp add:norm_minus_commute)
  2906       qed qed qed
  2907   from this[unfolded convergent_eq_cauchy[THEN sym]] guess s .. note s=this
  2908 
  2909   show ?thesis unfolding integrable_on_def apply(rule_tac x=s in exI) unfolding has_integral
  2910   proof(rule,rule)  
  2911     case goal1 hence *:"e/3 > 0" by auto
  2912     from LIMSEQ_D [OF s this] guess N1 .. note N1=this
  2913     from goal1 as have "e / 3 / content {a..b} > 0" by(auto simp add:field_simps)
  2914     from real_arch_invD[OF this] guess N2 apply-by(erule exE conjE)+ note N2=this
  2915     from i[of "N1 + N2",unfolded has_integral,rule_format,OF *] guess g' .. note g'=conjunctD2[OF this,rule_format]
  2916     have lem:"\<And>sf sg i. norm(sf - sg) \<le> e / 3 \<Longrightarrow> norm(i - s) < e / 3 \<Longrightarrow> norm(sg - i) < e / 3 \<Longrightarrow> norm(sf - s) < e"
  2917     proof- case goal1 have "norm (sf - s) \<le> norm (sf - sg) + norm (sg - i) + norm (i - s)"
  2918         using norm_triangle_ineq[of "sf - sg" "sg - s"]
  2919         using norm_triangle_ineq[of "sg -  i" " i - s"] by(auto simp add:algebra_simps)
  2920       also have "\<dots> < e" using goal1 unfolding norm_minus_commute by(auto simp add:algebra_simps)
  2921       finally show ?case .
  2922     qed
  2923     show ?case apply(rule_tac x=g' in exI) apply(rule,rule g')
  2924     proof(rule,rule) fix p assume p:"p tagged_division_of {a..b} \<and> g' fine p" note * = g'(2)[OF this]
  2925       show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - s) < e" apply-apply(rule lem[OF _ _ *])
  2926         apply(rule order_trans,rule rsum_diff_bound[OF p[THEN conjunct1]]) apply(rule,rule g,assumption)
  2927       proof- have "content {a..b} < e / 3 * (real N2)"
  2928           using N2 unfolding inverse_eq_divide using as by(auto simp add:field_simps)
  2929         hence "content {a..b} < e / 3 * (real (N1 + N2) + 1)"
  2930           apply-apply(rule less_le_trans,assumption) using `e>0` by auto 
  2931         thus "inverse (real (N1 + N2) + 1) * content {a..b} \<le> e / 3"
  2932           unfolding inverse_eq_divide by(auto simp add:field_simps)
  2933         show "norm (i (N1 + N2) - s) < e / 3" by(rule N1[rule_format],auto)
  2934       qed qed qed qed
  2935 
  2936 subsection {* Negligible sets. *}
  2937 
  2938 definition "negligible (s::('a::ordered_euclidean_space) set) \<equiv> (\<forall>a b. ((indicator s :: 'a\<Rightarrow>real) has_integral 0) {a..b})"
  2939 
  2940 subsection {* Negligibility of hyperplane. *}
  2941 
  2942 lemma vsum_nonzero_image_lemma: 
  2943   assumes "finite s" "g(a) = 0"
  2944   "\<forall>x\<in>s. \<forall>y\<in>s. f x = f y \<and> x \<noteq> y \<longrightarrow> g(f x) = 0"
  2945   shows "setsum g {f x |x. x \<in> s \<and> f x \<noteq> a} = setsum (g o f) s"
  2946   unfolding setsum_iterate[OF assms(1)] apply(subst setsum_iterate) defer
  2947   apply(rule iterate_nonzero_image_lemma) apply(rule assms monoidal_monoid)+
  2948   unfolding assms using neutral_add unfolding neutral_add using assms by auto 
  2949 
  2950 lemma interval_doublesplit:  fixes a::"'a::ordered_euclidean_space" assumes "k<DIM('a)"
  2951   shows "{a..b} \<inter> {x . abs(x$$k - c) \<le> (e::real)} = 
  2952   {(\<chi>\<chi> i. if i = k then max (a$$k) (c - e) else a$$i) .. (\<chi>\<chi> i. if i = k then min (b$$k) (c + e) else b$$i)}"
  2953 proof- have *:"\<And>x c e::real. abs(x - c) \<le> e \<longleftrightarrow> x \<ge> c - e \<and> x \<le> c + e" by auto
  2954   have **:"\<And>s P Q. s \<inter> {x. P x \<and> Q x} = (s \<inter> {x. Q x}) \<inter> {x. P x}" by blast
  2955   show ?thesis unfolding * ** interval_split[OF assms] by(rule refl) qed
  2956 
  2957 lemma division_doublesplit: fixes a::"'a::ordered_euclidean_space" assumes "p division_of {a..b}" and k:"k<DIM('a)"
  2958   shows "{l \<inter> {x. abs(x$$k - c) \<le> e} |l. l \<in> p \<and> l \<inter> {x. abs(x$$k - c) \<le> e} \<noteq> {}} division_of ({a..b} \<inter> {x. abs(x$$k - c) \<le> e})"
  2959 proof- have *:"\<And>x c. abs(x - c) \<le> e \<longleftrightarrow> x \<ge> c - e \<and> x \<le> c + e" by auto
  2960   have **:"\<And>p q p' q'. p division_of q \<Longrightarrow> p = p' \<Longrightarrow> q = q' \<Longrightarrow> p' division_of q'" by auto
  2961   note division_split(1)[OF assms, where c="c+e",unfolded interval_split[OF k]]
  2962   note division_split(2)[OF this, where c="c-e" and k=k,OF k] 
  2963   thus ?thesis apply(rule **) using k apply- unfolding interval_doublesplit unfolding * unfolding interval_split interval_doublesplit
  2964     apply(rule set_eqI) unfolding mem_Collect_eq apply rule apply(erule conjE exE)+ apply(rule_tac x=la in exI) defer
  2965     apply(erule conjE exE)+ apply(rule_tac x="l \<inter> {x. c + e \<ge> x $$ k}" in exI) apply rule defer apply rule
  2966     apply(rule_tac x=l in exI) by blast+ qed
  2967 
  2968 lemma content_doublesplit: fixes a::"'a::ordered_euclidean_space" assumes "0 < e" and k:"k<DIM('a)"
  2969   obtains d where "0 < d" "content({a..b} \<inter> {x. abs(x$$k - c) \<le> d}) < e"
  2970 proof(cases "content {a..b} = 0")
  2971   case True show ?thesis apply(rule that[of 1]) defer unfolding interval_doublesplit[OF k]
  2972     apply(rule le_less_trans[OF content_subset]) defer apply(subst True)
  2973     unfolding interval_doublesplit[THEN sym,OF k] using assms by auto 
  2974 next case False def d \<equiv> "e / 3 / setprod (\<lambda>i. b$$i - a$$i) ({..<DIM('a)} - {k})"
  2975   note False[unfolded content_eq_0 not_ex not_le, rule_format]
  2976   hence "\<And>x. x<DIM('a) \<Longrightarrow> b$$x > a$$x" by(auto simp add:not_le)
  2977   hence prod0:"0 < setprod (\<lambda>i. b$$i - a$$i) ({..<DIM('a)} - {k})" apply-apply(rule setprod_pos) by(auto simp add:field_simps)
  2978   hence "d > 0" unfolding d_def using assms by(auto simp add:field_simps) thus ?thesis
  2979   proof(rule that[of d]) have *:"{..<DIM('a)} = insert k ({..<DIM('a)} - {k})" using k by auto
  2980     have **:"{a..b} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d} \<noteq> {} \<Longrightarrow> 
  2981       (\<Prod>i\<in>{..<DIM('a)} - {k}. interval_upperbound ({a..b} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) $$ i
  2982       - interval_lowerbound ({a..b} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) $$ i)
  2983       = (\<Prod>i\<in>{..<DIM('a)} - {k}. b$$i - a$$i)" apply(rule setprod_cong,rule refl) 
  2984       unfolding interval_doublesplit[OF k] apply(subst interval_bounds) defer apply(subst interval_bounds)
  2985       unfolding interval_eq_empty not_ex not_less by auto
  2986     show "content ({a..b} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) < e" apply(cases) unfolding content_def apply(subst if_P,assumption,rule assms)
  2987       unfolding if_not_P apply(subst *) apply(subst setprod_insert) unfolding **
  2988       unfolding interval_doublesplit[OF k] interval_eq_empty not_ex not_less prefer 3
  2989       apply(subst interval_bounds) defer apply(subst interval_bounds) unfolding euclidean_lambda_beta'[OF k] if_P[OF refl]
  2990     proof- have "(min (b $$ k) (c + d) - max (a $$ k) (c - d)) \<le> 2 * d" by auto
  2991       also have "... < e / (\<Prod>i\<in>{..<DIM('a)} - {k}. b $$ i - a $$ i)" unfolding d_def using assms prod0 by(auto simp add:field_simps)
  2992       finally show "(min (b $$ k) (c + d) - max (a $$ k) (c - d)) * (\<Prod>i\<in>{..<DIM('a)} - {k}. b $$ i - a $$ i) < e"
  2993         unfolding pos_less_divide_eq[OF prod0] . qed auto qed qed
  2994 
  2995 lemma negligible_standard_hyperplane[intro]: fixes type::"'a::ordered_euclidean_space" assumes k:"k<DIM('a)"
  2996   shows "negligible {x::'a. x$$k = (c::real)}" 
  2997   unfolding negligible_def has_integral apply(rule,rule,rule,rule)
  2998 proof-
  2999   case goal1 from content_doublesplit[OF this k,of a b c] guess d . note d=this
  3000   let ?i = "indicator {x::'a. x$$k = c} :: 'a\<Rightarrow>real"
  3001   show ?case apply(rule_tac x="\<lambda>x. ball x d" in exI) apply(rule,rule gauge_ball,rule d)
  3002   proof(rule,rule) fix p assume p:"p tagged_division_of {a..b} \<and> (\<lambda>x. ball x d) fine p"
  3003     have *:"(\<Sum>(x, ka)\<in>p. content ka *\<^sub>R ?i x) = (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. abs(x$$k - c) \<le> d}) *\<^sub>R ?i x)"
  3004       apply(rule setsum_cong2) unfolding split_paired_all real_scaleR_def mult_cancel_right split_conv
  3005       apply(cases,rule disjI1,assumption,rule disjI2)
  3006     proof- fix x l assume as:"(x,l)\<in>p" "?i x \<noteq> 0" hence xk:"x$$k = c" unfolding indicator_def apply-by(rule ccontr,auto)
  3007       show "content l = content (l \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d})" apply(rule arg_cong[where f=content])
  3008         apply(rule set_eqI,rule,rule) unfolding mem_Collect_eq
  3009       proof- fix y assume y:"y\<in>l" note p[THEN conjunct2,unfolded fine_def,rule_format,OF as(1),unfolded split_conv]
  3010         note this[unfolded subset_eq mem_ball dist_norm,rule_format,OF y] note le_less_trans[OF component_le_norm[of _ k] this]
  3011         thus "\<bar>y $$ k - c\<bar> \<le> d" unfolding euclidean_simps xk by auto
  3012       qed auto qed
  3013     note p'= tagged_division_ofD[OF p[THEN conjunct1]] and p''=division_of_tagged_division[OF p[THEN conjunct1]]
  3014     show "norm ((\<Sum>(x, ka)\<in>p. content ka *\<^sub>R ?i x) - 0) < e" unfolding diff_0_right * unfolding real_scaleR_def real_norm_def
  3015       apply(subst abs_of_nonneg) apply(rule setsum_nonneg,rule) unfolding split_paired_all split_conv
  3016       apply(rule mult_nonneg_nonneg) apply(drule p'(4)) apply(erule exE)+ apply(rule_tac b=b in back_subst)
  3017       prefer 2 apply(subst(asm) eq_commute) apply assumption
  3018       apply(subst interval_doublesplit[OF k]) apply(rule content_pos_le) apply(rule indicator_pos_le)
  3019     proof- have "(\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) * ?i x) \<le> (\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}))"
  3020         apply(rule setsum_mono) unfolding split_paired_all split_conv 
  3021         apply(rule mult_right_le_one_le) apply(drule p'(4)) by(auto simp add:interval_doublesplit[OF k])
  3022       also have "... < e" apply(subst setsum_over_tagged_division_lemma[OF p[THEN conjunct1]])
  3023       proof- case goal1 have "content ({u..v} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) \<le> content {u..v}"
  3024           unfolding interval_doublesplit[OF k] apply(rule content_subset) unfolding interval_doublesplit[THEN sym,OF k] by auto
  3025         thus ?case unfolding goal1 unfolding interval_doublesplit[OF k] using content_pos_le by smt
  3026       next have *:"setsum content {l \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d} |l. l \<in> snd ` p \<and> l \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d} \<noteq> {}} \<ge> 0"
  3027           apply(rule setsum_nonneg,rule) unfolding mem_Collect_eq image_iff apply(erule exE bexE conjE)+ unfolding split_paired_all 
  3028         proof- fix x l a b assume as:"x = l \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}" "(a, b) \<in> p" "l = snd (a, b)"
  3029           guess u v using p'(4)[OF as(2)] apply-by(erule exE)+ note * = this
  3030           show "content x \<ge> 0" unfolding as snd_conv * interval_doublesplit[OF k] by(rule content_pos_le)
  3031         qed have **:"norm (1::real) \<le> 1" by auto note division_doublesplit[OF p'' k,unfolded interval_doublesplit[OF k]]
  3032         note dsum_bound[OF this **,unfolded interval_doublesplit[THEN sym,OF k]]
  3033         note this[unfolded real_scaleR_def real_norm_def mult_1_right mult_1, of c d] note le_less_trans[OF this d(2)]
  3034         from this[unfolded abs_of_nonneg[OF *]] show "(\<Sum>ka\<in>snd ` p. content (ka \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d})) < e"
  3035           apply(subst vsum_nonzero_image_lemma[of "snd ` p" content "{}", unfolded o_def,THEN sym])
  3036           apply(rule finite_imageI p' content_empty)+ unfolding forall_in_division[OF p'']
  3037         proof(rule,rule,rule,rule,rule,rule,rule,erule conjE) fix m n u v
  3038           assume as:"{m..n} \<in> snd ` p" "{u..v} \<in> snd ` p" "{m..n} \<noteq> {u..v}"  "{m..n} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d} = {u..v} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}"
  3039           have "({m..n} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) \<inter> ({u..v} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) \<subseteq> {m..n} \<inter> {u..v}" by blast
  3040           note interior_mono[OF this, unfolded division_ofD(5)[OF p'' as(1-3)] interior_inter[of "{m..n}"]]
  3041           hence "interior ({m..n} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) = {}" unfolding as Int_absorb by auto
  3042           thus "content ({m..n} \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) = 0" unfolding interval_doublesplit[OF k] content_eq_0_interior[THEN sym] .
  3043         qed qed
  3044       finally show "(\<Sum>(x, ka)\<in>p. content (ka \<inter> {x. \<bar>x $$ k - c\<bar> \<le> d}) * ?i x) < e" .
  3045     qed qed qed
  3046 
  3047 subsection {* A technical lemma about "refinement" of division. *}
  3048 
  3049 lemma tagged_division_finer: fixes p::"(('a::ordered_euclidean_space) \<times> (('a::ordered_euclidean_space) set)) set"
  3050   assumes "p tagged_division_of {a..b}" "gauge d"
  3051   obtains q where "q tagged_division_of {a..b}" "d fine q" "\<forall>(x,k) \<in> p. k \<subseteq> d(x) \<longrightarrow> (x,k) \<in> q"
  3052 proof-
  3053   let ?P = "\<lambda>p. p tagged_partial_division_of {a..b} \<longrightarrow> gauge d \<longrightarrow>
  3054     (\<exists>q. q tagged_division_of (\<Union>{k. \<exists>x. (x,k) \<in> p}) \<and> d fine q \<and>
  3055                    (\<forall>(x,k) \<in> p. k \<subseteq> d(x) \<longrightarrow> (x,k) \<in> q))"
  3056   { have *:"finite p" "p tagged_partial_division_of {a..b}" using assms(1) unfolding tagged_division_of_def by auto
  3057     presume "\<And>p. finite p \<Longrightarrow> ?P p" from this[rule_format,OF * assms(2)] guess q .. note q=this
  3058     thus ?thesis apply-apply(rule that[of q]) unfolding tagged_division_ofD[OF assms(1)] by auto
  3059   } fix p::"(('a::ordered_euclidean_space) \<times> (('a::ordered_euclidean_space) set)) set" assume as:"finite p"
  3060   show "?P p" apply(rule,rule) using as proof(induct p) 
  3061     case empty show ?case apply(rule_tac x="{}" in exI) unfolding fine_def by auto
  3062   next case (insert xk p) guess x k using surj_pair[of xk] apply- by(erule exE)+ note xk=this
  3063     note tagged_partial_division_subset[OF insert(4) subset_insertI]
  3064     from insert(3)[OF this insert(5)] guess q1 .. note q1 = conjunctD3[OF this]
  3065     have *:"\<Union>{l. \<exists>y. (y,l) \<in> insert xk p} = k \<union> \<Union>{l. \<exists>y. (y,l) \<in> p}" unfolding xk by auto
  3066     note p = tagged_partial_division_ofD[OF insert(4)]
  3067     from p(4)[unfolded xk, OF insertI1] guess u v apply-by(erule exE)+ note uv=this
  3068 
  3069     have "finite {k. \<exists>x. (x, k) \<in> p}" 
  3070       apply(rule finite_subset[of _ "snd ` p"],rule) unfolding subset_eq image_iff mem_Collect_eq
  3071       apply(erule exE,rule_tac x="(xa,x)" in bexI) using p by auto
  3072     hence int:"interior {u..v} \<inter> interior (\<Union>{k. \<exists>x. (x, k) \<in> p}) = {}"
  3073       apply(rule inter_interior_unions_intervals) apply(rule open_interior) apply(rule_tac[!] ballI)
  3074       unfolding mem_Collect_eq apply(erule_tac[!] exE) apply(drule p(4)[OF insertI2],assumption)      
  3075       apply(rule p(5))  unfolding uv xk apply(rule insertI1,rule insertI2) apply assumption
  3076       using insert(2) unfolding uv xk by auto
  3077 
  3078     show ?case proof(cases "{u..v} \<subseteq> d x")
  3079       case True thus ?thesis apply(rule_tac x="{(x,{u..v})} \<union> q1" in exI) apply rule
  3080         unfolding * uv apply(rule tagged_division_union,rule tagged_division_of_self)
  3081         apply(rule p[unfolded xk uv] insertI1)+  apply(rule q1,rule int) 
  3082         apply(rule,rule fine_union,subst fine_def) defer apply(rule q1)
  3083         unfolding Ball_def split_paired_All split_conv apply(rule,rule,rule,rule)
  3084         apply(erule insertE) defer apply(rule UnI2) apply(drule q1(3)[rule_format]) unfolding xk uv by auto
  3085     next case False from fine_division_exists[OF assms(2), of u v] guess q2 . note q2=this
  3086       show ?thesis apply(rule_tac x="q2 \<union> q1" in exI)
  3087         apply rule unfolding * uv apply(rule tagged_division_union q2 q1 int fine_union)+
  3088         unfolding Ball_def split_paired_All split_conv apply rule apply(rule fine_union)
  3089         apply(rule q1 q2)+ apply(rule,rule,rule,rule) apply(erule insertE)
  3090         apply(rule UnI2) defer apply(drule q1(3)[rule_format])using False unfolding xk uv by auto
  3091     qed qed qed
  3092 
  3093 subsection {* Hence the main theorem about negligible sets. *}
  3094 
  3095 lemma finite_product_dependent: assumes "finite s" "\<And>x. x\<in>s\<Longrightarrow> finite (t x)"
  3096   shows "finite {(i, j) |i j. i \<in> s \<and> j \<in> t i}" using assms
  3097 proof(induct) case (insert x s) 
  3098   have *:"{(i, j) |i j. i \<in> insert x s \<and> j \<in> t i} = (\<lambda>y. (x,y)) ` (t x) \<union> {(i, j) |i j. i \<in> s \<and> j \<in> t i}" by auto
  3099   show ?case unfolding * apply(rule finite_UnI) using insert by auto qed auto
  3100 
  3101 lemma sum_sum_product: assumes "finite s" "\<forall>i\<in>s. finite (t i)"
  3102   shows "setsum (\<lambda>i. setsum (x i) (t i)::real) s = setsum (\<lambda>(i,j). x i j) {(i,j) | i j. i \<in> s \<and> j \<in> t i}" using assms
  3103 proof(induct) case (insert a s)
  3104   have *:"{(i, j) |i j. i \<in> insert a s \<and> j \<in> t i} = (\<lambda>y. (a,y)) ` (t a) \<union> {(i, j) |i j. i \<in> s \<and> j \<in> t i}" by auto
  3105   show ?case unfolding * apply(subst setsum_Un_disjoint) unfolding setsum_insert[OF insert(1-2)]
  3106     prefer 4 apply(subst insert(3)) unfolding add_right_cancel
  3107   proof- show "setsum (x a) (t a) = (\<Sum>(xa, y)\<in>Pair a ` t a. x xa y)" apply(subst setsum_reindex) unfolding inj_on_def by auto
  3108     show "finite {(i, j) |i j. i \<in> s \<and> j \<in> t i}" apply(rule finite_product_dependent) using insert by auto
  3109   qed(insert insert, auto) qed auto
  3110 
  3111 lemma has_integral_negligible: fixes f::"'b::ordered_euclidean_space \<Rightarrow> 'a::real_normed_vector"
  3112   assumes "negligible s" "\<forall>x\<in>(t - s). f x = 0"
  3113   shows "(f has_integral 0) t"
  3114 proof- presume P:"\<And>f::'b::ordered_euclidean_space \<Rightarrow> 'a. \<And>a b. (\<forall>x. ~(x \<in> s) \<longrightarrow> f x = 0) \<Longrightarrow> (f has_integral 0) ({a..b})"
  3115   let ?f = "(\<lambda>x. if x \<in> t then f x else 0)"
  3116   show ?thesis apply(rule_tac f="?f" in has_integral_eq) apply(rule) unfolding if_P apply(rule refl)
  3117     apply(subst has_integral_alt) apply(cases,subst if_P,assumption) unfolding if_not_P
  3118   proof- assume "\<exists>a b. t = {a..b}" then guess a b apply-by(erule exE)+ note t = this
  3119     show "(?f has_integral 0) t" unfolding t apply(rule P) using assms(2) unfolding t by auto
  3120   next show "\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b} \<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> t then ?f x else 0) has_integral z) {a..b} \<and> norm (z - 0) < e)"
  3121       apply(safe,rule_tac x=1 in exI,rule) apply(rule zero_less_one,safe) apply(rule_tac x=0 in exI)
  3122       apply(rule,rule P) using assms(2) by auto
  3123   qed
  3124 next fix f::"'b \<Rightarrow> 'a" and a b::"'b" assume assm:"\<forall>x. x \<notin> s \<longrightarrow> f x = 0" 
  3125   show "(f has_integral 0) {a..b}" unfolding has_integral
  3126   proof(safe) case goal1
  3127     hence "\<And>n. e / 2 / ((real n+1) * (2 ^ n)) > 0" 
  3128       apply-apply(rule divide_pos_pos) defer apply(rule mult_pos_pos) by(auto simp add:field_simps)
  3129     note assms(1)[unfolded negligible_def has_integral,rule_format,OF this,of a b] note allI[OF this,of "\<lambda>x. x"] 
  3130     from choice[OF this] guess d .. note d=conjunctD2[OF this[rule_format]]
  3131     show ?case apply(rule_tac x="\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x" in exI) 
  3132     proof safe show "gauge (\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x)" using d(1) unfolding gauge_def by auto
  3133       fix p assume as:"p tagged_division_of {a..b}" "(\<lambda>x. d (nat \<lfloor>norm (f x)\<rfloor>) x) fine p" 
  3134       let ?goal = "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) < e"
  3135       { presume "p\<noteq>{} \<Longrightarrow> ?goal" thus ?goal apply(cases "p={}") using goal1 by auto  }
  3136       assume as':"p \<noteq> {}" from real_arch_simple[of "Sup((\<lambda>(x,k). norm(f x)) ` p)"] guess N ..
  3137       hence N:"\<forall>x\<in>(\<lambda>(x, k). norm (f x)) ` p. x \<le> real N" apply(subst(asm) Sup_finite_le_iff) using as as' by auto
  3138       have "\<forall>i. \<exists>q. q tagged_division_of {a..b} \<and> (d i) fine q \<and> (\<forall>(x, k)\<in>p. k \<subseteq> (d i) x \<longrightarrow> (x, k) \<in> q)"
  3139         apply(rule,rule tagged_division_finer[OF as(1) d(1)]) by auto
  3140       from choice[OF this] guess q .. note q=conjunctD3[OF this[rule_format]]
  3141       have *:"\<And>i. (\<Sum>(x, k)\<in>q i. content k *\<^sub>R indicator s x) \<ge> (0::real)" apply(rule setsum_nonneg,safe) 
  3142         unfolding real_scaleR_def apply(rule mult_nonneg_nonneg) apply(drule tagged_division_ofD(4)[OF q(1)]) by auto
  3143       have **:"\<And>f g s t. finite s \<Longrightarrow> finite t \<Longrightarrow> (\<forall>(x,y) \<in> t. (0::real) \<le> g(x,y)) \<Longrightarrow> (\<forall>y\<in>s. \<exists>x. (x,y) \<in> t \<and> f(y) \<le> g(x,y)) \<Longrightarrow> setsum f s \<le> setsum g t"
  3144       proof- case goal1 thus ?case apply-apply(rule setsum_le_included[of s t g snd f]) prefer 4
  3145           apply safe apply(erule_tac x=x in ballE) apply(erule exE) apply(rule_tac x="(xa,x)" in bexI) by auto qed
  3146       have "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - 0) \<le> setsum (\<lambda>i. (real i + 1) *
  3147                      norm(setsum (\<lambda>(x,k). content k *\<^sub>R indicator s x :: real) (q i))) {0..N+1}"
  3148         unfolding real_norm_def setsum_right_distrib abs_of_nonneg[OF *] diff_0_right
  3149         apply(rule order_trans,rule norm_setsum) apply(subst sum_sum_product) prefer 3 
  3150       proof(rule **,safe) show "finite {(i, j) |i j. i \<in> {0..N + 1} \<and> j \<in> q i}" apply(rule finite_product_dependent) using q by auto
  3151         fix i a b assume as'':"(a,b) \<in> q i" show "0 \<le> (real i + 1) * (content b *\<^sub>R indicator s a)"
  3152           unfolding real_scaleR_def apply(rule mult_nonneg_nonneg) defer apply(rule mult_nonneg_nonneg)
  3153           using tagged_division_ofD(4)[OF q(1) as''] by auto
  3154       next fix i::nat show "finite (q i)" using q by auto
  3155       next fix x k assume xk:"(x,k) \<in> p" def n \<equiv> "nat \<lfloor>norm (f x)\<rfloor>"
  3156         have *:"norm (f x) \<in> (\<lambda>(x, k). norm (f x)) ` p" using xk by auto
  3157         have nfx:"real n \<le> norm(f x)" "norm(f x) \<le> real n + 1" unfolding n_def by auto
  3158         hence "n \<in> {0..N + 1}" using N[rule_format,OF *] by auto
  3159         moreover  note as(2)[unfolded fine_def,rule_format,OF xk,unfolded split_conv]
  3160         note q(3)[rule_format,OF xk,unfolded split_conv,rule_format,OF this] note this[unfolded n_def[symmetric]]
  3161         moreover have "norm (content k *\<^sub>R f x) \<le> (real n + 1) * (content k * indicator s x)"
  3162         proof(cases "x\<in>s") case False thus ?thesis using assm by auto
  3163         next case True have *:"content k \<ge> 0" using tagged_division_ofD(4)[OF as(1) xk] by auto
  3164           moreover have "content k * norm (f x) \<le> content k * (real n + 1)" apply(rule mult_mono) using nfx * by auto
  3165           ultimately show ?thesis unfolding abs_mult using nfx True by(auto simp add:field_simps)
  3166         qed ultimately show "\<exists>y. (y, x, k) \<in> {(i, j) |i j. i \<in> {0..N + 1} \<and> j \<in> q i} \<and> norm (content k *\<^sub>R f x) \<le> (real y + 1) * (content k *\<^sub>R indicator s x)"
  3167           apply(rule_tac x=n in exI,safe) apply(rule_tac x=n in exI,rule_tac x="(x,k)" in exI,safe) by auto
  3168       qed(insert as, auto)
  3169       also have "... \<le> setsum (\<lambda>i. e / 2 / 2 ^ i) {0..N+1}" apply(rule setsum_mono) 
  3170       proof- case goal1 thus ?case apply(subst mult_commute, subst pos_le_divide_eq[THEN sym])
  3171           using d(2)[rule_format,of "q i" i] using q[rule_format] by(auto simp add:field_simps)
  3172       qed also have "... < e * inverse 2 * 2" unfolding divide_inverse setsum_right_distrib[THEN sym]
  3173         apply(rule mult_strict_left_mono) unfolding power_inverse atLeastLessThanSuc_atLeastAtMost[THEN sym]
  3174         apply(subst sumr_geometric) using goal1 by auto
  3175       finally show "?goal" by auto qed qed qed
  3176 
  3177 lemma has_integral_spike: fixes f::"'b::ordered_euclidean_space \<Rightarrow> 'a::real_normed_vector"
  3178   assumes "negligible s" "(\<forall>x\<in>(t - s). g x = f x)" "(f has_integral y) t"
  3179   shows "(g has_integral y) t"
  3180 proof- { fix a b::"'b" and f g ::"'b \<Rightarrow> 'a" and y::'a
  3181     assume as:"\<forall>x \<in> {a..b} - s. g x = f x" "(f has_integral y) {a..b}"
  3182     have "((\<lambda>x. f x + (g x - f x)) has_integral (y + 0)) {a..b}" apply(rule has_integral_add[OF as(2)])
  3183       apply(rule has_integral_negligible[OF assms(1)]) using as by auto
  3184     hence "(g has_integral y) {a..b}" by auto } note * = this
  3185   show ?thesis apply(subst has_integral_alt) using assms(2-) apply-apply(rule cond_cases,safe)
  3186     apply(rule *, assumption+) apply(subst(asm) has_integral_alt) unfolding if_not_P
  3187     apply(erule_tac x=e in allE,safe,rule_tac x=B in exI,safe) apply(erule_tac x=a in allE,erule_tac x=b in allE,safe)
  3188     apply(rule_tac x=z in exI,safe) apply(rule *[where fa2="\<lambda>x. if x\<in>t then f x else 0"]) by auto qed
  3189 
  3190 lemma has_integral_spike_eq:
  3191   assumes "negligible s" "\<forall>x\<in>(t - s). g x = f x"
  3192   shows "((f has_integral y) t \<longleftrightarrow> (g has_integral y) t)"
  3193   apply rule apply(rule_tac[!] has_integral_spike[OF assms(1)]) using assms(2) by auto
  3194 
  3195 lemma integrable_spike: assumes "negligible s" "\<forall>x\<in>(t - s). g x = f x" "f integrable_on t"
  3196   shows "g integrable_on  t"
  3197   using assms unfolding integrable_on_def apply-apply(erule exE)
  3198   apply(rule,rule has_integral_spike) by fastforce+
  3199 
  3200 lemma integral_spike: assumes "negligible s" "\<forall>x\<in>(t - s). g x = f x"
  3201   shows "integral t f = integral t g"
  3202   unfolding integral_def using has_integral_spike_eq[OF assms] by auto
  3203 
  3204 subsection {* Some other trivialities about negligible sets. *}
  3205 
  3206 lemma negligible_subset[intro]: assumes "negligible s" "t \<subseteq> s" shows "negligible t" unfolding negligible_def 
  3207 proof(safe) case goal1 show ?case using assms(1)[unfolded negligible_def,rule_format,of a b]
  3208     apply-apply(rule has_integral_spike[OF assms(1)]) defer apply assumption
  3209     using assms(2) unfolding indicator_def by auto qed
  3210 
  3211 lemma negligible_diff[intro?]: assumes "negligible s" shows "negligible(s - t)" using assms by auto
  3212 
  3213 lemma negligible_inter: assumes "negligible s \<or> negligible t" shows "negligible(s \<inter> t)" using assms by auto
  3214 
  3215 lemma negligible_union: assumes "negligible s" "negligible t" shows "negligible (s \<union> t)" unfolding negligible_def 
  3216 proof safe case goal1 note assm = assms[unfolded negligible_def,rule_format,of a b]
  3217   thus ?case apply(subst has_integral_spike_eq[OF assms(2)])
  3218     defer apply assumption unfolding indicator_def by auto qed
  3219 
  3220 lemma negligible_union_eq[simp]: "negligible (s \<union> t) \<longleftrightarrow> (negligible s \<and> negligible t)"
  3221   using negligible_union by auto
  3222 
  3223 lemma negligible_sing[intro]: "negligible {a::_::ordered_euclidean_space}" 
  3224   using negligible_standard_hyperplane[of 0 "a$$0"] by auto 
  3225 
  3226 lemma negligible_insert[simp]: "negligible(insert a s) \<longleftrightarrow> negligible s"
  3227   apply(subst insert_is_Un) unfolding negligible_union_eq by auto
  3228 
  3229 lemma negligible_empty[intro]: "negligible {}" by auto
  3230 
  3231 lemma negligible_finite[intro]: assumes "finite s" shows "negligible s"
  3232   using assms apply(induct s) by auto
  3233 
  3234 lemma negligible_unions[intro]: assumes "finite s" "\<forall>t\<in>s. negligible t" shows "negligible(\<Union>s)"
  3235   using assms by(induct,auto) 
  3236 
  3237 lemma negligible:  "negligible s \<longleftrightarrow> (\<forall>t::('a::ordered_euclidean_space) set. ((indicator s::'a\<Rightarrow>real) has_integral 0) t)"
  3238   apply safe defer apply(subst negligible_def)
  3239 proof -
  3240   fix t::"'a set" assume as:"negligible s"
  3241   have *:"(\<lambda>x. if x \<in> s \<inter> t then 1 else 0) = (\<lambda>x. if x\<in>t then if x\<in>s then 1 else 0 else 0)"
  3242     by auto
  3243   show "((indicator s::'a\<Rightarrow>real) has_integral 0) t"
  3244     apply(subst has_integral_alt)
  3245     apply(cases,subst if_P,assumption)
  3246     unfolding if_not_P
  3247     apply(safe,rule as[unfolded negligible_def,rule_format])
  3248     apply(rule_tac x=1 in exI)
  3249     apply(safe,rule zero_less_one)
  3250     apply(rule_tac x=0 in exI)
  3251     using negligible_subset[OF as,of "s \<inter> t"]
  3252     unfolding negligible_def indicator_def [abs_def]
  3253     unfolding *
  3254     apply auto
  3255     done
  3256 qed auto
  3257 
  3258 subsection {* Finite case of the spike theorem is quite commonly needed. *}
  3259 
  3260 lemma has_integral_spike_finite: assumes "finite s" "\<forall>x\<in>t-s. g x = f x" 
  3261   "(f has_integral y) t" shows "(g has_integral y) t"
  3262   apply(rule has_integral_spike) using assms by auto
  3263 
  3264 lemma has_integral_spike_finite_eq: assumes "finite s" "\<forall>x\<in>t-s. g x = f x"
  3265   shows "((f has_integral y) t \<longleftrightarrow> (g has_integral y) t)"
  3266   apply rule apply(rule_tac[!] has_integral_spike_finite) using assms by auto
  3267 
  3268 lemma integrable_spike_finite:
  3269   assumes "finite s" "\<forall>x\<in>t-s. g x = f x" "f integrable_on t" shows "g integrable_on  t"
  3270   using assms unfolding integrable_on_def apply safe apply(rule_tac x=y in exI)
  3271   apply(rule has_integral_spike_finite) by auto
  3272 
  3273 subsection {* In particular, the boundary of an interval is negligible. *}
  3274 
  3275 lemma negligible_frontier_interval: "negligible({a::'a::ordered_euclidean_space..b} - {a<..<b})"
  3276 proof- let ?A = "\<Union>((\<lambda>k. {x. x$$k = a$$k} \<union> {x::'a. x$$k = b$$k}) ` {..<DIM('a)})"
  3277   have "{a..b} - {a<..<b} \<subseteq> ?A" apply rule unfolding Diff_iff mem_interval not_all
  3278     apply(erule conjE exE)+ apply(rule_tac X="{x. x $$ xa = a $$ xa} \<union> {x. x $$ xa = b $$ xa}" in UnionI)
  3279     apply(erule_tac[!] x=xa in allE) by auto
  3280   thus ?thesis apply-apply(rule negligible_subset[of ?A]) apply(rule negligible_unions[OF finite_imageI]) by auto qed
  3281 
  3282 lemma has_integral_spike_interior:
  3283   assumes "\<forall>x\<in>{a<..<b}. g x = f x" "(f has_integral y) ({a..b})" shows "(g has_integral y) ({a..b})"
  3284   apply(rule has_integral_spike[OF negligible_frontier_interval _ assms(2)]) using assms(1) by auto
  3285 
  3286 lemma has_integral_spike_interior_eq:
  3287   assumes "\<forall>x\<in>{a<..<b}. g x = f x" shows "((f has_integral y) ({a..b}) \<longleftrightarrow> (g has_integral y) ({a..b}))"
  3288   apply rule apply(rule_tac[!] has_integral_spike_interior) using assms by auto
  3289 
  3290 lemma integrable_spike_interior: assumes "\<forall>x\<in>{a<..<b}. g x = f x" "f integrable_on {a..b}" shows "g integrable_on {a..b}"
  3291   using  assms unfolding integrable_on_def using has_integral_spike_interior[OF assms(1)] by auto
  3292 
  3293 subsection {* Integrability of continuous functions. *}
  3294 
  3295 lemma neutral_and[simp]: "neutral op \<and> = True"
  3296   unfolding neutral_def apply(rule some_equality) by auto
  3297 
  3298 lemma monoidal_and[intro]: "monoidal op \<and>" unfolding monoidal_def by auto
  3299 
  3300 lemma iterate_and[simp]: assumes "finite s" shows "(iterate op \<and>) s p \<longleftrightarrow> (\<forall>x\<in>s. p x)" using assms
  3301 apply induct unfolding iterate_insert[OF monoidal_and] by auto
  3302 
  3303 lemma operative_division_and: assumes "operative op \<and> P" "d division_of {a..b}"
  3304   shows "(\<forall>i\<in>d. P i) \<longleftrightarrow> P {a..b}"
  3305   using operative_division[OF monoidal_and assms] division_of_finite[OF assms(2)] by auto
  3306 
  3307 lemma operative_approximable: assumes "0 \<le> e" fixes f::"'b::ordered_euclidean_space \<Rightarrow> 'a::banach"
  3308   shows "operative op \<and> (\<lambda>i. \<exists>g. (\<forall>x\<in>i. norm (f x - g (x::'b)) \<le> e) \<and> g integrable_on i)" unfolding operative_def neutral_and
  3309 proof safe fix a b::"'b" { assume "content {a..b} = 0"
  3310     thus "\<exists>g. (\<forall>x\<in>{a..b}. norm (f x - g x) \<le> e) \<and> g integrable_on {a..b}" 
  3311       apply(rule_tac x=f in exI) using assms by(auto intro!:integrable_on_null) }
  3312   { fix c k g assume as:"\<forall>x\<in>{a..b}. norm (f x - g x) \<le> e" "g integrable_on {a..b}" and k:"k<DIM('b)"
  3313     show "\<exists>g. (\<forall>x\<in>{a..b} \<inter> {x. x $$ k \<le> c}. norm (f x - g x) \<le> e) \<and> g integrable_on {a..b} \<inter> {x. x $$ k \<le> c}"
  3314       "\<exists>g. (\<forall>x\<in>{a..b} \<inter> {x. c \<le> x $$ k}. norm (f x - g x) \<le> e) \<and> g integrable_on {a..b} \<inter> {x. c \<le> x $$ k}"
  3315       apply(rule_tac[!] x=g in exI) using as(1) integrable_split[OF as(2) k] by auto }
  3316   fix c k g1 g2 assume as:"\<forall>x\<in>{a..b} \<inter> {x. x $$ k \<le> c}. norm (f x - g1 x) \<le> e" "g1 integrable_on {a..b} \<inter> {x. x $$ k \<le> c}"
  3317                           "\<forall>x\<in>{a..b} \<inter> {x. c \<le> x $$ k}. norm (f x - g2 x) \<le> e" "g2 integrable_on {a..b} \<inter> {x. c \<le> x $$ k}"
  3318   assume k:"k<DIM('b)"
  3319   let ?g = "\<lambda>x. if x$$k = c then f x else if x$$k \<le> c then g1 x else g2 x"
  3320   show "\<exists>g. (\<forall>x\<in>{a..b}. norm (f x - g x) \<le> e) \<and> g integrable_on {a..b}" apply(rule_tac x="?g" in exI)
  3321   proof safe case goal1 thus ?case apply- apply(cases "x$$k=c", case_tac "x$$k < c") using as assms by auto
  3322   next case goal2 presume "?g integrable_on {a..b} \<inter> {x. x $$ k \<le> c}" "?g integrable_on {a..b} \<inter> {x. x $$ k \<ge> c}"
  3323     then guess h1 h2 unfolding integrable_on_def by auto from has_integral_split[OF this k] 
  3324     show ?case unfolding integrable_on_def by auto
  3325   next show "?g integrable_on {a..b} \<inter> {x. x $$ k \<le> c}" "?g integrable_on {a..b} \<inter> {x. x $$ k \<ge> c}"
  3326       apply(rule_tac[!] integrable_spike[OF negligible_standard_hyperplane[of k c]]) using k as(2,4) by auto qed qed
  3327 
  3328 lemma approximable_on_division: fixes f::"'b::ordered_euclidean_space \<Rightarrow> 'a::banach"
  3329   assumes "0 \<le> e" "d division_of {a..b}" "\<forall>i\<in>d. \<exists>g. (\<forall>x\<in>i. norm (f x - g x) \<le> e) \<and> g integrable_on i"
  3330   obtains g where "\<forall>x\<in>{a..b}. norm (f x - g x) \<le> e" "g integrable_on {a..b}"
  3331 proof- note * = operative_division[OF monoidal_and operative_approximable[OF assms(1)] assms(2)]
  3332   note this[unfolded iterate_and[OF division_of_finite[OF assms(2)]]] from assms(3)[unfolded this[of f]]
  3333   guess g .. thus thesis apply-apply(rule that[of g]) by auto qed
  3334 
  3335 lemma integrable_continuous: fixes f::"'b::ordered_euclidean_space \<Rightarrow> 'a::banach"
  3336   assumes "continuous_on {a..b} f" shows "f integrable_on {a..b}"
  3337 proof(rule integrable_uniform_limit,safe) fix e::real assume e:"0 < e"
  3338   from compact_uniformly_continuous[OF assms compact_interval,unfolded uniformly_continuous_on_def,rule_format,OF e] guess d ..
  3339   note d=conjunctD2[OF this,rule_format]
  3340   from fine_division_exists[OF gauge_ball[OF d(1)], of a b] guess p . note p=this
  3341   note p' = tagged_division_ofD[OF p(1)]
  3342   have *:"\<forall>i\<in>snd ` p. \<exists>g. (\<forall>x\<in>i. norm (f x - g x) \<le> e) \<and> g integrable_on i"
  3343   proof(safe,unfold snd_conv) fix x l assume as:"(x,l) \<in> p" 
  3344     from p'(4)[OF this] guess a b apply-by(erule exE)+ note l=this
  3345     show "\<exists>g. (\<forall>x\<in>l. norm (f x - g x) \<le> e) \<and> g integrable_on l" apply(rule_tac x="\<lambda>y. f x" in exI)
  3346     proof safe show "(\<lambda>y. f x) integrable_on l" unfolding integrable_on_def l by(rule,rule has_integral_const)
  3347       fix y assume y:"y\<in>l" note fineD[OF p(2) as,unfolded subset_eq,rule_format,OF this]
  3348       note d(2)[OF _ _ this[unfolded mem_ball]]
  3349       thus "norm (f y - f x) \<le> e" using y p'(2-3)[OF as] unfolding dist_norm l norm_minus_commute by fastforce qed qed
  3350   from e have "0 \<le> e" by auto from approximable_on_division[OF this division_of_tagged_division[OF p(1)] *] guess g .
  3351   thus "\<exists>g. (\<forall>x\<in>{a..b}. norm (f x - g x) \<le> e) \<and> g integrable_on {a..b}" by auto qed 
  3352 
  3353 subsection {* Specialization of additivity to one dimension. *}
  3354 
  3355 lemma operative_1_lt: assumes "monoidal opp"
  3356   shows "operative opp f \<longleftrightarrow> ((\<forall>a b. b \<le> a \<longrightarrow> f {a..b::real} = neutral opp) \<and>
  3357                 (\<forall>a b c. a < c \<and> c < b \<longrightarrow> opp (f{a..c})(f{c..b}) = f {a..b}))"
  3358   unfolding operative_def content_eq_0 DIM_real less_one simp_thms(39,41) Eucl_real_simps
  3359     (* The dnf_simps simplify "\<exists> x. x= _ \<and> _" and "\<forall>k. k = _ \<longrightarrow> _" *)
  3360 proof safe fix a b c::"real" assume as:"\<forall>a b c. f {a..b} = opp (f ({a..b} \<inter> {x. x \<le> c}))
  3361     (f ({a..b} \<inter> {x. c \<le> x}))" "a < c" "c < b"
  3362     from this(2-) have "{a..b} \<inter> {x. x \<le> c} = {a..c}" "{a..b} \<inter> {x. x \<ge> c} = {c..b}" by auto
  3363     thus "opp (f {a..c}) (f {c..b}) = f {a..b}" unfolding as(1)[rule_format,of a b "c"] by auto
  3364 next fix a b c::real
  3365   assume as:"\<forall>a b. b \<le> a \<longrightarrow> f {a..b} = neutral opp" "\<forall>a b c. a < c \<and> c < b \<longrightarrow> opp (f {a..c}) (f {c..b}) = f {a..b}"
  3366   show "f {a..b} = opp (f ({a..b} \<inter> {x. x \<le> c})) (f ({a..b} \<inter> {x. c \<le> x}))"
  3367   proof(cases "c \<in> {a .. b}")
  3368     case False hence "c<a \<or> c>b" by auto
  3369     thus ?thesis apply-apply(erule disjE)
  3370     proof- assume "c<a" hence *:"{a..b} \<inter> {x. x \<le> c} = {1..0}"  "{a..b} \<inter> {x. c \<le> x} = {a..b}" by auto
  3371       show ?thesis unfolding * apply(subst as(1)[rule_format,of 0 1]) using assms by auto
  3372     next   assume "b<c" hence *:"{a..b} \<inter> {x. x \<le> c} = {a..b}"  "{a..b} \<inter> {x. c \<le> x} = {1..0}" by auto
  3373       show ?thesis unfolding * apply(subst as(1)[rule_format,of 0 1]) using assms by auto
  3374     qed
  3375   next case True hence *:"min (b) c = c" "max a c = c" by auto
  3376     have **:"0 < DIM(real)" by auto
  3377     have ***:"\<And>P Q. (\<chi>\<chi> i. if i = 0 then P i else Q i) = (P 0::real)" apply(subst euclidean_eq)
  3378       apply safe unfolding euclidean_lambda_beta' by auto
  3379     show ?thesis unfolding interval_split[OF **,unfolded Eucl_real_simps(1,3)] unfolding *** *
  3380     proof(cases "c = a \<or> c = b")
  3381       case False thus "f {a..b} = opp (f {a..c}) (f {c..b})"
  3382         apply-apply(subst as(2)[rule_format]) using True by auto
  3383     next case True thus "f {a..b} = opp (f {a..c}) (f {c..b})" apply-
  3384       proof(erule disjE) assume *:"c=a"
  3385         hence "f {a..c} = neutral opp" apply-apply(rule as(1)[rule_format]) by auto
  3386         thus ?thesis using assms unfolding * by auto
  3387       next assume *:"c=b" hence "f {c..b} = neutral opp" apply-apply(rule as(1)[rule_format]) by auto
  3388         thus ?thesis using assms unfolding * by auto qed qed qed qed
  3389 
  3390 lemma operative_1_le: assumes "monoidal opp"
  3391   shows "operative opp f \<longleftrightarrow> ((\<forall>a b. b \<le> a \<longrightarrow> f {a..b::real} = neutral opp) \<and>
  3392                 (\<forall>a b c. a \<le> c \<and> c \<le> b \<longrightarrow> opp (f{a..c})(f{c..b}) = f {a..b}))"
  3393 unfolding operative_1_lt[OF assms]
  3394 proof safe fix a b c::"real" assume as:"\<forall>a b c. a \<le> c \<and> c \<le> b \<longrightarrow> opp (f {a..c}) (f {c..b}) = f {a..b}" "a < c" "c < b"
  3395   show "opp (f {a..c}) (f {c..b}) = f {a..b}" apply(rule as(1)[rule_format]) using as(2-) by auto
  3396 next fix a b c ::"real" assume "\<forall>a b. b \<le> a \<longrightarrow> f {a..b} = neutral opp"
  3397     "\<forall>a b c. a < c \<and> c < b \<longrightarrow> opp (f {a..c}) (f {c..b}) = f {a..b}" "a \<le> c" "c \<le> b"
  3398   note as = this[rule_format]
  3399   show "opp (f {a..c}) (f {c..b}) = f {a..b}"
  3400   proof(cases "c = a \<or> c = b")
  3401     case False thus ?thesis apply-apply(subst as(2)) using as(3-) by(auto)
  3402     next case True thus ?thesis apply-
  3403       proof(erule disjE) assume *:"c=a" hence "f {a..c} = neutral opp" apply-apply(rule as(1)[rule_format]) by auto
  3404         thus ?thesis using assms unfolding * by auto
  3405       next               assume *:"c=b" hence "f {c..b} = neutral opp" apply-apply(rule as(1)[rule_format]) by auto
  3406         thus ?thesis using assms unfolding * by auto qed qed qed 
  3407 
  3408 subsection {* Special case of additivity we need for the FCT. *}
  3409 
  3410 lemma interval_bound_sing[simp]: "interval_upperbound {a} = a"  "interval_lowerbound {a} = a"
  3411   unfolding interval_upperbound_def interval_lowerbound_def  by auto
  3412 
  3413 lemma additive_tagged_division_1: fixes f::"real \<Rightarrow> 'a::real_normed_vector"
  3414   assumes "a \<le> b" "p tagged_division_of {a..b}"
  3415   shows "setsum (\<lambda>(x,k). f(interval_upperbound k) - f(interval_lowerbound k)) p = f b - f a"
  3416 proof- let ?f = "(\<lambda>k::(real) set. if k = {} then 0 else f(interval_upperbound k) - f(interval_lowerbound k))"
  3417   have ***:"\<forall>i<DIM(real). a $$ i \<le> b $$ i" using assms by auto 
  3418   have *:"operative op + ?f" unfolding operative_1_lt[OF monoidal_monoid] interval_eq_empty by auto
  3419   have **:"{a..b} \<noteq> {}" using assms(1) by auto note operative_tagged_division[OF monoidal_monoid * assms(2)]
  3420   note * = this[unfolded if_not_P[OF **] interval_bounds[OF ***],THEN sym]
  3421   show ?thesis unfolding * apply(subst setsum_iterate[THEN sym]) defer
  3422     apply(rule setsum_cong2) unfolding split_paired_all split_conv using assms(2) by auto qed
  3423 
  3424 subsection {* A useful lemma allowing us to factor out the content size. *}
  3425 
  3426 lemma has_integral_factor_content:
  3427   "(f has_integral i) {a..b} \<longleftrightarrow> (\<forall>e>0. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p
  3428     \<longrightarrow> norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - i) \<le> e * content {a..b}))"
  3429 proof(cases "content {a..b} = 0")
  3430   case True show ?thesis unfolding has_integral_null_eq[OF True] apply safe
  3431     apply(rule,rule,rule gauge_trivial,safe) unfolding setsum_content_null[OF True] True defer 
  3432     apply(erule_tac x=1 in allE,safe) defer apply(rule fine_division_exists[of _ a b],assumption)
  3433     apply(erule_tac x=p in allE) unfolding setsum_content_null[OF True] by auto
  3434 next case False note F = this[unfolded content_lt_nz[THEN sym]]
  3435   let ?P = "\<lambda>e opp. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow> opp (norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - i)) e)"
  3436   show ?thesis apply(subst has_integral)
  3437   proof safe fix e::real assume e:"e>0"
  3438     { assume "\<forall>e>0. ?P e op <" thus "?P (e * content {a..b}) op \<le>" apply(erule_tac x="e * content {a..b}" in allE)
  3439         apply(erule impE) defer apply(erule exE,rule_tac x=d in exI)
  3440         using F e by(auto simp add:field_simps intro:mult_pos_pos) }
  3441     {  assume "\<forall>e>0. ?P (e * content {a..b}) op \<le>" thus "?P e op <" apply(erule_tac x="e / 2 / content {a..b}" in allE)
  3442         apply(erule impE) defer apply(erule exE,rule_tac x=d in exI)
  3443         using F e by(auto simp add:field_simps intro:mult_pos_pos) } qed qed
  3444 
  3445 subsection {* Fundamental theorem of calculus. *}
  3446 
  3447 lemma interval_bounds_real: assumes "a\<le>(b::real)"
  3448   shows "interval_upperbound {a..b} = b" "interval_lowerbound {a..b} = a"
  3449   apply(rule_tac[!] interval_bounds) using assms by auto
  3450 
  3451 lemma fundamental_theorem_of_calculus: fixes f::"real \<Rightarrow> 'a::banach"
  3452   assumes "a \<le> b"  "\<forall>x\<in>{a..b}. (f has_vector_derivative f' x) (at x within {a..b})"
  3453   shows "(f' has_integral (f b - f a)) ({a..b})"
  3454 unfolding has_integral_factor_content
  3455 proof safe fix e::real assume e:"e>0"
  3456   note assm = assms(2)[unfolded has_vector_derivative_def has_derivative_within_alt]
  3457   have *:"\<And>P Q. \<forall>x\<in>{a..b}. P x \<and> (\<forall>e>0. \<exists>d>0. Q x e d) \<Longrightarrow> \<forall>x. \<exists>(d::real)>0. x\<in>{a..b} \<longrightarrow> Q x e d" using e by blast
  3458   note this[OF assm,unfolded gauge_existence_lemma] from choice[OF this,unfolded Ball_def[symmetric]]
  3459   guess d .. note d=conjunctD2[OF this[rule_format],rule_format]
  3460   show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow>
  3461                  norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f' x) - (f b - f a)) \<le> e * content {a..b})"
  3462     apply(rule_tac x="\<lambda>x. ball x (d x)" in exI,safe)
  3463     apply(rule gauge_ball_dependent,rule,rule d(1))
  3464   proof- fix p assume as:"p tagged_division_of {a..b}" "(\<lambda>x. ball x (d x)) fine p"
  3465     show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f' x) - (f b - f a)) \<le> e * content {a..b}" 
  3466       unfolding content_real[OF assms(1)] additive_tagged_division_1[OF assms(1) as(1),of f,THEN sym]
  3467       unfolding additive_tagged_division_1[OF assms(1) as(1),of "\<lambda>x. x",THEN sym]
  3468       unfolding setsum_right_distrib defer unfolding setsum_subtractf[THEN sym] 
  3469     proof(rule setsum_norm_le,safe) fix x k assume "(x,k)\<in>p"
  3470       note xk = tagged_division_ofD(2-4)[OF as(1) this] from this(3) guess u v apply-by(erule exE)+ note k=this
  3471       have *:"u \<le> v" using xk unfolding k by auto
  3472       have ball:"\<forall>xa\<in>k. xa \<in> ball x (d x)" using as(2)[unfolded fine_def,rule_format,OF `(x,k)\<in>p`,
  3473         unfolded split_conv subset_eq] .
  3474       have "norm ((v - u) *\<^sub>R f' x - (f v - f u)) \<le>
  3475         norm (f u - f x - (u - x) *\<^sub>R f' x) + norm (f v - f x - (v - x) *\<^sub>R f' x)"
  3476         apply(rule order_trans[OF _ norm_triangle_ineq4]) apply(rule eq_refl) apply(rule arg_cong[where f=norm])
  3477         unfolding scaleR_diff_left by(auto simp add:algebra_simps)
  3478       also have "... \<le> e * norm (u - x) + e * norm (v - x)"
  3479         apply(rule add_mono) apply(rule d(2)[of "x" "u",unfolded o_def]) prefer 4
  3480         apply(rule d(2)[of "x" "v",unfolded o_def])
  3481         using ball[rule_format,of u] ball[rule_format,of v] 
  3482         using xk(1-2) unfolding k subset_eq by(auto simp add:dist_real_def) 
  3483       also have "... \<le> e * (interval_upperbound k - interval_lowerbound k)"
  3484         unfolding k interval_bounds_real[OF *] using xk(1) unfolding k by(auto simp add:dist_real_def field_simps)
  3485       finally show "norm (content k *\<^sub>R f' x - (f (interval_upperbound k) - f (interval_lowerbound k))) \<le>
  3486         e * (interval_upperbound k - interval_lowerbound k)" unfolding k interval_bounds_real[OF *] content_real[OF *] .
  3487     qed qed qed
  3488 
  3489 subsection {* Attempt a systematic general set of "offset" results for components. *}
  3490 
  3491 lemma gauge_modify:
  3492   assumes "(\<forall>s. open s \<longrightarrow> open {x. f(x) \<in> s})" "gauge d"
  3493   shows "gauge (\<lambda>x. {y. f y \<in> d (f x)})"
  3494   using assms unfolding gauge_def apply safe defer apply(erule_tac x="f x" in allE)
  3495   apply(erule_tac x="d (f x)" in allE) by auto
  3496 
  3497 subsection {* Only need trivial subintervals if the interval itself is trivial. *}
  3498 
  3499 lemma division_of_nontrivial: fixes s::"('a::ordered_euclidean_space) set set"
  3500   assumes "s division_of {a..b}" "content({a..b}) \<noteq> 0"
  3501   shows "{k. k \<in> s \<and> content k \<noteq> 0} division_of {a..b}" using assms(1) apply-
  3502 proof(induct "card s" arbitrary:s rule:nat_less_induct)
  3503   fix s::"'a set set" assume assm:"s division_of {a..b}"
  3504     "\<forall>m<card s. \<forall>x. m = card x \<longrightarrow> x division_of {a..b} \<longrightarrow> {k \<in> x. content k \<noteq> 0} division_of {a..b}" 
  3505   note s = division_ofD[OF assm(1)] let ?thesis = "{k \<in> s. content k \<noteq> 0} division_of {a..b}"
  3506   { presume *:"{k \<in> s. content k \<noteq> 0} \<noteq> s \<Longrightarrow> ?thesis"
  3507     show ?thesis apply cases defer apply(rule *,assumption) using assm(1) by auto }
  3508   assume noteq:"{k \<in> s. content k \<noteq> 0} \<noteq> s"
  3509   then obtain k where k:"k\<in>s" "content k = 0" by auto
  3510   from s(4)[OF k(1)] guess c d apply-by(erule exE)+ note k=k this
  3511   from k have "card s > 0" unfolding card_gt_0_iff using assm(1) by auto
  3512   hence card:"card (s - {k}) < card s" using assm(1) k(1) apply(subst card_Diff_singleton_if) by auto
  3513   have *:"closed (\<Union>(s - {k}))" apply(rule closed_Union) defer apply rule apply(drule DiffD1,drule s(4))
  3514     apply safe apply(rule closed_interval) using assm(1) by auto
  3515   have "k \<subseteq> \<Union>(s - {k})" apply safe apply(rule *[unfolded closed_limpt,rule_format]) unfolding islimpt_approachable
  3516   proof safe fix x and e::real assume as:"x\<in>k" "e>0"
  3517     from k(2)[unfolded k content_eq_0] guess i .. 
  3518     hence i:"c$$i = d$$i" "i<DIM('a)" using s(3)[OF k(1),unfolded k] unfolding interval_ne_empty by auto
  3519     hence xi:"x$$i = d$$i" using as unfolding k mem_interval by smt
  3520     def y \<equiv> "(\<chi>\<chi> j. if j = i then if c$$i \<le> (a$$i + b$$i) / 2 then c$$i +
  3521       min e (b$$i - c$$i) / 2 else c$$i - min e (c$$i - a$$i) / 2 else x$$j)::'a"
  3522     show "\<exists>x'\<in>\<Union>(s - {k}). x' \<noteq> x \<and> dist x' x < e" apply(rule_tac x=y in bexI) 
  3523     proof have "d \<in> {c..d}" using s(3)[OF k(1)] unfolding k interval_eq_empty mem_interval by(fastforce simp add: not_less)
  3524       hence "d \<in> {a..b}" using s(2)[OF k(1)] unfolding k by auto note di = this[unfolded mem_interval,THEN spec[where x=i]]
  3525       hence xyi:"y$$i \<noteq> x$$i" unfolding y_def unfolding i xi euclidean_lambda_beta'[OF i(2)] if_P[OF refl]
  3526         apply(cases) apply(subst if_P,assumption) unfolding if_not_P not_le using as(2)
  3527         using assms(2)[unfolded content_eq_0] using i(2) using [[z3_with_extensions]] by smt+
  3528       thus "y \<noteq> x" unfolding euclidean_eq[where 'a='a] using i by auto
  3529       have *:"{..<DIM('a)} = insert i ({..<DIM('a)} - {i})" using i by auto
  3530       have "norm (y - x) < e + setsum (\<lambda>i. 0) {..<DIM('a)}" apply(rule le_less_trans[OF norm_le_l1])
  3531         apply(subst *,subst setsum_insert) prefer 3 apply(rule add_less_le_mono)
  3532       proof- show "\<bar>(y - x) $$ i\<bar> < e" unfolding y_def euclidean_simps euclidean_lambda_beta'[OF i(2)] if_P[OF refl]
  3533           apply(cases) apply(subst if_P,assumption) unfolding if_not_P unfolding i xi using di as(2) by auto
  3534         show "(\<Sum>i\<in>{..<DIM('a)} - {i}. \<bar>(y - x) $$ i\<bar>) \<le> (\<Sum>i\<in>{..<DIM('a)}. 0)" unfolding y_def by auto 
  3535       qed auto thus "dist y x < e" unfolding dist_norm by auto
  3536       have "y\<notin>k" unfolding k mem_interval apply rule apply(erule_tac x=i in allE) using xyi unfolding k i xi by auto
  3537       moreover have "y \<in> \<Union>s" unfolding s mem_interval
  3538       proof(rule,rule) note simps = y_def euclidean_lambda_beta' if_not_P
  3539         fix j assume j:"j<DIM('a)" show "a $$ j \<le> y $$ j \<and> y $$ j \<le> b $$ j" 
  3540         proof(cases "j = i") case False have "x \<in> {a..b}" using s(2)[OF k(1)] as(1) by auto
  3541           thus ?thesis using j apply- unfolding simps if_not_P[OF False] unfolding mem_interval by auto
  3542         next case True note T = this show ?thesis
  3543           proof(cases "c $$ i \<le> (a $$ i + b $$ i) / 2")
  3544             case True show ?thesis unfolding simps if_P[OF T] if_P[OF True] unfolding i
  3545               using True as(2) di apply-apply rule unfolding T by (auto simp add:field_simps) 
  3546           next case False thus ?thesis unfolding simps if_P[OF T] if_not_P[OF False] unfolding i
  3547               using True as(2) di apply-apply rule unfolding T by (auto simp add:field_simps)
  3548           qed qed qed
  3549       ultimately show "y \<in> \<Union>(s - {k})" by auto
  3550     qed qed hence "\<Union>(s - {k}) = {a..b}" unfolding s(6)[THEN sym] by auto
  3551   hence  "{ka \<in> s - {k}. content ka \<noteq> 0} division_of {a..b}" apply-apply(rule assm(2)[rule_format,OF card refl])
  3552     apply(rule division_ofI) defer apply(rule_tac[1-4] s) using assm(1) by auto
  3553   moreover have "{ka \<in> s - {k}. content ka \<noteq> 0} = {k \<in> s. content k \<noteq> 0}" using k by auto ultimately show ?thesis by auto qed
  3554 
  3555 subsection {* Integrability on subintervals. *}
  3556 
  3557 lemma operative_integrable: fixes f::"'b::ordered_euclidean_space \<Rightarrow> 'a::banach" shows
  3558   "operative op \<and> (\<lambda>i. f integrable_on i)"
  3559   unfolding operative_def neutral_and apply safe apply(subst integrable_on_def)
  3560   unfolding has_integral_null_eq apply(rule,rule refl) apply(rule,assumption,assumption)+
  3561   unfolding integrable_on_def by(auto intro!: has_integral_split)
  3562 
  3563 lemma integrable_subinterval: fixes f::"'b::ordered_euclidean_space \<Rightarrow> 'a::banach" 
  3564   assumes "f integrable_on {a..b}" "{c..d} \<subseteq> {a..b}" shows "f integrable_on {c..d}" 
  3565   apply(cases "{c..d} = {}") defer apply(rule partial_division_extend_1[OF assms(2)],assumption)
  3566   using operative_division_and[OF operative_integrable,THEN sym,of _ _ _ f] assms(1) by auto
  3567 
  3568 subsection {* Combining adjacent intervals in 1 dimension. *}
  3569 
  3570 lemma has_integral_combine: assumes "(a::real) \<le> c" "c \<le> b"
  3571   "(f has_integral i) {a..c}" "(f has_integral (j::'a::banach)) {c..b}"
  3572   shows "(f has_integral (i + j)) {a..b}"
  3573 proof- note operative_integral[of f, unfolded operative_1_le[OF monoidal_lifted[OF monoidal_monoid]]]
  3574   note conjunctD2[OF this,rule_format] note * = this(2)[OF conjI[OF assms(1-2)],unfolded if_P[OF assms(3)]]
  3575   hence "f integrable_on {a..b}" apply- apply(rule ccontr) apply(subst(asm) if_P) defer
  3576     apply(subst(asm) if_P) using assms(3-) by auto
  3577   with * show ?thesis apply-apply(subst(asm) if_P) defer apply(subst(asm) if_P) defer apply(subst(asm) if_P)
  3578     unfolding lifted.simps using assms(3-) by(auto simp add: integrable_on_def integral_unique) qed
  3579 
  3580 lemma integral_combine: fixes f::"real \<Rightarrow> 'a::banach"
  3581   assumes "a \<le> c" "c \<le> b" "f integrable_on ({a..b})"
  3582   shows "integral {a..c} f + integral {c..b} f = integral({a..b}) f"
  3583   apply(rule integral_unique[THEN sym]) apply(rule has_integral_combine[OF assms(1-2)])
  3584   apply(rule_tac[!] integrable_integral integrable_subinterval[OF assms(3)])+ using assms(1-2) by auto
  3585 
  3586 lemma integrable_combine: fixes f::"real \<Rightarrow> 'a::banach"
  3587   assumes "a \<le> c" "c \<le> b" "f integrable_on {a..c}" "f integrable_on {c..b}"
  3588   shows "f integrable_on {a..b}" using assms unfolding integrable_on_def by(fastforce intro!:has_integral_combine)
  3589 
  3590 subsection {* Reduce integrability to "local" integrability. *}
  3591 
  3592 lemma integrable_on_little_subintervals: fixes f::"'b::ordered_euclidean_space \<Rightarrow> 'a::banach"
  3593   assumes "\<forall>x\<in>{a..b}. \<exists>d>0. \<forall>u v. x \<in> {u..v} \<and> {u..v} \<subseteq> ball x d \<and> {u..v} \<subseteq> {a..b} \<longrightarrow> f integrable_on {u..v}"
  3594   shows "f integrable_on {a..b}"
  3595 proof- have "\<forall>x. \<exists>d. x\<in>{a..b} \<longrightarrow> d>0 \<and> (\<forall>u v. x \<in> {u..v} \<and> {u..v} \<subseteq> ball x d \<and> {u..v} \<subseteq> {a..b} \<longrightarrow> f integrable_on {u..v})"
  3596     using assms by auto note this[unfolded gauge_existence_lemma] from choice[OF this] guess d .. note d=this[rule_format]
  3597   guess p apply(rule fine_division_exists[OF gauge_ball_dependent,of d a b]) using d by auto note p=this(1-2)
  3598   note division_of_tagged_division[OF this(1)] note * = operative_division_and[OF operative_integrable,OF this,THEN sym,of f]
  3599   show ?thesis unfolding * apply safe unfolding snd_conv
  3600   proof- fix x k assume "(x,k) \<in> p" note tagged_division_ofD(2-4)[OF p(1) this] fineD[OF p(2) this]
  3601     thus "f integrable_on k" apply safe apply(rule d[THEN conjunct2,rule_format,of x]) by auto qed qed
  3602 
  3603 subsection {* Second FCT or existence of antiderivative. *}
  3604 
  3605 lemma integrable_const[intro]:"(\<lambda>x. c) integrable_on {a..b}"
  3606   unfolding integrable_on_def by(rule,rule has_integral_const)
  3607 
  3608 lemma integral_has_vector_derivative: fixes f::"real \<Rightarrow> 'a::banach"
  3609   assumes "continuous_on {a..b} f" "x \<in> {a..b}"
  3610   shows "((\<lambda>u. integral {a..u} f) has_vector_derivative f(x)) (at x within {a..b})"
  3611   unfolding has_vector_derivative_def has_derivative_within_alt
  3612 apply safe apply(rule bounded_linear_scaleR_left)
  3613 proof- fix e::real assume e:"e>0"
  3614   note compact_uniformly_continuous[OF assms(1) compact_interval,unfolded uniformly_continuous_on_def]
  3615   from this[rule_format,OF e] guess d apply-by(erule conjE exE)+ note d=this[rule_format]
  3616   let ?I = "\<lambda>a b. integral {a..b} f"
  3617   show "\<exists>d>0. \<forall>y\<in>{a..b}. norm (y - x) < d \<longrightarrow> norm (?I a y - ?I a x - (y - x) *\<^sub>R f x) \<le> e * norm (y - x)"
  3618   proof(rule,rule,rule d,safe) case goal1 show ?case proof(cases "y < x")
  3619       case False have "f integrable_on {a..y}" apply(rule integrable_subinterval,rule integrable_continuous)
  3620         apply(rule assms)  unfolding not_less using assms(2) goal1 by auto
  3621       hence *:"?I a y - ?I a x = ?I x y" unfolding algebra_simps apply(subst eq_commute) apply(rule integral_combine)
  3622         using False unfolding not_less using assms(2) goal1 by auto
  3623       have **:"norm (y - x) = content {x..y}" apply(subst content_real) using False unfolding not_less by auto
  3624       show ?thesis unfolding ** apply(rule has_integral_bound[where f="(\<lambda>u. f u - f x)"]) unfolding * unfolding o_def
  3625         defer apply(rule has_integral_sub) apply(rule integrable_integral)
  3626         apply(rule integrable_subinterval,rule integrable_continuous) apply(rule assms)+
  3627       proof- show "{x..y} \<subseteq> {a..b}" using goal1 assms(2) by auto
  3628         have *:"y - x = norm(y - x)" using False by auto
  3629         show "((\<lambda>xa. f x) has_integral (y - x) *\<^sub>R f x) {x.. y}" apply(subst *) unfolding ** by auto
  3630         show "\<forall>xa\<in>{x..y}. norm (f xa - f x) \<le> e" apply safe apply(rule less_imp_le)
  3631           apply(rule d(2)[unfolded dist_norm]) using assms(2) using goal1 by auto
  3632       qed(insert e,auto)
  3633     next case True have "f integrable_on {a..x}" apply(rule integrable_subinterval,rule integrable_continuous)
  3634         apply(rule assms)+  unfolding not_less using assms(2) goal1 by auto
  3635       hence *:"?I a x - ?I a y = ?I y x" unfolding algebra_simps apply(subst eq_commute) apply(rule integral_combine)
  3636         using True using assms(2) goal1 by auto
  3637       have **:"norm (y - x) = content {y..x}" apply(subst content_real) using True unfolding not_less by auto
  3638       have ***:"\<And>fy fx c::'a. fx - fy - (y - x) *\<^sub>R c = -(fy - fx - (x - y) *\<^sub>R c)" unfolding scaleR_left.diff by auto 
  3639       show ?thesis apply(subst ***) unfolding norm_minus_cancel **
  3640         apply(rule has_integral_bound[where f="(\<lambda>u. f u - f x)"]) unfolding * unfolding o_def
  3641         defer apply(rule has_integral_sub) apply(subst minus_minus[THEN sym]) unfolding minus_minus
  3642         apply(rule integrable_integral) apply(rule integrable_subinterval,rule integrable_continuous) apply(rule assms)+
  3643       proof- show "{y..x} \<subseteq> {a..b}" using goal1 assms(2) by auto
  3644         have *:"x - y = norm(y - x)" using True by auto
  3645         show "((\<lambda>xa. f x) has_integral (x - y) *\<^sub>R f x) {y..x}" apply(subst *) unfolding ** by auto
  3646         show "\<forall>xa\<in>{y..x}. norm (f xa - f x) \<le> e" apply safe apply(rule less_imp_le)
  3647           apply(rule d(2)[unfolded dist_norm]) using assms(2) using goal1 by auto
  3648       qed(insert e,auto) qed qed qed
  3649 
  3650 lemma antiderivative_continuous: assumes "continuous_on {a..b::real} f"
  3651   obtains g where "\<forall>x\<in> {a..b}. (g has_vector_derivative (f(x)::_::banach)) (at x within {a..b})"
  3652   apply(rule that,rule) using integral_has_vector_derivative[OF assms] by auto
  3653 
  3654 subsection {* Combined fundamental theorem of calculus. *}
  3655 
  3656 lemma antiderivative_integral_continuous: fixes f::"real \<Rightarrow> 'a::banach" assumes "continuous_on {a..b} f"
  3657   obtains g where "\<forall>u\<in>{a..b}. \<forall>v \<in> {a..b}. u \<le> v \<longrightarrow> (f has_integral (g v - g u)) {u..v}"
  3658 proof- from antiderivative_continuous[OF assms] guess g . note g=this
  3659   show ?thesis apply(rule that[of g])
  3660   proof safe case goal1 have "\<forall>x\<in>{u..v}. (g has_vector_derivative f x) (at x within {u..v})"
  3661       apply(rule,rule has_vector_derivative_within_subset) apply(rule g[rule_format]) using goal1(1-2) by auto
  3662     thus ?case using fundamental_theorem_of_calculus[OF goal1(3),of "g" "f"] by auto qed qed
  3663 
  3664 subsection {* General "twiddling" for interval-to-interval function image. *}
  3665 
  3666 lemma has_integral_twiddle:
  3667   assumes "0 < r" "\<forall>x. h(g x) = x" "\<forall>x. g(h x) = x" "\<forall>x. continuous (at x) g"
  3668   "\<forall>u v. \<exists>w z. g ` {u..v} = {w..z}"
  3669   "\<forall>u v. \<exists>w z. h ` {u..v} = {w..z}"
  3670   "\<forall>u v. content(g ` {u..v}) = r * content {u..v}"
  3671   "(f has_integral i) {a..b}"
  3672   shows "((\<lambda>x. f(g x)) has_integral (1 / r) *\<^sub>R i) (h ` {a..b})"
  3673 proof- { presume *:"{a..b} \<noteq> {} \<Longrightarrow> ?thesis"
  3674     show ?thesis apply cases defer apply(rule *,assumption)
  3675     proof- case goal1 thus ?thesis unfolding goal1 assms(8)[unfolded goal1 has_integral_empty_eq] by auto qed }
  3676   assume "{a..b} \<noteq> {}" from assms(6)[rule_format,of a b] guess w z apply-by(erule exE)+ note wz=this
  3677   have inj:"inj g" "inj h" unfolding inj_on_def apply safe apply(rule_tac[!] ccontr)
  3678     using assms(2) apply(erule_tac x=x in allE) using assms(2) apply(erule_tac x=y in allE) defer
  3679     using assms(3) apply(erule_tac x=x in allE) using assms(3) apply(erule_tac x=y in allE) by auto
  3680   show ?thesis unfolding has_integral_def has_integral_compact_interval_def apply(subst if_P) apply(rule,rule,rule wz)
  3681   proof safe fix e::real assume e:"e>0" hence "e * r > 0" using assms(1) by(rule mult_pos_pos)
  3682     from assms(8)[unfolded has_integral,rule_format,OF this] guess d apply-by(erule exE conjE)+ note d=this[rule_format]
  3683     def d' \<equiv> "\<lambda>x. {y. g y \<in> d (g x)}" have d':"\<And>x. d' x = {y. g y \<in> (d (g x))}" unfolding d'_def ..
  3684     show "\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of h ` {a..b} \<and> d fine p \<longrightarrow> norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f (g x)) - (1 / r) *\<^sub>R i) < e)"
  3685     proof(rule_tac x=d' in exI,safe) show "gauge d'" using d(1) unfolding gauge_def d' using continuous_open_preimage_univ[OF assms(4)] by auto
  3686       fix p assume as:"p tagged_division_of h ` {a..b}" "d' fine p" note p = tagged_division_ofD[OF as(1)] 
  3687       have "(\<lambda>(x, k). (g x, g ` k)) ` p tagged_division_of {a..b} \<and> d fine (\<lambda>(x, k). (g x, g ` k)) ` p" unfolding tagged_division_of 
  3688       proof safe show "finite ((\<lambda>(x, k). (g x, g ` k)) ` p)" using as by auto
  3689         show "d fine (\<lambda>(x, k). (g x, g ` k)) ` p" using as(2) unfolding fine_def d' by auto
  3690         fix x k assume xk[intro]:"(x,k) \<in> p" show "g x \<in> g ` k" using p(2)[OF xk] by auto
  3691         show "\<exists>u v. g ` k = {u..v}" using p(4)[OF xk] using assms(5-6) by auto
  3692         { fix y assume "y \<in> k" thus "g y \<in> {a..b}" "g y \<in> {a..b}" using p(3)[OF xk,unfolded subset_eq,rule_format,of "h (g y)"]
  3693             using assms(2)[rule_format,of y] unfolding inj_image_mem_iff[OF inj(2)] by auto }
  3694         fix x' k' assume xk':"(x',k') \<in> p" fix z assume "z \<in> interior (g ` k)" "z \<in> interior (g ` k')"
  3695         hence *:"interior (g ` k) \<inter> interior (g ` k') \<noteq> {}" by auto
  3696         have same:"(x, k) = (x', k')" apply-apply(rule ccontr,drule p(5)[OF xk xk'])
  3697         proof- assume as:"interior k \<inter> interior k' = {}" from nonempty_witness[OF *] guess z .
  3698           hence "z \<in> g ` (interior k \<inter> interior k')" using interior_image_subset[OF assms(4) inj(1)]
  3699             unfolding image_Int[OF inj(1)] by auto thus False using as by blast
  3700         qed thus "g x = g x'" by auto
  3701         { fix z assume "z \<in> k"  thus  "g z \<in> g ` k'" using same by auto }
  3702         { fix z assume "z \<in> k'" thus  "g z \<in> g ` k"  using same by auto }
  3703       next fix x assume "x \<in> {a..b}" hence "h x \<in>  \<Union>{k. \<exists>x. (x, k) \<in> p}" using p(6) by auto
  3704         then guess X unfolding Union_iff .. note X=this from this(1) guess y unfolding mem_Collect_eq ..
  3705         thus "x \<in> \<Union>{k. \<exists>x. (x, k) \<in> (\<lambda>(x, k). (g x, g ` k)) ` p}" apply-
  3706           apply(rule_tac X="g ` X" in UnionI) defer apply(rule_tac x="h x" in image_eqI)
  3707           using X(2) assms(3)[rule_format,of x] by auto
  3708       qed note ** = d(2)[OF this] have *:"inj_on (\<lambda>(x, k). (g x, g ` k)) p" using inj(1) unfolding inj_on_def by fastforce
  3709        have "(\<Sum>(x, k)\<in>(\<lambda>(x, k). (g x, g ` k)) ` p. content k *\<^sub>R f x) - i = r *\<^sub>R (\<Sum>(x, k)\<in>p. content k *\<^sub>R f (g x)) - i" (is "?l = _") unfolding algebra_simps add_left_cancel
  3710         unfolding setsum_reindex[OF *] apply(subst scaleR_right.setsum) defer apply(rule setsum_cong2) unfolding o_def split_paired_all split_conv
  3711         apply(drule p(4)) apply safe unfolding assms(7)[rule_format] using p by auto
  3712       also have "... = r *\<^sub>R ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f (g x)) - (1 / r) *\<^sub>R i)" (is "_ = ?r") unfolding scaleR_diff_right scaleR_scaleR
  3713         using assms(1) by auto finally have *:"?l = ?r" .
  3714       show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f (g x)) - (1 / r) *\<^sub>R i) < e" using ** unfolding * unfolding norm_scaleR
  3715         using assms(1) by(auto simp add:field_simps) qed qed qed
  3716 
  3717 subsection {* Special case of a basic affine transformation. *}
  3718 
  3719 lemma interval_image_affinity_interval: shows "\<exists>u v. (\<lambda>x. m *\<^sub>R (x::'a::ordered_euclidean_space) + c) ` {a..b} = {u..v}"
  3720   unfolding image_affinity_interval by auto
  3721 
  3722 lemma setprod_cong2: assumes "\<And>x. x \<in> A \<Longrightarrow> f x = g x" shows "setprod f A = setprod g A"
  3723   apply(rule setprod_cong) using assms by auto
  3724 
  3725 lemma content_image_affinity_interval: 
  3726  "content((\<lambda>x::'a::ordered_euclidean_space. m *\<^sub>R x + c) ` {a..b}) = (abs m) ^ DIM('a) * content {a..b}" (is "?l = ?r")
  3727 proof- { presume *:"{a..b}\<noteq>{} \<Longrightarrow> ?thesis" show ?thesis apply(cases,rule *,assumption)
  3728       unfolding not_not using content_empty by auto }
  3729   have *:"DIM('a) = card {..<DIM('a)}" by auto
  3730   assume as:"{a..b}\<noteq>{}" show ?thesis proof(cases "m \<ge> 0")
  3731     case True show ?thesis unfolding image_affinity_interval if_not_P[OF as] if_P[OF True]
  3732       unfolding content_closed_interval'[OF as] apply(subst content_closed_interval') defer apply(subst(2) *)
  3733       apply(subst setprod_constant[THEN sym]) apply(rule finite_lessThan) unfolding setprod_timesf[THEN sym]
  3734       apply(rule setprod_cong2) using True as unfolding interval_ne_empty euclidean_simps not_le  
  3735       by(auto simp add:field_simps intro:mult_left_mono)
  3736   next case False show ?thesis unfolding image_affinity_interval if_not_P[OF as] if_not_P[OF False]
  3737       unfolding content_closed_interval'[OF as] apply(subst content_closed_interval') defer apply(subst(2) *)
  3738       apply(subst setprod_constant[THEN sym]) apply(rule finite_lessThan) unfolding setprod_timesf[THEN sym]
  3739       apply(rule setprod_cong2) using False as unfolding interval_ne_empty euclidean_simps not_le 
  3740       by(auto simp add:field_simps mult_le_cancel_left_neg) qed qed
  3741 
  3742 lemma has_integral_affinity: fixes a::"'a::ordered_euclidean_space" assumes "(f has_integral i) {a..b}" "m \<noteq> 0"
  3743   shows "((\<lambda>x. f(m *\<^sub>R x + c)) has_integral ((1 / (abs(m) ^ DIM('a))) *\<^sub>R i)) ((\<lambda>x. (1 / m) *\<^sub>R x + -((1 / m) *\<^sub>R c)) ` {a..b})"
  3744   apply(rule has_integral_twiddle,safe) apply(rule zero_less_power) unfolding euclidean_eq[where 'a='a]
  3745   unfolding scaleR_right_distrib euclidean_simps scaleR_scaleR
  3746   defer apply(insert assms(2), simp add:field_simps) apply(insert assms(2), simp add:field_simps)
  3747   apply(rule continuous_intros)+ apply(rule interval_image_affinity_interval)+ apply(rule content_image_affinity_interval) using assms by auto
  3748 
  3749 lemma integrable_affinity: assumes "f integrable_on {a..b}" "m \<noteq> 0"
  3750   shows "(\<lambda>x. f(m *\<^sub>R x + c)) integrable_on ((\<lambda>x. (1 / m) *\<^sub>R x + -((1/m) *\<^sub>R c)) ` {a..b})"
  3751   using assms unfolding integrable_on_def apply safe apply(drule has_integral_affinity) by auto
  3752 
  3753 subsection {* Special case of stretching coordinate axes separately. *}
  3754 
  3755 lemma image_stretch_interval:
  3756   "(\<lambda>x. \<chi>\<chi> k. m k * x$$k) ` {a..b::'a::ordered_euclidean_space} =
  3757   (if {a..b} = {} then {} else {(\<chi>\<chi> k. min (m(k) * a$$k) (m(k) * b$$k))::'a ..  (\<chi>\<chi> k. max (m(k) * a$$k) (m(k) * b$$k))})"
  3758   (is "?l = ?r")
  3759 proof(cases "{a..b}={}") case True thus ?thesis unfolding True by auto
  3760 next have *:"\<And>P Q. (\<forall>i<DIM('a). P i) \<and> (\<forall>i<DIM('a). Q i) \<longleftrightarrow> (\<forall>i<DIM('a). P i \<and> Q i)" by auto
  3761   case False note ab = this[unfolded interval_ne_empty]
  3762   show ?thesis apply-apply(rule set_eqI)
  3763   proof- fix x::"'a" have **:"\<And>P Q. (\<forall>i<DIM('a). P i = Q i) \<Longrightarrow> (\<forall>i<DIM('a). P i) = (\<forall>i<DIM('a). Q i)" by auto
  3764     show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" unfolding if_not_P[OF False] 
  3765       unfolding image_iff mem_interval Bex_def euclidean_simps euclidean_eq[where 'a='a] *
  3766       unfolding imp_conjR[THEN sym] apply(subst euclidean_lambda_beta'') apply(subst lambda_skolem'[THEN sym])
  3767       apply(rule **,rule,rule) unfolding euclidean_lambda_beta'
  3768     proof- fix i assume i:"i<DIM('a)" show "(\<exists>xa. (a $$ i \<le> xa \<and> xa \<le> b $$ i) \<and> x $$ i = m i * xa) =
  3769         (min (m i * a $$ i) (m i * b $$ i) \<le> x $$ i \<and> x $$ i \<le> max (m i * a $$ i) (m i * b $$ i))"
  3770       proof(cases "m i = 0") case True thus ?thesis using ab i by auto
  3771       next case False hence "0 < m i \<or> 0 > m i" by auto thus ?thesis apply-
  3772         proof(erule disjE) assume as:"0 < m i" hence *:"min (m i * a $$ i) (m i * b $$ i) = m i * a $$ i"
  3773             "max (m i * a $$ i) (m i * b $$ i) = m i * b $$ i" using ab i unfolding min_def max_def by auto
  3774           show ?thesis unfolding * apply rule defer apply(rule_tac x="1 / m i * x$$i" in exI)
  3775             using as by(auto simp add:field_simps)
  3776         next assume as:"0 > m i" hence *:"max (m i * a $$ i) (m i * b $$ i) = m i * a $$ i"
  3777             "min (m i * a $$ i) (m i * b $$ i) = m i * b $$ i" using ab as i unfolding min_def max_def 
  3778             by(auto simp add:field_simps mult_le_cancel_left_neg intro: order_antisym)
  3779           show ?thesis unfolding * apply rule defer apply(rule_tac x="1 / m i * x$$i" in exI)
  3780             using as by(auto simp add:field_simps) qed qed qed qed qed 
  3781 
  3782 lemma interval_image_stretch_interval: "\<exists>u v. (\<lambda>x. \<chi>\<chi> k. m k * x$$k) ` {a..b::'a::ordered_euclidean_space} = {u..v::'a}"
  3783   unfolding image_stretch_interval by auto 
  3784 
  3785 lemma content_image_stretch_interval:
  3786   "content((\<lambda>x::'a::ordered_euclidean_space. (\<chi>\<chi> k. m k * x$$k)::'a) ` {a..b}) = abs(setprod m {..<DIM('a)}) * content({a..b})"
  3787 proof(cases "{a..b} = {}") case True thus ?thesis
  3788     unfolding content_def image_is_empty image_stretch_interval if_P[OF True] by auto
  3789 next case False hence "(\<lambda>x. (\<chi>\<chi> k. m k * x $$ k)::'a) ` {a..b} \<noteq> {}" by auto
  3790   thus ?thesis using False unfolding content_def image_stretch_interval apply- unfolding interval_bounds' if_not_P
  3791     unfolding abs_setprod setprod_timesf[THEN sym] apply(rule setprod_cong2) unfolding lessThan_iff euclidean_lambda_beta'
  3792   proof- fix i assume i:"i<DIM('a)" have "(m i < 0 \<or> m i > 0) \<or> m i = 0" by auto
  3793     thus "max (m i * a $$ i) (m i * b $$ i) - min (m i * a $$ i) (m i * b $$ i) = \<bar>m i\<bar> * (b $$ i - a $$ i)"
  3794       apply-apply(erule disjE)+ unfolding min_def max_def using False[unfolded interval_ne_empty,rule_format,of i] i 
  3795       by(auto simp add:field_simps not_le mult_le_cancel_left_neg mult_le_cancel_left_pos) qed qed
  3796 
  3797 lemma has_integral_stretch: fixes f::"'a::ordered_euclidean_space => 'b::real_normed_vector"
  3798   assumes "(f has_integral i) {a..b}" "\<forall>k<DIM('a). ~(m k = 0)"
  3799   shows "((\<lambda>x. f(\<chi>\<chi> k. m k * x$$k)) has_integral
  3800              ((1/(abs(setprod m {..<DIM('a)}))) *\<^sub>R i)) ((\<lambda>x. (\<chi>\<chi> k. 1/(m k) * x$$k)::'a) ` {a..b})"
  3801   apply(rule has_integral_twiddle[where f=f]) unfolding zero_less_abs_iff content_image_stretch_interval
  3802   unfolding image_stretch_interval empty_as_interval euclidean_eq[where 'a='a] using assms
  3803 proof- show "\<forall>y::'a. continuous (at y) (\<lambda>x. (\<chi>\<chi> k. m k * x $$ k)::'a)"
  3804    apply(rule,rule linear_continuous_at) unfolding linear_linear
  3805    unfolding linear_def euclidean_simps euclidean_eq[where 'a='a] by(auto simp add:field_simps) qed auto
  3806 
  3807 lemma integrable_stretch:  fixes f::"'a::ordered_euclidean_space => 'b::real_normed_vector"
  3808   assumes "f integrable_on {a..b}" "\<forall>k<DIM('a). ~(m k = 0)"
  3809   shows "(\<lambda>x::'a. f(\<chi>\<chi> k. m k * x$$k)) integrable_on ((\<lambda>x. \<chi>\<chi> k. 1/(m k) * x$$k) ` {a..b})"
  3810   using assms unfolding integrable_on_def apply-apply(erule exE) 
  3811   apply(drule has_integral_stretch,assumption) by auto
  3812 
  3813 subsection {* even more special cases. *}
  3814 
  3815 lemma uminus_interval_vector[simp]:"uminus ` {a..b} = {-b .. -a::'a::ordered_euclidean_space}"
  3816   apply(rule set_eqI,rule) defer unfolding image_iff
  3817   apply(rule_tac x="-x" in bexI) by(auto simp add:minus_le_iff le_minus_iff eucl_le[where 'a='a])
  3818 
  3819 lemma has_integral_reflect_lemma[intro]: assumes "(f has_integral i) {a..b}"
  3820   shows "((\<lambda>x. f(-x)) has_integral i) {-b .. -a}"
  3821   using has_integral_affinity[OF assms, of "-1" 0] by auto
  3822 
  3823 lemma has_integral_reflect[simp]: "((\<lambda>x. f(-x)) has_integral i) {-b..-a} \<longleftrightarrow> (f has_integral i) ({a..b})"
  3824   apply rule apply(drule_tac[!] has_integral_reflect_lemma) by auto
  3825 
  3826 lemma integrable_reflect[simp]: "(\<lambda>x. f(-x)) integrable_on {-b..-a} \<longleftrightarrow> f integrable_on {a..b}"
  3827   unfolding integrable_on_def by auto
  3828 
  3829 lemma integral_reflect[simp]: "integral {-b..-a} (\<lambda>x. f(-x)) = integral ({a..b}) f"
  3830   unfolding integral_def by auto
  3831 
  3832 subsection {* Stronger form of FCT; quite a tedious proof. *}
  3833 
  3834 lemma bgauge_existence_lemma: "(\<forall>x\<in>s. \<exists>d::real. 0 < d \<and> q d x) \<longleftrightarrow> (\<forall>x. \<exists>d>0. x\<in>s \<longrightarrow> q d x)" by(meson zero_less_one)
  3835 
  3836 lemma additive_tagged_division_1': fixes f::"real \<Rightarrow> 'a::real_normed_vector"
  3837   assumes "a \<le> b" "p tagged_division_of {a..b}"
  3838   shows "setsum (\<lambda>(x,k). f (interval_upperbound k) - f(interval_lowerbound k)) p = f b - f a"
  3839   using additive_tagged_division_1[OF _ assms(2), of f] using assms(1) by auto
  3840 
  3841 lemma split_minus[simp]:"(\<lambda>(x, k). f x k) x - (\<lambda>(x, k). g x k) x = (\<lambda>(x, k). f x k - g x k) x"
  3842   unfolding split_def by(rule refl)
  3843 
  3844 lemma norm_triangle_le_sub: "norm x + norm y \<le> e \<Longrightarrow> norm (x - y) \<le> e"
  3845   apply(subst(asm)(2) norm_minus_cancel[THEN sym])
  3846   apply(drule norm_triangle_le) by(auto simp add:algebra_simps)
  3847 
  3848 lemma fundamental_theorem_of_calculus_interior: fixes f::"real => 'a::real_normed_vector"
  3849   assumes"a \<le> b" "continuous_on {a..b} f" "\<forall>x\<in>{a<..<b}. (f has_vector_derivative f'(x)) (at x)"
  3850   shows "(f' has_integral (f b - f a)) {a..b}"
  3851 proof- { presume *:"a < b \<Longrightarrow> ?thesis" 
  3852     show ?thesis proof(cases,rule *,assumption)
  3853       assume "\<not> a < b" hence "a = b" using assms(1) by auto
  3854       hence *:"{a .. b} = {b}" "f b - f a = 0" by(auto simp add:  order_antisym)
  3855       show ?thesis unfolding *(2) apply(rule has_integral_null) unfolding content_eq_0 using * `a=b` by auto
  3856     qed } assume ab:"a < b"
  3857   let ?P = "\<lambda>e. \<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow>
  3858                    norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f' x) - (f b - f a)) \<le> e * content {a..b})"
  3859   { presume "\<And>e. e>0 \<Longrightarrow> ?P e" thus ?thesis unfolding has_integral_factor_content by auto }
  3860   fix e::real assume e:"e>0"
  3861   note assms(3)[unfolded has_vector_derivative_def has_derivative_at_alt ball_conj_distrib]
  3862   note conjunctD2[OF this] note bounded=this(1) and this(2)
  3863   from this(2) have "\<forall>x\<in>{a<..<b}. \<exists>d>0. \<forall>y. norm (y - x) < d \<longrightarrow> norm (f y - f x - (y - x) *\<^sub>R f' x) \<le> e/2 * norm (y - x)"
  3864     apply-apply safe apply(erule_tac x=x in ballE,erule_tac x="e/2" in allE) using e by auto note this[unfolded bgauge_existence_lemma]
  3865   from choice[OF this] guess d .. note conjunctD2[OF this[rule_format]] note d = this[rule_format]
  3866   have "bounded (f ` {a..b})" apply(rule compact_imp_bounded compact_continuous_image)+ using compact_interval assms by auto
  3867   from this[unfolded bounded_pos] guess B .. note B = this[rule_format]
  3868 
  3869   have "\<exists>da. 0 < da \<and> (\<forall>c. a \<le> c \<and> {a..c} \<subseteq> {a..b} \<and> {a..c} \<subseteq> ball a da
  3870     \<longrightarrow> norm(content {a..c} *\<^sub>R f' a - (f c - f a)) \<le> (e * (b - a)) / 4)"
  3871   proof- have "a\<in>{a..b}" using ab by auto
  3872     note assms(2)[unfolded continuous_on_eq_continuous_within,rule_format,OF this]
  3873     note * = this[unfolded continuous_within Lim_within,rule_format] have "(e * (b - a)) / 8 > 0" using e ab by(auto simp add:field_simps)
  3874     from *[OF this] guess k .. note k = conjunctD2[OF this,rule_format]
  3875     have "\<exists>l. 0 < l \<and> norm(l *\<^sub>R f' a) \<le> (e * (b - a)) / 8"
  3876     proof(cases "f' a = 0") case True
  3877       thus ?thesis apply(rule_tac x=1 in exI) using ab e by(auto intro!:mult_nonneg_nonneg) 
  3878     next case False thus ?thesis
  3879         apply(rule_tac x="(e * (b - a)) / 8 / norm (f' a)" in exI) using ab e by(auto simp add:field_simps) 
  3880     qed then guess l .. note l = conjunctD2[OF this]
  3881     show ?thesis apply(rule_tac x="min k l" in exI) apply safe unfolding min_less_iff_conj apply(rule,(rule l k)+)
  3882     proof- fix c assume as:"a \<le> c" "{a..c} \<subseteq> {a..b}" "{a..c} \<subseteq> ball a (min k l)" 
  3883       note as' = this[unfolded subset_eq Ball_def mem_ball dist_real_def mem_interval]
  3884       have "norm ((c - a) *\<^sub>R f' a - (f c - f a)) \<le> norm ((c - a) *\<^sub>R f' a) + norm (f c - f a)" by(rule norm_triangle_ineq4)
  3885       also have "... \<le> e * (b - a) / 8 + e * (b - a) / 8" 
  3886       proof(rule add_mono) case goal1 have "\<bar>c - a\<bar> \<le> \<bar>l\<bar>" using as' by auto
  3887         thus ?case apply-apply(rule order_trans[OF _ l(2)]) unfolding norm_scaleR apply(rule mult_right_mono) by auto
  3888       next case goal2 show ?case apply(rule less_imp_le) apply(cases "a = c") defer
  3889           apply(rule k(2)[unfolded dist_norm]) using as' e ab by(auto simp add:field_simps)
  3890       qed finally show "norm (content {a..c} *\<^sub>R f' a - (f c - f a)) \<le> e * (b - a) / 4"
  3891         unfolding content_real[OF as(1)] by auto
  3892     qed qed then guess da .. note da=conjunctD2[OF this,rule_format]
  3893 
  3894   have "\<exists>db>0. \<forall>c\<le>b. {c..b} \<subseteq> {a..b} \<and> {c..b} \<subseteq> ball b db \<longrightarrow>
  3895     norm(content {c..b} *\<^sub>R f' b - (f b - f c)) \<le> (e * (b - a)) / 4"
  3896   proof- have "b\<in>{a..b}" using ab by auto
  3897     note assms(2)[unfolded continuous_on_eq_continuous_within,rule_format,OF this]
  3898     note * = this[unfolded continuous_within Lim_within,rule_format] have "(e * (b - a)) / 8 > 0"
  3899       using e ab by(auto simp add:field_simps)
  3900     from *[OF this] guess k .. note k = conjunctD2[OF this,rule_format]
  3901     have "\<exists>l. 0 < l \<and> norm(l *\<^sub>R f' b) \<le> (e * (b - a)) / 8"
  3902     proof(cases "f' b = 0") case True
  3903       thus ?thesis apply(rule_tac x=1 in exI) using ab e by(auto intro!:mult_nonneg_nonneg) 
  3904     next case False thus ?thesis 
  3905         apply(rule_tac x="(e * (b - a)) / 8 / norm (f' b)" in exI)
  3906         using ab e by(auto simp add:field_simps)
  3907     qed then guess l .. note l = conjunctD2[OF this]
  3908     show ?thesis apply(rule_tac x="min k l" in exI) apply safe unfolding min_less_iff_conj apply(rule,(rule l k)+)
  3909     proof- fix c assume as:"c \<le> b" "{c..b} \<subseteq> {a..b}" "{c..b} \<subseteq> ball b (min k l)" 
  3910       note as' = this[unfolded subset_eq Ball_def mem_ball dist_real_def mem_interval]
  3911       have "norm ((b - c) *\<^sub>R f' b - (f b - f c)) \<le> norm ((b - c) *\<^sub>R f' b) + norm (f b - f c)" by(rule norm_triangle_ineq4)
  3912       also have "... \<le> e * (b - a) / 8 + e * (b - a) / 8" 
  3913       proof(rule add_mono) case goal1 have "\<bar>c - b\<bar> \<le> \<bar>l\<bar>" using as' by auto
  3914         thus ?case apply-apply(rule order_trans[OF _ l(2)]) unfolding norm_scaleR apply(rule mult_right_mono) by auto
  3915       next case goal2 show ?case apply(rule less_imp_le) apply(cases "b = c") defer apply(subst norm_minus_commute)
  3916           apply(rule k(2)[unfolded dist_norm]) using as' e ab by(auto simp add:field_simps)
  3917       qed finally show "norm (content {c..b} *\<^sub>R f' b - (f b - f c)) \<le> e * (b - a) / 4"
  3918         unfolding content_real[OF as(1)] by auto
  3919     qed qed then guess db .. note db=conjunctD2[OF this,rule_format]
  3920 
  3921   let ?d = "(\<lambda>x. ball x (if x=a then da else if x=b then db else d x))"
  3922   show "?P e" apply(rule_tac x="?d" in exI)
  3923   proof safe case goal1 show ?case apply(rule gauge_ball_dependent) using ab db(1) da(1) d(1) by auto
  3924   next case goal2 note as=this let ?A = "{t. fst t \<in> {a, b}}" note p = tagged_division_ofD[OF goal2(1)]
  3925     have pA:"p = (p \<inter> ?A) \<union> (p - ?A)" "finite (p \<inter> ?A)" "finite (p - ?A)" "(p \<inter> ?A) \<inter> (p - ?A) = {}"  using goal2 by auto
  3926     note * = additive_tagged_division_1'[OF assms(1) goal2(1), THEN sym]
  3927     have **:"\<And>n1 s1 n2 s2::real. n2 \<le> s2 / 2 \<Longrightarrow> n1 - s1 \<le> s2 / 2 \<Longrightarrow> n1 + n2 \<le> s1 + s2" by arith
  3928     show ?case unfolding content_real[OF assms(1)] and *[of "\<lambda>x. x"] *[of f] setsum_subtractf[THEN sym] split_minus
  3929       unfolding setsum_right_distrib apply(subst(2) pA,subst pA) unfolding setsum_Un_disjoint[OF pA(2-)]
  3930     proof(rule norm_triangle_le,rule **) 
  3931       case goal1 show ?case apply(rule order_trans,rule setsum_norm_le) defer apply(subst setsum_divide_distrib)
  3932       proof(rule order_refl,safe,unfold not_le o_def split_conv fst_conv,rule ccontr) fix x k assume as:"(x,k) \<in> p"
  3933           "e * (interval_upperbound k -  interval_lowerbound k) / 2
  3934           < norm (content k *\<^sub>R f' x - (f (interval_upperbound k) - f (interval_lowerbound k)))"
  3935         from p(4)[OF this(1)] guess u v apply-by(erule exE)+ note k=this
  3936         hence "u \<le> v" and uv:"{u,v}\<subseteq>{u..v}" using p(2)[OF as(1)] by auto
  3937         note result = as(2)[unfolded k interval_bounds_real[OF this(1)] content_real[OF this(1)]]
  3938 
  3939         assume as':"x \<noteq> a" "x \<noteq> b" hence "x \<in> {a<..<b}" using p(2-3)[OF as(1)] by auto
  3940         note  * = d(2)[OF this]
  3941         have "norm ((v - u) *\<^sub>R f' (x) - (f (v) - f (u))) =
  3942           norm ((f (u) - f (x) - (u - x) *\<^sub>R f' (x)) - (f (v) - f (x) - (v - x) *\<^sub>R f' (x)))" 
  3943           apply(rule arg_cong[of _ _ norm]) unfolding scaleR_left.diff by auto 
  3944         also have "... \<le> e / 2 * norm (u - x) + e / 2 * norm (v - x)" apply(rule norm_triangle_le_sub)
  3945           apply(rule add_mono) apply(rule_tac[!] *) using fineD[OF goal2(2) as(1)] as' unfolding k subset_eq
  3946           apply- apply(erule_tac x=u in ballE,erule_tac[3] x=v in ballE) using uv by(auto simp:dist_real_def)
  3947         also have "... \<le> e / 2 * norm (v - u)" using p(2)[OF as(1)] unfolding k by(auto simp add:field_simps)
  3948         finally have "e * (v - u) / 2 < e * (v - u) / 2"
  3949           apply- apply(rule less_le_trans[OF result]) using uv by auto thus False by auto qed
  3950 
  3951     next have *:"\<And>x s1 s2::real. 0 \<le> s1 \<Longrightarrow> x \<le> (s1 + s2) / 2 \<Longrightarrow> x - s1 \<le> s2 / 2" by auto
  3952       case goal2 show ?case apply(rule *) apply(rule setsum_nonneg) apply(rule,unfold split_paired_all split_conv)
  3953         defer unfolding setsum_Un_disjoint[OF pA(2-),THEN sym] pA(1)[THEN sym] unfolding setsum_right_distrib[THEN sym] 
  3954         apply(subst additive_tagged_division_1[OF _ as(1)]) apply(rule assms)
  3955       proof- fix x k assume "(x,k) \<in> p \<inter> {t. fst t \<in> {a, b}}" note xk=IntD1[OF this]
  3956         from p(4)[OF this] guess u v apply-by(erule exE)+ note uv=this
  3957         with p(2)[OF xk] have "{u..v} \<noteq> {}" by auto
  3958         thus "0 \<le> e * ((interval_upperbound k) - (interval_lowerbound k))"
  3959           unfolding uv using e by(auto simp add:field_simps)
  3960       next have *:"\<And>s f t e. setsum f s = setsum f t \<Longrightarrow> norm(setsum f t) \<le> e \<Longrightarrow> norm(setsum f s) \<le> e" by auto
  3961         show "norm (\<Sum>(x, k)\<in>p \<inter> ?A. content k *\<^sub>R f' x -
  3962           (f ((interval_upperbound k)) - f ((interval_lowerbound k)))) \<le> e * (b - a) / 2" 
  3963           apply(rule *[where t="p \<inter> {t. fst t \<in> {a, b} \<and> content(snd t) \<noteq> 0}"])
  3964           apply(rule setsum_mono_zero_right[OF pA(2)]) defer apply(rule) unfolding split_paired_all split_conv o_def
  3965         proof- fix x k assume "(x,k) \<in> p \<inter> {t. fst t \<in> {a, b}} - p \<inter> {t. fst t \<in> {a, b} \<and> content (snd t) \<noteq> 0}"
  3966           hence xk:"(x,k)\<in>p" "content k = 0" by auto from p(4)[OF xk(1)] guess u v apply-by(erule exE)+ note uv=this
  3967           have "k\<noteq>{}" using p(2)[OF xk(1)] by auto hence *:"u = v" using xk
  3968             unfolding uv content_eq_0 interval_eq_empty by auto
  3969           thus "content k *\<^sub>R (f' (x)) - (f ((interval_upperbound k)) - f ((interval_lowerbound k))) = 0" using xk unfolding uv by auto
  3970         next have *:"p \<inter> {t. fst t \<in> {a, b} \<and> content(snd t) \<noteq> 0} = 
  3971             {t. t\<in>p \<and> fst t = a \<and> content(snd t) \<noteq> 0} \<union> {t. t\<in>p \<and> fst t = b \<and> content(snd t) \<noteq> 0}" by blast
  3972           have **:"\<And>s f. \<And>e::real. (\<forall>x y. x \<in> s \<and> y \<in> s \<longrightarrow> x = y) \<Longrightarrow> (\<forall>x. x \<in> s \<longrightarrow> norm(f x) \<le> e)
  3973             \<Longrightarrow> e>0 \<Longrightarrow> norm(setsum f s) \<le> e"
  3974           proof(case_tac "s={}") case goal2 then obtain x where "x\<in>s" by auto hence *:"s = {x}" using goal2(1) by auto
  3975             thus ?case using `x\<in>s` goal2(2) by auto
  3976           qed auto
  3977           case goal2 show ?case apply(subst *, subst setsum_Un_disjoint) prefer 4
  3978             apply(rule order_trans[of _ "e * (b - a)/4 + e * (b - a)/4"]) 
  3979             apply(rule norm_triangle_le,rule add_mono) apply(rule_tac[1-2] **)
  3980           proof- let ?B = "\<lambda>x. {t \<in> p. fst t = x \<and> content (snd t) \<noteq> 0}"
  3981             have pa:"\<And>k. (a, k) \<in> p \<Longrightarrow> \<exists>v. k = {a .. v} \<and> a \<le> v" 
  3982             proof- case goal1 guess u v using p(4)[OF goal1] apply-by(erule exE)+ note uv=this
  3983               have *:"u \<le> v" using p(2)[OF goal1] unfolding uv by auto
  3984               have u:"u = a" proof(rule ccontr)  have "u \<in> {u..v}" using p(2-3)[OF goal1(1)] unfolding uv by auto 
  3985                 have "u \<ge> a" using p(2-3)[OF goal1(1)] unfolding uv subset_eq by auto moreover assume "u\<noteq>a" ultimately
  3986                 have "u > a" by auto
  3987                 thus False using p(2)[OF goal1(1)] unfolding uv by(auto simp add:)
  3988               qed thus ?case apply(rule_tac x=v in exI) unfolding uv using * by auto
  3989             qed
  3990             have pb:"\<And>k. (b, k) \<in> p \<Longrightarrow> \<exists>v. k = {v .. b} \<and> b \<ge> v" 
  3991             proof- case goal1 guess u v using p(4)[OF goal1] apply-by(erule exE)+ note uv=this
  3992               have *:"u \<le> v" using p(2)[OF goal1] unfolding uv by auto
  3993               have u:"v =  b" proof(rule ccontr)  have "u \<in> {u..v}" using p(2-3)[OF goal1(1)] unfolding uv by auto 
  3994                 have "v \<le>  b" using p(2-3)[OF goal1(1)] unfolding uv subset_eq by auto moreover assume "v\<noteq> b" ultimately
  3995                 have "v <  b" by auto
  3996                 thus False using p(2)[OF goal1(1)] unfolding uv by(auto simp add:)
  3997               qed thus ?case apply(rule_tac x=u in exI) unfolding uv using * by auto
  3998             qed
  3999 
  4000             show "\<forall>x y. x \<in> ?B a \<and> y \<in> ?B a \<longrightarrow> x = y" apply(rule,rule,rule,unfold split_paired_all)
  4001               unfolding mem_Collect_eq fst_conv snd_conv apply safe
  4002             proof- fix x k k' assume k:"( a, k) \<in> p" "( a, k') \<in> p" "content k \<noteq> 0" "content k' \<noteq> 0"
  4003               guess v using pa[OF k(1)] .. note v = conjunctD2[OF this]
  4004               guess v' using pa[OF k(2)] .. note v' = conjunctD2[OF this] let ?v = " (min (v) (v'))"
  4005               have "{ a <..< ?v} \<subseteq> k \<inter> k'" unfolding v v' by(auto simp add:) note interior_mono[OF this,unfolded interior_inter]
  4006               moreover have " ((a + ?v)/2) \<in> { a <..< ?v}" using k(3-)
  4007                 unfolding v v' content_eq_0 not_le by(auto simp add:not_le)
  4008               ultimately have " ((a + ?v)/2) \<in> interior k \<inter> interior k'" unfolding interior_open[OF open_interval] by auto
  4009               hence *:"k = k'" apply- apply(rule ccontr) using p(5)[OF k(1-2)] by auto
  4010               { assume "x\<in>k" thus "x\<in>k'" unfolding * . } { assume "x\<in>k'" thus "x\<in>k" unfolding * . }
  4011             qed 
  4012             show "\<forall>x y. x \<in> ?B b \<and> y \<in> ?B b \<longrightarrow> x = y" apply(rule,rule,rule,unfold split_paired_all)
  4013               unfolding mem_Collect_eq fst_conv snd_conv apply safe
  4014             proof- fix x k k' assume k:"( b, k) \<in> p" "( b, k') \<in> p" "content k \<noteq> 0" "content k' \<noteq> 0"
  4015               guess v using pb[OF k(1)] .. note v = conjunctD2[OF this]
  4016               guess v' using pb[OF k(2)] .. note v' = conjunctD2[OF this] let ?v = " (max (v) (v'))"
  4017               have "{?v <..<  b} \<subseteq> k \<inter> k'" unfolding v v' by(auto simp add:) note interior_mono[OF this,unfolded interior_inter]
  4018               moreover have " ((b + ?v)/2) \<in> {?v <..<  b}" using k(3-) unfolding v v' content_eq_0 not_le by auto
  4019               ultimately have " ((b + ?v)/2) \<in> interior k \<inter> interior k'" unfolding interior_open[OF open_interval] by auto
  4020               hence *:"k = k'" apply- apply(rule ccontr) using p(5)[OF k(1-2)] by auto
  4021               { assume "x\<in>k" thus "x\<in>k'" unfolding * . } { assume "x\<in>k'" thus "x\<in>k" unfolding * . }
  4022             qed
  4023 
  4024             let ?a = a and ?b = b (* a is something else while proofing the next theorem. *)
  4025             show "\<forall>x. x \<in> ?B a \<longrightarrow> norm ((\<lambda>(x, k). content k *\<^sub>R f' (x) - (f ((interval_upperbound k)) -
  4026               f ((interval_lowerbound k)))) x) \<le> e * (b - a) / 4" apply(rule,rule) unfolding mem_Collect_eq
  4027               unfolding split_paired_all fst_conv snd_conv 
  4028             proof safe case goal1 guess v using pa[OF goal1(1)] .. note v = conjunctD2[OF this]
  4029               have " ?a\<in>{ ?a..v}" using v(2) by auto hence "v \<le> ?b" using p(3)[OF goal1(1)] unfolding subset_eq v by auto
  4030               moreover have "{?a..v} \<subseteq> ball ?a da" using fineD[OF as(2) goal1(1)]
  4031                 apply-apply(subst(asm) if_P,rule refl) unfolding subset_eq apply safe apply(erule_tac x=" x" in ballE)
  4032                 by(auto simp add:subset_eq dist_real_def v) ultimately
  4033               show ?case unfolding v interval_bounds_real[OF v(2)] apply- apply(rule da(2)[of "v"])
  4034                 using goal1 fineD[OF as(2) goal1(1)] unfolding v content_eq_0 by auto
  4035             qed
  4036             show "\<forall>x. x \<in> ?B b \<longrightarrow> norm ((\<lambda>(x, k). content k *\<^sub>R f' (x) -
  4037               (f ((interval_upperbound k)) - f ((interval_lowerbound k)))) x) \<le> e * (b - a) / 4"
  4038               apply(rule,rule) unfolding mem_Collect_eq unfolding split_paired_all fst_conv snd_conv 
  4039             proof safe case goal1 guess v using pb[OF goal1(1)] .. note v = conjunctD2[OF this]
  4040               have " ?b\<in>{v.. ?b}" using v(2) by auto hence "v \<ge> ?a" using p(3)[OF goal1(1)]
  4041                 unfolding subset_eq v by auto
  4042               moreover have "{v..?b} \<subseteq> ball ?b db" using fineD[OF as(2) goal1(1)]
  4043                 apply-apply(subst(asm) if_P,rule refl) unfolding subset_eq apply safe
  4044                 apply(erule_tac x=" x" in ballE) using ab
  4045                 by(auto simp add:subset_eq v dist_real_def) ultimately
  4046               show ?case unfolding v unfolding interval_bounds_real[OF v(2)] apply- apply(rule db(2)[of "v"])
  4047                 using goal1 fineD[OF as(2) goal1(1)] unfolding v content_eq_0 by auto
  4048             qed
  4049           qed(insert p(1) ab e, auto simp add:field_simps) qed auto qed qed qed qed
  4050 
  4051 subsection {* Stronger form with finite number of exceptional points. *}
  4052 
  4053 lemma fundamental_theorem_of_calculus_interior_strong: fixes f::"real \<Rightarrow> 'a::banach"
  4054   assumes"finite s" "a \<le> b" "continuous_on {a..b} f"
  4055   "\<forall>x\<in>{a<..<b} - s. (f has_vector_derivative f'(x)) (at x)"
  4056   shows "(f' has_integral (f b - f a)) {a..b}" using assms apply- 
  4057 proof(induct "card s" arbitrary:s a b)
  4058   case 0 show ?case apply(rule fundamental_theorem_of_calculus_interior) using 0 by auto
  4059 next case (Suc n) from this(2) guess c s' apply-apply(subst(asm) eq_commute) unfolding card_Suc_eq
  4060     apply(subst(asm)(2) eq_commute) by(erule exE conjE)+ note cs = this[rule_format]
  4061   show ?case proof(cases "c\<in>{a<..<b}")
  4062     case False thus ?thesis apply- apply(rule Suc(1)[OF cs(3) _ Suc(4,5)]) apply safe defer
  4063       apply(rule Suc(6)[rule_format]) using Suc(3) unfolding cs by auto
  4064   next have *:"f b - f a = (f c - f a) + (f b - f c)" by auto
  4065     case True hence "a \<le> c" "c \<le> b" by auto
  4066     thus ?thesis apply(subst *) apply(rule has_integral_combine) apply assumption+
  4067       apply(rule_tac[!] Suc(1)[OF cs(3)]) using Suc(3) unfolding cs
  4068     proof- show "continuous_on {a..c} f" "continuous_on {c..b} f"
  4069         apply(rule_tac[!] continuous_on_subset[OF Suc(5)]) using True by auto
  4070       let ?P = "\<lambda>i j. \<forall>x\<in>{i<..<j} - s'. (f has_vector_derivative f' x) (at x)"
  4071       show "?P a c" "?P c b" apply safe apply(rule_tac[!] Suc(6)[rule_format]) using True unfolding cs by auto
  4072     qed auto qed qed
  4073 
  4074 lemma fundamental_theorem_of_calculus_strong: fixes f::"real \<Rightarrow> 'a::banach"
  4075   assumes "finite s" "a \<le> b" "continuous_on {a..b} f"
  4076   "\<forall>x\<in>{a..b} - s. (f has_vector_derivative f'(x)) (at x)"
  4077   shows "(f' has_integral (f(b) - f(a))) {a..b}"
  4078   apply(rule fundamental_theorem_of_calculus_interior_strong[OF assms(1-3), of f'])
  4079   using assms(4) by auto
  4080 
  4081 lemma indefinite_integral_continuous_left: fixes f::"real \<Rightarrow> 'a::banach"
  4082   assumes "f integrable_on {a..b}" "a < c" "c \<le> b" "0 < e"
  4083   obtains d where "0 < d" "\<forall>t. c - d < t \<and> t \<le> c \<longrightarrow> norm(integral {a..c} f - integral {a..t} f) < e"
  4084 proof- have "\<exists>w>0. \<forall>t. c - w < t \<and> t < c \<longrightarrow> norm(f c) * norm(c - t) < e / 3"
  4085   proof(cases "f c = 0") case False hence "0 < e / 3 / norm (f c)"
  4086       apply-apply(rule divide_pos_pos) using `e>0` by auto
  4087     thus ?thesis apply-apply(rule,rule,assumption,safe)
  4088     proof- fix t assume as:"t < c" and "c - e / 3 / norm (f c) < t"
  4089       hence "c - t < e / 3 / norm (f c)" by auto
  4090       hence "norm (c - t) < e / 3 / norm (f c)" using as by auto
  4091       thus "norm (f c) * norm (c - t) < e / 3" using False apply-
  4092         apply(subst mult_commute) apply(subst pos_less_divide_eq[THEN sym]) by auto
  4093     qed next case True show ?thesis apply(rule_tac x=1 in exI) unfolding True using `e>0` by auto
  4094   qed then guess w .. note w = conjunctD2[OF this,rule_format]
  4095   
  4096   have *:"e / 3 > 0" using assms by auto
  4097   have "f integrable_on {a..c}" apply(rule integrable_subinterval[OF assms(1)]) using assms(2-3) by auto
  4098   from integrable_integral[OF this,unfolded has_integral,rule_format,OF *] guess d1 ..
  4099   note d1 = conjunctD2[OF this,rule_format] def d \<equiv> "\<lambda>x. ball x w \<inter> d1 x"
  4100   have "gauge d" unfolding d_def using w(1) d1 by auto
  4101   note this[unfolded gauge_def,rule_format,of c] note conjunctD2[OF this]
  4102   from this(2)[unfolded open_contains_ball,rule_format,OF this(1)] guess k .. note k=conjunctD2[OF this]
  4103 
  4104   let ?d = "min k (c - a)/2" show ?thesis apply(rule that[of ?d])
  4105   proof safe show "?d > 0" using k(1) using assms(2) by auto
  4106     fix t assume as:"c - ?d < t" "t \<le> c" let ?thesis = "norm (integral {a..c} f - integral {a..t} f) < e"
  4107     { presume *:"t < c \<Longrightarrow> ?thesis"
  4108       show ?thesis apply(cases "t = c") defer apply(rule *)
  4109         apply(subst less_le) using `e>0` as(2) by auto } assume "t < c"
  4110 
  4111     have "f integrable_on {a..t}" apply(rule integrable_subinterval[OF assms(1)]) using assms(2-3) as(2) by auto
  4112     from integrable_integral[OF this,unfolded has_integral,rule_format,OF *] guess d2 ..
  4113     note d2 = conjunctD2[OF this,rule_format]
  4114     def d3 \<equiv> "\<lambda>x. if x \<le> t then d1 x \<inter> d2 x else d1 x"
  4115     have "gauge d3" using d2(1) d1(1) unfolding d3_def gauge_def by auto
  4116     from fine_division_exists[OF this, of a t] guess p . note p=this
  4117     note p'=tagged_division_ofD[OF this(1)]
  4118     have pt:"\<forall>(x,k)\<in>p. x \<le> t" proof safe case goal1 from p'(2,3)[OF this] show ?case by auto qed
  4119     with p(2) have "d2 fine p" unfolding fine_def d3_def apply safe apply(erule_tac x="(a,b)" in ballE)+ by auto
  4120     note d2_fin = d2(2)[OF conjI[OF p(1) this]]
  4121     
  4122     have *:"{a..c} \<inter> {x. x $$0 \<le> t} = {a..t}" "{a..c} \<inter> {x. x$$0 \<ge> t} = {t..c}"
  4123       using assms(2-3) as by(auto simp add:field_simps)
  4124     have "p \<union> {(c, {t..c})} tagged_division_of {a..c} \<and> d1 fine p \<union> {(c, {t..c})}" apply rule
  4125       apply(rule tagged_division_union_interval[of _ _ _ 0 "t"]) unfolding * apply(rule p)
  4126       apply(rule tagged_division_of_self) unfolding fine_def
  4127     proof safe fix x k y assume "(x,k)\<in>p" "y\<in>k" thus "y\<in>d1 x"
  4128         using p(2) pt unfolding fine_def d3_def apply- apply(erule_tac x="(x,k)" in ballE)+ by auto
  4129     next fix x assume "x\<in>{t..c}" hence "dist c x < k" unfolding dist_real_def
  4130         using as(1) by(auto simp add:field_simps) 
  4131       thus "x \<in> d1 c" using k(2) unfolding d_def by auto
  4132     qed(insert as(2), auto) note d1_fin = d1(2)[OF this]
  4133 
  4134     have *:"integral{a..c} f - integral {a..t} f = -(((c - t) *\<^sub>R f c + (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)) -
  4135         integral {a..c} f) + ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - integral {a..t} f) + (c - t) *\<^sub>R f c" 
  4136       "e = (e/3 + e/3) + e/3" by auto
  4137     have **:"(\<Sum>(x, k)\<in>p \<union> {(c, {t..c})}. content k *\<^sub>R f x) = (c - t) *\<^sub>R f c + (\<Sum>(x, k)\<in>p. content k *\<^sub>R f x)"
  4138     proof- have **:"\<And>x F. F \<union> {x} = insert x F" by auto
  4139       have "(c, {t..c}) \<notin> p" proof safe case goal1 from p'(2-3)[OF this]
  4140         have "c \<in> {a..t}" by auto thus False using `t<c` by auto
  4141       qed thus ?thesis unfolding ** apply- apply(subst setsum_insert) apply(rule p')
  4142         unfolding split_conv defer apply(subst content_real) using as(2) by auto qed 
  4143 
  4144     have ***:"c - w < t \<and> t < c"
  4145     proof- have "c - k < t" using `k>0` as(1) by(auto simp add:field_simps)
  4146       moreover have "k \<le> w" apply(rule ccontr) using k(2) 
  4147         unfolding subset_eq apply(erule_tac x="c + ((k + w)/2)" in ballE)
  4148         unfolding d_def using `k>0` `w>0` by(auto simp add:field_simps not_le not_less dist_real_def)
  4149       ultimately show  ?thesis using `t<c` by(auto simp add:field_simps) qed
  4150 
  4151     show ?thesis unfolding *(1) apply(subst *(2)) apply(rule norm_triangle_lt add_strict_mono)+
  4152       unfolding norm_minus_cancel apply(rule d1_fin[unfolded **]) apply(rule d2_fin)
  4153       using w(2)[OF ***] unfolding norm_scaleR by(auto simp add:field_simps) qed qed 
  4154 
  4155 lemma indefinite_integral_continuous_right: fixes f::"real \<Rightarrow> 'a::banach"
  4156   assumes "f integrable_on {a..b}" "a \<le> c" "c < b" "0 < e"
  4157   obtains d where "0 < d" "\<forall>t. c \<le> t \<and> t < c + d \<longrightarrow> norm(integral{a..c} f - integral{a..t} f) < e"
  4158 proof- have *:"(\<lambda>x. f (- x)) integrable_on {- b..- a}" "- b < - c" "- c \<le> - a" using assms by auto
  4159   from indefinite_integral_continuous_left[OF * `e>0`] guess d . note d = this let ?d = "min d (b - c)"
  4160   show ?thesis apply(rule that[of "?d"])
  4161   proof safe show "0 < ?d" using d(1) assms(3) by auto
  4162     fix t::"real" assume as:"c \<le> t" "t < c + ?d"
  4163     have *:"integral{a..c} f = integral{a..b} f - integral{c..b} f"
  4164       "integral{a..t} f = integral{a..b} f - integral{t..b} f" unfolding algebra_simps
  4165       apply(rule_tac[!] integral_combine) using assms as by auto
  4166     have "(- c) - d < (- t) \<and> - t \<le> - c" using as by auto note d(2)[rule_format,OF this]
  4167     thus "norm (integral {a..c} f - integral {a..t} f) < e" unfolding * 
  4168       unfolding integral_reflect apply-apply(subst norm_minus_commute) by(auto simp add:algebra_simps) qed qed
  4169    
  4170 lemma indefinite_integral_continuous: fixes f::"real \<Rightarrow> 'a::banach"
  4171   assumes "f integrable_on {a..b}" shows  "continuous_on {a..b} (\<lambda>x. integral {a..x} f)"
  4172 proof(unfold continuous_on_iff, safe)  fix x e assume as:"x\<in>{a..b}" "0<(e::real)"
  4173   let ?thesis = "\<exists>d>0. \<forall>x'\<in>{a..b}. dist x' x < d \<longrightarrow> dist (integral {a..x'} f) (integral {a..x} f) < e"
  4174   { presume *:"a<b \<Longrightarrow> ?thesis"
  4175     show ?thesis apply(cases,rule *,assumption)
  4176     proof- case goal1 hence "{a..b} = {x}" using as(1) apply-apply(rule set_eqI)
  4177         unfolding atLeastAtMost_iff by(auto simp only:field_simps not_less DIM_real)
  4178       thus ?case using `e>0` by auto
  4179     qed } assume "a<b"
  4180   have "(x=a \<or> x=b) \<or> (a<x \<and> x<b)" using as(1) by (auto simp add:)
  4181   thus ?thesis apply-apply(erule disjE)+
  4182   proof- assume "x=a" have "a \<le> a" by auto
  4183     from indefinite_integral_continuous_right[OF assms(1) this `a<b` `e>0`] guess d . note d=this
  4184     show ?thesis apply(rule,rule,rule d,safe) apply(subst dist_commute)
  4185       unfolding `x=a` dist_norm apply(rule d(2)[rule_format]) by auto
  4186   next   assume "x=b" have "b \<le> b" by auto
  4187     from indefinite_integral_continuous_left[OF assms(1) `a<b` this `e>0`] guess d . note d=this
  4188     show ?thesis apply(rule,rule,rule d,safe) apply(subst dist_commute)
  4189       unfolding `x=b` dist_norm apply(rule d(2)[rule_format])  by auto
  4190   next assume "a<x \<and> x<b" hence xl:"a<x" "x\<le>b" and xr:"a\<le>x" "x<b" by(auto simp add: )
  4191     from indefinite_integral_continuous_left [OF assms(1) xl `e>0`] guess d1 . note d1=this
  4192     from indefinite_integral_continuous_right[OF assms(1) xr `e>0`] guess d2 . note d2=this
  4193     show ?thesis apply(rule_tac x="min d1 d2" in exI)
  4194     proof safe show "0 < min d1 d2" using d1 d2 by auto
  4195       fix y assume "y\<in>{a..b}" "dist y x < min d1 d2"
  4196       thus "dist (integral {a..y} f) (integral {a..x} f) < e" apply-apply(subst dist_commute)
  4197         apply(cases "y < x") unfolding dist_norm apply(rule d1(2)[rule_format]) defer
  4198         apply(rule d2(2)[rule_format]) unfolding not_less by(auto simp add:field_simps)
  4199     qed qed qed 
  4200 
  4201 subsection {* This doesn't directly involve integration, but that gives an easy proof. *}
  4202 
  4203 lemma has_derivative_zero_unique_strong_interval: fixes f::"real \<Rightarrow> 'a::banach"
  4204   assumes "finite k" "continuous_on {a..b} f" "f a = y"
  4205   "\<forall>x\<in>({a..b} - k). (f has_derivative (\<lambda>h. 0)) (at x within {a..b})" "x \<in> {a..b}"
  4206   shows "f x = y"
  4207 proof- have ab:"a\<le>b" using assms by auto
  4208   have *:"a\<le>x" using assms(5) by auto
  4209   have "((\<lambda>x. 0\<Colon>'a) has_integral f x - f a) {a..x}"
  4210     apply(rule fundamental_theorem_of_calculus_interior_strong[OF assms(1) *])
  4211     apply(rule continuous_on_subset[OF assms(2)]) defer
  4212     apply safe unfolding has_vector_derivative_def apply(subst has_derivative_within_open[THEN sym])
  4213     apply assumption apply(rule open_interval) apply(rule has_derivative_within_subset[where s="{a..b}"])
  4214     using assms(4) assms(5) by auto note this[unfolded *]
  4215   note has_integral_unique[OF has_integral_0 this]
  4216   thus ?thesis unfolding assms by auto qed
  4217 
  4218 subsection {* Generalize a bit to any convex set. *}
  4219 
  4220 lemma has_derivative_zero_unique_strong_convex: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::banach"
  4221   assumes "convex s" "finite k" "continuous_on s f" "c \<in> s" "f c = y"
  4222   "\<forall>x\<in>(s - k). (f has_derivative (\<lambda>h. 0)) (at x within s)" "x \<in> s"
  4223   shows "f x = y"
  4224 proof- { presume *:"x \<noteq> c \<Longrightarrow> ?thesis" show ?thesis apply(cases,rule *,assumption)
  4225       unfolding assms(5)[THEN sym] by auto } assume "x\<noteq>c"
  4226   note conv = assms(1)[unfolded convex_alt,rule_format]
  4227   have as1:"continuous_on {0..1} (f \<circ> (\<lambda>t. (1 - t) *\<^sub>R c + t *\<^sub>R x))"
  4228     apply(rule continuous_on_intros)+ apply(rule continuous_on_subset[OF assms(3)])
  4229     apply safe apply(rule conv) using assms(4,7) by auto
  4230   have *:"\<And>t xa. (1 - t) *\<^sub>R c + t *\<^sub>R x = (1 - xa) *\<^sub>R c + xa *\<^sub>R x \<Longrightarrow> t = xa"
  4231   proof- case goal1 hence "(t - xa) *\<^sub>R x = (t - xa) *\<^sub>R c" 
  4232       unfolding scaleR_simps by(auto simp add:algebra_simps)
  4233     thus ?case using `x\<noteq>c` by auto qed
  4234   have as2:"finite {t. ((1 - t) *\<^sub>R c + t *\<^sub>R x) \<in> k}" using assms(2) 
  4235     apply(rule finite_surj[where f="\<lambda>z. SOME t. (1-t) *\<^sub>R c + t *\<^sub>R x = z"])
  4236     apply safe unfolding image_iff apply rule defer apply assumption
  4237     apply(rule sym) apply(rule some_equality) defer apply(drule *) by auto
  4238   have "(f \<circ> (\<lambda>t. (1 - t) *\<^sub>R c + t *\<^sub>R x)) 1 = y"
  4239     apply(rule has_derivative_zero_unique_strong_interval[OF as2 as1, of ])
  4240     unfolding o_def using assms(5) defer apply-apply(rule)
  4241   proof- fix t assume as:"t\<in>{0..1} - {t. (1 - t) *\<^sub>R c + t *\<^sub>R x \<in> k}"
  4242     have *:"c - t *\<^sub>R c + t *\<^sub>R x \<in> s - k" apply safe apply(rule conv[unfolded scaleR_simps]) 
  4243       using `x\<in>s` `c\<in>s` as by(auto simp add: algebra_simps)
  4244     have "(f \<circ> (\<lambda>t. (1 - t) *\<^sub>R c + t *\<^sub>R x) has_derivative (\<lambda>x. 0) \<circ> (\<lambda>z. (0 - z *\<^sub>R c) + z *\<^sub>R x)) (at t within {0..1})"
  4245       apply(rule diff_chain_within) apply(rule has_derivative_add)
  4246       unfolding scaleR_simps
  4247       apply(intro has_derivative_intros)
  4248       apply(intro has_derivative_intros)
  4249       apply(rule has_derivative_within_subset,rule assms(6)[rule_format])
  4250       apply(rule *) apply safe apply(rule conv[unfolded scaleR_simps]) using `x\<in>s` `c\<in>s` by auto
  4251     thus "((\<lambda>xa. f ((1 - xa) *\<^sub>R c + xa *\<^sub>R x)) has_derivative (\<lambda>h. 0)) (at t within {0..1})" unfolding o_def .
  4252   qed auto thus ?thesis by auto qed
  4253 
  4254 subsection {* Also to any open connected set with finite set of exceptions. Could 
  4255  generalize to locally convex set with limpt-free set of exceptions. *}
  4256 
  4257 lemma has_derivative_zero_unique_strong_connected: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::banach"
  4258   assumes "connected s" "open s" "finite k" "continuous_on s f" "c \<in> s" "f c = y"
  4259   "\<forall>x\<in>(s - k). (f has_derivative (\<lambda>h. 0)) (at x within s)" "x\<in>s"
  4260   shows "f x = y"
  4261 proof- have "{x \<in> s. f x \<in> {y}} = {} \<or> {x \<in> s. f x \<in> {y}} = s"
  4262     apply(rule assms(1)[unfolded connected_clopen,rule_format]) apply rule defer
  4263     apply(rule continuous_closed_in_preimage[OF assms(4) closed_singleton])
  4264     apply(rule open_openin_trans[OF assms(2)]) unfolding open_contains_ball
  4265   proof safe fix x assume "x\<in>s" 
  4266     from assms(2)[unfolded open_contains_ball,rule_format,OF this] guess e .. note e=conjunctD2[OF this]
  4267     show "\<exists>e>0. ball x e \<subseteq> {xa \<in> s. f xa \<in> {f x}}" apply(rule,rule,rule e)
  4268     proof safe fix y assume y:"y \<in> ball x e" thus "y\<in>s" using e by auto
  4269       show "f y = f x" apply(rule has_derivative_zero_unique_strong_convex[OF convex_ball])
  4270         apply(rule assms) apply(rule continuous_on_subset,rule assms) apply(rule e)+
  4271         apply(subst centre_in_ball,rule e,rule) apply safe
  4272         apply(rule has_derivative_within_subset) apply(rule assms(7)[rule_format])
  4273         using y e by auto qed qed
  4274   thus ?thesis using `x\<in>s` `f c = y` `c\<in>s` by auto qed
  4275 
  4276 subsection {* Integrating characteristic function of an interval. *}
  4277 
  4278 lemma has_integral_restrict_open_subinterval: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::banach"
  4279   assumes "(f has_integral i) {c..d}" "{c..d} \<subseteq> {a..b}"
  4280   shows "((\<lambda>x. if x \<in> {c<..<d} then f x else 0) has_integral i) {a..b}"
  4281 proof- def g \<equiv> "\<lambda>x. if x \<in>{c<..<d} then f x else 0"
  4282   { presume *:"{c..d}\<noteq>{} \<Longrightarrow> ?thesis"
  4283     show ?thesis apply(cases,rule *,assumption)
  4284     proof- case goal1 hence *:"{c<..<d} = {}" using interval_open_subset_closed by auto 
  4285       show ?thesis using assms(1) unfolding * using goal1 by auto
  4286     qed } assume "{c..d}\<noteq>{}"
  4287   from partial_division_extend_1[OF assms(2) this] guess p . note p=this
  4288   note mon = monoidal_lifted[OF monoidal_monoid] 
  4289   note operat = operative_division[OF this operative_integral p(1), THEN sym]
  4290   let ?P = "(if g integrable_on {a..b} then Some (integral {a..b} g) else None) = Some i"
  4291   { presume "?P" hence "g integrable_on {a..b} \<and> integral {a..b} g = i"
  4292       apply- apply(cases,subst(asm) if_P,assumption) by auto
  4293     thus ?thesis using integrable_integral unfolding g_def by auto }
  4294 
  4295   note iterate_eq_neutral[OF mon,unfolded neutral_lifted[OF monoidal_monoid]]
  4296   note * = this[unfolded neutral_monoid]
  4297   have iterate:"iterate (lifted op +) (p - {{c..d}})
  4298       (\<lambda>i. if g integrable_on i then Some (integral i g) else None) = Some 0"
  4299   proof(rule *,rule) case goal1 hence "x\<in>p" by auto note div = division_ofD(2-5)[OF p(1) this]
  4300     from div(3) guess u v apply-by(erule exE)+ note uv=this
  4301     have "interior x \<inter> interior {c..d} = {}" using div(4)[OF p(2)] goal1 by auto
  4302     hence "(g has_integral 0) x" unfolding uv apply-apply(rule has_integral_spike_interior[where f="\<lambda>x. 0"])
  4303       unfolding g_def interior_closed_interval by auto thus ?case by auto
  4304   qed
  4305 
  4306   have *:"p = insert {c..d} (p - {{c..d}})" using p by auto
  4307   have **:"g integrable_on {c..d}" apply(rule integrable_spike_interior[where f=f])
  4308     unfolding g_def defer apply(rule has_integral_integrable) using assms(1) by auto
  4309   moreover have "integral {c..d} g = i" apply(rule has_integral_unique[OF _ assms(1)])
  4310     apply(rule has_integral_spike_interior[where f=g]) defer
  4311     apply(rule integrable_integral[OF **]) unfolding g_def by auto
  4312   ultimately show ?P unfolding operat apply- apply(subst *) apply(subst iterate_insert) apply rule+
  4313     unfolding iterate defer apply(subst if_not_P) defer using p by auto qed
  4314 
  4315 lemma has_integral_restrict_closed_subinterval: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::banach"
  4316   assumes "(f has_integral i) ({c..d})" "{c..d} \<subseteq> {a..b}" 
  4317   shows "((\<lambda>x. if x \<in> {c..d} then f x else 0) has_integral i) {a..b}"
  4318 proof- note has_integral_restrict_open_subinterval[OF assms]
  4319   note * = has_integral_spike[OF negligible_frontier_interval _ this]
  4320   show ?thesis apply(rule *[of c d]) using interval_open_subset_closed[of c d] by auto qed
  4321 
  4322 lemma has_integral_restrict_closed_subintervals_eq: fixes f::"'a::ordered_euclidean_space \<Rightarrow> 'b::banach" assumes "{c..d} \<subseteq> {a..b}" 
  4323   shows "((\<lambda>x. if x \<in> {c..d} then f x else 0) has_integral i) {a..b} \<longleftrightarrow> (f has_integral i) {c..d}" (is "?l = ?r")
  4324 proof(cases "{c..d} = {}") case False let ?g = "\<lambda>x. if x \<in> {c..d} then f x else 0"
  4325   show ?thesis apply rule defer apply(rule has_integral_restrict_closed_subinterval[OF _ assms])
  4326   proof assumption assume ?l hence "?g integrable_on {c..d}"
  4327       apply-apply(rule integrable_subinterval[OF _ assms]) by auto
  4328     hence *:"f integrable_on {c..d}"apply-apply(rule integrable_eq) by auto
  4329     hence "i = integral {c..d} f" apply-apply(rule has_integral_unique)
  4330       apply(rule `?l`) apply(rule has_integral_restrict_closed_subinterval[OF _ assms]) by auto
  4331     thus ?r using * by auto qed qed auto
  4332 
  4333 subsection {* Hence we can apply the limit process uniformly to all integrals. *}
  4334 
  4335 lemma has_integral': fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach" shows
  4336  "(f has_integral i) s \<longleftrightarrow> (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b}
  4337   \<longrightarrow> (\<exists>z. ((\<lambda>x. if x \<in> s then f(x) else 0) has_integral z) {a..b} \<and> norm(z - i) < e))" (is "?l \<longleftrightarrow> (\<forall>e>0. ?r e)")
  4338 proof- { presume *:"\<exists>a b. s = {a..b} \<Longrightarrow> ?thesis"
  4339     show ?thesis apply(cases,rule *,assumption)
  4340       apply(subst has_integral_alt) by auto }
  4341   assume "\<exists>a b. s = {a..b}" then guess a b apply-by(erule exE)+ note s=this
  4342   from bounded_interval[of a b, THEN conjunct1, unfolded bounded_pos] guess B ..
  4343   note B = conjunctD2[OF this,rule_format] show ?thesis apply safe
  4344   proof- fix e assume ?l "e>(0::real)"
  4345     show "?r e" apply(rule_tac x="B+1" in exI) apply safe defer apply(rule_tac x=i in exI)
  4346     proof fix c d assume as:"ball 0 (B+1) \<subseteq> {c..d::'n::ordered_euclidean_space}"
  4347       thus "((\<lambda>x. if x \<in> s then f x else 0) has_integral i) {c..d}" unfolding s
  4348         apply-apply(rule has_integral_restrict_closed_subinterval) apply(rule `?l`[unfolded s])
  4349         apply safe apply(drule B(2)[rule_format]) unfolding subset_eq apply(erule_tac x=x in ballE)
  4350         by(auto simp add:dist_norm)
  4351     qed(insert B `e>0`, auto)
  4352   next assume as:"\<forall>e>0. ?r e" 
  4353     from this[rule_format,OF zero_less_one] guess C .. note C=conjunctD2[OF this,rule_format]
  4354     def c \<equiv> "(\<chi>\<chi> i. - max B C)::'n::ordered_euclidean_space" and d \<equiv> "(\<chi>\<chi> i. max B C)::'n::ordered_euclidean_space"
  4355     have c_d:"{a..b} \<subseteq> {c..d}" apply safe apply(drule B(2)) unfolding mem_interval
  4356     proof case goal1 thus ?case using component_le_norm[of x i] unfolding c_def d_def
  4357         by(auto simp add:field_simps) qed
  4358     have "ball 0 C \<subseteq> {c..d}" apply safe unfolding mem_interval mem_ball dist_norm 
  4359     proof case goal1 thus ?case using component_le_norm[of x i] unfolding c_def d_def by auto qed
  4360     from C(2)[OF this] have "\<exists>y. (f has_integral y) {a..b}"
  4361       unfolding has_integral_restrict_closed_subintervals_eq[OF c_d,THEN sym] unfolding s by auto
  4362     then guess y .. note y=this
  4363 
  4364     have "y = i" proof(rule ccontr) assume "y\<noteq>i" hence "0 < norm (y - i)" by auto
  4365       from as[rule_format,OF this] guess C ..  note C=conjunctD2[OF this,rule_format]
  4366       def c \<equiv> "(\<chi>\<chi> i. - max B C)::'n::ordered_euclidean_space" and d \<equiv> "(\<chi>\<chi> i. max B C)::'n::ordered_euclidean_space"
  4367       have c_d:"{a..b} \<subseteq> {c..d}" apply safe apply(drule B(2)) unfolding mem_interval
  4368       proof case goal1 thus ?case using component_le_norm[of x i] unfolding c_def d_def
  4369           by(auto simp add:field_simps) qed
  4370       have "ball 0 C \<subseteq> {c..d}" apply safe unfolding mem_interval mem_ball dist_norm 
  4371       proof case goal1 thus ?case using component_le_norm[of x i] unfolding c_def d_def by auto qed
  4372       note C(2)[OF this] then guess z .. note z = conjunctD2[OF this, unfolded s]
  4373       note this[unfolded has_integral_restrict_closed_subintervals_eq[OF c_d]]
  4374       hence "z = y" "norm (z - i) < norm (y - i)" apply- apply(rule has_integral_unique[OF _ y(1)]) .
  4375       thus False by auto qed
  4376     thus ?l using y unfolding s by auto qed qed 
  4377 
  4378 (*lemma has_integral_trans[simp]: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real" shows
  4379   "((\<lambda>x. vec1 (f x)) has_integral vec1 i) s \<longleftrightarrow> (f has_integral i) s"
  4380   unfolding has_integral'[unfolded has_integral] 
  4381 proof case goal1 thus ?case apply safe
  4382   apply(erule_tac x=e in allE,safe) apply(rule_tac x=B in exI,safe)
  4383   apply(erule_tac x=a in allE, erule_tac x=b in allE,safe) 
  4384   apply(rule_tac x="dest_vec1 z" in exI,safe) apply(erule_tac x=ea in allE,safe) 
  4385   apply(rule_tac x=d in exI,safe) apply(erule_tac x=p in allE,safe)
  4386   apply(subst(asm)(2) norm_vector_1) unfolding split_def
  4387   unfolding setsum_component Cart_nth.diff cond_value_iff[of dest_vec1]
  4388     Cart_nth.scaleR vec1_dest_vec1 zero_index apply assumption
  4389   apply(subst(asm)(2) norm_vector_1) by auto
  4390 next case goal2 thus ?case apply safe
  4391   apply(erule_tac x=e in allE,safe) apply(rule_tac x=B in exI,safe)
  4392   apply(erule_tac x=a in allE, erule_tac x=b in allE,safe) 
  4393   apply(rule_tac x="vec1 z" in exI,safe) apply(erule_tac x=ea in allE,safe) 
  4394   apply(rule_tac x=d in exI,safe) apply(erule_tac x=p in allE,safe)
  4395   apply(subst norm_vector_1) unfolding split_def
  4396   unfolding setsum_component Cart_nth.diff cond_value_iff[of dest_vec1]
  4397     Cart_nth.scaleR vec1_dest_vec1 zero_index apply assumption
  4398   apply(subst norm_vector_1) by auto qed
  4399 
  4400 lemma integral_trans[simp]: assumes "(f::'n::ordered_euclidean_space \<Rightarrow> real) integrable_on s"
  4401   shows "integral s (\<lambda>x. vec1 (f x)) = vec1 (integral s f)"
  4402   apply(rule integral_unique) using assms by auto
  4403 
  4404 lemma integrable_on_trans[simp]: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real" shows
  4405   "(\<lambda>x. vec1 (f x)) integrable_on s \<longleftrightarrow> (f integrable_on s)"
  4406   unfolding integrable_on_def
  4407   apply(subst(2) vec1_dest_vec1(1)[THEN sym]) unfolding has_integral_trans
  4408   apply safe defer apply(rule_tac x="vec1 y" in exI) by auto *)
  4409 
  4410 lemma has_integral_le: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real"
  4411   assumes "(f has_integral i) s" "(g has_integral j) s"  "\<forall>x\<in>s. (f x) \<le> (g x)"
  4412   shows "i \<le> j" using has_integral_component_le[OF assms(1-2), of 0] using assms(3) by auto
  4413 
  4414 lemma integral_le: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real"
  4415   assumes "f integrable_on s" "g integrable_on s" "\<forall>x\<in>s. f x \<le> g x"
  4416   shows "integral s f \<le> integral s g"
  4417   using has_integral_le[OF assms(1,2)[unfolded has_integral_integral] assms(3)] .
  4418 
  4419 lemma has_integral_nonneg: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real"
  4420   assumes "(f has_integral i) s" "\<forall>x\<in>s. 0 \<le> f x" shows "0 \<le> i" 
  4421   using has_integral_component_nonneg[of "f" "i" s 0]
  4422   unfolding o_def using assms by auto
  4423 
  4424 lemma integral_nonneg: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real"
  4425   assumes "f integrable_on s" "\<forall>x\<in>s. 0 \<le> f x" shows "0 \<le> integral s f" 
  4426   using has_integral_nonneg[OF assms(1)[unfolded has_integral_integral] assms(2)] .
  4427 
  4428 subsection {* Hence a general restriction property. *}
  4429 
  4430 lemma has_integral_restrict[simp]: assumes "s \<subseteq> t" shows
  4431   "((\<lambda>x. if x \<in> s then f x else (0::'a::banach)) has_integral i) t \<longleftrightarrow> (f has_integral i) s"
  4432 proof- have *:"\<And>x. (if x \<in> t then if x \<in> s then f x else 0 else 0) =  (if x\<in>s then f x else 0)" using assms by auto
  4433   show ?thesis apply(subst(2) has_integral') apply(subst has_integral') unfolding * by rule qed
  4434 
  4435 lemma has_integral_restrict_univ: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach" shows
  4436   "((\<lambda>x. if x \<in> s then f x else 0) has_integral i) UNIV \<longleftrightarrow> (f has_integral i) s" by auto
  4437 
  4438 lemma has_integral_on_superset: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach" 
  4439   assumes "\<forall>x. ~(x \<in> s) \<longrightarrow> f x = 0" "s \<subseteq> t" "(f has_integral i) s"
  4440   shows "(f has_integral i) t"
  4441 proof- have "(\<lambda>x. if x \<in> s then f x else 0) = (\<lambda>x. if x \<in> t then f x else 0)"
  4442     apply(rule) using assms(1-2) by auto
  4443   thus ?thesis apply- using assms(3) apply(subst has_integral_restrict_univ[THEN sym])
  4444   apply- apply(subst(asm) has_integral_restrict_univ[THEN sym]) by auto qed
  4445 
  4446 lemma integrable_on_superset: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach" 
  4447   assumes "\<forall>x. ~(x \<in> s) \<longrightarrow> f x = 0" "s \<subseteq> t" "f integrable_on s"
  4448   shows "f integrable_on t"
  4449   using assms unfolding integrable_on_def by(auto intro:has_integral_on_superset)
  4450 
  4451 lemma integral_restrict_univ[intro]: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach" 
  4452   shows "f integrable_on s \<Longrightarrow> integral UNIV (\<lambda>x. if x \<in> s then f x else 0) = integral s f"
  4453   apply(rule integral_unique) unfolding has_integral_restrict_univ by auto
  4454 
  4455 lemma integrable_restrict_univ: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach" shows
  4456  "(\<lambda>x. if x \<in> s then f x else 0) integrable_on UNIV \<longleftrightarrow> f integrable_on s"
  4457   unfolding integrable_on_def by auto
  4458 
  4459 lemma negligible_on_intervals: "negligible s \<longleftrightarrow> (\<forall>a b. negligible(s \<inter> {a..b}))" (is "?l = ?r")
  4460 proof assume ?r show ?l unfolding negligible_def
  4461   proof safe case goal1 show ?case apply(rule has_integral_negligible[OF `?r`[rule_format,of a b]])
  4462       unfolding indicator_def by auto qed qed auto
  4463 
  4464 lemma has_integral_spike_set_eq: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach" 
  4465   assumes "negligible((s - t) \<union> (t - s))" shows "((f has_integral y) s \<longleftrightarrow> (f has_integral y) t)"
  4466   unfolding has_integral_restrict_univ[THEN sym,of f] apply(rule has_integral_spike_eq[OF assms]) by (auto split: split_if_asm)
  4467 
  4468 lemma has_integral_spike_set[dest]: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4469   assumes "negligible((s - t) \<union> (t - s))" "(f has_integral y) s"
  4470   shows "(f has_integral y) t"
  4471   using assms has_integral_spike_set_eq by auto
  4472 
  4473 lemma integrable_spike_set[dest]: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4474   assumes "negligible((s - t) \<union> (t - s))" "f integrable_on s"
  4475   shows "f integrable_on t" using assms(2) unfolding integrable_on_def 
  4476   unfolding has_integral_spike_set_eq[OF assms(1)] .
  4477 
  4478 lemma integrable_spike_set_eq: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4479   assumes "negligible((s - t) \<union> (t - s))"
  4480   shows "(f integrable_on s \<longleftrightarrow> f integrable_on t)"
  4481   apply(rule,rule_tac[!] integrable_spike_set) using assms by auto
  4482 
  4483 (*lemma integral_spike_set:
  4484  "\<forall>f:real^M->real^N g s t.
  4485         negligible(s DIFF t \<union> t DIFF s)
  4486         \<longrightarrow> integral s f = integral t f"
  4487 qed  REPEAT STRIP_TAC THEN REWRITE_TAC[integral] THEN
  4488   AP_TERM_TAC THEN ABS_TAC THEN MATCH_MP_TAC HAS_INTEGRAL_SPIKE_SET_EQ THEN
  4489   ASM_MESON_TAC[]);;
  4490 
  4491 lemma has_integral_interior:
  4492  "\<forall>f:real^M->real^N y s.
  4493         negligible(frontier s)
  4494         \<longrightarrow> ((f has_integral y) (interior s) \<longleftrightarrow> (f has_integral y) s)"
  4495 qed  REPEAT STRIP_TAC THEN MATCH_MP_TAC HAS_INTEGRAL_SPIKE_SET_EQ THEN
  4496   FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP (REWRITE_RULE[IMP_CONJ]
  4497     NEGLIGIBLE_SUBSET)) THEN
  4498   REWRITE_TAC[frontier] THEN
  4499   MP_TAC(ISPEC `s:real^M->bool` INTERIOR_SUBSET) THEN
  4500   MP_TAC(ISPEC `s:real^M->bool` CLOSURE_SUBSET) THEN
  4501   SET_TAC[]);;
  4502 
  4503 lemma has_integral_closure:
  4504  "\<forall>f:real^M->real^N y s.
  4505         negligible(frontier s)
  4506         \<longrightarrow> ((f has_integral y) (closure s) \<longleftrightarrow> (f has_integral y) s)"
  4507 qed  REPEAT STRIP_TAC THEN MATCH_MP_TAC HAS_INTEGRAL_SPIKE_SET_EQ THEN
  4508   FIRST_X_ASSUM(MATCH_MP_TAC o MATCH_MP (REWRITE_RULE[IMP_CONJ]
  4509     NEGLIGIBLE_SUBSET)) THEN
  4510   REWRITE_TAC[frontier] THEN
  4511   MP_TAC(ISPEC `s:real^M->bool` INTERIOR_SUBSET) THEN
  4512   MP_TAC(ISPEC `s:real^M->bool` CLOSURE_SUBSET) THEN
  4513   SET_TAC[]);;*)
  4514 
  4515 subsection {* More lemmas that are useful later. *}
  4516 
  4517 lemma has_integral_subset_component_le: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'm::ordered_euclidean_space"
  4518   assumes "s \<subseteq> t" "(f has_integral i) s" "(f has_integral j) t" "\<forall>x\<in>t. 0 \<le> f(x)$$k"
  4519   shows "i$$k \<le> j$$k"
  4520 proof- note has_integral_restrict_univ[THEN sym, of f]
  4521   note assms(2-3)[unfolded this] note * = has_integral_component_le[OF this]
  4522   show ?thesis apply(rule *) using assms(1,4) by auto qed
  4523 
  4524 lemma has_integral_subset_le: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real"
  4525   assumes "s \<subseteq> t" "(f has_integral i) s" "(f has_integral j) t" "\<forall>x\<in>t. 0 \<le> f(x)"
  4526   shows "i \<le> j" using has_integral_subset_component_le[OF assms(1), of "f" "i" "j" 0] using assms by auto
  4527 
  4528 lemma integral_subset_component_le: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'm::ordered_euclidean_space"
  4529   assumes "s \<subseteq> t" "f integrable_on s" "f integrable_on t" "\<forall>x \<in> t. 0 \<le> f(x)$$k"
  4530   shows "(integral s f)$$k \<le> (integral t f)$$k"
  4531   apply(rule has_integral_subset_component_le) using assms by auto
  4532 
  4533 lemma integral_subset_le: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real"
  4534   assumes "s \<subseteq> t" "f integrable_on s" "f integrable_on t" "\<forall>x \<in> t. 0 \<le> f(x)"
  4535   shows "(integral s f) \<le> (integral t f)"
  4536   apply(rule has_integral_subset_le) using assms by auto
  4537 
  4538 lemma has_integral_alt': fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4539   shows "(f has_integral i) s \<longleftrightarrow> (\<forall>a b. (\<lambda>x. if x \<in> s then f x else 0) integrable_on {a..b}) \<and>
  4540   (\<forall>e>0. \<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b} \<longrightarrow> norm(integral {a..b} (\<lambda>x. if x \<in> s then f x else 0) - i) < e)" (is "?l = ?r")
  4541 proof assume ?r
  4542   show ?l apply- apply(subst has_integral')
  4543   proof safe case goal1 from `?r`[THEN conjunct2,rule_format,OF this] guess B .. note B=conjunctD2[OF this]
  4544     show ?case apply(rule,rule,rule B,safe)
  4545       apply(rule_tac x="integral {a..b} (\<lambda>x. if x \<in> s then f x else 0)" in exI)
  4546       apply(drule B(2)[rule_format]) using integrable_integral[OF `?r`[THEN conjunct1,rule_format]] by auto
  4547   qed next
  4548   assume ?l note as = this[unfolded has_integral'[of f],rule_format]
  4549   let ?f = "\<lambda>x. if x \<in> s then f x else 0"
  4550   show ?r proof safe fix a b::"'n::ordered_euclidean_space"
  4551     from as[OF zero_less_one] guess B .. note B=conjunctD2[OF this,rule_format]
  4552     let ?a = "(\<chi>\<chi> i. min (a$$i) (-B))::'n::ordered_euclidean_space" and ?b = "(\<chi>\<chi> i. max (b$$i) B)::'n::ordered_euclidean_space"
  4553     show "?f integrable_on {a..b}" apply(rule integrable_subinterval[of _ ?a ?b])
  4554     proof- have "ball 0 B \<subseteq> {?a..?b}" apply safe unfolding mem_ball mem_interval dist_norm
  4555       proof case goal1 thus ?case using component_le_norm[of x i] by(auto simp add:field_simps) qed
  4556       from B(2)[OF this] guess z .. note conjunct1[OF this]
  4557       thus "?f integrable_on {?a..?b}" unfolding integrable_on_def by auto
  4558       show "{a..b} \<subseteq> {?a..?b}" apply safe unfolding mem_interval apply(rule,erule_tac x=i in allE) by auto qed
  4559 
  4560     fix e::real assume "e>0" from as[OF this] guess B .. note B=conjunctD2[OF this,rule_format]
  4561     show "\<exists>B>0. \<forall>a b. ball 0 B \<subseteq> {a..b} \<longrightarrow>
  4562                     norm (integral {a..b} (\<lambda>x. if x \<in> s then f x else 0) - i) < e"
  4563     proof(rule,rule,rule B,safe) case goal1 from B(2)[OF this] guess z .. note z=conjunctD2[OF this]
  4564       from integral_unique[OF this(1)] show ?case using z(2) by auto qed qed qed 
  4565 
  4566 
  4567 subsection {* Continuity of the integral (for a 1-dimensional interval). *}
  4568 
  4569 lemma integrable_alt: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach" shows 
  4570   "f integrable_on s \<longleftrightarrow>
  4571           (\<forall>a b. (\<lambda>x. if x \<in> s then f x else 0) integrable_on {a..b}) \<and>
  4572           (\<forall>e>0. \<exists>B>0. \<forall>a b c d. ball 0 B \<subseteq> {a..b} \<and> ball 0 B \<subseteq> {c..d}
  4573   \<longrightarrow> norm(integral {a..b} (\<lambda>x. if x \<in> s then f x else 0) -
  4574           integral {c..d}  (\<lambda>x. if x \<in> s then f x else 0)) < e)" (is "?l = ?r")
  4575 proof assume ?l then guess y unfolding integrable_on_def .. note this[unfolded has_integral_alt'[of f]]
  4576   note y=conjunctD2[OF this,rule_format] show ?r apply safe apply(rule y)
  4577   proof- case goal1 hence "e/2 > 0" by auto from y(2)[OF this] guess B .. note B=conjunctD2[OF this,rule_format]
  4578     show ?case apply(rule,rule,rule B)
  4579     proof safe case goal1 show ?case apply(rule norm_triangle_half_l)
  4580         using B(2)[OF goal1(1)] B(2)[OF goal1(2)] by auto qed qed
  4581         
  4582 next assume ?r note as = conjunctD2[OF this,rule_format]
  4583   have "Cauchy (\<lambda>n. integral ({(\<chi>\<chi> i. - real n)::'n .. (\<chi>\<chi> i. real n)}) (\<lambda>x. if x \<in> s then f x else 0))"
  4584   proof(unfold Cauchy_def,safe) case goal1
  4585     from as(2)[OF this] guess B .. note B = conjunctD2[OF this,rule_format]
  4586     from real_arch_simple[of B] guess N .. note N = this
  4587     { fix n assume n:"n \<ge> N" have "ball 0 B \<subseteq> {(\<chi>\<chi> i. - real n)::'n..\<chi>\<chi> i. real n}" apply safe
  4588         unfolding mem_ball mem_interval dist_norm
  4589       proof case goal1 thus ?case using component_le_norm[of x i]
  4590           using n N by(auto simp add:field_simps) qed }
  4591     thus ?case apply-apply(rule_tac x=N in exI) apply safe unfolding dist_norm apply(rule B(2)) by auto
  4592   qed from this[unfolded convergent_eq_cauchy[THEN sym]] guess i ..
  4593   note i = this[THEN LIMSEQ_D]
  4594 
  4595   show ?l unfolding integrable_on_def has_integral_alt'[of f] apply(rule_tac x=i in exI)
  4596     apply safe apply(rule as(1)[unfolded integrable_on_def])
  4597   proof- case goal1 hence *:"e/2 > 0" by auto
  4598     from i[OF this] guess N .. note N =this[rule_format]
  4599     from as(2)[OF *] guess B .. note B=conjunctD2[OF this,rule_format] let ?B = "max (real N) B"
  4600     show ?case apply(rule_tac x="?B" in exI)
  4601     proof safe show "0 < ?B" using B(1) by auto
  4602       fix a b assume ab:"ball 0 ?B \<subseteq> {a..b::'n::ordered_euclidean_space}"
  4603       from real_arch_simple[of ?B] guess n .. note n=this
  4604       show "norm (integral {a..b} (\<lambda>x. if x \<in> s then f x else 0) - i) < e"
  4605         apply(rule norm_triangle_half_l) apply(rule B(2)) defer apply(subst norm_minus_commute)
  4606         apply(rule N[of n])
  4607       proof safe show "N \<le> n" using n by auto
  4608         fix x::"'n::ordered_euclidean_space" assume x:"x \<in> ball 0 B" hence "x\<in> ball 0 ?B" by auto
  4609         thus "x\<in>{a..b}" using ab by blast 
  4610         show "x\<in>{\<chi>\<chi> i. - real n..\<chi>\<chi> i. real n}" using x unfolding mem_interval mem_ball dist_norm apply-
  4611         proof case goal1 thus ?case using component_le_norm[of x i]
  4612             using n by(auto simp add:field_simps) qed qed qed qed qed
  4613 
  4614 lemma integrable_altD: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4615   assumes "f integrable_on s"
  4616   shows "\<And>a b. (\<lambda>x. if x \<in> s then f x else 0) integrable_on {a..b}"
  4617   "\<And>e. e>0 \<Longrightarrow> \<exists>B>0. \<forall>a b c d. ball 0 B \<subseteq> {a..b} \<and> ball 0 B \<subseteq> {c..d}
  4618   \<longrightarrow> norm(integral {a..b} (\<lambda>x. if x \<in> s then f x else 0) - integral {c..d}  (\<lambda>x. if x \<in> s then f x else 0)) < e"
  4619   using assms[unfolded integrable_alt[of f]] by auto
  4620 
  4621 lemma integrable_on_subinterval: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4622   assumes "f integrable_on s" "{a..b} \<subseteq> s" shows "f integrable_on {a..b}"
  4623   apply(rule integrable_eq) defer apply(rule integrable_altD(1)[OF assms(1)])
  4624   using assms(2) by auto
  4625 
  4626 subsection {* A straddling criterion for integrability. *}
  4627 
  4628 lemma integrable_straddle_interval: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real"
  4629   assumes "\<forall>e>0. \<exists>g  h i j. (g has_integral i) ({a..b}) \<and> (h has_integral j) ({a..b}) \<and>
  4630   norm(i - j) < e \<and> (\<forall>x\<in>{a..b}. (g x) \<le> (f x) \<and> (f x) \<le>(h x))"
  4631   shows "f integrable_on {a..b}"
  4632 proof(subst integrable_cauchy,safe)
  4633   case goal1 hence e:"e/3 > 0" by auto note assms[rule_format,OF this]
  4634   then guess g h i j apply- by(erule exE conjE)+ note obt = this
  4635   from obt(1)[unfolded has_integral[of g], rule_format, OF e] guess d1 .. note d1=conjunctD2[OF this,rule_format]
  4636   from obt(2)[unfolded has_integral[of h], rule_format, OF e] guess d2 .. note d2=conjunctD2[OF this,rule_format]
  4637   show ?case apply(rule_tac x="\<lambda>x. d1 x \<inter> d2 x" in exI) apply(rule conjI gauge_inter d1 d2)+ unfolding fine_inter
  4638   proof safe have **:"\<And>i j g1 g2 h1 h2 f1 f2. g1 - h2 \<le> f1 - f2 \<Longrightarrow> f1 - f2 \<le> h1 - g2 \<Longrightarrow>
  4639       abs(i - j) < e / 3 \<Longrightarrow> abs(g2 - i) < e / 3 \<Longrightarrow>  abs(g1 - i) < e / 3 \<Longrightarrow> 
  4640       abs(h2 - j) < e / 3 \<Longrightarrow> abs(h1 - j) < e / 3 \<Longrightarrow> abs(f1 - f2) < e" using `e>0` by arith
  4641     case goal1 note tagged_division_ofD(2-4) note * = this[OF goal1(1)] this[OF goal1(4)]
  4642 
  4643     have "(\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p1. content k *\<^sub>R g x) \<ge> 0"
  4644       "0 \<le> (\<Sum>(x, k)\<in>p2. content k *\<^sub>R h x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x)" 
  4645       "(\<Sum>(x, k)\<in>p2. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p2. content k *\<^sub>R g x) \<ge> 0"
  4646       "0 \<le> (\<Sum>(x, k)\<in>p1. content k *\<^sub>R h x) - (\<Sum>(x, k)\<in>p1. content k *\<^sub>R f x)" 
  4647       unfolding setsum_subtractf[THEN sym] apply- apply(rule_tac[!] setsum_nonneg)
  4648       apply safe unfolding real_scaleR_def right_diff_distrib[THEN sym]
  4649       apply(rule_tac[!] mult_nonneg_nonneg)
  4650     proof- fix a b assume ab:"(a,b) \<in> p1"
  4651       show "0 \<le> content b" using *(3)[OF ab] apply safe using content_pos_le . thus "0 \<le> content b" .
  4652       show "0 \<le> f a - g a" "0 \<le> h a - f a" using *(1-2)[OF ab] using obt(4)[rule_format,of a] by auto
  4653     next fix a b assume ab:"(a,b) \<in> p2"
  4654       show "0 \<le> content b" using *(6)[OF ab] apply safe using content_pos_le . thus "0 \<le> content b" .
  4655       show "0 \<le> f a - g a" "0 \<le> h a - f a" using *(4-5)[OF ab] using obt(4)[rule_format,of a] by auto qed 
  4656 
  4657     thus ?case apply- unfolding real_norm_def apply(rule **) defer defer
  4658       unfolding real_norm_def[THEN sym] apply(rule obt(3))
  4659       apply(rule d1(2)[OF conjI[OF goal1(4,5)]])
  4660       apply(rule d1(2)[OF conjI[OF goal1(1,2)]])
  4661       apply(rule d2(2)[OF conjI[OF goal1(4,6)]])
  4662       apply(rule d2(2)[OF conjI[OF goal1(1,3)]]) by auto qed qed 
  4663      
  4664 lemma integrable_straddle: fixes f::"'n::ordered_euclidean_space \<Rightarrow> real"
  4665   assumes "\<forall>e>0. \<exists>g h i j. (g has_integral i) s \<and> (h has_integral j) s \<and>
  4666   norm(i - j) < e \<and> (\<forall>x\<in>s. (g x) \<le>(f x) \<and>(f x) \<le>(h x))"
  4667   shows "f integrable_on s"
  4668 proof- have "\<And>a b. (\<lambda>x. if x \<in> s then f x else 0) integrable_on {a..b}"
  4669   proof(rule integrable_straddle_interval,safe) case goal1 hence *:"e/4 > 0" by auto
  4670     from assms[rule_format,OF this] guess g h i j apply-by(erule exE conjE)+ note obt=this
  4671     note obt(1)[unfolded has_integral_alt'[of g]] note conjunctD2[OF this, rule_format]
  4672     note g = this(1) and this(2)[OF *] from this(2) guess B1 .. note B1 = conjunctD2[OF this,rule_format]
  4673     note obt(2)[unfolded has_integral_alt'[of h]] note conjunctD2[OF this, rule_format]
  4674     note h = this(1) and this(2)[OF *] from this(2) guess B2 .. note B2 = conjunctD2[OF this,rule_format]
  4675     def c \<equiv> "(\<chi>\<chi> i. min (a$$i) (- (max B1 B2)))::'n" and d \<equiv> "(\<chi>\<chi> i. max (b$$i) (max B1 B2))::'n"
  4676     have *:"ball 0 B1 \<subseteq> {c..d}" "ball 0 B2 \<subseteq> {c..d}" apply safe unfolding mem_ball mem_interval dist_norm
  4677     proof(rule_tac[!] allI)
  4678       case goal1 thus ?case using component_le_norm[of x i] unfolding c_def d_def by auto next
  4679       case goal2 thus ?case using component_le_norm[of x i] unfolding c_def d_def by auto qed
  4680     have **:"\<And>ch cg ag ah::real. norm(ah - ag) \<le> norm(ch - cg) \<Longrightarrow> norm(cg - i) < e / 4 \<Longrightarrow>
  4681       norm(ch - j) < e / 4 \<Longrightarrow> norm(ag - ah) < e"
  4682       using obt(3) unfolding real_norm_def by arith 
  4683     show ?case apply(rule_tac x="\<lambda>x. if x \<in> s then g x else 0" in exI)
  4684                apply(rule_tac x="\<lambda>x. if x \<in> s then h x else 0" in exI)
  4685       apply(rule_tac x="integral {a..b} (\<lambda>x. if x \<in> s then g x else 0)" in exI)
  4686       apply(rule_tac x="integral {a..b} (\<lambda>x. if x \<in> s then h x else 0)" in exI)
  4687       apply safe apply(rule_tac[1-2] integrable_integral,rule g,rule h)
  4688       apply(rule **[OF _ B1(2)[OF *(1)] B2(2)[OF *(2)]])
  4689     proof- have *:"\<And>x f g. (if x \<in> s then f x else 0) - (if x \<in> s then g x else 0) =
  4690         (if x \<in> s then f x - g x else (0::real))" by auto
  4691       note ** = abs_of_nonneg[OF integral_nonneg[OF integrable_sub, OF h g]]
  4692       show " norm (integral {a..b} (\<lambda>x. if x \<in> s then h x else 0) -
  4693                    integral {a..b} (\<lambda>x. if x \<in> s then g x else 0))
  4694            \<le> norm (integral {c..d} (\<lambda>x. if x \<in> s then h x else 0) -
  4695                    integral {c..d} (\<lambda>x. if x \<in> s then g x else 0))"
  4696         unfolding integral_sub[OF h g,THEN sym] real_norm_def apply(subst **) defer apply(subst **) defer
  4697         apply(rule has_integral_subset_le) defer apply(rule integrable_integral integrable_sub h g)+
  4698       proof safe fix x assume "x\<in>{a..b}" thus "x\<in>{c..d}" unfolding mem_interval c_def d_def
  4699           apply - apply rule apply(erule_tac x=i in allE) by auto
  4700       qed(insert obt(4), auto) qed(insert obt(4), auto) qed note interv = this
  4701 
  4702   show ?thesis unfolding integrable_alt[of f] apply safe apply(rule interv)
  4703   proof- case goal1 hence *:"e/3 > 0" by auto
  4704     from assms[rule_format,OF this] guess g h i j apply-by(erule exE conjE)+ note obt=this
  4705     note obt(1)[unfolded has_integral_alt'[of g]] note conjunctD2[OF this, rule_format]
  4706     note g = this(1) and this(2)[OF *] from this(2) guess B1 .. note B1 = conjunctD2[OF this,rule_format]
  4707     note obt(2)[unfolded has_integral_alt'[of h]] note conjunctD2[OF this, rule_format]
  4708     note h = this(1) and this(2)[OF *] from this(2) guess B2 .. note B2 = conjunctD2[OF this,rule_format]
  4709     show ?case apply(rule_tac x="max B1 B2" in exI) apply safe apply(rule min_max.less_supI1,rule B1)
  4710     proof- fix a b c d::"'n::ordered_euclidean_space" assume as:"ball 0 (max B1 B2) \<subseteq> {a..b}" "ball 0 (max B1 B2) \<subseteq> {c..d}"
  4711       have **:"ball 0 B1 \<subseteq> ball (0::'n::ordered_euclidean_space) (max B1 B2)" "ball 0 B2 \<subseteq> ball (0::'n::ordered_euclidean_space) (max B1 B2)" by auto
  4712       have *:"\<And>ga gc ha hc fa fc::real. abs(ga - i) < e / 3 \<and> abs(gc - i) < e / 3 \<and> abs(ha - j) < e / 3 \<and>
  4713         abs(hc - j) < e / 3 \<and> abs(i - j) < e / 3 \<and> ga \<le> fa \<and> fa \<le> ha \<and> gc \<le> fc \<and> fc \<le> hc\<Longrightarrow> abs(fa - fc) < e" using [[z3_with_extensions]] by smt
  4714       show "norm (integral {a..b} (\<lambda>x. if x \<in> s then f x else 0) - integral {c..d} (\<lambda>x. if x \<in> s then f x else 0)) < e"
  4715         unfolding real_norm_def apply(rule *, safe) unfolding real_norm_def[THEN sym]
  4716         apply(rule B1(2),rule order_trans,rule **,rule as(1)) 
  4717         apply(rule B1(2),rule order_trans,rule **,rule as(2)) 
  4718         apply(rule B2(2),rule order_trans,rule **,rule as(1)) 
  4719         apply(rule B2(2),rule order_trans,rule **,rule as(2)) 
  4720         apply(rule obt) apply(rule_tac[!] integral_le) using obt
  4721         by(auto intro!: h g interv) qed qed qed 
  4722 
  4723 subsection {* Adding integrals over several sets. *}
  4724 
  4725 lemma has_integral_union: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4726   assumes "(f has_integral i) s" "(f has_integral j) t" "negligible(s \<inter> t)"
  4727   shows "(f has_integral (i + j)) (s \<union> t)"
  4728 proof- note * = has_integral_restrict_univ[THEN sym, of f]
  4729   show ?thesis unfolding * apply(rule has_integral_spike[OF assms(3)])
  4730     defer apply(rule has_integral_add[OF assms(1-2)[unfolded *]]) by auto qed
  4731 
  4732 lemma has_integral_unions: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4733   assumes "finite t" "\<forall>s\<in>t. (f has_integral (i s)) s"  "\<forall>s\<in>t. \<forall>s'\<in>t. ~(s = s') \<longrightarrow> negligible(s \<inter> s')"
  4734   shows "(f has_integral (setsum i t)) (\<Union>t)"
  4735 proof- note * = has_integral_restrict_univ[THEN sym, of f]
  4736   have **:"negligible (\<Union>((\<lambda>(a,b). a \<inter> b) ` {(a,b). a \<in> t \<and> b \<in> {y. y \<in> t \<and> ~(a = y)}}))"
  4737     apply(rule negligible_unions) apply(rule finite_imageI) apply(rule finite_subset[of _ "t \<times> t"]) defer 
  4738     apply(rule finite_cartesian_product[OF assms(1,1)]) using assms(3) by auto 
  4739   note assms(2)[unfolded *] note has_integral_setsum[OF assms(1) this]
  4740   thus ?thesis unfolding * apply-apply(rule has_integral_spike[OF **]) defer apply assumption
  4741   proof safe case goal1 thus ?case
  4742     proof(cases "x\<in>\<Union>t") case True then guess s unfolding Union_iff .. note s=this
  4743       hence *:"\<forall>b\<in>t. x \<in> b \<longleftrightarrow> b = s" using goal1(3) by blast
  4744       show ?thesis unfolding if_P[OF True] apply(rule trans) defer
  4745         apply(rule setsum_cong2) apply(subst *, assumption) apply(rule refl)
  4746         unfolding setsum_delta[OF assms(1)] using s by auto qed auto qed qed
  4747 
  4748 subsection {* In particular adding integrals over a division, maybe not of an interval. *}
  4749 
  4750 lemma has_integral_combine_division: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4751   assumes "d division_of s" "\<forall>k\<in>d. (f has_integral (i k)) k"
  4752   shows "(f has_integral (setsum i d)) s"
  4753 proof- note d = division_ofD[OF assms(1)]
  4754   show ?thesis unfolding d(6)[THEN sym] apply(rule has_integral_unions)
  4755     apply(rule d assms)+ apply(rule,rule,rule)
  4756   proof- case goal1 from d(4)[OF this(1)] d(4)[OF this(2)]
  4757     guess a c b d apply-by(erule exE)+ note obt=this
  4758     from d(5)[OF goal1] show ?case unfolding obt interior_closed_interval
  4759       apply-apply(rule negligible_subset[of "({a..b}-{a<..<b}) \<union> ({c..d}-{c<..<d})"])
  4760       apply(rule negligible_union negligible_frontier_interval)+ by auto qed qed
  4761 
  4762 lemma integral_combine_division_bottomup: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4763   assumes "d division_of s" "\<forall>k\<in>d. f integrable_on k"
  4764   shows "integral s f = setsum (\<lambda>i. integral i f) d"
  4765   apply(rule integral_unique) apply(rule has_integral_combine_division[OF assms(1)])
  4766   using assms(2) unfolding has_integral_integral .
  4767 
  4768 lemma has_integral_combine_division_topdown: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4769   assumes "f integrable_on s" "d division_of k" "k \<subseteq> s"
  4770   shows "(f has_integral (setsum (\<lambda>i. integral i f) d)) k"
  4771   apply(rule has_integral_combine_division[OF assms(2)])
  4772   apply safe unfolding has_integral_integral[THEN sym]
  4773 proof- case goal1 from division_ofD(2,4)[OF assms(2) this]
  4774   show ?case apply safe apply(rule integrable_on_subinterval)
  4775     apply(rule assms) using assms(3) by auto qed
  4776 
  4777 lemma integral_combine_division_topdown: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4778   assumes "f integrable_on s" "d division_of s"
  4779   shows "integral s f = setsum (\<lambda>i. integral i f) d"
  4780   apply(rule integral_unique,rule has_integral_combine_division_topdown) using assms by auto
  4781 
  4782 lemma integrable_combine_division: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4783   assumes "d division_of s" "\<forall>i\<in>d. f integrable_on i"
  4784   shows "f integrable_on s"
  4785   using assms(2) unfolding integrable_on_def
  4786   by(metis has_integral_combine_division[OF assms(1)])
  4787 
  4788 lemma integrable_on_subdivision: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4789   assumes "d division_of i" "f integrable_on s" "i \<subseteq> s"
  4790   shows "f integrable_on i"
  4791   apply(rule integrable_combine_division assms)+
  4792 proof safe case goal1 note division_ofD(2,4)[OF assms(1) this]
  4793   thus ?case apply safe apply(rule integrable_on_subinterval[OF assms(2)])
  4794     using assms(3) by auto qed
  4795 
  4796 subsection {* Also tagged divisions. *}
  4797 
  4798 lemma has_integral_combine_tagged_division: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4799   assumes "p tagged_division_of s" "\<forall>(x,k) \<in> p. (f has_integral (i k)) k"
  4800   shows "(f has_integral (setsum (\<lambda>(x,k). i k) p)) s"
  4801 proof- have *:"(f has_integral (setsum (\<lambda>k. integral k f) (snd ` p))) s"
  4802     apply(rule has_integral_combine_division) apply(rule division_of_tagged_division[OF assms(1)])
  4803     using assms(2) unfolding has_integral_integral[THEN sym] by(safe,auto)
  4804   thus ?thesis apply- apply(rule subst[where P="\<lambda>i. (f has_integral i) s"]) defer apply assumption
  4805     apply(rule trans[of _ "setsum (\<lambda>(x,k). integral k f) p"]) apply(subst eq_commute)
  4806     apply(rule setsum_over_tagged_division_lemma[OF assms(1)]) apply(rule integral_null,assumption)
  4807     apply(rule setsum_cong2) using assms(2) by auto qed
  4808 
  4809 lemma integral_combine_tagged_division_bottomup: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4810   assumes "p tagged_division_of {a..b}" "\<forall>(x,k)\<in>p. f integrable_on k"
  4811   shows "integral {a..b} f = setsum (\<lambda>(x,k). integral k f) p"
  4812   apply(rule integral_unique) apply(rule has_integral_combine_tagged_division[OF assms(1)])
  4813   using assms(2) by auto
  4814 
  4815 lemma has_integral_combine_tagged_division_topdown: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4816   assumes "f integrable_on {a..b}" "p tagged_division_of {a..b}"
  4817   shows "(f has_integral (setsum (\<lambda>(x,k). integral k f) p)) {a..b}"
  4818   apply(rule has_integral_combine_tagged_division[OF assms(2)])
  4819 proof safe case goal1 note tagged_division_ofD(3-4)[OF assms(2) this]
  4820   thus ?case using integrable_subinterval[OF assms(1)] by auto qed
  4821 
  4822 lemma integral_combine_tagged_division_topdown: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4823   assumes "f integrable_on {a..b}" "p tagged_division_of {a..b}"
  4824   shows "integral {a..b} f = setsum (\<lambda>(x,k). integral k f) p"
  4825   apply(rule integral_unique,rule has_integral_combine_tagged_division_topdown) using assms by auto
  4826 
  4827 subsection {* Henstock's lemma. *}
  4828 
  4829 lemma henstock_lemma_part1: fixes f::"'n::ordered_euclidean_space \<Rightarrow> 'a::banach"
  4830   assumes "f integrable_on {a..b}" "0 < e" "gauge d"
  4831   "(\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow> norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p - integral({a..b}) f) < e)"
  4832   and p:"p tagged_partial_division_of {a..b}" "d fine p"
  4833   shows "norm(setsum (\<lambda>(x,k). content k *\<^sub>R f x - integral k f) p) \<le> e" (is "?x \<le> e")
  4834 proof-  { presume "\<And>k. 0<k \<Longrightarrow> ?x \<le> e + k" thus ?thesis by (blast intro: field_le_epsilon) }
  4835   fix k::real assume k:"k>0" note p' = tagged_partial_division_ofD[OF p(1)]
  4836   have "\<Union>snd ` p \<subseteq> {a..b}" using p'(3) by fastforce
  4837   note partial_division_of_tagged_division[OF p(1)] this
  4838   from partial_division_extend_interval[OF this] guess q . note q=this and q' = division_ofD[OF this(2)]
  4839   def r \<equiv> "q - snd ` p" have "snd ` p \<inter> r = {}" unfolding r_def by auto
  4840   have r:"finite r" using q' unfolding r_def by auto
  4841 
  4842   have "\<forall>i\<in>r. \<exists>p. p tagged_division_of i \<and> d fine p \<and>
  4843     norm(setsum (\<lambda>(x,j). content j *\<^sub>R f x) p - integral i f) < k / (real (card r) + 1)"
  4844   proof safe case goal1 hence i:"i \<in> q" unfolding r_def by auto
  4845     from q'(4)[OF this] guess u v apply-by(erule exE)+ note uv=this
  4846     have *:"k / (real (card r) + 1) > 0" apply(rule divide_pos_pos,rule k) by auto
  4847     have "f integrable_on {u..v}" apply(rule integrable_subinterval[OF assms(1)])
  4848       using q'(2)[OF i] unfolding uv by auto
  4849     note integrable_integral[OF this, unfolded has_integral[of f]]
  4850     from this[rule_format,OF *] guess dd .. note dd=conjunctD2[OF this,rule_format]
  4851     note gauge_inter[OF `gauge d` dd(1)] from fine_division_exists[OF this,of u v] guess qq .
  4852     thus ?case apply(rule_tac x=qq in exI) using dd(2)[of qq] unfolding fine_inter uv by auto qed
  4853   from bchoice[OF this] guess qq .. note qq=this[rule_format]
  4854 
  4855   let ?p = "p \<union> \<Union>qq ` r" have "norm ((\<Sum>(x, k)\<in>?p. content k *\<^sub>R f x) - integral {a..b} f) < e"
  4856     apply(rule assms(4)[rule_format])
  4857   proof show "d fine ?p" apply(rule fine_union,rule p) apply(rule fine_unions) using qq by auto 
  4858     note * = tagged_partial_division_of_union_self[OF p(1)]
  4859     have "p \<union> \<Union>qq ` r tagged_division_of \<Union>snd ` p \<union> \<Union>r"
  4860     proof(rule tagged_division_union[OF * tagged_division_unions])
  4861       show "finite r" by fact case goal2 thus ?case using qq by auto
  4862     next case goal3 thus ?case apply(rule,rule,rule) apply(rule q'(5)) unfolding r_def by auto
  4863     next case goal4 thus ?case apply(rule inter_interior_unions_intervals) apply(fact,rule)
  4864         apply(rule,rule q') defer apply(rule,subst Int_commute) 
  4865         apply(rule inter_interior_unions_intervals) apply(rule finite_imageI,rule p',rule) defer
  4866         apply(rule,rule q') using q(1) p' unfolding r_def by auto qed
  4867     moreover have "\<Union>snd ` p \<union> \<Union>r = {a..b}" "{qq i |i. i \<in> r} = qq ` r"
  4868       unfolding Union_Un_distrib[THEN sym] r_def using q by auto
  4869     ultimately show "?p tagged_division_of {a..b}" by fastforce qed
  4870 
  4871   hence "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) + (\<Sum>(x, k)\<in>\<Union>qq ` r. content k *\<^sub>R f x) -
  4872     integral {a..b} f) < e" apply(subst setsum_Un_zero[THEN sym]) apply(rule p') prefer 3 
  4873     apply assumption apply rule apply(rule finite_imageI,rule r) apply safe apply(drule qq)
  4874   proof- fix x l k assume as:"(x,l)\<in>p" "(x,l)\<in>qq k" "k\<in>r"
  4875     note qq[OF this(3)] note tagged_division_ofD(3,4)[OF conjunct1[OF this] as(2)]
  4876     from this(2) guess u v apply-by(erule exE)+ note uv=this
  4877     have "l\<in>snd ` p" unfolding image_iff apply(rule_tac x="(x,l)" in bexI) using as by auto
  4878     hence "l\<in>q" "k\<in>q" "l\<noteq>k" using as(1,3) q(1) unfolding r_def by auto
  4879     note q'(5)[OF this] hence "interior l = {}" using interior_mono[OF `l \<subseteq> k`] by blast
  4880     thus "content l *\<^sub>R f x = 0" unfolding uv content_eq_0_interior[THEN sym] by auto qed auto
  4881 
  4882   hence "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) + setsum (setsum (\<lambda>(x, k). content k *\<^sub>R f x))
  4883     (qq ` r) - integral {a..b} f) < e" apply(subst(asm) setsum_UNION_zero)
  4884     prefer 4 apply assumption apply(rule finite_imageI,fact)
  4885     unfolding split_paired_all split_conv image_iff defer apply(erule bexE)+
  4886   proof- fix x m k l T1 T2 assume "(x,m)\<in>T1" "(x,m)\<in>T2" "T1\<noteq>T2" "k\<in>r" "l\<in>r" "T1 = qq k" "T2 = qq l"
  4887     note as = this(1-5)[unfolded this(6-)] note kl = tagged_division_ofD(3,4)[OF qq[THEN conjunct1]]
  4888     from this(2)[OF as(4,1)] guess u v apply-by(erule exE)+ note uv=this
  4889     have *:"interior (k \<inter> l) = {}" unfolding interior_inter apply(rule q')
  4890       using as unfolding r_def by auto
  4891     have "interior m = {}" unfolding subset_empty[THEN sym] unfolding *[THEN sym]
  4892       apply(rule interior_mono) using kl(1)[OF as(4,1)] kl(1)[OF as(5,2)] by auto
  4893     thus "content m *\<^sub>R f x = 0" unfolding uv content_eq_0_interior[THEN sym] by auto 
  4894   qed(insert qq, auto)
  4895 
  4896   hence **:"norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) + setsum (setsum (\<lambda>(x, k). content k *\<^sub>R f x) \<circ> qq) r -
  4897     integral {a..b} f) < e" apply(subst(asm) setsum_reindex_nonzero) apply fact
  4898     apply(rule setsum_0',rule) unfolding split_paired_all split_conv defer apply assumption
  4899   proof- fix k l x m assume as:"k\<in>r" "l\<in>r" "k\<noteq>l" "qq k = qq l" "(x,m)\<in>qq k"
  4900     note tagged_division_ofD(6)[OF qq[THEN conjunct1]] from this[OF as(1)] this[OF as(2)] 
  4901     show "content m *\<^sub>R f x = 0"  using as(3) unfolding as by auto qed
  4902   
  4903   have *:"\<And>ir ip i cr cp. norm((cp + cr) - i) < e \<Longrightarrow> norm(cr - ir) < k \<Longrightarrow> 
  4904     ip + ir = i \<Longrightarrow> norm(cp - ip) \<le> e + k" 
  4905   proof- case goal1 thus ?case  using norm_triangle_le[of "cp + cr - i" "- (cr - ir)"]  
  4906       unfolding goal1(3)[THEN sym] norm_minus_cancel by(auto simp add:algebra_simps) qed
  4907   
  4908   have "?x =  norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R f x) - (\<Sum>(x, k)\<in>p. integral k f))"
  4909     unfolding split_def setsum_subtractf ..
  4910   also have "... \<le> e + k" apply(rule *[OF **, where ir="setsum (\<lambda>k. integral k f) r"])
  4911   proof- case goal2 have *:"(\<Sum>(x, k)\<in>p. integral k f) = (\<Sum>k\<in>snd ` p. integral k f)"
  4912       apply(subst setsum_reindex_nonzero) apply fact
  4913       unfolding split_paired_all snd_conv split_def o_def
  4914     proof- fix x l y m assume as:"(x,l)\<in>p" "(y,m)\<in>p" "(x,l)\<noteq>(y,m)" "l = m"
  4915       from p'(4)[OF as(1)] guess u v apply-by(erule exE)+ note uv=this
  4916       show "integral l f = 0" unfolding uv apply(rule integral_unique)
  4917         apply(rule has_integral_null) unfolding content_eq_0_interior
  4918         using p'(5)[OF as(1-3)] unfolding uv as(4)[THEN sym] by auto
  4919     qed auto 
  4920     show ?case unfolding integral_combine_division_topdown[OF assms(1) q(2)] * r_def
  4921       apply(rule setsum_Un_disjoint'[THEN sym]) using q(1) q'(1) p'(1) by auto
  4922   next  case goal1 have *:"k * real (card r) / (1 + real (card r)) < k" using k by(auto simp add:field_simps)
  4923     show ?case apply(rule le_less_trans[of _ "setsum (\<lambda>x. k / (real (card r) + 1)) r"])
  4924       unfolding setsum_subtractf[THEN sym] apply(rule setsum_norm_le)
  4925       apply rule apply(drule qq) defer unfolding divide_inverse setsum_left_distrib[THEN sym]
  4926       unfolding divide_inverse[THEN sym] using * by(auto simp add:field_simps real_eq_of_nat)
  4927   qed finally show "?x \<le> e + k" . qed
  4928 
  4929 lemma henstock_lemma_part2: fixes f::"'m::ordered_euclidean_space \<Rightarrow> 'n::ordered_euclidean_space"
  4930   assumes "f integrable_on {a..b}" "0 < e" "gauge d"
  4931   "\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow> norm (setsum (\<lambda>(x,k). content k *\<^sub>R f x) p -
  4932           integral({a..b}) f) < e"    "p tagged_partial_division_of {a..b}" "d fine p"
  4933   shows "setsum (\<lambda>(x,k). norm(content k *\<^sub>R f x - integral k f)) p \<le> 2 * real (DIM('n)) * e"
  4934   unfolding split_def apply(rule setsum_norm_allsubsets_bound) defer 
  4935   apply(rule henstock_lemma_part1[unfolded split_def,OF assms(1-3)])
  4936   apply safe apply(rule assms[rule_format,unfolded split_def]) defer
  4937   apply(rule tagged_partial_division_subset,rule assms,assumption)
  4938   apply(rule fine_subset,assumption,rule assms) using assms(5) by auto
  4939   
  4940 lemma henstock_lemma: fixes f::"'m::ordered_euclidean_space \<Rightarrow> 'n::ordered_euclidean_space"
  4941   assumes "f integrable_on {a..b}" "e>0"
  4942   obtains d where "gauge d"
  4943   "\<forall>p. p tagged_partial_division_of {a..b} \<and> d fine p
  4944   \<longrightarrow> setsum (\<lambda>(x,k). norm(content k *\<^sub>R f x - integral k f)) p < e"
  4945 proof- have *:"e / (2 * (real DIM('n) + 1)) > 0" apply(rule divide_pos_pos) using assms(2) by auto
  4946   from integrable_integral[OF assms(1),unfolded has_integral[of f],rule_format,OF this]
  4947   guess d .. note d = conjunctD2[OF this] show thesis apply(rule that,rule d)
  4948   proof safe case goal1 note * = henstock_lemma_part2[OF assms(1) * d this]
  4949     show ?case apply(rule le_less_trans[OF *]) using `e>0` by(auto simp add:field_simps) qed qed
  4950 
  4951 subsection {* Geometric progression *}
  4952 
  4953 text {* FIXME: Should one or more of these theorems be moved to @{file
  4954 "~~/src/HOL/Set_Interval.thy"}, alongside @{text geometric_sum}? *}
  4955 
  4956 lemma sum_gp_basic:
  4957   fixes x :: "'a::ring_1"
  4958   shows "(1 - x) * setsum (\<lambda>i. x^i) {0 .. n} = (1 - x^(Suc n))"
  4959 proof-
  4960   def y \<equiv> "1 - x"
  4961   have "y * (\<Sum>i=0..n. (1 - y) ^ i) = 1 - (1 - y) ^ Suc n"
  4962     by (induct n, simp, simp add: algebra_simps)
  4963   thus ?thesis
  4964     unfolding y_def by simp
  4965 qed
  4966 
  4967 lemma sum_gp_multiplied: assumes mn: "m <= n"
  4968   shows "((1::'a::{field}) - x) * setsum (op ^ x) {m..n} = x^m - x^ Suc n"
  4969   (is "?lhs = ?rhs")
  4970 proof-
  4971   let ?S = "{0..(n - m)}"
  4972   from mn have mn': "n - m \<ge> 0" by arith
  4973   let ?f = "op + m"
  4974   have i: "inj_on ?f ?S" unfolding inj_on_def by auto
  4975   have f: "?f ` ?S = {m..n}"
  4976     using mn apply (auto simp add: image_iff Bex_def) by arith
  4977   have th: "op ^ x o op + m = (\<lambda>i. x^m * x^i)"
  4978     by (rule ext, simp add: power_add power_mult)
  4979   from setsum_reindex[OF i, of "op ^ x", unfolded f th setsum_right_distrib[symmetric]]
  4980   have "?lhs = x^m * ((1 - x) * setsum (op ^ x) {0..n - m})" by simp
  4981   then show ?thesis unfolding sum_gp_basic using mn
  4982     by (simp add: field_simps power_add[symmetric])
  4983 qed
  4984 
  4985 lemma sum_gp: "setsum (op ^ (x::'a::{field})) {m .. n} =
  4986    (if n < m then 0 else if x = 1 then of_nat ((n + 1) - m)
  4987                     else (x^ m - x^ (Suc n)) / (1 - x))"
  4988 proof-
  4989   {assume nm: "n < m" hence ?thesis by simp}
  4990   moreover
  4991   {assume "\<not> n < m" hence nm: "m \<le> n" by arith
  4992     {assume x: "x = 1"  hence ?thesis by simp}
  4993     moreover
  4994     {assume x: "x \<noteq> 1" hence nz: "1 - x \<noteq> 0" by simp
  4995       from sum_gp_multiplied[OF nm, of x] nz have ?thesis by (simp add: field_simps)}
  4996     ultimately have ?thesis by metis
  4997   }
  4998   ultimately show ?thesis by metis
  4999 qed
  5000 
  5001 lemma sum_gp_offset: "setsum (op ^ (x::'a::{field})) {m .. m+n} =
  5002   (if x = 1 then of_nat n + 1 else x^m * (1 - x^Suc n) / (1 - x))"
  5003   unfolding sum_gp[of x m "m + n"] power_Suc
  5004   by (simp add: field_simps power_add)
  5005 
  5006 subsection {* monotone convergence (bounded interval first). *}
  5007 
  5008 lemma monotone_convergence_interval: fixes f::"nat \<Rightarrow> 'n::ordered_euclidean_space \<Rightarrow> real"
  5009   assumes "\<forall>k. (f k) integrable_on {a..b}"
  5010   "\<forall>k. \<forall>x\<in>{a..b}.(f k x) \<le> (f (Suc k) x)"
  5011   "\<forall>x\<in>{a..b}. ((\<lambda>k. f k x) ---> g x) sequentially"
  5012   "bounded {integral {a..b} (f k) | k . k \<in> UNIV}"
  5013   shows "g integrable_on {a..b} \<and> ((\<lambda>k. integral ({a..b}) (f k)) ---> integral ({a..b}) g) sequentially"
  5014 proof(case_tac[!] "content {a..b} = 0") assume as:"content {a..b} = 0"
  5015   show ?thesis using integrable_on_null[OF as] unfolding integral_null[OF as] using tendsto_const by auto
  5016 next assume ab:"content {a..b} \<noteq> 0"
  5017   have fg:"\<forall>x\<in>{a..b}. \<forall> k. (f k x) $$ 0 \<le> (g x) $$ 0"
  5018   proof safe case goal1 note assms(3)[rule_format,OF this]
  5019     note * = Lim_component_ge[OF this trivial_limit_sequentially]
  5020     show ?case apply(rule *) unfolding eventually_sequentially
  5021       apply(rule_tac x=k in exI) apply- apply(rule transitive_stepwise_le)
  5022       using assms(2)[rule_format,OF goal1] by auto qed
  5023   have "\<exists>i. ((\<lambda>k. integral ({a..b}) (f k)) ---> i) sequentially"
  5024     apply(rule bounded_increasing_convergent) defer
  5025     apply rule apply(rule integral_le) apply safe
  5026     apply(rule assms(1-2)[rule_format])+ using assms(4) by auto
  5027   then guess i .. note i=this
  5028   have i':"\<And>k. (integral({a..b}) (f k)) \<le> i$$0"
  5029     apply(rule Lim_component_ge,rule i) apply(rule trivial_limit_sequentially)
  5030     unfolding eventually_sequentially apply(rule_tac x=k in exI)
  5031     apply(rule transitive_stepwise_le) prefer 3 unfolding Eucl_real_simps apply(rule integral_le)
  5032     apply(rule assms(1-2)[rule_format])+ using assms(2) by auto
  5033 
  5034   have "(g has_integral i) {a..b}" unfolding has_integral
  5035   proof safe case goal1 note e=this
  5036     hence "\<forall>k. (\<exists>d. gauge d \<and> (\<forall>p. p tagged_division_of {a..b} \<and> d fine p \<longrightarrow>
  5037              norm ((\<Sum>(x, ka)\<in>p. content ka *\<^sub>R f k x) - integral {a..b} (f k)) < e / 2 ^ (k + 2)))"
  5038       apply-apply(rule,rule assms(1)[unfolded has_integral_integral has_integral,rule_format])
  5039       apply(rule divide_pos_pos) by auto
  5040     from choice[OF this] guess c .. note c=conjunctD2[OF this[rule_format],rule_format]
  5041 
  5042     have "\<exists>r. \<forall>k\<ge>r. 0 \<le> i$$0 - (integral {a..b} (f k)) \<and> i$$0 - (integral {a..b} (f k)) < e / 4"
  5043     proof- case goal1 have "e/4 > 0" using e by auto
  5044       from LIMSEQ_D [OF i this] guess r ..
  5045       thus ?case apply(rule_tac x=r in exI) apply rule
  5046         apply(erule_tac x=k in allE)
  5047       proof- case goal1 thus ?case using i'[of k] by auto qed qed
  5048     then guess r .. note r=conjunctD2[OF this[rule_format]]
  5049 
  5050     have "\<forall>x\<in>{a..b}. \<exists>n\<ge>r. \<forall>k\<ge>n. 0 \<le> (g x)$$0 - (f k x)$$0 \<and>
  5051            (g x)$$0 - (f k x)$$0 < e / (4 * content({a..b}))"
  5052     proof case goal1 have "e / (4 * content {a..b}) > 0" apply(rule divide_pos_pos,fact)
  5053         using ab content_pos_le[of a b] by auto
  5054       from assms(3)[rule_format, OF goal1, THEN LIMSEQ_D, OF this]
  5055       guess n .. note n=this
  5056       thus ?case apply(rule_tac x="n + r" in exI) apply safe apply(erule_tac[2-3] x=k in allE)
  5057         unfolding dist_real_def using fg[rule_format,OF goal1] by(auto simp add:field_simps) qed
  5058     from bchoice[OF this] guess m .. note m=conjunctD2[OF this[rule_format],rule_format]
  5059     def d \<equiv> "\<lambda>x. c (m x) x" 
  5060 
  5061     show ?case apply(rule_tac x=d in exI)
  5062     proof safe show "gauge d" using c(1) unfolding gauge_def d_def by auto
  5063     next fix p assume p:"p tagged_division_of {a..b}" "d fine p"
  5064       note p'=tagged_division_ofD[OF p(1)]
  5065       have "\<exists>a. \<forall>x\<in>p. m (fst x) \<le> a"
  5066         by (metis finite_imageI finite_nat_set_iff_bounded_le p'(1) rev_image_eqI)
  5067       then guess s .. note s=this
  5068       have *:"\<forall>a b c d. norm(a - b) \<le> e / 4 \<and> norm(b - c) < e / 2 \<and>
  5069             norm(c - d) < e / 4 \<longrightarrow> norm(a - d) < e" 
  5070       proof safe case goal1 thus ?case using norm_triangle_lt[of "a - b" "b - c" "3* e/4"]
  5071           norm_triangle_lt[of "a - b + (b - c)" "c - d" e] unfolding norm_minus_cancel
  5072           by(auto simp add:algebra_simps) qed
  5073       show "norm ((\<Sum>(x, k)\<in>p. content k *\<^sub>R g x) - i) < e" apply(rule *[rule_format,where
  5074           b="\<Sum>(x, k)\<in>p. content k *\<^sub>R f (m x) x" and c="\<Sum>(x, k)\<in>p. integral k (f (m x))"])
  5075       proof safe case goal1
  5076          show ?case apply(rule order_trans[of _ "\<Sum>(x, k)\<in>p. content k * (e / (4 * content {a..b}))"])
  5077            unfolding setsum_subtractf[THEN sym] apply(rule order_trans,rule norm_setsum)
  5078            apply(rule setsum_mono) unfolding split_paired_all split_conv
  5079            unfolding split_def setsum_left_distrib[THEN sym] scaleR_diff_right[THEN sym]
  5080            unfolding additive_content_tagged_division[OF p(1), unfolded split_def]
  5081          proof- fix x k assume xk:"(x,k) \<in> p" hence x:"x\<in>{a..b}" using p'(2-3)[OF xk] by auto
  5082            from p'(4)[OF xk] guess u v apply-by(erule exE)+ note uv=this
  5083            show " norm (content k *\<^sub>R (g x - f (m x) x)) \<le> content k * (e / (4 * content {a..b}))"
  5084              unfolding norm_scaleR uv unfolding abs_of_nonneg[OF content_pos_le] 
  5085              apply(rule mult_left_mono) using m(2)[OF x,of "m x"] by auto
  5086          qed(insert ab,auto)
  5087          
  5088        next case goal2 show ?case apply(rule le_less_trans[of _ "norm (\<Sum>j = 0..s.
  5089            \<Sum>(x, k)\<in>{xk\<in>p. m (fst xk) = j}. content k *\<^sub>R f (m x) x - integral k (f (m x)))"])
  5090            apply(subst setsum_group) apply fact apply(rule finite_atLeastAtMost) defer
  5091            apply(subst split_def)+ unfolding setsum_subtractf apply rule
  5092          proof- show "norm (\<Sum>j = 0..s. \<Sum>(x, k)\<in>{xk \<in> p.
  5093              m (fst xk) = j}. content k *\<^sub>R f (m x) x - integral k (f (m x))) < e / 2"
  5094              apply(rule le_less_trans[of _ "setsum (\<lambda>i. e / 2^(i+2)) {0..s}"])
  5095              apply(rule setsum_norm_le)
  5096            proof show "(\<Sum>i = 0..s. e / 2 ^ (i + 2)) < e / 2"
  5097                unfolding power_add divide_inverse inverse_mult_distrib
  5098                unfolding setsum_right_distrib[THEN sym] setsum_left_distrib[THEN sym]
  5099                unfolding power_inverse sum_gp apply(rule mult_strict_left_mono[OF _ e])
  5100                unfolding power2_eq_square by auto
  5101              fix t assume "t\<in>{0..s}"
  5102              show "norm (\<Sum>(x, k)\<in>{xk \<in> p. m (fst xk) = t}. content k *\<^sub>R f (m x) x -
  5103                integral k (f (m x))) \<le> e / 2 ^ (t + 2)"apply(rule order_trans[of _
  5104                "norm(setsum (\<lambda>(x,k). content k *\<^sub>R f t x - integral k (f t)) {xk \<in> p. m (fst xk) = t})"])
  5105                apply(rule eq_refl) apply(rule arg_cong[where f=norm]) apply(rule setsum_cong2) defer
  5106                apply(rule henstock_lemma_part1) apply(rule assms(1)[rule_format])