Logic & Inference
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
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.