reserve S for OrderSortedSign;
reserve S for OrderSortedSign,
  X for ManySortedSet of S,
  o for OperSymbol of S ,
  b for Element of ([:the carrier' of S,{the carrier of S}:] \/ Union (coprod X
  ))*;
reserve x for set;

theorem Th40:
  for S be locally_directed regular monotone OrderSortedSign, X
be non-empty ManySortedSet of S, t being Element of TS DTConOSA(X) holds (PTMin
X).t in OSClass(PTCongruence(X),t) & LeastSort ((PTMin X).t) <= LeastSort t & (
for s being Element of S, x being set st x in X.s & t = root-tree [x,s] holds (
  PTMin X).t = t ) & (for o being OperSymbol of S, ts being FinSequence of TS(
  DTConOSA(X)) st OSSym(o,X) ==> roots ts & t = OSSym(o,X)-tree ts holds
LeastSorts ((PTMin X)*ts) <= the_arity_of o & OSSym(o,X) ==> roots ((PTMin X)*
ts) & OSSym(LBound(o,LeastSorts ((PTMin X)*ts)),X) ==> roots ((PTMin X)*ts) & (
PTMin X).t = OSSym(LBound(o,LeastSorts ((PTMin X)*ts)),X)-tree ((PTMin X)*ts))
proof
  let S be locally_directed regular monotone OrderSortedSign, X be non-empty
  ManySortedSet of S, t be Element of TS DTConOSA(X);
  set PTA = ParsedTermsOSA(X), D = DTConOSA(X), R = PTCongruence(X), SPTA = (
  the Sorts of PTA), M = PTMin(X), F = PTClasses(X);
  defpred P1[Element of TS D] means (PTMin X).$1 in OSClass(R,$1);
  defpred P2[Element of TS D] means LeastSort ((PTMin X).$1) <= LeastSort $1;
  defpred P4[Element of TS D] means (for s being Element of S, x being set st
  x in X.s & $1 = root-tree [x,s] holds (PTMin X).$1 = $1 );
  defpred P5[Element of TS D] means for o being OperSymbol of S, ts being
  FinSequence of TS(DTConOSA(X)) st OSSym(o,X) ==> roots ts & $1 = OSSym(o,X)
  -tree ts holds LeastSorts ((PTMin X)*ts) <= the_arity_of o & OSSym(o,X) ==>
roots ((PTMin X)*ts) & OSSym(LBound(o,LeastSorts ((PTMin X)*ts)),X) ==> roots (
  (PTMin X)*ts) & (PTMin X).$1 = OSSym(LBound(o,LeastSorts ((PTMin X)*ts)),X)
  -tree ((PTMin X)*ts);
  defpred P[DecoratedTree of the carrier of D] means ex t1 being Element of TS
  D st t1 = $1 & P1[t1] & P2[t1] & P4[t1] & P5[t1];
A1: for nt being Symbol of D, ts1 being FinSequence of TS(D) st nt ==> roots
ts1 & (for dt1 being DecoratedTree of the carrier of D st dt1 in rng ts1 holds
  P[dt1]) holds P[nt-tree ts1]
  proof
    let nt being Symbol of D, ts1 being FinSequence of TS(D) such that
A2: nt ==> roots ts1 and
A3: for dt1 being DecoratedTree of the carrier of D st dt1 in rng ts1
ex t2 being Element of TS D st t2 = dt1 & P1[t2] & P2[t2] & P4[t2] & P5[t2];
    reconsider t1 = nt-tree ts1 as Element of TS D by A2,Th12;
A4: rng ts1 c= TS D by FINSEQ_1:def 4;
    consider o being OperSymbol of S such that
A5: nt = [o,the carrier of S] and
A6: ts1 in Args(o,PTA) and
A7: nt-tree ts1 = Den(o,PTA).ts1 and
    for s1 being Element of S holds nt-tree ts1 in SPTA.s1 iff
    the_result_sort_of o <= s1 by A2,Th12;
    set Ms = (PTMin X)*ts1, LSMs = LeastSorts Ms, w = the_arity_of o, so =
    the_result_sort_of o, Lo = LBound(o,LSMs);
A8: dom ts1 = dom w by A6,MSUALG_3:6;
A9: dom ((PTMin X)*ts1) = dom ts1 by FINSEQ_3:120;
    then
A10: dom LSMs = dom ts1 by Def13;
    @(X,nt) = o by A2,A5,Def15;
    then
A11: M.(nt-tree ts1) = pi(o,M*ts1) by A2,Def31;
A12: P5[t1]
    proof
      let o2 be OperSymbol of S, ts be FinSequence of TS(DTConOSA(X)) such
      that
      OSSym(o2,X) ==> roots ts and
A13:  t1 = OSSym(o2,X)-tree ts;
      set Ms1 = (PTMin X)*ts, LSMs1 = LeastSorts Ms1, Lo2 = LBound(o2,LSMs1);
