 reserve X,a,b,c,x,y,z,t for set;
 reserve R for Relation;

theorem YaoTh3: :: Yao Theorem 3
  for A being finite non empty set,
      L, H being Function of bool A, bool A
    st L = Flip H holds
::      (L.A = A &
::      (for X,Y being Subset of A holds L.(X /\ Y) = L.X /\ L.Y) &
        (H.{} = {} &
        (for X,Y being Subset of A holds H.(X \/ Y) = H.X \/ H.Y))
      iff
      ex R being non empty finite RelStr st
        the carrier of R = A & LAp R = L & UAp R = H &
        for x,y being Element of R holds
        [x,y] in the InternalRel of R iff x in H.{y}
  proof
    let A be finite non empty set,
        L, H be Function of bool A, bool A;
    assume
ZA: L = Flip H;
    thus H.{} = {} &
      (for X,Y being Subset of A holds H.(X \/ Y) = H.X \/ H.Y)
      implies
      ex R being non empty finite RelStr st
        the carrier of R = A &
        LAp R = L & UAp R = H &
        for x,y being Element of R holds
        [x,y] in the InternalRel of R iff x in H.{y}
    proof
      assume
A0:   H.{} = {} &
      (for X,Y being Subset of A holds H.(X \/ Y) = H.X \/ H.Y);
      defpred Q[set,set] means
        $1 in H.{$2};
      consider R being Relation of A such that
WW1:  for x, y being Element of A holds
      [x,y] in R iff Q[x,y] from RELSET_1:sch 2;
      set RR = RelStr (#A,R#);
      reconsider RR as finite non empty RelStr;
W1:   for X being Subset of RR holds (UAp RR).X = H.X
      proof
        let X be Subset of RR;
        deffunc A() = the carrier of RR;
        defpred P[set] means
          for X being Subset of RR st X c= $1 holds (UAp RR).X = H.X;
U1:     the carrier of RR is finite;
        for X being Subset of RR st X c= {} holds (UAp RR).X = H.X
        proof
          let X be Subset of RR;
F2:       UAp {}RR = {};
          assume X c= {}; then
          X = {};
          hence thesis by ROUGHS_2:def 11,F2,A0;
        end; then
U2:     P[{}];
U3:     for x,B being set st x in A() & B c= A() & P[B] holds P[B \/ {x}]
        proof
          let x,B be set;
          assume
I1:       x in A() & B c= A() & P[B];
BE:       H.{x} = { y where y is Element of RR :
            x in Class (the InternalRel of RR,y) }
          proof
            thus H.{x} c= { y where y is Element of RR :
              x in Class (the InternalRel of RR,y) }
            proof
              let w be object;
              assume
P1:           w in H.{x};
              {x} c= A by I1,ZFMISC_1:31; then
t1:           H.{x} in bool A by FUNCT_2:5;
              reconsider xxx = x as Element of A by I1;
              reconsider www = w as Element of RR by t1,P1;
P2:           [www,xxx] in the InternalRel of RR by WW1,P1;
              www in {www} by TARSKI:def 1; then
              x in Class (the InternalRel of RR,www) by P2,RELAT_1:def 13;
              hence thesis;
            end;
            let w be object;
            assume w in { y where y is Element of RR :
              x in Class (the InternalRel of RR,y) }; then
            consider ww being Element of RR such that
P3:         ww = w & x in Class (the InternalRel of RR,ww);
            consider xx being object such that
P4:         [xx,x] in the InternalRel of RR & xx in {ww}
              by P3,RELAT_1:def 13;
            xx = ww by P4,TARSKI:def 1;
            hence thesis by WW1,P3,P4;
          end;
          reconsider xx = {x} as Subset of RR by I1,ZFMISC_1:31;
WX:       { y where y is Element of RR :
             Class (the InternalRel of RR,y) meets xx } =
            { y where y is Element of RR :
              x in Class (the InternalRel of RR,y) }
          proof
            thus { y where y is Element of RR :
              Class (the InternalRel of RR,y) meets xx } c=
            { y where y is Element of RR :
              x in Class (the InternalRel of RR,y) }
            proof
              let w be object;
              assume w in { y where y is Element of RR :
                Class (the InternalRel of RR,y) meets xx };
              then consider ww being Element of RR such that
H1:           ww = w & Class (the InternalRel of RR,ww) meets xx;
              x in Class (the InternalRel of RR,ww) by H1,ZFMISC_1:50;
              hence thesis by H1;
            end;
            let w be object;
            assume w in { y where y is Element of RR :
              x in Class (the InternalRel of RR,y) }; then
            consider ww being Element of RR such that
H1:         ww = w & x in Class (the InternalRel of RR,ww);
            Class (the InternalRel of RR,ww) meets xx by H1,ZFMISC_1:48;
            hence thesis by H1;
          end;
          for X being Subset of RR st X c= B \/ {x}
            holds (UAp RR).X = H.X
          proof
            let X be Subset of RR;
            assume
WW:         X c= B \/ {x};
            X \ {x} c= B \/ {x} \ {x} by WW,XBOOLE_1:33; then
W8:         X \ {x} c= B \ {x} by XBOOLE_1:40;
            per cases;
            suppose
XX1:          x in X;
              reconsider XX = X \ {x} as Subset of RR;
Ji:           XX \/ xx = X by XX1,ZFMISC_1:116;
OP:           UAp xx = H.xx by BE,WX,ROUGHS_1:def 5;
OR:           H.XX = (UAp RR).XX by W8,XBOOLE_1:1,I1
                  .= UAp XX by ROUGHS_2:def 11;
              (UAp RR).X = UAp X by ROUGHS_2:def 11
                      .= UAp XX \/ UAp xx by ROUGHS_2:13,Ji
                      .= H.X by A0,Ji,OP,OR;
              hence thesis;
            end;
            suppose
              not x in X;
              hence thesis by I1,ZFMISC_1:135,WW;
            end;
          end;
          hence thesis;
        end;
        P[the carrier of RR] from FINSET_1:sch 2(U1,U2,U3);
        hence thesis;
      end;
T3:   UAp RR = H by W1,FUNCT_2:63; then
      LAp RR = L by ROUGHS_2:27,ZA;
      hence thesis by T3,WW1;
    end;
    given R being non empty finite RelStr such that
U0: the carrier of R = A & LAp R = L & UAp R = H &
        for x,y being Element of R holds
        [x,y] in the InternalRel of R iff x in H.{y};
    thus thesis by UApAdditive,U0,UApEmpty;
  end;
