Propositional Logic
Propositional logic is the branch of logic that deals with propositions—statements that are either definitely True (T) or definitely False (F). It is the language that computers reflect in their hardware, where every signal is high (1) or low (0).
1. What is a Proposition?
A proposition must have a fixed truth value.
- Propositions:
- “The sun is a star.” (True)
- “2 + 2 = 5.” (False)
- Non-Propositions:
- “Close the door!” (Command)
- “What time is it?” (Question)
- “This sentence is false.” (The Liar’s Paradox - it cannot be consistently True or False)
2. Logical Operators in Code
In programming, we use operators to combine these truth values. Let’s see how the basic connectives behave using Python’s boolean logic.
python
1# Logical Truth Tables in Python
2def truth_table():
3 print("P | Q | P AND Q | P OR Q | NOT P")
4 print("-" * 45)
5 for P in [True, False]:
6 for Q in [True, False]:
7 res_and = P and Q
8 res_or = P or Q
9 res_not = not P
10 print(f"{str(P):<6} | {str(Q):<6} | {str(res_and):<7} | {str(res_or):<6} | {str(res_not):<5}")
11
12truth_table()
13
3. The Implication ()
The implication “If , then ” is a promise. It is only broken (False) if is True but fails to happen (is False).
Example: “If you pass the exam (), I will buy you a car ().”
- If you pass () and you get the car (): Promise kept.
- If you pass () but you don’t get the car (): Promise broken.
- If you fail (): It doesn’t matter if I buy you a car or not; the promise wasn’t triggered. Therefore, the statement is vacuously true.
4. De Morgan’s Laws in Action
De Morgan’s laws allow us to simplify complex logic. In code, this is vital for readability.
not (A and B)is the same as(not A) or (not B)not (A or B)is the same as(not A) and (not B)
python
1# Testing De Morgan's Law
2A = True
3B = False
4
5# Left side: not (A and B)
6lhs = not (A and B)
7# Right side: (not A) or (not B)
8rhs = (not A) or (not B)
9
10print(f"not (A and B): {lhs}")
11print(f"(not A) or (not B): {rhs}")
12print(f"They are equal: {lhs == rhs}")
13