Wolf, goat and cabbage

The river crossing puzzle, with the shortest answer shown to be shortest.

wolf-goat-cabbage.js · output · proof · check · try it in the playground


The question

A farmer must get a wolf, a goat and a cabbage across a river. The boat holds the farmer and at most one passenger. Left alone, the wolf eats the goat, and the goat eats the cabbage. Finding a plan is one thing; showing that no shorter plan exists is another.

How can he do it, and what is the fewest number of crossings?


Historical background

A medieval collection of problems traditionally attributed to Alcuin includes this puzzle as problem 18. The three passengers and the boat’s small capacity already give the same constraints. The example turns the old solution into a search through safe crossings and checks the minimum length. Propositiones ad acuendos iuvenes, problem 18


What we tell eyemind: crossings

A situation, or state, lists which bank, west ('w') or east ('e'), the farmer, wolf, goat and cabbage are on, in that order. Everyone starts west: ['w', 'w', 'w', 'w'].

// A plan from a state is a safe move followed by a plan from the next state;
// everyone on the east bank needs no more moves.
fact(solution(['e', 'e', 'e', 'e'], []))
implied_by(
  solution(State, [Move, ...Rest]),
  [move(State, Move, Next), safe(Next), solution(Next, Rest)],
)

// The farmer crosses alone or with one passenger that is on his bank.
implied_by(move([X, X, Goat, Cabbage], 'wolf', [Y, Y, Goat, Cabbage]), change(X, Y))
implied_by(move([X, Wolf, X, Cabbage], 'goat', [Y, Wolf, Y, Cabbage]), change(X, Y))
implied_by(move([X, Wolf, Goat, X], 'cabbage', [Y, Wolf, Goat, Y]), change(X, Y))
implied_by(move([X, Wolf, Goat, Cabbage], 'nothing', [Y, Wolf, Goat, Cabbage]), change(X, Y))

fact(change('e', 'w'))
fact(change('w', 'e'))

// Safe when the goat is with the farmer, or with neither the wolf nor the cabbage.
implied_by(
  safe([Farmer, Wolf, Goat, Cabbage]),
  [one_eq(Farmer, Goat, Wolf), one_eq(Farmer, Goat, Cabbage)],
)
fact(one_eq(X, X, _))
fact(one_eq(X, _, X))

What we tell eyemind: the shortest plan

// Some plan of 0 to 6 crossings works.
implied_by(
  'shorter_solution',
  [in_range(0, 6, N), moves(N, Plan), solution(['w', 'w', 'w', 'w'], Plan)],
)

// once(...) stops at the first seven-crossing plan; shortest_crossing lists all.
implied_by(
  wolf_goat_cabbage_verified(7),
  [not('shorter_solution'), moves(7, Plan), once(solution(['w', 'w', 'w', 'w'], Plan))],
)
implied_by(
  shortest_crossing(Plan),
  [not('shorter_solution'), moves(7, Plan), solution(['w', 'w', 'w', 'w'], Plan)],
)

query(wolf_goat_cabbage_verified(7))
query(shortest_crossing(_))

What eyemind concludes

// Answers: each instance of a query() that holds.
wolf_goat_cabbage_verified(7)
shortest_crossing(['goat', 'nothing', 'wolf', 'goat', 'cabbage', 'nothing', 'goat'])
shortest_crossing(['goat', 'nothing', 'cabbage', 'goat', 'wolf', 'nothing', 'goat'])

Seven crossings is the minimum, and there are exactly two seven-crossing plans. Both start and end by taking the goat; they differ only in whether the wolf or the cabbage goes over first. The first plan, crossing by crossing:

  1. Take the goat over. (Wolf and cabbage are safe together.)
  2. Come back alone.
  3. Take the wolf over.
  4. Bring the goat back, or the wolf would eat it.
  5. Take the cabbage over.
  6. Come back alone.
  7. Take the goat over. Everyone is east.

The proof, line by line

eyemind --proof writes down how it reached each answer. The whole proof has 87 call tables; see the full proof. Here are its claims and a few of its tables:

// Answers: each instance of a query() that holds.
wolf_goat_cabbage_verified(7)
shortest_crossing(['goat', 'nothing', 'wolf', 'goat', 'cabbage', 'nothing', 'goat'])
shortest_crossing(['goat', 'nothing', 'cabbage', 'goat', 'wolf', 'nothing', 'goat'])

// Certificate: the derived facts and every answer of each call the questions make.
called(wolf_goat_cabbage_verified(7), [wolf_goat_cabbage_verified(7)])
called('shorter_solution', [])
called(in_range(0, 6, A), [in_range(0, 6, 0), in_range(0, 6, 1), in_range(0, 6, 2), in_range(0, 6, 3), in_range(0, 6, 4), in_range(0, 6, 5), in_range(0, 6, 6)])
// …
called(solution(['w', 'w', 'w', 'w'], [_79, _80, _81, _82, _83, _84]), [])
// …
called(moves(7, A4), [moves(7, [_105, _106, _107, _108, _109, _110, _111])])
// … 82 more called lines, 87 in all

It has two parts:

The puzzle is solved entirely by backward rules, so no forward rule derives a fact: the proof needs no steps and no listings. Clauses are numbered by their place in the program.

Read the shown tables as sentences:


Checked: what the report says

A proof is only worth something if someone else can check it. eyemind -c generates the proof and checks it against the program; --check-proof checks a saved proof. The checker never reasons by itself: it reads the clauses from the program and confirms each table in one procedure. It expands the call one level against the program, as a search would, computing tests such as gt(7, 0) and 7 − 1 = 6 itself, and answers each inner call from that call’s own table, so no table vouches for itself. For rule 17 it answers not('shorter_solution') from that call’s empty table, and once(solution(...)) from the first answer of the solution table. Every move must be legal and every state on the way safe, and a plan a table hid, or added, would show up as a disagreement. Finally the claims must be exactly the answers, and every table must have been used. This is the complete report:

// Checked answers: these follow from the input program and are every answer of its questions.
wolf_goat_cabbage_verified(7)
shortest_crossing(['goat', 'nothing', 'wolf', 'goat', 'cabbage', 'nothing', 'goat'])
shortest_crossing(['goat', 'nothing', 'cabbage', 'goat', 'wolf', 'nothing', 'goat'])

verdict('checked')

There is no shorter solution is not taken on trust: the empty table and the tables below it record the search behind it, and the checker confirms each one. “No shorter plan” is meant with respect to this program: the search covered every plan of 0 to 6 crossings the program allows. The verdict is checked: the two plans are fully checked, move by move, and so is the claim that 7 is the minimum. The claims are also every answer of the original questions.


Try it

eyemind examples/wolf-goat-cabbage.js            # the answers
eyemind --proof examples/wolf-goat-cabbage.js    # answers with their proof

Or open it in the playground. Change in_range(0, 6, N) to in_range(0, 7, N). Now “a shorter solution” includes seven-crossing plans, which exist, so not('shorter_solution') fails and eyemind reports // No answers: no query() instance holds.


Takeaway

“Here is a plan” and “no better plan exists” are different kinds of claim. eyemind records the search behind both, call by call, so that the second can be checked as well as the first.