reserve i,j,k,k1,k2,n,m,i1,i2,j1,j2 for Element of NAT,
  x for set;

theorem Th7:
  for n,m,i1,j1,i2,j2 st i1<=n & j1<=m & i2<=n & j2<=m
   ex fs1,fs2 being FinSequence of NAT
 st (for i,k1,k2 st i in dom fs1 & k1=fs1.i & k2=fs2.i
  holds k1 <= n & k2 <= m) & fs1.1=i1 & fs1.(len fs1)=i2 & fs2.1=j1 & fs2.(len
fs2)=j2 & len fs1=len fs2 & len fs1=(i1-'i2)+(i2-'i1)+(j1-'j2)+(j2-'j1)+1 & for
  i st 1<=i & i<len fs1 holds fs1/.i,fs2/.i,fs1/.(i+1),fs2/.(i+1) are_adjacent
proof
  let n,m,i1,j1,i2,j2;
  assume that
A1: i1<=n and
A2: j1<=m and
A3: i2<=n and
A4: j2<=m;
  consider gs1 being FinSequence of NAT such that
A5: gs1.1=i1 and
A6: gs1.(len gs1)=i2 and
A7: len gs1=(i1-'i2)+(i2-'i1)+1 and
A8: for k,k1 st 1<=k & k<=len gs1 & k1=gs1.k holds k1<=n and
A9: for k st 1<=k & k<len gs1 holds gs1/.k,gs1/.(k+1) are_adjacent by A1,A3,Th6
;
  consider gs2 being FinSequence of NAT such that
A10: gs2.1=j1 and
A11: gs2.(len gs2)=j2 and
A12: len gs2=(j1-'j2)+(j2-'j1)+1 and
A13: for k,k1 st 1<=k & k<=len gs2 & k1=gs2.k holds k1<=m and
A14: for k st 1<=k & k<len gs2 holds gs2/.k,gs2/.(k+1) are_adjacent by A2,A4
,Th6;
A15: 1<=len gs2 by A12,NAT_1:11;
  then
A16: len gs2 -1>=1-1 by XREAL_1:9;
  set hs1=gs1^((len gs2-'1) |-> i2), hs2=((len gs1-'1) |-> j1)^gs2;