A14:  ts = ts1 by A13,TREES_4:15;
A15:  OSSym(o2,X) = nt by A13,TREES_4:15;
      then
A16:  o2 = o by A5,XTUPLE_0:1;
A17:  LeastSorts ((PTMin X)*ts1) <= the_arity_of o
      proof
        thus len LSMs = len w by A8,A10,FINSEQ_3:29;
        let i be set such that
A18:    i in dom LSMs;
        reconsider k = i as Nat by A18;
        ts1.k in rng ts1 by A10,A18,FUNCT_1:3;
        then reconsider tr = ts1.k as Element of TS D by A4;
A19:    ex tr1 being Element of TS D st tr1 = tr &( P1[tr1])&( P2 [tr1])
        &( P4[tr1])& P5[tr1] by A3,A10,A18,FUNCT_1:3;
A20:    w/.k = w.i by A8,A10,A18,PARTFUN1:def 6;
A21:    Ms.k = (PTMin X).tr by A9,A10,A18,FINSEQ_3:120;
        ts1.k in SPTA.(w/.k) by A6,A8,A10,A18,MSUALG_6:2;
        then
A22:    LeastSort (tr) <= (w/.k) by Def12;
        let s1,s2 be Element of S such that
A23:    s1 = LSMs.i and
A24:    s2 = w.i;
        ex t3 being Element of TS D st t3 = Ms.k & LSMs.k = LeastSort
        t3 by A9,A10,A18,Def13;
        hence thesis by A19,A23,A24,A21,A22,A20,ORDERS_2:3;
      end;
      hence LeastSorts ((PTMin X)*ts) <= the_arity_of o2 by A5,A15,A14,
XTUPLE_0:1;
      LBound(o2,LSMs1) has_least_rank_for o2,LSMs1 by A14,A16,A17,OSALG_1:14;
      then LBound(o2,LSMs1) has_least_args_for o2,LSMs1;
      then LSMs1 <= the_arity_of Lo2;
      then
A25:  Ms1 in Args(Lo2,PTA) by Th18;
      Ms1 in Args(o2,PTA) by A14,A16,A17,Th18;
      hence OSSym(o2,X) ==> roots Ms1 & OSSym(Lo2,X) ==> roots Ms1 by A25,Th13;
      hence thesis by A11,A14,A16,Def14;
    end;
A26: for s being Element of S, x being set st x in X.s & t1 = root-tree [x
    ,s] holds M.t1 = t1
    proof
      let s being Element of S, x being set such that
A27:  x in X.s and
A28:  t1 = root-tree [x,s];
      t1.{} = [o,the carrier of S] by A5,TREES_4:def 4;
      hence thesis by A27,A28,Th10;
    end;
A29: F.t1 = @(nt,F*ts1) by A2,Def21
      .= {[Den(o2,ParsedTermsOSA(X)).x2,s3] where o2 is OperSymbol of S, x2
is Element of Args(o2,ParsedTermsOSA(X)), s3 is Element of S : ( ex o1 being
OperSymbol of S st nt = [o1,the carrier of S] & o1 ~= o2 & len the_arity_of o1
= len the_arity_of o2 & the_result_sort_of o1 <= s3 & the_result_sort_of o2 <=
s3 ) & ex w3 being Element of (the carrier of S)* st dom w3 = dom (F*ts1) & for
    y being Nat st y in dom (F*ts1) holds [x2.y,w3/.y] in (F*ts1).y};
    take t1;
    thus t1 = nt-tree ts1;
A30: ex l being Nat st dom ts1 = Seg l by FINSEQ_1:def 2;
    reconsider ta1 = t1 as Element of SPTA.(LeastSort t1) by Def12;
A31: dom (F*ts1) = dom ts1 by FINSEQ_3:120;
A32: nt = OSSym(o,X) by A5;
    then OSSym(LBound(o,LeastSorts ((PTMin X)*ts1)),X) ==> roots ((PTMin X)*
    ts1) by A2,A12;
    then consider o3 being OperSymbol of S such that
A33: OSSym(Lo,X) = [o3,the carrier of S] and
A34: Ms in Args(o3,ParsedTermsOSA(X)) and
A35: OSSym(Lo,X)-tree Ms = Den(o3,ParsedTermsOSA(X)).Ms and
    for s1 being Element of S holds OSSym(Lo,X)-tree Ms in (the Sorts of
    PTA).s1 iff the_result_sort_of o3 <= s1 by Th12;
    reconsider Msr = Ms as Element of Args(Lo,PTA) by A33,A34,XTUPLE_0:1;
A36: Lo = o3 by A33,XTUPLE_0:1;
    then
A37: M.t1 = Den(Lo,PTA).(Msr) by A2,A32,A12,A35;
A38: LeastSorts ((PTMin X)*ts1) <= the_arity_of o by A2,A32,A12;
    then
A39: Lo has_least_rank_for o,LSMs by OSALG_1:14;
    then
A40: Lo has_least_sort_for o,LSMs;
    then
