The Librarian hands you a magic tome. Mathematics is the foundation of everything.
Metadata Card
- Prerequisites: High school math
- Estimated time: 50 minutes
- Core difficulty: Beginner
- Completion marker: Able to translate natural language propositions into logical expressions, construct truth tables, and understand the meaning of predicates and quantifiers
Your Progress
You enter the Mathematics Tower. The ground floor is lined with stone doors, each engraved with a passage of text. The Librarian stands before the first door and motions for you to look closely — the words on the door are not ordinary sentences, but judgment statements built from symbols.
Your Task
Before you is a judgment system: feed it a sentence, and it outputs true or false. "If it rains today, the road will get wet" — why does this reasoning hold? Your task is to convert the fuzzy expressions of human language into precise logical symbols, so that a computer can determine whether a sentence is true.
Chapter Layers
- Required reading: Propositional logic basics, truth tables, predicates and quantifiers
- Optional reading: Formalization of inference rules
Breakthrough · Origin Story
Suppose you encounter a code bug. The conditional statement if (a > 0 || b < 10) { ... } has values for a and b that meet expectations, but the code simply doesn't execute. You stare at the condition for three minutes — what you need isn't a debugger, but logic.
Every judgment a computer makes boils down to a logical operation. The conditional expressions in if, while, for, the WHERE clause in databases, the matching rules of regular expressions — they all share the same underlying logical system. This system is propositional logic.
A proposition is a declarative statement that can be judged true or false. "2 is prime" is a true proposition. "5 < 3" is a false proposition. "How are you?" is not a proposition — it cannot be evaluated as true or false.
Propositional logic studies how simple propositions are combined into compound propositions using logical connectives. The five basic connectives:
| Name | Symbol | Meaning | Code Equivalent |
|---|---|---|---|
| Negation | ¬ | "not" | !p |
| Conjunction | ∧ | "and" | p && q |
| Disjunction | ∨ | "or" | p || q |
| Implication | → | "if...then" | if (p) q |
| Biconditional | ↔ | "iff" | p == q |
Let's construct a simple example. Let p stand for "today is sunny", and q stand for "I will go running".
- p ∧ q: Today is sunny and I will go running
- p → q: If today is sunny, I will go running
- ¬p ∨ q: Either it's not sunny, or I will go running
Wait — are p → q and ¬p ∨ q actually equivalent? Let's verify:
When p is false (not sunny), p → q is true (the promise "if sunny then run" hasn't been broken). When p is true (sunny), the truth of p → q depends on q — if you actually run, the promise is kept; if you don't, it's broken.
This correspondence, written as a table, is a truth table — the compiler backend relies on this kind of table-driven equivalence transformation to simplify your conditionals, quietly turning if (a || !a) into if (true).
p q p → q ¬p ∨ q
T T T T
T F F F
F T T T
F F T TThey match perfectly. This is logical equivalence — a reliable bridge between different forms of expression.
With propositional logic, you can express "p and q", but when you encounter sentences like "all apples are fruits", it falls short. Here you need something more powerful: predicate logic.
A predicate is a judgment with a variable. IsApple(x) means "x is an apple". IsFruit(x) means "x is a fruit". Combine them:
- All apples are fruits:
∀x (IsApple(x) → IsFruit(x)) - There exists a red apple:
∃x (IsApple(x) ∧ IsRed(x))
∀ is the universal quantifier (all), ∃ is the existential quantifier (exists). These are the most frequently used logical tools in programming. The essence of a type system is predicate logic — ∀T is generics, ∃T is existential types.
Back at the debugger. The condition a > 0 || b < 10 can be written in propositional logic: p is a > 0, q is b < 10. The whole condition is p ∨ q. Negated, it's ¬p ∧ ¬q (De Morgan's law). If the code doesn't execute, either a ≤ 0 and b ≥ 10. Logic can't fix the bug for you, but it can precisely tell you the condition under which the bug occurs.
Common Pitfalls
- Misinterpreting implication (→) as causality. "p → q" is always true when p is false, and doesn't require any real-world connection between p and q. "If the moon is made of cheese, then 1+1=3" is logically a true proposition — the premise is false, so the conclusion doesn't matter.
- Confusing exclusive and inclusive "or". In everyday language, "or" has two meanings. The computer's
||is inclusive or (at least one is true), while "either...or..." is exclusive or (exactly one is true). - Quantifier scope errors.
∀x P(x) ∧ Q(x)is different from∀x (P(x) ∧ Q(x)). The former means "all x satisfy P, and Q(x) (where x might be free)" — but x in Q(x) is unconstrained, making this incorrect.
Challenge Questions
- Translate "If the server load is too high, then new requests will be rejected" into a propositional logic expression.
- Construct a truth table for (p ∧ q) ∨ r.
- Translate "Every user has a unique ID" into predicate logic.
Traveler's Notes
Logic is the mathematical expression of computational judgment. Propositional logic handles simple truth values; predicate logic introduces variables and quantification — from here, you build the capacity for precise thinking.
→ Next stop: With tools for judging truth and falsehood, the natural question is "what belongs to what set" — set theory gives you a more fundamental classification ability.