8. Searching by backtracking

You have seen backtracking many times already. When a goal fails, Prolog goes back to the last choice it made and tries the next option. In this chapter, you use it on purpose to search for solutions.

Generate and test

The simplest way to search has two steps.

  1. Generate a candidate.
  2. Test it. If the test fails, Prolog backtracks to step 1 and generates the next candidate.

Here is a search for right triangles with integer sides, such as 3, 4, 5.

The three between/3 goals generate every combination, and the last line tests it. You described what a right triangle is, and Prolog did the loops. Press Next answer to see the other triangles.

Choice points

Each goal that can give more answers leaves a choice point. When a later goal fails, Prolog returns to the newest choice point, not to the start.

In triangle/3, when the test fails, Prolog first tries the next C. When C runs out, it goes back to the next B, and then to the next A. It is the same order as three nested for loops.

A logic puzzle

Three developers each use a different framework.

  • Ann doesn't use React.
  • Ben's framework comes before Cy's in alphabetical order.
  • Cy doesn't use Vue.

Who uses which one? Generate each way to give out the frameworks, and test the clues.

  • perm/2 gives each order of a list, one per answer. It picks the first item with select/3, then orders the rest.
  • A \= B is true when A and B can't be unified. Chapter 9 says more about it.
  • A @< B compares two terms in the standard order of Prolog. For atoms, that is alphabetical order.

The query finds the only answer. Try ?- perm([a, b, c], P). to see all six orders.

The eight queens, in a smaller size

Put N queens on an N by N chess board, so that no two queens attack each other. A queen attacks along its row, its column, and its two diagonals.

Give each column one queen, and write the row of each queen in a list. For example [2, 4, 1, 3] means the queen in column 1 is in row 2, the queen in column 2 is in row 4, and so on. If the list is an order of [1, 2, ..., N], then no two queens share a row or a column. We only need to test the diagonals.

The 6 by 6 board has four solutions. Prolog finds them by testing each of the 720 orders.

Test early

Generate and test is easy to write, but it can be slow. For 8 queens there are 40,320 orders. Change 6 to 8 above, and you will see that it takes a while.

The trick is to test each part as soon as it is made. This version places the queens one by one, and checks each new queen against the ones already placed. A bad position is rejected at once, without making the rest of the board.

This one finds a solution for 8 queens very quickly. The logic is the same. Only the order of the goals changed, so that the test runs inside the generator.

Try it

  1. In the box of the logic puzzle, add the clue "Ann doesn't use Svelte" as Ann \= svelte, and run it again. What happens?
  2. Find all integers X from 1 to 100, where X mod 3 =:= 0 and X mod 5 =:= 0.
  3. Run ?- queens(8, Qs). with each of the two versions, and compare how long you wait for the first answer.
An unhandled error has occurred. Reload 🗙