Conceptual questions about complex terms in Prolog

I just started studying Prolog in college, and I have a concept question for which I cannot find any concrete answer:

I want to improve the "philosophy" of this language, so I want to understand very precisely what a complex term (or compound term) is. To date, I have read that a complex term is a functor, and it's simple. And I can create a knowledge base using complex terms such as this:

love(john, sarah).

Say that now I can enter any possible pair of lines as an argument to “love”, and it will be false if the pair of lines is not “john, sarah”. That's right, so the “question” about the functor and its arguments is that which is true or false, depending on whether I said it specifically in my knowledge base or if Prolog can draw a conclusion from the information that it uses in the rules, unification and etc ..

That's right, because here I understand that a complex term says that the relationship between n entities is true or false . I do not understand what it is:

vertical(line(point(X,Y),point(X,Z))).

I understand which point (X, Y). does. He says that any object is connected with any other person through a "point". I do not understand how the point (X, Y) can be an argument to a string! So far, the complex term has simply said whether entities are connected or not. I could understand that this is a "traditional function" that returns true or false if the entities are related. But how can this be an argument to the line? The “line” in theory has 2 arguments (entities), and he will say whether they are connected or not. Now are the values ​​of the arguments true or false?

I could understand that the dot (X, Y) creates the dot object. Thus, the string argument is a "point entity". But this is not what I read about complex terms so far, so I would like a technical definition to explain the nested cases.

( , , Prolog)

!

+4
3

, Prolog Prolog, . Prolog, , " () , ".

, . foo - . foo(A, B) - . foo(bar(X), bah(Y,Z)) - ( foo/2), bar/1 ( ) bah/2 ( ).

Head :- Body ':-'(Head, Body). Prolog ( ), Prolog , Prolog , :- . ':-'(A, B) - " " .

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

, (X, Y). . , "".

Prolog point(X, Y) - (point/2), ( ), , , , . point(X, Y) Prolog :

?- point(1, 2).

Prolog , point(1, 2) . , , :

?- foo(point(1, 2)).

foo/1 , foo(_) point(1, 2), " " , , , . , foo/1 foo(a). , foo/1, foo(point(1, 2)). , Prolog a ( ) point(1, 2Y) ( ) , . foo/1, :

foo(X) :-
    X = point(A,B),
    ...   % do some things involving A and B

foo(point(1,2)) , X point(1,2), point(1, 2) = point(A, B) A = 1 B = 2 .. point/2 - .

, foo/1:

foo(X) :-
    call(X),
    ...

, , foo(point(1, 2))., foo/1 point(1, 2) ( ), Prolog , point(1, 2).

, (X, Y) !

, . , . Prolog, , . line(A, B), a, B. , , point(X, Y), line(point(X1, Y1), point(X2, Y2)). , (X1, Y1), (X2, Y2).

, .

, " ". , , () . , line(point(X1, Y1), point(X2, Y2)), ( ) (X1, Y1) (X2, Y2).

, " ", true false, .

. " ", true false. . Prolog, , . , , , Prolog ( /), . . , line(point(X1, Y1), point(X2, Y2)), Prolog , line(_, _), . , .

? "" 2 (), , .

line(point(X,Y), point(X,Z)) - , (X, Y) (X, Z).

?

, . , , .

, "point (X, Y)" "point".

. , X Y.

, " ".

line/2 , , ( ). line(P1, P2), "" ( , [X, Y] , , , point(X, Y)).

, , , .

? , , , .

</" > - , - , . , , point(X, Y), , line(P1, P2), P1 P2 - . , line(point(X1, Y1), point(X2, Y2)). .

Prolog? point(X, Y). X Y? ? , . , point:

valid_point(P) :-
    P = point(X, Y),   % a Point looks like point(X, Y)
    number(X),
    number(Y).

Prolog , :

valid_point(point(X, Y)) :-
    number(X),
    number(Y).

So valid_point(point(X, Y)) , X Y - . Prolog, (point(3, 5.2) ( valid_point(point(3, 5.2))., (, "" ). Prolog, point(a, 3) ( valid_point(point(a, 3))., (, "" ).

. , line(P1, P2), P1 P2 , . . , , :

valid_line(Line) :-
    Line = line(P1, P2),
    P1 = point(X1, Y1),    % P1 is a valid point
    valid_point(P1),    
    P2 = point(X2, Y2),    % P2 is a valid point
    valid_point(P2),
    ( X1 =\= X2 ; Y1 =\= Y2 ).

, Prolog , :

valid_line(line(point(X1, Y1), point(X2, Y2))) :-
    valid_point(point(X1, Y1)),
    valid_point(point(X2, Y2),
    ( X1 =\= X2 ; Y1 =\= Y2 ).

, point(X1, Y1) point(X2, Y2) , , X1, X2 , Y1 Y2 .

. , . , vertical_line, , :

vertical_line(line(point(X1, Y1), point(X2, Y2)) :-
    valid_line(line(point(X1, Y1), point(X2, Y2)),
    X1 = X2.

, :

vertical_line(line(point(X, Y1), point(X, Y2))) :-
    valid_line(line(point(X, Y1), point(X, Y2)).

structre. valid_line/1, line/2 . , , , . , Prolog , . , point/2, , :

point(X, Y) :-
    number(X),
    number(Y).

:

?- point(1, 3).
true

?- point(a, 7).
false.

, line/2:

line(P1, P2) :-
    P1 = point(X1, Y1),
    P2 = point(X2, Y2),
    ( X1 =\= X2 ; Y1 =\= Y2 ).

( point/2) P1 = point(X1, Y1). , Prolog . , line(point(a, 3), point(c, d)), , , , (=\=)/2. P1 = point(X1, Y1) '='(P1, point(X1, Y1)) Prolog. , Prolog , , '=', point(X1, Y1) " " . point/2 :

line(P1, P2) :-
    P1 = point(X1, Y1),
    call(P1),
    P2 = point(X2, Y2),
    call(P2),
    ( X1 =\= X2 ; Y1 =\= Y2 ).

Prolog ( point/2, ). , , valid_... .

+2

, , n [...] "" , 2 (), , .

" ", , . Prolog "", "", , .

:

love(john, sarah).

( , ), , Prolog. . "": ( ) love/2 (/2 - , .. ). "", "" :

?- love(X, Y).
X = john,
Y = sarah.

"" , love/2 .

line . vertical/1:

vertical(line(point(X,Y),point(X,Z))).

, "" vertical/1, . ( " ". " - , X".) line/2 ; . vertical/1:

?- vertical(T).
T = line(point(_G921, _G922), point(_G921, _G925)).

line/2:

?- line(P1, P2).
ERROR: toplevel: Undefined procedure: line/2 (DWIM could not correct goal)

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

+2

. ,

vertical(line(point(X,Y),point(X,Z))).

(a line(P1,P2)), ascissa P1, P2.

, , , , ...

0
source

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


All Articles