Knowing without seeing
In the wumpus world, an agent explores a dark cave of squares. Some squares hide bottomless pits; next to a pit you feel a breeze. Standing in square [1,1] and feeling no breeze, can you safely step to [1,2]? You can't see it — but you can reason to the answer. A knowledge-based agent keeps facts in a knowledge base (KB) and derives new facts from them.
Today you'll build the core of such an agent in propositional logic: evaluate formulas in a possible world, then decide whether a conclusion follows from the KB by checking every possible world — the truth-table method from the textbook.
Formulas and models
A propositional formula is built from symbols like P or B11 ("breeze in [1,1]") with ¬ (not), ∧ (and), ∨ (or), ⇒ (implies) and ⇔ (if and only if). A model sets every symbol to true or false — one possible world. We write formulas as nested arrays so a function can walk them recursively.
// B11 ⇔ (P12 ∨ P21): breeze in [1,1] exactly when a pit is next to it
const rule = ["iff", "B11", ["or", "P12", "P21"]];
holds(rule, { B11: false, P12: false, P21: false }); // true
holds(rule, { B11: false, P12: true, P21: false }); // false⇒ surprises people: P ⇒ Q is only false when P is true and Q is false. If P is false, the implication holds whatever Q is.