1. Home
  2. Explore
  3. Computer Science
  4. Logic and Constraint Reasoning

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Logic and Constraint Reasoning

Logic and Propositional Inference

Why logic is useful

Consider the constraints

X1+X2=10X_1 + X_2 = 10X1​+X2​=10

and

X1−X2=4.X_1 - X_2 = 4.X1​−X2​=4.

Adding the equations eliminates X2X_2X2​:

2X1=14,2X_1 = 14,2X1​=14,

so X1=7X_1 = 7X1​=7. A search algorithm could try possible values for both variables until it found a satisfying assignment, but algebra uses the equations’ structure to reach the answer directly. This illustrates a broader idea: inference can exploit structure in a problem to derive answers more efficiently than searching through possibilities.

Logic provides a formal way to represent facts and derive consequences from them. It is especially valuable when a system needs to combine information and answer questions by reasoning, rather than relying only on superficial patterns.

Logic among modeling approaches

A general AI workflow connects modeling, inference, and learning:

  • Modeling represents a problem in a form an algorithm can use.

  • Inference uses a model to answer a question.

  • Learning uses data or experience to construct or improve a model.

Different modeling approaches lead to different kinds of inference:

  • State-based models represent states as nodes and actions as transitions. Search problems seek paths, while Markov decision processes and games seek policies or strategies.

  • Variable-based models represent variables and constraints or factors over those variables. Constraint satisfaction problems seek assignments that satisfy constraints, and Bayesian networks support probabilistic queries.

  • Logic-based models represent information using formulas and derive consequences using inference rules.

Logic is highly expressive: a short formula can describe many possible situations at once. Its traditional limitations are that classical logic is deterministic, so it does not naturally represent uncertainty, and rule-based knowledge can be difficult to acquire from large amounts of data. Probability and machine learning address these limitations in different ways. The central strength of logic remains its ability to express complex relationships precisely and compactly.


Languages, syntax, semantics, and inference

Natural language is expressive but can be ambiguous. For example:

  • A dime is better than a nickel.

  • A nickel is better than a penny.

  • Therefore, a dime is better than a penny.

This reasoning appears valid because “better than” is being used consistently. But consider:

  • A penny is better than nothing.

  • Nothing is better than world peace.

  • Therefore, a penny is better than world peace.

The words “nothing” and “better” can be interpreted in different ways, so the conclusion does not follow reliably. Formal languages help prevent this kind of ambiguity.

A logical language is formal, because its valid expressions are precisely defined, and declarative, because its expressions state what is the case rather than specifying a sequence of actions to perform.

A logic has three essential components:

  • Syntax specifies which expressions are grammatically valid.

  • Semantics specifies what those expressions mean.

  • Inference rules specify how to derive new expressions from existing ones while preserving meaning.

Syntax and semantics are distinct. The expressions 2+32+32+3 and 3+23+23+2 have different syntax but the same meaning in ordinary arithmetic. Conversely, the expression 3/23/23/2 has the same syntax in different arithmetic systems, but it can have different meanings: integer division may yield 111, while real-number division yields 1.51.51.5.

A precise account of a language requires both its syntax and its semantics. The symbols alone do not determine what an expression means.


Propositional logic: syntax

Propositional logic is built from propositional symbols, also called atomic formulas. Each symbol represents a statement that can be true or false. Examples include AAA, BBB, and CCC, or named propositions such as Rain\text{Rain}Rain.

Logical connectives combine formulas:

  • Negation: ¬F\neg F¬F, meaning “not FFF”

  • Conjunction: F∧GF \land GF∧G, meaning “FFF and GGG”

  • Disjunction: F∨GF \lor GF∨G, meaning “FFF or GGG”

  • Implication: F→GF \to GF→G, meaning “if FFF, then GGG”

  • Biconditional: F↔GF \leftrightarrow GF↔G, meaning that FFF and GGG have the same truth value

Formulas are defined recursively:

  1. Every propositional symbol is a formula.

  2. If FFF and GGG are formulas, then ¬F\neg F¬F, F∧GF \land GF∧G, F∨GF \lor GF∨G, F→GF \to GF→G, and F↔GF \leftrightarrow GF↔G are formulas.

