In this article:
We will learn what logical programming ( LP ) is, and its areas of application
Install the most popular LP language - Prolog
Learn to write simple programs in Prolog
Learning Lists in Prolog
Let's take a look at the advantages and disadvantages of Prolog.
This article will be helpful for those who:
Is interested in unusual approaches and broadens his horizons
Begins to study Prolog (for example, at the institute)
, . , Prolog, . . .
. — !
. :
( ). . , . : C/C++, Java, C#, Python, Golang, , . ,
(). , . : Haskell, F#, . . . — . , . .
— (). . : Prolog , Mercury. , . , , , " ", " ", " , , , ", " - , ".
. , Prolog (PROgramming in LOGic). . . , - . , .
Prolog
() : SWI Prolog, Visual Prolog, GNU Prolog. SWI Prolog.
Arch Linux:
sudo pacman -S swi-prolog
Ubuntu:
sudo apt install swi-prolog
Prolog . SWI Prolog. swi-prolog, swipl:
[user@Raft ~]$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.2.3)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?-
! !
Windows.
. . . 64 . . , . .
Prolog. , ,
, :
(, )
(, , , )
(, , )
"predicate" " ".
: , , , , , , , , , , , , , , .
, . , . , : , , . . . . .. ? .
! . :
, simple.pl (.pl — )
SWI Prolog
simple.pl:
study(mark, book).
. :
[user@Raft referat]$ swipl simple.pl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.2.3)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?-
Windows notepad++ . SWI-Prolog consult.
? (, simple.pl) . : , SWI Prolog . . ( , "?- ". , , )
" ?" :
?- study(mark, book).
true.
?-
" study(mark, study) ?", "true." . , " ?"
?- study(mark, book).
true.
?- study(mark, docs).
false.
?-
"false.". , .
. , .
% . 11 .
% ".", , .
% "%".
% .
/*
*/
% . 11
study(mark, book). %
study(mark, studentbook). %
study(mark, docs). %
see(masha, mouse). %
see(masha, book). %
see(masha, notebook). %
see(masha, mark). %
study(misha, math). %
study(misha, lp). %
study(misha, docs). %
study(misha, studentbook). %
. (, ""). :
. . . : 1,36, 0, -1, 123.4, 0.23E-5. — : a, abc, neOdinSimvol, sTROKa. , , . : ' , . Eto kirilicca'.
. : X, Y, Z, Peremennaya, Var.
( ). , study(misha, lp).
. : [X1], [Head|Tail]. .
. .
study(mark, book). — . . , . , .
" " . : ?- study(mark, book). , true, , false. , , .
. , mark. "study(mark, X)." "Enter",
?- study(mark, X). X = book .
, ";".
?- study(mark, X). X = book ; X = studentbook ; X = docs.
, .
?- study(Who, docs). Who = mark ; Who = misha.
, !
?- study(Who, Object).
Who = mark,
Object = book ;
Who = mark,
Object = studentbook ;
Who = mark,
Object = docs ;
Who = misha,
Object = math ;
Who = misha,
Object = lp ;
Who = misha,
Object = docs ;
Who = misha,
Object = studentbook.
Who Object, study(Who, Object) .
. old.pl
%
older(sasha, lesha). %
older(misha, sasha). %
older(misha, dasha). %
older(masha, misha). %
%
older(X,Y) :- older(X, Z), older(Z,Y).
% X Y, X Z Z Y
% X, Y, Z - . ,
% .
% : ,
older(X,Y) :- older(X, Z), older(Z,Y) — . . , , older(X,Z) older(Z,Y) . , "X Y, X Z Z Y"
"," "". : "0 < X, X < 5". X 5 0.
";" "". "X < 0; X > 5". X 0 5.
"not(- )" "". "not(X==5)". X 5.
, .
?- older(masha, X). X = misha.
. . .. " ?". , , . . older(X,Y) :- older(X, Z), older(Z,Y).
.
?- older(masha, X).
X = misha ;
X = sasha ;
X = dasha ;
X = lesha ;
ERROR: Stack limit (1.0Gb) exceeded
ERROR: Stack sizes: local: 1.0Gb, global: 21Kb, trail: 1Kb
ERROR: Stack depth: 12,200,525, last-call: 0%, Choice points: 6
ERROR: Probable infinite recursion (cycle):
ERROR: [12,200,525] user:older(lesha, _5658)
ERROR: [12,200,524] user:older(lesha, _5678)
?-
. ? ! ! , ? , . — older(masha,X) , — . ?
. , , , . older(X, Y) older(X,Z), older ...
- . , "older(X, Z)" ? , older(X, Y) (, ).
, older(Z, Y), older(X, Y) —
, , . - , , .
, , — .
% older(sasha, lesha, fact). % older(misha, sasha, fact). % older(misha, dasha, fact). % older(masha, misha, fact). % % older(X,Y, rule) :- older(X, Z, fact), older(Z,Y, _). % X Y, X Z Z Y % X, Y, Z - . , % . % : ,
"_" - . , , . , . .
.
?- older(masha, X, _).
X = misha ;
X = sasha ;
X = dasha ;
X = lesha ;
false.
.
, : , ? ? study(mark, math) " "? " "?. . , "", "", "". -. , . .
N.
factorial(1, 1).
factorial(N, F):-
N1 is N-1,
factorial(N1, F1),
F is F1*N.
% , , C/C++.
% .
"is" , . N1 N-1. . "is" . , "is" "=".
. Enter, .
?- factorial(1,F).
F = 1 .
?- factorial(2,F).
F = 2 .
?- factorial(3,F).
F = 6 .
?- factorial(4,F).
F = 24 .
?- factorial(5,F).
F = 120 .
?- factorial(10,F).
F = 3628800 .
, , N 0. .
factorial(1, 1).
factorial(N, F):-
N >= 0,
N1 is N-1,
factorial(N1, F1),
F is F1*N.
:
female(X), male(X) parent(X,Y).
N .
( ) , .
Prolog
— . . — , . (, ) . Prolog , .. . .
C/C++, Python . — ; , , — . - .
: [],[a], [abc, bc], [' 1', ' 2', 1234], [X], [Head|Tail].
[Head|Tail]. , , , , . , "|".
[X1,X2,X3|Tail]. X1, X2, X3, Tail.
, . , , , , , . , , N G. , Elem List. . , - . ( ).
, .
% element(, )
element([Head|Tail], Element) :- Element = Head; element(Tail, Element).
?- element([1,2,3,4,5,6, 'abc', 'prolog'], Elem).
Elem = 1 ;
Elem = 2 ;
Elem = 3 ;
Elem = 4 ;
Elem = 5 ;
Elem = 6 ;
Elem = abc ;
Elem = prolog ;
false.
element([Head|Tail],Element) , Element Head ( ) element(Tail, Element) . - . ( : ? ?) , , Element [Head|Tail]. , .
. . .
% list_length(, )
list_length([], 0).
list_length([H|T], L) :- list_length(T, L1), L is L1+1.
?- list_length([123446,232,2332,23], L).
L = 4.
?- list_length([123446,232,2332,23,sdfds,sdfsf,sdfa,asd], L).
L = 8.
?- list_length([], L).
L = 0.
?- list_length([1], L).
L = 1.
?- list_length([1,9,8,7,6,5,4,3,2], L).
L = 9.
, H. , _, singleton .
SWI Prolog length. list_length. , . , .
, . , , .
, , . , 1, 2, 3 4 [1,2,3,4]. list_member.
mymember(Elem, [Elem|_]).
mymember(Elem, [_|Tail]) :- mymember(Elem, Tail).
, , . .
Prolog
, ( ) . .
, :
. ,
.
, . :
. , — , , .
.
.
.
.
.
.
.
.
.
.
, , .
.
.
.
? ?
: 6 .
! ?
:
-
(, Python)
. , O(n!). , , , "!". . , .
, , Python, C/C++, C#, Java, . , , , .
, . . . , — , , , .
?
, "" (, ). , .
( : . , . . , . . — . ?).
, . , , . , .
, !