Rewriting prolog predicates