The role of logic programming, and is it worth planning to study it in 2021

I’ll start, perhaps, by introducing the reader of this article, since nothing attracts attention to the text more than empathy for the main character, especially since you are now playing his role. Probably, having heard or read once the phrase "logic programming" and filled with interest, you, as a present or future programmer, went to Google. The first link, of course, leads to Wikipedia - read the definition:





Logic programming is a programming paradigm based on automatic theorem proving, as well as a section of discrete mathematics that studies the principles of logical inference of information based on given facts and inference rules. Logic programming is based on the theory and apparatus of mathematical logic using the mathematical principles of resolutions.





"Hmm" - you think, and that says it all. Complicated! And here our brave hero should have followed the second link, but I will allow myself to make a small insert, describing the main character: You , according to my idea, are a beginner in programming , and even if not, you are definitely not familiar with its logical appearance ... If the reader is already somewhat (or even a lot) tempted with knowledge in this area, then I recommend reading the article What is logical programming and why we need it , since you have an interest and curiosity about the topic, and leave the study of the material below to less experienced colleagues.





So it's time for the second link. What will it be? Article on Habré? Maybe an article on another resource? After reading the first couple of paragraphs on different sites, you, most likely, will not understand much, since, firstly, the material is usually aimed at a knowledgeable reader, and secondly, there is not so much good and understandable information on the topic on the Russian-speaking Internet, in- thirdly, for some reason there is always a question of a "prologue" (we are talking about the Prolog programming language, of course), but the language itself seems to be used by very few people (honorable 35th place in the TIOBE rating ). However, our hero does not lose motivation and, after a while, stumbles upon this very article, wanting to still understand:





  • What is Logic Programming





  • (, ?)













, , .





, , Pascal ( - ). Python, C/C++/C#, Java. , , - :






1
2
 
  3

  4

      
      



, , , , () . , . , , . , , , , . , , , , , … ?





:





.





- .





, .





. ? ! Prolog-e, . , Prolog () . , "" , .





, , ( ) " ":





% ,       - 
human('Plato'). %  - 
human('Socrates'). %  -  
human('Aristotle'). % ,    
% ... . 

mortal(X) :- human(X). %  : "X ,  X - "
      
      



, , :





?- mortal('Socrates').
true.
      
      



"true", , , , - .





, , . . . , . , , - . "human('Socrates')." ? "human" "mortal" - . , , , .





. ( ) , ( (true) (false)). - .





%     Prolog  ,      
like('Petya', 'Milk'). %  ,    
good('Kesha'). %  
number_of_sides('Triangle', 3). %    

like('Misha', X). %   ,     X  
      
      



. "mortal(X) :- human(X).". - , (/) . :





a(X,Y,Z) :- b(X), c(Y,Z), d().
      
      



a , b, c d. : " b X c X, Y d , a X, Y, Z ".





, , . , . , , -:





%     ,         
eat(father, cheese).
eat(father, apple).
eat(father, melon).
eat(mother, meat).
eat(sister, meat).
eat('Petya', cheese).
eat(brother, orange).
      
      



( ):





?- eat(father, apple). %    
true.

?- eat(father, meat).  %    
false.

?- eat(sister, X). %   
X = meat.

?- eat(X, cheese). %   
X = father ;
X = 'Petya'.

?- eat(X, Y). %   
X = father,
Y = cheese ;
X = father,
Y = apple ;
X = father,
Y = melon ;
X = mother,
Y = meat ;
X = sister,
Y = meat ;
X = 'Petya',
Y = cheese ;
X = brother,
Y = orange.
      
      



, . , ( ) . , , , .





( , ) , . , , , . , :





d(X,X,1) :- !. %  X  X = 1
d(T,X,0) :- atomic(T). %   = 0
d(U+V,X,DU+DV) :- d(U,X,DU), d(V,X,DV). %   =  
d(U-V,X,DU-DV) :- d(U,X,DU), d(V,X,DV). 
d(-T,X,-R) :- d(T,X,R).
d(C*U,X,C*W) :- atomic(C), C\=X, !, d(U,X,W). %  ,    =     
d(U*V,X,Vd*U+Ud*V) :- d(U,X,Ud), d(V,X,Vd). %  
d(U/V,X,(Ud*V-Vd*U)/(V*V)) :- d(U,X,Ud), d(V,X,Vd). 
      
      



:





