Week S2.5. Recursion and Negation-as-Failure
First-Order definite clause programming
Input: A program of FO definite rules, and a conjunctive query:
Where are all variables appearing in the FO positive atoms .
Output: If is a logical consequence of then output “Yes” and a substitution of variables in query , else output “No”.
Resolution
We perform the following algorithm to resolve:
- Set progress to .
- Set current goal to .
- While the current goal is not empty and progress is :
- Let be an atom in the current goal.
- If it exists, choose in a rule whose head unifies with .
- Let '' be the rule chosen at this step.
- If necessary, rename the variables in to not conflict.
- Compute an mgu for and the head of .
- In the current goal, replace for .
- Apply to the current goal.
- Set progress to .
- If the current goal is empty and progress is :
- Output “Yes”.
- Output the substitution for the variables in the query.
- Otherwise, output “No”.
Example 1
Consider the following example:
One can travel between any two cities x and y if there is a direct fight from x to y and there is a ticket available to travel from x to y. A ticket between x and y is available if it can be bought online or bought over the phone.
This can be encoded as:
title: Handling "And" and "Or"
Express $P$ if $A$ and $B$ and $\ldots$ and $Q$:
$$
A, B, \ldots, Q \rightarrow P
$$
Express $P$ if $A$ or $B$ or $\ldots$ or $Q$:
$$
\begin{aligned}
A &\rightarrow P \\
B &\rightarrow P \\
&\hspace{12px} \vdots \\
Q &\rightarrow P
\end{aligned}
$$Let use consider again the program with extra facts:
Thinking about choice points:

Resolution tree
For the given example above, we can produce a resolution tree as such:

Resolution with backtracking
We perform the following algorithm to resolve with backtracking:
- Set progress to .
- Set current goal to .
- While the current goal is not empty and progress is :
- Let be the first atom in the current goal.
- If it exists, choose in the first rule that has not been chosen yet for the current goal whose head unifies with .
- Let '' be the rule chosen at this step.
- If necessary, rename the variables in to not conflict.
- Compute an mgu for and the head of .
- In the current goal, replace for .
- Apply to the current goal.
- Else, if there exists a previous goal in the computation history.
- Backtrack to the previous goal individuated earlier.
- Set progress to .
- If the current goal is empty and progress is :
- Output “Yes”.
- Output the substitution for the variables in the query.
- Otherwise, output “No”.
Example 2

Never-ending resolution
Backtracking does not resolve all the possible issues.

The two programs are logically equivalent, but with the second one the resolution loops and does not give the correct answer; in particular it does not give any answer.
The way in which a program is written matters for the resolution, and this issue cannot be solved; unless we use a completely different algorithm.
Negation as failure
Definite rules do not have negated atoms. To express “someone is innocent if they are not guilty”:
- The following is not a definite rule: and furthermore definite rules cannot have negated atoms as their heads. (no rule can have as its head)
- Intuitively, someone is innocent if it is not known that they are guilty.
- So why not say that follows from a program if the query "" fails?
Closed World Assumption
Closed World Assumption
The closed world assumption is the presumption that what can currently not be shown to be true is false.
This is a different kind of negation for which we use the symbol “not”. Remember that for to be true, we need to explicitly prove ; for this reason '' is sometimes called strict negation.
For “not ” to be true, we need to show that is not known to be true; that is to say: every attempt to prove fails, there is no successful derivation tree for .
Link to original
(Predicate) Logic programming = Definite Clause Programming +
- Control (procedural) features with a selection of leftmost query atom and topmost program rule or fact
- Backtracking to choice points
- Negation as Failure (Closed World Assumption)
A predicate logic programming derivation includes the derivation trees obtained on backtracking in order to prove query and all trees attempting to prove given a query “not ”.
Recursion
Recursion is the process of repeating items in a self-similar way. Recursion in computer science is a method where the solution to a problem depends on the solutions to smaller instances of the same problem.
The smallest instance of the solution to the problem of whether one can travel from and is when there is a direct flight from to :
Otherwise, in the general case, the solution to the problem of whether one can travel from to depends on a smaller instance of the same problem - whether can one travel from to - given that there is a direct flight from to :