Logic and Constraint Reasoning


Logic and Constraint Reasoning

Simon
Let’s open with the little puzzle from the lecture: if X one plus X two is ten, and X one minus X two is four, what is X one? Seven. You can get there with algebra, but you could also imagine trying possible values for both variables until you hit an assignment that satisfies both equations. That would be backtracking search, and it would work—but it ignores the structure in the equations. The lecture’s central question is how logical inference can use structure to reach answers more efficiently.

Corey
And that example also connects to the course’s bigger picture. The course has organized problems around modeling, inference, and learning: data can be used to produce a model, and inference uses that model to answer a question. The question depends on the kind of model. For search, it might be the minimum-cost path; for an MDP or a game, a maximum-value policy; for a constraint satisfaction problem, a maximum-weight assignment; and for a Bayesian network, a probability conditioned on evidence.

Simon
Right. The modeling paradigms we’ve seen fall roughly into two groups. State-based models—search problems, MDPs, and games—describe states as nodes and actions as transitions, with costs or rewards, and ask us to find paths or policies. Variable-based models describe variables and factors that constrain their values. Logic gives us another way to model the world: with formulas and inference rules.

Corey
The lecture also gives some historical context. Logic was a dominant AI paradigm before the 1990s, with excitement comparable to the excitement around deep learning today. But classical logic is deterministic, so it doesn’t handle uncertainty naturally; probabilistic methods address that issue. And rule-based logic doesn’t naturally ingest large amounts of data to guide behavior, which is one reason machine learning became important. Logic’s enduring strength is expressiveness: it can state complicated things succinctly.

Simon
The motivating dream is an assistant that can take in information, remember it, and answer questions by reasoning over it—not just by matching a few words. For example, suppose we tell it, “All students like CS221,” and “Alice does not like CS221.” It should be able to answer, “Is Alice a student?” with “No,” if it can reason from those statements. Natural language is a convenient interface, but it can be slippery.

Corey
The lecture illustrates that slipperiness with comparisons. “A dime is better than a nickel,” and “a nickel is better than a penny,” support the conclusion that a dime is better than a penny. But “a penny is better than nothing,” and “nothing is better than world peace,” do not give us a sensible conclusion that a penny is better than world peace. The words “better than” can shift meaning, so natural language isn’t precise enough for rigorous reasoning.

Simon
That’s why we introduce a formal, declarative language: logical language. A logic needs to be expressive enough to represent knowledge about the world, and it needs to let us reason with that knowledge. It has three ingredients. Syntax says which expressions are valid; semantics says what those expressions mean; and inference rules let us manipulate formulas in ways that preserve meaning.

Corey
It helps to separate syntax from semantics. The expressions “two plus three” and “three plus two” have different syntax, but in ordinary arithmetic they have the same meaning. The expression “three divided by two” has the same syntax in two different mathematical settings, but its meaning can depend on which setting we’re using. So to define a language precisely, we need both its grammar and its interpretation.

Simon
Today’s language is propositional logic; the next lecture moves to first-order logic. The broader pattern is a trade-off: as we move to more expressive logics, we can represent more, but computation can become harder. And the aim here is not merely to teach people to manipulate one formula by hand. It’s to develop general algorithms that can operate on logic, just as inference algorithms can operate on Bayesian networks.

Corey
Let’s build propositional syntax from the ground up. Start with propositional symbols, such as A, B, or words like Rain. These are atomic formulas. Then use connectives: not, and, or, implication, and biconditional, which is also called bidirectional implication. Recursively, if F and G are formulas, then not F, F and G, F or G, F implies G, and F if and only if G are formulas too.

Simon
So A is a formula, and not A is a formula. Not B implies C is also a formula, as is a double negation. But A followed by not B with no connective between them is not a formula, and A plus B is not one in this language because plus isn’t one of its connectives. At this point, we’re defining valid strings—not relying on what the symbols seem to mean in everyday life.

Corey
That distinction matters: the connectives are symbols with a formal syntax, and their meanings come from semantics. A model in propositional logic is a complete assignment of truth values to the propositional symbols. Each symbol is true or false—often written one or zero. With three symbols, there are eight possible assignments, or models. The term “model” here means a possible assignment or world, not a model in the broader machine-learning sense.

