
theorem Th43:
  for G being _Graph, W1, W3 being Walk of G, e being object
  st e Joins W3.first(),W3.last(),G & not e in W3.edges() &
    G.walkOf(W3.first(),e,W3.last()) is_odd_substring_of W1, 0 &
    for n, m being even Nat st n in dom W1 & m in dom W1 & W1.n = e & W1.m = e
    holds n = m
  holds not e in W1.replaceEdgeWith(e, W3).edges()
proof
  let G be _Graph, W1,W3 be Walk of G;
  let e be object;
  assume that
    A1: e Joins W3.first(),W3.last(),G and
    A2: not e in W3.edges() and
    A3: G.walkOf(W3.first(),e,W3.last()) is_odd_substring_of W1, 0 and
    A4: for n, m being even Nat
      st n in dom W1 & m in dom W1 & W1.n = e & W1.m = e
      holds n = m;
  set W2 = G.walkOf(W3.first(),e,W3.last());
  set W9 = W1.replaceWith(W2,W3);
  A6: W2 is_odd_substring_of W1, 1 by A3, Th18;
  A7: len W2 = 3 by A1, GLIB_001:14;
  :: first step of Proof is to find first usable index of e in W1
  defpred P[Nat] means $1 is odd & 1 <= $1 & $1 <= len W1
    & mid(W1,$1,($1-'1)+len W2) = W2;
  A9: ex i being Nat st P[i] by A6;
  consider i being Nat such that
    A10: P[i] and
    A11: for n being Nat st P[n] holds i <= n from NAT_1:sch 5(A9);
  reconsider i as odd Element of NAT by A10, ORDINAL1:def 12;
  set j = i-'1+len W2;
  W2 is_odd_substring_of W1, i by A10;
  then W2 is_substring_of W1, i by Th12;
  then A12: (1 <= j & j <= len W1 & i <= j) by Th11;
  A13: mid(W1,i,(i-'1)+len W2)
    = <*W3.first(),e,W3.last()*> by A1, A10, GLIB_001:def 5;
  :: n1 is said index, show some properties of it
  set n1 = i+1;
  i-1 = i-'1 by A10, XREAL_1:233;
  then A15: j = i+2 by A7;
  A16: 1 <= 2 <= len mid(W1,i,j) by A7, A10;
  A19: 1 <= j & j <= len W1 by A12;
  A20: i <= j by A12;
  then mid(W1,i,j).2 = W1.(2+i-'1) by A10, A16, A19, FINSEQ_6:118;
  then <*W3.first(),e,W3.last()*>.2 = W1.(2+i-1) by A13, NAT_D:37
    .= W1.n1;
  :: finally, W1.n1 is proven to equal e
  then A21: W1.n1 = e;
  :: Now we show that i (=n1-1) is first Vertex index and
  :: i+2 (=n1+1) is last Vertex index of W2 in W1
  reconsider k=i-1 as even Nat by CHORD:1;
  :: substring property as in the First/LastVertex definitions
  A22: for n being Nat st 1 <= n & n <= len W2 holds W1.(k+n)=W2.n
  proof
    let n be Nat;
    assume A23: 1 <= n & n <= len W2;
    then 1 <= n & n <= len mid(W1,i,j) by A10;
    then A24: W1.(n+i-'1) = W2.n by A10, A19, A20, FINSEQ_6:118;
    n+i-'1 = n+i-1 by A23, NAT_D:37 .= k+n;
    hence W1.(k+n) = W2.n by A24;
  end;
  :: minimal property as in the First/LastVertex definitions
  A25: for l being even Nat
    st for n being Nat st 1 <= n & n <= len W2 holds W1.(l+n) = W2.n
    holds k <= l
  proof
    let l be even Nat;
    assume A26: for n being Nat st 1 <= n & n <= len W2 holds W1.(l+n)=W2.n;
    per cases;
    suppose A27: l <= len W1;
      reconsider k2 = l+1 as odd Nat;
      per cases;
      suppose A28: k2+2 <= len W1;
        P[k2]
        proof
          thus k2 is odd;
          thus A30: 1 <= k2 by ABIAN:12;
          thus A31: k2 <= len W1 by A27, Th1;
          k2-1 = k2-'1 by A30, XREAL_1:233;
          then A33: (k2-'1)+len W2 = k2+2 by A7;
          A34: k2 in dom W1 by A30, A31, FINSEQ_3:25;
          A35: 1 <= 1 & 1 <= len W2 &
            1 <= 2 & 2 <= len W2 &
            1 <= 3 & 3 <= len W2 by A7;
          mid(W1,k2,(k2-'1)+len W2)
             = <*W1.k2,W1.(k2+1),W1.(k2+2)*> by A33, A34, A28, Th10
            .= <*W2.1,W1.(l+2),W1.(l+3)*> by A26, A35
            .= <*W2.1,W2.2,W1.(l+3)*> by A26, A35
            .= <*W2.1,W2.2,W2.3*> by A26, A35;
          hence thesis by A7, FINSEQ_1:45;
        end;
        then i-1 <= k2-1 by A11, XREAL_1:9;
        hence k <= l;
      end;
      suppose k2+2 > len W1;
        then i+2 <= k2+2 by A15,A19,XXREAL_0:2;
        then i-1 <= k2-1 by XREAL_1:6, XREAL_1:9;
        hence k <= l;
      end;
    end;
    suppose l > len W1;
      then A37: len W1 <= l;
      A38: k <= len W1 -1 by A10, XREAL_1:9;
      len W1 -1 <= len W1 -0 by XREAL_1:13;
      then k <= len W1 by A38, XXREAL_0:2;
      hence k <= l by A37, XXREAL_0:2;
    end;
  end;
  :: we conclude i as first Vertex
  i <= len W1 & ex k being even Nat st i = k+1 &
    (for n being Nat st 1 <= n & n <= len W2 holds W1.(k+n) = W2.n) &
    for l being even Nat
      st for n being Nat st 1 <= n & n <= len W2 holds W1.(l+n) = W2.n
      holds k <= l
  proof
    thus i <= len W1 by A10;
    take k;
    thus i = k+1;
    thus thesis by A22, A25;
  end;
  then A39: i = W1.findFirstVertex(W2) by A3, Def3;
  :: we conclude i+2 as last Vertex
  i+2 <= len W1 & ex k being even Nat st i+2 = k+len W2 &
    (for n being Nat st 1 <= n & n <= len W2 holds W1.(k+n) = W2.n) &
    for l being even Nat
      st for n being Nat st 1 <= n & n <= len W2 holds W1.(l+n) = W2.n
      holds k <= l by A7, A15, A19, A22, A25;
  then A40: i+2 = W1.findLastVertex(W2) by A3, Def4;
  :: show that n1 is strictly between first and last vertex index of W2;
  :: this will be used to show a contradiction in the last two parts
  :: of this Proof
  A41: W1.findFirstVertex(W2) < n1
  proof
    assume W1.findFirstVertex(W2) >= n1;
    then W1.findFirstVertex(W2) +1 <= W1.findFirstVertex(W2) +0 by A39;
    hence contradiction by XREAL_1:6;
  end;
  A42: n1 < W1.findLastVertex(W2) by A40, XREAL_1:6;
  n1 in dom W1
  proof
    i+0 <= i+1 & i+1 <= i+2 by XREAL_1:6;
    then 1 <= i+1 & i+1 <= len W1 by XXREAL_0:2, A10, A15, A12;
    hence thesis by FINSEQ_3:25;
  end;
  then A44: n1 in dom W1 & W1.findFirstVertex(W2) < n1 &
    n1 < W1.findLastVertex(W2) by A41, A42;
  :: Now show first part of thesis
  A45: not e in W1.cut(1,W1.findFirstVertex(W2)).edges()
  proof
    :: we show that the negation leads to n1 <= first vertex index
    :: by using the one-to-one-in e property from the initial assumption
    assume e in W1.cut(1,W1.findFirstVertex(W2)).edges();
    then consider n2 being even Element of NAT such that
      A46: 1 <= n2 & n2 <= len W1.cut(1,W1.findFirstVertex(W2)) and
      A47: W1.cut(1,W1.findFirstVertex(W2)).n2 = e by GLIB_001:99;
    reconsider n9 = 1 as odd Element of NAT by POLYFORM:4;
    A48: n9 <= W1.findFirstVertex(W2) & W1.findFirstVertex(W2) <= len W1
      by A3, Th35;
    then A49: len W1.cut(1,W1.findFirstVertex(W2)) + 1
      = W1.findFirstVertex(W2) + 1 by GLIB_001:36;
    then A50: n2 <= W1.findFirstVertex(W2) by A46;
    n2 is non zero by A46;
    then reconsider n3=n2-1 as Nat by CHORD:1;
    reconsider n3 as Element of NAT by ORDINAL1:def 12;
    W1.findFirstVertex(W2) <= len W1 by A3, Th35;
    then n2 <= len W1 by A50, XXREAL_0:2;
    then A51: n2 in dom W1 by A46, FINSEQ_3:25;
    n3 < W1.findFirstVertex(W2)
    proof
      assume n3 >= W1.findFirstVertex(W2);
      then A52: n2-1+1 >= W1.findFirstVertex(W2) + 1 by XREAL_1:6;
      W1.findFirstVertex(W2) +0 < W1.findFirstVertex(W2) +1 by XREAL_1:8;
      hence contradiction by A50, A52, XXREAL_0:2;
    end;
    then W1.cut(n9,W1.findFirstVertex(W2)).(n3+1) = W1.(n9+n3)
      by A48, A49, GLIB_001:36;
    :: here goes the one-to-one-in e property
    hence contradiction by A50, A44, A21, A4, A47, A51;
  end;
  :: second part of thesis
  not e in W1.cut(W1.findLastVertex(W2), len W1).edges()
  proof
    :: same idea as before, just a little more sophisticated
    :: because the cut doesn't (necessarily) start with 1
    assume e in W1.cut(W1.findLastVertex(W2), len W1).edges();
    then consider n2 being even Element of NAT such that
      A53: 1 <= n2 & n2 <= len W1.cut(W1.findLastVertex(W2), len W1) and
      A54: W1.cut(W1.findLastVertex(W2), len W1).n2 = e by GLIB_001:99;
    reconsider n9 = 1 as odd Element of NAT by POLYFORM:4;
    set D = len W1 + 1 - W1.findLastVertex(W2);
    A55: n9 <= W1.findLastVertex(W2) & W1.findLastVertex(W2) <= len W1
      by A3, Th35;
    then A56: len W1.cut(W1.findLastVertex(W2), len W1) + W1.findLastVertex(W2)
      = len W1 + 1 by GLIB_001:36;
    then A57: n2 <= len W1 + 1 - W1.findLastVertex(W2) by A53;
    n2 is non zero by A53;
    then reconsider n3=n2-1 as Nat by CHORD:1;
    reconsider n3 as Element of NAT by ORDINAL1:def 12;
    1 <= W1.findLastVertex(W2) by A3, Th35;
    then 1+(n2-1) <= W1.findLastVertex(W2)+(n2-1) by XREAL_1:6;
    then A58: 1 <= W1.findLastVertex(W2)+n3 by A53, XXREAL_0:2;
    W1.findLastVertex(W2)+n3 = n2 +(W1.findLastVertex(W2)-1);
    then n3 +W1.findLastVertex(W2) <=
      (len W1 + 1 - W1.findLastVertex(W2)) +(W1.findLastVertex(W2)-1)
      by A57, XREAL_1:6;
    then A59: W1.findLastVertex(W2)+n3 in dom W1 by A58, FINSEQ_3:25;
    n3 < D
    proof
      assume n3 >= D;
      then A60: n2-1+1 >= D + 1 by XREAL_1:6;
      D +0 < D +1 by XREAL_1:8;
      hence contradiction by A57, A60, XXREAL_0:2;
    end;
    then W1.cut(W1.findLastVertex(W2), len W1).(n3+1) =
      W1.(W1.findLastVertex(W2)+n3) by A55, GLIB_001:36, A56;
    :: here goes the one-to-one-in e property again
    then W1.findLastVertex(W2)+n2-1 = n1 by A21, A4, A54, A59, A44;
    then W1.findLastVertex(W2)+1 <= n1+1 by A53, XREAL_1:7;
    hence contradiction by A44, XREAL_1:6;
  end;
  :: the two parts together with assumption not e in W3.edges()
  :: show thesis by use of last theorem
  hence not e in W1.replaceEdgeWith(e, W3).edges() by A2, A45, A1, A3, Th42;
end;
