rename_norm_term('$'(X),Subst,Res) :- !,
(member(rename(X,NewTerm),Subst) % TODO : use ord_member
-> Res=NewTerm
; Res='$'(X)).
rename_norm_term(conjunct(A,B),Subst,conjunct(SA,SB)) :- !,
rename_norm_term(A,Subst,SA), rename_norm_term(B,Subst,SB).
rename_norm_term(if_then_else(A,B,C),Subst,if_then_else(SA,SB,SC)) :- !,
rename_norm_term(A,Subst,SA), rename_norm_term(B,Subst,SB), rename_norm_term(C,Subst,SC).
rename_norm_term(equal(A,B),Subst,equal(SA,SB)) :- !,
rename_norm_term(A,Subst,SA), rename_norm_term(B,Subst,SB).
rename_norm_term(function(A,B),Subst,function(SA,SB)) :- !,
rename_norm_term(A,Subst,SA), rename_norm_term(B,Subst,SB).
rename_norm_term(field(F,B),Subst,field(F,SB)) :- !,
rename_norm_term(B,Subst,SB).
rename_norm_term(record_field(Rec,FieldName),Subst,record_field(Rec,FieldName)) :- !,
rename_norm_term(Rec,Subst,Rec).
rename_norm_term(member(A,B),Subst,member(SA,SB)) :- !,
rename_norm_term(A,Subst,SA), rename_norm_term(B,Subst,SB).
rename_norm_term(rec(F),Subst,rec(SF)) :- !,
l_rename_norm_terms(F,Subst,SF).
rename_norm_term(Nr,_,Res) :- simple_norm_term(Nr),!,Res=Nr.
rename_norm_term(QOP,Subst,RQOP) :-
quantified_op(QOP,Functor,Ids,Exprs,Bodies),!,
l_rename_norm_terms(Exprs,Subst,RExprs),
sort(Ids,SIds),
% TO DO: also ensure Subst is sorted and use linear algo
exclude(clash_with_new(SIds),Subst,NSubst),
%print(clashing(SIds,Subst,NSubst,Bodies)),nl,
l_rename_norm_terms(Bodies,NSubst,RBodies),
quantified_op(RQOP,Functor,Ids,RExprs,RBodies).
rename_norm_term(Term,Subst,RenTerm) :- functor(Term,BOP,2), bin_op(BOP),!,
functor(RenTerm,BOP,2),
arg(1,Term,A), arg(1,RenTerm,RA), rename_norm_term(A,Subst,RA),
arg(2,Term,B), arg(2,RenTerm,RB), rename_norm_term(B,Subst,RB).
rename_norm_term(Term,Subst,RenTerm) :- functor(Term,UOP,1), un_op(UOP),!,
functor(RenTerm,UOP,1),
arg(1,Term,A), arg(1,RenTerm,RA), rename_norm_term(A,Subst,RA).
rename_norm_term(Term,Subst,RenTerm) :- list_op(Term,Kind,List),!,
list_op(RenTerm,Kind,RList),
l_rename_norm_terms(List,Subst,RList).
rename_norm_term(A,_,_) :- print(uncovered_renaming(A)),nl,fail.