AI Knowledge & Reasoning
🧠 Knowledge-Based Agents & Logic
This section explores knowledge-based agents in AI, focusing on how artificial intelligence can represent and reason with knowledge to draw conclusions and solve problems. Unlike search problems that focus on actions in an environment, this approach emphasizes leveraging known facts to infer new information.
💡 The Essence of Knowledge & Reasoning
Human intelligence heavily relies on knowledge and the ability to reason about it. We use known facts to draw conclusions and understand how to act or discover new information. AI aims to replicate this by building knowledge-based agents that can represent knowledge internally and use algorithms to reason.
❓ Example: Harry Potter Deduction
Consider a simple deductive reasoning problem:
If it didn't rain, then Harry visited Hagrid today.
Harry visited Hagrid or Dumbledore today, but not both.
Harry visited Dumbledore today.
From these three facts, a human can deduce:
From (2) and (3): Harry must not have visited Hagrid today. (Because he visited Dumbledore, and not both.)
From (1) and the previous deduction: If "Harry visited Hagrid today" is false, then "it didn't rain" must also be false. Therefore, it did rain today.
This process of using logic to take known information and reach new conclusions is the core of what AI agents need to achieve.
📜 Propositional Logic
To enable computers to reason logically, we need a formal system. Propositional logic is a fundamental type of logic based on statements about the world, called propositions.
⚛️ Propositional Symbols
Propositional symbols are basic assertions, often represented by letters like
P,Q,R.Each symbol represents a fact or sentence about the world.
Example:
Pcould mean "it is raining."Example:
Qcould mean "Harry visited Hagrid today."
Each symbol can be either true or false.
🔗 Logical Connectives
To combine propositional symbols into more complex statements, we use logical connectives:
Symbol | Name | Meaning |
|---|---|---|
¬ | Not | Inverts the truth value of a proposition. |
∧ | And | True if both propositions are true. |
∨ | Or | True if at least one proposition is true (inclusive or). |
→ | Implication | "If P, then Q" (P implies Q). True unless P is true and Q is false. |
↔ | Biconditional | "P if and only if Q." True if P and Q have the same truth value. |
Truth Tables for Logical Connectives
Truth tables define the meaning of each connective by showing all possible combinations of truth values for its inputs and the resulting output.
Not (¬)
P
¬P
False
True
True
False
¬P is the opposite of P. If P is "it is raining," then ¬P is "it is not raining."
And (∧)
P
Q
P ∧ Q
False
False
False
False
True
False
True
False
False
True
True
True
P ∧ Q is true only when both P and Q are true.
Or (∨)
P
Q
P ∨ Q
False
False
False
False
True
True
True
False
True
True
True
True
P ∨ Q is true if P is true, or Q is true, or both are true (inclusive OR).
Implication (→)
P
Q
P → Q
False
False
True
False
True
True
True
False
False
True
True
True
P → Q (P implies Q) means "If P is true, then Q must also be true." It is only false if P is true and Q is false. If P is false, the implication makes no claim about Q, so it's considered true.
Biconditional (↔)
P
Q
P ↔ Q
False
False
True
False
True
False
True
False
False
True
True
True
P ↔ Q (P if and only if Q) is true only when P and Q have the same truth value. It implies P → Q and Q → P.
🌎 Models & Knowledge Bases
To bridge logical statements with the real world, AI uses models and knowledge bases.
🗺️ Models
A model assigns a truth value (true or false) to every propositional symbol.
A model represents a possible world or a specific state of affairs.
If there are N propositional symbols, there are 2N possible models.
Example: If
Pis "it is raining" andQis "it is a Tuesday", a model could be:P= True (it is raining)Q= False (it is not a Tuesday)
There are 22=4 possible models for these two symbols.
🧠 Knowledge Base (KB)
A knowledge base (KB) is a set of sentences in propositional logic that an AI agent knows to be true about the world.
The AI uses this KB to draw conclusions.
➡️ Entailment & Inference
The ultimate goal is for the AI to use its knowledge to infer new, true information.
⛓️ Entailment (⊨)
Alpha entails Beta (α⊨β) means: In every model (possible world) where sentence α is true, sentence β is also true.
If α⊨β, then knowing α to be true guarantees that β is also true.
Example: "It is a Tuesday in January" entails "It is January."
💡 Inference
Inference is the process of deriving new sentences from old ones based on logical rules.
The AI's task is to determine if its KB entails a query (α). That is, can we conclude α is true based on the KB alone?
🧩 Inference Example
Propositional Symbols:
P: It is a Tuesday.Q: It is raining.R: Harry will go for a run.
Knowledge Base (KB):
P ∧¬Q → R (If it is a Tuesday AND it is not raining, THEN Harry will go for a run.)
P (It is a Tuesday.)
¬Q (It is not raining.)
Inference Process:
From (2) and (3), we know
Pis true and$\neg$Qis true.Therefore,
P $\wedge \neg$Qis true.Since
P $\wedge \neg$Q $\rightarrow$ R(from 1) andP $\wedge \neg$Qis true, by the definition of implication,Rmust be true.Conclusion: R (Harry will go for a run) is true.
🔎 Model Checking Algorithm
One way to perform inference is model checking, which systematically checks all possible worlds.
⚙️ How Model Checking Works
To determine if KB $\models \alpha$:
Enumerate all possible models: List every combination of true/false assignments for all propositional symbols.
For each model:
Check if the knowledge base (KB) is true in that model.
If the KB is true, then check if the query (α) is also true in that model.
If α is true in every model where KB is true, then
KB $\models \alpha$. Otherwise, there is no entailment.
💻 Model Checking in Practice (Python Example)
A logic.py library can define classes for symbols and connectives (e.g., Symbol, Not, And, Or, Implication).
Harry Potter Example:
Symbols:
rain,hagrid,dumbledoreKnowledge Base (
knowledgevariable):Implication(Not(rain), hagrid): If it's not raining, Harry visited Hagrid.Or(hagrid, dumbledore): Harry visited Hagrid or Dumbledore.Not(And(hagrid, dumbledore)): Harry didn't visit both Hagrid and Dumbledore.dumbledore: Harry visited Dumbledore.
A model_check(knowledge, query) function would recursively explore all 2N possible truth assignments for rain, hagrid, dumbledore and verify the entailment. For the Harry Potter example, model_check(knowledge, rain) would return True.
🏗️ Knowledge Engineering
Knowledge engineering is the process of taking a real-world problem and distilling it into knowledge that is representable by a computer using logical symbols and formulas.
This allows general-purpose problems to be solved by inference algorithms.
🎲 Clue Game Example (Knowledge Engineering)
Goal: Determine the murderer, room, and weapon in the game Clue.
Propositional Symbols:
Mustard,Plum,Scarlet(for characters)Ballroom,Kitchen,Library(for rooms)Knife,Revolver,Wrench(for weapons)
Initial Knowledge (KB):
Or(Mustard, Plum, Scarlet): One person is the murderer.Or(Ballroom, Kitchen, Library): One room is where it happened.Or(Knife, Revolver, Wrench): One weapon was used.(Additional rules for exclusivity, e.g., if Mustard is the murderer, Plum is not)
Game Play (adding knowledge):
If you have the
Mustardcard:knowledge.add(Not(Mustard))If a guess (e.g., Scarlet, Library, Wrench) is revealed to be false (one of the cards is not in the envelope):
knowledge.add(Or(Not(Scarlet), Not(Library), Not(Wrench)))
By adding more knowledge (cards seen, deductions), the model checking algorithm can eventually deduce the solution (e.g., "Yes, Miss Scarlet," "Yes, Library," "Yes, Knife").
🧩 Logic Puzzle Example (Hogwarts Houses)
Goal: Assign each person (Gildaroy, Minerva, Pomona, Horace) to a unique house (Gryffindor, Hufflepuff, Ravenclaw, Slytherin).
Propositional Symbols:
GildaroyGryffindor,MinervaHufflepuff, etc. (16 symbols: 4 people x 4 houses). Each is true if the person is in that house.
Initial Knowledge (KB):
Every person belongs to a house:
Or(GildaroyGryffindor, GildaroyHufflepuff, ...)for each person.Only one house per person:
Implication(MinervaGryffindor, Not(MinervaHufflepuff))for all pairs.Every house has only one person:
Implication(MinervaGryffindor, Not(GildaroyGryffindor))for all pairs.
Given Information (adding knowledge):
Or(GildaroyGryffindor, GildaroyRavenclaw)Not(PomonaSlytherin)MinervaGryffindor
Model checking, though slow, can deduce the unique assignment for each person to their house.
🐢 Limitations of Model Checking
Computational Complexity: Model checking requires enumerating 2N possible models for N propositional symbols. This becomes intractable for larger numbers of variables.
For complex problems with many variables, more efficient inference methods are needed.
📚 Inference Rules
Inference rules are patterns that allow us to derive new true sentences from existing true sentences without enumerating all models.
📝 Structure of Inference Rules
Premise(s): Statement(s) above a horizontal line that are known to be true.
Conclusion: Statement below the line that can be derived.
📏 Common Inference Rules
Modus Ponens
Premises: α→β, α
Conclusion: β
English: "If it is raining, then Harry is inside. It is raining. Therefore, Harry is inside."
And Elimination
Premise: α∧β
Conclusion: α (or β)
English: "Harry is friends with Ron and Hermione. Therefore, Harry is friends with Hermione."
Double Negation Elimination
Premise: ¬(¬α)
Conclusion: α
English: "It is not true that Harry did not pass the test. Therefore, Harry passed the test."
Implication Elimination
Premise: α→β
Conclusion: ¬α∨β
English: "If it is raining, then Harry is inside. This means either it is not raining, or Harry is inside."
This rule is crucial for converting implications into disjunctions (OR statements).
Biconditional Elimination
Premise: α↔β
Conclusion: (α→β)∧(β→α)
English: "It is raining if and only if Harry is inside. This means if it is raining then Harry is inside, AND if Harry is inside then it is raining."
De Morgan's Laws (for moving negations)
Not (A and B) → (Not A or Not B)
Premise: ¬(α∧β)
Conclusion: ¬α∨¬β
English: "It is not true that both Harry and Ron passed the test. Therefore, either Harry did not pass the test, or Ron did not pass the test."
Not (A or B) → (Not A and Not B)
Premise: ¬(α∨β)
Conclusion: ¬α∧¬β
English: "It is not true that Harry or Ron passed the test. Therefore, Harry did not pass the test, AND Ron did not pass the test."
Distributive Laws (for rearranging AND/OR)
And over Or:
Premise: α∧(β∨γ)
Conclusion: (α∧β)∨(α∧γ)
Or over And:
Premise: α∨(β∧γ)
Conclusion: (α∨β)∧(α∨γ)
⚙️ Theorem Proving as Search
Inference rules can be used in a search problem framework to prove theorems:
Initial State: The initial knowledge base (KB) (set of known sentences).
Actions: Applying any valid inference rule to the current set of sentences.
Transition Model: The new KB after adding the conclusion of an applied inference rule.
Goal Test: Check if the query (α) is present in the KB.
Path Cost: Number of inference rules applied (steps in the proof).
🚀 Resolution
Resolution is a powerful inference rule that forms the basis of a common and complete inference algorithm.
🤝 The Resolution Rule
Unit Resolution: If
P $\vee$ Qis true, and$\neg$Pis true, thenQmust be true.English: "Ron is in the Great Hall or Hermione is in the library. Ron is not in the Great Hall. Therefore, Hermione is in the library."
Generalized Resolution:
Premises:
P $\vee$ Q(where Q can beQ1 $\vee$ Q2 ...) and$\neg$P $\vee$ R(where R can beR1 $\vee$ R2 ...)Conclusion:
Q $\vee$ REnglish: "Ron is in the Great Hall or Hermione is in the library. Ron is not in the Great Hall or Harry is sleeping. Therefore, Hermione is in the library or Harry is sleeping."
Complementary Literals: The key is finding a propositional symbol (
P) and its negation ($\neg$P) across two clauses. These "resolve" and are eliminated.
📝 Clauses & Conjunctive Normal Form (CNF)
Literal: A propositional symbol (e.g.,
P) or its negation (e.g.,$\neg$P).Clause: A disjunction of literals (literals connected by
$\vee$OR).Example:
P $\vee \neg$Q $\vee$ R
Conjunctive Normal Form (CNF): A logical sentence that is a conjunction of clauses (clauses connected by
$\wedge$AND).Example:
(A $\vee$ B $\vee$ C) $\wedge$ (D $\vee \neg$E) $\wedge$ (F $\vee$ G)Any logical sentence can be converted into CNF.
Converting to CNF
Eliminate Biconditionals:
A $\leftrightarrow$ B→(A $\rightarrow$ B) $\wedge$ (B $\rightarrow$ A)Eliminate Implications:
A $\rightarrow$ B→$\neg$A $\vee$ BMove ¬ Inwards: Use De Morgan's Laws (e.g.,
$\neg$(A $\wedge$ B)→$\neg$A $\vee \neg$B) and Double Negation Elimination ($\neg \neg$A→A).Distribute ∨ over ∧: Use distributive law to ensure
$\wedge$are on the outside (e.g.,A $\vee$ (B $\wedge$ C)→(A $\vee$ B) $\wedge$ (A $\vee$ C)).Example CNF Conversion: Convert
(P $\vee$ Q) $\rightarrow$ Rto CNF.
Eliminate implication:
$\neg$(P $\vee$ Q) $\vee$ RMove ¬ inwards (De Morgan's):
($\neg$P $\wedge \neg$Q) $\vee$ RDistribute ∨ over ∧:
($\neg$P $\vee$ R) $\wedge$ ($\neg$Q $\vee$ R)is is now in CNF.
👻 The Empty Clause
If
Pand$\neg$Pare resolved, and there are no other literals in the clauses, the result is the empty clause (represented as ⊥).The empty clause is always false because
P $\wedge \neg$Pis a contradiction.
🎯 Inference by Resolution Algorithm
To determine if KB $\models \alpha$:
Proof by Contradiction: Assume the query is false. We want to prove
KB $\wedge \neg \alpha$leads to a contradiction.Convert to CNF: Convert the entire statement
KB $\wedge \neg \alpha$into Conjunctive Normal Form. This gives a set of clauses.Repeated Resolution:
Continuously take any two clauses from the set.
If they contain complementary literals (e.g.,
Pin one,$\neg$Pin another), resolve them to produce a new clause.Add this new clause to the set (after factoring to remove duplicate literals).
Repeat this process until:
Empty Clause Produced: If the empty clause (⊥) is generated, it means
KB $\wedge \neg \alpha$is a contradiction. Therefore,KB $\models \alpha$(the query is true).No New Clauses: If no new clauses can be generated, and the empty clause has not been produced, then
KB $\not\models \alpha$(the query is not entailed).
Resolution Example
Knowledge Base: (A $\vee$ B) $\wedge$ ($\neg$B $\vee$ C) $\wedge \neg$C
Query: A
Assume ¬A: Add
$\neg$Ato the KB.Clauses:
(A $\vee$ B),($\neg$B $\vee$ C),($\neg$C),($\neg$A)
Resolve (¬B ∨ C) and (¬C):
Complementary literals:
Cand$\neg$CNew clause:
($\neg$B)
Resolve (A ∨ B) and (¬B):
Complementary literals:
Band$\neg$BNew clause:
(A)
Resolve (A) and (¬A):
Complementary literals:
Aand$\neg$ANew clause: The empty clause (⊥)
Conclusion: Since the empty clause is produced,
KB $\models$ A. The knowledge base entails A.
🌟 First-Order Logic
First-order logic (FOL) is more expressive than propositional logic, allowing us to represent complex ideas more concisely, especially when dealing with objects and relationships.
🚧 Limitations of Propositional Logic
Redundancy: In problems like the Hogwarts puzzle, we needed many propositional symbols (e.g.,
MinervaGryffindor,MinervaHufflepuff) to represent simple relationships.Lack of Structure: Propositional logic treats each symbol as an independent fact, making it hard to express general rules about objects.
🔑 Key Concepts in First-Order Logic
FOL introduces different types of symbols:
Constant Symbols: Represent objects (e.g., people, houses).
Examples:
Minerva,Pomona,Gryffindor,Slytherin.
Predicate Symbols: Represent relations or properties that hold true or false for constants.
Examples:
Person(Minerva)(Minerva is a person),House(Gryffindor)(Gryffindor is a house).BelongsTo(Minerva, Gryffindor)(Minerva belongs to Gryffindor).
Examples of FOL Sentences
Person(Minerva): Minerva is a person.House(Gryffindor): Gryffindor is a house.Not(House(Minerva)): Minerva is not a house.BelongsTo(Minerva, Gryffindor): Minerva belongs to Gryffindor.This is more concise: one
BelongsTopredicate handles all person-house relationships, unlike needing a separate propositional symbol for each combination.
🌐 Quantifiers
FOL introduces quantifiers to express ideas about collections of objects.
Universal Quantification (∀): "For all" or "For every".
Symbol: Upside-down A (
$\forall$)Example:
$\forall$x (BelongsTo(x, Gryffindor) $\rightarrow$ Not(BelongsTo(x, Hufflepuff)))Meaning: For all objects
x, ifxbelongs to Gryffindor, thenxdoes not belong to Hufflepuff. (Anyone in Gryffindor is not a Hufflepuff.)
Existential Quantification (∃): "There exists" or "For some".
Symbol: Backwards E (
$\exists$)Example:
$\exists$x (House(x) $\wedge$ BelongsTo(Minerva, x))Meaning: There exists an object
xsuch thatxis a house, AND Minerva belongs tox. (Minerva belongs to a house.)
Combining Quantifiers
Quantifiers can be combined to express complex rules:
$\forall$x (Person(x) $\rightarrow$ $\exists$y (House(y) $\wedge$ BelongsTo(x, y)))Meaning: For all
x, ifxis a person, then there exists aysuch thatyis a house andxbelongs toy. (Every person belongs to a house.)
📈 Beyond First-Order Logic
There are even more powerful logics (e.g., second-order logic, higher-order logic) that allow expressing increasingly complex ideas.
The core goal remains knowledge representation and reasoning to enable AI agents to understand and infer information.
⏭️ Next Steps: Embracing Uncertainty
While current methods focus on what AI knows for sure, future exploration will delve into uncertainty, allowing AI to reason with probabilities and likelihoods rather than just absolute truth or falsehood. This will enable even more sophisticated intelligent systems.

Ask anything
We've read this whole study set, so questions about it get the best answers.