Learn Prolog now Translations-chapter I-Facts, rules and queries-section I, some simple examples

Source: Internet
Author: User
Tags knowledge base

This series of articles is an online prolog learning material: Www.learnprolognow.org's Chinese translation. Hope to be able to translate this learning materials, to achieve two purposes: first, the system to learn prolog knowledge; second, improve the understanding of English articles

and translation capabilities.

Content Summary:

Give some simple examples of Prolog programming;

The basic structure of Prolog: facts, rules and inquiries;

Environment Description:

The Prolog operating environment used in this series of articles is: Swi-prolog, the official website address is: http://www.swi-prolog.org.

There are only three types of infrastructure in Prolog: Facts (facts), rules, and Queries (queries). The set of facts and rules is known as the Knowledge Base (knowledge Base), or a database, in order to differentiate it from the traditional database ,

Unified use of the Knowledge Base), Prolog programming is almost the writing of the Knowledge Base. In other words, Prolog programming is basically equivalent to writing a knowledge base, consisting of a combination of facts and some of the relationships we are interested in.

So how do you use the Prolog program? By querying, that is, by asking questions, the information and answers to these questions are stored in the Knowledge base.

It may sound strange, it seems to have nothing to do with the traditional programming, after all, programming should tell the computer what to do? But we will see that Prolog's programming is very meaningful, at least in a particular field, such as a computer

Linguistics and the field of artificial intelligence. Let's go into the practice of writing some simple knowledge base, in the process of learning Prolog, practice is the best and only way.

Knowledge Base 1

knowledge Base 1 (KB1) is just a collection of simple facts. The fact is that there are some states or relationships that are unconditionally true . For example: we can define mia,jody and Yolanda are ladies, Jody is playing the guitar, and then there's a party,

In Prolog, you can define the following 5 facts: (Note that each fact is defined as the end identifier with a period of English characters)

Woman (MIA) Woman (Jody). Woman (Yolanda). Playsairguitar (Jody). party.

The fact set above is KB1, which is our first example of Prolog programming. Note that the names mentioned above Mia,jody,yolanda, their women attributes (which can be understood first) and the guitar, and their gatherings, are all lowercase letters beginning with our

later will explain the reasons in detail.

How to use KB1? Through the query. That is, use KB1 by querying the information that some KB1 include. Here are some examples, such as the following query we can ask Prolog,mia is not a lady:

?-Women (MIA).

Prolog will answer: Yes

Because the fact that women (MIA) is clearly defined in KB1, the answer to Prolog is yes. Note that we do not need to explicitly type in the actual use of Prolog queries--this prolog (which may differ slightly from the Prolog interpreter) is to be lost

into symbols. at the end of the query statement, be sure to enter a period as the Terminator, if there is no input, then Prolog will not perform the query operation, but wait.

Similarly, we can ask Prolog,jody whether to play the guitar by using the following query statement:

?-Playsairguitar (Jody).

Prolog also answers: Yes, because this is also a fact in KB1. But if we try to ask Mia whether to play the guitar:

?-Playsairguitar (MIA).

Prolog will answer: No, because this is not the fact of KB1, and KB1 is very simple, other facts can not deduce this conclusion, so prolog that Playsairguitar (MIA) in the KB1 can not be established.

Here are two important examples. First, if we query:

?-Playsairguitar (Vincent).

Prolog will answer no again. Why? Because the Vincent mentioned in this query has no information in KB1, Prolog believes that no other information about him can be deduced in KB1. Similarly, if we query like this:

?-Tatooed (Jody).

Prolog will also answer No. Why? Because the tatooed attribute mentioned in this query has no information in KB1, Prolog does not think that any other information related to this attribute can be deduced.

Needless to say, we can query the properties of interest, such as:

?-party.

Prolog will answer yes. If we query:

?-Rockconcert.

Prolog will answer no, and our expectations are the same.

Knowledge Base 2

Here is the definition of KB2, our second Knowledge Base:

Happy (Yolanda). Listen2music (MIA). Listen2music (Yolanda):- Happy (Yolanda). Playsairguitar (MIA):- Listen2music (MIA). Playsairguitar (Yolanda):-Listen2music (Yolanda).

in KB2, there are two facts: Listen2music (MIA) and Happy (Yolanda), the remaining three are rules. the rules in Prolog are some states or relationships with conditions that are true. such as rule one can understand: Yolanda listen to music if

Yolanda was delighted that the last rule could be understood: Yolanda play the guitar if Yolanda listen to music. More abstract understanding, symbols:-Understood as "if", or "What is the premise". In:-The left part is the head of the rule, in:-The right part

Is the backbone of the rule, so the rules can be understood: if the backbone of a rule is true, then the head of the rule is true. Therefore, the following are the main points of the Prolog rule application:

If the Knowledge Base includes a rule, head:-Body, and Prolog knows in the knowledge base that the body part is true, then Prolog can deduce that head is true. The underlying step of derivation is called hypothetical inference (modus ponens).

