🧩 Logic
💭 Symbolic reasoning
Not every AI problem is solved by learning from examples. Sometimes you already know the rules perfectly. Drawing up a class timetable is one such problem: a room can host only one lesson per time slot, and a student cannot attend two lessons at once. You do not need to learn those rules from old timetables (with the risk that the system picks them up half wrong). You simply write them down and let an algorithm reason about them. That gives you a timetable that is guaranteed to satisfy every rule.
That is the core of symbolic reasoning: you give an AI system explicit rules and constraints, and it derives from them what is possible and what is not. It works like a detective: you list every scenario, cross out whatever breaks the rules, and what remains are the possible solutions.
🎉 A party for picky guests
Suppose you occasionally throw a party for four friends: Anna, Bram, Caroline and David. They are rather picky: whether they come depends on who else is coming. From experience you know their habits:
- 👭 Rule 1: If Anna comes, Bram comes too. (Anna and Bram are a couple. Bram happily goes on his own, but Anna always brings Bram along.)
- 🚗 Rule 2: If Bram does not come, Caroline does not come either. (Caroline has no car and rides along with Bram.)
- 🎉 Rule 3: If both Anna and Bram come, Caroline comes too. (Caroline does not want to miss an evening with the couple.)
- 🔑 Rule 4: Caroline or David always comes. (They each have a key to the party venue and have agreed that at least one of them will come to open up.)
Every combination of attendees that agrees with all the rules is called a possible world. Try it yourself in the party planner below: click the guests who are coming and watch which rules turn green or red. There are six possible worlds. Can you find all six?
While you search, you answer these questions along the way:
- 🎯 Is there anyone who is guaranteed to come?
- 🚷 Is there anyone who never comes?
- 😬 What happens if David cannot make it?
- 🤔 Can Bram stay home?
With four guests, trial and error will do. With forty guests (or forty courses in a timetable) there are more combinations than you could ever click through, and you need a systematic approach. That is what you learn in this chapter, in four steps:
- 🔣 Writing down the rules in a notation a computer understands: propositional logic.
- 📋 Reading off all possible worlds from a truth table.
- 🤖 Searching smarter than walking through the whole table: SAT solvers.
- ⛓️ Rules with numbers and preferences (times, rooms, costs): constraint satisfaction problems.
Finally, we look at when logic is, and is not, the right tool, and why ChatGPT is no substitute for it.
🔣 Propositional logic
So far the party rules are written in ordinary sentences. A language model like ChatGPT can already solve a small puzzle like this directly in plain language, but does not guarantee that its answer is right. To let a computer reason in a way that is guaranteed correct, you need a compact and unambiguous notation: propositional logic. Do not let the symbols put you off. They are shorthand for words you already know: "not", "and", "or", "if ... then" and "if and only if".
Propositional logic is a formal system with two building blocks:
- Propositions: statements that are true (✅) or false (❌), represented by a symbol such as .
- Operators such as "and" (), "or" () and "not" (), with which you combine propositions into larger formulas.
For the party you use four propositions, one per guest:
- 👩🦰 : Anna comes to the party.
- 👨🦳 : Bram comes.
- 💃 : Caroline comes.
- 🎩 : David comes.
On top of that there are five standard operators:
- Not (, negation): means "Anna does not come".
- And (, conjunction): means "Anna and Bram both come".
- Or (, disjunction): means "Anna or Bram comes (or both)".
- If ... then (, implication): means "If Anna comes, Bram comes too".
- If and only if (, equivalence): means "Anna comes exactly when Bram comes".
The four party rules then become:
- 👭 Rule 1:
("If Anna comes, Bram comes too") - 🚗 Rule 2:
("If Bram does not come, Caroline does not come either.") - 🎉 Rule 3:
("If both Anna and Bram come, Caroline comes too.") - 🔑 Rule 4:
("Caroline or David always comes.")
📊 What the operators mean exactly
In everyday language a word like "or" is sometimes ambiguous (if someone offers you "coffee or tea", can you have both?). In propositional logic the meaning of every operator is fixed exactly in a truth table: for every combination of values of and , it states whether the formula is true or false. The table below shows all five operators side by side, each time for specific values of A and B (see the two left-hand columns):
Read each column as the rules of the game for that operator:
- is always the opposite of .
- is true only when and are both true.
- is true when at least one of the two is true, including when both are.
- is false in only one case: true and false.
- is true when and have the same value.
The implication often surprises people. Take rule 1, "If Anna comes, Bram comes too": the rule is violated only when Anna comes and Bram does not. If Anna does not come, the rule says nothing about Bram: he may come or stay home, and in both cases remains true. An implication is thus a promise that is broken only when the condition (on the left) holds and the consequence (on the right) does not.
📋 Truth tables
A truth table does systematically what you did by hand in the party planner: it walks through all the combinations and checks, rule by rule, whether each one is respected. Put all the rules together in one table, and you can read off right away which worlds are possible.
A theory is a set of logical formulas that must all be true at the same time. The possible worlds of a theory are the assignments of ✅/❌ to all propositions for which every formula of the theory is true.
Four propositions with two possible values each give combinations.
Start with Anna alone: she comes (✅) or she does not (❌), two possibilities. For each of those two, Bram can come or stay home, combinations in all. Caroline doubles that to , David to .
In general, choices with options each give combinations: you multiply by itself times. A truth table with propositions therefore has rows. When the choices have different numbers of options, you multiply those numbers: a coin (2) and a die (6) together give combinations.
Each row of the table below is one such combination: a candidate world. The four blue columns show, per rule, whether it is respected in that world. The last column connects the four rules with a ("and"): it is true only when every rule is true, and so shows whether the whole theory is true. Only the green rows (✅ in the last column) are possible worlds.
If even one rule fails, the theory is false in that world. Rule 1, for instance, crosses out every row in which Anna comes without Bram, and rule 4 crosses out the rows in which Caroline and David both stay home.
🧑🤝🧑 Who comes to the party?
Six of the sixteen candidate worlds survive all the rules, exactly the six from the party planner:
- : only David comes
- : Bram and David
- : Bram and Caroline
- : Bram, Caroline and David
- : Anna, Bram and Caroline
- : everyone
That answers the first, second and fourth question from the introduction (we will come back to the third in a moment):
- 🎯 Nobody is guaranteed to come. Even Bram, who is present in five of the six worlds, can stay home (the first world).
- 🚷 Nobody always stays away. Every guest appears in at least one possible world.
- 🤔 So Bram can be absent, but only if Anna and Caroline stay away too and David comes.
😬 What if David cannot make it?
For the third question you add a fifth rule to the theory: . The table below shows what is left then:
Only two possible worlds remain: and . From that it follows:
- 💃 Caroline has to come: without David, only she can still make rule 4 true.
- 👨🦳 Bram has to come: without him Caroline stays home (rule 2), and that is not allowed.
- 👩🦰 Anna is still free to choose: in one world she comes, in the other she does not.
Notice what happened here: one extra fact (), combined with the rules, forces two other facts to hold. That is called inference, and it is exactly what a logical AI does.
✍️ Exercise. Suppose it is not David but Caroline who cannot come. Then who is certain to come, and who is certain not to? First reason it out yourself from the four rules. Then check in the table above: click Edit formula under the table and replace with . You can combine the propositions to with , , , and ; parentheses group subformulas, which then also get a column of their own.
🤖 SAT solver
A truth table works, but it does not scale: every extra proposition doubles the number of rows. Twenty guests already give more than a million candidate worlds, and a timetable for a whole school has hundreds of variables.
Yet just now you did not need to go through the whole table to see that Bram and Caroline have to come when David stays away: you can reason it out from a combination of rules. SAT solvers do exactly that, but automatically and on an enormous scale.
The satisfiability problem asks the question: is there at least one assignment of true/false to all propositions for which all formulas of the theory are true at the same time? If so, the theory is satisfiable. If not, it is unsatisfiable: no world at all satisfies every rule.
The party theory is satisfiable, because there are six possible worlds. The theory with the two formulas and is unsatisfiable: Anna cannot come and not come at the same time. An unsatisfiable theory is not necessarily bad news. Often it is precisely the answer you are looking for: "with these requirements, no timetable exists".
🧩 Rules as clauses
SAT solvers expect every rule in a fixed form: a clause, a short list of possibilities of which at least one must be true. Each possibility is a literal: a proposition such as , or its negation such as .
The trick for writing a rule as a clause: list the ways in which the rule can be obeyed. Rule 1, "if Anna comes, Bram comes too", is respected in two ways: Anna stays home, or Bram comes. As a clause: . The same goes for the other rules:
- 👭 Rule 1: becomes (Anna stays home, or Bram comes)
- 🚗 Rule 2: becomes (Bram comes, or Caroline stays home)
- 🎉 Rule 3: becomes (Anna stays home, or Bram stays home, or Caroline comes)
- 🔑 Rule 4: is already a clause
A clause is true if at least one of its literals is true, and all clauses must be true at the same time. The rewritten rule says exactly the same as the original: in the truth table both formulas are equal in every row. A list of clauses like this is said to be in conjunctive normal form (CNF): an "and" of "or" lists. The recipe for converting any formula into it is in the in-depth section below.
✍️ Exercise. Caroline and David agree on a fifth rule: "If David comes, Anna does not come." Write that rule as a formula and as a clause.
🧾 Conjunctive normal form
A theory in which every formula is a clause is in conjunctive normal form: a conjunction () of disjunctions () of literals, such as . Every formula of propositional logic can be rewritten into that form with a handful of equivalences. Two formulas are equivalent when they have the same value in every row of the truth table.
- Eliminating implication and equivalence. An implication is a hidden disjunction: is equivalent to . An equivalence is a double implication: becomes , and then .
- Double negation. is simply .
- De Morgan's laws. You push a negation "inward" by flipping the operator: is equivalent to , and to .
- Distributivity. An "or" over an "and" splits into two clauses: is equivalent to .
The tables below put each equivalence side by side as two columns; compare them row by row. First the implication and the two De Morgan laws:
And distributivity, with three propositions:
Apply these rules repeatedly (first eliminate and , then push negations inward, then distribute) and you always end up in CNF. Rule 3 of the party, for example, goes like this: becomes , and with De Morgan .
🎮 SAT game
There are various kinds of SAT solvers, each with its own strategy. Most set propositions to true or false step by step and check which clauses are still left. Sometimes contradictions arise and the algorithm has to go back to try other choices, just as in depth-first search.
In the game below you play the SAT solver yourself. Every bar is a clause. Click a literal to make it true, and try to get all clauses true at the same time. Start with the party and then try the harder puzzles from the drop-down list. You will discover the two tricks of real solvers by yourself: as soon as a clause has only one free literal left (in yellow), that literal has to be true, and when you get stuck, you undo your last choice.
Now choose the puzzle "Party without Bram and David" in the drop-down list at the top left: no matter how you click, it will not work out. Do you dare to claim that no solution exists? Then click No solution possible and the game will check your claim.
So real SAT solvers also have to be able to conclude that there is no solution (UNSAT), and they do so surprisingly efficiently: modern solvers can handle theories with millions of clauses.
🔍 How a SAT solver searches
The tricks you discovered in the SAT game are exactly the building blocks of real solvers:
- ⚡ Unit propagation: if all but one of the literals in a clause are already false, then that last literal has to be true. That assignment can in turn reduce other clauses to a single free literal, so that a whole chain of inferences follows without a single guess. Add to the party clauses and propagation immediately sets to ❌, then to ❌ and then to ✅: only David comes.
- 🎲 Deciding: when nothing more can be inferred, the solver picks a proposition that is still free and guesses its value, preferably one that appears in many open clauses.
- ↩️ Backtracking: if a guess leads to a clause whose literals are all false, the solver undoes the last decision and tries the opposite, as in depth-first search. If there is nothing left to undo, the theory is unsatisfiable.
- 🧠 Clause learning: at every conflict, modern solvers work out which decisions led to it and add a new clause that rules out precisely that combination, so that the same conflict never shows up again anywhere in the search tree.
How hard a theory is depends mostly on the number of clauses per proposition. With few clauses there are so many possible worlds that the solver quickly finds one; with very many clauses the propagation runs into a contradiction almost immediately. In between lies a narrow zone (for random theories with three literals per clause, around 4.3 clauses per proposition) where the solver has to guess and backtrack a lot: a phase transition, like water that turns from liquid to solid at one temperature. It explains why one puzzle takes a fraction of a second and another with just as many propositions takes hours.
💥 NP-completeness
A truth table with propositions has rows. For a festival with 10,000 attendees that is , a number with more than 3,000 digits. SAT solvers avoid such tables by inferring and pruning, and in practice they solve problems with millions of variables.
Still, SAT is NP-complete (external link). In fact, it was the first problem ever proven to be NP-complete (Cook-Levin theorem (external link), 1971). That means two things. Checking a proposed solution is fast: you can verify in the blink of an eye whether a guest list respects all the rules. But there is (as far as we know) no algorithm that quickly finds a solution for every instance. Moreover, every other problem in the class NP can be translated into SAT. Anyone who found a super-fast SAT algorithm would therefore crack a whole family of hard problems at once. Nobody knows whether that is possible: that is the famous Millennium Prize problem (external link) P vs NP, with a prize of a million dollars for whoever solves it.
🔗 Proving logical consequence
The question "is Bram guaranteed to come?" is a question about a logical consequence: a statement follows logically from a theory (written ) if is true in all possible worlds of .
With a SAT solver you check that through a proof by contradiction: add the opposite to the theory and ask whether the extended theory is still satisfiable.
- If is unsatisfiable, there is no world in which fails, so .
- If is satisfiable, the world that was found is a counterexample and does not follow from .
For Bram you add to the four party rules. The solver finds the world , so is not a logical consequence: Bram may stay home.
An example where it does work: follows from and . Add and the theory becomes unsatisfiable, as the truth table shows (not a single green row):
⛓️ Constraint satisfaction problems
Propositions are true or false. But in a timetable you want to say "French is in the classroom in the morning", and at the party "Anna arrives at 18:00". For that you need variables that can take more than two values. Such problems are called constraint satisfaction problems (CSP).
A constraint satisfaction problem consists of:
- a set of variables, for example the arrival time of every guest;
- for each variable, a domain of allowed values, for example ;
- a set of constraints: rules, expressed with logic and comparisons, that say which combinations of values are allowed.
A solution assigns to every variable a value from its domain such that all constraints are satisfied at the same time.
We extend the party with an arrival time for every guest , with domain , and add three rules:
- 🚗 "If Anna and Bram both come, they carpool":
- ⏰ "If Bram and Caroline come, Bram is there at least two hours earlier":
- 🔔 "If Caroline and David come, they do not arrive at the same time":
One solution is then, for example: everyone comes, Anna and Bram at 17:00, David at 18:00 and Caroline at 19:00.
A truth table is no longer an option here. Besides the 16 combinations of attendance there are now combinations of arrival times (three possible times for each of the four guests), rows in all, full of meaningless duplicates (Anna "arriving" at 17:00, 18:00 or 19:00 while she is not even coming). That is why constraint solvers search more cleverly: they prune impossible values and fill in the variables one by one, as you did in the SAT game.
🔬 Trying out the constraint solver
Below is the party as a constraint satisfaction problem: on the left the variables with their domain, on the right the rules, at the bottom the solutions that were found. This is how you work with it:
- 🔍 The solutions at the bottom have already been computed: every row is a possible world, with who comes and when.
- ✏️ Click the pencil next to a variable or rule to change it, or + to add one. Remove an arrival time from a domain, for example, or add the rule
¬B("Bram cannot make it"). - ▶️ Then click Solve again to see which worlds remain.
🏆 Constraint optimization
Often you do not want just any valid solution but the best solution: the timetable with the fewest free periods, the delivery route with the shortest travel time, the party with the most enjoyable guests. This is called a constraint optimization problem (COP).
A constraint optimization problem is a constraint satisfaction problem plus an objective function that gives every solution a score. The solver looks for the valid solution with the highest score (or the lowest cost).
Suppose you give every guest a rating: you would love Anna to come (+10), Caroline too (+8), David is fine (+2), but you would rather not have Bram (−5). The score of a party is then simply the sum of the ratings of whoever comes. For the six possible worlds from the truth table that gives:
- : score
- : score
- : score
- : score
- : score
- : score 🏆
So the best party is the one where everyone comes, despite the negative rating for Bram: without Bram, after all, Anna and Caroline do not come. Adjust the ratings with the Utility sliders below and watch how the winning row changes:
➕ The objective function as a formula
Call the ratings , , and . Mathematically you then write the objective function as:
where equal when the person comes and when they stay home, so that the ratings of absentees drop out automatically. This weighted sum of 0/1 variables is exactly the objective function you type into the constraint solver below.
🎯 COP solver
You can enter the same objective function in the constraint solver (under Advanced › Objective function) as 10*A + -5*B + 8*C + 2*D. The solver then finds the best party, with matching arrival times.
You can also use numbers from your problem here: append + T_D to the objective function and the solver additionally has David arrive as late as possible.
🔄 Translation to propositional logic
Some constraint satisfaction problems can also be handed directly to a SAT solver. If all variables have a finite domain, you attach a new proposition to every possible value. For example: means "Anna comes at 17:00".
For that you have to add extra rules so that every variable gets exactly one value:
- At most one value: a person never has two arrival times at once, e.g. for Anna: .
- At least one value: if Anna comes, she gets an arrival time: .
This approach quickly leads to a very large number of rules. That is why in practice you usually use specialized constraint solvers that work directly with variables and domains.
🏫 Example applications
The power of constraint satisfaction problems is that the approach is declarative: you describe what the rules are, not how to search for a solution. The same solver can then tackle a timetable, a sudoku or a map-coloring puzzle, as long as you write the right model. The same pattern sits behind the exam schedules of a university, the shift rosters in a hospital, the assignment of flights to gates at an airport and the check that the design of a computer chip contains no errors.
🗓️ Timetables
A school has five subjects (Algebra, French, History, Programming and Biology), three time slots (morning, midday and afternoon) and two rooms (the classroom and the lab).
- Variables: one variable per subject that determines both the time slot and the room.
- Domain: the six combinations of time slot and room, narrowed down per subject (French only in the classroom, Biology and Programming only in the lab, for the microscopes and the computers).
- Constraints: never two subjects in the same room at the same time, and a teacher with several subjects does not teach them at the same moment.
- Objective function (optional): as many lessons as possible at midday, so that there are fewer free periods.
Click Show to look at a timetable that was found, and then change a rule or domain (pencil) to see how the timetable changes.
🔲 Sudoku and map coloring
Two classics from the puzzle world are constraint satisfaction problems as well.
A sudoku (external link) asks you to give every cell a digit without duplicates in a row, column or block. To keep things manageable we take a 4x4 version with the digits 1 to 4. Real solvers tackle the 9x9 puzzle with 81 variables in exactly the same way.
- Variables: the 16 cells R1C1 (row 1, column 1) to R4C4.
- Domain: the digits 1 to 4.
- Constraints: all values different within each row, column and 2x2 block, plus the digits that are filled in beforehand.
In map coloring, neighboring countries must get a different color. For Belgium, the Netherlands, France, Germany and Luxembourg with four colors:
- Variables: one color per country.
- Domain: red, green, blue or yellow.
- Constraints: for every pair of neighboring countries (e.g. Belgium and the Netherlands) an inequality between their colors.
Below you will find both puzzles in the same solver; use the drop-down list at the top to switch between the sudoku and the map. In the sudoku, the cells that a rule fixes are shown in gray. Two things to try:
- 🔲 Sudoku: remove some of the rules that fix digits (the last five rules in the right-hand column: click the pencil and then Delete) and click Solve: the puzzle gets several solutions, which you then see side by side.
- 🗺️ Map: add a fifth color to every domain (pencil next to the country) and the number of solutions shoots up.
🧭 When to use logic?
All the applications above follow the same two-step pattern:
- 🧑🏫 Model. A domain expert (the school principal, the doctor, the planner) knows the rules from practice. Together with an AI expert you translate them into variables, domains and constraints.
- 🤖 Solve. You hand the model to a generic SAT or constraint solver, which finds a valid or optimal solution with clever search and inference strategies.
That way you separate the experts' knowledge from the computation. That separation is what gives logic its great strengths:
- ✅ Guarantee and explainability: every solution demonstrably satisfies all the rules, and for every rejected option you can say exactly which rule it violates. In legal, financial or safety-critical contexts ("never drive through a red light") you do not want to learn rules from examples, but to enforce them.
- 🗂️ No training data needed: you write the domain knowledge down directly. That does take work, because a lot of knowledge is implicit ("that is just how we do it") and has to be made explicit first.
- 🔁 Reusable: the same model answers a variety of questions ("can Bram be absent?", "how many rooms do I need at minimum?", "which requirement makes this timetable impossible?") without you writing a new program.
There are two limitations, though:
- 💥 Hard to scale: every extra variable enlarges the search space. Solvers prune cleverly, but in the worst case the computation time keeps growing exponentially (see the in-depth section on NP-completeness).
- 🖼️ Only for symbols: logic works with clear facts and numbers, not with raw signals such as pixels or sound. It does not tolerate noise either: one wrong fact or a typo in a rule can bring down the whole inference.
Logic and neural networks therefore complement each other: neural networks excel at perceiving (images, sound, text), logic at exact reasoning about facts.
| Property | Symbolic logic | Neural networks |
|---|---|---|
| Reasons by explicit rules? | ✅ | ❌ (estimates) |
| Transparent and explainable? | ✅ | ❌ (black box) |
| Guarantees a correct solution? | ✅ | ❌ |
| Robust to noise? | ❌ | ✅ |
| Works on images and audio? | ❌ | ✅ |
| Needs training data? | ❌ | ✅ |
💬 ChatGPT does not guarantee correct logic
Large language models such as ChatGPT can tackle logic puzzles too, and for our party that works fine. Give it the four rules, and the model finds the six possible worlds (translated from Dutch):
The difference from a solver, however, lies in what such a language model can guarantee, especially for larger logic problems. A SAT solver infers its answer from the rules: every world it returns demonstrably satisfies all the rules, and if there is none, it says so. A language model predicts word by word which answer is most plausible given its training data (it is still autocomplete). Usually, what is plausible is also correct. But a plain language model (without agentic capabilities) contains no truth table or solver that systematically walks through the rules.
Researchers (external link) presented language models with thousands of SAT problems, disguised as a menu problem: which dishes do you order so that everyone at the table is satisfied? Below is one such menu problem. Of the 128 possible menus, a SAT solver can show that not one satisfies all 35 people, and according to the task rules the model must then return two empty lists. It blithely presents a solution anyway, complete with a verification table (translated from Dutch):
You do not have to recompute the table to find the mistake. Take Meg: she likes bread, custard and gelato and dislikes nothing. All three end up under not orderable, so Meg is unsatisfied. Yet in her row the model writes "no problem (the rules allow this)" and marks her as satisfied. The same happens with Gia. The table looks like a check, but it is not one: it is text that looks like a check.
The solution is to combine the two, and with agentic language models that increasingly happens by itself: the language model translates the rules from a text into logic and hands them to a solver such as Z3 (external link) or a SAT solver, which delivers the answer with a guarantee. That way each does what it does best: the language model understands the text, the solver reasons.
🧪 Hands-on
In the demos, a solver searched for possible worlds for you. In this Jupyter notebook you write the rules yourself in Python. The solver can then find a solution, explain which rules clash when none exists, and pick the best solution when not every wish can be met. For this we use the CPMpy library, which offers an interface to various SAT and constraint solvers and which you can then apply to your own logic or optimization problems.

🕹️ Demos
Experiment further with the full simulators and demos featured above.




🗣️ Feedback
Just like machine learning algorithms, we also love learning from your feedback to improve the course.
Let us know how informative and how difficult you found this chapter, and where it could be better.