Simon
The interpretation function connects formulas to models. It takes a formula and a model and tells us whether the formula is true in that model. For a propositional symbol, the rule is straightforward: look up its truth value in the model. For a larger formula, recursively interpret its pieces and combine their truth values according to the connective’s truth table.

Corey
For example, F and G is true only when both F and G are true. F or G is true when at least one is true. F implies G is false only in the case where F is true and G is false; it’s true otherwise. F if and only if G is true when the two sides have the same truth value. And not F simply reverses F’s truth value. Those rules completely define the semantics of propositional logic.

Simon
Here’s how to use them. Take the formula, “not A and B, if and only if C,” and a model where A is true, B is true, and C is false. Start at the leaves: A is true, so not A is false; B is true; therefore, not A and B is false. C is false too. Since the two sides of the biconditional are both false, the entire formula is true in this model.

Corey
There’s a useful way to picture the meaning of a formula: it denotes the set of models in which it’s true. Call that set M of F. A formula therefore carves out a region of all possible situations. If I say, “There is a water bottle on the table,” I rule out the worlds where there isn’t one. A short formula can describe a set of models that may be very large, which is part of logic’s expressive power.

Simon
A knowledge base, or KB, is simply a set of formulas representing what we know. Its models are the assignments that make every formula in the knowledge base true. In set terms, that’s the intersection of the models of all its formulas. As we add more facts, the set of possible worlds usually shrinks, because each new formula imposes another constraint.

Corey
For instance, suppose the knowledge base says it’s raining and that rain implies wet. The models of the knowledge base are the worlds satisfying both formulas—that is, the intersection of their model sets. The knowledge base represents our current possibilities; adding information narrows those possibilities. This gives us a precise way to ask what a new statement would do to what we already know.

Simon
There are three relationships to distinguish when a new formula F comes in. First, entailment: the knowledge base entails F if every model of the knowledge base is also a model of F. F adds no new constraint, because it was already guaranteed. For example, if we already know rain or snow, being told that rain or snow is true again contributes nothing.

Corey
Second, contradiction: the knowledge base contradicts F if there’s no model satisfying both the knowledge base and F. The new claim is incompatible with what we know. Third, contingency: the overlap is nonempty, but F wasn’t already guaranteed, so it contributes some new information without contradicting the knowledge base. Those three cases—entailed, contradicted, or contingent—cover the possible relationships.

Simon
For a virtual assistant, that distinction changes how it should respond. If you tell it something entailed already, it can say, “I already knew that.” If the statement contradicts the knowledge base, it should say it doesn’t believe the statement because it conflicts with what it knows. If it’s contingent, it can add the information. And when you ask a yes-or-no question, there are three possible answers: a definite yes if the KB entails it, a definite no if the KB contradicts it, and “I don’t know” if it’s contingent.

Corey
These are strong logical answers, not probability estimates. “Yes” means every world consistent with the knowledge base makes the answer true; “no” means none do. If some consistent worlds make it true and others don’t, the right answer is “I don’t know.” There’s also a connection to Bayesian networks: treat the knowledge base as an event, and the proportion of its probability mass that also satisfies F is the conditional probability of F given the KB.

Simon
More explicitly, that probability is the probability of the intersection of F and the KB, divided by the probability of the KB. It lies between zero and one. Probability zero corresponds to contradiction, and probability one corresponds to entailment; intermediate values express uncertainty rather than just returning “I don’t know.” But computing that requires a probability distribution over complete assignments, and learning that distribution is a separate problem.

Corey
Back to classical logic: satisfiability gives us a practical primitive. A knowledge base is satisfiable if its set of models is nonempty—in other words, if there is at least one assignment that makes all its formulas true. We can use satisfiability checks to answer questions. To test whether the KB entails F, check whether KB together with not F is satisfiable. If it isn’t, then there’s no counterexample to F, so KB entails F.