Let's continue to look at specific examples if we check if Mia plays the guitar:

?-Playsairguitar (MIA).

Prolog will answer yes, why? After all, in KB2, Playsairguitar (MIA) is not a fact, but you can find a rule about it:

Playsairguitar (MIA):-Listen2music (MIA).

It is clear that KB2 contains the facts of Listen2music (MIA). So prolog can use the rules of hypothetical inference to derive Playsairguitar (MIA) as true.

Another example below shows that Prolog can use the hypothetical inference chain if we query:

?-Playsairguitar (Yolanda).

Prolog will answer yes, why? First, by using the fact of Happy (Yolanda) and its related rules:

Listen2music (Yolanda):-Happy (Yolanda).

Prolog can deduce a new fact: Listen2music (Yolanda). This new thing is implicit in the Knowledge base (derived from derivation), but Prolog can use it just as you would with an explicit fact. Next, through this derivation of the

Facts and their rules:

Playsairguitar (Yolanda):-Listen2music (Yolanda).

Prolog can deduce the new fact: Playsairguitar (Yolanda), that is, our query results are true. To summarize: any fact that a hypothetical inference can be used as input to other rules, by linking all the hypothetical inference application combinations

Up, Prolog can derive any logical information from the facts and rules contained in the knowledge base.

The facts and rules in the knowledge base are collectively referred to as clauses (clauses). So KB2 includes 5 clauses, including 3 rules and 2 facts. Another way to look at KB2 is to say that it is made up of 3 predicates (predicates) (or become procedures)

Composition, three predicates are: Listen2music,happy,playsairtguitar.

Where the happy predicate consists of a separate clause (a fact). The Listen2music and Playsairguitar predicates are made up of two clauses (listen2music two clauses, one is fact, the other is a rule; Playsairguitar two clauses are all rules

is composed. It can be thought that Prolog programming is composed of predicates. In essence, the concept of predicates is important, and the various clauses in programming are about the meaning of the predicate representation and the meaning of its derivation.

One more thing to mention is that we can look at the fact that there is no skeleton rule, that is, we can think of the fact that the rule is set up regardless of the conditions.

Knowledge Base 3

KB3, our third Knowledge base, consists of 5 clauses:

Happy (Vincent). Listen2music (Butch). Playsairguitar (Vincent):- Listen2music (Vincent), Happy (Vincent). Playsairguitar (Butch):- Happy (Butch). Playsairguitar (Butch):- Listen2music (Butch).

The KB3 defines two facts, happy (Vincent) and Listen2music (Butch), and three of its rules. KB3 defines three names in the same predicate as the KB2 (Happy,listen2music, and Playsairguitar), but its implementation is different,

In particular, some new meanings are introduced in the predicate of Playsairguitar. First, analyze This rule:

Playsairguitar (Vincent):-

Listen2music (Vincent),

Happy (Vincent).

There are two main components, or two goals. The most important thing here is the comma character, which separates the target Listens2music (Vincent) from the target Happy (Vincent). This is the form of logic and representation in Prolog. So it can be so

Understanding : "Vincent plays the guitar if he listens to music and he is happy".

So, if we query:

?-Playsairguitar (Vincent).

Prolog will answer No. This is because KB3 contains the facts of Happy (Vincent), but does not explicitly contain listen2music (Vincent), and Listen2music cannot be deduced. So KB3 can only meet Playsairguitar (Vincent)

two x one of the conditions, so the query fails, prolog answer No.

By the way, the space is meaningless in Prolog, for example, we can also write:

Playsairguitar (Vincent):-

Happy (Vincent),

Listen2music (Vincent).

This is the same effect as the previous definition. Prolog provides a high degree of writing freedom, allowing us to write high-readability code.

Next, analyze the two rules in the KB3 that have the same head:

Playsairguitar (Butch):-Happy (Butch).

Playsairguitar (Butch):-Listen2music (Butch).

The expression here means that Butch plays the guitar if he listens to music, or he is happy. That is, multiple rules with the same head are logical or expressive in Prolog. So, if we query:

?-Playsairguitar (Butch).

Prolog will answer yes. Although the first rule has no effect on this query (because Happy (Butch) does not exist in KB3 and cannot be deduced), KB3 contains Listen2music (Butch), so Prolog can use hypothetical inference:

Playsairguitar (Butch):-Listen2music (Butch).

Derivation in Playsairguitar (Butch).

There is another way of representing logic in Prolog or, you can replace the previous two rules with the following definitions:

Playsairguitar (Butch):-

Happy (Butch);

Listen2music (Butch).

The English semicolon character in Prolog also represents logic or, so the meaning of this single rule is the same as the meaning of the previous two rules. Which is better, using multiple rules, or using English semicolons? This needs to be judged according to the circumstances. one hand

Using semicolons makes the Prolog code less readable, but on the other hand, fewer rules are used after semicolons, making processing more efficient.

In the above study, it can be seen that Prolog explicitly includes a lot of logical identification, for example,:-"If", the English character comma "," means logical and; English character semicolon ";" Represents a logical OR. And we can see the standard logic proof rules

(hypothetical reasoning) played an important role in the Prolog. So, we can begin to understand why the name "Prolog" is abbreviated by "Programming with Logic":).

