# Rules

Conceptually, a rule is very simple:

    if
        A
        B
        C
    then
        D
        E

Meaning, "if A, B and C are true, then D and E are also true". These are often called if-then rules.

And what are A, B, C, D and E?

They are simply statements. Specifically, they are generalized statements containing patterns as arguments. You'll see more about this later.

## Premises and Conclusions

Rules have two parts to them: an if part (containing a list of statements called the premises), and a then part (containing a list of statements called the conclusions).

Note

Pyke doesn't use the words if and then, as you'll see shortly.

Each of these if and then parts contain one or more facts or goals which are just generalized statements (meaning statements with patterns as arguments).

## Processing the Premises Within the If Clause

Because each premise with the if clause is a generalized statement, the premise is pattern matched to known facts. This means that it may match more than one fact.

Pyke tries all combinations of matching facts through a process called backtracking. This will cause the same rule to potentially succeed multiple times, once for each unique combination of matching facts.

### Backtracking

Note that while processing the list of premises within a rule's `if` clause:

- If Pyke succeeds at proving a premise:
  - Pyke will proceed down to the next premise in the list.
  - Trying to proceed down from the last premise in the list (when it succeeds) causes the rule to succeed.
- If Pyke fails at proving a premise:
  - Pyke will back up to the prior premise in the list and try to find another solution for it. The process starts over at the prior premise, going back down or further up the list depending on whether another solution can be found to the prior premise or not.
  - Trying to back up from the first premise in the list (when it fails) causes the rule to fail.

Backtracking Flow Diagram

Thus, execution within each rule's `if` clause proceeds both backwards and forwards, up and down the list of premises, depending on whether each premise succeeds or fails. The process of backing up within the `if` clause to try alternate solutions is called backtracking.

## Inferencing

Rules are specified individually within a rule base. They are not nested or explicitly linked to each other. So Pyke must automatically figure out how to combine these rules to accomplish some larger task. This is called inferencing and there are two different approaches that Pyke uses:

- All forward-chaining rules are processed when a rule base is activated.
  - Forward-chaining rules may assert new facts, and activate more specific rule bases.
- Backward-chaining rules are processed when your program asks Pyke to prove a specific goal (i.e., asks Pyke a question).
  - These rules are designed to answer a question rather than assert new facts or activate more specific rule bases.
  - They also have the ability to assemble Python functions into a customized call graph or program, called a plan, to meet a specific need.

Note

Forward-chaining and backward-chaining rules may both be included within the same rule base. Pyke knows the difference between forward-chaining and backward-chaining rules because they have different syntax.

Copyright © 2007-2009 Bruce Frederiksen

Page last modified Mon, Oct 27 2008.