
theorem Th41:
for f be PartFunc of REAL,REAL, a,b be Real, A be non empty Subset of REAL
 st [.a,b.[ c= dom f & A = [.a,b.[ & f is_right_improper_integrable_on a,b
 & f|A is nonnegative
 holds right_improper_integral(f,a,b) = Integral(L-Meas,f|A)
  & (f is_right_ext_Riemann_integrable_on a,b
      implies f|A is_integrable_on L-Meas)
  & (not f is_right_ext_Riemann_integrable_on a,b
      implies Integral(L-Meas,f|A) = +infty)
proof
    let f be PartFunc of REAL,REAL, a,b be Real, A be non empty Subset of REAL;
    assume that
A1:  [.a,b.[ c= dom f and
A2:  A = [.a,b.[ and
A3:  f is_right_improper_integrable_on a,b and
A4:  f|A is nonnegative;

    reconsider A1 = A as Element of L-Field by A2,MEASUR10:5,MEASUR12:75;

A5: a < b by A2,XXREAL_1:27;

    per cases;
    suppose
A6:  f is_right_ext_Riemann_integrable_on a,b; then
A7:  right_improper_integral(f,a,b) = ext_right_integral(f,a,b)
       by A3,INTEGR24:39;

     consider Intf be PartFunc of REAL,REAL such that
A8:   dom Intf = [.a,b.[ and
A9:   for x be Real st x in dom Intf holds Intf.x = integral(f,a,x) and
A10:   Intf is_left_convergent_in b and
A11:   ext_right_integral(f,a,b) = lim_left(Intf,b)
        by A6,INTEGR10:def 3;

A12:  for p,q be Real st p in dom Intf & q in dom Intf & p < q holds
      Intf.p <= Intf.q
     proof
      let p,q be Real;
      assume that
A13:    p in dom Intf and
A14:    q in dom Intf and
A15:    p < q;

A16:   a <= q < b by A8,A14,XXREAL_1:3; then
      [.a,q.] c= [.a,b.[ by XXREAL_1:43; then
A17:   [.a,q.] c= dom f by A1;
A18:  [.a,q.] = ['a,q'] by A16,INTEGRA5:def 3;
A19:   a <= p by A8,A13,XXREAL_1:3;
A20:   f is_integrable_on ['a,q'] & f|['a,q'] is bounded
        by A6,A16,INTEGR10:def 1;
      (f|A)|[.a,q.] is nonnegative by A4,MESFUNC6:55; then
A21:   f|[.a,q.] is nonnegative by A16,A2,XXREAL_1:43,RELAT_1:74;
A22:   [.a,p.] c= [.a,q.] by A15,XXREAL_1:34;
      Intf.p = integral(f,a,p) & Intf.q = integral(f,a,q) by A13,A14,A9;
      hence Intf.p <= Intf.q by A17,A20,A21,A22,A19,A18,Th14;
     end; then
A23: Intf is non-decreasing by RFUNCT_2:def 3;

     consider E be SetSequence of L-Field such that
A24:   (for n be Nat holds E.n = [. a, b-(b-a)/(n+1) .] & E.n c= [.a,b.[ &
        E.n is non empty closed_interval Subset of REAL) &
      E is non-descending & E is convergent & Union E = [. a,b .[
        by A2,Th23,XXREAL_1:27;

A25: A1 = dom(f|A1) by A1,A2,RELAT_1:62; then
A26:  A1 = dom(R_EAL(f|A)) by MESFUNC5:def 7;

A27: lim E = Union E by A24,SETLIM_1:80;
A28:  lim E c= A1 by A24,A2,SETLIM_1:80;

     A1 \ lim E = {} by A24,A2,A27,XBOOLE_1:37; then
A29:  L-Meas.(A1 \ lim E) = 0 by VALUED_0:def 19;

A30:  R_EAL f is A1-measurable by A1,A2,A3,Th33,MESFUNC6:def 1;

     A1 = dom f /\ A1 by A25,RELAT_1:61; then
     A1 = dom(R_EAL f) /\ A1 by MESFUNC5:def 7; then
     (R_EAL f)|A is A1-measurable by A30,MESFUNC5:42; then
A31:  R_EAL(f|A) is A1-measurable by Th16;
A32: R_EAL (f|A) is nonnegative by A4,MESFUNC5:def 7; then
A33: integral+(L-Meas,max-(R_EAL(f|A))) < +infty by A31,A26,MESFUN11:53;

     consider I be ExtREAL_sequence such that
A34:  for n be Nat holds
       I.n = Integral(L-Meas,(R_EAL(f|A))|((Partial_Union E).n)) and
      I is convergent and
A35:   Integral(L-Meas,R_EAL(f|A)) = lim I by A24,A31,A26,A28,A29,A33,Th19;

A36: for x be Real st x in dom Intf holds Intf.x = Integral(L-Meas,f|[.a,x.])
     proof
      let x be Real;
      assume A37: x in dom Intf; then
A38:   a <= x < b by A8,XXREAL_1:3; then
A39:   f is_integrable_on ['a,x'] & f|['a,x'] is bounded by A3,INTEGR24:def 2;
      reconsider AX = [.a,x.] as non empty closed_interval Subset of REAL
        by A38,XXREAL_1:30,MEASURE5:def 3;
A40:  AX = ['a,x'] by A38,INTEGRA5:def 3;
      AX c= [.a,b.[ by A38,XXREAL_1:43; then
A41:  AX c= dom f by A1;

      reconsider AX1 = AX as Element of L-Field by MEASUR10:5,MEASUR12:75;
      AX = AX1; then
      integral(f||AX) = Integral(L-Meas,f|[.a,x.])
        by A39,A40,A41,MESFUN14:49; then
      integral(f,AX) = Integral(L-Meas,f|[.a,x.]) by INTEGRA5:def 2; then
      integral(f,a,x) = Integral(L-Meas,f|[.a,x.]) by A38,A40,INTEGRA5:def 4;
      hence Intf.x = Integral(L-Meas,f|[.a,x.]) by A9,A37;
     end;

A42: Partial_Union E = E by A24,PROB_4:15;

A43: for m be Nat holds I.m = integral(f,a,b-(b-a)/(m+1))
     proof
      let m be Nat;
A44:  a <= b-(b-a)/(m+1) < b by A5,Th22; then
A45:  f||['a,b-(b-a)/(m+1)'] is bounded by A6,INTEGR10:def 1;

A46:   ['a,b-(b-a)/(m+1)'] = [.a,b-(b-a)/(m+1).] by A44,INTEGRA5:def 3; then
      ['a,b-(b-a)/(m+1)'] c= [.a,b.[ by A44,XXREAL_1:43; then
A47:   ['a,b-(b-a)/(m+1)'] c= dom f by A1;

A48:  E.m = [.a,b-(b-a)/(m+1).] by A24;

      (R_EAL f|A)|(E.m) = (f|A)|(E.m) by MESFUNC5:def 7; then
      (R_EAL f|A)|(E.m) = f|(E.m) by A2,A24,RELAT_1:74; then
A49:   (R_EAL f|A)|(E.m) = R_EAL f|(E.m) by MESFUNC5:def 7;

      I.m = Integral(L-Meas,(R_EAL(f|A))|((Partial_Union E).m)) by A34; then
A50:   I.m = Integral(L-Meas,f|[.a,b-(b-a)/(m+1).])
        by A49,A42,A48,MESFUNC6:def 3;
      f is_integrable_on ['a,b-(b-a)/(m+1)'] by A44,A6,INTEGR10:def 1;
      hence I.m = integral(f,a,b-(b-a)/(m+1)) by A50,A44,A46,A47,A45
,MESFUN14:50;
     end;

     for p be Real st 0<p ex n be Nat st for m be Nat st n<=m
      holds |.I.m-ext_right_integral(f,a,b) qua ExtReal.|<p
     proof
      let p be Real;
      assume 0<p; then
      consider r be Real such that
A51:    r < b and
A52:    for r1 be Real st r<r1 & r1<b & r1 in dom Intf holds
        |.Intf.r1-lim_left(Intf,b).| < p by A10,LIMFUNC2:41;
      set rr = max(a,r);
A53:  a <= rr & r<= rr by XXREAL_0:25;
      rr < b by A5,A51,XXREAL_0:29; then
A54:   0 < b-rr by XREAL_1:50;

      consider n be Nat such that
A55:    (b-a)/(b-rr) < n by SEQ_4:3;
      n <= n+1 by NAT_1:13; then
      (b-a)/(b-rr) < 1*(n+1) by A55,XXREAL_0:2; then
      (b-a)/(n+1) < 1*(b-rr) by A54,XREAL_1:113; then
      rr+(b-a)/(n+1) < b by XREAL_1:20; then
      rr < b-(b-a)/(n+1) by XREAL_1:20; then
A56:   r < b-(b-a)/(n+1) by A53,XXREAL_0:2;

A57:  0 < b-a by A2,XXREAL_1:27,XREAL_1:50;

A58:  a <= b-(b-a)/(n+1) < b by A5,Th22; then
A59:  b-(b-a)/(n+1) in dom Intf by A8,XXREAL_1:3;
      set r1=b-(b-a)/(n+1);
A60:  |.Intf.r1-lim_left(Intf,b).| < p by A52,A56,A59,A58;

      take n;
      thus for m be Nat st n<=m holds
       |.I.m - ext_right_integral(f,a,b) qua ExtReal.|<p
      proof
       let m be Nat;
       set rm = b-(b-a)/(m+1);
       assume n<=m; then
       n+1 <= m+1 by XREAL_1:6; then
       (b-a)/(n+1) >= (b-a)/(m+1) by A57,XREAL_1:118; then
A61:   b-(b-a)/(n+1) <= b-(b-a)/(m+1) by XREAL_1:10;
A62:    a <= rm by A5,Th22;

A63:   rm < b by A57,XREAL_1:44; then
       [.a,rm.] c= [.a,b.[ by XXREAL_1:43; then
A64:    [.a,rm.] c= dom f by A1;

       f|['a,rm'] is bounded by A62,A63,A3,INTEGR24:def 2; then
A65:    f|[.a,rm.] is bounded by A61,A58,XXREAL_0:2,INTEGRA5:def 3;

A66:    f is_integrable_on ['a,rm'] by A62,A63,A3,INTEGR24:def 2;
       (f|A)|[.a,rm.] is nonnegative by A4,MESFUNC6:55; then
A67:    f|[.a,rm.] is nonnegative by A63,A2,XXREAL_1:43,RELAT_1:74;
       [.a,r1.] c= [.a,rm.] by A61,XXREAL_1:34; then
       integral(f,a,r1) <= integral(f,a,rm) by A58,A64,A65,A66,A67,Th14; then
       Intf.r1 <= integral(f,a,rm) by A9,A58,A8,XXREAL_1:3; then
A68:   Intf.r1 <= I.m by A43;

A69:   rm in dom Intf by A8,A62,A63,XXREAL_1:3;

       Intf.rm = integral(f,a,b-(b-a)/(m+1)) by A9,A8,A62,A63,XXREAL_1:3; then
       I.m = Intf.rm by A43; then
       I.m <= lim_left(Intf,b) by A10,A23,A69,Th6,A57,XREAL_1:44; then
A70:   lim_left(Intf,b) - I.m >= 0 by XXREAL_3:40; then
       -(lim_left(Intf,b) - I.m) <= 0; then
       I.m - ext_right_integral(f,a,b) <= 0 by A11,XXREAL_3:26; then
A71:   |.I.m-ext_right_integral(f,a,b).|
        = -(I.m - ext_right_integral(f,a,b)) by EXTREAL1:18
       .= ext_right_integral(f,a,b)-I.m by XXREAL_3:26;

       reconsider EX = ext_right_integral(f,a,b) as ExtReal;
A72:   EX - Intf.r1
        = EX + -((Intf.r1) qua ExtReal) by XXREAL_3:def 4
       .= ext_right_integral(f,a,b) + (-Intf.r1) by XXREAL_3:def 2
       .= ext_right_integral(f,a,b) - Intf.r1;

A73:   EX - I.m <= EX - Intf.r1 by A68,XXREAL_3:37; then
       -(ext_right_integral(f,a,b)-Intf.r1) <= 0 by A72,A70,A11; then
       |.Intf.r1 - ext_right_integral(f,a,b).|
        = -(Intf.r1 - ext_right_integral(f,a,b)) by ABSVALUE:30
       .= ext_right_integral(f,a,b) - Intf.r1;
       hence |.I.m - ext_right_integral(f,a,b) qua ExtReal.| < p
         by A60,A11,A72,A73,A71,XXREAL_0:2;
      end;
     end; then
     consider RI be Real such that
A74:   lim I = RI &
      for p be Real st 0<p ex n be Nat st for m be Nat st n<=m holds
       |.I.m-lim I.|< p by MESFUNC5:def 8,MESFUNC9:7;
A75:  RI = Integral(L-Meas,f|A) by A35,A74,MESFUNC6:def 3;

     for g1 be Real st 0 < g1 ex R be Real st R<b & for r1 be Real st R<r1 &
     r1<b & r1 in dom Intf holds |.Intf.r1-RI.|<g1
     proof
      let g1 be Real;
      assume A76: 0 < g1;
      set g2 = g1/2;

      consider N be Nat such that
A77:    for m be Nat st N <= m holds |.I.m - lim I.| < g1 by A76,A74;
      take R = b-(b-a)/(N+1);
A78:  a <= R < b by A5,Th22; then
A79:   R in dom Intf by A8,XXREAL_1:3;

      thus R < b by A5,Th22;
      thus for r1 be Real st R<r1 & r1<b & r1 in dom Intf holds
            |. Intf.r1 - RI .| < g1
      proof
       let r1 be Real;
       assume that
A80:     R < r1 and
A81:     r1 < b and
A82:     r1 in dom Intf;

       I.N = integral(f,a,b-(b-a)/(N+1)) by A43; then
       Intf.R = I.N by A78,A9,A8,XXREAL_1:3; then
A83:    I.N <= Intf.r1 by A80,A79,A82,A12;
       RI - I.N = RI qua ExtReal - I.N &
       RI - Intf.r1 = RI qua ExtReal - Intf.r1; then
A84:  RI - Intf.r1 <= RI - I.N by A83,XXREAL_3:37;

A85:  |. I.N - RI .| < g1 by A77,A74;

       reconsider A2 = [.a,r1.] as Element of L-Field
         by MEASUR10:5,MEASUR12:75;

       A2 c= A1 by A2,A81,XXREAL_1:43; then
       Integral(L-Meas,(f|A)|A2) <= Integral(L-Meas,(f|A)|A1)
         by A4,A25,A31,MESFUNC6:def 1,87; then
       Integral(L-Meas,f|A2) <= RI by A75,A2,A81,XXREAL_1:43,RELAT_1:74; then
A86:  Intf.r1 <= RI by A82,A36; then
A87:  |.Intf.r1 - RI.| = -(Intf.r1 - RI) by ABSVALUE:30,XREAL_1:47;

       I.N <= RI by A83,A86,XXREAL_0:2; then
       |.RI - I.N.| = RI - I.N by XXREAL_3:40,EXTREAL1:def 1; then
       |.-(RI - I.N).| = RI - I.N by EXTREAL1:29; then
       |.I.N - RI.| = RI - I.N by XXREAL_3:26;
       hence |.Intf.r1 - RI.| < g1 by A84,A87,A85,XXREAL_0:2;
      end;
     end;
     hence right_improper_integral(f,a,b) = Integral(L-Meas,f|A)
        by A11,A7,A75,A10,LIMFUNC2:41;

     max+(R_EAL(f|A)) = R_EAL(f|A) by A32,MESFUN11:31; then
     Integral(L-Meas,f|A) = integral+(L-Meas,max+(R_EAL(f|A)))
       by A4,A31,A25,MESFUNC6:def 1,82; then
     integral+(L-Meas,max+(R_EAL(f|A))) < +infty
       by A75,XREAL_0:def 1,XXREAL_0:9;
     hence f is_right_ext_Riemann_integrable_on a,b
      implies f|A is_integrable_on L-Meas
        by A31,A26,A33,MESFUNC5:def 17,MESFUNC6:def 4;

     thus not f is_right_ext_Riemann_integrable_on a,b
      implies Integral(L-Meas,f|A) = +infty by A6;
    end;
    suppose
A88:  not f is_right_ext_Riemann_integrable_on a,b;
     hence right_improper_integral(f,a,b) = Integral(L-Meas,f|A)
       by A1,A2,A3,A4,Lm8;
     thus f is_right_ext_Riemann_integrable_on a,b
      implies f|A is_integrable_on L-Meas by A88;
     thus not f is_right_ext_Riemann_integrable_on a,b
      implies Integral(L-Meas,f|A) = +infty by A1,A2,A3,A4,Lm8;
    end;
end;
