reserve p,q,r,s,A,B for Element of PL-WFF,
  F,G,H for Subset of PL-WFF,
  k,n for Element of NAT,
  f,f1,f2 for FinSequence of PL-WFF;
reserve M for PLModel;
reserve x,y for set;

theorem th37:
  for F st F is consistent ex G st F c= G & G is consistent & G is maximal
  proof
    let F;
    assume Z0: F is consistent;
    set L = PL-WFF;
    consider R be Relation such that
    A3: R well_orders L by WELLORD2:17;
    R /\ [:L,L:]c=[:L,L:] by XBOOLE_1:17;
    then reconsider R2 = R |_2 L as Relation of L by WELLORD1:def 6;
    R2 well_orders L by A3,PCOMPS_2:1;then
    A76: R2 is_connected_in L by WELLORD1:def 5;
    reconsider RS = RelStr(#L,R2#) as non empty RelStr;
    set cRS = the carrier of RS;
    defpred H[object,object,object] means
    for p for f be PartFunc of cRS,bool L st $1=p & $2=f holds
    ((union(rng (f qua bool L -valued Relation)) \/ F \/ {p} is consistent
    implies $3=union (rng f) \/ F \/ {p}) &
    (not union(rng (f qua bool L -valued Relation)) \/ F \/ {p} is consistent
    implies $3=union (rng f) \/ F));
    A4: for x,y be object st x in cRS & y in PFuncs(cRS, bool L)
    ex z be object st z in bool L &
    H[x,y,z]
    proof
      let x,y be object;
      assume A6: x in cRS & y in PFuncs(cRS, bool L);
      reconsider x1 = x as Element of L by A6;
      reconsider y1 = y as PartFunc of cRS,bool L by A6,PARTFUN1:46;
      per cases;
      suppose
A7: union(rng (y1 qua bool L -valued Relation)) \/ F \/ {x1} is consistent;
        take union(rng y1) \/ F \/ {x1};
        thus thesis by A7;
      end;
      suppose
A7a:not union(rng (y1 qua bool L -valued Relation)) \/ F \/ {x1} is consistent;
        take union(rng y1) \/ F;
        thus thesis by A7a;
      end;
    end;
    consider h be Function of [:cRS,PFuncs(cRS, bool L):],bool L such that
A9: for x,y be object st x in cRS & y in PFuncs(cRS, bool L)
    holds H[x,y,h.(x,y)] from BINOP_1:sch 1(A4);
    R2 well_orders L by A3,PCOMPS_2:1;then
    R2 is_well_founded_in L by WELLORD1:def 5;then
A11: RS is well_founded by WELLFND1:def 2;then
     consider f be Function of cRS, bool L such that
A12: f is_recursively_expressed_by h by WELLFND1:11;
A73: dom f = cRS by FUNCT_2:def 1;
     reconsider G=union (rng (f qua bool L -valued Relation))
       as Subset of PL-WFF;
     set iRS = the InternalRel of RS;
    F6: field R2 = L by A3,PCOMPS_2:1;
A17: for A,B holds [A,B] in R2 implies f.A c= f.B
    proof
      let A,B;assume F3: [A,B] in R2;
      per cases;
      suppose A = B;
        hence thesis;
      end;
      suppose S2: A <> B;
        set frA = f|iRS-Seg A;
        set frB = f|iRS-Seg B;
        iRS is well-ordering by A3,PCOMPS_2:1,WELLORD1:4,F6;then
        F12: iRS-Seg A c= iRS-Seg B by F3,WELLORD1:29,F6;
        iRS-Seg B c= field iRS by WELLORD1:9;then
        F21: frB is Function of iRS-Seg B,bool L by FUNCT_2:32,F6;
        iRS-Seg A c= field iRS by WELLORD1:9;then
        frA is Function of iRS-Seg A,bool L by FUNCT_2:32,F6;then
        F11: dom frA = iRS-Seg A by FUNCT_2:def 1;
        F13: dom frB = iRS-Seg B by FUNCT_2:def 1,F21;
        F18:
        now
          let x;assume F19: x in dom frA;
          thus frA.x = f.x by F19,FUNCT_1:47
            .= frB.x by F13,FUNCT_1:47,F12,F11,F19;
        end;
E1:  union rng frA c= union rng frB by ZFMISC_1:77,rnginc,F18,F11,F12,F13;then
F7:     union rng frA \/ F c= union rng frB \/ F by XBOOLE_1:9;
F15:    A in dom frB by F13,S2,F3,WELLORD1:1;
        frB.A = f.A by FUNCT_1:47,F13,S2,F3,WELLORD1:1;then
        F14: f.A c= union rng frB by ZFMISC_1:74,FUNCT_1:3,F15;
        F18: dom frA c= cRS;
        rng (frA qua bool L -valued Relation) c= bool L;then
E4:     frA in PFuncs(cRS, bool L) by PARTFUN1:def 3,F18;
        F18a: dom frB c= cRS;
        rng (frB qua bool L -valued Relation) c= bool L;then
E2:     frB in PFuncs(cRS, bool L) by PARTFUN1:def 3,F18a;
        per cases;
          suppose union(rng (frA qua bool L -valued Relation)) \/ F \/ {A}
            is consistent;
          per cases;
          suppose
F2a:        union(rng (frB qua bool L -valued Relation)) \/ F \/ {B}
              is consistent;
            F16: f.B = h.(B, frB) by WELLFND1:def 5,A12
            .= union (rng frB) \/ F \/ {B} by A9,E2,F2a;
            thus f.A c= f.B
            proof
              let x be object;assume x in f.A;then
              x in union (rng frB) \/ (F \/ {B}) by XBOOLE_0:def 3,F14;
              hence thesis by F16,XBOOLE_1:4;
            end;
          end;
          suppose
F2b:not union(rng (frB qua bool L -valued Relation)) \/ F \/ {B} is consistent;
            F16b: f.B = h.(B, frB) by WELLFND1:def 5,A12
            .= union (rng frB) \/ F by A9,E2,F2b;
            thus f.A c= f.B by F16b,XBOOLE_0:def 3,F14;
          end;
        end;
        suppose
F2:not union(rng (frA qua bool L -valued Relation)) \/ F \/ {A} is consistent;
          F8: f.A = h.(A, frA) by WELLFND1:def 5,A12
          .= union (rng frA) \/ F by A9,E4,F2;
          per cases;
          suppose F2a: union(rng (frB qua bool L -valued Relation)) \/ F \/ {B}
            is consistent;
            F9: f.B = h.(B, frB) by WELLFND1:def 5,A12
            .= union (rng frB) \/ F \/ {B} by A9,E2,F2a;
            thus f.A c= f.B by F8,F9,XBOOLE_1:10,F7;
          end;
          suppose F2b:
    not union(rng (frB qua bool L -valued Relation)) \/ F \/ {B} is consistent;
            f.B = h.(B, frB) by WELLFND1:def 5,A12
            .= union (rng frB) \/ F by A9,E2,F2b;
            hence f.A c= f.B by F8,XBOOLE_1:9,E1;
          end;
        end;
      end;
    end;
A54: rng f is c=-linear
    proof
      let x,y;assume B2: x in rng f & y in rng f;then
      consider x1 be object such that B3: x1 in dom f & f.x1 = x
        by FUNCT_1:def 3;
      consider y1 be object such that B4: y1 in dom f & f.y1 = y
        by FUNCT_1:def 3,B2;
      reconsider x1,y1 as Element of L by B3,B4;
      per cases;
      suppose x1 = y1;
        hence thesis by B3,B4;
      end;
      suppose B1: x1 <> y1;
        per cases by A76,RELAT_2:def 6,B1;
        suppose [x1,y1] in R2;then
          f.x1 c= f.y1 by A17;
          hence x,y are_c=-comparable by XBOOLE_0:def 9,B3,B4;
        end;
        suppose [y1,x1] in R2;then
          f.y1 c= f.x1 by A17;
          hence x,y are_c=-comparable by XBOOLE_0:def 9,B3,B4;
        end;
      end;
    end;
    defpred S[Element of RS] means f.$1 is consistent;
A26: now
      let x be Element of RS;
      A20: f.x = h.(x, f|iRS-Seg x) by WELLFND1:def 5,A12;
      f|iRS-Seg x in PFuncs(cRS, bool L) by PARTFUN1:45;
      hence H[x,f|iRS-Seg x,f.x] by A20,A9;
    end;
A27: now
      let x be Element of RS;
      reconsider x1 = {x} as Subset of L;
      set fr = f|iRS-Seg x;
      per cases;
      suppose union(rng (fr qua bool L -valued Relation)) \/ F \/ x1
        is consistent;then
        f.x = union(rng fr) \/ F \/ x1 by A26
        .= F \/ (union(rng fr) \/ x1) by XBOOLE_1:4;
        hence F c= f.x by XBOOLE_1:7;
      end;
      suppose not union(rng (fr qua bool L -valued Relation)) \/ F \/ x1
        is consistent;then
        f.x = F \/ union(rng fr) by A26;
        hence F c= f.x by XBOOLE_1:7;
      end;
    end;
A21: for x being Element of RS st
     for y being Element of RS st y <> x & [y,x] in iRS holds S[y] holds S[x]
    proof
      let x be Element of RS;
      assume A41: for y being Element of RS st y <> x & [y,x] in iRS
      holds S[y];
      set fr = f|iRS-Seg x;
      iRS-Seg x c= field iRS by WELLORD1:9; then
C2:   fr is Function of iRS-Seg x,bool L by FUNCT_2:32,F6; then
A39a: dom fr = iRS-Seg x by FUNCT_2:def 1;
      reconsider x1 = {x} as Subset of L;
      per cases;
      suppose union(rng (fr qua bool L -valued Relation)) \/ F \/ x1
        is consistent;
        hence S[x] by A26;
      end;
      suppose C1:not union(rng (fr qua bool L -valued Relation)) \/ F \/ x1
        is consistent;then
A22:    f.x = union(rng fr) \/ F by A26;
        per cases;
        suppose S4: for y be object holds (not [y,x] in iRS or y = x);
          iRS-Seg x = {}
          proof
            assume iRS-Seg x <> {};then
            consider y be object such that
F19:        y in iRS-Seg x by XBOOLE_0:7;
            y <> x & [y,x] in iRS by WELLORD1:1,F19;
            hence contradiction by S4;
          end;then
          dom fr = {} by FUNCT_2:def 1,C2;then
          rng fr = {} by RELAT_1:42;
          hence S[x] by Z0,ZFMISC_1:2,A26,C1;
        end;
        suppose A39: ex y be object st [y,x] in iRS & y <> x;
          assume A30: not S[x];
          consider y be object such that A29: [y,x] in iRS & y <> x by A39;
          y in dom iRS by A29,XTUPLE_0:def 12;then
          reconsider y as Element of RS;
          fr.y in rng fr by FUNCT_1:3,A39a,WELLORD1:1,A29;then
A37:      f.y in rng fr by WELLORD1:1,A29,A39a,FUNCT_1:47;
A37b:     rng fr <> {} by WELLORD1:1,A29,A39a,FUNCT_1:3;
A37a:     F c= f.y by A27;
          F c= union rng fr by TARSKI:def 4,A37,A37a;then
A23:      f.x = union(rng fr) by A22,XBOOLE_1:12;
          consider F such that
A31:      F is finite & not F is consistent & F c= f.x by A30,finin;
          rng fr c= rng f by RELAT_1:70;then
          consider z be set such that
A34:      F c= z & z in rng fr by uniolinf,A23,A31,A54,A37b;
          consider y be object such that
A36:      y in dom fr & fr.y = z by A34,FUNCT_1:def 3;
A42:      [y,x] in iRS by WELLORD1:1,A39a,A36;
A44:      y <> x by WELLORD1:1,A39a,A36;
          y in dom iRS by A42,XTUPLE_0:def 12;then
          reconsider y as Element of RS;
          f.y = fr.y by A36,FUNCT_1:47;
          hence contradiction by A36,A34,A31,incsub,A44,A42,A41;
        end;
      end;
    end;
    A13a: for A be Element of RS holds S[A] from WELLFND1:sch 3(A21,A11);
    take G;
    thus F c= G
    proof
      let y be object;assume A46: y in F;
      set z = the Element of RS;
A47:  F c= f.z by A27;
      f.z in rng f by FUNCT_1:3,A73;
      hence y in G by A46,A47,TARSKI:def 4;
    end;
    thus G is consistent
    proof
      assume not G is consistent;then
      consider F such that
A14:  F is finite & not F is consistent & F c= G by finin;
      consider z be set such that
A90:  F c= z & z in rng f by uniolinf,A14,A54,RELAT_1:42,A73;
      rng (f qua bool L -valued Relation) c= bool L;then
      reconsider z as Subset of PL-WFF by A90;
      consider x be object such that
A92:  x in dom f & f.x = z by A90,FUNCT_1:def 3;
      thus contradiction by A92,A13a,A90,A14,incsub;
    end;
    thus G is maximal
    proof
      given A such that A71: not A in G & not 'not' A in G;
      reconsider ARS = A as Element of RS;
      reconsider nA = 'not' A as Element of RS;
      set fA = f|iRS-Seg A;
      set fnA = f|iRS-Seg ('not' A);
      reconsider A1 = {A} as Subset of L;
      reconsider A1n = {'not' A} as Subset of L;
      A74: not union rng (fA qua bool L -valued Relation) \/ F \/ A1
      is consistent
      proof
        assume union rng (fA qua bool L -valued Relation) \/ F \/ A1
        is consistent;then
        A70: f.ARS = union rng fA \/ F \/ A1 by A26;
        ARS in A1 by TARSKI:def 1;then
C1:     ARS in union rng fA \/ F \/ A1 by XBOOLE_0:def 3;
        f.ARS in rng f by FUNCT_1:3,A73;
        hence contradiction by TARSKI:def 4,A71,A70,C1;
      end;
      A78: not union rng (fnA qua bool L -valued Relation) \/ F \/ A1n
      is consistent
      proof
        assume union rng (fnA qua bool L -valued Relation) \/ F \/ A1n
        is consistent;then
        A70a: f.nA = union rng fnA \/ F \/ A1n by A26;
        nA in A1n by TARSKI:def 1;then
        A72a: nA in f.nA by A70a,XBOOLE_0:def 3;
        f.nA in rng f by FUNCT_1:3,A73;
        hence contradiction by TARSKI:def 4,A71,A72a;
      end;then
      A80: f.nA = union rng fnA \/ F by A26;
      A79: f.A = union rng fA \/ F by A26,A74;
      reconsider AAA = A as Element of HP-WFF by plhp;
      reconsider fal=FaLSUM as Element of HP-WFF by plhp;
      AAA=>fal = A => FaLSUM;then
      len A <> len ('not' A) by HILBERT2:16;then
      per cases by A76,RELAT_2:def 6;
      suppose [A,'not' A] in R2;then
        f.A c= f.nA by A17;then
        not f.nA \/ A1 is consistent by A74,incsub,A79,XBOOLE_1:9;
        hence contradiction by A13a,onecon,A78,A80;
      end;
      suppose ['not' A,A] in R2;then
        f.nA c= f.A by A17;then
        not f.ARS \/ A1n is consistent by A78,incsub,A80,XBOOLE_1:9;
        hence contradiction by onecon,A74,A79,A13a;
      end;
    end;
  end;
