Prolog: redundant results in sentences related to anonymous variables

Consider the following Prolog program.

a(X) :- b(_), c(X).

b(1).
b(2).
b(3).

c(1).

Request run:

a(X).

in SWI-Prolog, we get three results: all X = 1.

Given that we do not care about the anonymous variable, what prevents SWI-Prolog from returning a single result? Why is this optimization not performed?

thank

+4
source share
1 answer

Good for Prolog, underscores are just an anonymous variable. Therefore, the predicate is a/1equivalent to:

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

b(Y), Y , , . , Y X, b(Y) X.

Prolog, , , :

  • b/1 /. , :

    b(a) :-
        print(a).
    b(a) :-
        print(b).
    

    a b .

  • b/1 , ,... . , , ,

  • b/1 asserta/1, assertz/1 .. . , , c/1, c/1 .

  • Prolog , , .

  • , b/1 c/1.

b/1 once/1 -. :

a(X) :-
    once(b(_)),
    c(X).
+4

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


All Articles