Knowledge & Reasoning10 min read

Logic & Inference

If A then B, A is true β€” so B must be true
scope:Core Conceptdifficulty:Beginner

The AI Detective

Imagine a detective walks into a crime scene. She notices muddy footprints leading from the window, the window is broken from the outside, and the safe is empty. She doesn't see the burglar, but she can infer what happened.

That's logic and inference in action β€” starting from known facts (premises) and applying rules to reach new conclusions. It's one of the oldest and most powerful ideas in AI.

Propositional Logic: True or False

The simplest form of logic deals with statements that are either true or false:

  • P: "It is raining" (true or false)
  • Q: "I have an umbrella" (true or false)

You combine them with logical connectives:

  • AND (∧): "It's raining AND I have an umbrella" β€” true only if both are true
  • OR (∨): "It's raining OR I have an umbrella" β€” true if either is true
  • NOT (Β¬): "It is NOT raining" β€” flips true to false
  • IMPLIES (β†’): "IF it's raining, THEN the ground is wet" β€” a rule

The crown jewel of inference: Modus Ponens. If you know "IF P THEN Q" is true, and "P" is true, then "Q" must be true. If it's raining, the ground is wet. It's raining. Therefore, the ground is wet. Simple but powerful.

Forward Chaining vs Backward Chaining

There are two ways to reason with rules:

Forward Chaining: Start from What You Know

You start with known facts and apply rules to derive new facts, which trigger more rules, and so on. Like a chain reaction.

Fact: "It's raining." Rule: "If raining β†’ ground is wet." Derive: "Ground is wet." Rule: "If ground is wet β†’ wear boots." Derive: "Wear boots."

Backward Chaining: Start from What You Want to Prove

You start with a goal and work backward to find facts that support it. Like a detective building a case.

Goal: "Should I wear boots?" Rule: "Wear boots if ground is wet." Sub-goal: "Is the ground wet?" Rule: "Ground is wet if raining." Check: "Is it raining?" Fact: Yes! Therefore: wear boots.

Forward chaining is data-driven (react to what you see). Backward chaining is goal-driven (figure out what you need). Both are used in real AI systems.

A Simple Inference Engine

def forward_chain(rules, facts):
"""Forward chaining: derive new facts from rules."""
facts = set(facts)
changed = True
while changed:
changed = False
for conditions, conclusion in rules:
# If all conditions are in our facts...
if all(c in facts for c in conditions):
if conclusion not in facts:
facts.add(conclusion)
print(f" Derived: {conclusion}")
changed = True
return facts
# Rules: ([conditions], conclusion)
rules = [
(["raining"], "ground_wet"),
(["ground_wet"], "wear_boots"),
(["cold", "raining"], "wear_coat"),
(["sunny"], "wear_sunglasses"),
(["sunny", "weekend"], "go_to_park"),
]
print("Scenario 1: It's raining and cold")
result = forward_chain(rules, ["raining", "cold"])
print(f" All facts: {result}\n")
print("Scenario 2: It's sunny on a weekend")
result = forward_chain(rules, ["sunny", "weekend"])
print(f" All facts: {result}")
Output
Scenario 1: It's raining and cold
  Derived: ground_wet
  Derived: wear_coat
  Derived: wear_boots
  All facts: {'raining', 'cold', 'ground_wet', 'wear_coat', 'wear_boots'}

Scenario 2: It's sunny on a weekend
  Derived: wear_sunglasses
  Derived: go_to_park
  All facts: {'sunny', 'weekend', 'wear_sunglasses', 'go_to_park'}
Note: Logic is the backbone of classic AI. Before neural networks took over, most AI systems were built on formal logic. Prolog, a programming language based entirely on logical inference, was once the dominant AI language. Even today, logic-based reasoning is making a comeback β€” modern AI combines neural networks (for pattern recognition) with logical inference (for structured reasoning).

Limitations of Pure Logic

Logic is powerful but rigid. Real-world knowledge is often uncertain, incomplete, or contradictory:

  • "Birds can fly" β€” but penguins can't. Logic doesn't handle exceptions gracefully.
  • "This patient probably has the flu" β€” logic deals in true/false, not probabilities.
  • "New evidence suggests the opposite" β€” classical logic can't retract conclusions easily.

These limitations led to extensions like fuzzy logic (degrees of truth), probabilistic logic (beliefs with confidence levels), and non-monotonic logic (conclusions that can be revised). The quest for better reasoning systems continues to this day.

Quick check

What is Modus Ponens?
Challenge

Continue reading