reserve X,Y for set,
  x,x1,x2,y,y1,y2,z for set,
  f,g,h for Function;
reserve M for non empty set;
reserve D for non empty set;
reserve P for Relation;
reserve O for Order of X;
reserve R,P for Relation,
  X,X1,X2,Y,Z,x,y,z,u for set,
  g,h for Function,
  O for Order of X,
  D for non empty set,
  d,d1,d2 for Element of D,
  A1,A2,B for Ordinal,
  L,L1,L2 for Sequence;
reserve A,C for Ordinal;

theorem
  R partially_orders X & field R = X implies ex P st R c= P & P
  linearly_orders X & field P = X
proof
  assume that
A1: R partially_orders X and
A2: field R = X;
  defpred P[object] means
   ex P st $1 = P & R c= P & P partially_orders X & field P = X;
  consider Rel being set such that
A3: for x being object holds x in Rel iff x in bool [:X,X:] & P[x]
   from XBOOLE_0:sch 1;
A4: for Z st Z <> {} & Z c= Rel & Z is c=-linear holds union Z in Rel
  proof
    reconsider T = [:X,X:] as Relation;
    let Z;
    assume that
A5: Z <> {} and
A6: Z c= Rel and
A7: Z is c=-linear;
A8: union bool [:X,X:] = T by ZFMISC_1:81;
    Z c= bool [:X,X:]
    by A3,A6;
    then
A9: union Z c= union bool [:X,X:] by ZFMISC_1:77;
    then reconsider S = union Z as Relation;
    set y = the Element of Z;
    y in Rel by A5,A6;
    then consider P such that
A10: y = P and
A11: R c= P and
A12: P partially_orders X and
    field P = X by A3;
A13: S is_antisymmetric_in X
    proof
      let x,y be object;
      assume that
A14:  x in X and
A15:  y in X and
A16:  [x,y] in S and
A17:  [y,x] in S;
      consider X1 such that
A18:  [x,y] in X1 and
A19:  X1 in Z by A16,TARSKI:def 4;
      consider P1 being Relation such that
A20:  X1 = P1 and
      R c= P1 and
A21:  P1 partially_orders X and
      field P1 = X by A3,A6,A19;
      consider X2 such that
A22:  [y,x] in X2 and
A23:  X2 in Z by A17,TARSKI:def 4;
      X1,X2 are_c=-comparable by A7,A19,A23;
      then
A24:  X1 c= X2 or X2 c= X1;
      consider P2 being Relation such that
A25:  X2 = P2 and
      R c= P2 and
A26:  P2 partially_orders X and
      field P2 = X by A3,A6,A23;
A27:  P2 is_antisymmetric_in X by A26;
      P1 is_antisymmetric_in X by A21;
      hence thesis by A14,A15,A18,A22,A24,A20,A25,A27;
    end;
A28: S is_transitive_in X
    proof
      let x,y,z be object;
      assume that
A29:  x in X and
A30:  y in X and
A31:  z in X and
A32:  [x,y] in S and
A33:  [y,z] in S;
      consider X1 such that
A34:  [x,y] in X1 and
A35:  X1 in Z by A32,TARSKI:def 4;
      consider P1 being Relation such that
A36:  X1 = P1 and
      R c= P1 and
A37:  P1 partially_orders X and
      field P1 = X by A3,A6,A35;
      consider X2 such that
A38:  [y,z] in X2 and
A39:  X2 in Z by A33,TARSKI:def 4;
      X1,X2 are_c=-comparable by A7,A35,A39;
      then
A40:  X1 c= X2 or X2 c= X1;
      consider P2 being Relation such that
A41:  X2 = P2 and
      R c= P2 and
A42:  P2 partially_orders X and
      field P2 = X by A3,A6,A39;
A43:  P2 is_transitive_in X by A42;
      P1 is_transitive_in X by A37;
      then [x,z] in P1 or [x,z] in P2 by A29,A30,A31,A34,A38,A40,A36,A41,A43;
      hence thesis by A35,A39,A36,A41,TARSKI:def 4;
    end;
A44: P is_reflexive_in X by A12;
    S is_reflexive_in X
    proof
      let x be object;
      assume x in X;
      then [x,x] in P by A44;
      hence thesis by A5,A10,TARSKI:def 4;
    end;
    then
A45: S partially_orders X by A28,A13;
A46: field S c= X
    proof
      let x be object;
A47:  now
        assume x in dom S;
        then consider y being object such that
A48:    [x,y] in S by XTUPLE_0:def 12;
        consider Y such that
A49:    [x,y] in Y and
A50:    Y in Z by A48,TARSKI:def 4;
        ex P st Y = P & R c= P & P partially_orders X & field P = X by A3,A6
