Search code examples
prologprolog-toplevelprolog-setof

How do you get only one output from a swi-prolog query?


How do I get only one output from a SWI-Prolog query? I have tried using cut (!) but it does not seem to work.

For example: I already filled my knowledge base up with statements and I wanted to find any one name who is both female and is the mother of someone.

I have already tried:

mother(X,Y), female(X).

...but that gives me all of the X-__ and Y-__

I have also tried:

mother(X,Y), female(X), !.

... but that still gives me both the X-__ and Y__

I only want to find the X. Does anyone have any tips for me to somehow only get one?


Solution

  • ?- setof(t, Y^ ( mother(X, Y), female(Y) ), _).
    

    which will remove duplicates (redundant answers/solutions), too. Or using library(lambda):

    ?- X+\ ( mother(X, Y), female(Y) ).
    

    which does not remove redundant answers.