Java: propositional entailment by model checking
Enumerate every truth assignment to decide whether a knowledge base entails a query.
On this page
Java loops and boolean operators.
- Enumerate every truth assignment to decide whether a knowledge base entails a query.
- Predict the sample output and explain the state changes.
- Complete the practice task and check the boundary cases.
The concept
A knowledge base is a set of sentences. In propositional logic, a model assigns true or false to every symbol. A sentence is entailed by the knowledge base if it is true in every model where the whole knowledge base is true.
The program checks the rules P implies Q, Q implies R, and the fact P. It enumerates all 8 models using the bits of a counter, keeps those that satisfy the knowledge base, and tests two queries. R is true in every such model, so it is entailed, and not-R is false in all of them, so it is not.
Code example
class Main {
public static void main(String[] args) {
boolean entailsR = true;
boolean entailsNotR = true;
int models = 0;
for (int bits = 0; bits < 8; bits++) {
boolean p = (bits & 1) != 0;
boolean q = (bits & 2) != 0;
boolean r = (bits & 4) != 0;
boolean kb = (!p || q) && (!q || r) && p;
if (kb) {
models++;
if (!r) {
entailsR = false;
}
if (r) {
entailsNotR = false;
}
}
}
System.out.println("models of KB: " + models);
System.out.println("KB entails R: " + entailsR);
System.out.println("KB entails not R: " + entailsNotR);
}
}models of KB: 1 KB entails R: true KB entails not R: false
Follow the code
Open the walkthrough to inspect the example one step at a time.
Show guided walkthrough
class Main { public static void main(String[] args) { boolean entailsR = true; boolean entailsNotR = true; int models = 0; for (int bits = 0; bits < 8; bits++) { boolean p = (bits & 1) != 0; boolean q = (bits & 2) != 0; boolean r = (bits & 4) != 0; boolean kb = (!p || q) && (!q || r) && p; if (kb) { models++; if (!r) { entailsR = false; } if (r) { entailsNotR = false; } } } System.out.println("models of KB: " + models); System.out.println("KB entails R: " + entailsR); System.out.println("KB entails not R: " + entailsNotR); }}Follow the example
Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.
View the complete dry-run table
| Step / line | What happens | State after step | Output so far |
|---|---|---|---|
| Three symbols give 2³ = 8 possible models. | — | — | |
| P implies Q is the same as not-P or Q. The fact P must also hold. | only P=Q=R=true satisfies all three | — | |
| A model of the KB where R is false would disprove entailment. None exists, so this never runs. | only P=Q=R=true satisfies all three | — | |
| Exactly one model satisfies the knowledge base. | models = 1 | models of KB: 1 | |
| R holds in the only model, so not-R is not entailed. | models = 1 | models of KB: 1 KB entails R: true KB entails not R: false |
Behind the scenes
How Java processes this code
The Java compiler produces bytecode. The JVM loads the class and invokes its main method.
Cost and common mistakes
O(2ⁿ · k) time for n symbols and a knowledge base of size k.
- Model checking takes 2^n steps for n symbols, so it is only practical for small problems. SAT solvers and resolution scale better.
- Entailment is not the same as truth in one model: the query must be true in all models of the KB.
Try it yourself
What happens if you remove the fact p from the knowledge base?
Reveal answer and reasoning
More models satisfy the KB, including ones where R is false, so R is no longer entailed.