reserve x,y,z for set;

theorem Th19:
  for S being non void Signature for Y being non-empty
ManySortedSet of the carrier of S for X being ManySortedSet of the carrier of S
for o being OperSymbol of S for p being ArgumentSeq of Sym(o,Y) holds Sym(o,Y)
-tree p in (S-Terms(X,Y)).the_result_sort_of o iff rng p c= Union (S-Terms(X,Y)
  )
proof
  let S be non void Signature;
  let Y be non-empty ManySortedSet of the carrier of S;
  let X be ManySortedSet of the carrier of S;
  let o be OperSymbol of S;
  let p be ArgumentSeq of Sym(o,Y);
  set s = the_result_sort_of o;
A1: dom (S-Terms(X,Y)) = the carrier of S by PARTFUN1:def 2;
A2: Sym(o,Y) = [o,the carrier of S] by MSAFREE:def 9;
A3: (S-Terms(X,Y)).s = {t where t is Term of S,Y: the_sort_of t = s &
  variables_in t c= X} by Def5;
  hereby
    assume Sym(o,Y)-tree p in (S-Terms(X,Y)).s;
    then consider t being Term of S,Y such that
A4: [o,the carrier of S]-tree p = t and
    the_sort_of t = s and
A5: variables_in t c= X by A3,A2;
    thus rng p c= Union (S-Terms(X,Y))
    proof
      let y be object;
      assume
A6:   y in rng p;
      then consider x being object such that
A7:   x in dom p and
A8:   y = p.x by FUNCT_1:def 3;
      reconsider x as Nat by A7;
      reconsider q = p.x as Term of S,Y by A7,MSATERM:22;
A9:   variables_in q c= X
      proof
        let z be object;
        assume
A10:    z in the carrier of S;
        let a be object;
        assume a in (variables_in q).z;
        then
A11:    a in (variables_in t).z by A4,A6,A8,A10,Th11;
        (variables_in t).z c= X.z by A5,A10;
        hence thesis by A11;
      end;
      set sq = the_sort_of q;
      (S-Terms(X,Y)).sq = {t9 where t9 is Term of S,Y: the_sort_of t9 =
      sq & variables_in t9 c= X} by Def5;
      then q in (S-Terms(X,Y)).sq by A9;
      hence thesis by A1,A8,CARD_5:2;
    end;
  end;
  set t = Sym(o,Y)-tree p;
  assume
A12: rng p c= Union (S-Terms(X,Y));
A13: variables_in t c= X
  proof
    let z be object;
    assume
A14: z in the carrier of S;
    let x be object;
    assume x in (variables_in t).z;
    then consider q being DecoratedTree such that
A15: q in rng p and
A16: x in (S variables_in q).z by A2,A14,Th11;
    consider y being object such that
A17: y in the carrier of S and
A18: q in (S-Terms(X,Y)).y by A1,A12,A15,CARD_5:2;
    (S-Terms(X,Y)).y = {t9 where t9 is Term of S,Y: the_sort_of t9 = y &
    variables_in t9 c= X} by A17,Def5;
    then consider t9 being Term of S,Y such that
A19: q = t9 and
    the_sort_of t9 = y and
A20: variables_in t9 c= X by A18;
    (variables_in t9).z c= X.z by A14,A20;
    hence thesis by A16,A19;
  end;
  the_sort_of t = s by MSATERM:20;
  hence thesis by A3,A13;
end;
