The rule-utils.scm defines the basic data structures and operations
used in the forward and backward chainers. It supports the following abstractions:
- Rules
- Assertions
- Variables
- Bindings
- Knowledge Files
Below, is a brief description of the operations on the various data types:
Rules
- (clear-rules) -- removes all current rules.
- (remember-rule rule) -- add a rule to the end of the rules. If a
rule with the same name exists, it simply replaces it in place. Note that this function
calls process-vars processes the variables in the rule so that ?x
becomes (? x), which is the internal representation of vars.
- (remember-rules rule 1 ... rule n) -- calls
remember-rule for each of the rules.
- (remember-rules-list list of rules) -- calls
remember-rule for each of the rules in the list.
- (get-rules) - returns the list of rules.
- Accessors for the various rule components: rule-name, rule-ifs, rule-thens, etc.
Assertions
- (clear-assertions) -- removes all current assertions.
- (remember-assertion assertion) -- add a assertion to the end of the assertions. If a assertion
with the same name exists, it simply replaces it in place.
- (remember-assertions assertion 1 ... assertion n) -- calls
-- remember-assertion for each of the assertions.
- (remember-assertions-list list of assertions) -- calls
-- remember-assertion for each of the assertions in the list.
- (get-assertions) - returns the list of assertions.
- (display-assertions) -- show the current list of
assertions.
Variables
- (make-simple-variable name) and (make-segment-variable
name) -- create a variable from a name.
- (variable-name variable) -- get the name of a variable.
- (simple-variable? thing) and (segment-variable?
thing) -- is it a ?x variable or a *x variable?
- (nameless-variable? variable) -- is it ?_ variable?
- (variables-in-thing list) -- returns a list of variables present in
the input list.
- (process-vars list) -- returns a new list in which the symbols of the
form ?x or *x are replaced by the internal representation
of variables.
Bindings
Bindings keep track of the values assigned to variables. We distinguish between a single
binding involving one variable and the plural bindings representing a set of
assignments. Bindings are like environments in Scheme.
A single binding has a key, the name of the variable, and a value assigned
to the variable of that name (which may be a variable).
- The basic operations on a single binding: (make-binding key value),
(binding-key binding), (binding-value binding), (binding?
thing).
- The basic operations on a set of bindings (analogous to list operation) --
(empty-bindings), analogous to () and
(add-binding variable value bindings), analogous to
cons.
- (find-binding variable bindings) -- returns a binding for a
variable if it exists or #f otherwise.
- (merge-bindings bindings1 bindings2) -- like append.
- (instantiate-variables thing bindings) -- replaces occurrences
of variables in the first argument with the corresponding value for the variable in the
specified bindings.
Knowledge Files
As a convenient way of packaging rules, assertions and Scheme code. We support "knowledge
files" (we use a .k suffix to keep track of them). A typical file would look like:
assertions
(parent ...)
(parent ...)
rules
(r1 if ... then ...)
(r2 if ... then ...)
code
(define (test-parent) ...)
The order of the assertions, rules and code segments is irrelevant.
The function (read-k-file file-name) reads the file and initializes the
rules and assertions for the chainer. Note that the variables can be written as
?x, which is a bit easier to type and read.
|