Logical Agents and Propositional Logic
A knowledge-based agent keeps a store of sentences and acts by asking it what to do. To make "asking" mean something we need entailment — the relation that holds when every model of the knowledge base is a model of the query.
╌╌╌╌
The search agents of the previous module know things, but only in a thin, inflexible sense. The transition model for a puzzle is buried inside the code of a successor function: it can predict the outcome of an action, but it cannot deduce that two tiles never share a square, or that a goal is unreachable from where it stands. A knowledge-based agent takes the opposite stance. It carries an explicit store of sentences about the world, adds to that store as it perceives, and chooses actions by reasoning over it — deriving new facts from old.1 The payoff is that the same machinery accepts new goals as sentences, absorbs new information by being told, and adapts to a changed world by updating what it believes, all without rewriting the agent.
The knowledge base and the agent loop
The central component is the knowledge base, or : a set of sentences,
each written in a knowledge representation language and each asserting
something about the world. A sentence taken as given, not derived from others, is
an axiom. Two operations act on the store. Tell adds a sentence; Ask
queries what follows from what is already there. Both may involve inference —
deriving new sentences from old — subject to a single requirement: when the agent
Asks a question, the answer must
follow from what has been Telled.
Each time the agent program runs, it does three things: it Tells the what
it just perceived; it Asks the what action to take, a query whose answer
may require extensive reasoning about the current state and the outcomes of action
sequences; and it Tells the which action it chose, before executing it.
Written out, the loop is short — the intelligence is hidden inside Ask.
- 1input: , the current percept
- 2persistent: , a knowledge base; , a counter, initially
- 3
- 4
- 5
- 6
- 7return
Because Tell and Ask are defined by entailment, not by arbitrary code, the
agent admits a description at the knowledge level: we can say what it knows
and what its goals are, and predict its behavior, without knowing how the
knowledge is stored or manipulated at the implementation level. An automated
taxi that knows the only route to Marin crosses a particular bridge because it
knows that will achieve its goal — an account that holds whether the knowledge
is a linked list, a pixel map, or a net of neurons. Building such an agent can be
as simple as Telling it, one sentence at a time, what it needs to know; this is
the declarative approach to system building, in contrast to the
procedural approach that hard-codes behavior directly.
The Wumpus World
The Wumpus World is an environment where knowing things pays off: a cave of
rooms on a
grid, connected by passages. Somewhere in the cave lurks the wumpus, a beast that
eats anyone who enters its room; some rooms hold bottomless pits; and one room
holds a heap of gold. The agent starts in square facing right, can move
Forward, TurnLeft, or TurnRight, can Grab gold in its square, can Shoot
its single arrow in a straight line, and can Climb out from . It dies
entering a pit or a live wumpus's room; the goal is to fetch the gold and climb
out alive.2
The agent never sees the whole board. It gets five one-bit percepts: a stench in the wumpus's square and the four directly adjacent to it; a breeze in any square adjacent to a pit; a glitter in the square with the gold; a bump on walking into a wall; and a scream, heard everywhere, when the wumpus dies. From this local, indirect evidence the agent must infer a global map of hazards.
Trace the agent's reasoning from the start square. Its initial holds the
rules of the game, so it knows is safe. The first percept is all-None —
no breeze, no stench — so neither neighbor nor hides a pit or
wumpus; both are OK. It steps to and now feels a breeze, so some
neighbor holds a pit; the pit cannot be in the visited-safe , so it is in
or (or both) — a possibility, not a fact about either one. Rather
than gamble, the prudent agent returns and tries , where it perceives a
stench. The wumpus is nearby, but not in (safe) and not in
(else the earlier stop at would have smelled it): the wumpus must be in
. And the absence of a breeze in means has no pit, which
combined with the earlier deduction forces the pit into . That last step
is genuinely hard — it fuses evidence gathered at different times in different
squares, and it turns on a percept that did not occur. Every such conclusion,
when drawn from correct information, is guaranteed correct. That guarantee is the
whole point of logical reasoning, and the rest of this lesson makes it precise.
Logic: models and entailment
Every logic rests on two ideas. Its syntax specifies which strings of symbols
are well-formed sentences — in ordinary arithmetic, is well formed
and
is not.3 Its semantics defines the truth of
each sentence with respect to each possible world. When we want to be precise
we drop
possible world
for model: a model is a mathematical object that
fixes the truth or falsehood of every relevant sentence. If sentence is
true in model , we say satisfies , or is a model of
, and we write for the set of all models of .
That much semantics is enough to define reasoning. Entailment is the relation that holds when one sentence follows logically from another. We write to mean entails , and the definition is
In words: exactly when, in every model where is true, is true as well.4 Note the direction of the : if then is the stronger assertion, because it rules out more worlds. The relation is familiar from arithmetic — entails , since in any world where is zero, is zero too, whatever is.
Apply this to the agent's reasoning after it has felt nothing in and a breeze in . Those percepts plus the rules form the . Ask whether the three adjacent squares , , contain pits. Each may or may not, so there are candidate models. The is false in any model that contradicts what the agent knows — false wherever has a pit, since then would be breezy. Exactly three of the eight models satisfy the . Now test two conclusions: "no pit in :q and no pit in ".
Naming the three unknown squares , , and their pit variables , the eight candidate models are the eight rows of a truth table. The percepts force (so and are pit-free by ) and (so at least one of , has a pit by ). Only three rows survive: the ones with and true.
| ? | |||||
|---|---|---|---|---|---|
| F | F | T | T | T | T |
| F | T | F | T | T | F |
| F | T | T | T | T | F |
| F | F | F | F | — | — |
| T | * | * | F | — | — |
Reading the three bold -rows: is true in all three, so ; but is false in two of them, so .
By inspection, in every model where is true, is true; hence — there is no pit in , and the agent may step there safely. But in some model where is true, is false; hence — the agent cannot yet conclude anything about . This procedure, enumerating every model and checking that holds in all those where holds, is called model checking: it verifies directly.
Inference, soundness, completeness
Model checking is one inference procedure; there are others. If a procedure
can derive from we write , read is derived from by .
Two properties separate the good procedures from the
dangerous.5 Picture the consequences of as a haystack and
as a needle: entailment is the needle being in the haystack; inference
is the act of finding it.
Model checking is sound (it implements the definition of entailment) and, when the space of models is finite, complete (it examines every one). These two properties, together, are what make an inference procedure trustworthy: soundness says its conclusions are true whenever the premises are, and completeness says it never misses a conclusion the premises support. A sound procedure operating on mere syntax — bits in registers, symbols on a page — nonetheless yields conclusions that hold in the real world, provided itself is true of the world. That correspondence between symbol-shuffling and reality is the reason logic is worth building.
Propositional logic
Propositional logic is the simplest logic rich enough to show all of this at work. Its syntax is built from proposition symbols — , , , — each standing for a proposition that is true or false. Two symbols have fixed meaning: , always true, and , always false. An atomic sentence is a single symbol; complex sentences are built from simpler ones with five connectives, in order of precedence:6
| Connective | Name | Reads as |
|---|---|---|
| negation | not | |
| conjunction | and | |
| disjunction | or | |
| implication | implies / if–then | |
| biconditional | if and only if |
A literal is an atomic sentence (a positive literal) or its negation (a negative literal). An implication has a premise (or antecedent) and a conclusion (or consequent) . The precedence from highest to lowest lets mean ; parentheses override it when needed.
The semantics fixes the truth of any sentence in a model. A model here simply
assigns or to every proposition symbol; with
symbols there are models. True is true everywhere and False false
everywhere; every other symbol takes the value the model gives it. For complex
sentences, five rules compose truth from parts, in any model :
- is true iff is false in .
- is true iff both and are true in .
- is true iff either or (or both) is true in .
- is true unless is true and is false in .
- is true iff and have the same truth value in .
The same rules read as a truth table, one row per assignment to the parts:
| F | F | T | F | F | T | T |
| F | T | T | F | T | T | F |
| T | F | F | F | T | F | F |
| T | T | F | T | T | T | T |
The one row that surprises people is when is false:
implication is true whenever its antecedent is false, so 5 is even implies Sam is smart
is a true sentence of propositional logic. Read as a
promise — if , then I claim
— that is broken only when holds and
fails. That is the only false row.
A knowledge base for the Wumpus World
With semantics in hand we can encode the cave. For each square let mean there is a pit, a wumpus, a perceived breeze, a perceived stench. The immutable rules and the first two percepts become a handful of labelled sentences:7
says the start is pitless; and say a square is breezy if and only if a neighbor has a pit — the biconditional matters, since a breeze both implies and is implied by an adjacent pit; and are the observed breezes. Now the question "is there a pit in ?" is the entailment query , and we already know the answer is yes.
Model checking as an algorithm
The direct way to decide is to enumerate models. With seven
symbols in the example there are of them; is true in exactly
three, and holds in all three, so the entailment goes through.
TT-Entails? implements this as a recursive, depth-first enumeration over
assignments to the symbols — the same shape as backtracking search.8
- 1input: , a sentence; , the query
- 2a list of the proposition symbols in and
- 3return
- 4
- 5function
- 6if is empty then
- 7if then
- 8return
- 9else
- 10returnwhen KB is false, vacuously OK
- 11else
- 12
- 13
- 14return and
- 15
TT-Entails? is sound because it implements the definition of entailment
directly, and complete because it always terminates — there are only finitely
many models. Its cost is the catch: symbols give models, so the time is
, though the depth-first recursion keeps space to . Propositional
entailment is co-NP-complete, so every known inference algorithm has worst-case
exponential time. The rest of the lesson is a search for methods that win in the
common case even though they cannot win in the worst.
Theorem proving
Model checking asks about every model. Theorem proving ignores models and applies rules of inference directly to the sentences, building a proof — a chain of conclusions to the goal. When models are many but the proof is short, this is far cheaper. Three concepts related to entailment set it up.9
The three concepts are tightly linked. Validity ties to entailment through the deduction theorem, known to the ancient Greeks:
Satisfiability ties to entailment through its mirror image. A sentence is valid iff its negation is unsatisfiable, which gives the result that powers every modern prover:
Proving from by showing has no model is proof by refutation (or reductio ad absurdum): assume the negation of what you want, and derive a contradiction. Deciding entailment thus reduces to deciding unsatisfiability — a single, uniform target.
Inference rules and equivalences
A proof strings together inference rules. The best known is Modus Ponens: from and , infer .
And-Elimination infers either conjunct from a conjunction: from , infer . Every logical equivalence doubles as a pair of inference rules — the standard set includes commutativity and associativity of and , double-negation elimination , contraposition, implication elimination , biconditional elimination, De Morgan's laws , and distributivity of over and vice versa. Chaining these rules by hand, the agent can prove from through : biconditional elimination on , And-Elimination, contraposition, Modus Ponens with the percept , and finally De Morgan's law deliver — neither nor holds a pit.
A proof search over these rules can ignore irrelevant propositions no matter how many there are — the proof above never mentions the far-off squares — which is precisely why theorem proving can beat model checking. Adding a million unrelated sentences leaves the proof untouched but multiplies the models exponentially. One subtlety underwrites this: logic is monotonic. Adding sentences to can only increase the set of entailed conclusions, never retract one:
An inference rule fires the moment its premises are present; its conclusion holds regardless of whatever else the knowledge base contains.
CNF and resolution
Modus Ponens and equivalences are sound, but a knowledge base can name rules that are collectively too weak — no proof of an entailed sentence exists using only the rules on hand — so the procedure is incomplete. One inference rule fixes this. Resolution, coupled with any complete search, is a complete inference procedure for all of propositional logic.10
Resolution operates on clauses — disjunctions of literals. The full rule takes two clauses containing complementary literals (one the negation of the other) and produces a new clause with all the literals of both, minus the complementary pair:
where and are complementary. For instance resolved with yields . The resulting clause keeps only one copy of each literal, a cleanup called factoring. Soundness is quick to see: the pivot literal is either true or false; whichever it is, the surviving literals of one clause or the other must carry the disjunction's truth.
Resolution applies only to clauses, so it would seem to cover only part of the language. It covers all of it, because every propositional sentence is equivalent to a conjunction of clauses — its conjunctive normal form (CNF). The conversion is mechanical: eliminate and via their equivalences, drive inward with De Morgan's laws until it touches only literals, then distribute over . Converting runs:
The three stages — eliminate the biconditional, drive negations inward, distribute — reappear unchanged as the pipeline for first-order sentences, minus the skolemization step that only quantifiers need. It always terminates and always produces an equivalent conjunction of clauses.
The resolution algorithm
To decide , refute: show is
unsatisfiable. PL-Resolution converts to CNF, then
resolves every pair of clauses with complementary literals, adding each new clause
back into the pool. Two things can end it. If a pair resolves to the empty
clause — a disjunction of no literals, equivalent to — then
is contradictory and . If no new clause
can be added, the empty clause never appears and .
- 1input: , a sentence; , the query
- 2the set of clauses in the CNF of
- 3
- 4loop
- 5for each pair of clauses in do
- 6
- 7if contains the empty clause then
- 8returncontradiction found: KB entails
- 9
- 10if then
- 11returnno progress: KB does not entail
- 12
The algorithm terminates because only finitely many distinct clauses can be built from finitely many symbols — the factoring step is what keeps that count finite. Its completeness is the ground resolution theorem: if a set of clauses is unsatisfiable, the closure of the clauses under resolution contains the empty clause. So a resolution prover can decide for any propositional — a general, complete method, at the cost of CNF conversion and a possibly large clause pool.
A resolution refutation, worked
Take the wumpus fragment and prove by refutation. The relevant clauses of come from , (), and the added negated goal . Converting to CNF gives the three clauses derived above; the one we need is . Add (the percept ) and (the negated query). Resolution now runs a two-step spine:
The empty clause appears, so is unsatisfiable and — the same conclusion model checking reached, now by symbol-shuffling that never enumerated a single model.
Where this continues
We now have two complete ways to decide : enumerate models with
TT-Entails?, or refute with resolution. Both are sound and
complete, and both can be exponential in the worst case. What is left is the
engineering that makes inference fast in the common case, and the machinery that
turns a static knowledge base into a situated agent acting over time.
This continues in Propositional Inference and Logical Agents, which develops the linear-time Horn-clause chaining behind logic programming, the DPLL and WalkSAT solvers that make model checking practical, and the time-indexed fluents, successor-state axioms, and SATPlan reduction that let a propositional agent perceive, remember, and plan.
Footnotes
- AIMA, Ch. 7 — Logical Agents; §7.1 Knowledge-Based Agents: intelligence as reasoning over internal representations, the of sentences, and the
Tell/Askinterface with its requirement that answers follow from what was told. ↩ - AIMA, §7.2 The Wumpus World: the PEAS description, the five percepts (stench, breeze, glitter, bump, scream), and the worked sequence of deductions the agent makes from its first moves. ↩
- AIMA, §7.3 Logic: syntax versus semantics, models as mathematical fixings of truth, for the set of models of , and satisfaction. ↩
- AIMA, §7.3 Logic: entailment defined as , the pit example with , and model checking as . ↩
- AIMA, §7.3 Logic: the derivation notation , the haystack-and-needle picture, and soundness (truth-preserving) versus completeness. ↩
- AIMA, §7.4.1 Syntax; §7.4.2 Semantics: proposition symbols, the five connectives with their precedence, literals, models as truth assignments, and the truth tables. ↩
- AIMA, §7.4.3 A Simple Knowledge Base: the symbols and the sentences through encoding the immutable rules and the first percepts. ↩
- AIMA, §7.4.4 A Simple Inference Procedure:
TT-Entails?andTT-Check-All, soundness and completeness by finite enumeration, the cost, and co-NP-completeness of propositional entailment. ↩ - AIMA, §7.5 Propositional Theorem Proving: logical equivalence, validity and tautologies, satisfiability and SAT, the deduction theorem, and the refutation identity iff is unsatisfiable; §7.5.1 inference rules; monotonicity. ↩
- AIMA, §7.5.2 Proof by Resolution: the resolution rule, complementary literals, factoring, conversion to CNF,
PL-Resolutionby refutation, the resolution closure, and the ground resolution theorem. ↩
╌╌ END ╌╌