12.8 Rules

Building Parsers with Java
By Steven  John  Metsker

Table of Contents
Chapter  12.   Engines


A rule is an axiom , or statement of truth, that has more than one structure. In a rule, the truth of the first structure follows from the ability to prove the remaining structures. For example, you could add to a program a rule that displays itself as

 custCharge (Name, Fee) :-      charge(City, Fee), customer(Name, City); 

The " :- " symbol is the "if" symbol. Prolog and Logikus use this symbol rather than a comma after the first structure in a rule. This convention emphasizes the meaning of rules: The first structure is true if the latter structures are provable. Figure 12.4 shows the Rule class.

Figure 12.4. The Rule class. A rule is a sequence of structures. A query can prove itself by unifying with the first structure in a rule and then proving the remaining structures.


The custCharge rule means that you can find customer charges by proving the charge and customer structures. As with queries, a variable's scope is the rule in which it appears. Thus, the custCharge rule joins charge and customer on the City variable.

To this point, you have seen structures prove themselves by unifying with facts in a program. A more comprehensive explanation of how structures prove themselves is that they unify with the first structure of an axiom in a program and prove the axiom's remaining structures. The first structure of an axiom is its head; the remainder is its tail. One view of a fact is that it is an axiom that has no tail. Because there is no tail to prove, a structure can prove itself simply by unifying with a fact in a program. In the case of a rule, a structure unifies with the head of the rule and asks the tail to prove itself.

For example, if you added the custCharge rule to the shipping charges program, you could query the program with a query that would display itself as

 custCharge(Name, 23) 

When this query proves itself, it unifies with the custCharge rule and asks the rule's tail to prove itself. This proof succeeds, binding the Name variable to "Acropolis Construction" .

Speaking somewhat loosely, rules provide the logic in a logic program. They allow flexible modeling of the relationships between objects such as customers and shipping charges. They also allow modeling of logical concepts such as the observation that if Yarmouth is in Maine and Maine is in the United States, then Yarmouth must be in the United States. Rules can also model arbitrary definitions, such as a business's definition of a "good" customer. Chapter 13, "Logic Programming," gives many examples of the use of rules in a logic program. Before turning to that chapter, however, we'll consider several other features of the logic engine.


Building Parsers with Java
Building Parsers With Javaв„ў
ISBN: 0201719622
EAN: 2147483647
Year: 2000
Pages: 169

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net