preventing backtracking without cut

188 views Asked by At

Fairly new to prolog and trying to solve some exercises. Given a knowledge base of components

component(ElementX,ElementY,Qty).
ElementX uses ElementY in it's structure in quantity Qty.

component(car, door, 4).
component(car, wheel, 4).
component(wheel, tire, 1).

I want to find every ElementX that isn't an ElementY at the same time.

This is the expected result

?-final_product(X). 
X = car; 
false. 

Which leads me to believe I can't use a cut since I get this

?-final_product(X). 
X = car.

and if I don't prevent backtracking all I get is

?-final_product(X). 
X = car; 
X = car; 
false. 

How exactly would I achieve the expected result?

EDIT:

final_product(X):-
  setof(X, (component(X, _, _), \+component(_, X, _)), Results),
  member(X, Results).
0

There are 0 answers