0
votes

so I have a fact weather(jan, 17, cold, wind, snow). and rule

match(W,D,T) :-
      weather(W,D,T,_,_)
  ;   weather(W,D,_,T,_)
  ;   weather(W,D,_,_,T).

When I type match(jan, 17, wind). Prolog returns true; false. I want it to only return true, but it's returning false as well, how can I fix this?

1
Your definition of weather/5 has some built-in ambiguity since the 3rd, 4th, and 5th arguments can interchangeably mean anything. Therefore, if you try to write a suitably general predicate like match/3, it's going to have choice points. When you see true as a prompt, that means Prolog succeeded but there was a choice point it needs to go back to check for another possible solution (since, if it were in the database, weather(jan, 17, wind, snow, cold) would also be a possible valid fact that would match). Prolog will eventually give false when it finds no more solutions. Normal.lurker
You could use cuts, as @Zebollo suggested, which would prune the choice point, but then you'd lose the generality of the solution. It would not yield multiple results for queries that legitimately have multiple solutions.lurker

1 Answers

-1
votes

I discourage the use of the ";" (OR) operator (unreadable code). This is what you should have written:

match(W,D,T) :-
    weather(W,D,T,_,_),
    !.
match(W,D,T) :-
    weather(W,D,_,T,_),
    !.
match(W,D,T) :-
    weather(W,D,_,_,T).

Your previous code "returns" true and false because of backtracking. There is nothing wrong with that.