Prolog Learning (1)

Source: Internet
Author: User

To write a simple expert system, we plan to use the PROLOG Language in a short time.

Reference: tyros73.pdf (Tutorial document on the official website. Many of the following content will be referenced since then. For short, tyros PS: it seems that it is not much simpler -_-! ), AI tutorial, two slides on Baidu Library

IDE: Visual Prolog (vip ps: Taobao ...)

1. Install and run VIP

Download the Personal Edition from the http://www.visual-prolog.com/vip6/product/ for free permanently (upgrade commercial edition-paid edition is always prompted ).

All the way to next. After successful registration, find help-> enter license and click Register Personal Edition To Go To The registration page on the official website. Register with your personal email address. After the registration is successful, the system will send the license to the mailbox, using this license

Activate VP (activation prompt will pop up when debug is not activated. It will be activated if no charge is charged anyway ).

2. Hello World

2.1 logical source

Tyros boast of the wisdom of the ancient Greek, and how such a society creates the importance of logic. However, I have to admit that the western society is inseparable from the ancient Greek culture.

It makes me a modern man feel overwhelmed. Next

"Our Constitution does not copy the laws of neighboring states;
We are rather a model to others than imitators ourselves. Its
Administration favors the specified instead of the few; this is why it
Is called a democracy. If we look to the laws, they a washord equal
Justice to all in their private di ff erences; if no social standing,
Advancement in public life falls to reputation for capacity, class
Considerations not being allowed to interfere with merit; nor again
Does poverty bar the way: if a man is able to serve the state, he
Is not hindered by the obscurity of his condition from proposing
Laws, and political actions ."

What would the world look like if the Republic of ancient Rome did not perish? Far away... Let's get down to the truth.

2.2 first GUI Applet

Click new project (you will find it immediately), the project name is proj1, and the project kind remains unchanged, that is, Gui. Select the path (base Directory) below and click OK.

Then you can run the test to check whether a healthy project has been created. Click build-> execute (Note: unlike other general ides, do not click Run in debug here; tyros writes build-> build, but it does not

The worker runs successfully.

After excute, a window in the name of proj1 will appear. It is similar to the window created in MFC and QT. Although it is small, it can be found on the toolbar of basic new help or similar.

However, all of these functions are in form and are not actually relevant. We will implement these functions step by step.

First, let's talk about the architecture. A window (that is, the "Sparrow" just popped up) can contain N forms, and a new one will produce a form.

First, implement the new form function. File-> New in new package, IDE automatically creates a prototype form. You can change the size of this form in the graphic editing window and parameter table, and name this form

Query. Return to the project table, find taskmenu, expand, and click & New \ tf7 in & file to remove the check mark before disabled. Close the editing window. Go to the project Editor, find taskwindow. Win, double-click it, and choose menus> taskmenu> {id_file_new-> onfilenew}. The code editor is displayed. Find

Clses % statement

Onfilenew (_ source, _ menutag). % The statement must end with "." (equivalent to ";" in C ";")

 

Change

Clses

Onfilenew (W, _ menutag):-_ x = query: Display (W). %: = indicates definition, and: Indicates meaning

Exit the code editor and save it. Build-> execute to view the running result and test whether the new function is easy to use (the form named query is popped out ).

 

2.5 proceed with logic: Aristotle's syllogistic)

The small sample above is equivalent to "Hello World" to stimulate the interests of the coders. Let's start with the question below.

For Aristotle, a proposition includes subject and predict, and is assisted by quanti aggreger. Preface is the preposition of a topic ).

Quantifiers are like descriptive words such as "every time", "no", and "some. The three-phase structure consists of two preconditions (premise: Major premise P and Minor premise S) and a conclusion M.

Every three-Field Theory forms a tuple, and many tuples are combined into a tone (mood or modu ).

Keys = All, keys = some

Rule inference from known facts

:-If rule is divided into rule header and rule body

Must use "." As the end, written in Clause

 

Problem

The variable name must start with uppercase letters or underscores, and has no value assignment statement. It can be obtained by matching with facts and constants. The variable is free before being assigned a value, and bound after being assigned a value. The variable is automatically returned to free after the related questions are answered.

Anonymous variables: variables that do not require "value assignment" at all (understanding)

Predicate

 

Domain Name (domins)

Once a parameter is declared as a specific domain, it cannot be mixed with other domain names, or even the same domain name. However, the custom domain can match the preset domain.

If a standard domain is used, the domain segment is not required.

Predicate)

Create a console Program

After creating an es_test, I haven't seen the legendary main after searching for a long time. pro, and later I realized that it would only appear after build-> build, or open  ̄ □ ̄ directly in file-> open |

implement main    open coreconstants    className = "main".    classVersion = "".clauses    classInfo(className, classVersion).clauses    run():-        console::init(),        succeed(). % place your own code hereend implement maingoal    mainExe::run(main::run).

Replace write

implement mainclass predicatesfact:(integer N, integer Res) procedure (i,o).clausesclassinfo("facfun", "1.0").fact(N, 1) :- N<1, !.fact(N, N*F) :- fact(N-1, F).run():- console::init(),fact(stdio::read(), F), stdio::write(F), stdio::nl.end implement maingoalmainExe::run(main::run).

Build-> run in window

Enter a number at will to view the running result

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.