public TermFunction ReplaceVariablesAndConstants(Term function)
            {
                var assignment = new Substitution();

                var termsToReplace = new HashSet<Term>();
                GdlVisitors.VisitTerm(function, new GdlVisitor
                {
                    VisitConstant = constant => termsToReplace.Add(constant),
                    VisitVariable = variable => termsToReplace.Add(variable)
                });

                foreach (TermVariable var in GetVariables(function))
                    assignment.AddMapping(var, GetUnusedVariable());

                return (TermFunction)function.ApplySubstitution(assignment);
            }