reserve X,x,y,z for set;
reserve n,m,k,k9,d9 for Nat;
reserve d for non zero Nat;
reserve i,i0,i1 for Element of Seg d;
reserve l,r,l9,r9,l99,r99,x,x9,l1,r1,l2,r2 for Element of REAL d;
reserve Gi for non trivial finite Subset of REAL;
reserve li,ri,li9,ri9,xi,xi9 for Real;
reserve G for Grating of d;

theorem Th36:
  for A being Subset of REAL d holds A in cells(d,G) iff
  ex l,r st A = cell(l,r) & (for i holds [l.i,r.i] is Gap of G.i) &
  ((for i holds l.i < r.i) or for i holds r.i < l.i )
proof
  let A be Subset of REAL d;
  hereby
    assume A in cells(d,G);
    then consider l,r such that
A1: A = cell(l,r) and
A2: (ex X being Subset of Seg d st card X = d & for i holds (i in X & l
.i < r.i & [l.i,r.i] is Gap of G.i) or (not i in X & l.i = r.i & l.i in G.i))
    or (d = d & for i holds r.i < l.i & [l.i,r.i] is Gap of G.i)
    by Th29;
    thus
    ex l,r st A = cell(l,r) & (for i holds [l.i,r.i] is Gap of G.i) &
    ((for i holds l.i < r.i) or for i holds r.i < l.i )
    proof
      take l,r;
      per cases by A2;
      suppose ex X being Subset of Seg d st card X = d &
        for i holds i in X & l.i < r.i & [l.i,r.i] is Gap of G.i or
        not i in X & l.i = r.i & l.i in G.i;
        then consider X being Subset of Seg d such that
A3:     card X = d and
A4:     for i holds i in X & l.i < r.i & [l.i,r.i] is Gap of G.i or not i
        in X & l.i = r.i & l.i in G.i;
        card X = card Seg d by A3,FINSEQ_1:57;
        then not X c< Seg d by CARD_2:48;
        then X = Seg d by XBOOLE_0:def 8;
        hence thesis by A1,A4;
      end;
      suppose for i holds r.i < l.i & [l.i,r.i] is Gap of G.i;
        hence thesis by A1;
      end;
    end;
  end;
  given l,r such that
A5: A = cell(l,r) and
A6: for i holds [l.i,r.i] is Gap of G.i and
A7: (for i holds l.i < r.i) or for i holds r.i < l.i;
  per cases by A7;
  suppose
A8: for i holds l.i < r.i;
    ex X being Subset of Seg d st card X = d &
    for i holds i in X & l.i < r.i & [l.i,r.i] is Gap of G.i or
    not i in X & l.i = r.i & l.i in G.i
    proof
      Seg d c= Seg d;
      then reconsider X = Seg d as Subset of Seg d;
      take X;
      thus card X = d by FINSEQ_1:57;
      thus thesis by A6,A8;
    end;
    hence thesis by A5,Th29;
  end;
  suppose for i holds r.i < l.i;
    hence thesis by A5,A6,Th29;
  end;
end;
