Learn Prolog now Translations-chapter II-Unity and Proof Search-section II, proving search

Source: Internet
Author: User
Tags knowledge base

Proof Search

In the previous section we have learned Oneness, and in this section we continue to learn how Prolog searches the Knowledge base to determine whether an input query can be met. We will learn to prove the search and to cover this basic concept through a simple example.

Suppose we have the following Knowledge Base:

f (a). f (b). G (a). g (b). h (b). K (x):-F (x), g (x), H (x).

If we query:

?-K (Y).

The result of this query is very obvious, that is, K (b), but how does the prolog solve it? Let's keep looking.

Prolog reads the entire knowledge base and then attempts to combine the query K (Y) with the facts in the Knowledge base or the header of the rule. The search knowledge Base is top-down and finds the corresponding information in the first possible location. This example just has a

May: K (Y) and rule, K (x):-F (x), g (x), H (x) of the head in Oneness.

When prolog a variable in a query with a fact or a variable in a rule, a new variable (such as _G34) is generated to represent the shared value, so the original query is converted to:

K (_G34)

And the rules in the Prolog knowledge Base are converted to:

K (_G34):-F (_G34), G (_G34), H (_G34).

What's the situation now? The query says, "I want to find someone who matches the attribute K." The rule says, "If you want to find the person who conforms to the attribute K, then this person must also conform to attribute f,g and H". So if Prolog find a letter that matches the attributes K,g and H

interest, then will be able to satisfy the original query. So Prolog uses the following target to replace the original query:

F (_G34), G (_G34), H (_G34).

If you use a graphical presentation, it will make the process more image, as follows:

In the box are all queries or targets. Specifically, our original goal is to prove K (Y), so it appears in the top-level box. When we combine K (Y) and the rule header in the database, _g34 as a new intermediate variable for shared values,

be assigned to x, y, so we have a new target: F (_G34), G (_G34), H (_G34), appears in the second box.

Now, whether or not there is a series of sub-goals to prove, Prolog will try to satisfy each other by a left-to-right order. On the leftmost target is F (_G34), which is "want to find information that satisfies the property F". Prolog Tasting

try to search from the top and the search for knowledge Library. The first to find the one that can and query is the fact that F (a). This satisfies the target F (_g34) and the remaining two targets. Now, when we combine F (_G34) and F (a), _G34 will be initialized to a,

and this initialization will target all _g34 in the list are replaced by a, so the remaining target list looks like this:

G (a), H (a)

The current proof search graph is as follows:

But G (a) is a fact in the knowledge base, so the first target in the remaining target list is already satisfied, so we're left with:

H (a)

The graphic of the proof search is as follows:

But it is no longer possible to prove H (a), the last goal. Because of property H, the only fact that we can know through the knowledge Base is that H (b) cannot be in unity with Target H (a).

So what's going to happen next? Prolog will admit to making a mistake and check if there are other possible values when the target is in unity. With the path shown in the graphic above, Prolog will return to a node that can be replaced by one.

now, in the Knowledge base There is no alternative to G (a), but there are other ways of uniting with F (_G34). There are multiple possible nodes that are called Select nodes. Prolog will keep track of the selected node, so

when it finds that an option is wrong, it can go back to the selection node and try another path. This process is called backtracking, and it is the basis of Prolog proof search.

So continue our example, Prolog back to the selection node. The node in the graph above is the following list of targets:

F (_G34), G (_G34), H (_G34).

Prolog must start working again and he must try to meet the first goal again. In the knowledge base, the objective can be met by combining facts F (b) and F (_G34). This oneness initializes the _g34 to B, so the remaining target columns

The table is:

G (b), H (b).

G (b) is also a knowledge base of facts that can also be satisfied, so the remaining:

H (b).

And this is a fact in the knowledge base, so this goal is fulfilled. The target list for Prolog is now empty. This means that the original query is sufficient for every purpose required, so the original query is successful and,

Prolog also found Initializes the variable y to B in order to achieve the goal and initialize it.

Consider when we enter ";" It is also interesting to query whether there are other solutions:

This forces prolog to backtrack and to search for other possibilities. However, the above example has no other solution, because there is no other possibility in the knowledge base to combine H (b), G (b), F (_g34) or K (Y), so Prolog

will answer false. on the other hand, if there is another rule that contains K, Prolog will continue to try the way we described it, that is, a top-down search in the knowledge base that satisfies the target list from left to right, and if there is a failure, it goes back to

Select the node.

Let's look at the overall search process, such as:

This figure is the form of a tree, in fact, this is our first example of a search tree. The non-leaf nodes of this tree are designed to meet the current target list of different steps to prove the search, and the edge of the tree preserves the knowledge base to meet the current goal

a fact or a rule in a make Unity of variable Initialization information (note that the current target is the first and leftmost target of the target list), and if the leaf contains a target that is not met, then it is the point of Prolog failure (the wrong path,

no solution exists); If the leaves Not package with any The goal is a possible solution. The path from the root node to the successful leaf node gives full information about the values that must be initialized for the variable to satisfy the original target.

Next we look at another example, assuming we have the following Knowledge Base:

loves (Vincent, Mia). Loves (Marcellus, Mia). Jealous (A, B):-Loves (A, C), loves (B, c).

If we query:

?-Jealous (X, Y).

The corresponding search tree is as follows:

There is only one way in the Knowledge Base for jealous (X, Y), which is to use the following rules:

Jealous (A, b):-Loves (A, C), loves (b, c).

So our target list becomes the following:

Loves (_g5, _g6), loves (_g7, _g6).

Now, we need to be in the Knowledge Base for loves (_g5, _g6). There are two ways to do this (fact 1 and fact 2), so this is also a choice node. Both cases result in the remaining loves of the target list (_g7, Mia),

This goal can also be met through two facts in the Knowledge base. Finally, all the leaf nodes have no unmet goals, so it means that there are four solutions that satisfy the original query. By initializing the information on a variable in the path, you can derive

The following four solutions:

1. X = _g5 = Vincent; Y = _g7 = Vincent

2. X = _g5 = Vincent; Y = _g7 = Marcellus

3. X = _g5 = Marcellus; Y = _g7 = Marcellus

4, X = _g5 = Marcellus; Y = _g7 = Vincent

Please carefully analyze the above examples and make sure that they are fully understood.

Learn Prolog now Translations-chapter II-Unity and Proof Search-section II, proving search

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.