Just released in the August TIOBE top 20, Prolog is an ancient artificial intelligence language!

TIOBE said: “And, even more astonishing, we see Prolog re-entering the top 20 after 15 years… Making an unexpected comeback.”

(Image courtesy of TIOBE Index for August 2021)

After more than a decade, this unique language has reappeared, ranking second only to Go! Don’t hurry up!

If you can’t learn, please follow my footsteps, today, we start from a fresh and refined example, appreciate this different “artificial intelligence language”.

Prolog

Like (mercury, Kathy). % Mercury likes Kathy. Like (Kathy, mercury). Lover (X, Y) :- like(X, Y), like(Y, X). % Ask Mercury Who he is in love with: lover(mercury, Who). The computer tells me Who = Kathy, that's Prolog!Copy the code

(This text is taken from a prominent position on the homepage of the Chinese forum for the ARTIFICIAL intelligence language on Prolog, an ancient website that still exists today.)

This is a thought-provoking poem! But you might find it hard to imagine that this is a “programming language.”

Looking back at the development of programming languages, there are several periods.

  • The first generation of programming language: machine language, write code with 0 and 1 (weld wudi classmates for us to show the lost for a long time god).
  • Second generation programming language: assembly language
  • Third generation programming language: high-level language, representative C, Java, C++, C#
    • 3.5GL: The later third generation, such as Python and Lisp1, is more abstract and has a hint of the fourth generation.
  • Fourth generation programming language: more abstract, not to deal with hardware related details, focus on problem description, problem solving, such as SQL, MATLAB

This is probably the end of your knowledge of programming languages. But we’ve had this for decades

  • Fifth generation programming language: artificial intelligence language, this generation of programming language expects the computer to be able to solve the problem automatically, based on certain limitations given by the problem, to the program to handle without the programmer to invest in the human development of the program.

When you say “artificial intelligence” today, you think of Python, but Python is not a fifth-generation ARTIFICIAL intelligence language. Prolog is one of the fifth generation programming languages that has been on the decline for more than a decade.

Today we’re not going to talk about why the fifth generation of programming languages is on the decline, but to learn about Prolog.

Prolog

The word “Prolog” comes from “PROgramming of LOGic,” which means LOGic PROgramming. Prolog doesn’t require you to write a program to run it, you just give the facts and the rules, and it automatically analyzes the logic. And then you can ask Prolog to do complex logical operations with queries.

SWI-Prolog

There are so many implementations of Prolog that Wikipedia even has an article comparing different Prolog implementations:

  • Wikipedia: Comparison of Prolog implementations

Here we use Swi-Prolog, a very well-developed open source implementation that is still under development and maintenance. On macOS, you can install SWI Prolog using BREW:

$ brew install swi-prolog
Copy the code

Debian Linux can also be easily installed using apt-get. Other systems can go to the official website download page to find the corresponding binary file or build from the source.

If you don’t want to or have trouble installing, There’s an online version for you.

Once the installation is complete, run swipl to enter a Python-like swi-Prolog REPL environment (what is the prompt for swi-prolog? -) :

$ swipl ? -Copy the code

In international practice, write Hello World:

? - write('Hello, World! '). Hello, World! % write Indicates the printed resulttrue. % return valueCopy the code

Notice that the Prolog statement ends with a.

To exit Swi-Prolog, press Control-d or enter Halt. (You can find out about “halt” by typing apropos(quit). Then use help(halt). See how to exit.)

Prolog basic syntax

Unlike the other variable-based programming languages we’re used to, Prolog is based on

  • Facts: the Facts
  • Rules of the Rules:
  • The Queries: Queries

These concepts are literally, as in

  • The facts:11 is not divisible by 2.
  • Rules:A number that is not divisible by 2 is odd.
  • Query:Is 11 an odd number?
    • Result (answer) :11 is odd.

In a Prolog, facts and rules are combined to form a Knowledge base, and we write them in a file named.pl.

Queries are written in the REPL and logically deduce an answer to a problem based on a known knowledge base.

FACTS

For example, suppose we know some facts (literally) that are:

  • Yomogi and Yume like each other;
  • Kaneishi also likes Yomogi.

In Prolog, these facts are expressed as:

like(yomogi, yume).
like(yume, yomogi).
like(kaneishi, yomogi).
Copy the code

We can write this to a./ssss.pl file, but not to the REPL.

In Prolog, words that start with a lowercase letter are constants, indicating that an object such as like, mercury, or Kathy does not need to be defined or assigned.

Note that liking this kind of thing is one-way. Just because A likes B doesn’t mean B likes A (kaneishi and Yomogi).

We call like a relationship, meaning that two or more objects are related to each other in some way. We can also define a fact that is related to only one object. These facts are called attributes:

male(yomogi).
female(yume).
female(kaneishi).
Copy the code

The rules

Now let’s make a rule:

  • If X likes Y and Y likes X, then X and Y are lovers.

The Prolog of a rule is head: -goals. When goals are true, the head is true.

lover(X, Y) :- like(X, Y), like(Y, X).
Copy the code

(This line of code is also written in./ssss.pl)

This line of code says that if like(X, Y) and like(Y, X) both hold, then lover(X, Y) holds. In Python, it is:

def lover(X, Y) :
    return like(X, Y) and like(Y, X)
Copy the code

Or and not in Prolog:

  • A, BSaid “AwithB”
  • A; BSaid “AorB”
  • \+ ASaid”nonA”

The query