?- d((x-1)/(x+1),x,R).   
R =  ((1-0)*(x+1)-(1+0)*(x-1))/((x+1)*(x+1)).
      
      



, . , , Prolog- . , , . . 8- . : - , . , , - - , , , . " ", , ( , , ).





, , , .





speciality(X,tech_translator) :- studied_languages(X), studied_technical(X). % X -  ,      
speciality(X,programmer) :- studied(X,mathematics), studied(X, compscience). % X - ,      
speciality(X,lit_translator) :- studied_languages(X), studied(X,literature). % X -  ,   
studied_technical(X) :- studied(X,mathematics). % X   ,   
studied_technical(X) :- studied(X,compscience). % ...  
studied_languages(X) :- studied(X,english). % X  ,   
studied_languages(X) :- studied(X,german). % ... 

studied(petya,mathematics). %   
studied(petya,compscience). % ... 
studied(petya,english). % ... 
studied(vasya,german). %   
studied(vasya,literature). %... 
      
      



, , - :





?- speciality(X,tech_translator).
X = petya ;
X = petya ;
false.
      
      



… , … - , . , X, :





, , . , , - , false. , , -, . , , - . "" , :





, ( ), . , ("" ). ( ). , 1, , . ., (). ? , . :





% : w -  , b - , e -  
is_ball(w). % w - 
is_ball(b). % b - 

near([X,e|T],[e,X|T]) :- is_ball(X). %      ,   
near([e,X|T],[X,e|T]) :- is_ball(X).
jump([X,Y,e|T],[e,Y,X|T]) :- is_ball(X), is_ball(Y). %       ,   
jump([e,Y,X|T],[X,Y,e|T]) :- is_ball(X), is_ball(Y).

%  .      ,       
move(L1,L2) :- near(L1,L2). 
move(L1,L2) :- jump(L1,L2).
move([X|T1],[X|T2]) :- move(T1,T2).

%    .    X     Y 
% Y     ,  Y -  
prolong([X|T],[Y,X|T]) :- move(X,Y), not(member(Y,[X|T])).

%   -  ,  -  ,  - ,    
bdth([[X|T]|_],X,R) :- reverse([X|T], R). %     ,        (   ,    )
bdth([P|QI],Y,R) :- bagof(Z,prolong(P,Z),T), append(QI,T,QO), !, bdth(QO,Y,R). %          ,   
bdth([_|T],Y,R) :- bdth(T,Y,R). %       ,   bagof  false,     
bsearch(X,Y,R) :- bdth([[X]],Y,R). %     bdth

% ,             
solve :- bsearch([w,w,w,e,b,b,b],[b,b,b,e,w,w,w],P), write(P), nl, length(P, Len), write(Len), nl.
      
      



solve, , , - . ( ) , , . - , ( ()) . . , , ( ) . , , .





. , ? : . , , . : , 1, 2, 4 . . :





%   -  ,  -  ,  - ,    
dpth_id([X|T],X,R,0) :- reverse([X|T], R). %   
dpth_id(P,Y,R,N) :- N > 0, prolong(P,P1), N1 is N - 1, dpth_id(P1,Y,R,N1). %   >0,       
generator(1). %    1
generator(N) :- generator(M), N is M + 1. %   2, 3, 4  . .
isearch(X,Y,R) :- generator(D), dpth_id([X],Y,R,D). %  ,         .
      
      



-, , , ( reverse, ). , ( ). -, "" , , ( , ), . -, - , . , , : 16.





, , , . , . , "". , , ? :





near([w,e|T],[e,w|T]).
near([e,b|T],[b,e|T]).
jump([w,X,e|T],[e,X,w|T]) :- is_ball(X).
jump([e,X,b|T],[b,X,e|T]) :- is_ball(X).
      
      



, . , ( ), -, , -, , -, . . , , . , , . , .





, .





  • : - . , , , , , , . , , . , . , . , . , " ", " ?" , , " ". - , , "" ( , , ). , . . .





  • : , - . , , ( ). . , , ( , , ).





  • -: , , . , . , . . , , , IT . - , -, , - . , , . .





, , ( , ). , ( ) . Prolog-. , , ( ), , ( . . ), : , , .





2021-

, :





  • - , , , , - . , ( , ) , , .





  • - , . -, - ( , , ). -, , , . -, , , . -, , , .





And here it remains only to wish a productive 2021 !








All Articles