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 Th37:
  for S be locally_directed OrderSortedSign, X be non-empty
  ManySortedSet of S, U1 be monotone non-empty OSAlgebra of S, f be
  ManySortedFunction of PTVars(X),the Sorts of U1 ex h be ManySortedFunction of
  ParsedTermsOSA(X),U1 st h is_homomorphism ParsedTermsOSA(X),U1 & h is
  order-sorted & h || PTVars(X) = f
proof
  let S be locally_directed OrderSortedSign, X be non-empty ManySortedSet of S;
  set D = DTConOSA(X), PTA = ParsedTermsOSA(X), PV = PTVars(X), SPTA = the
  Sorts of PTA;
  let U1 be non-empty monotone OSAlgebra of S, F be ManySortedFunction of PV,
  the Sorts of U1;
  set SA =the Sorts of PTA, AR = the Arity of S, S1 = the Sorts of U1, O = the
  carrier' of S, RS = the ResultSort of S;
  reconsider SAO=SA,S1O=S1 as OrderSortedSet of S by OSALG_1:17;
  deffunc TermVal(Symbol of D) = pi(F,the Sorts of U1,$1);
  deffunc NTermVal(Symbol of D,set,FinSequence) = pi(@(X,$1),U1, $3);
  consider G being Function of TS(D), Union (the Sorts of U1) such that
A1: for t being Symbol of D st t in Terminals D holds G.(root-tree t) =
  TermVal(t) and
A2: for nt be Symbol of D, ts be FinSequence of TS(D) st nt ==> roots ts
  holds G.(nt-tree ts) = NTermVal(nt,roots ts,G * ts) from DTCONSTR:sch 8;
  deffunc F(object) = G | ((the Sorts of PTA).$1);
  consider h be Function such that
A3: dom h = the carrier of S & for s be object st s in the carrier of S
  holds h.s = F(s) from FUNCT_1:sch 3;
  reconsider h as ManySortedSet of the carrier of S by A3,PARTFUN1:def 2
,RELAT_1:def 18;
  for s be object st s in dom h holds h.s is Function
  proof
    let s be object;
    assume s in dom h;
    then h.s = G | ((the Sorts of PTA).s) by A3;
    hence thesis;
  end;
  then reconsider h as ManySortedFunction of the carrier of S by FUNCOP_1:def 6
;
  defpred P[set] means for s be Element of S st $1 in (the Sorts of PTA).s
  holds (h.s).$1 in (the Sorts of U1).s;
A4: for nt being Symbol of D, ts being FinSequence of TS(D) st nt ==> roots
  ts & for t being DecoratedTree of the carrier of D st t in rng ts holds P[t]
  holds P[nt-tree ts]
  proof
    let nt be Symbol of D, ts be FinSequence of TS(D);
    assume that
A5: nt ==> roots ts and
A6: for t being DecoratedTree of the carrier of D st t in rng ts holds P[t];
    set p = G * ts, o = @(X,nt), ar = the_arity_of o, rs = the_result_sort_of
    o, OU = [:the carrier' of S,{the carrier of S}:] \/ Union (coprod (X qua
    ManySortedSet of S)), rt = roots ts;
A7: [o,the carrier of S] = nt by A5,Def15;
    then
A8: [[o,the carrier of S],rt] in the Rules of D by A5,LANG1:def 1;
    let s be Element of S;
    reconsider s2=s as Element of S;
    assume
A9: nt-tree ts in SA.s;
    consider op being OperSymbol of S such that
A10: nt = [op,the carrier of S] and
    ts in Args(op,PTA) and
    nt-tree ts = Den(op,PTA).ts and
A11: for s1 being Element of S holds nt-tree ts in SA.s1 iff
    the_result_sort_of op <= s1 by A5,Th12;
    op = o by A5,A10,Def15;
    then rs <= s by A9,A11;
    then
A12: S1O.rs c= S1O.s2 by OSALG_1:def 16;
A13: rng Den(o,U1) c= Result(o,U1) by RELAT_1:def 19;
A14: dom(S1* ar) = dom ar by PARTFUN1:def 2;
A15: [o,the carrier of S] = OSSym(o,X);
    then
