⚡0 XP🔥0

Knowledge Representation and Reasoning

Lesson 5 of 8 ⏱ 6 min read ✎ 1 exercises ❓ 4-question quiz ▶ Runs Python

How AI stores and uses knowledge - propositional and first-order logic, inference with modus ponens, forward and backward chaining, building a small expert system, uncertainty with probability, and fuzzy logic.

To act intelligently an agent needs knowledge - facts about the world and rules for drawing conclusions - and a way to reason with it. This is the heart of symbolic AI, and it is still used wherever decisions must be explainable: tax and loan rules, compliance checks, medical guidelines, configuration tools.

Propositional logic#

A proposition is a statement that is true or false: it_is_raining, exam_passed. Connectives combine them:

Symbol Meaning Example
¬P not P not raining
P ∧ Q P and Q raining and cold
P ∨ Q P or Q bus or metro
P → Q if P then Q if attendance < 75% then not eligible
P ↔ Q P if and only if Q
▶ Try it YourselfPython
Ctrl + Enter to run
Output
 P     Q     | P->Q   P and Q   P or Q
 True  True  | True   True      True 
 True  False | False  False     True 
 False True  | True   False     True 
 False False | True   False     False

Note that P → Q is only false when P is true and Q is false.

Inference: modus ponens#

The basic reasoning step:

Rule:  attendance_below_75  →  not_eligible_for_exam
Fact:  attendance_below_75
----------------------------------------------------
Conclude:  not_eligible_for_exam

First-order logic adds objects, variables and quantifiers, so one rule covers everyone: ∀x Student(x) ∧ Attendance(x) < 75 → ¬Eligible(x) ("for every student x...").

हिंदी में समझें

Logic में हम ज्ञान को साफ़ नियमों के रूप में लिखते हैं: "अगर attendance 75% से कम है, तो exam में नहीं बैठ सकते"। फिर कोई fact (Ravi की attendance 70%) मिलने पर engine नियम लगाकर निष्कर्ष निकालता है। यह तरीका पूरी तरह समझाया जा सकता है - हर फैसले का कारण बताया जा सकता है।

Forward and backward chaining#

  • Forward chaining (data-driven) - start with known facts, fire every rule whose conditions are satisfied, add the conclusions, repeat until nothing new appears.
  • Backward chaining (goal-driven) - start with a question ("is the student eligible?"), find rules that conclude it, and check their conditions recursively (this is how Prolog works).

A small expert system: course advisor#

▶ Try it YourselfPython
Ctrl + Enter to run
Output
Reasoning:
   comfortable_with_python + likes_maths  =>  suited_for_data_science
   likes_logic  =>  suited_for_programming
   suited_for_programming + wants_job_fast  =>  recommend_full_stack
   suited_for_data_science + wants_job_fast  =>  recommend_data_analysis
Recommendations: ['recommend_data_analysis', 'recommend_full_stack']

The trace is the big strength of rule-based systems: every conclusion comes with its reasons. Try adding "likes_research" or "likes_design" to the facts.

Backward chaining#

▶ Try it YourselfPython
Ctrl + Enter to run
Output
recommend_data_analysis: try rule needing ['suited_for_data_science', 'wants_job_fast']
  suited_for_data_science: try rule needing ['comfortable_with_python', 'likes_maths']
    comfortable_with_python: known fact
    likes_maths: known fact
  suited_for_data_science: PROVED
  wants_job_fast: known fact
recommend_data_analysis: PROVED

Limits of rules - and reasoning under uncertainty#

Rule-based expert systems (like the famous MYCIN for blood infections in the 1970s) worked well in narrow domains, but:

  • experts' knowledge is hard and slow to write down (the "knowledge acquisition bottleneck"),
  • rules are brittle - the world has endless exceptions,
  • the real world is uncertain - symptoms suggest a disease with some probability, not certainty.

Probabilistic reasoning handles uncertainty with Bayes' theorem and Bayesian networks:

▶ Try it YourselfPython
Ctrl + Enter to run
Output
P(positive test) = 0.0590
P(disease | positive) = 0.161

Surprisingly, a positive result means only about a 16% chance of having the disease, because the disease is rare - a classic reason doctors repeat tests.

Fuzzy logic#

Classical logic says a statement is either true or false. Fuzzy logic allows degrees of truth between 0 and 1 - useful for control systems like air conditioners, washing machines and metro braking.

▶ Try it YourselfPython
Ctrl + Enter to run
Output
22 °C: 'hot' to degree 0.0 -> fan speed 0
27 °C: 'hot' to degree 0.2 -> fan speed 1.0
30 °C: 'hot' to degree 0.5 -> fan speed 2.5
33 °C: 'hot' to degree 0.8 -> fan speed 4.0
38 °C: 'hot' to degree 1.0 -> fan speed 5

Symbolic AI vs machine learning#

Symbolic (rules, logic) Machine learning
Knowledge comes from human experts data
Explainability excellent - shows its reasons often a black box
Handles messy, uncertain data poorly well
Changing behaviour edit the rules retrain with new data
Typical use today compliance, tax/loan rules, configuration vision, language, prediction

Modern systems often combine them - for example, an LLM that calls a rule-based calculator, or a fraud model whose alerts pass through business rules ("neuro-symbolic" AI).

Summary#

  • Knowledge can be written as logic: propositions, connectives, rules (P → Q) and first-order quantifiers.
  • Modus ponens derives new facts; forward chaining is data-driven, backward chaining goal-driven.
  • Expert systems = knowledge base + inference engine; they explain their reasoning but are brittle.
  • Probability (Bayes) and fuzzy logic handle uncertainty and degrees of truth.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Forward chaining engine

Complete forward_chain(facts, rules) - rules are (set_of_conditions, conclusion). Keep adding conclusions whose conditions are all known until nothing new is added, and return the final set. Store the result for the given data in known.

Quick Quiz

Q1.

Modus ponens says: from 'P' and 'P → Q' we can conclude...

Q2.

Forward chaining starts from...

Q3.

An expert system has two main parts...

Q4.

Fuzzy logic allows statements to be...