Question

I have a series of facts in prolog that contain the anonymous variable _.

fact(a, _).
fact(b, _).
fact(c, _).

and i have some rules that take in lists of these facts:

f([H | T]) :- H == fact(a, _),
              % stuff %
              .

however this is not working. The rule is not going past the first clause H == fact(a, _). What am I doing wrong? Is there a different operator for equality?

Was it helpful?

Solution

Yes, you should probably use =/2:

1 ?- H == fact(a,_).
false.

2 ?- H = fact(a,_).
H = fact(a, _G0).

the difference is that ==/1 checks if the two terms are equal and non-instantiated variables (that have a different name) are treated as different. On the other hand, =/2 unifies the two terms, instantiating as required.

If you use an instantiation pattern in the head (f([fact(a,_)|T]):- stuff.) it has the same behaviour with =/2

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top