Programming Assignment Unit 5
Name Omitted
Professor:
MSc. Naeem Ahmed
CS 4408 – Artificial Intelligence
University of the People
10/08/2025
1
Programming Assignment Unit 5
Truth Table Generator for a Weather KB
1. Purpose
This program generates the truth table for the propositional knowledge base
KB = (((P ^ Q) => R) ^ (Q => P) ^ Q)
where the propositional symbols mean: P “It is hot,” Q “It is humid,” and R “It is raining.”
It also evaluates the query KB ⇒ R to check whether R is entailed by the KB.
2. Approach and Data Structures
The program is implemented in Python and produces all eight truth assignments for P, Q, R
using a Cartesian product over booleans. For each assignment, it computes the derived
expressions:
(P ∧Q)⇒ R
Q⇒P
KB = ((P∧ Q)⇒ R)∧(Q ⇒ P)∧ Q
KB ⇒ R
Values are computed on the fly and immediately printed, which is memory-efficient (no need to
store all rows). Internally the program uses simple boolean variables for intermediate results; this
is sufficient and efficient for the required outputs.
3. Logical Operators (Algorithm Rules)
Two helper functions define the operators:
Conjunction: conj(a, b) = a and b
Implication: implies(a, b) = (not a) or b
2
This captures the truth condition for implication (only False when antecedent is True and
consequent is False).
4. Code
#!/usr/bin/env python3
"""
Programming Assignment Unit 5
Propositional Logic Truth Table Generator for:
KB = (((P ^ Q) => R) ^ (Q => P) ^ Q)
and the query R (i.e., KB => R)
"""
from itertools import product # <-- required import
def conj(a: bool, b: bool) -> bool:
"""Logical conjunction (^)"""
return a and b
def implies(a: bool, b: bool) -> bool:
"""Logical implication (=>): equivalent to (not a) or b"""
return (not a) or b
def t_or_f(x: bool) -> str:
"""Format booleans as 'T' or 'F' for table output."""
3
return 'T' if x else 'F'
def main() -> None:
headers = ["P", "Q", "R", "(P ^ Q) => R", "Q => P", "KB", "KB => R"]
widths = [3, 3, 3, 15, 7, 5, 9] # tuned for clean alignment
# Print header
row_fmt = " ".join(f"{{:{w}}}" for w in widths)
print(row_fmt.format(*headers))
# Generate combinations in canonical truth-table order:
# P toggles every 4, Q every 2, R every 1 (T then F)
for P, Q, R in product([True, False], repeat=3):
pq_imp_r = implies(conj(P, Q), R)
q_imp_p = implies(Q, P)
KB = pq_imp_r and q_imp_p and Q
kb_imp_r = implies(KB, R)
row = [
t_or_f(P),
t_or_f(Q),
t_or_f(R),
t_or_f(pq_imp_r),
4
t_or_f(q_imp_p),
t_or_f(KB),
t_or_f(kb_imp_r),
print(row_fmt.format(*row))
if __name__ == "__main__":
main()
5. Output and Formatting
The output includes the columns:
P, Q, R, (P ^ Q) => R, Q => P, KB, KB => R
Columns are aligned using fixed widths for readability (no duplicate Q/R columns). A sample
run produces:
5
6. Results and Entailment
From the table:
KB is True only when P = T, Q = T, R = T (one model of the KB).
In that model, R is True; in all other rows, KB is False, so KB ⇒ R is vacuously True.
Therefore, R is entailed by KB (KB ⊨ R).
7. Complexity and Generalization (Brief)
With three variables, the table has 23=8rows. In general, a truth-table approach is O(2n )in the
number of propositional variables n. The structure used here (on-the-fly computation and
printing) keeps memory usage minimal and can be extended to additional formulas if needed.
9. Conclusion
The program:
Compiles and runs without input,
Dynamically generates all eight truth combinations,
Implements conjunction and implication correctly,
Computes (P ∧Q)⇒ R , Q ⇒ P, KB, and KB ⇒ R,
Produces aligned, readable output that demonstrates that KB ⊨ R.
6
References:
Poole, D. L., & Mackworth, A. K. (2017). Artificial intelligence: Foundations of computational
agents (2nd ed.). Cambridge University Press.
Russell, S., & Norvig, P. (2021). Artificial intelligence: A modern approach (4th ed.). Pearson