Web expert system for Visual Prolog (6)

Source: Internet
Author: User
Tags knowledge base

Save the user's answer to the query as a condition for further reasoning

Or start with the goal section. The starting sentence is Write_startform ()

Write_startform ():-        Write ("<form action=\" \cgi-bin\geni.exe\ "method=\" post\ ">\n").

Watch out! the previous sentence should be write ("<form action=\" geni.exe\ "method=\" post\ ">\n").

The Geni virtual host does not have the "cgi-bin" subdirectory.

In the goal, it is assert_conditions (PARMLIST1) that follows the previous sentence.

  Assert_conditions ([]):-!.  Assert_conditions ([Parm (Name,val) | Rest]):-concat ("Cond_", Condnumberstr,name), Str_int (Condnumberstr,cond),               !,assert_cond (cond,val), Assert_ Conditions (Rest).  Assert_conditions ([_parm| Rest]):-!, assert_conditions (rest).  Assert_conditions (_):-errorexit.

Prolog does not have the mechanism of the C language For,while and other processing loops.

Prolog is a recursive implementation of the "loop".

The 4 clauses of predicate assert_conditions represent a typical, standard recursive application.

Clause 1, sets the recursive termination condition.
The terminating condition is that the list to be processed is empty.

Clause 2, which handles the list header, which is the 1th member of the list.

Clause 3, ignoring the header of the list to be processed, continues processing the remaining footer.

Clause 4, prepare the processing after the error.
The prerequisites for Clause 4 execution are:

1, clause 1 header matching is unsuccessful, or, "truncate"! The previous statement failed;
2, and the clause 2 header match is unsuccessful.

Specifically which statement failed, and the cause of the failure, to be analyzed below.

Clauses 2 and 3, the statements at the end are recursive calls of their own.
This is the tail recursion.
The benefit of tail recursion is that it can be infinitely recursive and will not run out of memory.

Look at the statement in Clause 1.

Concat ("Cond_", Condnumberstr,name)

It is a built-in predicate, the flow pattern is (i, I, O), (o, I, i), (i, O, i), (I, I, i)
For connection of 2 strings

The flow pattern for this example is (i, O, i), and from the variable name, the CONDNUMBERSTR should be a number

So, if Name= "Cond_3", then condnumberstr=3

At this point, you can try a little trick on the previous section:

In Vde, the menu file| New, a blank file appears Noname.pro, write:

Goalconcat ("Cond_", A, "Cond_1").

Menu project| Test Goal, target compilation execution, result:

A=11 Solution

And look at the next statement.

Str_int (Condnumberstr,cond)
This is a built-in predicate: the Str_int (String stringarg, integer intarg) flow pattern is (i, O), (o, i), (I, I) for the mutual conversion of strings to integers it is recommended to try this little trick.

After this sentence, it is truncated "!", thus, the condition that causes clause 2 to go back to Clause 3 is:

1. The variable name is not a string that begins with "Cond_", or 2, the variable condnumberstr is not a numeric string.

This backtracking causes the execution of clause 3, which results in ignoring the current processing object, starting a new recursion,
Processes the post-processing objects in the list.

Keep looking down:

Assert_cond (Cond,val)
  Assert_cond (CNO, "yes"):-!, assert (yes (CNO)).  Assert_cond (CNO, "no"):-!, assert (No (CNO)).  Assert_cond (_cno, "why"):-!, assert (Whymeet).  Assert_cond (_,_):-errorexit.

CNO is the identification number of the "condition" in the Knowledge base.

Yes (CNO), no (CNO), stored in the fact base TMP,

It means: On condition cno, answer yes/no.

Whymeet, also stored in the fact library TMP,

This means: The user asks the system to reply, why ask this question.

The last sentence, Assert_conditions (Rest).

Recursively handles the list of tail rest.

Today's focus is the tail recursion, which is also a major feature of Prolog.

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.