Prolog's powerful problem-solving capabilities can be seen with an example, the classic farmer-goat-cabbage-problem.
The problem: A farmer and his goat, wolf and cabbage come to a river that they wish to cross. There is a boat, but it only has room for two, and the farmer is the only one that can row. If the goat and cabbage get in the boat at the same time, the cabbage gets eaten. Similarly, if the wolf and goat are together without the farmer, the goat is eaten.
Devise a series of crossings of the river so that all concerned make it across safely. The state of the system is indicated by stating where the farmer, the goat the wolf and the cabbage are located. state( Farmer, Wolf, Goat, Cabbage) The problem is that a state must only be visited once, and some states are illegal. This is checked by 'unsafe' and 'member'. The Predicate "go" can be called with a start state and a final state
A farmer with his goat, wolf and cabbage come to a river that
they wish to cross. There is a boat, but it only has room for
two, and the farmer is the only one that can row. If the goat and
cabbage get in the boat at the same time, the cabbage gets eaten.
Similarly, if the wolf and goat are together without the farmer,
the goat is eaten. Devise a series of crossings of the river so
that all concerned make it across safely.
The state of the system is indicated by a structure STATE stating
where the farmer, the goat the wolf and the cabbage are located.
The goal is then how to transform the start state to the endstate
through a series of valid states.
The valid states are checked by the predicate 'unsafe'
The problem is that a state must only be visited once, this is
handled by collecing the visited stetes in a list, and checking
that a new state isnot already in the list.
The Predicate "go" can be called with a start state and
a final state
go( state(east,east,east,east), state(west,west,west,west) ).