For example, AAA, ¬A\neg A¬A, and ¬B→C\neg B \to C¬B→C are formulas. The expression A¬BA \neg BA¬B is not a formula because it has no connective between its parts. The expression A+BA + BA+B is also not a formula in propositional logic because +++ is not one of its logical connectives.

The connectives are initially just symbols with syntactic rules. Their meanings are supplied by semantics.


Propositional logic: semantics

Models and possible worlds

A model in propositional logic is a complete assignment of truth values to all propositional symbols under consideration. A model can also be viewed as a possible world: it specifies which propositions are true and which are false.

With three symbols, such as AAA, BBB, and CCC, there are 23=82^3=823=8 possible models. One model might assign A=1A=1A=1, B=0B=0B=0, and C=0C=0C=0, where 111 means true and 000 means false.

Interpretation function

The interpretation function evaluates a formula in a model. It returns true if the formula is true under that assignment and false otherwise. For a propositional symbol, evaluation is a lookup in the model. More complex formulas are evaluated recursively from their parts.

The standard truth conditions for the connectives are:

  • ¬F\neg F¬F is true exactly when FFF is false.

  • F∧GF \land GF∧G is true exactly when both FFF and GGG are true.

  • F∨GF \lor GF∨G is true when at least one of FFF and GGG is true.

  • F→GF \to GF→G is false only when FFF is true and GGG is false. Equivalently, it is true when FFF is false or GGG is true.

  • F↔GF \leftrightarrow GF↔G is true when FFF and GGG have the same truth value.

These truth conditions define the semantics of propositional logic. They do not depend on an informal guess about what a connective “ought” to mean.

Formulas as sets of models

A useful way to understand a formula is as the set of all models in which it is true. Write Mod(F)\mathrm{Mod}(F)Mod(F) for the set of models satisfying FFF.

For example, Rain∨Wet\text{Rain} \lor \text{Wet}Rain∨Wet is true in every model where it is raining, where it is wet, or where both are true. It excludes only the model where neither is true.

Thus, asserting a formula narrows the possible worlds under consideration. A statement such as “there is a water bottle on the table” rules out every world in which there is no water bottle on the table.

A single formula can represent a large set of worlds. This compact representation is one source of logic’s expressive power.


Knowledge bases and their models

A knowledge base, written KB\mathrm{KB}KB, is a set of formulas representing the information currently known about the world. Its models are the worlds in which all its formulas are true:

Mod(KB)=⋂F∈KBMod(F).\mathrm{Mod}(\mathrm{KB}) = \bigcap_{F \in \mathrm{KB}}\mathrm{Mod}(F).Mod(KB)=⋂F∈KB​Mod(F).

A model of a knowledge base must satisfy every formula in it. If a new formula FFF is added, the resulting possible worlds are the intersection of the old possibilities with those satisfying FFF:

Mod(KB∪{F})=Mod(KB)∩Mod(F).\mathrm{Mod}(\mathrm{KB}\cup\{F\}) = \mathrm{Mod}(\mathrm{KB})\cap\mathrm{Mod}(F).Mod(KB∪{F})=Mod(KB)∩Mod(F).

Adding information can only leave the possible-world set unchanged or shrink it. If the new information is incompatible with what is already known, the intersection becomes empty.

Example

Suppose a knowledge base contains Rain\text{Rain}Rain and Rain→Wet\text{Rain}\to\text{Wet}Rain→Wet. Its models must make both formulas true. Since Rain\text{Rain}Rain must be true and rain implies wet, the models of the knowledge base must also make Wet\text{Wet}Wet true.


Relationships between a knowledge base and a formula

When a formula FFF is compared with a knowledge base KB\mathrm{KB}KB, three cases are important.

Entailment

The knowledge base entails FFF, written

KB⊨F,\mathrm{KB}\models F,KB⊨F,

when every model of the knowledge base is also a model of FFF:

Mod(KB)⊆Mod(F).\mathrm{Mod}(\mathrm{KB})\subseteq\mathrm{Mod}(F).Mod(KB)⊆Mod(F).

In that case, adding FFF does not narrow the possible-world set: the knowledge base already guarantees it.

For example, if the knowledge base says Rain∨Snow\text{Rain}\lor\text{Snow}Rain∨Snow and also says Snow\text{Snow}Snow, then it entails Rain∨Snow\text{Rain}\lor\text{Snow}Rain∨Snow. The disjunction was already guaranteed.

Contradiction

The knowledge base contradicts FFF when no model satisfies both:

Mod(KB)∩Mod(F)=∅.\mathrm{Mod}(\mathrm{KB})\cap\mathrm{Mod}(F)=\varnothing.Mod(KB)∩Mod(F)=∅.

Adding FFF would leave the knowledge base with no possible models. For example, a knowledge base that entails Snow\text{Snow}Snow contradicts ¬Snow\neg\text{Snow}¬Snow.

Contradiction can also be expressed as entailment of the negation:

KB contradicts F⟺KB⊨¬F.\mathrm{KB}\text{ contradicts }F \quad\Longleftrightarrow\quad \mathrm{KB}\models\neg F.KB contradicts F⟺KB⊨¬F.

Contingency

The relationship is contingent when the formula is compatible with the knowledge base but is not already guaranteed by it. The intersection is nonempty but is smaller than the original set of models:

∅≠Mod(KB)∩Mod(F)⊊Mod(KB).\varnothing \ne \mathrm{Mod}(\mathrm{KB})\cap\mathrm{Mod}(F) \subsetneq \mathrm{Mod}(\mathrm{KB}).∅=Mod(KB)∩Mod(F)⊊Mod(KB).

For instance, if the knowledge base entails Rain\text{Rain}Rain but says nothing about Snow\text{Snow}Snow, then learning Snow\text{Snow}Snow adds information without creating a contradiction.

Answering and telling

These relationships support two basic operations for a reasoning system:

  • Tell: When a new formula is supplied, the system can report that it already followed from its knowledge, that it conflicts with its knowledge, or that it adds new information.

  • Ask: For a yes-or-no question FFF, the system can answer:

    • Yes if KB⊨F\mathrm{KB}\models FKB⊨F.

    • No if KB⊨¬F\mathrm{KB}\models\neg FKB⊨¬F.

    • Unknown if neither FFF nor ¬F\neg F¬F is entailed.

A logical “yes” or “no” is conclusive relative to the knowledge base. If the answer is unknown, the system lacks enough information to determine the truth of the question.


Connection to probabilistic inference

A knowledge base identifies a set of possible worlds, while a Bayesian network assigns probabilities to worlds. This gives a probabilistic analogue of logical reasoning.

Treat the models of KB\mathrm{KB}KB as an event. Then the probability of FFF given the knowledge base can be written as

P(F∣KB)=P(F∧KB)P(KB),P(F\mid \mathrm{KB}) = \frac{P(F\land \mathrm{KB})}{P(\mathrm{KB})},P(F∣KB)=P(KB)P(F∧KB)​,

provided P(KB)>0P(\mathrm{KB})>0P(KB)>0.

The numerator is the probability mass of worlds satisfying both FFF and the knowledge base; the denominator is the probability mass of worlds satisfying the knowledge base. A probability of 111 corresponds to certainty under the distribution, while a probability of 000 corresponds to impossibility under it. Unlike purely logical entailment, probabilistic reasoning can express degrees of confidence, such as a probability of 0.90.90.9.

This connection assumes that a probability distribution over the relevant assignments is available. Learning that distribution is a separate problem from performing inference with it.


Satisfiability and model checking

A knowledge base is satisfiable if it has at least one model:

Mod(KB)≠∅.\mathrm{Mod}(\mathrm{KB})\ne\varnothing.Mod(KB)=∅.

A knowledge base with no models is unsatisfiable: its formulas cannot all be true at once.

Satisfiability provides a way to test entailment and contradiction using at most two checks:

  1. Check whether KB∪{¬F}\mathrm{KB}\cup\{\neg F\}KB∪{¬F} is satisfiable.

    • If it is unsatisfiable, then KB⊨F\mathrm{KB}\models FKB⊨F.

  2. If it is satisfiable, check whether KB∪{F}\mathrm{KB}\cup\{F\}KB∪{F} is satisfiable.

    • If that is unsatisfiable, then the knowledge base contradicts FFF.

    • If it is satisfiable, the relationship is contingent.

The first test works because KB⊨F\mathrm{KB}\models FKB⊨F exactly when there is no possible model satisfying both KB\mathrm{KB}KB and ¬F\neg F¬F.

SAT as a constraint satisfaction problem

For propositional logic, satisfiability is the classical SAT problem. It can be viewed as a constraint satisfaction problem:

  • Propositional symbols become variables.

  • Formulas become constraints.

  • A satisfying assignment is a model of the knowledge base.