A16: (PTDenOp(o,X)).ts = nt-tree ts by A5,A7,Def9;
A17: dom(SA* ar) = dom ar by PARTFUN1:def 2;
    dom (PTDenOp (o,X)) = ((ParsedTerms X)# * AR).o by FUNCT_2:def 1;
    then ts in dom (PTDenOp(o,X)) by A5,A7,A15,Th7;
    then
A18: nt-tree ts in rng (PTDenOp(o,X)) by A16,FUNCT_1:def 3;
A19: h.rs = (G | (SA.rs)) by A3;
A20: Seg len ar = dom ar by FINSEQ_1:def 3;
A21: rng ar c= the carrier of S by FINSEQ_1:def 4;
A22: ar = AR.o by MSUALG_1:def 1;
A23: dom rt = dom ts by TREES_3:def 18;
    reconsider rt as Element of OU* by A8,ZFMISC_1:87;
A24: len rt = len ar by A8,Th2;
A25: dom rt = Seg len rt by FINSEQ_1:def 3;
A26: dom p = dom ts by FINSEQ_3:120;
    then
A27: dom p = dom (S1 * ar) by A23,A14,A8,A25,A20,Th2;
A28: for x being object st x in dom (S1 * ar) holds p.x in (S1 * ar).x
    proof
      let x be object;
      assume
A29:  x in dom (S1 * ar);
      then reconsider n = x as Nat;
A30:  ts.n in rng ts by A23,A14,A24,A25,A20,A29,FUNCT_1:def 3;
      rng ts c= TS D by FINSEQ_1:def 4;
      then reconsider t = ts.n as Element of TS(D) by A30;
A31:  p.n = G.(ts.n) by A27,A29,FINSEQ_3:120;
      ar.x in rng ar by A14,A29,FUNCT_1:def 3;
      then reconsider s = ar.x as Element of S by A21;
A32:  h.s = G | (SA.s) by A3;
      dom SA = the carrier of S by PARTFUN1:def 2;
      then
A33:  SA.s in rng SA by FUNCT_1:def 3;
      dom G = TS D by FUNCT_2:def 1
        .= union rng SA by Th8;
      then
A34:  dom (h.s) = SA.s by A32,A33,RELAT_1:62,ZFMISC_1:74;
      ts in ((ParsedTerms X)# * AR).o by A5,A7,A15,Th7;
      then ts in product ((ParsedTerms X) * ar) by A22,MSAFREE:1;
      then ts.x in ((ParsedTerms X) * ar).x by A14,A17,A29,CARD_3:9;
      then
A35:  ts.x in (ParsedTerms X).(ar.x) by A14,A17,A29,FUNCT_1:12;
      then (h.s).t in S1.s by A6,A30;
      then G.t in S1.s by A35,A32,A34,FUNCT_1:47;
      hence thesis by A29,A31,FUNCT_1:12;
    end;
    set ppi = pi(o,U1,p);
A36: dom Den(o,U1) = Args(o,U1) by FUNCT_2:def 1;
A37: dom RS = O by FUNCT_2:def 1;
    Args(o,U1) = (S1# * AR).o by MSUALG_1:def 4
      .= product (S1 * ar) by A22,MSAFREE:1;
    then
A38: p in Args(o,U1) by A26,A23,A14,A24,A25,A20,A28,CARD_3:9;
    then pi(o,U1,p) = Den(o,U1).p by MSAFREE:def 21;
    then ppi in rng Den(o,U1) by A38,A36,FUNCT_1:def 3;
    then
A39: ppi in Result(o,U1) by A13;
    h.s = (G | (SA.s)) by A3;
    then
A40: (h.s).(nt-tree ts) = G.(nt-tree ts) by A9,FUNCT_1:49;
A41: rng RS c= the carrier of S by RELAT_1:def 19;
    dom SA = the carrier of S by PARTFUN1:def 2;
    then
A42: dom (SA * RS) = dom RS by A41,RELAT_1:27;
    dom S1 = the carrier of S by PARTFUN1:def 2;
    then
A43: dom (S1 *RS) = dom RS by A41,RELAT_1:27;
    rng (PTDenOp (o,X)) c= ((ParsedTerms X) * RS).o by RELAT_1:def 19;
    then nt-tree ts in (SA * RS).o by A18;
    then nt-tree ts in SA.(RS.o) by A37,A42,FUNCT_1:12;
    then
A44: nt-tree ts in SA.rs by MSUALG_1:def 2;
    G.(nt-tree ts) = ppi by A2,A5;
    then
A45: ppi = (G | (SA.rs)).(nt-tree ts) by A44,FUNCT_1:49;
    Result(o,U1) = (S1 *RS).o by MSUALG_1:def 5
      .= S1.(RS.o) by A43,A37,FUNCT_1:12
      .= S1.rs by MSUALG_1:def 2;
    then (h.rs).(nt-tree ts) in S1.s by A39,A45,A19,A12;
    hence thesis by A44,A19,A40,FUNCT_1:49;
  end;
A46: for t being Symbol of D st t in Terminals D holds P[root-tree t]
  proof
    let t be Symbol of D;
    assume
A47: t in Terminals D;
    then consider s be Element of S, x be set such that
A48: x in X.s and
A49: t = [x,s] by Th4;
    reconsider s as SortSymbol of S;
    set E = {root-tree tt where tt is Symbol of D: tt in Terminals D & tt`2 =
    s}, a = root-tree t;
A50: t`2 = s by A49;
    then
A51: a in E by A47;
    reconsider f = F.s as Function of PV.s,S1.s;
A52: dom f = PV.s by FUNCT_2:def 1;
A53: rng f c= S1.s by RELAT_1:def 19;
A54: E = PTVars(s,X) by Th28;
    then PV.s = E by Def24;
    then f.a in rng f by A51,A52,FUNCT_1:def 3;
    then
A55: f.a in S1.s by A53;
    let s1 be Element of S;
    reconsider s0=s,s11=s1 as Element of S;
    assume
A56: a in SA.s1;
    then s <= s1 by A48,A49,Th10;
    then
A57: S1O.s0 c= S1O.s11 by OSALG_1:def 16;
A58: h.s = G | (SA.s) by A3;
    then
A59: (h.s).a = G.a by A51,A54,FUNCT_1:49
      .= pi(F,S1,t) by A1,A47
      .= f.a by A47,A50,Def28;
    h.s1 = G | (SA.s1) by A3;
    then (h.s1).a = G.a by A56,FUNCT_1:49
      .= f.a by A51,A54,A58,A59,FUNCT_1:49;
    hence thesis by A55,A57;
  end;
A60: for t being DecoratedTree of the carrier of D st t in TS(D) holds P[t]
  from DTCONSTR:sch 7(A46,A4);
  for s be object st s in the carrier of S holds h.s is Function of (the
  Sorts of PTA).s, (the Sorts of U1).s
  proof
    let x be object;
    assume x in the carrier of S;
    then reconsider s = x as Element of S;
A61: dom G = TS D by FUNCT_2:def 1
      .= union rng SA by Th8;
    dom SA = the carrier of S by PARTFUN1:def 2;
    then
A62: SA.s in rng SA by FUNCT_1:def 3;
A63: h.s = G | (SA.s) by A3;
    then
A64: dom (h.s) = SA.s by A61,A62,RELAT_1:62,ZFMISC_1:74;
A65: SA.s c= dom G by A61,A62,ZFMISC_1:74;
    rng (h.s) c= S1.s
    proof
      let a be object;
      assume a in rng (h.s);
      then consider T be object such that
A66:  T in dom (h.s) and
A67:  (h.s).T = a by FUNCT_1:def 3;
      reconsider T as Element of TS(D) by A65,A64,A66,FUNCT_2:def 1;
      T in SA.s by A63,A61,A62,A66,RELAT_1:62,ZFMISC_1:74;
      hence thesis by A60,A67;
    end;
    hence thesis by A64,FUNCT_2:def 1,RELSET_1:4;
  end;
  then reconsider h as ManySortedFunction of PTA,U1 by PBOOLE:def 15;
  take h;
  thus h is_homomorphism PTA,U1
  proof
A68: dom SA = the carrier of S by PARTFUN1:def 2;
    rng RS c= the carrier of S by RELAT_1:def 19;
    then
A69: dom (SA * RS) = dom RS by A68,RELAT_1:27;
    let op be Element of the carrier' of S such that
    Args(op,PTA) <> {};
    reconsider o=op as OperSymbol of S;
    let x be Element of Args(op,PTA);
    set rs = the_result_sort_of o, DA = Den(o,PTA), D1 = Den(o,U1), OU = [:the
carrier' of S,{the carrier of S}:] \/ Union (coprod (X qua ManySortedSet of S))
    , ar = the_arity_of o;
A70: ar = AR.o by MSUALG_1:def 1;
A71: Args(o,PTA) = ((ParsedTerms X)# * AR).o by MSUALG_1:def 4;
    then reconsider p = x as FinSequence of TS(D) by Th5;
A72: OSSym(o,X) ==> roots p by A71,Th7;
    then
A73: @(X,OSSym(o,X)) = o by Def15;
A74: dom (G *p) = dom p by FINSEQ_3:120;
A75: x in ((ParsedTerms X)# * AR).o by A71;
A76: for a be object st a in dom p holds (G*p).a = (h#x).a
    proof
      set rt = roots p;
      let a be object;
A77:  dom(SA* ar) = dom ar by PARTFUN1:def 2;
A78:  [[o,the carrier of S],rt] in the Rules of D by A72,LANG1:def 1;
      assume
A79:  a in dom p;
      then reconsider n = a as Nat;
A80:  (h#x).n = (h.(ar/.n)).(x.n) by A79,MSUALG_3:def 6;
A81:  (G*p).n = G.(x.n) by A74,A79,FINSEQ_3:120;
A82:  h.(ar/.n) = G | (SA.(ar/.n)) by A3;
A83:  dom rt = dom p by TREES_3:def 18;
      reconsider rt as Element of OU* by A78,ZFMISC_1:87;
A84:  len rt = len ar by A78,Th2;
A85:  Seg len rt = dom rt by FINSEQ_1:def 3;
A86:  Seg len ar = dom ar by FINSEQ_1:def 3;
      p in product((ParsedTerms X) * ar) by A75,A70,MSAFREE:1;
      then
A87:  p.n in ((ParsedTerms X) * ar).n by A79,A77,A83,A84,A85,A86,CARD_3:9;
      ((ParsedTerms X) * ar).n = SA.(ar.n) by A79,A77,A83,A84,A85,A86,
FUNCT_1:12
        .= SA.(ar/.n) by A79,A83,A84,A85,A86,PARTFUN1:def 6;
      hence thesis by A81,A80,A82,A87,FUNCT_1:49;
    end;
    dom (h#x) = dom ar by MSUALG_3:6;
    then
A88: G*p = h#x by A74,A76,FUNCT_1:2,MSUALG_3:6;
A89: h.rs = G | (SA.rs) by A3;
A90: rng (PTDenOp (o,X)) c= ((ParsedTerms X) * RS).o by RELAT_1:def 19;
A91: dom (PTDenOp (o,X)) = ((ParsedTerms X)# * AR).o by FUNCT_2:def 1;
    (PTDenOp(o,X)).p = (OSSym(o,X))-tree p by A72,Def9;
    then (OSSym(o,X))-tree p in rng (PTDenOp(o,X)) by A71,A91,FUNCT_1:def 3;
    then
A92: (OSSym(o,X))-tree p in (SA * RS).o by A90;
    dom SA = the carrier of S by PARTFUN1:def 2;
    then
A93: SA.rs in rng SA by FUNCT_1:def 3;
    dom RS = O by FUNCT_2:def 1;
    then (OSSym(o,X))-tree p in SA.(RS.o) by A69,A92,FUNCT_1:12;
    then
A94: (OSSym(o,X))-tree p in SA.rs by MSUALG_1:def 2;
    dom G = TS D by FUNCT_2:def 1
      .= union rng SA by Th8;
    then
A95: dom (h.rs) = SA.rs by A89,A93,RELAT_1:62,ZFMISC_1:74;
    DA = (PTOper(X)).o by MSUALG_1:def 6
      .= PTDenOp(o,X) by Def10;
    then DA.x = (OSSym(o,X))-tree p by A72,Def9;
    then (h.rs).(DA.x) = G.((OSSym(o,X))-tree p) by A94,A89,A95,FUNCT_1:47
      .= pi(@(X,OSSym(o,X)),U1,G*p) by A2,A72
      .= D1.(h#x) by A73,A88,MSAFREE:def 21;
    hence thesis;
  end;
  thus h is order-sorted
  proof
    let s1,s2 being Element of S;
    assume s1 <= s2;
    then
A96: SAO.s1 c= SAO.s2 by OSALG_1:def 16;
    let a1 being set such that
A97: a1 in dom (h.s1);
A98: a1 in SAO.s1 by A97;
    then a1 in SA.s2 by A96;
    hence a1 in dom (h.s2) by FUNCT_2:def 1;
A99: h.s2 = G | (SPTA.s2) by A3;
    h.s1 = G | (SPTA.s1) by A3;
    hence (h.s1).a1 = G.a1 by A97,FUNCT_1:49
      .= (h.s2).a1 by A96,A98,A99,FUNCT_1:49;
  end;
  for x being object st x in the carrier of S holds (h || PV).x = F.x
  proof
    set hf = h || PV;
    let x be object;
    assume x in the carrier of S;
    then reconsider s = x as Element of S;
A100: dom (h.s) = SA.s by FUNCT_2:def 1;
A101: dom (hf.s) = PV.s by FUNCT_2:def 1;
A102: PV.s = PTVars(s,X) by Def24;
A103: hf.s = (h.s) | (PV.s) by MSAFREE:def 1;
A104: for a be object st a in PV.s holds (hf.s).a = (F.s).a
    proof
      let a be object;
A105: h.s = G | (SA.s) by A3;
      assume
A106: a in PV.s;
      then a in {root-tree t where t is Symbol of D: t in Terminals D & t`2 =
      s } by A102,Th28;
      then consider t be Symbol of D such that
A107: a = root-tree t and
A108: t in Terminals D and
A109: t`2 = s;
      thus (hf.s).a = (h.s).a by A103,A101,A106,FUNCT_1:47
        .= G.a by A100,A102,A106,A105,FUNCT_1:47
        .= pi(F,S1,t) by A1,A107,A108
        .= (F.s).a by A107,A108,A109,Def28;
    end;
    dom (F.s) = PV.s by FUNCT_2:def 1;
    hence thesis by A101,A104,FUNCT_1:2;
  end;
  hence thesis;
end;
