📜 ⬆️ ⬇️

A little about Prolog

The language of the prologue unfairly has a rather narrow fame; meanwhile, it is a rather interesting language, which is undoubtedly worth it to get to know it.

To be honest, I am too lazy to describe the syntax and features of the prologue, for whom it is interesting, you can easily find a sufficient amount of material on the Internet, since the language is quite academic. Let me just say what he interested me. The fact is that the prologue is essentially the only language that offers a qualitatively different approach to programming than the well-known imperative, OOP (which, in fact, is also imperative, but aims at structuring and modularity), functional. You can call this approach declarative-logical.
Without pretending to the accuracy of terminology, this approach can be defined as one in which the program is a description of the task condition itself by various programming language constructs. The role of PL in this case is to understand this description, and to draw from it some conclusion, which will prove to be nothing more than the correct solution of the problem.
We illustrate what is meant by this. Take the following task.


')
The condition of the problem: Socrates - man. All men are mortal.
Find: Is Socrates Mortal?

We write the condition in terms of the prologue language (comments begin with the% sign):
% -
human(sokrat).
% -
human(platon).

% ,
mortal(Someone) :- human(Someone).


* This source code was highlighted with Source Code Highlighter .

now ask the prologue of the system whether Socrates is mortal:
?- mortal(sokrat).

Yes %

?- mortal(stranger).

No % , stranger ,

% :
?- mortal(Who).

Who = platon ;

Who = sokrat


* This source code was highlighted with Source Code Highlighter .

It is not surprising that this prologue became an indispensable tool in the study of artificial intelligence problems, namely, return-search tasks, symbolic computations, expert systems, natural language analysis, and many others.

In fact, such flexibility of the prologue is built only on two concepts embedded in the language - unification (pattern matching) and enumeration with return.

Actually, I want to give for example a solution on the prologue of the task of placing 8 queens on a chessboard, so that they do not beat each other, based on the information given in the book by Bratko I. Programming in the PROLOGUE language for artificial intelligence (by the way, a very good book, I recommend) .
% 8 X/Y
% ,
% Y -
getSolution(S):-
S=[1/_,2/_,3/_,4/_,5/_,6/_,7/_,8/_],
solution(S).

% 0x0
solution([]).

% NxN , - (N-1)*(N-1),
% -.
solution([X/Y | Others]) :-
solution(Others),
member(Y, [1, 2, 3, 4, 5, 6, 7, 8]),
nokill(X/Y, Others).

% ,
%
nokill(_, []).

%
%
nokill(X/Y, [X1/Y1 | Others]) :-
Y =\= Y1, %
Y1-Y =\= X1-X, %
Y1-Y =\= X-X1,
nokill(X/Y, Others).


* This source code was highlighted with Source Code Highlighter .

To obtain the necessary coordinates of the queens, we ask the solution system based on the description presented from the prologue.
1 ?- getSolution(P).

P = [1/4, 2/2, 3/7, 4/3, 5/6, 6/8, 7/5, 8/1] ;

P = [1/5, 2/2, 3/4, 4/7, 5/3, 6/8, 7/6, 8/1] ;

P = [1/3, 2/5, 3/2, 4/8, 5/6, 6/4, 7/7, 8/1] ;

P = [1/3, 2/6, 3/4, 4/2, 5/8, 6/5, 7/7, 8/1] ;

P = [1/5, 2/7, 3/1, 4/3, 5/8, 6/6, 7/4, 8/2] ;
...


* This source code was highlighted with Source Code Highlighter .


By the way, what will it look like in your favorite programming language? =)

By the way, here you can see the solution of the same problem on the ref , another interesting functional programming language that ideologically echoes the prologue, by the way, the creator of which is our compatriot Valentin Turchin ( wiki ).

Source: https://habr.com/ru/post/47416/


All Articles