,A50;
        hence thesis by A49,RELAT_1:15;
      end;
A51:  now
        assume x in rng S;
        then consider y being object such that
A52:    [y,x] in S by XTUPLE_0:def 13;
        consider Y such that
A53:    [y,x] in Y and
A54:    Y in Z by A52,TARSKI:def 4;
        ex P st Y = P & R c= P & P partially_orders X & field P = X by A3,A6
,A54;
        hence thesis by A53,RELAT_1:15;
      end;
      assume x in field S;
      hence thesis by A47,A51,XBOOLE_0:def 3;
    end;
A55: R c= S
    by A5,A10,A11,TARSKI:def 4;
    then X c= field S by A2,RELAT_1:16;
    then field S = X by A46;
    hence thesis by A3,A9,A8,A55,A45;
  end;
  R c= [:X,X:] by A2,Lm6;
  then Rel <> {} by A1,A2,A3;
  then consider Y such that
A56: Y in Rel and
A57: for Z st Z in Rel & Z <> Y holds not Y c= Z by A4,Th67;
  consider P such that
A58: Y = P and
A59: R c= P and
A60: P partially_orders X and
A61: field P = X by A3,A56;
  take P;
  thus R c= P by A59;
  thus
A62: P is_reflexive_in X & P is_transitive_in X & P is_antisymmetric_in
  X by A60;
  thus P is_connected_in X
  proof
    let x,y be object such that
A63: x in X and
A64: y in X and
    x <> y and
A65: not [x,y] in P and
A66: not [y,x] in P;
    defpred Q[object,object] means [$1,$2] in P or [$1,x] in P & [y,$2] in P;
    consider Q being Relation such that
A67: for z,u being object holds [z,u] in Q iff z in X & u in X & Q[z,u]
     from RELAT_1:sch 1;
A68: field Q c= X
    proof
      let z be object;
A69:  now
        assume z in dom Q;
        then ex u being object st [z,u] in Q by XTUPLE_0:def 12;
        hence thesis by A67;
      end;
A70:  now
        assume z in rng Q;
        then ex u being object st [u,z] in Q by XTUPLE_0:def 13;
        hence thesis by A67;
      end;
      assume z in field Q;
      hence thesis by A69,A70,XBOOLE_0:def 3;
    end;
A71: P c= Q
    proof
      let z,u be object;
      assume
A72:  [z,u] in P;
      then
A73:  u in field P by RELAT_1:15;
      z in field P by A72,RELAT_1:15;
      hence thesis by A61,A67,A72,A73;
    end;
    then X c= field Q by A61,RELAT_1:16;
    then
A74: field Q = X by A68;
A75: Q is_transitive_in X
    proof
      let a,b,c be object;
      assume that
A76:  a in X and
A77:  b in X and
A78:  c in X and
A79:  [a,b] in Q and
A80:  [b,c] in Q;
A81:  [b,c] in P or [b,x] in P & [y,c] in P by A67,A80;
      [a,b] in P or [a,x] in P & [y,b] in P by A67,A79;
      then [a,c] in P or [a,x] in P & [y,c] in P or [a,x] in P & [y,c] in P
      or [y,x] in P by A62,A63,A64,A76,A77,A78,A81;
      hence thesis by A66,A67,A76,A78;
    end;
A82: Q is_antisymmetric_in X
    proof
      let a,b be object;
      assume that
A83:  a in X and
A84:  b in X and
A85:  [a,b] in Q and
A86:  [b,a] in Q;
A87:  [b,a] in P or [b,x] in P & [y,a] in P by A67,A86;
      [a,b] in P or [a,x] in P & [y,b] in P by A67,A85;
      then a = b or [a,x] in P & [y,a] in P or [y,x] in P by A62,A63,A64,A83
,A84,A87;
      hence thesis by A62,A63,A64,A66,A83;
    end;
    Q is_reflexive_in X
    by A62,A67;
    then
A88: Q partially_orders X by A75,A82;
A89: Q c= [:X,X:]
    proof
      let y,z be object;
      assume
A90:  [y,z] in Q;
      then
A91:  z in X by A67;
      y in X by A67,A90;
      hence thesis by A91,ZFMISC_1:87;
    end;
    R c= Q by A59,A71;
    then Q in Rel by A3,A89,A74,A88;
    then
A92: Q = P by A57,A58,A71;
A93: [y,y] in P by A62,A64;
    [x,x] in P by A62,A63;
    hence contradiction by A63,A64,A65,A67,A92,A93;
  end;
  thus thesis by A61;
end;
