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

theorem KeyTheorem:
  for R being finite non empty RelStr,
      H being Function of bool the carrier of R, bool the carrier of R st
    H.{} = {} & H is \/-preserving holds
    UAp GeneratedRelStr H = H
  proof
    let R be finite non empty RelStr,
        H be Function of bool the carrier of R, bool the carrier of R;
    assume
AA: H.{} = {} & H is \/-preserving;
K1: dom H = bool the carrier of R by FUNCT_2:def 1;
g2: for A being Subset of dom H st
      union A in dom H holds H.union A = union (H.:A)
    proof
      let A be Subset of dom H;
      per cases;
      suppose
        A = {};
        hence thesis by AA,ZFMISC_1:2;
      end;
      suppose
  KJ: A <> {};
      assume union A in dom H;
      reconsider AA = A as Element of Fin bool the carrier of R
        by K1,FINSUB_1:def 5;
      reconsider HH = H as Function of bool the carrier of R,
        Fin the carrier of R by FINSUB_1:14;
      defpred P[Subset of bool the carrier of R] means
        H.union $1 = union (H.:$1);
V1:   for x being Element of bool the carrier of R holds P[{x}]
      proof
        let x be Element of bool the carrier of R;
        H.union {x} = H.x by ZFMISC_1:25
            .= union {H.x} by ZFMISC_1:25
            .= union Im (H,x) by K1,FUNCT_1:59
            .= union (H.:{x});
        hence thesis;
      end;
V2:   for B1,B2 being non empty Subset of bool the carrier of R st
         P[B1] & P[B2] holds
        P[B1 \/ B2]
      proof
        let B1,B2 be non empty Subset of bool the carrier of R;
        assume
BB:     P[B1] & P[B2];
        set B12 = B1 \/ B2;
zx:     for g being set st g in B1 holds g c= the carrier of R;
        for g being set st g in B2 holds g c= the carrier of R; then
        reconsider U1 = union B1, U2 = union B2 as Subset of R
          by zx,ZFMISC_1:76;
        H.union B12 = H.(union B1 \/ union B2) by ZFMISC_1:78
          .= H.U1 \/ H.U2 by ROUGHS_4:def 9,AA
          .= union (H.:B1 \/ H.:B2) by ZFMISC_1:78,BB
          .= union (H.:(B1 \/ B2)) by RELAT_1:120;
        hence thesis;
      end;
SS:   for B being non empty Subset of bool the carrier of R holds
         P[B] from FinSubIndA2(V1,V2);
      reconsider AA = A as non empty Subset of bool the carrier of R
        by KJ,FUNCT_2:def 1;
      H.union AA = union (H.:AA) by SS;
      hence thesis;
      end;
    end;
    set HH = UAp GeneratedRelStr H;
    for X being Subset of R holds HH.X = H.X
    proof
      let X be Subset of R;
PS:   HH.X c= H.X
      proof
        let x be object;
        assume
a1:     x in HH.X;
        reconsider XX = X as Subset of GeneratedRelStr H;
a2:     x in UAp XX by ROUGHS_2:def 11,a1;
        reconsider xx = x as Element of R by a2;
        consider y being object such that
a4:     y in Class (GeneratedRelation (R,H), x) & y in X
          by a2,XBOOLE_0:3,ROUGHS_2:7;
        reconsider y as Element of R by a4;
        reconsider Y = {y} as Subset of R;
        [xx,y] in GeneratedRelation (R,H) by RELSET_2:9,a4; then
a5:     xx in H.{y} by GRDef;
        H.Y c= H.X by AA,ROUGHS_4:def 8,a4,ZFMISC_1:31;
        hence thesis by a5;
      end;
      H.X c= HH.X
      proof
        let x be object;
        assume
a1:     x in H.X;
        reconsider XX = X as Subset of GeneratedRelStr H;
        consider y being set such that
b1:     y in X & x in H.{y} by a1,KeyLemma,g2,K1,COHSP_1:def 9;
        reconsider xx = x as Element of R by a1;
        reconsider Y = {y} as Subset of R by b1,ZFMISC_1:31;
        reconsider xx = x, yy = y as Element of R by a1,b1;
        [xx,yy] in GeneratedRelation (R,H) by GRDef,b1; then
        yy in Class (GeneratedRelation (R,H), xx) & y in XX
          by b1,RELSET_2:9; then
        Class (the InternalRel of GeneratedRelStr H, xx) meets XX
          by XBOOLE_0:3; then
        xx in { x where x is Element of GeneratedRelStr H : Class (the
          InternalRel of GeneratedRelStr H, x) meets XX }; then
        xx in UAp XX by ROUGHS_1:def 5;
        hence thesis by ROUGHS_2:def 11;
      end;
      hence thesis by XBOOLE_0:def 10,PS;
    end;
    hence thesis by FUNCT_2:63;
  end;
