Frontend Labs
⋯

Part 2 — Era 1: Rules (1950s–1980s)

Why start here? Because rule-based systems are the clearest possible contrast to LLMs, and because understanding why they failed explains almost every design decision that came after. Also: rules never went away, and knowing when to use them will make you a better engineer than someone who reaches for an LLM every time.

Chapter 5 — Teaching a computer with IF-THEN rules

In one sentence

You write down the knowledge as explicit rules, and the computer chains them together to reach conclusions.

The problem

In the 1970s, computers were expensive and data was scarce. But experts existed — doctors, geologists, engineers. The idea: interview the expert, write their knowledge as rules, and let the machine apply them tirelessly.

The idea, in plain language

A rule system has three parts. Think of a detective:

+------------------+     +--------------------+     +-------------------+
|  WORKING MEMORY  |     |     RULE BASE      |     | INFERENCE ENGINE  |
|                  |     |                    |     |                   |
| what I currently |<--->| everything I know  |<--->| the detective who |
| know about THIS  |     | in general, as     |     | keeps applying    |
| case             |     | IF-THEN rules      |     | rules to facts    |
|                  |     |                    |     |                   |
| "has fever"      |     | IF fever AND cough |     | 1. which rules    |
| "has cough"      |     | THEN maybe flu     |     |    can fire?      |
| ...              |     | ...                |     | 2. fire one       |
|                  |     |                    |     | 3. add the new    |
|                  |     |                    |     |    fact, repeat   |
+------------------+     +--------------------+     +-------------------+

The inference engine loops: look at what you know, find a rule whose IF-part is satisfied, apply it, add the new fact, repeat until nothing new can be derived.

How it actually works

Two directions of reasoning:

Forward chaining — start from facts, see what follows. ("What can I conclude?")

Rules:
  R1: IF has_fur   AND says_woof  THEN is_dog
  R2: IF is_dog    AND is_small   THEN is_lapdog
  R3: IF is_lapdog                THEN needs_small_bed

Known facts: has_fur, says_woof, is_small

Step 1: R1 fires  -> add "is_dog"
Step 2: R2 fires  -> add "is_lapdog"
Step 3: R3 fires  -> add "needs_small_bed"
Nothing left to fire. Done.

Backward chaining — start from a question, work backwards. ("Can I prove X?")

Goal: needs_small_bed?
  R3 concludes it, but needs is_lapdog. Is is_lapdog true?
    R2 concludes it, but needs is_dog AND is_small.
      is_small? -> yes, it's a known fact.
      is_dog?
        R1 concludes it, but needs has_fur AND says_woof.
          has_fur?    -> yes (fact)
          says_woof?  -> yes (fact)
        -> is_dog PROVEN
    -> is_lapdog PROVEN
  -> needs_small_bed PROVEN

Forward chaining derives everything; backward chaining proves one thing. Prolog, a whole programming language, is built on backward chaining.

Worked example: the Help Desk with rules

rules = [
    ({"mentions_holiday", "asks_how_many"},  "answer_holiday_days"),
    ({"mentions_parental", "mentions_leave"}, "answer_parental_policy"),
    ({"mentions_expense", "mentions_monitor"}, "answer_equipment_policy"),
    ({"answer_holiday_days"},                 "send_holiday_email"),
]

answers = {
    "answer_holiday_days":    "You get 25 days of paid holiday per year.",
    "answer_parental_policy": "Parental leave is 26 weeks at full pay.",
    "answer_equipment_policy":"Monitors up to 400 EUR can be expensed.",
}

def extract_facts(question):
    q = question.lower()
    facts = set()
    if "holiday" in q:          facts.add("mentions_holiday")
    if "how many" in q:         facts.add("asks_how_many")
    if "parental" in q:         facts.add("mentions_parental")
    if "leave" in q:            facts.add("mentions_leave")
    if "expense" in q:          facts.add("mentions_expense")
    if "monitor" in q:          facts.add("mentions_monitor")
    return facts

def forward_chain(facts, rules):
    facts = set(facts)
    changed = True
    while changed:
        changed = False
        for conditions, conclusion in rules:
            if conditions <= facts and conclusion not in facts:
                facts.add(conclusion)
                changed = True
                print(f"   fired: {sorted(conditions)} => {conclusion}")
    return facts

def answer(question):
    print(f"\nQ: {question}")
    facts = extract_facts(question)
    print(f"   extracted facts: {sorted(facts) or 'NONE'}")
    derived = forward_chain(facts, rules)
    for key, text in answers.items():
        if key in derived:
            return text
    return "Sorry, I don't have an answer for that."

print(answer("How many holiday days do I get?"))
print(answer("What is the parental leave policy?"))
print(answer("How much annual leave do I have?"))     # <-- watch this one
print(answer("Can I expense a monitor?"))

Run it. The first, second, and fourth work perfectly, with a full trace of why. The third — "How much annual leave do I have?" — fails, because the words "holiday" and "how many" don't appear. This is not a bug you can fix by trying harder; it is the fundamental limit. More on this in Chapter 7.

Practice (20 min)

  1. Run the code above.
  2. Add rules to handle "annual leave", "vacation", "PTO", and "time off".
  3. Now add: "how much holiday can I carry over?" and "do I get more holiday after 5 years?" — these need different answers about holiday.
  4. Count how many rules you needed. Extrapolate: how many for a 400-page handbook?
  5. Ask a friend to write 10 questions without seeing your rules. How many does your system answer?

Record the numbers in notes.md. Step 5 is the whole lesson of Part 2.

Common confusions

  • "Isn't this just if statements?" Essentially yes — but with a separate rule base an expert can edit without touching code, plus an engine that chains rules automatically and can explain its reasoning.
  • "Is this AI?" Yes, historically and definitionally. It's symbolic AI. It just isn't machine learning — nothing is learned from data.

Check yourself

  1. What are the three parts of a rule-based system?
  2. When would you use backward instead of forward chaining?
  3. Why did "How much annual leave do I have?" fail?

Further reading

  • Book: Artificial Intelligence: A Modern Approach (Russell & Norvig), chapters 7–9 — the standard textbook on logic and inference. Dense but definitive; dip in, don't read cover to cover.
  • Video: "Expert Systems" — Computerphile (~10 min).
  • Try it: SWI-Prolog (swi-prolog.org) has a browser playground. Writing 20 lines of Prolog teaches backward chaining faster than any article.
  • Article: "CLIPS: A Tool for Building Expert Systems" — the NASA-built rule engine, still used and documented.

Generated from real-life-ai-example-v1.md · 8 Oct 2026 16:36