Given the following facts and predicates:
sound(time1).
sound(time2).
sun(time3).
relax(X):-sound(X),!,sun(X).
relax(_):-sun(_).
When executing relax(S).
I'd expect to get S=time1
due to the !
, that says (correct me if I'm wrong), that if 'X' is satisfied , then stop the backtracking.
Here is the trace:
3 ?- trace.
true.
[trace] 3 ?- relax(S).
Call: (6) relax(_G1831) ? creep
Call: (7) sound(_G1831) ? creep
Exit: (7) sound(time1) ? creep
Call: (7) sun(time1) ? creep
Fail: (7) sun(time1) ? creep
Fail: (6) relax(_G1831) ? creep
false.
So why does Prolog also checks sun(time1)
, even though that it met the exclamation mark after being satisfied by sound(X)
(because sound(time1)
is a fact).
To clarify this even more, if somebody still struggles how exclamation operator works (like i did), here is an example:
sound(time3).
sound(time1).
sun(time1).
relax(X):-sound(X),!,sun(X).
For this certain example, if you ask Prolog for ?-relax(S).
this results into false. We can describe Prolog working like this:
In opposition like I said in 4. without ! operator it results into success.
sound(time3).
sound(time1).
sun(time1).
relax(X):-sound(X),sun(X).
Feel free to correct me if I am wrong at some point.