Web expert system for Visual Prolog (8)

Source: Internet
Author: User
Tags knowledge base

The core of Geni--Inference Machine (2) Process Control

1, in detail fail, "!" and backtracking

Compared with other languages, the most important feature of Prolog is its backtracking mechanism.

Backtracking mechanism, the main means are 2, one is to use the predicate fail to trigger the backtracking, and the second is to use the special verb "!" (read as cut) cancels backtracking.

Prolog uses fail to trigger backtracking, to realize the cycle of the program, and to use "!" Control of backtracking.

Look at a typical example:

FACTSF (integer) Predicatesa () b () C () Clausesa ():-write ("------a-------\ n"), F (x), Writef ("%\n", x),fail. A ():-write ("A is sucsess."). %---------------------------------------------------------B ():-write ("\ n-------b-------\ n"), F (X), Writef ("%\n ", X),!,fail. B ():-write ("B is sucsess."). %---------------------------------------------------------C ():-        write ("\ n \ nthe-------c-------\ n"), A (), B (), !,write ("\nc sucsess."). C ():-write ("\nc backtracking.\n\n").%---------------------------------------------------------Goalassert (f (1)), Assert (f (2)), Assert (f (3)), C ().

In Vde, menu selection: file| New, create a file Noname.pro, put the above code in

Menu selection: project| Test Goal,

Run the result, the output is shown as follows:

-------C-------------a-------123a is sucsess.-------b-------1c Backtracking.yes

The program is very simple, goal first put the facts F (1), F (2), F (3) into the storage, and then verify C ().

Prolog "function call" is called "Verification (query) child target".

Not Prolog sentimental, other "small language" basically so.

For example, the function language Lisp, Erlang, and so on, the "function call" is called "Evaluation (evaluate)."

You have to talk about "sub-goals".

Theoretically speaking, the Prolog program is composed of a total target and several sub-goals, forming a "tree" logical relationship.

Goal is the total goal, that is, the root node of the tree, the sub-target is the tree's various layers of foliage.

In this example, the child target of the total goal is C (), and A (), B () is a child of C ().

In a () and B (), the child target is F (X).

This sample code of the watch, mainly fail and !.

Write (), Writef (), and other verbs that manage input and output are not child targets. Because, they do not participate in the verification, reasoning.

To correctly understand the relationship of child targets in predicate clauses, the comma "," is the key.

The meaning of the comma "," is the logical "with", and. Therefore, the clause body is actually a compound logic judgment:

For example, clause a ():-B (), C (), D (). Equivalent to: If a () then {  if (b () and C () and D ()) then A ()}

Here is not a mistake, the clause header and clause body is such a logical relationship.

Not only that, the semicolon ";" Represents a logical "or". For example, in this case, the predicate a (), 2 clauses can be combined to write this way:

A ():-write ("------a-------\ n"), F (x), Writef ("%\n", X), fail; Write ("A is sucsess.").

Just, with a semicolon "," rather than a side-by-side clause to express the logic or, will cause code difficult to read, difficult to understand and so on misunderstanding, very impractical.

So, in actual programming, it is almost impossible to see the use of semicolons ";" Of

Now go back to Geni's reasoning machine flow control this topic.

The "typical example" mentioned above is one of the methods of Geni's inference machine flow control.

Where the fail in predicate a () iterates through all f (X);

The!,fail in predicate B () obtains only the first F (X) and no longer backtracking;

predicate C ()! is also important. Without it, the program compiles with errors.

2, prolog the first deep search method to verify the sub-goal

The prolog itself is the inference machine which searches for the target in the way of first deep search.

Still say things in the "typical example" above.

Three predicates, such as a (), B (), C (), are the logical or, or, relationships of 2 clauses, respectively.

The clauses in their bodies make up the relationship between logic and, and.

According to the precedence rules of logical operations, first and after or. Therefore, the first deep search for the target verification method.

The rule in Geni's knowledge base is logically a "tree" relationship, and is fully consistent with the Prolog inference mechanism.

and Geni itself inference machine, it is according to its knowledge base structure, tailor-made deep search target verification machine.

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.