Prolog argument mode indicator causes output false

Viewed 65

I am trying to use Prolog's argument mode indicators in the signature of my method (https://www.swi-prolog.org/pldoc/man?section=argmode).

Without the indicators, my function works as expected (eg. palindrome([1,2,1]) gives true):

palindrome(List) :- 
     reverse(List, List) 

But when I say

palindrome(+List) :- 
    reverse(List, List) 

I get false every time. I don't get any errors or warnings. I also tried the following but had no luck:

palindrome(+List) :- 
    reverse(+List, +List) 

So I'm pretty sure I am using these indicators wrong somehow. Can anyone help? I am using SWI-Prolog and the SWISH online IDE.

1 Answers

Yes, wrong. You shouldn't be using them at all, in the code. Only in the comments.

+ is interpreted as a separate token:

6 ?- atom(+X).
false.

7 ?- +X =.. Z.
Z = [+, X].

8 ?- +X = + X.
true.

9 ?- +X = '+'(X).
true.

You could use the +-using predicate definition as you show, but it's rather pointless:

14 ?- [user].
bar(+X,+X).
|: 
true.

15 ?- bar( + 1, +Z).
Z = 1.

There are languages that do let us declare the modes, like I think Mercury does. But not Prolog. In Prolog we only use this as comments, to guide our use and understanding of the code.

Related