Knowledge Base 4

here is the definition of KB4, our fourth Knowledge Base:

Woman (MIA) Woman (Jody). Woman (Yolanda). Loves (Vincent, Mia). Loves (Marsellus, Mia). Loves (pumpkin, honey_bunny). loves (Honey_bunny, Pumpkin).

haha, this is a rather boring knowledge base. There are no rules, only the set of facts. Of course, we can contact a predicate relationship for the first time there are two names (this refers to the relationship defined by loves).

But this is not the case, the new idea is not the definition of the knowledge base, but the way we query. In fact, this is the first time we have touched the Prolog variable using the following:

?-Woman (X).

x represents a variable (in Prolog, a word that starts with a capital letter represents a variable, which is why all occurrences of the character in our previous example are the beginning of a lowercase letter). Here x is not a specific name, it is more like a placeholder for information

Symbol. That is, this query is asking Prolog: Tell us who you know is a lady (woman)?

Prolog to answer this query from top to bottom in KB4, Prolog tries to find (or match) the information of the expression Woman (X). In KB4, the first fact is woman (MIA), so Prolog will combine X with Mia so that it can be perfectly

Close query (Incidentally, Prolog does a lot of things in this process: we can simply understand that Prolog initializes x to Mia, or binds x values to MIA). The Prolog will return the results as follows:

X = Mia

It's not just that there is at least one result that satisfies the query that exists in KB4, but also explicitly tells the result value. Here Prolog does not answer yes, but actually gives the variable bindings (initialization of variables) that satisfy the query.

But that's not the end. The point of a variable is that they can represent, or be able to combine, different information that conforms to the query. And there are other women in the knowledge base that also satisfies this query. So we can enter the English character ";" to continue querying for the next match

The result:

X = mia;

Because the English character ";" The representation is logical OR, so this query can be understood as: are there any other results? So Prolog will continue to traverse the knowledge base (where it will mark the last result, and continue from there), look for the next possible result, and find

Jody is also satisfied, so Prolog will answer:

X = mia;

X = Jody

This tells us the result value of the second conforming query based on KB4. Of course, if we continue to enter English characters ";", Prolog will continue to answer:

X = mia;

X = Jody;

X = Yolanda

But when we enter English characters for the third time, ";" What's going to happen? Prolog will answer no, there is no other possibility of unity. Because there is no women beginning in KB4, the remaining 4 rules are about loves relationships, so there is no way to woman (X)

In Unity.

Next, let's try a more complex query, as follows:

?-Loves (Marsellus, x), Woman (x).

As already mentioned, the English character comma "," means logical and, so the meaning of this query is: whether there is such an x, it can meet marsellus love it, and still a lady? If you look at the Knowledge base again, you will find: Mia is a woman (fact one)

, while Marsellus love Mia (fact 5). Prolog can simulate this ability and find out the results. That is, Prolog is able to traverse the entire knowledge base and combine X with Mia, making the two subqueries in our query satisfy. Finally, Prolog will answer:

X = Mia

The ability to combine variables and information in a knowledge base is at the heart of Prolog. As we learn in depth, we will find many interesting ideas of Prolog--but the ability to prolog and return variable binding information is a key point for all of this.

Knowledge Base 5

OK, we've introduced the variables before, but they're only used in queries. In fact, variables can also be applied to the knowledge base. And only by doing so can we write a really useful prolog program. The following is a simple example of the definition of Knowledge base KB5:

loves (Vincent, Mia). Loves (Marsellus, Mia) loves (pumpkin, honey_bunny). Loves (Honey_bunny, pumpkin). Jealous (X, Y) :-Loves (X, z), loves (Y, z).

KB5 contains the facts and a rule of 4 loves relationships, the most interesting one we have ever defined: It includes three variables (x, y, z), how does this rule understand?

Essentially, this rule defines the concept of "rival". Can understand here, if x Love Z, at the same time y also love Z, then x and Y is the rival (hehe, here the rival is limited to learning, the reality will be more complex,:)). The key point is that this is a

General statements, which do not involve specific people, such as Mia,pumpkin, are, in a way, a person in the world. This is abstraction.

If we make a query:

?-Jealous (Marsellus, W).

The meaning of this query is: Is there such a person W, he and Marsellus are "rival"? Vincent is the man. If you check the definition of "rival", you can find Marsellus and Vincent are "rival", because they are two people love

The same lady, Mia. So Prolog will answer:

W = Vincent

Learn Prolog now Translations-chapter I-Facts, rules and queries-section I, some simple examples

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.