Prolog: 3x3 Square Picture-Puzzle Solution

I came accross a puzzle on a family weekend and decided it would be a fun challenge to ask my computer to solve it.

The aim of the puzzle is to find a 3x3 grid arrangement of the 9 tiles where the animals' heads and tails match between each adjacent tile.

This was also an exercise to refresh my memory of
Prolog
from my Comp Sci degree.

Prolog is a general purpose logic programming language associated with artificial intelligence and computational linguistics.

Prolog has its roots in first-order logic, a formal logic, and unlike many other programming languages, Prolog is declarative: the program logic is expressed in terms of relations, represented as facts and rules. A computation is initiated by running a query over these relations.

Prolog Solution

Tiles

My first step was to describe the tiles to Prolog.

I randomly arranged the tiles in a list and give each one an identification number (writing on the back in pencil).

(The order of the list does not matter and the solution to the puzzle was not yet known.)