Logic and Proof
ā¹ļø Why It Matters
Logic is the backbone of mathematical reasoning and computer science. Every algorithm, database query, and AI inference engine is built on logical foundations. Propositional logic determines whether programs are correct, circuits function properly, and knowledge bases are consistent. Understanding logic is essential for software verification, automated theorem proving, constraint satisfaction in AI, and formal reasoning in any technical discipline.
Logic gives us a formal language to distinguish truth from falsehood. When you write an if statement, you are making a logical proposition. When a search engine ranks results, it applies logical rules. When a mathematician proves a theorem, they chain logical steps. Mastering discrete logic means mastering the art of precise, unambiguous reasoning.
Propositions
DfProposition
A proposition (or statement) is a declarative sentence that is either true or false, but not both. Examples:
- "2 + 2 = 4" ā true proposition
- "The moon is made of cheese" ā false proposition
- "x > 3" ā not a proposition (x is a variable; truth depends on x)
- "Sit down!" ā not a proposition (imperative, not declarative)
DfLogical Connectives
Propositions can be combined using logical connectives to form compound propositions. The five fundamental connectives are:
| Connective | Symbol | Name | Read As |
|---|---|---|---|
| Conjunction | AND | "P and Q" | |
| Disjunction | OR | "P or Q" | |
| Negation | NOT | "not P" | |
| Conditional | IMPLIES | "if P then Q" | |
| Biconditional | IFF | "P if and only if Q" |
DfAtomic and Compound Propositions
An atomic proposition is a simple proposition with no connectives. A compound proposition is formed by combining atomic propositions with connectives. The truth value of a compound proposition is determined by the truth values of its components and the connectives used.
Logical Operators
Conjunction (AND)
Here,
- =True only when both P and Q are true
- =Propositions (each true or false)
Disjunction (OR)
Here,
- =True when at least one of P or Q is true
- =Propositions (each true or false)
Negation (NOT)
Here,
- =Flips the truth value of P
Conditional (IMPLIES)
Here,
- =False only when P is true and Q is false
- =Antecedent (hypothesis)
- =Consequent (conclusion)
ā ļø The Conditional Trap
The conditional P ā Q is counterintuitive. "If it rains, then the ground is wet" is only false when it rains and the ground is NOT wet. If it doesn't rain, the statement is true regardless of the ground's state. This is because P ā Q is defined as ¬P ⨠Q ā a false antecedent makes the whole conditional true (vacuously true).
Biconditional (IFF)
Here,
- =True when P and Q have the same truth value
Truth Tables
Truth tables exhaustively enumerate all possible input combinations and their resulting outputs. They are the primary tool for analyzing logical expressions.
Basic Truth Tables
| P | Q | |||||
|---|---|---|---|---|---|---|
| T | T | T | T | F | T | T |
| T | F | F | T | F | F | F |
| F | T | F | T | T | T | F |
| F | F | F | F | T | T | T |
Compound Example
| P | Q | R | ||
|---|---|---|---|---|
| T | T | T | T | T |
| T | T | F | T | F |
| T | F | T | F | T |
| T | F | R | F | T |
| F | T | T | F | T |
| F | T | F | F | T |
| F | F | T | F | T |
| F | F | F | F | T |
š” Building Truth Tables
For a compound proposition with n distinct atomic propositions, the truth table has rows. List all combinations systematically: the first column alternates T/F every rows, the second every rows, and so on. This ensures every combination is covered exactly once.
Logical Equivalences
Two propositions are logically equivalent if they have identical truth tables ā they are true in exactly the same cases.
ThFundamental Logical Equivalences
- Double Negation:
- Commutative Laws: and
- Associative Laws: and
- Distributive Laws: and
- Identity Laws: and
- Domination Laws: and
- Idempotent Laws: and
- Absorption Laws: and
ThImplication Equivalences
- Conditional as Disjunction:
- Contrapositive:
- Converse: is NOT equivalent to
- Inverse: is NOT equivalent to
- Biconditional:
ThDe Morgan's Laws
For any propositions P and Q:
In words: The negation of an AND is the OR of the negations. The negation of an OR is the AND of the negations.
Proof of the first law (by truth table):
| P | Q | |||||
|---|---|---|---|---|---|---|
| T | T | T | F | F | F | F |
| T | F | F | T | F | T | T |
| F | T | F | T | T | F | T |
| F | F | F | T | T | T | T |
Columns 4 and 7 are identical, confirming the equivalence. ā
š” De Morgan's in Code
De Morgan's laws are used constantly in programming to simplify conditions. Instead of if not (A and B):, write if not A or not B:. Both are equivalent, but the negated form is sometimes clearer. Compilers and linters often apply these transformations automatically.
Normal Forms
DfLiteral
A literal is an atomic proposition or its negation. Examples: , , .
DfConjunctive Normal Form (CNF)
A formula is in Conjunctive Normal Form if it is a conjunction (AND) of one or more clauses, where each clause is a disjunction (OR) of literals.
Example:
DfDisjunctive Normal Form (DNF)
A formula is in Disjunctive Normal Form if it is a disjunction (OR) of one or more terms, where each term is a conjunction (AND) of literals.
Example:
ThConversion to CNF
Every proposition can be converted to CNF using these steps:
- Eliminate using
- Eliminate using
- Push negations inward using De Morgan's laws and double negation
- Distribute over using ā wait, distribute over for CNF
- Distribute over using
šConverting to CNF
Convert to CNF.
Step 1: Eliminate implication:
This is already in CNF: a conjunction of and , each a disjunction of literals.
šConverting to CNF (Complex)
Convert to CNF.
Step 1: Eliminate implication: Step 2: Push negation (De Morgan's): Step 3: Push negation again:
Result: ā already in CNF.
Validity and Satisfiability
DfTautology
A tautology is a proposition that is true under every possible assignment of truth values to its atomic propositions. Example: is always true.
DfContradiction
A contradiction is a proposition that is false under every possible assignment. Example: is always false.
DfContingency
A contingency is a proposition that is neither a tautology nor a contradiction ā it is true for some assignments and false for others.
DfSatisfiability
A proposition is satisfiable if there exists at least one truth assignment that makes it true. A tautology is satisfiable, but a satisfiable formula is not necessarily a tautology. A contradiction is unsatisfiable.
SAT Problem: Given a propositional formula, determine whether it is satisfiable. This is the first problem proven to be NP-complete (Cook-Levin theorem, 1971), one of the most important results in computational complexity.
ā¹ļø SAT Solvers in Practice
Despite SAT being NP-complete, modern SAT solvers (MiniSat, CaDiCaL, Kissat) can handle formulas with millions of variables. They use techniques like unit propagation, pure literal elimination, and conflict-driven clause learning (CDCL). SAT solvers are used in hardware verification, software testing, AI planning, and constraint optimization.
Python Implementation
from itertools import product
def truth_table(propositions, expr_func, labels):
"""Generate a truth table for a logical expression."""
n = len(propositions)
header = " | ".join(labels) + " | Result"
print(header)
print("-" * len(header))
for combo in product([True, False], repeat=n):
vals = dict(zip(propositions, combo))
result = expr_func(**vals)
row = " | ".join(
f"{vals[p]:^5}" for p in propositions
) + f" | {result:^6}"
print(row)
# --- Basic Operators ---
def AND(p, q): return p and q
def OR(p, q): return p or q
def NOT(p): return not p
def IMPLIES(p, q): return (not p) or q
def IFF(p, q): return p == q
# --- Verify De Morgan's Laws ---
def verify_demorgan(n_tests=10000):
import random
for _ in range(n_tests):
A = random.randint(0, 1)
B = random.randint(0, 1)
lhs_or = not (A or B)
rhs_or = (not A) and (not B)
lhs_and = not (A and B)
rhs_and = (not A) or (not B)
assert lhs_or == rhs_or, "De Morgan OR violated!"
assert lhs_and == rhs_and, "De Morgan AND violated!"
print("De Morgan's laws verified!")
# --- Tautology Checker (brute force) ---
def is_tautology(propositions, expr_func):
"""Check if expr is true for all truth assignments."""
for combo in product([True, False], repeat=len(propositions)):
vals = dict(zip(propositions, combo))
if not expr_func(**vals):
return False
return True
# --- SAT Solver (simple DPLL-style) ---
def solve_dpll(clauses, assignment=None):
"""Simple DPLL satisfiability checker.
clauses: list of tuples, each tuple is a clause (disjunction of literals).
Literals are (variable_name, is_positive).
"""
if assignment is None:
assignment = {}
# Simplify clauses based on current assignment
simplified = []
for clause in clauses:
satisfied = False
new_clause = []
for (var, pos) in clause:
if var in assignment:
if assignment[var] == pos:
satisfied = True
break
else:
new_clause.append((var, pos))
if not satisfied:
simplified.append(new_clause)
# Remove clauses that are subsets of others (not needed for correctness)
# Check for empty clause (contradiction)
if any(len(c) == 0 for c in simplified):
return None
# All clauses satisfied
if len(simplified) == 0:
return assignment
# Unit propagation: if a clause has one literal, assign it
for clause in simplified:
if len(clause) == 1:
var, pos = clause[0]
if var not in assignment:
new_assign = dict(assignment)
new_assign[var] = pos
return solve_dpll(simplified, new_assign)
# Choose an unassigned variable and branch
all_vars = set()
for clause in simplified:
for (var, _) in clause:
all_vars.add(var)
unassigned = [v for v in all_vars if v not in assignment]
if not unassigned:
return assignment
var = unassigned[0]
for val in [True, False]:
new_assign = dict(assignment)
new_assign[var] = val
result = solve_dpll(simplified, new_assign)
if result is not None:
return result
return None
# --- CNF Conversion ---
def to_cnf(p, q):
"""Convert P ā Q to CNF and print."""
# P ā Q ┠¬P ⨠Q, which is already CNF
print(f" P ā Q ┠¬P ⨠Q (CNF)")
# Verify equivalence
for pv, qv in product([True, False], repeat=2):
impl = IMPLIES(pv, qv)
cnf = OR(NOT(pv), qv)
assert impl == cnf, f"CNF mismatch for P={pv}, Q={qv}"
print(" Verified: P ā Q ┠¬P ⨠Q")
if __name__ == "__main__":
print("=== Truth Table: P ā Q ===")
truth_table(["P", "Q"], IMPLIES, ["P", "Q"])
print("\n=== Truth Table: P ā Q ===")
truth_table(["P", "Q"], IFF, ["P", "Q"])
print("\n=== De Morgan's Laws ===")
verify_demorgan()
print("\n=== Tautology Check ===")
print(f"P ⨠¬P is tautology: {is_tautology(['P'], lambda P: OR(P, NOT(P)))}")
print(f"P ⧠¬P is tautology: {is_tautology(['P'], lambda P: AND(P, NOT(P)))}")
print(f"(P ā Q) ā (¬Q ā ¬P) is tautology: {is_tautology(['P', 'Q'], lambda P, Q: IFF(IMPLIES(P,Q), IMPLIES(NOT(Q), NOT(P))))}")
print("\n=== CNF Conversion ===")
to_cnf("P", "Q")
print("\n=== SAT Solver ===")
# (P ⨠Q) ⧠(¬P ⨠Q) ⧠(P ⨠¬Q)
clauses_sat = [
[("P", True), ("Q", True)],
[("P", False), ("Q", True)],
[("P", True), ("Q", False)],
]
result = solve_dpll(clauses_sat)
print(f"SAT: {clauses_sat}")
print(f"Satisfying assignment: {result}")
# (P) ā§ (¬P) ā unsatisfiable
clauses_unsat = [
[("P", True)],
[("P", False)],
]
result = solve_dpll(clauses_unsat)
print(f"\nSAT: {clauses_unsat}")
print(f"Result: {result}")
Applications in AI/ML
1. Logic Programming (Prolog)
Logic programming languages like Prolog encode knowledge as logical formulas and use unification and resolution to answer queries. A Prolog program is a set of Horn clauses (implications with a single consequent):
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
This directly encodes the logical rule: "X is an ancestor of Y if X is a parent of Y, or if X is a parent of Z and Z is an ancestor of Y."
2. Constraint Satisfaction Problems (CSPs)
Many AI problems are modeled as CSPs, where variables must satisfy logical constraints:
- Scheduling: Staff shifts must cover all hours, no person works two shifts at once
- Map coloring: Adjacent regions must have different colors
- Sudoku: Each row, column, and box contains each digit exactly once
CSP solvers use logical inference (arc consistency, forward checking) combined with search to find satisfying assignments.
3. Knowledge Representation and Reasoning
Description logics and OWL (Web Ontology Language) represent knowledge using logical constructs. Automated reasoners check consistency of ontologies and infer new facts:
- "All humans are mortal" + "Socrates is human" ā "Socrates is mortal"
- OWL reasoners detect contradictions in biomedical ontologies with millions of axioms
4. Hardware Verification
SAT solvers and model checkers verify that digital circuits satisfy their specifications. A circuit with inputs can be modeled as a Boolean formula; satisfiability of its negation indicates a bug.
5. AI Planning (SATPlan)
The planning problem ā finding a sequence of actions to reach a goal ā can be encoded as SAT. Each action and time step generates Boolean variables, and constraints ensure action preconditions are met. Modern planners encode planning as SAT and use CDCL solvers to find plans efficiently.
6. Neural Network Verification
Verifying that a neural network's output is correct within input bounds (e.g., a self-driving car always stops at red) is formulated as a satisfiability problem over piecewise-linear functions.
Common Mistakes
| Mistake | Why It's Wrong | Correct Approach |
|---|---|---|
| Confusing with | means "if P then Q"; means "P if and only if Q" | Use for one-directional implication, for equivalence |
| Thinking means P causes Q | is true whenever P is false, regardless of Q | is equivalent to , not "P causes Q" |
| Assuming | This is false. | Negation of implication: |
| Forgetting is true when P is false | A false antecedent makes the conditional vacuously true | Check truth table: and |
| Confusing De Morgan's: | Wrong! The negation of OR becomes AND | De Morgan's: |
| Thinking distributes over the same way | Both distribute, but the result is different | ; |
| Confusing contrapositive with converse | Contrapositive: . Converse: (NOT equivalent) | Only contrapositive is logically equivalent to the original |
| Writing | This is the most common De Morgan error | (AND becomes OR) |
Interview Questions
Q1: What is the truth table for the conditional? Why is "if False then True" true?
š”Answer
The conditional P ā Q is false only when P is true and Q is false. All other cases are true:
| P | Q | P ā Q |
|---|---|---|
| T | T | T |
| T | F | F |
| F | T | T |
| F | F | T |
"If False then True" is true because the conditional makes a promise only about what happens when P is true. If P is false, the promise is not broken ā it's vacuously true. Think of it as a contract: "If it rains, I'll bring an umbrella." If it doesn't rain, you haven't broken the contract regardless of whether you bring an umbrella.
Q2: Prove that the conditional and biconditional are not equivalent.
š”Answer
Consider P = True, Q = False:
Now consider P = False, Q = True:
Since they differ when P = F and Q = T, they are not equivalent. The conditional is asymmetric (PāQ ā QāP), while the biconditional is symmetric.
Q3: Convert to CNF.
š”Answer
Step 1: Eliminate :
Step 2: De Morgan's:
Step 3: De Morgan's:
CNF:
This is a conjunction of two clauses: and .
Q4: Explain the SAT problem and why it matters for computer science.
š”Answer
The SAT problem asks: given a propositional formula, does there exist a truth assignment that makes it true? Cook and Levin proved SAT is NP-complete in 1971, meaning:
- Any problem in NP can be reduced to SAT in polynomial time
- If SAT has a polynomial-time solution, then P = NP
- SAT serves as a "universal" NP problem ā solving it efficiently would solve all NP problems efficiently
Despite this worst-case hardness, modern SAT solvers are extremely fast in practice for structured problems. They are used in hardware verification (Intel, AMD verify CPUs with SAT), software testing (symbolic execution), AI planning, and constraint optimization.
Q5: What is the difference between tautology, satisfiability, and validity?
š”Answer
- Tautology: True for ALL truth assignments (e.g., P ⨠¬P). Always satisfiable.
- Contradiction: False for ALL truth assignments (e.g., P ⧠¬P). Never satisfiable.
- Contingency: True for some, false for others (e.g., P ⨠Q). Satisfiable but not a tautology.
- Satisfiable: True for AT LEAST ONE assignment. Includes tautologies and contingencies.
- Valid: A formula is valid if it is a tautology ā true under every interpretation.
A formula is valid āŗ its negation is unsatisfiable āŗ it is a tautology.
Q6: How does De Morgan's law apply to programming?
š”Answer
De Morgan's laws simplify complex boolean conditions:
# Instead of:
if not (age >= 18 and has_id):
deny()
# Write:
if age < 18 or not has_id:
deny()
Both are equivalent. The transformed version is often more readable because each condition is simpler. Compilers and static analyzers use De Morgan's laws to simplify expressions, optimize condition checks, and improve error message generation.
Practice Problems
Problem 1: Truth Table Construction
šPractice Problem 1
Construct the truth table for . What does this formula represent?
š”Solution
| P | Q | PāQ | QāP | (PāQ)ā§(QāP) |
|---|---|---|---|---|
| T | T | T | T | T |
| T | F | F | T | F |
| F | T | T | F | F |
| F | F | T | T | T |
This is ā the biconditional. It's true exactly when P and Q have the same truth value.
Problem 2: Logical Equivalence
šPractice Problem 2
Show that is logically equivalent to .
š”Solution
Left side:
Right side:
Both simplify to , so they are equivalent. ā
Problem 3: CNF Conversion
šPractice Problem 3
Convert to CNF.
š”Solution
Step 1: Eliminate :
Step 2: Distribute over :
CNF: ā a conjunction of two clauses, each a disjunction of two literals.
Problem 4: Satisfiability
šPractice Problem 4
Determine whether is satisfiable.
š”Solution
Try all four assignments:
- P=T, Q=T:
- P=T, Q=F:
- P=F, Q=T:
- P=F, Q=F:
No assignment satisfies the formula. It is unsatisfiable (a contradiction).
Problem 5: Prove Using Logical Equivalences
šPractice Problem 5
Prove that is logically equivalent to using equivalences only (no truth tables).
š”Solution
(conditional as disjunction)
(commutativity of )
(double negation on Q)
(conditional as disjunction, in reverse)
Wait ā this gives , not . Let me redo:
We want
Both equal . ā
Quick Reference
| Concept | Formula / Definition | Key Insight |
|---|---|---|
| AND () | True only when both true | |
| OR () | False only when both false | |
| NOT () | Flips truth value | |
| IMPLIES () | False only when T ā F | |
| IFF () | True when same truth value | |
| De Morgan (OR) | Negation of OR is AND of negations | |
| De Morgan (AND) | Negation of AND is OR of negations | |
| Contrapositive | Equivalent to original conditional | |
| CNF | AND of ORs | |
| DNF | OR of ANDs | |
| Tautology | Always true | Example: |
| Contradiction | Always false | Example: |
| Satisfiable | True for some assignment | Tautologies are satisfiable |
| SAT Problem | Is a formula satisfiable? | First NP-complete problem |
Cross-References
- Set Theory ā set operations and De Morgan's laws in the set context ā the logical operators and set operations share identical algebraic structure (AND ā Intersection, OR ā Union, NOT ā Complement)
- Boolean Algebra ā Boolean algebra, switching circuits, and gate-level implementations of logical operators
- Combinatorics ā counting techniques used in logical enumeration and proof by exhaustion
- Graph Theory ā logical constraints in graph coloring and path problems
- Automata Theory ā how logical formulas define regular languages and finite automata
- Information Theory ā the connection between logical uncertainty and Shannon entropy
- Discrete Mathematics Applications ā applied uses of logic in cryptography, coding theory, and verification