0

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?

false
  • 10,264
  • 13
  • 101
  • 209
Han Ben
  • 17
  • 4
  • 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 Mar 22 '18 at 13:15
  • 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 Mar 22 '18 at 13:16

1 Answers1

-1

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.