Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 36 ยท Knowledge Representation

Logic and Production Rules for Knowledge

Represent knowledge with propositional and predicate logic and with production rules: truth tables, entailment, modus ponens, variables and unification, in runnable Python.

Of all the ways to write down knowledge, logic is the one with the clearest meaning, and production rules are the one that experts find easiest to write. Expert systems usually use the second but borrow ideas from the first. This lesson shows both: how logic defines what follows from what, and how production rules turn knowledge into running programs.

Propositional logic

Propositional logic works with statements that are true or false, called propositions, combined with NOT, AND, OR, implication (->) and equivalence (<->). For example:

  • rain -> wet_ground ("if it rains, the ground is wet")
  • sprinkler -> wet_ground
  • rain (a fact)

From rain -> wet_ground and rain we may conclude wet_ground. That inference pattern is modus ponens. Its mirror, modus tollens, says: from rain -> wet_ground and NOT wet_ground, conclude NOT rain.

A set of statements entails a conclusion when the conclusion is true in every situation (every assignment of true/false to the propositions) where all the statements are true. For a few propositions we can check this by brute force with a truth table.

Python Does the knowledge base entail the conclusion?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The second answer comes with a counterexample: a world in which the rules and the fact rain hold but sprinkler is false. Entailment means "true in all such worlds", so one counterexample is enough to refuse a conclusion. The cost of this method is 2^n rows for n propositions, which is why real systems use smarter proof methods such as resolution.

Predicate logic

Propositions cannot say "every bird has feathers" without listing each bird. Predicate (first-order) logic adds objects, predicates and quantifiers:

  • Bird(x): a predicate that is true for some objects.
  • forall x: Bird(x) -> HasFeathers(x): for all x.
  • exists x: Bird(x) AND CanSwim(x): for at least one x.

To apply a rule with variables to a fact, the system must find values for the variables that make the pattern match. This matching step is called unification. The example below handles the simple case where variables are names starting with ?.

Python Rules with variables and unification

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first rule joined two facts through the shared variable ?y, the same idea as a join in a database. Our little engine matched bob in both. This pattern-matching core is exactly what real production-rule engines (CLIPS, Drools, Prolog) refine.

Production rules

A production rule has the form IF conditions THEN actions. It is the dominant representation in expert systems for good reasons:

  • It reads like advice an expert would give: IF the engine cranks but does not start AND the fuel gauge shows empty THEN advise refueling.
  • It is modular: each rule is independent, so you add or remove one without touching the others.
  • It separates knowledge (the rules) from the control (the engine that decides which rule to run).

A production system has three parts: a working memory of current facts, a rule base, and an inference engine that repeatedly picks a rule whose conditions match and executes its action. When several rules match at once, a conflict resolution strategy chooses one, for example:

Strategy Choose the rule that...
Priority (salience) the author marked as most important
Specificity has the most conditions (most specific wins)
Recency matches the most recently added fact
Order comes first in the list
Python Conflict resolution by specificity

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Here two rules match, but the more specific one (two conditions) gives the safer advice, so it wins.

Logic versus production rules

  • A logical implication is a statement: it can be used forward (modus ponens) or backward (modus tollens), and means the same either way.
  • A production rule is a procedure: the engine reads it in the direction the author intended, and its right side can do things (ask a question, print advice, retract a fact), not only conclude.
  • Fuzzy rules are production rules whose conditions are degrees of membership and whose conclusions are fuzzy sets or numbers. The next chapter builds complete rule-based systems on this idea.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does modus ponens conclude from 'p -> q' and 'p'?

    If the implication holds and its condition is true, the consequence follows.
  2. 2. A knowledge base entails a conclusion when...

    Entailment requires truth in all models of the knowledge; one counterexample refutes it.
  3. 3. What does unification do?

    Unification binds variables such as ?x so that a rule's condition equals a fact.
  4. 4. Which is an advantage of production rules?

    Modularity and readability make production rules easy to write and maintain.

Summary

  • Propositional logic combines true/false statements; entailment is truth in all models, checkable by truth tables for small cases.
  • Predicate logic adds objects, predicates and quantifiers; unification matches rules with variables to facts.
  • Production rules (IF-THEN) separate knowledge from control and are the main scheme in expert systems.
  • Conflict resolution (priority, specificity, recency, order) picks among simultaneously matching rules.
  • Fuzzy rules are production rules whose truth values are degrees.

Next: The Architecture of an Expert System