reserve c, c1, d for Real,
  k for Nat,
  n, m, N, n1, N1, N2, N3, N4, N5, M for Element of NAT,
  x for set;

theorem :: Generalized Threshold Rule (page 81)
  for f being eventually-positive Real_Sequence, t being
eventually-nonnegative Real_Sequence, N being Element of NAT st t in Big_Oh(f)
& for n st n >= N holds f.n > 0 holds ex c st c > 0 & for n st n >= N holds t.n
  <= c*f.n
proof
  let f be eventually-positive Real_Sequence, t be eventually-nonnegative
  Real_Sequence, N be Element of NAT;
  assume that
A1: t in Big_Oh(f) and
A2: for n st n >= N holds f.n > 0;
  deffunc T(Element of NAT) = t.$1;
  deffunc F(Element of NAT) = f.$1;
  ex s being Element of Funcs(NAT, REAL) st t = s & ex c,N st c > 0 & for n
  st n >= N holds s.n <= c*f.n & s.n >= 0 by A1;
  then consider c2 being Real, M such that
A3: c2 > 0 and
A4: for n st n >= M holds t.n <= c2*f.n & t.n >= 0;
  set fset = { F(n) : N <= n & n <= max(M,N) };
A5: N <= max(M,N) by XXREAL_0:25;
  fset is finite non empty Subset of REAL from FinSegRng1(A5);
  then reconsider fset as finite non empty Subset of REAL;
  set F = min(fset);
A6: M <= max(M,N) by XXREAL_0:25;
  set tset = { T(n) : N <= n & n <= max(M,N) };
  tset is finite non empty Subset of REAL from FinSegRng1(A5);
  then reconsider tset as finite non empty Subset of REAL;
  set T = max(tset);
  set c1 = T / F;
  reconsider c = max(c1,c2) as Element of REAL by XREAL_0:def 1;
  take c;
  thus c > 0 by A3,XXREAL_0:25;
  let n;
  assume
A7: n >= N;
  then
A8: f.n > 0 by A2;
A9: f.n <> 0 by A2,A7;
  F in fset by XXREAL_2:def 7;
  then
A10: ex n1 being Element of NAT st f.n1 = F & n1 >= N & n1 <= max(M,N);
  then
A11: F > 0 by A2;
A12: F <> 0 by A2,A10;
  per cases;
  suppose
    N >= M;
    then n >= M by A7,XXREAL_0:2;
    then
A13: t.n <= c2*f.n by A4;
    c2*f.n <= c*f.n by A8,XREAL_1:64,XXREAL_0:25;
    hence thesis by A13,XXREAL_0:2;
  end;
  suppose
A14: N <= M;
    thus t.n <= c*f.n
    proof
      per cases;
      suppose
        n <= M;
        then
A15:    n <= max(M,N) by A6,XXREAL_0:2;
        then t.n in tset by A7;
        then
A16:    t.n <= T by XXREAL_2:def 8;
        f.n in fset by A7,A15;
        then
A17:    f.n >= F by XXREAL_2:def 7;
        t.M in tset by A6,A14;
        then
A18:    t.M <= T by XXREAL_2:def 8;
        t.M >= 0 by A4;
        then
A19:    c1*f.n >= c1*F by A11,A18,A17,XREAL_1:64;
        now
          assume t.n/f.n > c1;
          then t.n/f.n*f.n > c1*f.n by A8,XREAL_1:68;
          then t.n > c1*f.n by A9,XCMPLX_1:87;
          then T > c1*f.n by A16,XXREAL_0:2;
          hence contradiction by A12,A19,XCMPLX_1:87;
        end;
        then t.n/f.n*f.n <= c1*f.n by A8,XREAL_1:64;
        then
A20:    t.n <= c1*f.n by A9,XCMPLX_1:87;
        c1*f.n <= c*f.n by A8,XREAL_1:64,XXREAL_0:25;
        hence thesis by A20,XXREAL_0:2;
      end;
      suppose
A21:    n >= M;
A22:    c2*f.n <= c*f.n by A8,XREAL_1:64,XXREAL_0:25;
        t.n <= c2*f.n by A4,A21;
        hence thesis by A22,XXREAL_0:2;
      end;
    end;
  end;
end;
