I’m trying to create a predicate which is true if the third list is composed of T and F values, depending on the fact whether two elements of same index from the first and the second list are equal. A query like truth_list(['abc','def'],['zui','def'],L). should give L=['F','T'].
Here’s my try:
truth_list([],[],_).
truth_list([H1|T1],[H2|T2],TL):-
(H1==H2)->(H3='T');(H3='F'),
Temp=TL,
TL=[H3|Temp],
truth_list(T1,T2,TL).
I’d be grateful if someone could offer an explanation why this doesn’t work as expected.
So we’re all on the same page, when I ran the query I got this result:
So the first thing that comes to mind is that you’re probably reusing a variable, and indeed, the following clauses look suspect:
Variables in Prolog aren’t really “assignables” as in other languages; all you can really do is establish a binding, so the above code is the same as saying this:
Which is why our result looks like it does: L=[‘F’|L]. I rewrote the body slightly to fix the problem and wound up with this code:
You need to have the parentheses around the whole condition or you’ll get strange behavior. From there, I just removed the unnecessary
TLbindings (they can’t work anyway because variables in Prolog are not assignables). It turns out once you fix these problems you’ll find another problem, which is that you’ll get this:If it’s not obvious, this is because your base case is too vague and should instead look like this:
So the final corrected version looks like this:
This is usually where @false shows up and points out that we have a problem using the predicate with different instantiations, so let’s check that now and avoid some fury:
These all look alright, so it doesn’t look like we’re hallucinating lies when all the arguments are instantiated. That’s good. Now let’s check partial instantiations:
Cool, that worked.
Eh. Well, it looks like Prolog doesn’t know to hallucinate some other binding for that
'F'value. Not sure if that’s a problem or not but I don’t see an obvious solution. That means the following probably won’t work:Surprising, but it actually did work, assigning to both the same list of two unknowns. Cool. I think we’re in good shape.
Edit: let’s incorporate @false’s improvement. Then we get the following:
Now we get the desired behavior:
So Prolog has inferred that Y is ‘def’, and concluded that X is at least not ‘abc’, so this is an improvement.