Improving Wang's algorithm in Prolog

(post deleted by author)