A41: o ~= Lo;
    Lo has_least_args_for o,LSMs by A39;
    then the_arity_of Lo <= w by A38;
    then
A42: len the_arity_of Lo = len w;
A43: OSClass(R,t1) = OSClass(R,ta1) by Def27
      .= proj1(F.t1) by Th25;
A44: the_result_sort_of Lo <= so by A38,A40;
A45: M.t1 in OSClass(R,t1)
    proof
      defpred P[object,object] means [Ms.$1,$2] in (F*ts1).$1;
A46:  for i being object st i in dom (F*ts1)
   ex s4 being object st s4 in the
      carrier of S & P[i,s4]
      proof
        let i be object such that
A47:    i in dom (F*ts1);
A48:    (F*ts1).i = F.(ts1.i) by A47,FINSEQ_3:120;
        ts1.i in rng ts1 by A31,A47,FUNCT_1:3;
        then reconsider td1 = ts1.i as Element of TS D by A4;
A49:    ex td2 being Element of TS D st td2 = td1 & M.td2 in OSClass(R,
        td2) &( P2[td2])&( P4[td2])& P5[td2] by A3,A31,A47,FUNCT_1:3;
A50:    Ms.i = M.(ts1.i) by A31,A47,FUNCT_1:13;
        reconsider tda = td1 as Element of SPTA.(LeastSort td1) by Def12;
        OSClass(R,td1) = OSClass(R,tda) by Def27
          .= proj1((F*ts1).i) by A48,Th25;
        then ex s4 being object st [Ms.i,s4] in (F*ts1).i by A50,A49,
XTUPLE_0:def 12;
        hence thesis by A48,Th23;
      end;
      consider f being Function such that
A51:  dom f = dom (F*ts1) & rng f c= the carrier of S & for z being
      object st z in dom (F*ts1) holds P[z,f.z] from FUNCT_1:sch 6(A46);
      reconsider wa = f as FinSequence by A31,A30,A51,FINSEQ_1:def 2;
      reconsider wa as FinSequence of the carrier of S by A51,FINSEQ_1:def 4;
      reconsider wa as Element of (the carrier of S)* by FINSEQ_1:def 11;
      for y being Nat st y in dom (F*ts1) holds [Msr.y,wa/.y] in (F*ts1). y
      proof
        let y be Nat such that
A52:    y in dom (F*ts1);
        (wa/.y) = wa.y by A51,A52,PARTFUN1:def 6;
        hence thesis by A51,A52;
      end;
      then [Den(Lo,PTA).Msr,so] in F.t1 by A5,A29,A41,A44,A42,A51;
      hence thesis by A43,A37,XTUPLE_0:def 12;
    end;
    (PTMin X).t1 = OSSym(LBound(o,LeastSorts ((PTMin X)*ts1)),X)-tree ((
    PTMin X)*ts1) by A2,A32,A12;
    then
A53: LeastSort (M.t1) = the_result_sort_of Lo by A34,A35,A36,Th17;
    LeastSort t1 = so by A6,A7,Th17;
    hence thesis by A12,A38,A53,A40,A45,A26;
  end;
A54: for s being Symbol of D st s in Terminals D holds P[root-tree s]
  proof
    let sy be Symbol of D such that
A55: sy in Terminals D;
    reconsider t1 = root-tree sy as Element of TS DTConOSA(X) by A55,
DTCONSTR:def 1;
    take t1;
    thus t1 = root-tree sy;
A56: M.(root-tree sy) = pi sy by A55,Def31
      .= root-tree sy by A55,Def16;
    hence M.t1 in OSClass(R,t1) by Th32;
    thus LeastSort (M.t1) <= LeastSort t1 by A56;
    thus for s1 being Element of S, x1 being set st x1 in X.s1 & t1 =
    root-tree [x1,s1] holds M.t1 = t1 by A56;
A57: ex s be Element of S, x be set st x in X.s & sy = [x,s] by A55,Th4;
    hereby
      let o being OperSymbol of S, ts being FinSequence of TS(DTConOSA(X))
      such that
      OSSym(o,X) ==> roots ts and
A58:  t1 = OSSym(o,X)-tree ts;
      t1.{} = [o,the carrier of S] by A58,TREES_4:def 4;
      hence LeastSorts ((PTMin X)*ts) <= the_arity_of o & OSSym(o,X) ==> roots
((PTMin X)*ts) & OSSym(LBound(o,LeastSorts ((PTMin X)*ts)),X) ==> roots ((PTMin
X)*ts) & (PTMin X).t1 = OSSym(LBound(o,LeastSorts ((PTMin X)*ts)),X)-tree ((
      PTMin X)*ts) by A57,Th10;
    end;
  end;
  for dt being DecoratedTree of the carrier of D st dt in TS(D) holds P[
  dt] from DTCONSTR:sch 7(A54,A1);
  then
  ex t1 being Element of TS D st t1 = t &( P1[t1])&( P2[t1] )&( P4[t1])&
  P5[t1];
  hence thesis;
end;
