How to solve it with prolog