reserve n,m,k for Nat;
reserve r,r1 for Real;
reserve f,seq,seq1 for Real_Sequence;
reserve x,y for set;
reserve e1,e2 for ExtReal;
reserve Nseq for increasing sequence of NAT;
reserve v for FinSequence of REAL,
  r,s for Real,
  n,m,i,j,k for Nat;

theorem
  v<>{} & rng v c= Seg n & v.1 = 1 & v.(len v) = n & (for k st 1<=k & k
  <=len v - 1 holds for r,s st r = v.k & s = v.(k+1) holds |.r-s.| = 1 or r=s)
  implies (for i st i in Seg n ex k st k in dom v & v.k = i) & for m,k,i,r st m
  in dom v & v.m = i & (for j st j in dom v & v.j = i holds j<=m) & m<k & k in
  dom v & r = v.k
   holds i<r
proof
  assume that
A1: v<>{} and
A2: rng v c= Seg n and
A3: v.1 = 1 and
A4: v.(len v) = n and
A5: for k st 1<=k & k<=len v - 1 holds for r,s st r = v.k & s = v.(k+1)
  holds |.r-s.| = 1 or r=s;
  defpred P[Nat] means
  $1 in Seg n implies ex k st k in dom v & v.k = $1;
A6: 0+1<=len v by A1,NAT_1:13;
  then
A7: len v in dom v by FINSEQ_3:25;
A8: for i st P[i] holds P[i+1]
  proof
    let i such that
A9: i in Seg n implies ex k st k in dom v & v.k = i;
    assume
A10: i+1 in Seg n;
    now
      per cases;
      suppose
A11:    i = 0;
        take k=1;
        thus k in dom v & v.k = i+1 by A3,A6,A11,FINSEQ_3:25;
      end;
      suppose
A12:    i<>0;
        defpred R[set] means $1 in dom v & v.$1 = i;
A13:    for k be Nat holds R[k] implies k<=len v by FINSEQ_3:25;
        i+1<=n by A10,FINSEQ_1:1;
        then
A14:    i<=n by NAT_1:13;
A15:    0+1<=i by A12,NAT_1:13;
        then
A16:    ex k be Nat st R[k] by A9,A14,FINSEQ_1:1;
        consider m be Nat such that
A17:    R[m] & for k be Nat st R[k] holds k<=m from NAT_1:sch 6(A13,
        A16);
        reconsider m as Element of NAT by ORDINAL1:def 12;
        take k = m+1;
        i in Seg n by A15,A14,FINSEQ_1:1;
        hence k in dom v & v.k = i+1 by A1,A2,A4,A5,A10,A17,Th44;
      end;
    end;
    hence thesis;
  end;
A18: P[0] by FINSEQ_1:1;
  thus for i holds P[i] from NAT_1:sch 2(A18,A8);
  let m,k,i,r;
  assume that
A19: m in dom v and
A20: v.m = i and
A21: for j st j in dom v & v.j = i holds j<=m and
A22: m<k and
A23: k in dom v and
A24: r = v.k;
A25: m<=len v & k<=len v by A19,A23,FINSEQ_3:25;
A26: 1<=m by A19,FINSEQ_3:25;
A27: i in rng v by A19,A20,FUNCT_1:def 3;
  then
A28: i<=n by A2,FINSEQ_1:1;
  now
    per cases;
    suppose
      i=n;
      then len v<=m by A4,A7,A21;
      hence thesis by A22,A25,XXREAL_0:1;
    end;
    suppose
A29:  i<>n;
      defpred P[set] means for j,s st j=$1 & j>0 & m+j in dom v & s=v.(m+j)
      holds i<s;
      i<n by A28,A29,XXREAL_0:1;
      then 1<=i+1 & i+1<=n by NAT_1:11,13;
      then i+1 in Seg n by FINSEQ_1:1;
      then
A30:  v.(m+1) = i+1 by A1,A2,A4,A5,A19,A20,A21,A27,Th44;
A31:  for k st P[k] holds P[k+1]
      proof
        let k such that
A32:    P[k];
        let j,s such that
A33:    j=k+1 and
A34:    j>0 and
A35:    m+j in dom v and
A36:    s=v.(m+j);
        per cases;
        suppose
          k=0;
          hence thesis by A30,A33,A36,NAT_1:13;
        end;
        suppose
A37:      k<>0;
          m<=m+k by NAT_1:11;
          then
A38:      1<=m+k by A26,XXREAL_0:2;
          s in rng v by A35,A36,FUNCT_1:def 3;
          then s in Seg n by A2;
          then reconsider s1=s as Element of NAT;
A39:      m+(k+1)<=len v by A33,A35,FINSEQ_3:25;
          m+k<=m+k+1 by NAT_1:11;
          then m+k<=len v by A39,XXREAL_0:2;
          then
A40:      m+k in dom v by A38,FINSEQ_3:25;
          then v.(m+k) in rng v by FUNCT_1:def 3;
          then v.(m+k) in Seg n by A2;
          then reconsider r1=v.(m+k) as Element of NAT;
A41:      i<r1 by A32,A37,A40;
A42:      m+k+1 = m+(k+1);
          then
A43:      m+k<= len v - 1 by A39,XREAL_1:19;
          now
            per cases by A5,A33,A36,A42,A38,A43;
            suppose
A44:          |.r1-s.|=1;
              now
                per cases by A44,Th40;
                suppose
                  r1>s & r1=s+1;
                  then
A45:              i<=s1 by A41,NAT_1:13;
                  now
                    per cases by A45,XXREAL_0:1;
                    case
                      i<s;
                      hence thesis;
                    end;
                    case
                      s=i;
                      then m+j<=m by A21,A35,A36;
                      then j<=m-m by XREAL_1:19;
                      hence contradiction by A34;
                    end;
                  end;
                  hence thesis;
                end;
                suppose
                  r1<s & s=r1+1;
                  hence thesis by A41,XXREAL_0:2;
                end;
              end;
              hence thesis;
            end;
            suppose
              r1=s;
              hence thesis by A32,A37,A40;
            end;
          end;
          hence thesis;
        end;
      end;
      reconsider l = k - m as Element of NAT by A22,INT_1:5;
A46:  0< k - m & m+l = k by A22,XREAL_1:50;
A47:  P[0];
      for k holds P[k] from NAT_1:sch 2(A47,A31);
      hence thesis by A23,A24,A46;
    end;
  end;
  hence thesis;
end;
