Prolog is short for programming in logic, which is a language that uses logic for programming and is widely used in the AI field.
Before learning Prolog, we need an interpreter. I chose SWI-Prolog, which can be downloaded from http://www.swi-prolog.org. This interpreter has powerful functions and provides interfaces for Java and C/C ++. The other is strawberryprolog and parser.
Several Basic concepts of PROLOG:
Terms: This actually represents data, but this data is relatively broad. It includes variables, constants, and composite terms.
Facts: Indicates facts. For example, if a is the parent of B, this is a fact, which can be expressed as parent (a, B). Here, A and B are terms. the basic form of fact is P (T1 ,...., tn ).
Now we are no longer involved in other concepts. We try to use the above two concepts to do something.
Go to SWI, and we can use the consult in the Command menu to load external. pl file (SWI Prolog format), or use the new command to create a file. in the file editing window, enter the following statement:
MAN (Jack ).
MAN (Tom ).
WOMAN (Mary ).
WOMAN (Helen ).
The "." Here represents what we commonly use Program Note! This is an important indicator for distinguishing facts and terms. Sometimes we can see such a statement man (FOO (gg )). foo (gg) is actually terms, while man (FOO (gg )). facts. exit the edit window and enter man (Jack) in the command line ). press enter to see "yes", indicating that there is a man Jack in the database. well, we have already taken the first step. You can enter your own name, but you can only get no results. Please try adding your own name, don't make a mistake in gender !.
Next, we need to improve it a little. We modified the above file:
MAN (Jack ).
MAN (Tom ).
WOMAN (Mary ).
WOMAN (Helen ).
Parent (Jack, Mary). % Jack is Mary's parent
Parent (Helen, Mary). % Helen is Mary's parent
In the command line, we write: parent (C, Mary ). this C is a parameter, indicating that we do not know who Mary's parents are. We want to get this answer. note: in Prolog, the first letter of the parameter is capitalized by default.
After you press enter, we can see the following results:
C = Jack; we just need to press ";" (redo command) and then c = Helen will appear. What do you have to think about? Can you use this to simulate a DB?
In the next article, we will further improve and learn the rules in Prolog. With this, we can write very complex Logic Programs. (I am new to Prolog. If there is something wrong, I hope to enlighten me)
Y