A satisfiability algorithm returns “satisfiable” if it finds an assignment satisfying every formula, and “unsatisfiable” if no such assignment exists.

Algorithms

  • DPLL is a SAT algorithm based on backtracking search and pruning. It uses the structure of the formulas to avoid exploring some assignments.

  • WalkSAT is a randomized local-search algorithm. It searches through assignments, making changes intended to increase the number of satisfied clauses.

The general task of checking whether a model satisfying a knowledge base exists is called model checking.


Inference rules and derivations

Instead of reasoning over sets of models, a system can manipulate formulas directly using inference rules.

A rule has premises and a conclusion. If the premises are available in the knowledge base, the rule permits the system to add its conclusion.

Modus ponens

Modus ponens says that from PPP and P→QP\to QP→Q, one may derive QQQ:

PP→QQ.\frac{P \qquad P\to Q}{Q}.QPP→Q​.

For example, from Rain\text{Rain}Rain and Rain→Wet\text{Rain}\to\text{Wet}Rain→Wet, the system derives Wet\text{Wet}Wet.

Inference rules can be applied repeatedly:

  1. Select formulas from the knowledge base.

  2. Find a rule whose premises match those formulas.

  3. Add the rule’s conclusion to the knowledge base.

  4. Continue until no rule produces a new formula.

A derivation operates on the syntax of formulas. Write KB⊢F\mathrm{KB}\vdash FKB⊢F to mean that FFF can be mechanically derived from KB\mathrm{KB}KB using a specified set of inference rules. This differs from KB⊨F\mathrm{KB}\models FKB⊨F, which is a semantic claim about all models of the knowledge base.

For example, from Rain\text{Rain}Rain, Rain→Wet\text{Rain}\to\text{Wet}Rain→Wet, and Wet→Slippery\text{Wet}\to\text{Slippery}Wet→Slippery, repeated applications of modus ponens derive Wet\text{Wet}Wet and then Slippery\text{Slippery}Slippery. These rules do not, by themselves, derive every formula that is semantically implied by the knowledge base.


Soundness and completeness

These properties describe how well a set of inference rules relates derivation to semantic truth.

Soundness

A rule system is sound if it never derives a formula that is not entailed by the knowledge base:

KB⊢F⟹KB⊨F.\mathrm{KB}\vdash F \quad\Longrightarrow\quad \mathrm{KB}\models F.KB⊢F⟹KB⊨F.

Sound rules produce only valid conclusions. Modus ponens is sound: whenever both PPP and P→QP\to QP→Q are true, QQQ must be true.

A rule that inferred Rain\text{Rain}Rain from Wet\text{Wet}Wet and Rain→Wet\text{Rain}\to\text{Wet}Rain→Wet would be unsound. It is possible for it to be wet without raining, so those premises do not guarantee rain.

Completeness

A rule system is complete if it can derive every formula that is semantically entailed by the knowledge base:

KB⊨F⟹KB⊢F.\mathrm{KB}\models F \quad\Longrightarrow\quad \mathrm{KB}\vdash F.KB⊨F⟹KB⊢F.

A system can be sound but incomplete: every conclusion it produces may be correct, yet it may fail to produce some correct conclusions.

Modus ponens is not complete for unrestricted propositional logic. Suppose the knowledge base contains Rain\text{Rain}Rain and (Rain∨Snow)→Wet(\text{Rain}\lor\text{Snow})\to\text{Wet}(Rain∨Snow)→Wet. The knowledge base entails Wet\text{Wet}Wet, because rain makes the antecedent true. But ordinary modus ponens does not match the disjunctive antecedent with the single premise Rain\text{Rain}Rain, so it cannot derive wet in this case.

Soundness is essential because unsound rules generate false conclusions. Completeness is also desirable, because an incomplete system can miss valid conclusions. More powerful inference rules can improve completeness; for example, resolution can handle general propositional formulas.


Horn clauses and generalized modus ponens

A restricted class of formulas called Horn clauses allows modus-ponens-style reasoning to be both sound and complete for atomic queries.

Definite clauses

A definite clause has the form

(P1∧P2∧⋯∧Pk)→Q,(P_1\land P_2\land\cdots\land P_k)\to Q,(P1​∧P2​∧⋯∧Pk​)→Q,