The facts and rules above are definitions, that we tell Prolog something we already know. The query is the big deal, which is to let Prolog do the logic problems for us.

Use the swipl command to enter the Prolog environment, then consult(“path/to/xxx.pl”) to load the code written in the.pl file to read the known conditions:

$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.24.)
?- consult('ssss.pl').
true.
Copy the code

Alternatively, you can use [XXX]. Syntax to load xxx.pl.

Then, if we want to know if Yomogi likes Yume, we can ask Prolog:

like(yomogi, yume).
true.
Copy the code

True = like(yomogi, yume) = yomogi likes yume And:

? - like(yomogi, kaneishi).false.

?- like(yomogi, gauma).
false.
Copy the code

False means that Yomogi does not like Kaneishi, of course, pure Yomogi is not likely to like the stranger We have not defined Gauma.

All of the previous queries ask if a relationship is true, that is, to see if a “fact” exists. With the lover rule defined, Prolog can also:

? - lover(yomogi, yume).true.

?- lover(yume, yomogi).
true.

?- lover(yomogi, kaneishi).
false.

?- lover(kaneishi, yume).
false.
Copy the code

Using the known like relationship (the fact), combined with the lover definition (the rule), Prolog can deduce:

  • Yomogi and Yume are lovers. Of course, the reverse is also true: yume and Yomogi are lovers.
  • Because Yomogi doesn’t like Kaneishi, Yomogi and Kaneishi are not lovers!
  • Yume and Kaneishi do not have a relationship that either of them likes, so they are not lovers.

If that’s all it is, if it’s just right or wrong, then Prolog is weak. Variables can also be used in Prolog queries.

For example, we want to know Who does Peng Yomogi like? You can ask Prolog:

? - like(yomogi, Who). Who = yume.Copy the code

Here Who is a variable, and Prolog is going to solve for the variable in the query, and it’s going to get Who = yume, which means that Yomogi likes yume.

If (yomogi, Who) = true, then Who=? If (yomogi, Who) = true .

The words that start with a capital letter in Prolog are variables. (Note that Prolog capitalized variables, lowercase constants, and our usual C programming conventions are reversed.) If the case is wrong, the meaning is wrong, try querying:

? - like(yomogi, who).falseYomogi doesn't like who. I don't know whoCopy the code

If we ask “does Yume like Yomogi?” and write yume in capital LETTERS, it becomes a query “who likes yomogi?” :

? - like(Yume, yomogi). % Yume is a variable, not Yume this person Yume = Yume; % Press TAB or press; The key displays the next result Yume = Kaneishi.Copy the code

So Yume is equal to Who, which is just a variable, people Who like Yomogi, not the Yume that Nan defined earlier. Based on the fact that we defined at the beginning, both Yume and Kaneishi like Yomogi, so yume (Who) could be Either Yume or Kaneishi.

For such queries with multiple results. By default, swi-prolog only displays one result at a time and then waits until we need to press it; (for “or,” remember) and then the next result. (You can also press TAB here)

One more thing to add here is that there is a useful predicate in Prolog, listing, to list all the facts about a relationship, or to see the definition of a rule:

? - listing(like). like(yomogi, yume). like(yume, yomogi). like(kaneishi, yomogi). ? - listing(lover). lover(X, Y) :- like(X, Y), like(Y, X).Copy the code

E.g. Will Socrates die?

Using this knowledge, you can solve many logical problems. For example, given:

  • Socrates is a man
  • People will die

So we can withdraw from the conclusion that Socrates is going to die.

Use Prolog to solve this problem:

Person (Socrates).% fact mortal(X) : -person (X).% rule -- % query? Mortal (Socrates)true.? Mortal (X). % X = SocratesCopy the code

If you feel that a complete program must not only include logical operations, but must also have input and output, then, in combination with write in Hello World, we can:

person(socrates).
person(plato).
person(aristotle).

mortal(X) :- person(X).

mortal_report :- 
    write('Known mortals are:'Nl, mortal(X), write(X), NL. % NL is newlineCopy the code

Then in the interpreter call:

? - mortal_report. socrates plato aristotleCopy the code

So you get some mortal beings with limited lives.

You can write many more interesting examples using this “logical programming”. If you find this example beyond comprehension, I recommend restudying basic Mathematical Logic (the first chapter of many discrete mathematics books).


At this point, we’ve really only seen the simplest uses of Prolog, but we haven’t touched the really powerful things in Prolog, such as recursion, which will be studied in a more in-depth article — but that’s the end of the article.

reference

[1] Blackburn, Patrick and Bos, Johan and Striegnitz, Kristina. Learn Prolog Now! . College Publications. 2006

[2] Ruan Yifeng. Introduction to Prolog language. Ruan Yifeng’s weblog. 2019

[3] SWI Prolog. Getting Started.

[4] Prolog Foundation <1>. Drap. Me, 2015

[5] Author unknown. Introduction to Prolog. Listening to bamboo in fishing. 2004

(This last post was from when I was learning Prolog nearly 10 years ago, and it’s very easy to read. But now there are only many copies of this article on the Internet, as well as its translation of the Original English, it is difficult to find this precious Chinese translation, so far I just found it may have come from a website called Fishing Listen to bamboo Xuan, but this website is down, the backups in archive.org are all 3xx errors, I will archaeological again at my free time)


  1. Lisp: There are sources that say Lisp is a fifth generation programming language, but in my own Lisp learning, Lisp is not a definition of fifth generation, but a fifth generation language can be implemented in Lisp. ↩