reserve x,y,z for set;

theorem Th30:
  for S being non void Signature for X,Y being non-empty
ManySortedSet of the carrier of S for t being Term of S,Y st variables_in t c=
  X holds t is Term of S,X
proof
  let S be non void Signature;
  let X,Y be non-empty ManySortedSet of the carrier of S;
  defpred P[DecoratedTree] means Y variables_in $1 c= X implies $1 is Term of
  S,X;
  let t be Term of S,Y;
A1: for o being OperSymbol of S, p being ArgumentSeq of Sym(o,Y) st for t
being Term of S,Y st t in rng p holds P[t] holds P[[o, the carrier of S]-tree p
  ]
  proof
    let o be OperSymbol of S, p be ArgumentSeq of Sym(o,Y) such that
A2: for t being Term of S,Y st t in rng p & Y variables_in t c= X
    holds t is Term of S,X and
A3: Y variables_in ([o, the carrier of S]-tree p) c= X;
A4: now
      let i be Nat;
      assume
A5:   i in dom p;
      then reconsider t = p.i as Term of S,Y by MSATERM:22;
A6:   t in rng p by A5,FUNCT_1:def 3;
      Y variables_in t c= X
      proof
        let y be object;
        assume y in the carrier of S;
        then reconsider s = y as SortSymbol of S;
        let x be object;
        assume x in (Y variables_in t).y;
        then
A7:     x in (Y variables_in ([o, the carrier of S]-tree p)).s by A6,Th13;
        (Y variables_in ([o, the carrier of S]-tree p)).s c= X.s by A3;
        hence thesis by A7;
      end;
      then reconsider t9 = t as Term of S,X by A2,A6;
      take t9;
      thus t9 = p.i;
      the_sort_of t = (the_arity_of o).i by A5,MSATERM:23;
      hence the_sort_of t9 = (the_arity_of o).i by Th29;
    end;
    len p = len the_arity_of o by MSATERM:22;
    then reconsider p as ArgumentSeq of Sym(o,X) by A4,MSATERM:24;
    Sym(o,X)-tree p is Term of S,X;
    hence thesis by MSAFREE:def 9;
  end;
  assume variables_in t c= X;
  then
A8: Y variables_in t c= X by Th15;
A9: for s being SortSymbol of S, x being Element of Y.s holds P[root-tree [x
  ,s]]
  proof
    let s be SortSymbol of S, x be Element of Y.s;
    assume Y variables_in root-tree [x,s] c= X;
    then
A10: (Y variables_in root-tree [x,s]).s c= X.s;
    (Y variables_in root-tree [x,s]).s = {x} by Th12;
    then x in X.s by A10,ZFMISC_1:31;
    hence thesis by MSATERM:4;
  end;
  for t being Term of S,Y holds P[t] from MSATERM:sch 1(A9,A1);
  hence thesis by A8;
end;
