Learning Outcomes
SLO | After this lecture you will be able to... | CLO | Bloom |
|---|---|---|---|
SLO 6.1 | Identify propositions and differentiate atomic from compound propositions | CLO2 | C2 Understand |
SLO 6.2 | Translate English sentences into propositional logic using the five connectives | CLO2 | C3 Apply |
SLO 6.3 | Build a truth table for a compound proposition using operator precedence | CLO2 | C3 Apply |
SLO 6.4 | Classify formulas as tautology, contradiction or contingency and prove equivalences | CLO2 | C3 Apply |
SLO 6.5 | Decide whether a knowledge base entails a sentence by model checking | CLO2 | C3 Apply |
CLO2: Demonstrate first-order predicate calculus, logical reasoning and problem-solving using Prolog language.
1. Logic and Propositions
§1.1 What logic is
Slide L06-S06
Logic is a formal (exact, rule-based) language for writing knowledge. It has two parts. Syntax is the grammar: which sentences are allowed ('P ∧ Q' is allowed, 'P ∧ ∧ Q' is not). Semantics is the meaning: in which situations a sentence is true or false. Propositional logic (PL) is the simplest logic. It is also called propositional calculus, Boolean logic, sentential logic or statement logic, because it works with whole statements that are true (1) or false (0). It fixes a weakness of Lecture 5: semantic networks could not say NOT or OR exactly, but logic can.
§1.2 Propositions
Slide L06-S07
Definition: A proposition is a declarative sentence (a statement) that is either true or false, but not both. |
Sentence | Proposition? | Reason |
|---|---|---|
Burewala is in Vehari district. | Yes | True |
2 + 2 = 5 | Yes | False - a false statement is still a proposition |
Pakistan won the 1992 Cricket World Cup. | Yes | True |
Close the door. | No | A command |
Is there load-shedding today? | No | A question |
What a beautiful campus! | No | An exclamation / opinion |
x + 2 = 5 | Not yet | Its value depends on x (handled by predicate logic, Lecture 7) |
§1.3 Atomic and compound propositions
Slide L06-S08
An atomic proposition is one simple statement written with one symbol (P, Q, or a meaningful letter). A compound proposition joins atomic ones with connectives and brackets. Always write a key first. Key: L = there is load-shedding, U = the UPS is charged, F = the fan runs. Then 'There is load-shedding and the UPS is charged' is L ∧ U, and 'If there is load-shedding and the UPS is charged, the fan runs' is (L ∧ U) → F. The truth of a compound proposition depends only on the truth of its atoms.
2. Connectives and Translation
§2.1 The five connectives
Slide L06-S10
Symbol | Name | Read as | Example (R = it rains, W = road is wet) | Python |
|---|---|---|---|---|
¬P | Negation | not P | ¬R: It is not raining. | not P |
P ∧ Q | Conjunction | P and Q | R ∧ W: It is raining and the road is wet. | P and Q |
P ∨ Q | Disjunction | P or Q (or both) | R ∨ W: It is raining or the road is wet. | P or Q |
P → Q | Implication | if P then Q | R → W: If it rains, the road is wet. | (not P) or Q |
P ↔ Q | Biconditional | P if and only if Q | R ↔ W: It rains if and only if the road is wet. | P == Q |
OR in logic is inclusive: P ∨ Q is also true when both are true. Other books may write ~ for ¬, & for ∧, ⇒ for → and ⇔ for ↔.
§2.2 Understanding implication
Slide L06-S11
Think of P → Q as a promise. Teacher: 'If your attendance is 75% or more (P), you can sit the final exam (Q).' The promise is broken only when P is true and Q is false. When P is false the promise says nothing, so P → Q is true (vacuously true). P is called the premise (antecedent) and Q the conclusion (consequent).
Related form | Formula | Same meaning as P → Q? |
|---|---|---|
Converse | Q → P | No |
Inverse | ¬P → ¬Q | No |
Contrapositive | ¬Q → ¬P | Yes |
§2.3 Biconditional and precedence
Slide L06-S12
P ↔ Q is true when P and Q have the same value; it equals (P → Q) ∧ (Q → P). Precedence (order of evaluation, like BODMAS) from highest to lowest: ¬, ∧, ∨, →, ↔. So ¬P ∧ Q ∨ R → S means (((¬P) ∧ Q) ∨ R) → S. In exams, always write the brackets.
§2.4 Translating English
Slide L06-S13
Method: (1) list the atomic statements and give each a letter; (2) find the connective words; (3) write the formula with brackets. Key: L = load-shedding, U = UPS charged, F = fan runs, S = Ali studies, E = exam tomorrow.
English | Clue | Logic |
|---|---|---|
There is load-shedding but the UPS is charged. | but = and | L ∧ U |
If there is load-shedding and the UPS is charged, the fan runs. | if ... then | (L ∧ U) → F |
Ali studies unless there is load-shedding. | A unless B = A ∨ B | S ∨ L (≡ ¬L → S) |
The fan runs only if the UPS is charged or there is no load-shedding. | P only if Q = P → Q | F → (U ∨ ¬L) |
Ali studies if there is an exam tomorrow. | Q if P = P → Q | E → S |
There is neither load-shedding nor an exam tomorrow. | neither ... nor | ¬L ∧ ¬E |
Ali studies if and only if there is an exam tomorrow. | iff | S ↔ E |
3. Truth Tables
§3.1 Truth table of the connectives
Slide L06-S15
A truth table lists every combination of values of the atoms. With n atoms there are 2ⁿ rows (2 atoms: 4 rows; 3 atoms: 8 rows). List the rows in the order TT, TF, FT, FF. The table below is the real output of l06/truth_table.py.
Truth table of the five connectives P Q | ¬P | P∧Q | P∨Q | P→Q | P↔Q T T | F | T | T | T | T T F | F | F | T | F | F F T | T | F | T | T | F F F | T | F | F | T | T |
- ∧ is true in one row only (both true).
- ∨ is false in one row only (both false).
- → is false in one row only (true → false).
- ↔ is true when both values are the same.
§3.2 Building a truth table step by step
Slide L06-S16
Example: ((P ∨ Q) ∧ ¬P) → Q. Story: P = the fault is in the fuse, Q = the fault is in the wiring. 'Fuse or wiring; not the fuse; so the wiring.' Make a column for each sub-formula, inside brackets first, and the whole formula last.
Step by step: ((P ∨ Q) ∧ ¬P) → Q P Q | P∨Q | ¬P | (P∨Q)∧¬P | whole T T | T | F | F | T T F | T | F | F | T F T | T | T | T | T F F | F | T | F | T |
The last column is all T, so this pattern of reasoning (disjunctive syllogism) is always valid.
§3.3 A truth-table maker in Python
Slide L06-S17
This small toolkit (standard Python only) is used for every demo in this lecture. A model is one row of the table, stored as a dictionary such as {'P': True, 'Q': False}; a formula is a Python function of the model.
# Tiny propositional-logic toolkit (CS-511 Lecture 06). Formulas are Python functions of a model (dict name -> bool). from itertools import product def implies(a, b): return (not a) or b # P -> Q is false only when P is True and Q is False def iff(a, b): return a == b # P <-> Q is true when both sides have the same value TF = lambda v: "T" if v else "F" def models(names): """All 2^n rows of a truth table, in the usual order (T first).""" for values in product([True, False], repeat=len(names)): yield dict(zip(names, values)) def truth_table(names, formulas): """formulas = [(label, function)]; prints one row per model.""" print(" ".join(f"{n:^3}" for n in names) + " | " + " | ".join(lbl for lbl, _ in formulas)) for m in models(names): cells = [f"{TF(m[n]):^3}" for n in names] outs = [f"{TF(f(m)):^{len(lbl)}}" for lbl, f in formulas] print(" ".join(cells) + " | " + " | ".join(outs)) def classify(names, f): vals = [f(m) for m in models(names)] if all(vals): return "tautology (valid)" if not any(vals): return "contradiction (unsatisfiable)" return "contingency (satisfiable)" def equivalent(names, f, g): return all(f(m) == g(m) for m in models(names)) def entails(names, kb, alpha): """KB |= alpha iff alpha is true in EVERY model in which the KB is true.""" kb_models = [m for m in models(names) if all(s(m) for s in kb)] return all(alpha(m) for m in kb_models), kb_models |
The program that printed the tables in §3.1-3.2:
from pl import * print("Truth table of the five connectives") truth_table(["P", "Q"], [ ("¬P", lambda m: not m["P"]), ("P∧Q", lambda m: m["P"] and m["Q"]), ("P∨Q", lambda m: m["P"] or m["Q"]), ("P→Q", lambda m: implies(m["P"], m["Q"])), ("P↔Q", lambda m: iff(m["P"], m["Q"])), ]) print() print("Step by step: ((P ∨ Q) ∧ ¬P) → Q") truth_table(["P", "Q"], [ ("P∨Q", lambda m: m["P"] or m["Q"]), ("¬P", lambda m: not m["P"]), ("(P∨Q)∧¬P", lambda m: (m["P"] or m["Q"]) and not m["P"]), ("whole", lambda m: implies((m["P"] or m["Q"]) and not m["P"], m["Q"])), ]) |
§3.4 Tautology, contradiction and contingency
Slide L06-S18
Type | Last column | Example |
|---|---|---|
Tautology (valid) | All T | P ∨ ¬P: 'It rains or it does not rain.' |
Contradiction (unsatisfiable) | All F | P ∧ ¬P: 'It rains and it does not rain.' |
Contingency | Some T, some F | P → Q |
Satisfiable | At least one T | Every tautology and contingency |
Real output of l06/classify.py (first part):
P ∨ ¬P tautology (valid) P ∧ ¬P contradiction (unsatisfiable) P → Q contingency (satisfiable) ((P ∨ Q) ∧ ¬P) → Q tautology (valid) (P → Q) ∧ P ∧ ¬Q contradiction (unsatisfiable) |
4. Equivalence and Entailment
§4.1 Logical equivalences
Slide L06-S20
Two formulas are logically equivalent (≡) if their truth-table columns are identical in every row. Equivalences let us rewrite formulas; Lecture 8 uses them to convert a knowledge base into a standard form for resolution.
Law | Equivalence |
|---|---|
Double negation | ¬¬P ≡ P |
De Morgan | ¬(P ∧ Q) ≡ ¬P ∨ ¬Q; ¬(P ∨ Q) ≡ ¬P ∧ ¬Q |
Implication elimination | P → Q ≡ ¬P ∨ Q |
Contrapositive | P → Q ≡ ¬Q → ¬P |
Biconditional elimination | P ↔ Q ≡ (P → Q) ∧ (Q → P) |
Commutative | P ∧ Q ≡ Q ∧ P; P ∨ Q ≡ Q ∨ P |
Associative | (P ∧ Q) ∧ R ≡ P ∧ (Q ∧ R); (P ∨ Q) ∨ R ≡ P ∨ (Q ∨ R) |
Distributive | P ∧ (Q ∨ R) ≡ (P ∧ Q) ∨ (P ∧ R); P ∨ (Q ∧ R) ≡ (P ∨ Q) ∧ (P ∨ R) |
§4.2 Checking equivalence
Slide L06-S21
Real output of l06/classify.py (second part). P → Q equals ¬P ∨ Q and its contrapositive, but not its converse; and the 'wrong De Morgan' is not an equivalence.
P → Q ≡ ¬P ∨ Q True P → Q ≡ ¬Q → ¬P (contrapositive) True P → Q ≡ Q → P (converse) False ¬(P ∧ Q) ≡ ¬P ∨ ¬Q (De Morgan) True ¬(P ∨ Q) ≡ ¬P ∧ ¬Q (De Morgan) True ¬(P ∧ Q) ≡ ¬P ∧ ¬Q (wrong!) False |
Converse trap: 'If it rains, the road is wet' does not mean 'if the road is wet, it rained' - a water tanker may have passed. Rows (T, F) and (F, T) are enough to show P → Q and Q → P are different. |
§4.3 Models and entailment
Slide L06-S22
A model is one possible world (one row of the truth table). A knowledge base (KB) entails a sentence α, written KB ⊨ α, if α is true in every model in which the KB is true. Model checking means listing all 2ⁿ models and checking them. Example 1: KB = {R → W, R}. Only the first row makes the KB true, and W is true there, so KB ⊨ W. Example 2 writes the Lecture 5 tubewell rules in logic (D soil dry, R rain forecast, L load-shedding, I irrigation needed, T start tubewell, G start diesel engine). Of the 64 models only 2 make the KB true; G is true in both, so the KB entails 'start the diesel engine' - the same answer forward chaining gave. The KB entails neither T nor ¬T: it simply does not say.
from pl import * # Example 1: R = it rains, W = the road is wet. KB = {R → W, R}. Does KB ⊨ W ? names = ["R", "W"] kb = [lambda m: implies(m["R"], m["W"]), lambda m: m["R"]] print("R W | R→W R | KB | W") for m in models(names): k = [s(m) for s in kb] print(f"{TF(m['R'])} {TF(m['W'])} | {TF(k[0])} {TF(k[1])} | {TF(all(k))} | {TF(m['W'])}") ok, _ = entails(names, kb, lambda m: m["W"]) print("KB entails W:", ok) print() # Example 2: tubewell controller (Lecture 5 homework) as propositional logic # D soil dry, R rain forecast, L load-shedding, I irrigation needed, T start tubewell, G start diesel engine names = ["D", "R", "L", "I", "T", "G"] kb = [ lambda m: implies(m["D"] and not m["R"], m["I"]), lambda m: implies(m["I"] and not m["L"], m["T"]), lambda m: implies(m["I"] and m["L"], m["G"]), lambda m: m["D"], lambda m: not m["R"], lambda m: m["L"], ] for q, f in [("G (start diesel engine)", lambda m: m["G"]), ("I (irrigation needed)", lambda m: m["I"]), ("T (start tubewell)", lambda m: m["T"]), ("¬T (tubewell off)", lambda m: not m["T"])]: ok, km = entails(names, kb, f) print(f"KB entails {q:<24}: {ok}") print(f"Models checked: {2 ** len(names)} | models where KB is true: {len(km)}") |
R W | R→W R | KB | W T T | T T | T | T T F | F T | F | F F T | T F | F | T F F | T F | F | F KB entails W: True KB entails G (start diesel engine) : True KB entails I (irrigation needed) : True KB entails T (start tubewell) : False KB entails ¬T (tubewell off) : False Models checked: 64 | models where KB is true: 2 |
§4.4 Strengths and limits
Slide L06-S23
Strengths | Limits |
|---|---|
Exact meaning | Truth tables grow as 2ⁿ: 50 atoms give about 10¹⁵ rows |
NOT and OR are easy | No objects: every student needs a separate symbol (AliRegular, SanaRegular, ...) |
A computer can check validity and entailment | Cannot say ALL or SOME ('All UAF students need 75% attendance') |
Predicate (first-order) logic in Lecture 7 adds objects, predicates, variables and the quantifiers ∀ (all) and ∃ (some). Resolution in Lecture 8 proves entailment without writing every row.
5. Common Mistakes
Slide L06-S25
Wrong idea | Correct idea |
|---|---|
Questions and commands are propositions. | Only declarative sentences with a true/false value are propositions. |
P → Q is false when P is false. | P → Q is false only when P is true and Q is false. |
P → Q means the same as Q → P. | Only the contrapositive ¬Q → ¬P is equivalent. |
¬(P ∧ Q) ≡ ¬P ∧ ¬Q | De Morgan: ¬(P ∧ Q) ≡ ¬P ∨ ¬Q. |
'P only if Q' means Q → P. | 'P only if Q' means P → Q. |
Word Bank
Word | Simple meaning | Example |
|---|---|---|
Proposition | A statement that is true or false | Burewala is in Vehari district. |
Connective | An operator that joins statements | ∧, ∨, ¬, →, ↔ |
Syntax | The grammar of a language | 'P ∧ Q' is allowed |
Semantics | The meaning: when a sentence is true | P ∧ Q is true when both are true |
Premise / conclusion | The IF part / the THEN part of P → Q | attendance ≥ 75% / can sit the exam |
Tautology | True in every row | P ∨ ¬P |
Contradiction | False in every row | P ∧ ¬P |
Contingency | True in some rows, false in others | P → Q |
Equivalent (≡) | Same truth value in every row | P → Q ≡ ¬P ∨ Q |
Model | One possible world; one row | {R: T, W: F} |
Entailment (⊨) | True in every model where the KB is true | {R → W, R} ⊨ W |
Summary
- A proposition is a statement that is true or false; atoms are joined by ¬, ∧, ∨, →, ↔.
- P → Q is false only when P is true and Q is false; its contrapositive is equivalent, its converse is not.
- Precedence: ¬, ∧, ∨, →, ↔. A truth table has 2ⁿ rows.
- Tautology = all T, contradiction = all F, contingency = mixed.
- Equivalent formulas have identical truth-table columns (De Morgan, implication elimination, contrapositive).
- KB ⊨ α when α is true in every model of the KB; model checking tests all models.
Self-Check Questions
- Which of these are propositions: 'Open the window', '5 > 3', 'Is it Friday?', 'Multan is in Sindh'? (see §1.2)
- Write 'If there is no rain and the canal is closed, the farmer runs the tubewell' in logic (give a key). (see §2.4)
- When is P → Q false? Explain with the attendance promise. (see §2.2)
- Add brackets to show the order: ¬A ∨ B ∧ C → D. (see §2.3)
- Make the truth table of (P ∧ Q) → P and classify it. (see §3.2, §3.4)
- How many rows does a truth table with 4 atoms have? (see §3.1)
- Prove ¬(P ∨ Q) ≡ ¬P ∧ ¬Q with a truth table. (see §4.1)
- Show with two rows that P → Q is not equivalent to Q → P. (see §4.2)
- Using model checking, does {P ∨ Q, ¬P} entail Q? (see §4.3)
- Give two limits of propositional logic. (see §4.4)
Reading: Russell & Norvig (4e) Sections 7.4-7.5; Luger (2009) Section 2.1.