solve([]). /* Trivially solved. */
solve([X|Xs]) :- /* You can solve a list of goals by solving */
solve(X), /* the head goal and the tail goals. */
solve(Xs).
solve(X) :- /* If there is a rule with head matching X */
rule(X, Xs), solve(Xs). /* and body Xs, then solve Xs. */
solve(X) :- /* Try asking the user. */
askable(X), /* See notes for explanation of askable. */
\+ known(X), /* Make sure answer is not already known. */
ask(X, Answer), /* Ask the user. */
process_reply(Answer, X). /* Process the reply. */
ask(X, Answer) :- /* Display the question and get the answer. */
display_query(X),
read(Answer).
process_reply(yes, X) :- /* If answer is yes, put the fact into the */
assert(true(X)). /* database. */
process_reply(no, X) :- /* If answer is no, assert that the fact */
assert(untrue(X)), /* was untrue, and force an attempt to find */
fail. /* another solution path. */
known(X) :- true(X). /* You know whether X is true or not */
known(X) :- untrue(X). /* according to whether true(X) or untrue(X)*/
/* are in the database. */
display_query(X) :- /* Ask the question rather uglily. */
write(X),
write('? ').