reserve x, y for object, X for set,
  i, j, k, l, n, m for Nat,
  D for non empty set,
  K for commutative Ring,
  a,b for Element of K,
  perm, p, q for Element of Permutations(n),
  Perm,P for Permutation of Seg n,
  F for Function of Seg n,Seg n,
  perm2, p2, q2, pq2 for Element of Permutations(n+2),
  Perm2 for Permutation of Seg (n+2);
reserve s for Element of 2Set Seg (n+2);
reserve pD for FinSequence of D,
  M for Matrix of n,m,D,
  pK,qK for FinSequence of K,
  A for Matrix of n,K;

theorem Th59:
  for A,B be Matrix of n,m,D, i st i <= n & 0 < n for F be
Function of Seg i, Seg n ex M be Matrix of n,m,D st
M = A +* ((B*(idseq n+*F)) | Seg i) &
for j holds (j in Seg i implies M.j = B.(F.j)) & (not j in Seg i
  implies M.j = A.j)
proof
  let A,B be Matrix of n,m,D, i such that
A1: i<= n and
A2: 0<n;
  set I=idseq n;
  let F be Function of Seg i,Seg n;
  set IF=I+*F;
A3: dom I=Seg n;
A4: rng I=Seg n;
  rng F c= Seg n by RELAT_1:def 19;
  then rng F\/Seg n=Seg n by XBOOLE_1:12;
  then
A5: rng IF c=Seg n by A4,FUNCT_4:17;
A6: Seg i c= Seg n by A1,FINSEQ_1:5;
  then
A7: Seg i\/Seg n=Seg n by XBOOLE_1:12;
A8: dom F=Seg i by A2,FUNCT_2:def 1;
  then dom F c= Seg n by A1,FINSEQ_1:5;
  then dom F\/Seg n =Seg n by XBOOLE_1:12;
  then dom IF=Seg n by A3,FUNCT_4:def 1;
  then reconsider IF as Function of Seg n,Seg n by A5,FUNCT_2:2;
  reconsider BIF=B*IF as Matrix of n,m,D;
  set BIFi=BIF|Seg i;
  set M=A+* BIFi;
A9: len B=n by A2,MATRIX_0:23;
  len BIF=len B by Def4;
  then dom BIF=Seg n by A9,FINSEQ_1:def 3;
  then
A10: dom BIFi=Seg i by A6,RELAT_1:62;
  len A=n by A2,MATRIX_0:23;
  then dom A=Seg n by FINSEQ_1:def 3;
  then
A11: dom M=Seg i \/Seg n by A10,FUNCT_4:def 1;
  then reconsider M as FinSequence by A7,FINSEQ_1:def 2;
A12: for j holds (j in Seg i implies M.j = B.(F.j) )&( not j in Seg i
  implies M.j = A.j )
  proof
    let j;
    thus j in Seg i implies M.j = B.(F.j)
    proof
A13:  Seg i c= Seg n by A1,FINSEQ_1:5;
      assume
A14:  j in Seg i;
      then
A15:  BIFi.j=BIF.j by A10,FUNCT_1:47;
      IF.j=F.j by A8,A14,FUNCT_4:13;
      then
A16:  Line(BIF,j)=B.(F.j) by A14,A13,Th38;
      Line(BIF,j)=BIF.j by A14,A13,MATRIX_0:52;
      hence thesis by A10,A14,A16,A15,FUNCT_4:13;
    end;
    assume not j in Seg i;
    hence thesis by A10,FUNCT_4:11;
  end;
A17: for x st x in rng M ex p be FinSequence of D st x = p & len p = m
  proof
    let x;
    assume x in rng M;
    then consider k be object such that
A18: k in dom M and
A19: M.k=x by FUNCT_1:def 3;
    reconsider k as Nat by A18;
    per cases;
    suppose
A20:  k in Seg i;
A21:  rng F c= Seg n by RELAT_1:def 19;
A22:  F.k in rng F by A8,A20,FUNCT_1:def 3;
      then reconsider Fk=F.k as Element of NAT by A21,TARSKI:def 3;
      take L=Line(B,Fk);
A23:  len L=width B by MATRIX_0:def 7;
      B.(F.k)=L by A22,A21,MATRIX_0:52;
      hence thesis by A2,A12,A19,A20,A23,MATRIX_0:23;
    end;
    suppose
A24:  not k in Seg i;
      take L=Line(A,k);
A25:  len L=width A by MATRIX_0:def 7;
      M.k=A.k by A12,A24;
      hence thesis by A2,A11,A7,A18,A19,A25,MATRIX_0:23,52;
    end;
  end;
  then reconsider M as Matrix of D by MATRIX_0:9;
  n is Element of NAT by ORDINAL1:def 12;
  then
A26: len M=n by A11,A7,FINSEQ_1:def 3;
  now
    let p be FinSequence of D;
    assume p in rng M;
    then ex q be FinSequence of D st p = q & len q = m by A17;
    hence len p=m;
  end;
  then reconsider M as Matrix of n,m,D by A26,MATRIX_0:def 2;
  take M;
  thus thesis by A12;
end;