A17: len hs1=len gs1 + len ((len gs2-'1) |-> i2) by FINSEQ_1:22
    .=len gs1 + (len gs2-'1) by CARD_1:def 7
    .=len gs1 + (len gs2-1) by A16,XREAL_0:def 2
    .=(len gs1-1)+ len gs2;
A18: 1<=len gs1 by A7,NAT_1:11;
  then
A19: len gs1 -1>=1-1 by XREAL_1:9;
  then
A20: (len gs1-1) + len gs2 =(len gs1-'1)+ len gs2 by XREAL_0:def 2
    .=len ((len gs1-'1) |-> j1) + len gs2 by CARD_1:def 7
    .=len hs2 by FINSEQ_1:22;
A21: for i,k1,k2 st i in dom hs1 & k1=hs1.i & k2=hs2.i holds k1<=n & k2<=m
  proof
    dom hs2=Seg(len ((len gs1-'1) |-> j1)+ len gs2) by FINSEQ_1:def 7;
    then len hs2=len ((len gs1-'1) |-> j1) + len gs2 by FINSEQ_1:def 3;
    then
A22: len hs2=(len gs1 -'1)+len gs2 by CARD_1:def 7;
    let i,k1,k2;
    assume that
A23: i in dom hs1 and
A24: k1=hs1.i and
A25: k2=hs2.i;
    i in Seg len hs1 by A23,FINSEQ_1:def 3;
    then 1<=i & i<=len gs1-'1 or (len gs1 -'1)+1<=i & i<=len hs2 by A17,A20,
FINSEQ_1:1,NAT_1:13;
    then 1<=i & i<=len gs1-'1 or len gs1-1+1<=i & i<=len hs2 by A19,
XREAL_0:def 2;
    then 1<=i & i<=len gs1-1 or len gs1 -(len gs1-1)<=i-(len gs1-1) & i <=len
    hs2 by XREAL_0:def 2,XREAL_1:9;
    then 1<=i & i<=len gs1-1 or 1<=i-(len gs1-1) & i-(len gs1-1) <=len hs2-(
    len gs1-1) by XREAL_1:9;
    then
A26: 1<=i & i<=len gs1-1 or 1<=i-len gs1+1 & i-len gs1+1 <=len hs2-len gs1 +1;
A27: now
      per cases by A26,XREAL_1:6;
      case
A28:    1<=i-len gs1+1 & i-len gs1 <=len hs2 - len gs1;
        then
A29:    i+1-len gs1<=len hs2 - len gs1+1 by XREAL_1:6;
A30:    len ((len gs1-'1) |-> j1)=len gs1-'1 by CARD_1:def 7;
A31:    len hs2+1 - len gs1=(len gs1 -1)+len gs2+1 - len gs1 by A19,A22,
XREAL_0:def 2
          .=len gs2;
A32:    i+1-len gs1=i+1-'len gs1 by A28,XREAL_0:def 2;
        i-len gs1+1<=len hs2 - len gs1+1 by A28,XREAL_1:6;
        then i+1-'len gs1 in Seg len gs2 by A28,A31,A32,FINSEQ_1:1;
        then
A33:    i+1-'len gs1 in dom gs2 by FINSEQ_1:def 3;
        i=len gs1-1+(i+1-len gs1)
          .=(len ((len gs1-'1) |-> j1)+(i+1-'len gs1)) by A19,A32,A30,
XREAL_0:def 2;
        then hs2.i= gs2.(i+1-'len gs1) by A33,FINSEQ_1:def 7;
        hence k2<=m by A13,A25,A28,A29,A31,A32;
      end;
      case
A34:    1<=i & i<=len gs1-1;
        then
A35:    i<=len gs1-'1 by XREAL_0:def 2;
        then i in Seg (len gs1 -'1) by A34,FINSEQ_1:1;
        then i in Seg len ((len gs1-'1) |-> j1) by CARD_1:def 7;
        then i in dom ((len gs1-'1) |-> j1) by FINSEQ_1:def 3;
        then hs2.i= ((len gs1-'1) |-> j1).i by FINSEQ_1:def 7
          .=j1 by A34,A35,Lm1;
        hence k2<=m by A2,A25;
      end;
    end;
    dom hs1=Seg(len gs1 + len ((len gs2-'1) |-> i2)) by FINSEQ_1:def 7;
    then
A36: len hs1=len gs1 + len ((len gs2-'1) |-> i2) by FINSEQ_1:def 3;
    then
A37: len hs1 -' len gs1 = len hs1 - len gs1 by XREAL_0:def 2;
A38: len hs1=len gs1 + (len gs2 -'1) by A36,CARD_1:def 7;
A39: 1<=i & i<=len gs1 or len gs1 -len gs1<i-len gs1 & i <=len hs1 by A23,
FINSEQ_3:25,XREAL_1:9;
    now
      per cases by A37,A39,XREAL_1:9;
      case
A40:    1<=i & i<=len gs1;
        then i in dom gs1 by FINSEQ_3:25;
        then hs1.i=gs1.i by FINSEQ_1:def 7;
        hence k1<=n by A8,A24,A40;
      end;
      case
A41:    0<i-len gs1 & i-len gs1 <=len hs1 -' len gs1;
        then
A42:    i-len gs1=i-'len gs1 by XREAL_0:def 2;
        then
A43:    0+1<=i-'len gs1 by A41,NAT_1:13;
        then (i-'len gs1) in Seg (len hs1-'len gs1) by A41,A42,FINSEQ_1:1;
        then
A44:    (i-'len gs1)in Seg (len gs2-'1) by A38,NAT_D:34;
        then
A45:    i-'len gs1<=len gs2 -'1 by FINSEQ_1:1;
A46:    i=len gs1+(i-len gs1);
        (i-'len gs1)in Seg len ((len gs2-'1) |-> i2) by A44,CARD_1:def 7;
        then
A47:    (i-'len gs1) in dom ((len gs2-'1) |-> i2) by FINSEQ_1:def 3;
        i-'len gs1=i- len gs1 by A41,XREAL_0:def 2;
        then hs1.i= ((len gs2-'1) |-> i2).(i-'len gs1) by A47,A46,
FINSEQ_1:def 7
          .=i2 by A43,A45,Lm1;
        hence k1<=n by A3,A24;
      end;
    end;
    hence thesis by A27;
  end;
A48: for i st 1<=i & i<len hs1 holds hs1/.i,hs2/.i,hs1/.(i+1),hs2/.(i+1)
  are_adjacent
  proof
    let i;
    assume that
A49: 1<=i and
A50: i<len hs1;
    now
      per cases;
      case
A51:    i<len gs1;
        then
A52:    i+1<=len gs1 by NAT_1:13;
A53:    now
          per cases;
          case
            i+1<=len gs1-'1;
            then i+1<=len gs1 -1 by XREAL_0:def 2;
            then i+1+1<=len gs1-1+1 by XREAL_1:6;
            then
A54:        i+1<len gs1 by NAT_1:13;
            then
A55:        i<len gs1 by NAT_1:13;
            then
A56:        i in dom gs1 by A49,FINSEQ_3:25;
            1<i+1 by A49,NAT_1:13;
            then
A57:        (i+1) in dom gs1 by A54,FINSEQ_3:25;
A58:        dom gs1 c= dom hs1 by FINSEQ_1:26;
            then
A59:        hs1/.(i+1)=hs1.(i+1) by A57,PARTFUN1:def 6
              .=gs1.(i+1) by A57,FINSEQ_1:def 7
              .=gs1/.(i+1) by A57,PARTFUN1:def 6;
            hs1/.i=hs1.i by A58,A56,PARTFUN1:def 6
              .=gs1.i by A56,FINSEQ_1:def 7
              .=gs1/.i by A56,PARTFUN1:def 6;
            hence hs1/.i,hs1/.(i+1) are_adjacent by A9,A49,A55,A59;
          end;
          case
            i+1>len gs1-'1;
            0+1<=i+1 by NAT_1:13;
            then
A60:        (i+1) in dom gs1 by A52,FINSEQ_3:25;
A61:        dom gs1 c= dom hs1 by FINSEQ_1:26;
            then
A62:        hs1/.(i+1)=hs1.(i+1) by A60,PARTFUN1:def 6
              .=gs1.(i+1) by A60,FINSEQ_1:def 7
              .=gs1/.(i+1)by A60,PARTFUN1:def 6;
A63:        i in dom gs1 by A49,A51,FINSEQ_3:25;
            then hs1/.i=hs1.i by A61,PARTFUN1:def 6
              .=gs1.i by A63,FINSEQ_1:def 7
              .=gs1/.i by A63,PARTFUN1:def 6;
            hence hs1/.i,hs1/.(i+1) are_adjacent by A9,A49,A51,A62;
          end;
        end;
A64:    1<=i+1 by A49,NAT_1:13;
A65:    now
          per cases;
          case
A66:        i+1<=len gs1-'1;
A67:        len ((len gs1-'1) |-> j1)=len gs1-'1 by CARD_1:def 7;
A68:        dom ((len gs1-'1) |-> j1) c= dom hs2 by FINSEQ_1:26;
            i+1 in Seg (len gs1 -'1) by A64,A66,FINSEQ_1:1;
            then
A69:        i+1 in dom ((len gs1-'1) |-> j1) by A67,FINSEQ_1:def 3;
            then hs2.(i+1)= ((len gs1-'1) |-> j1).(i+1) by FINSEQ_1:def 7
              .=j1 by A64,A66,Lm1;
            hence hs2/.(i+1)=j1 by A69,A68,PARTFUN1:def 6;
          end;
          case
A70:        i+1>len gs1-'1;
A71:        len gs1-'1+1=len gs1-1+1 by A7,XREAL_0:def 2
              .=len gs1;
            len gs1-'1+1 <=i+1 by A70,NAT_1:13;
            then
A72:        len gs1=i+1 by A52,A71,XXREAL_0:1;
            dom hs2=Seg(len ((len gs1-'1) |-> j1)+len gs2) by FINSEQ_1:def 7;
            then len hs2=len ((len gs1-'1) |-> j1)+len gs2 by FINSEQ_1:def 3
              .=(len gs1-'1)+len gs2 by CARD_1:def 7
              .=len gs1-1+len gs2 by A7,XREAL_0:def 2
              .=len gs1+(len gs2-1)
              .=len gs1+(len gs2-'1) by A12,XREAL_0:def 2;
            then len gs1 <= len hs2 by NAT_1:11;
            then len gs1 in Seg len hs2 by A18,FINSEQ_1:1;
            then
A73:        (i+1) in dom hs2 by A72,FINSEQ_1:def 3;
            1 in Seg len gs2 by A15,FINSEQ_1:1;
            then
A74:        1 in dom gs2 by FINSEQ_1:def 3;
            len ((len gs1-'1) |-> j1)+1=i+1 by A71,A72,CARD_1:def 7;
            then hs2.(i+1)=j1 by A10,A74,FINSEQ_1:def 7;
            hence hs2/.(i+1)=j1 by A73,PARTFUN1:def 6;
          end;
        end;
A75:    len ((len gs1-'1) |-> j1)=len gs1-'1 by CARD_1:def 7;
A76:    dom ((len gs1-'1) |-> j1) c= dom hs2 by FINSEQ_1:26;
        i+1-1<=len gs1 -1 by A52,XREAL_1:9;
        then
A77:    i<=len gs1-'1 by XREAL_0:def 2;
        then i in Seg (len gs1 -'1) by A49,FINSEQ_1:1;
        then
A78:    i in dom ((len gs1-'1) |-> j1) by A75,FINSEQ_1:def 3;
        then hs2.i= ((len gs1-'1) |-> j1).i by FINSEQ_1:def 7
          .=j1 by A49,A77,Lm1;
        hence
        hs1/.i,hs1/.(i+1) are_adjacent & hs2/.i=hs2/.(i+1) or hs1/.i=hs1
        /.(i+1) & hs2/.i,hs2/.(i+1) are_adjacent by A78,A76,A65,A53,
PARTFUN1:def 6;
      end;
      case
A79:    i>=len gs1;
        then
A80:    0<=i+1-len gs1 by NAT_1:12,XREAL_1:48;
A81:    len ((len gs1-'1) |-> j1)+(i+1-'len gs1+1) =(len gs1-'1)+(i+1-'
        len gs1+1) by CARD_1:def 7
          .=len gs1 -1+(i+1-'len gs1+1) by A19,XREAL_0:def 2
          .=len gs1 -1+(i+1-len gs1+1) by A80,XREAL_0:def 2
          .=i+1;
A82:    i+1 =len gs1+(i+1-len gs1)
          .=len gs1+(i+1-'len gs1) by A80,XREAL_0:def 2;
A83:    i-len gs1>=0 by A79,XREAL_1:48;
        then 0+1<=i-len gs1 +1 by XREAL_1:6;
        then
A84:    1<=i+1-'len gs1 by A80,XREAL_0:def 2;
A85:    i-len gs1=i-'len gs1 by A83,XREAL_0:def 2;
A86:    now
          assume not 1<=(i-'len gs1);
          then i-'len gs1<0+1;
          then
A87:      i-'len gs1=0 by NAT_1:13;
          len gs1 in Seg len gs1 by A7,FINSEQ_1:3;
          then i in dom gs1 by A85,A87,FINSEQ_1:def 3;
          hence hs1.i=i2 by A6,A85,A87,FINSEQ_1:def 7;
        end;
A88:    i+1-len gs1+len gs1<=len gs2 -1+len gs1 by A17,A50,NAT_1:13;
        then i+1-len gs1<=len gs2 -1 by XREAL_1:6;
        then i+1-len gs1<=len gs2 -'1 by XREAL_0:def 2;
        then
A89:    (i+1-'len gs1)<=len gs2-'1 by XREAL_0:def 2;
        i-len gs1+1>=0+1 by A83,XREAL_1:6;
        then
A90:    1<=(i+1-'len gs1) by A88,XREAL_0:def 2;
        len ((len gs2-'1) |-> i2)=len gs2-'1 by CARD_1:def 7;
        then (i+1-'len gs1) in Seg len ((len gs2-'1) |-> i2) by A90,A89,
FINSEQ_1:1;
        then (i+1-'len gs1) in dom ((len gs2-'1) |-> i2) by FINSEQ_1:def 3;
        then
A91:    hs1.(i+1)= ((len gs2-'1) |-> i2).((i+1)-'len gs1) by A82,FINSEQ_1:def 7
          .=i2 by A90,A89,Lm1;
A92:    len ((len gs1-'1) |-> j1)+(i+1-'len gs1) =(len gs1-'1)+(i+1-'len
        gs1) by CARD_1:def 7
          .=len gs1 -1+(i+1-'len gs1) by A19,XREAL_0:def 2
          .=len gs1 -1+(i-len gs1+1) by A80,XREAL_0:def 2
          .=i;
        len hs1=len gs1+len ((len gs2-'1) |-> i2) by FINSEQ_1:22
          .=len gs1 + (len gs2-'1) by CARD_1:def 7;
        then i-len gs1 < len gs1 + (len gs2 -'1) - len gs1 by A50,XREAL_1:9;
        then
A93:    (i-'len gs1)<=len gs2 -'1 by XREAL_0:def 2;
        i-(len gs1-1)<len gs1-1+len gs2 -(len gs1-1) by A17,A50,XREAL_1:9;
        then
A94:    i+1-'len gs1<len gs2 by A80,XREAL_0:def 2;
        then 1<=i+1-'len gs1 +1 & len gs2>=i+1-'len gs1+1 by NAT_1:11,13;
        then i+1-'len gs1+1 in Seg len gs2 by FINSEQ_1:1;
        then
A95:    i+1-'len gs1+1 in dom gs2 by FINSEQ_1:def 3;
        len gs2-'1<=len gs2-'1+1 by NAT_1:11;
        then (i+1-'len gs1)<=(len gs2-'1)+1 by A89,XXREAL_0:2;
        then (i+1-'len gs1)<=(len gs2-1)+1 by A16,XREAL_0:def 2;
        then
A96:    i+1-'len gs1 in dom gs2 by A90,FINSEQ_3:25;
A97:    len ((len gs2-'1) |-> i2)=len gs2-'1 by CARD_1:def 7;
A98:    now
A99:      i =len gs1+(i-len gs1) .=len gs1+(i-'len gs1) by A83,XREAL_0:def 2;
          assume
A100:     1<=(i-'len gs1);
          then (i-'len gs1) in Seg len ((len gs2-'1) |-> i2) by A93,A97,
FINSEQ_1:1;
          then (i-'len gs1) in dom ((len gs2-'1) |-> i2) by FINSEQ_1:def 3;
          then hs1.i= ((len gs2-'1) |-> i2).(i-'len gs1)by A99,FINSEQ_1:def 7
            .=i2 by A93,A100,Lm1;
          hence hs1.i=i2;
        end;
        i in dom hs2 by A17,A20,A49,A50,FINSEQ_3:25;
        then
A101:   hs2/.i=hs2.(len ((len gs1-'1) |-> j1)+(i+1-'len gs1)) by A92,
PARTFUN1:def 6
          .=gs2.(i+1-'len gs1) by A96,FINSEQ_1:def 7
          .=gs2/.(i+1-'len gs1) by A96,PARTFUN1:def 6;
        i in dom hs1 by A49,A50,FINSEQ_3:25;
        then
A102:   hs1/.i=i2 by A86,A98,PARTFUN1:def 6;
        i+1<=len hs1 & 0+1<=i+1 by A50,NAT_1:13;
        then
A103:   i+1 in Seg len hs1 by FINSEQ_1:1;
        then i+1 in dom hs2 by A17,A20,FINSEQ_1:def 3;
        then
A104:   hs2 /.(i+1)=hs2.(len ((len gs1-'1) |-> j1)+(i+1-'len gs1+1)) by A81,
PARTFUN1:def 6
          .=gs2.((i+1-'len gs1+1)) by A95,FINSEQ_1:def 7
          .=gs2/.(i+1-'len gs1+1) by A95,PARTFUN1:def 6;
        i+1 in dom hs1 by A103,FINSEQ_1:def 3;
        hence
        hs1/.i,hs1/.(i+1) are_adjacent & hs2/.i=hs2/.(i+1) or hs1/.i=hs1
        /.(i+1) & hs2/.i, hs2/.(i+1) are_adjacent by A14,A102,A91,A84,A94,A104
,A101,PARTFUN1:def 6;
      end;
    end;
    hence thesis;
  end;
A105: now
    per cases;
    case
      len gs1-'1=0;
      then ((len gs1-'1) |-> j1)^gs2=<*>(NAT)^gs2 .=gs2 by FINSEQ_1:34;
      hence hs2.1=j1 & hs2.len hs2=j2 by A10,A11;
    end;
    case
A106: len gs1-'1<>0;
      len ((len gs1-'1) |-> j1)=len gs1-'1 by CARD_1:def 7;
      then
A107: 0+1<= len ((len gs1-'1) |-> j1) by A106,NAT_1:13;
      then 1 in dom ((len gs1-'1) |-> j1) by FINSEQ_3:25;
      then
A108: hs2.1 = ((len gs1-'1) |-> j1).1 by FINSEQ_1:def 7;
      1<=len gs2 by A12,NAT_1:11;
      then
A109: len gs2 in dom gs2 by FINSEQ_3:25;
      len ((len gs1-'1) |-> j1) = len gs1-'1 & len hs2=len ((len gs1-'1)
      |-> j1) + len gs2 by CARD_1:def 7,FINSEQ_1:22;
      hence hs2.1=j1 & hs2.len hs2=j2 by A11,A107,A108,A109,Lm1,FINSEQ_1:def 7;
    end;
  end;
A110: 1 in dom gs1 by A18,FINSEQ_3:25;
  now
    per cases;
    case
      len gs2-'1=0;
      then gs1^((len gs2-'1) |-> i2) = gs1^<*>(NAT) .=gs1 by FINSEQ_1:34;
      hence hs1.1=i1 & hs1.len hs1=i2 by A5,A6;
    end;
    case
      len gs2-'1<>0;
      then
A111: 0+1<=len gs2-'1 by NAT_1:13;
A112: len hs1=len gs1+len ((len gs2-'1) |-> i2) by FINSEQ_1:22;
      len ((len gs2-'1) |-> i2) = len gs2-'1 by CARD_1:def 7;
      then len ((len gs2-'1) |-> i2) in dom ((len gs2-'1) |-> i2) & ((len gs2
      -'1) |-> i2).len ((len gs2-'1) |-> i2) = i2 by A111,Lm1,FINSEQ_3:25;
      hence hs1.1=i1 & hs1.len hs1=i2 by A5,A110,A112,FINSEQ_1:def 7;
    end;
  end;
  hence thesis by A7,A12,A17,A20,A105,A21,A48;
end;