Simon
If that first check does find a model, check whether KB together with F is satisfiable. If that second combination is unsatisfiable, then F contradicts the KB. If it is satisfiable, then F is contingent: the KB allows F, but doesn’t force it. So at most two satisfiability checks distinguish all three cases.

Corey
For propositional logic, satisfiability is the SAT problem, which can be viewed as a constraint satisfaction problem. The propositional symbols become variables, the formulas become constraints, and a satisfying assignment is a model. A model-checking procedure takes the knowledge base and determines whether such an assignment exists. DPLL is a well-known method: it uses backtracking search with pruning that exploits the structure of the formulas. WalkSAT is a randomized local-search approach.

Simon
So far, we’ve been reasoning in semantic terms—by checking sets of models. Inference rules offer another route: manipulate formulas directly in syntax. A rule has premises and a conclusion. Modus ponens is the familiar example: from P and P implies Q, conclude Q. If we know it’s raining and know that rain implies wet, we can derive that it’s wet.

Corey
An inference procedure can repeatedly look for formulas that match a rule’s premises, add the rule’s conclusion to the knowledge base, and continue until no more formulas can be added. The relationship written as “KB proves F” means that a particular set of inference rules can mechanically derive F from KB. That’s different from “KB entails F,” which is defined semantically by all the models.

Simon
The distinction is important because a set of rules might not capture every semantic consequence. For example, start with rain, rain implies wet, and wet implies slippery. Repeated modus ponens derives wet and then slippery. But modus ponens by itself doesn’t derive rain implies slippery, even though that implication is a logical consequence. The rules give us a procedure; entailment describes what is true in every model.

Corey
Two properties tell us how good an inference procedure is. Soundness means every formula it derives really is entailed by the knowledge base—nothing but the truth. Completeness means it can derive every formula that is entailed—the whole truth. Ideally, a procedure has both. Soundness is especially important because an unsound procedure can produce false conclusions; an incomplete one may miss some true conclusions, even if the conclusions it does produce are valid.

Simon
Modus ponens is sound: whenever P and P implies Q are true together, Q must be true too. But it isn’t complete for arbitrary propositional logic. For example, if we know rain and also know that rain or snow implies wet, then wet is entailed. Yet basic modus ponens doesn’t know how to use the disjunction “rain or snow” to derive wet.

Corey
One way to get completeness is to restrict the kinds of formulas we allow. A definite clause has the form P one and P two and so on and P k implies Q, where Q is a single propositional symbol. It reads: if all these premises hold, then Q holds. A fact like rain can be treated as a definite clause with no premises. But a formula whose conclusion is “traffic or peaceful” isn’t a definite clause, because the right-hand side must be a single symbol.

Simon
A Horn clause is either a definite clause or a goal clause, which has the form P one and so on and P k implies false. The lecture’s key result is that generalized modus ponens is complete when the knowledge base consists of Horn clauses. Generalized modus ponens matches a collection of atomic premises against a rule and derives its single-symbol conclusion.

Corey
Here’s that process in action. Suppose the knowledge base contains rain, rain implies wet, wet and weekday implies traffic, and weekday. From rain and rain implies wet, derive wet. Then wet, weekday, and the rule wet and weekday implies traffic let us derive traffic. For Horn-clause knowledge bases, if traffic is semantically entailed, this rule-based procedure can derive it; and because the procedure is also sound, derivation and entailment line up.

Simon
The intuition is that these rules work with positive facts: when all the premises are known to be true, they let us add one definite conclusion. A disjunctive conclusion, such as traffic or peaceful, leaves alternatives to reason about, so modus ponens can’t simply add one symbol and move on. Some formulas can be rewritten into equivalent Horn-clause forms—for example, a conclusion requiring both B and C can be split into two rules, one deriving B and one deriving C.

Corey
And that points ahead to the next lecture: a more powerful rule called resolution can handle broader propositional inferences, including cases involving disjunctions. The overall lesson is that logic combines a formal syntax for statements, semantics that specifies their meaning through models, and inference procedures that reason from what is known. We can answer queries through satisfiability and model checking, or derive consequences directly with rules—provided we understand what those rules can and cannot establish.

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