where each PiP_iPi​ and QQQ is a propositional symbol. It says that if all the premises P1,…,PkP_1,\ldots,P_kP1​,…,Pk​ hold, then QQQ holds.

For example,

Rain∧Snow→Traffic\text{Rain}\land\text{Snow}\to\text{Traffic}Rain∧Snow→Traffic

says that rain and snow together imply traffic. Facts such as Rain\text{Rain}Rain can be treated as definite clauses with no premises.

The conclusion of a definite clause must be a single propositional symbol. A formula such as

Rain∧Snow→(Traffic∨Peaceful)\text{Rain}\land\text{Snow}\to(\text{Traffic}\lor\text{Peaceful})Rain∧Snow→(Traffic∨Peaceful)

is not a definite clause because its conclusion is a disjunction.

Horn clauses

A Horn clause is either a definite clause or a goal clause, which has the form

(P1∧⋯∧Pk)→False.(P_1\land\cdots\land P_k)\to\mathrm{False}.(P1​∧⋯∧Pk​)→False.

A goal clause states that the combination of premises is forbidden. In standard clause form, a Horn clause contains at most one positive literal.

Generalized modus ponens

For definite clauses, modus ponens generalizes to several premises:

P1⋯Pk(P1∧⋯∧Pk)→QQ.\frac{P_1\qquad \cdots\qquad P_k\qquad (P_1\land\cdots\land P_k)\to Q}{Q}.QP1​⋯Pk​(P1​∧⋯∧Pk​)→Q​.

For example, from Wet\text{Wet}Wet, Weekday\text{Weekday}Weekday, and Wet∧Weekday→Traffic\text{Wet}\land\text{Weekday}\to\text{Traffic}Wet∧Weekday→Traffic, the system derives Traffic\text{Traffic}Traffic.

For a knowledge base consisting of Horn clauses, repeated generalized modus ponens is sound and complete for determining whether an atomic proposition is entailed. In other words, if a Horn knowledge base semantically entails an atomic proposition, this inference procedure can derive it.

Example of Horn-clause inference

Suppose the knowledge base contains:

  • Rain\text{Rain}Rain

  • Rain→Wet\text{Rain}\to\text{Wet}Rain→Wet

  • Wet∧Weekday→Traffic\text{Wet}\land\text{Weekday}\to\text{Traffic}Wet∧Weekday→Traffic

  • Weekday\text{Weekday}Weekday

To answer whether there is traffic:

  1. From Rain\text{Rain}Rain and Rain→Wet\text{Rain}\to\text{Wet}Rain→Wet, derive Wet\text{Wet}Wet.

  2. From Wet\text{Wet}Wet, Weekday\text{Weekday}Weekday, and Wet∧Weekday→Traffic\text{Wet}\land\text{Weekday}\to\text{Traffic}Wet∧Weekday→Traffic, derive Traffic\text{Traffic}Traffic.

The procedure builds up propositions known to be true. It does not need to branch between alternatives.

This restriction explains why Horn clauses work well with modus ponens. If a rule concludes Traffic∨Peaceful\text{Traffic}\lor\text{Peaceful}Traffic∨Peaceful, the system cannot safely add either proposition individually: one or the other may be true, but the rule does not say which. More general inference procedures are needed to reason with such disjunctions.

A conclusion containing a conjunction can be split into separate rules. For example, A→(B∧C)A\to(B\land C)A→(B∧C) is equivalent to having both A→BA\to BA→B and A→CA\to CA→C, each of which is a definite clause.


Key distinctions

  • Syntax determines which formulas are valid; semantics determines what they mean.

  • A model is a complete truth assignment; a formula denotes the models in which it is true.

  • A knowledge base restricts possible worlds to those satisfying all its formulas.

  • Entailment means a formula is true in every model of the knowledge base; contradiction means it is true in none of them; contingency means it is true in some but not all.

  • Satisfiability asks whether at least one model exists. It can be used to answer entailment and contradiction queries.

  • Derivation uses inference rules to manipulate formulas syntactically; entailment is a semantic relationship between formulas and models.

  • Soundness means rules derive only valid conclusions; completeness means they can derive all valid conclusions.

  • Generalized modus ponens is complete for atomic queries over Horn-clause knowledge bases, while unrestricted propositional logic requires more powerful inference methods.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent