On the business at present widely used programming languages are imperative or functional programming language, the language has the very high similarity in some respects, such as python and ruby is common in many places, learn one, learning the other door can get twice the result with half the effort, many languages are so, today to introduce the language, however, Prolog is a logical programming language.

Prolog is an acronym for Programming in Logic. It is widely used in artificial intelligence, natural language and other research fields. It is easy to use prolog to solve Logic problems. First, let’s look at the syntax of prolog.

Prolog Basic syntax

There are three basic elements in prologs: facts, rules, and queries. Facts and rules are used to describe data, and queries are used to get answers to questions.

We can define facts like this:

human(lucy).
human(lili).
father(lucy,david).
sister(lucy,lili).
sister(lili,lucy).
Copy the code

This code indicates that Lucy and Lili are human and that they are sisters, and David is Lucy’s father, and continues to define the rule:

daughter(Father.A.B) :-
    father(A.Father), sister(A.B)
Copy the code

For variables A and B, if Father is the Father of A and A and B are sisters, then A and B are daughters of Father.

Note that a word in Prolog is a fixed value if it begins with a lowercase letter, and a variable if it begins with an uppercase letter.

Put these facts and rules in A file, open prolog on the command line, compile the file, and present A query, such as Daughter (David,A,_). Prolog will evaluate the possible values of A and print them to the console. The underscore at the end is A placeholder and will not be evaluated.

You can also use recursion to complete lists and math in Prolog. This part is very powerful, but I won’t cover it here, because once you have the basics above, you can use it to solve logic problems. Let’s solve the Zebra problem.

Einstein logic problem

People from five different countries with different jobs live in five houses on one street. Each house is a different color. Everyone has his or her own pet and likes to drink different drinks. According to the following tips, can you tell me which house people keep zebras and which house people like to drink mineral water?

  1. English people live in red houses
  2. The Spaniard has a dog
  3. The Japanese is a painter
  4. Italians love drinking tea
  5. The Norwegian lives in the first house on the left
  6. The green house is on the right of the White House
  7. The photographer raised a snail
  8. Diplomats live in yellow houses
  9. The man in the middle house likes to drink milk
  10. Coffee drinkers live in green houses
  11. The Norwegian lives next to the blue house
  12. The violinist likes to drink orange juice
  13. The fox keeper lived in a house adjacent to the doctor’s
  14. The horse keeper lived in a house adjacent to the diplomat’s

This problem is the key to the problem solving, in a clear way of properties that are relevant to each house (country, color, work, pets, beverage, number), the previous five tip contains five countries, then can use this a little out of a table, each row represents a country, each column represents an attribute of the house. Step by step, draw some inferences according to the prompts, fill the results in the table, and the answer will gradually become clear. The result of reasoning in this artificial way is shown in the figure below:

Although we know the key to the problem, it still takes many steps to get to the answer. It’s much easier to get to the answer using Prolog. You just define the facts and rules, then ask Prolog the question, and the logic engine will figure it out for you.

Here is the prolog code to solve this problem.

house(A[A._._._._]).
house(A[_.A._._._]).
house(A[_._.A._._]).
house(A[_._._.A._]).
house(A[_._._._.A]).

right(A.B[A.B._._._]).
right(A.B[_.A.B._._]).
right(A.B[_._.A.B._]).
right(A.B[_._._.A.B]).

middle(A[_._.A._._]).

first(A[A._._._._]).

neighbor(A.B[A.B._._._]).
neighbor(A.B[_.A.B._._]).
neighbor(A.B[_._.A.B._]).
neighbor(A.B[_._._.A.B]).
neighbor(A.B[B.A._._._]).
neighbor(A.B[_.B.A._._]).
neighbor(A.B[_._.B.A._]).
neighbor(A.B[_._._.B.A]).

attr(Country.Pet.Color.Drink.Work).

all_houses(Houses) :-
    house(attr(britsh,_,red,_._), Houses),
    house(attr(spain,dog,_._._), Houses),
    house(attr(japan,_._._,painter), Houses),
    house(attr(italy,_._,tea,_), Houses),
    house(attr(norway,_._._._), Houses),
    first(attr(norway,_._._._), Houses),
    right(attr(_._,white,_._), attr(_._,green,_._), Houses),
    house(attr(_,snail,_._,photographer), Houses),
    house(attr(_._,yellow,_,diplomat), Houses),
    middle(attr(_._._,milk,_), Houses),
    house(attr(_._,green,cafe,_), Houses),
    neighbor(attr(norway,_._._._), attr(_._,blue,_._), Houses),
    house(attr(_._._,orange,violinst), Houses),
    neighbor(attr(_,fox,_._._), attr(_._._._,doctor), Houses),
    neighbor(attr(_,horse,_._._), attr(_._._._,diplomat), Houses),

    house(attr(_,zebra,_._._), Houses),
    house(attr(_._._,water,_), Houses).
Copy the code

In the fact section, the house is viewed as a whole, which describes the house in 5 houses, the left and right relationship of the house, the location of the middle house, the location of the first house, the adjacent relationship between the houses, and what properties each house has.

The rules section contains a description of the prompts in the problem and a description of the final problem, defined to tell the logic engine that these conditions must be met when evaluating. The final query is all_houses(A), and the prolog logic engine will find an array of houses that satisfy the result. Note that each house is composed of its properties, so the final result is:

[attr(norway, fox, yellow, water, diplomat),  
attr(italy, horse, blue, tea, doctor),  
attr(britsh, snail, red, milk, photographer),  
attr(spain, dog, white, orange, violinst),  
attr(japan, zebra, green, cafe, painter)]
Copy the code

Original link: github.com/lcomplete/T…