1

I'm writing a program in Prolog. I've re-ordered the goals to stop infinite recursion.

However, it's still having issues.

When querying...

Take for example the following predicate,

| ?- predicate([x,y,z],x).
yes
| ?- predicate(X,x).
X = [x] ?;
X = [x,y] ?;
no
| ?- predicate([x,y,Z],x).
no

Why might a predicate do this? I can't post my code, but some advice as to what causes this sort of thing would be great. If it knows the query holds when using ground terms, why would it not find it when using variables in it's place? It doesn't even get stuck recursing or anything, it just says there isn't any more solutions, when there is!

Wolff
  • 1,051
  • 3
  • 18
  • 31
  • In your second example, `no` in this case means it didn't find any more solutions after `X = [x,y]`. In your last example, it means no solutions found. – lurker Oct 16 '15 at 18:49
  • 1
    Is [this](http://stackoverflow.com/questions/33122122/why-would-prolog-match-a-variable-to-a-result-that-fails-if-plugged-in-directly) related? – vmg Oct 16 '15 at 18:52
  • @lurker I know what the no means. But my question is why might it be finding no more solutions. i.e. When ? - predicate([x,y,z],x). returns yes, why doesn't ? - predicate([x,y,Z],x) return z = Z ? or ?- predicate(X,x) return X = [x,y,z]? – Wolff Oct 16 '15 at 18:52
  • Then 'this' is related. – vmg Oct 16 '15 at 18:53
  • 1
    @vmg Replacing "\=" in one of my goals with "dif" seemed to help. I'm not quite sure I understand why, but I'll look into it. Thanks! – Wolff Oct 16 '15 at 19:00
  • 1
    `dif/2` is pure, and monotonic. `(\=)/2` is a hack which only only works for "sufficiently instantiated" goals. And it is quite complex and error-prone to determine what "sufficiently instantiated" actually means. – false Oct 16 '15 at 19:45

0 Answers0